| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | 7 | * Define wordpress.com VIP equivalent of uncached functions |
| 5 | 8 | * WordPress backward compatibility functions |
| @@ -5,75 +8,58 @@ | ||
| 5 | 8 | * WordPress backward compatibility functions |
| 6 | 9 | * and miscellaneous utility functions |
| 7 | 10 | */ |
| 8 | 11 | |
| 9 | -if ( ! function_exists( 'wpcom_vip_get_page_by_title' ) ) { | |
| 12 | +if ( ! function_exists( 'wpcom_vip_get_page_by_path' ) ) { | |
| 10 | 13 | /** |
| 11 | - * Retrieve a page given its title. | |
| 14 | + * Retrieves a page given its path. | |
| 12 | 15 | * |
| 13 | - * @since 2.0 | |
| 16 | + * @since 2.8.3 | |
| 14 | 17 | * |
| 15 | - * @param string $page_title Page title | |
| 16 | - * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N. Default OBJECT | |
| 17 | - * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. | |
| 18 | + * @param string $page_path Page path. | |
| 19 | + * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to | |
| 20 | + * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. | |
| 21 | + * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. | |
| 18 | 22 | * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
| 19 | 23 | */ |
| 20 | - function wpcom_vip_get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { | |
| 21 | - return get_page_by_title( $page_title, $output, $post_type ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_page_by_title_get_page_by_title | |
| 24 | + function wpcom_vip_get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { | |
| 25 | + return get_page_by_path( $page_path, $output, $post_type ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_page_by_path_get_page_by_path | |
| 22 | 26 | } |
| 23 | 27 | } |
| 24 | 28 | |
| 25 | -if ( ! function_exists( 'wp_doing_ajax' ) ) { | |
| 29 | +if ( ! function_exists( 'sanitize_locale_name' ) ) { | |
| 26 | 30 | /** |
| 27 | - * Determines whether the current request is a WordPress Ajax request. | |
| 28 | - * Backward compatibility function for WP < 4.7 | |
| 31 | + * Strips out all characters not allowed in a locale code. | |
| 32 | + * Backward compatibility with WP < 6.2.1. | |
| 29 | 33 | * |
| 30 | - * @since 2.2 | |
| 34 | + * @since 3.5 | |
| 31 | 35 | * |
| 32 | - * @return bool True if it's a WordPress Ajax request, false otherwise. | |
| 36 | + * @param string $locale_name The locale name to be sanitized. | |
| 37 | + * @return string The sanitized value. | |
| 33 | 38 | */ |
| 34 | - function wp_doing_ajax() { | |
| 35 | - /** This filter is documented in wp-includes/load.php */ | |
| 36 | - return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ); | |
| 37 | - } | |
| 38 | -} | |
| 39 | + function sanitize_locale_name( $locale_name ) { | |
| 40 | + // Limit to A-Z, a-z, 0-9, '_', '-'. | |
| 41 | + $sanitized = (string) preg_replace( '/[^A-Za-z0-9_-]/', '', $locale_name ); | |
| 39 | 42 | |
| 40 | -if ( ! function_exists( 'wp_doing_cron' ) ) { | |
| 41 | - /** | |
| 42 | - * Determines whether the current request is a WordPress cron request. | |
| 43 | - * Backward compatibility function for WP < 4.8 | |
| 44 | - * | |
| 45 | - * @since 2.6 | |
| 46 | - * | |
| 47 | - * @return bool True if it's a WordPress cron request, false otherwise. | |
| 48 | - */ | |
| 49 | - function wp_doing_cron() { | |
| 50 | - /** This filter is documented in wp-includes/load.php */ | |
| 51 | - return apply_filters( 'wp_doing_cron', defined( 'DOING_CRON' ) && DOING_CRON ); | |
| 43 | + /** | |
| 44 | + * Filters a sanitized locale name string. | |
| 45 | + * Backward compatibility with WP < 6.2.1. | |
| 46 | + * | |
| 47 | + * @since 3.5 | |
| 48 | + * | |
| 49 | + * @param string $sanitized The sanitized locale name. | |
| 50 | + * @param string $locale_name The locale name before sanitization. | |
| 51 | + */ | |
| 52 | + return apply_filters( 'sanitize_locale_name', $sanitized, $locale_name ); | |
| 52 | 53 | } |
| 53 | 54 | } |
| 54 | 55 | |
| 55 | -if ( ! function_exists( 'wp_using_themes' ) ) { | |
| 56 | - /** | |
| 57 | - * Determines whether the current request should use themes. | |
| 58 | - * Backward compatibility function for WP < 5.1 | |
| 59 | - * | |
| 60 | - * @since 2.6 | |
| 61 | - * | |
| 62 | - * @return bool True if themes should be used, false otherwise. | |
| 63 | - */ | |
| 64 | - function wp_using_themes() { | |
| 65 | - /** This filter is documented in wp-includes/load.php */ | |
| 66 | - return apply_filters( 'wp_using_themes', defined( 'WP_USE_THEMES' ) && WP_USE_THEMES ); | |
| 67 | - } | |
| 68 | -} | |
| 69 | - | |
| 70 | 56 | /** |
| 71 | 57 | * Determines whether we should load the cache compatibility |
| 72 | 58 | * |
| 73 | 59 | * @since 2.3.8 |
| 74 | 60 | * |
| 75 | - * return bool True if the cache compatibility must be loaded | |
| 61 | + * @return bool True if the cache compatibility must be loaded | |
| 76 | 62 | */ |
| 77 | 63 | function pll_is_cache_active() { |
| 78 | 64 | /** |
| 79 | 65 | * Filters whether we should load the cache compatibility |
| @@ -94,11 +80,14 @@ | ||
| 94 | 80 | * @return string Requested url |
| 95 | 81 | */ |
| 96 | 82 | function pll_get_requested_url() { |
| 97 | 83 | if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) { |
| 98 | - return set_url_scheme( esc_url_raw( wp_unslash( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) ); | |
| 84 | + return set_url_scheme( sanitize_url( wp_unslash( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) ); | |
| 99 | 85 | } |
| 100 | 86 | |
| 87 | + /** @var string */ | |
| 88 | + $home_url = get_option( 'home' ); | |
| 89 | + | |
| 101 | 90 | /* |
| 102 | 91 | * In WP CLI context, few developers define superglobals in wp-config.php |
| 103 | 92 | * as proposed in https://make.wordpress.org/cli/handbook/common-issues/#php-notice-undefined-index-on-_server-superglobal |
| 104 | 93 | * So let's return the unfiltered home url to avoid a bunch of notices. |
| @@ -103,11 +92,18 @@ | ||
| 103 | 92 | * as proposed in https://make.wordpress.org/cli/handbook/common-issues/#php-notice-undefined-index-on-_server-superglobal |
| 104 | 93 | * So let's return the unfiltered home url to avoid a bunch of notices. |
| 105 | 94 | */ |
| 106 | 95 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 107 | - return get_option( 'home' ); | |
| 96 | + return $home_url; | |
| 108 | 97 | } |
| 109 | 98 | |
| 99 | + /* | |
| 100 | + * When using system CRON instead of WP_CRON, the superglobals are likely undefined. | |
| 101 | + */ | |
| 102 | + if ( defined( 'DOING_CRON' ) && DOING_CRON ) { | |
| 103 | + return $home_url; | |
| 104 | + } | |
| 105 | + | |
| 110 | 106 | if ( WP_DEBUG ) { |
| 111 | 107 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions |
| 112 | 108 | trigger_error( '$_SERVER[\'HTTP_HOST\'] or $_SERVER[\'REQUEST_URI\'] are required but not set.' ); |
| 113 | 109 | } |
| @@ -119,9 +115,9 @@ | ||
| 119 | 115 | * Determines whether we should load the block editor plugin or the legacy languages metabox. |
| 120 | 116 | * |
| 121 | 117 | * @since 2.6.0 |
| 122 | 118 | * |
| 123 | - * return bool True to use the block editor plugin. | |
| 119 | + * @return bool True to use the block editor plugin. | |
| 124 | 120 | */ |
| 125 | 121 | function pll_use_block_editor_plugin() { |
| 126 | 122 | /** |
| 127 | 123 | * Filters whether we should load the block editor plugin or the legacy languages metabox. |
| @@ -129,6 +125,113 @@ | ||
| 129 | 125 | * @since 2.6.0 |
| 130 | 126 | * |
| 131 | 127 | * @param bool $use_plugin True when loading the block editor plugin. |
| 132 | 128 | */ |
| 133 | - return class_exists( 'PLL_Block_Editor_Plugin' ) && apply_filters( 'pll_use_block_editor_plugin', ! defined( 'PLL_USE_BLOCK_EDITOR_PLUGIN' ) || PLL_USE_BLOCK_EDITOR_PLUGIN ); | |
| 129 | + return class_exists( 'WP_Syntex\Polylang_Pro\Editors\Screens\Abstract_Screen' ) && apply_filters( 'pll_use_block_editor_plugin', ! defined( 'PLL_USE_BLOCK_EDITOR_PLUGIN' ) || PLL_USE_BLOCK_EDITOR_PLUGIN ); | |
| 130 | +} | |
| 131 | + | |
| 132 | +/** | |
| 133 | + * Tells if a constant is defined. | |
| 134 | + * | |
| 135 | + * @since 3.5 | |
| 136 | + * | |
| 137 | + * @param string $constant_name Name of the constant. | |
| 138 | + * @return bool True if the constant is defined, false otherwise. | |
| 139 | + * | |
| 140 | + * @phpstan-param non-falsy-string $constant_name | |
| 141 | + */ | |
| 142 | +function pll_has_constant( string $constant_name ): bool { | |
| 143 | + return defined( $constant_name ); // phpcs:ignore WordPressVIPMinimum.Constants.ConstantString.NotCheckingConstantName | |
| 144 | +} | |
| 145 | + | |
| 146 | +/** | |
| 147 | + * Returns the value of a constant if it is defined. | |
| 148 | + * | |
| 149 | + * @since 3.5 | |
| 150 | + * | |
| 151 | + * @param string $constant_name Name of the constant. | |
| 152 | + * @param mixed $default Optional. Value to return if the constant is not defined. Defaults to `null`. | |
| 153 | + * @return mixed The value of the constant. | |
| 154 | + * | |
| 155 | + * @phpstan-param non-falsy-string $constant_name | |
| 156 | + * @phpstan-param int|float|string|bool|array|null $default | |
| 157 | + */ | |
| 158 | +function pll_get_constant( string $constant_name, $default = null ) { | |
| 159 | + if ( ! pll_has_constant( $constant_name ) ) { | |
| 160 | + return $default; | |
| 161 | + } | |
| 162 | + | |
| 163 | + return constant( $constant_name ); | |
| 164 | +} | |
| 165 | + | |
| 166 | +/** | |
| 167 | + * Defines a constant if it is not already defined. | |
| 168 | + * | |
| 169 | + * @since 3.5 | |
| 170 | + * | |
| 171 | + * @param string $constant_name Name of the constant. | |
| 172 | + * @param mixed $value Value to set. | |
| 173 | + * @return bool True on success, false on failure or already defined. | |
| 174 | + * | |
| 175 | + * @phpstan-param non-falsy-string $constant_name | |
| 176 | + * @phpstan-param int|float|string|bool|array|null $value | |
| 177 | + */ | |
| 178 | +function pll_set_constant( string $constant_name, $value ): bool { | |
| 179 | + if ( pll_has_constant( $constant_name ) ) { | |
| 180 | + return false; | |
| 181 | + } | |
| 182 | + | |
| 183 | + return define( $constant_name, $value ); // phpcs:ignore WordPressVIPMinimum.Constants.ConstantString.NotCheckingConstantName | |
| 184 | +} | |
| 185 | + | |
| 186 | +/** | |
| 187 | + * Determines whether a plugin is active. | |
| 188 | + * | |
| 189 | + * We define our own function because `is_plugin_active()` is available only in the backend. | |
| 190 | + * | |
| 191 | + * @since 3.5 | |
| 192 | + * | |
| 193 | + * @param string $plugin_name Plugin basename. | |
| 194 | + * @return bool True if activated, false otherwise. | |
| 195 | + */ | |
| 196 | +function pll_is_plugin_active( string $plugin_name ) { | |
| 197 | + $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ); | |
| 198 | + $sitewide_plugins = ! empty( $sitewide_plugins ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array(); | |
| 199 | + $current_site_plugins = (array) get_option( 'active_plugins', array() ); | |
| 200 | + $plugins = array_merge( $sitewide_plugins, $current_site_plugins ); | |
| 201 | + | |
| 202 | + return in_array( $plugin_name, $plugins ); | |
| 203 | +} | |
| 204 | + | |
| 205 | +/** | |
| 206 | + * Prepares and registers notices. | |
| 207 | + * | |
| 208 | + * Wraps `add_settings_error()` to make its use more consistent. | |
| 209 | + * | |
| 210 | + * @since 3.6 | |
| 211 | + * | |
| 212 | + * @param WP_Error $error Error object. | |
| 213 | + * @return void | |
| 214 | + */ | |
| 215 | +function pll_add_notice( WP_Error $error ) { | |
| 216 | + if ( ! $error->has_errors() ) { | |
| 217 | + return; | |
| 218 | + } | |
| 219 | + | |
| 220 | + foreach ( $error->get_error_codes() as $error_code ) { | |
| 221 | + // Extract the "error" type. | |
| 222 | + $data = $error->get_error_data( $error_code ); | |
| 223 | + $type = empty( $data ) || ! is_string( $data ) ? 'error' : $data; | |
| 224 | + | |
| 225 | + $message = wp_kses( | |
| 226 | + implode( '<br>', $error->get_error_messages( $error_code ) ), | |
| 227 | + array( | |
| 228 | + 'a' => array( 'href' => true ), | |
| 229 | + 'br' => array(), | |
| 230 | + 'code' => array(), | |
| 231 | + 'em' => array(), | |
| 232 | + ) | |
| 233 | + ); | |
| 234 | + | |
| 235 | + add_settings_error( 'polylang', $error_code, $message, $type ); | |
| 236 | + } | |
| 134 | 237 | } |