Skip to content
Snippets Groups Projects
Commit 2a365288 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
Pipeline #213777 passed
## [3.6.0] - 2023-06-22
## [3.6.1] - 2023-06-22
### 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
### Changed
......
......@@ -29,11 +29,9 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
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 = 300;
const CACHE_TIME = 16;
/** @var string */
protected $plugin_name;
......@@ -356,16 +354,9 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
*/
private static function retrieve_plugins_data_in_transient( $use_transients = true ) {
$current_time = 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;
$plugins = self::get_plugins_data_from_cache( $use_transients, $current_time );
if ( $plugins === false || $is_expired ) {
if ( $plugins === false ) {
static $never_executed = true;
if ( $never_executed ) {
$never_executed = false;
......@@ -384,16 +375,55 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
}
$plugins = function_exists( 'get_plugins' ) ? get_plugins() : array();
self::update_plugins_data_in_cache( $plugins, $use_transients, $current_time );
}
return $plugins;
}
/**
* @param bool $use_transients
* @param int $current_time
*
* @return array|false
*/
private static function get_plugins_data_from_cache( $use_transients = true, $current_time ) {
if ( $use_transients ) {
set_transient( self::PLUGIN_INFO_TRANSIENT_NAME, $plugins, 0 );
set_transient( self::EXPIRATION_TRANSIENT_NAME, $current_time + self::CACHE_TIME, 0 );
return get_transient( self::PLUGIN_INFO_TRANSIENT_NAME );
} else {
update_option( self::PLUGIN_INFO_TRANSIENT_NAME, $plugins );
update_option( self::EXPIRATION_TRANSIENT_NAME, $current_time + self::CACHE_TIME );
try {
$plugins_option_value = json_decode( get_option( self::PLUGIN_INFO_TRANSIENT_NAME . '' ), true );
if ( is_array( $plugins_option_value )
&& isset( $plugins_option_value['expiration_time'], $plugins_option_value['plugins'] )
&& (int) $plugins_option_value['expiration_time'] > $current_time
) {
return $plugins_option_value['plugins'];
}
} catch ( \RuntimeException $e ) {
// Exception can be thrown when option is not json encoded
// Do nothing
}
}
return $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, json_encode( array(
'expiration_time' => $current_time + self::CACHE_TIME,
'plugins' => $plugins,
) ) );
}
}
/**
......@@ -674,9 +704,7 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
*/
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 );
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment