From 7b1a485100278451aceddcdf1e714cd969d1271f Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 2 Jul 2020 15:56:06 -0300 Subject: [PATCH] Added new note type (https://github.com/woocommerce/woocommerce-admin/pull/4686) * Added new note type This commit adds a new note type `survey`. The note is deleted after calling its CTA. * Now using const from WC_Admin_Note * Now using const from WC_Admin_Note in WC_Admin_Notes too * Moved note deletion from NoteActions to WC_Admin_Notes This commit moves the note deletion from NoteActions to WC_Admin_Notes. Also adds a hook to clean up actioned survey notes. Co-authored-by: Fernando Marichal --- .../client/wc-api/constants.js | 2 +- .../woocommerce-admin/src/API/NoteActions.php | 1 + .../src/Notes/WC_Admin_Note.php | 2 ++ .../src/Notes/WC_Admin_Notes.php | 27 ++++++++++++++++++- .../WC_Admin_Notes_Insight_First_Sale.php | 4 +-- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/plugins/woocommerce-admin/client/wc-api/constants.js b/plugins/woocommerce-admin/client/wc-api/constants.js index 6a9724fe71b..df6be3b1963 100644 --- a/plugins/woocommerce-admin/client/wc-api/constants.js +++ b/plugins/woocommerce-admin/client/wc-api/constants.js @@ -22,5 +22,5 @@ export const QUERY_DEFAULTS = { pageSize: 25, period: 'month', compare: 'previous_year', - noteTypes: 'info,warning,marketing', + noteTypes: 'info,warning,marketing,survey', }; diff --git a/plugins/woocommerce-admin/src/API/NoteActions.php b/plugins/woocommerce-admin/src/API/NoteActions.php index 9342c893064..ae37787d830 100644 --- a/plugins/woocommerce-admin/src/API/NoteActions.php +++ b/plugins/woocommerce-admin/src/API/NoteActions.php @@ -11,6 +11,7 @@ namespace Automattic\WooCommerce\Admin\API; defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Admin\Notes\WC_Admin_Note; use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes; /** diff --git a/plugins/woocommerce-admin/src/Notes/WC_Admin_Note.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Note.php index 253626106b3..1ab4922acae 100644 --- a/plugins/woocommerce-admin/src/Notes/WC_Admin_Note.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Note.php @@ -22,6 +22,7 @@ class WC_Admin_Note extends \WC_Data { const E_WC_ADMIN_NOTE_UPDATE = 'update'; // i.e. used when a new version is available. const E_WC_ADMIN_NOTE_INFORMATIONAL = 'info'; // used for presenting informational messages. const E_WC_ADMIN_NOTE_MARKETING = 'marketing'; // used for adding marketing messages. + const E_WC_ADMIN_NOTE_SURVEY = 'survey'; // used for adding survey messages. // Note status codes. const E_WC_ADMIN_NOTE_PENDING = 'pending'; // the note is pending - hidden but not actioned. @@ -126,6 +127,7 @@ class WC_Admin_Note extends \WC_Data { self::E_WC_ADMIN_NOTE_UPDATE, self::E_WC_ADMIN_NOTE_INFORMATIONAL, self::E_WC_ADMIN_NOTE_MARKETING, + self::E_WC_ADMIN_NOTE_SURVEY, ); return apply_filters( 'woocommerce_note_types', $allowed_types ); diff --git a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php index c66505cd90d..9104257777a 100644 --- a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes.php @@ -24,6 +24,7 @@ class WC_Admin_Notes { */ public static function init() { add_action( 'admin_init', array( __CLASS__, 'schedule_unsnooze_notes' ) ); + add_action( 'admin_init', array( __CLASS__, 'possibly_delete_survey_notes' ) ); add_action( 'update_option_woocommerce_show_marketplace_suggestions', array( __CLASS__, 'possibly_delete_marketing_notes' ), 10, 2 ); } @@ -164,7 +165,12 @@ class WC_Admin_Notes { 'orderby' => 'date_created', 'per_page' => 25, 'page' => 1, - 'type' => array( 'info', 'marketing', 'warning' ), + 'type' => array( + WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL, + WC_Admin_Note::E_WC_ADMIN_NOTE_MARKETING, + WC_Admin_Note::E_WC_ADMIN_NOTE_WARNING, + WC_Admin_Note::E_WC_ADMIN_NOTE_SURVEY, + ), 'is_deleted' => 0, ) ); @@ -241,4 +247,23 @@ class WC_Admin_Notes { $note->delete(); } } + + /** + * Delete actioned survey notes. + */ + public static function possibly_delete_survey_notes() { + $data_store = \WC_Data_Store::load( 'admin-note' ); + $notes = $data_store->get_notes( + array( + 'type' => array( WC_Admin_Note::E_WC_ADMIN_NOTE_SURVEY ), + 'status' => array( 'actioned' ), + ) + ); + + foreach ( $notes as $note ) { + $note = new WC_Admin_Note( $note->note_id ); + $note->set_is_deleted( 1 ); + $note->save(); + } + } } diff --git a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Insight_First_Sale.php b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Insight_First_Sale.php index afc2afa46d8..d5a0edc4d9c 100644 --- a/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Insight_First_Sale.php +++ b/plugins/woocommerce-admin/src/Notes/WC_Admin_Notes_Insight_First_Sale.php @@ -35,9 +35,9 @@ class WC_Admin_Notes_Insight_First_Sale { } $note = new WC_Admin_Note(); - $note->set_title( __( 'Insight', 'woocommerce-admin' ) ); + $note->set_title( __( 'Did you know?', 'woocommerce-admin' ) ); $note->set_content( __( 'A WooCommerce powered store needs on average 31 days to get the first sale. You\'re on the right track! Do you find this type of insight useful?', 'woocommerce-admin' ) ); - $note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); + $note->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_SURVEY ); $note->set_name( self::NOTE_NAME ); $note->set_content_data( (object) array() ); $note->set_source( 'woocommerce-admin' );