-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #2883: Imagify Plugin - Replace the documentation link with th…
…e upgrade one for Free users (#900) Co-authored-by: Rémy Perona <[email protected]>
- Loading branch information
1 parent
dc2a835
commit 75c1aa7
Showing
13 changed files
with
221 additions
and
29 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
Tests/Fixtures/classes/Admin/AdminSubscriber/pluginActionLinks.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() ); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,3 +197,7 @@ | |
content: "\2715"; | ||
color: #fff; | ||
} | ||
.imagify-plugin-upgrade { | ||
color: #6f9c3b; | ||
font-weight: 600; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters