Skip to content

Commit

Permalink
Improvements to the REST endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Oct 11, 2024
1 parent 9fa456c commit 652f8c4
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 122 deletions.
32 changes: 19 additions & 13 deletions includes/REST/apple-news-delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,39 @@

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.
*/
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 );
}
2 changes: 1 addition & 1 deletion includes/REST/apple-news-get-published-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
61 changes: 35 additions & 26 deletions includes/REST/apple-news-get-settings.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* This adds custom endpoints for perspective posts.
* A custom endpoint for getting settings.
*
* @package Apple_News
*/
Expand All @@ -9,26 +9,52 @@

use Apple_Exporter\Settings;
use Apple_News\Admin\Automation;
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',
'/get-settings',
[
'methods' => 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,
Expand All @@ -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 );
}
9 changes: 7 additions & 2 deletions includes/REST/apple-news-modify-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -85,6 +89,7 @@ function modify_post( $post_id, $operation ) {
]
);
}

try {
$action->perform();

Expand Down
32 changes: 19 additions & 13 deletions includes/REST/apple-news-publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,39 @@

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.
*/
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 );
}
53 changes: 30 additions & 23 deletions includes/REST/apple-news-sections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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',
]
);
}
);
32 changes: 19 additions & 13 deletions includes/REST/apple-news-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,39 @@

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.
*/
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 );
}
Loading

0 comments on commit 652f8c4

Please sign in to comment.