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