PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0.07
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0.07
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmInbox.php +68 -308 6.255.0.07 View file →
@@ -11,27 +11,22 @@
11 11 protected $cache_key;
12 12
13 13 private $option = 'frm_inbox';
14 14
15 - private static $messages = false;
15 + private $messages = false;
16 16
17 17 /**
18 - * @var array
18 + * @param array
19 19 */
20 20 private static $banner_messages;
21 21
22 - public function __construct() {
22 + public function __construct( $for_parent = null ) {
23 23 $this->set_cache_key();
24 -
25 - if ( false === self::$messages ) {
26 - $this->set_messages();
27 - }
24 + $this->set_messages();
28 25 }
29 26
30 27 /**
31 28 * @since 4.05
32 - *
33 - * @return void
34 29 */
35 30 protected function set_cache_key() {
36 31 $this->cache_key = 'frm_inbox_cache';
37 32 }
@@ -37,10 +32,8 @@
37 32 }
38 33
39 34 /**
40 35 * @since 4.05
41 - *
42 - * @return string
43 36 */
44 37 protected function api_url() {
45 38 return 'https://formidableforms.com/wp-json/inbox/v1/message/';
46 39 }
@@ -46,13 +39,11 @@
46 39 }
47 40
48 41 /**
49 42 * @since 4.05
50 - *
51 - * @param array|false $filter
52 43 */
53 44 public function get_messages( $filter = false ) {
54 - $messages = self::$messages;
45 + $messages = $this->messages;
55 46 if ( $filter === 'filter' ) {
56 47 $this->filter_messages( $messages );
57 48 }
58 49 return $messages;
@@ -59,15 +50,13 @@
59 50 }
60 51
61 52 /**
62 53 * @since 4.05
63 - *
64 - * @return void
65 54 */
66 55 public function set_messages() {
67 - self::$messages = get_option( $this->option );
68 - if ( ! is_array( self::$messages ) ) {
69 - self::$messages = array();
56 + $this->messages = get_option( $this->option );
57 + if ( empty( $this->messages ) ) {
58 + $this->messages = array();
70 59 }
71 60
72 61 $this->add_api_messages();
73 62
@@ -73,15 +62,13 @@
73 62
74 63 /**
75 64 * Messages are in an array.
76 65 */
77 - self::$messages = apply_filters( 'frm_inbox', self::$messages );
66 + $this->messages = apply_filters( 'frm_inbox', $this->messages );
78 67 }
79 68
80 69 /**
81 70 * @since 4.05
82 - *
83 - * @return void
84 71 */
85 72 private function add_api_messages() {
86 73 $api = $this->get_api_info();
87 74 if ( empty( $api ) ) {
@@ -94,34 +81,31 @@
94 81 }
95 82
96 83 /**
97 84 * @param array|string $message
98 - *
99 - * @return void
100 85 */
101 86 public function add_message( $message ) {
102 - if ( ! is_array( $message ) || ! isset( $message['key'] ) ) {
87 + if ( ! is_array( $message ) ) {
103 88 // if the API response is invalid, $message may not be an array.
104 - // if there are no messages from the API, it is returning a "No Entries Found" item with no key, so check for a key as well.
105 89 return;
106 90 }
107 91
108 - if ( isset( self::$messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
92 + if ( isset( $this->messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
109 93 // Don't replace messages unless required.
110 94 return;
111 95 }
112 96
113 - if ( ! $this->within_valid_timeframe( $message ) ) {
97 + if ( $this->is_expired( $message ) ) {
114 98 return;
115 99 }
116 100
117 - if ( isset( self::$messages[ $message['key'] ] ) ) {
101 + if ( isset( $this->messages[ $message['key'] ] ) ) {
118 102 // Move up and mark as new.
119 - unset( self::$messages[ $message['key'] ] );
103 + unset( $this->messages[ $message['key'] ] );
120 104 }
121 105
122 106 $this->fill_message( $message );
123 - self::$messages[ $message['key'] ] = $message;
107 + $this->messages[ $message['key'] ] = $message;
124 108
125 109 $this->update_list();
126 110
127 111 $this->clean_messages();
@@ -126,13 +110,8 @@
126 110
127 111 $this->clean_messages();
128 112 }
129 113
130 - /**
131 - * @param array $message
132 - *
133 - * @return void
134 - */
135 114 private function fill_message( &$message ) {
136 115 $defaults = array(
137 116 'time' => time(),
138 117 'message' => '',
@@ -139,10 +118,9 @@
139 118 'subject' => '',
140 119 'icon' => 'frm_tooltip_icon',
141 120 'cta' => '',
142 121 'expires' => false,
143 - // Use 'free', 'personal', 'business', 'elite', 'grandfathered'.
144 - 'who' => 'all',
122 + 'who' => 'all', // use 'free', 'personal', 'business', 'elite', 'grandfathered'
145 123 'type' => '',
146 124 );
147 125
148 126 $message = array_merge( $defaults, $message );
@@ -150,19 +128,16 @@
150 128 $message['created'] = $message['time'];
151 129 unset( $message['time'] );
152 130 }
153 131
154 - /**
155 - * @return void
156 - */
157 132 private function clean_messages() {
158 - $removed = false;
159 - foreach ( self::$messages as $t => $message ) {
160 - $read = ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
161 - $dismissed = ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' );
162 -
163 - if ( $read || $dismissed || ! $this->within_valid_timeframe( $message ) ) {
164 - unset( self::$messages[ $t ] );
133 + $removed = false;
134 + foreach ( $this->messages as $t => $message ) {
135 + $read = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
136 + $dismissed = isset( $message['dismissed'] ) && ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' );
137 + $expired = $this->is_expired( $message );
138 + if ( $read || $expired || $dismissed ) {
139 + unset( $this->messages[ $t ] );
165 140 $removed = true;
166 141 }
167 142 }
168 143
@@ -170,18 +145,13 @@
170 145 $this->update_list();
171 146 }
172 147 }
173 148
174 - /**
175 - * @param array $messages
176 - * @param string $type
177 - * @return void
178 - */
179 - public function filter_messages( &$messages, $type = 'unread' ) {
149 + private function filter_messages( &$messages ) {
180 150 $user_id = get_current_user_id();
181 151 foreach ( $messages as $k => $message ) {
182 152 $dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] );
183 - if ( empty( $k ) || ! $this->within_valid_timeframe( $message ) || ( $type === 'dismissed' ) !== $dismissed ) {
153 + if ( empty( $k ) || $this->is_expired( $message ) || $dismissed ) {
184 154 unset( $messages[ $k ] );
185 155 } elseif ( ! $this->is_for_user( $message ) ) {
186 156 unset( $messages[ $k ] );
187 157 }
@@ -188,43 +158,10 @@
188 158 }
189 159 $messages = apply_filters( 'frm_filter_inbox', $messages );
190 160 }
191 161
192 - /**
193 - * Check if a message has started and is not expired.
194 - *
195 - * @since 6.25
196 - *
197 - * @param array $message
198 - * @return bool
199 - */
200 - private function within_valid_timeframe( $message ) {
201 - return $this->has_started( $message ) && ! $this->is_expired( $message );
202 - }
203 -
204 - /**
205 - * Check if a message has actually started, so we can prevent showing something that is queued up early.
206 - *
207 - * @since 6.25
208 - *
209 - * @param array $message
210 - * @return bool
211 - */
212 - private function has_started( $message ) {
213 - if ( empty( $message['starts'] ) ) {
214 - return true;
215 - }
216 -
217 - return $message['starts'] <= time();
218 - }
219 -
220 - /**
221 - * @param array $message
222 - *
223 - * @return bool
224 - */
225 162 private function is_expired( $message ) {
226 - return ! empty( $message['expires'] ) && $message['expires'] < time();
163 + return isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
227 164 }
228 165
229 166 /**
230 167 * Show different messages for different accounts.
@@ -232,54 +169,51 @@
232 169 * @param array $message
233 170 * @return bool
234 171 */
235 172 private function is_for_user( $message ) {
236 - if ( FrmApiHelper::is_for_user( $message ) ) {
173 + if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) {
237 174 return true;
238 175 }
176 + $who = (array) $message['who'];
177 + if ( in_array( 'all', $who, true ) || in_array( 'everyone', $who, true ) ) {
178 + return true;
179 + }
180 + return in_array( $this->get_user_type(), $who, true );
181 + }
239 182
240 - $who = (array) $message['who'];
183 + private function get_user_type() {
184 + if ( ! FrmAppHelper::pro_is_installed() ) {
185 + return 'free';
186 + }
241 187
242 - /**
243 - * Allow for other special inbox cases in other add-ons.
244 - *
245 - * @since 6.8.1
246 - *
247 - * @param bool $is_for_user
248 - * @param array $who
249 - * @param array $message
250 - */
251 - return (bool) apply_filters( 'frm_inbox_message_is_for_user', false, $who, $message );
188 + return FrmAddonsController::license_type();
252 189 }
253 190
254 191 /**
255 192 * @param string $key
256 - *
257 - * @return void
258 193 */
259 194 public function mark_read( $key ) {
260 - if ( ! $key || ! isset( self::$messages[ $key ] ) ) {
195 + if ( ! $key || ! isset( $this->messages[ $key ] ) ) {
261 196 return;
262 197 }
263 198
264 - if ( ! isset( self::$messages[ $key ]['read'] ) ) {
265 - self::$messages[ $key ]['read'] = array();
199 + if ( ! isset( $this->messages[ $key ]['read'] ) ) {
200 + $this->messages[ $key ]['read'] = array();
266 201 }
267 - self::$messages[ $key ]['read'][ get_current_user_id() ] = time();
202 + $this->messages[ $key ]['read'][ get_current_user_id() ] = time();
268 203
269 204 $this->update_list();
270 205 }
271 206
272 207 /**
208 + * @param string $key
209 + *
273 210 * @since 4.05.02
274 - *
275 - * @param string $key
276 - * @return void
277 211 */
278 212 public function mark_unread( $key ) {
279 - $is_read = isset( self::$messages[ $key ] ) && isset( self::$messages[ $key ]['read'] ) && isset( self::$messages[ $key ]['read'][ get_current_user_id() ] );
213 + $is_read = isset( $this->messages[ $key ] ) && isset( $this->messages[ $key ]['read'] ) && isset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
280 214 if ( $is_read ) {
281 - unset( self::$messages[ $key ]['read'][ get_current_user_id() ] );
215 + unset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
282 216 $this->update_list();
283 217 }
284 218 }
285 219
@@ -284,10 +218,8 @@
284 218 }
285 219
286 220 /**
287 221 * @param string $key
288 - *
289 - * @return void
290 222 */
291 223 public function dismiss( $key ) {
292 224 if ( $key === 'all' ) {
293 225 $this->dismiss_all();
@@ -293,16 +225,16 @@
293 225 $this->dismiss_all();
294 226 return;
295 227 }
296 228
297 - if ( ! isset( self::$messages[ $key ] ) ) {
229 + if ( ! isset( $this->messages[ $key ] ) ) {
298 230 return;
299 231 }
300 232
301 - if ( ! isset( self::$messages[ $key ]['dismissed'] ) ) {
302 - self::$messages[ $key ]['dismissed'] = array();
233 + if ( ! isset( $this->messages[ $key ]['dismissed'] ) ) {
234 + $this->messages[ $key ]['dismissed'] = array();
303 235 }
304 - self::$messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
236 + $this->messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
305 237
306 238 $this->update_list();
307 239 }
308 240
@@ -307,20 +239,18 @@
307 239 }
308 240
309 241 /**
310 242 * @since 4.06
311 - *
312 - * @return void
313 243 */
314 244 private function dismiss_all() {
315 245 $user_id = get_current_user_id();
316 - foreach ( self::$messages as $key => $message ) {
246 + foreach ( $this->messages as $key => $message ) {
317 247 if ( ! isset( $message['dismissed'] ) ) {
318 - self::$messages[ $key ]['dismissed'] = array();
248 + $this->messages[ $key ]['dismissed'] = array();
319 249 }
320 250
321 251 if ( ! isset( $message['dismissed'][ $user_id ] ) ) {
322 - self::$messages[ $key ]['dismissed'][ $user_id ] = time();
252 + $this->messages[ $key ]['dismissed'][ $user_id ] = time();
323 253 }
324 254 }
325 255 $this->update_list();
326 256 }
@@ -335,111 +265,44 @@
335 265 }
336 266 return $messages;
337 267 }
338 268
339 - /**
340 - * @since 6.8.4 The $filtered parameter was added.
341 - *
342 - * @param bool $filtered
343 - * @return string
344 - */
345 - public function unread_html( $filtered = true ) {
269 + public function unread_html() {
270 + $html = '';
346 271 $count = count( $this->unread() );
347 - if ( ! $count ) {
348 - return '';
349 - }
272 + if ( $count ) {
273 + $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
350 274
351 - $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
352 -
353 - if ( ! $filtered ) {
354 - return $html;
275 + /**
276 + * @since 4.06.01
277 + */
278 + $html = apply_filters( 'frm_inbox_badge', $html );
355 279 }
356 -
357 - /**
358 - * @since 4.06.01
359 - *
360 - * @param string $html
361 - */
362 - return (string) apply_filters( 'frm_inbox_badge', $html );
280 + return $html;
363 281 }
364 282
365 283 /**
366 284 * @since 4.05.02
367 - *
368 - * @param string $key
369 - * @return void
370 285 */
371 286 public function remove( $key ) {
372 - if ( isset( self::$messages[ $key ] ) ) {
373 - unset( self::$messages[ $key ] );
287 + if ( isset( $this->messages[ $key ] ) ) {
288 + unset( $this->messages[ $key ] );
374 289 $this->update_list();
375 290 }
376 291 }
377 292
378 - /**
379 - * @return void
380 - */
381 293 private function update_list() {
382 - update_option( $this->option, self::$messages, 'no' );
294 + update_option( $this->option, $this->messages, 'no' );
383 295 }
384 296
385 - /**
386 - * Show a banner message if one is available.
387 - *
388 - * @return bool True if a banner is available and shown.
389 - */
390 297 public static function maybe_show_banner() {
391 298 if ( empty( self::$banner_messages ) ) {
392 - return false;
299 + return;
393 300 }
394 301 $message = end( self::$banner_messages );
395 - $cta = self::get_prepared_banner_cta( $message['cta'] );
396 -
397 302 require FrmAppHelper::plugin_path() . '/classes/views/inbox/banner.php';
398 - return true;
399 303 }
400 304
401 - /**
402 - * Make sure that the CTA uses utm_medium=banner.
403 - *
404 - * @since 6.8.4
405 - *
406 - * @param string $cta
407 - * @return string
408 - */
409 - private static function get_prepared_banner_cta( $cta ) {
410 - $cta = str_replace( 'button-secondary', 'button-primary', $cta );
411 - return preg_replace_callback(
412 - '/href=("|\')(.*?)("|\')/',
413 - /**
414 - * Replace a single href attribute in the CTA.
415 - *
416 - * @param array $matches The regex results for a single match.
417 - * @return string
418 - */
419 - function ( $matches ) {
420 - $url = $matches[2];
421 - $parts = parse_url( $url );
422 -
423 - if ( '#' === $url ) {
424 - return 'href="#"';
425 - }
426 -
427 - $query = array();
428 - if ( isset( $parts['query'] ) ) {
429 - parse_str( $parts['query'], $query );
430 - }
431 - $query['utm_medium'] = 'banner';
432 - $parts['query'] = http_build_query( $query );
433 - return 'href="' . $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . $parts['query'] . '"';
434 - },
435 - $cta
436 - );
437 - }
438 -
439 - /**
440 - * @return void
441 - */
442 305 public static function maybe_disable_screen_options() {
443 306 self::$banner_messages = self::get_banner_messages();
444 307 if ( self::$banner_messages ) {
445 308 // disable screen options tab when displaying banner messages because it gets in the way of the banner.
@@ -447,119 +310,16 @@
447 310 }
448 311 }
449 312
450 313 /**
451 - * Get all message with a "banner" message key defined.
452 - *
453 314 * @return array
454 315 */
455 316 private static function get_banner_messages() {
456 - return self::get_messages_with_key( 'banner' );
457 - }
458 -
459 - /**
460 - * Get all messages with a "slidein" message key defined.
461 - *
462 - * @since 6.8.4
463 - *
464 - * @return array
465 - */
466 - private static function get_slidein_messages() {
467 - return self::get_messages_with_key( 'slidein' );
468 - }
469 -
470 - /**
471 - * Get all messages with a $key message key defined.
472 - *
473 - * @since 6.8.4
474 - *
475 - * @param string $key The key we are checking for (ie. banner or slidein).
476 - * @return array
477 - */
478 - private static function get_messages_with_key( $key ) {
479 317 $inbox = new self();
480 318 return array_filter(
481 319 $inbox->get_messages( 'filter' ),
482 - function ( $message ) use ( $key ) {
483 - return ! empty( $message[ $key ] );
320 + function( $message ) {
321 + return ! empty( $message['banner'] );
484 322 }
485 323 );
486 - }
487 -
488 - /**
489 - * Check if there is at least one slidein message.
490 - *
491 - * @since 6.8.4
492 - *
493 - * @return bool
494 - */
495 - public static function has_a_slidein_message() {
496 - return (bool) self::get_slidein_messages();
497 - }
498 -
499 - /**
500 - * Get the array used for frmGlobal.inboxSlideIn
501 - *
502 - * @since 6.8.4
503 - *
504 - * @return array|false
505 - */
506 - public static function get_inbox_slide_in_value_for_js() {
507 - $messages = self::get_slidein_messages();
508 - if ( ! $messages ) {
509 - return false;
510 - }
511 -
512 - $message = reset( $messages );
513 -
514 - /**
515 - * Extend the keys in the global JS object.
516 - * This is used in Pro to include images.
517 - *
518 - * @since 6.8.4
519 - *
520 - * @param array $keys
521 - */
522 - $keys_to_return = apply_filters(
523 - 'frm_inbox_slidein_js_vars',
524 - array( 'key', 'slidein', 'subject', 'cta' )
525 - );
526 -
527 - return array_reduce(
528 - $keys_to_return,
529 - function ( $total, $key ) use ( $message ) {
530 - $total[ $key ] = $message[ $key ] ?? '';
531 - return $total;
532 - },
533 - array()
534 - );
535 - }
536 -
537 - /**
538 - * Clear the inbox cache by deleting the associated option from the database.
539 - *
540 - * @since 6.17
541 - *
542 - * @return void
543 - */
544 - public static function clear_cache() {
545 - delete_option( 'frm_inbox' );
546 - }
547 -
548 - /**
549 - * Check inbox for an "error" type. This is displayed on the form list page if one exists.
550 - *
551 - * @since 6.19
552 - *
553 - * @return array|false
554 - */
555 - public static function check_for_error() {
556 - $inbox = new self();
557 - $messages = $inbox->get_messages( 'filter' );
558 - foreach ( $messages as $message ) {
559 - if ( is_array( $message ) && isset( $message['type'] ) && 'error' === $message['type'] ) {
560 - return $message;
561 - }
562 - }
563 - return false;
564 324 }
565 325 }