-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock_Template_Command.php
More file actions
396 lines (362 loc) · 9.53 KB
/
Block_Template_Command.php
File metadata and controls
396 lines (362 loc) · 9.53 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
namespace WP_CLI\Block;
use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI_Command;
/**
* Retrieves details on block templates and template parts.
*
* Get information on block templates used in Full Site Editing (FSE) themes.
*
* ## EXAMPLES
*
* # List all templates
* $ wp block template list
*
* # List template parts for the header area
* $ wp block template list --type=wp_template_part --area=header
*
* # Get a specific template
* $ wp block template get twentytwentyfour//single
*
* @package wp-cli
*/
class Block_Template_Command extends WP_CLI_Command {
/**
* Default fields to display.
*
* @var array
*/
private $fields = [
'id',
'slug',
'title',
'source',
'type',
];
/**
* Lists block templates or template parts.
*
* ## OPTIONS
*
* [--type=<type>]
* : Template type.
* ---
* default: wp_template
* options:
* - wp_template
* - wp_template_part
* ---
*
* [--slug=<slug>]
* : Filter by template slug(s). Accepts a single slug or comma-separated list.
*
* [--area=<area>]
* : For template parts, filter by area (header, footer, sidebar, uncategorized).
*
* [--post-type=<post-type>]
* : Filter templates by post type they apply to.
*
* [--source=<source>]
* : Filter by source (theme, plugin, custom).
*
* [--field=<field>]
* : Prints the value of a single field for each template.
*
* [--fields=<fields>]
* : Limit the output to specific template 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 template:
*
* * id
* * slug
* * title
* * source
* * type
*
* These fields are optionally available:
*
* * theme
* * description
* * status
* * origin
* * is_custom
* * has_theme_file
* * author
* * area (template parts only)
* * content
*
* ## EXAMPLES
*
* # List all templates
* $ wp block template list
*
* # List template parts for the header area
* $ wp block template list --type=wp_template_part --area=header
*
* # List templates from the theme
* $ wp block template list --source=theme
*
* # List specific templates by slug
* $ wp block template list --slug=single,archive
*
* # List templates for a specific post type
* $ wp block template list --post-type=page
*
* # Export templates as JSON
* $ wp block template list --format=json
*
* @subcommand list
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
$type = isset( $assoc_args['type'] ) ? $assoc_args['type'] : 'wp_template';
unset( $assoc_args['type'] );
$query = [];
// Filter by slug(s).
if ( ! empty( $assoc_args['slug'] ) ) {
$slugs = array_map( 'trim', explode( ',', $assoc_args['slug'] ) );
$query['slug__in'] = $slugs;
unset( $assoc_args['slug'] );
}
// Filter by area for template parts.
if ( ! empty( $assoc_args['area'] ) ) {
$query['area'] = $assoc_args['area'];
unset( $assoc_args['area'] );
}
// Filter by post type.
if ( ! empty( $assoc_args['post-type'] ) ) {
$query['post_type'] = $assoc_args['post-type'];
unset( $assoc_args['post-type'] );
}
$templates = get_block_templates( $query, $type );
// Filter by source.
if ( ! empty( $assoc_args['source'] ) ) {
$source = $assoc_args['source'];
$templates = array_filter(
$templates,
function ( $template ) use ( $source ) {
return $template->source === $source;
}
);
unset( $assoc_args['source'] );
}
$formatter = $this->get_formatter( $assoc_args );
if ( 'ids' === $formatter->format ) {
$ids = wp_list_pluck( $templates, 'id' );
echo implode( ' ', $ids );
return;
}
$items = array_map( [ $this, 'template_to_array' ], $templates );
$formatter->display_items( $items );
}
/**
* Gets details about a block template.
*
* ## OPTIONS
*
* <id>
* : Template ID in format 'theme//slug' (e.g., 'twentytwentyfour//single').
*
* [--type=<type>]
* : Template type.
* ---
* default: wp_template
* options:
* - wp_template
* - wp_template_part
* ---
*
* [--field=<field>]
* : Instead of returning the whole template, 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 the single post template
* $ wp block template get twentytwentyfour//single
*
* # Get template content only
* $ wp block template get twentytwentyfour//single --field=content
*
* # Get as JSON
* $ wp block template get twentytwentyfour//single --format=json
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
$type = isset( $assoc_args['type'] ) ? $assoc_args['type'] : 'wp_template';
$template = get_block_template( $args[0], $type );
if ( ! $template ) {
WP_CLI::error( "Block template '{$args[0]}' not found." );
}
if ( empty( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = array_merge(
$this->fields,
[
'theme',
'description',
'status',
'origin',
'is_custom',
'has_theme_file',
'author',
'area',
'content',
]
);
}
$formatter = $this->get_formatter( $assoc_args );
$data = $this->template_to_array( $template );
$formatter->display_item( $data );
}
/**
* Exports a block template to a file.
*
* ## OPTIONS
*
* <id>
* : Template ID to export.
*
* [--type=<type>]
* : Template type.
* ---
* default: wp_template
* options:
* - wp_template
* - wp_template_part
* ---
*
* [--file=<file>]
* : File path to export to. Parent directories will be created if needed.
*
* [--dir=<directory>]
* : Directory to export to. Defaults to current directory. Creates directory if needed.
*
* [--stdout]
* : Output to stdout instead of file.
*
* ## EXAMPLES
*
* # Export template to file
* $ wp block template export twentytwentyfour//single
*
* # Export to stdout
* $ wp block template export twentytwentyfour//single --stdout
*
* # Export to specific directory
* $ wp block template export twentytwentyfour//single --dir=./templates/
*
* # Export to specific file path
* $ wp block template export twentytwentyfour//single --file=exports/templates/single.html
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function export( $args, $assoc_args ) {
// Check for mutually exclusive options.
if ( ! empty( $assoc_args['file'] ) && ! empty( $assoc_args['dir'] ) ) {
WP_CLI::error( 'The --file and --dir options are mutually exclusive.' );
}
$type = isset( $assoc_args['type'] ) ? $assoc_args['type'] : 'wp_template';
$template = get_block_template( $args[0], $type );
if ( ! $template ) {
WP_CLI::error( "Block template '{$args[0]}' not found." );
}
$content = $template->content;
if ( isset( $assoc_args['stdout'] ) ) {
echo $content;
return;
}
// Handle --file option for direct file path.
if ( ! empty( $assoc_args['file'] ) ) {
$filepath = $assoc_args['file'];
$dir = dirname( $filepath );
// Create parent directories if needed.
if ( ! empty( $dir ) && '.' !== $dir && ! is_dir( $dir ) ) {
if ( ! wp_mkdir_p( $dir ) ) {
WP_CLI::error( "Could not create directory '{$dir}'." );
}
}
} else {
$dir = isset( $assoc_args['dir'] ) ? rtrim( $assoc_args['dir'], '/' ) : '.';
// Create directory if needed.
if ( ! is_dir( $dir ) ) {
if ( ! wp_mkdir_p( $dir ) ) {
WP_CLI::error( "Could not create directory '{$dir}'." );
}
}
$filename = $template->slug . '.html';
$filepath = $dir . '/' . $filename;
}
$result = file_put_contents( $filepath, $content );
if ( false === $result ) {
WP_CLI::error( "Failed to write to '{$filepath}'." );
}
WP_CLI::success( "Exported template to '{$filepath}'." );
}
/**
* Converts a template object to a standardized associative array.
*
* @param WP_Block_Template $template Template object.
* @return array
*/
private function template_to_array( $template ) {
return [
'id' => $template->id,
'slug' => $template->slug,
'theme' => $template->theme,
'type' => $template->type,
'source' => $template->source,
'origin' => $template->origin,
'title' => is_array( $template->title ) ? $template->title['rendered'] : $template->title,
'description' => $template->description,
'status' => $template->status,
'author' => $template->author,
'is_custom' => $template->is_custom,
'has_theme_file' => $template->has_theme_file,
'area' => isset( $template->area ) ? $template->area : '',
'content' => $template->content,
];
}
/**
* 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-template' );
}
}