From 5c78ec258e303a7cbef2619347e1485832dc85bc Mon Sep 17 00:00:00 2001 From: Vraja Das Date: Wed, 11 Sep 2024 09:57:25 +0300 Subject: [PATCH 1/5] refactor: add news seo check to editors script data --- .../framework/integrations/news-seo.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/editors/framework/integrations/news-seo.php diff --git a/src/editors/framework/integrations/news-seo.php b/src/editors/framework/integrations/news-seo.php new file mode 100644 index 00000000000..c013eb86aec --- /dev/null +++ b/src/editors/framework/integrations/news-seo.php @@ -0,0 +1,53 @@ +addon_manager = new WPSEO_Addon_Manager(); + } + + /** + * If the plugin is activated. + * + * @return bool If the plugin is activated. + */ + public function is_enabled(): bool { + return \is_plugin_active( $this->addon_manager->get_plugin_file( WPSEO_Addon_Manager::NEWS_SLUG ) ); + } + + /** + * Return this object represented by a key value array. + * + * @return array Returns the name and if the feature is enabled. + */ + public function to_array(): array { + return [ 'isNewsSEOActive' => $this->is_enabled() ]; + } + + /** + * Returns this object represented by a key value structure that is compliant with the script data array. + * + * @return array Returns the legacy key and if the feature is enabled. + */ + public function to_legacy_array(): array { + return [ 'isNewsSEOActive' => $this->is_enabled() ]; + } +} From 13b3326d9da342f58f0f457ab5d900a66de3cd8a Mon Sep 17 00:00:00 2001 From: Vraja Das Date: Wed, 11 Sep 2024 09:58:03 +0300 Subject: [PATCH 2/5] refactor: add new path to is news seo active to store --- packages/js/src/redux/reducers/preferences.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/js/src/redux/reducers/preferences.js b/packages/js/src/redux/reducers/preferences.js index 207303bbb76..d0fc8b5dd1f 100644 --- a/packages/js/src/redux/reducers/preferences.js +++ b/packages/js/src/redux/reducers/preferences.js @@ -32,7 +32,7 @@ function getDefaultState() { useTwitterData: window.wpseoScriptData.metabox.showSocial.twitter, isWincherIntegrationActive: isWincherIntegrationActive(), isInsightsEnabled: get( window, "wpseoScriptData.metabox.isInsightsEnabled", false ), - isNewsEnabled: ! ! window.wpseoAdminL10n.news_seo_is_active, + isNewsEnabled: get( window, "wpseoScriptData.metabox.isNewsSEOActive", false ), isAiFeatureActive: Boolean( window.wpseoAdminL10n.isAiFeatureActive ), }; } From 36601be8b0274bd9159011994a8904eeb6c64b69 Mon Sep 17 00:00:00 2001 From: Vraja Das Date: Wed, 11 Sep 2024 09:58:24 +0300 Subject: [PATCH 3/5] remove old news seo active property --- inc/class-wpseo-utils.php | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/class-wpseo-utils.php b/inc/class-wpseo-utils.php index 37a8d5a17db..e9885f54529 100644 --- a/inc/class-wpseo-utils.php +++ b/inc/class-wpseo-utils.php @@ -865,7 +865,6 @@ public static function get_admin_l10n() { 'isBreadcrumbsDisabled' => WPSEO_Options::get( 'breadcrumbs-enable', false ) !== true && ! current_theme_supports( 'yoast-seo-breadcrumbs' ), // phpcs:ignore Generic.ControlStructures.DisallowYodaConditions -- Bug: squizlabs/PHP_CodeSniffer#2962. 'isPrivateBlog' => ( (string) get_option( 'blog_public' ) ) === '0', - 'news_seo_is_active' => ( defined( 'WPSEO_NEWS_FILE' ) ), 'isAiFeatureActive' => (bool) WPSEO_Options::get( 'enable_ai_generator' ), ]; From 9edfb08641862a7dd1500587d2dcded48241746c Mon Sep 17 00:00:00 2001 From: Vraja Das Date: Wed, 11 Sep 2024 14:35:49 +0300 Subject: [PATCH 4/5] refactor: changed naming to match woocommerce checks in different PR --- packages/js/src/redux/reducers/preferences.js | 2 +- src/editors/framework/integrations/news-seo.php | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/js/src/redux/reducers/preferences.js b/packages/js/src/redux/reducers/preferences.js index d0fc8b5dd1f..5292a6ff030 100644 --- a/packages/js/src/redux/reducers/preferences.js +++ b/packages/js/src/redux/reducers/preferences.js @@ -32,7 +32,7 @@ function getDefaultState() { useTwitterData: window.wpseoScriptData.metabox.showSocial.twitter, isWincherIntegrationActive: isWincherIntegrationActive(), isInsightsEnabled: get( window, "wpseoScriptData.metabox.isInsightsEnabled", false ), - isNewsEnabled: get( window, "wpseoScriptData.metabox.isNewsSEOActive", false ), + isNewsEnabled: get( window, "wpseoScriptData.metabox.isNewsSeoActive", false ), isAiFeatureActive: Boolean( window.wpseoAdminL10n.isAiFeatureActive ), }; } diff --git a/src/editors/framework/integrations/news-seo.php b/src/editors/framework/integrations/news-seo.php index c013eb86aec..6ad2c5cc77d 100644 --- a/src/editors/framework/integrations/news-seo.php +++ b/src/editors/framework/integrations/news-seo.php @@ -19,9 +19,11 @@ class News_SEO implements Integration_Data_Provider_Interface { /** * The constructor. + * + * @param WPSEO_Addon_Manager $addon_manager The addon manager. */ - public function __construct() { - $this->addon_manager = new WPSEO_Addon_Manager(); + public function __construct( WPSEO_Addon_Manager $addon_manager ) { + $this->addon_manager = $addon_manager; } /** @@ -39,7 +41,7 @@ public function is_enabled(): bool { * @return array Returns the name and if the feature is enabled. */ public function to_array(): array { - return [ 'isNewsSEOActive' => $this->is_enabled() ]; + return [ 'isNewsSeoActive' => $this->is_enabled() ]; } /** @@ -48,6 +50,6 @@ public function to_array(): array { * @return array Returns the legacy key and if the feature is enabled. */ public function to_legacy_array(): array { - return [ 'isNewsSEOActive' => $this->is_enabled() ]; + return [ 'isNewsSeoActive' => $this->is_enabled() ]; } } From b811e227dcaa18e335cd2a35ae41b3e9a5b9703b Mon Sep 17 00:00:00 2001 From: Vraja Das Date: Wed, 11 Sep 2024 14:40:46 +0300 Subject: [PATCH 5/5] tests: added unit tests to News_SEO integration class and fix php cs --- .../framework/integrations/news-seo.php | 2 +- .../Framework/Integrations/News_SEO_Test.php | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Editors/Framework/Integrations/News_SEO_Test.php diff --git a/src/editors/framework/integrations/news-seo.php b/src/editors/framework/integrations/news-seo.php index 6ad2c5cc77d..d7059dc8511 100644 --- a/src/editors/framework/integrations/news-seo.php +++ b/src/editors/framework/integrations/news-seo.php @@ -19,7 +19,7 @@ class News_SEO implements Integration_Data_Provider_Interface { /** * The constructor. - * + * * @param WPSEO_Addon_Manager $addon_manager The addon manager. */ public function __construct( WPSEO_Addon_Manager $addon_manager ) { diff --git a/tests/Unit/Editors/Framework/Integrations/News_SEO_Test.php b/tests/Unit/Editors/Framework/Integrations/News_SEO_Test.php new file mode 100644 index 00000000000..00bc0f10a6a --- /dev/null +++ b/tests/Unit/Editors/Framework/Integrations/News_SEO_Test.php @@ -0,0 +1,95 @@ +addon_manager = Mockery::mock( WPSEO_Addon_Manager::class ); + $this->instance = new News_SEO( $this->addon_manager ); + } + + /** + * Tests the is_enabled method. + * + * @dataProvider data_provider_is_enabled + * + * @param bool $news_seo_enabled If the news plugin is enabled. + * @param bool $expected The expected outcome. + * + * @return void + */ + public function test_is_enabled( + bool $news_seo_enabled, + bool $expected + ) { + + $this->addon_manager + ->expects( 'get_plugin_file' ) + ->times( 3 ) + ->with( 'yoast-seo-news' ) + ->andReturn( 'wpseo-news/wpseo-news.php' ); + + Monkey\Functions\expect( 'is_plugin_active' ) + ->times( 3 ) + ->with( 'wpseo-news/wpseo-news.php' ) + ->andReturn( $news_seo_enabled ); + + $is_woo_seo_active = $this->instance->is_enabled(); + + $this->assertSame( $expected, $is_woo_seo_active ); + $this->assertSame( [ 'isNewsSeoActive' => $is_woo_seo_active ], $this->instance->to_legacy_array() ); + $this->assertSame( [ 'isNewsSeoActive' => $is_woo_seo_active ], $this->instance->to_array() ); + } + + /** + * Data provider for test_is_enabled. + * + * @return array> + */ + public static function data_provider_is_enabled() { + return [ + 'Enabled' => [ + 'news_seo_enabled' => true, + 'expected' => true, + ], + 'Disabled' => [ + 'news_seo_enabled' => false, + 'expected' => false, + ], + ]; + } +}