namespace WPForms\Emails;
use WPForms\Admin\Notifications\Notifications;
* This class is responsible for displaying the notification block in the email summaries.
class NotificationBlocks {
* Notifications class instance.
* Initializes the Notifications class instance.
public function __construct() {
// Store the instance of the "Notifications" class.
$this->notifications = wpforms()->obj( 'notifications' );
* Retrieves the notification block from the feed, considering shown notifications and license type.
public function get_block(): array {
// Check if the user has access to notifications.
// If the user has disabled announcements, return an empty array.
if ( ! $this->notifications || ! $this->notifications->has_access() ) {
// Get the response array from the notifications.
$notifications = $this->notifications->get_option();
// Check if 'feed' key is present and non-empty.
if ( empty( $notifications['feed'] ) || ! is_array( $notifications['feed'] ) ) {
// Remove items from $feed where their id index is in `shown_notifications` option value.
$feed = $this->filter_feed( $notifications['feed'] );
// Sort the array of items using usort and the custom comparison function.
$feed = $this->sort_feed( $feed );
// Get the very first item from the $feed.
// Check if $block is empty.
// Return the notification block.
return $this->prepare_and_sanitize_content( $block );
* Save the shown notification block if it's not empty.
* @param array $notification The notification to be saved.
public function maybe_remember_shown_block( array $notification ) {
// Check if the notification or its ID is empty.
if ( empty( $notification ) || empty( $notification['id'] ) ) {
// If the notification or its ID is empty, return early.
// Get shown notifications from options.
$shown_notifications = (array) get_option( 'wpforms_email_summaries_shown_notifications', [] );
// Add the notification id to the $shown_notifications array.
$shown_notifications[] = (int) $notification['id'];
// Update the shown notifications in the options.
// Avoid autoloading the option, as it's not needed.
update_option( 'wpforms_email_summaries_shown_notifications', $shown_notifications, false );
* Filter the feed to remove shown notifications.
* @param array $feed The feed to filter.
private function filter_feed( array $feed ): array {
$shown_notifications = (array) get_option( 'wpforms_email_summaries_shown_notifications', [] );
static function ( $item ) use ( $shown_notifications ) {
return ! in_array( $item['id'], $shown_notifications, true );
* Sort the feed in descending order by start date.
* @param array $feed The feed to sort.
private function sort_feed( array $feed ): array {
static function ( $a, $b ) {
return strtotime( $b['start'] ) - strtotime( $a['start'] );
* Prepare and sanitize content for display.
* @param string|array $content The content to be prepared and sanitized.
private function prepare_and_sanitize_content( $content ) {
// If the content is empty, return as is.
if ( empty( $content ) ) {
// If the content is already a string, sanitize and return it.
if ( is_string( $content ) ) {
// Define allowed HTML tags and attributes.
$content_allowed_tags = $this->notifications->get_allowed_tags();
// For design consistency, remove the 'p' tag from the allowed tags.
unset( $content_allowed_tags['p'] );
// Apply wp_kses() for sanitization.
return wp_kses( $content, $content_allowed_tags );
// If the content is an array with the 'content' index, modify and sanitize it.
if ( is_array( $content ) && isset( $content['content'] ) ) {
// Sanitize the content of the array.
$content['content'] = $this->prepare_and_sanitize_content( $content['content'] );
// Return the modified array.
// If the content is not a string or an array with 'content' index, return the content as is.