PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.9.2
Jetpack – WP Security, Backup, Speed, & Growth v11.9.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / functions.global.php

functions.global.php in Jetpack – WP Security, Backup, Speed, & Growth 11.9.2, at functions.global.php

466 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file is meant to be the home for any generic & reusable functions
4 * that can be accessed anywhere within Jetpack.
5 *
6 * This file is loaded whether Jetpack is active.
7 *
8 * Please namespace with jetpack_
9 *
10 * @package automattic/jetpack
11 */
12
13 use Automattic\Jetpack\Connection\Client;
14 use Automattic\Jetpack\Redirect;
15 use Automattic\Jetpack\Status\Host;
16 use Automattic\Jetpack\Sync\Functions;
17
18 /**
19 * Disable direct access.
20 */
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit;
23 }
24
25 require_once __DIR__ . '/functions.is-mobile.php';
26
27 /**
28 * Hook into Core's _deprecated_function
29 * Add more details about when a deprecated function will be removed.
30 *
31 * @since 8.8.0
32 *
33 * @param string $function The function that was called.
34 * @param string $replacement Optional. The function that should have been called. Default null.
35 * @param string $version The version of Jetpack that deprecated the function.
36 */
37 function jetpack_deprecated_function( $function, $replacement, $version ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
38 // Bail early for non-Jetpack deprecations.
39 if ( 0 !== strpos( $version, 'jetpack-' ) ) {
40 return;
41 }
42
43 // Look for when a function will be removed based on when it was deprecated.
44 $removed_version = jetpack_get_future_removed_version( $version );
45
46 // If we could find a version, let's log a message about when removal will happen.
47 if (
48 ! empty( $removed_version )
49 && ( defined( 'WP_DEBUG' ) && WP_DEBUG )
50 /** This filter is documented in core/src/wp-includes/functions.php */
51 && apply_filters( 'deprecated_function_trigger_error', true )
52 ) {
53 error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
54 sprintf(
55 /* Translators: 1. Function name. 2. Jetpack version number. */
56 __( 'The %1$s function will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ),
57 $function,
58 $removed_version
59 )
60 );
61
62 }
63 }
64 add_action( 'deprecated_function_run', 'jetpack_deprecated_function', 10, 3 );
65
66 /**
67 * Hook into Core's _deprecated_file
68 * Add more details about when a deprecated file will be removed.
69 *
70 * @since 8.8.0
71 *
72 * @param string $file The file that was called.
73 * @param string $replacement The file that should have been included based on ABSPATH.
74 * @param string $version The version of WordPress that deprecated the file.
75 * @param string $message A message regarding the change.
76 */
77 function jetpack_deprecated_file( $file, $replacement, $version, $message ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
78 // Bail early for non-Jetpack deprecations.
79 if ( 0 !== strpos( $version, 'jetpack-' ) ) {
80 return;
81 }
82
83 // Look for when a file will be removed based on when it was deprecated.
84 $removed_version = jetpack_get_future_removed_version( $version );
85
86 // If we could find a version, let's log a message about when removal will happen.
87 if (
88 ! empty( $removed_version )
89 && ( defined( 'WP_DEBUG' ) && WP_DEBUG )
90 /** This filter is documented in core/src/wp-includes/functions.php */
91 && apply_filters( 'deprecated_file_trigger_error', true )
92 ) {
93 error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
94 sprintf(
95 /* Translators: 1. File name. 2. Jetpack version number. */
96 __( 'The %1$s file will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ),
97 $file,
98 $removed_version
99 )
100 );
101
102 }
103 }
104 add_action( 'deprecated_file_included', 'jetpack_deprecated_file', 10, 4 );
105
106 /**
107 * Get the major version number of Jetpack 6 months after provided version.
108 * Useful to indicate when a deprecated function will be removed from Jetpack.
109 *
110 * @since 8.8.0
111 *
112 * @param string $version The version of WordPress that deprecated the function.
113 *
114 * @return bool|float Return a Jetpack Major version number, or false.
115 */
116 function jetpack_get_future_removed_version( $version ) {
117 /*
118 * Extract the version number from a deprecation notice.
119 * (let's only keep the first decimal, e.g. 8.8 and not 8.8.0)
120 */
121 preg_match( '#(([0-9]+\.([0-9]+))(?:\.[0-9]+)*)#', $version, $matches );
122
123 if ( isset( $matches[2], $matches[3] ) ) {
124 $deprecated_version = (float) $matches[2];
125 $deprecated_minor = (float) $matches[3];
126
127 /*
128 * If the detected minor version number
129 * (e.g. "7" in "8.7")
130 * is higher than 9, we know the version number is malformed.
131 * Jetpack does not use semver yet.
132 * Bail.
133 */
134 if ( 10 <= $deprecated_minor ) {
135 return false;
136 }
137
138 // We'll remove the function from the code 6 months later, thus 6 major versions later.
139 $removed_version = $deprecated_version + 0.6;
140
141 return (float) $removed_version;
142 }
143
144 return false;
145 }
146
147 /**
148 * Determine if this site is an WoA site or not by looking for presence of the wpcomsh plugin.
149 *
150 * @since 4.8.1
151 * @deprecated 10.3.0
152 *
153 * @return bool
154 */
155 function jetpack_is_atomic_site() {
156 jetpack_deprecated_function( __FUNCTION__, 'Automattic/Jetpack/Status/Host::is_woa_site', 'jetpack-10.3.0' );
157 return ( new Host() )->is_woa_site();
158 }
159
160 /**
161 * Register post type for migration.
162 *
163 * @since 5.2
164 */
165 function jetpack_register_migration_post_type() {
166 register_post_type(
167 'jetpack_migration',
168 array(
169 'supports' => array(),
170 'taxonomies' => array(),
171 'hierarchical' => false,
172 'public' => false,
173 'has_archive' => false,
174 'can_export' => true,
175 )
176 );
177 }
178
179 /**
180 * Stores migration data in the database.
181 *
182 * @since 5.2
183 *
184 * @param string $option_name Option name.
185 * @param bool $option_value Option value.
186 *
187 * @return int|WP_Error
188 */
189 function jetpack_store_migration_data( $option_name, $option_value ) {
190 jetpack_register_migration_post_type();
191
192 $insert = array(
193 'post_title' => $option_name,
194 'post_content_filtered' => $option_value,
195 'post_type' => 'jetpack_migration',
196 'post_date' => gmdate( 'Y-m-d H:i:s', time() ),
197 );
198
199 $post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
200
201 if ( null !== $post ) {
202 $insert['ID'] = $post->ID;
203 }
204
205 return wp_insert_post( $insert, true );
206 }
207
208 /**
209 * Retrieves legacy image widget data.
210 *
211 * @since 5.2
212 *
213 * @param string $option_name Option name.
214 *
215 * @return mixed|null
216 */
217 function jetpack_get_migration_data( $option_name ) {
218 $post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
219
220 return null !== $post ? maybe_unserialize( $post->post_content_filtered ) : null;
221 }
222
223 /**
224 * Prints a TOS blurb used throughout the connection prompts.
225 *
226 * Note: custom ToS messages are also defined in Jetpack_Pre_Connection_JITMs->get_raw_messages()
227 *
228 * @since 5.3
229 *
230 * @echo string
231 */
232 function jetpack_render_tos_blurb() {
233 printf(
234 wp_kses(
235 /* Translators: placeholders are links. */
236 __( 'By clicking the <strong>Set up Jetpack</strong> button, you agree to our <a href="%1$s" target="_blank" rel="noopener noreferrer">Terms of Service</a> and to <a href="%2$s" target="_blank" rel="noopener noreferrer">share details</a> with WordPress.com.', 'jetpack' ),
237 array(
238 'a' => array(
239 'href' => array(),
240 'target' => array(),
241 'rel' => array(),
242 ),
243 'strong' => true,
244 )
245 ),
246 esc_url( Redirect::get_url( 'wpcom-tos' ) ),
247 esc_url( Redirect::get_url( 'jetpack-support-what-data-does-jetpack-sync' ) )
248 );
249 }
250
251 /**
252 * Intervene upgrade process so Jetpack themes are downloaded with credentials.
253 *
254 * @since 5.3
255 *
256 * @param bool $preempt Whether to preempt an HTTP request's return value. Default false.
257 * @param array $r HTTP request arguments.
258 * @param string $url The request URL.
259 *
260 * @return array|bool|WP_Error
261 */
262 function jetpack_theme_update( $preempt, $r, $url ) {
263 if ( 0 === stripos( $url, JETPACK__WPCOM_JSON_API_BASE . '/rest/v1/themes/download' ) ) {
264 $file = $r['filename'];
265 if ( ! $file ) {
266 return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) );
267 }
268 $theme = pathinfo( wp_parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME );
269
270 // Remove filter to avoid endless loop since wpcom_json_api_request_as_blog uses this too.
271 remove_filter( 'pre_http_request', 'jetpack_theme_update' );
272 $result = Client::wpcom_json_api_request_as_blog(
273 "themes/download/$theme.zip",
274 '1.1',
275 array(
276 'stream' => true,
277 'filename' => $file,
278 )
279 );
280
281 if ( 200 !== wp_remote_retrieve_response_code( $result ) ) {
282 return new WP_Error( 'problem_fetching_theme', esc_html__( 'Problem downloading theme', 'jetpack' ) );
283 }
284 return $result;
285 }
286 return $preempt;
287 }
288
289 /**
290 * Add the filter when a upgrade is going to be downloaded.
291 *
292 * @since 5.3
293 *
294 * @param bool $reply Whether to bail without returning the package. Default false.
295 *
296 * @return bool
297 */
298 function jetpack_upgrader_pre_download( $reply ) {
299 add_filter( 'pre_http_request', 'jetpack_theme_update', 10, 3 );
300 return $reply;
301 }
302
303 add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' );
304
305 /**
306 * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion.
307 *
308 * @since 6.1.0
309
310 * @deprecated Automattic\Jetpack\Sync\Functions::json_wrap
311 *
312 * @param array|obj $any Source data to be cleaned up.
313 * @param array $seen_nodes Built array of nodes.
314 *
315 * @return array
316 */
317 function jetpack_json_wrap( &$any, $seen_nodes = array() ) {
318 _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\Jetpack\Sync\Functions' );
319
320 return Functions::json_wrap( $any, $seen_nodes );
321 }
322
323 /**
324 * Checks if the mime_content_type function is available and return it if so.
325 *
326 * The function mime_content_type is enabled by default in PHP, but can be disabled. We attempt to
327 * enforce this via composer.json, but that won't be checked in majority of cases where
328 * this would be happening.
329 *
330 * @since 7.8.0
331 *
332 * @param string $file File location.
333 *
334 * @return string|false MIME type or false if functionality is not available.
335 */
336 function jetpack_mime_content_type( $file ) {
337 if ( function_exists( 'mime_content_type' ) ) {
338 return mime_content_type( $file );
339 }
340
341 return false;
342 }
343
344 /**
345 * Checks that the mime type of the specified file is among those in a filterable list of mime types.
346 *
347 * @since 7.8.0
348 *
349 * @param string $file Path to file to get its mime type.
350 *
351 * @return bool
352 */
353 function jetpack_is_file_supported_for_sideloading( $file ) {
354 $type = jetpack_mime_content_type( $file );
355
356 if ( ! $type ) {
357 return false;
358 }
359
360 /**
361 * Filter the list of supported mime types for media sideloading.
362 *
363 * @since 4.0.0
364 *
365 * @module json-api
366 *
367 * @param array $supported_mime_types Array of the supported mime types for media sideloading.
368 */
369 $supported_mime_types = apply_filters(
370 'jetpack_supported_media_sideload_types',
371 array(
372 'image/png',
373 'image/jpeg',
374 'image/gif',
375 'image/bmp',
376 'image/webp',
377 'video/quicktime',
378 'video/mp4',
379 'video/mpeg',
380 'video/ogg',
381 'video/3gpp',
382 'video/3gpp2',
383 'video/h261',
384 'video/h262',
385 'video/h264',
386 'video/x-msvideo',
387 'video/x-ms-wmv',
388 'video/x-ms-asf',
389 )
390 );
391
392 // If the type returned was not an array as expected, then we know we don't have a match.
393 if ( ! is_array( $supported_mime_types ) ) {
394 return false;
395 }
396
397 return in_array( $type, $supported_mime_types, true );
398 }
399
400 /**
401 * Determine whether the current request is for accessing the frontend.
402 *
403 * @return bool True if it's a frontend request, false otherwise.
404 */
405 function jetpack_is_frontend() {
406 $is_frontend = true;
407
408 if (
409 is_admin() ||
410 wp_doing_ajax() ||
411 wp_is_json_request() ||
412 wp_is_jsonp_request() ||
413 wp_is_xml_request() ||
414 is_feed() ||
415 ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ||
416 ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
417 ( defined( 'WP_CLI' ) && WP_CLI )
418 ) {
419 $is_frontend = false;
420 }
421
422 /**
423 * Filter whether the current request is for accessing the frontend.
424 *
425 * @since 9.0.0
426 *
427 * @param bool $is_frontend Whether the current request is for accessing the frontend.
428 */
429 return (bool) apply_filters( 'jetpack_is_frontend', $is_frontend );
430 }
431
432 /**
433 * Build a list of Mastodon instance hosts.
434 * That list can be extended via a filter.
435 *
436 * @since 11.8
437 *
438 * @return array
439 */
440 function jetpack_mastodon_get_instance_list() {
441 $mastodon_instance_list = array(
442 // Regex pattern to match any .tld for the mastodon host name.
443 '#https?:\/\/(www\.)?mastodon\.(\w+)(\.\w+)?#',
444 // Regex pattern to match any .tld for the mstdn host name.
445 '#https?:\/\/(www\.)?mstdn\.(\w+)(\.\w+)?#',
446 'counter.social',
447 'fosstodon.org',
448 'gc2.jp',
449 'hachyderm.io',
450 'infosec.exchange',
451 'mas.to',
452 'pawoo.net',
453 );
454
455 /**
456 * Filter the list of Mastodon instances.
457 *
458 * @since 11.8
459 *
460 * @module widgets, theme-tools
461 *
462 * @param array $mastodon_instance_list Array of Mastodon instances.
463 */
464 return (array) apply_filters( 'jetpack_mastodon_instance_list', $mastodon_instance_list );
465 }
466