Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions lib/compat/wordpress-6.9/entity-block-bindings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Entity source for the block bindings (explicit entity args).
*
* @since 6.9.0
* @package gutenberg
* @subpackage Block Bindings
*/

/**
* Gets value for Entity source.
*
* This source is intentionally **not** tied to the current post context.
* Consumers must pass entity identifiers via `$source_args`.
*
* Supported args:
* - key: currently only supports `url`
* - kind: `post-type` | `taxonomy`
* - type: post type slug or taxonomy slug
* - id: entity ID
*
* @since 6.9.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you've been waiting for a while for reviews, thanks for your patience!

I don't know if this will make the next WordPress release, but the next version is now 7.0.0

* @access private
*
* @param array $source_args Array containing source arguments used to look up the value.
* @param WP_Block $block_instance The block instance.
* @return mixed The value computed for the source.
*/
function gutenberg_block_bindings_entity_get_value( array $source_args, $block_instance ) {

Check warning on line 29 in lib/compat/wordpress-6.9/entity-block-bindings.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Unused function parameter $block_instance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to get test coverage here, e.g.,

  • Valid post-type and taxonomy URL resolution
  • Non-public post denial
  • Password-protected post denial
  • Invalid/missing args
  • Deleted entity handling (?)

if ( empty( $source_args['key'] ) || 'url' !== $source_args['key'] ) {
return null;
}
if ( empty( $source_args['kind'] ) || empty( $source_args['type'] ) || empty( $source_args['id'] ) ) {
return null;
}

$kind = (string) $source_args['kind'];
$type = (string) $source_args['type'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check if the post type exists? E.g.,

if ( ! post_type_exists( $type ) ) {
    return null;
}

$id = (int) $source_args['id'];

if ( $id <= 0 ) {
return null;
}

if ( 'post-type' === $kind ) {
$post = get_post( $id );
if ( ! $post ) {
return null;
}
// Prevent unauthorized users from accessing non-public post data.
if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $id ) ) || post_password_required( $post ) ) {
return null;
}
$permalink = get_permalink( $id );
return false === $permalink ? null : esc_url( $permalink );
}

if ( 'taxonomy' === $kind ) {
// Map UI shorthand to taxonomy slug when using args.
$taxonomy = ( 'tag' === $type ) ? 'post_tag' : $type;

$term = get_term( $id, $taxonomy );
if ( is_wp_error( $term ) || ! $term ) {
return null;
}

$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $taxonomy_object || ! $taxonomy_object->publicly_queryable ) {
if ( ! current_user_can( 'read' ) ) {
return null;
}
}

$term_link = get_term_link( $term );
return is_wp_error( $term_link ) ? null : esc_url( $term_link );
}

return null;
}

/**
* Registers Entity source in the block bindings registry.
*
* @since 6.9.0
* @access private
*/
function gutenberg_register_block_bindings_entity_source() {
if ( get_block_bindings_source( 'core/entity' ) ) {
// The source is already registered.
return;
}

register_block_bindings_source(
'core/entity',
array(
'label' => _x( 'Entity', 'block bindings source' ),
'get_value_callback' => 'gutenberg_block_bindings_entity_get_value',
)
);
}

add_action( 'init', 'gutenberg_register_block_bindings_entity_source' );

Check failure on line 102 in lib/compat/wordpress-6.9/entity-block-bindings.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 blank line at end of file; 2 found

1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function gutenberg_is_experiment_enabled( $name ) {
// WordPress 6.9 compat.
require __DIR__ . '/compat/wordpress-6.9/class-gutenberg-rest-attachments-controller-6-9.php';
require __DIR__ . '/compat/wordpress-6.9/block-bindings.php';
require __DIR__ . '/compat/wordpress-6.9/entity-block-bindings.php';
require __DIR__ . '/compat/wordpress-6.9/post-data-block-bindings.php';
require __DIR__ . '/compat/wordpress-6.9/term-data-block-bindings.php';
require __DIR__ . '/compat/wordpress-6.9/rest-api.php';
Expand Down
Loading
Loading