PluginProbe ʕ •ᴥ•ʔ
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More / 1.10.0.1
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More v1.10.0.1
1.10.1.1 1.10.1 1.10.0.5 trunk 1.1.4 1.1.4.2 1.1.5 1.1.5.1 1.1.6 1.1.6.1 1.1.7 1.1.7.1 1.1.7.2 1.1.8 1.1.8.1 1.1.8.2 1.1.8.3 1.1.8.4 1.10.0.1 1.10.0.2 1.10.0.3 1.10.0.4 1.2.0 1.2.0.1 1.2.1 1.2.2 1.2.2.1 1.2.2.2 1.2.3 1.2.3.1 1.2.3.2 1.2.4 1.2.4.1 1.2.5 1.2.5.1 1.2.6 1.2.7 1.2.8 1.2.8.1 1.2.9 1.3.0 1.3.1 1.3.1.1 1.3.1.2 1.3.2 1.3.3 1.3.5 1.3.6 1.3.6.1 1.3.6.2 1.3.7.2 1.3.7.3 1.3.7.4 1.3.8 1.3.9.1 1.4.0.1 1.4.1.1 1.4.2 1.4.2.1 1.4.2.2 1.4.3 1.4.4 1.4.4.1 1.4.5 1.4.5.1 1.4.5.2 1.4.5.3 1.4.6 1.4.7.1 1.4.7.2 1.4.8.1 1.4.9 1.5.0.1 1.5.0.3 1.5.0.4 1.5.1 1.5.1.1 1.5.1.3 1.5.2.1 1.5.2.2 1.5.2.3 1.5.3 1.5.3.1 1.5.4.1 1.5.4.2 1.5.5 1.5.5.1 1.5.6 1.5.6.2 1.5.7 1.5.8.2 1.5.9.1 1.5.9.4 1.5.9.5 1.6.0.1 1.6.0.2 1.6.1 1.6.2.2 1.6.2.3 1.6.3.1 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.7 1.6.7.1 1.6.7.2 1.6.7.3 1.6.8 1.6.8.1 1.6.9 1.7.0 1.7.1.1 1.7.1.2 1.7.2 1.7.2.1 1.7.3 1.7.4 1.7.4.1 1.7.4.2 1.7.5.1 1.7.5.2 1.7.5.3 1.7.5.5 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.7.9.1 1.8.0.1 1.8.0.2 1.8.1.1 1.8.1.2 1.8.1.3 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.3.1 1.8.4 1.8.4.1 1.8.5.2 1.8.5.3 1.8.5.4 1.8.6.2 1.8.6.3 1.8.6.4 1.8.7.2 1.8.8.2 1.8.8.3 1.8.9.1 1.8.9.2 1.8.9.4 1.8.9.5 1.8.9.6 1.9.0.1 1.9.0.2 1.9.0.3 1.9.0.4 1.9.1.1 1.9.1.2 1.9.1.3 1.9.1.4 1.9.1.5 1.9.1.6 1.9.2.1 1.9.2.2 1.9.2.3 1.9.3.1 1.9.3.2 1.9.4.1 1.9.4.2 1.9.5 1.9.5.1 1.9.5.2 1.9.6 1.9.6.1 1.9.6.2 1.9.7.1 1.9.7.2 1.9.7.3 1.9.8.1 1.9.8.2 1.9.8.4 1.9.8.7 1.9.9.2 1.9.9.3 1.9.9.4
wpforms-lite / src / Emails / NotificationBlocks.php
wpforms-lite / src / Emails Last commit date
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