Skip to content
Snippets Groups Projects

Feature/transients

Merged Grzegorz Rola requested to merge feature/transients into master
2 unresolved threads

Files

+ 61
40
@@ -29,9 +29,9 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
const PLUGIN_INFO_APPEND_PLUGIN_DATA = 'required_version';
const PLUGIN_INFO_TRANSIENT_NAME = 'require_plugins_data';
const PLUGIN_INFO_TRANSIENT_NAME = 'wpdesk_plugins_data';
const EXPIRATION_TRANSIENT_NAME = 'require_plugins_data_exp';
const EXPIRATION_TRANSIENT_NAME = 'wpdesk_plugins_data_exp';
const CACHE_TIME = 300;
@@ -48,16 +48,16 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
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;
@@ -70,6 +70,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
/** @var @string */
private $text_domain;
/**
* @var mixed|true
*/
private $use_transients;
/**
* @param string $plugin_file
@@ -77,11 +81,13 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @param string $text_domain
* @param string $php_version
* @param string $wp_version
* @param bool $use_transients
*/
public function __construct( $plugin_file, $plugin_name, $text_domain, $php_version, $wp_version ) {
$this->plugin_file = $plugin_file;
$this->plugin_name = $plugin_name;
$this->text_domain = $text_domain;
public function __construct( $plugin_file, $plugin_name, $text_domain, $php_version, $wp_version, $use_transients = true ) {
$this->plugin_file = $plugin_file;
$this->plugin_name = $plugin_name;
$this->text_domain = $text_domain;
$this->use_transients = $use_transients;
$this->set_min_php_require( $php_version );
$this->set_min_wp_require( $wp_version );
@@ -143,14 +149,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,
);
@@ -166,12 +172,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;
@@ -183,8 +189,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;
@@ -229,11 +235,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 ) ) );
@@ -344,18 +350,25 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
/**
* Check the plugins directory and retrieve all plugin files with plugin data.
*
* @param bool $use_transients
*
* @return array In format [ 'plugindir/pluginfile.php' => ['Name' => 'Plugin Name', 'Version' => '1.0.1', ...], ]
*/
private static function retrieve_plugins_data_in_transient() {
private static function retrieve_plugins_data_in_transient( $use_transients = true ) {
$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 ( $use_transients) {
$plugins = get_transient( self::PLUGIN_INFO_TRANSIENT_NAME );
$expiration_time = get_transient( self::EXPIRATION_TRANSIENT_NAME );
} else {
$plugins = get_option( self::PLUGIN_INFO_TRANSIENT_NAME );
$expiration_time = get_option( self::EXPIRATION_TRANSIENT_NAME );
}
$is_expired = ! $expiration_time || $current_time > $expiration_time;
if ( $plugins === FALSE || $is_expired ) {
static $never_executed = TRUE;
if ( $plugins === false || $is_expired ) {
static $never_executed = true;
if ( $never_executed ) {
$never_executed = FALSE;
$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';
@@ -371,8 +384,13 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
}
$plugins = function_exists( 'get_plugins' ) ? get_plugins() : array();
set_transient( self::PLUGIN_INFO_TRANSIENT_NAME, $plugins, 0 );
set_transient( self::EXPIRATION_TRANSIENT_NAME, $current_time + self::CACHE_TIME, 0 );
if ( $use_transients ) {
set_transient( self::PLUGIN_INFO_TRANSIENT_NAME, $plugins, 0 );
set_transient( self::EXPIRATION_TRANSIENT_NAME, $current_time + self::CACHE_TIME, 0 );
} else {
update_option( self::PLUGIN_INFO_TRANSIENT_NAME, $plugins );
update_option( self::EXPIRATION_TRANSIENT_NAME, $current_time + self::CACHE_TIME );
}
}
return $plugins;
@@ -385,7 +403,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
*/
private function retrieve_required_plugins_data() {
$require_plugins = array();
$plugins = self::retrieve_plugins_data_in_transient();
$plugins = self::retrieve_plugins_data_in_transient( $this->use_transients );
if ( is_array( $plugins ) ) {
if ( count( $plugins ) > 0 ) {
if ( ! empty( $this->plugin_require ) ) {
@@ -413,7 +431,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 ) ) {
@@ -423,7 +441,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;
}
}
@@ -447,7 +465,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
}
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
) {
@@ -475,7 +493,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
$nice_name = $plugin_info[ self::PLUGIN_INFO_KEY_NICE_NAME ];
if ( ! self::is_wp_plugin_active( $name ) ) {
if ( ! self::is_wp_plugin_installed( $name ) ) {
if ( ! self::is_wp_plugin_installed( $name, $this->use_transients ) ) {
$install_url = $this->prepare_plugin_repository_install_url( $plugin_info );
return $this->prepare_notice_message( sprintf( wp_kses( __( 'The &#8220;%s&#8221; plugin requires free %s plugin. <a href="%s">Install %s</a>',
@@ -492,7 +510,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;
}
/**
@@ -516,11 +534,12 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* Checks if plugin is installed. Needs to be enabled in deferred way.
*
* @param string $plugin_file
* @param bool $use_transients
*
* @return bool
*/
public static function is_wp_plugin_installed( $plugin_file ) {
$plugins_data = self::retrieve_plugins_data_in_transient();
public static function is_wp_plugin_installed( $plugin_file, $use_transients = false ) {
$plugins_data = self::retrieve_plugins_data_in_transient( $use_transients );
return array_key_exists( $plugin_file, (array) $plugins_data );
}
@@ -628,7 +647,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
if ( isset( $this->plugin_file ) ) {
deactivate_plugins( plugin_basename( $this->plugin_file ) );
delete_transient( self::PLUGIN_INFO_TRANSIENT_NAME );
$this->clear_plugin_info_data();
}
}
@@ -640,11 +659,11 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
public function transient_delete_on_plugin_version_changed() {
add_action( self::HOOK_PLUGIN_DEACTIVATED_ACTION, array(
$this,
'handle_transient_delete_action'
'clear_plugin_info_data'
) );
add_action( self::HOOK_PLUGIN_ACTIVATED_ACTION, array(
$this,
'handle_transient_delete_action'
'clear_plugin_info_data'
) );
}
@@ -653,9 +672,11 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
*
* @return void
*/
public function handle_transient_delete_action() {
public function clear_plugin_info_data() {
delete_transient( self::PLUGIN_INFO_TRANSIENT_NAME );
delete_transient( self::EXPIRATION_TRANSIENT_NAME );
delete_option( self::PLUGIN_INFO_TRANSIENT_NAME );
delete_option( self::EXPIRATION_TRANSIENT_NAME );
}
/**
Loading