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