From 9fa456cf60dfb8d06b571e06ef3e30cab9262d68 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Fri, 11 Oct 2024 17:43:12 -0300 Subject: [PATCH 1/3] WIP: issue-1164 From 652f8c42e421bd2d8bce49bbb7e87f16d0b2b23c Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Fri, 11 Oct 2024 19:46:44 -0300 Subject: [PATCH 2/3] Improvements to the REST endpoints --- includes/REST/apple-news-delete.php | 32 +++++---- .../REST/apple-news-get-published-state.php | 2 +- includes/REST/apple-news-get-settings.php | 61 ++++++++++------- includes/REST/apple-news-modify-post.php | 9 ++- includes/REST/apple-news-publish.php | 32 +++++---- includes/REST/apple-news-sections.php | 53 ++++++++------- includes/REST/apple-news-update.php | 32 +++++---- includes/REST/apple-news-user-can-publish.php | 68 ++++++++++--------- 8 files changed, 167 insertions(+), 122 deletions(-) diff --git a/includes/REST/apple-news-delete.php b/includes/REST/apple-news-delete.php index 3be0bdd50..f6a998112 100644 --- a/includes/REST/apple-news-delete.php +++ b/includes/REST/apple-news-delete.php @@ -9,17 +9,8 @@ use WP_Error; use WP_REST_Request; - -/** - * Handle a REST POST request to the /apple-news/v1/delete endpoint. - * - * @param WP_REST_Request $data Data from query args. - * - * @return array|WP_Error Response to the request - either data about a successfully deleted article, or error. - */ -function rest_post_delete( $data ) { - return modify_post( (int) $data->get_param( 'id' ), 'delete' ); -} +use WP_REST_Response; +use WP_REST_Server; /** * Initialize this REST Endpoint. @@ -27,15 +18,30 @@ function rest_post_delete( $data ) { add_action( 'rest_api_init', function () { - // Register route count argument. register_rest_route( 'apple-news/v1', '/delete', [ - 'methods' => 'POST', + 'methods' => WP_REST_Server::CREATABLE, 'callback' => __NAMESPACE__ . '\rest_post_delete', 'permission_callback' => '__return_true', ] ); } ); + +/** + * Handle a REST POST request to the /apple-news/v1/delete endpoint. + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error + */ +function rest_post_delete( $request ): WP_REST_Response|WP_Error { + $post = modify_post( (int) $request->get_param( 'id' ), 'delete' ); + + if ( is_wp_error( $post ) ) { + return $post; + } + + return rest_ensure_response( $post ); +} diff --git a/includes/REST/apple-news-get-published-state.php b/includes/REST/apple-news-get-published-state.php index 35ef147ee..32603dece 100644 --- a/includes/REST/apple-news-get-published-state.php +++ b/includes/REST/apple-news-get-published-state.php @@ -44,7 +44,7 @@ function () { * Get the published state of a post. * * @param WP_REST_Request $request Full details about the request. - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + * @return WP_REST_Response|WP_Error */ function get_published_state_response( $request ): WP_REST_Response|WP_Error { $id = $request->get_param( 'id' ); diff --git a/includes/REST/apple-news-get-settings.php b/includes/REST/apple-news-get-settings.php index fedaeff4d..ade4fcc72 100644 --- a/includes/REST/apple-news-get-settings.php +++ b/includes/REST/apple-news-get-settings.php @@ -1,6 +1,6 @@ WP_REST_Server::READABLE, + 'callback' => __NAMESPACE__ . '\get_settings_response', + 'permission_callback' => '__return_true', + ] + ); + } +); /** * Get API response. * - * @param array $data data from query args. - * @return array updated response. + * @return WP_REST_Response|WP_Error */ -function get_settings_response( $data ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found +function get_settings_response(): WP_REST_Response|WP_Error { + // Ensure Apple News is first initialized. - \Apple_News::has_uninitialized_error(); + $retval = \Apple_News::has_uninitialized_error(); + + if ( is_wp_error( $retval ) ) { + return $retval; + } if ( empty( get_current_user_id() ) ) { - return []; + return rest_ensure_response( [] ); } // Compile non-sensitive plugin settings into a JS-friendly format and return. $admin_settings = new \Admin_Apple_Settings(); $settings = $admin_settings->fetch_settings(); $default_settings = ( new Settings() )->all(); - return [ + + $response = [ 'adminUrl' => esc_url_raw( admin_url( 'admin.php?page=apple-news-options' ) ), 'automaticAssignment' => ! empty( Automation::get_automation_rules() ), 'apiAsync' => 'yes' === $settings->api_async, @@ -42,23 +68,6 @@ function get_settings_response( $data ) { // phpcs:ignore Generic.CodeAnalysis.U 'showMetabox' => 'yes' === $settings->show_metabox, 'useRemoteImages' => 'yes' === $settings->use_remote_images, ]; -} -/** - * Initialize this REST Endpoint. - */ -add_action( - 'rest_api_init', - function () { - // Register route count argument. - register_rest_route( - 'apple-news/v1', - '/get-settings', - [ - 'methods' => 'GET', - 'callback' => __NAMESPACE__ . '\get_settings_response', - 'permission_callback' => '__return_true', - ] - ); - } -); + return rest_ensure_response( $response ); +} diff --git a/includes/REST/apple-news-modify-post.php b/includes/REST/apple-news-modify-post.php index f8cdb8405..f1ac20a99 100644 --- a/includes/REST/apple-news-modify-post.php +++ b/includes/REST/apple-news-modify-post.php @@ -23,9 +23,13 @@ * * @return array|WP_Error Response to the request - either data about a successful operation, or error. */ -function modify_post( $post_id, $operation ) { +function modify_post( $post_id, $operation ): array|WP_Error { // Ensure Apple News is first initialized. - \Apple_News::has_uninitialized_error(); + $retval = \Apple_News::has_uninitialized_error(); + + if ( is_wp_error( $retval ) ) { + return $retval; + } // Ensure there is a post ID provided in the data. if ( empty( $post_id ) ) { @@ -85,6 +89,7 @@ function modify_post( $post_id, $operation ) { ] ); } + try { $action->perform(); diff --git a/includes/REST/apple-news-publish.php b/includes/REST/apple-news-publish.php index ea6095065..762a99cb4 100644 --- a/includes/REST/apple-news-publish.php +++ b/includes/REST/apple-news-publish.php @@ -9,17 +9,8 @@ use WP_Error; use WP_REST_Request; - -/** - * Handle a REST POST request to the /apple-news/v1/publish endpoint. - * - * @param WP_REST_Request $data Data from query args. - * - * @return array|WP_Error Response to the request - either data about a successfully published article, or error. - */ -function rest_post_publish( $data ) { - return modify_post( (int) $data->get_param( 'id' ), 'publish' ); -} +use WP_REST_Response; +use WP_REST_Server; /** * Initialize this REST Endpoint. @@ -27,15 +18,30 @@ function rest_post_publish( $data ) { add_action( 'rest_api_init', function () { - // Register route count argument. register_rest_route( 'apple-news/v1', '/publish', [ - 'methods' => 'POST', + 'methods' => WP_REST_Server::CREATABLE, 'callback' => __NAMESPACE__ . '\rest_post_publish', 'permission_callback' => '__return_true', ] ); } ); + +/** + * Handle a REST POST request to the /apple-news/v1/publish endpoint. + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error + */ +function rest_post_publish( $request ): WP_REST_Response|WP_Error { + $post = modify_post( (int) $request->get_param( 'id' ), 'publish' ); + + if ( is_wp_error( $post ) ) { + return $post; + } + + return rest_ensure_response( $post ); +} diff --git a/includes/REST/apple-news-sections.php b/includes/REST/apple-news-sections.php index 5e9590195..113bc5c35 100644 --- a/includes/REST/apple-news-sections.php +++ b/includes/REST/apple-news-sections.php @@ -7,14 +7,40 @@ namespace Apple_News\REST; +use WP_Error; +use WP_REST_Response; +use WP_REST_Server; + +/** + * Initialize this REST Endpoint. + */ +add_action( + 'rest_api_init', + function () { + register_rest_route( + 'apple-news/v1', + '/sections', + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => __NAMESPACE__ . '\get_sections_response', + 'permission_callback' => '__return_true', + ] + ); + } +); + /** * Get API response. * - * @return array An array of information about sections. + * @return WP_REST_Response|WP_Error */ -function get_sections_response() { +function get_sections_response(): WP_REST_Response|WP_Error { // Ensure Apple News is first initialized. - \Apple_News::has_uninitialized_error(); + $retval = \Apple_News::has_uninitialized_error(); + + if ( is_wp_error( $retval ) ) { + return $retval; + } $sections = \Admin_Apple_Sections::get_sections(); $response = []; @@ -28,24 +54,5 @@ function get_sections_response() { } } - return $response; + return rest_ensure_response( $response ); } - -/** - * Initialize this REST Endpoint. - */ -add_action( - 'rest_api_init', - function () { - // Register route count argument. - register_rest_route( - 'apple-news/v1', - '/sections', - [ - 'methods' => 'GET', - 'callback' => __NAMESPACE__ . '\get_sections_response', - 'permission_callback' => '__return_true', - ] - ); - } -); diff --git a/includes/REST/apple-news-update.php b/includes/REST/apple-news-update.php index 49c4e6eea..9cc1ed550 100644 --- a/includes/REST/apple-news-update.php +++ b/includes/REST/apple-news-update.php @@ -9,17 +9,8 @@ use WP_Error; use WP_REST_Request; - -/** - * Handle a REST POST request to the /apple-news/v1/update endpoint. - * - * @param WP_REST_Request $data Data from query args. - * - * @return array|WP_Error Response to the request - either data about a successfully updated article, or error. - */ -function rest_post_update( $data ) { - return modify_post( (int) $data->get_param( 'id' ), 'update' ); -} +use WP_REST_Response; +use WP_REST_Server; /** * Initialize this REST Endpoint. @@ -27,15 +18,30 @@ function rest_post_update( $data ) { add_action( 'rest_api_init', function () { - // Register route count argument. register_rest_route( 'apple-news/v1', '/update', [ - 'methods' => 'POST', + 'methods' => WP_REST_Server::CREATABLE, 'callback' => __NAMESPACE__ . '\rest_post_update', 'permission_callback' => '__return_true', ] ); } ); + +/** + * Handle a REST POST request to the /apple-news/v1/update endpoint. + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error + */ +function rest_post_update( $request ): WP_REST_Response|WP_Error { + $post = modify_post( (int) $request->get_param( 'id' ), 'update' ); + + if ( is_wp_error( $post ) ) { + return $post; + } + + return rest_ensure_response( $post ); +} diff --git a/includes/REST/apple-news-user-can-publish.php b/includes/REST/apple-news-user-can-publish.php index c73931030..8310123d0 100644 --- a/includes/REST/apple-news-user-can-publish.php +++ b/includes/REST/apple-news-user-can-publish.php @@ -8,34 +8,57 @@ namespace Apple_News\REST; use Apple_News; +use WP_Error; +use WP_REST_Response; +use WP_REST_Server; + +/** + * Initialize this REST Endpoint. + */ +add_action( + 'rest_api_init', + function () { + register_rest_route( + 'apple-news/v1', + '/user-can-publish/(?P\d+)', + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => __NAMESPACE__ . '\get_user_can_publish', + 'permission_callback' => '__return_true', + ] + ); + } +); /** * Get API response. * - * @param array $args Args present in the URL. - * - * @return array An array that contains the key 'userCanPublish' which is true if the user can publish, false if not. + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error */ -function get_user_can_publish( $args ) { +function get_user_can_publish( $request ): WP_REST_Response|WP_Error { + // Ensure Apple News is first initialized. + $retval = Apple_News::has_uninitialized_error(); + + if ( is_wp_error( $retval ) ) { + return $retval; + } // Ensure there is a post ID provided in the data. - $id = ! empty( $args['id'] ) ? (int) $args['id'] : 0; + $id = (int) $request->get_param( 'id' ); + if ( empty( $id ) ) { - return [ - 'userCanPublish' => false, - ]; + return rest_ensure_response( [ 'userCanPublish' => false ] ); } // Try to get the post by ID. $post = get_post( $id ); if ( empty( $post ) ) { - return [ - 'userCanPublish' => false, - ]; + return rest_ensure_response( [ 'userCanPublish' => false ] ); } // Ensure the user is authorized to make changes to Apple News posts. - return [ + $response = [ 'userCanPublish' => current_user_can( /** This filter is documented in admin/class-admin-apple-post-sync.php */ apply_filters( @@ -44,23 +67,6 @@ function get_user_can_publish( $args ) { ) ), ]; -} -/** - * Initialize this REST Endpoint. - */ -add_action( - 'rest_api_init', - function () { - // Register route count argument. - register_rest_route( - 'apple-news/v1', - '/user-can-publish/(?P\d+)', - [ - 'methods' => 'GET', - 'callback' => __NAMESPACE__ . '\get_user_can_publish', - 'permission_callback' => '__return_true', - ] - ); - } -); + return rest_ensure_response( $response ); +} From 8092ab16856bc5d9a061089215f160558d088e2c Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Fri, 11 Oct 2024 19:46:56 -0300 Subject: [PATCH 3/3] Ready for review