PluginProbe
Gutenberg / 23.2.1
Gutenberg v23.2.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-6.9 / term-data-block-bindings.php

term-data-block-bindings.php in Gutenberg 23.2.1, at lib/compat/wordpress-6.9/term-data-block-bindings.php

120 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Term Data source for the block bindings.
4 *
5 * @since 6.9.0
6 * @package gutenberg
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( "field" => "name" ).
18 * @param WP_Block $block_instance The block instance.
19 * @return mixed The value computed for the source.
20 */
21 function gutenberg_block_bindings_term_data_get_value( array $source_args, $block_instance ) {
22 if ( empty( $source_args['field'] ) ) {
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['field'] ) {
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 gutenberg_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' => 'gutenberg_block_bindings_term_data_get_value',
114 'uses_context' => array( 'termId', 'taxonomy' ),
115 )
116 );
117 }
118
119 add_action( 'init', 'gutenberg_register_block_bindings_term_data_source' );
120