diff options
Diffstat (limited to 'plugins/jetpack/functions.global.php')
-rw-r--r-- | plugins/jetpack/functions.global.php | 287 |
1 files changed, 219 insertions, 68 deletions
diff --git a/plugins/jetpack/functions.global.php b/plugins/jetpack/functions.global.php index 9d983fd9..fd403dda 100644 --- a/plugins/jetpack/functions.global.php +++ b/plugins/jetpack/functions.global.php @@ -3,14 +3,18 @@ * This file is meant to be the home for any generic & reusable functions * that can be accessed anywhere within Jetpack. * - * This file is loaded whether or not Jetpack is active. + * This file is loaded whether Jetpack is active. * * Please namespace with jetpack_ * - * @package Jetpack + * @package automattic/jetpack */ use Automattic\Jetpack\Connection\Client; +use Automattic\Jetpack\Device_Detection; +use Automattic\Jetpack\Redirect; +use Automattic\Jetpack\Status\Host; +use Automattic\Jetpack\Sync\Functions; /** * Disable direct access. @@ -19,62 +23,138 @@ if ( ! defined( 'ABSPATH' ) ) { exit; } -if ( ! function_exists( 'wp_timezone' ) ) { - /** - * Shim for WordPress 5.3's wp_timezone() function. - * - * This is a mix of wp_timezone(), which calls wp_timezone_string(). - * We don't need both in Jetpack, so providing only one function. - * - * @since 7.9.0 - * @todo Remove when WP 5.3 is Jetpack's minimum - * - * @return DateTimeZone Site's DateTimeZone - */ - function wp_timezone() { - $timezone_string = get_option( 'timezone_string' ); +/** + * Hook into Core's _deprecated_function + * Add more details about when a deprecated function will be removed. + * + * @since 8.8.0 + * + * @param string $function The function that was called. + * @param string $replacement Optional. The function that should have been called. Default null. + * @param string $version The version of Jetpack that deprecated the function. + */ +function jetpack_deprecated_function( $function, $replacement, $version ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + // Bail early for non-Jetpack deprecations. + if ( 0 !== strpos( $version, 'jetpack-' ) ) { + return; + } - if ( $timezone_string ) { - return new DateTimeZone( $timezone_string ); - } + // Look for when a function will be removed based on when it was deprecated. + $removed_version = jetpack_get_future_removed_version( $version ); + + // If we could find a version, let's log a message about when removal will happen. + if ( + ! empty( $removed_version ) + && ( defined( 'WP_DEBUG' ) && WP_DEBUG ) + /** This filter is documented in core/src/wp-includes/functions.php */ + && apply_filters( 'deprecated_function_trigger_error', true ) + ) { + error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log + sprintf( + /* Translators: 1. Function name. 2. Jetpack version number. */ + __( 'The %1$s function will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ), + $function, + $removed_version + ) + ); - $offset = (float) get_option( 'gmt_offset' ); - $hours = (int) $offset; - $minutes = ( $offset - $hours ); + } +} +add_action( 'deprecated_function_run', 'jetpack_deprecated_function', 10, 3 ); + +/** + * Hook into Core's _deprecated_file + * Add more details about when a deprecated file will be removed. + * + * @since 8.8.0 + * + * @param string $file The file that was called. + * @param string $replacement The file that should have been included based on ABSPATH. + * @param string $version The version of WordPress that deprecated the file. + * @param string $message A message regarding the change. + */ +function jetpack_deprecated_file( $file, $replacement, $version, $message ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + // Bail early for non-Jetpack deprecations. + if ( 0 !== strpos( $version, 'jetpack-' ) ) { + return; + } - $sign = ( $offset < 0 ) ? '-' : '+'; - $abs_hour = abs( $hours ); - $abs_mins = abs( $minutes * 60 ); - $tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); + // Look for when a file will be removed based on when it was deprecated. + $removed_version = jetpack_get_future_removed_version( $version ); + + // If we could find a version, let's log a message about when removal will happen. + if ( + ! empty( $removed_version ) + && ( defined( 'WP_DEBUG' ) && WP_DEBUG ) + /** This filter is documented in core/src/wp-includes/functions.php */ + && apply_filters( 'deprecated_file_trigger_error', true ) + ) { + error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log + sprintf( + /* Translators: 1. File name. 2. Jetpack version number. */ + __( 'The %1$s file will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ), + $file, + $removed_version + ) + ); - return new DateTimeZone( $tz_offset ); } } +add_action( 'deprecated_file_included', 'jetpack_deprecated_file', 10, 4 ); /** - * Set the admin language, based on user language. + * Get the major version number of Jetpack 6 months after provided version. + * Useful to indicate when a deprecated function will be removed from Jetpack. + * + * @since 8.8.0 * - * @since 4.5.0 - * @deprecated 6.6.0 Use Core function instead. + * @param string $version The version of WordPress that deprecated the function. * - * @return string + * @return bool|float Return a Jetpack Major version number, or false. */ -function jetpack_get_user_locale() { - _deprecated_function( __FUNCTION__, 'jetpack-6.6.0', 'get_user_locale' ); - return get_user_locale(); +function jetpack_get_future_removed_version( $version ) { + /* + * Extract the version number from a deprecation notice. + * (let's only keep the first decimal, e.g. 8.8 and not 8.8.0) + */ + preg_match( '#(([0-9]+\.([0-9]+))(?:\.[0-9]+)*)#', $version, $matches ); + + if ( isset( $matches[2], $matches[3] ) ) { + $deprecated_version = (float) $matches[2]; + $deprecated_minor = (float) $matches[3]; + + /* + * If the detected minor version number + * (e.g. "7" in "8.7") + * is higher than 9, we know the version number is malformed. + * Jetpack does not use semver yet. + * Bail. + */ + if ( 10 <= $deprecated_minor ) { + return false; + } + + // We'll remove the function from the code 6 months later, thus 6 major versions later. + $removed_version = $deprecated_version + 0.6; + + return (float) $removed_version; + } + + return false; } /** - * Determine if this site is an Atomic site or not looking first at the 'at_options' option. + * Determine if this site is an WoA site or not looking first at the 'at_options' option. * As a fallback, check for presence of wpcomsh plugin to determine if a current site has undergone AT. * * @since 4.8.1 + * @deprecated $$next_version$$ * * @return bool */ function jetpack_is_atomic_site() { - $at_options = get_option( 'at_options', array() ); - return ! empty( $at_options ) || defined( 'WPCOMSH__PLUGIN_FILE' ); + jetpack_deprecated_function( __FUNCTION__, 'Automattic/Jetpack/Status/Host::is_woa_site', '$$next_version$$' ); + return ( new Host() )->is_woa_site(); } /** @@ -113,7 +193,7 @@ function jetpack_store_migration_data( $option_name, $option_value ) { 'post_title' => $option_name, 'post_content_filtered' => $option_value, 'post_type' => 'jetpack_migration', - 'post_date' => date( 'Y-m-d H:i:s', time() ), + 'post_date' => gmdate( 'Y-m-d H:i:s', time() ), ); $post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' ); @@ -143,6 +223,8 @@ function jetpack_get_migration_data( $option_name ) { /** * Prints a TOS blurb used throughout the connection prompts. * + * Note: custom ToS messages are also defined in Jetpack_Pre_Connection_JITMs->get_raw_messages() + * * @since 5.3 * * @echo string @@ -161,8 +243,8 @@ function jetpack_render_tos_blurb() { 'strong' => true, ) ), - 'https://wordpress.com/tos', - 'https://jetpack.com/support/what-data-does-jetpack-sync' + esc_url( Redirect::get_url( 'wpcom-tos' ) ), + esc_url( Redirect::get_url( 'jetpack-support-what-data-does-jetpack-sync' ) ) ); } @@ -178,7 +260,7 @@ function jetpack_render_tos_blurb() { * @return array|bool|WP_Error */ function jetpack_theme_update( $preempt, $r, $url ) { - if ( false !== stripos( $url, JETPACK__WPCOM_JSON_API_HOST . '/rest/v1/themes/download' ) ) { + if ( 0 === stripos( $url, JETPACK__WPCOM_JSON_API_BASE . '/rest/v1/themes/download' ) ) { $file = $r['filename']; if ( ! $file ) { return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) ); @@ -220,11 +302,12 @@ function jetpack_upgrader_pre_download( $reply ) { add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' ); - /** * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion. * * @since 6.1.0 + + * @deprecated Automattic\Jetpack\Sync\Functions::json_wrap * * @param array|obj $any Source data to be cleaned up. * @param array $seen_nodes Built array of nodes. @@ -232,33 +315,9 @@ add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' ); * @return array */ function jetpack_json_wrap( &$any, $seen_nodes = array() ) { - if ( is_object( $any ) ) { - $input = get_object_vars( $any ); - $input['__o'] = 1; - } else { - $input = &$any; - } - - if ( is_array( $input ) ) { - $seen_nodes[] = &$any; + _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\Jetpack\Sync\Functions' ); - $return = array(); - - foreach ( $input as $k => &$v ) { - if ( ( is_array( $v ) || is_object( $v ) ) ) { - if ( in_array( $v, $seen_nodes, true ) ) { - continue; - } - $return[ $k ] = jetpack_json_wrap( $v, $seen_nodes ); - } else { - $return[ $k ] = $v; - } - } - - return $return; - } - - return $any; + return Functions::json_wrap( $any, $seen_nodes ); } /** @@ -314,6 +373,7 @@ function jetpack_is_file_supported_for_sideloading( $file ) { 'image/jpeg', 'image/gif', 'image/bmp', + 'image/webp', 'video/quicktime', 'video/mp4', 'video/mpeg', @@ -336,3 +396,94 @@ function jetpack_is_file_supported_for_sideloading( $file ) { return in_array( $type, $supported_mime_types, true ); } + +/** + * Determine if the current User Agent matches the passed $kind + * + * @param string $kind Category of mobile device to check for. + * Either: any, dumb, smart. + * @param bool $return_matched_agent Boolean indicating if the UA should be returned. + * + * @return bool|string Boolean indicating if current UA matches $kind. If + * $return_matched_agent is true, returns the UA string + */ +function jetpack_is_mobile( $kind = 'any', $return_matched_agent = false ) { + + /** + * Filter the value of jetpack_is_mobile before it is calculated. + * + * Passing a truthy value to the filter will short-circuit determining the + * mobile type, returning the passed value instead. + * + * @since 4.2.0 + * + * @param bool|string $matches Boolean if current UA matches $kind or not. If + * $return_matched_agent is true, should return the UA string + * @param string $kind Category of mobile device being checked + * @param bool $return_matched_agent Boolean indicating if the UA should be returned + */ + $pre = apply_filters( 'pre_jetpack_is_mobile', null, $kind, $return_matched_agent ); + if ( $pre ) { + return $pre; + } + + $return = false; + $device_info = Device_Detection::get_info(); + + if ( 'any' === $kind ) { + $return = $device_info['is_phone']; + } elseif ( 'smart' === $kind ) { + $return = $device_info['is_smartphone']; + } elseif ( 'dumb' === $kind ) { + $return = $device_info['is_phone'] && ! $device_info['is_smartphone']; + } + + if ( $return_matched_agent && true === $return ) { + $return = $device_info['is_phone_matched_ua']; + } + + /** + * Filter the value of jetpack_is_mobile + * + * @since 4.2.0 + * + * @param bool|string $matches Boolean if current UA matches $kind or not. If + * $return_matched_agent is true, should return the UA string + * @param string $kind Category of mobile device being checked + * @param bool $return_matched_agent Boolean indicating if the UA should be returned + */ + return apply_filters( 'jetpack_is_mobile', $return, $kind, $return_matched_agent ); +} + +/** + * Determine whether the current request is for accessing the frontend. + * + * @return bool True if it's a frontend request, false otherwise. + */ +function jetpack_is_frontend() { + $is_frontend = true; + + if ( + is_admin() || + wp_doing_ajax() || + wp_doing_cron() || + wp_is_json_request() || + wp_is_jsonp_request() || + wp_is_xml_request() || + is_feed() || + ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || + ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) || + ( defined( 'WP_CLI' ) && WP_CLI ) + ) { + $is_frontend = false; + } + + /** + * Filter whether the current request is for accessing the frontend. + * + * @since 9.0.0 + * + * @param bool $is_frontend Whether the current request is for accessing the frontend. + */ + return (bool) apply_filters( 'jetpack_is_frontend', $is_frontend ); +} |