Skip to content

Commit

Permalink
Merge branch 'develop' into feature/issue-1165/setup-configure-phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves authored Oct 14, 2024
2 parents 1054327 + 9e4101c commit 63fad4c
Show file tree
Hide file tree
Showing 15 changed files with 258 additions and 212 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
75 changes: 50 additions & 25 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,40 +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 {
/**
* Actions to be taken after an article failed to be pushed to Apple News.
* Reindex the article if it was deleted in the iCloud News Publisher dashboard.
*
* @param int $post_id The ID of the post.
* @param string $original_error_message The original error message.
* @see https://github.com/alleyinteractive/apple-news/issues/1154
*/
do_action( 'apple_news_after_push_failure', $this->id, $original_error_message );
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.
}
}

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 ) );
}
}

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;
}
}
32 changes: 19 additions & 13 deletions includes/REST/apple-news-delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,39 @@

use WP_Error;
use WP_REST_Request;

/**
* Handle a REST POST request to the /apple-news/v1/delete endpoint.
*
* @param WP_REST_Request $data Data from query args.
*
* @return array|WP_Error Response to the request - either data about a successfully deleted article, or error.
*/
function rest_post_delete( $data ) {
return modify_post( (int) $data->get_param( 'id' ), 'delete' );
}
use WP_REST_Response;
use WP_REST_Server;

/**
* Initialize this REST Endpoint.
*/
add_action(
'rest_api_init',
function () {
// Register route count argument.
register_rest_route(
'apple-news/v1',
'/delete',
[
'methods' => 'POST',
'methods' => WP_REST_Server::CREATABLE,
'callback' => __NAMESPACE__ . '\rest_post_delete',
'permission_callback' => '__return_true',
]
);
}
);

/**
* Handle a REST POST request to the /apple-news/v1/delete endpoint.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
function rest_post_delete( $request ): WP_REST_Response|WP_Error {
$post = modify_post( (int) $request->get_param( 'id' ), 'delete' );

if ( is_wp_error( $post ) ) {
return $post;
}

return rest_ensure_response( $post );
}
2 changes: 1 addition & 1 deletion includes/REST/apple-news-get-published-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function () {
* Get the published state of a post.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
* @return WP_REST_Response|WP_Error
*/
function get_published_state_response( $request ): WP_REST_Response|WP_Error {
$id = $request->get_param( 'id' );
Expand Down
61 changes: 35 additions & 26 deletions includes/REST/apple-news-get-settings.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* This adds custom endpoints for perspective posts.
* A custom endpoint for getting settings.
*
* @package Apple_News
*/
Expand All @@ -9,26 +9,52 @@

use Apple_Exporter\Settings;
use Apple_News\Admin\Automation;
use WP_Error;
use WP_REST_Response;
use WP_REST_Server;

/**
* Initialize this REST Endpoint.
*/
add_action(
'rest_api_init',
function () {
register_rest_route(
'apple-news/v1',
'/get-settings',
[
'methods' => WP_REST_Server::READABLE,
'callback' => __NAMESPACE__ . '\get_settings_response',
'permission_callback' => '__return_true',
]
);
}
);

/**
* Get API response.
*
* @param array $data data from query args.
* @return array updated response.
* @return WP_REST_Response|WP_Error
*/
function get_settings_response( $data ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
function get_settings_response(): WP_REST_Response|WP_Error {

// Ensure Apple News is first initialized.
\Apple_News::has_uninitialized_error();
$retval = \Apple_News::has_uninitialized_error();

if ( is_wp_error( $retval ) ) {
return $retval;
}

if ( empty( get_current_user_id() ) ) {
return [];
return rest_ensure_response( [] );
}

// Compile non-sensitive plugin settings into a JS-friendly format and return.
$admin_settings = new \Admin_Apple_Settings();
$settings = $admin_settings->fetch_settings();
$default_settings = ( new Settings() )->all();
return [

$response = [
'adminUrl' => esc_url_raw( admin_url( 'admin.php?page=apple-news-options' ) ),
'automaticAssignment' => ! empty( Automation::get_automation_rules() ),
'apiAsync' => 'yes' === $settings->api_async,
Expand All @@ -42,23 +68,6 @@ function get_settings_response( $data ) { // phpcs:ignore Generic.CodeAnalysis.U
'showMetabox' => 'yes' === $settings->show_metabox,
'useRemoteImages' => 'yes' === $settings->use_remote_images,
];
}

/**
* Initialize this REST Endpoint.
*/
add_action(
'rest_api_init',
function () {
// Register route count argument.
register_rest_route(
'apple-news/v1',
'/get-settings',
[
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\get_settings_response',
'permission_callback' => '__return_true',
]
);
}
);
return rest_ensure_response( $response );
}
9 changes: 7 additions & 2 deletions includes/REST/apple-news-modify-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
*
* @return array|WP_Error Response to the request - either data about a successful operation, or error.
*/
function modify_post( $post_id, $operation ) {
function modify_post( $post_id, $operation ): array|WP_Error {
// Ensure Apple News is first initialized.
\Apple_News::has_uninitialized_error();
$retval = \Apple_News::has_uninitialized_error();

if ( is_wp_error( $retval ) ) {
return $retval;
}

// Ensure there is a post ID provided in the data.
if ( empty( $post_id ) ) {
Expand Down Expand Up @@ -85,6 +89,7 @@ function modify_post( $post_id, $operation ) {
]
);
}

try {
$action->perform();

Expand Down
Loading

0 comments on commit 63fad4c

Please sign in to comment.