summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony G. Basile <blueness@gentoo.org>2017-11-20 16:50:38 -0500
committerAnthony G. Basile <blueness@gentoo.org>2017-11-20 16:50:38 -0500
commit159ec5c8052e1d061a430893a4525629849e2589 (patch)
tree6613f22dab82e5c683141f53b1281e18510896ef /plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php
parentUpdate akismet 4.0.1 (diff)
downloadblogs-gentoo-159ec5c8052e1d061a430893a4525629849e2589.tar.gz
blogs-gentoo-159ec5c8052e1d061a430893a4525629849e2589.tar.bz2
blogs-gentoo-159ec5c8052e1d061a430893a4525629849e2589.zip
Update jetpack 5.5
Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
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.php278
1 files changed, 230 insertions, 48 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 979dd57a..30cf823b 100644
--- a/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php
+++ b/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php
@@ -48,6 +48,7 @@ class Jetpack_Core_Json_Api_Endpoints {
// Load API endpoints
require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php';
require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php';
+ require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-widgets-endpoints.php';
self::$user_permissions_error_msg = esc_html__(
'You do not have the correct user permissions to perform this action.
@@ -64,6 +65,7 @@ class Jetpack_Core_Json_Api_Endpoints {
$module_data_endpoint = new Jetpack_Core_API_Module_Data_Endpoint();
$module_toggle_endpoint = new Jetpack_Core_API_Module_Toggle_Endpoint( new Jetpack_IXR_Client() );
$site_endpoint = new Jetpack_Core_API_Site_Endpoint();
+ $widget_endpoint = new Jetpack_Core_API_Widget_Endpoint();
register_rest_route( 'jetpack/v4', '/jitm', array(
'methods' => WP_REST_Server::READABLE,
@@ -75,6 +77,18 @@ class Jetpack_Core_Json_Api_Endpoints {
'callback' => __CLASS__ . '::delete_jitm_message'
) );
+ // Register a site
+ register_rest_route( 'jetpack/v4', '/verify_registration', array(
+ 'methods' => WP_REST_Server::EDITABLE,
+ 'callback' => __CLASS__ . '::verify_registration',
+ ) );
+
+ // Authorize a remote user
+ register_rest_route( 'jetpack/v4', '/remote_authorize', array(
+ 'methods' => WP_REST_Server::EDITABLE,
+ 'callback' => __CLASS__ . '::remote_authorize',
+ ) );
+
// Get current connection status of Jetpack
register_rest_route( 'jetpack/v4', '/connection', array(
'methods' => WP_REST_Server::READABLE,
@@ -320,30 +334,95 @@ class Jetpack_Core_Json_Api_Endpoints {
'callback' => __CLASS__ . '::get_plugin',
'permission_callback' => __CLASS__ . '::activate_plugins_permission_check',
) );
+
+ // Widgets: get information about a widget that supports it.
+ register_rest_route( 'jetpack/v4', '/widgets/(?P<id>[0-9a-z\-_]+)', array(
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $widget_endpoint, 'process' ),
+ 'permission_callback' => array( $widget_endpoint, 'can_request' ),
+ ) );
}
/**
+ * Asks for a jitm, unless they've been disabled, in which case it returns an empty array
+ *
* @param $request WP_REST_Request
*
- * @return array
+ * @return array An array of jitms
*/
public static function get_jitm_message( $request ) {
require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-jitm.php' );
$jitm = Jetpack_JITM::init();
+ if ( ! $jitm ) {
+ return array();
+ }
+
return $jitm->get_messages( $request['message_path'], urldecode_deep( $request['query'] ) );
}
+ /**
+ * Dismisses a jitm
+ * @param $request WP_REST_Request The request
+ *
+ * @return bool Always True
+ */
public static function delete_jitm_message( $request ) {
require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-jitm.php' );
$jitm = Jetpack_JITM::init();
+ if ( ! $jitm ) {
+ return true;
+ }
+
return $jitm->dismiss( $request['id'], $request['feature_class'] );
}
/**
+ * Handles verification that a site is registered
+ *
+ * @since 5.4.0
+ *
+ * @param WP_REST_Request $request The request sent to the WP REST API.
+ *
+ * @return array|wp-error
+ */
+ public static function verify_registration( $request ) {
+ require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php';
+ $xmlrpc_server = new Jetpack_XMLRPC_Server();
+ $result = $xmlrpc_server->verify_registration( array( $request['secret_1'], $request['state'] ) );
+
+ if ( is_a( $result, 'IXR_Error' ) ) {
+ $result = new WP_Error( $result->code, $result->message );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Handles verification that a site is registered
+ *
+ * @since 5.4.0
+ *
+ * @param WP_REST_Request $request The request sent to the WP REST API.
+ *
+ * @return array|wp-error
+ */
+ public static function remote_authorize( $request ) {
+ require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php';
+ $xmlrpc_server = new Jetpack_XMLRPC_Server();
+ $result = $xmlrpc_server->remote_authorize( $request );
+
+ if ( is_a( $result, 'IXR_Error' ) ) {
+ $result = new WP_Error( $result->code, $result->message );
+ }
+
+ return $result;
+ }
+
+ /**
* Handles dismissing of Jetpack Notices
*
* @since 4.3.0
@@ -716,38 +795,67 @@ class Jetpack_Core_Json_Api_Endpoints {
}
/**
- * Get site data, including for example, the site's current plan.
+ * Fetch site data from .com including the site's current plan.
*
- * @since 4.3.0
+ * @since 5.5.0
*
- * @return array Array of Jetpack modules.
+ * @return array Array of site properties.
*/
- public static function get_site_data() {
+ public static function site_data() {
+ $site_id = Jetpack_Options::get_option( 'id' );
- if ( $site_id = Jetpack_Options::get_option( 'id' ) ) {
+ if ( ! $site_id ) {
+ new WP_Error( 'site_id_missing' );
+ }
- $response = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d', $site_id ) .'?force=wpcom', '1.1' );
+ $response = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d', $site_id ) .'?force=wpcom', '1.1' );
- if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
- return new WP_Error( 'site_data_fetch_failed', esc_html__( 'Failed fetching site data. Try again later.', 'jetpack' ), array( 'status' => 400 ) );
- }
+ if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
+ 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 );
- // 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'] ) ) {
- if ( is_array( $results ) && isset( $results['plan'] ) ) {
- update_option( 'jetpack_active_plan', $results['plan'] );
+ // 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 );
+ }
+ /**
+ * Get site data, including for example, the site's current plan.
+ *
+ * @since 4.3.0
+ *
+ * @return array Array of site properties.
+ */
+ public static function get_site_data() {
+ $site_data = self::site_data();
+
+ if ( ! is_wp_error( $site_data ) ) {
return rest_ensure_response( array(
'code' => 'success',
'message' => esc_html__( 'Site data correctly received.', 'jetpack' ),
- 'data' => wp_remote_retrieve_body( $response ),
+ 'data' => json_encode( $site_data ),
)
);
}
+ if ( $site_data->get_error_code() === 'site_data_fetch_failed' ) {
+ return new WP_Error( 'site_data_fetch_failed', esc_html__( 'Failed fetching site data. Try again later.', 'jetpack' ), array( 'status' => 400 ) );
+ }
- return new WP_Error( 'site_id_missing', esc_html__( 'The ID of this site does not exist.', 'jetpack' ), array( 'status' => 404 ) );
+ if ( $site_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 ) );
+ }
}
/**
@@ -1723,6 +1831,39 @@ class Jetpack_Core_Json_Api_Endpoints {
'jp_group' => 'settings',
),
+ 'onboarding' => array(
+ 'description' => '',
+ 'type' => 'object',
+ 'default' => array(
+ 'token' => '',
+ 'siteTitle' => '',
+ 'siteDescription' => '',
+ 'genre' => 'blog',
+ 'businessPersonal' => 'personal',
+ 'businessInfo' => array(
+ 'businessName' => '',
+ 'businessAddress' => '',
+ 'businessCity' => '',
+ 'businessState' => '',
+ 'businessZipCode' => '',
+ ),
+ 'homepageFormat' => 'news',
+ 'addContactForm' => false,
+ 'end' => false,
+ ),
+ 'validate_callback' => __CLASS__ . '::validate_onboarding',
+ 'jp_group' => 'settings',
+ ),
+
+ // Show welcome for newly purchased plan
+ 'show_welcome_for_new_plan' => array(
+ 'description' => '',
+ 'type' => 'boolean',
+ 'default' => 0,
+ 'validate_callback' => __CLASS__ . '::validate_boolean',
+ 'jp_group' => 'settings',
+ ),
+
);
// Add modules to list so they can be toggled
@@ -1768,6 +1909,36 @@ class Jetpack_Core_Json_Api_Endpoints {
}
/**
+ * Validates that the parameters are proper values that can be set during Jetpack onboarding.
+ *
+ * @since 5.4.0
+ *
+ * @param array $onboarding_data Values to check.
+ * @param WP_REST_Request $request The request sent to the WP REST API.
+ * @param string $param Name of the parameter passed to endpoint holding $value.
+ *
+ * @return bool|WP_Error
+ */
+ public static function validate_onboarding( $onboarding_data, $request, $param ) {
+ if ( ! is_array( $onboarding_data ) ) {
+ return new WP_Error( 'invalid_param', esc_html__( 'Not valid onboarding data.', 'jetpack' ) );
+ }
+ foreach ( $onboarding_data as $value ) {
+ if ( is_string( $value ) ) {
+ $onboarding_choice = self::validate_string( $value, $request, $param );
+ } elseif ( is_array( $value ) ) {
+ $onboarding_choice = self::validate_onboarding( $value, $request, $param );
+ } else {
+ $onboarding_choice = self::validate_boolean( $value, $request, $param );
+ }
+ if ( is_wp_error( $onboarding_choice ) ) {
+ return $onboarding_choice;
+ }
+ }
+ return true;
+ }
+
+ /**
* Validates that the parameter is either a pure boolean or a numeric string that can be mapped to a boolean.
*
* @since 4.3.0
@@ -1776,7 +1947,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_boolean( $value, $request, $param ) {
if ( ! is_bool( $value ) && ! ( ( ctype_digit( $value ) || is_numeric( $value ) ) && in_array( $value, array( 0, 1 ) ) ) ) {
@@ -1794,7 +1965,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_posint( $value = 0, $request, $param ) {
if ( ! is_numeric( $value ) || $value <= 0 ) {
@@ -1812,7 +1983,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_list_item( $value = '', $request, $param ) {
$attributes = $request->get_attributes();
@@ -1843,7 +2014,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_module_list( $value = '', $request, $param ) {
if ( ! is_array( $value ) ) {
@@ -1868,7 +2039,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_alphanum( $value = '', $request, $param ) {
if ( ! empty( $value ) && ( ! is_string( $value ) || ! preg_match( '/^[a-z0-9]+$/i', $value ) ) ) {
@@ -1886,7 +2057,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_verification_service( $value = '', $request, $param ) {
if ( ! empty( $value ) && ! ( is_string( $value ) && ( preg_match( '/^[a-z0-9_-]+$/i', $value ) || preg_match( '#^<meta name="([a-z0-9_\-.:]+)?" content="([a-z0-9_-]+)?" />$#i', $value ) ) ) ) {
@@ -1904,7 +2075,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_stats_roles( $value, $request, $param ) {
if ( ! empty( $value ) && ! array_intersect( self::$stats_roles, $value ) ) {
@@ -1925,7 +2096,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_sharing_show( $value, $request, $param ) {
$views = array( 'index', 'post', 'page', 'attachment', 'jetpack-portfolio' );
@@ -1955,7 +2126,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_services( $value, $request, $param ) {
if ( ! is_array( $value ) || ! isset( $value['visible'] ) || ! isset( $value['hidden'] ) ) {
@@ -1995,7 +2166,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_custom_service( $value, $request, $param ) {
if ( ! is_array( $value ) || ! isset( $value['sharing_name'] ) || ! isset( $value['sharing_url'] ) || ! isset( $value['sharing_icon'] ) ) {
@@ -2028,7 +2199,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_custom_service_id( $value = '', $request, $param ) {
if ( ! empty( $value ) && ( ! is_string( $value ) || ! preg_match( '/custom\-[0-1]+/i', $value ) ) ) {
@@ -2057,7 +2228,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_twitter_username( $value = '', $request, $param ) {
if ( ! empty( $value ) && ( ! is_string( $value ) || ! preg_match( '/^@?\w{1,15}$/i', $value ) ) ) {
@@ -2075,7 +2246,7 @@ class Jetpack_Core_Json_Api_Endpoints {
* @param WP_REST_Request $request The request sent to the WP REST API.
* @param string $param Name of the parameter passed to endpoint holding $value.
*
- * @return bool
+ * @return bool|WP_Error
*/
public static function validate_string( $value = '', $request, $param ) {
if ( ! is_string( $value ) ) {
@@ -2092,7 +2263,7 @@ class Jetpack_Core_Json_Api_Endpoints {
*
* @param string|bool $value Value to check.
*
- * @return bool
+ * @return bool|array
*/
public static function sanitize_stats_allowed_roles( $value ) {
if ( empty( $value ) ) {
@@ -2108,7 +2279,7 @@ class Jetpack_Core_Json_Api_Endpoints {
*
* @param string $route Regular expression for the endpoint with the module slug to return.
*
- * @return array
+ * @return array|string
*/
public static function get_module_requested( $route = '/module/(?P<slug>[a-z\-]+)' ) {
@@ -2130,10 +2301,10 @@ class Jetpack_Core_Json_Api_Endpoints {
*
* @since 4.3.0
*
- * @param string $modules Can be a single module or a list of modules.
- * @param null|string $slug Slug of the module in the first parameter.
+ * @param string|array $modules Can be a single module or a list of modules.
+ * @param null|string $slug Slug of the module in the first parameter.
*
- * @return array
+ * @return array|string
*/
public static function prepare_modules_for_response( $modules = '', $slug = null ) {
global $wp_rewrite;
@@ -2181,19 +2352,6 @@ class Jetpack_Core_Json_Api_Endpoints {
return $options;
}
- foreach ( $options as $key => $value ) {
-
- if ( isset( $options[ $key ]['validate_callback'] ) ) {
- unset( $options[ $key ]['validate_callback'] );
- }
-
- $default_value = isset( $options[ $key ]['default'] ) ? $options[ $key ]['default'] : '';
-
- $current_value = get_option( $key, $default_value );
-
- $options[ $key ]['current_value'] = self::cast_value( $current_value, $options[ $key ] );
- }
-
// Some modules need special treatment.
switch ( $module ) {
@@ -2243,6 +2401,12 @@ class Jetpack_Core_Json_Api_Endpoints {
$sharer = new Sharing_Service();
$options = self::split_options( $options, $sharer->get_global_options() );
$options['sharing_services']['current_value'] = $sharer->get_blog_services();
+ $other_sharedaddy_options = array( 'jetpack-twitter-cards-site-tag', 'sharedaddy_disable_resources', 'sharing_delete_service' );
+ foreach ( $other_sharedaddy_options as $key ) {
+ $default_value = isset( $options[ $key ]['default'] ) ? $options[ $key ]['default'] : '';
+ $current_value = get_option( $key, $default_value );
+ $options[ $key ]['current_value'] = self::cast_value( $current_value, $options[ $key ] );
+ }
break;
case 'after-the-deadline':
@@ -2267,8 +2431,26 @@ class Jetpack_Core_Json_Api_Endpoints {
}
$options = self::split_options( $options, stats_get_options() );
break;
+ default:
+ // These option are just stored as plain WordPress options.
+ foreach ( $options as $key => $value ) {
+ $default_value = isset( $options[ $key ]['default'] ) ? $options[ $key ]['default'] : '';
+ $current_value = get_option( $key, $default_value );
+ $options[ $key ]['current_value'] = self::cast_value( $current_value, $options[ $key ] );
+ }
+ }
+ // At this point some options have current_value not set because they're options
+ // that only get written on update, so we set current_value to the default one.
+ foreach ( $options as $key => $value ) {
+ // We don't need validate_callback in the response
+ if ( isset( $options[ $key ]['validate_callback'] ) ) {
+ unset( $options[ $key ]['validate_callback'] );
+ }
+ $default_value = isset( $options[ $key ]['default'] ) ? $options[ $key ]['default'] : '';
+ if ( ! array_key_exists( 'current_value', $options[ $key ] ) ) {
+ $options[ $key ]['current_value'] = self::cast_value( $default_value, $options[ $key ] );
+ }
}
-
return $options;
}