Skip to content

Commit

Permalink
Merge pull request #21161 from Yoast/21141-decouple-hidden-fields-for…
Browse files Browse the repository at this point in the history
…-metabox

21141 decouple hidden fields for metabox
  • Loading branch information
igorschoester authored May 16, 2024
2 parents 97f4aa2 + 76702b7 commit 1ed281f
Show file tree
Hide file tree
Showing 102 changed files with 1,572 additions and 2,344 deletions.
39 changes: 7 additions & 32 deletions admin/class-primary-term-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class WPSEO_Primary_Term_Admin implements WPSEO_WordPress_Integration {

/**
* Constructor.
* Register hooks.
*
* @return void
*/
Expand Down Expand Up @@ -80,7 +80,7 @@ protected function primary_term_field( $taxonomy_name ) {
* @return string The field id.
*/
protected function generate_field_id( $taxonomy_name ) {
return 'yoast-wpseo-primary-' . $taxonomy_name;
return WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy_name;
}

/**
Expand Down Expand Up @@ -156,7 +156,7 @@ protected function get_primary_term( $taxonomy_name ) {
* Returns all the taxonomies for which the primary term selection is enabled.
*
* @param int|null $post_id Default current post ID.
* @return array
* @return array<WP_Taxonomy> The primary term taxonomies.
*/
protected function get_primary_term_taxonomies( $post_id = null ) {
if ( $post_id === null ) {
Expand All @@ -168,7 +168,7 @@ protected function get_primary_term_taxonomies( $post_id = null ) {
return $taxonomies;
}

$taxonomies = $this->generate_primary_term_taxonomies( $post_id );
$taxonomies = YoastSEO()->helpers->primary_term->get_primary_term_taxonomies( $post_id );

wp_cache_set( 'primary_term_taxonomies_' . $post_id, $taxonomies, 'wpseo' );

Expand All @@ -184,37 +184,12 @@ protected function include_js_templates() {
include_once WPSEO_PATH . 'admin/views/js-templates-primary-term.php';
}

/**
* Generates the primary term taxonomies.
*
* @param int $post_id ID of the post.
*
* @return array
*/
protected function generate_primary_term_taxonomies( $post_id ) {
$post_type = get_post_type( $post_id );
$all_taxonomies = get_object_taxonomies( $post_type, 'objects' );
$all_taxonomies = array_filter( $all_taxonomies, [ $this, 'filter_hierarchical_taxonomies' ] );

/**
* Filters which taxonomies for which the user can choose the primary term.
*
* @param array $taxonomies An array of taxonomy objects that are primary_term enabled.
* @param string $post_type The post type for which to filter the taxonomies.
* @param array $all_taxonomies All taxonomies for this post types, even ones that don't have primary term
* enabled.
*/
$taxonomies = (array) apply_filters( 'wpseo_primary_term_taxonomies', $all_taxonomies, $post_type, $all_taxonomies );

return $taxonomies;
}

/**
* Creates a map of taxonomies for localization.
*
* @param array $taxonomies The taxononmies that should be mapped.
* @param array<WP_Taxonomy> $taxonomies The taxonomies that should be mapped.
*
* @return array The mapped taxonomies.
* @return array<string,array<string,string|int|array<int|string>>> The mapped taxonomies.
*/
protected function get_mapped_taxonomies_for_js( $taxonomies ) {
return array_map( [ $this, 'map_taxonomies_for_js' ], $taxonomies );
Expand All @@ -225,7 +200,7 @@ protected function get_mapped_taxonomies_for_js( $taxonomies ) {
*
* @param stdClass $taxonomy The taxonomy to map.
*
* @return array The mapped taxonomy.
* @return array<string,string|int|array<int|string>> The mapped taxonomy.
*/
private function map_taxonomies_for_js( $taxonomy ) {
$primary_term = $this->get_primary_term( $taxonomy->name );
Expand Down
68 changes: 67 additions & 1 deletion admin/formatter/class-post-metabox-formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @package WPSEO\Admin\Formatter
*/

use Yoast\WP\SEO\Config\Schema_Types;
use Yoast\WP\SEO\Editors\Framework\Metadata_Groups;

/**
* This class provides data for the post metabox by return its values for localization.
*/
Expand Down Expand Up @@ -83,6 +86,12 @@ public function get_values() {
'social_description_template' => $this->get_social_description_template(),
'social_image_template' => $this->get_social_image_template(),
'isInsightsEnabled' => $this->is_insights_enabled(),
'metadata' => $this->get_post_metadata(),
'schemaDefaults' => $this->get_schema_defaults( $this->post->post_type ),
'entity' => [
'id' => $this->post->ID,
'slug' => $this->post->post_name,
],
];

$values = ( $values_to_set + $values );
Expand All @@ -92,7 +101,7 @@ public function get_values() {
* Filter: 'wpseo_post_edit_values' - Allows changing the values Yoast SEO uses inside the post editor.
*
* @param array $values The key-value map Yoast SEO uses inside the post editor.
* @param WP_Post $post The post opened in the editor.
* @param WP_Post $post The post opened in the editor.
*/
return apply_filters( 'wpseo_post_edit_values', $values, $this->post );
}
Expand Down Expand Up @@ -314,4 +323,61 @@ private function get_metadesc_date() {
protected function is_insights_enabled() {
return WPSEO_Options::get( 'enable_metabox_insights', false );
}

/**
* Get post meta data.
*
* @return array<string>
*/
private function get_post_metadata() {
$post_type = $this->post->post_type;
$metadata = [];

/**
* The metadata groups.
*
* @var Metadata_Groups $metadata_groups The metadata groups.
*/
$metadata_groups = YoastSEO()->classes->get( Metadata_Groups::class );

$groups = $metadata_groups->get_post_metadata_groups();

foreach ( $groups as $group ) {
$fields = WPSEO_Meta::get_meta_field_defs( $group, $post_type );
foreach ( $fields as $key => $meta_field ) {
$metadata[ $key ] = WPSEO_Meta::get_value( $key, $this->post->ID );
}
}

return $metadata;
}

/**
* Get schema defaults.
*
* @param string $post_type The post type.
* @return array<string> The schema defaults.
*/
private function get_schema_defaults( $post_type ) {
if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) && WPSEO_Options::get( 'disableadvanced_meta' ) ) {
return [];
}
$schema_defaults = [];

$schema_defaults['pageType'] = WPSEO_Options::get( 'schema-page-type-' . $post_type );

if ( YoastSEO()->helpers->schema->article->is_article_post_type( $post_type ) ) {
$default_schema_article_type = WPSEO_Options::get( 'schema-article-type-' . $post_type );

/** This filter is documented in inc/options/class-wpseo-option-titles.php */
$allowed_article_types = apply_filters( 'wpseo_schema_article_types', Schema_Types::ARTICLE_TYPES );

if ( ! array_key_exists( $default_schema_article_type, $allowed_article_types ) ) {
$default_schema_article_type = WPSEO_Options::get_default( 'wpseo_titles', 'schema-article-type-' . $post_type );
}

$schema_defaults['articleType'] = $default_schema_article_type;
}
return $schema_defaults;
}
}
35 changes: 35 additions & 0 deletions admin/formatter/class-term-metabox-formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @package WPSEO\Admin\Formatter
*/
use Yoast\WP\SEO\Editors\Framework\Metadata_Groups;

/**
* This class provides data for the term metabox by return its values for localization.
Expand Down Expand Up @@ -86,12 +87,46 @@ public function get_values() {
'social_image_template' => $this->get_social_image_template(),
'wincherIntegrationActive' => 0,
'isInsightsEnabled' => $this->is_insights_enabled(),
'metadata' => $this->get_term_metadata(),
'entity' => [
'id' => $this->term->term_id,
'slug' => $this->term->slug,
],
];
}

return $values;
}

/**
* Returns the metadata for the term.
*
* @return array<string>
*/
private function get_term_metadata() {
$metadata = [];
$fields_presenter = new WPSEO_Taxonomy_Fields_Presenter( $this->term );
$field_definitions = new WPSEO_Taxonomy_Fields();
$meta_prefix = 'wpseo_';

/**
* The metadata groups.
*
* @var Metadata_Groups $metadata_groups The metadata groups.
*/
$metadata_groups = YoastSEO()->classes->get( Metadata_Groups::class );

$groups = $metadata_groups->get_term_metadata_groups();

foreach ( $groups as $group ) {
foreach ( $field_definitions->get( $group ) as $key => $field ) {
$metadata[ $key ] = $fields_presenter->get_field_value( $meta_prefix . $key );
}
}

return $metadata;
}

/**
* Gets the image URL for the term's social preview.
*
Expand Down
27 changes: 10 additions & 17 deletions admin/metabox/class-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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\Editors\Framework\Metadata_Groups;
use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository;
use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter;
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
Expand Down Expand Up @@ -208,12 +209,6 @@ public static function translate_meta_boxes() {
'<a href="https://googlewebmastercentral.blogspot.com/2009/12/handling-legitimate-cross-domain.html" target="_blank" rel="noopener">',
WPSEO_Admin_Utils::get_new_tab_message() . '</a>'
);
/* translators: %s expands to the post type name. */
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['title'] = __( 'Timestamp this %s', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['description'] = __( 'Use WordProof to timestamp this page to comply with legal regulations and join the fight for a more transparant and accountable internet.', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['options']['0'] = __( 'Off', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['options']['1'] = __( 'On', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['wordproof_timestamp']['type'] = 'hidden';

WPSEO_Meta::$meta_fields['advanced']['redirect']['title'] = __( '301 Redirect', 'wordpress-seo' );
WPSEO_Meta::$meta_fields['advanced']['redirect']['description'] = __( 'The URL that this page should redirect to.', 'wordpress-seo' );
Expand Down Expand Up @@ -357,20 +352,18 @@ public function meta_box() {
protected function render_hidden_fields() {
wp_nonce_field( 'yoast_free_metabox', 'yoast_free_metabox_nonce' );

// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'general' );

if ( $this->is_advanced_metadata_enabled ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'advanced' );
}
/**
* The metadata groups.
*
* @var Metadata_Groups $metadata_groups The metadata groups.
*/
$metadata_groups = YoastSEO()->classes->get( Metadata_Groups::class );

// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'schema', $this->get_metabox_post()->post_type );
$groups = $metadata_groups->get_post_metadata_groups();

if ( $this->social_is_enabled ) {
foreach ( $groups as $group ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
echo new Meta_Fields_Presenter( $this->get_metabox_post(), 'social' );
echo new Meta_Fields_Presenter( $this->get_metabox_post(), $group, $this->get_metabox_post()->post_type );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion admin/taxonomy/class-taxonomy-fields-presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private function get_field( $field_type, $field_name, $field_value, array $optio
*
* @return string
*/
private function get_field_value( $field_name ) {
public function get_field_value( $field_name ) {
if ( isset( $this->tax_meta[ $field_name ] ) && $this->tax_meta[ $field_name ] !== '' ) {
return $this->tax_meta[ $field_name ];
}
Expand Down
19 changes: 13 additions & 6 deletions admin/taxonomy/class-taxonomy-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package WPSEO\Admin
*/

use Yoast\WP\SEO\Editors\Framework\Metadata_Groups;

/**
* This class generates the metabox on the edit term page.
*/
Expand Down Expand Up @@ -96,13 +98,18 @@ protected function render_hidden_fields() {
$fields_presenter = new WPSEO_Taxonomy_Fields_Presenter( $this->term );
$field_definitions = new WPSEO_Taxonomy_Fields();

echo $fields_presenter->html( $field_definitions->get( 'content' ) );
if ( WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) || WPSEO_Options::get( 'disableadvanced_meta' ) === false ) {
echo $fields_presenter->html( $field_definitions->get( 'settings' ) );
}
/**
* The metadata groups.
*
* @var Metadata_Groups $metadata_groups The metadata groups.
*/
$metadata_groups = YoastSEO()->classes->get( Metadata_Groups::class );

if ( $this->is_social_enabled ) {
echo $fields_presenter->html( $field_definitions->get( 'social' ) );
$groups = $metadata_groups->get_term_metadata_groups();

foreach ( $groups as $group ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in class.
echo $fields_presenter->html( $field_definitions->get( $group ) );
}
}

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=2527",
"@putenv YOASTCS_THRESHOLD_ERRORS=2518",
"@putenv YOASTCS_THRESHOLD_WARNINGS=253",
"Yoast\\WP\\SEO\\Composer\\Actions::check_cs_thresholds"
],
Expand Down
Loading

0 comments on commit 1ed281f

Please sign in to comment.