From ff17b59347cd29f9113174420ced10c7526670f4 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Fri, 12 Apr 2024 19:03:36 +0200 Subject: [PATCH] Scheduled Updates: Pass status fields directly (#36644) --- .../changelog/move-status-fields | 5 + ...-rest-api-v2-endpoint-update-schedules.php | 104 ++++++++---------- 2 files changed, 48 insertions(+), 61 deletions(-) create mode 100644 projects/packages/scheduled-updates/changelog/move-status-fields diff --git a/projects/packages/scheduled-updates/changelog/move-status-fields b/projects/packages/scheduled-updates/changelog/move-status-fields new file mode 100644 index 0000000000000..02c4649329a2d --- /dev/null +++ b/projects/packages/scheduled-updates/changelog/move-status-fields @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: No functional changes, just moved some code around + + diff --git a/projects/packages/scheduled-updates/src/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-update-schedules.php b/projects/packages/scheduled-updates/src/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-update-schedules.php index 434f44552d67f..be90fc9164086 100644 --- a/projects/packages/scheduled-updates/src/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-update-schedules.php +++ b/projects/packages/scheduled-updates/src/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-update-schedules.php @@ -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' ) ); } /** @@ -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. * @@ -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 ); @@ -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'] ); } @@ -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 * @@ -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'] );