diff options
Diffstat (limited to 'plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php')
-rw-r--r-- | plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php | 139 |
1 files changed, 124 insertions, 15 deletions
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 30cf823b..60e73540 100644 --- a/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php +++ b/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php @@ -67,6 +67,13 @@ class Jetpack_Core_Json_Api_Endpoints { $site_endpoint = new Jetpack_Core_API_Site_Endpoint(); $widget_endpoint = new Jetpack_Core_API_Widget_Endpoint(); + register_rest_route( 'jetpack/v4', 'plans', array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => __CLASS__ . '::get_plans', + 'permission_callback' => __CLASS__ . '::connect_url_permission_callback', + + ) ); + register_rest_route( 'jetpack/v4', '/jitm', array( 'methods' => WP_REST_Server::READABLE, 'callback' => __CLASS__ . '::get_jitm_message', @@ -95,6 +102,12 @@ class Jetpack_Core_Json_Api_Endpoints { 'callback' => __CLASS__ . '::jetpack_connection_status', ) ); + register_rest_route( 'jetpack/v4', '/rewind', array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => __CLASS__ . '::get_rewind_data', + 'permission_callback' => __CLASS__ . '::view_admin_page_permission_check', + ) ); + // Fetches a fresh connect URL register_rest_route( 'jetpack/v4', '/connection/url', array( 'methods' => WP_REST_Server::READABLE, @@ -343,6 +356,45 @@ class Jetpack_Core_Json_Api_Endpoints { ) ); } + public static function get_plans( $request ) { + /** + * Filter to turn off caching of Jetpack plans + * + * @since 5.9.0 + * + * @param bool true Whether to cache Jetpack plans locally + */ + $use_cache = apply_filters( 'jetpack_cache_plans', true ); + + $data = $use_cache ? get_transient( 'jetpack_plans' ) : false; + + if ( false === $data ) { + $path = '/plans'; + // passing along from client to help geolocate currency + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; // if we already have an list of forwarded ips, then just use that + if ( empty( $ip ) ) { + $ip = $_SERVER['HTTP_CLIENT_IP']; // another popular one for proxy servers + } + if ( empty( $ip ) ) { + $ip = $_SERVER['REMOTE_ADDR']; // if we don't have an ip by now, take the closest node's ip (likely directly connected client) + } + $request = Jetpack_Client::wpcom_json_api_request_as_blog( $path, '2', array( 'headers' => array( 'X-Forwarded-For' => $ip ) ), null, 'wpcom' ); + $body = wp_remote_retrieve_body( $request ); + if ( 200 === wp_remote_retrieve_response_code( $request ) ) { + $data = $body; + } else { + // something went wrong so we'll just return the response without caching + return $body; + } + + if ( true === $use_cache ) { + set_transient( 'jetpack_plans', $data, DAY_IN_SECONDS ); + } + } + + return $data; + } + /** * Asks for a jitm, unless they've been disabled, in which case it returns an empty array * @@ -647,6 +699,58 @@ class Jetpack_Core_Json_Api_Endpoints { ); } + public static function rewind_data() { + $site_id = Jetpack_Options::get_option( 'id' ); + + if ( ! $site_id ) { + return new WP_Error( 'site_id_missing' ); + } + + $response = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d/rewind', $site_id ) .'?force=wpcom', '2', array(), null, 'wpcom' ); + + if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { + return new WP_Error( 'rewind_data_fetch_failed' ); + } + + $body = wp_remote_retrieve_body( $response ); + + return json_decode( $body ); + } + + /** + * Get rewind data + * + * @since 5.7.0 + * + * @return array Array of rewind properties. + */ + public static function get_rewind_data() { + $rewind_data = self::rewind_data(); + + if ( ! is_wp_error( $rewind_data ) ) { + return rest_ensure_response( array( + 'code' => 'success', + 'message' => esc_html__( 'Rewind data correctly received.', 'jetpack' ), + 'data' => wp_json_encode( $rewind_data ), + ) + ); + } + + if ( $rewind_data->get_error_code() === 'rewind_data_fetch_failed' ) { + return new WP_Error( 'rewind_data_fetch_failed', esc_html__( 'Failed fetching rewind data. Try again later.', 'jetpack' ), array( 'status' => 400 ) ); + } + + if ( $rewind_data->get_error_code() === 'site_id_missing' ) { + return new WP_Error( 'site_id_missing', esc_html__( 'The ID of this site does not exist.', 'jetpack' ), array( 'status' => 404 ) ); + } + + return new WP_Error( + 'error_get_rewind_data', + esc_html__( 'Could not retrieve Rewind data.', 'jetpack' ), + array( 'status' => 500 ) + ); + } + /** * Disconnects Jetpack from the WordPress.com Servers * @@ -1673,7 +1777,7 @@ class Jetpack_Core_Json_Api_Endpoints { 'enable_header_ad' => array( 'description' => esc_html__( 'Display an ad unit at the top of each page.', 'jetpack' ), 'type' => 'boolean', - 'default' => 0, + 'default' => 1, 'validate_callback' => __CLASS__ . '::validate_boolean', 'jp_group' => 'wordads', ), @@ -1831,25 +1935,30 @@ class Jetpack_Core_Json_Api_Endpoints { 'jp_group' => 'settings', ), + 'lang_id' => array( + 'description' => esc_html__( 'Primary language for the site.', 'jetpack' ), + 'type' => 'string', + 'default' => 'en_US', + 'jp_group' => 'settings', + ), + 'onboarding' => array( 'description' => '', 'type' => 'object', 'default' => array( - 'token' => '', - 'siteTitle' => '', - 'siteDescription' => '', - 'genre' => 'blog', - 'businessPersonal' => 'personal', - 'businessInfo' => array( - 'businessName' => '', - 'businessAddress' => '', - 'businessCity' => '', - 'businessState' => '', - 'businessZipCode' => '', + 'siteTitle' => '', + 'siteDescription' => '', + 'siteType' => 'personal', + 'homepageFormat' => 'posts', + 'addContactForm' => 0, + 'businessAddress' => array( + 'name' => '', + 'street' => '', + 'city' => '', + 'state' => '', + 'zip' => '', ), - 'homepageFormat' => 'news', - 'addContactForm' => false, - 'end' => false, + 'installWooCommerce' => false, ), 'validate_callback' => __CLASS__ . '::validate_onboarding', 'jp_group' => 'settings', |