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