Skip to content

Commit

Permalink
Merge branch 'develop' into feature/issue-1164/rest-endpoints-return-…
Browse files Browse the repository at this point in the history
…wp-error
  • Loading branch information
renatonascalves authored Oct 14, 2024
2 parents 8092ab1 + 9cd91db commit a141cf5
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 88 deletions.
11 changes: 1 addition & 10 deletions admin/apple-actions/index/class-get.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
73 changes: 53 additions & 20 deletions admin/apple-actions/index/class-push.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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' ) );
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -450,32 +454,61 @@ 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 ) {
/**
* 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|null $original_error_message The original error message, if available.
* @param string $error_message The error message to be displayed.
*/
do_action( 'apple_news_after_push_failure', $this->id, $original_error_message, $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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];

Expand All @@ -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'
);
}
Expand Down
21 changes: 19 additions & 2 deletions admin/settings/class-admin-apple-settings-section.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Admin_Apple_Settings_Section extends Apple_News {
'max' => [],
'step' => [],
'type' => [],
'multiple' => [],
'required' => [],
'size' => [],
'id' => [],
Expand Down Expand Up @@ -324,6 +325,14 @@ public function render_field( $args ) {
$field = '<textarea id="%s" name="%s">%s</textarea>';
} elseif ( 'number' === $type ) {
$field = '<input type="number" id="%s" name="%s" value="%s" size="%s" min="%s" max="%s" step="%s" %s>';
} elseif ( 'email' === $type ) {
$field = '<input type="email" id="%s" name="%s" value="%s" size="%s"';

if ( $this->is_multiple( $name ) ) {
$field .= ' multiple %s>';
} else {
$field .= ' %s>';
}
} else {
// If nothing else matches, it's a string.
$field = '<input type="text" id="%s" name="%s" value="%s" size="%s" %s>';
Expand Down Expand Up @@ -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'];
}

/**
Expand Down Expand Up @@ -643,5 +652,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;
}
}
22 changes: 15 additions & 7 deletions includes/apple-push-api/request/class-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand All @@ -191,8 +199,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' );
}
Expand All @@ -210,14 +218,14 @@ 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', '' );

// 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
Expand Down
39 changes: 0 additions & 39 deletions tests/admin/apple-actions/index/test-class-get.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}
13 changes: 6 additions & 7 deletions tests/rest/test-class-rest-post-published-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}

0 comments on commit a141cf5

Please sign in to comment.