-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock_Binding_Command.php
More file actions
189 lines (173 loc) · 4.05 KB
/
Block_Binding_Command.php
File metadata and controls
189 lines (173 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace WP_CLI\Block;
use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI_Command;
/**
* Retrieves details on registered block binding sources.
*
* Get information on block binding sources from the WP_Block_Bindings_Registry.
* Block bindings allow dynamic data to be connected to block attributes.
*
* ## EXAMPLES
*
* # List all registered binding sources
* $ wp block binding list
*
* # Get details about the post-meta binding
* $ wp block binding get core/post-meta --format=json
*
* @package wp-cli
*/
class Block_Binding_Command extends WP_CLI_Command {
/**
* Default fields to display.
*
* @var array
*/
private $fields = [
'name',
'label',
];
/**
* Lists registered block binding sources.
*
* ## OPTIONS
*
* [--field=<field>]
* : Prints the value of a single field for each source.
*
* [--fields=<fields>]
* : Limit the output to specific source fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - yaml
* - ids
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each source:
*
* * name
* * label
*
* These fields are optionally available:
*
* * uses_context
*
* ## EXAMPLES
*
* # List all binding sources
* $ wp block binding list
*
* # Get source names only
* $ wp block binding list --field=name
*
* # Export sources to JSON
* $ wp block binding list --format=json
*
* @subcommand list
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
$sources = get_all_registered_block_bindings_sources();
$formatter = $this->get_formatter( $assoc_args );
if ( 'ids' === $formatter->format ) {
$ids = array_keys( $sources );
echo implode( ' ', $ids );
return;
}
$items = [];
foreach ( $sources as $name => $source ) {
$items[] = $this->source_to_array( $name, $source );
}
$formatter->display_items( $items );
}
/**
* Gets details about a registered block binding source.
*
* ## OPTIONS
*
* <name>
* : Binding source name (e.g., 'core/post-meta').
*
* [--field=<field>]
* : Instead of returning the whole source, returns the value of a single field.
*
* [--fields=<fields>]
* : Limit the output to specific fields. Defaults to all fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* # Get details about the post-meta binding
* $ wp block binding get core/post-meta
*
* # Get as JSON
* $ wp block binding get core/post-meta --format=json
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
$source = get_block_bindings_source( $args[0] );
if ( ! $source ) {
WP_CLI::error( "Block binding source '{$args[0]}' is not registered." );
}
if ( empty( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = array_merge(
$this->fields,
[
'uses_context',
]
);
}
$formatter = $this->get_formatter( $assoc_args );
$data = $this->source_to_array( $args[0], $source );
$formatter->display_item( $data );
}
/**
* Converts a binding source to a standardized associative array.
*
* @param string $name Source name.
* @param WP_Block_Bindings_Source $source Source object.
* @return array
*/
private function source_to_array( $name, $source ) {
return [
'name' => $name,
'label' => $source->label,
'uses_context' => $source->uses_context,
];
}
/**
* Gets the formatter instance.
*
* @param array $assoc_args Associative arguments.
* @return Formatter
*/
private function get_formatter( &$assoc_args ) {
return new Formatter( $assoc_args, $this->fields, 'block-binding' );
}
}