← Back
Editing: post-meta-rest-fields.php
<?php namespace Yoast\WP\SEO\Initializers; use WP_Post; use WP_REST_Response; use WPSEO_Meta; use WPSEO_Primary_Term; use Yoast\WP\SEO\Conditionals\No_Conditionals; use Yoast\WP\SEO\Helpers\Capability_Helper; use Yoast\WP\SEO\Helpers\Options_Helper; use Yoast\WP\SEO\Helpers\Post_Type_Helper; use Yoast\WP\SEO\Helpers\Primary_Term_Helper; use Yoast\WP\SEO\Helpers\Taxonomy_Helper; /** * Registers Yoast post meta fields with the REST API per post type. * * WPSEO_Meta::init() registers meta globally (no object_subtype) at plugins_loaded, * before custom post types are available. A global registration without object_subtype * causes WordPress to omit fields with empty values from CPT REST responses, so the * block editor never sees them in save payloads. * * Hooked to wp_loaded so all CPTs and taxonomies registered at any init priority are * present before registration runs. */ class Post_Meta_Rest_Fields implements Initializer_Interface { use No_Conditionals; /** * The primary term helper. * * @var Primary_Term_Helper */ private $primary_term_helper; /** * The taxonomy helper. * * @var Taxonomy_Helper */ private $taxonomy_helper; /** * The options helper. * * @var Options_Helper */ private $options_helper; /** * The capability helper. * * @var Capability_Helper */ private $capability_helper; /** * The post type helper. * * @var Post_Type_Helper */ private $post_type_helper; /** * Constructor. * * @param Primary_Term_Helper $primary_term_helper The primary term helper. * @param Taxonomy_Helper $taxonomy_helper The taxonomy helper. * @param Options_Helper $options_helper The options helper. * @param Capability_Helper $capability_helper The capability helper. * @param Post_Type_Helper $post_type_helper The post type helper. */ public function __construct( Primary_Term_Helper $primary_term_helper, Taxonomy_Helper $taxonomy_helper, Options_Helper $options_helper, Capability_Helper $capability_helper, Post_Type_Helper $post_type_helper ) { $this->primary_term_helper = $primary_term_helper; $this->taxonomy_helper = $taxonomy_helper; $this->options_helper = $options_helper; $this->capability_helper = $capability_helper; $this->post_type_helper = $post_type_helper; } /** * Initializes the post meta REST field registrations. * * @return void */ public function initialize() { \add_action( 'wp_loaded', [ $this, 'register_post_meta' ] ); \add_action( 'rest_api_init', [ $this, 'register_rest_hooks' ] ); } /** * Registers the hooks that only apply when the REST API is in use. * * @return void */ public function register_rest_hooks() { \add_filter( 'get_post_metadata', [ $this, 'normalize_primary_term_meta' ], \PHP_INT_MAX, 3 ); } /** * Normalizes the primary term meta that another plugin returned through the get_post_metadata filter. * * The REST API handles the primary term meta as a string. Scalar values are cast to strings. * A value without scalar values counts as not stored, so that WordPress reads the meta itself. * * @param array<string|int|float|bool|null>|string|int|float|bool|null $value The value from the earlier filters. * @param int $object_id The post ID. * @param string $meta_key The meta key. * * @return array<string|int|float|bool|null>|string|int|float|bool|null The normalized value. */ public function normalize_primary_term_meta( $value, $object_id, $meta_key ) { if ( $value === null || ! \is_string( $meta_key ) || \strpos( $meta_key, WPSEO_Meta::$meta_prefix . 'primary_' ) !== 0 ) { return $value; } if ( \is_scalar( $value ) ) { return (string) $value; } if ( ! \is_array( $value ) ) { return null; } $stored_values = []; foreach ( $value as $stored_value ) { if ( \is_scalar( $stored_value ) ) { $stored_values[] = (string) $stored_value; } } return ( $stored_values === [] ) ? null : $stored_values; } /** * Registers all Yoast meta fields per accessible, REST-enabled post type and * adds the unauthorized-read filter for each. * * Also populates WPSEO_Meta::$fields_index and WPSEO_Meta::$defaults, which were * previously built inside the registration loop in WPSEO_Meta::init(). * * @return void */ public function register_post_meta() { foreach ( WPSEO_Meta::$meta_fields as $subset => $field_group ) { foreach ( $field_group as $key => $field_def ) { $full_key = WPSEO_Meta::$meta_prefix . $key; WPSEO_Meta::$fields_index[ $full_key ] = [ 'subset' => $subset, 'key' => $key, ]; WPSEO_Meta::$defaults[ $full_key ] = ( $field_def['default_value'] ?? '' ); } } $metabox_disabled_in_block_editor = \apply_filters( 'wpseo_disable_metabox_in_block_editor', false ); $post_types = \array_intersect( \get_post_types( [ 'show_in_rest' => true ], 'names' ), $this->post_type_helper->get_accessible_post_types(), ); foreach ( $post_types as $post_type ) { foreach ( WPSEO_Meta::$meta_fields as $subset => $field_group ) { // primary_term is registered per-post-type by register_primary_term_meta() below. // Skipping it here prevents fields added for one post type from bleeding into // the registration of every subsequent post type. if ( $subset === 'primary_term' ) { continue; } $requires_advanced_cap = \in_array( $subset, [ 'advanced', 'schema' ], true ); foreach ( $field_group as $key => $field_def ) { $this->register_meta( $post_type, $key, $field_def, $requires_advanced_cap ); } } $this->register_primary_term_meta( $post_type ); // Without custom-fields support, WordPress omits registered meta from the REST schema for CPTs // (WP_REST_Posts_Controller::get_item_schema), so the block editor cannot read or write Yoast // fields via REST. Only needed when our metabox is disabled in the block editor; otherwise the // sidebar handles saving and classic-editor post types are unaffected. if ( $metabox_disabled_in_block_editor && \use_block_editor_for_post_type( $post_type ) && ! \post_type_supports( $post_type, 'custom-fields' ) ) { \add_post_type_support( $post_type, 'custom-fields' ); } // Add a filter to strip REST-exposed Yoast meta fields from the response for users without edit_post capability. \add_filter( 'rest_prepare_' . $post_type, [ $this, 'hide_meta_from_unauthorized_rest_response' ], 10, 2 ); \add_filter( 'rest_prepare_' . $post_type, [ $this, 'add_meta_defaults_to_rest_response' ] ); // Add a filter to trigger wpseo_saved_postdata after a post is updated via REST API, same as in WPSEO_Metabox::save_postdata. // The $creating guard ensures it only fires on updates (not on new post creation via REST), matching the // classic-editor save_postdata path which is only reachable when a post already exists with an ID in $_POST. if ( $metabox_disabled_in_block_editor ) { \add_action( 'rest_after_insert_' . $post_type, function ( $post, $request, $creating ) { $this->ensure_primary_terms( $post->ID ); if ( ! $creating ) { \do_action( 'wpseo_saved_postdata' ); } }, 10, 3, ); } } } /** * Ensures a primary term is stored for every hierarchical taxonomy on the post. * * Called after a REST save. When no primary is set (or the stored primary is no longer * among the post's terms), picks the first assigned term so the client never needs to * write it back — matching the classic-editor metabox path in trunk. * * @param int $post_id The post ID. * * @return void */ private function ensure_primary_terms( int $post_id ): void { foreach ( $this->primary_term_helper->get_primary_term_taxonomies( $post_id ) as $taxonomy ) { $primary = new WPSEO_Primary_Term( $taxonomy->name, $post_id ); if ( $primary->get_primary_term() !== false ) { continue; } $terms = \wp_get_object_terms( $post_id, $taxonomy->name, [ 'fields' => 'ids' ] ); if ( ! \is_wp_error( $terms ) && $terms !== [] ) { $primary->set_primary_term( (string) $terms[0] ); } } } /** * Registers primary term meta for all non-excluded hierarchical taxonomies on a post type. * * @param string $post_type The post type slug. * * @return void */ private function register_primary_term_meta( string $post_type ) { foreach ( \get_object_taxonomies( $post_type, 'objects' ) as $taxonomy ) { if ( ! $taxonomy->hierarchical || $this->taxonomy_helper->is_excluded( $taxonomy->name ) ) { continue; } $primary_key = 'primary_' . $taxonomy->name; $full_key = WPSEO_Meta::$meta_prefix . $primary_key; if ( ! isset( WPSEO_Meta::$fields_index[ $full_key ] ) ) { WPSEO_Meta::$meta_fields['primary_term'][ $primary_key ] = [ 'type' => 'hidden' ]; WPSEO_Meta::$fields_index[ $full_key ] = [ 'subset' => 'primary_term', 'key' => $primary_key, ]; WPSEO_Meta::$defaults[ $full_key ] = '-1'; } $this->register_meta( $post_type, $primary_key, [ 'type' => 'hidden', ], ); } } /** * Registers a single Yoast meta field for a specific post type. * * Fields with `type: null` are internal/serialized fields not suitable for REST API * access and will be registered with `show_in_rest: false`. * * @param string $post_type The post type slug. * @param string $key The internal key of the meta field (without prefix). * @param array<string,array<string,string>> $field_def The field definition array. * @param bool $requires_advanced_cap Whether the field is in the advanced or schema subset * and therefore also requires wpseo_edit_advanced_metadata. * * @return void */ private function register_meta( string $post_type, string $key, array $field_def = [], bool $requires_advanced_cap = false ) { $show_in_rest = ! \array_key_exists( 'type', $field_def ) || $field_def['type'] !== null; $args = [ 'show_in_rest' => $show_in_rest, 'single' => true, 'type' => 'string', 'sanitize_callback' => [ WPSEO_Meta::class, 'sanitize_post_meta' ], ]; if ( $requires_advanced_cap ) { $args['auth_callback'] = [ $this, 'auth_callback_for_advanced_meta' ]; } else { $args['auth_callback'] = static function ( $allowed, $meta_key, $object_id ) { return \current_user_can( 'edit_post', $object_id ); }; } \register_post_meta( $post_type, WPSEO_Meta::$meta_prefix . $key, $args ); } /** * Auth callback for advanced and schema meta fields. * * Mirrors the gate in WPSEO_Meta::get_tab_field_defs(): when disableadvanced_meta is on, * only users with wpseo_edit_advanced_metadata (or wpseo_manage_options) may write these fields. * * @param bool $allowed Whether the user is allowed (unused; re-evaluated from scratch). * @param string $meta_key The meta key being written. * @param int $object_id The post ID. * * @return bool */ public function auth_callback_for_advanced_meta( $allowed, $meta_key, $object_id ) { if ( ! \current_user_can( 'edit_post', $object_id ) ) { return false; } return ! $this->options_helper->get( 'disableadvanced_meta' ) || $this->capability_helper->current_user_can( 'wpseo_edit_advanced_metadata' ); } /** * Strips restricted Yoast meta fields from the REST response based on the current user's capabilities. * * The register_meta's auth_callback only covers writes; read access must be restricted separately so that * fields an author may not write never reach their editor and are never sent back in a save payload. * * @param WP_REST_Response $response The REST response. * @param WP_Post $post The post object. * * @return WP_REST_Response The (possibly modified) response. */ public function hide_meta_from_unauthorized_rest_response( $response, $post ) { $can_edit_post = \current_user_can( 'edit_post', $post->ID ); $can_edit_advanced_meta = $this->auth_callback_for_advanced_meta( false, '', $post->ID ); if ( $can_edit_post && $can_edit_advanced_meta ) { return $response; } $data = $response->get_data(); foreach ( WPSEO_Meta::$meta_fields as $subset => $field_group ) { // Users who can edit the post but lack the advanced meta permission only need the advanced/schema subsets stripped. if ( $can_edit_post && ! \in_array( $subset, [ 'advanced', 'schema' ], true ) ) { continue; } foreach ( $field_group as $key => $field_def ) { // Mirror the show_in_rest logic from register_meta(): only expose fields whose type is not null. if ( ! \array_key_exists( 'type', $field_def ) || $field_def['type'] !== null ) { unset( $data['meta'][ WPSEO_Meta::$meta_prefix . $key ] ); } } } $response->set_data( $data ); return $response; } /** * Fills unset Yoast meta fields in the REST response with their default values. * * Defaults are not passed to register_post_meta(), because WordPress would then return them from * get_post_meta() for unset keys site-wide. WPSEO_Meta deletes values equal to their default on save, * so without this the editor would read "" where it writes the default, and the post would stay dirty. * * @param WP_REST_Response $response The REST response. * * @return WP_REST_Response The (possibly modified) response. */ public function add_meta_defaults_to_rest_response( $response ) { $data = $response->get_data(); if ( empty( $data['meta'] ) || ! \is_array( $data['meta'] ) ) { return $response; } foreach ( WPSEO_Meta::$meta_fields as $field_group ) { foreach ( $field_group as $key => $field_def ) { $full_key = WPSEO_Meta::$meta_prefix . $key; if ( isset( $field_def['default_value'] ) && $field_def['default_value'] !== '' && ( $data['meta'][ $full_key ] ?? null ) === '' ) { $data['meta'][ $full_key ] = $field_def['default_value']; } } } $response->set_data( $data ); return $response; } }
Save File
Cancel