Skip to content
Prev Previous commit
Next Next commit
Return null when the field is unavailable or protected
  • Loading branch information
SantosGuillamot committed Feb 27, 2024
commit b667e5c75965bb13102c1c39eae18e5a08c73fb7
8 changes: 4 additions & 4 deletions lib/compat/wordpress-6.5/block-bindings/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ function gutenberg_block_bindings_post_meta_callback( $source_attrs, $block_inst
}

if ( empty( $block_instance->context['postId'] ) ) {
return '';
return null;
}
$post_id = $block_instance->context['postId'];
// If a post isn't public, we need to prevent unauthorized users from accessing the post meta.
$post = get_post( $post_id );
if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) {
return '';
return null;
}

// Check if the meta field is protected.
if ( is_protected_meta( $source_attrs['key'], 'post' ) ) {
return '';
return null;
}

// Check if the meta field is registered to be shown in REST.
$meta_keys = get_registered_meta_keys( 'post', $block_instance->context['postType'] );
// Add fields registered for all subtypes.
$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( 'post', '' ) );
if ( empty( $meta_keys[ $source_attrs['key'] ]['show_in_rest'] ) || false === $meta_keys[ $source_attrs['key'] ]['show_in_rest'] ) {
Comment thread
SantosGuillamot marked this conversation as resolved.
Outdated
return '';
return null;
}

return get_post_meta( $post_id, $source_attrs['key'], true );
Expand Down