| @@ -9,16 +9,10 @@ | ||
| 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 | 18 | * @var array |
| @@ -24,9 +18,9 @@ | ||
| 24 | 18 | * @var 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(); |
| @@ -54,14 +48,11 @@ | ||
| 54 | 48 | /** |
| 55 | 49 | * @since 4.05 |
| 56 | 50 | * |
| 57 | 51 | * @param array|false $filter |
| 58 | - * | |
| 59 | - * @return array | |
| 60 | 52 | */ |
| 61 | 53 | public function get_messages( $filter = false ) { |
| 62 | 54 | $messages = self::$messages; |
| 63 | - | |
| 64 | 55 | if ( $filter === 'filter' ) { |
| 65 | 56 | $this->filter_messages( $messages ); |
| 66 | 57 | } |
| 67 | 58 | return $messages; |
| @@ -73,9 +64,8 @@ | ||
| 73 | 64 | * @return void |
| 74 | 65 | */ |
| 75 | 66 | public function set_messages() { |
| 76 | 67 | self::$messages = get_option( $this->option ); |
| 77 | - | |
| 78 | 68 | if ( ! is_array( self::$messages ) ) { |
| 79 | 69 | self::$messages = array(); |
| 80 | 70 | } |
| 81 | 71 | |
| @@ -93,9 +83,8 @@ | ||
| 93 | 83 | * @return void |
| 94 | 84 | */ |
| 95 | 85 | private function add_api_messages() { |
| 96 | 86 | $api = $this->get_api_info(); |
| 97 | - | |
| 98 | 87 | if ( empty( $api ) ) { |
| 99 | 88 | return; |
| 100 | 89 | } |
| 101 | 90 | |
| @@ -120,9 +109,9 @@ | ||
| 120 | 109 | // Don't replace messages unless required. |
| 121 | 110 | return; |
| 122 | 111 | } |
| 123 | 112 | |
| 124 | - if ( ! $this->within_valid_timeframe( $message ) ) { | |
| 113 | + if ( $this->is_expired( $message ) ) { | |
| 125 | 114 | return; |
| 126 | 115 | } |
| 127 | 116 | |
| 128 | 117 | if ( isset( self::$messages[ $message['key'] ] ) ) { |
| @@ -166,14 +155,13 @@ | ||
| 166 | 155 | * @return void |
| 167 | 156 | */ |
| 168 | 157 | private function clean_messages() { |
| 169 | 158 | $removed = false; |
| 170 | - | |
| 171 | 159 | foreach ( self::$messages as $t => $message ) { |
| 172 | 160 | $read = ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' ); |
| 173 | 161 | $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 ) ) { | |
| 162 | + $expired = $this->is_expired( $message ); | |
| 163 | + if ( $read || $expired || $dismissed ) { | |
| 176 | 164 | unset( self::$messages[ $t ] ); |
| 177 | 165 | $removed = true; |
| 178 | 166 | } |
| 179 | 167 | } |
| @@ -185,62 +173,28 @@ | ||
| 185 | 173 | |
| 186 | 174 | /** |
| 187 | 175 | * @param array $messages |
| 188 | 176 | * @param string $type |
| 189 | - * | |
| 190 | 177 | * @return void |
| 191 | 178 | */ |
| 192 | 179 | public function filter_messages( &$messages, $type = 'unread' ) { |
| 193 | 180 | $user_id = get_current_user_id(); |
| 194 | - | |
| 195 | 181 | foreach ( $messages as $k => $message ) { |
| 196 | 182 | $dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] ); |
| 197 | - | |
| 198 | - if ( empty( $k ) || ! $this->within_valid_timeframe( $message ) || ( $type === 'dismissed' ) !== $dismissed ) { | |
| 183 | + if ( empty( $k ) || $this->is_expired( $message ) || ( $type === 'dismissed' ) !== $dismissed ) { | |
| 199 | 184 | unset( $messages[ $k ] ); |
| 200 | 185 | } elseif ( ! $this->is_for_user( $message ) ) { |
| 201 | 186 | unset( $messages[ $k ] ); |
| 202 | 187 | } |
| 203 | 188 | } |
| 204 | - | |
| 205 | 189 | $messages = apply_filters( 'frm_filter_inbox', $messages ); |
| 206 | 190 | } |
| 207 | 191 | |
| 208 | 192 | /** |
| 209 | - * Check if a message has started and is not expired. | |
| 210 | - * | |
| 211 | - * @since 6.25 | |
| 212 | - * | |
| 213 | 193 | * @param array $message |
| 214 | 194 | * |
| 215 | 195 | * @return bool |
| 216 | 196 | */ |
| 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 | 197 | private function is_expired( $message ) { |
| 244 | 198 | return ! empty( $message['expires'] ) && $message['expires'] < time(); |
| 245 | 199 | } |
| 246 | 200 | |
| @@ -247,18 +201,24 @@ | ||
| 247 | 201 | /** |
| 248 | 202 | * Show different messages for different accounts. |
| 249 | 203 | * |
| 250 | 204 | * @param array $message |
| 251 | - * | |
| 252 | 205 | * @return bool |
| 253 | 206 | */ |
| 254 | 207 | private function is_for_user( $message ) { |
| 255 | - if ( FrmApiHelper::is_for_user( $message ) ) { | |
| 208 | + if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) { | |
| 256 | 209 | return true; |
| 257 | 210 | } |
| 258 | - | |
| 259 | 211 | $who = (array) $message['who']; |
| 260 | - | |
| 212 | + if ( in_array( 'all', $who, true ) || in_array( 'everyone', $who, true ) ) { | |
| 213 | + return true; | |
| 214 | + } | |
| 215 | + if ( in_array( $this->get_user_type(), $who, true ) ) { | |
| 216 | + return true; | |
| 217 | + } | |
| 218 | + if ( in_array( 'free_first_30', $who, true ) && $this->is_free_first_30() ) { | |
| 219 | + return true; | |
| 220 | + } | |
| 261 | 221 | /** |
| 262 | 222 | * Allow for other special inbox cases in other add-ons. |
| 263 | 223 | * |
| 264 | 224 | * @since 6.8.1 |
| @@ -269,8 +229,16 @@ | ||
| 269 | 229 | */ |
| 270 | 230 | return (bool) apply_filters( 'frm_inbox_message_is_for_user', false, $who, $message ); |
| 271 | 231 | } |
| 272 | 232 | |
| 233 | + private function get_user_type() { | |
| 234 | + if ( ! FrmAppHelper::pro_is_installed() ) { | |
| 235 | + return 'free'; | |
| 236 | + } | |
| 237 | + | |
| 238 | + return FrmAddonsController::license_type(); | |
| 239 | + } | |
| 240 | + | |
| 273 | 241 | /** |
| 274 | 242 | * @param string $key |
| 275 | 243 | * |
| 276 | 244 | * @return void |
| @@ -291,14 +259,12 @@ | ||
| 291 | 259 | /** |
| 292 | 260 | * @since 4.05.02 |
| 293 | 261 | * |
| 294 | 262 | * @param string $key |
| 295 | - * | |
| 296 | 263 | * @return void |
| 297 | 264 | */ |
| 298 | 265 | public function mark_unread( $key ) { |
| 299 | 266 | $is_read = isset( self::$messages[ $key ] ) && isset( self::$messages[ $key ]['read'] ) && isset( self::$messages[ $key ]['read'][ get_current_user_id() ] ); |
| 300 | - | |
| 301 | 267 | if ( $is_read ) { |
| 302 | 268 | unset( self::$messages[ $key ]['read'][ get_current_user_id() ] ); |
| 303 | 269 | $this->update_list(); |
| 304 | 270 | } |
| @@ -333,9 +299,8 @@ | ||
| 333 | 299 | * @return void |
| 334 | 300 | */ |
| 335 | 301 | private function dismiss_all() { |
| 336 | 302 | $user_id = get_current_user_id(); |
| 337 | - | |
| 338 | 303 | foreach ( self::$messages as $key => $message ) { |
| 339 | 304 | if ( ! isset( $message['dismissed'] ) ) { |
| 340 | 305 | self::$messages[ $key ]['dismissed'] = array(); |
| 341 | 306 | } |
| @@ -346,15 +311,11 @@ | ||
| 346 | 311 | } |
| 347 | 312 | $this->update_list(); |
| 348 | 313 | } |
| 349 | 314 | |
| 350 | - /** | |
| 351 | - * @return array | |
| 352 | - */ | |
| 353 | 315 | public function unread() { |
| 354 | 316 | $messages = $this->get_messages( 'filter' ); |
| 355 | 317 | $user_id = get_current_user_id(); |
| 356 | - | |
| 357 | 318 | foreach ( $messages as $t => $message ) { |
| 358 | 319 | if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) { |
| 359 | 320 | unset( $messages[ $t ] ); |
| 360 | 321 | } |
| @@ -365,14 +326,12 @@ | ||
| 365 | 326 | /** |
| 366 | 327 | * @since 6.8.4 The $filtered parameter was added. |
| 367 | 328 | * |
| 368 | 329 | * @param bool $filtered |
| 369 | - * | |
| 370 | 330 | * @return string |
| 371 | 331 | */ |
| 372 | 332 | public function unread_html( $filtered = true ) { |
| 373 | 333 | $count = count( $this->unread() ); |
| 374 | - | |
| 375 | 334 | if ( ! $count ) { |
| 376 | 335 | return ''; |
| 377 | 336 | } |
| 378 | 337 | |
| @@ -393,9 +352,8 @@ | ||
| 393 | 352 | /** |
| 394 | 353 | * @since 4.05.02 |
| 395 | 354 | * |
| 396 | 355 | * @param string $key |
| 397 | - * | |
| 398 | 356 | * @return void |
| 399 | 357 | */ |
| 400 | 358 | public function remove( $key ) { |
| 401 | 359 | if ( isset( self::$messages[ $key ] ) ) { |
| @@ -411,8 +369,31 @@ | ||
| 411 | 369 | update_option( $this->option, self::$messages, 'no' ); |
| 412 | 370 | } |
| 413 | 371 | |
| 414 | 372 | /** |
| 373 | + * Check if user is still using the Lite version only, and within | |
| 374 | + * the first 30 days of activation. | |
| 375 | + * | |
| 376 | + * @since 6.16 | |
| 377 | + * | |
| 378 | + * @return bool | |
| 379 | + */ | |
| 380 | + private function is_free_first_30() { | |
| 381 | + $activation_timestamp = get_option( 'frm_first_activation' ); | |
| 382 | + if ( false === $activation_timestamp ) { | |
| 383 | + // If the option does not exist, assume that it is | |
| 384 | + // because the user was active before this option was introduced. | |
| 385 | + return false; | |
| 386 | + } | |
| 387 | + if ( FrmAppHelper::pro_is_included() ) { | |
| 388 | + // Not free. | |
| 389 | + return false; | |
| 390 | + } | |
| 391 | + $cutoff = strtotime( '-30 days' ); | |
| 392 | + return $activation_timestamp > $cutoff; | |
| 393 | + } | |
| 394 | + | |
| 395 | + /** | |
| 415 | 396 | * Show a banner message if one is available. |
| 416 | 397 | * |
| 417 | 398 | * @return bool True if a banner is available and shown. |
| 418 | 399 | */ |
| @@ -419,9 +400,8 @@ | ||
| 419 | 400 | public static function maybe_show_banner() { |
| 420 | 401 | if ( empty( self::$banner_messages ) ) { |
| 421 | 402 | return false; |
| 422 | 403 | } |
| 423 | - | |
| 424 | 404 | $message = end( self::$banner_messages ); |
| 425 | 405 | $cta = self::get_prepared_banner_cta( $message['cta'] ); |
| 426 | 406 | |
| 427 | 407 | require FrmAppHelper::plugin_path() . '/classes/views/inbox/banner.php'; |
| @@ -433,9 +413,8 @@ | ||
| 433 | 413 | * |
| 434 | 414 | * @since 6.8.4 |
| 435 | 415 | * |
| 436 | 416 | * @param string $cta |
| 437 | - * | |
| 438 | 417 | * @return string |
| 439 | 418 | */ |
| 440 | 419 | private static function get_prepared_banner_cta( $cta ) { |
| 441 | 420 | $cta = str_replace( 'button-secondary', 'button-primary', $cta ); |
| @@ -444,9 +423,8 @@ | ||
| 444 | 423 | /** |
| 445 | 424 | * Replace a single href attribute in the CTA. |
| 446 | 425 | * |
| 447 | 426 | * @param array $matches The regex results for a single match. |
| 448 | - * | |
| 449 | 427 | * @return string |
| 450 | 428 | */ |
| 451 | 429 | function ( $matches ) { |
| 452 | 430 | $url = $matches[2]; |
| @@ -456,9 +434,8 @@ | ||
| 456 | 434 | return 'href="#"'; |
| 457 | 435 | } |
| 458 | 436 | |
| 459 | 437 | $query = array(); |
| 460 | - | |
| 461 | 438 | if ( isset( $parts['query'] ) ) { |
| 462 | 439 | parse_str( $parts['query'], $query ); |
| 463 | 440 | } |
| 464 | 441 | $query['utm_medium'] = 'banner'; |
| @@ -473,9 +450,8 @@ | ||
| 473 | 450 | * @return void |
| 474 | 451 | */ |
| 475 | 452 | public static function maybe_disable_screen_options() { |
| 476 | 453 | self::$banner_messages = self::get_banner_messages(); |
| 477 | - | |
| 478 | 454 | if ( self::$banner_messages ) { |
| 479 | 455 | // disable screen options tab when displaying banner messages because it gets in the way of the banner. |
| 480 | 456 | add_filter( 'screen_options_show_screen', '__return_false' ); |
| 481 | 457 | } |
| @@ -506,9 +482,8 @@ | ||
| 506 | 482 | * |
| 507 | 483 | * @since 6.8.4 |
| 508 | 484 | * |
| 509 | 485 | * @param string $key The key we are checking for (ie. banner or slidein). |
| 510 | - * | |
| 511 | 486 | * @return array |
| 512 | 487 | */ |
| 513 | 488 | private static function get_messages_with_key( $key ) { |
| 514 | 489 | $inbox = new self(); |
| @@ -539,9 +514,8 @@ | ||
| 539 | 514 | * @return array|false |
| 540 | 515 | */ |
| 541 | 516 | public static function get_inbox_slide_in_value_for_js() { |
| 542 | 517 | $messages = self::get_slidein_messages(); |
| 543 | - | |
| 544 | 518 | if ( ! $messages ) { |
| 545 | 519 | return false; |
| 546 | 520 | } |
| 547 | 521 | |
| @@ -562,41 +536,11 @@ | ||
| 562 | 536 | |
| 563 | 537 | return array_reduce( |
| 564 | 538 | $keys_to_return, |
| 565 | 539 | function ( $total, $key ) use ( $message ) { |
| 566 | - $total[ $key ] = $message[ $key ] ?? ''; | |
| 540 | + $total[ $key ] = isset( $message[ $key ] ) ? $message[ $key ] : ''; | |
| 567 | 541 | return $total; |
| 568 | 542 | }, |
| 569 | 543 | array() |
| 570 | 544 | ); |
| 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 | 545 | } |
| 602 | 546 | } |