Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache imagify user response even in case of error #916

Merged
merged 11 commits into from
Nov 8, 2024
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions Tests/Integration/inc/classes/ImagifyUser/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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');



}
}
16 changes: 5 additions & 11 deletions classes/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion imagify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions inc/classes/class-imagify-admin-ajax-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 2 additions & 0 deletions inc/classes/class-imagify-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
4 changes: 4 additions & 0 deletions inc/classes/class-imagify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
25 changes: 24 additions & 1 deletion inc/functions/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading