diff --git a/src/wp-includes/block-bindings/term-data.php b/src/wp-includes/block-bindings/term-data.php new file mode 100644 index 0000000000000..94f1002d72e30 --- /dev/null +++ b/src/wp-includes/block-bindings/term-data.php @@ -0,0 +1,96 @@ + "name" ). + * @param WP_Block $block_instance The block instance. + * @return mixed The value computed for the source. + */ +function gutenberg_block_bindings_term_data_get_value( array $source_args, $block_instance ) { + if ( empty( $source_args['key'] ) ) { + return null; + } + + if ( empty( $block_instance->context['termId'] ) || empty( $block_instance->context['taxonomy'] ) ) { + return null; + } + + $term_id = $block_instance->context['termId']; + $taxonomy = $block_instance->context['taxonomy']; + + // Get the term data. + $term = get_term( $term_id, $taxonomy ); + if ( is_wp_error( $term ) || ! $term ) { + return null; + } + + // Check if taxonomy exists and is publicly queryable. + $taxonomy_object = get_taxonomy( $taxonomy ); + if ( ! $taxonomy_object || ! $taxonomy_object->publicly_queryable ) { + if ( ! current_user_can( 'read' ) ) { + return null; + } + } + + switch ( $source_args['key'] ) { + case 'id': + return esc_html( (string) $term_id ); + + case 'name': + return esc_html( $term->name ); + + case 'link': + return esc_url( get_term_link( $term ) ); + + case 'slug': + return esc_html( $term->slug ); + + case 'description': + return wp_kses_post( $term->description ); + + case 'parent': + return esc_html( (string) $term->parent ); + + case 'count': + return esc_html( (string) '(' . $term->count . ')' ); + + default: + return null; + } +} + +/** + * Registers Term Data source in the block bindings registry. + * + * @since 6.9.0 + * @access private + */ +function gutenberg_register_block_bindings_term_data_source() { + if ( get_block_bindings_source( 'core/term-data' ) ) { + // The source is already registered. + return; + } + + register_block_bindings_source( + 'core/term-data', + array( + 'label' => _x( 'Term Data', 'block bindings source' ), + 'get_value_callback' => 'gutenberg_block_bindings_term_data_get_value', + 'uses_context' => array( 'termId', 'taxonomy' ), + ) + ); +} + +add_action( 'init', 'gutenberg_register_block_bindings_term_data_source' ); diff --git a/src/wp-settings.php b/src/wp-settings.php index 3a36f88dbb853..8537706eb69c2 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -367,6 +367,7 @@ require ABSPATH . WPINC . '/block-bindings/pattern-overrides.php'; require ABSPATH . WPINC . '/block-bindings/post-data.php'; require ABSPATH . WPINC . '/block-bindings/post-meta.php'; +require ABSPATH . WPINC . '/block-bindings/term-data.php'; require ABSPATH . WPINC . '/blocks.php'; require ABSPATH . WPINC . '/blocks/index.php'; require ABSPATH . WPINC . '/block-editor.php';