Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue-1154: Add Support for Handling Deleted Articles (Part II) #1173

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions admin/apple-actions/index/class-push.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ public function __construct( $settings, $id ) {
* @param boolean $doing_async Optional. Whether the action is being performed asynchronously.
* @param int $user_id Optional. The ID of the user performing the action. Defaults to the current user ID.
*
* @access public
* @return boolean
* @throws Action_Exception If the push fails.
*/
public function perform( $doing_async = false, $user_id = null ) {
if ( 'yes' === $this->settings->__get( 'api_async' ) && false === $doing_async ) {

// Do not proceed if this is already pending publish.
$pending = get_post_meta( $this->id, 'apple_news_api_pending', true );

if ( ! empty( $pending ) ) {
return false;
}
Expand All @@ -94,9 +95,9 @@ public function perform( $doing_async = false, $user_id = null ) {
update_post_meta( $this->id, 'apple_news_api_pending', time() );

wp_schedule_single_event( time(), Admin_Apple_Async::ASYNC_PUSH_HOOK, [ $this->id, get_current_user_id() ] );
} else {
return $this->push( $user_id );
}

return $this->push( $user_id );
}

/**
Expand Down Expand Up @@ -213,7 +214,6 @@ private function get(): void {
*
* @param int $user_id Optional. The ID of the user performing the push. Defaults to current user.
*
* @access private
* @throws Action_Exception If unable to push.
*/
private function push( $user_id = null ): void {
Expand Down Expand Up @@ -438,11 +438,20 @@ private function push( $user_id = null ): void {

$this->clean_workspace();

if ( str_contains( $e->getMessage(), 'WRONG_REVISION' ) ) {
throw new Action_Exception( esc_html__( 'Apple News Error: It seems like the article was updated by another call. If the problem persists, try removing and pushing again.', 'apple-news' ) );
$original_error_message = $e->getMessage();

if ( str_contains( $original_error_message, 'WRONG_REVISION' ) ) {
$error_message = __( 'Apple News Error: It seems like the article was updated by another call. If the problem persists, try removing and pushing again.', 'apple-news' );
} elseif ( str_contains( $original_error_message, 'NOT_FOUND (keyPath articleId)' ) ) {
// Reset the API postmeta if the article is deleted in Apple News.
$this->delete_post_meta( $this->id );

$error_message = __( 'The article seems to be deleted in Apple News. Reindexing the article in Apple News.', 'apple-news' );
} else {
throw new Action_Exception( esc_html__( 'There has been an error with the Apple News API: ', 'apple-news' ) . esc_html( $e->getMessage() ) );
$error_message = __( 'There has been an error with the Apple News API: ', 'apple-news' ) . esc_html( $original_error_message );
}

throw new Action_Exception( esc_html( $error_message ) );
}

// Print success message.
Expand Down
3 changes: 2 additions & 1 deletion admin/class-admin-apple-post-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public function action__transition_post_status( $new_status, $old_status, $post
* When a post is published, or a published post updated, trigger this function.
*
* @since 0.4.0
*
* @param int $id The ID of the post being updated.
* @param WP_Post $post The post object being updated.
* @access public
*/
public function do_publish( $id, $post ) {
if ( 'publish' !== $post->post_status
Expand Down Expand Up @@ -170,6 +170,7 @@ public function do_publish( $id, $post ) {

// Proceed with the push.
$action = new Apple_Actions\Index\Push( $this->settings, $id );

try {
$action->perform();
} catch ( Apple_Actions\Action_Exception $e ) {
Expand Down
41 changes: 41 additions & 0 deletions tests/admin/apple-actions/index/test-class-push.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,45 @@ public function test_update() {
$body = $this->get_body_from_request( $request );
$this->assertEquals( 'Test New Title', $body['title'] );
}

/**
* Test that the action is able to handle a deleted article.
*/
public function test_update_with_deleted_article(): void {
$article_id = self::factory()->post->create();
$api_id = 'efabcdef123456';

add_post_meta( $article_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\Push( $this->settings, $article_id );

try {
$action->perform();
} catch ( Action_Exception $e ) {
$this->assertSame( 'The article seems to be deleted in Apple News. Reindexing the article in Apple News.', $e->getMessage() );
}

$this->assertEmpty( get_post_meta( $article_id, 'apple_news_api_id', true ) );
}
}