diff --git a/packages/js/src/general/initialize.js b/packages/js/src/general/initialize.js index 510b67d6215..782ed289d05 100644 --- a/packages/js/src/general/initialize.js +++ b/packages/js/src/general/initialize.js @@ -33,8 +33,8 @@ domReady( () => { } ); const isRtl = select( STORE_NAME ).selectPreference( "isRtl", false ); - const contentTypes = get( window, "wpseoScriptData.contentTypes", [] ); - const userName = get( window, "wpseoScriptData.dash.userName", "User" ); + const contentTypes = get( window, "wpseoScriptData.dashboard.contentTypes", [] ); + const userName = get( window, "wpseoScriptData.dashboard.displayName", "User" ); const router = createHashRouter( createRoutesFromElements( diff --git a/src/dashboard/application/configuration/dashboard-configuration.php b/src/dashboard/application/configuration/dashboard-configuration.php new file mode 100644 index 00000000000..c9eb9fef674 --- /dev/null +++ b/src/dashboard/application/configuration/dashboard-configuration.php @@ -0,0 +1,87 @@ +content_types_repository = $content_types_repository; + $this->indexable_helper = $indexable_helper; + $this->user_helper = $user_helper; + $this->enabled_analysis_features_repository = $enabled_analysis_features_repository; + } + + /** + * Returns a configuration + * + * @return array> + */ + public function get_configuration(): array { + return [ + 'contentTypes' => $this->content_types_repository->get_content_types(), + 'indexablesEnabled' => $this->indexable_helper->should_index_indexables(), + 'displayName' => $this->user_helper->get_current_user_display_name(), + 'enabledAnalysisFeatures' => $this->enabled_analysis_features_repository->get_features_by_keys( + [ + Readability_Analysis::NAME, + Keyphrase_Analysis::NAME, + ] + )->to_array(), + ]; + } +} diff --git a/src/editors/application/analysis-features/enabled-analysis-features-repository.php b/src/editors/application/analysis-features/enabled-analysis-features-repository.php index 2cec11db2ac..66312a610a7 100644 --- a/src/editors/application/analysis-features/enabled-analysis-features-repository.php +++ b/src/editors/application/analysis-features/enabled-analysis-features-repository.php @@ -50,6 +50,27 @@ public function get_enabled_features(): Analysis_Features_List { $this->enabled_analysis_features->add_feature( $analysis_feature ); } } + return $this->enabled_analysis_features; } + + /** + * Returns the analysis list for the given names. + * + * @param array $feature_names The feature names to include. + * + * @return Analysis_Features_List The analysis list. + */ + public function get_features_by_keys( array $feature_names ): Analysis_Features_List { + $enabled_analysis_features = new Analysis_Features_List(); + + foreach ( $this->plugin_features as $plugin_feature ) { + if ( \in_array( $plugin_feature->get_name(), $feature_names, true ) ) { + $analysis_feature = new Analysis_Feature( $plugin_feature->is_enabled(), $plugin_feature->get_name(), $plugin_feature->get_legacy_key() ); + $enabled_analysis_features->add_feature( $analysis_feature ); + } + } + + return $enabled_analysis_features; + } } diff --git a/src/editors/domain/analysis-features/analysis-features-list.php b/src/editors/domain/analysis-features/analysis-features-list.php index 3fb19d04fec..bc918d25d03 100644 --- a/src/editors/domain/analysis-features/analysis-features-list.php +++ b/src/editors/domain/analysis-features/analysis-features-list.php @@ -35,6 +35,21 @@ public function parse_to_legacy_array(): array { foreach ( $this->features as $feature ) { $array = \array_merge( $array, $feature->to_legacy_array() ); } + + return $array; + } + + /** + * Parses the feature list to an array representation. + * + * @return array The list presented as a key value representation. + */ + public function to_array(): array { + $array = []; + foreach ( $this->features as $feature ) { + $array = \array_merge( $array, $feature->to_array() ); + } + return $array; } } diff --git a/src/editors/framework/cornerstone-content.php b/src/editors/framework/cornerstone-content.php index 8dff716c8c9..e75f3bdbf50 100644 --- a/src/editors/framework/cornerstone-content.php +++ b/src/editors/framework/cornerstone-content.php @@ -10,6 +10,8 @@ */ class Cornerstone_Content implements Analysis_Feature_Interface { + public const NAME = 'cornerstoneContent'; + /** * The options helper. * @@ -41,7 +43,7 @@ public function is_enabled(): bool { * @return string The name. */ public function get_name(): string { - return 'cornerstoneContent'; + return self::NAME; } /** diff --git a/src/editors/framework/inclusive-language-analysis.php b/src/editors/framework/inclusive-language-analysis.php index 4cfbe0254dc..8a8328b58e1 100644 --- a/src/editors/framework/inclusive-language-analysis.php +++ b/src/editors/framework/inclusive-language-analysis.php @@ -12,6 +12,8 @@ */ class Inclusive_Language_Analysis implements Analysis_Feature_Interface { + public const NAME = 'inclusiveLanguageAnalysis'; + /** * The options helper. * @@ -102,7 +104,7 @@ private function is_current_version_supported(): bool { * @return string The name. */ public function get_name(): string { - return 'inclusiveLanguageAnalysis'; + return self::NAME; } /** diff --git a/src/editors/framework/keyphrase-analysis.php b/src/editors/framework/keyphrase-analysis.php index 648639b2e55..287477ec38a 100644 --- a/src/editors/framework/keyphrase-analysis.php +++ b/src/editors/framework/keyphrase-analysis.php @@ -10,6 +10,8 @@ */ class Keyphrase_Analysis implements Analysis_Feature_Interface { + public const NAME = 'keyphraseAnalysis'; + /** * The options helper. * @@ -59,7 +61,7 @@ public function is_globally_enabled(): bool { * @return string The name. */ public function get_name(): string { - return 'keyphraseAnalysis'; + return self::NAME; } /** diff --git a/src/editors/framework/previously-used-keyphrase.php b/src/editors/framework/previously-used-keyphrase.php index 3400f561c6d..e4f3da7288f 100644 --- a/src/editors/framework/previously-used-keyphrase.php +++ b/src/editors/framework/previously-used-keyphrase.php @@ -9,6 +9,8 @@ */ class Previously_Used_Keyphrase implements Analysis_Feature_Interface { + public const NAME = 'previouslyUsedKeyphrase'; + /** * If this analysis is enabled. * @@ -29,7 +31,7 @@ public function is_enabled(): bool { * @return string */ public function get_name(): string { - return 'previouslyUsedKeyphrase'; + return self::NAME; } /** diff --git a/src/editors/framework/readability-analysis.php b/src/editors/framework/readability-analysis.php index 03accffe776..9e3b4b9cf9a 100644 --- a/src/editors/framework/readability-analysis.php +++ b/src/editors/framework/readability-analysis.php @@ -9,6 +9,7 @@ * This class describes the Readability analysis feature. */ class Readability_Analysis implements Analysis_Feature_Interface { + public const NAME = 'readabilityAnalysis'; /** * The options helper. @@ -59,7 +60,7 @@ private function is_globally_enabled(): bool { * @return string The name. */ public function get_name(): string { - return 'readabilityAnalysis'; + return self::NAME; } /** diff --git a/src/editors/framework/word-form-recognition.php b/src/editors/framework/word-form-recognition.php index da63caf798e..e54dbf828ee 100644 --- a/src/editors/framework/word-form-recognition.php +++ b/src/editors/framework/word-form-recognition.php @@ -10,6 +10,8 @@ */ class Word_Form_Recognition implements Analysis_Feature_Interface { + public const NAME = 'wordFormRecognition'; + /** * The language helper. * @@ -41,7 +43,7 @@ public function is_enabled(): bool { * @return string */ public function get_name(): string { - return 'wordFormRecognition'; + return self::NAME; } /** diff --git a/src/general/user-interface/general-page-integration.php b/src/general/user-interface/general-page-integration.php index 3346da75324..0a119835477 100644 --- a/src/general/user-interface/general-page-integration.php +++ b/src/general/user-interface/general-page-integration.php @@ -6,7 +6,7 @@ use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; use Yoast\WP\SEO\Conditionals\Admin\Non_Network_Admin_Conditional; use Yoast\WP\SEO\Conditionals\Admin_Conditional; -use Yoast\WP\SEO\Dashboard\Application\Content_Types\Content_Types_Repository; +use Yoast\WP\SEO\Dashboard\Application\Configuration\Dashboard_Configuration; use Yoast\WP\SEO\Helpers\Current_Page_Helper; use Yoast\WP\SEO\Helpers\Notification_Helper; use Yoast\WP\SEO\Helpers\Product_Helper; @@ -31,6 +31,13 @@ class General_Page_Integration implements Integration_Interface { */ protected $notification_helper; + /** + * The dashboard configuration. + * + * @var Dashboard_Configuration + */ + private $dashboard_configuration; + /** * Holds the WPSEO_Admin_Asset_Manager. * @@ -73,24 +80,17 @@ class General_Page_Integration implements Integration_Interface { */ private $alert_dismissal_action; - /** - * The content types repository. - * - * @var Content_Types_Repository $content_types_repository - */ - private $content_types_repository; - /** * Constructs Academy_Integration. * - * @param WPSEO_Admin_Asset_Manager $asset_manager The WPSEO_Admin_Asset_Manager. - * @param Current_Page_Helper $current_page_helper The Current_Page_Helper. - * @param Product_Helper $product_helper The Product_Helper. - * @param Short_Link_Helper $shortlink_helper The Short_Link_Helper. - * @param Notification_Helper $notification_helper The Notification_Helper. - * @param Alert_Dismissal_Action $alert_dismissal_action The alert dismissal action. - * @param Promotion_Manager $promotion_manager The promotion manager. - * @param Content_Types_Repository $content_types_repository The content types repository. + * @param WPSEO_Admin_Asset_Manager $asset_manager The WPSEO_Admin_Asset_Manager. + * @param Current_Page_Helper $current_page_helper The Current_Page_Helper. + * @param Product_Helper $product_helper The Product_Helper. + * @param Short_Link_Helper $shortlink_helper The Short_Link_Helper. + * @param Notification_Helper $notification_helper The Notification_Helper. + * @param Alert_Dismissal_Action $alert_dismissal_action The alert dismissal action. + * @param Promotion_Manager $promotion_manager The promotion manager. + * @param Dashboard_Configuration $dashboard_configuration The dashboard configuration. */ public function __construct( WPSEO_Admin_Asset_Manager $asset_manager, @@ -100,16 +100,16 @@ public function __construct( Notification_Helper $notification_helper, Alert_Dismissal_Action $alert_dismissal_action, Promotion_Manager $promotion_manager, - Content_Types_Repository $content_types_repository + Dashboard_Configuration $dashboard_configuration ) { - $this->asset_manager = $asset_manager; - $this->current_page_helper = $current_page_helper; - $this->product_helper = $product_helper; - $this->shortlink_helper = $shortlink_helper; - $this->notification_helper = $notification_helper; - $this->alert_dismissal_action = $alert_dismissal_action; - $this->promotion_manager = $promotion_manager; - $this->content_types_repository = $content_types_repository; + $this->asset_manager = $asset_manager; + $this->current_page_helper = $current_page_helper; + $this->product_helper = $product_helper; + $this->shortlink_helper = $shortlink_helper; + $this->notification_helper = $notification_helper; + $this->alert_dismissal_action = $alert_dismissal_action; + $this->promotion_manager = $promotion_manager; + $this->dashboard_configuration = $dashboard_configuration; } /** @@ -213,7 +213,7 @@ private function get_script_data() { 'alerts' => $this->notification_helper->get_alerts(), 'currentPromotions' => $this->promotion_manager->get_current_promotions(), 'dismissedAlerts' => $this->alert_dismissal_action->all_dismissed(), - 'contentTypes' => $this->content_types_repository->get_content_types(), + 'dashboard' => $this->dashboard_configuration->get_configuration(), ]; } } diff --git a/src/helpers/user-helper.php b/src/helpers/user-helper.php index d319a8c2ad9..a34dbf0d529 100644 --- a/src/helpers/user-helper.php +++ b/src/helpers/user-helper.php @@ -65,6 +65,20 @@ public function get_current_user_id() { return \get_current_user_id(); } + /** + * Returns the current users display_name. + * + * @return string + */ + public function get_current_user_display_name(): string { + $user = \wp_get_current_user(); + if ( $user && $user->display_name ) { + return $user->display_name; + } + + return ''; + } + /** * Updates user meta field for a user. * diff --git a/tests/Unit/Editors/Domain/Analysis_Features/Analysis_Features_List_Test.php b/tests/Unit/Editors/Domain/Analysis_Features/Analysis_Features_List_Test.php index 3792992aba0..435076d252e 100644 --- a/tests/Unit/Editors/Domain/Analysis_Features/Analysis_Features_List_Test.php +++ b/tests/Unit/Editors/Domain/Analysis_Features/Analysis_Features_List_Test.php @@ -22,16 +22,6 @@ final class Analysis_Features_List_Test extends TestCase { */ private $instance; - /** - * Set up the test. - * - * @return void - */ - protected function set_up(): void { - parent::set_up(); - $this->instance = new Analysis_Features_List(); - } - /** * Tests the getters. * @@ -51,4 +41,34 @@ public function test_parse_to_legacy_array(): void { $this->instance->parse_to_legacy_array() ); } + + /** + * Tests the to array. + * + * @covers ::add_feature + * @covers ::parse_to_array + * + * @return void + */ + public function test_parse_to_array(): void { + $this->instance->add_feature( new Analysis_Feature( false, 'name-false', 'legacy-key-false' ) ); + $this->instance->add_feature( new Analysis_Feature( true, 'name-true', 'legacy-key-true' ) ); + $this->assertSame( + [ + 'name-false' => false, + 'name-true' => true, + ], + $this->instance->to_array() + ); + } + + /** + * Set up the test. + * + * @return void + */ + protected function set_up(): void { + parent::set_up(); + $this->instance = new Analysis_Features_List(); + } } diff --git a/tests/Unit/General/User_Interface/General_Page_Integration_Test.php b/tests/Unit/General/User_Interface/General_Page_Integration_Test.php index 1c21f9435d8..e40003f3148 100644 --- a/tests/Unit/General/User_Interface/General_Page_Integration_Test.php +++ b/tests/Unit/General/User_Interface/General_Page_Integration_Test.php @@ -8,7 +8,7 @@ use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; use Yoast\WP\SEO\Conditionals\Admin\Non_Network_Admin_Conditional; use Yoast\WP\SEO\Conditionals\Admin_Conditional; -use Yoast\WP\SEO\Dashboard\Application\Content_Types\Content_Types_Repository; +use Yoast\WP\SEO\Dashboard\Application\Configuration\Dashboard_Configuration; use Yoast\WP\SEO\General\User_Interface\General_Page_Integration; use Yoast\WP\SEO\Helpers\Current_Page_Helper; use Yoast\WP\SEO\Helpers\Notification_Helper; @@ -76,11 +76,11 @@ final class General_Page_Integration_Test extends TestCase { private $promotion_manager; /** - * Holds the content types repository. + * Holds the dashboard configuration. * - * @var Mockery\MockInterface|Content_Types_Repository + * @var Mockery\MockInterface|Dashboard_Configuration */ - private $content_types_repository; + private $dashboard_configuration; /** * The class under test. @@ -97,14 +97,14 @@ final class General_Page_Integration_Test extends TestCase { public function set_up() { $this->stubTranslationFunctions(); - $this->asset_manager = Mockery::mock( WPSEO_Admin_Asset_Manager::class ); - $this->current_page_helper = Mockery::mock( Current_Page_Helper::class ); - $this->product_helper = Mockery::mock( Product_Helper::class ); - $this->shortlink_helper = Mockery::mock( Short_Link_Helper::class ); - $this->notifications_helper = Mockery::mock( Notification_Helper::class ); - $this->alert_dismissal_action = Mockery::mock( Alert_Dismissal_Action::class ); - $this->promotion_manager = Mockery::mock( Promotion_Manager::class ); - $this->content_types_repository = Mockery::mock( Content_Types_Repository::class ); + $this->asset_manager = Mockery::mock( WPSEO_Admin_Asset_Manager::class ); + $this->current_page_helper = Mockery::mock( Current_Page_Helper::class ); + $this->product_helper = Mockery::mock( Product_Helper::class ); + $this->shortlink_helper = Mockery::mock( Short_Link_Helper::class ); + $this->notifications_helper = Mockery::mock( Notification_Helper::class ); + $this->alert_dismissal_action = Mockery::mock( Alert_Dismissal_Action::class ); + $this->promotion_manager = Mockery::mock( Promotion_Manager::class ); + $this->dashboard_configuration = Mockery::mock( Dashboard_Configuration::class ); $this->instance = new General_Page_Integration( $this->asset_manager, @@ -114,7 +114,7 @@ public function set_up() { $this->notifications_helper, $this->alert_dismissal_action, $this->promotion_manager, - $this->content_types_repository + $this->dashboard_configuration ); } @@ -136,7 +136,7 @@ public function test_construct() { $this->notifications_helper, $this->alert_dismissal_action, $this->promotion_manager, - $this->content_types_repository + $this->dashboard_configuration ) ); } @@ -342,8 +342,8 @@ public function expect_get_script_data() { ->once() ->andReturn( [] ); - $this->content_types_repository - ->expects( 'get_content_types' ) + $this->dashboard_configuration + ->expects( 'get_configuration' ) ->once() ->andReturn( [] ); diff --git a/tests/Unit/Helpers/User_Helper_Test.php b/tests/Unit/Helpers/User_Helper_Test.php index f461ad81c52..48632ec35b5 100644 --- a/tests/Unit/Helpers/User_Helper_Test.php +++ b/tests/Unit/Helpers/User_Helper_Test.php @@ -3,6 +3,7 @@ namespace Yoast\WP\SEO\Tests\Unit\Helpers; use Brain\Monkey\Functions; +use WP_User; use Yoast\WP\SEO\Helpers\User_Helper; use Yoast\WP\SEO\Tests\Unit\TestCase; @@ -139,4 +140,42 @@ public function test_delete_meta() { $this->assertTrue( $this->instance->delete_meta( 1, 'key', 'value' ) ); } + + /** + * Tests that get_current_user_display_name return the display_name if its there or an empty string. + * + * @covers ::get_current_user_display_name + * + * @dataProvider current_user_display_name_provider + * + * @param WP_User|null $user The user. + * @param string $expected_display_name The expected display name. + * + * @return void + */ + public function test_get_current_user_display_name( $user, $expected_display_name ) { + Functions\expect( 'wp_get_current_user' ) + ->once() + ->andReturn( $user ); + + $this->assertSame( $expected_display_name, $this->instance->get_current_user_display_name() ); + } + + /** + * Data provider for current_user_display_name_provider test. + * + * @return array> + */ + public static function current_user_display_name_provider() { + $user1 = new WP_User(); + $user1->display_name = 'admin'; + $user2 = new WP_User(); + $user2->display_name = 'First_name'; + + return [ + [ $user1, 'admin' ], + [ $user2, 'First_name' ], + [ null, '' ], + ]; + } }