Skip to content

Commit

Permalink
Refactored dependency check to use Kubitomakita/Requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
dmhendricks committed Sep 26, 2018
1 parent 99c6f6a commit e6911a6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 70 deletions.
112 changes: 44 additions & 68 deletions app/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ function __construct() {
*/
public function load_plugin() {

if( !$this->verify_dependencies( 'carbon_fields' ) ) return;
if( !$this->verify_dependencies() ) {
deactivate_plugins( self::$config->get( 'plugin/identifier' ) );
return;
}

// Add TGM Plugin Activation notices for required/recommended plugins
new TGMPA();
Expand Down Expand Up @@ -77,8 +80,7 @@ public function load_plugin() {
*/
public function activate() {

$dependency_check = $this->verify_dependencies( true, array( 'activate' => true, 'echo' => false ) );
if( $dependency_check !== true ) die( $dependency_check );
$this->verify_dependencies( true );

}

Expand All @@ -95,89 +97,63 @@ public function load_dependencies() {
new TGMPA();
}

if( $this->verify_dependencies( 'carbon_fields' ) === true ) {
add_action( 'carbon_fields_fields_registered', array( $this, 'load_plugin' ));
}
add_action( 'carbon_fields_fields_registered', array( $this, 'load_plugin' ));

}

/**
* Function to verify dependencies, such as if an outdated version of Carbon
* Fields is detected.
*
* Normally, we wouldn't be so persistant about checking for dependencies and
* I would just pass it off to TGMPA, however, if they have an ancient version
* of Carbon Fields loaded (via plugin or dependency), it causes problems.
*
* @param string|array|bool $deps A string (single) or array of deps to check. `true`
* checks all defined dependencies.
* @param array $args An array of arguments.
* @return bool|string Result of dependency check. Returns bool if $args['echo']
* is false, string if true.
* @param bool $die If true, plugin execution is halted with die(), useful for
* outputting error(s) in during activate()
* @return bool
* @since 0.2.0
*/
private function verify_dependencies( $deps = true, $args = array() ) {

if( is_bool( $deps ) && $deps ) $deps = self::$config->get( 'dependencies' );
if( !is_array( $deps ) ) $deps = array( $deps => self::$config->get( 'dependencies/' . $deps ) );

$args = ArrayHelper::set_default_atts( array(
'echo' => true,
'activate' => true
), $args);

$notices = array();

foreach( $deps as $dep => $version ) {

switch( $dep ) {

case 'php':

if( version_compare( phpversion(), $version, '<' ) ) {
$notices[] = __( 'This plugin is not supported on versions of PHP below', self::$textdomain ) . ' ' . self::$config->get( 'dependencies/php' ) . '.' ;
}
break;

case 'wordpress-toolkit':

$wordpress_toolkit_version = defined( '\WordPress_ToolKit\VERSION' ) ? \WordPress_ToolKit\VERSION : null;

if( !$wordpress_toolkit_version || version_compare( $wordpress_toolkit_version, $version, '<' ) ) {
$notices[] = sprintf( __( 'Unable to activate %s. An outdated version of WordPress ToolKit has been detected: %s (&gt;= %s required)', self::$textdomain ), self::$config->get( 'plugin/meta/Name' ), $wordpress_toolkit_version, $version );
}
break;

case 'carbon_fields':

$cf_version = defined('\\Carbon_Fields\\VERSION') ? current( explode( '-', \Carbon_Fields\VERSION ) ) : null;
if( !$args['activate'] && !defined('\\Carbon_Fields\\VERSION') ) {
$notices[] = __( 'An unknown error occurred while trying to load the Carbon Fields framework.', self::$textdomain );
} else if ( defined('\\Carbon_Fields\\VERSION') && version_compare( $cf_version, $version, '<' ) ) {
$notices[] = __( 'An outdated version of Carbon Fields has been detected:', self::$textdomain ) . ' ' . $cf_version . ' (&gt;= ' . self::$config->get( 'dependencies/carbon_fields' ) . ' ' . __( 'required', self::$textdomain ) . ').' . ' <strong>' . self::$config->get( 'plugin/meta/Name' ) . '</strong> ' . __( 'deactivated.', self::$textdomain ) ;
}
break;

}
private function verify_dependencies( $die = false ) {

// Check if underDEV_Requirements class is loaded
if( !class_exists( 'underDEV_Requirements' ) ) {
if( $die ) {
die( sprintf( __( '<strong>%s</strong>: One or more dependencies failed to load', self::$textdomain ), __( self::$config->get( 'plugin/meta/Name' ) ) ) );
} else {
return false;
}
}

if( $notices ) {
$requirements = new \underDEV_Requirements( __( self::$config->get( 'plugin/meta/Name' ), self::$textdomain ), self::$config->get( 'dependencies' ) );

deactivate_plugins( self::$config->get( 'plugin/identifier' ) );

$notices = '<ul><li>' . implode( "</li>\n<li>", $notices ) . '</li></ul>';
// Check for WordPress Toolkit
$requirements->add_check( 'wordpress-toolkit', function( $val, $res ) {
$wordpress_toolkit_version = defined( '\WordPress_ToolKit\VERSION' ) ? \WordPress_ToolKit\VERSION : null;
if( !$wordpress_toolkit_version ) {
$res->add_error( __( 'WordPress ToolKit not loaded.', self::$textdomain ) );
} else if( version_compare( $wordpress_toolkit_version, self::$config->get( 'dependencies/wordpress-toolkit' ), '<' ) ) {
$res->add_error( sprintf( __( 'An outdated version of WordPress ToolKit has been detected: %s (&gt;= %s required).', self::$textdomain ), $wordpress_toolkit_version, self::$config->get( 'dependencies/wordpress-toolkit' ) ) );
}
});

// Check for Carbon Fields
$requirements->add_check( 'carbon_fields', function( $val, $res ) {
$cf_version = defined('\\Carbon_Fields\\VERSION') ? current( explode( '-', \Carbon_Fields\VERSION ) ) : null;
if( !$cf_version ) {
$res->add_error( __( 'The Carbon Fields framework is not loaded.', self::$textdomain ) );
} else if( version_compare( $cf_version, self::$config->get( 'dependencies/carbon_fields' ), '>' ) ) {
$res->add_error( sprintf( __( 'An outdated version of Carbon Fields has been detected: %s (&gt;= %s required).', self::$textdomain ), $cf_version, self::$config->get( 'dependencies/carbon_fields' ) ) );
}
});

if( $args['echo'] ) {
Helpers::show_notice( $notices, [ 'type' => 'error', 'dismissible' => false ] );
return false;
// Display errors if requirements not met
if( !$requirements->satisfied() ) {
if( $die ) {
die( $requirements->notice() );
} else {
return $notices;
add_action( 'admin_notices', array( $requirements, 'notice' ) );
return false;
}

}

return !$notices;
return true;

}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"tgmpa/tgm-plugin-activation": "^2.6.1",
"tareq1988/wordpress-settings-api-class": "dev-master",
"dmhendricks/wordpress-toolkit": "0.4.0",
"inc2734/wp-customizer-framework": "^3.2.3",
"underdev/requirements": "^1.3",
"inc2734/wp-customizer-framework": "3.2.3",
"composer/installers": "^1.0.6"
},
"minimum-stability": "dev",
Expand Down
3 changes: 2 additions & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"expire": 86400
},
"dependencies": {
"php": "5.6.4",
"php": "5.6",
"wp": "4.7",
"carbon_fields": "2.2.0",
"wordpress-toolkit": "0.4.0"
},
Expand Down

0 comments on commit e6911a6

Please sign in to comment.