From d6c321ae5115a0c8cc8ae727ae5dfad01954d5a4 Mon Sep 17 00:00:00 2001
From: Grzegorz Rola <grola@seostudio.pl>
Date: Thu, 22 Jun 2023 08:52:26 +0200
Subject: [PATCH] feature(transients): as autoloaded options

---
 CHANGELOG.md                      |  6 +++--
 src/Basic_Requirement_Checker.php | 41 +++++++++++++++++--------------
 2 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3186392..5af358a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,7 +1,9 @@
-## [3.5.2] - 2023-02-10
-
+## [3.6.0] - 2023-06-22
 ### Changed
+- Plugin info transient changed to auto loaded option (expiration 0) 
 
+## [3.5.2] - 2023-02-10
+### Changed
 - Removed arrows from user-facing messages.
 
 ## [3.5.1] - 2022-08-30
diff --git a/src/Basic_Requirement_Checker.php b/src/Basic_Requirement_Checker.php
index e942fff..8180465 100644
--- a/src/Basic_Requirement_Checker.php
+++ b/src/Basic_Requirement_Checker.php
@@ -21,9 +21,10 @@ if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker' ) ) {
 		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 */
+        /** @var string */
 		protected $plugin_name;
 		/** @var string */
 		private $plugin_file;
@@ -324,29 +325,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 );
-				} );
-			}
-
+            $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;
@@ -615,6 +619,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 );
 		}
 
 		/**
-- 
GitLab