= home_url( $path, $scheme ); Sitepress::get()->restore_home_url_filter(); return $home_url; } /** * Get roles. * * @codeCoverageIgnore * * @param string $output How to return roles. * * @return array */ public static function get_roles( $output = 'names' ) { $wp_roles = wp_roles(); if ( 'names' !== $output ) { return $wp_roles->roles; } return $wp_roles->get_names(); } /** * Retrieves the sitename. * * @return string */ public static function get_site_name() { return wp_strip_all_tags( get_bloginfo( 'name' ), true ); } /** * Get action from request. * * @return bool|string */ public static function get_request_action() { if ( empty( $_REQUEST['action'] ) ) { return false; } if ( '-1' === $_REQUEST['action'] && ! empty( $_REQUEST['action2'] ) ) { $_REQUEST['action'] = sanitize_key( $_REQUEST['action2'] ); } return sanitize_key( $_REQUEST['action'] ); } /** * Instantiates the WordPress filesystem for use. * * @return object */ public static function get_filesystem() { global $wp_filesystem; if ( empty( $wp_filesystem ) ) { global $wp_file_descriptions; require_once ABSPATH . '/wp-admin/includes/file.php'; // @phpstan-ignore-line WP_Filesystem(); } return $wp_filesystem; } /** * Get current post type. * * This function has some fallback strategies to get the current screen post type. * * @return string|bool */ public static function get_post_type() { global $pagenow; if ( Helper::is_site_editor() ) { return 'page'; } $post_type = self::post_type_from_globals(); if ( false !== $post_type ) { return $post_type; } $post_type = self::post_type_from_request(); if ( false !== $post_type ) { return $post_type; } return 'post-new.php' === $pagenow ? 'post' : false; } /** * Get post type from global variables * * @return string|bool */ private static function post_type_from_globals() { global $post, $typenow, $current_screen; if ( $post && $post->post_type ) { return $post->post_type; } if ( $typenow ) { return $typenow; } if ( $current_screen && $current_screen->post_type ) { return $current_screen->post_type; } return false; } /** * Get post type from request variables * * @return string|bool */ private static function post_type_from_request() { if ( $post_type = Param::request( 'post_type' ) ) { // phpcs:ignore return sanitize_key( $post_type ); } if ( $post_id = Param::request( 'post_ID', 0, FILTER_VALIDATE_INT ) ) { // phpcs:ignore return get_post_type( $post_id ); } // @codeCoverageIgnoreStart if ( $post = Param::get( 'post' ) ) { // phpcs:ignore return get_post_type( $post ); } // @codeCoverageIgnoreEnd return false; } /** * Strip all shortcodes active or orphan. * * @param string $content Content to remove shortcodes from. * * @return string */ public static function strip_shortcodes( $content ) { if ( ! Str::contains( '[', $content ) ) { return $content; } // Remove Caption shortcode. $content = \preg_replace( '#\s*\[caption[^]]*\].*?\[/caption\]\s*#is', '', $content ); return preg_replace( '~\[\/?.*?\]~s', '', $content ); } /** * Get the current time as a Unix timestamp (seconds since epoch). * * @return int The current Unix timestamp. */ public static function get_current_time() { return strtotime( current_time( 'mysql' ) ); } /** * Handles the upload process to allow .txt and .json file types in WordPress. * * This function hooks into 'upload_mimes' and 'wp_check_filetype_and_ext' * to permit the upload of plain text (.txt) and JSON (.json) files via the media uploader. * It ensures the correct MIME types and file extensions are accepted. * * @return array|WP_Error Array of upload results, including file URL, path, and type, or error information. */ public static function handle_file_upload() { // Add upload hooks. add_filter( 'upload_mimes', [ __CLASS__, 'allow_txt_upload' ] ); add_filter( 'wp_check_filetype_and_ext', [ __CLASS__, 'filetype_and_ext' ], 10, 3 ); if ( isset( $_FILES['import-me'] ) ) { // Do the upload. if ( ! function_exists( 'wp_handle_upload' ) ) { $required_file = ABSPATH . 'wp-admin/includes/file.php'; if ( file_exists( $required_file ) ) { require_once $required_file; // @phpstan-ignore-line } } $file = wp_handle_upload( $_FILES['import-me'], [ 'test_form' => false ] ); } else { $file = new WP_Error( 'missing_file', __( 'No file selected for upload.', 'seo-by-rank-math' ) ); } // Remove upload hooks. remove_filter( 'upload_mimes', [ __CLASS__, 'allow_txt_upload' ] ); remove_filter( 'wp_check_filetype_and_ext', [ __CLASS__, 'filetype_and_ext' ], 10 ); return $file; } /** * Allow txt & json file upload. * * @param array $types Mime types keyed by the file extension regex corresponding to those types. * * @return array */ public static function allow_txt_upload( $types ) { $types['json'] = 'application/json'; $types['txt'] = 'text/plain'; return $types; } /** * Filters the "real" file type of the given file. * * @param array $types { * Values for the extension, mime type, and corrected filename. * * @type string|false $ext File extension, or false if the file doesn't match a mime type. * @type string|false $type File mime type, or false if the file doesn't match a mime type. * @type string|false $proper_filename File name with its correct extension, or false if it cannot be determined. * } * @param string $file Full path to the file. * @param string $filename The name of the file (may differ from $file due to * $file being in a tmp directory). * * @return array */ public static function filetype_and_ext( $types, $file, $filename ) { if ( false !== strpos( $filename, '.json' ) ) { $types['ext'] = 'json'; $types['type'] = 'application/json'; } elseif ( false !== strpos( $filename, '.txt' ) ) { $types['ext'] = 'txt'; $types['type'] = 'text/plain'; } return $types; } }