Skip to content

Commit

Permalink
Add Scheduled Updates /active endpoint (#37130)
Browse files Browse the repository at this point in the history
* Add /active endpoint

* changelog

* Fix: status required

* Move /active endpoint to separate class

* Remove default value

* Add sanitize

* Update sync option

* Fix: wrong function name
  • Loading branch information
zaerl authored May 2, 2024
1 parent 8143549 commit bc58ae0
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added a new Scheduled Updates active endpoint
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,6 @@ public static function clear( $schedule_id ) {
}
}

/**
* Update the active value for a scheduled update hook.
*
* @param string $id The ID of the schedule.
* @param object $event The event object.
* @param \WP_REST_Request $request The request object.
* @return bool
*/
public static function updates_active( $id, $event, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$schedule = $request['schedule'];
$active = $schedule['active'] ?? true;

return self::update( $id, $active );
}

/**
* REST prepare_item_for_response filter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public static function init() {
add_filter( 'plugin_auto_update_setting_html', array( Scheduled_Updates_Admin::class, 'show_scheduled_updates' ), 10, 2 );

add_action( 'jetpack_scheduled_update_created', array( __CLASS__, 'maybe_disable_autoupdates' ), 10, 3 );
add_action( 'jetpack_scheduled_update_created', array( Scheduled_Updates_Active::class, 'updates_active' ), 10, 3 );
add_action( 'jetpack_scheduled_update_created', array( Scheduled_Updates_Health_Paths::class, 'updates_health_paths' ), 10, 3 );

add_action( 'jetpack_scheduled_update_updated', array( Scheduled_Updates_Logs::class, 'replace_logs_schedule_id' ), 10, 2 );
Expand All @@ -83,6 +82,10 @@ public static function init() {
add_action( 'add_option_' . Scheduled_Updates_Logs::OPTION_NAME, $callback );
add_action( 'update_option_' . Scheduled_Updates_Logs::OPTION_NAME, $callback );

// Active flag saving.
add_action( 'add_option_' . Scheduled_Updates_Active::OPTION_NAME, $callback );
add_action( 'update_option_' . Scheduled_Updates_Active::OPTION_NAME, $callback );

// This is a temporary solution for backward compatibility. It will be removed in the future.
// It's needed to ensure that preexisting schedules are loaded into the sync option.
if ( false === get_option( self::PLUGIN_CRON_HOOK ) ) {
Expand All @@ -109,6 +112,7 @@ public static function load_rest_api_endpoints() {
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Update_Schedules_Capabilities' );
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Update_Schedules_Logs' );
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Update_Schedules_Status' );
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Update_Schedules_Active' );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Endpoint to manage plugin and theme update schedules active status.
*
* Example: https://public-api.wordpress.com/wpcom/v2/update-schedules/$ID/active
*
* @package automattic/scheduled-updates
*/

use Automattic\Jetpack\Scheduled_Updates;
use Automattic\Jetpack\Scheduled_Updates_Active;

/**
* Class WPCOM_REST_API_V2_Endpoint_Update_Schedules_Active
*/
class WPCOM_REST_API_V2_Endpoint_Update_Schedules_Active extends WP_REST_Controller {
/**
* The namespace of this controller's route.
*
* @var string
*/
public $namespace = 'wpcom/v2';

/**
* The base of this controller's route.
*
* @var string
*/
public $rest_base = 'update-schedules';

/**
* WPCOM_REST_API_V2_Endpoint_Update_Schedules_Active constructor.
*/
public function __construct() {
// Priority 9 to run before the main endpoint and avoid "/active" being treated as a schedule ID.
add_action( 'rest_api_init', array( $this, 'register_routes' ), 9 );
}

/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<schedule_id>[\w]+)/active',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'args' => array(
'active' => array(
'description' => 'Whether the schedule is active.',
'type' => 'boolean',
'required' => true,
'sanitize_callback' => 'rest_sanitize_boolean',
),
),
),
)
);
}

/**
* Permission check for updating active status.
*
* @param WP_REST_Request $request Request object.
* @return bool|WP_Error
*/
public function update_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to access this endpoint.', 'jetpack-scheduled-updates' ), array( 'status' => 403 ) );
}

return current_user_can( 'update_plugins' );
}

/**
* Updates active of an existing update schedule.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error The updated active value or a WP_Error if the schedule could not be found.
*/
public function update_item( $request ) {
$events = wp_get_scheduled_events( Scheduled_Updates::PLUGIN_CRON_HOOK );

if ( empty( $events[ $request['schedule_id'] ] ) ) {
return new WP_Error( 'rest_invalid_schedule', __( 'The schedule could not be found.', 'jetpack-scheduled-updates' ), array( 'status' => 404 ) );
}

Scheduled_Updates_Active::update( $request['schedule_id'], (bool) $request['active'] );

return rest_ensure_response( array( 'active' => (bool) $request['active'] ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,6 @@ public function get_object_params() {
'type' => 'integer',
'required' => true,
),
'active' => array(
'description' => 'Whether the schedule is active.',
'type' => 'boolean',
'required' => false,
'default' => true,
),
'health_check_paths' => array(
'description' => 'List of paths to check for site health after the update.',
'type' => 'array',
Expand Down
Loading

0 comments on commit bc58ae0

Please sign in to comment.