Skip to content

Commit

Permalink
Closes #2883: Imagify Plugin - Replace the documentation link with th…
Browse files Browse the repository at this point in the history
…e upgrade one for Free users (#900)

Co-authored-by: Rémy Perona <[email protected]>
  • Loading branch information
jeawhanlee and Tabrisrp authored Oct 4, 2024
1 parent dc2a835 commit 75c1aa7
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 29 deletions.
23 changes: 23 additions & 0 deletions Tests/Fixtures/classes/Admin/AdminSubscriber/pluginActionLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

return [
'test_data' => [
'testShouldReturnDocumentationLinkAmongPluginLinksIfPlanLabelIsNotStarter' => [
'config' => [
'plan_id' => 2,
],
'expected' => [
'Documentation'
],
],
'testShouldReturnUpgradeLinkAmongPluginLinksIfPlanLabelIsStarter' => [
'config' => [
'plan_id' => 1,
],
'expected' => [
'Upgrade',
'class="imagify-plugin-upgrade"'
],
],
]
];
42 changes: 42 additions & 0 deletions Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace Imagify\Tests\Unit\classes;

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

/**
* Tests for \Imagify\Admin\AdminSubscriber->plugin_action_links().
*
* @covers \Imagify\Admin\AdminSubscriber::plugin_action_links
* @group ImagifyAPI
*/
class Test_PluginActionLinks extends TestCase {
protected $admin_subscriber, $user, $plan_id;

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

$this->user = Mockery::mock( User::class );
$this->admin_subscriber = new AdminSubscriber( $this->user );
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected ) {
$this->user->plan_id = $config['plan_id'];

Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' );
Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' );

$plugin_action_links = $this->admin_subscriber->plugin_action_links([]);
$plugin_action_links = implode( '|', $plugin_action_links );

foreach ( $expected as $text ) {
$this->assertStringContainsString( $text, $plugin_action_links );
}
}
}
26 changes: 26 additions & 0 deletions Tests/Unit/inc/classes/ImagifyUser/getError.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,43 @@ public function testShouldReturnFalseWhenFetchedUserData() {
'is_monthly' => true,
];

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

$this->assertFalse( ( new User() )->get_error() );
}

/**
* Test \Imagify\User\User() should return cached user data if available.
*/
public function testShouldReturnFromCachedUserDataIfAvailable() {
$userData = (object) [
'id' => 1,
'email' => '[email protected]',
'plan_id' => '1',
'plan_label' => 'free',
'quota' => 456,
'extra_quota' => 0,
'extra_quota_consumed' => 0,
'consumed_current_month_quota' => 123,
'next_date_update' => '',
'is_active' => 1,
'is_monthly' => true,
];

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

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

/**
* Test \Imagify\User\User->get_error() should return a WP_Error object when couldn’t fetch user account data.
*/
public function testShouldReturnErrorWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $wp_error );

$this->assertSame( $wp_error, ( new User() )->get_error() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Test_GetPercentConsumedQuota extends TestCase {
public function testShouldReturnZeroWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $wp_error );
Functions\expect( 'imagify_round_half_five' )->never();

Expand All @@ -47,6 +48,7 @@ public function testShouldReturnQuotaWhenFetchedUserData() {
'is_monthly' => true,
];

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );
Functions\expect( 'imagify_round_half_five' )
->twice()
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Test_IsOverQuota extends TestCase {
public function testShouldReturnFalseWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $wp_error );

$this->assertFalse( ( new User() )->is_over_quota() );
Expand All @@ -45,6 +46,7 @@ public function testShouldReturnFalseWhenPaidAccount() {
'is_monthly' => true,
];

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

$this->assertFalse( ( new User() )->is_over_quota() );
Expand Down Expand Up @@ -97,6 +99,7 @@ public function testShouldReturnTrueWhenFreeOverQuota() {
}

private function createMocks( $userData, $dataPreviousQuotaPercent ) {
Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );
Functions\expect( 'imagify_round_half_five' )
->once()
Expand Down
1 change: 1 addition & 0 deletions Tests/Unit/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<testsuites>
<testsuite name="unit">
<directory suffix=".php">inc</directory>
<directory suffix=".php">classes</directory>
</testsuite>
</testsuites>

Expand Down
4 changes: 4 additions & 0 deletions assets/css/admin-bar.css
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,7 @@
content: "\2715";
color: #fff;
}
.imagify-plugin-upgrade {
color: #6f9c3b;
font-weight: 600;
}
2 changes: 1 addition & 1 deletion assets/css/admin-bar.min.css

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

67 changes: 67 additions & 0 deletions classes/Admin/AdminSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);

namespace Imagify\Admin;

use Imagify\EventManagement\SubscriberInterface;
use Imagify\User\User;

/**
* Admin Subscriber
*/
class AdminSubscriber implements SubscriberInterface {

/**
* User instance.
*
* @var User
*/
protected $user;

/**
* Instantiate the class
*
* @param User $user User instance.
*/
public function __construct( User $user ) {
$this->user = $user;
}

/**
* Returns an array of events this subscriber listens to
*
* @return array
*/
public static function get_subscribed_events() {
$basename = plugin_basename( IMAGIFY_FILE );

return [
'plugin_action_links_' . $basename => 'plugin_action_links',
'network_admin_plugin_action_links_' . $basename => 'plugin_action_links',
];
}

/**
* Add links to the plugin row in the plugins list.
*
* @since 1.7
*
* @param array $actions An array of action links.
* @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"';

array_unshift( $actions, sprintf( '<a href="%s" target="_blank"%s>%s</a>',
esc_url( imagify_get_external_url( $url ) ),
$class,
$text
) );

array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url( 'bulk-optimization' ) ), __( 'Bulk Optimization', 'imagify' ) ) );
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url() ), __( 'Settings', 'imagify' ) ) );
return $actions;
}
}
49 changes: 49 additions & 0 deletions classes/Admin/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace Imagify\Admin;

use Imagify\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use Imagify\Admin\AdminSubscriber;

/**
* Service provider for Admin.
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* Services provided by this provider
*
* @var array
*/
protected $provides = [
'admin_subscriber',
];

/**
* Subscribers provided by this provider
*
* @var array
*/
public $subscribers = [
'admin_subscriber',
];

/**
* Registers the provided classes
*
* @return void
*/
public function register() {
$this->getContainer()->share( 'admin_subscriber', AdminSubscriber::class )
->addArgument( $this->getContainer()->get( 'user' ) );
}

/**
* Returns the subscribers array
*
* @return array
*/
public function get_subscribers() {
return $this->subscribers;
}
}
2 changes: 1 addition & 1 deletion classes/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class User {
* @return void
*/
public function __construct() {
$user = get_imagify_user();
$user = imagify_get_cached_user() ?: get_imagify_user();

if ( is_wp_error( $user ) ) {
$this->error = $user;
Expand Down
3 changes: 2 additions & 1 deletion config/providers.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
return [
'Imagify\User\ServiceProvider',
'Imagify\Avif\ServiceProvider',
'Imagify\CDN\ServiceProvider',
'Imagify\Picture\ServiceProvider',
'Imagify\Stats\ServiceProvider',
'Imagify\Webp\ServiceProvider',
'Imagify\User\ServiceProvider',
'Imagify\Admin\ServiceProvider',
];
26 changes: 0 additions & 26 deletions inc/classes/class-imagify-views.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ public function init() {
add_action( 'network_admin_menu', [ $this, 'add_network_menus' ] );
}

// Action links in plugins list.
$basename = plugin_basename( IMAGIFY_FILE );
add_filter( 'plugin_action_links_' . $basename, [ $this, 'plugin_action_links' ] );
add_filter( 'network_admin_plugin_action_links_' . $basename, [ $this, 'plugin_action_links' ] );

// Save the "per page" option value from the files list screen.
add_filter( 'set-screen-option', [ 'Imagify_Files_List_Table', 'save_screen_options' ], 10, 3 );

Expand Down Expand Up @@ -208,27 +203,6 @@ public function add_network_menus() {
}
}


/** ----------------------------------------------------------------------------------------- */
/** PLUGIN ACTION LINKS ===================================================================== */
/** ----------------------------------------------------------------------------------------- */

/**
* Add links to the plugin row in the plugins list.
*
* @since 1.7
*
* @param array $actions An array of action links.
* @return array
*/
public function plugin_action_links( $actions ) {
array_unshift( $actions, sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( imagify_get_external_url( 'documentation' ) ), __( 'Documentation', 'imagify' ) ) );
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url( 'bulk-optimization' ) ), __( 'Bulk Optimization', 'imagify' ) ) );
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url() ), __( 'Settings', 'imagify' ) ) );
return $actions;
}


/** ----------------------------------------------------------------------------------------- */
/** MAIN PAGE TEMPLATES ===================================================================== */
/** ----------------------------------------------------------------------------------------- */
Expand Down

0 comments on commit 75c1aa7

Please sign in to comment.