Avoid `get_notes` call in `CouponPageMoved` (https://github.com/woocommerce/woocommerce-admin/pull/8202)
* First draft of CouponPageMoved::has_(unactioned|dismissed)_note() method changes to avoid get_notes(). * Add static function get_note_by_name( $note_name ) to Automattic\WooCommerce\Admin\Notes\Notes class. * Use Notes::get_note_by_name() in Notes::get_note_status(). * Use new Notes::get_note_by_name() in CouponPageMoved::has_unactioned_note(). * Use new Notes::get_note_by_name() in CouponPageMoved::has_dismissed_note(). * Add changelog for 7986/8202.
This commit is contained in:
parent
8d3eefb508
commit
9c18a427fa
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: Performance
|
||||
|
||||
Avoid expensive get_notes() queries in CouponPageMoved admin_init actions by using new Notes::get_note_by_name() helper method. #8202
|
|
@ -94,15 +94,13 @@ class CouponPageMoved {
|
|||
* @return bool
|
||||
*/
|
||||
protected static function has_unactioned_note() {
|
||||
$notes = self::get_data_store()->get_notes(
|
||||
[
|
||||
'name' => [ self::NOTE_NAME ],
|
||||
'status' => [ 'unactioned' ],
|
||||
'is_deleted' => false,
|
||||
]
|
||||
);
|
||||
$note = Notes::get_note_by_name( self::NOTE_NAME );
|
||||
|
||||
return ! empty( $notes );
|
||||
if ( ! $note ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $note->get_status() === 'unactioned';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -111,14 +109,13 @@ class CouponPageMoved {
|
|||
* @return bool
|
||||
*/
|
||||
protected static function has_dismissed_note() {
|
||||
$notes = self::get_data_store()->get_notes(
|
||||
[
|
||||
'name' => [ self::NOTE_NAME ],
|
||||
'is_deleted' => true,
|
||||
]
|
||||
);
|
||||
$note = Notes::get_note_by_name( self::NOTE_NAME );
|
||||
|
||||
return ! empty( $notes );
|
||||
if ( ! $note ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! $note->get_is_deleted();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -82,6 +82,26 @@ class Notes {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get admin note using its name.
|
||||
*
|
||||
* This is a shortcut for the common pattern of looking up note ids by name and then passing the first id to get_note().
|
||||
* It will behave unpredictably when more than one note with the given name exists.
|
||||
*
|
||||
* @param string $note_name Note name.
|
||||
* @return Note|bool
|
||||
**/
|
||||
public static function get_note_by_name( $note_name ) {
|
||||
$data_store = self::load_data_store();
|
||||
$note_ids = $data_store->get_notes_with_name( $note_name );
|
||||
|
||||
if ( empty( $note_ids ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::get_note( $note_ids[0] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total number of notes
|
||||
*
|
||||
|
@ -296,15 +316,12 @@ class Notes {
|
|||
* @return string|bool The note status.
|
||||
*/
|
||||
public static function get_note_status( $note_name ) {
|
||||
$data_store = self::load_data_store();
|
||||
$note_ids = $data_store->get_notes_with_name( $note_name );
|
||||
$note = self::get_note_by_name( $note_name );
|
||||
|
||||
if ( empty( $note_ids ) ) {
|
||||
if ( ! $note ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$note = self::get_note( $note_ids[0] );
|
||||
|
||||
return $note->get_status();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue