From e8056d63b4f98d883402e2489dcac961a0506c38 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 8 Nov 2024 05:43:06 +0100 Subject: [PATCH 1/2] Revert "Add User service provider (#902)" This reverts commit dc2a8354503375b587114775e50fb45967b110a2. --- classes/User/ServiceProvider.php | 38 -------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 classes/User/ServiceProvider.php diff --git a/classes/User/ServiceProvider.php b/classes/User/ServiceProvider.php deleted file mode 100644 index c2b89bd8..00000000 --- a/classes/User/ServiceProvider.php +++ /dev/null @@ -1,38 +0,0 @@ -getContainer()->add( 'user', User::class ); - } - - /** - * Returns the subscribers array - * - * @return array - */ - public function get_subscribers() { - return []; - } -} From 7f45a010f3759f67c6efc9907cb64342e537ed26 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 8 Nov 2024 05:44:05 +0100 Subject: [PATCH 2/2] Revert "Cache user API call for 5 minutes (#907)" This reverts commit 25c8bfa8e225573b659162789353d7b21a8b82d1. --- Tests/Unit/inc/classes/ImagifyUser/getError.php | 9 +++------ .../ImagifyUser/getPercentConsumedQuota.php | 6 ++---- Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php | 9 +++------ Tests/bootstrap-functions.php | 1 - classes/User/User.php | 14 ++++---------- 5 files changed, 12 insertions(+), 27 deletions(-) diff --git a/Tests/Unit/inc/classes/ImagifyUser/getError.php b/Tests/Unit/inc/classes/ImagifyUser/getError.php index def6526d..035012cf 100644 --- a/Tests/Unit/inc/classes/ImagifyUser/getError.php +++ b/Tests/Unit/inc/classes/ImagifyUser/getError.php @@ -31,9 +31,8 @@ public function testShouldReturnFalseWhenFetchedUserData() { 'is_monthly' => true, ]; - Functions\when( 'get_transient' )->justReturn( false ); + Functions\when( 'imagify_get_cached_user' )->justReturn( false ); Functions\when( 'get_imagify_user' )->justReturn( $userData ); - Functions\when( 'set_transient')->justReturn(); $this->assertFalse( ( new User() )->get_error() ); } @@ -56,9 +55,8 @@ public function testShouldReturnFromCachedUserDataIfAvailable() { 'is_monthly' => true, ]; - Functions\when( 'get_transient' )->justReturn( $userData ); + Functions\when( 'imagify_get_cached_user' )->justReturn( $userData ); Functions\expect( 'get_imagify_user' )->never(); - Functions\when( 'set_transient')->justReturn(); $this->assertSame( 'imagify@example.com', ( new User() )->email ); } @@ -69,9 +67,8 @@ public function testShouldReturnFromCachedUserDataIfAvailable() { public function testShouldReturnErrorWhenCouldNotFetchUserData() { $wp_error = new WP_Error( 'error_id', 'Error Message' ); - Functions\when( 'get_transient' )->justReturn( false ); + Functions\when( 'imagify_get_cached_user' )->justReturn( false ); Functions\when( 'get_imagify_user' )->justReturn( $wp_error ); - Functions\when( 'set_transient')->justReturn(); $this->assertSame( $wp_error, ( new User() )->get_error() ); } diff --git a/Tests/Unit/inc/classes/ImagifyUser/getPercentConsumedQuota.php b/Tests/Unit/inc/classes/ImagifyUser/getPercentConsumedQuota.php index edffc78f..a981d338 100644 --- a/Tests/Unit/inc/classes/ImagifyUser/getPercentConsumedQuota.php +++ b/Tests/Unit/inc/classes/ImagifyUser/getPercentConsumedQuota.php @@ -23,9 +23,8 @@ class Test_GetPercentConsumedQuota extends TestCase { public function testShouldReturnZeroWhenCouldNotFetchUserData() { $wp_error = new WP_Error( 'error_id', 'Error Message' ); - Functions\when( 'get_transient' )->justReturn( false ); + Functions\when( 'imagify_get_cached_user' )->justReturn( false ); Functions\when( 'get_imagify_user' )->justReturn( $wp_error ); - Functions\when( 'set_transient')->justReturn(); Functions\expect( 'imagify_round_half_five' )->never(); $this->assertSame( ( new User() )->get_percent_consumed_quota(), 0 ); @@ -49,9 +48,8 @@ public function testShouldReturnQuotaWhenFetchedUserData() { 'is_monthly' => true, ]; - Functions\when( 'get_transient' )->justReturn( false ); + Functions\when( 'imagify_get_cached_user' )->justReturn( false ); Functions\when( 'get_imagify_user' )->justReturn( $userData ); - Functions\when( 'set_transient')->justReturn(); Functions\expect( 'imagify_round_half_five' ) ->twice() ->with( 0 ) // extra_quota_consumed. diff --git a/Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php b/Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php index 43f9a9f3..8fbd48b1 100644 --- a/Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php +++ b/Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php @@ -22,9 +22,8 @@ class Test_IsOverQuota extends TestCase { public function testShouldReturnFalseWhenCouldNotFetchUserData() { $wp_error = new WP_Error( 'error_id', 'Error Message' ); - Functions\when( 'get_transient' )->justReturn( false ); + Functions\when( 'imagify_get_cached_user' )->justReturn( false ); Functions\when( 'get_imagify_user' )->justReturn( $wp_error ); - Functions\when( 'set_transient')->justReturn(); $this->assertFalse( ( new User() )->is_over_quota() ); } @@ -47,9 +46,8 @@ public function testShouldReturnFalseWhenPaidAccount() { 'is_monthly' => true, ]; - Functions\when( 'get_transient' )->justReturn( false ); + Functions\when( 'imagify_get_cached_user' )->justReturn( false ); Functions\when( 'get_imagify_user' )->justReturn( $userData ); - Functions\when( 'set_transient')->justReturn(); $this->assertFalse( ( new User() )->is_over_quota() ); } @@ -101,9 +99,8 @@ public function testShouldReturnTrueWhenFreeOverQuota() { } private function createMocks( $userData, $dataPreviousQuotaPercent ) { - Functions\when( 'get_transient' )->justReturn( false ); + Functions\when( 'imagify_get_cached_user' )->justReturn( false ); Functions\when( 'get_imagify_user' )->justReturn( $userData ); - Functions\when( 'set_transient')->justReturn(); Functions\expect( 'imagify_round_half_five' ) ->once() ->with( 0 ) // extra_quota_consumed. diff --git a/Tests/bootstrap-functions.php b/Tests/bootstrap-functions.php index 131efc07..80db8b09 100644 --- a/Tests/bootstrap-functions.php +++ b/Tests/bootstrap-functions.php @@ -44,6 +44,5 @@ function init_constants( $test_suite_folder ) { if ( 'Unit' === $test_suite_folder && ! defined( 'ABSPATH' ) ) { define( 'ABSPATH', IMAGIFY_PLUGIN_ROOT ); - define( 'MINUTE_IN_SECONDS', 60 ); } } diff --git a/classes/User/User.php b/classes/User/User.php index 73726a28..fa9c184a 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 = imagify_get_cached_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;