| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use Yoast_Notification; |
| 6 |
use Yoast_Notification_Center; |
| 7 |
|
| 8 |
/** |
| 9 |
* A helper object for notifications. |
| 10 |
*/ |
| 11 |
class Notification_Helper { |
| 12 |
|
| 13 |
/** |
| 14 |
* Restores a notification (wrapper function). |
| 15 |
* |
| 16 |
* @codeCoverageIgnore |
| 17 |
* |
| 18 |
* @param Yoast_Notification $notification The notification to restore. |
| 19 |
* |
| 20 |
* @return bool True if restored, false otherwise. |
| 21 |
*/ |
| 22 |
public function restore_notification( Yoast_Notification $notification ) { |
| 23 |
return Yoast_Notification_Center::restore_notification( $notification ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Return the notifications sorted on type and priority. (wrapper function) |
| 28 |
* |
| 29 |
* @codeCoverageIgnore |
| 30 |
* |
| 31 |
* @return Yoast_Notification[] Sorted Notifications |
| 32 |
*/ |
| 33 |
public function get_sorted_notifications() { |
| 34 |
$notification_center = Yoast_Notification_Center::get(); |
| 35 |
|
| 36 |
return $notification_center->get_sorted_notifications(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Check if the user has dismissed a notification. (wrapper function) |
| 41 |
* |
| 42 |
* @codeCoverageIgnore |
| 43 |
* |
| 44 |
* @param Yoast_Notification $notification The notification to check for dismissal. |
| 45 |
* @param int|null $user_id User ID to check on. |
| 46 |
* |
| 47 |
* @return bool |
| 48 |
*/ |
| 49 |
private function is_notification_dismissed( Yoast_Notification $notification, $user_id = null ) { |
| 50 |
return Yoast_Notification_Center::is_notification_dismissed( $notification, $user_id ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Parses all the notifications to an array with just id, message, nonce, type and dismissed. |
| 55 |
* |
| 56 |
* @return array<string, string|bool> |
| 57 |
*/ |
| 58 |
public function get_alerts(): array { |
| 59 |
$all_notifications = $this->get_sorted_notifications(); |
| 60 |
|
| 61 |
return \array_map( |
| 62 |
function ( $notification ) { |
| 63 |
return [ |
| 64 |
'id' => $notification->get_id(), |
| 65 |
'message' => $notification->get_message(), |
| 66 |
'nonce' => $notification->get_nonce(), |
| 67 |
'type' => $notification->get_type(), |
| 68 |
'dismissed' => $this->is_notification_dismissed( $notification ), |
| 69 |
'resolveNonce' => $notification->get_resolve_nonce(), |
| 70 |
]; |
| 71 |
}, |
| 72 |
$all_notifications, |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
|