Skip to content
Snippets Groups Projects

Feature/transients

Merged Grzegorz Rola requested to merge feature/transients into master

Files

+ 84
42
@@ -10,41 +10,64 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* have to be compatible with PHP 5.3.x
*/
class WPDesk_Basic_Requirement_Checker implements WPDesk_Requirement_Checker {
const EXTENSION_NAME_OPENSSL = 'openssl';
const HOOK_ADMIN_NOTICES_ACTION = 'admin_notices';
const HOOK_PLUGIN_DEACTIVATED_ACTION = 'deactivated_plugin';
const HOOK_PLUGIN_ACTIVATED_ACTION = 'activated_plugin';
const PLUGIN_INFO_KEY_NICE_NAME = 'nice_name';
const PLUGIN_INFO_KEY_NAME = 'name';
const PLUGIN_INFO_VERSION = 'version';
const PLUGIN_INFO_FAKE_REQUIRED_MINIMUM_VERSION = '0.0';
const PLUGIN_INFO_APPEND_PLUGIN_DATA = 'required_version';
const PLUGIN_INFO_TRANSIENT_NAME = 'require_plugins_data';
const PLUGIN_INFO_TRANSIENT_EXPIRATION_TIME = 16;
const EXPIRATION_TRANSIENT_NAME = 'require_plugins_data_exp';
const CACHE_TIME = 300;
/** @var string */
protected $plugin_name;
/** @var string */
private $plugin_file;
/** @var string */
private $min_php_version;
/** @var string */
private $min_wp_version;
/** @var string|null */
private $min_wc_version = null;
private $min_wc_version = NULL;
/** @var int|null */
private $min_openssl_version = null;
private $min_openssl_version = NULL;
/** @var array */
protected $plugin_require;
/** @var bool */
protected $should_check_plugin_versions = false;
protected $should_check_plugin_versions = FALSE;
/** @var array */
private $module_require;
/** @var array */
private $setting_require;
/** @var array */
protected $notices;
/** @var @string */
private $text_domain;
@@ -120,14 +143,14 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
*
* @return $this
*/
public function add_plugin_require( $plugin_name, $nice_plugin_name = null, $plugin_require_version = null ) {
public function add_plugin_require( $plugin_name, $nice_plugin_name = NULL, $plugin_require_version = NULL ) {
if ( $plugin_require_version ) {
$this->should_check_plugin_versions = true;
$this->should_check_plugin_versions = TRUE;
}
$this->plugin_require[ $plugin_name ] = array(
self::PLUGIN_INFO_KEY_NAME => $plugin_name,
self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === null ? $plugin_name : $nice_plugin_name,
self::PLUGIN_INFO_VERSION => $plugin_require_version === null ?
self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === NULL ? $plugin_name : $nice_plugin_name,
self::PLUGIN_INFO_VERSION => $plugin_require_version === NULL ?
self::PLUGIN_INFO_FAKE_REQUIRED_MINIMUM_VERSION : $plugin_require_version,
);
@@ -143,12 +166,12 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
*
* @return $this
*/
public function add_plugin_repository_require( $plugin_name, $version, $nice_plugin_name = null ) {
public function add_plugin_repository_require( $plugin_name, $version, $nice_plugin_name = NULL ) {
$this->plugin_require[ $plugin_name ] = array(
self::PLUGIN_INFO_KEY_NAME => $plugin_name,
self::PLUGIN_INFO_VERSION => $version,
'repository_url' => 'http://downloads.wordpress.org/plugin/' . dirname( $plugin_name ) . '.latest-stable.zip',
self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === null ? $plugin_name : $nice_plugin_name
self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === NULL ? $plugin_name : $nice_plugin_name
);
return $this;
@@ -160,8 +183,8 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
*
* @return $this
*/
public function add_php_module_require( $module_name, $nice_name = null ) {
if ( $nice_name === null ) {
public function add_php_module_require( $module_name, $nice_name = NULL ) {
if ( $nice_name === NULL ) {
$this->module_require[ $module_name ] = $module_name;
} else {
$this->module_require[ $module_name ] = $nice_name;
@@ -206,11 +229,11 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on WordPress versions older than %s. Please update WordPress.',
$this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_wp_version ) );
}
if ( $this->min_wc_version !== null && $this->can_check_plugin_version() && ! self::is_wc_at_least( $this->min_wc_version ) ) {
if ( $this->min_wc_version !== NULL && $this->can_check_plugin_version() && ! self::is_wc_at_least( $this->min_wc_version ) ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run on WooCommerce versions older than %s. Please update WooCommerce.',
$this->get_text_domain() ), esc_html( $this->plugin_name ), $this->min_wc_version ) );
}
if ( $this->min_openssl_version !== null && ! self::is_open_ssl_at_least( $this->min_openssl_version ) ) {
if ( $this->min_openssl_version !== NULL && ! self::is_open_ssl_at_least( $this->min_openssl_version ) ) {
$notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin cannot run without OpenSSL module version at least %s. Please update OpenSSL module.',
$this->get_text_domain() ), esc_html( $this->plugin_name ),
'0x' . dechex( $this->min_openssl_version ) ) );
@@ -324,29 +347,32 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @return array In format [ 'plugindir/pluginfile.php' => ['Name' => 'Plugin Name', 'Version' => '1.0.1', ...], ]
*/
private static function retrieve_plugins_data_in_transient() {
static $never_executed = true;
if ( $never_executed ) {
$never_executed = false;
/** Required when WC starts later and these data should be in cache */
add_filter( 'extra_plugin_headers', function ( $headers = array() ) {
$headers[] = 'WC tested up to';
$headers[] = 'WC requires at least';
$headers[] = 'Woo';
return array_unique( $headers );
} );
}
$plugins = get_transient( self::PLUGIN_INFO_TRANSIENT_NAME );
$current_time = time();
$plugins = get_transient( self::PLUGIN_INFO_TRANSIENT_NAME );
$expiration_time = get_transient( self::EXPIRATION_TRANSIENT_NAME );
$is_expired = ! $expiration_time || $current_time > $expiration_time;
if ( $plugins === FALSE || $is_expired ) {
static $never_executed = TRUE;
if ( $never_executed ) {
$never_executed = FALSE;
/** Required when WC starts later and these data should be in cache */
add_filter( 'extra_plugin_headers', function( $headers = array() ) {
$headers[] = 'WC tested up to';
$headers[] = 'WC requires at least';
$headers[] = 'Woo';
return array_unique( $headers );
} );
}
if ( $plugins === false ) {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
$plugins = function_exists( 'get_plugins' ) ? get_plugins() : array();
set_transient( self::PLUGIN_INFO_TRANSIENT_NAME, $plugins,
self::PLUGIN_INFO_TRANSIENT_EXPIRATION_TIME );
set_transient( self::PLUGIN_INFO_TRANSIENT_NAME, $plugins, 0 );
set_transient( self::EXPIRATION_TRANSIENT_NAME, $current_time + self::CACHE_TIME, 0 );
}
return $plugins;
@@ -358,7 +384,6 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @return array In format [ 'plugindir/pluginfile.php' => ['Name' => 'Plugin Name', 'Version' => '1.0.1', 'required_version' => '1.0.2']... ]
*/
private function retrieve_required_plugins_data() {
$require_plugins = array();
$plugins = self::retrieve_plugins_data_in_transient();
if ( is_array( $plugins ) ) {
@@ -388,7 +413,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
private function append_plugin_require_notices( $notices ) {
if ( count( $this->plugin_require ) > 0 ) {
foreach ( $this->plugin_require as $plugin_name => $plugin_info ) {
$notice = null;
$notice = NULL;
if ( isset( $plugin_info['repository_url'] ) ) {
$notice = $this->prepare_plugin_repository_require_notice( $plugin_info );
} elseif ( ! self::is_wp_plugin_active( $plugin_name ) ) {
@@ -398,7 +423,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
esc_html( basename( $plugin_info[ self::PLUGIN_INFO_KEY_NICE_NAME ] ) ) ) );
}
if ( $notice !== null ) {
if ( $notice !== NULL ) {
$notices[] = $notice;
}
}
@@ -420,9 +445,9 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
if ( function_exists( 'wp_nonce_url' ) && function_exists( 'wp_create_nonce' ) ) {
$install_url = wp_nonce_url( $install_url, 'install-plugin_' . $slug );
}
add_filter( 'plugins_api', function ( $api, $action, $args ) use ( $plugin_info, $slug ) {
add_filter( 'plugins_api', function( $api, $action, $args ) use ( $plugin_info, $slug ) {
if ( 'plugin_information' !== $action ||
false !== $api ||
FALSE !== $api ||
! isset( $args->slug ) ||
$slug !== $args->slug
) {
@@ -467,7 +492,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
$this->plugin_name, $nice_name, esc_url( admin_url( $activate_url ) ), $nice_name ) );
}
return null;
return NULL;
}
/**
@@ -564,7 +589,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @deprecated use render_notices or disable_plugin
*/
public function disable_plugin_render_notice() {
add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'handle_render_notices_action' ) );
add_action( self::HOOK_ADMIN_NOTICES_ACTION, array(
$this,
'handle_render_notices_action'
) );
}
/**
@@ -573,7 +601,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @return void
*/
public function render_notices() {
add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'handle_render_notices_action' ) );
add_action( self::HOOK_ADMIN_NOTICES_ACTION, array(
$this,
'handle_render_notices_action'
) );
}
/**
@@ -582,7 +613,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @return void
*/
public function disable_plugin() {
add_action( self::HOOK_ADMIN_NOTICES_ACTION, array( $this, 'handle_deactivate_action' ) );
add_action( self::HOOK_ADMIN_NOTICES_ACTION, array(
$this,
'handle_deactivate_action'
) );
}
/**
@@ -604,8 +638,14 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @return void
*/
public function transient_delete_on_plugin_version_changed() {
add_action( self::HOOK_PLUGIN_DEACTIVATED_ACTION, array( $this, 'handle_transient_delete_action' ) );
add_action( self::HOOK_PLUGIN_ACTIVATED_ACTION, array( $this, 'handle_transient_delete_action' ) );
add_action( self::HOOK_PLUGIN_DEACTIVATED_ACTION, array(
$this,
'handle_transient_delete_action'
) );
add_action( self::HOOK_PLUGIN_ACTIVATED_ACTION, array(
$this,
'handle_transient_delete_action'
) );
}
/**
@@ -615,6 +655,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
*/
public function handle_transient_delete_action() {
delete_transient( self::PLUGIN_INFO_TRANSIENT_NAME );
delete_transient( self::EXPIRATION_TRANSIENT_NAME );
}
/**
@@ -629,5 +670,6 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
echo $notice;
}
}
}
}
Loading