Tasks
8 months ago
Templates
2 years ago
Helpers.php
2 years ago
InfoBlocks.php
1 year ago
Mailer.php
4 months ago
NotificationBlocks.php
1 year ago
Notifications.php
2 months ago
Preview.php
10 months ago
Styler.php
2 years ago
Summaries.php
8 months ago
NotificationBlocks.php
187 lines
| 1 | <?php |
| 2 | namespace WPForms\Emails; |
| 3 | |
| 4 | use WPForms\Admin\Notifications\Notifications; |
| 5 | |
| 6 | /** |
| 7 | * Notification class. |
| 8 | * This class is responsible for displaying the notification block in the email summaries. |
| 9 | * |
| 10 | * @since 1.8.8 |
| 11 | */ |
| 12 | class NotificationBlocks { |
| 13 | |
| 14 | /** |
| 15 | * Notifications class instance. |
| 16 | * |
| 17 | * @since 1.8.8 |
| 18 | * |
| 19 | * @var Notifications |
| 20 | */ |
| 21 | private $notifications; |
| 22 | |
| 23 | /** |
| 24 | * Class constructor. |
| 25 | * Initializes the Notifications class instance. |
| 26 | * |
| 27 | * @since 1.8.8 |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | |
| 31 | // Store the instance of the "Notifications" class. |
| 32 | $this->notifications = wpforms()->obj( 'notifications' ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Retrieves the notification block from the feed, considering shown notifications and license type. |
| 37 | * |
| 38 | * @since 1.8.8 |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public function get_block(): array { |
| 43 | |
| 44 | // Check if the user has access to notifications. |
| 45 | // If the user has disabled announcements, return an empty array. |
| 46 | if ( ! $this->notifications || ! $this->notifications->has_access() ) { |
| 47 | return []; |
| 48 | } |
| 49 | |
| 50 | // Get the response array from the notifications. |
| 51 | $notifications = $this->notifications->get_option(); |
| 52 | |
| 53 | // Check if 'feed' key is present and non-empty. |
| 54 | if ( empty( $notifications['feed'] ) || ! is_array( $notifications['feed'] ) ) { |
| 55 | return []; |
| 56 | } |
| 57 | |
| 58 | // Remove items from $feed where their id index is in `shown_notifications` option value. |
| 59 | $feed = $this->filter_feed( $notifications['feed'] ); |
| 60 | |
| 61 | // Sort the array of items using usort and the custom comparison function. |
| 62 | $feed = $this->sort_feed( $feed ); |
| 63 | |
| 64 | // Get the very first item from the $feed. |
| 65 | $block = reset( $feed ); |
| 66 | |
| 67 | // Check if $block is empty. |
| 68 | if ( empty( $block ) ) { |
| 69 | return []; |
| 70 | } |
| 71 | |
| 72 | // Return the notification block. |
| 73 | return $this->prepare_and_sanitize_content( $block ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Save the shown notification block if it's not empty. |
| 78 | * |
| 79 | * @since 1.8.8 |
| 80 | * |
| 81 | * @param array $notification The notification to be saved. |
| 82 | */ |
| 83 | public function maybe_remember_shown_block( array $notification ) { |
| 84 | |
| 85 | // Check if the notification or its ID is empty. |
| 86 | if ( empty( $notification ) || empty( $notification['id'] ) ) { |
| 87 | // If the notification or its ID is empty, return early. |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // Get shown notifications from options. |
| 92 | $shown_notifications = (array) get_option( 'wpforms_email_summaries_shown_notifications', [] ); |
| 93 | |
| 94 | // Add the notification id to the $shown_notifications array. |
| 95 | $shown_notifications[] = (int) $notification['id']; |
| 96 | |
| 97 | // Update the shown notifications in the options. |
| 98 | // Avoid autoloading the option, as it's not needed. |
| 99 | update_option( 'wpforms_email_summaries_shown_notifications', $shown_notifications, false ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Filter the feed to remove shown notifications. |
| 104 | * |
| 105 | * @since 1.8.8 |
| 106 | * |
| 107 | * @param array $feed The feed to filter. |
| 108 | * |
| 109 | * @return array |
| 110 | */ |
| 111 | private function filter_feed( array $feed ): array { |
| 112 | |
| 113 | $shown_notifications = (array) get_option( 'wpforms_email_summaries_shown_notifications', [] ); |
| 114 | |
| 115 | return array_filter( |
| 116 | $feed, |
| 117 | static function ( $item ) use ( $shown_notifications ) { |
| 118 | |
| 119 | return ! in_array( $item['id'], $shown_notifications, true ); |
| 120 | } |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Sort the feed in descending order by start date. |
| 126 | * |
| 127 | * @since 1.8.8 |
| 128 | * |
| 129 | * @param array $feed The feed to sort. |
| 130 | * |
| 131 | * @return array |
| 132 | */ |
| 133 | private function sort_feed( array $feed ): array { |
| 134 | |
| 135 | usort( |
| 136 | $feed, |
| 137 | static function ( $a, $b ) { |
| 138 | |
| 139 | return strtotime( $b['start'] ) - strtotime( $a['start'] ); |
| 140 | } |
| 141 | ); |
| 142 | |
| 143 | return $feed; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Prepare and sanitize content for display. |
| 148 | * |
| 149 | * @since 1.8.8 |
| 150 | * |
| 151 | * @param string|array $content The content to be prepared and sanitized. |
| 152 | * |
| 153 | * @return string|array |
| 154 | */ |
| 155 | private function prepare_and_sanitize_content( $content ) { |
| 156 | |
| 157 | // If the content is empty, return as is. |
| 158 | if ( empty( $content ) ) { |
| 159 | return $content; |
| 160 | } |
| 161 | |
| 162 | // If the content is already a string, sanitize and return it. |
| 163 | if ( is_string( $content ) ) { |
| 164 | // Define allowed HTML tags and attributes. |
| 165 | $content_allowed_tags = $this->notifications->get_allowed_tags(); |
| 166 | |
| 167 | // For design consistency, remove the 'p' tag from the allowed tags. |
| 168 | unset( $content_allowed_tags['p'] ); |
| 169 | |
| 170 | // Apply wp_kses() for sanitization. |
| 171 | return wp_kses( $content, $content_allowed_tags ); |
| 172 | } |
| 173 | |
| 174 | // If the content is an array with the 'content' index, modify and sanitize it. |
| 175 | if ( is_array( $content ) && isset( $content['content'] ) ) { |
| 176 | // Sanitize the content of the array. |
| 177 | $content['content'] = $this->prepare_and_sanitize_content( $content['content'] ); |
| 178 | |
| 179 | // Return the modified array. |
| 180 | return $content; |
| 181 | } |
| 182 | |
| 183 | // If the content is not a string or an array with 'content' index, return the content as is. |
| 184 | return $content; |
| 185 | } |
| 186 | } |
| 187 |