Skip to content

Commit d2ffe31

Browse files
committed
Add user-friendly version check to bootstrap file
Most of the plugin uses syntax from PHP 7.1, so this is the minimum version. Provide a version check, admin notice and self-deactivation if the site is running an earlier version of PHP. The syntax in the main plugin file should be compatible with PHP 5.2.4, so that on attempted activation / update, it doesn't give a White Screen if the site is running on less than the required PHP version. The rest of the plugin initialisation is moved to `init.php`.
1 parent 943d483 commit d2ffe31

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

genesis-js-no-js.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22
/**
3-
* Genesis js / no-js WordPress plugin
3+
* Genesis JS / No JS WordPress plugin
44
*
55
* For child themes of the Genesis Framework.
66
*
7-
* Adds a no-js body class to the front-end, and a script on genesis_before
7+
* Adds a no-js body class to the front end, and a script on genesis_before
88
* which immediately changes the class to js if JavaScript is enabled. This is
9-
* how WP does things on the back-end, to allow different styles for the same
9+
* how WP does things on the back end, to allow different styles for the same
1010
* elements depending if JavaScript is active or not.
1111
*
12+
* This file should only use syntax available in PHP 5.2.4 or later.
13+
*
1214
* @package Gamajo\GenesisJsNoJs
1315
* @author Gary Jones
1416
* @copyright 2011 Gary Jones, Gamajo
@@ -26,16 +28,50 @@
2628
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
2729
* GitHub Plugin URI: https://github.com/GaryJones/genesis-js-no-js
2830
* GitHub Branch: master
31+
* Requires PHP: 7.1
32+
* Requires WP: 4.6
2933
*/
3034

35+
// If this file is called directly, abort.
36+
if ( ! defined( 'WPINC' ) ) {
37+
die;
38+
}
39+
3140
if ( version_compare( PHP_VERSION, '7.1', '<' ) ) {
41+
if ( current_user_can( 'activate_plugins' ) ) {
42+
add_action( 'admin_init', 'plugin_slug_deactivate' );
43+
add_action( 'admin_notices', 'plugin_slug_deactivation_notice' );
44+
45+
/**
46+
* Deactivate the plugin.
47+
*/
48+
function plugin_slug_deactivate() {
49+
deactivate_plugins( plugin_basename( __FILE__ ) );
50+
}
51+
52+
/**
53+
* Show deactivation admin notice.
54+
*/
55+
function plugin_slug_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.1',
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+
}
68+
}
69+
}
70+
3271
return false;
3372
}
3473

3574
/**
36-
* Load include files.
75+
* Load plugin initialisation file.
3776
*/
38-
require __DIR__ . '/src/GenesisJsNoJs.php';
39-
40-
$genesis_js_no_js = new Gamajo\GenesisJsNoJs\GenesisJsNoJs();
41-
$genesis_js_no_js->run();
77+
require plugin_dir_path( __FILE__ ) . '/init.php';

init.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Initialise the plugin
4+
*
5+
* This file can use syntax from the required level of PHP or later.
6+
*
7+
* @package Gamajo\GenesisJsNoJs
8+
* @author Gary Jones
9+
* @copyright 2011 Gary Jones, Gamajo
10+
* @license GPL-2.0+
11+
*/
12+
13+
declare( strict_types = 1 );
14+
15+
namespace Gamajo\GenesisJsNoJs;
16+
17+
// If this file is called directly, abort.
18+
if ( ! defined( 'WPINC' ) ) {
19+
die;
20+
}
21+
22+
/**
23+
* Load src files.
24+
*/
25+
require __DIR__ . '/src/GenesisJsNoJs.php';
26+
27+
$genesis_js_no_js = new GenesisJsNoJs();
28+
$genesis_js_no_js->run();

0 commit comments

Comments
 (0)