Skip to content

Commit

Permalink
fix registering primary term meta
Browse files Browse the repository at this point in the history
  • Loading branch information
vraja-pro committed May 14, 2024
1 parent b6d96b4 commit 20e8eeb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
64 changes: 45 additions & 19 deletions admin/class-primary-term-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ class WPSEO_Primary_Term_Admin implements WPSEO_WordPress_Integration {
*
* @var Primary_Term_Helper
*/
protected $primary_term_helper;
private $primary_term_helper;

/**
* Registered primary taxonomies.
*
* @var array<string>
*/
private $registered_primary_taxonomies = [];

/**
* Constructor.
Expand All @@ -37,7 +44,20 @@ public function register_hooks() {
add_action( 'admin_footer', [ $this, 'wp_footer' ], 10 );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
add_filter( 'wpseo_metabox_entries_general', [ $this, 'add_input_fields' ], 10, 2 );
add_action( 'registered_taxonomy', [ $this, 'register_primary_term_meta' ], 10, 3 );
add_action( 'registered_taxonomy', [ $this, 'register_meta_for_registered_taxonomy' ], 10, 3 );
add_action( 'rest_api_init', [ $this, 'register_meta_for_filtered_taxonomies' ] );
}

/**
* Register primary term meta for taxonomies that are added through the 'wpseo_primary_term_taxonomies' filter.
*
* @return void
*/
public function register_meta_for_filtered_taxonomies() {
$taxonomies = $this->get_primary_term_taxonomies();
foreach ( $taxonomies as $taxonomy ) {
$this->register_primary_term_meta( $taxonomy->name );
}
}

/**
Expand All @@ -49,13 +69,11 @@ public function register_hooks() {
*
* @return void
*/
public function register_primary_term_meta( $taxonomy, $object_type, $args ) {
public function register_meta_for_registered_taxonomy( $taxonomy, $object_type, $args ) {
if ( ! $args['hierarchical'] ) {
return;
}

WPSEO_Meta::register_meta( 'primary_' . $taxonomy, 'hidden', '' );
add_filter( 'wpseo_sanitize_post_meta_primary_' . $taxonomy, [ $this, 'sanitize_primary_term' ], 10, 4 );
$this->register_primary_term_meta( $taxonomy );
}

/**
Expand Down Expand Up @@ -105,11 +123,14 @@ protected function get_current_id() {
public function add_input_fields( $field_defs ) {
$taxonomies = $this->get_primary_term_taxonomies();
foreach ( $taxonomies as $taxonomy ) {
$field_defs[ 'primary_' . $taxonomy->name ] = [
'type' => 'hidden',
'default_value' => '',
];
if ( in_array( $taxonomy->name, $this->registered_primary_taxonomies ) ) {

Check warning on line 126 in admin/class-primary-term-admin.php

View workflow job for this annotation

GitHub Actions / Check code style

Not using strict comparison for in_array; supply true for $strict argument.

Check warning on line 126 in admin/class-primary-term-admin.php

View workflow job for this annotation

GitHub Actions / Check code style

Not using strict comparison for in_array; supply true for $strict argument.
$field_defs[ 'primary_' . $taxonomy->name ] = [
'type' => 'hidden',
'default_value' => '',
];
}
}

return $field_defs;
}

Expand Down Expand Up @@ -255,14 +276,19 @@ private function map_taxonomies_for_js( $taxonomy ) {
];
}

/**
* Returns whether or not a taxonomy is hierarchical.
*
* @param stdClass $taxonomy Taxonomy object.
*
* @return bool
*/
private function filter_hierarchical_taxonomies( $taxonomy ) {
return (bool) $taxonomy->hierarchical;
/**
* Register primary term meta.
*
* @param string $taxonomy The taxonomy name.
*
* @return void
*/
private function register_primary_term_meta( $taxonomy ) {
if ( in_array( $taxonomy, $this->registered_primary_taxonomies ) ) {

Check warning on line 287 in admin/class-primary-term-admin.php

View workflow job for this annotation

GitHub Actions / Check code style

Not using strict comparison for in_array; supply true for $strict argument.

Check warning on line 287 in admin/class-primary-term-admin.php

View workflow job for this annotation

GitHub Actions / Check code style

Not using strict comparison for in_array; supply true for $strict argument.
return;
}
WPSEO_Meta::register_meta( 'primary_' . $taxonomy, 'hidden', '' );
add_filter( 'wpseo_sanitize_post_meta_primary_' . $taxonomy, [ $this, 'sanitize_primary_term' ], 10, 4 );
$this->registered_primary_taxonomies[] = $taxonomy;
}
}
4 changes: 2 additions & 2 deletions inc/class-wpseo-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public static function init() {

foreach ( self::$meta_fields as $subset => $field_group ) {
foreach ( $field_group as $key => $field_def ) {
self::register_meta( $key, $field_def['type'], $field_def['default_value'] );
self::register_meta( $key, $field_def['type'], ( $field_def['default_value'] ?? '' ) );

// Set the $fields_index property for efficiency.
self::$fields_index[ self::$meta_prefix . $key ] = [
Expand Down Expand Up @@ -355,7 +355,7 @@ public static function register_meta( $key, $field_type, $default_value ) {
'auth_callback' => [ self::class, 'auth_callback' ],
'type' => 'string',
'single' => true,
'default' => ( $default_value ?? '' ),
'default' => $default_value,
]
);
}
Expand Down

0 comments on commit 20e8eeb

Please sign in to comment.