From 470c266635566e11153688f1de862af1a3a5073f Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 25 Sep 2024 17:53:36 +0100 Subject: [PATCH 01/24] Return cached user data if exist --- classes/User/User.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/User/User.php b/classes/User/User.php index 6786152ac..fa9c184ab 100755 --- a/classes/User/User.php +++ b/classes/User/User.php @@ -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; From 17981c3cde9f302dd09b5d673b673a8b3d7dbc3c Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 25 Sep 2024 17:54:19 +0100 Subject: [PATCH 02/24] Check user starter user plan and add upgrade link --- inc/classes/class-imagify-views.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/inc/classes/class-imagify-views.php b/inc/classes/class-imagify-views.php index 726641027..0386f556c 100644 --- a/inc/classes/class-imagify-views.php +++ b/inc/classes/class-imagify-views.php @@ -222,7 +222,17 @@ public function add_network_menus() { * @return array */ public function plugin_action_links( $actions ) { - array_unshift( $actions, sprintf( '%s', esc_url( imagify_get_external_url( 'documentation' ) ), __( 'Documentation', 'imagify' ) ) ); + $user = new User(); + $text = 1 !== $user->plan_id ? __( 'Documentation', 'imagify' ) : __( 'Upgrade', 'imagify' ); + $url = 1 !== $user->plan_id ? 'documentation' : 'subscription'; + $class = 1 !== $user->plan_id ? '' : ' class="imagify-plugin-upgrade"'; + + array_unshift( $actions, sprintf( '%s', + esc_url( imagify_get_external_url( $url ) ), + $class, + $text + ) ); + array_unshift( $actions, sprintf( '%s', esc_url( get_imagify_admin_url( 'bulk-optimization' ) ), __( 'Bulk Optimization', 'imagify' ) ) ); array_unshift( $actions, sprintf( '%s', esc_url( get_imagify_admin_url() ), __( 'Settings', 'imagify' ) ) ); return $actions; From 62690999c79c669ead0049ff44a27746d9d7c54a Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 25 Sep 2024 17:54:46 +0100 Subject: [PATCH 03/24] Updated style --- assets/css/admin-bar.css | 4 ++++ assets/css/admin-bar.min.css | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/assets/css/admin-bar.css b/assets/css/admin-bar.css index 92936f18a..f64be278d 100755 --- a/assets/css/admin-bar.css +++ b/assets/css/admin-bar.css @@ -197,3 +197,7 @@ content: "\2715"; color: #fff; } +.imagify-plugin-upgrade { + color: #8BC34A; + font-weight: 600; +} diff --git a/assets/css/admin-bar.min.css b/assets/css/admin-bar.min.css index 90142c9a1..6229d82b2 100755 --- a/assets/css/admin-bar.min.css +++ b/assets/css/admin-bar.min.css @@ -1 +1 @@ -.imagify-account,.imagify-account-link{padding-right:15px}.imagify-meteo-icon{display:inline-block;height:38px;vertical-align:middle;margin-right:10px}.imagify-user-plan{color:#40b1d0}.imagify-meteo-title.imagify-meteo-title{color:#fff;font-size:17px}.imagify-space-left>p{color:#fff}#wp-admin-bar-imagify-profile [class^=imagify-bar-]{position:relative;height:1.5em;width:100%;background:#60758d;color:#fff;font-size:10px}#wp-admin-bar-imagify-profile .imagify-progress{height:1.5em;font-size:1em}.imagify-progress{-webkit-transition:width .3s;-o-transition:width .3s;transition:width .3s}.imagify-bar-positive .imagify-progress{background:#8cc152}.imagify-bar-positive .imagify-barnb{color:#8cc152}.imagify-bar-negative .imagify-progress{background:#73818c}.imagify-bar-negative .imagify-barnb{color:#73818c}.imagify-bar-neutral .imagify-progress{background:#f5a623}.imagify-space-left .imagify-bar-negative .imagify-progress{background:#d0021b}#wpadminbar #wp-admin-bar-imagify-profile *{line-height:1.5;white-space:initial}#wpadminbar #wp-admin-bar-imagify .ab-submenu{padding-bottom:0}#wpadminbar #wp-admin-bar-imagify-profile .ab-item{height:auto;padding:0 13px}#wpadminbar #wp-admin-bar-imagify-profile{min-width:200px;padding:15px 0 10px;margin-top:.7em;background:#222}#wp-admin-bar-imagify .dashicons{font-family:dashicons;font-size:18px;vertical-align:middle;margin:0 5px 0 0}#wp-admin-bar-imagify .button-text{display:inline-block;vertical-align:middle}#wp-admin-bar-imagify .imagify-abq-row{display:table;width:100%}#wp-admin-bar-imagify .imagify-abq-row+.imagify-abq-row{margin-top:.75em}#wp-admin-bar-imagify .imagify-abq-row>*{display:table-cell}#wp-admin-bar-imagify-profile .imagify-meteo-icon{padding-right:7px}#wp-admin-bar-imagify-profile .imagify-meteo-icon img{width:37px}#wp-admin-bar-imagify-profile .imagify-meteo-title{font-size:17px}#wp-admin-bar-imagify-profile .imagify-meteo-subs{color:#72889f}#wpadminbar #wp-admin-bar-imagify-profile strong{font-weight:700}#wpadminbar #wp-admin-bar-imagify-profile .imagify-user-plan,#wpadminbar #wp-admin-bar-imagify-profile a{padding:0;color:#40b1d0}#wpadminbar #wp-admin-bar-imagify-profile .imagify-account-link{display:table}#wpadminbar #wp-admin-bar-imagify-profile .imagify-account-link>*{display:table-cell}#wpadminbar #wp-admin-bar-imagify-profile .imagify-space-left{max-width:210px;min-width:210px;width:210px}#wpadminbar #wp-admin-bar-imagify-profile .imagify-space-left p{font-size:12px}#wp-admin-bar-imagify-profile .imagify-error,#wp-admin-bar-imagify-profile .imagify-warning{padding:10px;margin:0 -13px -13px}#wp-admin-bar-imagify-profile .imagify-error p+p,#wp-admin-bar-imagify-profile .imagify-warning p+p{margin-top:.5em}#wp-admin-bar-imagify-profile .imagify-error p+p+p,#wp-admin-bar-imagify-profile .imagify-warning p+p+p{margin-top:1em}#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost{display:inline-block;height:auto;padding:7px 10px;border:1px solid #fff;text-align:center;background:0 0;color:#fff;border-radius:3px;-webkit-transition:all .275s;-o-transition:all .275s;transition:all .275s}#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost:focus,#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost:hover{background:#fff;color:#888}#wpadminbar .imagify-warning *{background:#f5a623;color:#fff;text-shadow:0 0 2px rgba(0,0,0,.2)}#wp-admin-bar-imagify-profile .imagify-upsell-admin-bar{position:relative;background:#c51161;margin:10px -13px -10px -13px;padding:20px}#wp-admin-bar-imagify-profile .imagify-upsell-admin-bar p{color:#fff}#wp-admin-bar-imagify-profile a.imagify-upsell-admin-bar-button{display:block;height:auto!important;border:1px solid #fff;border-radius:5px;color:#fff!important;padding:5px 10px!important;text-align:center;text-decoration:none;margin-top:10px}#wpadminbar #wp-admin-bar-imagify-profile a.imagify-upsell-dismiss{display:inline!important;height:auto!important}#wpadminbar #wp-admin-bar-imagify-profile .imagify-upsell-dismiss::before{position:absolute;top:5px;right:10px;content:"\2715";color:#fff} \ No newline at end of file +.imagify-account,.imagify-account-link{padding-right:15px}.imagify-meteo-icon{display:inline-block;height:38px;vertical-align:middle;margin-right:10px}.imagify-user-plan{color:#40b1d0}.imagify-meteo-title.imagify-meteo-title{color:#fff;font-size:17px}.imagify-space-left>p{color:#fff}#wp-admin-bar-imagify-profile [class^=imagify-bar-]{position:relative;height:1.5em;width:100%;background:#60758d;color:#fff;font-size:10px}#wp-admin-bar-imagify-profile .imagify-progress{height:1.5em;font-size:1em}.imagify-progress{-webkit-transition:width .3s;-o-transition:width .3s;transition:width .3s}.imagify-bar-positive .imagify-progress{background:#8cc152}.imagify-bar-positive .imagify-barnb{color:#8cc152}.imagify-bar-negative .imagify-progress{background:#73818c}.imagify-bar-negative .imagify-barnb{color:#73818c}.imagify-bar-neutral .imagify-progress{background:#f5a623}.imagify-space-left .imagify-bar-negative .imagify-progress{background:#d0021b}#wpadminbar #wp-admin-bar-imagify-profile *{line-height:1.5;white-space:initial}#wpadminbar #wp-admin-bar-imagify .ab-submenu{padding-bottom:0}#wpadminbar #wp-admin-bar-imagify-profile .ab-item{height:auto;padding:0 13px}#wpadminbar #wp-admin-bar-imagify-profile{min-width:200px;padding:15px 0 10px;margin-top:.7em;background:#222}#wp-admin-bar-imagify .dashicons{font-family:dashicons;font-size:18px;vertical-align:middle;margin:0 5px 0 0}#wp-admin-bar-imagify .button-text{display:inline-block;vertical-align:middle}#wp-admin-bar-imagify .imagify-abq-row{display:table;width:100%}#wp-admin-bar-imagify .imagify-abq-row+.imagify-abq-row{margin-top:.75em}#wp-admin-bar-imagify .imagify-abq-row>*{display:table-cell}#wp-admin-bar-imagify-profile .imagify-meteo-icon{padding-right:7px}#wp-admin-bar-imagify-profile .imagify-meteo-icon img{width:37px}#wp-admin-bar-imagify-profile .imagify-meteo-title{font-size:17px}#wp-admin-bar-imagify-profile .imagify-meteo-subs{color:#72889f}#wpadminbar #wp-admin-bar-imagify-profile strong{font-weight:700}#wpadminbar #wp-admin-bar-imagify-profile .imagify-user-plan,#wpadminbar #wp-admin-bar-imagify-profile a{padding:0;color:#40b1d0}#wpadminbar #wp-admin-bar-imagify-profile .imagify-account-link{display:table}#wpadminbar #wp-admin-bar-imagify-profile .imagify-account-link>*{display:table-cell}#wpadminbar #wp-admin-bar-imagify-profile .imagify-space-left{max-width:210px;min-width:210px;width:210px}#wpadminbar #wp-admin-bar-imagify-profile .imagify-space-left p{font-size:12px}#wp-admin-bar-imagify-profile .imagify-error,#wp-admin-bar-imagify-profile .imagify-warning{padding:10px;margin:0 -13px -13px}#wp-admin-bar-imagify-profile .imagify-error p+p,#wp-admin-bar-imagify-profile .imagify-warning p+p{margin-top:.5em}#wp-admin-bar-imagify-profile .imagify-error p+p+p,#wp-admin-bar-imagify-profile .imagify-warning p+p+p{margin-top:1em}#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost{display:inline-block;height:auto;padding:7px 10px;border:1px solid #fff;text-align:center;background:0 0;color:#fff;border-radius:3px;-webkit-transition:all .275s;-o-transition:all .275s;transition:all .275s}#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost:focus,#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost:hover{background:#fff;color:#888}#wpadminbar .imagify-warning *{background:#f5a623;color:#fff;text-shadow:0 0 2px rgba(0,0,0,.2)}#wp-admin-bar-imagify-profile .imagify-upsell-admin-bar{position:relative;background:#c51161;margin:10px -13px -10px -13px;padding:20px}#wp-admin-bar-imagify-profile .imagify-upsell-admin-bar p{color:#fff}#wp-admin-bar-imagify-profile a.imagify-upsell-admin-bar-button{display:block;height:auto!important;border:1px solid #fff;border-radius:5px;color:#fff!important;padding:5px 10px!important;text-align:center;text-decoration:none;margin-top:10px}#wpadminbar #wp-admin-bar-imagify-profile a.imagify-upsell-dismiss{display:inline!important;height:auto!important}#wpadminbar #wp-admin-bar-imagify-profile .imagify-upsell-dismiss::before{position:absolute;top:5px;right:10px;content:"\2715";color:#fff}.imagify-plugin-upgrade{color:#8bc34a;font-weight:600} \ No newline at end of file From 1b9d5a077ad43b28635b63a1aef70777d34f8265 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 25 Sep 2024 20:09:37 +0100 Subject: [PATCH 04/24] Check function exist for failing test --- inc/functions/common.php | 138 ++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 68 deletions(-) diff --git a/inc/functions/common.php b/inc/functions/common.php index ac4bffc3a..b2cd21bd9 100755 --- a/inc/functions/common.php +++ b/inc/functions/common.php @@ -238,90 +238,92 @@ function imagify_can_optimize_custom_folders() { return $can; } -/** - * Simple helper to get some external URLs, like to the documentation. - * - * @since 1.6.12 - * @author Grégory Viguier - * - * @param string $target What we want. - * @param array $query_args An array of query arguments. - * @return string The URL. - */ -function imagify_get_external_url( $target, $query_args = array() ) { - $site_url = IMAGIFY_SITE_DOMAIN . '/'; - $app_url = IMAGIFY_APP_DOMAIN . '/#/'; - - switch ( $target ) { - case 'plugin': - /* translators: Plugin URI of the plugin/theme */ - $url = __( 'https://wordpress.org/plugins/imagify/', 'imagify' ); - break; +if ( ! function_exists( 'imagify_get_external_url' ) ) { + /** + * Simple helper to get some external URLs, like to the documentation. + * + * @since 1.6.12 + * @author Grégory Viguier + * + * @param string $target What we want. + * @param array $query_args An array of query arguments. + * @return string The URL. + */ + function imagify_get_external_url( $target, $query_args = array() ) { + $site_url = IMAGIFY_SITE_DOMAIN . '/'; + $app_url = IMAGIFY_APP_DOMAIN . '/#/'; + + switch ( $target ) { + case 'plugin': + /* translators: Plugin URI of the plugin/theme */ + $url = __( 'https://wordpress.org/plugins/imagify/', 'imagify' ); + break; - case 'rate': - $url = 'https://wordpress.org/support/view/plugin-reviews/imagify?rate=5#postform'; - break; + case 'rate': + $url = 'https://wordpress.org/support/view/plugin-reviews/imagify?rate=5#postform'; + break; - case 'share-twitter': - $url = rawurlencode( imagify_get_external_url( 'plugin' ) ); - $url = 'https://twitter.com/intent/tweet?source=webclient&original_referer=' . $url . '&url=' . $url . '&related=imagify&hastags=performance,web,wordpress'; - break; + case 'share-twitter': + $url = rawurlencode( imagify_get_external_url( 'plugin' ) ); + $url = 'https://twitter.com/intent/tweet?source=webclient&original_referer=' . $url . '&url=' . $url . '&related=imagify&hastags=performance,web,wordpress'; + break; - case 'share-facebook': - $url = rawurlencode( imagify_get_external_url( 'plugin' ) ); - $url = 'https://www.facebook.com/sharer/sharer.php?u=' . $url; - break; + case 'share-facebook': + $url = rawurlencode( imagify_get_external_url( 'plugin' ) ); + $url = 'https://www.facebook.com/sharer/sharer.php?u=' . $url; + break; - case 'contact': - $lang = imagify_get_current_lang_in( 'fr' ); - $paths = array( - 'en' => 'contact', - 'fr' => 'fr/contact', - ); + case 'contact': + $lang = imagify_get_current_lang_in( 'fr' ); + $paths = array( + 'en' => 'contact', + 'fr' => 'fr/contact', + ); - $url = $site_url . $paths[ $lang ] . '/'; - break; + $url = $site_url . $paths[ $lang ] . '/'; + break; - case 'documentation': - $url = $site_url . 'documentation/'; - break; + case 'documentation': + $url = $site_url . 'documentation/'; + break; - case 'documentation-imagick-gd': - $url = $site_url . 'documentation/solve-imagemagick-gd-required/'; - break; + case 'documentation-imagick-gd': + $url = $site_url . 'documentation/solve-imagemagick-gd-required/'; + break; - case 'register': - $partner = imagify_get_partner(); + case 'register': + $partner = imagify_get_partner(); - if ( $partner ) { - $query_args['partner'] = $partner; - } + if ( $partner ) { + $query_args['partner'] = $partner; + } - $url = $app_url . 'register'; - break; + $url = $app_url . 'register'; + break; - case 'subscription': - $url = $app_url . 'subscription'; - break; + case 'subscription': + $url = $app_url . 'subscription'; + break; - case 'get-api-key': - $url = $app_url . 'api'; - break; + case 'get-api-key': + $url = $app_url . 'api'; + break; - case 'payment': - // Don't remove the trailing slash. - $url = $app_url . 'plugin/'; - break; + case 'payment': + // Don't remove the trailing slash. + $url = $app_url . 'plugin/'; + break; - default: - return ''; - } + default: + return ''; + } - if ( $query_args ) { - $url = add_query_arg( $query_args, $url ); - } + if ( $query_args ) { + $url = add_query_arg( $query_args, $url ); + } - return $url; + return $url; + } } /** From dc87e1cce58c49d383407265de4ac031a39c44bf Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 25 Sep 2024 20:09:54 +0100 Subject: [PATCH 05/24] Updated tests --- .../Unit/inc/classes/ImagifyUser/getError.php | 26 +++++++++++++++++++ .../ImagifyUser/getPercentConsumedQuota.php | 2 ++ .../inc/classes/ImagifyUser/isOverQuota.php | 3 +++ 3 files changed, 31 insertions(+) diff --git a/Tests/Unit/inc/classes/ImagifyUser/getError.php b/Tests/Unit/inc/classes/ImagifyUser/getError.php index c13bf70b0..035012cf3 100644 --- a/Tests/Unit/inc/classes/ImagifyUser/getError.php +++ b/Tests/Unit/inc/classes/ImagifyUser/getError.php @@ -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' => 'imagify@example.com', + '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( 'imagify@example.com', ( 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() ); diff --git a/Tests/Unit/inc/classes/ImagifyUser/getPercentConsumedQuota.php b/Tests/Unit/inc/classes/ImagifyUser/getPercentConsumedQuota.php index 5d50b89a6..a981d3389 100644 --- a/Tests/Unit/inc/classes/ImagifyUser/getPercentConsumedQuota.php +++ b/Tests/Unit/inc/classes/ImagifyUser/getPercentConsumedQuota.php @@ -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(); @@ -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() diff --git a/Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php b/Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php index 35f2bc20d..8fbd48b1f 100644 --- a/Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php +++ b/Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php @@ -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() ); @@ -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() ); @@ -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() From 9326a116a26704032e591d95052e43472ec1c5a9 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 25 Sep 2024 20:10:09 +0100 Subject: [PATCH 06/24] Added new test --- .../classImagifyViews/pluginActionLinks.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php diff --git a/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php b/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php new file mode 100644 index 000000000..803ae4d3c --- /dev/null +++ b/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php @@ -0,0 +1,90 @@ +plugin_action_links(). + * + * @covers \Imagify_Views::plugin_action_links + * @group ImagifyAPI + */ +class Test_PluginActionLinks extends TestCase { + protected $imagify_views; + + public function setUp(): void { + parent::setUp(); + + // Create Imagify_Views instance without calling the constructor + $reflection = new \ReflectionClass(Imagify_Views::class); + $this->imagify_views = $reflection->newInstanceWithoutConstructor(); + } + + /** + * Test \Imagify_Views->plugin_action_links() should return Documentation link if plan label is not starter. + */ + public function testShouldReturnDocumentationLinkAmongPluginLinksIfPlanLabelIsNotStarter() { + $userData = (object) [ + 'id' => 1, + 'email' => 'imagify@example.com', + 'plan_id' => '2', + '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\when( 'get_imagify_user' )->justReturn( $userData ); + Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' ); + Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' ); + + $reflection = new \ReflectionClass(Imagify_Views::class); + $imagify_views = $reflection->newInstanceWithoutConstructor(); + + $plugin_action_links = $this->imagify_views->plugin_action_links([]); + $plugin_action_links = implode( '|', $plugin_action_links ); + + $this->assertStringContainsString( 'Documentation', $plugin_action_links ); + } + + /** + * Test \Imagify_Views->plugin_action_links() should return Upgrade link if plan label is starter. + */ + public function testShouldReturnUpgradeLinkAmongPluginLinksIfPlanLabelIsStarter() { + $userData = (object) [ + 'id' => 1, + 'email' => 'imagify@example.com', + '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\when( 'get_imagify_user' )->justReturn( $userData ); + Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' ); + Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' ); + + $reflection = new \ReflectionClass(Imagify_Views::class); + $imagify_views = $reflection->newInstanceWithoutConstructor(); + + $plugin_action_links = $this->imagify_views->plugin_action_links([]); + $plugin_action_links = implode( '|', $plugin_action_links ); + + $this->assertStringContainsString( 'Upgrade', $plugin_action_links ); + $this->assertStringContainsString( 'class="imagify-plugin-upgrade"', $plugin_action_links ); + } +} From 408a823f1bae8ccd2ab2dd1a58c48e6ad914f602 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 25 Sep 2024 20:21:40 +0100 Subject: [PATCH 07/24] Updated test --- .../classImagifyViews/pluginActionLinks.php | 50 +++++++------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php b/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php index 803ae4d3c..21008e2a7 100644 --- a/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php +++ b/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php @@ -3,7 +3,6 @@ use Brain\Monkey\Functions; use Imagify\Tests\Unit\TestCase; -use Imagify\User\User; use Imagify_Views; /** @@ -14,6 +13,19 @@ */ class Test_PluginActionLinks extends TestCase { protected $imagify_views; + protected $user_data = [ + 'id' => 1, + 'email' => 'imagify@example.com', + 'plan_id' => '2', + '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, + ]; public function setUp(): void { parent::setUp(); @@ -27,22 +39,8 @@ public function setUp(): void { * Test \Imagify_Views->plugin_action_links() should return Documentation link if plan label is not starter. */ public function testShouldReturnDocumentationLinkAmongPluginLinksIfPlanLabelIsNotStarter() { - $userData = (object) [ - 'id' => 1, - 'email' => 'imagify@example.com', - 'plan_id' => '2', - '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\when( 'get_imagify_user' )->justReturn( $userData ); + Functions\when( 'imagify_get_cached_user' )->justReturn( (object) $this->user_data ); + Functions\when( 'get_imagify_user' )->justReturn( (object) $this->user_data ); Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' ); Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' ); @@ -59,22 +57,10 @@ public function testShouldReturnDocumentationLinkAmongPluginLinksIfPlanLabelIsNo * Test \Imagify_Views->plugin_action_links() should return Upgrade link if plan label is starter. */ public function testShouldReturnUpgradeLinkAmongPluginLinksIfPlanLabelIsStarter() { - $userData = (object) [ - 'id' => 1, - 'email' => 'imagify@example.com', - '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, - ]; + $this->user_data['plan_id'] = '1'; - Functions\when( 'imagify_get_cached_user' )->justReturn( $userData ); - Functions\when( 'get_imagify_user' )->justReturn( $userData ); + Functions\when( 'imagify_get_cached_user' )->justReturn( (object) $this->user_data ); + Functions\when( 'get_imagify_user' )->justReturn( (object) $this->user_data ); Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' ); Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' ); From a5774c20c8ef77c45259f42b4c6830f983a92796 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 25 Sep 2024 20:25:11 +0100 Subject: [PATCH 08/24] Updated test --- .../inc/classes/classImagifyViews/pluginActionLinks.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php b/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php index 21008e2a7..e87b628dd 100644 --- a/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php +++ b/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php @@ -44,9 +44,6 @@ public function testShouldReturnDocumentationLinkAmongPluginLinksIfPlanLabelIsNo Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' ); Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' ); - $reflection = new \ReflectionClass(Imagify_Views::class); - $imagify_views = $reflection->newInstanceWithoutConstructor(); - $plugin_action_links = $this->imagify_views->plugin_action_links([]); $plugin_action_links = implode( '|', $plugin_action_links ); @@ -64,9 +61,6 @@ public function testShouldReturnUpgradeLinkAmongPluginLinksIfPlanLabelIsStarter( Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' ); Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' ); - $reflection = new \ReflectionClass(Imagify_Views::class); - $imagify_views = $reflection->newInstanceWithoutConstructor(); - $plugin_action_links = $this->imagify_views->plugin_action_links([]); $plugin_action_links = implode( '|', $plugin_action_links ); From f8c2c188ab2150b341bb1c97354f5d42034b80bb Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 25 Sep 2024 20:26:34 +0100 Subject: [PATCH 09/24] Updated test --- Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php b/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php index e87b628dd..e1abf710a 100644 --- a/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php +++ b/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php @@ -1,5 +1,5 @@ Date: Mon, 30 Sep 2024 10:52:04 +0100 Subject: [PATCH 10/24] Removed plugin action links --- inc/classes/class-imagify-views.php | 36 ----------------------------- 1 file changed, 36 deletions(-) diff --git a/inc/classes/class-imagify-views.php b/inc/classes/class-imagify-views.php index 0386f556c..64b015083 100644 --- a/inc/classes/class-imagify-views.php +++ b/inc/classes/class-imagify-views.php @@ -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 ); @@ -208,37 +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 ) { - $user = new User(); - $text = 1 !== $user->plan_id ? __( 'Documentation', 'imagify' ) : __( 'Upgrade', 'imagify' ); - $url = 1 !== $user->plan_id ? 'documentation' : 'subscription'; - $class = 1 !== $user->plan_id ? '' : ' class="imagify-plugin-upgrade"'; - - array_unshift( $actions, sprintf( '%s', - esc_url( imagify_get_external_url( $url ) ), - $class, - $text - ) ); - - array_unshift( $actions, sprintf( '%s', esc_url( get_imagify_admin_url( 'bulk-optimization' ) ), __( 'Bulk Optimization', 'imagify' ) ) ); - array_unshift( $actions, sprintf( '%s', esc_url( get_imagify_admin_url() ), __( 'Settings', 'imagify' ) ) ); - return $actions; - } - - /** ----------------------------------------------------------------------------------------- */ /** MAIN PAGE TEMPLATES ===================================================================== */ /** ----------------------------------------------------------------------------------------- */ From 60781817e2b24fedd0be29f12f300259bd6dc8a4 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 10:53:44 +0100 Subject: [PATCH 11/24] Add subscriber & service provider --- classes/Admin/AdminSubscriber.php | 64 +++++++++++++++++++++++++++++++ classes/Admin/ServiceProvider.php | 53 +++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 classes/Admin/AdminSubscriber.php create mode 100644 classes/Admin/ServiceProvider.php diff --git a/classes/Admin/AdminSubscriber.php b/classes/Admin/AdminSubscriber.php new file mode 100644 index 000000000..128ed2e07 --- /dev/null +++ b/classes/Admin/AdminSubscriber.php @@ -0,0 +1,64 @@ +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( '%s', + esc_url( imagify_get_external_url( $url ) ), + $class, + $text + ) ); + + array_unshift( $actions, sprintf( '%s', esc_url( get_imagify_admin_url( 'bulk-optimization' ) ), __( 'Bulk Optimization', 'imagify' ) ) ); + array_unshift( $actions, sprintf( '%s', esc_url( get_imagify_admin_url() ), __( 'Settings', 'imagify' ) ) ); + return $actions; + } +} diff --git a/classes/Admin/ServiceProvider.php b/classes/Admin/ServiceProvider.php new file mode 100644 index 000000000..b11e2c6ba --- /dev/null +++ b/classes/Admin/ServiceProvider.php @@ -0,0 +1,53 @@ +getContainer()->add( 'user', User::class ); + + $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; + } +} From 504bb8c3792257c5c772db9f60d94f4865018021 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 10:54:05 +0100 Subject: [PATCH 12/24] Registered new service provider --- config/providers.php | 1 + 1 file changed, 1 insertion(+) diff --git a/config/providers.php b/config/providers.php index 0a40a32a6..80d2e4e59 100644 --- a/config/providers.php +++ b/config/providers.php @@ -5,4 +5,5 @@ 'Imagify\Picture\ServiceProvider', 'Imagify\Stats\ServiceProvider', 'Imagify\Webp\ServiceProvider', + 'Imagify\Admin\ServiceProvider', ]; From 95da848f27bba0fd84bca76bd5b162df4057cb76 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 11:03:01 +0100 Subject: [PATCH 13/24] Remove test in wrong path --- .../classImagifyViews/pluginActionLinks.php | 70 ------------------- 1 file changed, 70 deletions(-) delete mode 100644 Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php diff --git a/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php b/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php deleted file mode 100644 index e1abf710a..000000000 --- a/Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php +++ /dev/null @@ -1,70 +0,0 @@ -plugin_action_links(). - * - * @covers \Imagify_Views::plugin_action_links - * @group ImagifyAPI - */ -class Test_PluginActionLinks extends TestCase { - protected $imagify_views; - protected $user_data = [ - 'id' => 1, - 'email' => 'imagify@example.com', - 'plan_id' => '2', - '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, - ]; - - public function setUp(): void { - parent::setUp(); - - // Create Imagify_Views instance without calling the constructor - $reflection = new \ReflectionClass(Imagify_Views::class); - $this->imagify_views = $reflection->newInstanceWithoutConstructor(); - } - - /** - * Test \Imagify_Views->plugin_action_links() should return Documentation link if plan label is not starter. - */ - public function testShouldReturnDocumentationLinkAmongPluginLinksIfPlanLabelIsNotStarter() { - Functions\when( 'imagify_get_cached_user' )->justReturn( (object) $this->user_data ); - Functions\when( 'get_imagify_user' )->justReturn( (object) $this->user_data ); - Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' ); - Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' ); - - $plugin_action_links = $this->imagify_views->plugin_action_links([]); - $plugin_action_links = implode( '|', $plugin_action_links ); - - $this->assertStringContainsString( 'Documentation', $plugin_action_links ); - } - - /** - * Test \Imagify_Views->plugin_action_links() should return Upgrade link if plan label is starter. - */ - public function testShouldReturnUpgradeLinkAmongPluginLinksIfPlanLabelIsStarter() { - $this->user_data['plan_id'] = '1'; - - Functions\when( 'imagify_get_cached_user' )->justReturn( (object) $this->user_data ); - Functions\when( 'get_imagify_user' )->justReturn( (object) $this->user_data ); - Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' ); - Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' ); - - $plugin_action_links = $this->imagify_views->plugin_action_links([]); - $plugin_action_links = implode( '|', $plugin_action_links ); - - $this->assertStringContainsString( 'Upgrade', $plugin_action_links ); - $this->assertStringContainsString( 'class="imagify-plugin-upgrade"', $plugin_action_links ); - } -} From 2ccda9f926c2827261869f34aef4da1e09d0d413 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 12:43:47 +0100 Subject: [PATCH 14/24] revert changes --- inc/functions/common.php | 138 +++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 70 deletions(-) diff --git a/inc/functions/common.php b/inc/functions/common.php index b2cd21bd9..ac4bffc3a 100755 --- a/inc/functions/common.php +++ b/inc/functions/common.php @@ -238,92 +238,90 @@ function imagify_can_optimize_custom_folders() { return $can; } -if ( ! function_exists( 'imagify_get_external_url' ) ) { - /** - * Simple helper to get some external URLs, like to the documentation. - * - * @since 1.6.12 - * @author Grégory Viguier - * - * @param string $target What we want. - * @param array $query_args An array of query arguments. - * @return string The URL. - */ - function imagify_get_external_url( $target, $query_args = array() ) { - $site_url = IMAGIFY_SITE_DOMAIN . '/'; - $app_url = IMAGIFY_APP_DOMAIN . '/#/'; - - switch ( $target ) { - case 'plugin': - /* translators: Plugin URI of the plugin/theme */ - $url = __( 'https://wordpress.org/plugins/imagify/', 'imagify' ); - break; - - case 'rate': - $url = 'https://wordpress.org/support/view/plugin-reviews/imagify?rate=5#postform'; - break; +/** + * Simple helper to get some external URLs, like to the documentation. + * + * @since 1.6.12 + * @author Grégory Viguier + * + * @param string $target What we want. + * @param array $query_args An array of query arguments. + * @return string The URL. + */ +function imagify_get_external_url( $target, $query_args = array() ) { + $site_url = IMAGIFY_SITE_DOMAIN . '/'; + $app_url = IMAGIFY_APP_DOMAIN . '/#/'; + + switch ( $target ) { + case 'plugin': + /* translators: Plugin URI of the plugin/theme */ + $url = __( 'https://wordpress.org/plugins/imagify/', 'imagify' ); + break; - case 'share-twitter': - $url = rawurlencode( imagify_get_external_url( 'plugin' ) ); - $url = 'https://twitter.com/intent/tweet?source=webclient&original_referer=' . $url . '&url=' . $url . '&related=imagify&hastags=performance,web,wordpress'; - break; + case 'rate': + $url = 'https://wordpress.org/support/view/plugin-reviews/imagify?rate=5#postform'; + break; - case 'share-facebook': - $url = rawurlencode( imagify_get_external_url( 'plugin' ) ); - $url = 'https://www.facebook.com/sharer/sharer.php?u=' . $url; - break; + case 'share-twitter': + $url = rawurlencode( imagify_get_external_url( 'plugin' ) ); + $url = 'https://twitter.com/intent/tweet?source=webclient&original_referer=' . $url . '&url=' . $url . '&related=imagify&hastags=performance,web,wordpress'; + break; - case 'contact': - $lang = imagify_get_current_lang_in( 'fr' ); - $paths = array( - 'en' => 'contact', - 'fr' => 'fr/contact', - ); + case 'share-facebook': + $url = rawurlencode( imagify_get_external_url( 'plugin' ) ); + $url = 'https://www.facebook.com/sharer/sharer.php?u=' . $url; + break; - $url = $site_url . $paths[ $lang ] . '/'; - break; + case 'contact': + $lang = imagify_get_current_lang_in( 'fr' ); + $paths = array( + 'en' => 'contact', + 'fr' => 'fr/contact', + ); - case 'documentation': - $url = $site_url . 'documentation/'; - break; + $url = $site_url . $paths[ $lang ] . '/'; + break; - case 'documentation-imagick-gd': - $url = $site_url . 'documentation/solve-imagemagick-gd-required/'; - break; + case 'documentation': + $url = $site_url . 'documentation/'; + break; - case 'register': - $partner = imagify_get_partner(); + case 'documentation-imagick-gd': + $url = $site_url . 'documentation/solve-imagemagick-gd-required/'; + break; - if ( $partner ) { - $query_args['partner'] = $partner; - } + case 'register': + $partner = imagify_get_partner(); - $url = $app_url . 'register'; - break; + if ( $partner ) { + $query_args['partner'] = $partner; + } - case 'subscription': - $url = $app_url . 'subscription'; - break; + $url = $app_url . 'register'; + break; - case 'get-api-key': - $url = $app_url . 'api'; - break; + case 'subscription': + $url = $app_url . 'subscription'; + break; - case 'payment': - // Don't remove the trailing slash. - $url = $app_url . 'plugin/'; - break; + case 'get-api-key': + $url = $app_url . 'api'; + break; - default: - return ''; - } + case 'payment': + // Don't remove the trailing slash. + $url = $app_url . 'plugin/'; + break; - if ( $query_args ) { - $url = add_query_arg( $query_args, $url ); - } + default: + return ''; + } - return $url; + if ( $query_args ) { + $url = add_query_arg( $query_args, $url ); } + + return $url; } /** From 2f82a64c9255115330d27f886cfbdf1d2dc872d4 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 12:44:37 +0100 Subject: [PATCH 15/24] Added new directory --- Tests/Unit/phpunit.xml.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/Unit/phpunit.xml.dist b/Tests/Unit/phpunit.xml.dist index 02869f9be..9cf67a8c4 100644 --- a/Tests/Unit/phpunit.xml.dist +++ b/Tests/Unit/phpunit.xml.dist @@ -16,6 +16,7 @@ inc + classes From 91228cf2bb35496c5e7ab5924df58ed40382fbca Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 12:47:21 +0100 Subject: [PATCH 16/24] Apply phpcs fix --- classes/Admin/AdminSubscriber.php | 7 +++++-- classes/Admin/ServiceProvider.php | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/classes/Admin/AdminSubscriber.php b/classes/Admin/AdminSubscriber.php index 128ed2e07..edc79b397 100644 --- a/classes/Admin/AdminSubscriber.php +++ b/classes/Admin/AdminSubscriber.php @@ -6,6 +6,9 @@ use Imagify\EventManagement\SubscriberInterface; use Imagify\User\User; +/** + * Admin Subscriber + */ class AdminSubscriber implements SubscriberInterface { /** @@ -23,7 +26,7 @@ class AdminSubscriber implements SubscriberInterface { public function __construct( User $user ) { $this->user = $user; } - + /** * Returns an array of events this subscriber listens to * @@ -33,7 +36,7 @@ public static function get_subscribed_events() { $basename = plugin_basename( IMAGIFY_FILE ); return [ - 'plugin_action_links_' . $basename => 'plugin_action_links', + 'plugin_action_links_' . $basename => 'plugin_action_links', 'network_admin_plugin_action_links_' . $basename => 'plugin_action_links', ]; } diff --git a/classes/Admin/ServiceProvider.php b/classes/Admin/ServiceProvider.php index b11e2c6ba..465d1135b 100644 --- a/classes/Admin/ServiceProvider.php +++ b/classes/Admin/ServiceProvider.php @@ -18,7 +18,7 @@ class ServiceProvider extends AbstractServiceProvider { */ protected $provides = [ 'admin_subscriber', - 'user', + 'user', ]; /** @@ -36,10 +36,10 @@ class ServiceProvider extends AbstractServiceProvider { * @return void */ public function register() { - $this->getContainer()->add( 'user', User::class ); - + $this->getContainer()->add( 'user', User::class ); + $this->getContainer()->share( 'admin_subscriber', AdminSubscriber::class ) - ->addArgument( $this->getContainer()->get( 'user' ) ); + ->addArgument( $this->getContainer()->get( 'user' ) ); } /** From 65d0dcdc7e0244c45dc5478f164c89d7dea3bcc5 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 12:48:20 +0100 Subject: [PATCH 17/24] Updated tests --- .../AdminSubscriber/pluginActionLinks.php | 23 ++++++++++ .../AdminSubscriber/pluginActionLinks.php | 45 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 Tests/Fixtures/classes/Admin/AdminSubscriber/pluginActionLinks.php create mode 100644 Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php diff --git a/Tests/Fixtures/classes/Admin/AdminSubscriber/pluginActionLinks.php b/Tests/Fixtures/classes/Admin/AdminSubscriber/pluginActionLinks.php new file mode 100644 index 000000000..60db68cca --- /dev/null +++ b/Tests/Fixtures/classes/Admin/AdminSubscriber/pluginActionLinks.php @@ -0,0 +1,23 @@ + [ + 'testShouldReturnDocumentationLinkAmongPluginLinksIfPlanLabelIsNotStarter' => [ + 'config' => [ + 'plan_id' => 2, + ], + 'expected' => [ + 'Documentation' + ], + ], + 'testShouldReturnUpgradeLinkAmongPluginLinksIfPlanLabelIsStarter' => [ + 'config' => [ + 'plan_id' => 1, + ], + 'expected' => [ + 'Upgrade', + 'class="imagify-plugin-upgrade"' + ], + ], + ] +]; diff --git a/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php b/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php new file mode 100644 index 000000000..3a1e71655 --- /dev/null +++ b/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php @@ -0,0 +1,45 @@ +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 = $this->createMock( User::class ); + $reflection = new \ReflectionClass( $this->user ); + $this->plan_id = $reflection->getProperty( 'plan_id' ); + $this->plan_id->setAccessible( true ); + + $this->admin_subscriber = new AdminSubscriber( $this->user ); + } + + /** + * @dataProvider configTestData + */ + public function testShouldReturnAsExpected( $config, $expected ) { + $this->plan_id->setValue( $this->user, $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 ); + } + } +} From 4fedd34caf33cf171327b109d5d0ea363c4bc802 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 13:01:33 +0100 Subject: [PATCH 18/24] Updated phpunit support --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4cc4a0d8b..50d3922bd 100644 --- a/composer.json +++ b/composer.json @@ -45,12 +45,12 @@ "league/container": "^3.0", "mnsami/composer-custom-directory-installer": "^2.0", "phpcompatibility/phpcompatibility-wp": "^2.0", - "phpunit/phpunit": "^7.5", + "phpunit/phpunit": "^7.5 || ^8 || ^9", "roave/security-advisories": "dev-master", "woocommerce/action-scheduler": "^3.4", "wp-coding-standards/wpcs": "^2", "wp-media/event-manager": "^3.1", - "wp-media/phpunit": "2.0" + "wp-media/phpunit": "^3" }, "autoload": { "psr-4": { From 4756f05a13a6f9c1a495c2cfaab25ab17ac1475f Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 15:20:44 +0100 Subject: [PATCH 19/24] Revert version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 50d3922bd..126c53b9b 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,7 @@ "woocommerce/action-scheduler": "^3.4", "wp-coding-standards/wpcs": "^2", "wp-media/event-manager": "^3.1", - "wp-media/phpunit": "^3" + "wp-media/phpunit": "2.0" }, "autoload": { "psr-4": { From aee1c503137ce5dc72727b916d0d12697245491e Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 15:25:33 +0100 Subject: [PATCH 20/24] Revert php unit version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 126c53b9b..4cc4a0d8b 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ "league/container": "^3.0", "mnsami/composer-custom-directory-installer": "^2.0", "phpcompatibility/phpcompatibility-wp": "^2.0", - "phpunit/phpunit": "^7.5 || ^8 || ^9", + "phpunit/phpunit": "^7.5", "roave/security-advisories": "dev-master", "woocommerce/action-scheduler": "^3.4", "wp-coding-standards/wpcs": "^2", From 24e79bb63eb80e5301027101641f9de24308f7a7 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 30 Sep 2024 17:56:13 +0100 Subject: [PATCH 21/24] Use mockery --- .../classes/Admin/AdminSubscriber/pluginActionLinks.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php b/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php index 3a1e71655..8adb3e753 100644 --- a/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php +++ b/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php @@ -1,6 +1,7 @@ user = $this->createMock( User::class ); - $reflection = new \ReflectionClass( $this->user ); - $this->plan_id = $reflection->getProperty( 'plan_id' ); - $this->plan_id->setAccessible( true ); - + $this->user = Mockery::mock( User::class ); $this->admin_subscriber = new AdminSubscriber( $this->user ); } @@ -30,7 +27,7 @@ public function setUp(): void { * @dataProvider configTestData */ public function testShouldReturnAsExpected( $config, $expected ) { - $this->plan_id->setValue( $this->user, $config['plan_id'] ); + $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' ); From e36f132eb0966e99b7c0aca1f3a3a21ec7d7b3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Mon, 30 Sep 2024 15:34:00 -0400 Subject: [PATCH 22/24] update service provider --- classes/Admin/ServiceProvider.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/classes/Admin/ServiceProvider.php b/classes/Admin/ServiceProvider.php index 465d1135b..8a130cb9f 100644 --- a/classes/Admin/ServiceProvider.php +++ b/classes/Admin/ServiceProvider.php @@ -18,7 +18,6 @@ class ServiceProvider extends AbstractServiceProvider { */ protected $provides = [ 'admin_subscriber', - 'user', ]; /** @@ -36,8 +35,6 @@ class ServiceProvider extends AbstractServiceProvider { * @return void */ public function register() { - $this->getContainer()->add( 'user', User::class ); - $this->getContainer()->share( 'admin_subscriber', AdminSubscriber::class ) ->addArgument( $this->getContainer()->get( 'user' ) ); } From 2a048b32488687c5ecdf95ce834c163f7ef7a62c Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 1 Oct 2024 08:59:28 +0100 Subject: [PATCH 23/24] Remove unused class dep --- classes/Admin/ServiceProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/classes/Admin/ServiceProvider.php b/classes/Admin/ServiceProvider.php index 8a130cb9f..2d500f9b9 100644 --- a/classes/Admin/ServiceProvider.php +++ b/classes/Admin/ServiceProvider.php @@ -4,7 +4,6 @@ namespace Imagify\Admin; use Imagify\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider; -use Imagify\User\User; use Imagify\Admin\AdminSubscriber; /** From d3f617769add85a8a3695991e77fffffae6d5ea6 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 2 Oct 2024 09:49:24 +0100 Subject: [PATCH 24/24] Updated text color --- assets/css/admin-bar.css | 2 +- assets/css/admin-bar.min.css | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/css/admin-bar.css b/assets/css/admin-bar.css index f64be278d..8bd6d82a7 100755 --- a/assets/css/admin-bar.css +++ b/assets/css/admin-bar.css @@ -198,6 +198,6 @@ color: #fff; } .imagify-plugin-upgrade { - color: #8BC34A; + color: #6f9c3b; font-weight: 600; } diff --git a/assets/css/admin-bar.min.css b/assets/css/admin-bar.min.css index 6229d82b2..ae6105fba 100755 --- a/assets/css/admin-bar.min.css +++ b/assets/css/admin-bar.min.css @@ -1 +1 @@ -.imagify-account,.imagify-account-link{padding-right:15px}.imagify-meteo-icon{display:inline-block;height:38px;vertical-align:middle;margin-right:10px}.imagify-user-plan{color:#40b1d0}.imagify-meteo-title.imagify-meteo-title{color:#fff;font-size:17px}.imagify-space-left>p{color:#fff}#wp-admin-bar-imagify-profile [class^=imagify-bar-]{position:relative;height:1.5em;width:100%;background:#60758d;color:#fff;font-size:10px}#wp-admin-bar-imagify-profile .imagify-progress{height:1.5em;font-size:1em}.imagify-progress{-webkit-transition:width .3s;-o-transition:width .3s;transition:width .3s}.imagify-bar-positive .imagify-progress{background:#8cc152}.imagify-bar-positive .imagify-barnb{color:#8cc152}.imagify-bar-negative .imagify-progress{background:#73818c}.imagify-bar-negative .imagify-barnb{color:#73818c}.imagify-bar-neutral .imagify-progress{background:#f5a623}.imagify-space-left .imagify-bar-negative .imagify-progress{background:#d0021b}#wpadminbar #wp-admin-bar-imagify-profile *{line-height:1.5;white-space:initial}#wpadminbar #wp-admin-bar-imagify .ab-submenu{padding-bottom:0}#wpadminbar #wp-admin-bar-imagify-profile .ab-item{height:auto;padding:0 13px}#wpadminbar #wp-admin-bar-imagify-profile{min-width:200px;padding:15px 0 10px;margin-top:.7em;background:#222}#wp-admin-bar-imagify .dashicons{font-family:dashicons;font-size:18px;vertical-align:middle;margin:0 5px 0 0}#wp-admin-bar-imagify .button-text{display:inline-block;vertical-align:middle}#wp-admin-bar-imagify .imagify-abq-row{display:table;width:100%}#wp-admin-bar-imagify .imagify-abq-row+.imagify-abq-row{margin-top:.75em}#wp-admin-bar-imagify .imagify-abq-row>*{display:table-cell}#wp-admin-bar-imagify-profile .imagify-meteo-icon{padding-right:7px}#wp-admin-bar-imagify-profile .imagify-meteo-icon img{width:37px}#wp-admin-bar-imagify-profile .imagify-meteo-title{font-size:17px}#wp-admin-bar-imagify-profile .imagify-meteo-subs{color:#72889f}#wpadminbar #wp-admin-bar-imagify-profile strong{font-weight:700}#wpadminbar #wp-admin-bar-imagify-profile .imagify-user-plan,#wpadminbar #wp-admin-bar-imagify-profile a{padding:0;color:#40b1d0}#wpadminbar #wp-admin-bar-imagify-profile .imagify-account-link{display:table}#wpadminbar #wp-admin-bar-imagify-profile .imagify-account-link>*{display:table-cell}#wpadminbar #wp-admin-bar-imagify-profile .imagify-space-left{max-width:210px;min-width:210px;width:210px}#wpadminbar #wp-admin-bar-imagify-profile .imagify-space-left p{font-size:12px}#wp-admin-bar-imagify-profile .imagify-error,#wp-admin-bar-imagify-profile .imagify-warning{padding:10px;margin:0 -13px -13px}#wp-admin-bar-imagify-profile .imagify-error p+p,#wp-admin-bar-imagify-profile .imagify-warning p+p{margin-top:.5em}#wp-admin-bar-imagify-profile .imagify-error p+p+p,#wp-admin-bar-imagify-profile .imagify-warning p+p+p{margin-top:1em}#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost{display:inline-block;height:auto;padding:7px 10px;border:1px solid #fff;text-align:center;background:0 0;color:#fff;border-radius:3px;-webkit-transition:all .275s;-o-transition:all .275s;transition:all .275s}#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost:focus,#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost:hover{background:#fff;color:#888}#wpadminbar .imagify-warning *{background:#f5a623;color:#fff;text-shadow:0 0 2px rgba(0,0,0,.2)}#wp-admin-bar-imagify-profile .imagify-upsell-admin-bar{position:relative;background:#c51161;margin:10px -13px -10px -13px;padding:20px}#wp-admin-bar-imagify-profile .imagify-upsell-admin-bar p{color:#fff}#wp-admin-bar-imagify-profile a.imagify-upsell-admin-bar-button{display:block;height:auto!important;border:1px solid #fff;border-radius:5px;color:#fff!important;padding:5px 10px!important;text-align:center;text-decoration:none;margin-top:10px}#wpadminbar #wp-admin-bar-imagify-profile a.imagify-upsell-dismiss{display:inline!important;height:auto!important}#wpadminbar #wp-admin-bar-imagify-profile .imagify-upsell-dismiss::before{position:absolute;top:5px;right:10px;content:"\2715";color:#fff}.imagify-plugin-upgrade{color:#8bc34a;font-weight:600} \ No newline at end of file +.imagify-account,.imagify-account-link{padding-right:15px}.imagify-meteo-icon{display:inline-block;height:38px;vertical-align:middle;margin-right:10px}.imagify-user-plan{color:#40b1d0}.imagify-meteo-title.imagify-meteo-title{color:#fff;font-size:17px}.imagify-space-left>p{color:#fff}#wp-admin-bar-imagify-profile [class^=imagify-bar-]{position:relative;height:1.5em;width:100%;background:#60758d;color:#fff;font-size:10px}#wp-admin-bar-imagify-profile .imagify-progress{height:1.5em;font-size:1em}.imagify-progress{-webkit-transition:width .3s;-o-transition:width .3s;transition:width .3s}.imagify-bar-positive .imagify-progress{background:#8cc152}.imagify-bar-positive .imagify-barnb{color:#8cc152}.imagify-bar-negative .imagify-progress{background:#73818c}.imagify-bar-negative .imagify-barnb{color:#73818c}.imagify-bar-neutral .imagify-progress{background:#f5a623}.imagify-space-left .imagify-bar-negative .imagify-progress{background:#d0021b}#wpadminbar #wp-admin-bar-imagify-profile *{line-height:1.5;white-space:initial}#wpadminbar #wp-admin-bar-imagify .ab-submenu{padding-bottom:0}#wpadminbar #wp-admin-bar-imagify-profile .ab-item{height:auto;padding:0 13px}#wpadminbar #wp-admin-bar-imagify-profile{min-width:200px;padding:15px 0 10px;margin-top:.7em;background:#222}#wp-admin-bar-imagify .dashicons{font-family:dashicons;font-size:18px;vertical-align:middle;margin:0 5px 0 0}#wp-admin-bar-imagify .button-text{display:inline-block;vertical-align:middle}#wp-admin-bar-imagify .imagify-abq-row{display:table;width:100%}#wp-admin-bar-imagify .imagify-abq-row+.imagify-abq-row{margin-top:.75em}#wp-admin-bar-imagify .imagify-abq-row>*{display:table-cell}#wp-admin-bar-imagify-profile .imagify-meteo-icon{padding-right:7px}#wp-admin-bar-imagify-profile .imagify-meteo-icon img{width:37px}#wp-admin-bar-imagify-profile .imagify-meteo-title{font-size:17px}#wp-admin-bar-imagify-profile .imagify-meteo-subs{color:#72889f}#wpadminbar #wp-admin-bar-imagify-profile strong{font-weight:700}#wpadminbar #wp-admin-bar-imagify-profile .imagify-user-plan,#wpadminbar #wp-admin-bar-imagify-profile a{padding:0;color:#40b1d0}#wpadminbar #wp-admin-bar-imagify-profile .imagify-account-link{display:table}#wpadminbar #wp-admin-bar-imagify-profile .imagify-account-link>*{display:table-cell}#wpadminbar #wp-admin-bar-imagify-profile .imagify-space-left{max-width:210px;min-width:210px;width:210px}#wpadminbar #wp-admin-bar-imagify-profile .imagify-space-left p{font-size:12px}#wp-admin-bar-imagify-profile .imagify-error,#wp-admin-bar-imagify-profile .imagify-warning{padding:10px;margin:0 -13px -13px}#wp-admin-bar-imagify-profile .imagify-error p+p,#wp-admin-bar-imagify-profile .imagify-warning p+p{margin-top:.5em}#wp-admin-bar-imagify-profile .imagify-error p+p+p,#wp-admin-bar-imagify-profile .imagify-warning p+p+p{margin-top:1em}#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost{display:inline-block;height:auto;padding:7px 10px;border:1px solid #fff;text-align:center;background:0 0;color:#fff;border-radius:3px;-webkit-transition:all .275s;-o-transition:all .275s;transition:all .275s}#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost:focus,#wpadminbar #wp-admin-bar-imagify-profile .imagify-btn-ghost:hover{background:#fff;color:#888}#wpadminbar .imagify-warning *{background:#f5a623;color:#fff;text-shadow:0 0 2px rgba(0,0,0,.2)}#wp-admin-bar-imagify-profile .imagify-upsell-admin-bar{position:relative;background:#c51161;margin:10px -13px -10px -13px;padding:20px}#wp-admin-bar-imagify-profile .imagify-upsell-admin-bar p{color:#fff}#wp-admin-bar-imagify-profile a.imagify-upsell-admin-bar-button{display:block;height:auto!important;border:1px solid #fff;border-radius:5px;color:#fff!important;padding:5px 10px!important;text-align:center;text-decoration:none;margin-top:10px}#wpadminbar #wp-admin-bar-imagify-profile a.imagify-upsell-dismiss{display:inline!important;height:auto!important}#wpadminbar #wp-admin-bar-imagify-profile .imagify-upsell-dismiss::before{position:absolute;top:5px;right:10px;content:"\2715";color:#fff}.imagify-plugin-upgrade{color:#6f9c3b;font-weight:600} \ No newline at end of file