| @@ -1,32 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package Polylang |
| 4 | - */ | |
| 5 | - | |
| 6 | -/** | |
| 7 | - * Define wordpress.com VIP equivalent of uncached functions | |
| 4 | + * | |
| 8 | 5 | * WordPress backward compatibility functions |
| 9 | - * and miscellaneous utility functions | |
| 6 | + * and miscellaneous utility functions. | |
| 10 | 7 | */ |
| 11 | 8 | |
| 12 | -if ( ! function_exists( 'wpcom_vip_get_page_by_path' ) ) { | |
| 13 | - /** | |
| 14 | - * Retrieves a page given its path. | |
| 15 | - * | |
| 16 | - * @since 2.8.3 | |
| 17 | - * | |
| 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'. | |
| 22 | - * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. | |
| 23 | - */ | |
| 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 | |
| 26 | - } | |
| 27 | -} | |
| 28 | - | |
| 29 | 9 | /** |
| 30 | 10 | * Determines whether we should load the cache compatibility |
| 31 | 11 | * |
| 32 | 12 | * @since 2.3.8 |
| @@ -45,9 +25,9 @@ | ||
| 45 | 25 | return apply_filters( 'pll_is_cache_active', ( defined( 'WP_CACHE' ) && WP_CACHE ) || defined( 'WPFC_MAIN_PATH' ) ); |
| 46 | 26 | } |
| 47 | 27 | |
| 48 | 28 | /** |
| 49 | - * Get the the current requested url | |
| 29 | + * Get the current requested url. | |
| 50 | 30 | * |
| 51 | 31 | * @since 2.6 |
| 52 | 32 | * |
| 53 | 33 | * @return string Requested url |
| @@ -150,9 +130,9 @@ | ||
| 150 | 130 | 'em' => array(), |
| 151 | 131 | ) |
| 152 | 132 | ); |
| 153 | 133 | |
| 154 | - add_settings_error( 'polylang', $error_code, $message, $type ); | |
| 134 | + add_settings_error( 'polylang', (string) $error_code, $message, $type ); | |
| 155 | 135 | } |
| 156 | 136 | } |
| 157 | 137 | |
| 158 | 138 | /** |
| @@ -170,5 +150,135 @@ | ||
| 170 | 150 | return true; |
| 171 | 151 | } |
| 172 | 152 | |
| 173 | 153 | return 'GET' === $request->get_method() && 'edit' === $request->get_param( 'context' ); |
| 154 | +} | |
| 155 | + | |
| 156 | +/** | |
| 157 | + * Sanitizes an ID as positive integer. | |
| 158 | + * Kind of similar to `absint()`, but rejects negative integers instead of making them positive. | |
| 159 | + * | |
| 160 | + * @since 3.9 | |
| 161 | + * | |
| 162 | + * @param mixed $id A supposedly numeric ID. | |
| 163 | + * @return int A positive integer. `0` for non numeric values and negative integers. | |
| 164 | + * | |
| 165 | + * @phpstan-return int<0, max> | |
| 166 | + */ | |
| 167 | +function pll_sanitize_id( $id ): int { | |
| 168 | + return is_numeric( $id ) && $id >= 1 ? abs( (int) $id ) : 0; | |
| 169 | +} | |
| 170 | + | |
| 171 | +/** | |
| 172 | + * Sanitizes an array of IDs as positive integers. | |
| 173 | + * `0` values are removed. | |
| 174 | + * | |
| 175 | + * @since 3.9 | |
| 176 | + * | |
| 177 | + * @param mixed $ids A supposedly array of numeric IDs. | |
| 178 | + * @return int[] | |
| 179 | + * | |
| 180 | + * @phpstan-return array<positive-int> | |
| 181 | + */ | |
| 182 | +function pll_sanitize_ids( $ids ): array { | |
| 183 | + if ( empty( $ids ) || ! is_array( $ids ) ) { | |
| 184 | + return array(); | |
| 185 | + } | |
| 186 | + | |
| 187 | + return array_filter( array_map( 'pll_sanitize_id', $ids ) ); | |
| 188 | +} | |
| 189 | + | |
| 190 | +/** | |
| 191 | + * Enqueues a JS script. | |
| 192 | + * | |
| 193 | + * @since 3.9 | |
| 194 | + * | |
| 195 | + * @param string $name The filename of the script (without the extension). | |
| 196 | + * @param string[] $dependencies Optional. An array of registered script handles this script depends on. | |
| 197 | + * @param array $args { | |
| 198 | + * Optional. An array of additional script loading strategies. Default empty array. | |
| 199 | + * | |
| 200 | + * @type string $strategy Optional. If provided, may be either 'defer' or 'async'. | |
| 201 | + * @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. | |
| 202 | + * @type string $fetchpriority Optional. The fetch priority for the script. Default 'auto'. | |
| 203 | + * } | |
| 204 | + * @param string $prefix Optional. Prefix to use for the script handle. Default 'pll-'. | |
| 205 | + * @return void | |
| 206 | + * | |
| 207 | + * @phpstan-param non-empty-string $name | |
| 208 | + */ | |
| 209 | +function pll_enqueue_script( string $name, array $dependencies = array(), array $args = array(), string $prefix = 'pll-' ): void { | |
| 210 | + pll_register_script( $name, $dependencies, $args ); | |
| 211 | + wp_scripts()->enqueue( $prefix . $name ); | |
| 212 | +} | |
| 213 | + | |
| 214 | +/** | |
| 215 | + * Registers a JS script. | |
| 216 | + * | |
| 217 | + * @since 3.9 | |
| 218 | + * | |
| 219 | + * @param string $name The filename of the script (without the extension). | |
| 220 | + * @param string[] $dependencies Optional. An array of registered script handles this script depends on. | |
| 221 | + * @param array $args { | |
| 222 | + * Optional. An array of additional script loading strategies. Default empty array. | |
| 223 | + * | |
| 224 | + * @type string $strategy Optional. If provided, may be either 'defer' or 'async'. | |
| 225 | + * @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. | |
| 226 | + * @type string $fetchpriority Optional. The fetch priority for the script. Default 'auto'. | |
| 227 | + * } | |
| 228 | + * @param string $prefix Optional. Prefix to use for the script handle. Default 'pll-'. | |
| 229 | + * @return void | |
| 230 | + * | |
| 231 | + * @phpstan-param non-empty-string $name | |
| 232 | + */ | |
| 233 | +function pll_register_script( string $name, array $dependencies = array(), array $args = array(), string $prefix = 'pll-' ): void { | |
| 234 | + $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; | |
| 235 | + $src = plugins_url( "/js/build/{$name}{$suffix}.js", POLYLANG_ROOT_FILE ); | |
| 236 | + | |
| 237 | + if ( '' === $src ) { | |
| 238 | + return; | |
| 239 | + } | |
| 240 | + | |
| 241 | + $version = WP_DEBUG ? time() : POLYLANG_VERSION; | |
| 242 | + wp_register_script( $prefix . $name, $src, array_filter( $dependencies ), (string) $version, $args ); | |
| 243 | +} | |
| 244 | + | |
| 245 | +/** | |
| 246 | + * Enqueues a CSS stylesheet. | |
| 247 | + * | |
| 248 | + * @since 3.9 | |
| 249 | + * | |
| 250 | + * @param string $name The filename of the stylesheet (without the extension). | |
| 251 | + * @param string[] $dependencies Optional. An array of registered stylesheet handles this stylesheet depends on. | |
| 252 | + * @param string $prefix Optional. Prefix to use for the stylesheet handle. Default 'pll-'. | |
| 253 | + * @return void | |
| 254 | + * | |
| 255 | + * @phpstan-param non-empty-string $name | |
| 256 | + */ | |
| 257 | +function pll_enqueue_style( string $name, array $dependencies = array(), string $prefix = 'pll-' ): void { | |
| 258 | + pll_register_style( $name, $dependencies ); | |
| 259 | + wp_styles()->enqueue( $prefix . $name ); | |
| 260 | +} | |
| 261 | + | |
| 262 | +/** | |
| 263 | + * Registers a CSS stylesheet. | |
| 264 | + * | |
| 265 | + * @since 3.9 | |
| 266 | + * | |
| 267 | + * @param string $name The filename of the stylesheet (without the extension). | |
| 268 | + * @param string[] $dependencies Optional. An array of registered stylesheet handles this stylesheet depends on. | |
| 269 | + * @param string $prefix Optional. Prefix to use for the stylesheet handle. Default 'pll-'. | |
| 270 | + * @return void | |
| 271 | + * | |
| 272 | + * @phpstan-param non-empty-string $name | |
| 273 | + */ | |
| 274 | +function pll_register_style( string $name, array $dependencies = array(), string $prefix = 'pll-' ): void { | |
| 275 | + $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; | |
| 276 | + $src = plugins_url( "/css/build/{$name}{$suffix}.css", POLYLANG_ROOT_FILE ); | |
| 277 | + | |
| 278 | + if ( '' === $src ) { | |
| 279 | + return; | |
| 280 | + } | |
| 281 | + | |
| 282 | + $version = WP_DEBUG ? time() : POLYLANG_VERSION; | |
| 283 | + wp_register_style( $prefix . $name, $src, $dependencies, (string) $version ); | |
| 174 | 284 | } |