← All changes
|
jetpack_vendor/automattic/jetpack-protect-status/src/class-rest-controller.php
+112
-0
16.2-beta
→
16.3-beta
View file →
| @@ -1,0 +1,112 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Class file for managing REST API endpoints for package protect-status. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-protect-status | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\Protect_Status; | |
| 9 | + | |
| 10 | +use Automattic\Jetpack\Connection\Rest_Authentication; | |
| 11 | +use WP_REST_Request; | |
| 12 | +use WP_REST_Response; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Class REST_Controller | |
| 16 | + */ | |
| 17 | +class REST_Controller { | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Initialize the plugin's REST API. | |
| 21 | + * | |
| 22 | + * @return void | |
| 23 | + */ | |
| 24 | + public static function init() { | |
| 25 | + // Set up the REST authentication hooks. | |
| 26 | + Rest_Authentication::init(); | |
| 27 | + | |
| 28 | + // Add custom WP REST API endoints. | |
| 29 | + add_action( 'rest_api_init', array( __CLASS__, 'register_rest_endpoints' ) ); | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Register the REST API routes. | |
| 34 | + * | |
| 35 | + * @return void | |
| 36 | + */ | |
| 37 | + public static function register_rest_endpoints() { | |
| 38 | + register_rest_route( | |
| 39 | + 'jetpack-protect/v1', | |
| 40 | + 'check-plan', | |
| 41 | + array( | |
| 42 | + 'methods' => \WP_REST_Server::READABLE, | |
| 43 | + 'callback' => __CLASS__ . '::api_check_plan', | |
| 44 | + 'permission_callback' => function () { | |
| 45 | + return current_user_can( 'manage_options' ); | |
| 46 | + }, | |
| 47 | + ) | |
| 48 | + ); | |
| 49 | + | |
| 50 | + register_rest_route( | |
| 51 | + 'jetpack-protect/v1', | |
| 52 | + 'status', | |
| 53 | + array( | |
| 54 | + 'methods' => \WP_REST_Server::READABLE, | |
| 55 | + 'callback' => __CLASS__ . '::api_get_status', | |
| 56 | + 'permission_callback' => function () { | |
| 57 | + return current_user_can( 'manage_options' ); | |
| 58 | + }, | |
| 59 | + ) | |
| 60 | + ); | |
| 61 | + | |
| 62 | + register_rest_route( | |
| 63 | + 'jetpack-protect/v1', | |
| 64 | + 'clear-scan-cache', | |
| 65 | + array( | |
| 66 | + 'methods' => \WP_REST_Server::EDITABLE, | |
| 67 | + 'callback' => __CLASS__ . '::api_clear_scan_cache', | |
| 68 | + 'permission_callback' => function () { | |
| 69 | + return current_user_can( 'manage_options' ); | |
| 70 | + }, | |
| 71 | + ) | |
| 72 | + ); | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Return site plan data for the API endpoint | |
| 77 | + * | |
| 78 | + * @return WP_REST_Response | |
| 79 | + */ | |
| 80 | + public static function api_check_plan() { | |
| 81 | + $has_required_plan = Plan::has_required_plan(); | |
| 82 | + | |
| 83 | + return rest_ensure_response( $has_required_plan ); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Return Protect Status for the API endpoint | |
| 88 | + * | |
| 89 | + * @param WP_REST_Request $request The request object. | |
| 90 | + * | |
| 91 | + * @return WP_REST_Response | |
| 92 | + */ | |
| 93 | + public static function api_get_status( $request ) { | |
| 94 | + $status = Status::get_status( $request['hard_refresh'] ); | |
| 95 | + return rest_ensure_response( $status ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Clear the Scan_Status cache for the API endpoint | |
| 100 | + * | |
| 101 | + * @return WP_REST_Response | |
| 102 | + */ | |
| 103 | + public static function api_clear_scan_cache() { | |
| 104 | + $cache_cleared = Scan_Status::delete_option(); | |
| 105 | + | |
| 106 | + if ( ! $cache_cleared ) { | |
| 107 | + return new WP_REST_Response( 'An error occurred while attempting to clear the Jetpack Scan cache.', 500 ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + return new WP_REST_Response( 'Jetpack Scan cache cleared.' ); | |
| 111 | + } | |
| 112 | +} | |