PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / functions.php

functions.php in Polylang trunk, at src/functions.php

285 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 *
5 * WordPress backward compatibility functions
6 * and miscellaneous utility functions.
7 */
8
9 /**
10 * Determines whether we should load the cache compatibility
11 *
12 * @since 2.3.8
13 *
14 * @return bool True if the cache compatibility must be loaded
15 */
16 function pll_is_cache_active() {
17 /**
18 * Filters whether we should load the cache compatibility
19 *
20 * @since 2.3.8
21 *
22 * @bool $is_cache True if a known cache plugin is active
23 * incl. WP Fastest Cache which doesn't use WP_CACHE
24 */
25 return apply_filters( 'pll_is_cache_active', ( defined( 'WP_CACHE' ) && WP_CACHE ) || defined( 'WPFC_MAIN_PATH' ) );
26 }
27
28 /**
29 * Get the current requested url.
30 *
31 * @since 2.6
32 *
33 * @return string Requested url
34 */
35 function pll_get_requested_url() {
36 if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) {
37 return set_url_scheme( sanitize_url( wp_unslash( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) );
38 }
39
40 /** @var string */
41 $home_url = get_option( 'home' );
42
43 /*
44 * In WP CLI context, few developers define superglobals in wp-config.php
45 * as proposed in https://make.wordpress.org/cli/handbook/common-issues/#php-notice-undefined-index-on-_server-superglobal
46 * So let's return the unfiltered home url to avoid a bunch of notices.
47 */
48 if ( defined( 'WP_CLI' ) && WP_CLI ) {
49 return $home_url;
50 }
51
52 /*
53 * When using system CRON instead of WP_CRON, the superglobals are likely undefined.
54 */
55 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
56 return $home_url;
57 }
58
59 if ( WP_DEBUG ) {
60 // phpcs:ignore WordPress.PHP.DevelopmentFunctions
61 trigger_error( '$_SERVER[\'HTTP_HOST\'] or $_SERVER[\'REQUEST_URI\'] are required but not set.' );
62 }
63
64 return '';
65 }
66
67 /**
68 * Determines whether we should load the block editor plugin or the legacy languages metabox.
69 *
70 * @since 2.6.0
71 *
72 * @return bool True to use the block editor plugin.
73 */
74 function pll_use_block_editor_plugin() {
75 /**
76 * Filters whether we should load the block editor plugin or the legacy languages metabox.
77 *
78 * @since 2.6.0
79 *
80 * @param bool $use_plugin True when loading the block editor plugin.
81 */
82 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 );
83 }
84
85 /**
86 * Determines whether a plugin is active.
87 *
88 * We define our own function because `is_plugin_active()` is available only in the backend.
89 *
90 * @since 3.5
91 *
92 * @param string $plugin_name Plugin basename.
93 * @return bool True if activated, false otherwise.
94 */
95 function pll_is_plugin_active( string $plugin_name ) {
96 $sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
97 $sitewide_plugins = ! empty( $sitewide_plugins ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array();
98 $current_site_plugins = (array) get_option( 'active_plugins', array() );
99 $plugins = array_merge( $sitewide_plugins, $current_site_plugins );
100
101 return in_array( $plugin_name, $plugins );
102 }
103
104 /**
105 * Prepares and registers notices.
106 *
107 * Wraps `add_settings_error()` to make its use more consistent.
108 *
109 * @since 3.6
110 *
111 * @param WP_Error $error Error object.
112 * @return void
113 */
114 function pll_add_notice( WP_Error $error ) {
115 if ( ! $error->has_errors() ) {
116 return;
117 }
118
119 foreach ( $error->get_error_codes() as $error_code ) {
120 // Extract the "error" type.
121 $data = $error->get_error_data( $error_code );
122 $type = empty( $data ) || ! is_string( $data ) ? 'error' : $data;
123
124 $message = wp_kses(
125 implode( '<br>', $error->get_error_messages( $error_code ) ),
126 array(
127 'a' => array( 'href' => true ),
128 'br' => array(),
129 'code' => array(),
130 'em' => array(),
131 )
132 );
133
134 add_settings_error( 'polylang', (string) $error_code, $message, $type );
135 }
136 }
137
138 /**
139 * Tells if the given REST request is in the edit context.
140 *
141 * @since 3.5
142 *
143 * @param WP_REST_Request $request A REST request.
144 * @return bool
145 *
146 * @phpstan-param WP_REST_Request<array> $request
147 */
148 function pll_is_edit_rest_request( WP_REST_Request $request ): bool {
149 if ( in_array( $request->get_method(), array( 'PATCH', 'POST', 'PUT' ), true ) ) {
150 return true;
151 }
152
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 );
284 }
285