Skip to content
Snippets Groups Projects
Commit 5ba9d1d5 authored by Krzysiek's avatar Krzysiek
Browse files

Requirement checker class

parent ed8d5935
Branches
Tags
1 merge request!1Devel 1.10 - Wymagana wersja PHP i Wordpress
......@@ -4,7 +4,9 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
require_once __DIR__ . 'vendor/autoload.php';
require_once __DIR__ . '/vendor/autoload.php';
require_once( 'classes/wpdesk/class-plugin.php' );
require_once( 'classes/plugin-template-plugin.php' );
$wpdesk_plugin_template_plugin_data = array(
'plugin' => plugin_basename( __FILE__ ),
......@@ -13,4 +15,5 @@ $wpdesk_plugin_template_plugin_data = array(
'config_uri' => admin_url( 'edit.php?post_type=inspire_invoice&page=plugin_template' )
);
require_once( 'classes/wpdesk/class-plugin.php' );
\ No newline at end of file
(new WPDesk_Plugin_Template_Plugin( __FILE__, $wpdesk_plugin_template_plugin_data ));
<?php
require_once( 'classes/wpdesk/class-plugin.php' );
require_once( 'wpdesk/class-plugin.php' );
class WPDesk_Plugin_Template_Plugin extends WPDesk_Plugin_1_10 {
/**
* WPDesk_Plugin_Template_Plugin constructor.
*
* @param array $plugin_data
*/
public function __construct( $base_file, $plugin_data ) {
$this->plugin_namespace = 'plugin-template';
$this->plugin_text_domain = 'plugin-template';
......@@ -19,7 +16,7 @@ class WPDesk_Plugin_Template_Plugin extends WPDesk_Plugin_1_10 {
if ( is_array( $plugin_data ) && count( $plugin_data ) ) {
if ( ! class_exists( 'WPDesk_Helper_Plugin' ) ) {
require_once( 'classes/wpdesk/class-helper.php' );
require_once( 'wpdesk/class-helper.php' );
add_filter( 'plugins_api', array( $this, 'wpdesk_helper_install' ), 10, 3 );
add_action( 'admin_notices', array( $this, 'wpdesk_helper_notice' ) );
}
......@@ -32,7 +29,7 @@ class WPDesk_Plugin_Template_Plugin extends WPDesk_Plugin_1_10 {
parent::__construct( $base_file, $plugin_data );
if ( $this->plugin_is_active() ) {
require_once 'classes/class-plugin-template-settings-hooks.php';
require_once 'class-plugin-template-settings-hooks.php';
$this->settings_hooks = new WPDesk_Plugin_Template_Settings_Hooks( $this );
$this->settings_hooks->hooks();
$this->init();
......@@ -98,14 +95,4 @@ class WPDesk_Plugin_Template_Plugin extends WPDesk_Plugin_1_10 {
}
echo '<div class="error fade"><p>' . $message . '</p></div>' . "\n";
}
}
function wpdesk_plugin_template() {
global $wpdesk_plugin_template;
global $wpdesk_plugin_template_plugin_data;
if ( ! isset( $wpdesk_plugin_template ) ) {
$wpdesk_plugin_template = new WPDesk_Plugin_Template_Plugin( __FILE__, $wpdesk_plugin_template_plugin_data );
}
}
\ No newline at end of file
......@@ -8,14 +8,21 @@ if ( ! class_exists( 'WPDesk_Helper_Plugin' ) ) {
class WPDesk_Helper_Plugin {
/** @var array */
protected $plugin_data;
/** @var string */
protected $text_domain;
/** @var string */
protected $ame_activated_key;
/** @var string */
protected $ame_activation_tab_key;
/**
* @param array $plugin_data
*/
function __construct( $plugin_data ) {
global $wpdesk_helper_plugins;
......@@ -31,6 +38,9 @@ if ( ! class_exists( 'WPDesk_Helper_Plugin' ) ) {
}
/**
* @return void
*/
public function inactive_notice() { ?>
<?php if ( ! current_user_can( 'manage_options' ) ) {
return;
......@@ -44,7 +54,11 @@ if ( ! class_exists( 'WPDesk_Helper_Plugin' ) ) {
<?php
}
/**
* @param bool $add_notice
*
* @return bool
*/
function is_active( $add_notice = false ) {
if ( get_option( $this->ame_activated_key, '0' ) != 'Activated' ) {
if ( $add_notice ) {
......@@ -58,5 +72,4 @@ if ( ! class_exists( 'WPDesk_Helper_Plugin' ) ) {
}
}
}
......@@ -20,27 +20,48 @@ if ( ! class_exists( 'WPDesk_Plugin_1_10' ) ) {
const VERSION = '1.10';
/** @var bool */
protected $plugin_is_active = true;
/** @var array */
protected $default_view_args;
/** @var string */
public $plugin_namespace = 'wpdesk_plugin';
/** @var string */
public $plugin_text_domain = 'wpdesk-plugin';
/** @var bool */
public $plugin_has_settings = false;
/** @var string */
public $plugin_path;
/** @var string */
public $template_path;
/** @var string */
public $plugin_file_path;
/** @var string */
public $plugin_url;
/** @var bool */
public $settings_url = false;
/** @var bool */
public $docs_url = false;
/** @var string */
public $default_settings_tab = 'general';
public $settings_hooks = null;
/** @var WPDesk_Settings_1_10 */
public $settings = null;
/** @var array */
public $options = null;
protected function __construct( $base_file, $plugin_data = false ) {
......@@ -54,33 +75,54 @@ if ( ! class_exists( 'WPDesk_Plugin_1_10' ) ) {
$this->hooks();
}
/**
* @return bool
*/
public function plugin_is_active() {
return $this->plugin_is_active;
}
/**
* @return WPDesk_Settings_1_10
*/
public function get_settings() {
return $this->settings;
}
/**
* @param $key
* @param $default
*
* @return mixed
*/
public function get_option( $key, $default ) {
return $this->settings->get_option( $key, $default );
}
/**
* @return $this
*/
public function get_plugin() {
return $this;
}
/**
* @return string
*/
public function get_text_domain() {
return $this->plugin_text_domain;
}
/**
* @return void
*/
public function load_plugin_text_domain() {
$wpdesk_translation = load_plugin_textdomain( 'wpdesk-plugin', false, $this->get_namespace() . '/classes/wpdesk/lang/' );
$plugin_translation = load_plugin_textdomain( $this->get_text_domain(), false, $this->get_namespace() . '/lang/' );
}
/**
*
* @param string $base_file
*/
public function init_base_variables( $base_file ) {
// Set Plugin Path
......@@ -100,7 +142,7 @@ if ( ! class_exists( 'WPDesk_Plugin_1_10' ) ) {
}
/**
*
* @return void
*/
public function hooks() {
......@@ -136,10 +178,16 @@ if ( ! class_exists( 'WPDesk_Plugin_1_10' ) ) {
return trailingslashit( $this->template_path );
}
/**
* @return string
*/
public function get_plugin_file_path() {
return $this->plugin_file_path;
}
/**
* @return string
*/
public function get_namespace() {
return $this->plugin_namespace;
}
......
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'WPDesk_Requirement_Checker_1_10' ) ) {
/**
......@@ -12,31 +14,148 @@ if ( ! class_exists( 'WPDesk_Requirement_Checker_1_10' ) ) {
private $plugin_name = '';
/** @var string */
private $php = '5.2.4';
private $plugin_file = '';
/** @var string */
private $wp = '3.8';
private $min_php_version;
/** @var string */
private $file;
private $min_wp_version;
/** @var string */
private $title;
private $min_wc_version;
/** @var array */
private $plugin_require;
/** @var array */
private $module_require;
/** @var array */
private $setting_require;
/** @var array */
private $notices;
/**
* Requirement_Checker constructor.
*
* @param string $plugin_file
* @param string $php_version
* @param string $wp_version
* @param $plugin_file
* @param $php_version
* @param $wp_version
* @param string|null $wc_version
*/
public function __construct( $plugin_file, $php_version, $wp_version ) {
public function __construct( $plugin_file, $php_version, $wp_version, $wc_version = null ) {
$this->plugin_file = $plugin_file;
$this->plugin_name = $this->extract_plugin_title_from_header( $plugin_file );
$this->php = $php_version;
$this->wp = $wp_version;
$this->title = $this->extract_plugin_title_from_header( $plugin_file );
$this->set_min_php_require( $php_version );
$this->set_min_wp_require( $wp_version );
$this->set_min_wc_require( $wc_version );
$this->plugin_require = array();
$this->module_require = array();
$this->setting_require = array();
$this->notices = array();
}
/**
* Should be used when you want to check if others plugin are loaded (WC for example)
*/
public function check_requirements_and_load_plugin_deferred() {
add_action( "plugins_loaded", [ $this, "check_requirements_and_load_plugin" ] );
}
/**
* Checks if requirements are met and loads bootstrap
*/
public function check_requirements_and_load_plugin() {
if ( $this->is_requirements_met() ) {
require_once plugin_dir_path( $this->plugin_file ) . DIRECTORY_SEPARATOR . 'bootstrap.php';
} else {
$this->disable_plugin_render_notice();
}
}
/**
* @param string $version
*
* @return $this
*/
public function set_min_php_require( $version ) {
$this->min_php_version = $version;
return $this;
}
/**
* @param string $version
*
* @return $this
*/
public function set_min_wp_require( $version ) {
$this->min_wp_version = $version;
return $this;
}
/**
* @param string $version
*
* @return $this
*/
public function set_min_wc_require( $version ) {
$this->min_wc_version = $version;
return $this;
}
/**
* @param string $plugin_name
* @param string $nice_plugin_name Nice plugin name for better looks in notice
*
* @return $this
*/
public function add_plugin_require( $plugin_name, $nice_plugin_name = null ) {
if ( is_null( $nice_plugin_name ) ) {
$this->plugin_require[ $plugin_name ] = $plugin_name;
} else {
$this->plugin_require[ $plugin_name ] = $nice_plugin_name;
}
return $this;
}
/**
* @param string $module_name
* @param string $nice_name Nice module name for better looks in notice
*
* @return $this
*/
public function add_php_module_require( $module_name, $nice_name = null ) {
if ( is_null( $nice_name ) ) {
$this->module_require[ $module_name ] = $module_name;
} else {
$this->module_require[ $module_name ] = $nice_name;
}
return $this;
}
/**
* @param string $setting
* @param mixed $value
*
* @return $this
*/
public function add_php_setting_require( $setting, $value ) {
$this->setting_require[ $setting ] = $value;
return $this;
}
/**
* @param string $file
*
* @return string mixed
*/
private function extract_plugin_title_from_header( $file ) {
$plugin_data = get_file_data( $file, [
'title' => 'Plugin Name'
......@@ -45,59 +164,152 @@ if ( ! class_exists( 'WPDesk_Requirement_Checker_1_10' ) ) {
return $plugin_data['title'];
}
public function passes() {
$passes = $this->php_passes() && $this->wp_passes();
if ( ! $passes ) {
add_action( 'admin_notices', [ $this, 'deactivate' ] );
}
/**
* @return bool
*/
public function is_requirements_met() {
$this->notices = $this->prepare_requirement_notices();
return $passes;
return count( $this->notices ) === 0;
}
public function deactivate() {
if ( isset( $this->file ) ) {
deactivate_plugins( plugin_basename( $this->file ) );
/**
* @return array
*/
private function prepare_requirement_notices() {
$notices = array();
if ( ! $this->is_php_at_least( $this->min_php_version ) ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The &#8220;%s&#8221; plugin cannot run on PHP versions older than %s. Please contact your host and ask them to upgrade.' ), esc_html( $this->plugin_name ), $this->min_php_version ) );
}
if ( ! $this->is_wp_at_least( $this->min_wp_version ) ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The &#8220;%s&#8221; plugin cannot run on WordPress versions older than %s. Please update WordPress.' ), esc_html( $this->plugin_name ), $this->min_wp_version ) );
}
if ( ! is_null( $this->min_wc_version ) && ! $this->is_wc_at_least( $this->min_wc_version ) ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The &#8220;%s&#8221; plugin cannot run on WooCommerce versions older than %s. Please update WooCommerce.' ), esc_html( $this->plugin_name ), $this->min_wc_version ) );
}
if ( count( $this->plugin_require ) > 0 ) {
foreach ( $this->plugin_require as $plugin_name => $nice_plugin_name ) {
if ( ! $this->is_wp_plugin_active( $plugin_name ) ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The &#8220;%s&#8221; plugin cannot run without %s active. Please install and activate %s plugin.' ), esc_html( $this->plugin_name ), esc_html( basename( $nice_plugin_name ) ), esc_html( basename( $nice_plugin_name ) ) ) );
}
}
}
if ( count( $this->module_require ) > 0 ) {
foreach ( $this->module_require as $module_name => $nice_module_name ) {
if ( ! $this->is_module_active( $module_name ) ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The &#8220;%s&#8221; plugin cannot run without %s php module installed. Please contact your host and ask them to install %s.' ), esc_html( $this->plugin_name ), esc_html( basename( $nice_module_name ) ), esc_html( basename( $nice_module_name ) ) ) );
}
}
}
if ( count( $this->setting_require ) > 0 ) {
foreach ( $this->setting_require as $setting => $value ) {
if ( ! $this->is_setting_set( $setting, $value ) ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The &#8220;%s&#8221; plugin cannot run without %s php setting set to %s. Please contact your host and ask them to set %s.' ), esc_html( $this->plugin_name ), esc_html( basename( $setting ) ), esc_html( basename( $value ) ), esc_html( basename( $setting ) ) ) );
}
}
}
private function php_passes() {
if ( $this->__php_at_least( $this->php ) ) {
return true;
} else {
add_action( 'admin_notices', [ $this, 'php_version_notice' ] );
return $notices;
}
return false;
/**
* @param string $name
* @param mixed $value
*
* @return bool
*/
private function is_setting_set( $name, $value ) {
return ini_get( $name ) === strval( $value );
}
/**
* @param string $name
*
* @return bool
*/
private function is_module_active( $name ) {
return extension_loaded( $name );
}
private static function __php_at_least( $min_version ) {
return version_compare( phpversion(), $min_version, '>=' );
/**
* @return void
*/
public function disable_plugin_render_notice() {
add_action( 'admin_notices', [ $this, 'deactivate_action' ] );
add_action( 'admin_notices', [ $this, 'render_notices_action' ] );
}
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>';
/**
* Shoud be called as WordPress action
*
* @return void
*/
public function render_notices_action() {
foreach ( $this->notices as $notice ) {
echo $notice;
}
}
private function wp_passes() {
if ( $this->__wp_at_least( $this->wp ) ) {
return true;
} else {
add_action( 'admin_notices', [ $this, 'wp_version_notice' ] );
/**
* Prepares message in html format
*
* @param string $message
*
* @return string
*/
private function prepare_notice_message( $message ) {
return '<div class="error"><p>' . $message . '</p></div>';
}
return false;
/**
* @return void
*/
public function deactivate_action() {
if ( isset( $this->plugin_file ) ) {
deactivate_plugins( plugin_basename( $this->plugin_file ) );
}
}
private static function __wp_at_least( $min_version ) {
/**
* @param $min_version
*
* @return mixed
*/
private function is_php_at_least( $min_version ) {
return version_compare( phpversion(), $min_version, '>=' );
}
/**
* @param string $min_version
*
* @return bool
*/
private function is_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>';
/**
* Checks if plugin is active. Needs to be enabled in deferred way.
*
* @param string $plugin_name
*
* @return bool
*/
private function is_wp_plugin_active( $plugin_name ) {
return in_array( $plugin_name, apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
}
/**
* Checks if plugin is active and have designated version. Needs to be enabled in deferred way.
*
* @param string $min_version
*
* @return bool
*/
private function is_wc_at_least( $min_version ) {
return class_exists( 'WooCommerce' ) &&
$this->is_wp_plugin_active( 'woocommerce/woocommerce.php' ) &&
version_compare( WooCommerce::instance()->version, $min_version, '>=' );
}
}
}
......@@ -28,18 +28,21 @@
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
require_once( dirname( __FILE__ ) . '/classes/wpdesk/class-requirement-checker.php' );
$requirements_check = new WPDesk_Requirement_Checker_1_10(
$requirements_checker = new WPDesk_Requirement_Checker_1_10(
__FILE__,
'7.3',
'4.4'
'5.5',
'4.5',
'2.6.14'
);
$requirements_checker
->add_plugin_require('hello.php', 'Hello Dolly')
->add_php_module_require('curl' )
->add_php_setting_require('allow_url_fopen', 1);
if ( $requirements_check->passes() ) {
require_once 'classes/bootstrap.php';
}
unset( $your_plugin_requirements_check );
$requirements_checker->check_requirements_and_load_plugin_deferred();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment