From 2028130f90e51350f7dcbc5246e37188b9519f2f Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 15:48:44 -0300
Subject: [PATCH 01/10] A different approach to reindex the deleted article
---
admin/apple-actions/index/class-get.php | 11 +---
admin/apple-actions/index/class-push.php | 64 +++++++++++++------
.../apple-actions/index/test-class-get.php | 39 -----------
.../test-class-rest-post-published-state.php | 13 ++--
4 files changed, 51 insertions(+), 76 deletions(-)
diff --git a/admin/apple-actions/index/class-get.php b/admin/apple-actions/index/class-get.php
index e1c64895..c4ccbabf 100644
--- a/admin/apple-actions/index/class-get.php
+++ b/admin/apple-actions/index/class-get.php
@@ -52,16 +52,7 @@ public function perform() {
}
// Get the article from the API.
- try {
- $article = $this->get_api()->get_article( $apple_id );
- } catch ( \Apple_Push_API\Request\Request_Exception $e ) {
- $article = $e->getMessage();
-
- // Reset the API postmeta if the article is deleted in Apple News.
- if ( is_string( $article ) && str_contains( $article, 'NOT_FOUND (keyPath articleId)' ) ) {
- $this->delete_post_meta( $this->id );
- }
- }
+ $article = $this->get_api()->get_article( $apple_id );
if ( empty( $article->data ) ) {
return null;
diff --git a/admin/apple-actions/index/class-push.php b/admin/apple-actions/index/class-push.php
index b679e598..a456495e 100644
--- a/admin/apple-actions/index/class-push.php
+++ b/admin/apple-actions/index/class-push.php
@@ -14,10 +14,10 @@
use Admin_Apple_Async;
use Admin_Apple_Notice;
use Admin_Apple_Sections;
-use Apple_Actions\Action_Exception;
-use Apple_Actions\API_Action;
use Apple_Exporter\Exporter;
use Apple_Exporter\Settings;
+use Apple_Actions\API_Action;
+use Apple_Actions\Action_Exception;
use Apple_Push_API\Request\Request_Exception;
/**
@@ -212,11 +212,12 @@ private function get(): void {
/**
* Push the post using the API data.
*
- * @param int $user_id Optional. The ID of the user performing the push. Defaults to current user.
+ * @param int $user_id Optional. The ID of the user performing the push. Defaults to current user.
+ * @param bool $display_notices Optional. Whether to display notices. Defaults to true.
*
* @throws Action_Exception If unable to push.
*/
- private function push( $user_id = null ): void {
+ private function push( $user_id = null, $display_notices = true ): void {
if ( ! $this->is_api_configuration_valid() ) {
throw new Action_Exception( esc_html__( 'Your Apple News API settings seem to be empty. Please fill in the API key, API secret and API channel fields in the plugin configuration page.', 'apple-news' ) );
}
@@ -387,6 +388,9 @@ private function push( $user_id = null ): void {
);
}
+ $original_error_message = null;
+ $error_message = null;
+
try {
if ( $remote_id ) {
// Update the current article from the API in case the revision changed.
@@ -450,32 +454,52 @@ private function push( $user_id = null ): void {
} else {
$error_message = __( 'There has been an error with the Apple News API: ', 'apple-news' ) . esc_html( $original_error_message );
}
+ } finally {
+ /**
+ * Reindex the article if it was deleted in the iCloud News Publisher dashboard.
+ *
+ * @see https://github.com/alleyinteractive/apple-news/issues/1154
+ */
+ if ( $original_error_message && str_contains( $original_error_message, 'NOT_FOUND (keyPath articleId)' ) ) {
+ try {
+ self::push(
+ user_id: $user_id,
+ display_notices: false
+ );
+ } catch ( Action_Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
+ // Do nothing, even if the second push fails.
+ }
+ }
- throw new Action_Exception( esc_html( $error_message ) );
+ if ( $error_message ) {
+ throw new Action_Exception( esc_html( $error_message ) );
+ }
+ }
+
+ // If we're not supposed to display notices, bail out.
+ if ( false === $display_notices ) {
+ return;
}
// Print success message.
$post = get_post( $this->id );
+
+ $success_message = sprintf(
+ // translators: token is the post title.
+ __( 'Article %s has been pushed successfully to Apple News!', 'apple-news' ),
+ $post->post_title
+ );
+
if ( $remote_id ) {
- Admin_Apple_Notice::success(
- sprintf(
+ $success_message = sprintf(
// translators: token is the post title.
- __( 'Article %s has been successfully updated on Apple News!', 'apple-news' ),
- $post->post_title
- ),
- $user_id
- );
- } else {
- Admin_Apple_Notice::success(
- sprintf(
- // translators: token is the post title.
- __( 'Article %s has been pushed successfully to Apple News!', 'apple-news' ),
- $post->post_title
- ),
- $user_id
+ __( 'Article %s has been successfully updated on Apple News!', 'apple-news' ),
+ $post->post_title
);
}
+ Admin_Apple_Notice::success( $success_message, $user_id );
+
$this->clean_workspace();
}
diff --git a/tests/admin/apple-actions/index/test-class-get.php b/tests/admin/apple-actions/index/test-class-get.php
index 5164e95f..a10bc914 100644
--- a/tests/admin/apple-actions/index/test-class-get.php
+++ b/tests/admin/apple-actions/index/test-class-get.php
@@ -57,43 +57,4 @@ public function test_get_action(): void {
$this->assertSame( $response['data']['revision'], $data->data->revision );
$this->assertSame( $response['data']['type'], $data->data->type );
}
-
- /**
- * Test the behavior of the get action with a deleted Apple News article assigned to the post.
- */
- public function test_get_deleted_article(): void {
- $api_id = 'def456';
- $post_id = self::factory()->post->create();
- $action = new Apple_Actions\Index\Get( $this->settings, $post_id );
-
- $this->assertNull( $action->perform() );
-
- add_post_meta( $post_id, 'apple_news_api_id', $api_id );
-
- // Fake the API response for the GET request.
- $this->add_http_response(
- verb: 'GET',
- url: 'https://news-api.apple.com/articles/' . $api_id,
- body: wp_json_encode(
- [
- 'errors' => [
- [
- 'code' => 'NOT_FOUND',
- 'keyPath' => [ 'articleId' ],
- 'value' => $api_id,
- ],
- ],
- ]
- ),
- response: [
- 'code' => 404,
- 'message' => 'Not Found',
- ]
- );
-
- $action = new Apple_Actions\Index\Get( $this->settings, $post_id );
-
- $this->assertNull( $action->perform() );
- $this->assertEmpty( get_post_meta( $post_id, 'apple_news_api_id', true ) );
- }
}
diff --git a/tests/rest/test-class-rest-post-published-state.php b/tests/rest/test-class-rest-post-published-state.php
index 183dcff3..944fbdaf 100644
--- a/tests/rest/test-class-rest-post-published-state.php
+++ b/tests/rest/test-class-rest-post-published-state.php
@@ -92,13 +92,12 @@ public function test_get_post_published_state_of_an_invalid_id_when_authenticate
$this->get( rest_url( '/apple-news/v1/get-published-state/' . $post_id ) )
->assertOk()
- ->assertJsonPath( 'publishState', 'N/A' );
+ ->assertJsonPath( 'publishState', 'NOT_FOUND (keyPath articleId)' );
- // Ensure that the API postmeta _was_ reset.
- $this->assertEmpty( get_post_meta( $post_id, 'apple_news_api_created_at', true ) );
- $this->assertEmpty( get_post_meta( $post_id, 'apple_news_api_id', true ) );
- $this->assertEmpty( get_post_meta( $post_id, 'apple_news_api_modified_at', true ) );
- $this->assertEmpty( get_post_meta( $post_id, 'apple_news_api_revision', true ) );
- $this->assertEmpty( get_post_meta( $post_id, 'apple_news_api_share_url', true ) );
+ $this->assertEquals( 'abc123', get_post_meta( $post_id, 'apple_news_api_created_at', true ) );
+ $this->assertEquals( $api_id, get_post_meta( $post_id, 'apple_news_api_id', true ) );
+ $this->assertEquals( 'ghi789', get_post_meta( $post_id, 'apple_news_api_modified_at', true ) );
+ $this->assertEquals( 'jkl123', get_post_meta( $post_id, 'apple_news_api_revision', true ) );
+ $this->assertEquals( 'mno456', get_post_meta( $post_id, 'apple_news_api_share_url', true ) );
}
}
From 53a00e5becb43552468b5b7b7a8ab735f642fb3a Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 16:42:06 -0300
Subject: [PATCH 02/10] WIP: issue-1178
From 4394589bf5cb92cdccb898cceff5cf7470cc493b Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 16:43:11 -0300
Subject: [PATCH 03/10] Update the cached settings after an update
---
admin/settings/class-admin-apple-settings-section.php | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/admin/settings/class-admin-apple-settings-section.php b/admin/settings/class-admin-apple-settings-section.php
index 5ad1c7d6..9f6c540d 100644
--- a/admin/settings/class-admin-apple-settings-section.php
+++ b/admin/settings/class-admin-apple-settings-section.php
@@ -643,5 +643,13 @@ public function save_settings() {
// Save to options.
update_option( self::$section_option_name, $settings, 'no' );
+
+ /**
+ * Update the cached settings with new one after an update.
+ *
+ * The `self::get_value` method uses this cached data. By resetting it, we ensure
+ * that the new value is used after an update instead of the old value.
+ */
+ self::$loaded_settings = $settings;
}
}
From 60a38d9a535cfc35f62abb05288ae6c60ba69e87 Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 16:43:25 -0300
Subject: [PATCH 04/10] Ready for review
From 7be906016ec77bc5e85d25ef3bd58e4ef91e533a Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 16:56:49 -0300
Subject: [PATCH 05/10] WIP: issue-1153
From cd50df48d8f1ee075781e9009827765a4249a470 Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 17:00:05 -0300
Subject: [PATCH 06/10] A new hook fired when a post fails to be pushed to
Apple News
---
admin/apple-actions/index/class-push.php | 8 ++++++++
includes/apple-push-api/request/class-request.php | 7 ++++---
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/admin/apple-actions/index/class-push.php b/admin/apple-actions/index/class-push.php
index b679e598..bed4261a 100644
--- a/admin/apple-actions/index/class-push.php
+++ b/admin/apple-actions/index/class-push.php
@@ -451,6 +451,14 @@ private function push( $user_id = null ): void {
$error_message = __( 'There has been an error with the Apple News API: ', 'apple-news' ) . esc_html( $original_error_message );
}
+ /**
+ * Actions to be taken after an article failed to be pushed to Apple News.
+ *
+ * @param int $post_id The ID of the post.
+ * @param string $original_error_message The original error message.
+ */
+ do_action( 'apple_news_after_push_failure', $this->id, $original_error_message );
+
throw new Action_Exception( esc_html( $error_message ) );
}
diff --git a/includes/apple-push-api/request/class-request.php b/includes/apple-push-api/request/class-request.php
index 1c29a018..844904cb 100644
--- a/includes/apple-push-api/request/class-request.php
+++ b/includes/apple-push-api/request/class-request.php
@@ -173,6 +173,7 @@ private function parse_response( $response, $json = true, $type = 'post', $meta
// Get the admin email.
$admin_email = filter_var( $settings['apple_news_admin_email'], FILTER_VALIDATE_EMAIL );
+
if ( empty( $admin_email ) ) {
return; // TODO Fix inconsistent return value.
}
@@ -191,8 +192,8 @@ private function parse_response( $response, $json = true, $type = 'post', $meta
if ( 'yes' === $settings['use_remote_images'] ) {
$body .= esc_html__( 'Use Remote images enabled ', 'apple-news' );
} elseif ( ! empty( $bundles ) ) {
- $body .= "\n" . esc_html__( 'Bundled images', 'apple-news' ) . ":\n";
- $body .= implode( "\n", $bundles );
+ $body .= "\n" . esc_html__( 'Bundled images', 'apple-news' ) . ":\n";
+ $body .= implode( "\n", $bundles );
} else {
$body .= esc_html__( 'No bundled images found.', 'apple-news' );
}
@@ -210,7 +211,7 @@ private function parse_response( $response, $json = true, $type = 'post', $meta
*
* @since 1.4.4
*
- * @param string|array $headers Optional. Additional headers.
+ * @param string|array $headers Optional. Additional headers.
*/
$headers = apply_filters( 'apple_news_notification_headers', '' );
From c49c0aa58b4be6a3e2a00f4fde089c308d7c488a Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 17:00:15 -0300
Subject: [PATCH 07/10] Ready for review
From e4dcc3d91842ac9d42e97c9c86f6adb9c783c60f Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 17:31:31 -0300
Subject: [PATCH 08/10] WIP: issue-1161
From ed59dff7bab53e8552bbd349e9140a59756451d4 Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 17:35:38 -0300
Subject: [PATCH 09/10] Support sending debug notifications to multiple emails
---
...in-apple-settings-section-developer-tools.php | 7 ++++---
.../class-admin-apple-settings-section.php | 13 +++++++++++--
.../apple-push-api/request/class-request.php | 16 ++++++++++++----
3 files changed, 27 insertions(+), 9 deletions(-)
diff --git a/admin/settings/class-admin-apple-settings-section-developer-tools.php b/admin/settings/class-admin-apple-settings-section-developer-tools.php
index 77db19a5..7e372794 100644
--- a/admin/settings/class-admin-apple-settings-section-developer-tools.php
+++ b/admin/settings/class-admin-apple-settings-section-developer-tools.php
@@ -41,10 +41,11 @@ public function __construct( $page ) {
'type' => [ 'no', 'yes' ],
],
'apple_news_admin_email' => [
- 'label' => __( 'Administrator Email', 'apple-news' ),
+ 'label' => __( 'Email(s)', 'apple-news' ),
'required' => false,
- 'type' => 'string',
+ 'type' => 'email',
'size' => 40,
+ 'multiple' => true,
],
];
@@ -70,7 +71,7 @@ public function __construct( $page ) {
*/
public function get_section_info() {
return __(
- 'If debugging is enabled, emails will be sent to an administrator for every publish, update or delete action with a detailed API response.',
+ 'If debugging is enabled (and valid emails are provided), emails will be sent for every publish, update or delete action with a detailed API response.',
'apple-news'
);
}
diff --git a/admin/settings/class-admin-apple-settings-section.php b/admin/settings/class-admin-apple-settings-section.php
index 5ad1c7d6..fdb292fe 100644
--- a/admin/settings/class-admin-apple-settings-section.php
+++ b/admin/settings/class-admin-apple-settings-section.php
@@ -126,6 +126,7 @@ class Admin_Apple_Settings_Section extends Apple_News {
'max' => [],
'step' => [],
'type' => [],
+ 'multiple' => [],
'required' => [],
'size' => [],
'id' => [],
@@ -324,6 +325,14 @@ public function render_field( $args ) {
$field = '';
} elseif ( 'number' === $type ) {
$field = '';
+ } elseif ( 'email' === $type ) {
+ $field = 'is_multiple( $name ) ) {
+ $field .= ' multiple %s>';
+ } else {
+ $field .= ' %s>';
+ }
} else {
// If nothing else matches, it's a string.
$field = '';
@@ -403,9 +412,9 @@ public function render_field( $args ) {
protected function get_type_for( $name ) {
if ( $this->hidden ) {
return 'hidden';
- } else {
- return empty( $this->settings[ $name ]['type'] ) ? 'string' : $this->settings[ $name ]['type'];
}
+
+ return empty( $this->settings[ $name ]['type'] ) ? 'string' : $this->settings[ $name ]['type'];
}
/**
diff --git a/includes/apple-push-api/request/class-request.php b/includes/apple-push-api/request/class-request.php
index 1c29a018..5271eb6f 100644
--- a/includes/apple-push-api/request/class-request.php
+++ b/includes/apple-push-api/request/class-request.php
@@ -171,9 +171,17 @@ private function parse_response( $response, $json = true, $type = 'post', $meta
&& 'yes' === $settings['apple_news_enable_debugging']
&& 'get' !== $type ) {
- // Get the admin email.
- $admin_email = filter_var( $settings['apple_news_admin_email'], FILTER_VALIDATE_EMAIL );
- if ( empty( $admin_email ) ) {
+ $emails = $settings['apple_news_admin_email'] ?? '';
+
+ if ( str_contains( $emails, ',' ) ) {
+ $emails = array_map( 'trim', explode( ',', $emails ) );
+ } else {
+ $emails = [ $emails ];
+ }
+
+ $to = array_filter( $emails, 'is_email' );
+
+ if ( empty( $to ) ) {
return; // TODO Fix inconsistent return value.
}
@@ -217,7 +225,7 @@ private function parse_response( $response, $json = true, $type = 'post', $meta
// Send the email.
if ( ! empty( $body ) ) {
wp_mail( // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
- $admin_email,
+ $to,
esc_html__( 'Apple News Notification', 'apple-news' ),
$body,
$headers
From 456463b7dd64efa81a69790f5e1179d85c4503ea Mon Sep 17 00:00:00 2001
From: Renato Alves <19148962+renatonascalves@users.noreply.github.com>
Date: Fri, 11 Oct 2024 17:35:48 -0300
Subject: [PATCH 10/10] Ready for review