Skip to content

Commit

Permalink
Better implementation of the original idea
Browse files Browse the repository at this point in the history
  • Loading branch information
pls78 committed Oct 30, 2023
1 parent f06f1ee commit 2fad611
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
35 changes: 8 additions & 27 deletions admin/class-yoast-notification-center.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,15 @@ private function retrieve_notifications_from_storage( $user_id ) {
// Apply array_values to ensure we get a 0-indexed array.
$notifications = array_values( array_filter( $notifications, [ $this, 'filter_notification_current_user' ] ) );

$processed_notifications = $this->maybe_unset_notification_user_field( $user_id, $notifications );
foreach ( $notifications as $notification ) {
$notification_has_been_updated = $notification->user_to_user_id();
// Just one notification changed is enough to update the storage.
if ( $notification_has_been_updated ) {
$this->notifications_need_storage = true;
}
}

$this->notifications[ $user_id ] = $processed_notifications;
$this->notifications[ $user_id ] = $notifications;
}
}

Expand Down Expand Up @@ -898,31 +904,6 @@ private function add_transaction_to_queue( $callback, $args ) {
$this->queued_transactions[] = [ $callback, $args ];
}

/**
* Removes the 'user' field from notifications if it is set, and adds the 'user_id' one.
*
* @param int $user_id The ID of the user to set the notifications for.
* @param Yoast_Notification[] $notifications The notifications to process.
*
* @return Yoast_Notification[] The processed notifications.
*/
private function maybe_unset_notification_user_field( $user_id, $notifications ) {
$processed_notifications = [];

foreach ( $notifications as $notification ) {
$serialized_notification = $this->notification_to_array( $notification );
if ( array_key_exists( 'user', $serialized_notification['options'] ) ) {
unset( $serialized_notification['options']['user'] );
$serialized_notification['options']['user_id'] = $user_id;

$this->notifications_need_storage = true;
}
$processed_notifications[] = $this->array_to_notification( $serialized_notification );
}

return $processed_notifications;
}

/**
* Removes all notifications from storage.
*
Expand Down
18 changes: 18 additions & 0 deletions admin/class-yoast-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,22 @@ private function normalize_options( $options ) {
private function parse_attributes( &$value, $key ) {
$value = sprintf( '%s="%s"', sanitize_key( $key ), esc_attr( $value ) );
}

/**
* Unsets user field if present and set user_id.
*
* @internal This is meant to be used by the Yoast SEO plugin only.
*
* @return bool If the user field was present.
*/
public function user_to_user_id() {
if ( array_key_exists( 'user', $this->options ) ) {
// No check needed as we call this once the notification has already been stored.
$user_id = $this->options['user']->ID;
unset( $this->options['user'] );
$this->options['user_id'] = $user_id;
return true;
}
return false;
}
}

0 comments on commit 2fad611

Please sign in to comment.