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

Fix/923 reduce the number of api #936

Merged
merged 12 commits into from
Dec 19, 2024
2 changes: 2 additions & 0 deletions Tests/Integration/inc/classes/ImagifyUser/isOverQuota.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function testShouldReturnFalseWhenFreeNotOverQuota() {
$this->assertNull( $this->getNonPublicPropertyValue( 'user', Imagify::class ) );

$imagifyUser = new User();
$imagifyUser->init_user();
// Make sure the account is not over-quota.
$imagifyUser->quota = 1000;
$imagifyUser->consumed_current_month_quota = 200;
Expand All @@ -78,6 +79,7 @@ public function testShouldReturnTrueWhenFreeOverQuota() {
$this->assertNull( $this->getNonPublicPropertyValue( 'user', Imagify::class ) );

$imagifyUser = new User();
$imagifyUser->init_user();
//
$imagifyUser->plan_id = 1;
// Make it over-quota.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function setUp(): void {
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected ) {
$this->user->plan_id = $config['plan_id'];
$this->user->shouldReceive( 'get_plan_id' )
->andReturn( $config['plan_id'] );

Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' );
Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' );
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/inc/classes/ImagifyUser/getError.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testShouldReturnFromCachedUserDataIfAvailable() {

Functions\when( 'get_transient' )->justReturn( $userData );

$this->assertSame( '[email protected]', ( new User() )->email );
$this->assertSame( '[email protected]', ( new User() )->get_email() );
}

/**
Expand Down
50 changes: 50 additions & 0 deletions Tests/Unit/inc/classes/ImagifyUser/getUserData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace Imagify\Tests\Unit\inc\classes\ImagifyUser;

use Imagify\Tests\Unit\TestCase;
use Imagify\User\User;
use Brain\Monkey\Functions;
use Mockery;

/**
* Tests for \Imagify\User\User->init_user().
*
* @covers \Imagify\User\User::init_user
* @group ImagifyAPI
*/
class Test_GetUserData extends TestCase {
private $user;

protected function setUp(): void {
parent::setUp();

$this->user = Mockery::mock(User::class)->makePartial();
}

/**
* Test \Imagify\User\User->init_user().
*/
public function testEnsureInitUserIsCalled() {
$userData = (object) [
'id' => 14,
'email' => '[email protected]',
'plan_id' => '1',
'plan_label' => 'free',
'quota' => 1000,
'extra_quota' => 0,
'extra_quota_consumed' => 0,
'consumed_current_month_quota' => 10,
'next_date_update' => '',
'is_active' => 1,
'is_monthly' => true,
];

Functions\when( 'get_imagify_user' )->justReturn( $userData );

Functions\when( 'is_wp_error' )->justReturn( false );

$this->assertEquals(14, $this->user->get_id());
$this->assertEquals('[email protected]', $this->user->get_email(), 'Email should be initialized and returned correctly.');
$this->assertTrue($this->user->is_monthly, 'is_monthly should be initialized to true.');
}
}
15 changes: 13 additions & 2 deletions assets/js/admin-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var busy = false;

$( d ).on( 'mouseenter', '#wp-admin-bar-imagify', function() {
var $adminBarProfile, url;
var $adminBarProfile, url, $adminBarPricing;

if ( true === busy ) {
return;
Expand All @@ -13,6 +13,7 @@
busy = true;

$adminBarProfile = $( '#wp-admin-bar-imagify-profile-content' );
$adminBarPricing = $( '#wp-admin-bar-imagify-pricing-content' );

if ( ! $adminBarProfile.is( ':empty' ) ) {
return;
Expand All @@ -28,8 +29,18 @@

$.get( url + 'action=imagify_get_admin_bar_profile&imagifygetadminbarprofilenonce=' + $( '#imagifygetadminbarprofilenonce' ).val() )
.done( function( response ) {
$adminBarProfile.html( response.data );
var $templates = response.data;
$( '#wp-admin-bar-imagify-profile-loading' ).remove();
if ( $templates.admin_bar_pricing ) {
$adminBarPricing.html( $templates.admin_bar_pricing );
} else {
$adminBarPricing.remove();
}

if ( $templates.admin_bar_status ) {
$adminBarProfile.html( $templates.admin_bar_status );
}

busy = false;
} );
} );
Expand Down
2 changes: 1 addition & 1 deletion assets/js/admin-bar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions assets/js/pricing-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,14 @@
imagifyModal.getPricing($(this));
});

/**
* Get pricing on modal opening for admin bar menu, the button is added dynamically
* Build the pricing tables inside modal.
*/
$(document).on('click', '.imagify-admin-bar-upgrade-plan', function () {
imagifyModal.getPricing($(this));
});

/**
* Reset the modal on close.
*/
Expand Down
2 changes: 1 addition & 1 deletion assets/js/pricing-modal.min.js

Large diffs are not rendered by default.

25 changes: 12 additions & 13 deletions classes/Admin/AdminBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,11 @@ public function add_imagify_admin_bar_menu( $wp_admin_bar ) {
return;
}

if (
$this->user->is_free()
&&
$this->user->get_percent_unconsumed_quota() > 20
) {
$wp_admin_bar->add_menu( [
'parent' => 'imagify',
'id' => 'imagify-upgrade-plan',
'title' => '<button data-nonce="' . wp_create_nonce( 'imagify_get_pricing_' . get_current_user_id() ) . '" data-target="#imagify-pricing-modal" type="button" class="imagify-get-pricing-modal imagify-modal-trigger imagify-admin-bar-upgrade-plan">' . __( 'Upgrade Plan', 'imagify' ) . '</button>',
] );
}
$wp_admin_bar->add_menu( array(
'parent' => 'imagify',
'id' => 'imagify-upgrade-plan',
'title' => '<div id="wp-admin-bar-imagify-pricing-content" class="hide-if-no-js"></div>',
) );

$wp_admin_bar->add_menu( array(
'parent' => 'imagify',
Expand Down Expand Up @@ -169,14 +163,19 @@ public function get_admin_bar_profile_callback() {
'plan_label' => $this->user->plan_label,
'plan_with_quota' => $this->user->is_free() || $this->user->is_growth(),
'unconsumed_quota' => $unconsumed_quota,
'user_quota' => $this->user->quota,
'user_quota' => $this->user->get_quota(),
'next_update' => $this->user->next_date_update,
'text' => $text,
'button_text' => $button_text,
'upgrade_link' => $upgrade_link,
];

$template = $views->get_template( 'admin/admin-bar-status', $data );
$template = [
'admin_bar_status' => $views->get_template( 'admin/admin-bar-status', $data ),
'admin_bar_pricing' => $views->get_template( 'admin/admin-bar-pricing', [
'upgrade_pricing' => $this->user->is_free() && ( $this->user->get_percent_unconsumed_quota() > 20 ),
] ),
];

wp_send_json_success( $template );
}
Expand Down
6 changes: 3 additions & 3 deletions classes/Admin/AdminSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public static function get_subscribed_events() {
* @return array
*/
public function plugin_action_links( $actions ) {
$text = 1 !== $this->user->plan_id ? __( 'Documentation', 'imagify' ) : __( 'Upgrade', 'imagify' );
$url = 1 !== $this->user->plan_id ? 'documentation' : 'subscription';
$class = 1 !== $this->user->plan_id ? '' : ' class="imagify-plugin-upgrade"';
$text = 1 !== $this->user->get_plan_id() ? __( 'Documentation', 'imagify' ) : __( 'Upgrade', 'imagify' );
$url = 1 !== $this->user->get_plan_id() ? 'documentation' : 'subscription';
$class = 1 !== $this->user->get_plan_id() ? '' : ' class="imagify-plugin-upgrade"';

array_unshift( $actions, sprintf( '<a href="%s" target="_blank"%s>%s</a>',
esc_url( imagify_get_external_url( $url ) ),
Expand Down
77 changes: 74 additions & 3 deletions classes/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,41 @@ class User {
private $error = false;

/**
* The constructor.
* Initialisation.
*
* @since 1.0
* @var bool
*/
protected $initialized = false;

/**
* Initialise the user data by fetching the api data
*
* @return void
*/
public function __construct() {
public function init_user() {
if ( $this->initialized ) {
return;
}

$user = get_imagify_user();

if ( is_wp_error( $user ) ) {
$this->error = $user;
return;
}

$this->set_user_properties( $user );
$this->initialized = true;
}

/**
* Set user properties
*
* @param object $user User object data.
*
* @return void
*/
private function set_user_properties( $user ) {
$this->id = $user->id;
$this->email = $user->email;
$this->plan_id = (int) $user->plan_id;
Expand All @@ -151,6 +172,8 @@ public function __construct() {
* @since 1.9.9
*/
public function get_error() {
$this->init_user();

return $this->error;
}

Expand Down Expand Up @@ -236,6 +259,7 @@ public function get_percent_consumed_quota() {
* @return float|int
*/
public function get_percent_unconsumed_quota() {
$this->init_user();
return 100 - $this->get_percent_consumed_quota();
}

Expand All @@ -247,6 +271,7 @@ public function get_percent_unconsumed_quota() {
* @return bool
*/
public function is_free() {
$this->init_user();
return 1 === $this->plan_id;
}

Expand All @@ -256,6 +281,7 @@ public function is_free() {
* @return bool
*/
public function is_growth() {
$this->init_user();
return ( 16 === $this->plan_id || 18 === $this->plan_id );
}

Expand All @@ -265,6 +291,7 @@ public function is_growth() {
* @return bool
*/
public function is_infinite() {
$this->init_user();
return ( 15 === $this->plan_id || 17 === $this->plan_id );
}

Expand All @@ -287,4 +314,48 @@ public function is_over_quota() {
floatval( 100 ) === round( $this->get_percent_consumed_quota() )
);
}

/**
* Get user Id
*
* @return string
*/
public function get_id() {
$this->init_user();

return $this->id;
}

/**
* Get user email.
*
* @return string
*/
public function get_email() {
$this->init_user();

return $this->email;
}

/**
* Get plan id.
*
* @return int
*/
public function get_plan_id() {
$this->init_user();

return $this->plan_id;
}

/**
* Get user quota.
*
* @return int
*/
public function get_quota() {
$this->init_user();

return $this->quota;
}
}
2 changes: 1 addition & 1 deletion inc/functions/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ function imagify_cache_user() {
}
}

$data->quota_formatted = imagify_size_format( $user->quota * pow( 1024, 2 ) );
$data->quota_formatted = imagify_size_format( $user->get_quota() * pow( 1024, 2 ) );
$data->next_date_update_formatted = date_i18n( get_option( 'date_format' ), strtotime( $user->next_date_update ) );

if ( imagify_is_active_for_network() ) {
Expand Down
10 changes: 10 additions & 0 deletions views/admin/admin-bar-pricing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );

if ( $data['upgrade_pricing'] ) : ?>
<button data-nonce="<?php echo wp_create_nonce( 'imagify_get_pricing_' . get_current_user_id() ); ?>"
data-target="#imagify-pricing-modal" type="button"
class="imagify-get-pricing-modal imagify-modal-trigger imagify-admin-bar-upgrade-plan">
<?php esc_html_e( 'Upgrade Plan', 'imagify' ); ?>
</button>
<?php endif; ?>
Loading