Skip to content

Commit d4a9939

Browse files
committed
Block Bindings: Add core/term-data source; extend core/post-data.
Add a new `core/term-data` source to make information about a taxonomy term available for use in Block Bindings. Additionally, expose a post object's permalink through the `core/post-data` source. Props bernhard-reiter, get_dave, jeryj, cbravobernal. Fixes #64107. git-svn-id: https://develop.svn.wordpress.org/trunk@60946 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6507e47 commit d4a9939

File tree

5 files changed

+148
-2
lines changed

5 files changed

+148
-2
lines changed

src/wp-includes/block-bindings/post-data.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,29 @@ function _block_bindings_post_data_get_value( array $source_args, $block_instanc
2323
return null;
2424
}
2525

26-
if ( empty( $block_instance->context['postId'] ) ) {
26+
/*
27+
* BACKWARDS COMPATIBILITY: Hardcoded exception for navigation blocks.
28+
* Required for WordPress 6.9+ navigation blocks. DO NOT REMOVE.
29+
*/
30+
$block_name = $block_instance->name ?? '';
31+
$is_navigation_block = in_array(
32+
$block_name,
33+
array( 'core/navigation-link', 'core/navigation-submenu' ),
34+
true
35+
);
36+
37+
if ( $is_navigation_block ) {
38+
// Navigation blocks: read from block attributes.
39+
$post_id = $block_instance->attributes['id'] ?? null;
40+
} else {
41+
// All other blocks: use context.
42+
$post_id = $block_instance->context['postId'] ?? null;
43+
}
44+
45+
// If we don't have an entity ID, bail early.
46+
if ( empty( $post_id ) ) {
2747
return null;
2848
}
29-
$post_id = $block_instance->context['postId'];
3049

3150
// If a post isn't public, we need to prevent unauthorized users from accessing the post data.
3251
$post = get_post( $post_id );
@@ -46,6 +65,11 @@ function _block_bindings_post_data_get_value( array $source_args, $block_instanc
4665
return '';
4766
}
4867
}
68+
69+
if ( 'link' === $source_args['key'] ) {
70+
$permalink = get_permalink( $post_id );
71+
return false === $permalink ? null : esc_url( $permalink );
72+
}
4973
}
5074

5175
/**
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Term Data source for Block Bindings.
4+
*
5+
* @since 6.9.0
6+
* @package WordPress
7+
* @subpackage Block Bindings
8+
*/
9+
10+
/**
11+
* Gets value for Term Data source.
12+
*
13+
* @since 6.9.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" => "name" ).
18+
* @param WP_Block $block_instance The block instance.
19+
* @return mixed The value computed for the source.
20+
*/
21+
function _block_bindings_term_data_get_value( array $source_args, $block_instance ) {
22+
if ( empty( $source_args['key'] ) ) {
23+
return null;
24+
}
25+
26+
/*
27+
* BACKWARDS COMPATIBILITY: Hardcoded exception for navigation blocks.
28+
* Required for WordPress 6.9+ navigation blocks. DO NOT REMOVE.
29+
*/
30+
$block_name = $block_instance->name ?? '';
31+
$is_navigation_block = in_array(
32+
$block_name,
33+
array( 'core/navigation-link', 'core/navigation-submenu' ),
34+
true
35+
);
36+
37+
if ( $is_navigation_block ) {
38+
// Navigation blocks: read from block attributes.
39+
$term_id = $block_instance->attributes['id'] ?? null;
40+
$type = $block_instance->attributes['type'] ?? '';
41+
// Map UI shorthand to taxonomy slug when using attributes.
42+
$taxonomy = ( 'tag' === $type ) ? 'post_tag' : $type;
43+
} else {
44+
// All other blocks: use context
45+
$term_id = $block_instance->context['termId'] ?? null;
46+
$taxonomy = $block_instance->context['taxonomy'] ?? '';
47+
}
48+
49+
// If we don't have required identifiers, bail early.
50+
if ( empty( $term_id ) || empty( $taxonomy ) ) {
51+
return null;
52+
}
53+
54+
// Get the term data.
55+
$term = get_term( $term_id, $taxonomy );
56+
if ( is_wp_error( $term ) || ! $term ) {
57+
return null;
58+
}
59+
60+
// Check if taxonomy exists and is publicly queryable.
61+
$taxonomy_object = get_taxonomy( $taxonomy );
62+
if ( ! $taxonomy_object || ! $taxonomy_object->publicly_queryable ) {
63+
if ( ! current_user_can( 'read' ) ) {
64+
return null;
65+
}
66+
}
67+
68+
switch ( $source_args['key'] ) {
69+
case 'id':
70+
return esc_html( (string) $term_id );
71+
72+
case 'name':
73+
return esc_html( $term->name );
74+
75+
case 'link':
76+
// Only taxonomy entities are supported by Term Data.
77+
$term_link = get_term_link( $term );
78+
return is_wp_error( $term_link ) ? null : esc_url( $term_link );
79+
80+
case 'slug':
81+
return esc_html( $term->slug );
82+
83+
case 'description':
84+
return wp_kses_post( $term->description );
85+
86+
case 'parent':
87+
return esc_html( (string) $term->parent );
88+
89+
case 'count':
90+
return esc_html( (string) $term->count );
91+
92+
default:
93+
return null;
94+
}
95+
}
96+
97+
/**
98+
* Registers Term Data source in the block bindings registry.
99+
*
100+
* @since 6.9.0
101+
* @access private
102+
*/
103+
function _register_block_bindings_term_data_source() {
104+
if ( get_block_bindings_source( 'core/term-data' ) ) {
105+
// The source is already registered.
106+
return;
107+
}
108+
109+
register_block_bindings_source(
110+
'core/term-data',
111+
array(
112+
'label' => _x( 'Term Data', 'block bindings source' ),
113+
'get_value_callback' => '_block_bindings_term_data_get_value',
114+
'uses_context' => array( 'termId', 'taxonomy' ),
115+
)
116+
);
117+
}
118+
119+
add_action( 'init', '_register_block_bindings_term_data_source' );

src/wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@
369369
require ABSPATH . WPINC . '/block-bindings/pattern-overrides.php';
370370
require ABSPATH . WPINC . '/block-bindings/post-data.php';
371371
require ABSPATH . WPINC . '/block-bindings/post-meta.php';
372+
require ABSPATH . WPINC . '/block-bindings/term-data.php';
372373
require ABSPATH . WPINC . '/blocks.php';
373374
require ABSPATH . WPINC . '/blocks/index.php';
374375
require ABSPATH . WPINC . '/block-editor.php';

tests/phpunit/includes/functions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ function _unhook_block_registration() {
350350
remove_action( 'init', '_register_block_bindings_pattern_overrides_source' );
351351
remove_action( 'init', '_register_block_bindings_post_data_source' );
352352
remove_action( 'init', '_register_block_bindings_post_meta_source' );
353+
remove_action( 'init', '_register_block_bindings_term_data_source' );
353354
}
354355
tests_add_filter( 'init', '_unhook_block_registration', 1000 );
355356

tests/phpunit/tests/block-bindings/register.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function test_get_all_registered() {
7575
'core/post-data' => get_block_bindings_source( 'core/post-data' ),
7676
'core/post-meta' => get_block_bindings_source( 'core/post-meta' ),
7777
'core/pattern-overrides' => get_block_bindings_source( 'core/pattern-overrides' ),
78+
'core/term-data' => get_block_bindings_source( 'core/term-data' ),
7879
);
7980

8081
$registered = get_all_registered_block_bindings_sources();

0 commit comments

Comments
 (0)