functions.global.php
| 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] ) && 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 | * Note: custom ToS messages are also defined in Jetpack_Pre_Connection_JITMs->get_raw_messages() |
| 257 | * |
| 258 | * @since 5.3 |
| 259 | * |
| 260 | * @echo string |
| 261 | */ |
| 262 | function jetpack_render_tos_blurb() { |
| 263 | printf( |
| 264 | wp_kses( |
| 265 | /* Translators: placeholders are links. */ |
| 266 | __( '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' ), |
| 267 | array( |
| 268 | 'a' => array( |
| 269 | 'href' => array(), |
| 270 | 'target' => array(), |
| 271 | 'rel' => array(), |
| 272 | ), |
| 273 | 'strong' => true, |
| 274 | ) |
| 275 | ), |
| 276 | esc_url( Redirect::get_url( 'wpcom-tos' ) ), |
| 277 | esc_url( Redirect::get_url( 'jetpack-support-what-data-does-jetpack-sync' ) ) |
| 278 | ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Intervene upgrade process so Jetpack themes are downloaded with credentials. |
| 283 | * |
| 284 | * @since 5.3 |
| 285 | * |
| 286 | * @param bool $preempt Whether to preempt an HTTP request's return value. Default false. |
| 287 | * @param array $r HTTP request arguments. |
| 288 | * @param string $url The request URL. |
| 289 | * |
| 290 | * @return array|bool|WP_Error |
| 291 | */ |
| 292 | function jetpack_theme_update( $preempt, $r, $url ) { |
| 293 | if ( 0 === stripos( $url, JETPACK__WPCOM_JSON_API_BASE . '/rest/v1/themes/download' ) ) { |
| 294 | $file = $r['filename']; |
| 295 | if ( ! $file ) { |
| 296 | return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) ); |
| 297 | } |
| 298 | $theme = pathinfo( wp_parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME ); |
| 299 | |
| 300 | // Remove filter to avoid endless loop since wpcom_json_api_request_as_blog uses this too. |
| 301 | remove_filter( 'pre_http_request', 'jetpack_theme_update' ); |
| 302 | $result = Client::wpcom_json_api_request_as_blog( |
| 303 | "themes/download/$theme.zip", |
| 304 | '1.1', |
| 305 | array( |
| 306 | 'stream' => true, |
| 307 | 'filename' => $file, |
| 308 | ) |
| 309 | ); |
| 310 | |
| 311 | if ( 200 !== wp_remote_retrieve_response_code( $result ) ) { |
| 312 | return new WP_Error( 'problem_fetching_theme', esc_html__( 'Problem downloading theme', 'jetpack' ) ); |
| 313 | } |
| 314 | return $result; |
| 315 | } |
| 316 | return $preempt; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Add the filter when a upgrade is going to be downloaded. |
| 321 | * |
| 322 | * @since 5.3 |
| 323 | * |
| 324 | * @param bool $reply Whether to bail without returning the package. Default false. |
| 325 | * |
| 326 | * @return bool |
| 327 | */ |
| 328 | function jetpack_upgrader_pre_download( $reply ) { |
| 329 | add_filter( 'pre_http_request', 'jetpack_theme_update', 10, 3 ); |
| 330 | return $reply; |
| 331 | } |
| 332 | |
| 333 | add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' ); |
| 334 | |
| 335 | /** |
| 336 | * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion. |
| 337 | * |
| 338 | * @since 6.1.0 |
| 339 | |
| 340 | * @deprecated Automattic\Jetpack\Sync\Functions::json_wrap |
| 341 | * |
| 342 | * @param array|obj $any Source data to be cleaned up. |
| 343 | * @param array $seen_nodes Built array of nodes. |
| 344 | * |
| 345 | * @return array |
| 346 | */ |
| 347 | function jetpack_json_wrap( &$any, $seen_nodes = array() ) { |
| 348 | _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\Jetpack\Sync\Functions' ); |
| 349 | |
| 350 | return Functions::json_wrap( $any, $seen_nodes ); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Checks if the mime_content_type function is available and return it if so. |
| 355 | * |
| 356 | * The function mime_content_type is enabled by default in PHP, but can be disabled. We attempt to |
| 357 | * enforce this via composer.json, but that won't be checked in majority of cases where |
| 358 | * this would be happening. |
| 359 | * |
| 360 | * @since 7.8.0 |
| 361 | * |
| 362 | * @param string $file File location. |
| 363 | * |
| 364 | * @return string|false MIME type or false if functionality is not available. |
| 365 | */ |
| 366 | function jetpack_mime_content_type( $file ) { |
| 367 | if ( function_exists( 'mime_content_type' ) ) { |
| 368 | return mime_content_type( $file ); |
| 369 | } |
| 370 | |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Checks that the mime type of the specified file is among those in a filterable list of mime types. |
| 376 | * |
| 377 | * @since 7.8.0 |
| 378 | * |
| 379 | * @param string $file Path to file to get its mime type. |
| 380 | * |
| 381 | * @return bool |
| 382 | */ |
| 383 | function jetpack_is_file_supported_for_sideloading( $file ) { |
| 384 | $type = jetpack_mime_content_type( $file ); |
| 385 | |
| 386 | if ( ! $type ) { |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Filter the list of supported mime types for media sideloading. |
| 392 | * |
| 393 | * @since 4.0.0 |
| 394 | * |
| 395 | * @module json-api |
| 396 | * |
| 397 | * @param array $supported_mime_types Array of the supported mime types for media sideloading. |
| 398 | */ |
| 399 | $supported_mime_types = apply_filters( |
| 400 | 'jetpack_supported_media_sideload_types', |
| 401 | array( |
| 402 | 'image/png', |
| 403 | 'image/jpeg', |
| 404 | 'image/gif', |
| 405 | 'image/bmp', |
| 406 | 'image/webp', |
| 407 | 'video/quicktime', |
| 408 | 'video/mp4', |
| 409 | 'video/mpeg', |
| 410 | 'video/ogg', |
| 411 | 'video/3gpp', |
| 412 | 'video/3gpp2', |
| 413 | 'video/h261', |
| 414 | 'video/h262', |
| 415 | 'video/h264', |
| 416 | 'video/x-msvideo', |
| 417 | 'video/x-ms-wmv', |
| 418 | 'video/x-ms-asf', |
| 419 | ) |
| 420 | ); |
| 421 | |
| 422 | // If the type returned was not an array as expected, then we know we don't have a match. |
| 423 | if ( ! is_array( $supported_mime_types ) ) { |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | return in_array( $type, $supported_mime_types, true ); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Go through headers and get a list of Vary headers to add, |
| 432 | * including a Vary Accept header if necessary. |
| 433 | * |
| 434 | * @since 12.2 |
| 435 | * |
| 436 | * @param array $headers The headers to be sent. |
| 437 | * |
| 438 | * @return array $vary_header_parts Vary Headers to be sent. |
| 439 | */ |
| 440 | function jetpack_get_vary_headers( $headers = array() ) { |
| 441 | $vary_header_parts = array( 'accept', 'content-type' ); |
| 442 | |
| 443 | foreach ( $headers as $header ) { |
| 444 | // Check for a Vary header. |
| 445 | if ( 'vary:' !== substr( strtolower( $header ), 0, 5 ) ) { |
| 446 | continue; |
| 447 | } |
| 448 | |
| 449 | // If the header is a wildcard, we'll return that. |
| 450 | if ( false !== strpos( $header, '*' ) ) { |
| 451 | $vary_header_parts = array( '*' ); |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | // Remove the Vary: part of the header. |
| 456 | $header = preg_replace( '/^vary\:\s?/i', '', $header ); |
| 457 | |
| 458 | // Remove spaces from the header. |
| 459 | $header = str_replace( ' ', '', $header ); |
| 460 | |
| 461 | // Break the header into parts. |
| 462 | $header_parts = explode( ',', strtolower( $header ) ); |
| 463 | |
| 464 | // Build an array with the Accept header and what was already there. |
| 465 | $vary_header_parts = array_values( array_unique( array_merge( $vary_header_parts, $header_parts ) ) ); |
| 466 | } |
| 467 | |
| 468 | return $vary_header_parts; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Determine whether the current request is for accessing the frontend. |
| 473 | * Also update Vary headers to indicate that the response may vary by Accept header. |
| 474 | * |
| 475 | * @return bool True if it's a frontend request, false otherwise. |
| 476 | */ |
| 477 | function jetpack_is_frontend() { |
| 478 | $is_frontend = true; |
| 479 | $is_varying_request = true; |
| 480 | |
| 481 | if ( |
| 482 | is_admin() |
| 483 | || wp_doing_ajax() |
| 484 | || wp_is_jsonp_request() |
| 485 | || is_feed() |
| 486 | || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) |
| 487 | || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) |
| 488 | || ( defined( 'WP_CLI' ) && WP_CLI ) |
| 489 | ) { |
| 490 | $is_frontend = false; |
| 491 | $is_varying_request = false; |
| 492 | } elseif ( |
| 493 | wp_is_json_request() |
| 494 | || wp_is_xml_request() |
| 495 | ) { |
| 496 | $is_frontend = false; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Check existing headers for the request. |
| 501 | * If there is no existing Vary Accept header, add one. |
| 502 | */ |
| 503 | if ( $is_varying_request && ! headers_sent() ) { |
| 504 | $headers = headers_list(); |
| 505 | $vary_header_parts = jetpack_get_vary_headers( $headers ); |
| 506 | |
| 507 | header( 'Vary: ' . implode( ', ', $vary_header_parts ) ); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Filter whether the current request is for accessing the frontend. |
| 512 | * |
| 513 | * @since 9.0.0 |
| 514 | * |
| 515 | * @param bool $is_frontend Whether the current request is for accessing the frontend. |
| 516 | */ |
| 517 | return (bool) apply_filters( 'jetpack_is_frontend', $is_frontend ); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Build a list of Mastodon instance hosts. |
| 522 | * That list can be extended via a filter. |
| 523 | * |
| 524 | * @since 11.8 |
| 525 | * |
| 526 | * @return array |
| 527 | */ |
| 528 | function jetpack_mastodon_get_instance_list() { |
| 529 | $mastodon_instance_list = array( |
| 530 | // Regex pattern to match any .tld for the mastodon host name. |
| 531 | '#https?:\/\/(www\.)?mastodon\.(\w+)(\.\w+)?#', |
| 532 | // Regex pattern to match any .tld for the mstdn host name. |
| 533 | '#https?:\/\/(www\.)?mstdn\.(\w+)(\.\w+)?#', |
| 534 | 'counter.social', |
| 535 | 'fosstodon.org', |
| 536 | 'gc2.jp', |
| 537 | 'hachyderm.io', |
| 538 | 'infosec.exchange', |
| 539 | 'mas.to', |
| 540 | 'pawoo.net', |
| 541 | ); |
| 542 | |
| 543 | /** |
| 544 | * Filter the list of Mastodon instances. |
| 545 | * |
| 546 | * @since 11.8 |
| 547 | * |
| 548 | * @module widgets, theme-tools |
| 549 | * |
| 550 | * @param array $mastodon_instance_list Array of Mastodon instances. |
| 551 | */ |
| 552 | return (array) apply_filters( 'jetpack_mastodon_instance_list', $mastodon_instance_list ); |
| 553 | } |
| 554 |