Skip to content

Commit

Permalink
Add Feed Name exists failure possible reason;
Browse files Browse the repository at this point in the history
Remove unused method from token invalid notice class;
  • Loading branch information
message-dimke committed Jul 29, 2024
1 parent 36b195f commit b6289c9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/Feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Automattic\WooCommerce\Pinterest\API\APIV5;
use Automattic\WooCommerce\Pinterest\LocalFeedConfigs;
use Automattic\WooCommerce\Pinterest\Exception\PinterestApiLocaleException;
use Automattic\WooCommerce\Pinterest\Notes\FeedNameExistsFailure;
use Exception;
use Pinterest_For_Woocommerce;
use Throwable;

/**
Expand Down Expand Up @@ -152,6 +154,10 @@ public static function create_feed(): string {
try {
$feed = APIV5::create_feed( $data, $ad_account_id );
} catch ( PinterestApiException $e ) {
if ( 422 === $e->getCode() ) {
FeedNameExistsFailure::possibly_add_note();
}

$delay = Pinterest_For_Woocommerce()::get_data( 'create_feed_delay' ) ?? MINUTE_IN_SECONDS;
set_transient( $cache_key, $e->getCode(), $delay );
// Double the delay.
Expand Down
83 changes: 83 additions & 0 deletions src/Notes/FeedNameExistsFailure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* WooCommerce Admin: Feed with the same name already exists at Pinterest notice.
*
* Adds a note to inform clients there is a feed from another website already at Pinterest.
*
* @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;

/**
* Feed name already exists at Pinterest admin notice.
*/
class FeedNameExistsFailure {
/**
* Note traits.
*/
use NoteTraits;

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

/**
* Get the note.
*
* @since x.x.x
*
* @return Note
*/
public static function get_note() {
$content_lines = array(
__( 'The Pinterest For WooCommerce plugin has detected an issue with your feed.<br/>The feed with the same name already exists at Pinterest.<br/>This can be due to another website already connected to the same Pinterest account.', 'pinterest-for-woocommerce' ),
);

$additional_data = array(
'role' => 'administrator',
);

$note = new Note();
$note->set_title( __( 'Pinterest For WooCommerce action required.', 'pinterest-for-woocommerce' ) );
$note->set_content( implode( '', $content_lines ) );
$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' );
$note->add_action(
'pinterest-for-woocommerce-feed-name-exists-failure-disconnect',
__( 'Disconnect Pinterest', 'pinterest-for-woocommerce' ),
admin_url( 'admin.php?page=wc-admin&path=/pinterest/onboarding' ),
Note::E_WC_ADMIN_NOTE_ACTIONED,
true,
'primary'
);
return $note;
}

/**
* Add the note if it passes predefined conditions.
*
* @since x.x.x
*
* @return void
* @throws NotesUnavailableException Throws exception when notes are unavailable.
*/
public static function possibly_add_note() {
if ( self::note_exists() ) {
return;
}

$note = self::get_note();
$note->save();
}
}
9 changes: 0 additions & 9 deletions src/Notes/TokenInvalidFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,4 @@ public static function possibly_add_note() {
$note = self::get_note();
$note->save();
}

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

0 comments on commit b6289c9

Please sign in to comment.