diff --git a/plugins/woocommerce/changelog/fix-unsnooze-notes b/plugins/woocommerce/changelog/fix-unsnooze-notes new file mode 100644 index 00000000000..a2f5b082011 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-unsnooze-notes @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix wc_admin_unsnooze_admin_notes events are being needlessly created diff --git a/plugins/woocommerce/src/Admin/Notes/Note.php b/plugins/woocommerce/src/Admin/Notes/Note.php index de8cde59dc7..af986572efa 100644 --- a/plugins/woocommerce/src/Admin/Notes/Note.php +++ b/plugins/woocommerce/src/Admin/Notes/Note.php @@ -534,7 +534,7 @@ class Note extends \WC_Data { $this->error( 'admin_note_invalid_data', __( 'The admin note date prop cannot be empty.', 'woocommerce' ) ); } - if ( is_string( $date ) ) { + if ( is_string( $date ) && ! is_numeric( $date ) ) { $date = wc_string_to_timestamp( $date ); } $this->set_date_prop( 'date_created', $date ); @@ -546,7 +546,7 @@ class Note extends \WC_Data { * @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date. */ public function set_date_reminder( $date ) { - if ( is_string( $date ) ) { + if ( is_string( $date ) && ! is_numeric( $date ) ) { $date = wc_string_to_timestamp( $date ); } $this->set_date_prop( 'date_reminder', $date ); diff --git a/plugins/woocommerce/src/Admin/Notes/Notes.php b/plugins/woocommerce/src/Admin/Notes/Notes.php index 138a49cafd6..8a7c6ccd2c2 100644 --- a/plugins/woocommerce/src/Admin/Notes/Notes.php +++ b/plugins/woocommerce/src/Admin/Notes/Notes.php @@ -24,6 +24,7 @@ class Notes { 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 ); + add_action( self::UNSNOOZE_HOOK, array( __CLASS__, 'unsnooze_notes' ) ); } /** @@ -406,7 +407,6 @@ class Notes { wp_set_current_user( $user_id ); self::record_tracks_event_without_cookies( $event_name, $params ); wp_set_current_user( $current_user_id ); - } /** diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/notes/class-wc-tests-notes-note.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/notes/class-wc-tests-notes-note.php index 700fb702d0e..d4f7d0f8455 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/notes/class-wc-tests-notes-note.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/notes/class-wc-tests-notes-note.php @@ -53,4 +53,31 @@ class WC_Admin_Tests_Notes_Note extends WC_Unit_Test_Case { $this->assertEquals( $date_created_from_first_save, $date_created_from_second_save ); } + /** + * Tests setting date_reminder with various input types. + * + * @dataProvider date_reminder_provider + * @param mixed $input Input date value. + * @param int $expected_timestamp Expected timestamp. + */ + public function test_set_date_reminder_with_various_inputs( $input, $expected_timestamp ) { + $note = new Note(); + $note->set_date_reminder( $input ); + $date_reminder = $note->get_date_reminder(); + $this->assertEquals( $expected_timestamp, $date_reminder ); + } + + /** + * Data provider for test_set_date_reminder_with_various_inputs. + * + * @return array + */ + public function date_reminder_provider() { + return array( + 'timestamp' => array( 1609459200, '2021-01-01T00:00:00+00:00' ), + 'timestamp string' => array( '1609459200', '2021-01-01T00:00:00+00:00' ), + 'date string' => array( '2021-01-01', '2021-01-01T00:00:00+00:00' ), + 'WC_DateTime object' => array( new WC_DateTime( '2021-01-01' ), '2021-01-01T00:00:00+00:00' ), + ); + } }