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 1 commit
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
Next Next commit
Move generic site data into self contained objects.
  • Loading branch information
thijsoo committed May 6, 2024
commit b2c48bce251465cc98605aae3f7bae6bba27ef9b
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 @@ -34,8 +34,8 @@
/**
* Constructor.
*
* @param WP_Post|array $post Post object.

Check failure on line 37 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@param annotation of method \WPSEO_Post_Metabox_Formatter::__construct() does not specify type hint for items of its traversable parameter $post.

Check failure on line 37 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@param annotation of method \WPSEO_Post_Metabox_Formatter::__construct() does not specify type hint for items of its traversable parameter $post.
* @param array $options Title options to use.

Check failure on line 38 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@param annotation of method \WPSEO_Post_Metabox_Formatter::__construct() does not specify type hint for items of its traversable parameter $options.

Check failure on line 38 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@param annotation of method \WPSEO_Post_Metabox_Formatter::__construct() does not specify type hint for items of its traversable parameter $options.
* @param string $structure The permalink to follow.
*/
public function __construct( $post, array $options, $structure ) {
Expand All @@ -57,14 +57,11 @@
/**
* Returns the translated values.
*
* @return array

Check failure on line 60 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Post_Metabox_Formatter::get_values() does not specify type hint for items of its traversable return value.

Check failure on line 60 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Post_Metabox_Formatter::get_values() does not specify type hint for items of its traversable return value.
*/
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,56 +103,10 @@
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.
*
* @return array The keyword and the associated posts that use it.

Check failure on line 109 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Post_Metabox_Formatter::get_focus_keyword_usage() does not specify type hint for items of its traversable return value.

Check failure on line 109 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Post_Metabox_Formatter::get_focus_keyword_usage() does not specify type hint for items of its traversable return value.
*/
private function get_focus_keyword_usage() {
$keyword = WPSEO_Meta::get_value( 'focuskw', $this->post->ID );
Expand All @@ -173,7 +124,7 @@
/**
* Retrieves the post types for the given post IDs.
*
* @param array $post_ids_per_keyword An associative array with keywords as keys and an array of post ids where those keywords are used.

Check failure on line 127 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@param annotation of method \WPSEO_Post_Metabox_Formatter::get_post_types_for_all_ids() does not specify type hint for items of its traversable parameter $post_ids_per_keyword.

Check failure on line 127 in admin/formatter/class-post-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@param annotation of method \WPSEO_Post_Metabox_Formatter::get_post_types_for_all_ids() does not specify type hint for items of its traversable parameter $post_ids_per_keyword.
* @return array The post types for the given post IDs.
*/
private function get_post_types_for_all_ids( $post_ids_per_keyword ) {
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 @@ -34,7 +34,7 @@
/**
* Array with the WPSEO_Titles options.
*
* @var array

Check failure on line 37 in admin/formatter/class-term-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@var annotation of property \WPSEO_Term_Metabox_Formatter::$options does not specify type hint for its items.

Check failure on line 37 in admin/formatter/class-term-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@var annotation of property \WPSEO_Term_Metabox_Formatter::$options does not specify type hint for its items.
*/
protected $options;

Expand Down Expand Up @@ -63,7 +63,7 @@
/**
* Returns the translated values.
*
* @return array

Check failure on line 66 in admin/formatter/class-term-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Term_Metabox_Formatter::get_values() does not specify type hint for items of its traversable return value.

Check failure on line 66 in admin/formatter/class-term-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Term_Metabox_Formatter::get_values() does not specify type hint for items of its traversable return value.
*/
public function get_values() {
$values = [];
Expand All @@ -71,9 +71,6 @@
// 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,45 +98,10 @@
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.
*
* @return array

Check failure on line 104 in admin/formatter/class-term-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Term_Metabox_Formatter::get_focus_keyword_usage() does not specify type hint for items of its traversable return value.

Check failure on line 104 in admin/formatter/class-term-metabox-formatter.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Term_Metabox_Formatter::get_focus_keyword_usage() does not specify type hint for items of its traversable return value.
*/
private function get_focus_keyword_usage() {
$focuskw = WPSEO_Taxonomy_Meta::get_term_meta( $this->term, $this->term->taxonomy, 'focuskw' );
Expand Down
43 changes: 24 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,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() {
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 @@
'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 Expand Up @@ -288,7 +295,7 @@
*
* @param int $term_id The ID of the term to localize the script for.
*
* @return array

Check failure on line 298 in admin/taxonomy/class-taxonomy.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Taxonomy::localize_term_scraper_script() does not specify type hint for items of its traversable return value.

Check failure on line 298 in admin/taxonomy/class-taxonomy.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Taxonomy::localize_term_scraper_script() does not specify type hint for items of its traversable return value.
*/
public function localize_term_scraper_script( $term_id ) {
$term = get_term_by( 'id', $term_id, $this::get_taxonomy() );
Expand All @@ -304,7 +311,7 @@
/**
* Pass some variables to js for replacing variables.
*
* @return array

Check failure on line 314 in admin/taxonomy/class-taxonomy.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Taxonomy::localize_replace_vars_script() does not specify type hint for items of its traversable return value.

Check failure on line 314 in admin/taxonomy/class-taxonomy.php

View workflow job for this annotation

GitHub Actions / Check code style

@return annotation of method \WPSEO_Taxonomy::localize_replace_vars_script() does not specify type hint for items of its traversable return value.
*/
public function localize_replace_vars_script() {
return [
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;
}
}