-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock_Synced_Pattern_Command.php
More file actions
555 lines (500 loc) · 13 KB
/
Block_Synced_Pattern_Command.php
File metadata and controls
555 lines (500 loc) · 13 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php
namespace WP_CLI\Block;
use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI\Utils;
use WP_CLI_Command;
use WP_Post;
/**
* Manages synced patterns (reusable blocks).
*
* Synced patterns are stored as the 'wp_block' post type and can be either
* synced (changes reflect everywhere) or not synced (regular patterns).
*
* ## EXAMPLES
*
* # List all synced patterns
* $ wp block synced-pattern list
*
* # Create a synced pattern
* $ wp block synced-pattern create --title="My Pattern" --content='<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->'
*
* # Delete a synced pattern
* $ wp block synced-pattern delete 123
*
* @package wp-cli
*/
class Block_Synced_Pattern_Command extends WP_CLI_Command {
/**
* Default fields to display.
*
* @var array
*/
private $fields = [
'ID',
'post_title',
'post_name',
'sync_status',
'post_date',
];
/**
* Lists synced patterns.
*
* ## OPTIONS
*
* [--sync-status=<status>]
* : Filter by sync status.
* ---
* default: all
* options:
* - synced
* - unsynced
* - all
* ---
*
* [--search=<search>]
* : Search by title.
*
* [--field=<field>]
* : Prints the value of a single field for each pattern.
*
* [--fields=<fields>]
* : Limit the output to specific 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 pattern:
*
* * ID
* * post_title
* * post_name
* * sync_status
* * post_date
*
* These fields are optionally available:
*
* * post_content
* * post_status
* * post_author
*
* ## EXAMPLES
*
* # List all synced patterns
* $ wp block synced-pattern list --sync-status=synced
*
* # Search for patterns by title
* $ wp block synced-pattern list --search=hero
*
* # Export all synced patterns to JSON
* $ wp block synced-pattern list --format=json > synced-patterns.json
*
* @subcommand list
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
$query_args = [
'post_type' => 'wp_block',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
];
// Search by title.
if ( ! empty( $assoc_args['search'] ) ) {
$query_args['s'] = $assoc_args['search'];
unset( $assoc_args['search'] );
}
// Filter by sync status.
$sync_status = isset( $assoc_args['sync-status'] ) ? $assoc_args['sync-status'] : 'all';
unset( $assoc_args['sync-status'] );
if ( 'synced' === $sync_status ) {
$query_args['meta_query'] = [
'relation' => 'OR',
[
'key' => 'wp_pattern_sync_status',
'compare' => 'NOT EXISTS',
],
[
'key' => 'wp_pattern_sync_status',
'value' => '',
'compare' => '=',
],
];
} elseif ( 'unsynced' === $sync_status ) {
$query_args['meta_query'] = [
[
'key' => 'wp_pattern_sync_status',
'value' => 'unsynced',
'compare' => '=',
],
];
}
$patterns = get_posts( $query_args );
$formatter = $this->get_formatter( $assoc_args );
if ( 'ids' === $formatter->format ) {
$ids = wp_list_pluck( $patterns, 'ID' );
echo implode( ' ', $ids );
return;
}
$items = array_map( [ $this, 'pattern_to_array' ], $patterns );
$formatter->display_items( $items );
}
/**
* Gets details about a synced pattern.
*
* ## OPTIONS
*
* <id>
* : The synced pattern ID.
*
* [--field=<field>]
* : Instead of returning the whole pattern, 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 a synced pattern
* $ wp block synced-pattern get 123
*
* # Get pattern content only
* $ wp block synced-pattern get 123 --field=post_content
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
$pattern = get_post( $args[0] );
if ( ! $pattern || 'wp_block' !== $pattern->post_type ) {
WP_CLI::error( "Synced pattern with ID {$args[0]} not found." );
}
if ( empty( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = array_merge(
$this->fields,
[
'post_content',
'post_status',
'post_author',
]
);
}
$formatter = $this->get_formatter( $assoc_args );
$data = $this->pattern_to_array( $pattern );
$formatter->display_item( $data );
}
/**
* Creates a synced pattern.
*
* ## OPTIONS
*
* [--title=<title>]
* : The pattern title.
*
* [--slug=<slug>]
* : The pattern slug. Default: sanitized title.
*
* [--content=<content>]
* : The block content.
*
* [--sync-status=<status>]
* : Sync status.
* ---
* default: synced
* options:
* - synced
* - unsynced
* ---
*
* [--status=<status>]
* : Post status.
* ---
* default: publish
* ---
*
* [<file>]
* : Read content from file. Pass '-' for STDIN.
*
* [--porcelain]
* : Output only the new pattern ID.
*
* ## EXAMPLES
*
* # Create a synced pattern from content
* $ wp block synced-pattern create --title="My Hero" --content='<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->'
*
* # Create from file
* $ wp block synced-pattern create --title="Header" header.html
*
* # Create an unsynced pattern
* $ wp block synced-pattern create --title="Footer" --sync-status=unsynced footer.html
*
* # Create from STDIN
* $ cat content.html | wp block synced-pattern create --title="From STDIN" -
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function create( $args, $assoc_args ) {
$content = '';
// Read content from file or STDIN.
if ( ! empty( $args[0] ) ) {
$content = $this->read_from_file_or_stdin( $args[0] );
} elseif ( ! empty( $assoc_args['content'] ) ) {
$content = $assoc_args['content'];
}
if ( empty( $assoc_args['title'] ) ) {
WP_CLI::error( 'Pattern title is required. Use --title=<title>.' );
}
if ( empty( $content ) ) {
WP_CLI::error( 'Pattern content is required. Use --content=<content> or provide a file.' );
}
// Warn if content doesn't appear to contain valid blocks.
$this->validate_block_content( $content );
$post_data = [
'post_type' => 'wp_block',
'post_title' => $assoc_args['title'],
'post_content' => $content,
'post_status' => isset( $assoc_args['status'] ) ? $assoc_args['status'] : 'publish',
];
if ( ! empty( $assoc_args['slug'] ) ) {
$post_data['post_name'] = $assoc_args['slug'];
}
$post_id = wp_insert_post( $post_data, true );
if ( is_wp_error( $post_id ) ) {
WP_CLI::error( $post_id->get_error_message() );
}
// Set sync status meta.
$sync_status = isset( $assoc_args['sync-status'] ) ? $assoc_args['sync-status'] : 'synced';
if ( 'unsynced' === $sync_status ) {
update_post_meta( $post_id, 'wp_pattern_sync_status', 'unsynced' );
}
if ( isset( $assoc_args['porcelain'] ) ) {
WP_CLI::line( (string) $post_id );
} else {
$status_label = 'synced' === $sync_status ? 'synced' : 'unsynced';
WP_CLI::success( "Created {$status_label} pattern {$post_id}." );
}
}
/**
* Updates a synced pattern.
*
* ## OPTIONS
*
* <id>
* : The synced pattern ID.
*
* [--title=<title>]
* : The pattern title.
*
* [--content=<content>]
* : The block content.
*
* [--sync-status=<status>]
* : Sync status.
* ---
* options:
* - synced
* - unsynced
* ---
*
* [<file>]
* : Read content from file. Pass '-' for STDIN.
*
* ## EXAMPLES
*
* # Update pattern title
* $ wp block synced-pattern update 123 --title="Updated Hero"
*
* # Update content from file
* $ wp block synced-pattern update 123 updated-content.html
*
* # Change sync status
* $ wp block synced-pattern update 123 --sync-status=unsynced
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function update( $args, $assoc_args ) {
$pattern_id = $args[0];
$pattern = get_post( $pattern_id );
if ( ! $pattern || 'wp_block' !== $pattern->post_type ) {
WP_CLI::error( "Synced pattern with ID {$pattern_id} not found." );
}
$post_data = [
'ID' => $pattern_id,
];
// Update title.
if ( ! empty( $assoc_args['title'] ) ) {
$post_data['post_title'] = $assoc_args['title'];
}
// Read content from file or option.
if ( ! empty( $args[1] ) ) {
$post_data['post_content'] = $this->read_from_file_or_stdin( $args[1] );
} elseif ( ! empty( $assoc_args['content'] ) ) {
$post_data['post_content'] = $assoc_args['content'];
}
if ( count( $post_data ) > 1 ) {
$result = wp_update_post( $post_data, true );
if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
}
}
// Update sync status meta.
if ( isset( $assoc_args['sync-status'] ) ) {
if ( 'unsynced' === $assoc_args['sync-status'] ) {
update_post_meta( $pattern_id, 'wp_pattern_sync_status', 'unsynced' );
} else {
delete_post_meta( $pattern_id, 'wp_pattern_sync_status' );
}
}
WP_CLI::success( "Updated synced pattern {$pattern_id}." );
}
/**
* Deletes one or more synced patterns.
*
* ## OPTIONS
*
* <id>...
* : One or more synced pattern IDs.
*
* [--force]
* : Skip trash and permanently delete.
*
* ## EXAMPLES
*
* # Delete a synced pattern (to trash)
* $ wp block synced-pattern delete 123
*
* # Permanently delete
* $ wp block synced-pattern delete 123 --force
*
* # Delete multiple
* $ wp block synced-pattern delete 123 456 789
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function delete( $args, $assoc_args ) {
$force = Utils\get_flag_value( $assoc_args, 'force', false );
$count = 0;
$errored = 0;
foreach ( $args as $pattern_id ) {
$pattern = get_post( $pattern_id );
if ( ! $pattern || 'wp_block' !== $pattern->post_type ) {
WP_CLI::warning( "Synced pattern with ID {$pattern_id} not found." );
++$errored;
continue;
}
$result = wp_delete_post( $pattern_id, $force );
if ( $result ) {
++$count;
} else {
WP_CLI::warning( "Failed to delete synced pattern {$pattern_id}." );
++$errored;
}
}
if ( $count > 0 ) {
$action = $force ? 'Deleted' : 'Trashed';
WP_CLI::success( "{$action} {$count} synced pattern(s)." );
}
if ( $errored > 0 ) {
WP_CLI::error( "Failed to delete {$errored} synced pattern(s)." );
}
}
/**
* Converts a pattern post to a standardized associative array.
*
* @param WP_Post $pattern Pattern post object.
* @return array
*/
private function pattern_to_array( $pattern ) {
$sync_status = get_post_meta( $pattern->ID, 'wp_pattern_sync_status', true );
return [
'ID' => $pattern->ID,
'post_title' => $pattern->post_title,
'post_name' => $pattern->post_name,
'post_content' => $pattern->post_content,
'post_status' => $pattern->post_status,
'post_author' => $pattern->post_author,
'post_date' => $pattern->post_date,
'sync_status' => 'unsynced' === $sync_status ? 'unsynced' : 'synced',
];
}
/**
* Reads content from a file or STDIN.
*
* @param string $file File path or '-' for STDIN.
* @return string File content.
*/
private function read_from_file_or_stdin( $file ) {
if ( '-' === $file ) {
$content = file_get_contents( 'php://stdin' );
if ( false === $content ) {
WP_CLI::error( 'Failed to read from STDIN.' );
}
return $content;
}
if ( ! file_exists( $file ) ) {
WP_CLI::error( "File '{$file}' does not exist." );
}
$content = file_get_contents( $file );
if ( false === $content ) {
WP_CLI::error( "Failed to read file '{$file}'." );
}
return $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, 'synced-pattern' );
}
/**
* Validates block content and warns if it appears malformed.
*
* @param string $content Block content to validate.
*/
private function validate_block_content( $content ) {
$blocks = parse_blocks( $content );
// Filter out empty/null blocks (freeform content).
$valid_blocks = array_filter(
$blocks,
function ( $block ) {
return ! empty( $block['blockName'] );
}
);
if ( empty( $valid_blocks ) ) {
WP_CLI::warning( 'Content does not appear to contain valid blocks. The pattern will be created with the provided content.' );
}
}
}