Skip to content

Commit

Permalink
Update REST API endpoints and update PHPDOC
Browse files Browse the repository at this point in the history
Added strict_types declaration and updated namespaces in wp-content/plugins/wp-newsletter-builder/src/class-rest-api-endpoints.php to improve code readability and maintainability.

Updated REST API endpoints and return type. Return types are changed to either WP_Error or array for better error handling. Made transitions from root namespaces to class-based PHP namespaces.

In addition, PHPDOC is updated to match changes in parameter or return types, names and increase accuracy. This allows better readability for other developers.

Also, adjusted usage of WP_POST on class-wp-newsletter-builder.php for consistency.

This change is part of an overall effort in modernizing and improving the codebase's readability and maintainability.
  • Loading branch information
attackant committed Nov 17, 2023
1 parent 0a28265 commit c82e35d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
49 changes: 28 additions & 21 deletions src/class-rest-api-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
* @package wp-newsletter-builder
*/

declare( strict_types=1 );

namespace WP_Newsletter_Builder;

use function WP_Newsletter_Builder\get_byline;
use WP_Error;
use WP_REST_Request;

/**
* Adds additional Endpoints to the REST API.
Expand All @@ -25,7 +28,7 @@ public function __construct() {
*
* @return void
*/
public function register_endpoints() {
public function register_endpoints(): void {
register_rest_route(
'wp-newsletter-builder/v1',
'/lists/',
Expand Down Expand Up @@ -61,7 +64,7 @@ public function register_endpoints() {
]
);
register_rest_route(
'wp-newsletter-builder/v1/',
'wp-newsletter-builder/v1',
'/status/(?P<post_id>[a-f0-9]+)',
[
'methods' => 'GET',
Expand All @@ -72,7 +75,7 @@ public function register_endpoints() {
]
);
register_rest_route(
'wp-newsletter-builder/v1/',
'wp-newsletter-builder/v1',
'/subscribe/',
[
'methods' => 'POST',
Expand All @@ -85,25 +88,27 @@ public function register_endpoints() {
/**
* Gets the lists from the Campaign Monitor API.
*
* @return array
* @return WP_Error|array
*/
public function get_lists() {
public function get_lists(): WP_Error|array {
if ( ! current_user_can( 'edit_posts' ) ) {
// return new \WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
// phpcs:disable
// TODO return new \WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
// phpcs:enable
}
global $newsletter_builder_email_provider;
$lists = $newsletter_builder_email_provider->get_lists();
return $lists;

return $newsletter_builder_email_provider->get_lists();
}

/**
* Gets the email types from options.
*
* @return array
* @return WP_Error|array
*/
public function get_email_types() {
public function get_email_types(): WP_Error|array {
if ( ! current_user_can( 'edit_posts' ) ) {
return new \WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
}
$types_class = new Email_Types();
$types = $types_class->get_email_types();
Expand All @@ -119,24 +124,25 @@ public function get_email_types() {
/**
* Gets the settings from options.
*
* @return array
* @return WP_Error|array
*/
public function get_footer_settings() {
public function get_footer_settings(): WP_Error|array {
if ( ! current_user_can( 'edit_posts' ) ) {
return new \WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this endpoint.', 'wp-newsletter-builder' ), [ 'status' => 401 ] );
}
$settings = new Settings();
$footer_settings = $settings->get_footer_settings();
return $footer_settings;

return $settings->get_footer_settings();
}

/**
* Gets the status for a newsletter.
*
* @param \WP_REST_Request $request The request object.
* @param WP_REST_Request $request The request object.
*
* @return array
*/
public function get_status( $request ) {
public function get_status( WP_REST_Request $request ): array {
$post_id = $request->get_param( 'post_id' );
if ( empty( $post_id ) ) {
return [];
Expand Down Expand Up @@ -194,10 +200,11 @@ public function get_status( $request ) {
/**
* Subscribes a user to a list.
*
* @param \WP_Rest_Request $request The request object.
* @param WP_Rest_Request $request The request object.
*
* @return array
*/
public function subscribe( $request ) {
public function subscribe( WP_REST_Request $request ): array {
$email = $request->get_param( 'email' );
if ( empty( $email ) ) {
return [
Expand Down
20 changes: 12 additions & 8 deletions src/class-wp-newsletter-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace WP_Newsletter_Builder;

use WP_Post;

/**
* Example Plugin
*/
Expand Down Expand Up @@ -97,7 +99,7 @@ public function include_template( $template ) {
$local_path = WP_NEWSLETTER_BUILDER_DIR . '/single-nb_newsletter.php';

if (
$post instanceof \WP_Post
$post instanceof WP_Post
&& is_singular( 'nb_newsletter' )
&& file_exists( $local_path )
&& 0 === validate_file( $local_path )
Expand All @@ -111,18 +113,20 @@ public function include_template( $template ) {
/**
* Sends the newsletter when the newsletter post is published.
*
* @param int $post_id The post id.
* @param \WP_Post $post The post.
* @param bool $update Whether this is an update.
* @param \WP_Post $post_before The post before the update.
* @param int $post_id The post id.
* @param WP_Post $post The post.
* @param bool $update Whether this is an update.
* @param WP_Post|null $post_before The post before the update.
*/
public function on_newsletter_after_insert_post( $post_id, $post, $update, $post_before ): void {
public function on_newsletter_after_insert_post( int $post_id, WP_Post $post, bool $update, ?WP_Post $post_before ): void {
if ( 'nb_newsletter' !== $post->post_type ) {
return;
}
$new_status = $post->post_status;
$old_status = $post_before->post_status;
if ( $new_status === $old_status || 'publish' !== $new_status ) {
if (
$new_status === $post_before?->post_status
|| 'publish' !== $new_status
) {
return;
}
$this->do_send( $post->ID );
Expand Down

0 comments on commit c82e35d

Please sign in to comment.