Avoid get_notes() in Notes::possibly_delete_survey_notes() (https://github.com/woocommerce/woocommerce-admin/pull/8206)

* Query for admin survey note_ids directly in Notes::possibly_delete_survey_notes().

* Add get_notes_with_type( $note_type ) method to Automattic\WooCommerce\Admin\Notes.
Name selected for consistency with preexisting get_notes_with_name( $name ) method.

* Use new get_notes_with_type() in Notes::possibly_delete_marketing_notes().

* Use new get_notes_with_type() in Notes::possibly_delete_survey_notes().

* Correct copy-pasta error querying for MARKETING notes instead of SURVEY notes in possibly_delete_survey_notes().

* Only delete actioned notes in possibly_delete_survey_notes(), which was the previous behavior.

* Changed get_notes_with_type() to get_note_ids_by_type() in Notes data store.

* Changelog for 7987/8206.

* Add missing method name in changelog for 7987/8206.
This commit is contained in:
Jacob Sewell 2022-03-10 18:57:29 -06:00 committed by GitHub
parent 7c8c0ee893
commit 2a7df822b3
3 changed files with 27 additions and 16 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: Enhancement
Add get_note_ids_by_type method to Notes DataStore to avoid woocommerce_note_where_clauses filter in possibly_delete_survey_notes() and possibly_delete_marketing_notes() actions. #8206

View File

@ -477,4 +477,20 @@ class DataStore extends \WC_Data_Store_WP implements \WC_Object_Data_Store_Inter
)
);
}
/**
* Find the ids of all notes with a given type.
*
* @param string $note_type Type to search for.
* @return array An array of matching note ids.
*/
public function get_note_ids_by_type( $note_type ) {
global $wpdb;
return $wpdb->get_col(
$wpdb->prepare(
"SELECT note_id FROM {$wpdb->prefix}wc_admin_notes WHERE type = %s ORDER BY note_id ASC",
$note_type
)
);
}
}

View File

@ -274,14 +274,10 @@ class Notes {
}
$data_store = self::load_data_store();
$notes = $data_store->get_notes(
array(
'type' => array( Note::E_WC_ADMIN_NOTE_MARKETING ),
)
);
$note_ids = $data_store->get_note_ids_by_type( Note::E_WC_ADMIN_NOTE_MARKETING );
foreach ( $notes as $note ) {
$note = self::get_note( $note->note_id );
foreach ( $note_ids as $note_id ) {
$note = self::get_note( $note_id );
if ( $note ) {
$note->delete();
}
@ -293,16 +289,11 @@ class Notes {
*/
public static function possibly_delete_survey_notes() {
$data_store = self::load_data_store();
$notes = $data_store->get_notes(
array(
'type' => array( Note::E_WC_ADMIN_NOTE_SURVEY ),
'status' => array( 'actioned' ),
)
);
$note_ids = $data_store->get_note_ids_by_type( Note::E_WC_ADMIN_NOTE_SURVEY );
foreach ( $notes as $note ) {
$note = self::get_note( $note->note_id );
if ( $note ) {
foreach ( $note_ids as $note_id ) {
$note = self::get_note( $note_id );
if ( $note && ( $note->get_status() === Note::E_WC_ADMIN_NOTE_ACTIONED ) ) {
$note->set_is_deleted( 1 );
$note->save();
}