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

Add notices for important api exceptions #1089

12 changes: 12 additions & 0 deletions src/API/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ public static function make_request( $endpoint, $method = 'POST', $payload = arr
do_action( 'pinterest_for_woocommerce_disconnect' );
}

/**
* Action used to intercept the Pinterest API exception with code and a message and decide whether or not
* to show an admin notice for the exception.
*
* @since x.x.x
*
* @param int $http_code Pinterest API HTTP response code.
* @param int $pinterest_api_code Pinterest API error code.
* @param string $pinterest_api_message Pinterest API error message.
*/
do_action( 'pinterest_for_woocommerce_show_admin_notice_for_api_exception', $e->getCode(), $e->get_pinterest_code(), $e->getMessage() );

throw $e;
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/FeedRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Automattic\WooCommerce\Pinterest;

use Automattic\WooCommerce\Pinterest\Notes\AccountFailure;
use Exception;
use Throwable;
use Automattic\WooCommerce\Pinterest\Utilities\ProductFeedLogger;
Expand Down Expand Up @@ -65,6 +66,9 @@ public function init() {

// We do not want to disconnect the merchant if the authentication fails, since it running action can not be unscheduled.
add_filter( 'pinterest_for_woocommerce_disconnect_on_authentication_failure', '__return_false' );

add_action( 'pinterest_for_woocommerce_show_admin_notice_for_api_exception', array( self::class, 'maybe_account_failure_notice' ), 10, 3 );
add_action( 'pinterest_for_woocommerce_disconnect', array( AccountFailure::class, 'delete_note' ) );
}

/**
Expand Down Expand Up @@ -223,4 +227,21 @@ public static function deregister() {
Pinterest_For_Woocommerce()::save_data( 'feed_registered', false );
self::cancel_jobs();
}

/**
* Maybe show admin notice about account being disapproved.
*
* @since x.x.x
*
* @param int $http_code Pinterest API HTTP response code.
* @param int $pinterest_api_code Pinterest API error code.
* @param string $pinterest_api_message Pinterest API error message.
*
* @return void
*/
public static function maybe_account_failure_notice( int $http_code, int $pinterest_api_code, string $pinterest_api_message ): void {
if ( 403 === $http_code ) {
AccountFailure::maybe_add_note( $pinterest_api_message );
}
}
}
82 changes: 82 additions & 0 deletions src/Notes/AccountFailure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Adds an error note to display Pinterest API account status failure responses.
*
* @since x.x.x
* @package Automattic\WooCommerce\Pinterest\Notes
*/

namespace Automattic\WooCommerce\Pinterest\Notes;

defined( 'ABSPATH' ) || exit;

use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Admin\Notes\NotesUnavailableException;
use Automattic\WooCommerce\Admin\Notes\NoteTraits;

/**
* Account Failure admin notice.
*
* @since x.x.x
*/
class AccountFailure {
/**
* Note traits.
*/
use NoteTraits;

/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'pinterest-for-woocommerce-account-failure';

/**
* Get the note.
*
* @since x.x.x
* @param string $message Pinterest API error message.
* @return Note
*/
public static function get_note( string $message ) {
$additional_data = array(
'role' => 'administrator',
);

$note = new Note();
$note->set_title( __( 'Pinterest For WooCommerce action required.', 'pinterest-for-woocommerce' ) );
$note->set_content( esc_html( $message ) );
$note->set_content_data( (object) $additional_data );
$note->set_type( Note::E_WC_ADMIN_NOTE_ERROR );
$note->set_name( self::NOTE_NAME );
$note->set_source( 'woocommerce-admin' );
return $note;
}

/**
* Used to add an account failure note if the one does not exist.
*
* @since x.x.x
* @param string $message Pinterest API error message.
* @return void
* @throws NotesUnavailableException An exception when notes are unavailable.
*/
public static function maybe_add_note( string $message ): void {
if ( self::note_exists() ) {
return;
}

$note = self::get_note( $message );
$note->save();
}

/**
* Delete the note.
*
* @since x.x.x
* @return void
*/
public static function delete_note() {
Notes::delete_notes_with_name( self::NOTE_NAME );
}
}
Loading