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 | 150 |
1 files changed, 85 insertions, 65 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 60e73540..71bccc4c 100644 --- a/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php +++ b/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php @@ -122,6 +122,23 @@ class Jetpack_Core_Json_Api_Endpoints { 'permission_callback' => __CLASS__ . '::get_user_connection_data_permission_callback', ) ); + // Current user: get or set tracking settings. + register_rest_route( 'jetpack/v4', '/tracking/settings', array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => __CLASS__ . '::get_user_tracking_settings', + 'permission_callback' => __CLASS__ . '::view_admin_page_permission_check', + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => __CLASS__ . '::update_user_tracking_settings', + 'permission_callback' => __CLASS__ . '::view_admin_page_permission_check', + 'args' => array( + 'tracks_opt_out' => array( 'type' => 'boolean' ), + ), + ), + ) ); + // Disconnect site from WordPress.com servers register_rest_route( 'jetpack/v4', '/connection', array( 'methods' => WP_REST_Server::EDITABLE, @@ -816,62 +833,6 @@ class Jetpack_Core_Json_Api_Endpoints { } /** - * Returns the proper name for Jetpack Holiday Snow setting. - * When the REST route starts, the holiday-snow.php file where jetpack_holiday_snow_option_name() function is defined is not loaded, - * so where using this to replicate it and have the same functionality. - * - * @since 4.4.0 - * - * @return string - */ - public static function holiday_snow_option_name() { - /** This filter is documented in modules/holiday-snow.php */ - return apply_filters( 'jetpack_holiday_snow_option_name', 'jetpack_holiday_snow_enabled' ); - } - - /** - * Update a single miscellaneous setting for this Jetpack installation, like Holiday Snow. - * - * @since 4.3.0 - * - * @param WP_REST_Request $request The request sent to the WP REST API. - * - * @return object Jetpack miscellaneous settings. - */ - public static function update_setting( $request ) { - // Get parameters to update the module. - $param = $request->get_params(); - - // Exit if no parameters were passed. - if ( ! is_array( $param ) ) { - return new WP_Error( 'missing_setting', esc_html__( 'Missing setting.', 'jetpack' ), array( 'status' => 404 ) ); - } - - // Get option name and value. - $option = key( $param ); - $value = current( $param ); - - // Log success or not - $updated = false; - - switch ( $option ) { - case self::holiday_snow_option_name(): - $updated = update_option( $option, ( true == (bool) $value ) ? 'letitsnow' : '' ); - break; - } - - if ( $updated ) { - return rest_ensure_response( array( - 'code' => 'success', - 'message' => esc_html__( 'Setting updated.', 'jetpack' ), - 'value' => $value, - ) ); - } - - return new WP_Error( 'setting_not_updated', esc_html__( 'The setting was not updated.', 'jetpack' ), array( 'status' => 400 ) ); - } - - /** * Unlinks current user from the WordPress.com Servers. * * @since 4.3.0 @@ -899,6 +860,74 @@ class Jetpack_Core_Json_Api_Endpoints { } /** + * Gets current user's tracking settings. + * + * @since 6.0.0 + * + * @param WP_REST_Request $request The request sent to the WP REST API. + * + * @return WP_REST_Response|WP_Error Response, else error. + */ + public static function get_user_tracking_settings( $request ) { + if ( ! Jetpack::is_user_connected() ) { + $response = array( + 'tracks_opt_out' => true, // Default to opt-out if not connected to wp.com. + ); + } else { + $response = Jetpack_Client::wpcom_json_api_request_as_user( + '/jetpack-user-tracking', + 'v2', + array( + 'method' => 'GET', + 'headers' => array( + 'X-Forwarded-For' => Jetpack::current_user_ip( true ), + ), + ) + ); + if ( ! is_wp_error( $response ) ) { + $response = json_decode( wp_remote_retrieve_body( $response ), true ); + } + } + + return rest_ensure_response( $response ); + } + + /** + * Updates current user's tracking settings. + * + * @since 6.0.0 + * + * @param WP_REST_Request $request The request sent to the WP REST API. + * + * @return WP_REST_Response|WP_Error Response, else error. + */ + public static function update_user_tracking_settings( $request ) { + if ( ! Jetpack::is_user_connected() ) { + $response = array( + 'tracks_opt_out' => true, // Default to opt-out if not connected to wp.com. + ); + } else { + $response = Jetpack_Client::wpcom_json_api_request_as_user( + '/jetpack-user-tracking', + 'v2', + array( + 'method' => 'PUT', + 'headers' => array( + 'Content-Type' => 'application/json', + 'X-Forwarded-For' => Jetpack::current_user_ip( true ), + ), + ), + wp_json_encode( $request->get_params() ) + ); + if ( ! is_wp_error( $response ) ) { + $response = json_decode( wp_remote_retrieve_body( $response ), true ); + } + } + + return rest_ensure_response( $response ); + } + + /** * Fetch site data from .com including the site's current plan. * * @since 5.5.0 @@ -1891,15 +1920,6 @@ class Jetpack_Core_Json_Api_Endpoints { 'jp_group' => 'stats', ), - // Settings - Not a module - self::holiday_snow_option_name() => array( - 'description' => '', - 'type' => 'boolean', - 'default' => 0, - 'validate_callback' => __CLASS__ . '::validate_boolean', - 'jp_group' => 'settings', - ), - // Akismet - Not a module, but a plugin. The options can be passed and handled differently. 'akismet_show_user_comments_approved' => array( 'description' => '', |