From f0aebb804692d7b85fbfe76480d1a29a9f7edbc5 Mon Sep 17 00:00:00 2001 From: Jacob Sewell Date: Mon, 15 Nov 2021 22:51:53 -0600 Subject: [PATCH] Have NavigationNudge and NavigationFeedback self-delete when irrelevant (https://github.com/woocommerce/woocommerce-admin/pull/7914) * If the navigation feature does not exist, have the NavigationNudge note self-action. * Create delete_if_not_supported method on NavigationNudge note. If we encounter this situation again we might consider adding a version of this method to NoteTraits. * Add possibly_delete_notes() to do_wc_admin_daily(). * Add changelog entry for Issue 7807. * Add should_note_exist() method to NavigationNudge note. * Add should_note_exist() and delete_if_not_supported() to NavigationFeedback. * Add NavigationFeedback::delete_if_not_supported() to possibly_delete_notes() in daily cron. * Fix changelog missing PR number. * Update changelog to include navigation feedback note. * Rename should_note_exist and delete_if_not_supported to is_applicable and delete_if_not_applicable, add to NoteTraits. --- ...807-navigation-nudge-shows-up-when-invalid | 4 +++ plugins/woocommerce-admin/src/Events.php | 9 +++++++ .../src/Notes/NavigationFeedback.php | 7 ++++++ .../src/Notes/NavigationNudge.php | 9 ++++++- .../src/Notes/NoteTraits.php | 25 +++++++++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce-admin/changelogs/fix-7807-navigation-nudge-shows-up-when-invalid diff --git a/plugins/woocommerce-admin/changelogs/fix-7807-navigation-nudge-shows-up-when-invalid b/plugins/woocommerce-admin/changelogs/fix-7807-navigation-nudge-shows-up-when-invalid new file mode 100644 index 00000000000..fb6cc764e1b --- /dev/null +++ b/plugins/woocommerce-admin/changelogs/fix-7807-navigation-nudge-shows-up-when-invalid @@ -0,0 +1,4 @@ +Significance: patch +Type: Fix + +Navigation nudge note and navigation feedback notes will delete themselves if the navigation feature is not available. #7914 diff --git a/plugins/woocommerce-admin/src/Events.php b/plugins/woocommerce-admin/src/Events.php index 156e5b21a61..08a0d0b4f2e 100644 --- a/plugins/woocommerce-admin/src/Events.php +++ b/plugins/woocommerce-admin/src/Events.php @@ -97,6 +97,7 @@ class Events { */ public function do_wc_admin_daily() { $this->possibly_add_notes(); + $this->possibly_delete_notes(); if ( $this->is_remote_inbox_notifications_enabled() ) { DataSourcePoller::get_instance()->read_specs_from_data_sources(); @@ -156,6 +157,14 @@ class Events { UpdateStoreDetails::possibly_add_note(); } + /** + * Deletes notes that should be deleted. + */ + protected function possibly_delete_notes() { + NavigationNudge::delete_if_not_applicable(); + NavigationFeedback::delete_if_not_applicable(); + } + /** * Checks if remote inbox notifications are enabled. * diff --git a/plugins/woocommerce-admin/src/Notes/NavigationFeedback.php b/plugins/woocommerce-admin/src/Notes/NavigationFeedback.php index 53361079cc5..9a85392c710 100644 --- a/plugins/woocommerce-admin/src/Notes/NavigationFeedback.php +++ b/plugins/woocommerce-admin/src/Notes/NavigationFeedback.php @@ -24,6 +24,13 @@ class NavigationFeedback { */ const NOTE_NAME = 'wc-admin-navigation-feedback'; + /** + * Should this note exist? (The navigation feature should exist.) + */ + public static function is_applicable() { + return Features::exists( 'navigation' ); + } + /** * Get the note. * diff --git a/plugins/woocommerce-admin/src/Notes/NavigationNudge.php b/plugins/woocommerce-admin/src/Notes/NavigationNudge.php index d1691cebb71..9c95b6f7850 100644 --- a/plugins/woocommerce-admin/src/Notes/NavigationNudge.php +++ b/plugins/woocommerce-admin/src/Notes/NavigationNudge.php @@ -33,13 +33,20 @@ class NavigationNudge { add_action( 'update_option_' . Navigation::TOGGLE_OPTION_NAME, array( $this, 'action_note' ), 10, 2 ); } + /** + * Should this note exist? (The navigation feature should exist.) + */ + public static function is_applicable() { + return Features::exists( 'navigation' ); + } + /** * Get the note. * * @return Note */ public static function get_note() { - if ( Features::is_enabled( 'navigation' ) || ! Features::exists( 'navigation' ) ) { + if ( Features::is_enabled( 'navigation' ) || ! self::is_applicable() ) { return; } diff --git a/plugins/woocommerce-admin/src/Notes/NoteTraits.php b/plugins/woocommerce-admin/src/Notes/NoteTraits.php index e8281a9a0cb..712fc4d8389 100644 --- a/plugins/woocommerce-admin/src/Notes/NoteTraits.php +++ b/plugins/woocommerce-admin/src/Notes/NoteTraits.php @@ -98,6 +98,31 @@ trait NoteTraits { self::possibly_add_note(); } + /** + * Should this note exist? (Default implementation is generous. Override as needed.) + */ + public static function is_applicable() { + return true; + } + + /** + * Delete this note if it is not applicable, unless has been soft-deleted or actioned already. + */ + public static function delete_if_not_applicable() { + if ( ! self::is_applicable() ) { + $data_store = Notes::load_data_store(); + $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME ); + + if ( ! empty( $note_ids ) ) { + $note = Notes::get_note( $note_ids[0] ); + + if ( ! $note->get_is_deleted() && ( Note::E_WC_ADMIN_NOTE_ACTIONED !== $note->get_status() ) ) { + return self::possibly_delete_note(); + } + } + } + } + /** * Possibly delete the note, if it exists in the database. Note that this * is a hard delete, for where it doesn't make sense to soft delete or