Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
/**
* Plugin Name: YOUR PLUGIN NAME
*/
include( dirname( __FILE__ ) . '/lib/requirements-check.php' );
$your_plugin_requirements_check = new YOUR_PREFIX_Requirements_Check( array(
'title' => 'YOUR PLUGIN NAME',
'php' => '5.4',
'wp' => '4.8',
'file' => __FILE__,
));
if ( $your_plugin_requirements_check->passes() ) {
// 1. Load classes or define an autoloader.
// 2. Initialize your plugin.
}
unset( $your_plugin_requirements_check );
<?php
class YOUR_PREFIX_Requirements_Check {
private $title = '';
private $php = '5.2.4';
private $wp = '3.8';
private $file;
public function __construct( $args ) {
foreach ( array( 'title', 'php', 'wp', 'file' ) as $setting ) {
if ( isset( $args[$setting] ) ) {
$this->$setting = $args[$setting];
}
}
}
public function passes() {
$passes = $this->php_passes() && $this->wp_passes();
if ( ! $passes ) {
add_action( 'admin_notices', array( $this, 'deactivate' ) );
}
return $passes;
}
public function deactivate() {
if ( isset( $this->file ) ) {
deactivate_plugins( plugin_basename( $this->file ) );
}
}
private function php_passes() {
if ( $this->__php_at_least( $this->php ) ) {
return true;
} else {
add_action( 'admin_notices', array( $this, 'php_version_notice' ) );
return false;
}
}
private static function __php_at_least( $min_version ) {
return version_compare( phpversion(), $min_version, '>=' );
}
public function php_version_notice() {
echo '<div class="error">';
echo "<p>The &#8220;" . esc_html( $this->title ) . "&#8221; plugin cannot run on PHP versions older than " . $this->php . '. Please contact your host and ask them to upgrade.</p>';
echo '</div>';
}
private function wp_passes() {
if ( $this->__wp_at_least( $this->wp ) ) {
return true;
} else {
add_action( 'admin_notices', array( $this, 'wp_version_notice' ) );
return false;
}
}
private static function __wp_at_least( $min_version ) {
return version_compare( get_bloginfo( 'version' ), $min_version, '>=' );
}
public function wp_version_notice() {
echo '<div class="error">';
echo "<p>The &#8220;" . esc_html( $this->title ) . "&#8221; plugin cannot run on WordPress versions older than " . $this->wp . '. Please update WordPress.</p>';
echo '</div>';
}
}
@thefrosty

This comment has been minimized.

Show comment Hide comment
@thefrosty

thefrosty Feb 21, 2018

Shouldn't __php_at_least and __wp_at_least not be static methods? You seem to be invoking them non-statically.

Shouldn't __php_at_least and __wp_at_least not be static methods? You seem to be invoking them non-statically.

@codex-m

This comment has been minimized.

Show comment Hide comment
@codex-m

codex-m Feb 22, 2018

@markjaquith I believe this one:

add_action( 'admin_notices', array( $this, 'deactivate' ) );

should be:

add_action( 'admin_init', array( $this, 'deactivate' ) );

Otherwise the plugin is not deactivated if it fails to meet requirements :)

codex-m commented Feb 22, 2018

@markjaquith I believe this one:

add_action( 'admin_notices', array( $this, 'deactivate' ) );

should be:

add_action( 'admin_init', array( $this, 'deactivate' ) );

Otherwise the plugin is not deactivated if it fails to meet requirements :)

@tomprosch1980

This comment has been minimized.

Show comment Hide comment
@tomprosch1980

tomprosch1980 Mar 20, 2018

Shouldn't __wp_at_least not be static methods? it should work.

Regards
tom
office.com/setup

Shouldn't __wp_at_least not be static methods? it should work.

Regards
tom
office.com/setup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment