jetpack
Last commit date
3rd-party
4 years ago
_inc
4 years ago
css
4 years ago
extensions
4 years ago
images
4 years ago
jetpack_vendor
4 years ago
json-endpoints
3 years ago
modules
1 year ago
sal
4 years ago
src
4 years ago
vendor
4 years ago
views
4 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
4 years ago
class-jetpack-recommendations-banner.php
4 years ago
class-jetpack-stats-dashboard-widget.php
4 years ago
class-jetpack-wizard-banner.php
5 years ago
class-jetpack-xmlrpc-methods.php
5 years ago
class.frame-nonce-preview.php
4 years ago
class.jetpack-admin.php
4 years ago
class.jetpack-affiliate.php
4 years ago
class.jetpack-autoupdate.php
4 years ago
class.jetpack-bbpress-json-api.compat.php
5 years ago
class.jetpack-cli.php
4 years ago
class.jetpack-client-server.php
4 years ago
class.jetpack-connection-banner.php
4 years ago
class.jetpack-data.php
5 years ago
class.jetpack-gutenberg.php
4 years ago
class.jetpack-heartbeat.php
4 years ago
class.jetpack-idc.php
4 years ago
class.jetpack-modules-list-table.php
4 years ago
class.jetpack-network-sites-list-table.php
4 years ago
class.jetpack-network.php
4 years ago
class.jetpack-plan.php
4 years ago
class.jetpack-post-images.php
4 years ago
class.jetpack-twitter-cards.php
5 years ago
class.jetpack-user-agent.php
4 years ago
class.jetpack.php
4 years ago
class.json-api-endpoints.php
3 years ago
class.json-api.php
4 years ago
class.photon.php
4 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
4 years ago
functions.opengraph.php
4 years ago
functions.photon.php
4 years ago
jest.config.js
4 years ago
jetpack.php
1 year 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
1 year ago
require-lib.php
5 years ago
uninstall.php
5 years ago
wpml-config.xml
10 years ago
functions.global.php
489 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 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\Status\Host; |
| 17 | use Automattic\Jetpack\Sync\Functions; |
| 18 | |
| 19 | /** |
| 20 | * Disable direct access. |
| 21 | */ |
| 22 | if ( ! defined( 'ABSPATH' ) ) { |
| 23 | exit; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Hook into Core's _deprecated_function |
| 28 | * Add more details about when a deprecated function will be removed. |
| 29 | * |
| 30 | * @since 8.8.0 |
| 31 | * |
| 32 | * @param string $function The function that was called. |
| 33 | * @param string $replacement Optional. The function that should have been called. Default null. |
| 34 | * @param string $version The version of Jetpack that deprecated the function. |
| 35 | */ |
| 36 | function jetpack_deprecated_function( $function, $replacement, $version ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 37 | // Bail early for non-Jetpack deprecations. |
| 38 | if ( 0 !== strpos( $version, 'jetpack-' ) ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // Look for when a function will be removed based on when it was deprecated. |
| 43 | $removed_version = jetpack_get_future_removed_version( $version ); |
| 44 | |
| 45 | // If we could find a version, let's log a message about when removal will happen. |
| 46 | if ( |
| 47 | ! empty( $removed_version ) |
| 48 | && ( defined( 'WP_DEBUG' ) && WP_DEBUG ) |
| 49 | /** This filter is documented in core/src/wp-includes/functions.php */ |
| 50 | && apply_filters( 'deprecated_function_trigger_error', true ) |
| 51 | ) { |
| 52 | error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 53 | sprintf( |
| 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 | $function, |
| 57 | $removed_version |
| 58 | ) |
| 59 | ); |
| 60 | |
| 61 | } |
| 62 | } |
| 63 | add_action( 'deprecated_function_run', 'jetpack_deprecated_function', 10, 3 ); |
| 64 | |
| 65 | /** |
| 66 | * Hook into Core's _deprecated_file |
| 67 | * Add more details about when a deprecated file will be removed. |
| 68 | * |
| 69 | * @since 8.8.0 |
| 70 | * |
| 71 | * @param string $file The file that was called. |
| 72 | * @param string $replacement The file that should have been included based on ABSPATH. |
| 73 | * @param string $version The version of WordPress that deprecated the file. |
| 74 | * @param string $message A message regarding the change. |
| 75 | */ |
| 76 | function jetpack_deprecated_file( $file, $replacement, $version, $message ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 77 | // Bail early for non-Jetpack deprecations. |
| 78 | if ( 0 !== strpos( $version, 'jetpack-' ) ) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Look for when a file will be removed based on when it was deprecated. |
| 83 | $removed_version = jetpack_get_future_removed_version( $version ); |
| 84 | |
| 85 | // If we could find a version, let's log a message about when removal will happen. |
| 86 | if ( |
| 87 | ! empty( $removed_version ) |
| 88 | && ( defined( 'WP_DEBUG' ) && WP_DEBUG ) |
| 89 | /** This filter is documented in core/src/wp-includes/functions.php */ |
| 90 | && apply_filters( 'deprecated_file_trigger_error', true ) |
| 91 | ) { |
| 92 | error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 93 | sprintf( |
| 94 | /* Translators: 1. File name. 2. Jetpack version number. */ |
| 95 | __( 'The %1$s file will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ), |
| 96 | $file, |
| 97 | $removed_version |
| 98 | ) |
| 99 | ); |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | add_action( 'deprecated_file_included', 'jetpack_deprecated_file', 10, 4 ); |
| 104 | |
| 105 | /** |
| 106 | * Get the major version number of Jetpack 6 months after provided version. |
| 107 | * Useful to indicate when a deprecated function will be removed from Jetpack. |
| 108 | * |
| 109 | * @since 8.8.0 |
| 110 | * |
| 111 | * @param string $version The version of WordPress that deprecated the function. |
| 112 | * |
| 113 | * @return bool|float Return a Jetpack Major version number, or false. |
| 114 | */ |
| 115 | function jetpack_get_future_removed_version( $version ) { |
| 116 | /* |
| 117 | * Extract the version number from a deprecation notice. |
| 118 | * (let's only keep the first decimal, e.g. 8.8 and not 8.8.0) |
| 119 | */ |
| 120 | preg_match( '#(([0-9]+\.([0-9]+))(?:\.[0-9]+)*)#', $version, $matches ); |
| 121 | |
| 122 | if ( isset( $matches[2], $matches[3] ) ) { |
| 123 | $deprecated_version = (float) $matches[2]; |
| 124 | $deprecated_minor = (float) $matches[3]; |
| 125 | |
| 126 | /* |
| 127 | * If the detected minor version number |
| 128 | * (e.g. "7" in "8.7") |
| 129 | * is higher than 9, we know the version number is malformed. |
| 130 | * Jetpack does not use semver yet. |
| 131 | * Bail. |
| 132 | */ |
| 133 | if ( 10 <= $deprecated_minor ) { |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | // We'll remove the function from the code 6 months later, thus 6 major versions later. |
| 138 | $removed_version = $deprecated_version + 0.6; |
| 139 | |
| 140 | return (float) $removed_version; |
| 141 | } |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Determine if this site is an WoA site or not looking first at the 'at_options' option. |
| 148 | * As a fallback, check for presence of wpcomsh plugin to determine if a current site has undergone AT. |
| 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 if the current User Agent matches the passed $kind |
| 402 | * |
| 403 | * @param string $kind Category of mobile device to check for. |
| 404 | * Either: any, dumb, smart. |
| 405 | * @param bool $return_matched_agent Boolean indicating if the UA should be returned. |
| 406 | * |
| 407 | * @return bool|string Boolean indicating if current UA matches $kind. If |
| 408 | * $return_matched_agent is true, returns the UA string |
| 409 | */ |
| 410 | function jetpack_is_mobile( $kind = 'any', $return_matched_agent = false ) { |
| 411 | |
| 412 | /** |
| 413 | * Filter the value of jetpack_is_mobile before it is calculated. |
| 414 | * |
| 415 | * Passing a truthy value to the filter will short-circuit determining the |
| 416 | * mobile type, returning the passed value instead. |
| 417 | * |
| 418 | * @since 4.2.0 |
| 419 | * |
| 420 | * @param bool|string $matches Boolean if current UA matches $kind or not. If |
| 421 | * $return_matched_agent is true, should return the UA string |
| 422 | * @param string $kind Category of mobile device being checked |
| 423 | * @param bool $return_matched_agent Boolean indicating if the UA should be returned |
| 424 | */ |
| 425 | $pre = apply_filters( 'pre_jetpack_is_mobile', null, $kind, $return_matched_agent ); |
| 426 | if ( $pre ) { |
| 427 | return $pre; |
| 428 | } |
| 429 | |
| 430 | $return = false; |
| 431 | $device_info = Device_Detection::get_info(); |
| 432 | |
| 433 | if ( 'any' === $kind ) { |
| 434 | $return = $device_info['is_phone']; |
| 435 | } elseif ( 'smart' === $kind ) { |
| 436 | $return = $device_info['is_smartphone']; |
| 437 | } elseif ( 'dumb' === $kind ) { |
| 438 | $return = $device_info['is_phone'] && ! $device_info['is_smartphone']; |
| 439 | } |
| 440 | |
| 441 | if ( $return_matched_agent && true === $return ) { |
| 442 | $return = $device_info['is_phone_matched_ua']; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Filter the value of jetpack_is_mobile |
| 447 | * |
| 448 | * @since 4.2.0 |
| 449 | * |
| 450 | * @param bool|string $matches Boolean if current UA matches $kind or not. If |
| 451 | * $return_matched_agent is true, should return the UA string |
| 452 | * @param string $kind Category of mobile device being checked |
| 453 | * @param bool $return_matched_agent Boolean indicating if the UA should be returned |
| 454 | */ |
| 455 | return apply_filters( 'jetpack_is_mobile', $return, $kind, $return_matched_agent ); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Determine whether the current request is for accessing the frontend. |
| 460 | * |
| 461 | * @return bool True if it's a frontend request, false otherwise. |
| 462 | */ |
| 463 | function jetpack_is_frontend() { |
| 464 | $is_frontend = true; |
| 465 | |
| 466 | if ( |
| 467 | is_admin() || |
| 468 | wp_doing_ajax() || |
| 469 | wp_is_json_request() || |
| 470 | wp_is_jsonp_request() || |
| 471 | wp_is_xml_request() || |
| 472 | is_feed() || |
| 473 | ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || |
| 474 | ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) || |
| 475 | ( defined( 'WP_CLI' ) && WP_CLI ) |
| 476 | ) { |
| 477 | $is_frontend = false; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Filter whether the current request is for accessing the frontend. |
| 482 | * |
| 483 | * @since 9.0.0 |
| 484 | * |
| 485 | * @param bool $is_frontend Whether the current request is for accessing the frontend. |
| 486 | */ |
| 487 | return (bool) apply_filters( 'jetpack_is_frontend', $is_frontend ); |
| 488 | } |
| 489 |