diff --git a/src/API/Base.php b/src/API/Base.php index 81ced96a..dae56be6 100644 --- a/src/API/Base.php +++ b/src/API/Base.php @@ -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; } } diff --git a/src/FeedRegistration.php b/src/FeedRegistration.php index 01a539a1..2979e5b5 100644 --- a/src/FeedRegistration.php +++ b/src/FeedRegistration.php @@ -8,6 +8,7 @@ namespace Automattic\WooCommerce\Pinterest; +use Automattic\WooCommerce\Pinterest\Notes\AccountFailure; use Exception; use Throwable; use Automattic\WooCommerce\Pinterest\Utilities\ProductFeedLogger; @@ -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' ) ); } /** @@ -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 ); + } + } } diff --git a/src/Notes/AccountFailure.php b/src/Notes/AccountFailure.php new file mode 100644 index 00000000..e136d85a --- /dev/null +++ b/src/Notes/AccountFailure.php @@ -0,0 +1,82 @@ + '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 ); + } +}