notice.php
758 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WPMUDEV - Recommended Plugins Notice |
| 4 | * |
| 5 | * @author WPMUDEV (https://wpmudev.com) |
| 6 | * @license GPLv2 |
| 7 | * @package WPMUDEV_Recommended_Plugins_Notice_Registered_Plugin |
| 8 | */ |
| 9 | |
| 10 | if ( ! class_exists( 'WPMUDEV_Recommended_Plugins_Notice_Registered_Plugin' ) ) { |
| 11 | /** |
| 12 | * Class WPMUDEV_Recommended_Plugins_Notice_Registered_Plugin |
| 13 | * |
| 14 | * Hold registered plugin as object |
| 15 | */ |
| 16 | class WPMUDEV_Recommended_Plugins_Notice_Registered_Plugin { //phpcs:ignore |
| 17 | |
| 18 | /** |
| 19 | * Plugin basename |
| 20 | * plugin-dir/plugin-name.php |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $basename = ''; |
| 25 | |
| 26 | /** |
| 27 | * Plugin nice name |
| 28 | * |
| 29 | * `My Plugin` |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | public $name = ''; |
| 34 | |
| 35 | /** |
| 36 | * Screens which notice should be displayed |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | public $screens = array(); |
| 41 | |
| 42 | /** |
| 43 | * Time the plugin registered to notice |
| 44 | * |
| 45 | * @var int |
| 46 | */ |
| 47 | public $registered_at = 0; |
| 48 | |
| 49 | /** |
| 50 | * Element selector which notice should be append-ed |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
| 54 | public $selector = array(); |
| 55 | |
| 56 | /** |
| 57 | * Current active screen being displayed |
| 58 | * |
| 59 | * @var string |
| 60 | */ |
| 61 | protected $active_screen = ''; |
| 62 | |
| 63 | /** |
| 64 | * WPMUDEV_Recommended_Plugins_Notice_Registered_Plugin constructor. |
| 65 | * |
| 66 | * @param string $basename Plugin basename. |
| 67 | */ |
| 68 | public function __construct( $basename ) { |
| 69 | $this->basename = $basename; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get plugin basename |
| 74 | * |
| 75 | * @return string |
| 76 | */ |
| 77 | public function get_basename() { |
| 78 | return $this->basename; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Build object properties from array |
| 83 | * |
| 84 | * @param array $data Notice data. |
| 85 | */ |
| 86 | public function from_array( $data ) { |
| 87 | if ( is_array( $data ) ) { |
| 88 | if ( isset( $data['registered_at'] ) ) { |
| 89 | $this->registered_at = (int) $data['registered_at']; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Export to array |
| 96 | * |
| 97 | * @return array |
| 98 | */ |
| 99 | public function to_array() { |
| 100 | return array( |
| 101 | 'registered_at' => $this->registered_at, |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Check if screen is listen on plugin screen |
| 107 | * |
| 108 | * @param string $screen_id Screen ID. |
| 109 | * |
| 110 | * @return bool |
| 111 | */ |
| 112 | public function is_my_screen( $screen_id ) { |
| 113 | foreach ( $this->screens as $screen ) { |
| 114 | if ( $screen_id === $screen ) { |
| 115 | $this->active_screen = $screen_id; |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get where notice should be moved |
| 126 | * |
| 127 | * @return array |
| 128 | */ |
| 129 | public function get_selector() { |
| 130 | $selector = $this->selector; |
| 131 | $active_screen = $this->active_screen; |
| 132 | |
| 133 | /** |
| 134 | * Filter selector which notice should be moved to |
| 135 | * |
| 136 | * @param array $selector |
| 137 | * @param string $active_screen |
| 138 | * |
| 139 | * @return array |
| 140 | */ |
| 141 | return apply_filters( "wpmudev-recommended-plugin-$this->basename-notice-selector", $selector, $active_screen ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Check whether now is the time to display |
| 146 | * |
| 147 | * @return bool |
| 148 | */ |
| 149 | public function is_time_to_display_notice() { |
| 150 | $active_screen = $this->active_screen; |
| 151 | $seconds_after_registered = 14 * DAY_IN_SECONDS; |
| 152 | |
| 153 | /** |
| 154 | * Filter how many seconds after registered before notice displayed |
| 155 | * |
| 156 | * This filter is globally used. |
| 157 | * |
| 158 | * @param int $seconds_after_registered |
| 159 | * @param string $active_screen |
| 160 | * |
| 161 | * @return string |
| 162 | */ |
| 163 | $seconds_after_registered = apply_filters( "wpmudev-recommended-plugins-notice-display-seconds-after-registered", $seconds_after_registered, $active_screen ); |
| 164 | |
| 165 | /** |
| 166 | * Filter how many seconds after registered before notice displayed |
| 167 | * |
| 168 | * This filter is for plugin based, overriding global value. |
| 169 | * |
| 170 | * @param int $seconds_after_registered |
| 171 | * @param string $active_screen |
| 172 | * |
| 173 | * @return string |
| 174 | */ |
| 175 | $seconds_after_registered = apply_filters( "wpmudev-recommended-plugin-{$this->basename}-notice-display-seconds-after-registered", $seconds_after_registered, $active_screen ); |
| 176 | |
| 177 | $now = time(); |
| 178 | |
| 179 | if ( $now >= ( $this->registered_at + $seconds_after_registered ) ) { |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Get pre text on displayed notice |
| 188 | * |
| 189 | * @return string |
| 190 | */ |
| 191 | public function get_pre_text_notice() { |
| 192 | $pre_text_notice = sprintf( /* translators: %s - plugin name */ |
| 193 | __( 'Enjoying %s? Try out a few of our other popular free plugins...', 'wpmudev_recommended_plugins_notice' ), |
| 194 | $this->name |
| 195 | ); |
| 196 | |
| 197 | /** |
| 198 | * Filter pre text on displayed notice |
| 199 | * |
| 200 | * @param string $pre_text_notice |
| 201 | * |
| 202 | * @return string |
| 203 | */ |
| 204 | return apply_filters( "wpmudev-recommended-plugin-$this->basename-pre-text-notice", $pre_text_notice ); |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if ( ! class_exists( 'WPMUDEV_Recommended_Plugins_Notice' ) ) { |
| 211 | |
| 212 | /** |
| 213 | * Class WPMUDEV_Recommended_Plugins_Notice |
| 214 | * |
| 215 | * @internal |
| 216 | */ |
| 217 | class WPMUDEV_Recommended_Plugins_Notice {//phpcs:ignore |
| 218 | /** |
| 219 | * Class instance. |
| 220 | * |
| 221 | * @var WPMUDEV_Recommended_Plugins_Notice |
| 222 | */ |
| 223 | private static $instance = null; |
| 224 | |
| 225 | /** |
| 226 | * Collection of recommended plugins |
| 227 | * |
| 228 | * @var array |
| 229 | */ |
| 230 | protected $recommended_plugins = array(); |
| 231 | |
| 232 | /** |
| 233 | * Version |
| 234 | */ |
| 235 | const VERSION = '1.0.0'; |
| 236 | |
| 237 | /** |
| 238 | * Pointer name |
| 239 | */ |
| 240 | const POINTER_NAME = 'wpmudev_recommended_plugins'; |
| 241 | |
| 242 | /** |
| 243 | * Registered plugins |
| 244 | */ |
| 245 | const OPTION_NAME = 'wpmudev_recommended_plugins_registered'; |
| 246 | |
| 247 | /** |
| 248 | * Collection of registered plugins to use this notice |
| 249 | * |
| 250 | * @var WPMUDEV_Recommended_Plugins_Notice_Registered_Plugin[] |
| 251 | */ |
| 252 | protected $registered_plugins = array(); |
| 253 | |
| 254 | /** |
| 255 | * Active registered plugin on this screen |
| 256 | * |
| 257 | * @var null |
| 258 | */ |
| 259 | protected $active_registered_plugin = null; |
| 260 | |
| 261 | /** |
| 262 | * WPMUDEV_Recommended_Plugins_Notice constructor. |
| 263 | */ |
| 264 | public function __construct() { |
| 265 | // Only do things when its on admin screen. |
| 266 | if ( is_admin() ) { |
| 267 | $this->init_recommended_plugins(); |
| 268 | |
| 269 | $this->parse_saved_registered_plugins(); |
| 270 | add_action( 'wpmudev-recommended-plugins-register-notice', array( $this, 'register' ), 10, 4 ); |
| 271 | |
| 272 | add_action( 'all_admin_notices', array( $this, 'display' ), 6 ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Init recommended plugins |
| 278 | * |
| 279 | * @return void |
| 280 | */ |
| 281 | private function init_recommended_plugins() { |
| 282 | $recommended_plugins = array( |
| 283 | array( |
| 284 | 'name' => 'Smush Image Compression', |
| 285 | 'desc' => __( 'Resize, optimize and compress all of your images to the max.', 'wpmudev_recommended_plugins_notice' ), |
| 286 | 'image' => trailingslashit( plugin_dir_url( __FILE__ ) ) . '/assets/images/plugins-smush.png', |
| 287 | 'free_slug' => 'wp-smushit/wp-smush.php', |
| 288 | 'pro_slug' => 'wp-smush-pro/wp-smush.php', |
| 289 | 'install_link' => 'https://wordpress.org/plugins/wp-smushit/', |
| 290 | ), |
| 291 | array( |
| 292 | 'name' => 'Hummingbird Performance', |
| 293 | 'desc' => __( 'Add powerful caching and optimize your assets.', 'wpmudev_recommended_plugins_notice' ), |
| 294 | 'image' => trailingslashit( plugin_dir_url( __FILE__ ) ) . '/assets/images/plugins-hummingbird.png', |
| 295 | 'free_slug' => 'hummingbird-performance/wp-hummingbird.php', |
| 296 | 'pro_slug' => 'wp-hummingbird/wp-hummingbird.php', |
| 297 | 'install_link' => 'https://wordpress.org/plugins/hummingbird-performance/', |
| 298 | ), |
| 299 | array( |
| 300 | 'name' => 'Defender Security', |
| 301 | 'desc' => __( 'Secure and protect your site from malicious hackers and bots.', 'wpmudev_recommended_plugins_notice' ), |
| 302 | 'image' => trailingslashit( plugin_dir_url( __FILE__ ) ) . '/assets/images/plugins-defender.png', |
| 303 | 'free_slug' => 'defender-security/wp-defender.php', |
| 304 | 'pro_slug' => 'wp-defender/wp-defender.php', |
| 305 | 'install_link' => 'https://wordpress.org/plugins/defender-security/', |
| 306 | ), |
| 307 | array( |
| 308 | 'name' => 'SmartCrawl SEO', |
| 309 | 'desc' => __( 'Configure your markup for optimal page and social ranking.', 'wpmudev_recommended_plugins_notice' ), |
| 310 | 'image' => trailingslashit( plugin_dir_url( __FILE__ ) ) . '/assets/images/plugins-smartcrawl.png', |
| 311 | 'free_slug' => 'smartcrawl-seo/wpmu-dev-seo.php', |
| 312 | 'pro_slug' => 'wpmu-dev-seo/wpmu-dev-seo.php', |
| 313 | 'install_link' => 'https://wordpress.org/plugins/smartcrawl-seo/', |
| 314 | ), |
| 315 | array( |
| 316 | 'name' => 'Forminator Forms, Polls & Quizzes', |
| 317 | 'desc' => __( 'Create dynamic forms easily and quickly with our form builder.', 'wpmudev_recommended_plugins_notice' ), |
| 318 | 'image' => trailingslashit( plugin_dir_url( __FILE__ ) ) . '/assets/images/plugins-forminator.png', |
| 319 | 'free_slug' => 'forminator/forminator.php', |
| 320 | 'pro_slug' => '', |
| 321 | 'install_link' => 'https://wordpress.org/plugins/forminator/', |
| 322 | ), |
| 323 | array( |
| 324 | 'name' => 'Hustle Marketing', |
| 325 | 'desc' => __( 'Generate leads with pop-ups, slide-ins and email opt-ins.', 'wpmudev_recommended_plugins_notice' ), |
| 326 | 'image' => trailingslashit( plugin_dir_url( __FILE__ ) ) . '/assets/images/plugins-hustle.png', |
| 327 | 'free_slug' => 'wordpress-popup/popover.php', |
| 328 | 'pro_slug' => 'hustle/opt-in.php', |
| 329 | 'install_link' => 'https://wordpress.org/plugins/wordpress-popup/', |
| 330 | ), |
| 331 | ); |
| 332 | |
| 333 | $recommended_plugins = apply_filters( 'wpmudev-all-recommended-plugins', $recommended_plugins ); |
| 334 | |
| 335 | $this->recommended_plugins = $recommended_plugins; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Get recommended plugins to be displayed on notice |
| 340 | * |
| 341 | * This function will only return recommended plugins that `not installed` yet |
| 342 | * |
| 343 | * @param int $min Minimum plugins to be displayed. |
| 344 | * @param int $max Maximum plugins to be displayed. |
| 345 | * |
| 346 | * @return array |
| 347 | */ |
| 348 | protected function get_recommended_plugins_for_notice( $min = 2, $max = 2 ) { |
| 349 | $recommended_plugins_for_notice = array(); |
| 350 | $recommended_plugins = $this->recommended_plugins; |
| 351 | |
| 352 | foreach ( $recommended_plugins as $recommended_plugin ) { |
| 353 | |
| 354 | if ( $this->is_plugin_installed( $recommended_plugin ) ) { |
| 355 | continue; |
| 356 | } |
| 357 | |
| 358 | $recommended_plugins_for_notice[] = $recommended_plugin; |
| 359 | // Stop when we reached max. |
| 360 | if ( count( $recommended_plugins_for_notice ) >= $max ) { |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Not enough! |
| 366 | if ( count( $recommended_plugins_for_notice ) < $min ) { |
| 367 | $recommended_plugins_for_notice = array(); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Filter recommended plugins to be displayed on notice |
| 372 | * |
| 373 | * @param array $recommended_plugins_for_notice recommended plugins to be displayed |
| 374 | * @param array $recommended_plugins all recommended plugins |
| 375 | * @param int $min minimum plugins to be displayed |
| 376 | * @param int $max maximum plugins to be displayed |
| 377 | * |
| 378 | * @return array |
| 379 | */ |
| 380 | return apply_filters( 'wpmudev-recommended-plugins', $recommended_plugins_for_notice, $recommended_plugins, $min, $max ); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Check whether plugin is installed |
| 385 | * |
| 386 | * @uses get_plugins() |
| 387 | * |
| 388 | * @param array $plugin_data Plugin data. |
| 389 | * |
| 390 | * @return bool |
| 391 | */ |
| 392 | protected function is_plugin_installed( $plugin_data ) { |
| 393 | $is_installed = false; |
| 394 | |
| 395 | if ( ! function_exists( 'get_plugins' ) ) { |
| 396 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 397 | } |
| 398 | |
| 399 | $installed_plugins = get_plugins(); |
| 400 | |
| 401 | // Check the free one. |
| 402 | if ( isset( $plugin_data['free_slug'] ) && ! empty( $plugin_data['free_slug'] ) ) { |
| 403 | |
| 404 | if ( isset( $installed_plugins[ $plugin_data['free_slug'] ] ) ) { |
| 405 | $is_installed = true; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // Check the pro one. |
| 410 | if ( ! $is_installed ) { |
| 411 | if ( isset( $plugin_data['pro_slug'] ) && ! empty( $plugin_data['pro_slug'] ) ) { |
| 412 | if ( isset( $installed_plugins[ $plugin_data['pro_slug'] ] ) ) { |
| 413 | $is_installed = true; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Filter is_installed status of recommended plugin |
| 420 | * |
| 421 | * @param bool $is_installed |
| 422 | * @param array $plugin_data plugin to be check |
| 423 | * @param array $installed_plugins current installed plugins |
| 424 | * |
| 425 | * @return bool |
| 426 | */ |
| 427 | return apply_filters( 'wpmudev-recommended-plugin-is-installed', $is_installed, $plugin_data, $installed_plugins ); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Display the notice |
| 432 | * |
| 433 | * @return void |
| 434 | */ |
| 435 | public function display() { |
| 436 | /** |
| 437 | * Fires before displaying notice |
| 438 | * |
| 439 | * This action fired before any check done. |
| 440 | */ |
| 441 | do_action( 'wpmudev-recommended-plugins-before-display' ); |
| 442 | |
| 443 | $is_displayable = $this->is_displayable(); |
| 444 | |
| 445 | /** |
| 446 | * Filter whether notice is displayable |
| 447 | * |
| 448 | * @param bool $is_displayable |
| 449 | * |
| 450 | * @return bool |
| 451 | */ |
| 452 | $is_displayable = apply_filters( 'wpmudev-recommended-plugins-is-displayable', $is_displayable ); |
| 453 | if ( ! $is_displayable ) { |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | $active_registered_plugin = $this->active_registered_plugin; |
| 458 | |
| 459 | /** |
| 460 | * Filter whether notice is displayable |
| 461 | * |
| 462 | * @param bool $is_displayable |
| 463 | * |
| 464 | * @return bool |
| 465 | */ |
| 466 | $active_registered_plugin = apply_filters( 'wpmudev-recommended-plugin-active-registered', $active_registered_plugin ); |
| 467 | |
| 468 | if ( ! $active_registered_plugin instanceof WPMUDEV_Recommended_Plugins_Notice_Registered_Plugin ) { |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | $recommended_plugins = $this->get_recommended_plugins_for_notice(); |
| 473 | |
| 474 | // no plugins to be recommended. |
| 475 | if ( empty( $recommended_plugins ) ) { |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | wp_register_style( 'wpmudev-recommended-plugins-css', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/css/notice.css', array(), self::VERSION ); |
| 480 | wp_enqueue_style( 'wpmudev-recommended-plugins-css' ); |
| 481 | |
| 482 | wp_register_script( 'wpmudev-recommended-plugins-js', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/js/notice.js', array( 'jquery' ), self::VERSION, true ); |
| 483 | wp_enqueue_script( 'wpmudev-recommended-plugins-js' ); |
| 484 | |
| 485 | $dismissed_text = __( 'Dismiss', 'wpmudev_recommended_plugins_notice' ); |
| 486 | |
| 487 | /** |
| 488 | * Filter dismissed text |
| 489 | * |
| 490 | * @param string $dismissed_text |
| 491 | * |
| 492 | * @return string |
| 493 | */ |
| 494 | $dismissed_text = apply_filters( 'wpmudev-recommended-plugins-dismissed-text', $dismissed_text ); |
| 495 | |
| 496 | // placement customizer. |
| 497 | $selector = $active_registered_plugin->get_selector(); |
| 498 | $data_selector_el = ''; |
| 499 | $data_selector_fn = ''; |
| 500 | |
| 501 | if ( is_array( $selector ) ) { |
| 502 | if ( isset( $selector[0] ) ) { |
| 503 | $data_selector_fn = (string) $selector[0]; |
| 504 | } |
| 505 | if ( isset( $selector[1] ) ) { |
| 506 | $data_selector_el = (string) $selector[1]; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | ?> |
| 511 | <div class="wpmudev-recommended-plugins" style="display: none" |
| 512 | data-selector-el="<?php echo esc_attr( $data_selector_el ); ?>" |
| 513 | data-selector-fn="<?php echo esc_attr( $data_selector_fn ); ?>"> |
| 514 | <p class="wpmudev-notice-status"><?php echo wp_kses_post( $active_registered_plugin->get_pre_text_notice() ); ?></p> |
| 515 | <div class="wpmudev-recommended-plugin-blocks"> |
| 516 | <?php foreach ( $recommended_plugins as $recommended_plugin ) : ?> |
| 517 | <div class="wpmudev-recommended-plugin-block"> |
| 518 | <a href="<?php echo esc_attr( $recommended_plugin['install_link'] ); ?>" target="_blank"> |
| 519 | <div class="wpmudev-recommended-plugin-block-image"> |
| 520 | <img src="<?php echo esc_url( $recommended_plugin['image'] ); ?>" alt="<?php echo esc_attr( $recommended_plugin['name'] ); ?>"/> |
| 521 | </div> |
| 522 | <div class="wpmudev-recommended-plugin-block-detail"> |
| 523 | <h3 class="wpmudev-plugin-name"><?php echo esc_html( $recommended_plugin['name'] ); ?></h3> |
| 524 | <p class="wpmudev-plugin-description"><?php echo esc_html( $recommended_plugin['desc'] ); ?></p> |
| 525 | </div> |
| 526 | </a> |
| 527 | </div> |
| 528 | <?php endforeach; ?> |
| 529 | </div> |
| 530 | |
| 531 | <div class="wpmudev-recommended-plugins-dismiss"> |
| 532 | <a class="dismiss" |
| 533 | href="#" |
| 534 | aria-label="Dismiss" |
| 535 | data-action="dismiss-wp-pointer" |
| 536 | data-pointer="<?php echo esc_attr( self::POINTER_NAME ); ?>"> |
| 537 | <i class="icon-close" aria-hidden="true"></i> |
| 538 | </a> |
| 539 | </div> |
| 540 | </div> |
| 541 | <?php |
| 542 | |
| 543 | /** |
| 544 | * Fires after displaying notice |
| 545 | * |
| 546 | * This action fired after notice markup sent if any check above fails, this action won't fire. |
| 547 | */ |
| 548 | do_action( 'wpmudev-recommended-plugins-after-display' ); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Check if notice can be displayed |
| 553 | * |
| 554 | * @uses current_user_can() |
| 555 | * |
| 556 | * @return bool |
| 557 | */ |
| 558 | public function is_displayable() { |
| 559 | /** |
| 560 | * CHECK #1 whether dismissed previously ? |
| 561 | * |
| 562 | * @uses dismissed_wp_pointers use builtin dismissed_wp_pointers to hold data, to avoid add more rows on WP |
| 563 | */ |
| 564 | $dismissed_wp_pointers = get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ); |
| 565 | $dismissed_wp_pointers = explode( ',', (string) $dismissed_wp_pointers ); |
| 566 | |
| 567 | $dismissed = ( is_array( $dismissed_wp_pointers ) && in_array( self::POINTER_NAME, $dismissed_wp_pointers, true ) ); |
| 568 | |
| 569 | /** |
| 570 | * Filter flag of dismissed status |
| 571 | * |
| 572 | * @param bool $dismissed |
| 573 | * |
| 574 | * @return bool |
| 575 | */ |
| 576 | $dismissed = apply_filters( 'wpmudev-recommended-plugins-dismissed', $dismissed, $dismissed_wp_pointers ); |
| 577 | |
| 578 | if ( $dismissed ) { |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * CHECK #2 Cap check |
| 584 | * default is for user that capable of `install_plugins`, which make sense because this notice will suggest user to install plugin |
| 585 | */ |
| 586 | $capability_to_check = 'install_plugins'; |
| 587 | |
| 588 | /** |
| 589 | * Filter user capability which will be shown this notice |
| 590 | * |
| 591 | * @param string $capability_to_check |
| 592 | * |
| 593 | * @return string |
| 594 | */ |
| 595 | $capability_to_check = apply_filters( 'wpmudev-recommended-plugins-capability', $capability_to_check ); |
| 596 | |
| 597 | if ( ! current_user_can( $capability_to_check ) ) { |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * CHECK #3 is_wpmudev_member |
| 603 | * |
| 604 | * This guy is a pro!! |
| 605 | * - wpmudev dash activated and or membership type = full |
| 606 | */ |
| 607 | if ( function_exists( 'is_wpmudev_member' ) ) { |
| 608 | if ( is_wpmudev_member() ) { |
| 609 | return false; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * CHECK #4 only display on screens that defined by plugin |
| 615 | */ |
| 616 | $current_screen = get_current_screen(); |
| 617 | |
| 618 | if ( ! $current_screen instanceof WP_Screen || ! isset( $current_screen->id ) ) { |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | $active_registered_plugin = null; |
| 623 | foreach ( $this->registered_plugins as $registered_plugin ) { |
| 624 | if ( $registered_plugin->is_my_screen( $current_screen->id ) ) { |
| 625 | $active_registered_plugin = $registered_plugin; |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if ( is_null( $active_registered_plugin ) ) { |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * CHECK #5 time after register to display notice |
| 636 | */ |
| 637 | if ( ! $active_registered_plugin->is_time_to_display_notice() ) { |
| 638 | return false; |
| 639 | } |
| 640 | |
| 641 | $this->active_registered_plugin = $active_registered_plugin; |
| 642 | |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Register plugin for notice to be added |
| 648 | * |
| 649 | * @param string $plugin_basename Plugin basename. |
| 650 | * @param string $plugin_name Plugin name. |
| 651 | * @param array $screens Screens. |
| 652 | * @param array $selector [jqueryFn, jQuerySelector]. |
| 653 | */ |
| 654 | public function register( $plugin_basename, $plugin_name, $screens = array(), $selector = array() ) { |
| 655 | // Invalid register. |
| 656 | if ( empty( $plugin_basename ) || empty( $plugin_name ) ) { |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | if ( ! isset( $this->registered_plugins[ $plugin_basename ] ) ) { |
| 661 | $registered_plugin_object = new WPMUDEV_Recommended_Plugins_Notice_Registered_Plugin( $plugin_basename ); |
| 662 | $registered_plugin_object->registered_at = time(); |
| 663 | $this->registered_plugins[ $plugin_basename ] = $registered_plugin_object; |
| 664 | |
| 665 | $this->save_registered_plugins(); |
| 666 | } |
| 667 | |
| 668 | $registered_plugin_object = $this->registered_plugins[ $plugin_basename ]; |
| 669 | $registered_plugin_object->name = $plugin_name; |
| 670 | $registered_plugin_object->screens = $screens; |
| 671 | $registered_plugin_object->selector = $selector; |
| 672 | |
| 673 | $this->registered_plugins[ $plugin_basename ] = $registered_plugin_object; |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Parse registered plugins from storage |
| 678 | */ |
| 679 | public function parse_saved_registered_plugins() { |
| 680 | /** |
| 681 | * Fired before saved registered plugins being parse |
| 682 | */ |
| 683 | do_action( 'wpmudev-recommended-plugins-before-parse-saved-registered-plugins' ); |
| 684 | |
| 685 | $registered_plugins = get_site_option( self::OPTION_NAME, array() ); |
| 686 | |
| 687 | if ( is_array( $registered_plugins ) ) { |
| 688 | foreach ( $registered_plugins as $plugin_basename => $registered_plugin ) { |
| 689 | $registered_plugin_object = new WPMUDEV_Recommended_Plugins_Notice_Registered_Plugin( $plugin_basename ); |
| 690 | $registered_plugin_object->from_array( $registered_plugin ); |
| 691 | $this->registered_plugins[ $plugin_basename ] = $registered_plugin_object; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Save registered plugins to storage |
| 698 | */ |
| 699 | public function save_registered_plugins() { |
| 700 | $registered_plugins = array(); |
| 701 | |
| 702 | foreach ( $this->registered_plugins as $registered_plugin ) { |
| 703 | $registered_plugins[ $registered_plugin->get_basename() ] = $registered_plugin->to_array(); |
| 704 | } |
| 705 | |
| 706 | update_site_option( self::OPTION_NAME, $registered_plugins ); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * Un-dismiss the notice |
| 711 | * |
| 712 | * @internal |
| 713 | */ |
| 714 | public function un_dismiss() { |
| 715 | $dismissed_wp_pointers = get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ); |
| 716 | $dismissed_wp_pointers = explode( ',', (string) $dismissed_wp_pointers ); |
| 717 | |
| 718 | $dismissed = ( is_array( $dismissed_wp_pointers ) && in_array( self::POINTER_NAME, $dismissed_wp_pointers, true ) ); |
| 719 | |
| 720 | if ( $dismissed ) { |
| 721 | foreach ( $dismissed_wp_pointers as $key => $dismissed_wp_pointer ) { |
| 722 | if ( self::POINTER_NAME === $dismissed_wp_pointer ) { |
| 723 | unset( $dismissed_wp_pointers[ $key ] ); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | $dismissed_wp_pointers = implode( ',', $dismissed_wp_pointers ); |
| 728 | |
| 729 | update_user_meta( get_current_user_id(), 'dismissed_wp_pointers', $dismissed_wp_pointers ); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Reset saved registered plugins |
| 735 | * |
| 736 | * @internal |
| 737 | */ |
| 738 | public function reset_registered_plugins() { |
| 739 | delete_site_option( self::OPTION_NAME ); |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * Get class instance. |
| 744 | * |
| 745 | * @return WPMUDEV_Recommended_Plugins_Notice |
| 746 | */ |
| 747 | public static function get_instance() { |
| 748 | if ( is_null( self::$instance ) ) { |
| 749 | self::$instance = new self(); |
| 750 | } |
| 751 | |
| 752 | return self::$instance; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | $GLOBALS['WPMUDEV_Recommended_Plugins_Notice'] = WPMUDEV_Recommended_Plugins_Notice::get_instance(); |
| 757 | } |
| 758 |