Skip to content

Commit

Permalink
Extracted actions to the beginning of the function to allow usage in …
Browse files Browse the repository at this point in the history
…note_up_to_date check.
  • Loading branch information
peterfabian committed May 13, 2020
1 parent a7203ec commit eb0879c
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions includes/admin/notes/class-wc-notes-run-db-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ public static function set_notice_actioned() {
*
* @param WC_Admin_Note $note Note to check.
* @param string $update_url URL to check the note against.
* @param array(string) $current_actions List of actions to check for.
* @return bool
*/
private static function note_up_to_date( $note, $update_url ) {
private static function note_up_to_date( $note, $update_url, $current_actions ) {
$actions = $note->get_actions();
if ( 2 === count( array_intersect( wp_list_pluck( $actions, 'name' ), array( 'update-db_run', 'update-db_learn-more' ) ) )
if ( count( $current_actions ) === count( array_intersect( wp_list_pluck( $actions, 'name' ), $current_actions ) )
&& in_array( $update_url, wp_list_pluck( $actions, 'query' ) ) ) {
return true;
}
Expand All @@ -120,14 +121,31 @@ private static function update_needed_notice( $note_id = null ) {
)
);

$note_actions = array(
array(
'name' => 'update-db_run',
'label' => __( 'Update WooCommerce Database', 'woocommerce' ),
'url' => $update_url,
'status' => 'unactioned',
'primary' => true,
),
array(
'name' => 'update-db_learn-more',
'label' => __( 'Learn more about updates', 'woocommerce' ),
'url' => 'https://docs.woocommerce.com/document/how-to-update-woocommerce/',
'status' => 'unactioned',
'primary' => false,
),
);

if ( $note_id ) {
$note = new WC_Admin_Note( $note_id );
} else {
$note = new WC_Admin_Note();
}

// Check if the note needs to be updated (e.g. expired nonce or different note type stored in the previous run).
if ( self::note_up_to_date( $note, $update_url ) ) {
if ( self::note_up_to_date( $note, $update_url, wp_list_pluck( $note_actions, 'name' ) ) ) {
return $note_id;
}

Expand All @@ -148,20 +166,9 @@ private static function update_needed_notice( $note_id = null ) {

// Set new actions.
$note->clear_actions();
$note->add_action(
'update-db_run',
__( 'Update WooCommerce Database', 'woocommerce' ),
$update_url,
'unactioned',
true
);
$note->add_action(
'update-db_learn-more',
__( 'Learn more about updates', 'woocommerce' ),
'https://docs.woocommerce.com/document/how-to-update-woocommerce/',
'unactioned',
false
);
foreach ( $note_actions as $note_action ) {
$note->add_action( ...array_values( $note_action ) );
}

return $note->save();
}
Expand Down

0 comments on commit eb0879c

Please sign in to comment.