PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 14.2
Jetpack – WP Security, Backup, Speed, & Growth v14.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 14.2, at functions.global.php

557 lines 15.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
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 // Disable direct access.
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22
23 require_once __DIR__ . '/functions.is-mobile.php';
24
25 /**
26 * Hook into Core's _deprecated_function
27 * Add more details about when a deprecated function will be removed.
28 *
29 * @since 8.8.0
30 *
31 * @param string $function The function that was called.
32 * @param string $replacement Optional. The function that should have been called. Default null.
33 * @param string $version The version of Jetpack that deprecated the function.
34 */
35 function jetpack_deprecated_function( $function, $replacement, $version ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
36 // Bail early for non-Jetpack deprecations.
37 if ( ! str_starts_with( $version, 'jetpack-' ) ) {
38 return;
39 }
40
41 // Look for when a function will be removed based on when it was deprecated.
42 $removed_version = jetpack_get_future_removed_version( $version );
43
44 // If we could find a version, let's log a message about when removal will happen.
45 if (
46 ! empty( $removed_version )
47 && ( defined( 'WP_DEBUG' ) && WP_DEBUG )
48 /** This filter is documented in core/src/wp-includes/functions.php */
49 && apply_filters( 'deprecated_function_trigger_error', true )
50 ) {
51 error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
52 sprintf(
53 doing_action( 'after_setup_theme' ) || did_action( 'after_setup_theme' ) ?
54 /* Translators: 1. Function name. 2. Jetpack version number. */
55 __( 'The %1$s function will be removed from the Jetpack plugin in version %2$s.', 'jetpack' )
56 : 'The %1$s function will be removed from the Jetpack plugin in version %2$s.',
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 ( ! str_starts_with( $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] ) && isset( $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 * Checks whether the Post DB threat currently exists on the site.
181 *
182 * @since 12.0
183 *
184 * @param string $option_name Option name.
185 *
186 * @return WP_Post|bool
187 */
188 function jetpack_migration_post_exists( $option_name ) {
189 $query = new WP_Query(
190 array(
191 'post_type' => 'jetpack_migration',
192 'title' => $option_name,
193 'post_status' => 'all',
194 'posts_per_page' => 1,
195 'no_found_rows' => true,
196 'ignore_sticky_posts' => true,
197 'update_post_term_cache' => false,
198 'update_post_meta_cache' => false,
199 'orderby' => 'post_date ID',
200 'order' => 'ASC',
201 )
202 );
203 if ( ! empty( $query->post ) ) {
204 return $query->post;
205 }
206
207 return false;
208 }
209
210 /**
211 * Stores migration data in the database.
212 *
213 * @since 5.2
214 *
215 * @param string $option_name Option name.
216 * @param bool $option_value Option value.
217 *
218 * @return int|WP_Error
219 */
220 function jetpack_store_migration_data( $option_name, $option_value ) {
221 jetpack_register_migration_post_type();
222
223 $insert = array(
224 'post_title' => $option_name,
225 'post_content_filtered' => $option_value,
226 'post_type' => 'jetpack_migration',
227 'post_date' => gmdate( 'Y-m-d H:i:s', time() ),
228 );
229
230 $migration_post = jetpack_migration_post_exists( $option_name );
231 if ( $migration_post ) {
232 $insert['ID'] = $migration_post->ID;
233 }
234
235 return wp_insert_post( $insert, true );
236 }
237
238 /**
239 * Retrieves legacy image widget data.
240 *
241 * @since 5.2
242 *
243 * @param string $option_name Option name.
244 *
245 * @return mixed|null
246 */
247 function jetpack_get_migration_data( $option_name ) {
248 $post = jetpack_migration_post_exists( $option_name );
249
250 return null !== $post ? maybe_unserialize( $post->post_content_filtered ) : null;
251 }
252
253 /**
254 * Prints a TOS blurb used throughout the connection prompts.
255 *
256 * @since 5.3
257 *
258 * @echo string
259 */
260 function jetpack_render_tos_blurb() {
261 printf(
262 wp_kses(
263 /* Translators: placeholders are links. */
264 __( 'By clicking <strong>Set up Jetpack</strong>, 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">sync your site‘s data</a> with us.', 'jetpack' ),
265 array(
266 'a' => array(
267 'href' => array(),
268 'target' => array(),
269 'rel' => array(),
270 ),
271 'strong' => true,
272 )
273 ),
274 esc_url( Redirect::get_url( 'wpcom-tos' ) ),
275 esc_url( Redirect::get_url( 'jetpack-support-what-data-does-jetpack-sync' ) )
276 );
277 }
278
279 /**
280 * Intervene upgrade process so Jetpack themes are downloaded with credentials.
281 *
282 * @since 5.3
283 *
284 * @param bool $preempt Whether to preempt an HTTP request's return value. Default false.
285 * @param array $r HTTP request arguments.
286 * @param string $url The request URL.
287 *
288 * @return array|bool|WP_Error
289 */
290 function jetpack_theme_update( $preempt, $r, $url ) {
291 if ( 0 === stripos( $url, JETPACK__WPCOM_JSON_API_BASE . '/rest/v1/themes/download' ) ) {
292 $file = $r['filename'];
293 if ( ! $file ) {
294 return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) );
295 }
296 $theme = pathinfo( wp_parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME );
297
298 // Remove filter to avoid endless loop since wpcom_json_api_request_as_blog uses this too.
299 remove_filter( 'pre_http_request', 'jetpack_theme_update' );
300 $result = Client::wpcom_json_api_request_as_blog(
301 "themes/download/$theme.zip",
302 '1.1',
303 array(
304 'stream' => true,
305 'filename' => $file,
306 )
307 );
308
309 if ( 200 !== wp_remote_retrieve_response_code( $result ) ) {
310 return new WP_Error( 'problem_fetching_theme', esc_html__( 'Problem downloading theme', 'jetpack' ) );
311 }
312 return $result;
313 }
314 return $preempt;
315 }
316
317 /**
318 * Add the filter when a upgrade is going to be downloaded.
319 *
320 * @since 5.3
321 *
322 * @param bool $reply Whether to bail without returning the package. Default false.
323 *
324 * @return bool
325 */
326 function jetpack_upgrader_pre_download( $reply ) {
327 add_filter( 'pre_http_request', 'jetpack_theme_update', 10, 3 );
328 return $reply;
329 }
330
331 add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' );
332
333 /**
334 * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion.
335 *
336 * @since 6.1.0
337
338 * @deprecated Automattic\Jetpack\Sync\Functions::json_wrap
339 *
340 * @param mixed $any Source data to be cleaned up.
341 * @param array $seen_nodes Built array of nodes.
342 *
343 * @return array
344 */
345 function jetpack_json_wrap( &$any, $seen_nodes = array() ) {
346 _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\Jetpack\Sync\Functions' );
347
348 return Functions::json_wrap( $any, $seen_nodes );
349 }
350
351 /**
352 * Checks if the mime_content_type function is available and return it if so.
353 *
354 * The function mime_content_type is enabled by default in PHP, but can be disabled. We attempt to
355 * enforce this via composer.json, but that won't be checked in majority of cases where
356 * this would be happening.
357 *
358 * @since 7.8.0
359 *
360 * @param string $file File location.
361 *
362 * @return string|false MIME type or false if functionality is not available.
363 */
364 function jetpack_mime_content_type( $file ) {
365 if ( function_exists( 'mime_content_type' ) ) {
366 return mime_content_type( $file );
367 }
368
369 return false;
370 }
371
372 /**
373 * Checks that the mime type of the specified file is among those in a filterable list of mime types.
374 *
375 * @since 7.8.0
376 *
377 * @param string $file Path to file to get its mime type.
378 *
379 * @return bool
380 */
381 function jetpack_is_file_supported_for_sideloading( $file ) {
382 $type = jetpack_mime_content_type( $file );
383
384 if ( ! $type ) {
385 return false;
386 }
387
388 /**
389 * Filter the list of supported mime types for media sideloading.
390 *
391 * @since 4.0.0
392 *
393 * @module json-api
394 *
395 * @param array $supported_mime_types Array of the supported mime types for media sideloading.
396 */
397 $supported_mime_types = apply_filters(
398 'jetpack_supported_media_sideload_types',
399 array(
400 'image/png',
401 'image/jpeg',
402 'image/gif',
403 'image/bmp',
404 'image/webp',
405 'video/quicktime',
406 'video/mp4',
407 'video/mpeg',
408 'video/ogg',
409 'video/3gpp',
410 'video/3gpp2',
411 'video/h261',
412 'video/h262',
413 'video/h264',
414 'video/x-msvideo',
415 'video/x-ms-wmv',
416 'video/x-ms-asf',
417 )
418 );
419
420 // If the type returned was not an array as expected, then we know we don't have a match.
421 if ( ! is_array( $supported_mime_types ) ) {
422 return false;
423 }
424
425 return in_array( $type, $supported_mime_types, true );
426 }
427
428 /**
429 * Go through headers and get a list of Vary headers to add,
430 * including a Vary Accept header if necessary.
431 *
432 * @since 12.2
433 *
434 * @param array $headers The headers to be sent.
435 *
436 * @return array $vary_header_parts Vary Headers to be sent.
437 */
438 function jetpack_get_vary_headers( $headers = array() ) {
439 $vary_header_parts = array( 'accept', 'content-type' );
440
441 foreach ( $headers as $header ) {
442 // Check for a Vary header.
443 if ( ! str_starts_with( strtolower( $header ), 'vary:' ) ) {
444 continue;
445 }
446
447 // If the header is a wildcard, we'll return that.
448 if ( str_contains( $header, '*' ) ) {
449 $vary_header_parts = array( '*' );
450 break;
451 }
452
453 // Remove the Vary: part of the header.
454 $header = preg_replace( '/^vary\:\s?/i', '', $header );
455
456 // Remove spaces from the header.
457 $header = str_replace( ' ', '', $header );
458
459 // Break the header into parts.
460 $header_parts = explode( ',', strtolower( $header ) );
461
462 // Build an array with the Accept header and what was already there.
463 $vary_header_parts = array_values( array_unique( array_merge( $vary_header_parts, $header_parts ) ) );
464 }
465
466 return $vary_header_parts;
467 }
468
469 /**
470 * Determine whether the current request is for accessing the frontend.
471 * Also update Vary headers to indicate that the response may vary by Accept header.
472 *
473 * @return bool True if it's a frontend request, false otherwise.
474 */
475 function jetpack_is_frontend() {
476 $is_frontend = true;
477 $is_varying_request = true;
478
479 if (
480 is_admin()
481 || wp_doing_ajax()
482 || wp_is_jsonp_request()
483 || is_feed()
484 || ( defined( 'REST_REQUEST' ) && REST_REQUEST )
485 || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST )
486 || ( defined( 'WP_CLI' ) && WP_CLI )
487 ) {
488 $is_frontend = false;
489 $is_varying_request = false;
490 } elseif (
491 wp_is_json_request()
492 || wp_is_xml_request()
493 ) {
494 $is_frontend = false;
495 }
496
497 /*
498 * Check existing headers for the request.
499 * If there is no existing Vary Accept header, add one.
500 */
501 if ( $is_varying_request && ! headers_sent() ) {
502 $headers = headers_list();
503 $vary_header_parts = jetpack_get_vary_headers( $headers );
504
505 header( 'Vary: ' . implode( ', ', $vary_header_parts ) );
506 }
507
508 /**
509 * Filter whether the current request is for accessing the frontend.
510 *
511 * @since 9.0.0
512 *
513 * @param bool $is_frontend Whether the current request is for accessing the frontend.
514 */
515 return (bool) apply_filters( 'jetpack_is_frontend', $is_frontend );
516 }
517
518 if ( ! function_exists( 'jetpack_mastodon_get_instance_list' ) ) {
519 /**
520 * Build a list of Mastodon instance hosts.
521 * That list can be extended via a filter.
522 *
523 * @todo This function is now replicated in the Classic Theme Helper package and can be
524 * removed here once Social Links are moved out of Jetpack.
525 *
526 * @since 11.8
527 *
528 * @return array
529 */
530 function jetpack_mastodon_get_instance_list() {
531 $mastodon_instance_list = array(
532 // Regex pattern to match any .tld for the mastodon host name.
533 '#https?:\/\/(www\.)?mastodon\.(\w+)(\.\w+)?#',
534 // Regex pattern to match any .tld for the mstdn host name.
535 '#https?:\/\/(www\.)?mstdn\.(\w+)(\.\w+)?#',
536 'counter.social',
537 'fosstodon.org',
538 'gc2.jp',
539 'hachyderm.io',
540 'infosec.exchange',
541 'mas.to',
542 'pawoo.net',
543 );
544
545 /**
546 * Filter the list of Mastodon instances.
547 *
548 * @since 11.8
549 *
550 * @module widgets, theme-tools
551 *
552 * @param array $mastodon_instance_list Array of Mastodon instances.
553 */
554 return (array) apply_filters( 'jetpack_mastodon_instance_list', $mastodon_instance_list );
555 }
556 }
557