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');



}
}
39 changes: 32 additions & 7 deletions classes/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,15 @@ class User {
*/
public function __construct() {
$user = get_transient( 'imagify_user_cache' );

if ( ! $user ) {
$user = get_imagify_user();

if ( is_wp_error( $user ) ) {
$this->error = $user;
return;
}
set_transient( 'imagify_user_cache', $this->fill_user_for_error( $user ), 5 * MINUTE_IN_SECONDS );
}

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 +148,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 Expand Up @@ -295,4 +294,30 @@ public function is_over_quota() {
floatval( 100 ) === round( $this->get_percent_consumed_quota() )
);
}

/**
* Fill user object with missed details before saving the transient.
*
* @param object $user Error object.
* @return object
*/
private function fill_user_for_error( $user ) {
if ( ! is_wp_error( $user ) ) {
return $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;

return $user;
}
}
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
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
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