wp/wp-includes/block-bindings/post-meta.php
changeset 21 48c4eec2b7e6
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     1 <?php
       
     2 /**
       
     3  * Post Meta source for the block bindings.
       
     4  *
       
     5  * @since 6.5.0
       
     6  * @package WordPress
       
     7  * @subpackage Block Bindings
       
     8  */
       
     9 
       
    10 /**
       
    11  * Gets value for Post Meta source.
       
    12  *
       
    13  * @since 6.5.0
       
    14  * @access private
       
    15  *
       
    16  * @param array    $source_args    Array containing source arguments used to look up the override value.
       
    17  *                                 Example: array( "key" => "foo" ).
       
    18  * @param WP_Block $block_instance The block instance.
       
    19  * @return mixed The value computed for the source.
       
    20  */
       
    21 function _block_bindings_post_meta_get_value( array $source_args, $block_instance ) {
       
    22 	if ( empty( $source_args['key'] ) ) {
       
    23 		return null;
       
    24 	}
       
    25 
       
    26 	if ( empty( $block_instance->context['postId'] ) ) {
       
    27 		return null;
       
    28 	}
       
    29 	$post_id = $block_instance->context['postId'];
       
    30 
       
    31 	// If a post isn't public, we need to prevent unauthorized users from accessing the post meta.
       
    32 	$post = get_post( $post_id );
       
    33 	if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) {
       
    34 		return null;
       
    35 	}
       
    36 
       
    37 	// Check if the meta field is protected.
       
    38 	if ( is_protected_meta( $source_args['key'], 'post' ) ) {
       
    39 		return null;
       
    40 	}
       
    41 
       
    42 	// Check if the meta field is registered to be shown in REST.
       
    43 	$meta_keys = get_registered_meta_keys( 'post', $block_instance->context['postType'] );
       
    44 	// Add fields registered for all subtypes.
       
    45 	$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( 'post', '' ) );
       
    46 	if ( empty( $meta_keys[ $source_args['key'] ]['show_in_rest'] ) ) {
       
    47 		return null;
       
    48 	}
       
    49 
       
    50 	return get_post_meta( $post_id, $source_args['key'], true );
       
    51 }
       
    52 
       
    53 /**
       
    54  * Registers Post Meta source in the block bindings registry.
       
    55  *
       
    56  * @since 6.5.0
       
    57  * @access private
       
    58  */
       
    59 function _register_block_bindings_post_meta_source() {
       
    60 	register_block_bindings_source(
       
    61 		'core/post-meta',
       
    62 		array(
       
    63 			'label'              => _x( 'Post Meta', 'block bindings source' ),
       
    64 			'get_value_callback' => '_block_bindings_post_meta_get_value',
       
    65 			'uses_context'       => array( 'postId', 'postType' ),
       
    66 		)
       
    67 	);
       
    68 }
       
    69 
       
    70 add_action( 'init', '_register_block_bindings_post_meta_source' );