Skip to content

Commit

Permalink
Scheduled Updates: Pass status fields directly (#36644)
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland authored Apr 12, 2024
1 parent 5437048 commit ff17b59
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: No functional changes, just moved some code around


Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class WPCOM_REST_API_V2_Endpoint_Update_Schedules extends WP_REST_Controller {
*/
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
add_action( 'rest_api_init', array( $this, 'add_status_fields' ) );
}

/**
Expand Down Expand Up @@ -189,40 +188,6 @@ public function register_routes() {
);
}

/**
* Add status fields to the jetpack_scheduled_plugins_update object.
*/
public function add_status_fields() {
$object_type = $this->get_object_type();

register_rest_field(
$object_type,
'last_run_timestamp',
array(
'get_callback' => array( $this, 'get_last_run_field' ),
'update_callback' => null,
'schema' => array(
'description' => 'Unix timestamp (UTC) for when the last run occurred.',
'type' => 'integer',
),
)
);

register_rest_field(
$object_type,
'last_run_status',
array(
'get_callback' => array( $this, 'get_last_run_field' ),
'update_callback' => null,
'schema' => array(
'description' => 'Status of last run.',
'type' => 'string',
'enum' => array( 'success', 'failure-and-rollback', 'failure-and-rollback-fail' ),
),
)
);
}

/**
* Permission check for retrieving schedules.
*
Expand All @@ -247,12 +212,14 @@ public function get_items( $request ) {
$events = wp_get_scheduled_events( Scheduled_Updates::PLUGIN_CRON_HOOK );
$response = array();

foreach ( array_keys( $events ) as $schedule_id ) {
foreach ( $events as $schedule_id => $event ) {
// Add the schedule_id to the object.
$events[ $schedule_id ]->schedule_id = $schedule_id;
$event->schedule_id = $schedule_id;

// Run through the prepare_item_for_response method to add the last run status.
$response[ $schedule_id ] = $this->prepare_item_for_response( $events[ $schedule_id ], $request )->data;
$response[ $schedule_id ] = $this->prepare_response_for_collection(
$this->prepare_item_for_response( $event, $request )
);
}

return rest_ensure_response( $response );
Expand Down Expand Up @@ -386,7 +353,7 @@ public function update_item( $request ) {

$item = $this->create_item( $request );

// Sets the previous status
// Sets the previous status.
if ( $previous_schedule_status ) {
Scheduled_Updates::set_scheduled_update_status( $item->data, $previous_schedule_status['last_run_timestamp'], $previous_schedule_status['last_run_status'] );
}
Expand Down Expand Up @@ -426,25 +393,6 @@ public function update_status( $request ) {
return rest_ensure_response( $option[ $request['schedule_id'] ] );
}

/**
* Get the last run value of a schedule.
*
* @param array $item Prepared response object.
* @param string $field_name Field name.
* @param WP_REST_Request $request Full details about the request.
* @param string $object_type Object type.
* @return object|null
*/
public function get_last_run_field( $item, $field_name, $request, $object_type ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$status = Scheduled_Updates::get_scheduled_update_status( $item['schedule_id'] );

if ( $status ) {
return $status[ $field_name ];
}

return null;
}

/**
* Permission check for adding a log entry
*
Expand Down Expand Up @@ -565,15 +513,49 @@ public function delete_item( $request ) {
return rest_ensure_response( true );
}

/**
* Prepares a response for insertion into a collection.
*
* @param WP_REST_Response $response Response object.
* @return array|mixed Response data, ready for insertion into collection data.
*/
public function prepare_response_for_collection( $response ) {
if ( ! ( $response instanceof WP_REST_Response ) ) {
return $response;
}

$data = (array) $response->get_data();
$server = rest_get_server();
$links = $server::get_compact_response_links( $response );

if ( ! empty( $links ) ) {
$data['_links'] = $links;
}

return $data;
}

/**
* Prepares the scheduled update for the REST response.
*
* @param mixed $item WordPress representation of the item.
* @param object $item WP Cron event.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response Response object on success.
*/
public function prepare_item_for_response( $item, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$item = $this->add_additional_fields_to_object( (array) $item, $request );
public function prepare_item_for_response( $item, $request ) {
$item = (array) $item;

$status = Scheduled_Updates::get_scheduled_update_status( $item['schedule_id'] );
if ( ! $status ) {
$status = array(
'last_run_timestamp' => null,
'last_run_status' => null,
);
}

$item = array_merge( $item, $status );

$item = $this->add_additional_fields_to_object( $item, $request );

// Remove schedule ID, not needed in the response.
unset( $item['schedule_id'] );
Expand Down

0 comments on commit ff17b59

Please sign in to comment.