Skip to content

Commit

Permalink
Check and deactivate correctly if PHP req. not met
Browse files Browse the repository at this point in the history
While `current_user_can()` is available at the time the plugins are being loaded, one of the later functions, `wp_get_current_user()` is not.

This solution still checks for the PHP level at plugin parsing time, but if it's not sufficient, then functions are defined and hooked in to `plugins_loaded` to deactivate and show a notice.

Fixes #7
  • Loading branch information
GaryJones committed Nov 29, 2017
1 parent ebb31c7 commit f438272
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions genesis-js-no-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,40 @@
}

if ( version_compare( PHP_VERSION, '7.0', '<' ) ) {
if ( current_user_can( 'activate_plugins' ) ) {
add_action( 'admin_init', 'genesis_js_no_js_deactivate' );
add_action( 'admin_notices', 'genesis_js_no_js_deactivation_notice' );
add_action( 'plugins_loaded', 'genesis_js_no_js_init_deactivation' );

/**
* Deactivate the plugin.
*/
function genesis_js_no_js_deactivate() {
deactivate_plugins( plugin_basename( __FILE__ ) );
/**
* Initialise deactivation functions.
*/
function genesis_js_no_js_init_deactivation() {
if ( current_user_can( 'activate_plugins' ) ) {
add_action( 'admin_init', 'genesis_js_no_js_deactivate' );
add_action( 'admin_notices', 'genesis_js_no_js_deactivation_notice' );
}
}

/**
* Deactivate the plugin.
*/
function genesis_js_no_js_deactivate() {
deactivate_plugins( plugin_basename( __FILE__ ) );
}

/**
* Show deactivation admin notice.
*/
function genesis_js_no_js_deactivation_notice() {
$notice = sprintf(
// Translators: 1: Required PHP version, 2: Current PHP version.
'<strong>Plugin name</strong> requires PHP %1$s to run. This site uses %2$s, so the plugin has been <strong>deactivated</strong>.',
'7.0',
PHP_VERSION
);
?>
<div class="updated"><p><?php echo wp_kses_post( $notice ); ?></p></div>
<?php
if ( isset( $_GET['activate'] ) ) { // WPCS: input var okay, CSRF okay.
unset( $_GET['activate'] ); // WPCS: input var okay.
}
/**
* Show deactivation admin notice.
*/
function genesis_js_no_js_deactivation_notice() {
$notice = sprintf(
// Translators: 1: Required PHP version, 2: Current PHP version.
'<strong>Genesis JS / No JS</strong> requires PHP %1$s to run. This site uses %2$s, so the plugin has been <strong>deactivated</strong>.',
'7.0',
PHP_VERSION
);
?>
<div class="updated"><p><?php echo wp_kses_post( $notice ); ?></p></div>
<?php
if ( isset( $_GET['activate'] ) ) { // WPCS: input var okay, CSRF okay.
unset( $_GET['activate'] ); // WPCS: input var okay.
}
}

Expand Down

0 comments on commit f438272

Please sign in to comment.