Skip to content

Commit

Permalink
Merge pull request #9 from wp-media/fix/8-fix-error-warning-for-non-a…
Browse files Browse the repository at this point in the history
…ctive-plugin-in-category

Fixes #8: Any active plugin in a category returns copy of the original array and throws an error on frontend
  • Loading branch information
jeawhanlee authored Oct 25, 2024
2 parents e664dde + 806d2dc commit d50c8b8
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Model/PluginFamily.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
}

Expand Down
158 changes: 158 additions & 0 deletions tests/Fixtures/src/Model/PluginFamily/filterPluginsByActivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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,
]
];

0 comments on commit d50c8b8

Please sign in to comment.