|
19 | 19 | */ |
20 | 20 | class WPEngine_PHPCompat { |
21 | 21 |
|
| 22 | + /* Capability required to run the scanner and access the admin page */ |
| 23 | + const CAPABILITY = 'manage_options'; |
| 24 | + |
| 25 | + /* Slug for the admin page */ |
| 26 | + const ADMIN_PAGE_SLUG = 'php-compatibility-checker'; |
| 27 | + |
22 | 28 | /* Define and register singleton */ |
23 | 29 | private static $instance = false; |
24 | 30 |
|
@@ -59,6 +65,13 @@ public static function init() { |
59 | 65 |
|
60 | 66 | // Create custom post type. |
61 | 67 | add_action( 'init', array( self::instance(), 'create_job_queue' ) ); |
| 68 | + |
| 69 | + // Handle activation notice. |
| 70 | + register_activation_hook( __FILE__, array( self::instance(), 'set_activation_notice_flag' ) ); |
| 71 | + add_action( 'admin_notices', array( self::instance(), 'maybe_show_activation_notice' ) ); |
| 72 | + |
| 73 | + // Add plugin action link. |
| 74 | + add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( self::instance(), 'filter_plugin_links' ) ); |
62 | 75 | } |
63 | 76 |
|
64 | 77 | /** |
@@ -100,7 +113,7 @@ function get_phpversions() { |
100 | 113 | * @return null |
101 | 114 | */ |
102 | 115 | function start_test() { |
103 | | - if ( current_user_can( 'manage_options' ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
| 116 | + if ( current_user_can( self::CAPABILITY ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
104 | 117 | global $wpdb; |
105 | 118 |
|
106 | 119 | $wpephpc = new WPEPHPCompat( dirname( __FILE__ ) ); |
@@ -144,7 +157,7 @@ function start_test() { |
144 | 157 | * @return null |
145 | 158 | */ |
146 | 159 | function check_status() { |
147 | | - if ( current_user_can( 'manage_options' ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
| 160 | + if ( current_user_can( self::CAPABILITY ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
148 | 161 | $scan_status = get_option( 'wpephpcompat.status' ); |
149 | 162 | $count_jobs = wp_count_posts( 'wpephpcompat_jobs' ); |
150 | 163 | $total_jobs = get_option( 'wpephpcompat.numdirs' ); |
@@ -230,7 +243,7 @@ function fork_scan( $test_version, $only_active ) { |
230 | 243 | * @action wp_ajax_wpephpcompat_clean_up |
231 | 244 | */ |
232 | 245 | function clean_up() { |
233 | | - if ( current_user_can( 'manage_options' ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
| 246 | + if ( current_user_can( self::CAPABILITY ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
234 | 247 | $wpephpc = new WPEPHPCompat( dirname( __FILE__ ) ); |
235 | 248 | $wpephpc->clean_after_scan(); |
236 | 249 | delete_option( 'wpephpcompat.scan_results' ); |
@@ -307,7 +320,7 @@ function admin_enqueue( $hook ) { |
307 | 320 | */ |
308 | 321 | function create_menu() { |
309 | 322 | // Create Tools sub-menu. |
310 | | - $this->page = add_submenu_page( 'tools.php', __( 'PHP Compatibility', 'php-compatibility-checker' ), __( 'PHP Compatibility', 'php-compatibility-checker' ), 'manage_options', 'php-compatibility-checker', array( self::instance(), 'settings_page' ) ); |
| 323 | + $this->page = add_submenu_page( 'tools.php', __( 'PHP Compatibility', 'php-compatibility-checker' ), __( 'PHP Compatibility', 'php-compatibility-checker' ), self::CAPABILITY, self::ADMIN_PAGE_SLUG, array( self::instance(), 'settings_page' ) ); |
311 | 324 | } |
312 | 325 |
|
313 | 326 | /** |
@@ -466,6 +479,68 @@ function settings_page() { |
466 | 479 | </script> |
467 | 480 | <?php |
468 | 481 | } |
| 482 | + |
| 483 | + /** |
| 484 | + * Sets the activation notice flag so that it is shown in the admin. |
| 485 | + * |
| 486 | + * @since 1.4.4 |
| 487 | + */ |
| 488 | + function set_activation_notice_flag() { |
| 489 | + add_option( 'wpephpcompat.show_notice', true ); |
| 490 | + } |
| 491 | + |
| 492 | + /** |
| 493 | + * Shows the activation notice if the flag for it is set. |
| 494 | + * |
| 495 | + * @since 1.4.4 |
| 496 | + */ |
| 497 | + function maybe_show_activation_notice() { |
| 498 | + $option = get_option( 'wpephpcompat.show_notice' ); |
| 499 | + |
| 500 | + if ( ! $option ) { |
| 501 | + return; |
| 502 | + } |
| 503 | + |
| 504 | + delete_option( 'wpephpcompat.show_notice' ); |
| 505 | + |
| 506 | + if ( ! current_user_can( self::CAPABILITY ) ) { |
| 507 | + return; |
| 508 | + } |
| 509 | + |
| 510 | + $url = add_query_arg( 'page', self::ADMIN_PAGE_SLUG, admin_url( 'tools.php' ) ); |
| 511 | + |
| 512 | + ?> |
| 513 | + <div class="notice updated is-dismissible"> |
| 514 | + <p> |
| 515 | + <?php |
| 516 | + printf( |
| 517 | + /* translators: %s: URL to admin page */ |
| 518 | + __( 'You have just activated the <strong>PHP Compatibility Checker</strong>. <a href="%s">Start scanning your plugins and themes for compatibility with the latest PHP versions now!</a>', 'php-compatibility-checker' ), |
| 519 | + esc_url( $url ) |
| 520 | + ); |
| 521 | + ?> |
| 522 | + </p> |
| 523 | + </div> |
| 524 | + <?php |
| 525 | + } |
| 526 | + |
| 527 | + /** |
| 528 | + * Adds a link to the admin page to the plugin action links. |
| 529 | + * |
| 530 | + * @since 1.4.4 |
| 531 | + * |
| 532 | + * @param array $links Plugin action links. |
| 533 | + * @return array Modified plugin action links. |
| 534 | + */ |
| 535 | + function filter_plugin_links( $links ) { |
| 536 | + if ( current_user_can( self::CAPABILITY ) ) { |
| 537 | + $url = add_query_arg( 'page', self::ADMIN_PAGE_SLUG, admin_url( 'tools.php' ) ); |
| 538 | + |
| 539 | + array_unshift( $links, '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Start Scan', 'php-compatibility-checker' ) . '</a>' ); |
| 540 | + } |
| 541 | + |
| 542 | + return $links; |
| 543 | + } |
469 | 544 | } |
470 | 545 | // Register the WPEngine_PHPCompat instance |
471 | 546 | WPEngine_PHPCompat::init(); |
0 commit comments