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

feature(transients): as autoloaded options

parent c7af1af2
No related branches found
No related tags found
1 merge request!43feature(transients): as autoloaded options
## [3.6.0] - 2023-06-22 ## [3.6.1] - 2023-06-22
### Changed ### Changed
- Plugin info transient changed to auto loaded option (expiration 0) - Plugin info transient changed to auto loaded option
## [3.5.2] - 2023-02-10 ## [3.5.2] - 2023-02-10
### Changed ### Changed
......
...@@ -29,11 +29,13 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -29,11 +29,13 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
const PLUGIN_INFO_APPEND_PLUGIN_DATA = 'required_version'; const PLUGIN_INFO_APPEND_PLUGIN_DATA = 'required_version';
const PLUGIN_INFO_TRANSIENT_NAME = 'wpdesk_plugins_data'; const PLUGIN_INFO_TRANSIENT_NAME = 'wpdesk_requirements_plugins_data';
const EXPIRATION_TRANSIENT_NAME = 'wpdesk_plugins_data_exp'; const CACHE_TIME = 16;
const CACHE_TIME = 300; const EXPIRATION_TIME = 'expiration_time';
const PLUGINS = 'plugins';
/** @var string */ /** @var string */
protected $plugin_name; protected $plugin_name;
...@@ -355,17 +357,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -355,17 +357,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
* @return array In format [ 'plugindir/pluginfile.php' => ['Name' => 'Plugin Name', 'Version' => '1.0.1', ...], ] * @return array In format [ 'plugindir/pluginfile.php' => ['Name' => 'Plugin Name', 'Version' => '1.0.1', ...], ]
*/ */
private static function retrieve_plugins_data_in_transient( $use_transients = true ) { private static function retrieve_plugins_data_in_transient( $use_transients = true ) {
$current_time = time(); $current_time = time();
if ( $use_transients) { $plugins = self::get_plugins_data_from_cache( $use_transients, $current_time );
$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 ) { if ( $plugins === false ) {
static $never_executed = true; static $never_executed = true;
if ( $never_executed ) { if ( $never_executed ) {
$never_executed = false; $never_executed = false;
...@@ -384,18 +379,51 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -384,18 +379,51 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
} }
$plugins = function_exists( 'get_plugins' ) ? get_plugins() : array(); $plugins = function_exists( 'get_plugins' ) ? get_plugins() : array();
if ( $use_transients ) {
set_transient( self::PLUGIN_INFO_TRANSIENT_NAME, $plugins, 0 ); self::update_plugins_data_in_cache( $plugins, $use_transients, $current_time );
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; return $plugins;
} }
/**
* @param bool $use_transients
* @param int $current_time
*
* @return array|false
*/
private static function get_plugins_data_from_cache( $use_transients, $current_time ) {
if ( $use_transients ) {
return get_transient( self::PLUGIN_INFO_TRANSIENT_NAME );
} else {
$plugins_option_value = get_option( self::PLUGIN_INFO_TRANSIENT_NAME );
if ( is_array( $plugins_option_value )
&& isset( $plugins_option_value[ self::EXPIRATION_TIME ], $plugins_option_value[ self::PLUGINS ] )
&& (int) $plugins_option_value[ self::EXPIRATION_TIME ] > $current_time
) {
return $plugins_option_value[ self::PLUGINS ];
}
}
return false;
}
/**
* @param array $plugins
* @param bool $use_transients
* @param int $current_time
*/
private static function update_plugins_data_in_cache( $plugins, $use_transients, $current_time ) {
if ( $use_transients ) {
set_transient( self::PLUGIN_INFO_TRANSIENT_NAME, $plugins, self::CACHE_TIME );
} else {
update_option( self::PLUGIN_INFO_TRANSIENT_NAME, array(
self::EXPIRATION_TIME => $current_time + self::CACHE_TIME,
self::PLUGINS => $plugins,
) );
}
}
/** /**
* Check the plugins directory and retrieve all required plugin files with plugin data. * Check the plugins directory and retrieve all required plugin files with plugin data.
* *
...@@ -674,9 +702,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) { ...@@ -674,9 +702,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
*/ */
public function clear_plugin_info_data() { public function clear_plugin_info_data() {
delete_transient( self::PLUGIN_INFO_TRANSIENT_NAME ); delete_transient( self::PLUGIN_INFO_TRANSIENT_NAME );
delete_transient( self::EXPIRATION_TRANSIENT_NAME );
delete_option( self::PLUGIN_INFO_TRANSIENT_NAME ); delete_option( self::PLUGIN_INFO_TRANSIENT_NAME );
delete_option( self::EXPIRATION_TRANSIENT_NAME );
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment