ad-health-notices.php
4 weeks ago
class-ad-network-ad-unit.php
5 months ago
class-ad-network.php
1 year ago
class-licenses.php
3 months ago
class-notices.php
5 months ago
class-overview-widgets.php
1 year ago
class-plugins-screen-updates.php
2 months ago
notices.php
5 months ago
class-notices.php
591 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName |
| 2 | /** |
| 3 | * Container class for admin notices |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.x.x |
| 8 | */ |
| 9 | |
| 10 | use AdvancedAds\Abstracts\Ad; |
| 11 | use AdvancedAds\Utilities\WordPress; |
| 12 | use AdvancedAds\Utilities\Conditional; |
| 13 | use AdvancedAds\Framework\Utilities\Arr; |
| 14 | |
| 15 | /** |
| 16 | * Container class for admin notices |
| 17 | * |
| 18 | * @package WordPress |
| 19 | * @subpackage Advanced Ads Plugin |
| 20 | */ |
| 21 | class Advanced_Ads_Admin_Notices { |
| 22 | |
| 23 | /** |
| 24 | * Maximum number of notices to show at once |
| 25 | */ |
| 26 | const MAX_NOTICES = 2; |
| 27 | |
| 28 | /** |
| 29 | * Options |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | protected $options; |
| 34 | |
| 35 | /** |
| 36 | * Notices to be displayed |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | public $notices = []; |
| 41 | |
| 42 | /** |
| 43 | * Advanced_Ads_Admin_Notices constructor to load notices |
| 44 | */ |
| 45 | public function __construct() { |
| 46 | $this->load_notices(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Return an instance of this class. |
| 51 | * |
| 52 | * @return object A single instance of this class. |
| 53 | */ |
| 54 | public static function get_instance() { |
| 55 | static $instance; |
| 56 | |
| 57 | // if the single instance hasn't been set, set it now. |
| 58 | if ( null === $instance ) { |
| 59 | $instance = new self(); |
| 60 | } |
| 61 | |
| 62 | return $instance; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Determines if a notice can be displayed. |
| 67 | * |
| 68 | * @param string $notice The notice identifier. |
| 69 | * |
| 70 | * @return bool Returns true if the notice can be displayed, false otherwise. |
| 71 | */ |
| 72 | public function can_display( $notice ) { |
| 73 | $options = $this->options(); |
| 74 | $closed = $options['closed'] ?? []; |
| 75 | |
| 76 | return ! array_key_exists( $notice, $closed ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Load admin notices |
| 81 | */ |
| 82 | public function load_notices() { |
| 83 | $options = $this->options(); |
| 84 | $plugin_options = Advanced_Ads::get_instance()->options(); |
| 85 | |
| 86 | // load notices from queue. |
| 87 | $this->notices = isset( $options['queue'] ) ? $options['queue'] : []; |
| 88 | $notices_before = $this->notices; |
| 89 | |
| 90 | // check license notices. |
| 91 | $this->register_license_notices(); |
| 92 | |
| 93 | // check wizard notice. |
| 94 | $this->register_wizard_notice(); |
| 95 | |
| 96 | // check non org plugins update. |
| 97 | $this->check_non_org_plugins(); |
| 98 | |
| 99 | // don’t check non-critical notices if they are disabled. |
| 100 | if ( ! isset( $plugin_options['disable-notices'] ) ) { |
| 101 | // check other notices. |
| 102 | $this->check_notices(); |
| 103 | } |
| 104 | |
| 105 | // register notices in db so they get displayed until closed for good. |
| 106 | if ( $this->notices !== $notices_before ) { |
| 107 | $this->add_to_queue( $this->notices ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Check various notices conditions |
| 113 | */ |
| 114 | public function check_notices() { |
| 115 | $internal_options = Advanced_Ads::get_instance()->internal_options(); |
| 116 | $now = time(); |
| 117 | $activation = ( isset( $internal_options['installed'] ) ) ? $internal_options['installed'] : $now; // activation time. |
| 118 | |
| 119 | $options = $this->options(); |
| 120 | $closed = isset( $options['closed'] ) ? $options['closed'] : []; |
| 121 | $queue = isset( $options['queue'] ) ? $options['queue'] : []; |
| 122 | $paused = isset( $options['paused'] ) ? $options['paused'] : []; |
| 123 | |
| 124 | // offer free add-ons if not yet subscribed. |
| 125 | if ( Conditional::user_can_subscribe( 'nl_free_addons' ) && ! in_array( 'nl_free_addons', $queue, true ) && ! isset( $closed['nl_free_addons'] ) ) { |
| 126 | // get number of ads. |
| 127 | if ( WordPress::get_count_ads() ) { |
| 128 | $this->notices[] = 'nl_free_addons'; |
| 129 | } |
| 130 | } |
| 131 | $number_of_ads = 0; |
| 132 | // needed error handling due to a weird bug in the piklist plugin. |
| 133 | try { |
| 134 | $number_of_ads = WordPress::get_count_ads(); |
| 135 | } catch ( Exception $e ) { // phpcs:ignore |
| 136 | // no need to catch anything since we just use TRY/CATCH to prevent an issue caused by another plugin. |
| 137 | } |
| 138 | |
| 139 | // ask for a review after 2 days and when 3 ads were created and when not paused. |
| 140 | if ( |
| 141 | ! in_array( 'review', $queue, true ) |
| 142 | && ! isset( $closed['review'] ) |
| 143 | && ( ! isset( $paused['review'] ) || $paused['review'] <= time() ) |
| 144 | && 172800 < ( time() - $activation ) |
| 145 | && 3 <= $number_of_ads |
| 146 | ) { |
| 147 | $this->notices[] = 'review'; |
| 148 | } elseif ( in_array( 'review', $queue, true ) && 3 > $number_of_ads ) { |
| 149 | $review_key = array_search( 'review', $this->notices, true ); |
| 150 | if ( false !== $review_key ) { |
| 151 | unset( $this->notices[ $review_key ] ); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Register license key notices |
| 158 | */ |
| 159 | public function register_license_notices() { |
| 160 | |
| 161 | if ( ! Conditional::is_screen_advanced_ads() ) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | $options = $this->options(); |
| 166 | $queue = isset( $options['queue'] ) ? $options['queue'] : []; |
| 167 | // check license keys. |
| 168 | |
| 169 | if ( Advanced_Ads_Checks::licenses_invalid() ) { |
| 170 | if ( ! in_array( 'license_invalid', $queue, true ) ) { |
| 171 | $this->notices[] = 'license_invalid'; |
| 172 | } |
| 173 | } else { |
| 174 | $this->remove_from_queue( 'license_invalid' ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Register wizard notice. |
| 180 | */ |
| 181 | public function register_wizard_notice() { |
| 182 | if ( ! Conditional::is_screen_advanced_ads() ) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | $options = $this->options(); |
| 187 | $queue = isset( $options['queue'] ) ? $options['queue'] : []; |
| 188 | |
| 189 | if ( Advanced_Ads_Checks::can_launch_wizard() ) { |
| 190 | if ( ! in_array( 'monetize_wizard', $queue, true ) ) { |
| 191 | $this->notices[] = 'monetize_wizard'; |
| 192 | } |
| 193 | } else { |
| 194 | $this->remove_from_queue( 'monetize_wizard' ); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Check for updates of non wp.org plugins |
| 200 | */ |
| 201 | public function check_non_org_plugins() { |
| 202 | if ( ! Conditional::is_screen_advanced_ads() ) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | $addons = \AdvancedAds\Constants::ADDONS_NON_COMPATIBLE_VERSIONS; |
| 207 | $plugins = WordPress::get_wp_plugins(); |
| 208 | $options = $this->options(); |
| 209 | $queue = isset( $options['queue'] ) ? $options['queue'] : []; |
| 210 | $closed = isset( $options['closed'] ) ? $options['closed'] : []; |
| 211 | |
| 212 | foreach ( $addons as $version => $slug ) { |
| 213 | $addon = $plugins[ $slug ] ?? null; |
| 214 | if ( ! $addon ) { |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | $notice = $slug . '_upgrade'; |
| 219 | |
| 220 | if ( version_compare( $addon['version'], $version, '<=' ) ) { |
| 221 | if ( ! in_array( $notice, $queue, true ) && ! array_key_exists( $notice, $closed ) ) { |
| 222 | $this->notices[] = $notice; |
| 223 | } |
| 224 | } else { |
| 225 | $this->remove_from_queue( $notice ); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Add update notices to the queue of all notices that still needs to be closed |
| 232 | * |
| 233 | * @param mixed $notices one or more notices to be added to the queue. |
| 234 | * |
| 235 | * @since 1.5.3 |
| 236 | */ |
| 237 | public function add_to_queue( $notices = 0 ) { |
| 238 | if ( ! $notices ) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | // get queue from options. |
| 243 | $options = $this->options(); |
| 244 | $queue = isset( $options['queue'] ) ? $options['queue'] : []; |
| 245 | |
| 246 | if ( is_array( $notices ) ) { |
| 247 | $queue = array_merge( $queue, $notices ); |
| 248 | } else { |
| 249 | $queue[] = $notices; |
| 250 | } |
| 251 | |
| 252 | // remove possible duplicated. |
| 253 | $queue = array_unique( $queue ); |
| 254 | |
| 255 | // update db. |
| 256 | $options['queue'] = $queue; |
| 257 | $this->update_options( $options ); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Remove update notice from queue |
| 262 | * move notice into "closed" |
| 263 | * |
| 264 | * @param string $notice notice to be removed from the queue. |
| 265 | * |
| 266 | * @since 1.5.3 |
| 267 | */ |
| 268 | public function remove_from_queue( $notice ) { |
| 269 | if ( ! isset( $notice ) ) { |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | // get queue from options. |
| 274 | $options = $this->options(); |
| 275 | $options_before = $options; |
| 276 | if ( ! isset( $options['queue'] ) ) { |
| 277 | return; |
| 278 | } |
| 279 | $queue = (array) $options['queue']; |
| 280 | $closed = isset( $options['closed'] ) ? $options['closed'] : []; |
| 281 | $paused = isset( $options['paused'] ) ? $options['paused'] : []; |
| 282 | |
| 283 | $key = array_search( $notice, $queue, true ); |
| 284 | if ( false !== $key ) { |
| 285 | unset( $queue[ $key ] ); |
| 286 | // close message with timestamp. |
| 287 | } |
| 288 | // don’t close again twice. |
| 289 | if ( ! isset( $closed[ $notice ] ) ) { |
| 290 | $closed[ $notice ] = time(); |
| 291 | } |
| 292 | // remove from pause. |
| 293 | if ( isset( $paused[ $notice ] ) ) { |
| 294 | unset( $paused[ $notice ] ); |
| 295 | } |
| 296 | |
| 297 | // update db. |
| 298 | $options['queue'] = $queue; |
| 299 | $options['closed'] = $closed; |
| 300 | $options['paused'] = $paused; |
| 301 | |
| 302 | // only update if changed. |
| 303 | if ( $options_before !== $options ) { |
| 304 | $this->update_options( $options ); |
| 305 | // update already registered notices. |
| 306 | $this->load_notices(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Hide any notice for a given time |
| 312 | * move notice into "paused" with notice as key and timestamp as value |
| 313 | * |
| 314 | * @param string $notice notice to be paused. |
| 315 | */ |
| 316 | public function hide_notice( $notice ) { |
| 317 | if ( ! isset( $notice ) ) { |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | // get queue from options. |
| 322 | $options = $this->options(); |
| 323 | $options_before = $options; |
| 324 | if ( ! isset( $options['queue'] ) ) { |
| 325 | return; |
| 326 | } |
| 327 | $queue = (array) $options['queue']; |
| 328 | $paused = isset( $options['paused'] ) ? $options['paused'] : []; |
| 329 | |
| 330 | $key = array_search( $notice, $queue, true ); |
| 331 | if ( false !== $key ) { |
| 332 | unset( $queue[ $key ] ); |
| 333 | } |
| 334 | // close message with timestamp in 7 days |
| 335 | // don’t close again twice. |
| 336 | if ( ! isset( $paused[ $notice ] ) ) { |
| 337 | $paused[ $notice ] = time() + WEEK_IN_SECONDS; |
| 338 | } |
| 339 | |
| 340 | // update db. |
| 341 | $options['queue'] = $queue; |
| 342 | $options['paused'] = $paused; |
| 343 | |
| 344 | // only update if changed. |
| 345 | if ( $options_before !== $options ) { |
| 346 | $this->update_options( $options ); |
| 347 | // update already registered notices. |
| 348 | $this->load_notices(); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Display notices |
| 354 | */ |
| 355 | public function display_notices() { |
| 356 | if ( wp_doing_ajax() ) { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | // register Black Friday 2025 deals. |
| 361 | if ( time() > 1764025200 && |
| 362 | time() <= 1764630000 ) { |
| 363 | $options = $this->options(); |
| 364 | $closed = isset( $options['closed'] ) ? $options['closed'] : []; |
| 365 | |
| 366 | if ( ! isset( $closed['bfcm25'] ) ) { |
| 367 | $this->notices[] = 'bfcm25'; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if ( [] === $this->notices ) { |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | include_once ADVADS_ABSPATH . '/admin/includes/notices.php'; |
| 376 | |
| 377 | // Register Conflict Plugins notice |
| 378 | $this->register_plugin_conflict_notices($plugin_conflicts); |
| 379 | |
| 380 | // Iterate through notices. |
| 381 | $count = 0; |
| 382 | foreach ( $this->notices as $_notice ) { |
| 383 | |
| 384 | if ( isset( $advanced_ads_admin_notices[ $_notice ] ) ) { |
| 385 | $notice = $advanced_ads_admin_notices[ $_notice ]; |
| 386 | $text = $advanced_ads_admin_notices[ $_notice ]['text']; |
| 387 | $type = isset( $advanced_ads_admin_notices[ $_notice ]['type'] ) ? $advanced_ads_admin_notices[ $_notice ]['type'] : ''; |
| 388 | } else { |
| 389 | continue; |
| 390 | } |
| 391 | |
| 392 | // don’t display non-global notices on other than plugin related pages. |
| 393 | if ( |
| 394 | ( ! isset( $advanced_ads_admin_notices[ $_notice ]['global'] ) || ! $advanced_ads_admin_notices[ $_notice ]['global'] ) |
| 395 | && ! Conditional::is_screen_advanced_ads() |
| 396 | ) { |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | // don't display license nag if ADVANCED_ADS_SUPPRESS_PLUGIN_ERROR_NOTICES is defined. |
| 401 | if ( defined( 'ADVANCED_ADS_SUPPRESS_PLUGIN_ERROR_NOTICES' ) && 'plugin_error' === $advanced_ads_admin_notices[ $_notice ]['type'] ) { |
| 402 | continue; |
| 403 | } |
| 404 | |
| 405 | $hash = [ |
| 406 | 'info' => '/admin/views/notices/info.php', |
| 407 | 'subscribe' => '/admin/views/notices/subscribe.php', |
| 408 | 'plugin_error' => '/admin/views/notices/plugin_error.php', |
| 409 | 'promo' => '/admin/views/notices/promo.php', |
| 410 | ]; |
| 411 | |
| 412 | $locate_tempalte = isset( $hash[ $type ] ) ? $hash[ $type ] : '/admin/views/notices/error.php'; |
| 413 | include ADVADS_ABSPATH . $locate_tempalte; |
| 414 | |
| 415 | // phpcs:disable |
| 416 | // if ( self::MAX_NOTICES === ++$count ) { |
| 417 | // break; |
| 418 | // } |
| 419 | // phpcs:enable |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Return notices options |
| 425 | * |
| 426 | * @return array $options |
| 427 | */ |
| 428 | public function options() { |
| 429 | if ( ! isset( $this->options ) ) { |
| 430 | $this->options = get_option( ADVADS_SLUG . '-notices', [] ); |
| 431 | } |
| 432 | |
| 433 | return $this->options; |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Update notices options |
| 438 | * |
| 439 | * @param array $options new options. |
| 440 | */ |
| 441 | public function update_options( array $options ) { |
| 442 | // do not allow to clear options. |
| 443 | if ( [] === $options ) { |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | $this->options = $options; |
| 448 | update_option( ADVADS_SLUG . '-notices', $options ); |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Subscribe to newsletter and autoresponder |
| 453 | * |
| 454 | * @param string $notice slug of the subscription notice to send the correct reply. |
| 455 | * |
| 456 | * @return string |
| 457 | */ |
| 458 | public function subscribe( $notice ) { |
| 459 | if ( ! isset( $notice ) ) { |
| 460 | return ''; |
| 461 | } |
| 462 | |
| 463 | $user = wp_get_current_user(); |
| 464 | |
| 465 | if ( '' === $user->user_email ) { |
| 466 | /* translators: %s: is a URL. */ |
| 467 | return sprintf( __( 'You don’t seem to have an email address. Please use <a href="%s" target="_blank">this form</a> to sign up.', 'advanced-ads' ), 'http://eepurl.com/bk4z4P' ); |
| 468 | } |
| 469 | |
| 470 | $data = [ |
| 471 | 'email' => $user->user_email, |
| 472 | 'notice' => $notice, |
| 473 | ]; |
| 474 | |
| 475 | $result = wp_remote_post( |
| 476 | 'https://wpadvancedads.com/remote/subscribe.php?source=plugin', |
| 477 | [ |
| 478 | 'method' => 'POST', |
| 479 | 'timeout' => 20, |
| 480 | 'redirection' => 5, |
| 481 | 'httpversion' => '1.1', |
| 482 | 'blocking' => true, |
| 483 | 'body' => $data, |
| 484 | ] |
| 485 | ); |
| 486 | |
| 487 | if ( is_wp_error( $result ) ) { |
| 488 | return __( 'How embarrassing. The email server seems to be down. Please try again later.', 'advanced-ads' ); |
| 489 | } |
| 490 | |
| 491 | // Mark as subscribed and move notice from queue. |
| 492 | $this->mark_as_subscribed( $notice ); |
| 493 | $this->remove_from_queue( $notice ); |
| 494 | |
| 495 | /* translators: the first %s is an email address, the seconds %s is a URL. */ |
| 496 | return sprintf( __( 'Please check your email (%1$s) for the confirmation message. If you didn’t receive one or want to use another email address then please use <a href="%2$s" target="_blank">this form</a> to sign up.', 'advanced-ads' ), $user->user_email, 'http://eepurl.com/bk4z4P' ); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Update information that the current user is subscribed |
| 501 | * |
| 502 | * @param string $notice notice slug. |
| 503 | */ |
| 504 | private function mark_as_subscribed( $notice ) { |
| 505 | // Early bail!! |
| 506 | if ( empty( $notice ) || ! Conditional::user_can_subscribe( $notice ) ) { |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | $user_id = get_current_user_id(); |
| 511 | $subscribed_notices = get_user_meta( $user_id, 'advanced-ads-subscribed', true ); |
| 512 | |
| 513 | // backward compatibility. |
| 514 | if ( ! is_array( $subscribed_notices ) ) { |
| 515 | $subscribed_notices = []; |
| 516 | } |
| 517 | |
| 518 | $subscribed_notices[ $notice ] = true; |
| 519 | |
| 520 | update_user_meta( $user_id, 'advanced-ads-subscribed', $subscribed_notices ); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Check if a usesr can be subscribed to our newsletter |
| 525 | * check if is already subscribed or email is invalid |
| 526 | * |
| 527 | * @deprecated version 2.0 use Conditional::user_can_subscribe() instead |
| 528 | * |
| 529 | * @return bool true if user can subscribe |
| 530 | */ |
| 531 | public function user_can_subscribe() { |
| 532 | _deprecated_function( __METHOD__, '2.0', '\AdvancedAds\Utilities\Conditional::user_can_subscribe()' ); |
| 533 | |
| 534 | return Conditional::user_can_subscribe( 'nl_first_steps' ); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Add AdSense tutorial notice |
| 539 | * |
| 540 | * @param Ad $ad ad object. |
| 541 | */ |
| 542 | public function adsense_tutorial( $ad ) { |
| 543 | $options = $this->options(); |
| 544 | $_notice = 'nl_adsense'; |
| 545 | |
| 546 | if ( 'adsense' !== $ad->get_type() || isset( $options['closed'][ $_notice ] ) ) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | include ADVADS_ABSPATH . '/admin/includes/notices.php'; |
| 551 | |
| 552 | if ( ! isset( $advanced_ads_admin_notices[ $_notice ] ) ) { |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | $notice = $advanced_ads_admin_notices[ $_notice ]; |
| 557 | $text = $notice['text']; |
| 558 | include ADVADS_ABSPATH . '/admin/views/notices/inline.php'; |
| 559 | } |
| 560 | |
| 561 | |
| 562 | |
| 563 | /** |
| 564 | * Register plugin conflict notices already defined in notices.php |
| 565 | */ |
| 566 | public function register_plugin_conflict_notices($plugin_conflicts) { |
| 567 | |
| 568 | // Early Bail ! |
| 569 | if (empty($plugin_conflicts)) { |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | $options = $this->options(); |
| 574 | $queue = $options['queue'] ?? []; |
| 575 | $closed = $options['closed'] ?? []; |
| 576 | |
| 577 | |
| 578 | foreach($plugin_conflicts as $slug => $name){ |
| 579 | $noticeId = str_replace(' ', '_', strtolower($name)); |
| 580 | $notice_id = sanitize_title( $noticeId ) . '_active'; |
| 581 | if ( ! is_plugin_active( $slug ) ) { |
| 582 | continue; |
| 583 | } |
| 584 | if ( ! in_array( $notice_id, $queue, true ) && ! isset( $closed[ $notice_id ] ) ) { |
| 585 | $this->notices[] = $notice_id; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | } |
| 591 |