-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock_Pattern_Category_Command.php
More file actions
177 lines (162 loc) · 4.09 KB
/
Block_Pattern_Category_Command.php
File metadata and controls
177 lines (162 loc) · 4.09 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
<?php
namespace WP_CLI\Block;
use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI_Command;
use WP_Block_Pattern_Categories_Registry;
/**
* Retrieves details on registered block pattern categories.
*
* Get information on block pattern categories from the
* WP_Block_Pattern_Categories_Registry.
*
* ## EXAMPLES
*
* # List all registered pattern categories
* $ wp block pattern-category list
*
* # Get details about a specific category
* $ wp block pattern-category get featured --format=json
*
* @package wp-cli
*/
class Block_Pattern_Category_Command extends WP_CLI_Command {
/**
* Default fields to display.
*
* @var array
*/
private $fields = [
'name',
'label',
'description',
];
/**
* Lists registered block pattern categories.
*
* ## OPTIONS
*
* [--field=<field>]
* : Prints the value of a single field for each category.
*
* [--fields=<fields>]
* : Limit the output to specific category 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 category:
*
* * name
* * label
* * description
*
* ## EXAMPLES
*
* # List all pattern categories
* $ wp block pattern-category list
*
* # Get category names only
* $ wp block pattern-category list --field=name
*
* # Export categories to JSON
* $ wp block pattern-category list --format=json
*
* @subcommand list
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
$registry = WP_Block_Pattern_Categories_Registry::get_instance();
$categories = $registry->get_all_registered();
$formatter = $this->get_formatter( $assoc_args );
if ( 'ids' === $formatter->format ) {
$ids = wp_list_pluck( $categories, 'name' );
echo implode( ' ', $ids );
return;
}
$items = array_map( [ $this, 'category_to_array' ], $categories );
$formatter->display_items( $items );
}
/**
* Gets details about a registered block pattern category.
*
* ## OPTIONS
*
* <name>
* : Category name (e.g., 'buttons', 'columns').
*
* [--field=<field>]
* : Instead of returning the whole category, 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 'buttons' category
* $ wp block pattern-category get buttons
*
* # Get as JSON
* $ wp block pattern-category get featured --format=json
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
$registry = WP_Block_Pattern_Categories_Registry::get_instance();
$category = $registry->get_registered( $args[0] );
if ( ! $category ) {
WP_CLI::error( "Block pattern category '{$args[0]}' is not registered." );
}
$formatter = $this->get_formatter( $assoc_args );
$data = $this->category_to_array( $category );
$formatter->display_item( $data );
}
/**
* Converts a category array to a standardized associative array.
*
* @param array $category Category array from registry.
* @return array
*/
private function category_to_array( $category ) {
return [
'name' => isset( $category['name'] ) ? $category['name'] : '',
'label' => isset( $category['label'] ) ? $category['label'] : '',
'description' => isset( $category['description'] ) ? $category['description'] : '',
];
}
/**
* 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-pattern-category' );
}
}