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