From f639d4a32ef9942123178bffb3db74b1a81b5ebf Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Tue, 5 May 2020 15:38:16 -0400 Subject: [PATCH] Remove obsolete inbox messages (https://github.com/woocommerce/woocommerce-admin/pull/4182) * Remove welcome note * Remove store notice setting note * Remove review your experience note * Add tests for delete_notes_with_name * Add filter support so that other code can add notes to be deleted on upgrade. --- plugins/woocommerce-admin/src/Events.php | 2 - .../woocommerce-admin/src/FeaturePlugin.php | 2 - .../src/Features/ActivityPanels.php | 9 -- plugins/woocommerce-admin/src/Install.php | 37 +++++- .../src/Notes/WC_Admin_Notes.php | 21 +++- .../WC_Admin_Notes_Giving_Feedback_Notes.php | 66 ---------- .../Notes/WC_Admin_Notes_Settings_Notes.php | 55 --------- .../Notes/WC_Admin_Notes_Welcome_Message.php | 55 --------- .../notes/class-wc-tests-notes-data-store.php | 116 +++++++++++++++++- 9 files changed, 162 insertions(+), 201 deletions(-) delete mode 100644 plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Giving_Feedback_Notes.php delete mode 100644 plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Settings_Notes.php delete mode 100644 plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Welcome_Message.php diff --git a/plugins/woocommerce-admin/src/Events.php b/plugins/woocommerce-admin/src/Events.php index 3382b3cd3b6..bcd446f87a4 100644 --- a/plugins/woocommerce-admin/src/Events.php +++ b/plugins/woocommerce-admin/src/Events.php @@ -10,7 +10,6 @@ namespace Automattic\WooCommerce\Admin; defined( 'ABSPATH' ) || exit; -use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Giving_Feedback_Notes; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Mobile_App; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_New_Sales_Record; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Tracking_Opt_In; @@ -64,7 +63,6 @@ class Events { */ public function do_wc_admin_daily() { WC_Admin_Notes_New_Sales_Record::possibly_add_sales_record_note(); - WC_Admin_Notes_Giving_Feedback_Notes::add_notes_for_admin_giving_feedback(); WC_Admin_Notes_Mobile_App::possibly_add_mobile_app_note(); WC_Admin_Notes_Tracking_Opt_In::possibly_add_tracking_opt_in_note(); WC_Admin_Notes_Onboarding_Email_Marketing::possibly_add_onboarding_email_marketing_note(); diff --git a/plugins/woocommerce-admin/src/FeaturePlugin.php b/plugins/woocommerce-admin/src/FeaturePlugin.php index ca81aebe5c0..488be40f1a8 100644 --- a/plugins/woocommerce-admin/src/FeaturePlugin.php +++ b/plugins/woocommerce-admin/src/FeaturePlugin.php @@ -12,7 +12,6 @@ defined( 'ABSPATH' ) || exit; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Historical_Data; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Order_Milestones; -use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Welcome_Message; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Woo_Subscriptions_Notes; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Tracking_Opt_In; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_WooCommerce_Payments; @@ -184,7 +183,6 @@ class FeaturePlugin { new WC_Admin_Notes_Woo_Subscriptions_Notes(); new WC_Admin_Notes_Historical_Data(); new WC_Admin_Notes_Order_Milestones(); - new WC_Admin_Notes_Welcome_Message(); new WC_Admin_Notes_Tracking_Opt_In(); new WC_Admin_Notes_WooCommerce_Payments(); new WC_Admin_Notes_Install_JP_And_WCS_Plugins(); diff --git a/plugins/woocommerce-admin/src/Features/ActivityPanels.php b/plugins/woocommerce-admin/src/Features/ActivityPanels.php index 052d44e9dfb..55071585c60 100644 --- a/plugins/woocommerce-admin/src/Features/ActivityPanels.php +++ b/plugins/woocommerce-admin/src/Features/ActivityPanels.php @@ -9,7 +9,6 @@ namespace Automattic\WooCommerce\Admin\Features; use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes; -use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Settings_Notes; /** * Contains backend logic for the activity panel feature. @@ -46,7 +45,6 @@ class ActivityPanels { add_filter( 'woocommerce_components_settings', array( $this, 'component_settings' ), 20 ); // New settings injection. add_filter( 'woocommerce_shared_settings', array( $this, 'component_settings' ), 20 ); - add_action( 'woocommerce_admin_updated', array( $this, 'woocommerce_updated_note' ) ); add_action( 'woocommerce_update_product', array( __CLASS__, 'clear_low_out_of_stock_count_transient' ) ); } @@ -117,11 +115,4 @@ class ActivityPanels { $settings['hasLowStock'] = $this->has_low_stock_products(); return $settings; } - - /** - * Things to do after WooCommerce updates. - */ - public function woocommerce_updated_note() { - WC_Admin_Notes_Settings_Notes::add_notes_for_settings_that_have_moved(); - } } diff --git a/plugins/woocommerce-admin/src/Install.php b/plugins/woocommerce-admin/src/Install.php index 17c36a11537..61026a058f5 100644 --- a/plugins/woocommerce-admin/src/Install.php +++ b/plugins/woocommerce-admin/src/Install.php @@ -10,8 +10,8 @@ namespace Automattic\WooCommerce\Admin; defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Admin\API\Reports\Cache; +use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Historical_Data; -use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Welcome_Message; /** * Install Class. @@ -40,7 +40,7 @@ class Install { 'wc_admin_update_0251_remove_unsnooze_action', 'wc_admin_update_0251_db_version', ), - '1.1.0' => array( + '1.1.0' => array( 'wc_admin_update_110_remove_facebook_note', ), ); @@ -158,6 +158,7 @@ class Install { self::create_tables(); self::create_events(); + self::delete_obsolete_notes(); self::create_notes(); self::maybe_update_db_version(); @@ -448,16 +449,40 @@ class Install { if ( ! wp_next_scheduled( 'wc_admin_daily' ) ) { wp_schedule_event( time(), 'daily', 'wc_admin_daily' ); } - // @todo This is potentially redundant when the core package exists. + // Note: this is potentially redundant when the core package exists. wp_schedule_single_event( time() + 10, 'generate_category_lookup_table' ); } + /** + * Delete obsolete notes. + */ + protected static function delete_obsolete_notes() { + $obsolete_notes_names = array( + 'wc-admin-welcome-note', + 'wc-admin-store-notice-setting-moved', + 'wc-admin-store-notice-giving-feedback', + ); + + $additional_obsolete_notes_names = apply_filters( + 'woocommerce_admin_obsolete_notes_names', + array() + ); + + if ( is_array( $additional_obsolete_notes_names ) ) { + $obsolete_notes_names = array_merge( + $obsolete_notes_names, + $additional_obsolete_notes_names + ); + } + + WC_Admin_Notes::delete_notes_with_name( $obsolete_notes_names ); + } + /** * Create notes. */ protected static function create_notes() { WC_Admin_Notes_Historical_Data::add_note(); - WC_Admin_Notes_Welcome_Message::add_welcome_note(); } /** @@ -471,7 +496,9 @@ class Install { $tables = self::get_tables(); foreach ( $tables as $table ) { - $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); // WPCS: unprepared SQL ok. + /* phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared */ + $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); + /* phpcs:enable */ } } } diff --git a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php index d5a4b357941..6effc6a8e89 100644 --- a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php @@ -89,14 +89,23 @@ class WC_Admin_Notes { /** * Deletes admin notes with a given name. * - * @param string $name Name to search for. + * @param string|array $names Name(s) to search for. */ - public static function delete_notes_with_name( $name ) { + public static function delete_notes_with_name( $names ) { + if ( is_string( $names ) ) { + $names = array( $names ); + } elseif ( ! is_array( $names ) ) { + return; + } + $data_store = \WC_Data_Store::load( 'admin-note' ); - $note_ids = $data_store->get_notes_with_name( $name ); - foreach ( (array) $note_ids as $note_id ) { - $note = new WC_Admin_Note( $note_id ); - $note->delete(); + + foreach ( $names as $name ) { + $note_ids = $data_store->get_notes_with_name( $name ); + foreach ( (array) $note_ids as $note_id ) { + $note = new WC_Admin_Note( $note_id ); + $note->delete(); + } } } diff --git a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Giving_Feedback_Notes.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Giving_Feedback_Notes.php deleted file mode 100644 index 1ba1e885ba7..00000000000 --- a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Giving_Feedback_Notes.php +++ /dev/null @@ -1,66 +0,0 @@ -get_notes_with_name( $name ); - if ( ! empty( $note_ids ) ) { - return; - } - - // We need to show Admin Giving feeback notification after 3 days of install. - $three_days_in_seconds = 3 * DAY_IN_SECONDS; - if ( ! self::wc_admin_active_for( $three_days_in_seconds ) ) { - return; - } - - // Otherwise, create our new note. - $note = new WC_Admin_Note(); - $note->set_title( __( 'Review your experience', 'woocommerce-admin' ) ); - $note->set_content( __( 'If you like WooCommerce Admin please leave us a 5 star rating. A huge thanks in advance!', 'woocommerce-admin' ) ); - $note->set_content_data( (object) array() ); - $note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); - $note->set_icon( 'info' ); - $note->set_name( $name ); - $note->set_source( 'woocommerce-admin' ); - $note->add_action( - 'share-feedback', - __( 'Review', 'woocommerce-admin' ), - 'https://wordpress.org/support/plugin/woocommerce-admin/reviews/?rate=5#new-post' - ); - $note->save(); - } -} diff --git a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Settings_Notes.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Settings_Notes.php deleted file mode 100644 index 31caf056253..00000000000 --- a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Settings_Notes.php +++ /dev/null @@ -1,55 +0,0 @@ -get_notes_with_name( $name ); - if ( ! empty( $note_ids ) ) { - return; - } - - // Otherwise, create our new note. - $note = new WC_Admin_Note(); - $note->set_title( __( 'Looking for the Store Notice setting?', 'woocommerce-admin' ) ); - $note->set_content( __( 'It can now be found in the Customizer.', 'woocommerce-admin' ) ); - $note->set_content_data( (object) array() ); - $note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); - $note->set_icon( 'info' ); - $note->set_name( $name ); - $note->set_source( 'woocommerce-admin' ); - $note->add_action( - 'open-customizer', - __( 'Open Customizer', 'woocommerce-admin' ), - 'customize.php' - ); - $note->save(); - } -} diff --git a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Welcome_Message.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Welcome_Message.php deleted file mode 100644 index 3f048c21f36..00000000000 --- a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Welcome_Message.php +++ /dev/null @@ -1,55 +0,0 @@ -get_notes_with_name( self::NOTE_NAME ); - if ( ! empty( $note_ids ) ) { - return; - } - - $note = new WC_Admin_Note(); - $note->set_title( __( 'New feature(s)', 'woocommerce-admin' ) ); - $note->set_content( __( 'Welcome to the new WooCommerce experience! In this new release you\'ll be able to have a glimpse of how your store is doing in the Dashboard, manage important aspects of your business (such as managing orders, stock, reviews) from anywhere in the interface, dive into your store data with a completely new Analytics section and more!', 'woocommerce-admin' ) ); - $note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); - $note->set_icon( 'info' ); - $note->set_name( self::NOTE_NAME ); - $note->set_content_data( (object) array() ); - $note->set_source( 'woocommerce-admin' ); - $note->add_action( - 'learn-more', - __( 'Learn more', 'woocommerce-admin' ), - 'https://woocommerce.wordpress.com/' - ); - - $note->save(); - } -} diff --git a/plugins/woocommerce-admin/tests/notes/class-wc-tests-notes-data-store.php b/plugins/woocommerce-admin/tests/notes/class-wc-tests-notes-data-store.php index 1e71422fd0d..1037c877fd3 100644 --- a/plugins/woocommerce-admin/tests/notes/class-wc-tests-notes-data-store.php +++ b/plugins/woocommerce-admin/tests/notes/class-wc-tests-notes-data-store.php @@ -5,7 +5,8 @@ * @package WooCommerce\Tests\Notes */ -use Automattic\WooCommerce\Admin\Notes\WC_Admin_Note; +use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes; +use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Note; /** * Class WC_Tests_Notes_Data_Store @@ -84,4 +85,117 @@ class WC_Tests_Notes_Data_Store extends WC_Unit_Test_Case { $this->assertEquals( 'PHPUNIT_TEST_NOTE', $note->get_title() ); $this->assertEquals( 'PHPUNIT_TEST_NOTE_CONTENT', $note->get_content() ); } + + /** + * Tests that the delete_notes_with_name method works when a single name is + * passed in. + */ + public function test_delete_notes_with_single_name() { + $note_name_to_delete = 'PHPUNIT_TEST_NOTE_TO_DELETE_' . gmdate( 'U' ); + $note_name_to_keep = 'PHPUNIT_TEST_NOTE_TO_KEEP_' . gmdate( 'U' ); + $note_names = array( + $note_name_to_delete, + $note_name_to_keep, + ); + + $data_store = WC_Data_Store::load( 'admin-note' ); + + // Create notes. + foreach ( $note_names as $note_name ) { + for ( $i = 0; $i < 3; $i++ ) { + $note = new WC_Admin_Note(); + $note->set_name( $note_name ); + $note->set_title( 'PHPUNIT_TEST_NOTE' ); + $note->set_content( 'PHPUNIT_TEST_NOTE_CONTENT' ); + $note->save(); + } + } + + // Make sure that we know that the notes were added properly. + $this->assertEquals( + 3, + count( $data_store->get_notes_with_name( $note_name_to_delete ) ) + ); + $this->assertEquals( + 3, + count( $data_store->get_notes_with_name( $note_name_to_keep ) ) + ); + + // Delete the notes. + WC_Admin_Notes::delete_notes_with_name( $note_name_to_delete ); + + // Make sure the notes were deleted. + $this->assertEquals( + 0, + count( $data_store->get_notes_with_name( $note_name_to_delete ) ) + ); + $this->assertEquals( + 3, + count( $data_store->get_notes_with_name( $note_name_to_keep ) ) + ); + } + + /** + * Tests that the delete_notes_with_name method works when multiple names + * are passed in. + */ + public function test_delete_notes_with_multiple_names() { + $note_name_to_delete_1 = 'PHPUNIT_TEST_NOTE_TO_DELETE_1_' . gmdate( 'U' ); + $note_name_to_delete_2 = 'PHPUNIT_TEST_NOTE_TO_DELETE_2_' . gmdate( 'U' ); + $note_name_to_keep = 'PHPUNIT_TEST_NOTE_TO_KEEP_' . gmdate( 'U' ); + $note_names = array( + $note_name_to_delete_1, + $note_name_to_delete_2, + $note_name_to_keep, + ); + + $data_store = WC_Data_Store::load( 'admin-note' ); + + // Create notes. + foreach ( $note_names as $note_name ) { + for ( $i = 0; $i < 3; $i++ ) { + $note = new WC_Admin_Note(); + $note->set_name( $note_name ); + $note->set_title( 'PHPUNIT_TEST_NOTE' ); + $note->set_content( 'PHPUNIT_TEST_NOTE_CONTENT' ); + $note->save(); + } + } + + // Make sure that we know that the notes were added properly. + $this->assertEquals( + 3, + count( $data_store->get_notes_with_name( $note_name_to_delete_1 ) ) + ); + $this->assertEquals( + 3, + count( $data_store->get_notes_with_name( $note_name_to_delete_2 ) ) + ); + $this->assertEquals( + 3, + count( $data_store->get_notes_with_name( $note_name_to_keep ) ) + ); + + // Delete the notes. + WC_Admin_Notes::delete_notes_with_name( + array( + $note_name_to_delete_1, + $note_name_to_delete_2, + ) + ); + + // Make sure the notes were deleted. + $this->assertEquals( + 0, + count( $data_store->get_notes_with_name( $note_name_to_delete_1 ) ) + ); + $this->assertEquals( + 0, + count( $data_store->get_notes_with_name( $note_name_to_delete_2 ) ) + ); + $this->assertEquals( + 3, + count( $data_store->get_notes_with_name( $note_name_to_keep ) ) + ); + } }