Skip to content

Commit bd15b48

Browse files
author
Felix Arntz
committed
Show a plugin action link and activation notice link that lead to the admin page.
1 parent 6ba1bdc commit bd15b48

File tree

1 file changed

+79
-4
lines changed

1 file changed

+79
-4
lines changed

wpengine-phpcompat.php

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
*/
2020
class WPEngine_PHPCompat {
2121

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+
2228
/* Define and register singleton */
2329
private static $instance = false;
2430

@@ -59,6 +65,13 @@ public static function init() {
5965

6066
// Create custom post type.
6167
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' ) );
6275
}
6376

6477
/**
@@ -100,7 +113,7 @@ function get_phpversions() {
100113
* @return null
101114
*/
102115
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 ) ) {
104117
global $wpdb;
105118

106119
$wpephpc = new WPEPHPCompat( dirname( __FILE__ ) );
@@ -144,7 +157,7 @@ function start_test() {
144157
* @return null
145158
*/
146159
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 ) ) {
148161
$scan_status = get_option( 'wpephpcompat.status' );
149162
$count_jobs = wp_count_posts( 'wpephpcompat_jobs' );
150163
$total_jobs = get_option( 'wpephpcompat.numdirs' );
@@ -230,7 +243,7 @@ function fork_scan( $test_version, $only_active ) {
230243
* @action wp_ajax_wpephpcompat_clean_up
231244
*/
232245
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 ) ) {
234247
$wpephpc = new WPEPHPCompat( dirname( __FILE__ ) );
235248
$wpephpc->clean_after_scan();
236249
delete_option( 'wpephpcompat.scan_results' );
@@ -307,7 +320,7 @@ function admin_enqueue( $hook ) {
307320
*/
308321
function create_menu() {
309322
// 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' ) );
311324
}
312325

313326
/**
@@ -466,6 +479,68 @@ function settings_page() {
466479
</script>
467480
<?php
468481
}
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+
}
469544
}
470545
// Register the WPEngine_PHPCompat instance
471546
WPEngine_PHPCompat::init();

0 commit comments

Comments
 (0)