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