PluginProbe
Polylang / 3.7.2
Polylang v3.7.2
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 / include / functions.php

functions.php in Polylang 3.7.2, at include/functions.php

238 lines 7.0 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
6 /**
7 * Define wordpress.com VIP equivalent of uncached functions
8 * WordPress backward compatibility functions
9 * and miscellaneous utility functions
10 */
11
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 if ( ! function_exists( 'sanitize_locale_name' ) ) {
30 /**
31 * Strips out all characters not allowed in a locale code.
32 * Backward compatibility with WP < 6.2.1.
33 *
34 * @since 3.5
35 *
36 * @param string $locale_name The locale name to be sanitized.
37 * @return string The sanitized value.
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 );
42
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 );
53 }
54 }
55
56 /**
57 * Determines whether we should load the cache compatibility
58 *
59 * @since 2.3.8
60 *
61 * @return bool True if the cache compatibility must be loaded
62 */
63 function pll_is_cache_active() {
64 /**
65 * Filters whether we should load the cache compatibility
66 *
67 * @since 2.3.8
68 *
69 * @bool $is_cache True if a known cache plugin is active
70 * incl. WP Fastest Cache which doesn't use WP_CACHE
71 */
72 return apply_filters( 'pll_is_cache_active', ( defined( 'WP_CACHE' ) && WP_CACHE ) || defined( 'WPFC_MAIN_PATH' ) );
73 }
74
75 /**
76 * Get the the current requested url
77 *
78 * @since 2.6
79 *
80 * @return string Requested url
81 */
82 function pll_get_requested_url() {
83 if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) {
84 return set_url_scheme( sanitize_url( wp_unslash( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) );
85 }
86
87 /** @var string */
88 $home_url = get_option( 'home' );
89
90 /*
91 * In WP CLI context, few developers define superglobals in wp-config.php
92 * as proposed in https://make.wordpress.org/cli/handbook/common-issues/#php-notice-undefined-index-on-_server-superglobal
93 * So let's return the unfiltered home url to avoid a bunch of notices.
94 */
95 if ( defined( 'WP_CLI' ) && WP_CLI ) {
96 return $home_url;
97 }
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
106 if ( WP_DEBUG ) {
107 // phpcs:ignore WordPress.PHP.DevelopmentFunctions
108 trigger_error( '$_SERVER[\'HTTP_HOST\'] or $_SERVER[\'REQUEST_URI\'] are required but not set.' );
109 }
110
111 return '';
112 }
113
114 /**
115 * Determines whether we should load the block editor plugin or the legacy languages metabox.
116 *
117 * @since 2.6.0
118 *
119 * @return bool True to use the block editor plugin.
120 */
121 function pll_use_block_editor_plugin() {
122 /**
123 * Filters whether we should load the block editor plugin or the legacy languages metabox.
124 *
125 * @since 2.6.0
126 *
127 * @param bool $use_plugin True when loading the block editor plugin.
128 */
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 }
237 }
238