PluginProbe
Polylang / 3.8.8
Polylang v3.8.8
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 3.8.8, at src/functions.php

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