PluginProbe
Polylang / 3.5
Polylang v3.5
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.5, at include/functions.php

194 lines 6.1 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( esc_url_raw( wp_unslash( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) );
85 }
86
87 /*
88 * In WP CLI context, few developers define superglobals in wp-config.php
89 * as proposed in https://make.wordpress.org/cli/handbook/common-issues/#php-notice-undefined-index-on-_server-superglobal
90 * So let's return the unfiltered home url to avoid a bunch of notices.
91 */
92 if ( defined( 'WP_CLI' ) && WP_CLI ) {
93 return get_option( 'home' );
94 }
95
96 if ( WP_DEBUG ) {
97 // phpcs:ignore WordPress.PHP.DevelopmentFunctions
98 trigger_error( '$_SERVER[\'HTTP_HOST\'] or $_SERVER[\'REQUEST_URI\'] are required but not set.' );
99 }
100
101 return '';
102 }
103
104 /**
105 * Determines whether we should load the block editor plugin or the legacy languages metabox.
106 *
107 * @since 2.6.0
108 *
109 * @return bool True to use the block editor plugin.
110 */
111 function pll_use_block_editor_plugin() {
112 /**
113 * Filters whether we should load the block editor plugin or the legacy languages metabox.
114 *
115 * @since 2.6.0
116 *
117 * @param bool $use_plugin True when loading the block editor plugin.
118 */
119 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 );
120 }
121
122 /**
123 * Tells if a constant is defined.
124 *
125 * @since 3.5
126 *
127 * @param string $constant_name Name of the constant.
128 * @return bool True if the constant is defined, false otherwise.
129 *
130 * @phpstan-param non-falsy-string $constant_name
131 */
132 function pll_has_constant( string $constant_name ): bool {
133 return defined( $constant_name ); // phpcs:ignore WordPressVIPMinimum.Constants.ConstantString.NotCheckingConstantName
134 }
135
136 /**
137 * Returns the value of a constant if it is defined.
138 *
139 * @since 3.5
140 *
141 * @param string $constant_name Name of the constant.
142 * @param mixed $default Optional. Value to return if the constant is not defined. Defaults to `null`.
143 * @return mixed The value of the constant.
144 *
145 * @phpstan-param non-falsy-string $constant_name
146 * @phpstan-param int|float|string|bool|array|null $default
147 */
148 function pll_get_constant( string $constant_name, $default = null ) {
149 if ( ! pll_has_constant( $constant_name ) ) {
150 return $default;
151 }
152
153 return constant( $constant_name );
154 }
155
156 /**
157 * Defines a constant if it is not already defined.
158 *
159 * @since 3.5
160 *
161 * @param string $constant_name Name of the constant.
162 * @param mixed $value Value to set.
163 * @return bool True on success, false on failure or already defined.
164 *
165 * @phpstan-param non-falsy-string $constant_name
166 * @phpstan-param int|float|string|bool|array|null $value
167 */
168 function pll_set_constant( string $constant_name, $value ): bool {
169 if ( pll_has_constant( $constant_name ) ) {
170 return false;
171 }
172
173 return define( $constant_name, $value ); // phpcs:ignore WordPressVIPMinimum.Constants.ConstantString.NotCheckingConstantName
174 }
175
176 /**
177 * Determines whether a plugin is active.
178 *
179 * We define our own function because `is_plugin_active()` is available only in the backend.
180 *
181 * @since 3.5
182 *
183 * @param string $plugin_name Plugin basename.
184 * @return bool True if activated, false otherwise.
185 */
186 function pll_is_plugin_active( string $plugin_name ) {
187 $sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
188 $sitewide_plugins = ! empty( $sitewide_plugins ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array();
189 $current_site_plugins = (array) get_option( 'active_plugins', array() );
190 $plugins = array_merge( $sitewide_plugins, $current_site_plugins );
191
192 return in_array( $plugin_name, $plugins );
193 }
194