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

552 lines 15.1 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 ( 0 !== strpos( $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 /* Translators: 1. Function name. 2. Jetpack version number. */
54 __( 'The %1$s function will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ),
55 $function,
56 $removed_version
57 )
58 );
59
60 }
61 }
62 add_action( 'deprecated_function_run', 'jetpack_deprecated_function', 10, 3 );
63
64 /**
65 * Hook into Core's _deprecated_file
66 * Add more details about when a deprecated file will be removed.
67 *
68 * @since 8.8.0
69 *
70 * @param string $file The file that was called.
71 * @param string $replacement The file that should have been included based on ABSPATH.
72 * @param string $version The version of WordPress that deprecated the file.
73 * @param string $message A message regarding the change.
74 */
75 function jetpack_deprecated_file( $file, $replacement, $version, $message ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
76 // Bail early for non-Jetpack deprecations.
77 if ( 0 !== strpos( $version, 'jetpack-' ) ) {
78 return;
79 }
80
81 // Look for when a file will be removed based on when it was deprecated.
82 $removed_version = jetpack_get_future_removed_version( $version );
83
84 // If we could find a version, let's log a message about when removal will happen.
85 if (
86 ! empty( $removed_version )
87 && ( defined( 'WP_DEBUG' ) && WP_DEBUG )
88 /** This filter is documented in core/src/wp-includes/functions.php */
89 && apply_filters( 'deprecated_file_trigger_error', true )
90 ) {
91 error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
92 sprintf(
93 /* Translators: 1. File name. 2. Jetpack version number. */
94 __( 'The %1$s file will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ),
95 $file,
96 $removed_version
97 )
98 );
99
100 }
101 }
102 add_action( 'deprecated_file_included', 'jetpack_deprecated_file', 10, 4 );
103
104 /**
105 * Get the major version number of Jetpack 6 months after provided version.
106 * Useful to indicate when a deprecated function will be removed from Jetpack.
107 *
108 * @since 8.8.0
109 *
110 * @param string $version The version of WordPress that deprecated the function.
111 *
112 * @return bool|float Return a Jetpack Major version number, or false.
113 */
114 function jetpack_get_future_removed_version( $version ) {
115 /*
116 * Extract the version number from a deprecation notice.
117 * (let's only keep the first decimal, e.g. 8.8 and not 8.8.0)
118 */
119 preg_match( '#(([0-9]+\.([0-9]+))(?:\.[0-9]+)*)#', $version, $matches );
120
121 if ( isset( $matches[2] ) && isset( $matches[3] ) ) {
122 $deprecated_version = (float) $matches[2];
123 $deprecated_minor = (float) $matches[3];
124
125 /*
126 * If the detected minor version number
127 * (e.g. "7" in "8.7")
128 * is higher than 9, we know the version number is malformed.
129 * Jetpack does not use semver yet.
130 * Bail.
131 */
132 if ( 10 <= $deprecated_minor ) {
133 return false;
134 }
135
136 // We'll remove the function from the code 6 months later, thus 6 major versions later.
137 $removed_version = $deprecated_version + 0.6;
138
139 return (float) $removed_version;
140 }
141
142 return false;
143 }
144
145 /**
146 * Determine if this site is an WoA site or not by looking for presence of the wpcomsh plugin.
147 *
148 * @since 4.8.1
149 * @deprecated 10.3.0
150 *
151 * @return bool
152 */
153 function jetpack_is_atomic_site() {
154 jetpack_deprecated_function( __FUNCTION__, 'Automattic/Jetpack/Status/Host::is_woa_site', 'jetpack-10.3.0' );
155 return ( new Host() )->is_woa_site();
156 }
157
158 /**
159 * Register post type for migration.
160 *
161 * @since 5.2
162 */
163 function jetpack_register_migration_post_type() {
164 register_post_type(
165 'jetpack_migration',
166 array(
167 'supports' => array(),
168 'taxonomies' => array(),
169 'hierarchical' => false,
170 'public' => false,
171 'has_archive' => false,
172 'can_export' => true,
173 )
174 );
175 }
176
177 /**
178 * Checks whether the Post DB threat currently exists on the site.
179 *
180 * @since 12.0
181 *
182 * @param string $option_name Option name.
183 *
184 * @return WP_Post|bool
185 */
186 function jetpack_migration_post_exists( $option_name ) {
187 $query = new WP_Query(
188 array(
189 'post_type' => 'jetpack_migration',
190 'title' => $option_name,
191 'post_status' => 'all',
192 'posts_per_page' => 1,
193 'no_found_rows' => true,
194 'ignore_sticky_posts' => true,
195 'update_post_term_cache' => false,
196 'update_post_meta_cache' => false,
197 'orderby' => 'post_date ID',
198 'order' => 'ASC',
199 )
200 );
201 if ( ! empty( $query->post ) ) {
202 return $query->post;
203 }
204
205 return false;
206 }
207
208 /**
209 * Stores migration data in the database.
210 *
211 * @since 5.2
212 *
213 * @param string $option_name Option name.
214 * @param bool $option_value Option value.
215 *
216 * @return int|WP_Error
217 */
218 function jetpack_store_migration_data( $option_name, $option_value ) {
219 jetpack_register_migration_post_type();
220
221 $insert = array(
222 'post_title' => $option_name,
223 'post_content_filtered' => $option_value,
224 'post_type' => 'jetpack_migration',
225 'post_date' => gmdate( 'Y-m-d H:i:s', time() ),
226 );
227
228 $migration_post = jetpack_migration_post_exists( $option_name );
229 if ( $migration_post ) {
230 $insert['ID'] = $migration_post->ID;
231 }
232
233 return wp_insert_post( $insert, true );
234 }
235
236 /**
237 * Retrieves legacy image widget data.
238 *
239 * @since 5.2
240 *
241 * @param string $option_name Option name.
242 *
243 * @return mixed|null
244 */
245 function jetpack_get_migration_data( $option_name ) {
246 $post = jetpack_migration_post_exists( $option_name );
247
248 return null !== $post ? maybe_unserialize( $post->post_content_filtered ) : null;
249 }
250
251 /**
252 * Prints a TOS blurb used throughout the connection prompts.
253 *
254 * Note: custom ToS messages are also defined in Jetpack_Pre_Connection_JITMs->get_raw_messages()
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 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' ),
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 array|obj $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 ( 'vary:' !== substr( strtolower( $header ), 0, 5 ) ) {
444 continue;
445 }
446
447 // If the header is a wildcard, we'll return that.
448 if ( false !== strpos( $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 /**
519 * Build a list of Mastodon instance hosts.
520 * That list can be extended via a filter.
521 *
522 * @since 11.8
523 *
524 * @return array
525 */
526 function jetpack_mastodon_get_instance_list() {
527 $mastodon_instance_list = array(
528 // Regex pattern to match any .tld for the mastodon host name.
529 '#https?:\/\/(www\.)?mastodon\.(\w+)(\.\w+)?#',
530 // Regex pattern to match any .tld for the mstdn host name.
531 '#https?:\/\/(www\.)?mstdn\.(\w+)(\.\w+)?#',
532 'counter.social',
533 'fosstodon.org',
534 'gc2.jp',
535 'hachyderm.io',
536 'infosec.exchange',
537 'mas.to',
538 'pawoo.net',
539 );
540
541 /**
542 * Filter the list of Mastodon instances.
543 *
544 * @since 11.8
545 *
546 * @module widgets, theme-tools
547 *
548 * @param array $mastodon_instance_list Array of Mastodon instances.
549 */
550 return (array) apply_filters( 'jetpack_mastodon_instance_list', $mastodon_instance_list );
551 }
552