Skip to content

Commit

Permalink
Merge pull request #21160 from Yoast/try/refactor-enabled-plugin-feat…
Browse files Browse the repository at this point in the history
…ures

Refactor enabled plugin features
  • Loading branch information
igorschoester committed Mar 5, 2024
2 parents 11af8f4 + c483707 commit 03e8ea6
Show file tree
Hide file tree
Showing 27 changed files with 1,475 additions and 133 deletions.
51 changes: 21 additions & 30 deletions admin/formatter/class-metabox-formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yoast\WP\SEO\Conditionals\Third_Party\WPML_Conditional;
use Yoast\WP\SEO\Config\Schema_Types;
use Yoast\WP\SEO\Config\SEMrush_Client;
use Yoast\WP\SEO\Editors\Application\Analysis_Features\Enabled_Analysis_Features_Repository;
use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception;
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Property_Exception;
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Token_Exception;
Expand Down Expand Up @@ -38,7 +39,7 @@ public function __construct( WPSEO_Metabox_Formatter_Interface $formatter ) {
/**
* Returns the values.
*
* @return array
* @return array<string,string|array<string|int|bool>|bool|int>
*/
public function get_values() {
$defaults = $this->get_defaults();
Expand All @@ -50,17 +51,14 @@ public function get_values() {
/**
* Returns array with all the values always needed by a scraper object.
*
* @return array Default settings for the metabox.
* @return array<string,string|array<string|int|bool>|bool|int> Default settings for the metabox.
*/
private function get_defaults() {
$analysis_seo = new WPSEO_Metabox_Analysis_SEO();
$analysis_readability = new WPSEO_Metabox_Analysis_Readability();
$analysis_inclusive_language = new WPSEO_Metabox_Analysis_Inclusive_Language();
$schema_types = new Schema_Types();
$is_wincher_active = YoastSEO()->helpers->wincher->is_active();
$host = YoastSEO()->helpers->url->get_url_host( get_site_url() );
$schema_types = new Schema_Types();
$is_wincher_active = YoastSEO()->helpers->wincher->is_active();
$host = YoastSEO()->helpers->url->get_url_host( get_site_url() );

return [
$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' ),
Expand All @@ -76,15 +74,10 @@ private function get_defaults() {
'keyword_usage' => [],
'title_template' => '',
'metadesc_template' => '',
'contentAnalysisActive' => $analysis_readability->is_enabled() ? 1 : 0,
'keywordAnalysisActive' => $analysis_seo->is_enabled() ? 1 : 0,
'inclusiveLanguageAnalysisActive' => $analysis_inclusive_language->is_enabled() ? 1 : 0,
'cornerstoneActive' => WPSEO_Options::get( 'enable_cornerstone_content', false ) ? 1 : 0,
'semrushIntegrationActive' => WPSEO_Options::get( 'semrush_integration_active', true ) ? 1 : 0,
'intl' => $this->get_content_analysis_component_translations(),
'isRtl' => is_rtl(),
'isPremium' => YoastSEO()->helpers->product->is_premium(),
'wordFormRecognitionActive' => YoastSEO()->helpers->language->is_word_form_recognition_active( WPSEO_Language_Utils::get_language( get_locale() ) ),
'siteIconUrl' => get_site_icon_url(),
'countryCode' => WPSEO_Options::get( 'semrush_country_code', false ),
'SEMrushLoginStatus' => WPSEO_Options::get( 'semrush_integration_active', true ) ? $this->get_semrush_login_status() : false,
Expand All @@ -98,13 +91,6 @@ private function get_defaults() {
'articleTypeOptions' => $schema_types->get_article_type_options(),
],
'twitterCardType' => 'summary_large_image',

/**
* Filter to determine if the markers should be enabled or not.
*
* @param bool $showMarkers Should the markers being enabled. Default = true.
*/
'show_markers' => apply_filters( 'wpseo_enable_assessment_markers', true ),
'publish_box' => [
'labels' => [
'keyword' => [
Expand Down Expand Up @@ -199,6 +185,12 @@ private function get_defaults() {
],
],
],
/**
* Filter to determine if the markers should be enabled or not.
*
* @param bool $showMarkers Should the markers being enabled. Default = true.
*/
'show_markers' => apply_filters( 'wpseo_enable_assessment_markers', true ),
'markdownEnabled' => $this->is_markdown_enabled(),
'analysisHeadingTitle' => __( 'Analysis', 'wordpress-seo' ),
'zapierIntegrationActive' => WPSEO_Options::get( 'zapier_integration_active', false ) ? 1 : 0,
Expand All @@ -209,24 +201,23 @@ private function get_defaults() {
'wincherAutoAddKeyphrases' => WPSEO_Options::get( 'wincher_automatically_add_keyphrases', false ),
'wordproofIntegrationActive' => YoastSEO()->helpers->wordproof->is_active() ? 1 : 0,
'multilingualPluginActive' => $this->multilingual_plugin_active(),

/**
* Filter to determine whether the PreviouslyUsedKeyword assessment should run.
*
* @param bool $previouslyUsedKeywordActive Whether the PreviouslyUsedKeyword assessment should run.
*/
'previouslyUsedKeywordActive' => apply_filters( 'wpseo_previously_used_keyword_active', true ),
'getJetpackBoostPrePublishLink' => WPSEO_Shortlinker::get( 'https://yoa.st/jetpack-boost-get-prepublish?domain=' . $host ),
'upgradeJetpackBoostPrePublishLink' => WPSEO_Shortlinker::get( 'https://yoa.st/jetpack-boost-upgrade-prepublish?domain=' . $host ),
'woocommerceUpsellSchemaLink' => WPSEO_Shortlinker::get( 'https://yoa.st/product-schema-metabox' ),
'woocommerceUpsellGooglePreviewLink' => WPSEO_Shortlinker::get( 'https://yoa.st/product-google-preview-metabox' ),
];

$enabled_features_repo = YoastSEO()->classes->get( Enabled_Analysis_Features_Repository::class );

$enabled_features = $enabled_features_repo->get_enabled_features()->parse_to_legacy_array();

return array_merge( $defaults, $enabled_features );
}

/**
* Returns required yoast-component translations.
*
* @return array
* @return string[]
*/
private function get_content_analysis_component_translations() {
// Esc_html is not needed because React already handles HTML in the (translations of) these strings.
Expand All @@ -248,7 +239,7 @@ private function get_content_analysis_component_translations() {
/**
* Returns Jed compatible YoastSEO.js translations.
*
* @return array
* @return string[]
*/
private function get_translations() {
$locale = get_user_locale();
Expand Down
25 changes: 12 additions & 13 deletions admin/metabox/class-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ public function add_meta_box() {
/**
* Adds CSS classes to the meta box.
*
* @param array $classes An array of postbox CSS classes.
* @param string[] $classes An array of postbox CSS classes.
*
* @return array List of classes that will be applied to the editbox container.
* @return string[] List of classes that will be applied to the editbox container.
*/
public function wpseo_metabox_class( $classes ) {
$classes[] = 'yoast wpseo-metabox';
Expand All @@ -282,7 +282,7 @@ public function wpseo_metabox_class( $classes ) {
/**
* Passes variables to js for use with the post-scraper.
*
* @return array
* @return array<string,string|array<string|int|bool>|bool|int>
*/
public function get_metabox_script_data() {
$permalink = '';
Expand All @@ -297,7 +297,6 @@ public function get_metabox_script_data() {
);

$values = $post_formatter->get_values();

/** This filter is documented in admin/filters/class-cornerstone-filter.php. */
$post_types = apply_filters( 'wpseo_cornerstone_post_types', WPSEO_Post_Type::get_accessible_post_types() );
if ( $values['cornerstoneActive'] && ! in_array( $this->get_metabox_post()->post_type, $post_types, true ) ) {
Expand Down Expand Up @@ -513,8 +512,8 @@ protected function get_additional_tabs() {
*
* @todo [JRF] Check if $class is added appropriately everywhere.
*
* @param array $meta_field_def Contains the vars based on which output is generated.
* @param string $key Internal key (without prefix).
* @param string[] $meta_field_def Contains the vars based on which output is generated.
* @param string $key Internal key (without prefix).
*
* @return string
*/
Expand Down Expand Up @@ -967,7 +966,7 @@ public function enqueue() {
/**
* Returns post in metabox context.
*
* @return WP_Post|array
* @return WP_Post|array<string|int|bool>
*/
protected function get_metabox_post() {
if ( $this->post !== null ) {
Expand Down Expand Up @@ -996,7 +995,7 @@ protected function get_metabox_post() {
/**
* Returns an array with shortcode tags for all registered shortcodes.
*
* @return array
* @return string[]
*/
private function get_valid_shortcode_tags() {
$shortcode_tags = [];
Expand All @@ -1011,7 +1010,7 @@ private function get_valid_shortcode_tags() {
/**
* Prepares the replace vars for localization.
*
* @return array Replace vars.
* @return string[] Replace vars.
*/
private function get_replace_vars() {
$cached_replacement_vars = [];
Expand Down Expand Up @@ -1060,7 +1059,7 @@ protected function get_hidden_replace_vars() {
/**
* Prepares the recommended replace vars for localization.
*
* @return array Recommended replacement variables.
* @return array<string[]> Recommended replacement variables.
*/
private function get_recommended_replace_vars() {
$recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
Expand All @@ -1076,7 +1075,7 @@ private function get_recommended_replace_vars() {
*
* @param WP_Post $post The post to check for custom taxonomies and fields.
*
* @return array Array containing all the replacement variables.
* @return array<string[]> Array containing all the replacement variables.
*/
private function get_custom_replace_vars( $post ) {
return [
Expand All @@ -1090,7 +1089,7 @@ private function get_custom_replace_vars( $post ) {
*
* @param WP_Post $post The post to check for custom taxonomies.
*
* @return array Array containing all the replacement variables.
* @return array<string[]> Array containing all the replacement variables.
*/
private function get_custom_taxonomies_replace_vars( $post ) {
$taxonomies = get_object_taxonomies( $post, 'objects' );
Expand Down Expand Up @@ -1121,7 +1120,7 @@ private function get_custom_taxonomies_replace_vars( $post ) {
*
* @param WP_Post $post The post to check for custom fields.
*
* @return array Array containing all the replacement variables.
* @return array<string[]> Array containing all the replacement variables.
*/
private function get_custom_fields_replace_vars( $post ) {
$custom_replace_vars = [];
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"Yoast\\WP\\SEO\\Composer\\Actions::check_coding_standards"
],
"check-cs-thresholds": [
"@putenv YOASTCS_THRESHOLD_ERRORS=2601",
"@putenv YOASTCS_THRESHOLD_ERRORS=2583",
"@putenv YOASTCS_THRESHOLD_WARNINGS=294",
"Yoast\\WP\\SEO\\Composer\\Actions::check_cs_thresholds"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/analysis/isContentAnalysisActive.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { get } from "lodash";
export default function isContentAnalysisActive() {
const l10nObject = getL10nObject();

return get( l10nObject, "contentAnalysisActive", 0 ) === 1;
return get( l10nObject, "contentAnalysisActive", false ) === true;
}
2 changes: 1 addition & 1 deletion packages/js/src/analysis/isCornerstoneContentActive.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { get } from "lodash";
export default function isCornerstoneContentActive() {
const l10nObject = getL10nObject();

return get( l10nObject, "cornerstoneActive", 0 ) === 1;
return get( l10nObject, "cornerstoneActive", false ) === true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { get } from "lodash";
export default function isInclusiveLanguageAnalysisActive() {
const l10nObject = getL10nObject();

return get( l10nObject, "inclusiveLanguageAnalysisActive", 0 ) === 1;
return get( l10nObject, "inclusiveLanguageAnalysisActive", false ) === true;
}
2 changes: 1 addition & 1 deletion packages/js/src/analysis/isKeywordAnalysisActive.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { get } from "lodash";
export default function isKeywordAnalysisActive() {
const l10nObject = getL10nObject();

return get( l10nObject, "keywordAnalysisActive", 0 ) === 1;
return get( l10nObject, "keywordAnalysisActive", false ) === true;
}
25 changes: 0 additions & 25 deletions packages/js/src/initializers/post-scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,6 @@ export default function initPostScraper( $, store, editorData ) {
}
} );

/**
* Determines if markers should be shown.
*
* @returns {boolean} True when markers should be shown.
*/
function displayMarkers() {
return ! isBlockEditor() && wpseoScriptData.metabox.show_markers;
}

/**
* Updates the store to indicate if the markers should be hidden.
*
* @param {Object} store The store.
*
* @returns {void}
*/
function updateMarkerStatus( store ) {
// Only add markers when tinyMCE is loaded and show_markers is enabled (can be disabled by a WordPress hook).
// Only check for the tinyMCE object because the actual editor isn't loaded at this moment yet.
if ( typeof window.tinyMCE === "undefined" || ! displayMarkers() ) {
store.dispatch( setMarkerStatus( "disabled" ) );
}
}

/**
* Initializes keyword analysis.
*
Expand Down Expand Up @@ -254,7 +230,6 @@ export default function initPostScraper( $, store, editorData ) {
* @returns {Object} The arguments to initialize the app
*/
function getAppArgs( store ) {
updateMarkerStatus( store );
const args = {
// ID's of elements that need to trigger updating the analyzer.
elementTarget: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

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

use Yoast\WP\SEO\Editors\Domain\Analysis_Features\Analysis_Feature;
use Yoast\WP\SEO\Editors\Domain\Analysis_Features\Analysis_Features_List;
use Yoast\WP\SEO\Editors\Framework\Analysis_Feature_Interface;

/**
* The repository to get all enabled features.
*
* @makePublic
*/
class Enabled_Analysis_Features_Repository {

/**
* All plugin features.
*
* @var Analysis_Feature_Interface[] $plugin_features
*/
private $plugin_features;

/**
* The list of analysis features.
*
* @var Analysis_Features_List $enabled_analysis_features
*/
private $enabled_analysis_features;

/**
* The constructor.
*
* @param Analysis_Feature_Interface ...$plugin_features All analysis objects.
*/
public function __construct( Analysis_Feature_Interface ...$plugin_features ) {
$this->enabled_analysis_features = new Analysis_Features_List();
$this->plugin_features = $plugin_features;
}

/**
* Returns the analysis list.
*
* @return Analysis_Features_List The analysis list.
*/
public function get_enabled_features(): Analysis_Features_List {
if ( \count( $this->enabled_analysis_features->parse_to_legacy_array() ) === 0 ) {
foreach ( $this->plugin_features as $plugin_feature ) {
$analysis_feature = new Analysis_Feature( $plugin_feature->is_enabled(), $plugin_feature->get_name(), $plugin_feature->get_legacy_key() );
$this->enabled_analysis_features->add_feature( $analysis_feature );
}
}
return $this->enabled_analysis_features;
}
}
Loading

0 comments on commit 03e8ea6

Please sign in to comment.