| @@ -18,9 +18,9 @@ | ||
| 18 | 18 | * @var 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(); |
| @@ -47,9 +47,9 @@ | ||
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | 49 | * @since 4.05 |
| 50 | 50 | * |
| 51 | - * @param array|false $filter | |
| 51 | + * @param false|array $filter | |
| 52 | 52 | */ |
| 53 | 53 | public function get_messages( $filter = false ) { |
| 54 | 54 | $messages = self::$messages; |
| 55 | 55 | if ( $filter === 'filter' ) { |
| @@ -109,9 +109,9 @@ | ||
| 109 | 109 | // Don't replace messages unless required. |
| 110 | 110 | return; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - if ( ! $this->within_valid_timeframe( $message ) ) { | |
| 113 | + if ( $this->is_expired( $message ) ) { | |
| 114 | 114 | return; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | if ( isset( self::$messages[ $message['key'] ] ) ) { |
| @@ -156,12 +156,12 @@ | ||
| 156 | 156 | */ |
| 157 | 157 | private function clean_messages() { |
| 158 | 158 | $removed = false; |
| 159 | 159 | 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 ) ) { | |
| 160 | + $read = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' ); | |
| 161 | + $dismissed = isset( $message['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 | 164 | unset( self::$messages[ $t ] ); |
| 165 | 165 | $removed = true; |
| 166 | 166 | } |
| 167 | 167 | } |
| @@ -171,10 +171,8 @@ | ||
| 171 | 171 | } |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
| 175 | - * @param array $messages | |
| 176 | - * @param string $type | |
| 177 | 175 | * @return void |
| 178 | 176 | */ |
| 179 | 177 | public function filter_messages( &$messages, $type = 'unread' ) { |
| 180 | 178 | $user_id = get_current_user_id(); |
| @@ -179,9 +177,9 @@ | ||
| 179 | 177 | public function filter_messages( &$messages, $type = 'unread' ) { |
| 180 | 178 | $user_id = get_current_user_id(); |
| 181 | 179 | foreach ( $messages as $k => $message ) { |
| 182 | 180 | $dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] ); |
| 183 | - if ( empty( $k ) || ! $this->within_valid_timeframe( $message ) || ( $type === 'dismissed' ) !== $dismissed ) { | |
| 181 | + if ( empty( $k ) || $this->is_expired( $message ) || ( $type === 'dismissed' ) !== $dismissed ) { | |
| 184 | 182 | unset( $messages[ $k ] ); |
| 185 | 183 | } elseif ( ! $this->is_for_user( $message ) ) { |
| 186 | 184 | unset( $messages[ $k ] ); |
| 187 | 185 | } |
| @@ -189,42 +187,14 @@ | ||
| 189 | 187 | $messages = apply_filters( 'frm_filter_inbox', $messages ); |
| 190 | 188 | } |
| 191 | 189 | |
| 192 | 190 | /** |
| 193 | - * Check if a message has started and is not expired. | |
| 194 | - * | |
| 195 | - * @since 6.25 | |
| 196 | - * | |
| 197 | 191 | * @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 | 192 | * |
| 207 | - * @since 6.25 | |
| 208 | - * | |
| 209 | - * @param array $message | |
| 210 | 193 | * @return bool |
| 211 | 194 | */ |
| 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 | 195 | private function is_expired( $message ) { |
| 226 | - return ! empty( $message['expires'] ) && $message['expires'] < time(); | |
| 196 | + return isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time(); | |
| 227 | 197 | } |
| 228 | 198 | |
| 229 | 199 | /** |
| 230 | 200 | * Show different messages for different accounts. |
| @@ -232,24 +202,24 @@ | ||
| 232 | 202 | * @param array $message |
| 233 | 203 | * @return bool |
| 234 | 204 | */ |
| 235 | 205 | private function is_for_user( $message ) { |
| 236 | - if ( FrmApiHelper::is_for_user( $message ) ) { | |
| 206 | + if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) { | |
| 237 | 207 | return true; |
| 238 | 208 | } |
| 209 | + $who = (array) $message['who']; | |
| 210 | + if ( in_array( 'all', $who, true ) || in_array( 'everyone', $who, true ) ) { | |
| 211 | + return true; | |
| 212 | + } | |
| 213 | + return in_array( $this->get_user_type(), $who, true ); | |
| 214 | + } | |
| 239 | 215 | |
| 240 | - $who = (array) $message['who']; | |
| 216 | + private function get_user_type() { | |
| 217 | + if ( ! FrmAppHelper::pro_is_installed() ) { | |
| 218 | + return 'free'; | |
| 219 | + } | |
| 241 | 220 | |
| 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 ); | |
| 221 | + return FrmAddonsController::license_type(); | |
| 252 | 222 | } |
| 253 | 223 | |
| 254 | 224 | /** |
| 255 | 225 | * @param string $key |
| @@ -269,11 +239,12 @@ | ||
| 269 | 239 | $this->update_list(); |
| 270 | 240 | } |
| 271 | 241 | |
| 272 | 242 | /** |
| 243 | + * @param string $key | |
| 244 | + * | |
| 273 | 245 | * @since 4.05.02 |
| 274 | 246 | * |
| 275 | - * @param string $key | |
| 276 | 247 | * @return void |
| 277 | 248 | */ |
| 278 | 249 | public function mark_unread( $key ) { |
| 279 | 250 | $is_read = isset( self::$messages[ $key ] ) && isset( self::$messages[ $key ]['read'] ) && isset( self::$messages[ $key ]['read'][ get_current_user_id() ] ); |
| @@ -335,32 +306,20 @@ | ||
| 335 | 306 | } |
| 336 | 307 | return $messages; |
| 337 | 308 | } |
| 338 | 309 | |
| 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 ) { | |
| 310 | + public function unread_html() { | |
| 311 | + $html = ''; | |
| 346 | 312 | $count = count( $this->unread() ); |
| 347 | - if ( ! $count ) { | |
| 348 | - return ''; | |
| 349 | - } | |
| 313 | + if ( $count ) { | |
| 314 | + $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>'; | |
| 350 | 315 | |
| 351 | - $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>'; | |
| 352 | - | |
| 353 | - if ( ! $filtered ) { | |
| 354 | - return $html; | |
| 316 | + /** | |
| 317 | + * @since 4.06.01 | |
| 318 | + */ | |
| 319 | + $html = apply_filters( 'frm_inbox_badge', $html ); | |
| 355 | 320 | } |
| 356 | - | |
| 357 | - /** | |
| 358 | - * @since 4.06.01 | |
| 359 | - * | |
| 360 | - * @param string $html | |
| 361 | - */ | |
| 362 | - return (string) apply_filters( 'frm_inbox_badge', $html ); | |
| 321 | + return $html; | |
| 363 | 322 | } |
| 364 | 323 | |
| 365 | 324 | /** |
| 366 | 325 | * @since 4.05.02 |
| @@ -365,8 +324,9 @@ | ||
| 365 | 324 | /** |
| 366 | 325 | * @since 4.05.02 |
| 367 | 326 | * |
| 368 | 327 | * @param string $key |
| 328 | + * | |
| 369 | 329 | * @return void |
| 370 | 330 | */ |
| 371 | 331 | public function remove( $key ) { |
| 372 | 332 | if ( isset( self::$messages[ $key ] ) ) { |
| @@ -391,53 +351,13 @@ | ||
| 391 | 351 | if ( empty( self::$banner_messages ) ) { |
| 392 | 352 | return false; |
| 393 | 353 | } |
| 394 | 354 | $message = end( self::$banner_messages ); |
| 395 | - $cta = self::get_prepared_banner_cta( $message['cta'] ); | |
| 396 | - | |
| 397 | 355 | require FrmAppHelper::plugin_path() . '/classes/views/inbox/banner.php'; |
| 398 | 356 | return true; |
| 399 | 357 | } |
| 400 | 358 | |
| 401 | 359 | /** |
| 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 | 360 | * @return void |
| 441 | 361 | */ |
| 442 | 362 | public static function maybe_disable_screen_options() { |
| 443 | 363 | self::$banner_messages = self::get_banner_messages(); |
| @@ -447,119 +367,16 @@ | ||
| 447 | 367 | } |
| 448 | 368 | } |
| 449 | 369 | |
| 450 | 370 | /** |
| 451 | - * Get all message with a "banner" message key defined. | |
| 452 | - * | |
| 453 | 371 | * @return array |
| 454 | 372 | */ |
| 455 | 373 | 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 | 374 | $inbox = new self(); |
| 480 | 375 | return array_filter( |
| 481 | 376 | $inbox->get_messages( 'filter' ), |
| 482 | - function ( $message ) use ( $key ) { | |
| 483 | - return ! empty( $message[ $key ] ); | |
| 377 | + function( $message ) { | |
| 378 | + return ! empty( $message['banner'] ); | |
| 484 | 379 | } |
| 485 | 380 | ); |
| 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 | 381 | } |
| 565 | 382 | } |