PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.07.01
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.07.01
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 +59 -385 6.26.14.07.01 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
@@ -104,35 +75,23 @@
104 75 }
105 76 }
106 77
107 78 /**
108 - * @param array|string $message
109 - *
110 - * @return void
79 + * @param array $message
111 80 */
112 81 public function add_message( $message ) {
113 - if ( ! is_array( $message ) || ! isset( $message['key'] ) ) {
114 - // 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 - return;
117 - }
118 -
119 - if ( isset( self::$messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
82 + if ( isset( $this->messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
120 83 // Don't replace messages unless required.
121 84 return;
122 85 }
123 86
124 - if ( ! $this->within_valid_timeframe( $message ) ) {
125 - return;
126 - }
127 -
128 - if ( isset( self::$messages[ $message['key'] ] ) ) {
87 + if ( isset( $this->messages[ $message['key'] ] ) ) {
129 88 // Move up and mark as new.
130 - unset( self::$messages[ $message['key'] ] );
89 + unset( $this->messages[ $message['key'] ] );
131 90 }
132 91
133 92 $this->fill_message( $message );
134 - self::$messages[ $message['key'] ] = $message;
93 + $this->messages[ $message['key'] ] = $message;
135 94
136 95 $this->update_list();
137 96
138 97 $this->clean_messages();
@@ -137,13 +96,8 @@
137 96
138 97 $this->clean_messages();
139 98 }
140 99
141 - /**
142 - * @param array $message
143 - *
144 - * @return void
145 - */
146 100 private function fill_message( &$message ) {
147 101 $defaults = array(
148 102 'time' => time(),
149 103 'message' => '',
@@ -150,10 +104,9 @@
150 104 'subject' => '',
151 105 'icon' => 'frm_tooltip_icon',
152 106 'cta' => '',
153 107 'expires' => false,
154 - // Use 'free', 'personal', 'business', 'elite', 'grandfathered'.
155 - 'who' => 'all',
108 + 'who' => 'all', // use 'free', 'personal', 'business', 'elite', 'grandfathered'
156 109 'type' => '',
157 110 );
158 111
159 112 $message = array_merge( $defaults, $message );
@@ -161,20 +114,16 @@
161 114 $message['created'] = $message['time'];
162 115 unset( $message['time'] );
163 116 }
164 117
165 - /**
166 - * @return void
167 - */
168 118 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 ] );
119 + $removed = false;
120 + foreach ( $this->messages as $t => $message ) {
121 + $read = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
122 + $dismissed = isset( $message['dismissed'] ) && ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' );
123 + $expired = $this->is_expired( $message );
124 + if ( $read || $expired || $dismissed ) {
125 + unset( $this->messages[ $t ] );
177 126 $removed = true;
178 127 }
179 128 }
180 129
@@ -182,125 +131,69 @@
182 131 $this->update_list();
183 132 }
184 133 }
185 134
186 - /**
187 - * @param array $messages
188 - * @param string $type
189 - *
190 - * @return void
191 - */
192 - public function filter_messages( &$messages, $type = 'unread' ) {
135 + private function filter_messages( &$messages ) {
193 136 $user_id = get_current_user_id();
194 -
195 137 foreach ( $messages as $k => $message ) {
196 138 $dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] );
197 -
198 - if ( empty( $k ) || ! $this->within_valid_timeframe( $message ) || ( $type === 'dismissed' ) !== $dismissed ) {
139 + if ( $this->is_expired( $message ) || $dismissed ) {
199 140 unset( $messages[ $k ] );
200 141 } elseif ( ! $this->is_for_user( $message ) ) {
201 142 unset( $messages[ $k ] );
202 143 }
203 144 }
204 -
205 145 $messages = apply_filters( 'frm_filter_inbox', $messages );
206 146 }
207 147
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 );
148 + private function is_expired( $message ) {
149 + return isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
219 150 }
220 151
221 152 /**
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
153 + * Show different messages for different accounts.
229 154 */
230 - private function has_started( $message ) {
231 - if ( empty( $message['starts'] ) ) {
155 + private function is_for_user( $message ) {
156 + if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) {
232 157 return true;
233 158 }
234 159
235 - return $message['starts'] <= time();
160 + return in_array( $this->get_user_type(), (array) $message['who'] );
236 161 }
237 162
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;
163 + private function get_user_type() {
164 + if ( ! FrmAppHelper::pro_is_installed() ) {
165 + return 'free';
257 166 }
258 167
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 );
168 + return FrmAddonsController::license_type();
271 169 }
272 170
273 171 /**
274 172 * @param string $key
275 - *
276 - * @return void
277 173 */
278 174 public function mark_read( $key ) {
279 - if ( ! $key || ! isset( self::$messages[ $key ] ) ) {
175 + if ( ! isset( $this->messages[ $key ] ) ) {
280 176 return;
281 177 }
282 178
283 - if ( ! isset( self::$messages[ $key ]['read'] ) ) {
284 - self::$messages[ $key ]['read'] = array();
179 + if ( ! isset( $this->messages[ $key ]['read'] ) ) {
180 + $this->messages[ $key ]['read'] = array();
285 181 }
286 - self::$messages[ $key ]['read'][ get_current_user_id() ] = time();
182 + $this->messages[ $key ]['read'][ get_current_user_id() ] = time();
287 183
288 184 $this->update_list();
289 185 }
290 186
291 187 /**
292 - * @since 4.05.02
293 - *
294 188 * @param string $key
295 189 *
296 - * @return void
190 + * @since 4.05.02
297 191 */
298 192 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 -
193 + $is_read = isset( $this->messages[ $key ] ) && isset( $this->messages[ $key ]['read'] ) && isset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
301 194 if ( $is_read ) {
302 - unset( self::$messages[ $key ]['read'][ get_current_user_id() ] );
195 + unset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
303 196 $this->update_list();
304 197 }
305 198 }
306 199
@@ -305,10 +198,8 @@
305 198 }
306 199
307 200 /**
308 201 * @param string $key
309 - *
310 - * @return void
311 202 */
312 203 public function dismiss( $key ) {
313 204 if ( $key === 'all' ) {
314 205 $this->dismiss_all();
@@ -314,16 +205,16 @@
314 205 $this->dismiss_all();
315 206 return;
316 207 }
317 208
318 - if ( ! isset( self::$messages[ $key ] ) ) {
209 + if ( ! isset( $this->messages[ $key ] ) ) {
319 210 return;
320 211 }
321 212
322 - if ( ! isset( self::$messages[ $key ]['dismissed'] ) ) {
323 - self::$messages[ $key ]['dismissed'] = array();
213 + if ( ! isset( $this->messages[ $key ]['dismissed'] ) ) {
214 + $this->messages[ $key ]['dismissed'] = array();
324 215 }
325 - self::$messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
216 + $this->messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
326 217
327 218 $this->update_list();
328 219 }
329 220
@@ -328,33 +219,26 @@
328 219 }
329 220
330 221 /**
331 222 * @since 4.06
332 - *
333 - * @return void
334 223 */
335 224 private function dismiss_all() {
336 225 $user_id = get_current_user_id();
337 -
338 - foreach ( self::$messages as $key => $message ) {
226 + foreach ( $this->messages as $key => $message ) {
339 227 if ( ! isset( $message['dismissed'] ) ) {
340 - self::$messages[ $key ]['dismissed'] = array();
228 + $this->messages[ $key ]['dismissed'] = array();
341 229 }
342 230
343 231 if ( ! isset( $message['dismissed'][ $user_id ] ) ) {
344 - self::$messages[ $key ]['dismissed'][ $user_id ] = time();
232 + $this->messages[ $key ]['dismissed'][ $user_id ] = time();
345 233 }
346 234 }
347 235 $this->update_list();
348 236 }
349 237
350 - /**
351 - * @return array
352 - */
353 238 public function unread() {
354 239 $messages = $this->get_messages( 'filter' );
355 240 $user_id = get_current_user_id();
356 -
357 241 foreach ( $messages as $t => $message ) {
358 242 if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
359 243 unset( $messages[ $t ] );
360 244 }
@@ -361,242 +245,32 @@
361 245 }
362 246 return $messages;
363 247 }
364 248
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 ) {
249 + public function unread_html() {
250 + $html = '';
373 251 $count = count( $this->unread() );
252 + if ( $count ) {
253 + $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
374 254
375 - if ( ! $count ) {
376 - return '';
255 + /**
256 + * @since 4.06.01
257 + */
258 + $html = apply_filters( 'frm_inbox_badge', $html );
377 259 }
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 );
260 + return $html;
391 261 }
392 262
393 263 /**
394 264 * @since 4.05.02
395 - *
396 - * @param string $key
397 - *
398 - * @return void
399 265 */
400 266 public function remove( $key ) {
401 - if ( isset( self::$messages[ $key ] ) ) {
402 - unset( self::$messages[ $key ] );
267 + if ( isset( $this->messages[ $key ] ) ) {
268 + unset( $this->messages[ $key ] );
403 269 $this->update_list();
404 270 }
405 271 }
406 272
407 - /**
408 - * @return void
409 - */
410 273 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;
274 + update_option( $this->option, $this->messages, 'no' );
601 275 }
602 276 }