diff options
Diffstat (limited to 'plugins/jetpack/_inc/lib')
24 files changed, 3849 insertions, 273 deletions
diff --git a/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-admin-page.php b/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-admin-page.php index d352aa56..0c50f380 100644 --- a/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-admin-page.php +++ b/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-admin-page.php @@ -35,6 +35,7 @@ abstract class Jetpack_Admin_Page { } function add_actions() { + global $pagenow; // If user is not an admin and site is in Dev Mode, don't do anything if ( ! current_user_can( 'manage_options' ) && Jetpack::is_development_mode() ) { @@ -61,6 +62,18 @@ abstract class Jetpack_Admin_Page { if ( ! self::$block_page_rendering_for_idc ) { add_action( "admin_print_styles-$hook", array( $this, 'additional_styles' ) ); } + // If someone just activated Jetpack, let's show them a fullscreen connection banner. + if ( + ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) + && ! Jetpack::is_active() + && current_user_can( 'jetpack_connect' ) + && ! Jetpack::is_development_mode() + ) { + add_action( 'admin_enqueue_scripts', array( 'Jetpack_Connection_Banner', 'enqueue_banner_scripts' ) ); + add_action( 'admin_print_styles', array( Jetpack::init(), 'admin_banner_styles' ) ); + add_action( 'admin_notices', array( 'Jetpack_Connection_Banner', 'render_connect_prompt_full_screen' ) ); + delete_transient( 'activated_jetpack' ); + } // Check if the site plan changed and deactivate modules accordingly. add_action( 'current_screen', array( $this, 'check_plan_deactivate_modules' ) ); @@ -72,12 +85,12 @@ abstract class Jetpack_Admin_Page { function admin_head() { if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) { /** - * Fires in the <head> of a particular Jetpack configuation page. + * Fires in the <head> of a particular Jetpack configuration page. * * The dynamic portion of the hook name, `$_GET['configure']`, * refers to the slug of module, such as 'stats', 'sso', etc. * A complete hook for the latter would be - * 'jetpack_module_configuation_head_sso'. + * 'jetpack_module_configuration_head_sso'. * * @since 3.0.0 */ @@ -130,18 +143,6 @@ abstract class Jetpack_Admin_Page { } /** - * Checks if WordPress version is too old to have REST API. - * - * @since 4.3 - * - * @return bool - */ - function is_wp_version_too_old() { - global $wp_version; - return ( ! function_exists( 'rest_api_init' ) || version_compare( $wp_version, '4.4-z', '<=' ) ); - } - - /** * Checks if REST API is enabled. * * @since 4.4.2 @@ -184,7 +185,7 @@ abstract class Jetpack_Admin_Page { return false; } - $current = Jetpack::get_active_plan(); + $current = Jetpack_Plan::get(); $to_deactivate = array(); if ( isset( $current['product_slug'] ) ) { @@ -206,7 +207,7 @@ abstract class Jetpack_Admin_Page { $to_leave_enabled = array(); foreach ( $to_deactivate as $feature ) { - if ( Jetpack::active_plan_supports( $feature ) ) { + if ( Jetpack_Plan::supports( $feature ) ) { $to_leave_enabled []= $feature; } } @@ -231,9 +232,9 @@ abstract class Jetpack_Admin_Page { padding-left: 0 !important; } #wpbody-content { - background-color: #f3f6f8; + background-color: #f6f6f6; } - + #jp-plugin-container .wrap { margin: 0 auto; max-width:45rem; @@ -242,6 +243,9 @@ abstract class Jetpack_Admin_Page { #jp-plugin-container.is-wide .wrap { max-width: 1040px; } + #jp-plugin-container .wrap .jetpack-wrap-container { + margin-top: 1em; + } .wp-admin #dolly { float: none; position: relative; diff --git a/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-react-page.php b/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-react-page.php index 45f7d1e5..f84b62c3 100644 --- a/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-react-page.php +++ b/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-react-page.php @@ -25,8 +25,8 @@ class Jetpack_React_Page extends Jetpack_Admin_Page { return; // No need to handle the fallback redirection if we are not on the Jetpack page } - // Adding a redirect meta tag for older WordPress versions or if the REST API is disabled - if ( $this->is_wp_version_too_old() || ! $this->is_rest_api_enabled() ) { + // Adding a redirect meta tag if the REST API is disabled + if ( ! $this->is_rest_api_enabled() ) { $this->is_redirecting = true; add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) ); } @@ -169,18 +169,25 @@ class Jetpack_React_Page extends Jetpack_Admin_Page { function page_admin_scripts() { if ( $this->is_redirecting || isset( $_GET['configure'] ) ) { - return; // No need for scripts on a fallback page + return; // No need for scripts on a fallback page. } - // Enqueue jp.js and localize it - wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); + wp_enqueue_script( + 'react-plugin', + plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), + array( 'wp-i18n' ), + JETPACK__VERSION, + true + ); + + wp_set_script_translations( 'react-plugin', 'jetpack', JETPACK__PLUGIN_DIR . 'languages/json' ); if ( ! Jetpack::is_development_mode() && Jetpack::is_active() ) { - // Required for Analytics + // Required for Analytics. wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); } - // Add objects to be passed to the initial state of the app + // Add objects to be passed to the initial state of the app. wp_localize_script( 'react-plugin', 'Initial_State', $this->get_initial_state() ); } @@ -231,6 +238,11 @@ class Jetpack_React_Page extends Jetpack_Admin_Page { ? get_permalink( $last_post[0]->ID ) : get_home_url(); + // Ensure that class to get the affiliate code is loaded + if ( ! class_exists( 'Jetpack_Affiliate' ) ) { + require_once JETPACK__PLUGIN_DIR . 'class.jetpack-affiliate.php'; + } + return array( 'WP_API_root' => esc_url_raw( rest_url() ), 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), @@ -252,7 +264,7 @@ class Jetpack_React_Page extends Jetpack_Admin_Page { 'dismissedNotices' => $this->get_dismissed_jetpack_notices(), 'isDevVersion' => Jetpack::is_development_version(), 'currentVersion' => JETPACK__VERSION, - 'is_gutenberg_available' => Jetpack_Gutenberg::is_gutenberg_available(), + 'is_gutenberg_available' => true, 'getModules' => $modules, 'showJumpstart' => jetpack_show_jumpstart(), 'rawUrl' => Jetpack::build_raw_urls( get_home_url() ), @@ -267,6 +279,7 @@ class Jetpack_React_Page extends Jetpack_Admin_Page { ), 'roles' => $stats_roles, ), + 'aff' => Jetpack_Affiliate::init()->get_affiliate_code(), 'settings' => $this->get_flattened_settings( $modules ), 'userData' => array( // 'othersLinked' => Jetpack::get_other_linked_admins(), @@ -286,7 +299,7 @@ class Jetpack_React_Page extends Jetpack_Admin_Page { */ 'showPromotions' => apply_filters( 'jetpack_show_promotions', true ), 'isAtomicSite' => jetpack_is_atomic_site(), - 'plan' => Jetpack::get_active_plan(), + 'plan' => Jetpack_Plan::get(), 'showBackups' => Jetpack::show_backups_ui(), ), 'themeData' => array( diff --git a/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-settings-page.php b/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-settings-page.php index 13854cc7..35369ada 100644 --- a/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-settings-page.php +++ b/plugins/jetpack/_inc/lib/admin-pages/class.jetpack-settings-page.php @@ -29,7 +29,7 @@ class Jetpack_Settings_Page extends Jetpack_Admin_Page { // We have static.html so let's continue trying to fetch the others $noscript_notice = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static-noscript-notice.html' ); - $version_notice = $rest_api_notice = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static-version-notice.html' ); + $rest_api_notice = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static-version-notice.html' ); $ie_notice = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static-ie-notice.html' ); $noscript_notice = str_replace( @@ -43,17 +43,6 @@ class Jetpack_Settings_Page extends Jetpack_Admin_Page { $noscript_notice ); - $version_notice = str_replace( - '#HEADER_TEXT#', - esc_html__( 'You are using an outdated version of WordPress', 'jetpack' ), - $version_notice - ); - $version_notice = str_replace( - '#TEXT#', - esc_html__( "Update WordPress to unlock Jetpack's full potential!", 'jetpack' ), - $version_notice - ); - $rest_api_notice = str_replace( '#HEADER_TEXT#', esc_html( __( 'WordPress REST API is disabled', 'jetpack' ) ), @@ -76,9 +65,6 @@ class Jetpack_Settings_Page extends Jetpack_Admin_Page { $ie_notice ); - if ( $this->is_wp_version_too_old() ) { - echo $version_notice; - } if ( ! $this->is_rest_api_enabled() ) { echo $rest_api_notice; } diff --git a/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php b/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php index 6af6a1ef..ba57e923 100644 --- a/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php +++ b/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php @@ -112,6 +112,13 @@ class Jetpack_Core_Json_Api_Endpoints { 'permission_callback' => __CLASS__ . '::manage_modules_permission_check', ) ); + // Endpoint specific for privileged servers to request detailed debug information. + register_rest_route( 'jetpack/v4', '/connection/test-wpcom/', array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => __CLASS__ . '::jetpack_connection_test_for_external', + 'permission_callback' => __CLASS__ . '::view_jetpack_connection_test_check', + ) ); + register_rest_route( 'jetpack/v4', '/rewind', array( 'methods' => WP_REST_Server::READABLE, 'callback' => __CLASS__ . '::get_rewind_data', @@ -184,13 +191,6 @@ class Jetpack_Core_Json_Api_Endpoints { 'permission_callback' => array( $site_endpoint , 'can_request' ), ) ); - // Get related posts of a certain site post - register_rest_route( 'jetpack/v4', '/site/posts/related', array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $site_endpoint, 'get_related_posts' ), - 'permission_callback' => array( $site_endpoint , 'can_request' ), - ) ); - // Confirm that a site in identity crisis should be in staging mode register_rest_route( 'jetpack/v4', '/identity-crisis/confirm-safe-mode', array( 'methods' => WP_REST_Server::EDITABLE, @@ -437,7 +437,7 @@ class Jetpack_Core_Json_Api_Endpoints { array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => __CLASS__ . '::update_service_api_key', - 'permission_callback' => __CLASS__ . '::edit_others_posts_check', + 'permission_callback' => array( 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys','edit_others_posts_check' ), 'args' => array( 'service_api_key' => array( 'required' => true, @@ -448,7 +448,7 @@ class Jetpack_Core_Json_Api_Endpoints { array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => __CLASS__ . '::delete_service_api_key', - 'permission_callback' => __CLASS__ . '::edit_others_posts_check', + 'permission_callback' => array( 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys','edit_others_posts_check' ), ), ) ); @@ -916,40 +916,131 @@ class Jetpack_Core_Json_Api_Endpoints { } /** - * Test connection status for this Jetpack site. It uses the /jetpack-blogs/%d/test-connection wpcom endpoint. + * Test connection status for this Jetpack site. * * @since 6.8.0 * * @return array|WP_Error WP_Error returned if connection test does not succeed. */ public static function jetpack_connection_test() { - $response = Jetpack_Client::wpcom_json_api_request_as_blog( - sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ), - Jetpack_Client::WPCOM_JSON_API_VERSION + jetpack_require_lib( 'debugger' ); + $cxntests = new Jetpack_Cxn_Tests(); + + if ( $cxntests->pass() ) { + return rest_ensure_response( + array( + 'code' => 'success', + 'message' => __( 'All connection tests passed.', 'jetpack' ), + ) + ); + } else { + return $cxntests->output_fails_as_wp_error(); + } + } + + /** + * Test connection permission check method. + * + * @since 7.1.0 + * + * @return bool + */ + public static function view_jetpack_connection_test_check() { + if ( ! isset( $_GET['signature'], $_GET['timestamp'], $_GET['url'] ) ) { + return false; + } + $signature = base64_decode( $_GET['signature'] ); + + $signature_data = wp_json_encode( + array( + 'rest_route' => $_GET['rest_route'], + 'timestamp' => intval( $_GET['timestamp'] ), + 'url' => wp_unslash( $_GET['url'] ), + ) ); - if ( is_wp_error( $response ) ) { - /* translators: %1$s is the error code, %2$s is the error message */ - return new WP_Error( 'connection_test_failed', sprintf( __( 'Connection test failed (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ), array( 'status' => $response->get_error_code() ) ); + if ( + ! function_exists( 'openssl_verify' ) + || ! openssl_verify( + $signature_data, + $signature, + JETPACK__DEBUGGER_PUBLIC_KEY + ) + ) { + return false; } - $body = wp_remote_retrieve_body( $response ); - if ( ! $body ) { - return new WP_Error( 'connection_test_failed', __( 'Connection test failed (empty response body)', 'jetpack' ), array( 'status' => $response->get_error_code() ) ); + // signature timestamp must be within 5min of current time + if ( abs( time() - intval( $_GET['timestamp'] ) ) > 300 ) { + return false; } - $result = json_decode( $body ); - $is_connected = (bool) $result->connected; - $message = $result->message; + return true; + } - if ( $is_connected ) { - return rest_ensure_response( array( - 'code' => 'success', - 'message' => $message, - ) ); + /** + * Test connection status for this Jetpack site, encrypt the results for decryption by a third-party. + * + * @since 7.1.0 + * + * @return array|mixed|object|WP_Error + */ + public static function jetpack_connection_test_for_external() { + // Since we are running this test for inclusion in the WP.com testing suite, let's not try to run them as part of these results. + add_filter( 'jetpack_debugger_run_self_test', '__return_false' ); + jetpack_require_lib( 'debugger' ); + $cxntests = new Jetpack_Cxn_Tests(); + + if ( $cxntests->pass() ) { + $result = array( + 'code' => 'success', + 'message' => __( 'All connection tests passed.', 'jetpack' ), + ); } else { - return new WP_Error( 'connection_test_failed', $message, array( 'status' => $response->get_error_code() ) ); + $error = $cxntests->output_fails_as_wp_error(); // Using this so the output is similar both ways. + $errors = array(); + + // Borrowed from WP_REST_Server::error_to_response(). + foreach ( (array) $error->errors as $code => $messages ) { + foreach ( (array) $messages as $message ) { + $errors[] = array( + 'code' => $code, + 'message' => $message, + 'data' => $error->get_error_data( $code ), + ); + } + } + + $result = $errors[0]; + if ( count( $errors ) > 1 ) { + // Remove the primary error. + array_shift( $errors ); + $result['additional_errors'] = $errors; + } + } + + $result = wp_json_encode( $result ); + + $encrypted = $cxntests->encrypt_string_for_wpcom( $result ); + + if ( ! $encrypted || ! is_array( $encrypted ) ) { + return rest_ensure_response( + array( + 'code' => 'action_required', + 'message' => 'Please request results from the in-plugin debugger', + ) + ); } + + return rest_ensure_response( + array( + 'code' => 'response', + 'debug' => array( + 'data' => $encrypted['data'], + 'key' => $encrypted['key'], + ), + ) + ); } public static function rewind_data() { @@ -1252,19 +1343,8 @@ class Jetpack_Core_Json_Api_Endpoints { return new WP_Error( 'site_data_fetch_failed' ); } - // Save plan details in the database for future use without API calls - $results = json_decode( $response['body'], true ); - - if ( is_array( $results ) && isset( $results['plan'] ) ) { + Jetpack_Plan::update_from_sites_response( $response ); - // Set flag for newly purchased plan - $current_plan = Jetpack::get_active_plan(); - if ( $current_plan['product_slug'] !== $results['plan']['product_slug'] && 'jetpack_free' !== $results['plan']['product_slug'] ) { - update_option( 'show_welcome_for_new_plan', true ) ; - } - - update_option( 'jetpack_active_plan', $results['plan'] ); - } $body = wp_remote_retrieve_body( $response ); return json_decode( $body ); @@ -1510,7 +1590,6 @@ class Jetpack_Core_Json_Api_Endpoints { $visible = array( 'twitter', 'facebook', - 'google-plus-1', ); $hidden = array(); @@ -3080,9 +3159,9 @@ class Jetpack_Core_Json_Api_Endpoints { return array(); } - /** - * Get third party plugin API keys. + * Deprecated - Get third party plugin API keys. + * @deprecated * * @param WP_REST_Request $request { * Array of parameters received by request. @@ -3091,22 +3170,13 @@ class Jetpack_Core_Json_Api_Endpoints { * } */ public static function get_service_api_key( $request ) { - $service = self::validate_service_api_service( $request['service'] ); - if ( ! $service ) { - return self::service_api_invalid_service_response(); - } - $option = self::key_for_api_service( $service ); - $message = esc_html__( 'API key retrieved successfully.', 'jetpack' ); - return array( - 'code' => 'success', - 'service' => $service, - 'service_api_key' => Jetpack_Options::get_option( $option, '' ), - 'message' => $message, - ); + _deprecated_function( __METHOD__, 'jetpack-6.9.0', 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys::get_service_api_key' ); + return WPCOM_REST_API_V2_Endpoint_Service_API_Keys::get_service_api_key( $request ); } /** - * Update third party plugin API keys. + * Deprecated - Update third party plugin API keys. + * @deprecated * * @param WP_REST_Request $request { * Array of parameters received by request. @@ -3115,29 +3185,13 @@ class Jetpack_Core_Json_Api_Endpoints { * } */ public static function update_service_api_key( $request ) { - $service = self::validate_service_api_service( $request['service'] ); - if ( ! $service ) { - return self::service_api_invalid_service_response(); - } - $params = $request->get_json_params(); - $service_api_key = trim( $params['service_api_key'] ); - $option = self::key_for_api_service( $service ); - $validation = self::validate_service_api_key( $service_api_key, $service ); - if ( ! $validation['status'] ) { - return new WP_Error( 'invalid_key', esc_html__( 'Invalid API Key', 'jetpack' ), array( 'status' => 404 ) ); - } - $message = esc_html__( 'API key updated successfully.', 'jetpack' ); - Jetpack_Options::update_option( $option, $service_api_key ); - return array( - 'code' => 'success', - 'service' => $service, - 'service_api_key' => Jetpack_Options::get_option( $option, '' ), - 'message' => $message, - ); + _deprecated_function( __METHOD__, 'jetpack-6.9.0', 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys::update_service_api_key' ); + return WPCOM_REST_API_V2_Endpoint_Service_API_Keys::update_service_api_key( $request ) ; } /** - * Delete a third party plugin API key. + * Deprecated - Delete a third party plugin API key. + * @deprecated * * @param WP_REST_Request $request { * Array of parameters received by request. @@ -3146,114 +3200,57 @@ class Jetpack_Core_Json_Api_Endpoints { * } */ public static function delete_service_api_key( $request ) { - $service = self::validate_service_api_service( $request['service'] ); - if ( ! $service ) { - return self::service_api_invalid_service_response(); - } - $option = self::key_for_api_service( $service ); - Jetpack_Options::delete_option( $option ); - $message = esc_html__( 'API key deleted successfully.', 'jetpack' ); - return array( - 'code' => 'success', - 'service' => $service, - 'service_api_key' => Jetpack_Options::get_option( $option, '' ), - 'message' => $message, - ); + _deprecated_function( __METHOD__, 'jetpack-6.9.0', 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys::delete_service_api_key' ); + return WPCOM_REST_API_V2_Endpoint_Service_API_Keys::delete_service_api_key( $request ); } /** - * Validate the service provided in /service-api-keys/ endpoints. + * Deprecated - Validate the service provided in /service-api-keys/ endpoints. * To add a service to these endpoints, add the service name to $valid_services * and add '{service name}_api_key' to the non-compact return array in get_option_names(), * in class-jetpack-options.php + * @deprecated * * @param string $service The service the API key is for. * @return string Returns the service name if valid, null if invalid. */ public static function validate_service_api_service( $service = null ) { - $valid_services = array( - 'mapbox', - ); - return in_array( $service, $valid_services, true ) ? $service : null; + _deprecated_function( __METHOD__, 'jetpack-6.9.0', 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys::validate_service_api_service' ); + return WPCOM_REST_API_V2_Endpoint_Service_API_Keys::validate_service_api_service( $service ); } /** * Error response for invalid service API key requests with an invalid service. */ public static function service_api_invalid_service_response() { - return new WP_Error( - 'invalid_service', - esc_html__( 'Invalid Service', 'jetpack' ), - array( 'status' => 404 ) - ); + _deprecated_function( __METHOD__, 'jetpack-6.9.0', 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys::service_api_invalid_service_response' ); + return WPCOM_REST_API_V2_Endpoint_Service_API_Keys::service_api_invalid_service_response(); } /** - * Validate API Key + * Deprecated - Validate API Key + * @deprecated * * @param string $key The API key to be validated. * @param string $service The service the API key is for. + * */ public static function validate_service_api_key( $key = null, $service = null ) { - $validation = false; - switch ( $service ) { - case 'mapbox': - $validation = self::validate_service_api_key_mapbox( $key ); - break; - } - return $validation; + _deprecated_function( __METHOD__, 'jetpack-6.9.0', 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys::validate_service_api_key' ); + return WPCOM_REST_API_V2_Endpoint_Service_API_Keys::validate_service_api_key( $key , $service ); } /** - * Validate Mapbox API key + * Deprecated - Validate Mapbox API key * Based loosely on https://github.com/mapbox/geocoding-example/blob/master/php/MapboxTest.php + * @deprecated * * @param string $key The API key to be validated. */ public static function validate_service_api_key_mapbox( $key ) { - $status = true; - $msg = null; - $mapbox_url = sprintf( - 'https://api.mapbox.com?%s', - $key - ); - $mapbox_response = wp_safe_remote_get( esc_url_raw( $mapbox_url ) ); - $mapbox_body = wp_remote_retrieve_body( $mapbox_response ); - if ( '{"api":"mapbox"}' !== $mapbox_body ) { - $status = false; - $msg = esc_html__( 'Can\'t connect to Mapbox', 'jetpack' ); - return array( - 'status' => $status, - 'error_message' => $msg, - ); - } - $mapbox_geocode_url = esc_url_raw( - sprintf( - 'https://api.mapbox.com/geocoding/v5/mapbox.places/%s.json?access_token=%s', - '1+broadway+new+york+ny+usa', - $key - ) - ); - $mapbox_geocode_response = wp_safe_remote_get( esc_url_raw( $mapbox_geocode_url ) ); - $mapbox_geocode_body = wp_remote_retrieve_body( $mapbox_geocode_response ); - $mapbox_geocode_json = json_decode( $mapbox_geocode_body ); - if ( isset( $mapbox_geocode_json->message ) && ! isset( $mapbox_geocode_json->query ) ) { - $status = false; - $msg = $mapbox_geocode_json->message; - } - return array( - 'status' => $status, - 'error_message' => $msg, - ); - } + _deprecated_function( __METHOD__, 'jetpack-6.9.0', 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys::validate_service_api_key' ); + return WPCOM_REST_API_V2_Endpoint_Service_API_Keys::validate_service_api_key_mapbox( $key ); - /** - * Create site option key for service - * - * @param string $service The service to create key for. - */ - private static function key_for_api_service( $service ) { - return $service . '_api_key'; } /** diff --git a/plugins/jetpack/_inc/lib/class.jetpack-password-checker.php b/plugins/jetpack/_inc/lib/class.jetpack-password-checker.php new file mode 100644 index 00000000..14de6053 --- /dev/null +++ b/plugins/jetpack/_inc/lib/class.jetpack-password-checker.php @@ -0,0 +1,1288 @@ +<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName +/** + * The password strength checker. + * + * @package jetpack + */ + +/** + * Checks passwords strength. + */ +class Jetpack_Password_Checker { + + /** + * Minimum entropy bits a password should contain. 36 bits of entropy is considered + * to be a reasonable password, 28 stands for a weak one. + * + * @const Integer + */ + const MINIMUM_BITS = 28; + + /** + * Currently tested password. + * + * @var String + */ + public $password = ''; + + /** + * Test results array. + * + * @var Array + */ + public $test_results = ''; + + /** + * Current password score. + * + * @var Integer + */ + public $score = 0; + + /** + * Current multiplier affecting the score. + * + * @var Integer + */ + public $multiplier = 4; + + /** + * A common password blacklist, which on match will immediately disqualify the password. + * + * @var Array + */ + public $common_passwords = array(); + + /** + * Minimum password length setting. + * + * @var Integer + */ + public $min_password_length = 6; + + /** + * User defined strings that passwords need to be tested for a match against. + * + * @var Array + */ + private $user_strings_to_test = array(); + + /** + * The user object for whom the password is being tested. + * + * @var WP_User + */ + protected $user; + + /** + * The user identifier for whom the password is being tested, used if there's no user object. + * + * @var WP_User + */ + protected $user_id; + + /** + * Creates an instance of the password checker class for the specified user, or + * defaults to the currently logged in user. + * + * @param Mixed $user can be an integer ID, or a WP_User object. + */ + public function __construct( $user = null ) { + + /** + * Filters Jetpack's password strength enforcement settings. You can supply your own passwords + * that should not be used for authenticating in addition to weak and easy to guess strings for + * each user. For example, you can add passwords from known password databases to avoid compromised + * password usage. + * + * @since 7.2.0 + * + * @param Array $restricted_passwords strings that are forbidden for use as passwords. + */ + $this->common_passwords = apply_filters( 'jetpack_password_checker_restricted_strings', array() ); + + if ( is_null( $user ) ) { + $this->user_id = get_current_user_id(); + } elseif ( is_object( $user ) && isset( $user->ID ) ) { + + // Existing user, using their ID. + $this->user_id = $user->ID; + + } elseif ( is_object( $user ) ) { + + // Newly created user, using existing data. + $this->user = $user; + $this->user_id = 'new_user'; + + } else { + $this->user_id = $user; + } + $this->min_password_length = apply_filters( 'better_password_min_length', $this->min_password_length ); + } + + /** + * Run tests against a password. + * + * @param String $password the tested string. + * @param Boolean $required_only only test against required conditions, defaults to false. + * @return Array $results an array containing failed and passed test results. + */ + public function test( $password, $required_only = false ) { + + $this->password = $password; + $results = $this->run_tests( $this->list_tests(), $required_only ); + + // If we've failed on the required tests, return now. + if ( ! empty( $results['failed'] ) ) { + return array( + 'passed' => false, + 'test_results' => $results, + ); + } + + /** + * Filters Jetpack's password strength enforcement settings. You can modify the minimum + * entropy bits requirement using this filter. + * + * @since 7.2.0 + * + * @param Array $minimum_entropy_bits minimum entropy bits requirement. + */ + $bits = apply_filters( 'jetpack_password_checker_minimum_entropy_bits', self::MINIMUM_BITS ); + $entropy_bits = $this->calculate_entropy_bits( $this->password ); + + // If we have failed the entropy bits test, run the regex tests so we can suggest improvements. + if ( $entropy_bits < $bits ) { + $results['failed']['entropy_bits'] = $entropy_bits; + $results = array_merge( + $results, + $this->run_tests( $this->list_tests( 'preg_match' ), false ) + ); + } + + return( array( + 'passed' => empty( $results['failed'] ), + 'test_results' => $results, + ) ); + } + + /** + * Run the tests using the currently set up object values. + * + * @param Array $tests tests to run. + * @param Boolean $required_only whether to run only required tests. + * @return Array test results. + */ + protected function run_tests( $tests, $required_only = false ) { + + $results = array( + 'passed' => array(), + 'failed' => array(), + ); + + foreach ( $tests as $test_type => $section_tests ) { + foreach ( $section_tests as $test_name => $test_data ) { + + // Skip non-required tests if required_only param is set. + if ( $required_only && ! $test_data['required'] ) { + continue; + } + + $test_function = 'test_' . $test_type; + + $result = call_user_func( array( $this, $test_function ), $test_data ); + + if ( $result ) { + $results['passed'][] = array( 'test_name' => $test_name ); + } else { + $results['failed'][] = array( + 'test_name' => $test_name, + 'explanation' => $test_data['error'], + ); + + if ( isset( $test_data['fail_immediately'] ) ) { + return $results; + } + } + } + } + + return $results; + } + + /** + * Returns a list of tests that need to be run on password strings. + * + * @param Array $sections only return specific sections with the passed keys, defaults to all. + * @return Array test descriptions. + */ + protected function list_tests( $sections = false ) { + // Note: these should be in order of priority. + $tests = array( + 'preg_match' => array( + 'no_backslashes' => array( + 'pattern' => '^[^\\\\]*$', + 'error' => __( 'Passwords may not contain the character "\".', 'jetpack' ), + 'required' => true, + 'fail_immediately' => true, + ), + 'minimum_length' => array( + 'pattern' => '^.{' . $this->min_password_length . ',}', + /* translators: %d is a number of characters in the password. */ + 'error' => sprintf( __( 'Password must be at least %d characters.', 'jetpack' ), $this->min_password_length ), + 'required' => true, + 'fail_immediately' => true, + ), + 'has_mixed_case' => array( + 'pattern' => '([a-z].*?[A-Z]|[A-Z].*?[a-z])', + 'error' => __( 'This password is too easy to guess: you can improve it by adding additional uppercase letters, lowercase letters, or numbers.', 'jetpack' ), + 'trim' => true, + 'required' => false, + ), + 'has_digit' => array( + 'pattern' => '\d', + 'error' => __( 'This password is too easy to guess: you can improve it by mixing both letters and numbers.', 'jetpack' ), + 'trim' => false, + 'required' => false, + ), + 'has_special_char' => array( + 'pattern' => '[^a-zA-Z\d]', + 'error' => __( 'This password is too easy to guess: you can improve it by including special characters such as !#=?*&.', 'jetpack' ), + 'required' => false, + ), + ), + 'compare_to_list' => array( + 'not_a_common_password' => array( + 'list_callback' => 'get_common_passwords', + 'compare_callback' => 'negative_in_array', + 'error' => __( 'This is a very common password. Choose something that will be harder for others to guess.', 'jetpack' ), + 'required' => true, + ), + 'not_same_as_other_user_data' => array( + 'list_callback' => 'get_other_user_data', + 'compare_callback' => 'test_not_same_as_other_user_data', + 'error' => __( 'Your password is too weak: Looks like you\'re including easy to guess information about yourself. Try something a little more unique.', 'jetpack' ), + 'required' => true, + ), + ), + ); + + /** + * Filters Jetpack's password strength enforcement settings. You can determine the tests run + * and their order based on whatever criteria you wish to specify. + * + * @since 7.2.0 + * + * @param Array $minimum_entropy_bits minimum entropy bits requirement. + */ + $tests = apply_filters( 'jetpack_password_checker_tests', $tests ); + + if ( ! $sections ) { + return $tests; + } + + $sections = (array) $sections; + return array_intersect_key( $tests, array_flip( $sections ) ); + } + + /** + * Provides the regular expression tester functionality. + * + * @param Array $test_data the current test data. + * @return Boolean does the test pass? + */ + protected function test_preg_match( $test_data ) { + $password = stripslashes( $this->password ); + + if ( isset( $test_data['trim'] ) ) { + $password = substr( $password, 1, -1 ); + } + + if ( ! preg_match( '/' . $test_data['pattern'] . '/u', $password ) ) { + return false; + } + + return true; + } + + /** + * Provides the comparison tester functionality. + * + * @param Array $test_data the current test data. + * @return Boolean does the test pass? + */ + protected function test_compare_to_list( $test_data ) { + $list_callback = $test_data['list_callback']; + $compare_callback = $test_data['compare_callback']; + + if ( + ! is_callable( array( $this, $list_callback ) ) + || ! is_callable( array( $this, $compare_callback ) ) + ) { + return false; + } + + $list = call_user_func( array( $this, $list_callback ) ); + if ( empty( $list ) ) { + return true; + } + + return call_user_func( array( $this, $compare_callback ), $this->password, $list ); + } + + /** + * Getter for the common password list. + * + * @return Array common passwords. + */ + protected function get_common_passwords() { + return $this->common_passwords; + } + + /** + * Returns the widely known user data that can not be used in the password to avoid + * predictable strings. + * + * @return Array user data. + */ + protected function get_other_user_data() { + + if ( ! isset( $this->user ) ) { + $user_data = get_userdata( $this->user_id ); + + $first_name = get_user_meta( $user_data->ID, 'first_name', true ); + $last_name = get_user_meta( $user_data->ID, 'last_name', true ); + $nickname = get_user_meta( $user_data->ID, 'nickname', true ); + + $this->add_user_strings_to_test( $nickname ); + $this->add_user_strings_to_test( $user_data->user_nicename ); + $this->add_user_strings_to_test( $user_data->display_name ); + } else { + $user_data = $this->user; + + $first_name = $user_data->first_name; + $last_name = $user_data->last_name; + } + $email_username = substr( $user_data->user_email, 0, strpos( $user_data->user_email, '@' ) ); + + $this->add_user_strings_to_test( $user_data->user_email ); + $this->add_user_strings_to_test( $email_username, '.' ); + $this->add_user_strings_to_test( $first_name ); + $this->add_user_strings_to_test( $last_name ); + + return $this->user_strings_to_test; + } + + /** + * Compare the password for matches with known user data. + * + * @param String $password the string to be tested. + * @param Array $strings_to_test known user data. + * @return Boolean does the test pass? + */ + protected function test_not_same_as_other_user_data( $password, $strings_to_test ) { + $password_lowercase = strtolower( $password ); + foreach ( array_unique( $strings_to_test ) as $string ) { + if ( empty( $string ) ) { + continue; + } + + $string = strtolower( $string ); + $string_reversed = strrev( $string ); + + if ( $password_lowercase === $string || $password_lowercase === $string_reversed ) { + return false; + } + + // Also check for the string or reversed string with any numbers just stuck to the end to catch things like bob123 as passwords. + if ( + preg_match( '/^' . preg_quote( $string, '/' ) . '\d+$/', $password_lowercase ) + || preg_match( '/^' . preg_quote( $string_reversed, '/' ) . '\d+$/', $password_lowercase ) + ) { + return false; + } + } + return true; + } + + /** + * A shorthand for the not in array construct. + * + * @param Mixed $needle the needle. + * @param Array $haystack the haystack. + * @return is the needle not in the haystack? + */ + protected function negative_in_array( $needle, $haystack ) { + if ( in_array( $needle, $haystack, true ) ) { + return false; + } + + return true; + } + + /** + * A helper function used to break a single string into its constituents so + * that both the full string and its constituents and any variants thereof + * can be tested against the password. + * + * @param String $string the string to be broken down. + * @param String $explode_delimiter delimiter. + * @return NULL|Array array of fragments, or NULL on empty string. + */ + protected function add_user_strings_to_test( $string, $explode_delimiter = ' ' ) { + + // Don't check against empty strings. + if ( empty( $string ) ) { + return; + } + + $strings = explode( $explode_delimiter, $string ); + + // Remove any non alpha numeric characters from the strings to check against. + foreach ( $strings as $key => $_string ) { + $strings[ $key ] = preg_replace( '/[^a-zA-Z0-9]/', '', $_string ); + } + + // Check the original too. + $strings[] = $string; + + // Check the original minus non alpha numeric characters. + $strings[] = preg_replace( '/[^a-zA-Z0-9]/', '', $string ); + + // Remove any empty strings. + $strings = array_filter( $strings ); + $this->user_strings_to_test = array_merge( $this->user_strings_to_test, $strings ); + } + + /** + * Return a character set size that is used in the string. + * + * @param String $password the password. + * @return Integer number of different character sets in use. + */ + protected function get_charset_size( $password ) { + $size = 0; + + // Lowercase a-z. + if ( preg_match( '/[a-z]/', $password ) ) { + $size += 26; + } + + // Uppercase A-Z. + if ( preg_match( '/[A-Z]/', substr( $password, 1, -1 ) ) ) { + $size += 26; + } + + // Digits. + if ( preg_match( '/\d/', substr( $password, 1, -1 ) ) ) { + $size += 10; + } + + // Over digits symbols. + if ( preg_match( '/[!|@|#|$|%|^|&|*|(|)]/', $password ) ) { + $size += 10; + } + + // Other symbols. + if ( preg_match( '#[`|~|-|_|=|+|\[|{|\]|}|\\|\|;:\'",<\.>/\?]#', $password ) ) { + $size += 20; + } + + // Spaces. + if ( strpos( $password, ' ' ) ) { + $size++; + } + + return $size; + } + + /** + * Shorthand for getting a character index. + * + * @param String $char character. + * @return Integer the character code. + */ + protected function get_char_index( $char ) { + $char = strtolower( $char[0] ); + if ( $char < 'a' || $char > 'z' ) { + return 0; + } else { + return ord( $char[0] ) - ord( 'a' ) + 1; + } + } + + /** + * This is the password strength calculation algorithm, based on the formula H = L(logN/log2). + * + * H = Entropy + * L = String length (the for iterator) + * N = Our charset size, via get_charset_size() + * + * @see http://en.wikipedia.org/wiki/Password_strength#Random_passwords + * + * On top of the base formula, we're also multiplying the bits of entropy for every char + * by 1 - (the probabily of it following the previous char) + * i.e.: the probablity of U following Q is ~0.84. If our password contains this pair of characters, + * the u char will only add ( 0.16^2 * charset_score ) to our total of entropy bits. + * + * @param String $password the password. + */ + protected function calculate_entropy_bits( $password ) { + $bits = 0; + $charset_score = log( $this->get_charset_size( $password ) ) / log( 2 ); + + $aidx = $this->get_char_index( $password[0] ); + $length = strlen( $password ); + + for ( $b = 1; $b < $length; $b++ ) { + $bidx = $this->get_char_index( $password[ $b ] ); + + // 27 = number of chars in the index (a-z,' '). + $c = 1.0 - $this->frequency_table[ $aidx * 27 + $bidx ]; + $bits += $charset_score * $c * $c; + + // Move on to next pair. + $aidx = $bidx; + } + + return $bits; + } + + /** + * A frequency table of character pairs, starting with ' ' then ' a', ' b' [...] , 'a ', 'aa' etc. + * + * @see http://rumkin.com/tools/password/passchk.php + * @var Array + */ + public $frequency_table = array( + 0.23653710453418866, + 0.04577693541332556, + 0.03449832337075375, + 0.042918209651552706, + 0.037390873305146524, + 0.028509112115468728, + 0.02350896632162123, + 0.022188657238664526, + 0.028429800262428927, + 0.04357019973757107, + 0.00913602565971716, + 0.03223093745443942, + 0.02235311269864412, + 0.04438081352966905, + 0.04512377897652719, + 0.020055401662049863, + 0.055903192885260244, + 0.0024388394809739026, + 0.035207464644991984, + 0.07355941099285611, + 0.036905671380667734, + 0.026134421927394666, + 0.023787724158040528, + 0.011352092141711621, + 0.0032354570637119114, + 0.005986878553725033, + 0.008861933226417843, + 0.11511532293337222, + 0.027556203528211108, + 0.024331243621519172, + 0.039266365359381834, + 0.031599941682461, + 0.014403265782183991, + 0.015480973902901297, + 0.027770812071730572, + 0.00942761335471643, + 0.039872867764980315, + 0.0078122175244204695, + 0.02808456043154979, + 0.08429100451960927, + 0.04688963405744277, + 0.13831170724595424, + 0.002540311998833649, + 0.025211838460416972, + 0.001543082081936142, + 0.09519638431258201, + 0.061845750109345385, + 0.08907071001603732, + 0.02137571074500656, + 0.027093162268552268, + 0.005521504592506197, + 0.003023181221752442, + 0.007086747339262283, + 0.010262720513194342, + 0.08785070710016038, + 0.14617757690625455, + 0.03417291150313457, + 0.0059635515381250915, + 0.006146668610584633, + 0.195202799241872, + 0.002774748505613063, + 0.004715556203528212, + 0.0044776206444088066, + 0.11205481848665985, + 0.005654468581425864, + 0.0028820527773727946, + 0.07383000437381543, + 0.005516839189386207, + 0.006496573844583759, + 0.09843067502551392, + 0.0027140982650532145, + 0.0006893133109782768, + 0.08425368129464937, + 0.021325557661466685, + 0.006493074792243767, + 0.07023414491908442, + 0.002077270739174807, + 0.0024633328473538415, + 0.0007744569179180639, + 0.015413325557661468, + 0.0011990086018370024, + 0.13162851727657093, + 0.10115993585070711, + 0.0026989357049132527, + 0.03319317684793702, + 0.002946202070272634, + 0.0783216212275842, + 0.0018358361277154103, + 0.00258813238081353, + 0.2141688292754046, + 0.09853681294649366, + 0.0032482869222918796, + 0.04359352675317102, + 0.01993526753171016, + 0.0036880011663507797, + 0.008011663507799971, + 0.12014696019827964, + 0.0029846916460125384, + 0.0017553579238956116, + 0.029470185158186325, + 0.010413179763813967, + 0.030699518880303252, + 0.03508499781309229, + 0.002021285901734947, + 0.0010613792097973467, + 0.0005295232541186761, + 0.009677212421635807, + 0.010585799679253535, + 0.17101734946785244, + 0.07968625164018078, + 0.007839043592360402, + 0.005438693687126403, + 0.0183606939787141, + 0.2732701559994168, + 0.004953491762647616, + 0.007259367254701851, + 0.008104971570199739, + 0.13274588132380813, + 0.004210526315789474, + 0.004997813092287506, + 0.017006560723137484, + 0.007442484327161393, + 0.016789619478058026, + 0.08477737279486806, + 0.005106283714827234, + 0.0005026971861787433, + 0.04040355736987899, + 0.037535500801866156, + 0.00885960052485785, + 0.0336410555474559, + 0.007066919376002332, + 0.005344219273946639, + 0.0006333284735384167, + 0.010684939495553289, + 0.0063064586674442345, + 0.15386849394955532, + 0.015049424114302375, + 0.012162705933809595, + 0.020425134859308938, + 0.037366379938766583, + 0.02157165767604607, + 0.009373961218836564, + 0.0173214754337367, + 0.009616562181075958, + 0.029522670943286193, + 0.010154249890654615, + 0.018600962239393497, + 0.06362210234728094, + 0.03157078291296107, + 0.151603440734801, + 0.0062329785683044175, + 0.014775331681003062, + 0.0020854351946347867, + 0.1826342032366234, + 0.0878017203674005, + 0.054190989940224525, + 0.010329202507654177, + 0.012763376585508092, + 0.0064872430383437815, + 0.006381105117364048, + 0.005388540603586529, + 0.0090800408222773, + 0.09611196967487973, + 0.09940691062837148, + 0.01033969966467415, + 0.004034407348009914, + 0.008826942703017933, + 0.11474675608689314, + 0.07132584924916169, + 0.012388977985129028, + 0.005435194634786413, + 0.1417174515235457, + 0.0037066627788307337, + 0.0045802595130485495, + 0.060800699810468, + 0.005341886572386646, + 0.005683627350925791, + 0.12434932205860913, + 0.004596588423968508, + 0.0007534626038781163, + 0.07107041842834232, + 0.022361277154104096, + 0.04784720804782038, + 0.06277533168100306, + 0.003441901151771395, + 0.005828254847645429, + 0.0009669047966175828, + 0.009470768333576322, + 0.002077270739174807, + 0.12797667298440007, + 0.08797783933518005, + 0.005388540603586529, + 0.0024913252660737715, + 0.007550954949701123, + 0.2786866890217233, + 0.002509986878553725, + 0.029002478495407494, + 0.0303204548768042, + 0.07576614666861058, + 0.00246799825047383, + 0.00592389561160519, + 0.039574281965301064, + 0.00706808572678233, + 0.03304505029887739, + 0.05474150750838315, + 0.0028633911648928414, + 0.0005073625892987316, + 0.07293541332555767, + 0.053528502697186175, + 0.022566554891383584, + 0.038151334013704616, + 0.002716430966613209, + 0.005049132526607377, + 0.0009902318122175246, + 0.008997229916897508, + 0.0011861787432570347, + 0.1666377022889634, + 0.14414462749671964, + 0.003374252806531564, + 0.005169266656947077, + 0.008468873013558828, + 0.16337541915731155, + 0.002873888321912815, + 0.004305000728969237, + 0.0031141565825922144, + 0.1241172182533897, + 0.0052800699810468, + 0.008969237498177577, + 0.024094474413179766, + 0.017029887738737422, + 0.01722700102055693, + 0.10618457501093455, + 0.006147834961364631, + 0.0008269427030179326, + 0.03303571949263741, + 0.024188948826359528, + 0.05213937891820965, + 0.04505846333284735, + 0.0035270447587111824, + 0.006799825047383001, + 0.0008199445983379502, + 0.02206735675754483, + 0.001010059775477475, + 0.11971191135734072, + 0.04656538854060359, + 0.011243621519171892, + 0.06513019390581717, + 0.032375564951159064, + 0.06347047674588133, + 0.013678961947805804, + 0.03309870243475726, + 0.006982942119842543, + 0.009726199154395685, + 0.010121592068814697, + 0.032514360693978714, + 0.04986032949409535, + 0.039734072022160664, + 0.15690683773144773, + 0.03949963551538125, + 0.014790494241143023, + 0.002722262720513194, + 0.02614375273363464, + 0.10753637556495116, + 0.06764834523983088, + 0.006221315060504448, + 0.021317393206006705, + 0.0030826651115322934, + 0.002399183554454002, + 0.0019069835252952323, + 0.015595276279341012, + 0.0925126111678087, + 0.18437906400349907, + 0.006538562472663654, + 0.008719638431258201, + 0.02116693395538708, + 0.18241376293920394, + 0.007290858725761773, + 0.005976381396705059, + 0.005629975215045925, + 0.09721300481119698, + 0.004810030616707975, + 0.024303251202799244, + 0.012954658113427612, + 0.011057005394372358, + 0.02733459688001166, + 0.10135121737862662, + 0.012016912086309959, + 0.001055547455897361, + 0.009027555037177431, + 0.07162326869806095, + 0.01007143898527482, + 0.07297623560285756, + 0.006741507508383147, + 0.0036891675171307776, + 0.0008409389123778977, + 0.011272780288671819, + 0.007020265344802449, + 0.1030389269572824, + 0.15350809155853623, + 0.004232686980609419, + 0.004353987461729115, + 0.0023385333138941536, + 0.14450386353695874, + 0.002546143752733635, + 0.0024470039364338824, + 0.01200758128006998, + 0.0981227584195947, + 0.003161976964572095, + 0.040695145064878264, + 0.03460446129173349, + 0.003908441463770229, + 0.01598483743986004, + 0.13107216795451232, + 0.003129319142732177, + 0.00032307916605919226, + 0.04050386353695874, + 0.05452689896486368, + 0.03589677795597026, + 0.07087097244496282, + 0.006143169558244642, + 0.008684647907858289, + 0.0004607085580988482, + 0.022010205569324977, + 0.0009097536083977258, + 0.07328765126111678, + 0.14751421490013122, + 0.008015162560139961, + 0.006601545414783497, + 0.025279486805656802, + 0.1682449336637994, + 0.008313748359819215, + 0.007010934538562473, + 0.005886572386645284, + 0.16889575739903775, + 0.004123050007289692, + 0.011925936725470185, + 0.10007289692374982, + 0.013380376148126549, + 0.009021723283277445, + 0.08650823735238372, + 0.007756232686980609, + 0.0007243038343781893, + 0.0026791077416533026, + 0.02797492345823006, + 0.032384895757399036, + 0.04187432570345531, + 0.00882461000145794, + 0.0032401224668318998, + 0.00033357632307916605, + 0.027878116343490307, + 0.0022277299897944304, + 0.14333518005540166, + 0.1725534334451086, + 0.02781629975215046, + 0.006909462020702727, + 0.005264907420906838, + 0.16661437527336345, + 0.004325995043009185, + 0.003334596880011664, + 0.005312727802886718, + 0.14024668318996938, + 0.0013261408368566844, + 0.003504884093891238, + 0.006375273363464061, + 0.04964922000291588, + 0.008290421344219274, + 0.09536783787724158, + 0.05394372357486515, + 0.005505175681586237, + 0.005339553870826651, + 0.01782067356757545, + 0.006710016037323225, + 0.05105933809593235, + 0.002983525295232541, + 0.002940370316372649, + 0.0004548768041988629, + 0.01208456043154979, + 0.000915585362297711, + 0.20146260387811635, + 0.067196967487972, + 0.006158332118384605, + 0.025438110511736407, + 0.07753783350342616, + 0.1273876658405015, + 0.009337804344656656, + 0.07683452398308792, + 0.0070412596588423975, + 0.08747164309666132, + 0.0038827817466102928, + 0.018116926665694706, + 0.005017641055547455, + 0.004567429654468581, + 0.028277008310249308, + 0.05271555620352821, + 0.004394809739029013, + 0.0013343052923166642, + 0.00411605190260971, + 0.059621519171890944, + 0.09073859163143316, + 0.01446858142586383, + 0.006770666277883074, + 0.003425572240851436, + 0.0004455459979588861, + 0.010401516256013998, + 0.005825922146085436, + 0.10833882490158916, + 0.007584779122321038, + 0.016903921854497742, + 0.02719580113719201, + 0.0304814112844438, + 0.02206385770520484, + 0.013064295086747339, + 0.02696369733197259, + 0.009581571657676046, + 0.026761918647033093, + 0.006510570053943724, + 0.021941390873305145, + 0.07042659279778393, + 0.05437410701268406, + 0.1425175681586237, + 0.027802303542790494, + 0.037690625455605774, + 0.0019606356611750987, + 0.1095623268698061, + 0.06157748942994606, + 0.044618749088788455, + 0.04955124653739612, + 0.03608689313310978, + 0.018381688292754043, + 0.003404577926811489, + 0.015036594255722409, + 0.009600233270156, + 0.10794693103951014, + 0.12447528794284882, + 0.0031981338387520046, + 0.0074716430966613205, + 0.003202799241871993, + 0.13437643971424407, + 0.006655197550663361, + 0.0036693395538708266, + 0.049338970695436656, + 0.09486863974340283, + 0.0015990669193760023, + 0.0026604461291733486, + 0.051775477474850555, + 0.0041347135150896636, + 0.005450357194926374, + 0.12030325120279925, + 0.04581309228750547, + 0.0004537104534188657, + 0.12425601399620935, + 0.025981629975215047, + 0.023926519900860182, + 0.04423385333138941, + 0.0017950138504155123, + 0.002661612479953346, + 0.0006333284735384167, + 0.008449045050298877, + 0.000653156436798367, + 0.04816678816153958, + 0.008625164018078437, + 0.0039037760606502403, + 0.005228750546726928, + 0.004531272780288672, + 0.0056672984400058316, + 0.00359585945473101, + 0.0032179618020119548, + 0.0038093016474704767, + 0.011452398308791368, + 0.002519317684793702, + 0.00280390727511299, + 0.005572824026826068, + 0.004554599795888614, + 0.004531272780288672, + 0.0035841959469310393, + 0.004400641492928998, + 0.0036670068523108326, + 0.004839189386207902, + 0.006258638285464354, + 0.004897506925207757, + 0.840776789619478, + 0.004968654322787578, + 0.002886718180492783, + 0.0019757982213150604, + 0.0018568304417553576, + 0.001691208630995772, + 0.09009243329931477, + 0.14030150167662925, + 0.013242746756086894, + 0.013746610293045632, + 0.027342761335471644, + 0.16938912377897652, + 0.006607377168683481, + 0.01661933226417845, + 0.008173786266219566, + 0.13297448607668758, + 0.0034675608689313307, + 0.016641492928998396, + 0.011722991689750693, + 0.021493512173786266, + 0.03430820819361423, + 0.10099548039072752, + 0.00873596734217816, + 0.0018323370753754193, + 0.020103222044029742, + 0.047197550663362, + 0.040833940807697915, + 0.03361189677795597, + 0.010844729552412887, + 0.005544831608106138, + 0.0007522962530981193, + 0.01525120279924187, + 0.00815512465373961, + 0.2109648636827526, + 0.058258055110074355, + 0.007181221752442048, + 0.043560868931331105, + 0.004058900714389853, + 0.10618107595859454, + 0.0062399766729844, + 0.004835690333867911, + 0.02679224376731302, + 0.08414637702288964, + 0.0030698352529523252, + 0.03637498177576906, + 0.01592885260242018, + 0.017413617145356466, + 0.008430383437818923, + 0.037231083248286924, + 0.03290275550371775, + 0.007538125091121154, + 0.004500947660008748, + 0.05932409972299169, + 0.16006764834523984, + 0.03309636973319726, + 0.007766729844000583, + 0.005225251494386936, + 0.0006321621227584196, + 0.012989648636827526, + 0.005274238227146815, + 0.1254503571949264, + 0.12852719055255868, + 0.0035433736696311416, + 0.005203090829566993, + 0.0019314768916751715, + 0.20520775623268697, + 0.002509986878553725, + 0.00343606939787141, + 0.027138649948972155, + 0.13926578218399185, + 0.004565096952908587, + 0.005614812654905963, + 0.00874413179763814, + 0.004109053797929727, + 0.008300918501239247, + 0.08270943286193323, + 0.002912377897652719, + 0.0037066627788307337, + 0.06909578655780726, + 0.03242805073625893, + 0.05237614812654906, + 0.04723487388832191, + 0.0038991106575302524, + 0.006299460562764251, + 0.00043388249015891526, + 0.020029741944889927, + 0.005311561452106721, + 0.09334072022160665, + 0.022940953491762648, + 0.024658988190698353, + 0.02901297565242747, + 0.03531593526753171, + 0.0758023035427905, + 0.013711619769645722, + 0.021597317393206007, + 0.009670214316955824, + 0.044728386062108175, + 0.010596296836273509, + 0.03264382563055839, + 0.0604822860475288, + 0.05489546581134276, + 0.11501851581863246, + 0.01837585653885406, + 0.026237060796034405, + 0.0011255285026971862, + 0.08704125965884241, + 0.10156349322058608, + 0.06660562764251349, + 0.023434319871701415, + 0.010777081207173057, + 0.005409534917626476, + 0.003123487388832191, + 0.0028762210234728096, + 0.0089995626184575, + 0.07518297127861205, + 0.2314868056568013, + 0.002226563639014434, + 0.003285610147251786, + 0.0027455897361131363, + 0.2724537104534189, + 0.0016655489138358362, + 0.0019209797346551977, + 0.0022137337804344656, + 0.17690392185449774, + 0.0014532730718763668, + 0.0024994897215337513, + 0.015302522233561744, + 0.003441901151771395, + 0.015303688584341741, + 0.09314593964134713, + 0.0017833503426155418, + 0.0005108616416387229, + 0.017828838023035427, + 0.010385187345094037, + 0.003168975069252078, + 0.01902901297565243, + 0.005525003644846187, + 0.0010088934246974776, + 0.0009272488700976819, + 0.036282840064149294, + 0.0022977110365942554, + 0.0766805656801283, + 0.22270418428342326, + 0.005283569033386791, + 0.007155562035282111, + 0.01173582154833066, + 0.1715620352821111, + 0.003925936725470185, + 0.004425134859308937, + 0.020040239101909902, + 0.14243242455168392, + 0.0016737133692958156, + 0.0066808572678232975, + 0.011980755212130047, + 0.012638577052048404, + 0.07206065024055984, + 0.08115701997375711, + 0.00710424260096224, + 0.0007278028867181805, + 0.02347630849978131, + 0.04595538708266512, + 0.01481965301064295, + 0.013925061962385188, + 0.0018125091121154687, + 0.00529173348884677, + 0.0016340574427759146, + 0.03072401224668319, + 0.0023746901880740633, + 0.25174165330223064, + 0.06673392622831317, + 0.00878378772415804, + 0.03956261845750109, + 0.010077270739174807, + 0.0844787869951888, + 0.00985216503863537, + 0.004973319725907567, + 0.01893220586091267, + 0.11200583175389998, + 0.0028715556203528212, + 0.004095057588569762, + 0.01202391019098994, + 0.01756757544831608, + 0.014825484764542934, + 0.05312961073042717, + 0.06746872721971132, + 0.003845458521650386, + 0.0210806239976673, + 0.019443067502551394, + 0.08017028721387957, + 0.01825572240851436, + 0.005365213587986587, + 0.01959702580551101, + 0.026184575010934536, + 0.02474879720075813, + 0.002171745152354571, + 0.25827321767021433, + 0.048050153083539875, + 0.01043184137629392, + 0.03930485493512174, + 0.027640180784370902, + 0.03294007872867765, + 0.006474413179763814, + 0.018314039947514214, + 0.015119405161102202, + 0.014706516984983233, + 0.005494678524566263, + 0.03309870243475726, + 0.043864120134130345, + 0.058996355153812505, + 0.06265986295378335, + 0.04633328473538417, + 0.03790756670068523, + 0.0004642076104388394, + 0.037849249161685375, + 0.08369966467415076, + 0.04999679253535501, + 0.02392768625164018, + 0.010998687855372504, + 0.009881323808135296, + 0.003867619186470331, + 0.012434465665548913, + 0.007253535500801866, + 0.11106225397288234, + 0.17624726636535937, + 0.008209943140399476, + 0.008390727511299025, + 0.012682898381688294, + 0.1825653885406036, + 0.001538416678816154, + 0.004590756670068524, + 0.008710307625018223, + 0.1299513048549351, + 0.002677941390873305, + 0.012309666132089225, + 0.014087184720804781, + 0.01199941682461, + 0.031246537396121883, + 0.07206648199445984, + 0.008254264470039366, + 0.0007033095203382417, + 0.007034261554162415, + 0.006599212713223502, + 0.013906400349905234, + 0.050098265053214755, + 0.007133401370462167, + 0.017750692520775622, + 0.0008257763522379356, + 0.03918821985712203, + 0.06015454147834961, + ); +} diff --git a/plugins/jetpack/_inc/lib/class.jetpack-photon-image-sizes.php b/plugins/jetpack/_inc/lib/class.jetpack-photon-image-sizes.php new file mode 100644 index 00000000..8dc22d19 --- /dev/null +++ b/plugins/jetpack/_inc/lib/class.jetpack-photon-image-sizes.php @@ -0,0 +1,182 @@ +<?php +/** + * The Image Sizes library. + * + * @package jetpack + */ + +jetpack_require_lib( 'class.jetpack-photon-image' ); + +/** + * Class Jetpack_Photon_ImageSizes + * + * Manages image resizing via Jetpack CDN Service. + */ +class Jetpack_Photon_ImageSizes { + + /** + * @var array $data Attachment metadata. + */ + public $data; + + /** + * @var Image Image to be resized. + */ + public $image; + + /** + * @var null|array $sizes Intermediate sizes. + */ + public static $sizes = null; + + /** + * Construct new sizes meta + * + * @param int $attachment_id Attachment ID. + * @param array $data Attachment metadata. + */ + public function __construct( $attachment_id, $data ) { + $this->data = $data; + $this->image = new Jetpack_Photon_Image( $data, get_post_mime_type( $attachment_id ) ); + $this->generate_sizes(); + } + + /** + * Generate sizes for attachment. + * + * @return array Array of sizes; empty array as failure fallback. + */ + protected function generate_sizes() { + + // There is no need to generate the sizes a new for every single image. + if ( null !== self::$sizes ) { + return self::$sizes; + } + + /* + * The following logic is copied over from wp_generate_attachment_metadata + */ + $_wp_additional_image_sizes = wp_get_additional_image_sizes(); + + $sizes = array(); + + $intermediate_image_sizes = get_intermediate_image_sizes(); + + foreach ( $intermediate_image_sizes as $s ) { + $sizes[ $s ] = array( + 'width' => '', + 'height' => '', + 'crop' => false, + ); + if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) { + // For theme-added sizes. + $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] ); + } else { + // For default sizes set in options. + $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); + } + + if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) { + // For theme-added sizes. + $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] ); + } else { + // For default sizes set in options. + $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); + } + + if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) { + // For theme-added sizes. + $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; + } else { + // For default sizes set in options. + $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); + } + } + + self::$sizes = $sizes; + + return $sizes; + } + + /** + * @return array + */ + public function filtered_sizes() { + // Remove filter preventing the creation of advanced sizes. + remove_filter( + 'intermediate_image_sizes_advanced', + array( 'Jetpack_Photon', 'filter_photon_noresize_intermediate_sizes' ) + ); + + /** This filter is documented in wp-admin/includes/image.php */ + $sizes = apply_filters( 'intermediate_image_sizes_advanced', self::$sizes, $this->data ); + + // Re-add the filter removed above. + add_filter( + 'intermediate_image_sizes_advanced', + array( 'Jetpack_Photon', 'filter_photon_noresize_intermediate_sizes' ) + ); + + return (array) $sizes; + } + + /** + * Standardises and validates the size_data array. + * + * @param array $size_data Size data array - at least containing height or width key. Can contain crop as well. + * + * @return array Array with populated width, height and crop keys; empty array if no width and height are provided. + */ + public function standardize_size_data( $size_data ) { + $has_at_least_width_or_height = ( isset( $size_data['width'] ) || isset( $size_data['height'] ) ); + if ( ! $has_at_least_width_or_height ) { + return array(); + } + + $defaults = array( + 'width' => null, + 'height' => null, + 'crop' => false, + ); + + return array_merge( $defaults, $size_data ); + } + + /** + * Get sizes for attachment post meta. + * + * @return array ImageSizes for attachment postmeta. + */ + public function generate_sizes_meta() { + + $metadata = array(); + + foreach ( $this->filtered_sizes() as $size => $size_data ) { + + $size_data = $this->standardize_size_data( $size_data ); + + if ( true === empty( $size_data ) ) { + continue; + } + + $resized_image = $this->resize( $size_data ); + + if ( true === is_array( $resized_image ) ) { + $metadata[ $size ] = $resized_image; + } + } + + return $metadata; + } + + /** + * @param array $size_data + * + * @return array|\WP_Error Array for usage in $metadata['sizes']; WP_Error on failure. + */ + protected function resize( $size_data ) { + + return $this->image->get_size( $size_data ); + + } +} diff --git a/plugins/jetpack/_inc/lib/class.jetpack-photon-image.php b/plugins/jetpack/_inc/lib/class.jetpack-photon-image.php new file mode 100644 index 00000000..d364f2a3 --- /dev/null +++ b/plugins/jetpack/_inc/lib/class.jetpack-photon-image.php @@ -0,0 +1,243 @@ +<?php +/** + * The Image Class. + * + * @package Jetpack + */ + +/** + * Represents a resizable image, exposing properties necessary for properly generating srcset. + */ +class Jetpack_Photon_Image { + + /** + * @var string $filename Attachment's Filename. + */ + public $filename; + + /** + * @var string/WP_Erorr $mime_type Attachment's mime-type, WP_Error on failure when recalculating the dimensions. + */ + private $mime_type; + + /** + * @var int $original_width Image original width. + */ + private $original_width; + + /** + * @var int $original_width Image original height. + */ + private $original_height; + + /** + * @var int $width Current attachment's width. + */ + private $width; + + /** + * @var int $height Current attachment's height. + */ + private $height; + + /** + * @var bool $is_resized Whether the attachment has been resized yet, or not. + */ + private $is_resized = false; + + /** + * Constructs the image object. + * + * The $data array should provide at least + * file : string Image file path + * width : int Image width + * height : int Image height + * + * @param array $data Array of attachment metadata, typically value of _wp_attachment_metadata postmeta + * @param string|\WP_Error $mime_type Typically value returned from get_post_mime_type function. + */ + public function __construct( $data, $mime_type ) { + $this->filename = $data['file']; + $this->width = $this->original_width = $data['width']; + $this->height = $this->original_height = $data['height']; + $this->mime_type = $mime_type; + } + + /** + * Resizes the image to given size. + * + * @param array $size_data Array of width, height, and crop properties of a size. + * + * @return bool|\WP_Error True if resize was successful, WP_Error on failure. + */ + public function resize( $size_data ) { + + $dimensions = $this->image_resize_dimensions( $size_data['width'], $size_data['height'], $size_data['crop'] ); + + if ( true === is_wp_error( $dimensions ) ) { + return $dimensions; // Returns \WP_Error. + } + + if ( true === is_wp_error( $this->mime_type ) ) { + return $this->mime_type; // Returns \WP_Error. + } + + $this->set_width_height( $dimensions ); + + return $this->is_resized = true; + } + + /** + * Generates size data for usage in $metadata['sizes'];. + * + * @param array $size_data Array of width, height, and crop properties of a size. + * + * @return array|\WP_Error An array containing file, width, height, and mime-type keys and it's values. WP_Error on failure. + */ + public function get_size( $size_data ) { + + $is_resized = $this->resize( $size_data ); + + if ( true === is_wp_error( $is_resized ) ) { + return $is_resized; + } + + return array( + 'file' => $this->get_filename(), + 'width' => $this->get_width(), + 'height' => $this->get_height(), + 'mime-type' => $this->get_mime_type(), + ); + } + + /** + * Resets the image to it's original dimensions. + * + * @return bool True on successful reset to original dimensions. + */ + public function reset_to_original() { + $this->width = $this->original_width; + $this->height = $this->original_height; + $this->is_resized = false; + + return true; + } + + /** + * Return the basename filename. If the image has been resized, including + * the resizing params for Jetpack CDN. + * + * @return string Basename of the filename. + */ + public function get_filename() { + + if ( true === $this->is_resized() ) { + $filename = $this->get_resized_filename(); + } else { + $filename = $this->filename; + } + + return wp_basename( $filename ); + } + + /** + * Returns current image width. Either original, or after resize. + * + * @return int + */ + public function get_width() { + return (int) $this->width; + } + + /** + * Returns current image height. Either original, or after resize. + * + * @return int + */ + public function get_height() { + return (int) $this->height; + } + + /** + * Returns image mime type. + * + * @return string|WP_Error Image's mime type or WP_Error if it was not determined. + */ + public function get_mime_type() { + return $this->mime_type; + } + + /** + * Checks the resize status of the image. + * + * @return bool If the image has been resized. + */ + public function is_resized() { + return ( true === $this->is_resized ); + } + + /** + * Get filename with proper args for the Photon service. + * + * @return string Filename with query args for Photon service + */ + protected function get_resized_filename() { + $query_args = array( + 'resize' => join( + ',', + array( + $this->get_width(), + $this->get_height(), + ) + ), + ); + + return add_query_arg( $query_args, $this->filename ); + } + + /** + * Get resize dimensions used for the Jetpack CDN service. + * + * Converts the list of values returned from `image_resize_dimensions()` to + * associative array for the sake of more readable code no relying on index + * nor `list`. + * + * @param int $max_width + * @param int $max_height + * @param bool|array $crop + * + * @return array|\WP_Error Array of dimensions matching the parameters to imagecopyresampled. WP_Error on failure. + */ + protected function image_resize_dimensions( $max_width, $max_height, $crop ) { + $dimensions = image_resize_dimensions( $this->original_width, $this->original_height, $max_width, $max_height, $crop ); + if ( ! $dimensions ) { + return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->filename ); + } + + return array_combine( + array( + 'dst_x', + 'dst_y', + 'src_x', + 'src_y', + 'dst_w', + 'dst_h', + 'src_w', + 'src_h', + ), + $dimensions + ); + } + + /** + * Sets proper width and height from dimensions. + * + * @param Array $dimensions an array of image dimensions. + * @return void + */ + protected function set_width_height( $dimensions ) { + $this->width = (int) $dimensions['dst_w']; + $this->height = (int) $dimensions['dst_h']; + } + +} diff --git a/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php b/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php index 45d10d14..368b381a 100644 --- a/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php +++ b/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php @@ -67,7 +67,7 @@ class Jetpack_Core_API_Module_Toggle_Endpoint ); } - if ( ! Jetpack::active_plan_supports( $module_slug ) ) { + if ( ! Jetpack_Plan::supports( $module_slug ) ) { return new WP_Error( 'not_supported', esc_html__( 'The requested Jetpack module is not supported by your plan.', 'jetpack' ), @@ -1503,7 +1503,7 @@ class Jetpack_Core_API_Module_Data_Endpoint { * @type string $date Date range to restrict results to. * } * - * @return int|string Number of spam blocked by Akismet. Otherwise, an error message. + * @return WP_Error|WP_HTTP_Response|WP_REST_Response Stats information relayed from WordPress.com. */ public function get_stats_data( WP_REST_Request $request ) { // Get parameters to fetch Stats data. diff --git a/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php b/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php index c8fba69c..68327f51 100644 --- a/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php +++ b/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php @@ -48,69 +48,6 @@ class Jetpack_Core_API_Site_Endpoint { } /** - * Returns the result of `/sites/%s/posts/%d/related` endpoint call. - * Results are not cached and are retrieved in real time. - * - * @since 6.7.0 - * - * @param int ID of the post to get related posts of - * - * @return array - */ - public static function get_related_posts( $api_request ) { - $params = $api_request->get_params(); - $post_id = ! empty( $params['post_id'] ) ? absint( $params['post_id'] ) : 0; - - if ( ! $post_id ) { - return new WP_Error( - 'incorrect_post_id', - esc_html__( 'You need to specify a correct ID of a post to return related posts for.', 'jetpack' ), - array( 'status' => 400 ) - ); - } - - // Make the API request - $request = sprintf( '/sites/%d/posts/%d/related', Jetpack_Options::get_option( 'id' ), $post_id ); - $request_args = array( - 'headers' => array( - 'Content-Type' => 'application/json', - ), - 'timeout' => 10, - 'method' => 'POST', - ); - $response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '1.1', $request_args ); - - // Bail if there was an error or malformed response - if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { - return new WP_Error( - 'failed_to_fetch_data', - esc_html__( 'Unable to fetch the requested data.', 'jetpack' ), - array( 'status' => 400 ) - ); - } - - // Decode the results - $results = json_decode( wp_remote_retrieve_body( $response ), true ); - - $related_posts = array(); - if ( isset( $results['hits'] ) && is_array( $results['hits'] ) ) { - $related_posts_ids = array_map( array( 'Jetpack_Core_API_Site_Endpoint', 'get_related_post_id' ), $results['hits'] ); - - $related_posts_instance = Jetpack_RelatedPosts::init(); - foreach ( $related_posts_ids as $related_post_id ) { - $related_posts[] = $related_posts_instance->get_related_post_data_for_post( $related_post_id, 0, 0 ); - } - } - - return rest_ensure_response( array( - 'code' => 'success', - 'message' => esc_html__( 'Related posts retrieved successfully.', 'jetpack' ), - 'posts' => $related_posts, - ) - ); - } - - /** * Check that the current user has permissions to request information about this site. * * @since 5.1.0 @@ -120,16 +57,4 @@ class Jetpack_Core_API_Site_Endpoint { public static function can_request() { return current_user_can( 'jetpack_manage_modules' ); } - - /** - * Returns the post ID out of a related post entry from the - * `/sites/%s/posts/%d/related` WP.com endpoint. - * - * @since 6.7.0 - * - * @return int - */ - public static function get_related_post_id( $item ) { - return $item['fields']['post_id']; - } } diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/business-hours.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/business-hours.php new file mode 100644 index 00000000..2bf80939 --- /dev/null +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/business-hours.php @@ -0,0 +1,49 @@ +<?php + +/** + * Business Hours: Localized week + * + * @since 7.1 + */ +class WPCOM_REST_API_V2_Endpoint_Business_Hours extends WP_REST_Controller { + function __construct() { + $this->namespace = 'wpcom/v2'; + $this->rest_base = 'business-hours'; + // This endpoint *does not* need to connect directly to Jetpack sites. + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + } + + public function register_routes() { + // GET /sites/<blog_id>/business-hours/localized-week - Return the localized + register_rest_route( $this->namespace, '/' . $this->rest_base . '/localized-week', array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_localized_week' ), + ) + ) ); + } + + /** + * Retreives localized business hours + * + * @return array data object containing information about business hours + */ + public function get_localized_week() { + global $wp_locale; + + return array( + 'days' => array( + 'Sun' => $wp_locale->get_weekday( 0 ), + 'Mon' => $wp_locale->get_weekday( 1 ), + 'Tue' => $wp_locale->get_weekday( 2 ), + 'Wed' => $wp_locale->get_weekday( 3 ), + 'Thu' => $wp_locale->get_weekday( 4 ), + 'Fri' => $wp_locale->get_weekday( 5 ), + 'Sat' => $wp_locale->get_weekday( 6 ), + ), + 'startOfWeek' => (int) get_option( 'start_of_week', 0 ), + ); + } +} + +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Business_Hours' ); diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php new file mode 100644 index 00000000..09ef9499 --- /dev/null +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php @@ -0,0 +1,79 @@ +<?php + +/** + * Mailchimp: Get Mailchimp Status. + * API to determine if current site has linked Mailchimp account and mailing list selected. + * This API is meant to be used in Jetpack and on WPCOM. + * + * @since 7.1 + */ +class WPCOM_REST_API_V2_Endpoint_Mailchimp extends WP_REST_Controller { + public function __construct() { + $this->namespace = 'wpcom/v2'; + $this->rest_base = 'mailchimp'; + $this->wpcom_is_wpcom_only_endpoint = true; + + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + } + + /** + * Called automatically on `rest_api_init()`. + */ + public function register_routes() { + register_rest_route( + $this->namespace, + $this->rest_base, + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_mailchimp_status' ), + ), + ) + ); + } + + /** + * Check if MailChimp is set up properly. + * + * @return bool + */ + private function is_connected() { + $option = get_option( 'jetpack_mailchimp' ); + if ( ! $option ) { + return false; + } + $data = json_decode( $option, true ); + if ( ! $data ) { + return false; + } + return isset( $data['follower_list_id'], $data['keyring_id'] ); + } + + /** + * Get the status of current blog's Mailchimp connection + * + * @return mixed + * code:string (connected|unconnected), + * connect_url:string + * site_id:int + */ + public function get_mailchimp_status() { + $is_wpcom = ( defined( 'IS_WPCOM' ) && IS_WPCOM ); + $site_id = $is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' ); + if ( ! $site_id ) { + return new WP_Error( + 'unavailable_site_id', + __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ), + 403 + ); + } + $connect_url = sprintf( 'https://wordpress.com/sharing/%s', rawurlencode( $site_id ) ); + return array( + 'code' => $this->is_connected() ? 'connected' : 'not_connected', + 'connect_url' => $connect_url, + 'site_id' => $site_id, + ); + } +} + +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Mailchimp' ); diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/gutenberg-available-extensions.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/gutenberg-available-extensions.php new file mode 100644 index 00000000..a10a4056 --- /dev/null +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/gutenberg-available-extensions.php @@ -0,0 +1,71 @@ +<?php + +/* + * Gutenberg: List Available Gutenberg Extensions (Blocks and Plugins) + * + * [ + * { # Availabilty Object. See schema for more detail. + * available: (boolean) Whether the extension is available + * unavailable_reason: (string) Reason for the extension not being available + * }, + * ... + * ] + * + * @since 6.9 + */ +class WPCOM_REST_API_V2_Endpoint_Gutenberg_Available_Extensions extends WP_REST_Controller { + function __construct() { + $this->namespace = 'wpcom/v2'; + $this->rest_base = 'gutenberg'; + $this->wpcom_is_site_specific_endpoint = true; + + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + } + + public function register_routes() { + register_rest_route( $this->namespace, $this->rest_base . '/available-extensions', array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( 'Jetpack_Gutenberg', 'get_availability' ), + 'permission_callback' => array( $this, 'get_items_permission_check' ), + ), + 'schema' => array( $this, 'get_item_schema' ), + ) ); + } + + /** + * Return the available Gutenberg extensions schema + * + * @return array Available Gutenberg extensions schema + */ + public function get_public_item_schema() { + $schema = array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => 'gutenberg-available-extensions', + 'type' => 'object', + 'properties' => array( + 'available' => array( + 'description' => __( 'Whether the extension is available', 'jetpack' ), + 'type' => 'boolean', + ), + 'unavailable_reason' => array( + 'description' => __( 'Reason for the extension not being available', 'jetpack' ), + 'type' => 'string', + ), + ), + ); + + return $this->add_additional_fields_schema( $schema ); + } + + /** + * Ensure the user has proper permissions + * + * @return boolean + */ + public function get_items_permission_check() { + return current_user_can( 'edit_posts' ); + } +} + +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Gutenberg_Available_Extensions' ); diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php index 86019880..6e04a289 100644 --- a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php @@ -105,6 +105,10 @@ class WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results extends } } + if ( 'linkedin' === $item['id'] && 'must_reauth' === $test_result['connectionTestPassed'] ) { + $item['test_success'] = 'must_reauth'; + } + $response = rest_ensure_response( $items ); $response->header( 'X-WP-Total', count( $items ) ); diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connections.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connections.php index f7e9b351..34d6b2a6 100644 --- a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connections.php +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connections.php @@ -171,6 +171,14 @@ class WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections extends WP_REST_Cont public function get_items_permission_check() { global $publicize; + if ( ! $publicize ) { + return new WP_Error( + 'publicize_not_available', + __( 'Sorry, Publicize is not available on your site right now.', 'jetpack' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + if ( $publicize->current_user_can_access_publicize_data() ) { return true; } diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-services.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-services.php index fb418263..4641b218 100644 --- a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-services.php +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-services.php @@ -144,6 +144,14 @@ class WPCOM_REST_API_V2_Endpoint_List_Publicize_Services extends WP_REST_Control public function get_items_permission_check() { global $publicize; + if ( ! $publicize ) { + return new WP_Error( + 'publicize_not_available', + __( 'Sorry, Publicize is not available on your site right now.', 'jetpack' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + if ( $publicize->current_user_can_access_publicize_data() ) { return true; } diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php new file mode 100644 index 00000000..05d0ddd3 --- /dev/null +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php @@ -0,0 +1,281 @@ +<?php +/* + * Service API Keys: Exposes 3rd party api keys that are used on a site. + * + * [ + * { # Availabilty Object. See schema for more detail. + * code: (string) Displays success if the operation was successfully executed and an error code if it was not + * service: (string) The name of the service in question + * service_api_key: (string) The API key used by the service empty if one is not set yet + * message: (string) User friendly message + * }, + * ... + * ] + * + * @since 6.9 + */ +class WPCOM_REST_API_V2_Endpoint_Service_API_Keys extends WP_REST_Controller { + + function __construct() { + $this->namespace = 'wpcom/v2'; + $this->rest_base = 'service-api-keys'; + + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + } + + public function register_routes() { + register_rest_route( + 'wpcom/v2', + '/service-api-keys/(?P<service>[a-z\-_]+)', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( __CLASS__, 'get_service_api_key' ), + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( __CLASS__, 'update_service_api_key' ), + 'permission_callback' => array( __CLASS__, 'edit_others_posts_check' ), + 'args' => array( + 'service_api_key' => array( + 'required' => true, + 'type' => 'text', + ), + ), + ), + array( + 'methods' => WP_REST_Server::DELETABLE, + 'callback' => array( __CLASS__, 'delete_service_api_key' ), + 'permission_callback' => array( __CLASS__, 'edit_others_posts_check' ), + ), + ) + ); + } + + public static function edit_others_posts_check() { + if ( current_user_can( 'edit_others_posts' ) ) { + return true; + } + + $user_permissions_error_msg = esc_html__( + 'You do not have the correct user permissions to perform this action. + Please contact your site admin if you think this is a mistake.', + 'jetpack' + ); + + return new WP_Error( 'invalid_user_permission_edit_others_posts', $user_permissions_error_msg, rest_authorization_required_code() ); + } + + /** + * Return the available Gutenberg extensions schema + * + * @return array Service API Key schema + */ + public function get_public_item_schema() { + $schema = array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => 'service-api-keys', + 'type' => 'object', + 'properties' => array( + 'code' => array( + 'description' => __( 'Displays success if the operation was successfully executed and an error code if it was not', 'jetpack' ), + 'type' => 'string', + ), + 'service' => array( + 'description' => __( 'The name of the service in question', 'jetpack' ), + 'type' => 'string', + ), + 'service_api_key' => array( + 'description' => __( 'The API key used by the service. Empty if none has been set yet', 'jetpack' ), + 'type' => 'string', + ), + 'message' => array( + 'description' => __( 'User friendly message', 'jetpack' ), + 'type' => 'string', + ), + ), + ); + + return $this->add_additional_fields_schema( $schema ); + } + + /** + * Get third party plugin API keys. + * + * @param WP_REST_Request $request { + * Array of parameters received by request. + * + * @type string $slug Plugin slug with the syntax 'plugin-directory/plugin-main-file.php'. + * } + */ + public static function get_service_api_key( $request ) { + + $service = self::validate_service_api_service( $request['service'] ); + if ( ! $service ) { + return self::service_api_invalid_service_response(); + } + $option = self::key_for_api_service( $service ); + $message = esc_html__( 'API key retrieved successfully.', 'jetpack' ); + return array( + 'code' => 'success', + 'service' => $service, + 'service_api_key' => Jetpack_Options::get_option( $option, '' ), + 'message' => $message, + ); + } + + /** + * Update third party plugin API keys. + * + * @param WP_REST_Request $request { + * Array of parameters received by request. + * + * @type string $slug Plugin slug with the syntax 'plugin-directory/plugin-main-file.php'. + * } + */ + public static function update_service_api_key( $request ) { + $service = self::validate_service_api_service( $request['service'] ); + if ( ! $service ) { + return self::service_api_invalid_service_response(); + } + $json_params = $request->get_json_params(); + $params = ! empty( $json_params ) ? $json_params : $request->get_body_params(); + $service_api_key = trim( $params['service_api_key'] ); + $option = self::key_for_api_service( $service ); + + $validation = self::validate_service_api_key( $service_api_key, $service, $params ); + if ( ! $validation['status'] ) { + return new WP_Error( 'invalid_key', esc_html__( 'Invalid API Key', 'jetpack' ), array( 'status' => 404 ) ); + } + $message = esc_html__( 'API key updated successfully.', 'jetpack' ); + Jetpack_Options::update_option( $option, $service_api_key ); + return array( + 'code' => 'success', + 'service' => $service, + 'service_api_key' => Jetpack_Options::get_option( $option, '' ), + 'message' => $message, + ); + } + + /** + * Delete a third party plugin API key. + * + * @param WP_REST_Request $request { + * Array of parameters received by request. + * + * @type string $slug Plugin slug with the syntax 'plugin-directory/plugin-main-file.php'. + * } + */ + public static function delete_service_api_key( $request ) { + $service = self::validate_service_api_service( $request['service'] ); + if ( ! $service ) { + return self::service_api_invalid_service_response(); + } + $option = self::key_for_api_service( $service ); + Jetpack_Options::delete_option( $option ); + $message = esc_html__( 'API key deleted successfully.', 'jetpack' ); + return array( + 'code' => 'success', + 'service' => $service, + 'service_api_key' => Jetpack_Options::get_option( $option, '' ), + 'message' => $message, + ); + } + + /** + * Validate the service provided in /service-api-keys/ endpoints. + * To add a service to these endpoints, add the service name to $valid_services + * and add '{service name}_api_key' to the non-compact return array in get_option_names(), + * in class-jetpack-options.php + * + * @param string $service The service the API key is for. + * @return string Returns the service name if valid, null if invalid. + */ + public static function validate_service_api_service( $service = null ) { + $valid_services = array( + 'mapbox', + ); + return in_array( $service, $valid_services, true ) ? $service : null; + } + + /** + * Error response for invalid service API key requests with an invalid service. + */ + public static function service_api_invalid_service_response() { + return new WP_Error( + 'invalid_service', + esc_html__( 'Invalid Service', 'jetpack' ), + array( 'status' => 404 ) + ); + } + + /** + * Validate API Key + * + * @param string $key The API key to be validated. + * @param string $service The service the API key is for. + */ + public static function validate_service_api_key( $key = null, $service = null ) { + $validation = false; + switch ( $service ) { + case 'mapbox': + $validation = self::validate_service_api_key_mapbox( $key ); + break; + } + return $validation; + } + + /** + * Validate Mapbox API key + * Based loosely on https://github.com/mapbox/geocoding-example/blob/master/php/MapboxTest.php + * + * @param string $key The API key to be validated. + */ + public static function validate_service_api_key_mapbox( $key ) { + $status = true; + $msg = null; + $mapbox_url = sprintf( + 'https://api.mapbox.com?%s', + $key + ); + $mapbox_response = wp_safe_remote_get( esc_url_raw( $mapbox_url ) ); + $mapbox_body = wp_remote_retrieve_body( $mapbox_response ); + if ( '{"api":"mapbox"}' !== $mapbox_body ) { + $status = false; + $msg = esc_html__( 'Can\'t connect to Mapbox', 'jetpack' ); + return array( + 'status' => $status, + 'error_message' => $msg, + ); + } + $mapbox_geocode_url = esc_url_raw( + sprintf( + 'https://api.mapbox.com/geocoding/v5/mapbox.places/%s.json?access_token=%s', + '1+broadway+new+york+ny+usa', + $key + ) + ); + $mapbox_geocode_response = wp_safe_remote_get( esc_url_raw( $mapbox_geocode_url ) ); + $mapbox_geocode_body = wp_remote_retrieve_body( $mapbox_geocode_response ); + $mapbox_geocode_json = json_decode( $mapbox_geocode_body ); + if ( isset( $mapbox_geocode_json->message ) && ! isset( $mapbox_geocode_json->query ) ) { + $status = false; + $msg = $mapbox_geocode_json->message; + } + return array( + 'status' => $status, + 'error_message' => $msg, + ); + } + + /** + * Create site option key for service + * + * @param string $service The service to create key for. + */ + private static function key_for_api_service( $service ) { + return $service . '_api_key'; + } +} + +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys' ); diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php new file mode 100644 index 00000000..c1a712bd --- /dev/null +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php @@ -0,0 +1,62 @@ +<?php + +/** + * Subscribers: Get subscriber count + * + * @since 6.9 + */ +class WPCOM_REST_API_V2_Endpoint_Subscribers extends WP_REST_Controller { + function __construct() { + $this->namespace = 'wpcom/v2'; + $this->rest_base = 'subscribers'; + // This endpoint *does not* need to connect directly to Jetpack sites. + $this->wpcom_is_wpcom_only_endpoint = true; + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + } + + public function register_routes() { + // GET /sites/<blog_id>/subscribers/count - Return number of subscribers for this site. + register_rest_route( $this->namespace, '/' . $this->rest_base . '/count', array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_subscriber_count' ), + 'permission_callback' => array( $this, 'readable_permission_check' ), + ) + ) ); + } + + public function readable_permission_check() { + if ( ! current_user_can_for_blog( get_current_blog_id(), 'edit_posts' ) ) { + return new WP_Error( 'authorization_required', 'Only users with the permission to edit posts can see the subscriber count.', array( 'status' => 401 ) ); + } + + return true; + } + + /** + * Retrieves subscriber count + * + * @param WP_REST_Request $request incoming API request info + * @return array data object containing subscriber count + */ + public function get_subscriber_count( $request ) { + // Get the most up to date subscriber count when request is not a test + if ( ! Jetpack_Constants::is_defined( 'TESTING_IN_JETPACK' ) ) { + delete_transient( 'wpcom_subscribers_total' ); + } + + $subscriber_info = Jetpack_Subscriptions_Widget::fetch_subscriber_count(); + $subscriber_count = $subscriber_info['value']; + + return array( + 'count' => $subscriber_count + ); + } +} + +if ( + Jetpack::is_module_active( 'subscriptions' ) || + ( Jetpack_Constants::is_defined( 'TESTING_IN_JETPACK' ) && Jetpack_Constants::get_constant( 'TESTING_IN_JETPACK' ) ) +) { + wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Subscribers' ); +} diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-fields/attachment-fields-videopress.php b/plugins/jetpack/_inc/lib/core-api/wpcom-fields/attachment-fields-videopress.php new file mode 100644 index 00000000..b615c4e6 --- /dev/null +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-fields/attachment-fields-videopress.php @@ -0,0 +1,171 @@ +<?php +/** + * Extend the REST API functionality for VideoPress users. + * + * @package Jetpack + */ + +/** + * Add per-attachment VideoPress data. + * + * { # Attachment Object + * ... + * jetpack_videopress_guid: (string) VideoPress identifier + * ... + * } + * + * @since 7.1.0 + */ +class WPCOM_REST_API_V2_Attachment_VideoPress_Field extends WPCOM_REST_API_V2_Field_Controller { + /** + * The REST Object Type to which the jetpack_videopress_guid field will be added. + * + * @var string + */ + protected $object_type = 'attachment'; + + /** + * The name of the REST API field to add. + * + * @var string $field_name + */ + protected $field_name = 'jetpack_videopress_guid'; + + /** + * Registers the jetpack_videopress field and adds a filter to remove it for attachments that are not videos. + */ + public function register_fields() { + parent::register_fields(); + + add_filter( 'rest_prepare_attachment', array( $this, 'remove_field_for_non_videos' ), 10, 2 ); + } + + /** + * Defines data structure and what elements are visible in which contexts + */ + public function get_schema() { + return array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => $this->field_name, + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'description' => __( 'Unique VideoPress ID', 'jetpack' ), + ); + } + + /** + * Getter: Retrieve current VideoPress data for a given attachment. + * + * @param array $attachment Response from the attachment endpoint. + * @param WP_REST_Request $request Request to the attachment endpoint. + * + * @return string + */ + public function get( $attachment, $request ) { + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { + $blog_id = get_current_blog_id(); + } else { + $blog_id = Jetpack_Options::get_option( 'id' ); + } + + $post_id = absint( $attachment['id'] ); + + $videopress_guid = $this->get_videopress_guid( $post_id, $blog_id ); + + if ( ! $videopress_guid ) { + return ''; + } + + return $videopress_guid; + } + + /** + * Gets the VideoPress GUID for a given attachment. + * + * This is pulled out into a separate method to support unit test mocking. + * + * @param int $attachment_id Attachment ID. + * @param int $blog_id Blog ID. + * + * @return string + */ + public function get_videopress_guid( $attachment_id, $blog_id ) { + return video_get_info_by_blogpostid( $blog_id, $attachment_id )->guid; + } + + /** + * Checks if the given attachment is a video. + * + * @param object $attachment The attachment object. + * + * @return false|int + */ + public function is_video( $attachment ) { + return wp_startswith( $attachment->post_mime_type, 'video/' ); + } + + /** + * Removes the jetpack_videopress_guid field from the response if the + * given attachment is not a video. + * + * @param WP_REST_Response $response Response from the attachment endpoint. + * @param WP_Post $attachment The original attachment object. + * + * @return mixed + */ + public function remove_field_for_non_videos( $response, $attachment ) { + if ( ! $this->is_video( $attachment ) ) { + unset( $response->data[ $this->field_name ] ); + } + + return $response; + } + + /** + * Setter: It does nothing since `jetpack_videopress` is a read-only field. + * + * @param mixed $value The new value for the field. + * @param WP_Post $object The attachment object. + * @param WP_REST_Request $request The request object. + * + * @return null + */ + public function update( $value, $object, $request ) { + return null; + } + + /** + * Permission Check for the field's getter. Delegate the responsibility to the + * attachment endpoint, so it always returns true. + * + * @param mixed $object Response from the attachment endpoint. + * @param WP_REST_Request $request Request to the attachment endpoint. + * + * @return true + */ + public function get_permission_check( $object, $request ) { + return true; + } + + /** + * Permission Check for the field's setter. Delegate the responsibility to the + * attachment endpoint, so it always returns true. + * + * @param mixed $value The new value for the field. + * @param WP_Post $object The attachment object. + * @param WP_REST_Request $request Request to the attachment endpoint. + * + * @return true + */ + public function update_permission_check( $value, $object, $request ) { + return true; + } +} + +if ( + ( method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active() ) || + ( defined( 'IS_WPCOM' ) && IS_WPCOM ) +) { + wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Attachment_VideoPress_Field' ); +} diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php b/plugins/jetpack/_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php index 1aa8ec86..c4254a9d 100644 --- a/plugins/jetpack/_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php @@ -115,6 +115,14 @@ class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_ function permission_check( $post_id ) { global $publicize; + if ( ! $publicize ) { + return new WP_Error( + 'publicize_not_available', + __( 'Sorry, Publicize is not available on your site right now.', 'jetpack' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + if ( $publicize->current_user_can_access_publicize_data( $post_id ) ) { return true; } @@ -160,6 +168,10 @@ class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_ public function get( $post_array, $request ) { global $publicize; + if ( ! $publicize ) { + return array(); + } + $schema = $this->post_connection_schema(); $properties = array_keys( $schema['properties'] ); @@ -238,6 +250,10 @@ class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_ protected function get_meta_to_update( $requested_connections, $post_id = 0 ) { global $publicize; + if ( ! $publicize ) { + return array(); + } + if ( isset( $this->memoized_updates[$post_id] ) ) { return $this->memoized_updates[$post_id]; } @@ -334,4 +350,4 @@ class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_ if ( Jetpack::is_module_active( 'publicize' ) ) { wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Post_Publicize_Connections_Field' ); -}
\ No newline at end of file +} diff --git a/plugins/jetpack/_inc/lib/debugger/0-load.php b/plugins/jetpack/_inc/lib/debugger/0-load.php new file mode 100644 index 00000000..b097dc50 --- /dev/null +++ b/plugins/jetpack/_inc/lib/debugger/0-load.php @@ -0,0 +1,13 @@ +<?php +/** + * Loading the various functions used for Jetpack Debugging. + * + * @package Jetpack. + */ + +/* Jetpack Connection Testing Framework */ +require_once 'class-jetpack-cxn-test-base.php'; +/* Jetpack Connection Tests */ +require_once 'class-jetpack-cxn-tests.php'; +/* The "In-Plugin Debugger" admin page. */ +require_once 'class-jetpack-debugger.php'; diff --git a/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-test-base.php b/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-test-base.php new file mode 100644 index 00000000..0cc861db --- /dev/null +++ b/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-test-base.php @@ -0,0 +1,302 @@ +<?php +/** + * Jetpack Connection Testing + * + * Framework for various "unit tests" against the Jetpack connection. + * + * Individual tests should be added to the class-jetpack-cxn-tests.php file. + * + * @author Brandon Kraft + * @package Jetpack + */ + +/** + * "Unit Tests" for the Jetpack connection. + */ +class Jetpack_Cxn_Test_Base { + + /** + * Tests to run on the Jetpack connection. + * + * @var array $tests + */ + protected $tests = array(); + + /** + * Results of the Jetpack connection tests. + * + * @var array $results + */ + protected $results = array(); + + /** + * Status of the testing suite. + * + * Used internally to determine if a test should be skipped since the tests are already failing. Assume passing. + * + * @var bool $pass + */ + protected $pass = true; + + /** + * Jetpack_Cxn_Test constructor. + */ + public function __construct() { + $this->tests = array(); + $this->results = array(); + } + + /** + * Adds a new test to the Jetpack Connection Testing suite. + * + * @param callable $callable Test to add to queue. + * @param array $groups Testing groups to add test to. + * + * @return bool True if successfully added. False for a failure. + */ + public function add_test( $callable, $groups = array( 'default' ) ) { + if ( is_callable( $callable ) ) { + $this->tests[] = array( + 'test' => $callable, + 'group' => $groups, + ); + return true; + } + + return false; + } + + /** + * Runs the Jetpack connection suite. + */ + public function run_tests() { + foreach ( $this->tests as $test ) { + $result = call_user_func( $test['test'] ); + $result['group'] = $test['group']; + $this->results[] = $result; + if ( false === $result['pass'] ) { + $this->pass = false; + } + } + } + + /** + * Returns the full results array. + * + * @param string $group Testing group whose results we want. Defaults to "default" group. Use "all" for all tests. + * @return array Array of test results. + */ + public function raw_results( $group = 'default' ) { + if ( ! $this->results ) { + $this->run_tests(); + } + + $results = $this->results; + + if ( 'all' === $group ) { + return $results; + } + + foreach ( $results as $test => $result ) { + if ( ! in_array( $group, $result['group'], true ) ) { + unset( $results[ $test ] ); + } + } + + return $results; + } + + /** + * Returns the status of the connection suite. + * + * @param string $group Testing group to check status of. Optional, default all tests. + * + * @return true|array True if all tests pass. Array of failed tests. + */ + public function pass( $group = 'default' ) { + $results = $this->raw_results( $group ); + + foreach ( $results as $result ) { + // 'pass' could be true, false, or 'skipped'. We only want false. + if ( isset( $result['pass'] ) && false === $result['pass'] ) { + return false; + } + } + + return true; + + } + + /** + * Return array of failed test messages. + * + * @param string $group Testing group whose failures we want. Defaults to "default". Use "all" for all tests. + * + * @return false|array False if no failed tests. Otherwise, array of failed tests. + */ + public function list_fails( $group = 'default' ) { + $results = $this->raw_results( $group ); + + foreach ( $results as $test => $result ) { + // We do not want tests that passed or ones that are misconfigured (no pass status or no failure message). + if ( ! isset( $result['pass'] ) || false !== $result['pass'] || ! isset( $result['message'] ) ) { + unset( $results[ $test ] ); + } + } + + return $results; + } + + /** + * Helper function to return consistent responses for a passing test. + * + * @param string $name Test name. + * + * @return array Test results. + */ + public static function passing_test( $name = 'Unnamed' ) { + return array( + 'name' => $name, + 'pass' => true, + 'message' => __( 'Test Passed!', 'jetpack' ), + 'resolution' => false, + ); + } + + /** + * Helper function to return consistent responses for a skipped test. + * + * @param string $name Test name. + * @param string $message Reason for skipping the test. Optional. + * + * @return array Test results. + */ + public static function skipped_test( $name = 'Unnamed', $message = false ) { + return array( + 'name' => $name, + 'pass' => 'skipped', + 'message' => $message, + 'resolution' => false, + ); + } + + /** + * Helper function to return consistent responses for a failing test. + * + * @param string $name Test name. + * @param string $message Message detailing the failure. + * @param string $resolution Steps to resolve. + * + * @return array Test results. + */ + public static function failing_test( $name, $message, $resolution = false ) { + // Provide standard resolutions steps, but allow pass-through of non-standard ones. + switch ( $resolution ) { + case 'cycle_connection': + $resolution = __( 'Please disconnect and reconnect Jetpack.', 'jetpack' ); // @todo: Link. + break; + case 'outbound_requests': + $resolution = __( 'Please ask your hosting provider to confirm your server can make outbound requests to jetpack.com.', 'jetpack' ); + break; + case 'support': + $resolution = __( 'Please contact support.', 'jetpack' ); // @todo: Link to support. + break; + } + + return array( + 'name' => $name, + 'pass' => false, + 'message' => $message, + 'resolution' => $resolution, + ); + } + + /** + * Provide WP_CLI friendly testing results. + * + * @param string $group Testing group whose results we are outputting. Default "default". Use "all" for all tests. + */ + public function output_results_for_cli( $group = 'default' ) { + if ( defined( 'WP_CLI' ) && WP_CLI ) { + if ( Jetpack::is_development_mode() ) { + WP_CLI::line( __( 'Jetpack is in Development Mode:', 'jetpack' ) ); + WP_CLI::line( Jetpack::development_mode_trigger_text() ); + } + WP_CLI::line( __( 'TEST RESULTS:', 'jetpack' ) ); + foreach ( $this->raw_results( $group ) as $test ) { + if ( true === $test['pass'] ) { + WP_CLI::log( WP_CLI::colorize( '%gPassed:%n ' . $test['name'] ) ); + } elseif ( 'skipped' === $test['pass'] ) { + WP_CLI::log( WP_CLI::colorize( '%ySkipped:%n ' . $test['name'] ) ); + if ( $test['message'] ) { + WP_CLI::log( ' ' . $test['message'] ); // Number of spaces to "tab indent" the reason. + } + } else { // Failed. + WP_CLI::log( WP_CLI::colorize( '%rFailed:%n ' . $test['name'] ) ); + WP_CLI::log( ' ' . $test['message'] ); // Number of spaces to "tab indent" the reason. + } + } + } + } + + /** + * Provide single WP Error instance of all failures. + * + * @param string $group Testing group whose failures we want converted. Default "default". Use "all" for all tests. + * + * @return WP_Error|false WP_Error with all failed tests or false if there were no failures. + */ + public function output_fails_as_wp_error( $group = 'default' ) { + if ( $this->pass( $group ) ) { + return false; + } + $fails = $this->list_fails( $group ); + $error = false; + + foreach ( $fails as $result ) { + $code = 'failed_' . $result['name']; + $message = $result['message']; + $data = array( + 'resolution' => $result['resolution'], + ); + if ( ! $error ) { + $error = new WP_Error( $code, $message, $data ); + } else { + $error->add( $code, $message, $data ); + } + } + + return $error; + } + + /** + * Encrypt data for sending to WordPress.com. + * + * @todo When PHP minimum is 5.3+, add cipher detection to use an agreed better cipher than RC4. RC4 should be the last resort. + * + * @param string $data Data to encrypt with the WP.com Public Key. + * + * @return false|array False if functionality not available. Array of encrypted data, encryption key. + */ + public function encrypt_string_for_wpcom( $data ) { + $return = false; + if ( ! function_exists( 'openssl_get_publickey' ) || ! function_exists( 'openssl_seal' ) ) { + return $return; + } + + $public_key = openssl_get_publickey( JETPACK__DEBUGGER_PUBLIC_KEY ); + + if ( $public_key && openssl_seal( $data, $encrypted_data, $env_key, array( $public_key ) ) ) { + // We are returning base64-encoded values to ensure they're characters we can use in JSON responses without issue. + $return = array( + 'data' => base64_encode( $encrypted_data ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode + 'key' => base64_encode( $env_key[0] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode + 'cipher' => 'RC4', // When Jetpack's minimum WP version is at PHP 5.3+, we will add in detecting and using a stronger one. + ); + } + + openssl_free_key( $public_key ); + + return $return; + } +} diff --git a/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-tests.php b/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-tests.php new file mode 100644 index 00000000..6d4f00e6 --- /dev/null +++ b/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-tests.php @@ -0,0 +1,338 @@ +<?php +/** + * Collection of tests to run on the Jetpack connection locally. + * + * @package Jetpack + */ + +/** + * Class Jetpack_Cxn_Tests contains all of the actual tests. + */ +class Jetpack_Cxn_Tests extends Jetpack_Cxn_Test_Base { + + /** + * Jetpack_Cxn_Tests constructor. + */ + public function __construct() { + parent::__construct(); + + $methods = get_class_methods( 'Jetpack_Cxn_Tests' ); + + foreach ( $methods as $method ) { + if ( false === strpos( $method, 'test__' ) ) { + continue; + } + $this->add_test( array( $this, $method ) ); + } + + /** + * Fires after loading default Jetpack Connection tests. + * + * @since 7.1.0 + */ + do_action( 'jetpack_connection_tests_loaded' ); + + /** + * Determines if the WP.com testing suite should be included. + * + * @since 7.1.0 + * + * @param bool $run_test To run the WP.com testing suite. Default true. + */ + if ( apply_filters( 'jetpack_debugger_run_self_test', true ) ) { + /** + * Intentionally added last as it checks for an existing failure state before attempting. + * Generally, any failed location condition would result in the WP.com check to fail too, so + * we will skip it to avoid confusing error messages. + */ + $this->add_test( array( $this, 'last__wpcom_self_test' ) ); + } + } + + /** + * Helper function to look up the expected master user and return the local WP_User. + * + * @return WP_User Jetpack's expected master user. + */ + protected function helper_retrieve_local_master_user() { + $master_user = Jetpack_Options::get_option( 'master_user' ); + return new WP_User( $master_user ); + } + + /** + * Is Jetpack even connected and supposed to be talking to WP.com? + */ + protected function helper_is_jetpack_connected() { + return ( Jetpack::is_active() && ! Jetpack::is_development_mode() ); + } + + /** + * Test if Jetpack is connected. + */ + protected function test__check_if_connected() { + $name = __FUNCTION__; + if ( $this->helper_is_jetpack_connected() ) { + $result = self::passing_test( $name ); + } elseif ( Jetpack::is_development_mode() ) { + $result = self::skipped_test( $name, __( 'Jetpack is in Development Mode:', 'jetpack' ) . ' ' . Jetpack::development_mode_trigger_text(), __( 'Disable development mode.', 'jetpack' ) ); + } else { + $result = self::failing_test( $name, __( 'Jetpack is not connected.', 'jetpack' ), 'cycle_connection' ); + } + + return $result; + } + + /** + * Test that the master user still exists on this site. + * + * @return array Test results. + */ + protected function test__master_user_exists_on_site() { + $name = __FUNCTION__; + if ( ! $this->helper_is_jetpack_connected() ) { + return self::skipped_test( $name, __( 'Jetpack is not connected. No master user to check.', 'jetpack' ) ); // Skip test. + } + $local_user = $this->helper_retrieve_local_master_user(); + + if ( $local_user->exists() ) { + $result = self::passing_test( $name ); + } else { + $result = self::failing_test( $name, __( 'The user who setup the Jetpack connection no longer exists on this site.', 'jetpack' ), 'cycle_connection' ); + } + + return $result; + } + + /** + * Test that the master user has the manage options capability (e.g. is an admin). + * + * Generic calls from WP.com execute on Jetpack as the master user. If it isn't an admin, random things will fail. + * + * @return array Test results. + */ + protected function test__master_user_can_manage_options() { + $name = __FUNCTION__; + if ( ! $this->helper_is_jetpack_connected() ) { + return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test. + } + $master_user = $this->helper_retrieve_local_master_user(); + + if ( user_can( $master_user, 'manage_options' ) ) { + $result = self::passing_test( $name ); + } else { + /* translators: a WordPress username */ + $result = self::failing_test( $name, sprintf( __( 'The user (%s) who setup the Jetpack connection is not an administrator.', 'jetpack' ), $master_user->user_login ), __( 'Either upgrade the user or disconnect and reconnect Jetpack.', 'jetpack' ) ); // @todo: Link to the right places. + } + + return $result; + } + + /** + * Test that the PHP's XML library is installed. + * + * While it should be installed by default, increasingly in PHP 7, some OSes require an additional php-xml package. + * + * @return array Test results. + */ + protected function test__xml_parser_available() { + $name = __FUNCTION__; + if ( function_exists( 'xml_parser_create' ) ) { + $result = self::passing_test( $name ); + } else { + $result = self::failing_test( $name, __( 'PHP XML manipluation libraries are not available.', 'jetpack' ), __( "Please ask your hosting provider to refer to our server requirements at https://jetpack.com/support/server-requirements/ and enable PHP's XML module.", 'jetpack' ) ); + } + + return $result; + } + + /** + * Test that the server is able to send an outbound http communication. + * + * @return array Test results. + */ + protected function test__outbound_http() { + $name = __FUNCTION__; + $request = wp_remote_get( preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/' ); + $code = wp_remote_retrieve_response_code( $request ); + + if ( 200 === intval( $code ) ) { + $result = self::passing_test( $name ); + } else { + $result = self::failing_test( $name, __( 'Your server did not successfully connect to the Jetpack server using HTTP', 'jetpack' ), 'outbound_requests' ); + } + + return $result; + } + + /** + * Test that the server is able to send an outbound https communication. + * + * @return array Test results. + */ + protected function test__outbound_https() { + $name = __FUNCTION__; + $request = wp_remote_get( preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/' ); + $code = wp_remote_retrieve_response_code( $request ); + + if ( 200 === intval( $code ) ) { + $result = self::passing_test( $name ); + } else { + $result = self::failing_test( $name, __( 'Your server did not successfully connect to the Jetpack server using HTTPS', 'jetpack' ), 'outbound_requests' ); + } + + return $result; + } + + /** + * Check for an IDC. + * + * @return array Test results. + */ + protected function test__identity_crisis() { + $name = __FUNCTION__; + if ( ! $this->helper_is_jetpack_connected() ) { + return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test. + } + $identity_crisis = Jetpack::check_identity_crisis(); + + if ( ! $identity_crisis ) { + $result = self::passing_test( $name ); + } else { + $message = sprintf( + /* translators: Two URLs. The first is the locally-recorded value, the second is the value as recorded on WP.com. */ + __( 'Your url is set as `%1$s`, but your WordPress.com connection lists it as `%2$s`!', 'jetpack' ), + $identity_crisis['home'], + $identity_crisis['wpcom_home'] + ); + $result = self::failing_test( $name, $message, 'support' ); + } + return $result; + } + + /** + * Tests connection status against wp.com's test-connection endpoint + * + * @todo: Compare with the wpcom_self_test. We only need one of these. + * + * @return array Test results. + */ + protected function test__wpcom_connection_test() { + $name = __FUNCTION__; + + if ( ! Jetpack::is_active() || Jetpack::is_development_mode() || Jetpack::is_staging_site() || ! $this->pass ) { + return self::skipped_test( $name ); + } + + $response = Jetpack_Client::wpcom_json_api_request_as_blog( + sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ), + Jetpack_Client::WPCOM_JSON_API_VERSION + ); + + if ( is_wp_error( $response ) ) { + /* translators: %1$s is the error code, %2$s is the error message */ + $message = sprintf( __( 'Connection test failed (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ); + return self::failing_test( $name, $message ); + } + + $body = wp_remote_retrieve_body( $response ); + if ( ! $body ) { + $message = __( 'Connection test failed (empty response body)', 'jetpack' ) . wp_remote_retrieve_response_code( $response ); + return self::failing_test( $name, $message ); + } + + $result = json_decode( $body ); + $is_connected = (bool) $result->connected; + $message = $result->message . wp_remote_retrieve_response_code( $response ); + + if ( $is_connected ) { + return self::passing_test( $name ); + } else { + return self::failing_test( $name, $message ); + } + } + + /** + * Tests the port number to ensure it is an expected value. + * + * We expect that sites on be on one of: + * port 80, + * port 443 (https sites only), + * the value of JETPACK_SIGNATURE__HTTP_PORT, + * unless the site is intentionally on a different port (e.g. example.com:8080 is the site's URL). + * + * If the value isn't one of those and the site's URL doesn't include a port, then the signature verification will fail. + * + * This happens most commonly on sites with reverse proxies, so the edge (e.g. Varnish) is running on 80/443, but nginx + * or Apache is responding internally on a different port (e.g. 81). + * + * @return array Test results + */ + protected function test__server_port_value() { + $name = __FUNCTION__; + if ( ! isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) && ! isset( $_SERVER['SERVER_PORT'] ) ) { + $message = 'The server port values are not defined. This is most common when running PHP via a CLI.'; + return self::skipped_test( $name, $message ); + } + $site_port = wp_parse_url( home_url(), PHP_URL_PORT ); + $server_port = isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ? (int) $_SERVER['HTTP_X_FORWARDED_PORT'] : (int) $_SERVER['SERVER_PORT']; + $http_ports = array( 80 ); + $https_ports = array( 80, 443 ); + + if ( defined( 'JETPACK_SIGNATURE__HTTP_PORT' ) ) { + $http_ports[] = JETPACK_SIGNATURE__HTTP_PORT; + } + + if ( defined( 'JETPACK_SIGNATURE__HTTPS_PORT' ) ) { + $https_ports[] = JETPACK_SIGNATURE__HTTPS_PORT; + } + + if ( $site_port ) { + return self::skipped_test( $name ); // Not currently testing for this situation. + } + + if ( is_ssl() && in_array( $server_port, $https_ports, true ) ) { + return self::passing_test( $name ); + } elseif ( in_array( $server_port, $http_ports, true ) ) { + return self::passing_test( $name ); + } else { + if ( is_ssl() ) { + $needed_constant = 'JETPACK_SIGNATURE__HTTPS_PORT'; + } else { + $needed_constant = 'JETPACK_SIGNATURE__HTTP_PORT'; + } + $message = __( 'The server port value is unexpected.', 'jetpack' ); + $resolution = __( 'Try adding the following to your wp-config.php file:', 'jetpack' ) . " define( '$needed_constant', $server_port );"; + return self::failing_test( $name, $message, $resolution ); + } + } + + /** + * Calls to WP.com to run the connection diagnostic testing suite. + * + * Intentionally added last as it will be skipped if any local failed conditions exist. + * + * @return array Test results. + */ + protected function last__wpcom_self_test() { + $name = 'test__wpcom_self_test'; + if ( ! Jetpack::is_active() || Jetpack::is_development_mode() || Jetpack::is_staging_site() || ! $this->pass ) { + return self::skipped_test( $name ); + } + + $self_xml_rpc_url = site_url( 'xmlrpc.php' ); + + $testsite_url = Jetpack::fix_url_for_bad_hosts( JETPACK__API_BASE . 'testsite/1/?url=' ); + + add_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) ); + + $response = wp_remote_get( $testsite_url . $self_xml_rpc_url ); + + remove_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) ); + + if ( 200 === wp_remote_retrieve_response_code( $response ) ) { + return self::passing_test( $name ); + } else { + return self::failing_test( $name, __( 'Jetpack.com detected an error.', 'jetpack' ), __( 'Visit the Jetpack.com debugging page for more information or contact support.', 'jetpack' ) ); // @todo direct links. + } + } +} diff --git a/plugins/jetpack/_inc/lib/debugger/class-jetpack-debugger.php b/plugins/jetpack/_inc/lib/debugger/class-jetpack-debugger.php new file mode 100644 index 00000000..599ccfda --- /dev/null +++ b/plugins/jetpack/_inc/lib/debugger/class-jetpack-debugger.php @@ -0,0 +1,530 @@ +<?php +/** + * Jetpack Debugger functionality allowing for self-service diagnostic information. + * + * @package jetpack + */ + +/** + * Class Jetpack_Debugger + * + * A namespacing class for functionality related to the in-plugin diagnostic tooling. + */ +class Jetpack_Debugger { + + /** + * Determine the active plan and normalize it for the debugger results. + * + * @return string The plan slug prepended with "JetpackPlan" + */ + private static function what_jetpack_plan() { + $plan = Jetpack_Plan::get(); + $plan = ! empty( $plan['class'] ) ? $plan['class'] : 'undefined'; + return 'JetpackPlan' . $plan; + } + + /** + * Convert seconds to human readable time. + * + * A dedication function instead of using Core functionality to allow for output in seconds. + * + * @param int $seconds Number of seconds to convert to human time. + * + * @return string Human readable time. + */ + public static function seconds_to_time( $seconds ) { + $seconds = intval( $seconds ); + $units = array( + 'week' => WEEK_IN_SECONDS, + 'day' => DAY_IN_SECONDS, + 'hour' => HOUR_IN_SECONDS, + 'minute' => MINUTE_IN_SECONDS, + 'second' => 1, + ); + // specifically handle zero. + if ( 0 === $seconds ) { + return '0 seconds'; + } + $human_readable = ''; + foreach ( $units as $name => $divisor ) { + $quot = intval( $seconds / $divisor ); + if ( $quot ) { + $human_readable .= "$quot $name"; + $human_readable .= ( abs( $quot ) > 1 ? 's' : '' ) . ', '; + $seconds -= $quot * $divisor; + } + } + return substr( $human_readable, 0, -2 ); + } + + /** + * Returns 30 for use with a filter. + * + * To allow time for WP.com to run upstream testing, this function exists to increase the http_request_timeout value + * to 30. + * + * @return int 30 + */ + public static function jetpack_increase_timeout() { + return 30; // seconds. + } + + /** + * Disconnect Jetpack and redirect user to connection flow. + */ + public static function disconnect_and_redirect() { + if ( ! ( isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'jp_disconnect' ) ) ) { + return; + } + + if ( isset( $_GET['disconnect'] ) && $_GET['disconnect'] ) { + if ( Jetpack::is_active() ) { + Jetpack::disconnect(); + wp_safe_redirect( Jetpack::admin_url() ); + exit; + } + } + } + + /** + * Handles output to the browser for the in-plugin debugger. + */ + public static function jetpack_debug_display_handler() { + if ( ! current_user_can( 'manage_options' ) ) { + wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'jetpack' ) ); + } + + $user_id = get_current_user_id(); + $user_tokens = Jetpack_Options::get_option( 'user_tokens' ); + if ( is_array( $user_tokens ) && array_key_exists( $user_id, $user_tokens ) ) { + $user_token = $user_tokens[ $user_id ]; + } else { + $user_token = '[this user has no token]'; + } + unset( $user_tokens ); + + $debug_info = "\r\n"; + foreach ( array( + 'CLIENT_ID' => 'id', + 'BLOG_TOKEN' => 'blog_token', + 'MASTER_USER' => 'master_user', + 'CERT' => 'fallback_no_verify_ssl_certs', + 'TIME_DIFF' => 'time_diff', + 'VERSION' => 'version', + 'OLD_VERSION' => 'old_version', + 'PUBLIC' => 'public', + ) as $label => $option_name ) { + $debug_info .= "\r\n" . esc_html( $label . ': ' . Jetpack_Options::get_option( $option_name ) ); + } + + $debug_info .= "\r\n" . esc_html( 'USER_ID: ' . $user_id ); + $debug_info .= "\r\n" . esc_html( 'USER_TOKEN: ' . $user_token ); + $debug_info .= "\r\n" . esc_html( 'PHP_VERSION: ' . PHP_VERSION ); + $debug_info .= "\r\n" . esc_html( 'WORDPRESS_VERSION: ' . $GLOBALS['wp_version'] ); + $debug_info .= "\r\n" . esc_html( 'JETPACK__VERSION: ' . JETPACK__VERSION ); + $debug_info .= "\r\n" . esc_html( 'JETPACK__PLUGIN_DIR: ' . JETPACK__PLUGIN_DIR ); + $debug_info .= "\r\n" . esc_html( 'SITE_URL: ' . site_url() ); + $debug_info .= "\r\n" . esc_html( 'HOME_URL: ' . home_url() ); + $debug_info .= "\r\n" . esc_html( 'PLAN: ' . self::what_jetpack_plan() ); + + $debug_info .= "\r\n"; + + $debug_info .= "\r\n" . '-- SYNC Status -- '; + require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php'; + $sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' ); + if ( $sync_module ) { + $sync_statuses = $sync_module->get_status(); + $human_readable_sync_status = array(); + foreach ( $sync_statuses as $sync_status => $sync_status_value ) { + $human_readable_sync_status[ $sync_status ] = + in_array( $sync_status, array( 'started', 'queue_finished', 'send_started', 'finished' ), true ) + ? date( 'r', $sync_status_value ) : $sync_status_value; + } + /* translators: A string reporting status. Example: "started" */ + $debug_info .= "\r\n" . sprintf( esc_html__( 'Jetpack Sync Full Status: `%1$s`', 'jetpack' ), print_r( $human_readable_sync_status, 1 ) ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r + } + + require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; + + $queue = Jetpack_Sync_Sender::get_instance()->get_sync_queue(); + + /* translators: The number of items waiting to be synced. */ + $debug_info .= "\r\n" . sprintf( esc_html__( 'Sync Queue size: %1$s', 'jetpack' ), $queue->size() ); + /* translators: Human-readable time since the oldest item in the sync queue. */ + $debug_info .= "\r\n" . sprintf( esc_html__( 'Sync Queue lag: %1$s', 'jetpack' ), self::seconds_to_time( $queue->lag() ) ); + + $full_sync_queue = Jetpack_Sync_Sender::get_instance()->get_full_sync_queue(); + + /* translators: The number of items waiting to be synced. */ + $debug_info .= "\r\n" . sprintf( esc_html__( 'Full Sync Queue size: %1$s', 'jetpack' ), $full_sync_queue->size() ); + /* translators: Human-readable time since the oldest item in the sync queue. */ + $debug_info .= "\r\n" . sprintf( esc_html__( 'Full Sync Queue lag: %1$s', 'jetpack' ), self::seconds_to_time( $full_sync_queue->lag() ) ); + + require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; + $idc_urls = array( + 'home' => Jetpack_Sync_Functions::home_url(), + 'siteurl' => Jetpack_Sync_Functions::site_url(), + 'WP_HOME' => Jetpack_Constants::is_defined( 'WP_HOME' ) ? Jetpack_Constants::get_constant( 'WP_HOME' ) : '', + 'WP_SITEURL' => Jetpack_Constants::is_defined( 'WP_SITEURL' ) ? Jetpack_Constants::get_constant( 'WP_SITEURL' ) : '', + ); + /* translators: List of URLs. */ + $debug_info .= "\r\n" . esc_html( sprintf( 'Sync IDC URLs: %s', wp_json_encode( $idc_urls ) ) ); + /* translators: String of a current option. */ + $debug_info .= "\r\n" . esc_html( sprintf( 'Sync error IDC option: %s', wp_json_encode( Jetpack_Options::get_option( 'sync_error_idc' ) ) ) ); + /* translators: String of a current option. */ + $debug_info .= "\r\n" . esc_html( sprintf( 'Sync IDC Optin: %s', (string) Jetpack::sync_idc_optin() ) ); + + $debug_info .= "\r\n"; + + foreach ( array( + 'HTTP_HOST', + 'SERVER_PORT', + 'HTTPS', + 'GD_PHP_HANDLER', + 'HTTP_AKAMAI_ORIGIN_HOP', + 'HTTP_CF_CONNECTING_IP', + 'HTTP_CLIENT_IP', + 'HTTP_FASTLY_CLIENT_IP', + 'HTTP_FORWARDED', + 'HTTP_FORWARDED_FOR', + 'HTTP_INCAP_CLIENT_IP', + 'HTTP_TRUE_CLIENT_IP', + 'HTTP_X_CLIENTIP', + 'HTTP_X_CLUSTER_CLIENT_IP', + 'HTTP_X_FORWARDED', + 'HTTP_X_FORWARDED_FOR', + 'HTTP_X_IP_TRAIL', + 'HTTP_X_REAL_IP', + 'HTTP_X_VARNISH', + 'REMOTE_ADDR', + ) as $header ) { + if ( isset( $_SERVER[ $header ] ) ) { + $debug_info .= "\r\n" . esc_html( $header . ': ' . $_SERVER[ $header ] ); + } + } + + $debug_info .= "\r\n" . esc_html( 'PROTECT_TRUSTED_HEADER: ' . wp_json_encode( get_site_option( 'trusted_ip_header' ) ) ); + + $debug_info .= "\r\n\r\nTEST RESULTS:\r\n\r\n"; + + $cxntests = new Jetpack_Cxn_Tests(); + ?> + <div class="wrap"> + <h2><?php esc_html_e( 'Debugging Center', 'jetpack' ); ?></h2> + <h3><?php esc_html_e( "Testing your site's compatibility with Jetpack...", 'jetpack' ); ?></h3> + <div class="jetpack-debug-test-container"> + <?php + if ( $cxntests->pass() ) { + echo '<div class="jetpack-tests-succeed">' . esc_html__( 'Your Jetpack setup looks a-okay!', 'jetpack' ) . '</div>'; + $debug_info .= "All tests passed.\r\n"; + $debug_info .= print_r( $cxntests->raw_results(), true ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r + } else { + $failures = $cxntests->list_fails(); + foreach ( $failures as $fail ) { + echo '<div class="jetpack-test-error">'; + echo '<p><a class="jetpack-test-heading" href="#">' . esc_html( $fail['message'] ); + echo '<span class="noticon noticon-collapse"></span></a></p>'; + echo '<p class="jetpack-test-details">' . esc_html( $fail['resolution'] ) . '</p>'; + echo '</div>'; + + $debug_info .= "FAILED TESTS!\r\n"; + $debug_info .= $fail['name'] . ': ' . $fail['message'] . "\r\n"; + $debug_info .= print_r( $cxntests->raw_results(), true ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r + } + } + ?> + </div> + <div class="entry-content"> + <h3><?php esc_html_e( 'Trouble with Jetpack?', 'jetpack' ); ?></h3> + <h4><?php esc_html_e( 'It may be caused by one of these issues, which you can diagnose yourself:', 'jetpack' ); ?></h4> + <ol> + <li><b><em> + <?php + esc_html_e( 'A known issue.', 'jetpack' ); + ?> + </em></b> + <?php + echo sprintf( + wp_kses( + /* translators: URLs to Jetpack support pages. */ + __( 'Some themes and plugins have <a href="%1$s" target="_blank">known conflicts</a> with Jetpack – check the <a href="%2$s" target="_blank">list</a>. (You can also browse the <a href="%3$s" target="_blank">Jetpack support pages</a> or <a href="%4$s" target="_blank">Jetpack support forum</a> to see if others have experienced and solved the problem.)', 'jetpack' ), + array( + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ), + 'http://jetpack.com/support/getting-started-with-jetpack/known-issues/', + 'http://jetpack.com/support/getting-started-with-jetpack/known-issues/', + 'http://jetpack.com/support/', + 'https://wordpress.org/support/plugin/jetpack' + ); + ?> + </li> + <li><b><em><?php esc_html_e( 'An incompatible plugin.', 'jetpack' ); ?></em></b> <?php esc_html_e( "Find out by disabling all plugins except Jetpack. If the problem persists, it's not a plugin issue. If the problem is solved, turn your plugins on one by one until the problem pops up again – there's the culprit! Let us know, and we'll try to help.", 'jetpack' ); ?></li> + <li> + <b><em><?php esc_html_e( 'A theme conflict.', 'jetpack' ); ?></em></b> + <?php + $default_theme = wp_get_theme( WP_DEFAULT_THEME ); + + if ( $default_theme->exists() ) { + /* translators: %s is the name of a theme */ + echo esc_html( sprintf( __( "If your problem isn't known or caused by a plugin, try activating %s (the default WordPress theme).", 'jetpack' ), $default_theme->get( 'Name' ) ) ); + } else { + esc_html_e( "If your problem isn't known or caused by a plugin, try activating the default WordPress theme.", 'jetpack' ); + } + ?> + <?php esc_html_e( "If this solves the problem, something in your theme is probably broken – let the theme's author know.", 'jetpack' ); ?> + </li> + <li><b><em><?php esc_html_e( 'A problem with your XMLRPC file.', 'jetpack' ); ?></em></b> + <?php + echo sprintf( + wp_kses( + /* translators: The URL to the site's xmlrpc.php file. */ + __( 'Load your <a href="%s">XMLRPC file</a>. It should say “XML-RPC server accepts POST requests only.” on a line by itself.', 'jetpack' ), + array( 'a' => array( 'href' => array() ) ) + ), + esc_attr( site_url( 'xmlrpc.php' ) ) + ); + ?> + <ul> + <li>- <?php esc_html_e( "If it's not by itself, a theme or plugin is displaying extra characters. Try steps 2 and 3.", 'jetpack' ); ?></li> + <li>- <?php esc_html_e( 'If you get a 404 message, contact your web host. Their security may block XMLRPC.', 'jetpack' ); ?></li> + </ul> + </li> + <?php if ( current_user_can( 'jetpack_disconnect' ) && Jetpack::is_active() ) : ?> + <li> + <strong><em><?php esc_html_e( 'A connection problem with WordPress.com.', 'jetpack' ); ?></em></strong> + <?php + echo sprintf( + wp_kses( + /* translators: URL to disconnect and reconnect Jetpack. */ + __( 'Jetpack works by connecting to WordPress.com for a lot of features. Sometimes, when the connection gets messed up, you need to disconnect and reconnect to get things working properly. <a href="%s">Disconnect from WordPress.com</a>', 'jetpack' ), + array( + 'a' => array( + 'href' => array(), + 'class' => array(), + ), + ) + ), + esc_attr( + wp_nonce_url( + Jetpack::admin_url( + array( + 'page' => 'jetpack-debugger', + 'disconnect' => true, + ) + ), + 'jp_disconnect', + 'nonce' + ) + ) + ); + ?> + </li> + <?php endif; ?> + </ol> + <h4><?php esc_html_e( 'Still having trouble?', 'jetpack' ); ?></h4> + <p><b><em><?php esc_html_e( 'Ask us for help!', 'jetpack' ); ?></em></b> + <?php + echo sprintf( + wp_kses( + /* translators: URL for Jetpack support. */ + __( '<a href="%s">Contact our Happiness team</a>. When you do, please include the full debug information below.', 'jetpack' ), + array( 'a' => array( 'href' => array() ) ) + ), + 'https://jetpack.com/contact-support/' + ); + ?> + </p> + <hr /> + <?php if ( Jetpack::is_active() ) : ?> + <div id="connected-user-details"> + <h3><?php esc_html_e( 'More details about your Jetpack settings', 'jetpack' ); ?></h3> + <p> + <?php + printf( + wp_kses( + /* translators: %s is an e-mail address */ + __( 'The primary connection is owned by <strong>%s</strong>\'s WordPress.com account.', 'jetpack' ), + array( 'strong' => array() ) + ), + esc_html( Jetpack::get_master_user_email() ) + ); + ?> + </p> + </div> + <?php else : ?> + <div id="dev-mode-details"> + <p> + <?php + printf( + wp_kses( + /* translators: Link to a Jetpack support page. */ + __( 'Would you like to use Jetpack on your local development site? You can do so thanks to <a href="%s">Jetpack\'s development mode</a>.', 'jetpack' ), + array( 'a' => array( 'href' => array() ) ) + ), + 'https://jetpack.com/support/development-mode/' + ); + ?> + </p> + </div> + <?php endif; ?> + <?php + if ( + current_user_can( 'jetpack_manage_modules' ) + && ( Jetpack::is_development_mode() || Jetpack::is_active() ) + ) { + printf( + wp_kses( + '<p><a href="%1$s">%2$s</a></p>', + array( + 'a' => array( 'href' => array() ), + 'p' => array(), + ) + ), + esc_attr( Jetpack::admin_url( 'page=jetpack_modules' ) ), + esc_html__( 'Access the full list of Jetpack modules available on your site.', 'jetpack' ) + ); + } + ?> + </div> + <hr /> + <div id="toggle_debug_info"><?php esc_html_e( 'Advanced Debug Results', 'jetpack' ); ?></div> + <div id="debug_info_div"> + <h4><?php esc_html_e( 'Debug Info', 'jetpack' ); ?></h4> + <div id="debug_info"><pre><?php echo esc_html( $debug_info ); ?></pre></div> + </div> + </div> + <?php + } + + /** + * Outputs html needed within the <head> for the in-plugin debugger page. + */ + public static function jetpack_debug_admin_head() { + + Jetpack_Admin_Page::load_wrapper_styles(); + ?> + <style type="text/css"> + + .jetpack-debug-test-container { + margin-top: 20px; + margin-bottom: 30px; + } + + .jetpack-tests-succeed { + font-size: large; + color: #8BAB3E; + } + + .jetpack-test-details { + margin: 4px 6px; + padding: 10px; + overflow: auto; + display: none; + } + + .jetpack-test-error { + margin-bottom: 10px; + background: #FFEBE8; + border: solid 1px #C00; + border-radius: 3px; + } + + .jetpack-test-error p { + margin: 0; + padding: 0; + } + + p.jetpack-test-details { + margin: 4px 6px; + padding: 10px; + } + + .jetpack-test-error a.jetpack-test-heading { + padding: 4px 6px; + display: block; + text-decoration: none; + color: inherit; + } + + .jetpack-test-error .noticon { + float: right; + } + + .formbox { + margin: 0 0 25px 0; + } + + .formbox input[type="text"], .formbox input[type="email"], .formbox input[type="url"], .formbox textarea, #debug_info_div { + border: 1px solid #e5e5e5; + border-radius: 11px; + box-shadow: inset 0 1px 1px rgba(0,0,0,0.1); + color: #666; + font-size: 14px; + padding: 10px; + width: 97%; + } + #debug_info_div { + border-radius: 0; + margin-top: 16px; + background: #FFF; + padding: 16px; + } + .formbox .contact-support input[type="submit"] { + float: right; + margin: 0 !important; + border-radius: 20px !important; + cursor: pointer; + font-size: 13pt !important; + height: auto !important; + margin: 0 0 2em 10px !important; + padding: 8px 16px !important; + background-color: #ddd; + border: 1px solid rgba(0,0,0,0.05); + border-top-color: rgba(255,255,255,0.1); + border-bottom-color: rgba(0,0,0,0.15); + color: #333; + font-weight: 400; + display: inline-block; + text-align: center; + text-decoration: none; + } + + .formbox span.errormsg { + margin: 0 0 10px 10px; + color: #d00; + display: none; + } + + .formbox.error span.errormsg { + display: block; + } + + #debug_info_div, #toggle_debug_info, #debug_info_div p { + font-size: 12px; + } + + #category_div ul li { + list-style-type: none; + } + + </style> + <script type="text/javascript"> + jQuery( document ).ready( function($) { + + $( '#debug_info' ).prepend( 'jQuery version: ' + jQuery.fn.jquery + "\r\n" ); + $( '#debug_form_info' ).prepend( 'jQuery version: ' + jQuery.fn.jquery + "\r\n" ); + + $( '.jetpack-test-error .jetpack-test-heading' ).on( 'click', function() { + $( this ).parents( '.jetpack-test-error' ).find( '.jetpack-test-details' ).slideToggle(); + return false; + } ); + + } ); + </script> + <?php + } +} diff --git a/plugins/jetpack/_inc/lib/tracks/tracks-ajax.js b/plugins/jetpack/_inc/lib/tracks/tracks-ajax.js index e4169432..f9ed5cfd 100644 --- a/plugins/jetpack/_inc/lib/tracks/tracks-ajax.js +++ b/plugins/jetpack/_inc/lib/tracks/tracks-ajax.js @@ -1,7 +1,7 @@ /* global jpTracksAJAX, jQuery */ - (function( $, jpTracksAJAX ) { window.jpTracksAJAX = window.jpTracksAJAX || {}; + const debugSet = localStorage.getItem( 'debug' ) === 'dops:analytics'; window.jpTracksAJAX.record_ajax_event = function ( eventName, eventType, eventProp ) { var data = { @@ -15,11 +15,17 @@ return $.ajax( { type: 'POST', url: jpTracksAJAX.ajaxurl, - data: data + data: data, + success: function( response ) { + if ( debugSet ) { + // eslint-disable-next-line + console.log( 'AJAX tracks event recorded: ', data, response ); + } + } } ); }; - $( document ).ready( function () { + $( document ).ready( function() { $( 'body' ).on( 'click', '.jptracks a, a.jptracks', function( event ) { // We know that the jptracks element is either this, or its ancestor var $jptracks = $( this ).closest( '.jptracks' ); |