PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0
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 +60 -377 6.265.0 View file →
@@ -9,35 +9,19 @@
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 - /**
24 - * @var array
25 - */
26 - private static $banner_messages;
27 -
28 - public function __construct() {
17 + public function __construct( $for_parent = null ) {
29 18 $this->set_cache_key();
30 -
31 - if ( false === self::$messages ) {
32 - $this->set_messages();
33 - }
19 + $this->set_messages();
34 20 }
35 21
36 22 /**
37 23 * @since 4.05
38 - *
39 - * @return void
40 24 */
41 25 protected function set_cache_key() {
42 26 $this->cache_key = 'frm_inbox_cache';
43 27 }
@@ -43,10 +27,8 @@
43 27 }
44 28
45 29 /**
46 30 * @since 4.05
47 - *
48 - * @return string
49 31 */
50 32 protected function api_url() {
51 33 return 'https://formidableforms.com/wp-json/inbox/v1/message/';
52 34 }
@@ -52,16 +34,11 @@
52 34 }
53 35
54 36 /**
55 37 * @since 4.05
56 - *
57 - * @param array|false $filter
58 - *
59 - * @return array
60 38 */
61 39 public function get_messages( $filter = false ) {
62 - $messages = self::$messages;
63 -
40 + $messages = $this->messages;
64 41 if ( $filter === 'filter' ) {
65 42 $this->filter_messages( $messages );
66 43 }
67 44 return $messages;
@@ -68,16 +45,13 @@
68 45 }
69 46
70 47 /**
71 48 * @since 4.05
72 - *
73 - * @return void
74 49 */
75 50 public function set_messages() {
76 - self::$messages = get_option( $this->option );
77 -
78 - if ( ! is_array( self::$messages ) ) {
79 - self::$messages = array();
51 + $this->messages = get_option( $this->option );
52 + if ( empty( $this->messages ) ) {
53 + $this->messages = array();
80 54 }
81 55
82 56 $this->add_api_messages();
83 57
@@ -83,19 +57,16 @@
83 57
84 58 /**
85 59 * Messages are in an array.
86 60 */
87 - self::$messages = apply_filters( 'frm_inbox', self::$messages );
61 + $this->messages = apply_filters( 'frm_inbox', $this->messages );
88 62 }
89 63
90 64 /**
91 65 * @since 4.05
92 - *
93 - * @return void
94 66 */
95 67 private function add_api_messages() {
96 68 $api = $this->get_api_info();
97 -
98 69 if ( empty( $api ) ) {
99 70 return;
100 71 }
101 72
@@ -105,34 +76,31 @@
105 76 }
106 77
107 78 /**
108 79 * @param array|string $message
109 - *
110 - * @return void
111 80 */
112 81 public function add_message( $message ) {
113 - if ( ! is_array( $message ) || ! isset( $message['key'] ) ) {
82 + if ( ! is_array( $message ) ) {
114 83 // if the API response is invalid, $message may not be an array.
115 - // 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.
116 84 return;
117 85 }
118 86
119 - if ( isset( self::$messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
87 + if ( isset( $this->messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
120 88 // Don't replace messages unless required.
121 89 return;
122 90 }
123 91
124 - if ( ! $this->within_valid_timeframe( $message ) ) {
92 + if ( $this->is_expired( $message ) ) {
125 93 return;
126 94 }
127 95
128 - if ( isset( self::$messages[ $message['key'] ] ) ) {
96 + if ( isset( $this->messages[ $message['key'] ] ) ) {
129 97 // Move up and mark as new.
130 - unset( self::$messages[ $message['key'] ] );
98 + unset( $this->messages[ $message['key'] ] );
131 99 }
132 100
133 101 $this->fill_message( $message );
134 - self::$messages[ $message['key'] ] = $message;
102 + $this->messages[ $message['key'] ] = $message;
135 103
136 104 $this->update_list();
137 105
138 106 $this->clean_messages();
@@ -137,13 +105,8 @@
137 105
138 106 $this->clean_messages();
139 107 }
140 108
141 - /**
142 - * @param array $message
143 - *
144 - * @return void
145 - */
146 109 private function fill_message( &$message ) {
147 110 $defaults = array(
148 111 'time' => time(),
149 112 'message' => '',
@@ -150,10 +113,9 @@
150 113 'subject' => '',
151 114 'icon' => 'frm_tooltip_icon',
152 115 'cta' => '',
153 116 'expires' => false,
154 - // Use 'free', 'personal', 'business', 'elite', 'grandfathered'.
155 - 'who' => 'all',
117 + 'who' => 'all', // use 'free', 'personal', 'business', 'elite', 'grandfathered'
156 118 'type' => '',
157 119 );
158 120
159 121 $message = array_merge( $defaults, $message );
@@ -161,20 +123,16 @@
161 123 $message['created'] = $message['time'];
162 124 unset( $message['time'] );
163 125 }
164 126
165 - /**
166 - * @return void
167 - */
168 127 private function clean_messages() {
169 - $removed = false;
170 -
171 - 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 ) ) {
176 - unset( self::$messages[ $t ] );
128 + $removed = false;
129 + foreach ( $this->messages as $t => $message ) {
130 + $read = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
131 + $dismissed = isset( $message['dismissed'] ) && ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' );
132 + $expired = $this->is_expired( $message );
133 + if ( $read || $expired || $dismissed ) {
134 + unset( $this->messages[ $t ] );
177 135 $removed = true;
178 136 }
179 137 }
180 138
@@ -182,125 +140,69 @@
182 140 $this->update_list();
183 141 }
184 142 }
185 143
186 - /**
187 - * @param array $messages
188 - * @param string $type
189 - *
190 - * @return void
191 - */
192 - public function filter_messages( &$messages, $type = 'unread' ) {
144 + private function filter_messages( &$messages ) {
193 145 $user_id = get_current_user_id();
194 -
195 146 foreach ( $messages as $k => $message ) {
196 147 $dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] );
197 -
198 - if ( empty( $k ) || ! $this->within_valid_timeframe( $message ) || ( $type === 'dismissed' ) !== $dismissed ) {
148 + if ( empty( $k ) || $this->is_expired( $message ) || $dismissed ) {
199 149 unset( $messages[ $k ] );
200 150 } elseif ( ! $this->is_for_user( $message ) ) {
201 151 unset( $messages[ $k ] );
202 152 }
203 153 }
204 -
205 154 $messages = apply_filters( 'frm_filter_inbox', $messages );
206 155 }
207 156
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 );
157 + private function is_expired( $message ) {
158 + return isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
219 159 }
220 160
221 161 /**
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
162 + * Show different messages for different accounts.
229 163 */
230 - private function has_started( $message ) {
231 - if ( empty( $message['starts'] ) ) {
164 + private function is_for_user( $message ) {
165 + if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) {
232 166 return true;
233 167 }
234 168
235 - return $message['starts'] <= time();
169 + return in_array( $this->get_user_type(), (array) $message['who'] );
236 170 }
237 171
238 - /**
239 - * @param array $message
240 - *
241 - * @return bool
242 - */
243 - private function is_expired( $message ) {
244 - return ! empty( $message['expires'] ) && $message['expires'] < time();
245 - }
246 -
247 - /**
248 - * Show different messages for different accounts.
249 - *
250 - * @param array $message
251 - *
252 - * @return bool
253 - */
254 - private function is_for_user( $message ) {
255 - if ( FrmApiHelper::is_for_user( $message ) ) {
256 - return true;
172 + private function get_user_type() {
173 + if ( ! FrmAppHelper::pro_is_installed() ) {
174 + return 'free';
257 175 }
258 176
259 - $who = (array) $message['who'];
260 -
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 );
177 + return FrmAddonsController::license_type();
271 178 }
272 179
273 180 /**
274 181 * @param string $key
275 - *
276 - * @return void
277 182 */
278 183 public function mark_read( $key ) {
279 - if ( ! $key || ! isset( self::$messages[ $key ] ) ) {
184 + if ( ! $key || ! isset( $this->messages[ $key ] ) ) {
280 185 return;
281 186 }
282 187
283 - if ( ! isset( self::$messages[ $key ]['read'] ) ) {
284 - self::$messages[ $key ]['read'] = array();
188 + if ( ! isset( $this->messages[ $key ]['read'] ) ) {
189 + $this->messages[ $key ]['read'] = array();
285 190 }
286 - self::$messages[ $key ]['read'][ get_current_user_id() ] = time();
191 + $this->messages[ $key ]['read'][ get_current_user_id() ] = time();
287 192
288 193 $this->update_list();
289 194 }
290 195
291 196 /**
292 - * @since 4.05.02
293 - *
294 197 * @param string $key
295 198 *
296 - * @return void
199 + * @since 4.05.02
297 200 */
298 201 public function mark_unread( $key ) {
299 - $is_read = isset( self::$messages[ $key ] ) && isset( self::$messages[ $key ]['read'] ) && isset( self::$messages[ $key ]['read'][ get_current_user_id() ] );
300 -
202 + $is_read = isset( $this->messages[ $key ] ) && isset( $this->messages[ $key ]['read'] ) && isset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
301 203 if ( $is_read ) {
302 - unset( self::$messages[ $key ]['read'][ get_current_user_id() ] );
204 + unset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
303 205 $this->update_list();
304 206 }
305 207 }
306 208
@@ -305,10 +207,8 @@
305 207 }
306 208
307 209 /**
308 210 * @param string $key
309 - *
310 - * @return void
311 211 */
312 212 public function dismiss( $key ) {
313 213 if ( $key === 'all' ) {
314 214 $this->dismiss_all();
@@ -314,16 +214,16 @@
314 214 $this->dismiss_all();
315 215 return;
316 216 }
317 217
318 - if ( ! isset( self::$messages[ $key ] ) ) {
218 + if ( ! isset( $this->messages[ $key ] ) ) {
319 219 return;
320 220 }
321 221
322 - if ( ! isset( self::$messages[ $key ]['dismissed'] ) ) {
323 - self::$messages[ $key ]['dismissed'] = array();
222 + if ( ! isset( $this->messages[ $key ]['dismissed'] ) ) {
223 + $this->messages[ $key ]['dismissed'] = array();
324 224 }
325 - self::$messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
225 + $this->messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
326 226
327 227 $this->update_list();
328 228 }
329 229
@@ -328,33 +228,26 @@
328 228 }
329 229
330 230 /**
331 231 * @since 4.06
332 - *
333 - * @return void
334 232 */
335 233 private function dismiss_all() {
336 234 $user_id = get_current_user_id();
337 -
338 - foreach ( self::$messages as $key => $message ) {
235 + foreach ( $this->messages as $key => $message ) {
339 236 if ( ! isset( $message['dismissed'] ) ) {
340 - self::$messages[ $key ]['dismissed'] = array();
237 + $this->messages[ $key ]['dismissed'] = array();
341 238 }
342 239
343 240 if ( ! isset( $message['dismissed'][ $user_id ] ) ) {
344 - self::$messages[ $key ]['dismissed'][ $user_id ] = time();
241 + $this->messages[ $key ]['dismissed'][ $user_id ] = time();
345 242 }
346 243 }
347 244 $this->update_list();
348 245 }
349 246
350 - /**
351 - * @return array
352 - */
353 247 public function unread() {
354 248 $messages = $this->get_messages( 'filter' );
355 249 $user_id = get_current_user_id();
356 -
357 250 foreach ( $messages as $t => $message ) {
358 251 if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
359 252 unset( $messages[ $t ] );
360 253 }
@@ -361,242 +254,32 @@
361 254 }
362 255 return $messages;
363 256 }
364 257
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 ) {
258 + public function unread_html() {
259 + $html = '';
373 260 $count = count( $this->unread() );
261 + if ( $count ) {
262 + $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
374 263
375 - if ( ! $count ) {
376 - return '';
264 + /**
265 + * @since 4.06.01
266 + */
267 + $html = apply_filters( 'frm_inbox_badge', $html );
377 268 }
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 );
269 + return $html;
391 270 }
392 271
393 272 /**
394 273 * @since 4.05.02
395 - *
396 - * @param string $key
397 - *
398 - * @return void
399 274 */
400 275 public function remove( $key ) {
401 - if ( isset( self::$messages[ $key ] ) ) {
402 - unset( self::$messages[ $key ] );
276 + if ( isset( $this->messages[ $key ] ) ) {
277 + unset( $this->messages[ $key ] );
403 278 $this->update_list();
404 279 }
405 280 }
406 281
407 - /**
408 - * @return void
409 - */
410 282 private function update_list() {
411 - update_option( $this->option, self::$messages, 'no' );
412 - }
413 -
414 - /**
415 - * Show a banner message if one is available.
416 - *
417 - * @return bool True if a banner is available and shown.
418 - */
419 - public static function maybe_show_banner() {
420 - if ( empty( self::$banner_messages ) ) {
421 - return false;
422 - }
423 -
424 - $message = end( self::$banner_messages );
425 - $cta = self::get_prepared_banner_cta( $message['cta'] );
426 -
427 - require FrmAppHelper::plugin_path() . '/classes/views/inbox/banner.php';
428 - return true;
429 - }
430 -
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 - public static function maybe_disable_screen_options() {
476 - self::$banner_messages = self::get_banner_messages();
477 -
478 - if ( self::$banner_messages ) {
479 - // disable screen options tab when displaying banner messages because it gets in the way of the banner.
480 - add_filter( 'screen_options_show_screen', '__return_false' );
481 - }
482 - }
483 -
484 - /**
485 - * Get all message with a "banner" message key defined.
486 - *
487 - * @return array
488 - */
489 - 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 - $inbox = new self();
515 - return array_filter(
516 - $inbox->get_messages( 'filter' ),
517 - function ( $message ) use ( $key ) {
518 - return ! empty( $message[ $key ] );
519 - }
520 - );
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;
283 + update_option( $this->option, $this->messages, 'no' );
601 284 }
602 285 }