Skip to content
Snippets Groups Projects
Commit 3d2c9b75 authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

feature(transients): as autoloaded options

parent d6c321ae
No related branches found
No related tags found
1 merge request!42Feature/transients
Pipeline #213588 failed
...@@ -10,42 +10,64 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -10,42 +10,64 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* have to be compatible with PHP 5.3.x * have to be compatible with PHP 5.3.x
*/ */
class WPDesk_Basic_Requirement_Checker implements WPDesk_Requirement_Checker { class WPDesk_Basic_Requirement_Checker implements WPDesk_Requirement_Checker {
const EXTENSION_NAME_OPENSSL = 'openssl'; const EXTENSION_NAME_OPENSSL = 'openssl';
const HOOK_ADMIN_NOTICES_ACTION = 'admin_notices'; const HOOK_ADMIN_NOTICES_ACTION = 'admin_notices';
const HOOK_PLUGIN_DEACTIVATED_ACTION = 'deactivated_plugin'; const HOOK_PLUGIN_DEACTIVATED_ACTION = 'deactivated_plugin';
const HOOK_PLUGIN_ACTIVATED_ACTION = 'activated_plugin'; const HOOK_PLUGIN_ACTIVATED_ACTION = 'activated_plugin';
const PLUGIN_INFO_KEY_NICE_NAME = 'nice_name'; const PLUGIN_INFO_KEY_NICE_NAME = 'nice_name';
const PLUGIN_INFO_KEY_NAME = 'name'; const PLUGIN_INFO_KEY_NAME = 'name';
const PLUGIN_INFO_VERSION = 'version'; const PLUGIN_INFO_VERSION = 'version';
const PLUGIN_INFO_FAKE_REQUIRED_MINIMUM_VERSION = '0.0'; const PLUGIN_INFO_FAKE_REQUIRED_MINIMUM_VERSION = '0.0';
const PLUGIN_INFO_APPEND_PLUGIN_DATA = 'required_version'; const PLUGIN_INFO_APPEND_PLUGIN_DATA = 'required_version';
const PLUGIN_INFO_TRANSIENT_NAME = 'require_plugins_data'; const PLUGIN_INFO_TRANSIENT_NAME = 'require_plugins_data';
const EXPIRATION_TRANSIENT_NAME = 'require_plugins_data_exp'; const EXPIRATION_TRANSIENT_NAME = 'require_plugins_data_exp';
const CACHE_TIME = 300; const CACHE_TIME = 300;
/** @var string */ /** @var string */
protected $plugin_name; protected $plugin_name;
/** @var string */ /** @var string */
private $plugin_file; private $plugin_file;
/** @var string */ /** @var string */
private $min_php_version; private $min_php_version;
/** @var string */ /** @var string */
private $min_wp_version; private $min_wp_version;
/** @var string|null */ /** @var string|null */
private $min_wc_version = null; private $min_wc_version = NULL;
/** @var int|null */ /** @var int|null */
private $min_openssl_version = null; private $min_openssl_version = NULL;
/** @var array */ /** @var array */
protected $plugin_require; protected $plugin_require;
/** @var bool */ /** @var bool */
protected $should_check_plugin_versions = false; protected $should_check_plugin_versions = FALSE;
/** @var array */ /** @var array */
private $module_require; private $module_require;
/** @var array */ /** @var array */
private $setting_require; private $setting_require;
/** @var array */ /** @var array */
protected $notices; protected $notices;
/** @var @string */ /** @var @string */
private $text_domain; private $text_domain;
...@@ -121,14 +143,14 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -121,14 +143,14 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* *
* @return $this * @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) { if ($plugin_require_version) {
$this->should_check_plugin_versions = true; $this->should_check_plugin_versions = TRUE;
} }
$this->plugin_require[$plugin_name] = array( $this->plugin_require[$plugin_name] = array(
self::PLUGIN_INFO_KEY_NAME => $plugin_name, 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_KEY_NICE_NAME => $nice_plugin_name === NULL ? $plugin_name : $nice_plugin_name,
self::PLUGIN_INFO_VERSION => $plugin_require_version === null ? self::PLUGIN_INFO_VERSION => $plugin_require_version === NULL ?
self::PLUGIN_INFO_FAKE_REQUIRED_MINIMUM_VERSION : $plugin_require_version, self::PLUGIN_INFO_FAKE_REQUIRED_MINIMUM_VERSION : $plugin_require_version,
); );
...@@ -144,12 +166,12 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -144,12 +166,12 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* *
* @return $this * @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( $this->plugin_require[$plugin_name] = array(
self::PLUGIN_INFO_KEY_NAME => $plugin_name, self::PLUGIN_INFO_KEY_NAME => $plugin_name,
self::PLUGIN_INFO_VERSION => $version, self::PLUGIN_INFO_VERSION => $version,
'repository_url' => 'http://downloads.wordpress.org/plugin/' . dirname($plugin_name) . '.latest-stable.zip', '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; return $this;
...@@ -161,10 +183,11 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -161,10 +183,11 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* *
* @return $this * @return $this
*/ */
public function add_php_module_require( $module_name, $nice_name = null ) { public function add_php_module_require($module_name, $nice_name = NULL) {
if ( $nice_name === null ) { if ($nice_name === NULL) {
$this->module_require[$module_name] = $module_name; $this->module_require[$module_name] = $module_name;
} else { }
else {
$this->module_require[$module_name] = $nice_name; $this->module_require[$module_name] = $nice_name;
} }
...@@ -207,11 +230,11 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -207,11 +230,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.', $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)); $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.', $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)); $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.', $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), $this->get_text_domain()), esc_html($this->plugin_name),
'0x' . dechex($this->min_openssl_version))); '0x' . dechex($this->min_openssl_version)));
...@@ -330,10 +353,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -330,10 +353,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
$expiration_time = get_transient(self::EXPIRATION_TRANSIENT_NAME); $expiration_time = get_transient(self::EXPIRATION_TRANSIENT_NAME);
$is_expired = !$expiration_time || $current_time > $expiration_time; $is_expired = !$expiration_time || $current_time > $expiration_time;
if ( $plugins === false || $is_expired ) { if ($plugins === FALSE || $is_expired) {
static $never_executed = true; static $never_executed = TRUE;
if ($never_executed) { if ($never_executed) {
$never_executed = false; $never_executed = FALSE;
/** Required when WC starts later and these data should be in cache */ /** Required when WC starts later and these data should be in cache */
add_filter('extra_plugin_headers', function($headers = array()) { add_filter('extra_plugin_headers', function($headers = array()) {
$headers[] = 'WC tested up to'; $headers[] = 'WC tested up to';
...@@ -362,7 +385,6 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -362,7 +385,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']... ] * @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() { private function retrieve_required_plugins_data() {
$require_plugins = array(); $require_plugins = array();
$plugins = self::retrieve_plugins_data_in_transient(); $plugins = self::retrieve_plugins_data_in_transient();
if (is_array($plugins)) { if (is_array($plugins)) {
...@@ -392,17 +414,18 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -392,17 +414,18 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
private function append_plugin_require_notices($notices) { private function append_plugin_require_notices($notices) {
if (count($this->plugin_require) > 0) { if (count($this->plugin_require) > 0) {
foreach ($this->plugin_require as $plugin_name => $plugin_info) { foreach ($this->plugin_require as $plugin_name => $plugin_info) {
$notice = null; $notice = NULL;
if (isset($plugin_info['repository_url'])) { if (isset($plugin_info['repository_url'])) {
$notice = $this->prepare_plugin_repository_require_notice($plugin_info); $notice = $this->prepare_plugin_repository_require_notice($plugin_info);
} elseif ( ! self::is_wp_plugin_active( $plugin_name ) ) { }
elseif (!self::is_wp_plugin_active($plugin_name)) {
$notice = $this->prepare_notice_message(sprintf(__('The “%s” plugin cannot run without %s active. Please install and activate %s plugin.', $notice = $this->prepare_notice_message(sprintf(__('The “%s” plugin cannot run without %s active. Please install and activate %s plugin.',
$this->get_text_domain()), esc_html($this->plugin_name), $this->get_text_domain()), esc_html($this->plugin_name),
esc_html(basename($plugin_info[self::PLUGIN_INFO_KEY_NICE_NAME])), esc_html(basename($plugin_info[self::PLUGIN_INFO_KEY_NICE_NAME])),
esc_html(basename($plugin_info[self::PLUGIN_INFO_KEY_NICE_NAME])))); esc_html(basename($plugin_info[self::PLUGIN_INFO_KEY_NICE_NAME]))));
} }
if ( $notice !== null ) { if ($notice !== NULL) {
$notices[] = $notice; $notices[] = $notice;
} }
} }
...@@ -426,7 +449,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -426,7 +449,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
} }
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 || if ('plugin_information' !== $action ||
false !== $api || FALSE !== $api ||
!isset($args->slug) || !isset($args->slug) ||
$slug !== $args->slug $slug !== $args->slug
) { ) {
...@@ -471,7 +494,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -471,7 +494,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
$this->plugin_name, $nice_name, esc_url(admin_url($activate_url)), $nice_name)); $this->plugin_name, $nice_name, esc_url(admin_url($activate_url)), $nice_name));
} }
return null; return NULL;
} }
/** /**
...@@ -568,7 +591,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -568,7 +591,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @deprecated use render_notices or disable_plugin * @deprecated use render_notices or disable_plugin
*/ */
public function disable_plugin_render_notice() { 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'
));
} }
/** /**
...@@ -577,7 +603,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -577,7 +603,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @return void * @return void
*/ */
public function render_notices() { 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'
));
} }
/** /**
...@@ -586,7 +615,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -586,7 +615,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @return void * @return void
*/ */
public function disable_plugin() { 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'
));
} }
/** /**
...@@ -608,8 +640,14 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -608,8 +640,14 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @return void * @return void
*/ */
public function transient_delete_on_plugin_version_changed() { 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_DEACTIVATED_ACTION, array(
add_action( self::HOOK_PLUGIN_ACTIVATED_ACTION, array( $this, 'handle_transient_delete_action' ) ); $this,
'handle_transient_delete_action'
));
add_action(self::HOOK_PLUGIN_ACTIVATED_ACTION, array(
$this,
'handle_transient_delete_action'
));
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment