| @@ -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(); |
| @@ -53,19 +47,15 @@ | ||
| 53 | 47 | |
| 54 | 48 | /** |
| 55 | 49 | * @since 4.05 |
| 56 | 50 | * |
| 57 | - * @param false|string $filter | |
| 58 | - * | |
| 59 | - * @return array | |
| 51 | + * @param array|false $filter | |
| 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 | - | |
| 68 | 58 | return $messages; |
| 69 | 59 | } |
| 70 | 60 | |
| 71 | 61 | /** |
| @@ -74,9 +64,8 @@ | ||
| 74 | 64 | * @return void |
| 75 | 65 | */ |
| 76 | 66 | public function set_messages() { |
| 77 | 67 | self::$messages = get_option( $this->option ); |
| 78 | - | |
| 79 | 68 | if ( ! is_array( self::$messages ) ) { |
| 80 | 69 | self::$messages = array(); |
| 81 | 70 | } |
| 82 | 71 | |
| @@ -94,10 +83,9 @@ | ||
| 94 | 83 | * @return void |
| 95 | 84 | */ |
| 96 | 85 | private function add_api_messages() { |
| 97 | 86 | $api = $this->get_api_info(); |
| 98 | - | |
| 99 | - if ( ! $api ) { | |
| 87 | + if ( empty( $api ) ) { | |
| 100 | 88 | return; |
| 101 | 89 | } |
| 102 | 90 | |
| 103 | 91 | foreach ( $api as $message ) { |
| @@ -111,10 +99,10 @@ | ||
| 111 | 99 | * @return void |
| 112 | 100 | */ |
| 113 | 101 | public function add_message( $message ) { |
| 114 | 102 | if ( ! is_array( $message ) || ! isset( $message['key'] ) ) { |
| 115 | - // If the API response is invalid, $message may not be an array. | |
| 116 | - // 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. | |
| 103 | + // if the API response is invalid, $message may not be an array. | |
| 104 | + // 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. | |
| 117 | 105 | return; |
| 118 | 106 | } |
| 119 | 107 | |
| 120 | 108 | if ( isset( self::$messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) { |
| @@ -121,9 +109,9 @@ | ||
| 121 | 109 | // Don't replace messages unless required. |
| 122 | 110 | return; |
| 123 | 111 | } |
| 124 | 112 | |
| 125 | - if ( ! $this->within_valid_timeframe( $message ) ) { | |
| 113 | + if ( $this->is_expired( $message ) ) { | |
| 126 | 114 | return; |
| 127 | 115 | } |
| 128 | 116 | |
| 129 | 117 | if ( isset( self::$messages[ $message['key'] ] ) ) { |
| @@ -167,19 +155,16 @@ | ||
| 167 | 155 | * @return void |
| 168 | 156 | */ |
| 169 | 157 | private function clean_messages() { |
| 170 | 158 | $removed = false; |
| 171 | - | |
| 172 | 159 | foreach ( self::$messages as $t => $message ) { |
| 173 | 160 | $read = ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' ); |
| 174 | - $dismissed = ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 175 | - | |
| 176 | - if ( ! $read && ! $dismissed && $this->within_valid_timeframe( $message ) ) { | |
| 177 | - continue; | |
| 161 | + $dismissed = ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' ); | |
| 162 | + $expired = $this->is_expired( $message ); | |
| 163 | + if ( $read || $expired || $dismissed ) { | |
| 164 | + unset( self::$messages[ $t ] ); | |
| 165 | + $removed = true; | |
| 178 | 166 | } |
| 179 | - | |
| 180 | - unset( self::$messages[ $t ] ); | |
| 181 | - $removed = true; | |
| 182 | 167 | } |
| 183 | 168 | |
| 184 | 169 | if ( $removed ) { |
| 185 | 170 | $this->update_list(); |
| @@ -188,58 +173,28 @@ | ||
| 188 | 173 | |
| 189 | 174 | /** |
| 190 | 175 | * @param array $messages |
| 191 | 176 | * @param string $type |
| 192 | - * | |
| 193 | 177 | * @return void |
| 194 | 178 | */ |
| 195 | 179 | public function filter_messages( &$messages, $type = 'unread' ) { |
| 196 | 180 | $user_id = get_current_user_id(); |
| 197 | - | |
| 198 | 181 | foreach ( $messages as $k => $message ) { |
| 199 | 182 | $dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] ); |
| 200 | - | |
| 201 | - if ( ! $k || ! $this->within_valid_timeframe( $message ) || ( $type === 'dismissed' ) !== $dismissed ) { | |
| 183 | + if ( empty( $k ) || $this->is_expired( $message ) || ( $type === 'dismissed' ) !== $dismissed ) { | |
| 202 | 184 | unset( $messages[ $k ] ); |
| 203 | 185 | } elseif ( ! $this->is_for_user( $message ) ) { |
| 204 | 186 | unset( $messages[ $k ] ); |
| 205 | 187 | } |
| 206 | 188 | } |
| 207 | - | |
| 208 | 189 | $messages = apply_filters( 'frm_filter_inbox', $messages ); |
| 209 | 190 | } |
| 210 | 191 | |
| 211 | 192 | /** |
| 212 | - * Check if a message has started and is not expired. | |
| 213 | - * | |
| 214 | - * @since 6.25 | |
| 215 | - * | |
| 216 | 193 | * @param array $message |
| 217 | 194 | * |
| 218 | 195 | * @return bool |
| 219 | 196 | */ |
| 220 | - private function within_valid_timeframe( $message ) { | |
| 221 | - return $this->has_started( $message ) && ! $this->is_expired( $message ); | |
| 222 | - } | |
| 223 | - | |
| 224 | - /** | |
| 225 | - * Check if a message has actually started, so we can prevent showing something that is queued up early. | |
| 226 | - * | |
| 227 | - * @since 6.25 | |
| 228 | - * | |
| 229 | - * @param array $message | |
| 230 | - * | |
| 231 | - * @return bool | |
| 232 | - */ | |
| 233 | - private function has_started( $message ) { | |
| 234 | - return ! empty( $message['starts'] ) ? $message['starts'] <= time() : true; | |
| 235 | - } | |
| 236 | - | |
| 237 | - /** | |
| 238 | - * @param array $message | |
| 239 | - * | |
| 240 | - * @return bool | |
| 241 | - */ | |
| 242 | 197 | private function is_expired( $message ) { |
| 243 | 198 | return ! empty( $message['expires'] ) && $message['expires'] < time(); |
| 244 | 199 | } |
| 245 | 200 | |
| @@ -246,18 +201,27 @@ | ||
| 246 | 201 | /** |
| 247 | 202 | * Show different messages for different accounts. |
| 248 | 203 | * |
| 249 | 204 | * @param array $message |
| 250 | - * | |
| 251 | 205 | * @return bool |
| 252 | 206 | */ |
| 253 | 207 | private function is_for_user( $message ) { |
| 254 | - if ( FrmApiHelper::is_for_user( $message ) ) { | |
| 208 | + if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) { | |
| 255 | 209 | return true; |
| 256 | 210 | } |
| 257 | - | |
| 258 | 211 | $who = (array) $message['who']; |
| 259 | - | |
| 212 | + if ( $this->is_for_everyone( $who ) ) { | |
| 213 | + return true; | |
| 214 | + } | |
| 215 | + if ( $this->is_user_type( $who ) ) { | |
| 216 | + return true; | |
| 217 | + } | |
| 218 | + if ( in_array( 'free_first_30', $who, true ) && $this->is_free_first_30() ) { | |
| 219 | + return true; | |
| 220 | + } | |
| 221 | + if ( in_array( 'free_not_first_30', $who, true ) && $this->is_free_not_first_30() ) { | |
| 222 | + return true; | |
| 223 | + } | |
| 260 | 224 | /** |
| 261 | 225 | * Allow for other special inbox cases in other add-ons. |
| 262 | 226 | * |
| 263 | 227 | * @since 6.8.1 |
| @@ -269,8 +233,36 @@ | ||
| 269 | 233 | return (bool) apply_filters( 'frm_inbox_message_is_for_user', false, $who, $message ); |
| 270 | 234 | } |
| 271 | 235 | |
| 272 | 236 | /** |
| 237 | + * @since 6.16.3 | |
| 238 | + * | |
| 239 | + * @param array $who | |
| 240 | + * @return bool | |
| 241 | + */ | |
| 242 | + private function is_for_everyone( $who ) { | |
| 243 | + return in_array( 'all', $who, true ) || in_array( 'everyone', $who, true ); | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * @since 6.16.3 | |
| 248 | + * | |
| 249 | + * @param array $who | |
| 250 | + * @return bool | |
| 251 | + */ | |
| 252 | + private function is_user_type( $who ) { | |
| 253 | + return in_array( $this->get_user_type(), $who, true ); | |
| 254 | + } | |
| 255 | + | |
| 256 | + private function get_user_type() { | |
| 257 | + if ( ! FrmAppHelper::pro_is_installed() ) { | |
| 258 | + return 'free'; | |
| 259 | + } | |
| 260 | + | |
| 261 | + return FrmAddonsController::license_type(); | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 273 | 265 | * @param string $key |
| 274 | 266 | * |
| 275 | 267 | * @return void |
| 276 | 268 | */ |
| @@ -290,20 +282,16 @@ | ||
| 290 | 282 | /** |
| 291 | 283 | * @since 4.05.02 |
| 292 | 284 | * |
| 293 | 285 | * @param string $key |
| 294 | - * | |
| 295 | 286 | * @return void |
| 296 | 287 | */ |
| 297 | 288 | public function mark_unread( $key ) { |
| 298 | 289 | $is_read = isset( self::$messages[ $key ] ) && isset( self::$messages[ $key ]['read'] ) && isset( self::$messages[ $key ]['read'][ get_current_user_id() ] ); |
| 299 | - | |
| 300 | - if ( ! $is_read ) { | |
| 301 | - return; | |
| 290 | + if ( $is_read ) { | |
| 291 | + unset( self::$messages[ $key ]['read'][ get_current_user_id() ] ); | |
| 292 | + $this->update_list(); | |
| 302 | 293 | } |
| 303 | - | |
| 304 | - unset( self::$messages[ $key ]['read'][ get_current_user_id() ] ); | |
| 305 | - $this->update_list(); | |
| 306 | 294 | } |
| 307 | 295 | |
| 308 | 296 | /** |
| 309 | 297 | * @param string $key |
| @@ -334,9 +322,8 @@ | ||
| 334 | 322 | * @return void |
| 335 | 323 | */ |
| 336 | 324 | private function dismiss_all() { |
| 337 | 325 | $user_id = get_current_user_id(); |
| 338 | - | |
| 339 | 326 | foreach ( self::$messages as $key => $message ) { |
| 340 | 327 | if ( ! isset( $message['dismissed'] ) ) { |
| 341 | 328 | self::$messages[ $key ]['dismissed'] = array(); |
| 342 | 329 | } |
| @@ -347,21 +334,16 @@ | ||
| 347 | 334 | } |
| 348 | 335 | $this->update_list(); |
| 349 | 336 | } |
| 350 | 337 | |
| 351 | - /** | |
| 352 | - * @return array | |
| 353 | - */ | |
| 354 | 338 | public function unread() { |
| 355 | 339 | $messages = $this->get_messages( 'filter' ); |
| 356 | 340 | $user_id = get_current_user_id(); |
| 357 | - | |
| 358 | 341 | foreach ( $messages as $t => $message ) { |
| 359 | 342 | if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) { |
| 360 | 343 | unset( $messages[ $t ] ); |
| 361 | 344 | } |
| 362 | 345 | } |
| 363 | - | |
| 364 | 346 | return $messages; |
| 365 | 347 | } |
| 366 | 348 | |
| 367 | 349 | /** |
| @@ -367,14 +349,12 @@ | ||
| 367 | 349 | /** |
| 368 | 350 | * @since 6.8.4 The $filtered parameter was added. |
| 369 | 351 | * |
| 370 | 352 | * @param bool $filtered |
| 371 | - * | |
| 372 | 353 | * @return string |
| 373 | 354 | */ |
| 374 | 355 | public function unread_html( $filtered = true ) { |
| 375 | 356 | $count = count( $this->unread() ); |
| 376 | - | |
| 377 | 357 | if ( ! $count ) { |
| 378 | 358 | return ''; |
| 379 | 359 | } |
| 380 | 360 | |
| @@ -395,18 +375,15 @@ | ||
| 395 | 375 | /** |
| 396 | 376 | * @since 4.05.02 |
| 397 | 377 | * |
| 398 | 378 | * @param string $key |
| 399 | - * | |
| 400 | 379 | * @return void |
| 401 | 380 | */ |
| 402 | 381 | public function remove( $key ) { |
| 403 | - if ( ! isset( self::$messages[ $key ] ) ) { | |
| 404 | - return; | |
| 382 | + if ( isset( self::$messages[ $key ] ) ) { | |
| 383 | + unset( self::$messages[ $key ] ); | |
| 384 | + $this->update_list(); | |
| 405 | 385 | } |
| 406 | - | |
| 407 | - unset( self::$messages[ $key ] ); | |
| 408 | - $this->update_list(); | |
| 409 | 386 | } |
| 410 | 387 | |
| 411 | 388 | /** |
| 412 | 389 | * @return void |
| @@ -411,21 +388,68 @@ | ||
| 411 | 388 | /** |
| 412 | 389 | * @return void |
| 413 | 390 | */ |
| 414 | 391 | private function update_list() { |
| 415 | - update_option( $this->option, self::$messages, false ); | |
| 392 | + update_option( $this->option, self::$messages, 'no' ); | |
| 416 | 393 | } |
| 417 | 394 | |
| 418 | 395 | /** |
| 396 | + * Check if user is still using the Lite version only, and within | |
| 397 | + * the first 30 days of activation. | |
| 398 | + * | |
| 399 | + * @since 6.16 | |
| 400 | + * | |
| 401 | + * @return bool | |
| 402 | + */ | |
| 403 | + private function is_free_first_30() { | |
| 404 | + return $this->is_free() && $this->is_first_30(); | |
| 405 | + } | |
| 406 | + | |
| 407 | + /** | |
| 408 | + * @since 6.16.3 | |
| 409 | + * | |
| 410 | + * @return bool | |
| 411 | + */ | |
| 412 | + private function is_first_30() { | |
| 413 | + $activation_timestamp = get_option( 'frm_first_activation' ); | |
| 414 | + if ( false === $activation_timestamp ) { | |
| 415 | + // If the option does not exist, assume that it is | |
| 416 | + // because the user was active before this option was introduced. | |
| 417 | + return false; | |
| 418 | + } | |
| 419 | + $cutoff = strtotime( '-30 days' ); | |
| 420 | + return $activation_timestamp > $cutoff; | |
| 421 | + } | |
| 422 | + | |
| 423 | + /** | |
| 424 | + * @since 6.16.3 | |
| 425 | + * | |
| 426 | + * @return bool | |
| 427 | + */ | |
| 428 | + private function is_free_not_first_30() { | |
| 429 | + return $this->is_free() && ! $this->is_first_30(); | |
| 430 | + } | |
| 431 | + | |
| 432 | + /** | |
| 433 | + * Check if the Pro plugin is active. If not, consider the user to be on the free version. | |
| 434 | + * | |
| 435 | + * @since 6.16.3 | |
| 436 | + * | |
| 437 | + * @return bool | |
| 438 | + */ | |
| 439 | + private function is_free() { | |
| 440 | + return ! FrmAppHelper::pro_is_included(); | |
| 441 | + } | |
| 442 | + | |
| 443 | + /** | |
| 419 | 444 | * Show a banner message if one is available. |
| 420 | 445 | * |
| 421 | 446 | * @return bool True if a banner is available and shown. |
| 422 | 447 | */ |
| 423 | 448 | public static function maybe_show_banner() { |
| 424 | - if ( ! self::$banner_messages ) { | |
| 449 | + if ( empty( self::$banner_messages ) ) { | |
| 425 | 450 | return false; |
| 426 | 451 | } |
| 427 | - | |
| 428 | 452 | $message = end( self::$banner_messages ); |
| 429 | 453 | $cta = self::get_prepared_banner_cta( $message['cta'] ); |
| 430 | 454 | |
| 431 | 455 | require FrmAppHelper::plugin_path() . '/classes/views/inbox/banner.php'; |
| @@ -437,9 +461,8 @@ | ||
| 437 | 461 | * |
| 438 | 462 | * @since 6.8.4 |
| 439 | 463 | * |
| 440 | 464 | * @param string $cta |
| 441 | - * | |
| 442 | 465 | * @return string |
| 443 | 466 | */ |
| 444 | 467 | private static function get_prepared_banner_cta( $cta ) { |
| 445 | 468 | $cta = str_replace( 'button-secondary', 'button-primary', $cta ); |
| @@ -448,28 +471,25 @@ | ||
| 448 | 471 | /** |
| 449 | 472 | * Replace a single href attribute in the CTA. |
| 450 | 473 | * |
| 451 | 474 | * @param array $matches The regex results for a single match. |
| 452 | - * | |
| 453 | 475 | * @return string |
| 454 | 476 | */ |
| 455 | 477 | function ( $matches ) { |
| 456 | - $url = $matches[2]; | |
| 478 | + $url = $matches[2]; | |
| 479 | + $parts = parse_url( $url ); | |
| 457 | 480 | |
| 458 | 481 | if ( '#' === $url ) { |
| 459 | 482 | return 'href="#"'; |
| 460 | 483 | } |
| 461 | 484 | |
| 462 | - $parts = parse_url( $url ); | |
| 463 | 485 | $query = array(); |
| 464 | - | |
| 465 | 486 | if ( isset( $parts['query'] ) ) { |
| 466 | 487 | parse_str( $parts['query'], $query ); |
| 467 | 488 | } |
| 468 | - | |
| 469 | 489 | $query['utm_medium'] = 'banner'; |
| 470 | 490 | $parts['query'] = http_build_query( $query ); |
| 471 | - return 'href="' . esc_url( $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . $parts['query'] ) . '"'; | |
| 491 | + return 'href="' . $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . $parts['query'] . '"'; | |
| 472 | 492 | }, |
| 473 | 493 | $cta |
| 474 | 494 | ); |
| 475 | 495 | } |
| @@ -478,11 +498,10 @@ | ||
| 478 | 498 | * @return void |
| 479 | 499 | */ |
| 480 | 500 | public static function maybe_disable_screen_options() { |
| 481 | 501 | self::$banner_messages = self::get_banner_messages(); |
| 482 | - | |
| 483 | 502 | if ( self::$banner_messages ) { |
| 484 | - // Disable screen options tab when displaying banner messages because it gets in the way of the banner. | |
| 503 | + // disable screen options tab when displaying banner messages because it gets in the way of the banner. | |
| 485 | 504 | add_filter( 'screen_options_show_screen', '__return_false' ); |
| 486 | 505 | } |
| 487 | 506 | } |
| 488 | 507 | |
| @@ -511,9 +530,8 @@ | ||
| 511 | 530 | * |
| 512 | 531 | * @since 6.8.4 |
| 513 | 532 | * |
| 514 | 533 | * @param string $key The key we are checking for (ie. banner or slidein). |
| 515 | - * | |
| 516 | 534 | * @return array |
| 517 | 535 | */ |
| 518 | 536 | private static function get_messages_with_key( $key ) { |
| 519 | 537 | $inbox = new self(); |
| @@ -544,9 +562,8 @@ | ||
| 544 | 562 | * @return array|false |
| 545 | 563 | */ |
| 546 | 564 | public static function get_inbox_slide_in_value_for_js() { |
| 547 | 565 | $messages = self::get_slidein_messages(); |
| 548 | - | |
| 549 | 566 | if ( ! $messages ) { |
| 550 | 567 | return false; |
| 551 | 568 | } |
| 552 | 569 | |
| @@ -567,42 +584,11 @@ | ||
| 567 | 584 | |
| 568 | 585 | return array_reduce( |
| 569 | 586 | $keys_to_return, |
| 570 | 587 | function ( $total, $key ) use ( $message ) { |
| 571 | - $total[ $key ] = $message[ $key ] ?? ''; | |
| 588 | + $total[ $key ] = isset( $message[ $key ] ) ? $message[ $key ] : ''; | |
| 572 | 589 | return $total; |
| 573 | 590 | }, |
| 574 | 591 | array() |
| 575 | 592 | ); |
| 576 | - } | |
| 577 | - | |
| 578 | - /** | |
| 579 | - * Clear the inbox cache by deleting the associated option from the database. | |
| 580 | - * | |
| 581 | - * @since 6.17 | |
| 582 | - * | |
| 583 | - * @return void | |
| 584 | - */ | |
| 585 | - public static function clear_cache() { | |
| 586 | - delete_option( 'frm_inbox' ); | |
| 587 | - } | |
| 588 | - | |
| 589 | - /** | |
| 590 | - * Check inbox for an "error" type. This is displayed on the form list page if one exists. | |
| 591 | - * | |
| 592 | - * @since 6.19 | |
| 593 | - * | |
| 594 | - * @return array|false | |
| 595 | - */ | |
| 596 | - public static function check_for_error() { | |
| 597 | - $inbox = new self(); | |
| 598 | - $messages = $inbox->get_messages( 'filter' ); | |
| 599 | - | |
| 600 | - foreach ( $messages as $message ) { | |
| 601 | - if ( is_array( $message ) && isset( $message['type'] ) && 'error' === $message['type'] ) { | |
| 602 | - return $message; | |
| 603 | - } | |
| 604 | - } | |
| 605 | - | |
| 606 | - return false; | |
| 607 | 593 | } |
| 608 | 594 | } |