jetpack
Last commit date
3rd-party
5 years ago
_inc
4 years ago
css
4 years ago
extensions
4 years ago
images
5 years ago
json-endpoints
4 years ago
modules
4 years ago
sal
4 years ago
src
5 years ago
vendor
4 years ago
views
5 years ago
CHANGELOG.md
4 years ago
LICENSE.txt
5 years ago
SECURITY.md
5 years ago
class-jetpack-connection-status.php
5 years ago
class-jetpack-pre-connection-jitms.php
5 years ago
class-jetpack-recommendations-banner.php
5 years ago
class-jetpack-wizard-banner.php
5 years ago
class-jetpack-xmlrpc-methods.php
5 years ago
class.frame-nonce-preview.php
6 years ago
class.jetpack-admin.php
5 years ago
class.jetpack-affiliate.php
6 years ago
class.jetpack-autoupdate.php
5 years ago
class.jetpack-bbpress-json-api.compat.php
5 years ago
class.jetpack-cli.php
4 years ago
class.jetpack-client-server.php
5 years ago
class.jetpack-connection-banner.php
5 years ago
class.jetpack-data.php
5 years ago
class.jetpack-gutenberg.php
4 years ago
class.jetpack-heartbeat.php
5 years ago
class.jetpack-idc.php
4 years ago
class.jetpack-ixr-client.php
5 years ago
class.jetpack-modules-list-table.php
5 years ago
class.jetpack-network-sites-list-table.php
5 years ago
class.jetpack-network.php
5 years ago
class.jetpack-plan.php
5 years ago
class.jetpack-post-images.php
5 years ago
class.jetpack-twitter-cards.php
5 years ago
class.jetpack-user-agent.php
5 years ago
class.jetpack.php
4 years ago
class.json-api-endpoints.php
4 years ago
class.json-api.php
5 years ago
class.photon.php
5 years ago
composer.json
4 years ago
functions.compat.php
5 years ago
functions.cookies.php
5 years ago
functions.gallery.php
6 years ago
functions.global.php
5 years ago
functions.opengraph.php
5 years ago
functions.photon.php
5 years ago
jest.config.js
5 years ago
jetpack.php
4 years ago
json-api-config.php
5 years ago
json-endpoints.php
7 years ago
load-jetpack.php
4 years ago
locales.php
7 years ago
readme.txt
4 years ago
require-lib.php
5 years ago
uninstall.php
5 years ago
wpml-config.xml
10 years ago
functions.global.php
500 lines
| 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 or not 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\Device_Detection; |
| 15 | use Automattic\Jetpack\Redirect; |
| 16 | use Automattic\Jetpack\Sync\Functions; |
| 17 | |
| 18 | /** |
| 19 | * Disable direct access. |
| 20 | */ |
| 21 | if ( ! defined( 'ABSPATH' ) ) { |
| 22 | exit; |
| 23 | } |
| 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], $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 | * Set the admin language, based on user language. |
| 147 | * |
| 148 | * @since 4.5.0 |
| 149 | * @deprecated 6.6.0 Use Core function instead. |
| 150 | * |
| 151 | * @return string |
| 152 | */ |
| 153 | function jetpack_get_user_locale() { |
| 154 | _deprecated_function( __FUNCTION__, 'jetpack-6.6.0', 'get_user_locale' ); |
| 155 | return get_user_locale(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Determine if this site is an Atomic site or not looking first at the 'at_options' option. |
| 160 | * As a fallback, check for presence of wpcomsh plugin to determine if a current site has undergone AT. |
| 161 | * |
| 162 | * @since 4.8.1 |
| 163 | * |
| 164 | * @return bool |
| 165 | */ |
| 166 | function jetpack_is_atomic_site() { |
| 167 | $at_options = get_option( 'at_options', array() ); |
| 168 | return ! empty( $at_options ) || defined( 'WPCOMSH__PLUGIN_FILE' ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Register post type for migration. |
| 173 | * |
| 174 | * @since 5.2 |
| 175 | */ |
| 176 | function jetpack_register_migration_post_type() { |
| 177 | register_post_type( |
| 178 | 'jetpack_migration', |
| 179 | array( |
| 180 | 'supports' => array(), |
| 181 | 'taxonomies' => array(), |
| 182 | 'hierarchical' => false, |
| 183 | 'public' => false, |
| 184 | 'has_archive' => false, |
| 185 | 'can_export' => true, |
| 186 | ) |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Stores migration data in the database. |
| 192 | * |
| 193 | * @since 5.2 |
| 194 | * |
| 195 | * @param string $option_name Option name. |
| 196 | * @param bool $option_value Option value. |
| 197 | * |
| 198 | * @return int|WP_Error |
| 199 | */ |
| 200 | function jetpack_store_migration_data( $option_name, $option_value ) { |
| 201 | jetpack_register_migration_post_type(); |
| 202 | |
| 203 | $insert = array( |
| 204 | 'post_title' => $option_name, |
| 205 | 'post_content_filtered' => $option_value, |
| 206 | 'post_type' => 'jetpack_migration', |
| 207 | 'post_date' => gmdate( 'Y-m-d H:i:s', time() ), |
| 208 | ); |
| 209 | |
| 210 | $post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' ); |
| 211 | |
| 212 | if ( null !== $post ) { |
| 213 | $insert['ID'] = $post->ID; |
| 214 | } |
| 215 | |
| 216 | return wp_insert_post( $insert, true ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Retrieves legacy image widget data. |
| 221 | * |
| 222 | * @since 5.2 |
| 223 | * |
| 224 | * @param string $option_name Option name. |
| 225 | * |
| 226 | * @return mixed|null |
| 227 | */ |
| 228 | function jetpack_get_migration_data( $option_name ) { |
| 229 | $post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' ); |
| 230 | |
| 231 | return null !== $post ? maybe_unserialize( $post->post_content_filtered ) : null; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Prints a TOS blurb used throughout the connection prompts. |
| 236 | * |
| 237 | * Note: custom ToS messages are also defined in Jetpack_Pre_Connection_JITMs->get_raw_messages() |
| 238 | * |
| 239 | * @since 5.3 |
| 240 | * |
| 241 | * @echo string |
| 242 | */ |
| 243 | function jetpack_render_tos_blurb() { |
| 244 | printf( |
| 245 | wp_kses( |
| 246 | /* Translators: placeholders are links. */ |
| 247 | __( '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' ), |
| 248 | array( |
| 249 | 'a' => array( |
| 250 | 'href' => array(), |
| 251 | 'target' => array(), |
| 252 | 'rel' => array(), |
| 253 | ), |
| 254 | 'strong' => true, |
| 255 | ) |
| 256 | ), |
| 257 | esc_url( Redirect::get_url( 'wpcom-tos' ) ), |
| 258 | esc_url( Redirect::get_url( 'jetpack-support-what-data-does-jetpack-sync' ) ) |
| 259 | ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Intervene upgrade process so Jetpack themes are downloaded with credentials. |
| 264 | * |
| 265 | * @since 5.3 |
| 266 | * |
| 267 | * @param bool $preempt Whether to preempt an HTTP request's return value. Default false. |
| 268 | * @param array $r HTTP request arguments. |
| 269 | * @param string $url The request URL. |
| 270 | * |
| 271 | * @return array|bool|WP_Error |
| 272 | */ |
| 273 | function jetpack_theme_update( $preempt, $r, $url ) { |
| 274 | if ( 0 === stripos( $url, JETPACK__WPCOM_JSON_API_BASE . '/rest/v1/themes/download' ) ) { |
| 275 | $file = $r['filename']; |
| 276 | if ( ! $file ) { |
| 277 | return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) ); |
| 278 | } |
| 279 | $theme = pathinfo( wp_parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME ); |
| 280 | |
| 281 | // Remove filter to avoid endless loop since wpcom_json_api_request_as_blog uses this too. |
| 282 | remove_filter( 'pre_http_request', 'jetpack_theme_update' ); |
| 283 | $result = Client::wpcom_json_api_request_as_blog( |
| 284 | "themes/download/$theme.zip", |
| 285 | '1.1', |
| 286 | array( |
| 287 | 'stream' => true, |
| 288 | 'filename' => $file, |
| 289 | ) |
| 290 | ); |
| 291 | |
| 292 | if ( 200 !== wp_remote_retrieve_response_code( $result ) ) { |
| 293 | return new WP_Error( 'problem_fetching_theme', esc_html__( 'Problem downloading theme', 'jetpack' ) ); |
| 294 | } |
| 295 | return $result; |
| 296 | } |
| 297 | return $preempt; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Add the filter when a upgrade is going to be downloaded. |
| 302 | * |
| 303 | * @since 5.3 |
| 304 | * |
| 305 | * @param bool $reply Whether to bail without returning the package. Default false. |
| 306 | * |
| 307 | * @return bool |
| 308 | */ |
| 309 | function jetpack_upgrader_pre_download( $reply ) { |
| 310 | add_filter( 'pre_http_request', 'jetpack_theme_update', 10, 3 ); |
| 311 | return $reply; |
| 312 | } |
| 313 | |
| 314 | add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' ); |
| 315 | |
| 316 | /** |
| 317 | * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion. |
| 318 | * |
| 319 | * @since 6.1.0 |
| 320 | |
| 321 | * @deprecated Automattic\Jetpack\Sync\Functions::json_wrap |
| 322 | * |
| 323 | * @param array|obj $any Source data to be cleaned up. |
| 324 | * @param array $seen_nodes Built array of nodes. |
| 325 | * |
| 326 | * @return array |
| 327 | */ |
| 328 | function jetpack_json_wrap( &$any, $seen_nodes = array() ) { |
| 329 | _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\Jetpack\Sync\Functions' ); |
| 330 | |
| 331 | return Functions::json_wrap( $any, $seen_nodes ); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Checks if the mime_content_type function is available and return it if so. |
| 336 | * |
| 337 | * The function mime_content_type is enabled by default in PHP, but can be disabled. We attempt to |
| 338 | * enforce this via composer.json, but that won't be checked in majority of cases where |
| 339 | * this would be happening. |
| 340 | * |
| 341 | * @since 7.8.0 |
| 342 | * |
| 343 | * @param string $file File location. |
| 344 | * |
| 345 | * @return string|false MIME type or false if functionality is not available. |
| 346 | */ |
| 347 | function jetpack_mime_content_type( $file ) { |
| 348 | if ( function_exists( 'mime_content_type' ) ) { |
| 349 | return mime_content_type( $file ); |
| 350 | } |
| 351 | |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Checks that the mime type of the specified file is among those in a filterable list of mime types. |
| 357 | * |
| 358 | * @since 7.8.0 |
| 359 | * |
| 360 | * @param string $file Path to file to get its mime type. |
| 361 | * |
| 362 | * @return bool |
| 363 | */ |
| 364 | function jetpack_is_file_supported_for_sideloading( $file ) { |
| 365 | $type = jetpack_mime_content_type( $file ); |
| 366 | |
| 367 | if ( ! $type ) { |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Filter the list of supported mime types for media sideloading. |
| 373 | * |
| 374 | * @since 4.0.0 |
| 375 | * |
| 376 | * @module json-api |
| 377 | * |
| 378 | * @param array $supported_mime_types Array of the supported mime types for media sideloading. |
| 379 | */ |
| 380 | $supported_mime_types = apply_filters( |
| 381 | 'jetpack_supported_media_sideload_types', |
| 382 | array( |
| 383 | 'image/png', |
| 384 | 'image/jpeg', |
| 385 | 'image/gif', |
| 386 | 'image/bmp', |
| 387 | 'video/quicktime', |
| 388 | 'video/mp4', |
| 389 | 'video/mpeg', |
| 390 | 'video/ogg', |
| 391 | 'video/3gpp', |
| 392 | 'video/3gpp2', |
| 393 | 'video/h261', |
| 394 | 'video/h262', |
| 395 | 'video/h264', |
| 396 | 'video/x-msvideo', |
| 397 | 'video/x-ms-wmv', |
| 398 | 'video/x-ms-asf', |
| 399 | ) |
| 400 | ); |
| 401 | |
| 402 | // If the type returned was not an array as expected, then we know we don't have a match. |
| 403 | if ( ! is_array( $supported_mime_types ) ) { |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | return in_array( $type, $supported_mime_types, true ); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Determine if the current User Agent matches the passed $kind |
| 412 | * |
| 413 | * @param string $kind Category of mobile device to check for. |
| 414 | * Either: any, dumb, smart. |
| 415 | * @param bool $return_matched_agent Boolean indicating if the UA should be returned. |
| 416 | * |
| 417 | * @return bool|string Boolean indicating if current UA matches $kind. If |
| 418 | * $return_matched_agent is true, returns the UA string |
| 419 | */ |
| 420 | function jetpack_is_mobile( $kind = 'any', $return_matched_agent = false ) { |
| 421 | |
| 422 | /** |
| 423 | * Filter the value of jetpack_is_mobile before it is calculated. |
| 424 | * |
| 425 | * Passing a truthy value to the filter will short-circuit determining the |
| 426 | * mobile type, returning the passed value instead. |
| 427 | * |
| 428 | * @since 4.2.0 |
| 429 | * |
| 430 | * @param bool|string $matches Boolean if current UA matches $kind or not. If |
| 431 | * $return_matched_agent is true, should return the UA string |
| 432 | * @param string $kind Category of mobile device being checked |
| 433 | * @param bool $return_matched_agent Boolean indicating if the UA should be returned |
| 434 | */ |
| 435 | $pre = apply_filters( 'pre_jetpack_is_mobile', null, $kind, $return_matched_agent ); |
| 436 | if ( $pre ) { |
| 437 | return $pre; |
| 438 | } |
| 439 | |
| 440 | $return = false; |
| 441 | $device_info = Device_Detection::get_info(); |
| 442 | |
| 443 | if ( 'any' === $kind ) { |
| 444 | $return = $device_info['is_phone']; |
| 445 | } elseif ( 'smart' === $kind ) { |
| 446 | $return = $device_info['is_smartphone']; |
| 447 | } elseif ( 'dumb' === $kind ) { |
| 448 | $return = $device_info['is_phone'] && ! $device_info['is_smartphone']; |
| 449 | } |
| 450 | |
| 451 | if ( $return_matched_agent && true === $return ) { |
| 452 | $return = $device_info['is_phone_matched_ua']; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Filter the value of jetpack_is_mobile |
| 457 | * |
| 458 | * @since 4.2.0 |
| 459 | * |
| 460 | * @param bool|string $matches Boolean if current UA matches $kind or not. If |
| 461 | * $return_matched_agent is true, should return the UA string |
| 462 | * @param string $kind Category of mobile device being checked |
| 463 | * @param bool $return_matched_agent Boolean indicating if the UA should be returned |
| 464 | */ |
| 465 | return apply_filters( 'jetpack_is_mobile', $return, $kind, $return_matched_agent ); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Determine whether the current request is for accessing the frontend. |
| 470 | * |
| 471 | * @return bool True if it's a frontend request, false otherwise. |
| 472 | */ |
| 473 | function jetpack_is_frontend() { |
| 474 | $is_frontend = true; |
| 475 | |
| 476 | if ( |
| 477 | is_admin() || |
| 478 | wp_doing_ajax() || |
| 479 | wp_doing_cron() || |
| 480 | wp_is_json_request() || |
| 481 | wp_is_jsonp_request() || |
| 482 | wp_is_xml_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 | } |
| 490 | |
| 491 | /** |
| 492 | * Filter whether the current request is for accessing the frontend. |
| 493 | * |
| 494 | * @since 9.0.0 |
| 495 | * |
| 496 | * @param bool $is_frontend Whether the current request is for accessing the frontend. |
| 497 | */ |
| 498 | return (bool) apply_filters( 'jetpack_is_frontend', $is_frontend ); |
| 499 | } |
| 500 |