Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
21.43% |
6 / 28 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_REST_API_V2_Endpoint_Update_Schedules_Active | |
21.43% |
6 / 28 |
|
0.00% |
0 / 4 |
30.77 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| register_routes | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
2 | |||
| update_item_permissions_check | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| update_item | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Endpoint to manage plugin and theme update schedules active status. |
| 4 | * |
| 5 | * Example: https://public-api.wordpress.com/wpcom/v2/update-schedules/$ID/active |
| 6 | * |
| 7 | * @package automattic/scheduled-updates |
| 8 | */ |
| 9 | |
| 10 | use Automattic\Jetpack\Scheduled_Updates; |
| 11 | use Automattic\Jetpack\Scheduled_Updates_Active; |
| 12 | |
| 13 | /** |
| 14 | * Class WPCOM_REST_API_V2_Endpoint_Update_Schedules_Active |
| 15 | */ |
| 16 | class WPCOM_REST_API_V2_Endpoint_Update_Schedules_Active extends WP_REST_Controller { |
| 17 | /** |
| 18 | * The namespace of this controller's route. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | public $namespace = 'wpcom/v2'; |
| 23 | |
| 24 | /** |
| 25 | * The base of this controller's route. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | public $rest_base = 'update-schedules'; |
| 30 | |
| 31 | /** |
| 32 | * WPCOM_REST_API_V2_Endpoint_Update_Schedules_Active constructor. |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Register routes. |
| 40 | */ |
| 41 | public function register_routes() { |
| 42 | register_rest_route( |
| 43 | $this->namespace, |
| 44 | '/' . $this->rest_base . '/(?P<schedule_id>[\w]+)/active', |
| 45 | array( |
| 46 | array( |
| 47 | 'methods' => WP_REST_Server::EDITABLE, |
| 48 | 'callback' => array( $this, 'update_item' ), |
| 49 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
| 50 | 'args' => array( |
| 51 | 'active' => array( |
| 52 | 'description' => 'Whether the schedule is active.', |
| 53 | 'type' => 'boolean', |
| 54 | 'required' => true, |
| 55 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 56 | ), |
| 57 | ), |
| 58 | ), |
| 59 | ) |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Permission check for updating active status. |
| 65 | * |
| 66 | * @param WP_REST_Request $request Request object. |
| 67 | * @return bool|WP_Error |
| 68 | */ |
| 69 | public function update_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 70 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 71 | return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to access this endpoint.', 'jetpack-scheduled-updates' ), array( 'status' => 403 ) ); |
| 72 | } |
| 73 | |
| 74 | return current_user_can( 'update_plugins' ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Updates active of an existing update schedule. |
| 79 | * |
| 80 | * @param WP_REST_Request $request Request object. |
| 81 | * @return WP_REST_Response|WP_Error The updated active value or a WP_Error if the schedule could not be found. |
| 82 | */ |
| 83 | public function update_item( $request ) { |
| 84 | $events = wp_get_scheduled_events( Scheduled_Updates::PLUGIN_CRON_HOOK ); |
| 85 | |
| 86 | if ( empty( $events[ $request['schedule_id'] ] ) ) { |
| 87 | return new WP_Error( 'rest_invalid_schedule', __( 'The schedule could not be found.', 'jetpack-scheduled-updates' ), array( 'status' => 404 ) ); |
| 88 | } |
| 89 | |
| 90 | Scheduled_Updates_Active::update( $request['schedule_id'], (bool) $request['active'] ); |
| 91 | |
| 92 | return rest_ensure_response( array( 'active' => (bool) $request['active'] ) ); |
| 93 | } |
| 94 | } |