diff --git a/src/class-rest-api-endpoints.php b/src/class-rest-api-endpoints.php index edffd8b5..8a9d0ea4 100644 --- a/src/class-rest-api-endpoints.php +++ b/src/class-rest-api-endpoints.php @@ -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. @@ -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/', @@ -61,7 +64,7 @@ public function register_endpoints() { ] ); register_rest_route( - 'wp-newsletter-builder/v1/', + 'wp-newsletter-builder/v1', '/status/(?P[a-f0-9]+)', [ 'methods' => 'GET', @@ -72,7 +75,7 @@ public function register_endpoints() { ] ); register_rest_route( - 'wp-newsletter-builder/v1/', + 'wp-newsletter-builder/v1', '/subscribe/', [ 'methods' => 'POST', @@ -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(); @@ -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 []; @@ -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 [ diff --git a/src/class-wp-newsletter-builder.php b/src/class-wp-newsletter-builder.php index a5c885c4..5929cac1 100644 --- a/src/class-wp-newsletter-builder.php +++ b/src/class-wp-newsletter-builder.php @@ -7,6 +7,8 @@ namespace WP_Newsletter_Builder; +use WP_Post; + /** * Example Plugin */ @@ -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 ) @@ -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 );