diff --git a/src/Model/PluginFamily.php b/src/Model/PluginFamily.php index ef75280..711362f 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', @@ -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; } diff --git a/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php b/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php index 14ca685..0bf34af 100644 --- a/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php +++ b/tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php @@ -298,6 +298,146 @@ ], ]; +$expectedWithSecondOrLastPluginActiveInCategory = [ + 'optimize_performance' => [ + 'title' => 'Optimize Performance', + 'plugins' => [ + 'imagify/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' => '#' + ], + ], + ], + ], +]; + +$expectedOptimizeCategoryAsFirstWhenContainingMainPlugin = [ + 'optimize_performance' => [ + 'title' => 'Optimize Performance', + 'plugins' => [ + 'imagify/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' => [ @@ -344,4 +484,22 @@ ], '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, + ], + '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