From 9b2e5f57bc3538d0a147a5eb74b25ca1060cbf5a Mon Sep 17 00:00:00 2001 From: Mathieu Lamiot Date: Fri, 8 Nov 2024 19:26:27 +0100 Subject: [PATCH] cache imagify user response even in case of error (#916) Co-authored-by: WordPressFan --- README.md | 3 +++ .../inc/classes/ImagifyUser/TestCase.php | 8 ++++++ classes/User/User.php | 16 ++++-------- imagify.php | 2 +- inc/classes/class-imagify-admin-ajax-post.php | 2 ++ inc/classes/class-imagify-settings.php | 2 ++ inc/classes/class-imagify.php | 4 +++ inc/functions/api.php | 25 ++++++++++++++++++- readme.txt | 5 +++- 9 files changed, 53 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5e570c4b9..31341d639 100755 --- a/README.md +++ b/README.md @@ -155,6 +155,9 @@ When the plugin is disabled, your existing images remain optimized. Backups of t Please report security bugs found in the site-reviews plugin's source code through the [Patchstack Vulnerability Disclosure Program](https://patchstack.com/database/vdp/imagify). The Patchstack team will assist you with verification, CVE assignment and take care of notifying the developers of this plugin. ## Changelog +### 2.2.3.1 +- Enhancement: Decrease the amount of requests to imagify servers. + ### 2.2.3 - Enhancement: Cache the calls to the license API to avoid sending unnecessary requests - 3rd-party compatibility: Update priority on `template_redirect` to improve compatibility with WP Rocket’s LazyLoad diff --git a/Tests/Integration/inc/classes/ImagifyUser/TestCase.php b/Tests/Integration/inc/classes/ImagifyUser/TestCase.php index c4aaecb5f..79c1a9a57 100644 --- a/Tests/Integration/inc/classes/ImagifyUser/TestCase.php +++ b/Tests/Integration/inc/classes/ImagifyUser/TestCase.php @@ -12,6 +12,9 @@ public function setUp() { parent::setUp(); $this->originalUserInstance = $this->resetPropertyValue( 'user', Imagify::class ); + + //Clean up the transients for API cache + delete_transient('imagify_user_cache'); } public function tearDown() { @@ -20,5 +23,10 @@ public function tearDown() { // Restore the user on the static property. $this->setPropertyValue( 'user', Imagify::class, $this->originalUserInstance ); + //Clean up the transients for API cache + delete_transient('imagify_user_cache'); + + + } } diff --git a/classes/User/User.php b/classes/User/User.php index 73726a282..477c78c3a 100755 --- a/classes/User/User.php +++ b/classes/User/User.php @@ -125,17 +125,11 @@ class User { * @return void */ public function __construct() { - $user = get_transient( 'imagify_user_cache' ); + $user = get_imagify_user(); - if ( ! $user ) { - $user = get_imagify_user(); - - if ( is_wp_error( $user ) ) { - $this->error = $user; - return; - } - - set_transient( 'imagify_user_cache', $user, 5 * MINUTE_IN_SECONDS ); + if ( is_wp_error( $user ) ) { + $this->error = $user; + return; } $this->id = $user->id; @@ -149,7 +143,7 @@ public function __construct() { $this->next_date_update = $user->next_date_update; $this->is_active = $user->is_active; $this->is_monthly = $user->is_monthly; - $this->error = false; + $this->error = is_wp_error( $user ); } /** diff --git a/imagify.php b/imagify.php index 2d4c81bd7..465bf96b8 100644 --- a/imagify.php +++ b/imagify.php @@ -3,7 +3,7 @@ * Plugin Name: Imagify * Plugin URI: https://wordpress.org/plugins/imagify/ * Description: Dramatically reduce image file sizes without losing quality, make your website load faster, boost your SEO and save money on your bandwidth using Imagify, the new most advanced image optimization tool. - * Version: 2.2.3 + * Version: 2.2.3.1 * Requires at least: 5.3 * Requires PHP: 7.3 * Author: Imagify Image Optimizer – Optimize Images & Convert WebP & Avif diff --git a/inc/classes/class-imagify-admin-ajax-post.php b/inc/classes/class-imagify-admin-ajax-post.php index 5b7d2de9f..37c7bd7c6 100755 --- a/inc/classes/class-imagify-admin-ajax-post.php +++ b/inc/classes/class-imagify-admin-ajax-post.php @@ -825,6 +825,8 @@ public function imagify_check_api_key_validity_callback() { update_imagify_option( 'api_key', $api_key ); + delete_transient( 'imagify_user_cache' ); + wp_send_json_success(); } diff --git a/inc/classes/class-imagify-settings.php b/inc/classes/class-imagify-settings.php index abfca2df6..5013a7ad6 100644 --- a/inc/classes/class-imagify-settings.php +++ b/inc/classes/class-imagify-settings.php @@ -359,6 +359,8 @@ public function after_save_options( $old_value, $value ) { return; } + delete_transient( 'imagify_user_cache' ); + // Handle API key validation cache and notices. if ( Imagify_Requirements::is_api_key_valid( true ) ) { Notices::dismiss_notice( 'wrong-api-key' ); diff --git a/inc/classes/class-imagify.php b/inc/classes/class-imagify.php index b7d7669dd..e5bd172cf 100644 --- a/inc/classes/class-imagify.php +++ b/inc/classes/class-imagify.php @@ -86,6 +86,10 @@ protected function __construct() { * @return object */ public function get_user() { + if ( empty( $this->api_key ) ) { + return new WP_Error( 'api_key_missing', __( 'API key required.', 'imagify' ) ); + } + global $wp_current_filter; if ( isset( static::$user ) ) { diff --git a/inc/functions/api.php b/inc/functions/api.php index 2168dd70e..032ae2932 100755 --- a/inc/functions/api.php +++ b/inc/functions/api.php @@ -40,7 +40,30 @@ function update_imagify_user( $data ) { * @return object */ function get_imagify_user() { - return imagify()->get_user(); + $user = get_transient( 'imagify_user_cache' ); + if ( false !== $user ) { + return $user; + } + + $user = imagify()->get_user(); + + // Fill user object with missed details before saving the transient. + if ( is_wp_error( $user ) ) { + $user->id = 0; + $user->email = ''; + $user->plan_id = 0; + $user->plan_label = ''; + $user->quota = 0; + $user->extra_quota = 0; + $user->extra_quota_consumed = 0; + $user->consumed_current_month_quota = 0; + $user->next_date_update = null; + $user->is_active = false; + $user->is_monthly = false; + } + + set_transient( 'imagify_user_cache', $user, 5 * MINUTE_IN_SECONDS ); + return $user; } /** diff --git a/readme.txt b/readme.txt index cdb7bdaac..55da4e38f 100644 --- a/readme.txt +++ b/readme.txt @@ -2,7 +2,7 @@ Contributors: wp_rocket, imagify Tags: optimize images, image optimization, compress images, convert webp, convert AVIF Tested up to: 6.6 -Stable tag: 2.2.3 +Stable tag: 2.2.3.1 Requires PHP: 7.3 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -266,6 +266,9 @@ You can report any security bugs found in the source code of the site-reviews pl 4. Other Media Page == Changelog == += 2.2.3.1 = +- Enhancement: Decrease the amount of requests to imagify servers. + = 2.2.3 = - Enhancement: Cache the calls to the license API to avoid sending unnecessary requests - 3rd-party compatibility: Update priority on `template_redirect` to improve compatibility with WP Rocket’s LazyLoad