Skip to content

Commit f438272

Browse files
committed
Check and deactivate correctly if PHP req. not met
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
1 parent ebb31c7 commit f438272

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

genesis-js-no-js.php

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,40 @@
3838
}
3939

4040
if ( version_compare( PHP_VERSION, '7.0', '<' ) ) {
41-
if ( current_user_can( 'activate_plugins' ) ) {
42-
add_action( 'admin_init', 'genesis_js_no_js_deactivate' );
43-
add_action( 'admin_notices', 'genesis_js_no_js_deactivation_notice' );
41+
add_action( 'plugins_loaded', 'genesis_js_no_js_init_deactivation' );
4442

45-
/**
46-
* Deactivate the plugin.
47-
*/
48-
function genesis_js_no_js_deactivate() {
49-
deactivate_plugins( plugin_basename( __FILE__ ) );
43+
/**
44+
* Initialise deactivation functions.
45+
*/
46+
function genesis_js_no_js_init_deactivation() {
47+
if ( current_user_can( 'activate_plugins' ) ) {
48+
add_action( 'admin_init', 'genesis_js_no_js_deactivate' );
49+
add_action( 'admin_notices', 'genesis_js_no_js_deactivation_notice' );
5050
}
51+
}
52+
53+
/**
54+
* Deactivate the plugin.
55+
*/
56+
function genesis_js_no_js_deactivate() {
57+
deactivate_plugins( plugin_basename( __FILE__ ) );
58+
}
5159

52-
/**
53-
* Show deactivation admin notice.
54-
*/
55-
function genesis_js_no_js_deactivation_notice() {
56-
$notice = sprintf(
57-
// Translators: 1: Required PHP version, 2: Current PHP version.
58-
'<strong>Plugin name</strong> requires PHP %1$s to run. This site uses %2$s, so the plugin has been <strong>deactivated</strong>.',
59-
'7.0',
60-
PHP_VERSION
61-
);
62-
?>
63-
<div class="updated"><p><?php echo wp_kses_post( $notice ); ?></p></div>
64-
<?php
65-
if ( isset( $_GET['activate'] ) ) { // WPCS: input var okay, CSRF okay.
66-
unset( $_GET['activate'] ); // WPCS: input var okay.
67-
}
60+
/**
61+
* Show deactivation admin notice.
62+
*/
63+
function genesis_js_no_js_deactivation_notice() {
64+
$notice = sprintf(
65+
// Translators: 1: Required PHP version, 2: Current PHP version.
66+
'<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>.',
67+
'7.0',
68+
PHP_VERSION
69+
);
70+
?>
71+
<div class="updated"><p><?php echo wp_kses_post( $notice ); ?></p></div>
72+
<?php
73+
if ( isset( $_GET['activate'] ) ) { // WPCS: input var okay, CSRF okay.
74+
unset( $_GET['activate'] ); // WPCS: input var okay.
6875
}
6976
}
7077

0 commit comments

Comments
 (0)