Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move generic site data into self contained objects. #21368

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 0 additions & 9 deletions admin/formatter/class-metabox-formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down
49 changes: 0 additions & 49 deletions admin/formatter/class-post-metabox-formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
];

Expand Down Expand Up @@ -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.
*
Expand Down
38 changes: 0 additions & 38 deletions admin/formatter/class-term-metabox-formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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.
*
Expand Down
45 changes: 26 additions & 19 deletions admin/metabox/class-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -285,12 +283,7 @@ public function wpseo_metabox_class( $classes ) {
* @return array<string,string|array<string|int|bool>|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 )
Expand Down Expand Up @@ -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();
Expand All @@ -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() );
thijsoo marked this conversation as resolved.
Show resolved Hide resolved
$script_data = array_merge_recursive( $repo->get_post_site_information()->get_site_information(), $script_data );
thijsoo marked this conversation as resolved.
Show resolved Hide resolved
if ( post_type_supports( get_post_type(), 'thumbnail' ) ) {
$asset_manager->enqueue_style( 'featured-image' );

Expand Down Expand Up @@ -1207,4 +1198,20 @@ public static function is_post_edit( $page ) {
protected function get_product_title() {
return YoastSEO()->helpers->product->get_product_name();
}

/**
* Gets the permalink.
*
* @return string
*/
public function get_permalink() {
thijsoo marked this conversation as resolved.
Show resolved Hide resolved
$permalink = '';

if ( is_object( $this->get_metabox_post() ) ) {
$permalink = get_sample_permalink( $this->get_metabox_post()->ID );
$permalink = $permalink[0];
}

return $permalink;
}
}
15 changes: 11 additions & 4 deletions admin/taxonomy/class-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's assign the $repo->get_term_site_information() result to a variable, so as to not call it twice here and in the line above.


$asset_manager->localize_script( 'term-edit', 'wpseoScriptData', $script_data );
$asset_manager->enqueue_user_language_script();
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"Yoast\\WP\\SEO\\Composer\\Actions::check_coding_standards"
],
"check-cs-thresholds": [
"@putenv YOASTCS_THRESHOLD_ERRORS=2545",
"@putenv YOASTCS_THRESHOLD_ERRORS=2544",
"@putenv YOASTCS_THRESHOLD_WARNINGS=253",
"Yoast\\WP\\SEO\\Composer\\Actions::check_cs_thresholds"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand Down
61 changes: 61 additions & 0 deletions src/editors/application/site/website-information-repository.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we creating a repository that just returns instances of a couple of classes? We could just instantiate them in the metabox classes, without the use of a repository.

Unless I'm missing something...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because I wanted to follow the same as I did for all the other script data code, and I wanted the entry point for this to be in the application folder and not in the framework folder to more adhere to the Onion way.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong
namespace Yoast\WP\SEO\Editors\Application\Site;

use Yoast\WP\SEO\Editors\Framework\Site\Post_Site_Information;
use Yoast\WP\SEO\Editors\Framework\Site\Term_Site_Information;

/**
* This class manages getting the two site information wrappers.
*
* @makePublic
*/
class Website_Information_Repository {

/**
* The post site information wrapper.
*
* @var Post_Site_Information $post_site_information
*/
private $post_site_information;

/**
* The term site information wrapper.
*
* @var Term_Site_Information $term_site_information
*/
private $term_site_information;

/**
* The constructor.
*
* @param Post_Site_Information $post_site_information The post specific wrapper.
* @param Term_Site_Information $term_site_information The term specific wrapper.
*/
public function __construct(
Post_Site_Information $post_site_information,
Term_Site_Information $term_site_information
) {
$this->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;
}
}
21 changes: 21 additions & 0 deletions src/editors/domain/site/site-information-interface.php
thijsoo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Editors\Domain\Site;

interface Site_Information_Interface {

/**
* Returns specific site information together with the generic site information.
*
* @return array<string|string,string[]>
*/
public function get_site_information(): array;

/**
* Returns specific site information together with the generic site information compatible with a legacy way.
*
* @return array<string|string,string[]>
*/
public function get_legacy_site_information(): array;
}