PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.7.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.7.2
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 +45 -269 6.286.7.2 View file →
@@ -9,16 +9,10 @@
9 9 class FrmInbox extends FrmFormApi {
10 10
11 11 protected $cache_key;
12 12
13 - /**
14 - * @var string
15 - */
16 13 private $option = 'frm_inbox';
17 14
18 - /**
19 - * @var array|false
20 - */
21 15 private static $messages = false;
22 16
23 17 /**
24 18 * @var array
@@ -24,9 +18,9 @@
24 18 * @var array
25 19 */
26 20 private static $banner_messages;
27 21
28 - public function __construct() {
22 + public function __construct( $for_parent = null ) {
29 23 $this->set_cache_key();
30 24
31 25 if ( false === self::$messages ) {
32 26 $this->set_messages();
@@ -53,19 +47,15 @@
53 47
54 48 /**
55 49 * @since 4.05
56 50 *
57 - * @param false|string $filter
58 - *
59 - * @return array
51 + * @param false|array $filter
60 52 */
61 53 public function get_messages( $filter = false ) {
62 54 $messages = self::$messages;
63 -
64 55 if ( $filter === 'filter' ) {
65 56 $this->filter_messages( $messages );
66 57 }
67 -
68 58 return $messages;
69 59 }
70 60
71 61 /**
@@ -74,9 +64,8 @@
74 64 * @return void
75 65 */
76 66 public function set_messages() {
77 67 self::$messages = get_option( $this->option );
78 -
79 68 if ( ! is_array( self::$messages ) ) {
80 69 self::$messages = array();
81 70 }
82 71
@@ -94,10 +83,9 @@
94 83 * @return void
95 84 */
96 85 private function add_api_messages() {
97 86 $api = $this->get_api_info();
98 -
99 - if ( ! $api ) {
87 + if ( empty( $api ) ) {
100 88 return;
101 89 }
102 90
103 91 foreach ( $api as $message ) {
@@ -111,10 +99,10 @@
111 99 * @return void
112 100 */
113 101 public function add_message( $message ) {
114 102 if ( ! is_array( $message ) || ! isset( $message['key'] ) ) {
115 - // If the API response is invalid, $message may not be an array.
116 - // 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.
103 + // 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.
117 105 return;
118 106 }
119 107
120 108 if ( isset( self::$messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
@@ -121,9 +109,9 @@
121 109 // Don't replace messages unless required.
122 110 return;
123 111 }
124 112
125 - if ( ! $this->within_valid_timeframe( $message ) ) {
113 + if ( $this->is_expired( $message ) ) {
126 114 return;
127 115 }
128 116
129 117 if ( isset( self::$messages[ $message['key'] ] ) ) {
@@ -167,19 +155,16 @@
167 155 * @return void
168 156 */
169 157 private function clean_messages() {
170 158 $removed = false;
171 -
172 159 foreach ( self::$messages as $t => $message ) {
173 - $read = ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
174 - $dismissed = ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
175 -
176 - if ( ! $read && ! $dismissed && $this->within_valid_timeframe( $message ) ) {
177 - continue;
160 + $read = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
161 + $dismissed = isset( $message['dismissed'] ) && ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' );
162 + $expired = $this->is_expired( $message );
163 + if ( $read || $expired || $dismissed ) {
164 + unset( self::$messages[ $t ] );
165 + $removed = true;
178 166 }
179 -
180 - unset( self::$messages[ $t ] );
181 - $removed = true;
182 167 }
183 168
184 169 if ( $removed ) {
185 170 $this->update_list();
@@ -186,62 +171,30 @@
186 171 }
187 172 }
188 173
189 174 /**
190 - * @param array $messages
191 - * @param string $type
192 - *
193 175 * @return void
194 176 */
195 - public function filter_messages( &$messages, $type = 'unread' ) {
177 + private function filter_messages( &$messages ) {
196 178 $user_id = get_current_user_id();
197 -
198 179 foreach ( $messages as $k => $message ) {
199 180 $dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] );
200 -
201 - if ( ! $k || ! $this->within_valid_timeframe( $message ) || ( $type === 'dismissed' ) !== $dismissed ) {
181 + if ( empty( $k ) || $this->is_expired( $message ) || $dismissed ) {
202 182 unset( $messages[ $k ] );
203 183 } elseif ( ! $this->is_for_user( $message ) ) {
204 184 unset( $messages[ $k ] );
205 185 }
206 186 }
207 -
208 187 $messages = apply_filters( 'frm_filter_inbox', $messages );
209 188 }
210 189
211 190 /**
212 - * Check if a message has started and is not expired.
213 - *
214 - * @since 6.25
215 - *
216 191 * @param array $message
217 192 *
218 193 * @return bool
219 194 */
220 - private function within_valid_timeframe( $message ) {
221 - return $this->has_started( $message ) && ! $this->is_expired( $message );
222 - }
223 -
224 - /**
225 - * Check if a message has actually started, so we can prevent showing something that is queued up early.
226 - *
227 - * @since 6.25
228 - *
229 - * @param array $message
230 - *
231 - * @return bool
232 - */
233 - private function has_started( $message ) {
234 - return empty( $message['starts'] ) ? true : $message['starts'] <= time();
235 - }
236 -
237 - /**
238 - * @param array $message
239 - *
240 - * @return bool
241 - */
242 195 private function is_expired( $message ) {
243 - return ! empty( $message['expires'] ) && $message['expires'] < time();
196 + return isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
244 197 }
245 198
246 199 /**
247 200 * Show different messages for different accounts.
@@ -246,28 +199,27 @@
246 199 /**
247 200 * Show different messages for different accounts.
248 201 *
249 202 * @param array $message
250 - *
251 203 * @return bool
252 204 */
253 205 private function is_for_user( $message ) {
254 - if ( FrmApiHelper::is_for_user( $message ) ) {
206 + if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) {
255 207 return true;
256 208 }
209 + $who = (array) $message['who'];
210 + if ( in_array( 'all', $who, true ) || in_array( 'everyone', $who, true ) ) {
211 + return true;
212 + }
213 + return in_array( $this->get_user_type(), $who, true );
214 + }
257 215
258 - $who = (array) $message['who'];
216 + private function get_user_type() {
217 + if ( ! FrmAppHelper::pro_is_installed() ) {
218 + return 'free';
219 + }
259 220
260 - /**
261 - * Allow for other special inbox cases in other add-ons.
262 - *
263 - * @since 6.8.1
264 - *
265 - * @param bool $is_for_user
266 - * @param array $who
267 - * @param array $message
268 - */
269 - return (bool) apply_filters( 'frm_inbox_message_is_for_user', false, $who, $message );
221 + return FrmAddonsController::license_type();
270 222 }
271 223
272 224 /**
273 225 * @param string $key
@@ -287,23 +239,20 @@
287 239 $this->update_list();
288 240 }
289 241
290 242 /**
243 + * @param string $key
244 + *
291 245 * @since 4.05.02
292 246 *
293 - * @param string $key
294 - *
295 247 * @return void
296 248 */
297 249 public function mark_unread( $key ) {
298 250 $is_read = isset( self::$messages[ $key ] ) && isset( self::$messages[ $key ]['read'] ) && isset( self::$messages[ $key ]['read'][ get_current_user_id() ] );
299 -
300 - if ( ! $is_read ) {
301 - return;
251 + if ( $is_read ) {
252 + unset( self::$messages[ $key ]['read'][ get_current_user_id() ] );
253 + $this->update_list();
302 254 }
303 -
304 - unset( self::$messages[ $key ]['read'][ get_current_user_id() ] );
305 - $this->update_list();
306 255 }
307 256
308 257 /**
309 258 * @param string $key
@@ -334,9 +283,8 @@
334 283 * @return void
335 284 */
336 285 private function dismiss_all() {
337 286 $user_id = get_current_user_id();
338 -
339 287 foreach ( self::$messages as $key => $message ) {
340 288 if ( ! isset( $message['dismissed'] ) ) {
341 289 self::$messages[ $key ]['dismissed'] = array();
342 290 }
@@ -347,50 +295,31 @@
347 295 }
348 296 $this->update_list();
349 297 }
350 298
351 - /**
352 - * @return array
353 - */
354 299 public function unread() {
355 300 $messages = $this->get_messages( 'filter' );
356 301 $user_id = get_current_user_id();
357 -
358 302 foreach ( $messages as $t => $message ) {
359 303 if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
360 304 unset( $messages[ $t ] );
361 305 }
362 306 }
363 -
364 307 return $messages;
365 308 }
366 309
367 - /**
368 - * @since 6.8.4 The $filtered parameter was added.
369 - *
370 - * @param bool $filtered
371 - *
372 - * @return string
373 - */
374 - public function unread_html( $filtered = true ) {
310 + public function unread_html() {
311 + $html = '';
375 312 $count = count( $this->unread() );
313 + if ( $count ) {
314 + $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
376 315
377 - if ( ! $count ) {
378 - return '';
316 + /**
317 + * @since 4.06.01
318 + */
319 + $html = apply_filters( 'frm_inbox_badge', $html );
379 320 }
380 -
381 - $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
382 -
383 - if ( ! $filtered ) {
384 - return $html;
385 - }
386 -
387 - /**
388 - * @since 4.06.01
389 - *
390 - * @param string $html
391 - */
392 - return (string) apply_filters( 'frm_inbox_badge', $html );
321 + return $html;
393 322 }
394 323
395 324 /**
396 325 * @since 4.05.02
@@ -409,9 +338,9 @@
409 338 /**
410 339 * @return void
411 340 */
412 341 private function update_list() {
413 - update_option( $this->option, self::$messages, false );
342 + update_option( $this->option, self::$messages, 'no' );
414 343 }
415 344
416 345 /**
417 346 * Show a banner message if one is available.
@@ -421,186 +350,33 @@
421 350 public static function maybe_show_banner() {
422 351 if ( empty( self::$banner_messages ) ) {
423 352 return false;
424 353 }
425 -
426 354 $message = end( self::$banner_messages );
427 - $cta = self::get_prepared_banner_cta( $message['cta'] );
428 -
429 355 require FrmAppHelper::plugin_path() . '/classes/views/inbox/banner.php';
430 356 return true;
431 357 }
432 358
433 359 /**
434 - * Make sure that the CTA uses utm_medium=banner.
435 - *
436 - * @since 6.8.4
437 - *
438 - * @param string $cta
439 - *
440 - * @return string
441 - */
442 - private static function get_prepared_banner_cta( $cta ) {
443 - $cta = str_replace( 'button-secondary', 'button-primary', $cta );
444 - return preg_replace_callback(
445 - '/href=("|\')(.*?)("|\')/',
446 - /**
447 - * Replace a single href attribute in the CTA.
448 - *
449 - * @param array $matches The regex results for a single match.
450 - *
451 - * @return string
452 - */
453 - function ( $matches ) {
454 - $url = $matches[2];
455 -
456 - if ( '#' === $url ) {
457 - return 'href="#"';
458 - }
459 -
460 - $parts = parse_url( $url );
461 - $query = array();
462 -
463 - if ( isset( $parts['query'] ) ) {
464 - parse_str( $parts['query'], $query );
465 - }
466 -
467 - $query['utm_medium'] = 'banner';
468 - $parts['query'] = http_build_query( $query );
469 - return 'href="' . $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . $parts['query'] . '"';
470 - },
471 - $cta
472 - );
473 - }
474 -
475 - /**
476 360 * @return void
477 361 */
478 362 public static function maybe_disable_screen_options() {
479 363 self::$banner_messages = self::get_banner_messages();
480 -
481 364 if ( self::$banner_messages ) {
482 - // Disable screen options tab when displaying banner messages because it gets in the way of the banner.
365 + // disable screen options tab when displaying banner messages because it gets in the way of the banner.
483 366 add_filter( 'screen_options_show_screen', '__return_false' );
484 367 }
485 368 }
486 369
487 370 /**
488 - * Get all message with a "banner" message key defined.
489 - *
490 371 * @return array
491 372 */
492 373 private static function get_banner_messages() {
493 - return self::get_messages_with_key( 'banner' );
494 - }
495 -
496 - /**
497 - * Get all messages with a "slidein" message key defined.
498 - *
499 - * @since 6.8.4
500 - *
501 - * @return array
502 - */
503 - private static function get_slidein_messages() {
504 - return self::get_messages_with_key( 'slidein' );
505 - }
506 -
507 - /**
508 - * Get all messages with a $key message key defined.
509 - *
510 - * @since 6.8.4
511 - *
512 - * @param string $key The key we are checking for (ie. banner or slidein).
513 - *
514 - * @return array
515 - */
516 - private static function get_messages_with_key( $key ) {
517 374 $inbox = new self();
518 375 return array_filter(
519 376 $inbox->get_messages( 'filter' ),
520 - function ( $message ) use ( $key ) {
521 - return ! empty( $message[ $key ] );
377 + function( $message ) {
378 + return ! empty( $message['banner'] );
522 379 }
523 380 );
524 - }
525 -
526 - /**
527 - * Check if there is at least one slidein message.
528 - *
529 - * @since 6.8.4
530 - *
531 - * @return bool
532 - */
533 - public static function has_a_slidein_message() {
534 - return (bool) self::get_slidein_messages();
535 - }
536 -
537 - /**
538 - * Get the array used for frmGlobal.inboxSlideIn
539 - *
540 - * @since 6.8.4
541 - *
542 - * @return array|false
543 - */
544 - public static function get_inbox_slide_in_value_for_js() {
545 - $messages = self::get_slidein_messages();
546 -
547 - if ( ! $messages ) {
548 - return false;
549 - }
550 -
551 - $message = reset( $messages );
552 -
553 - /**
554 - * Extend the keys in the global JS object.
555 - * This is used in Pro to include images.
556 - *
557 - * @since 6.8.4
558 - *
559 - * @param array $keys
560 - */
561 - $keys_to_return = apply_filters(
562 - 'frm_inbox_slidein_js_vars',
563 - array( 'key', 'slidein', 'subject', 'cta' )
564 - );
565 -
566 - return array_reduce(
567 - $keys_to_return,
568 - function ( $total, $key ) use ( $message ) {
569 - $total[ $key ] = $message[ $key ] ?? '';
570 - return $total;
571 - },
572 - array()
573 - );
574 - }
575 -
576 - /**
577 - * Clear the inbox cache by deleting the associated option from the database.
578 - *
579 - * @since 6.17
580 - *
581 - * @return void
582 - */
583 - public static function clear_cache() {
584 - delete_option( 'frm_inbox' );
585 - }
586 -
587 - /**
588 - * Check inbox for an "error" type. This is displayed on the form list page if one exists.
589 - *
590 - * @since 6.19
591 - *
592 - * @return array|false
593 - */
594 - public static function check_for_error() {
595 - $inbox = new self();
596 - $messages = $inbox->get_messages( 'filter' );
597 -
598 - foreach ( $messages as $message ) {
599 - if ( is_array( $message ) && isset( $message['type'] ) && 'error' === $message['type'] ) {
600 - return $message;
601 - }
602 - }
603 -
604 - return false;
605 381 }
606 382 }