From 9e303488571b5989c4582d6452830eba45c29441 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 25 Oct 2024 11:31:11 +0100 Subject: [PATCH 1/6] Directly reference plugin array copy --- src/Model/PluginFamily.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Model/PluginFamily.php b/src/Model/PluginFamily.php index b3e616d..785f4d2 100644 --- a/src/Model/PluginFamily.php +++ b/src/Model/PluginFamily.php @@ -71,16 +71,18 @@ public function filter_plugins_by_activation( array $plugins, string $main_plugi $active_plugins[ $plugin ] = $plugins[ $cat ]['plugins'][ $plugin ]; // Remove active plugin from current category. - unset( $cat_data['plugins'][ $plugin ] ); + $active_plugin = $plugins[ $cat ]['plugins'][ $plugin ]; + unset( $plugins[ $cat ]['plugins'][ $plugin ] ); // Send active plugin to the end of array in current category. - $cat_data['plugins'][ $plugin ] = $plugins[ $cat ]['plugins'][ $plugin ]; + $plugins[ $cat ]['plugins'][ $plugin ] = $active_plugin; // Remove category with active plugin from current array. + $active_cat = $plugins[ $cat ]; unset( $plugins[ $cat ] ); // Send category with active plugins to the end of array. - $plugins[ $cat ] = $cat_data; + $plugins[ $cat ] = $active_cat; continue; } From 3de5d91ff958d50a59d437284844d1e5bb8f9529 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 25 Oct 2024 11:46:36 +0100 Subject: [PATCH 2/6] Updated test --- .../filterPluginsByActivation.php | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php b/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php index f37df58..6653a2b 100644 --- a/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php +++ b/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php @@ -298,6 +298,76 @@ ], ]; +$expectedWithSecondOrLastPluginActiveInCategory = [ + 'optimize_performance' => [ + 'title' => 'Optimize Performance', + 'plugins' => [ + 'imagify-plugin/imagify' => [ + 'logo' => [ + 'file' => 'logo-imagify.svg', + 'width' => '50%', + ], + 'title' => 'Speed Up Your Website With Lighter Images', + 'desc' => 'Imagify is the easiest WordPress image optimizer. It automatically compresses images, converts them to WebP and AVIF formats, and lets you resize and optimize with just one click!', + 'link' => 'https://imagify.io/', + 'cta' => [ + 'text' => 'Install', + 'url' => 'http://example.org/wp-admin/admin-post.php?action=install_imagify_from_partner_wp-rocket&_wpnonce=9a68f00b8d&_wp_http_referer=https%3A%2F%2Fexample.org%2Fwp-admin' + ], + ], + ], + ], + 'boost_traffic' => [ + 'title' => 'Boost Traffic', + 'plugins' => [ + 'seo-by-rank-math/rank-math' => [ + 'logo' => [ + 'file' => 'logo-rank-math.svg', + 'width' => '60%', + ], + 'title' => 'The Swiss Army Knife of SEO Tools', + 'desc' => 'Rank Math SEO is the Best WordPress SEO plugin with the features of many SEO and AI SEO tools in a single package to help multiply your SEO traffic.', + 'link' => 'https://rankmath.com/wordpress/plugin/seo-suite/', + 'cta' => [ + 'text' => 'Install', + 'url' => 'http://example.org/wp-admin/admin-post.php?action=plugin_family_install_seo-by-rank-math&_wpnonce=9a68f00b8d&plugin_to_install=seo-by-rank-math%2Frank-math' + ], + ], + ], + ], + 'protect_secure' => [ + 'title' => 'Protect & Secure', + 'plugins' => [ + 'backwpup/backwpup' => [ + 'logo' => [ + 'file' => 'logo-backwpup.svg', + 'width' => '60%', + ], + 'title' => 'The Easiest Way to Protect Your Website', + 'desc' => 'BackWPup is the most comprehensive and user-friendly backup & restore plugin for WordPress. Easily schedule automatic backups, securely store and restore with just a few clicks!', + 'link' => 'https://backwpup.com/', + 'cta' => [ + 'text' => 'Install', + 'url' => 'http://example.org/wp-admin/admin-post.php?action=plugin_family_install_backwpup&_wpnonce=9a68f00b8d&plugin_to_install=backwpup%2Fbackwpup' + ], + ], + 'uk-cookie-consent/uk-cookie-consent' => [ + 'logo' => [ + 'file' => 'logo-termly.svg', + 'width' => '50%', + ], + 'title' => 'GDPR/CCPA Cookie Consent Banner', + 'desc' => 'One of the easiest, most comprehensive, and popular cookie consent plugins available. Google Gold Certified Partner to quickly comply with data privacy laws from around the world.', + 'link' => 'https://termly.io/resources/articles/wordpress-cookies-guide/', + 'cta' => [ + 'text' => 'Activated', + 'url' => '#' + ], + ], + ], + ], +]; + return [ 'testShouldReturnEmptyArrayIfEmptyArrayParsed' => [ 'config' => [ @@ -344,4 +414,13 @@ ], 'expected' => $expectedUniqueInstallLinkForImagify, ], + 'testShouldReturnAsExpectedWithSecondOrLastPluginActiveInCategory' => [ + 'config' => [ + 'plugins' => $plugins, + 'main_plugin' => 'wp-rocket/wp-rocket', + 'order' => 'categorized', + 'active_plugin' => 'uk-cookie-consent/uk-cookie-consent.php', + ], + 'expected' => $expectedWithSecondOrLastPluginActiveInCategory, + ] ]; \ No newline at end of file From acb4ae0f8f56237136d1aced103381354fe0c039 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 25 Oct 2024 12:31:24 +0100 Subject: [PATCH 3/6] Keep optimize category at the top even when containing main plugin --- src/Model/PluginFamily.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/PluginFamily.php b/src/Model/PluginFamily.php index 785f4d2..dcf3073 100644 --- a/src/Model/PluginFamily.php +++ b/src/Model/PluginFamily.php @@ -60,7 +60,7 @@ public function filter_plugins_by_activation( array $plugins, string $main_plugi * to re-add them back using array_merge to be displayed after * plugins that are not installed or not activated. */ - if ( is_plugin_active( $plugin_path ) ) { + if ( is_plugin_active( $plugin_path ) && $main_plugin.'.php' !== $plugin_path ) { // set cta data of active plugins. $plugins[ $cat ]['plugins'][ $plugin ]['cta'] = [ 'text' => 'Activated', From dce70461447dc8d74488b2c7e30882ffd68823f4 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 25 Oct 2024 12:31:40 +0100 Subject: [PATCH 4/6] Added test --- .../filterPluginsByActivation.php | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php b/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php index 6653a2b..7da1070 100644 --- a/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php +++ b/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php @@ -368,6 +368,76 @@ ], ]; +$expectedOptimizeCategoryAsFirstWhenContainingMainPlugin = [ + 'optimize_performance' => [ + 'title' => 'Optimize Performance', + 'plugins' => [ + 'imagify-plugin/imagify' => [ + 'logo' => [ + 'file' => 'logo-imagify.svg', + 'width' => '50%', + ], + 'title' => 'Speed Up Your Website With Lighter Images', + 'desc' => 'Imagify is the easiest WordPress image optimizer. It automatically compresses images, converts them to WebP and AVIF formats, and lets you resize and optimize with just one click!', + 'link' => 'https://imagify.io/', + 'cta' => [ + 'text' => 'Install', + 'url' => 'http://example.org/wp-admin/admin-post.php?action=install_imagify_from_partner_wp-rocket&_wpnonce=9a68f00b8d&_wp_http_referer=https%3A%2F%2Fexample.org%2Fwp-admin' + ], + ], + ], + ], + 'boost_traffic' => [ + 'title' => 'Boost Traffic', + 'plugins' => [ + 'seo-by-rank-math/rank-math' => [ + 'logo' => [ + 'file' => 'logo-rank-math.svg', + 'width' => '60%', + ], + 'title' => 'The Swiss Army Knife of SEO Tools', + 'desc' => 'Rank Math SEO is the Best WordPress SEO plugin with the features of many SEO and AI SEO tools in a single package to help multiply your SEO traffic.', + 'link' => 'https://rankmath.com/wordpress/plugin/seo-suite/', + 'cta' => [ + 'text' => 'Install', + 'url' => 'http://example.org/wp-admin/admin-post.php?action=plugin_family_install_seo-by-rank-math&_wpnonce=9a68f00b8d&plugin_to_install=seo-by-rank-math%2Frank-math' + ], + ], + ], + ], + 'protect_secure' => [ + 'title' => 'Protect & Secure', + 'plugins' => [ + 'backwpup/backwpup' => [ + 'logo' => [ + 'file' => 'logo-backwpup.svg', + 'width' => '60%', + ], + 'title' => 'The Easiest Way to Protect Your Website', + 'desc' => 'BackWPup is the most comprehensive and user-friendly backup & restore plugin for WordPress. Easily schedule automatic backups, securely store and restore with just a few clicks!', + 'link' => 'https://backwpup.com/', + 'cta' => [ + 'text' => 'Install', + 'url' => 'http://example.org/wp-admin/admin-post.php?action=plugin_family_install_backwpup&_wpnonce=9a68f00b8d&plugin_to_install=backwpup%2Fbackwpup' + ], + ], + 'uk-cookie-consent/uk-cookie-consent' => [ + 'logo' => [ + 'file' => 'logo-termly.svg', + 'width' => '50%', + ], + 'title' => 'GDPR/CCPA Cookie Consent Banner', + 'desc' => 'One of the easiest, most comprehensive, and popular cookie consent plugins available. Google Gold Certified Partner to quickly comply with data privacy laws from around the world.', + 'link' => 'https://termly.io/resources/articles/wordpress-cookies-guide/', + 'cta' => [ + 'text' => 'Install', + 'url' => 'http://example.org/wp-admin/admin-post.php?action=plugin_family_install_uk-cookie-consent&_wpnonce=9a68f00b8d&plugin_to_install=uk-cookie-consent%2Fuk-cookie-consent' + ], + ], + ], + ], +]; + return [ 'testShouldReturnEmptyArrayIfEmptyArrayParsed' => [ 'config' => [ @@ -422,5 +492,14 @@ 'active_plugin' => 'uk-cookie-consent/uk-cookie-consent.php', ], 'expected' => $expectedWithSecondOrLastPluginActiveInCategory, + ], + 'testShouldStillReturnOptimizeCategoryAsFirstWhenContainingMainPlugin' => [ + 'config' => [ + 'plugins' => $plugins, + 'main_plugin' => 'wp-rocket/wp-rocket', + 'order' => 'categorized', + 'active_plugin' => 'wp-rocket/wp-rocket.php', + ], + 'expected' => $expectedOptimizeCategoryAsFirstWhenContainingMainPlugin, ] ]; \ No newline at end of file From 749d4ebd63bd397806c13fe7969e377951218797 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 25 Oct 2024 12:33:24 +0100 Subject: [PATCH 5/6] phpcs fix --- src/Model/PluginFamily.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/PluginFamily.php b/src/Model/PluginFamily.php index dcf3073..a567f4d 100644 --- a/src/Model/PluginFamily.php +++ b/src/Model/PluginFamily.php @@ -60,7 +60,7 @@ public function filter_plugins_by_activation( array $plugins, string $main_plugi * to re-add them back using array_merge to be displayed after * plugins that are not installed or not activated. */ - if ( is_plugin_active( $plugin_path ) && $main_plugin.'.php' !== $plugin_path ) { + if ( is_plugin_active( $plugin_path ) && $main_plugin . '.php' !== $plugin_path ) { // set cta data of active plugins. $plugins[ $cat ]['plugins'][ $plugin ]['cta'] = [ 'text' => 'Activated', From 806d2dc94cc1655365e73d76b9a772c33889f62d Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 25 Oct 2024 12:52:36 +0100 Subject: [PATCH 6/6] Updated tests --- .../src/Model/PluginFamily/filterPluginsByActivation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php b/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php index d0f3dda..0bf34af 100644 --- a/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php +++ b/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php @@ -302,7 +302,7 @@ 'optimize_performance' => [ 'title' => 'Optimize Performance', 'plugins' => [ - 'imagify-plugin/imagify' => [ + 'imagify/imagify' => [ 'logo' => [ 'file' => 'logo-imagify.svg', 'width' => '50%', @@ -372,7 +372,7 @@ 'optimize_performance' => [ 'title' => 'Optimize Performance', 'plugins' => [ - 'imagify-plugin/imagify' => [ + 'imagify/imagify' => [ 'logo' => [ 'file' => 'logo-imagify.svg', 'width' => '50%',