From b2c48bce251465cc98605aae3f7bae6bba27ef9b Mon Sep 17 00:00:00 2001 From: Thijs van der heijden Date: Mon, 6 May 2024 15:48:20 +0200 Subject: [PATCH 1/9] Move generic site data into self contained objects. --- admin/formatter/class-metabox-formatter.php | 9 - .../class-post-metabox-formatter.php | 49 ------ .../class-term-metabox-formatter.php | 38 ---- admin/metabox/class-metabox.php | 43 +++-- admin/taxonomy/class-taxonomy.php | 15 +- .../integration-information-repository.php | 3 +- .../site/website-information-repository.php | 61 +++++++ .../framework/site/base-site-information.php | 114 ++++++++++++ .../framework/site/post-site-information.php | 90 ++++++++++ .../framework/site/term-site-information.php | 127 ++++++++++++++ .../Editors/Site_Information_Mocks_Trait.php | 40 +++++ .../Site/Post_Site_Information_Test.php | 145 +++++++++++++++ .../Site/Term_Site_Information_Test.php | 166 ++++++++++++++++++ 13 files changed, 779 insertions(+), 121 deletions(-) create mode 100644 src/editors/application/site/website-information-repository.php create mode 100644 src/editors/framework/site/base-site-information.php create mode 100644 src/editors/framework/site/post-site-information.php create mode 100644 src/editors/framework/site/term-site-information.php create mode 100644 tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php create mode 100644 tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php create mode 100644 tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php diff --git a/admin/formatter/class-metabox-formatter.php b/admin/formatter/class-metabox-formatter.php index 3a248fd8d5d..0d0012a2d87 100644 --- a/admin/formatter/class-metabox-formatter.php +++ b/admin/formatter/class-metabox-formatter.php @@ -53,24 +53,15 @@ private function get_defaults() { $defaults = [ 'author_name' => get_the_author_meta( 'display_name' ), - 'site_name' => YoastSEO()->meta->for_current_page()->site_name, 'sitewide_social_image' => WPSEO_Options::get( 'og_default_image' ), - 'search_url' => '', - 'post_edit_url' => '', - 'base_url' => '', 'contentTab' => __( 'Readability', 'wordpress-seo' ), 'keywordTab' => __( 'Keyphrase:', 'wordpress-seo' ), 'removeKeyword' => __( 'Remove keyphrase', 'wordpress-seo' ), - 'contentLocale' => get_locale(), - 'userLocale' => get_user_locale(), 'translations' => $this->get_translations(), 'keyword_usage' => [], 'title_template' => '', 'metadesc_template' => '', 'intl' => $this->get_content_analysis_component_translations(), - 'isRtl' => is_rtl(), - 'isPremium' => YoastSEO()->helpers->product->is_premium(), - 'siteIconUrl' => get_site_icon_url(), 'showSocial' => [ 'facebook' => WPSEO_Options::get( 'opengraph', false ), 'twitter' => WPSEO_Options::get( 'twitter', false ), diff --git a/admin/formatter/class-post-metabox-formatter.php b/admin/formatter/class-post-metabox-formatter.php index aa4eb2f296f..b5ecc516dcc 100644 --- a/admin/formatter/class-post-metabox-formatter.php +++ b/admin/formatter/class-post-metabox-formatter.php @@ -62,9 +62,6 @@ public function use_social_templates() { public function get_values() { $values = [ - 'search_url' => $this->search_url(), - 'post_edit_url' => $this->edit_url(), - 'base_url' => $this->base_url_for_js(), 'metaDescriptionDate' => '', ]; @@ -106,52 +103,6 @@ protected function get_image_url() { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $this->post->ID ); } - /** - * Returns the url to search for keyword for the post. - * - * @return string - */ - private function search_url() { - return admin_url( 'edit.php?seo_kw_filter={keyword}' ); - } - - /** - * Returns the url to edit the taxonomy. - * - * @return string - */ - private function edit_url() { - return admin_url( 'post.php?post={id}&action=edit' ); - } - - /** - * Returns a base URL for use in the JS, takes permalink structure into account. - * - * @return string - */ - private function base_url_for_js() { - global $pagenow; - - // The default base is the home_url. - $base_url = home_url( '/', null ); - - if ( $pagenow === 'post-new.php' ) { - return $base_url; - } - - // If %postname% is the last tag, just strip it and use that as a base. - if ( preg_match( '#%postname%/?$#', $this->permalink ) === 1 ) { - $base_url = preg_replace( '#%postname%/?$#', '', $this->permalink ); - } - - // If %pagename% is the last tag, just strip it and use that as a base. - if ( preg_match( '#%pagename%/?$#', $this->permalink ) === 1 ) { - $base_url = preg_replace( '#%pagename%/?$#', '', $this->permalink ); - } - - return $base_url; - } - /** * Counts the number of given keywords used for other posts other than the given post_id. * diff --git a/admin/formatter/class-term-metabox-formatter.php b/admin/formatter/class-term-metabox-formatter.php index aa596cdcaa2..cec44471c1a 100644 --- a/admin/formatter/class-term-metabox-formatter.php +++ b/admin/formatter/class-term-metabox-formatter.php @@ -71,9 +71,6 @@ public function get_values() { // Todo: a column needs to be added on the termpages to add a filter for the keyword, so this can be used in the focus keyphrase doubles. if ( is_object( $this->term ) && property_exists( $this->term, 'taxonomy' ) ) { $values = [ - 'search_url' => $this->search_url(), - 'post_edit_url' => $this->edit_url(), - 'base_url' => $this->base_url_for_js(), 'taxonomy' => $this->term->taxonomy, 'keyword_usage' => $this->get_focus_keyword_usage(), 'title_template' => $this->get_title_template(), @@ -101,41 +98,6 @@ protected function get_image_url() { return WPSEO_Image_Utils::get_first_content_image_for_term( $this->term->term_id ); } - /** - * Returns the url to search for keyword for the taxonomy. - * - * @return string - */ - private function search_url() { - return admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' ); - } - - /** - * Returns the url to edit the taxonomy. - * - * @return string - */ - private function edit_url() { - return admin_url( 'term.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' ); - } - - /** - * Returns a base URL for use in the JS, takes permalink structure into account. - * - * @return string - */ - private function base_url_for_js() { - - $base_url = home_url( '/', null ); - if ( ! WPSEO_Options::get( 'stripcategorybase', false ) ) { - if ( $this->taxonomy->rewrite ) { - $base_url = trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] ); - } - } - - return $base_url; - } - /** * Counting the number of given keyword used for other term than given term_id. * diff --git a/admin/metabox/class-metabox.php b/admin/metabox/class-metabox.php index 7dc1329ff3d..c4ba2176f0c 100644 --- a/admin/metabox/class-metabox.php +++ b/admin/metabox/class-metabox.php @@ -5,14 +5,12 @@ * @package WPSEO\Admin */ -use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; use Yoast\WP\SEO\Conditionals\Third_Party\Jetpack_Boost_Active_Conditional; use Yoast\WP\SEO\Conditionals\Third_Party\Jetpack_Boost_Not_Premium_Conditional; use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional; -use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; +use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository; use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter; use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter; -use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; /** * This class generates the metabox on the edit post / page as well as contains all page analysis functionality. @@ -285,12 +283,7 @@ public function wpseo_metabox_class( $classes ) { * @return array|bool|int> */ public function get_metabox_script_data() { - $permalink = ''; - - if ( is_object( $this->get_metabox_post() ) ) { - $permalink = get_sample_permalink( $this->get_metabox_post()->ID ); - $permalink = $permalink[0]; - } + $permalink = $this->get_permalink(); $post_formatter = new WPSEO_Metabox_Formatter( new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink ) @@ -913,8 +906,6 @@ public function enqueue() { 'log_level' => WPSEO_Utils::get_analysis_worker_log_level(), ]; - $alert_dismissal_action = YoastSEO()->classes->get( Alert_Dismissal_Action::class ); - $dismissed_alerts = $alert_dismissal_action->all_dismissed(); $woocommerce_conditional = new WooCommerce_Conditional(); $woocommerce_active = $woocommerce_conditional->is_met(); $wpseo_plugin_availability_checker = new WPSEO_Plugin_Availability(); @@ -936,20 +927,20 @@ public function enqueue() { 'plugins' => $plugins_script_data, 'worker' => $worker_script_data, ], - 'dismissedAlerts' => $dismissed_alerts, - 'currentPromotions' => YoastSEO()->classes->get( Promotion_Manager::class )->get_current_promotions(), - 'webinarIntroBlockEditorUrl' => WPSEO_Shortlinker::get( 'https://yoa.st/webinar-intro-block-editor' ), - 'blackFridayBlockEditorUrl' => ( YoastSEO()->classes->get( Promotion_Manager::class )->is( 'black-friday-2023-checklist' ) ) ? WPSEO_Shortlinker::get( 'https://yoa.st/black-friday-checklist' ) : '', 'isJetpackBoostActive' => ( $is_block_editor ) ? YoastSEO()->classes->get( Jetpack_Boost_Active_Conditional::class )->is_met() : false, 'isJetpackBoostNotPremium' => ( $is_block_editor ) ? YoastSEO()->classes->get( Jetpack_Boost_Not_Premium_Conditional::class )->is_met() : false, 'isWooCommerceSeoActive' => $woocommerce_seo_active, 'isWooCommerceActive' => $woocommerce_active, 'woocommerceUpsell' => get_post_type( $post_id ) === 'product' && ! $woocommerce_seo_active && $woocommerce_active, - 'linkParams' => WPSEO_Shortlinker::get_query_params(), - 'pluginUrl' => plugins_url( '', WPSEO_FILE ), - 'wistiaEmbedPermission' => YoastSEO()->classes->get( Wistia_Embed_Permission_Repository::class )->get_value_for_user( get_current_user_id() ), ]; - + /** + * The website information repository. + * + * @var $repo Website_Information_Repository + */ + $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); + $repo->get_post_site_information()->set_permalink( $this->get_permalink() ); + $script_data = array_merge_recursive( $repo->get_post_site_information()->get_site_information(), $script_data ); if ( post_type_supports( get_post_type(), 'thumbnail' ) ) { $asset_manager->enqueue_style( 'featured-image' ); @@ -1207,4 +1198,18 @@ public static function is_post_edit( $page ) { protected function get_product_title() { return YoastSEO()->helpers->product->get_product_name(); } + + /** + * @return mixed|string + */ + public function get_permalink() { + $permalink = ''; + + if ( is_object( $this->get_metabox_post() ) ) { + $permalink = get_sample_permalink( $this->get_metabox_post()->ID ); + $permalink = $permalink[0]; + } + + return $permalink; + } } diff --git a/admin/taxonomy/class-taxonomy.php b/admin/taxonomy/class-taxonomy.php index 423b02d9bb2..b7186c2698d 100644 --- a/admin/taxonomy/class-taxonomy.php +++ b/admin/taxonomy/class-taxonomy.php @@ -5,7 +5,7 @@ * @package WPSEO\Admin */ -use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; +use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository; use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter; /** @@ -192,10 +192,17 @@ public function admin_enqueue_scripts() { 'postId' => $tag_id, 'termType' => $this->get_taxonomy(), 'usedKeywordsNonce' => wp_create_nonce( 'wpseo-keyword-usage' ), - 'linkParams' => WPSEO_Shortlinker::get_query_params(), - 'pluginUrl' => plugins_url( '', WPSEO_FILE ), - 'wistiaEmbedPermission' => YoastSEO()->classes->get( Wistia_Embed_Permission_Repository::class )->get_value_for_user( get_current_user_id() ), ]; + + /** + * The website information repository. + * + * @var $repo Website_Information_Repository + */ + $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); + $repo->get_term_site_information()->set_term( get_term_by( 'id', $tag_id, $this::get_taxonomy() ) ); + $script_data = array_merge_recursive( $repo->get_term_site_information()->get_site_information(), $script_data ); + $asset_manager->localize_script( 'term-edit', 'wpseoScriptData', $script_data ); $asset_manager->enqueue_user_language_script(); } diff --git a/src/editors/application/integrations/integration-information-repository.php b/src/editors/application/integrations/integration-information-repository.php index 373cc0021d6..c066fc2f740 100644 --- a/src/editors/application/integrations/integration-information-repository.php +++ b/src/editors/application/integrations/integration-information-repository.php @@ -4,7 +4,6 @@ namespace Yoast\WP\SEO\Editors\Application\Integrations; use Yoast\WP\SEO\Editors\Domain\Integrations\Integration_Data_Provider_Interface; -use Yoast\WP\SEO\Editors\Framework\Analysis_Feature_Interface; /** * The repository to get all enabled integrations. @@ -16,7 +15,7 @@ class Integration_Information_Repository { /** * All plugin integrations. * - * @var Analysis_Feature_Interface[] $plugin_integrations + * @var Integration_Data_Provider_Interface[] $plugin_integrations */ private $plugin_integrations; diff --git a/src/editors/application/site/website-information-repository.php b/src/editors/application/site/website-information-repository.php new file mode 100644 index 00000000000..6ff099f9175 --- /dev/null +++ b/src/editors/application/site/website-information-repository.php @@ -0,0 +1,61 @@ +post_site_information = $post_site_information; + $this->term_site_information = $term_site_information; + } + + /** + * Returns the Post Site Information container. + * + * @return Post_Site_Information + */ + public function get_post_site_information(): Post_Site_Information { + return $this->post_site_information; + } + + /** + * Returns the Term Site Information container. + * + * @return Term_Site_Information + */ + public function get_term_site_information(): Term_Site_Information { + return $this->term_site_information; + } +} diff --git a/src/editors/framework/site/base-site-information.php b/src/editors/framework/site/base-site-information.php new file mode 100644 index 00000000000..142994868f9 --- /dev/null +++ b/src/editors/framework/site/base-site-information.php @@ -0,0 +1,114 @@ +promotion_manager = $promotion_manager; + $this->short_link_helper = $short_link_helper; + $this->wistia_embed_permission_repository = $wistia_embed_permission_repository; + $this->meta = $meta; + $this->product_helper = $product_helper; + $this->alert_dismissal_action = $alert_dismissal_action; + } + + /** + * Returns site information that is the + * + * @throws Exception If an invalid user ID is supplied to the wistia repository. + * @return array + */ + public function get_site_information(): array { + $dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); + + return [ + 'dismissedAlerts' => $dismissed_alerts, + 'currentPromotions' => $this->promotion_manager->get_current_promotions(), + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), + 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', + 'linkParams' => $this->short_link_helper->get_query_params(), + 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), + 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), + + 'metabox' => [ + 'site_name' => $this->meta->for_current_page()->site_name, + 'contentLocale' => \get_locale(), + 'userLocale' => \get_user_locale(), + 'isRtl' => \is_rtl(), + 'isPremium' => $this->product_helper->is_premium(), + 'siteIconUrl' => \get_site_icon_url(), + ], + ]; + } +} diff --git a/src/editors/framework/site/post-site-information.php b/src/editors/framework/site/post-site-information.php new file mode 100644 index 00000000000..9f53f82c3cb --- /dev/null +++ b/src/editors/framework/site/post-site-information.php @@ -0,0 +1,90 @@ +permalink = $permalink; + } + + /** + * Returns post specific site information together with the generic site information. + * + * @return array + */ + public function get_site_information(): array { + $data = [ + 'metabox' => [ + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), + ], + ]; + + return \array_merge_recursive( $data, parent::get_site_information() ); + } + + /** + * Returns the url to search for keyword for the post. + * + * @return string + */ + private function search_url(): string { + return \admin_url( 'edit.php?seo_kw_filter={keyword}' ); + } + + /** + * Returns the url to edit the taxonomy. + * + * @return string + */ + private function edit_url(): string { + return \admin_url( 'post.php?post={id}&action=edit' ); + } + + /** + * Returns a base URL for use in the JS, takes permalink structure into account. + * + * @return string + */ + private function base_url_for_js(): string { + global $pagenow; + + // The default base is the home_url. + $base_url = \home_url( '/', null ); + + if ( $pagenow === 'post-new.php' ) { + return $base_url; + } + + // If %postname% is the last tag, just strip it and use that as a base. + if ( \preg_match( '#%postname%/?$#', $this->permalink ) === 1 ) { + $base_url = \preg_replace( '#%postname%/?$#', '', $this->permalink ); + } + + // If %pagename% is the last tag, just strip it and use that as a base. + if ( \preg_match( '#%pagename%/?$#', $this->permalink ) === 1 ) { + $base_url = \preg_replace( '#%pagename%/?$#', '', $this->permalink ); + } + + return $base_url; + } +} diff --git a/src/editors/framework/site/term-site-information.php b/src/editors/framework/site/term-site-information.php new file mode 100644 index 00000000000..2d11ffba295 --- /dev/null +++ b/src/editors/framework/site/term-site-information.php @@ -0,0 +1,127 @@ +options_helper = $options_helper; + } + + /** + * Sets the term for the information object and retrieves its taxonomy. + * + * @param WP_Term|string|false $term The term. + * + * @return void + */ + public function set_term( $term ) { + $this->term = $term; + $this->taxonomy = \get_taxonomy( $term->taxonomy ); + } + + /** + * Returns term specific site information together with the generic site information. + * + * @return array + */ + public function get_site_information(): array { + $data = [ + 'metabox' => [ + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), + ], + ]; + + return \array_merge_recursive( $data, parent::get_site_information() ); + } + + /** + * Returns the url to search for keyword for the taxonomy. + * + * @return string + */ + private function search_url(): string { + return \admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' ); + } + + /** + * Returns the url to edit the taxonomy. + * + * @return string + */ + private function edit_url(): string { + return \admin_url( 'term.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' ); + } + + /** + * Returns a base URL for use in the JS, takes permalink structure into account. + * + * @return string + */ + private function base_url_for_js(): string { + $base_url = \home_url( '/', null ); + if ( ! $this->options_helper->get( 'stripcategorybase', false ) ) { + if ( $this->taxonomy->rewrite ) { + $base_url = \trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] ); + } + } + + return $base_url; + } +} diff --git a/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php b/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php new file mode 100644 index 00000000000..b0685b617cf --- /dev/null +++ b/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php @@ -0,0 +1,40 @@ +site_name = 'examepl.com'; + $locale = 'nl_NL'; + + Monkey\Functions\expect( 'plugins_url' )->andReturn( $plugin_url ); + Monkey\Functions\expect( 'get_current_user_id' )->andReturn( 1 ); + Monkey\Functions\expect( 'get_locale' )->andReturn( $locale ); + Monkey\Functions\expect( 'get_user_locale' )->andReturn( $locale ); + Monkey\Functions\expect( 'is_rtl' )->andReturnFalse(); + Monkey\Functions\expect( 'get_site_icon_url' )->andReturn( 'https://example.org' ); + + $this->alert_dismissal_action->expects( 'all_dismissed' )->andReturn( $dismissed_alerts ); + $this->promotion_manager->expects( 'get_current_promotions' )->andReturn( $promotions ); + $this->promotion_manager->expects( 'is' )->andReturnFalse(); + $this->short_link_helper->expects( 'get' )->andReturn( $short_link ); + $this->short_link_helper->expects( 'get_query_params' )->andReturn( $query_params ); + $this->wistia_embed_repo->expects( 'get_value_for_user' )->with( 1 )->andReturnTrue(); + $this->meta_surface->expects( 'for_current_page' )->andReturn( $meta_surface ); + $this->product_helper->expects( 'is_premium' )->andReturnTrue(); + } +} diff --git a/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php new file mode 100644 index 00000000000..4ade86f0728 --- /dev/null +++ b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php @@ -0,0 +1,145 @@ +promotion_manager = Mockery::mock( Promotion_Manager::class ); + $this->short_link_helper = Mockery::mock( Short_Link_Helper::class ); + $this->wistia_embed_repo = Mockery::mock( Wistia_Embed_Permission_Repository::class ); + $this->meta_surface = Mockery::mock( Meta_Surface::class ); + $this->product_helper = Mockery::mock( Product_Helper::class ); + $this->alert_dismissal_action = Mockery::mock( Alert_Dismissal_Action::class ); + + $this->instance = new Post_Site_Information( $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + } + + /** + * Tests the get_site_information. + * + * @covers ::__construct + * @covers ::get_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_permalink + * + * @return void + */ + public function test_site_information() { + $this->instance->set_permalink( 'perma' ); + $this->set_mocks(); + $expected = [ + 'metabox' => [ + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', + 'site_name' => 'examepl.com', + 'contentLocale' => 'nl_NL', + 'userLocale' => 'nl_NL', + 'isRtl' => false, + 'isPremium' => true, + 'siteIconUrl' => 'https://example.org', + ], + 'dismissedAlerts' => [ + 'the alert', + ], + 'currentPromotions' => [ + 'the promotion', + 'another one', + ], + 'webinarIntroBlockEditorUrl' => 'https://expl.c', + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => [ + 'param', + 'param2', + ], + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, + + ]; + + Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); + Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + + $this->assertSame( $expected, $this->instance->get_site_information() ); + } +} diff --git a/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php new file mode 100644 index 00000000000..30b90756708 --- /dev/null +++ b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php @@ -0,0 +1,166 @@ +options_helper = Mockery::mock( Options_Helper::class ); + $this->promotion_manager = Mockery::mock( Promotion_Manager::class ); + $this->short_link_helper = Mockery::mock( Short_Link_Helper::class ); + $this->wistia_embed_repo = Mockery::mock( Wistia_Embed_Permission_Repository::class ); + $this->meta_surface = Mockery::mock( Meta_Surface::class ); + $this->product_helper = Mockery::mock( Product_Helper::class ); + $this->alert_dismissal_action = Mockery::mock( Alert_Dismissal_Action::class ); + + $this->instance = new Term_Site_Information( $this->options_helper, $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + } + + /** + * Tests the get_site_information. + * + * @covers ::__construct + * @covers ::get_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_term + * + * @return void + */ + public function test_site_information() { + $taxonomy = Mockery::mock( WP_Taxonomy::class )->makePartial(); + $taxonomy->rewrite = false; + $mock_term = Mockery::mock( WP_Term::class )->makePartial(); + $mock_term->taxonomy = 'tax'; + $mock_term->term_id = 1; + + Monkey\Functions\expect( 'get_taxonomy' )->andReturn( $taxonomy ); + + $this->instance->set_term( $mock_term ); + $this->options_helper->expects( 'get' )->with( 'stripcategorybase', false )->andReturnFalse(); + + $this->set_mocks(); + $expected = [ + 'metabox' => [ + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', + 'site_name' => 'examepl.com', + 'contentLocale' => 'nl_NL', + 'userLocale' => 'nl_NL', + 'isRtl' => false, + 'isPremium' => true, + 'siteIconUrl' => 'https://example.org', + ], + 'dismissedAlerts' => [ + 'the alert', + ], + 'currentPromotions' => [ + 'the promotion', + 'another one', + ], + 'webinarIntroBlockEditorUrl' => 'https://expl.c', + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => [ + 'param', + 'param2', + ], + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, + + ]; + + Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); + Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + + $this->assertSame( $expected, $this->instance->get_site_information() ); + } +} From ce1957272e0cdcdbdb96bcae4d519f15ff97edc4 Mon Sep 17 00:00:00 2001 From: Thijs van der heijden Date: Tue, 7 May 2024 09:50:59 +0200 Subject: [PATCH 2/9] Add legacy non legacy difference. --- admin/metabox/class-metabox.php | 4 +- .../site/site-information-interface.php | 21 ++++++ .../framework/site/base-site-information.php | 32 ++++++++- .../framework/site/post-site-information.php | 25 +++++-- .../framework/site/term-site-information.php | 17 ++++- .../Site/Post_Site_Information_Test.php | 58 +++++++++++++-- .../Site/Term_Site_Information_Test.php | 70 ++++++++++++++++--- 7 files changed, 202 insertions(+), 25 deletions(-) create mode 100644 src/editors/domain/site/site-information-interface.php diff --git a/admin/metabox/class-metabox.php b/admin/metabox/class-metabox.php index c4ba2176f0c..0be2f9ae3b5 100644 --- a/admin/metabox/class-metabox.php +++ b/admin/metabox/class-metabox.php @@ -1200,7 +1200,9 @@ protected function get_product_title() { } /** - * @return mixed|string + * Gets the permalink. + * + * @return string */ public function get_permalink() { $permalink = ''; diff --git a/src/editors/domain/site/site-information-interface.php b/src/editors/domain/site/site-information-interface.php new file mode 100644 index 00000000000..01c3abb6fd6 --- /dev/null +++ b/src/editors/domain/site/site-information-interface.php @@ -0,0 +1,21 @@ + + */ + public function get_site_information(): array; + + /** + * Returns specific site information together with the generic site information compatible with a legacy way. + * + * @return array + */ + public function get_legacy_site_information(): array; +} diff --git a/src/editors/framework/site/base-site-information.php b/src/editors/framework/site/base-site-information.php index 142994868f9..6cbae89c8a8 100644 --- a/src/editors/framework/site/base-site-information.php +++ b/src/editors/framework/site/base-site-information.php @@ -4,6 +4,7 @@ use Exception; use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; +use Yoast\WP\SEO\Editors\Domain\Site\Site_Information_Interface; use Yoast\WP\SEO\Helpers\Product_Helper; use Yoast\WP\SEO\Helpers\Short_Link_Helper; use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; @@ -13,7 +14,7 @@ /** * The Base_Site_Information class. */ -abstract class Base_Site_Information { +abstract class Base_Site_Information implements Site_Information_Interface { /** * The alert dismissal action. @@ -62,7 +63,8 @@ abstract class Base_Site_Information { * * @param Promotion_Manager $promotion_manager The promotion manager. * @param Short_Link_Helper $short_link_helper The short link helper. - * @param Wistia_Embed_Permission_Repository $wistia_embed_permission_repository The wistia embed permission repository. + * @param Wistia_Embed_Permission_Repository $wistia_embed_permission_repository The wistia embed permission + * repository. * @param Meta_Surface $meta The meta surface. * @param Product_Helper $product_helper The product helper. * @param Alert_Dismissal_Action $alert_dismissal_action The alert dismissal action. @@ -92,6 +94,32 @@ public function __construct( public function get_site_information(): array { $dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); + return [ + 'dismissedAlerts' => $dismissed_alerts, + 'currentPromotions' => $this->promotion_manager->get_current_promotions(), + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), + 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', + 'linkParams' => $this->short_link_helper->get_query_params(), + 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), + 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), + 'site_name' => $this->meta->for_current_page()->site_name, + 'contentLocale' => \get_locale(), + 'userLocale' => \get_user_locale(), + 'isRtl' => \is_rtl(), + 'isPremium' => $this->product_helper->is_premium(), + 'siteIconUrl' => \get_site_icon_url(), + ]; + } + + /** + * Returns site information that is the + * + * @throws Exception If an invalid user ID is supplied to the wistia repository. + * @return array + */ + public function get_legacy_site_information(): array { + $dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); + return [ 'dismissedAlerts' => $dismissed_alerts, 'currentPromotions' => $this->promotion_manager->get_current_promotions(), diff --git a/src/editors/framework/site/post-site-information.php b/src/editors/framework/site/post-site-information.php index 9f53f82c3cb..741ec9412e8 100644 --- a/src/editors/framework/site/post-site-information.php +++ b/src/editors/framework/site/post-site-information.php @@ -30,16 +30,31 @@ public function set_permalink( string $permalink ): void { * * @return array */ - public function get_site_information(): array { + public function get_legacy_site_information(): array { $data = [ 'metabox' => [ - 'search_url' => $this->search_url(), - 'post_edit_url' => $this->edit_url(), - 'base_url' => $this->base_url_for_js(), + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), ], ]; - return \array_merge_recursive( $data, parent::get_site_information() ); + return \array_merge_recursive( $data, parent::get_legacy_site_information() ); + } + + /** + * Returns post specific site information together with the generic site information. + * + * @return array + */ + public function get_site_information(): array { + $data = [ + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), + ]; + + return \array_merge( $data, parent::get_site_information() ); } /** diff --git a/src/editors/framework/site/term-site-information.php b/src/editors/framework/site/term-site-information.php index 2d11ffba295..939b36007d5 100644 --- a/src/editors/framework/site/term-site-information.php +++ b/src/editors/framework/site/term-site-information.php @@ -80,6 +80,21 @@ public function set_term( $term ) { * @return array */ public function get_site_information(): array { + $data = [ + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), + ]; + + return \array_merge_recursive( $data, parent::get_site_information() ); + } + + /** + * Returns term specific site information together with the generic site information. + * + * @return array + */ + public function get_legacy_site_information(): array { $data = [ 'metabox' => [ 'search_url' => $this->search_url(), @@ -88,7 +103,7 @@ public function get_site_information(): array { ], ]; - return \array_merge_recursive( $data, parent::get_site_information() ); + return \array_merge_recursive( $data, parent::get_legacy_site_information() ); } /** diff --git a/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php index 4ade86f0728..2f72a31f2fa 100644 --- a/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php +++ b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php @@ -90,13 +90,15 @@ protected function set_up() { $this->alert_dismissal_action = Mockery::mock( Alert_Dismissal_Action::class ); $this->instance = new Post_Site_Information( $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + $this->instance->set_permalink( 'perma' ); + $this->set_mocks(); } /** - * Tests the get_site_information. + * Tests the get_legacy_site_information. * * @covers ::__construct - * @covers ::get_site_information + * @covers ::get_legacy_site_information * @covers ::search_url * @covers ::base_url_for_js * @covers ::edit_url @@ -104,9 +106,7 @@ protected function set_up() { * * @return void */ - public function test_site_information() { - $this->instance->set_permalink( 'perma' ); - $this->set_mocks(); + public function test_legacy_site_information() { $expected = [ 'metabox' => [ 'search_url' => 'https://example.org', @@ -140,6 +140,54 @@ public function test_site_information() { Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + $this->assertSame( $expected, $this->instance->get_legacy_site_information() ); + } + + /** + * Tests the get_site_information. + * + * @covers ::__construct + * @covers ::get_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_permalink + * + * @return void + */ + public function test_site_information() { + $expected = [ + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', + + 'dismissedAlerts' => [ + 'the alert', + ], + 'currentPromotions' => [ + 'the promotion', + 'another one', + ], + 'webinarIntroBlockEditorUrl' => 'https://expl.c', + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => [ + 'param', + 'param2', + ], + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, + 'site_name' => 'examepl.com', + 'contentLocale' => 'nl_NL', + 'userLocale' => 'nl_NL', + 'isRtl' => false, + 'isPremium' => true, + 'siteIconUrl' => 'https://example.org', + + ]; + + Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); + Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + $this->assertSame( $expected, $this->instance->get_site_information() ); } } diff --git a/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php index 30b90756708..310200fd200 100644 --- a/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php +++ b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php @@ -100,7 +100,19 @@ protected function set_up() { $this->product_helper = Mockery::mock( Product_Helper::class ); $this->alert_dismissal_action = Mockery::mock( Alert_Dismissal_Action::class ); - $this->instance = new Term_Site_Information( $this->options_helper, $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + $this->instance = new Term_Site_Information( $this->options_helper, $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + $taxonomy = Mockery::mock( WP_Taxonomy::class )->makePartial(); + $taxonomy->rewrite = false; + $mock_term = Mockery::mock( WP_Term::class )->makePartial(); + $mock_term->taxonomy = 'tax'; + $mock_term->term_id = 1; + + Monkey\Functions\expect( 'get_taxonomy' )->andReturn( $taxonomy ); + + $this->instance->set_term( $mock_term ); + $this->options_helper->expects( 'get' )->with( 'stripcategorybase', false )->andReturnFalse(); + + $this->set_mocks(); } /** @@ -116,18 +128,54 @@ protected function set_up() { * @return void */ public function test_site_information() { - $taxonomy = Mockery::mock( WP_Taxonomy::class )->makePartial(); - $taxonomy->rewrite = false; - $mock_term = Mockery::mock( WP_Term::class )->makePartial(); - $mock_term->taxonomy = 'tax'; - $mock_term->term_id = 1; + $expected = [ + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', - Monkey\Functions\expect( 'get_taxonomy' )->andReturn( $taxonomy ); + 'dismissedAlerts' => [ + 'the alert', + ], + 'currentPromotions' => [ + 'the promotion', + 'another one', + ], + 'webinarIntroBlockEditorUrl' => 'https://expl.c', + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => [ + 'param', + 'param2', + ], + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, + 'site_name' => 'examepl.com', + 'contentLocale' => 'nl_NL', + 'userLocale' => 'nl_NL', + 'isRtl' => false, + 'isPremium' => true, + 'siteIconUrl' => 'https://example.org', + ]; - $this->instance->set_term( $mock_term ); - $this->options_helper->expects( 'get' )->with( 'stripcategorybase', false )->andReturnFalse(); + Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); + Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + + $this->assertSame( $expected, $this->instance->get_site_information() ); + } + + /** + * Tests the get_legacy_site_information. + * + * @covers ::__construct + * @covers ::get_legacy_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_term + * + * @return void + */ + public function test_legacy_site_information() { - $this->set_mocks(); $expected = [ 'metabox' => [ 'search_url' => 'https://example.org', @@ -161,6 +209,6 @@ public function test_site_information() { Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); - $this->assertSame( $expected, $this->instance->get_site_information() ); + $this->assertSame( $expected, $this->instance->get_legacy_site_information() ); } } From 36c618ea1600a98eab60d1600639bb27b5f80ae6 Mon Sep 17 00:00:00 2001 From: Thijs van der heijden Date: Tue, 7 May 2024 12:12:45 +0200 Subject: [PATCH 3/9] Update tests. --- composer.json | 2 +- tests/Unit/Admin/User_Profile_Test.php | 1 - .../Site/Post_Site_Information_Test.php | 184 ++++++++++++++++++ tests/WP/Formatter/Metabox_Formatter_Test.php | 1 - .../Formatter/Post_Metabox_Formatter_Test.php | 85 -------- .../Formatter/Term_Metabox_Formatter_Test.php | 4 - 6 files changed, 185 insertions(+), 92 deletions(-) create mode 100644 tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php diff --git a/composer.json b/composer.json index 5bad5b6086a..fda1dd8e7f7 100644 --- a/composer.json +++ b/composer.json @@ -92,7 +92,7 @@ "Yoast\\WP\\SEO\\Composer\\Actions::check_coding_standards" ], "check-cs-thresholds": [ - "@putenv YOASTCS_THRESHOLD_ERRORS=2549", + "@putenv YOASTCS_THRESHOLD_ERRORS=2548", "@putenv YOASTCS_THRESHOLD_WARNINGS=267", "Yoast\\WP\\SEO\\Composer\\Actions::check_cs_thresholds" ], diff --git a/tests/Unit/Admin/User_Profile_Test.php b/tests/Unit/Admin/User_Profile_Test.php index 9f70d799c26..7a8c945b7b5 100644 --- a/tests/Unit/Admin/User_Profile_Test.php +++ b/tests/Unit/Admin/User_Profile_Test.php @@ -2,7 +2,6 @@ namespace Yoast\WP\SEO\Tests\Unit\Admin; -use Brain\Monkey; use WPSEO_Admin_User_Profile; use Yoast\WP\SEO\Tests\Unit\TestCase; diff --git a/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php b/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php new file mode 100644 index 00000000000..47edfd1c382 --- /dev/null +++ b/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php @@ -0,0 +1,184 @@ +promotion_manager = \YoastSEO()->classes->get( Promotion_Manager::class ); + $this->short_link_helper = \YoastSEO()->helpers->short_link; + $this->wistia_embed_repo = Mockery::mock( Wistia_Embed_Permission_Repository::class ); + $this->wistia_embed_repo->expects( 'get_value_for_user' )->with( 0 )->andReturnTrue(); + $this->meta_surface = \YoastSEO()->meta; + $this->product_helper = \YoastSEO()->helpers->product; + $this->alert_dismissal_action = \YoastSEO()->classes->get( Alert_Dismissal_Action::class ); + + $this->instance = new Post_Site_Information( $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + $this->instance->set_permalink( 'perma' ); + } + + /** + * Tests the get_legacy_site_information. + * + * @covers ::__construct + * @covers ::get_legacy_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_permalink + * + * @return void + */ + public function test_legacy_site_information() { + $expected = [ + 'metabox' => + [ + 'search_url' => 'http://example.org/wp-admin/edit.php?seo_kw_filter={keyword}', + 'post_edit_url' => 'http://example.org/wp-admin/post.php?post={id}&action=edit', + 'base_url' => 'http://example.org/', + 'site_name' => 'Test Blog', + 'contentLocale' => 'en_US', + 'userLocale' => 'en_US', + 'isRtl' => false, + 'isPremium' => false, + 'siteIconUrl' => '', + ], + 'dismissedAlerts' => false, + 'currentPromotions' => [], + 'webinarIntroBlockEditorUrl' => 'https://yoa.st/webinar-intro-block-editor?php_version=7.4&platform=wordpress&platform_version=6.6-alpha-57778-src&software=free&software_version=22.7-RC3&days_active=14&user_language=en_US', + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => + [ + 'php_version' => '7.4', + 'platform' => 'wordpress', + 'platform_version' => '6.6-alpha-57778-src', + 'software' => 'free', + 'software_version' => '22.7-RC3', + 'days_active' => 14, + 'user_language' => 'en_US', + ], + 'pluginUrl' => 'http://example.org/wp-content/plugins/wordpress-seo', + 'wistiaEmbedPermission' => true, + ]; + + $this->assertSame( $expected, $this->instance->get_legacy_site_information() ); + } + + /** + * Tests the get_site_information. + * + * @covers ::__construct + * @covers ::get_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_permalink + * + * @return void + */ + public function test_site_information() { + $expected = [ + 'search_url' => 'http://example.org/wp-admin/edit.php?seo_kw_filter={keyword}', + 'post_edit_url' => 'http://example.org/wp-admin/post.php?post={id}&action=edit', + 'base_url' => 'http://example.org/', + 'dismissedAlerts' => false, + 'currentPromotions' => [], + 'webinarIntroBlockEditorUrl' => 'https://yoa.st/webinar-intro-block-editor?php_version=7.4&platform=wordpress&platform_version=6.6-alpha-57778-src&software=free&software_version=22.7-RC3&days_active=14&user_language=en_US', + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => + [ + 'php_version' => '7.4', + 'platform' => 'wordpress', + 'platform_version' => '6.6-alpha-57778-src', + 'software' => 'free', + 'software_version' => '22.7-RC3', + 'days_active' => 14, + 'user_language' => 'en_US', + ], + 'pluginUrl' => 'http://example.org/wp-content/plugins/wordpress-seo', + 'wistiaEmbedPermission' => true, + 'site_name' => 'Test Blog', + 'contentLocale' => 'en_US', + 'userLocale' => 'en_US', + 'isRtl' => false, + 'isPremium' => false, + 'siteIconUrl' => '', + + ]; + + $this->assertSame( $expected, $this->instance->get_site_information() ); + } +} diff --git a/tests/WP/Formatter/Metabox_Formatter_Test.php b/tests/WP/Formatter/Metabox_Formatter_Test.php index 60c4225ad1f..80aa4a9ba5c 100644 --- a/tests/WP/Formatter/Metabox_Formatter_Test.php +++ b/tests/WP/Formatter/Metabox_Formatter_Test.php @@ -34,7 +34,6 @@ public function test_getting_the_values() { $result = $class_instance->get_values(); $this->assertEquals( 'Readability', $result['contentTab'] ); - $this->assertTrue( \array_key_exists( 'contentLocale', $result ) ); $this->assertTrue( \array_key_exists( 'translations', $result ) ); $this->assertTrue( \is_array( $result['translations'] ) ); } diff --git a/tests/WP/Formatter/Post_Metabox_Formatter_Test.php b/tests/WP/Formatter/Post_Metabox_Formatter_Test.php index ec909cf80c6..25845667e20 100644 --- a/tests/WP/Formatter/Post_Metabox_Formatter_Test.php +++ b/tests/WP/Formatter/Post_Metabox_Formatter_Test.php @@ -29,26 +29,6 @@ public function set_up() { $this->post = $this->factory->post->create_and_get(); } - /** - * Test the formatter when there isn't a post object and without any options. - * - * @covers WPSEO_Post_Metabox_Formatter::__construct - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::search_url - * @covers WPSEO_Post_Metabox_Formatter::edit_url - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_no_post_with_empty_options() { - $instance = new WPSEO_Post_Metabox_Formatter( null, [], '' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['search_url'], \admin_url( 'edit.php?seo_kw_filter={keyword}' ) ); - $this->assertEquals( $result['post_edit_url'], \admin_url( 'post.php?post={id}&action=edit' ) ); - $this->assertEquals( $result['base_url'], \YoastSEO()->helpers->url->home() ); - } - /** * Test with a post being set but with no options being set. * @@ -90,69 +70,4 @@ public function test_metabox_metadescription_date() { $this->assertEquals( $result['title_template'], 'This is the title' ); $this->assertEquals( $result['metadesc_template'], 'This is the metadescription' ); } - - /** - * Testing the formatter when 'being' on the new post page. - * - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_post_on_add_page() { - - $GLOBALS['pagenow'] = 'post-new.php'; - - $instance = new WPSEO_Post_Metabox_Formatter( $this->post, [], '' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['base_url'], \YoastSEO()->helpers->url->home() ); - - unset( $GLOBALS['pagenow'] ); - } - - /** - * Testing when the permalink structure contains '%postname%/'. This should be stripped. - * - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_with_permalink_structure() { - $instance = new WPSEO_Post_Metabox_Formatter( $this->post, [], 'http://example.org/test/%postname%/' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['base_url'], 'http://example.org/test/' ); - } - - /** - * Testing when the permalink structure contains '%pagename%/'. This should be stripped. - * - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_with_page_permalink_structure() { - $instance = new WPSEO_Post_Metabox_Formatter( $this->post, [], 'http://example.org/test/%pagename%/' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['base_url'], 'http://example.org/test/' ); - } - - /** - * Testing when the permalink structure contains '%postname%/'. This should be stripped. - * - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_with_unreplaceble_permalink_structure() { - $instance = new WPSEO_Post_Metabox_Formatter( $this->post, [], '%isnotreplaced%/' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['base_url'], \YoastSEO()->helpers->url->home() ); - } } diff --git a/tests/WP/Formatter/Term_Metabox_Formatter_Test.php b/tests/WP/Formatter/Term_Metabox_Formatter_Test.php index 8b902914ba0..bb19baaba5a 100644 --- a/tests/WP/Formatter/Term_Metabox_Formatter_Test.php +++ b/tests/WP/Formatter/Term_Metabox_Formatter_Test.php @@ -76,10 +76,6 @@ public function test_with_taxonomy_and_term_and_without_options() { $result = $instance->get_values(); - $this->assertEquals( $result['search_url'], \admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' ) ); - $this->assertEquals( $result['post_edit_url'], \admin_url( 'term.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' ) ); - - $this->assertEquals( \trailingslashit( \home_url( 'tag' ) ), $result['base_url'] ); $this->assertEquals( [ '' => [] ], $result['keyword_usage'] ); $this->assertEquals( '%%term_title%% Archives %%page%% %%sep%% %%sitename%%', $result['title_template'] ); $this->assertEquals( '', $result['metadesc_template'] ); From 41803e3b41e438d98b7896181e77fa0aa9c61e7c Mon Sep 17 00:00:00 2001 From: Thijs van der heijden Date: Tue, 7 May 2024 12:21:48 +0200 Subject: [PATCH 4/9] Use dynamic links. --- .../Site/Post_Site_Information_Test.php | 26 +++---------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php b/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php index 47edfd1c382..45f75860f69 100644 --- a/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php +++ b/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php @@ -118,18 +118,9 @@ public function test_legacy_site_information() { ], 'dismissedAlerts' => false, 'currentPromotions' => [], - 'webinarIntroBlockEditorUrl' => 'https://yoa.st/webinar-intro-block-editor?php_version=7.4&platform=wordpress&platform_version=6.6-alpha-57778-src&software=free&software_version=22.7-RC3&days_active=14&user_language=en_US', + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), 'blackFridayBlockEditorUrl' => '', - 'linkParams' => - [ - 'php_version' => '7.4', - 'platform' => 'wordpress', - 'platform_version' => '6.6-alpha-57778-src', - 'software' => 'free', - 'software_version' => '22.7-RC3', - 'days_active' => 14, - 'user_language' => 'en_US', - ], + 'linkParams' => $this->short_link_helper->get_query_params(), 'pluginUrl' => 'http://example.org/wp-content/plugins/wordpress-seo', 'wistiaEmbedPermission' => true, ]; @@ -156,18 +147,9 @@ public function test_site_information() { 'base_url' => 'http://example.org/', 'dismissedAlerts' => false, 'currentPromotions' => [], - 'webinarIntroBlockEditorUrl' => 'https://yoa.st/webinar-intro-block-editor?php_version=7.4&platform=wordpress&platform_version=6.6-alpha-57778-src&software=free&software_version=22.7-RC3&days_active=14&user_language=en_US', + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), 'blackFridayBlockEditorUrl' => '', - 'linkParams' => - [ - 'php_version' => '7.4', - 'platform' => 'wordpress', - 'platform_version' => '6.6-alpha-57778-src', - 'software' => 'free', - 'software_version' => '22.7-RC3', - 'days_active' => 14, - 'user_language' => 'en_US', - ], + 'linkParams' => $this->short_link_helper->get_query_params(), 'pluginUrl' => 'http://example.org/wp-content/plugins/wordpress-seo', 'wistiaEmbedPermission' => true, 'site_name' => 'Test Blog', From 0e183c6ceedc902f681e6be4e787ce8a15bc5acb Mon Sep 17 00:00:00 2001 From: Thijs van der heijden Date: Thu, 30 May 2024 11:42:34 +0200 Subject: [PATCH 5/9] Feedback. --- admin/metabox/class-metabox.php | 7 +++--- admin/taxonomy/class-taxonomy.php | 5 ++-- .../site/site-information-interface.php | 21 ---------------- .../framework/site/base-site-information.php | 20 +++------------ .../framework/site/post-site-information.php | 13 ++++++++++ .../Site/Post_Site_Information_Test.php | 25 ++++++++++--------- 6 files changed, 37 insertions(+), 54 deletions(-) delete mode 100644 src/editors/domain/site/site-information-interface.php diff --git a/admin/metabox/class-metabox.php b/admin/metabox/class-metabox.php index 0be2f9ae3b5..00b22db821c 100644 --- a/admin/metabox/class-metabox.php +++ b/admin/metabox/class-metabox.php @@ -939,8 +939,9 @@ public function enqueue() { * @var $repo Website_Information_Repository */ $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); - $repo->get_post_site_information()->set_permalink( $this->get_permalink() ); - $script_data = array_merge_recursive( $repo->get_post_site_information()->get_site_information(), $script_data ); + $site_information = $repo->get_post_site_information(); + $site_information->set_permalink( $this->get_permalink() ); + $script_data = array_merge_recursive( $site_information->get_legacy_site_information(), $script_data ); if ( post_type_supports( get_post_type(), 'thumbnail' ) ) { $asset_manager->enqueue_style( 'featured-image' ); @@ -1204,7 +1205,7 @@ protected function get_product_title() { * * @return string */ - public function get_permalink() { + protected function get_permalink() { $permalink = ''; if ( is_object( $this->get_metabox_post() ) ) { diff --git a/admin/taxonomy/class-taxonomy.php b/admin/taxonomy/class-taxonomy.php index b7186c2698d..f57ba9f3240 100644 --- a/admin/taxonomy/class-taxonomy.php +++ b/admin/taxonomy/class-taxonomy.php @@ -200,8 +200,9 @@ public function admin_enqueue_scripts() { * @var $repo Website_Information_Repository */ $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); - $repo->get_term_site_information()->set_term( get_term_by( 'id', $tag_id, $this::get_taxonomy() ) ); - $script_data = array_merge_recursive( $repo->get_term_site_information()->get_site_information(), $script_data ); + $term_information = $repo->get_term_site_information(); + $term_information->set_term( get_term_by( 'id', $tag_id, $this::get_taxonomy() ) ); + $script_data = array_merge_recursive( $term_information->get_site_information(), $script_data ); $asset_manager->localize_script( 'term-edit', 'wpseoScriptData', $script_data ); $asset_manager->enqueue_user_language_script(); diff --git a/src/editors/domain/site/site-information-interface.php b/src/editors/domain/site/site-information-interface.php deleted file mode 100644 index 01c3abb6fd6..00000000000 --- a/src/editors/domain/site/site-information-interface.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ - public function get_site_information(): array; - - /** - * Returns specific site information together with the generic site information compatible with a legacy way. - * - * @return array - */ - public function get_legacy_site_information(): array; -} diff --git a/src/editors/framework/site/base-site-information.php b/src/editors/framework/site/base-site-information.php index 6cbae89c8a8..7193f7e6ac8 100644 --- a/src/editors/framework/site/base-site-information.php +++ b/src/editors/framework/site/base-site-information.php @@ -14,28 +14,28 @@ /** * The Base_Site_Information class. */ -abstract class Base_Site_Information implements Site_Information_Interface { +abstract class Base_Site_Information { /** * The alert dismissal action. * * @var Alert_Dismissal_Action $alert_dismissal_action */ - private $alert_dismissal_action; + protected $alert_dismissal_action; /** * The promotion manager. * * @var Promotion_Manager $promotion_manager */ - private $promotion_manager; + protected $promotion_manager; /** * The short link helper. * * @var Short_Link_Helper $shortlink_helper */ - private $short_link_helper; + protected $short_link_helper; /** * The wistia embed permission repository. @@ -92,13 +92,7 @@ public function __construct( * @return array */ public function get_site_information(): array { - $dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); - return [ - 'dismissedAlerts' => $dismissed_alerts, - 'currentPromotions' => $this->promotion_manager->get_current_promotions(), - 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), - 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', 'linkParams' => $this->short_link_helper->get_query_params(), 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), @@ -118,17 +112,11 @@ public function get_site_information(): array { * @return array */ public function get_legacy_site_information(): array { - $dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); return [ - 'dismissedAlerts' => $dismissed_alerts, - 'currentPromotions' => $this->promotion_manager->get_current_promotions(), - 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), - 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', 'linkParams' => $this->short_link_helper->get_query_params(), 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), - 'metabox' => [ 'site_name' => $this->meta->for_current_page()->site_name, 'contentLocale' => \get_locale(), diff --git a/src/editors/framework/site/post-site-information.php b/src/editors/framework/site/post-site-information.php index 741ec9412e8..d347e0cab02 100644 --- a/src/editors/framework/site/post-site-information.php +++ b/src/editors/framework/site/post-site-information.php @@ -31,7 +31,14 @@ public function set_permalink( string $permalink ): void { * @return array */ public function get_legacy_site_information(): array { + $dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); + $data = [ + 'dismissedAlerts' => $dismissed_alerts, + 'currentPromotions' => $this->promotion_manager->get_current_promotions(), + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), + 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', + 'metabox' => [ 'search_url' => $this->search_url(), 'post_edit_url' => $this->edit_url(), @@ -48,7 +55,13 @@ public function get_legacy_site_information(): array { * @return array */ public function get_site_information(): array { + $dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); + $data = [ + 'dismissedAlerts' => $dismissed_alerts, + 'currentPromotions' => $this->promotion_manager->get_current_promotions(), + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), + 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', 'search_url' => $this->search_url(), 'post_edit_url' => $this->edit_url(), 'base_url' => $this->base_url_for_js(), diff --git a/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php index 2f72a31f2fa..ef4259feeb9 100644 --- a/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php +++ b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php @@ -108,6 +108,15 @@ protected function set_up() { */ public function test_legacy_site_information() { $expected = [ + 'dismissedAlerts' => [ + 'the alert', + ], + 'currentPromotions' => [ + 'the promotion', + 'another one', + ], + 'webinarIntroBlockEditorUrl' => 'https://expl.c', + 'blackFridayBlockEditorUrl' => '', 'metabox' => [ 'search_url' => 'https://example.org', 'post_edit_url' => 'https://example.org', @@ -119,15 +128,6 @@ public function test_legacy_site_information() { 'isPremium' => true, 'siteIconUrl' => 'https://example.org', ], - 'dismissedAlerts' => [ - 'the alert', - ], - 'currentPromotions' => [ - 'the promotion', - 'another one', - ], - 'webinarIntroBlockEditorUrl' => 'https://expl.c', - 'blackFridayBlockEditorUrl' => '', 'linkParams' => [ 'param', 'param2', @@ -157,9 +157,6 @@ public function test_legacy_site_information() { */ public function test_site_information() { $expected = [ - 'search_url' => 'https://example.org', - 'post_edit_url' => 'https://example.org', - 'base_url' => 'https://example.org', 'dismissedAlerts' => [ 'the alert', @@ -170,6 +167,10 @@ public function test_site_information() { ], 'webinarIntroBlockEditorUrl' => 'https://expl.c', 'blackFridayBlockEditorUrl' => '', + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', + 'linkParams' => [ 'param', 'param2', From b977e057b160f996fbaaa1a8ff70d253caaa4b17 Mon Sep 17 00:00:00 2001 From: Thijs van der heijden Date: Thu, 30 May 2024 12:28:34 +0200 Subject: [PATCH 6/9] CS. --- admin/metabox/class-metabox.php | 2 +- admin/taxonomy/class-taxonomy.php | 2 +- src/editors/framework/site/base-site-information.php | 1 - src/editors/framework/site/post-site-information.php | 8 ++++---- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/admin/metabox/class-metabox.php b/admin/metabox/class-metabox.php index 00b22db821c..10c3a921da5 100644 --- a/admin/metabox/class-metabox.php +++ b/admin/metabox/class-metabox.php @@ -938,7 +938,7 @@ public function enqueue() { * * @var $repo Website_Information_Repository */ - $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); + $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); $site_information = $repo->get_post_site_information(); $site_information->set_permalink( $this->get_permalink() ); $script_data = array_merge_recursive( $site_information->get_legacy_site_information(), $script_data ); diff --git a/admin/taxonomy/class-taxonomy.php b/admin/taxonomy/class-taxonomy.php index f57ba9f3240..a0f1fd40b73 100644 --- a/admin/taxonomy/class-taxonomy.php +++ b/admin/taxonomy/class-taxonomy.php @@ -199,7 +199,7 @@ public function admin_enqueue_scripts() { * * @var $repo Website_Information_Repository */ - $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); + $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); $term_information = $repo->get_term_site_information(); $term_information->set_term( get_term_by( 'id', $tag_id, $this::get_taxonomy() ) ); $script_data = array_merge_recursive( $term_information->get_site_information(), $script_data ); diff --git a/src/editors/framework/site/base-site-information.php b/src/editors/framework/site/base-site-information.php index 7193f7e6ac8..c87587c9d8c 100644 --- a/src/editors/framework/site/base-site-information.php +++ b/src/editors/framework/site/base-site-information.php @@ -4,7 +4,6 @@ use Exception; use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; -use Yoast\WP\SEO\Editors\Domain\Site\Site_Information_Interface; use Yoast\WP\SEO\Helpers\Product_Helper; use Yoast\WP\SEO\Helpers\Short_Link_Helper; use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; diff --git a/src/editors/framework/site/post-site-information.php b/src/editors/framework/site/post-site-information.php index d347e0cab02..454ae44dcc4 100644 --- a/src/editors/framework/site/post-site-information.php +++ b/src/editors/framework/site/post-site-information.php @@ -39,7 +39,7 @@ public function get_legacy_site_information(): array { 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', - 'metabox' => [ + 'metabox' => [ 'search_url' => $this->search_url(), 'post_edit_url' => $this->edit_url(), 'base_url' => $this->base_url_for_js(), @@ -62,9 +62,9 @@ public function get_site_information(): array { 'currentPromotions' => $this->promotion_manager->get_current_promotions(), 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', - 'search_url' => $this->search_url(), - 'post_edit_url' => $this->edit_url(), - 'base_url' => $this->base_url_for_js(), + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), ]; return \array_merge( $data, parent::get_site_information() ); From d8ef0d808e368468f75baaa77f82c2c5b2308634 Mon Sep 17 00:00:00 2001 From: Igor <35524806+igorschoester@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:44:29 +0200 Subject: [PATCH 7/9] Fix taxonomy metadata: should use legacy formatting still --- admin/taxonomy/class-taxonomy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/taxonomy/class-taxonomy.php b/admin/taxonomy/class-taxonomy.php index a0f1fd40b73..41edb10ca38 100644 --- a/admin/taxonomy/class-taxonomy.php +++ b/admin/taxonomy/class-taxonomy.php @@ -202,7 +202,7 @@ public function admin_enqueue_scripts() { $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); $term_information = $repo->get_term_site_information(); $term_information->set_term( get_term_by( 'id', $tag_id, $this::get_taxonomy() ) ); - $script_data = array_merge_recursive( $term_information->get_site_information(), $script_data ); + $script_data = array_merge_recursive( $term_information->get_legacy_site_information(), $script_data ); $asset_manager->localize_script( 'term-edit', 'wpseoScriptData', $script_data ); $asset_manager->enqueue_user_language_script(); From dc4e5fad4e9aeafc8f1a1633aa3762a5db71fe20 Mon Sep 17 00:00:00 2001 From: Igor <35524806+igorschoester@users.noreply.github.com> Date: Mon, 1 Jul 2024 16:31:24 +0200 Subject: [PATCH 8/9] Move dependencies specific to posts out of the base The mocks in the trait did not work for terms since the counts were off. This seems like the actual fix. Note: I took the freedom to ensure the base uses protected on the dependencies. But private in the post/term ones. --- .../framework/site/base-site-information.php | 57 ++++++------------- .../framework/site/post-site-information.php | 49 +++++++++++++++- .../framework/site/term-site-information.php | 15 ++--- .../Editors/Site_Information_Mocks_Trait.php | 7 --- .../Site/Post_Site_Information_Test.php | 10 ++++ .../Site/Term_Site_Information_Test.php | 40 +------------ 6 files changed, 82 insertions(+), 96 deletions(-) diff --git a/src/editors/framework/site/base-site-information.php b/src/editors/framework/site/base-site-information.php index c87587c9d8c..7620b3a7b2e 100644 --- a/src/editors/framework/site/base-site-information.php +++ b/src/editors/framework/site/base-site-information.php @@ -3,11 +3,9 @@ namespace Yoast\WP\SEO\Editors\Framework\Site; use Exception; -use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; use Yoast\WP\SEO\Helpers\Product_Helper; use Yoast\WP\SEO\Helpers\Short_Link_Helper; use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; -use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; use Yoast\WP\SEO\Surfaces\Meta_Surface; /** @@ -15,20 +13,6 @@ */ abstract class Base_Site_Information { - /** - * The alert dismissal action. - * - * @var Alert_Dismissal_Action $alert_dismissal_action - */ - protected $alert_dismissal_action; - - /** - * The promotion manager. - * - * @var Promotion_Manager $promotion_manager - */ - protected $promotion_manager; - /** * The short link helper. * @@ -41,47 +25,41 @@ abstract class Base_Site_Information { * * @var Wistia_Embed_Permission_Repository $wistia_embed_permission_repository */ - private $wistia_embed_permission_repository; + protected $wistia_embed_permission_repository; /** * The meta surface. * * @var Meta_Surface $meta */ - private $meta; + protected $meta; /** * The product helper. * * @var Product_Helper $product_helper */ - private $product_helper; + protected $product_helper; /** * The constructor. * - * @param Promotion_Manager $promotion_manager The promotion manager. * @param Short_Link_Helper $short_link_helper The short link helper. * @param Wistia_Embed_Permission_Repository $wistia_embed_permission_repository The wistia embed permission * repository. * @param Meta_Surface $meta The meta surface. * @param Product_Helper $product_helper The product helper. - * @param Alert_Dismissal_Action $alert_dismissal_action The alert dismissal action. */ public function __construct( - Promotion_Manager $promotion_manager, Short_Link_Helper $short_link_helper, Wistia_Embed_Permission_Repository $wistia_embed_permission_repository, Meta_Surface $meta, - Product_Helper $product_helper, - Alert_Dismissal_Action $alert_dismissal_action + Product_Helper $product_helper ) { - $this->promotion_manager = $promotion_manager; $this->short_link_helper = $short_link_helper; $this->wistia_embed_permission_repository = $wistia_embed_permission_repository; $this->meta = $meta; $this->product_helper = $product_helper; - $this->alert_dismissal_action = $alert_dismissal_action; } /** @@ -92,15 +70,15 @@ public function __construct( */ public function get_site_information(): array { return [ - 'linkParams' => $this->short_link_helper->get_query_params(), - 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), - 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), - 'site_name' => $this->meta->for_current_page()->site_name, - 'contentLocale' => \get_locale(), - 'userLocale' => \get_user_locale(), - 'isRtl' => \is_rtl(), - 'isPremium' => $this->product_helper->is_premium(), - 'siteIconUrl' => \get_site_icon_url(), + 'linkParams' => $this->short_link_helper->get_query_params(), + 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), + 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), + 'site_name' => $this->meta->for_current_page()->site_name, + 'contentLocale' => \get_locale(), + 'userLocale' => \get_user_locale(), + 'isRtl' => \is_rtl(), + 'isPremium' => $this->product_helper->is_premium(), + 'siteIconUrl' => \get_site_icon_url(), ]; } @@ -111,12 +89,11 @@ public function get_site_information(): array { * @return array */ public function get_legacy_site_information(): array { - return [ - 'linkParams' => $this->short_link_helper->get_query_params(), - 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), - 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), - 'metabox' => [ + 'linkParams' => $this->short_link_helper->get_query_params(), + 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), + 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), + 'metabox' => [ 'site_name' => $this->meta->for_current_page()->site_name, 'contentLocale' => \get_locale(), 'userLocale' => \get_user_locale(), diff --git a/src/editors/framework/site/post-site-information.php b/src/editors/framework/site/post-site-information.php index 454ae44dcc4..bd0b85b7c41 100644 --- a/src/editors/framework/site/post-site-information.php +++ b/src/editors/framework/site/post-site-information.php @@ -2,6 +2,13 @@ // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong namespace Yoast\WP\SEO\Editors\Framework\Site; +use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; +use Yoast\WP\SEO\Helpers\Product_Helper; +use Yoast\WP\SEO\Helpers\Short_Link_Helper; +use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; +use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; +use Yoast\WP\SEO\Surfaces\Meta_Surface; + /** * The Post_Site_Information class. */ @@ -14,6 +21,46 @@ class Post_Site_Information extends Base_Site_Information { */ private $permalink; + /** + * The alert dismissal action. + * + * @var Alert_Dismissal_Action $alert_dismissal_action + */ + private $alert_dismissal_action; + + /** + * The promotion manager. + * + * @var Promotion_Manager $promotion_manager + */ + private $promotion_manager; + + /** + * Constructs the class. + * + * @param Promotion_Manager $promotion_manager The promotion manager. + * @param Short_Link_Helper $short_link_helper The short link helper. + * @param Wistia_Embed_Permission_Repository $wistia_embed_permission_repository The wistia embed permission + * repository. + * @param Meta_Surface $meta The meta surface. + * @param Product_Helper $product_helper The product helper. + * @param Alert_Dismissal_Action $alert_dismissal_action The alert dismissal action. + * + * @return void + */ + public function __construct( + Promotion_Manager $promotion_manager, + Short_Link_Helper $short_link_helper, + Wistia_Embed_Permission_Repository $wistia_embed_permission_repository, + Meta_Surface $meta, + Product_Helper $product_helper, + Alert_Dismissal_Action $alert_dismissal_action + ) { + parent::__construct( $short_link_helper, $wistia_embed_permission_repository, $meta, $product_helper ); + $this->promotion_manager = $promotion_manager; + $this->alert_dismissal_action = $alert_dismissal_action; + } + /** * Sets the permalink. * @@ -39,7 +86,7 @@ public function get_legacy_site_information(): array { 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', - 'metabox' => [ + 'metabox' => [ 'search_url' => $this->search_url(), 'post_edit_url' => $this->edit_url(), 'base_url' => $this->base_url_for_js(), diff --git a/src/editors/framework/site/term-site-information.php b/src/editors/framework/site/term-site-information.php index 939b36007d5..90c212f2a5a 100644 --- a/src/editors/framework/site/term-site-information.php +++ b/src/editors/framework/site/term-site-information.php @@ -4,12 +4,10 @@ use WP_Taxonomy; use WP_Term; -use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; use Yoast\WP\SEO\Helpers\Options_Helper; use Yoast\WP\SEO\Helpers\Product_Helper; use Yoast\WP\SEO\Helpers\Short_Link_Helper; use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; -use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; use Yoast\WP\SEO\Surfaces\Meta_Surface; /** @@ -22,7 +20,7 @@ class Term_Site_Information extends Base_Site_Information { * * @var Options_Helper */ - protected $options_helper; + private $options_helper; /** * The taxonomy. @@ -42,23 +40,20 @@ class Term_Site_Information extends Base_Site_Information { * The constructor. * * @param Options_Helper $options_helper The options helper. - * @param Promotion_Manager $promotion_manager The promotion manager. * @param Short_Link_Helper $short_link_helper The short link helper. - * @param Wistia_Embed_Permission_Repository $wistia_embed_permission_repository The wistia embed permission repository. + * @param Wistia_Embed_Permission_Repository $wistia_embed_permission_repository The wistia embed permission + * repository. * @param Meta_Surface $meta The meta surface. * @param Product_Helper $product_helper The product helper. - * @param Alert_Dismissal_Action $alert_dismissal_action The alert dismissal action. */ public function __construct( Options_Helper $options_helper, - Promotion_Manager $promotion_manager, Short_Link_Helper $short_link_helper, Wistia_Embed_Permission_Repository $wistia_embed_permission_repository, Meta_Surface $meta, - Product_Helper $product_helper, - Alert_Dismissal_Action $alert_dismissal_action + Product_Helper $product_helper ) { - parent::__construct( $promotion_manager, $short_link_helper, $wistia_embed_permission_repository, $meta, $product_helper, $alert_dismissal_action ); + parent::__construct( $short_link_helper, $wistia_embed_permission_repository, $meta, $product_helper ); $this->options_helper = $options_helper; } diff --git a/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php b/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php index b0685b617cf..2e729abc03b 100644 --- a/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php +++ b/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php @@ -12,9 +12,6 @@ trait Site_Information_Mocks_Trait { * @return void */ public function set_mocks() { - $dismissed_alerts = [ 'the alert' ]; - $promotions = [ 'the promotion', 'another one' ]; - $short_link = 'https://expl.c'; $query_params = [ 'param', 'param2' ]; $plugin_url = '/location'; $meta_surface = new stdClass(); @@ -28,10 +25,6 @@ public function set_mocks() { Monkey\Functions\expect( 'is_rtl' )->andReturnFalse(); Monkey\Functions\expect( 'get_site_icon_url' )->andReturn( 'https://example.org' ); - $this->alert_dismissal_action->expects( 'all_dismissed' )->andReturn( $dismissed_alerts ); - $this->promotion_manager->expects( 'get_current_promotions' )->andReturn( $promotions ); - $this->promotion_manager->expects( 'is' )->andReturnFalse(); - $this->short_link_helper->expects( 'get' )->andReturn( $short_link ); $this->short_link_helper->expects( 'get_query_params' )->andReturn( $query_params ); $this->wistia_embed_repo->expects( 'get_value_for_user' )->with( 1 )->andReturnTrue(); $this->meta_surface->expects( 'for_current_page' )->andReturn( $meta_surface ); diff --git a/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php index ef4259feeb9..d42cde5715c 100644 --- a/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php +++ b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php @@ -140,6 +140,11 @@ public function test_legacy_site_information() { Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + $this->alert_dismissal_action->expects( 'all_dismissed' )->andReturn( [ 'the alert' ] ); + $this->promotion_manager->expects( 'get_current_promotions' )->andReturn( [ 'the promotion', 'another one' ] ); + $this->promotion_manager->expects( 'is' )->andReturnFalse(); + $this->short_link_helper->expects( 'get' )->andReturn( 'https://expl.c' ); + $this->assertSame( $expected, $this->instance->get_legacy_site_information() ); } @@ -189,6 +194,11 @@ public function test_site_information() { Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + $this->alert_dismissal_action->expects( 'all_dismissed' )->andReturn( [ 'the alert' ] ); + $this->promotion_manager->expects( 'get_current_promotions' )->andReturn( [ 'the promotion', 'another one' ] ); + $this->promotion_manager->expects( 'is' )->andReturnFalse(); + $this->short_link_helper->expects( 'get' )->andReturn( 'https://expl.c' ); + $this->assertSame( $expected, $this->instance->get_site_information() ); } } diff --git a/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php index 310200fd200..b2147ee880b 100644 --- a/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php +++ b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php @@ -7,13 +7,11 @@ use Mockery; use WP_Taxonomy; use WP_Term; -use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; use Yoast\WP\SEO\Editors\Framework\Site\Term_Site_Information; use Yoast\WP\SEO\Helpers\Options_Helper; use Yoast\WP\SEO\Helpers\Product_Helper; use Yoast\WP\SEO\Helpers\Short_Link_Helper; use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; -use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; use Yoast\WP\SEO\Surfaces\Meta_Surface; use Yoast\WP\SEO\Tests\Unit\Doubles\Editors\Site_Information_Mocks_Trait; use Yoast\WP\SEO\Tests\Unit\TestCase; @@ -34,21 +32,7 @@ final class Term_Site_Information_Test extends TestCase { * * @var Mockery\MockInterface|Options_Helper */ - protected $options_helper; - - /** - * Holds the Alert_Dismissal_Action instance. - * - * @var Mockery\MockInterface|Alert_Dismissal_Action - */ - protected $alert_dismissal_action; - - /** - * Holds the Promotion_Manager instance. - * - * @var Mockery\MockInterface|Promotion_Manager - */ - private $promotion_manager; + private $options_helper; /** * Holds the Short_Link_Helper instance. @@ -93,14 +77,12 @@ final class Term_Site_Information_Test extends TestCase { protected function set_up() { parent::set_up(); $this->options_helper = Mockery::mock( Options_Helper::class ); - $this->promotion_manager = Mockery::mock( Promotion_Manager::class ); $this->short_link_helper = Mockery::mock( Short_Link_Helper::class ); $this->wistia_embed_repo = Mockery::mock( Wistia_Embed_Permission_Repository::class ); $this->meta_surface = Mockery::mock( Meta_Surface::class ); $this->product_helper = Mockery::mock( Product_Helper::class ); - $this->alert_dismissal_action = Mockery::mock( Alert_Dismissal_Action::class ); - $this->instance = new Term_Site_Information( $this->options_helper, $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + $this->instance = new Term_Site_Information( $this->options_helper, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper ); $taxonomy = Mockery::mock( WP_Taxonomy::class )->makePartial(); $taxonomy->rewrite = false; $mock_term = Mockery::mock( WP_Term::class )->makePartial(); @@ -133,15 +115,6 @@ public function test_site_information() { 'post_edit_url' => 'https://example.org', 'base_url' => 'https://example.org', - 'dismissedAlerts' => [ - 'the alert', - ], - 'currentPromotions' => [ - 'the promotion', - 'another one', - ], - 'webinarIntroBlockEditorUrl' => 'https://expl.c', - 'blackFridayBlockEditorUrl' => '', 'linkParams' => [ 'param', 'param2', @@ -188,15 +161,6 @@ public function test_legacy_site_information() { 'isPremium' => true, 'siteIconUrl' => 'https://example.org', ], - 'dismissedAlerts' => [ - 'the alert', - ], - 'currentPromotions' => [ - 'the promotion', - 'another one', - ], - 'webinarIntroBlockEditorUrl' => 'https://expl.c', - 'blackFridayBlockEditorUrl' => '', 'linkParams' => [ 'param', 'param2', From 7699f46c095bdae3c8b496debcdda3cc8cd5c60f Mon Sep 17 00:00:00 2001 From: Igor <35524806+igorschoester@users.noreply.github.com> Date: Mon, 1 Jul 2024 16:44:00 +0200 Subject: [PATCH 9/9] Fix PHPCS --- .../framework/site/post-site-information.php | 3 +- .../Site/Term_Site_Information_Test.php | 43 +++++++++---------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/editors/framework/site/post-site-information.php b/src/editors/framework/site/post-site-information.php index bd0b85b7c41..4882642aa7a 100644 --- a/src/editors/framework/site/post-site-information.php +++ b/src/editors/framework/site/post-site-information.php @@ -85,8 +85,7 @@ public function get_legacy_site_information(): array { 'currentPromotions' => $this->promotion_manager->get_current_promotions(), 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', - - 'metabox' => [ + 'metabox' => [ 'search_url' => $this->search_url(), 'post_edit_url' => $this->edit_url(), 'base_url' => $this->base_url_for_js(), diff --git a/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php index b2147ee880b..7eef482a907 100644 --- a/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php +++ b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php @@ -76,11 +76,11 @@ final class Term_Site_Information_Test extends TestCase { */ protected function set_up() { parent::set_up(); - $this->options_helper = Mockery::mock( Options_Helper::class ); - $this->short_link_helper = Mockery::mock( Short_Link_Helper::class ); - $this->wistia_embed_repo = Mockery::mock( Wistia_Embed_Permission_Repository::class ); - $this->meta_surface = Mockery::mock( Meta_Surface::class ); - $this->product_helper = Mockery::mock( Product_Helper::class ); + $this->options_helper = Mockery::mock( Options_Helper::class ); + $this->short_link_helper = Mockery::mock( Short_Link_Helper::class ); + $this->wistia_embed_repo = Mockery::mock( Wistia_Embed_Permission_Repository::class ); + $this->meta_surface = Mockery::mock( Meta_Surface::class ); + $this->product_helper = Mockery::mock( Product_Helper::class ); $this->instance = new Term_Site_Information( $this->options_helper, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper ); $taxonomy = Mockery::mock( WP_Taxonomy::class )->makePartial(); @@ -111,22 +111,21 @@ protected function set_up() { */ public function test_site_information() { $expected = [ - 'search_url' => 'https://example.org', - 'post_edit_url' => 'https://example.org', - 'base_url' => 'https://example.org', - - 'linkParams' => [ + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', + 'linkParams' => [ 'param', 'param2', ], - 'pluginUrl' => '/location', - 'wistiaEmbedPermission' => true, - 'site_name' => 'examepl.com', - 'contentLocale' => 'nl_NL', - 'userLocale' => 'nl_NL', - 'isRtl' => false, - 'isPremium' => true, - 'siteIconUrl' => 'https://example.org', + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, + 'site_name' => 'examepl.com', + 'contentLocale' => 'nl_NL', + 'userLocale' => 'nl_NL', + 'isRtl' => false, + 'isPremium' => true, + 'siteIconUrl' => 'https://example.org', ]; Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); @@ -150,7 +149,7 @@ public function test_site_information() { public function test_legacy_site_information() { $expected = [ - 'metabox' => [ + 'metabox' => [ 'search_url' => 'https://example.org', 'post_edit_url' => 'https://example.org', 'base_url' => 'https://example.org', @@ -161,12 +160,12 @@ public function test_legacy_site_information() { 'isPremium' => true, 'siteIconUrl' => 'https://example.org', ], - 'linkParams' => [ + 'linkParams' => [ 'param', 'param2', ], - 'pluginUrl' => '/location', - 'wistiaEmbedPermission' => true, + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, ];