diff --git a/app/Plugin.php b/app/Plugin.php index e04bf35..db180ab 100644 --- a/app/Plugin.php +++ b/app/Plugin.php @@ -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(); @@ -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 ); } @@ -95,9 +97,7 @@ 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' )); } @@ -105,79 +105,55 @@ public function load_dependencies() { * 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 (>= %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 . ' (>= ' . self::$config->get( 'dependencies/carbon_fields' ) . ' ' . __( 'required', self::$textdomain ) . ').' . ' ' . self::$config->get( 'plugin/meta/Name' ) . ' ' . __( '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( __( '%s: 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 = '