| @@ -14,13 +14,13 @@ | ||
| 14 | 14 | |
| 15 | 15 | private static $messages = false; |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * @param array | |
| 18 | + * @var array | |
| 19 | 19 | */ |
| 20 | 20 | private static $banner_messages; |
| 21 | 21 | |
| 22 | - public function __construct( $for_parent = null ) { | |
| 22 | + public function __construct() { | |
| 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 false|string $filter | |
| 51 | + * @param array|false $filter | |
| 52 | 52 | */ |
| 53 | 53 | public function get_messages( $filter = false ) { |
| 54 | 54 | $messages = self::$messages; |
| 55 | 55 | if ( $filter === 'filter' ) { |
| @@ -139,9 +139,10 @@ | ||
| 139 | 139 | 'subject' => '', |
| 140 | 140 | 'icon' => 'frm_tooltip_icon', |
| 141 | 141 | 'cta' => '', |
| 142 | 142 | 'expires' => false, |
| 143 | - 'who' => 'all', // use 'free', 'personal', 'business', 'elite', 'grandfathered' | |
| 143 | + // Use 'free', 'personal', 'business', 'elite', 'grandfathered'. | |
| 144 | + 'who' => 'all', | |
| 144 | 145 | 'type' => '', |
| 145 | 146 | ); |
| 146 | 147 | |
| 147 | 148 | $message = array_merge( $defaults, $message ); |
| @@ -155,10 +156,10 @@ | ||
| 155 | 156 | */ |
| 156 | 157 | private function clean_messages() { |
| 157 | 158 | $removed = false; |
| 158 | 159 | foreach ( self::$messages as $t => $message ) { |
| 159 | - $read = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' ); | |
| 160 | - $dismissed = isset( $message['dismissed'] ) && ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' ); | |
| 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' ); | |
| 161 | 162 | $expired = $this->is_expired( $message ); |
| 162 | 163 | if ( $read || $expired || $dismissed ) { |
| 163 | 164 | unset( self::$messages[ $t ] ); |
| 164 | 165 | $removed = true; |
| @@ -170,15 +171,17 @@ | ||
| 170 | 171 | } |
| 171 | 172 | } |
| 172 | 173 | |
| 173 | 174 | /** |
| 175 | + * @param array $messages | |
| 176 | + * @param string $type | |
| 174 | 177 | * @return void |
| 175 | 178 | */ |
| 176 | - private function filter_messages( &$messages ) { | |
| 179 | + public function filter_messages( &$messages, $type = 'unread' ) { | |
| 177 | 180 | $user_id = get_current_user_id(); |
| 178 | 181 | foreach ( $messages as $k => $message ) { |
| 179 | 182 | $dismissed = isset( $message['dismissed'] ) && isset( $message['dismissed'][ $user_id ] ); |
| 180 | - if ( empty( $k ) || $this->is_expired( $message ) || $dismissed ) { | |
| 183 | + if ( empty( $k ) || $this->is_expired( $message ) || ( $type === 'dismissed' ) !== $dismissed ) { | |
| 181 | 184 | unset( $messages[ $k ] ); |
| 182 | 185 | } elseif ( ! $this->is_for_user( $message ) ) { |
| 183 | 186 | unset( $messages[ $k ] ); |
| 184 | 187 | } |
| @@ -191,9 +194,9 @@ | ||
| 191 | 194 | * |
| 192 | 195 | * @return bool |
| 193 | 196 | */ |
| 194 | 197 | private function is_expired( $message ) { |
| 195 | - return isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time(); | |
| 198 | + return ! empty( $message['expires'] ) && $message['expires'] < time(); | |
| 196 | 199 | } |
| 197 | 200 | |
| 198 | 201 | /** |
| 199 | 202 | * Show different messages for different accounts. |
| @@ -201,24 +204,24 @@ | ||
| 201 | 204 | * @param array $message |
| 202 | 205 | * @return bool |
| 203 | 206 | */ |
| 204 | 207 | private function is_for_user( $message ) { |
| 205 | - if ( ! isset( $message['who'] ) || $message['who'] === 'all' ) { | |
| 208 | + if ( FrmApiHelper::is_for_user( $message ) ) { | |
| 206 | 209 | return true; |
| 207 | 210 | } |
| 211 | + | |
| 208 | 212 | $who = (array) $message['who']; |
| 209 | - if ( in_array( 'all', $who, true ) || in_array( 'everyone', $who, true ) ) { | |
| 210 | - return true; | |
| 211 | - } | |
| 212 | - return in_array( $this->get_user_type(), $who, true ); | |
| 213 | - } | |
| 214 | 213 | |
| 215 | - private function get_user_type() { | |
| 216 | - if ( ! FrmAppHelper::pro_is_installed() ) { | |
| 217 | - return 'free'; | |
| 218 | - } | |
| 219 | - | |
| 220 | - return FrmAddonsController::license_type(); | |
| 214 | + /** | |
| 215 | + * Allow for other special inbox cases in other add-ons. | |
| 216 | + * | |
| 217 | + * @since 6.8.1 | |
| 218 | + * | |
| 219 | + * @param bool $is_for_user | |
| 220 | + * @param array $who | |
| 221 | + * @param array $message | |
| 222 | + */ | |
| 223 | + return (bool) apply_filters( 'frm_inbox_message_is_for_user', false, $who, $message ); | |
| 221 | 224 | } |
| 222 | 225 | |
| 223 | 226 | /** |
| 224 | 227 | * @param string $key |
| @@ -238,12 +241,11 @@ | ||
| 238 | 241 | $this->update_list(); |
| 239 | 242 | } |
| 240 | 243 | |
| 241 | 244 | /** |
| 242 | - * @param string $key | |
| 243 | - * | |
| 244 | 245 | * @since 4.05.02 |
| 245 | 246 | * |
| 247 | + * @param string $key | |
| 246 | 248 | * @return void |
| 247 | 249 | */ |
| 248 | 250 | public function mark_unread( $key ) { |
| 249 | 251 | $is_read = isset( self::$messages[ $key ] ) && isset( self::$messages[ $key ]['read'] ) && isset( self::$messages[ $key ]['read'][ get_current_user_id() ] ); |
| @@ -305,20 +307,32 @@ | ||
| 305 | 307 | } |
| 306 | 308 | return $messages; |
| 307 | 309 | } |
| 308 | 310 | |
| 309 | - public function unread_html() { | |
| 310 | - $html = ''; | |
| 311 | + /** | |
| 312 | + * @since 6.8.4 The $filtered parameter was added. | |
| 313 | + * | |
| 314 | + * @param bool $filtered | |
| 315 | + * @return string | |
| 316 | + */ | |
| 317 | + public function unread_html( $filtered = true ) { | |
| 311 | 318 | $count = count( $this->unread() ); |
| 312 | - if ( $count ) { | |
| 313 | - $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>'; | |
| 319 | + if ( ! $count ) { | |
| 320 | + return ''; | |
| 321 | + } | |
| 314 | 322 | |
| 315 | - /** | |
| 316 | - * @since 4.06.01 | |
| 317 | - */ | |
| 318 | - $html = apply_filters( 'frm_inbox_badge', $html ); | |
| 323 | + $html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>'; | |
| 324 | + | |
| 325 | + if ( ! $filtered ) { | |
| 326 | + return $html; | |
| 319 | 327 | } |
| 320 | - return $html; | |
| 328 | + | |
| 329 | + /** | |
| 330 | + * @since 4.06.01 | |
| 331 | + * | |
| 332 | + * @param string $html | |
| 333 | + */ | |
| 334 | + return (string) apply_filters( 'frm_inbox_badge', $html ); | |
| 321 | 335 | } |
| 322 | 336 | |
| 323 | 337 | /** |
| 324 | 338 | * @since 4.05.02 |
| @@ -323,9 +337,8 @@ | ||
| 323 | 337 | /** |
| 324 | 338 | * @since 4.05.02 |
| 325 | 339 | * |
| 326 | 340 | * @param string $key |
| 327 | - * | |
| 328 | 341 | * @return void |
| 329 | 342 | */ |
| 330 | 343 | public function remove( $key ) { |
| 331 | 344 | if ( isset( self::$messages[ $key ] ) ) { |
| @@ -350,13 +363,53 @@ | ||
| 350 | 363 | if ( empty( self::$banner_messages ) ) { |
| 351 | 364 | return false; |
| 352 | 365 | } |
| 353 | 366 | $message = end( self::$banner_messages ); |
| 367 | + $cta = self::get_prepared_banner_cta( $message['cta'] ); | |
| 368 | + | |
| 354 | 369 | require FrmAppHelper::plugin_path() . '/classes/views/inbox/banner.php'; |
| 355 | 370 | return true; |
| 356 | 371 | } |
| 357 | 372 | |
| 358 | 373 | /** |
| 374 | + * Make sure that the CTA uses utm_medium=banner. | |
| 375 | + * | |
| 376 | + * @since 6.8.4 | |
| 377 | + * | |
| 378 | + * @param string $cta | |
| 379 | + * @return string | |
| 380 | + */ | |
| 381 | + private static function get_prepared_banner_cta( $cta ) { | |
| 382 | + $cta = str_replace( 'button-secondary', 'button-primary', $cta ); | |
| 383 | + return preg_replace_callback( | |
| 384 | + '/href=("|\')(.*?)("|\')/', | |
| 385 | + /** | |
| 386 | + * Replace a single href attribute in the CTA. | |
| 387 | + * | |
| 388 | + * @param array $matches The regex results for a single match. | |
| 389 | + * @return string | |
| 390 | + */ | |
| 391 | + function ( $matches ) { | |
| 392 | + $url = $matches[2]; | |
| 393 | + $parts = parse_url( $url ); | |
| 394 | + | |
| 395 | + if ( '#' === $url ) { | |
| 396 | + return 'href="#"'; | |
| 397 | + } | |
| 398 | + | |
| 399 | + $query = array(); | |
| 400 | + if ( isset( $parts['query'] ) ) { | |
| 401 | + parse_str( $parts['query'], $query ); | |
| 402 | + } | |
| 403 | + $query['utm_medium'] = 'banner'; | |
| 404 | + $parts['query'] = http_build_query( $query ); | |
| 405 | + return 'href="' . $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . $parts['query'] . '"'; | |
| 406 | + }, | |
| 407 | + $cta | |
| 408 | + ); | |
| 409 | + } | |
| 410 | + | |
| 411 | + /** | |
| 359 | 412 | * @return void |
| 360 | 413 | */ |
| 361 | 414 | public static function maybe_disable_screen_options() { |
| 362 | 415 | self::$banner_messages = self::get_banner_messages(); |
| @@ -366,16 +419,101 @@ | ||
| 366 | 419 | } |
| 367 | 420 | } |
| 368 | 421 | |
| 369 | 422 | /** |
| 423 | + * Get all message with a "banner" message key defined. | |
| 424 | + * | |
| 370 | 425 | * @return array |
| 371 | 426 | */ |
| 372 | 427 | private static function get_banner_messages() { |
| 428 | + return self::get_messages_with_key( 'banner' ); | |
| 429 | + } | |
| 430 | + | |
| 431 | + /** | |
| 432 | + * Get all messages with a "slidein" message key defined. | |
| 433 | + * | |
| 434 | + * @since 6.8.4 | |
| 435 | + * | |
| 436 | + * @return array | |
| 437 | + */ | |
| 438 | + private static function get_slidein_messages() { | |
| 439 | + return self::get_messages_with_key( 'slidein' ); | |
| 440 | + } | |
| 441 | + | |
| 442 | + /** | |
| 443 | + * Get all messages with a $key message key defined. | |
| 444 | + * | |
| 445 | + * @since 6.8.4 | |
| 446 | + * | |
| 447 | + * @param string $key The key we are checking for (ie. banner or slidein). | |
| 448 | + * @return array | |
| 449 | + */ | |
| 450 | + private static function get_messages_with_key( $key ) { | |
| 373 | 451 | $inbox = new self(); |
| 374 | 452 | return array_filter( |
| 375 | 453 | $inbox->get_messages( 'filter' ), |
| 376 | - function( $message ) { | |
| 377 | - return ! empty( $message['banner'] ); | |
| 454 | + function ( $message ) use ( $key ) { | |
| 455 | + return ! empty( $message[ $key ] ); | |
| 378 | 456 | } |
| 379 | 457 | ); |
| 458 | + } | |
| 459 | + | |
| 460 | + /** | |
| 461 | + * Check if there is at least one slidein message. | |
| 462 | + * | |
| 463 | + * @since 6.8.4 | |
| 464 | + * | |
| 465 | + * @return bool | |
| 466 | + */ | |
| 467 | + public static function has_a_slidein_message() { | |
| 468 | + return (bool) self::get_slidein_messages(); | |
| 469 | + } | |
| 470 | + | |
| 471 | + /** | |
| 472 | + * Get the array used for frmGlobal.inboxSlideIn | |
| 473 | + * | |
| 474 | + * @since 6.8.4 | |
| 475 | + * | |
| 476 | + * @return array|false | |
| 477 | + */ | |
| 478 | + public static function get_inbox_slide_in_value_for_js() { | |
| 479 | + $messages = self::get_slidein_messages(); | |
| 480 | + if ( ! $messages ) { | |
| 481 | + return false; | |
| 482 | + } | |
| 483 | + | |
| 484 | + $message = reset( $messages ); | |
| 485 | + | |
| 486 | + /** | |
| 487 | + * Extend the keys in the global JS object. | |
| 488 | + * This is used in Pro to include images. | |
| 489 | + * | |
| 490 | + * @since 6.8.4 | |
| 491 | + * | |
| 492 | + * @param array $keys | |
| 493 | + */ | |
| 494 | + $keys_to_return = apply_filters( | |
| 495 | + 'frm_inbox_slidein_js_vars', | |
| 496 | + array( 'key', 'slidein', 'subject', 'cta' ) | |
| 497 | + ); | |
| 498 | + | |
| 499 | + return array_reduce( | |
| 500 | + $keys_to_return, | |
| 501 | + function ( $total, $key ) use ( $message ) { | |
| 502 | + $total[ $key ] = isset( $message[ $key ] ) ? $message[ $key ] : ''; | |
| 503 | + return $total; | |
| 504 | + }, | |
| 505 | + array() | |
| 506 | + ); | |
| 507 | + } | |
| 508 | + | |
| 509 | + /** | |
| 510 | + * Clear the inbox cache by deleting the associated option from the database. | |
| 511 | + * | |
| 512 | + * @since 6.17 | |
| 513 | + * | |
| 514 | + * @return void | |
| 515 | + */ | |
| 516 | + public static function clear_cache() { | |
| 517 | + delete_option( 'frm_inbox' ); | |
| 380 | 518 | } |
| 381 | 519 | } |