|
| 1 | +<?php |
| 2 | + |
| 3 | +use WP_CLI\Formatter; |
| 4 | + |
| 5 | +/** |
| 6 | + * Manages block types. |
| 7 | + * |
| 8 | + * Lists and gets information about registered block types in the WordPress block editor. |
| 9 | + * |
| 10 | + * ## EXAMPLES |
| 11 | + * |
| 12 | + * # List all registered block types |
| 13 | + * $ wp block list --format=csv |
| 14 | + * name,title,description,category |
| 15 | + * core/paragraph,Paragraph,"Start with the building block of all narrative.",text |
| 16 | + * |
| 17 | + * # Get details about a specific block type |
| 18 | + * $ wp block get core/paragraph --fields=name,title,category |
| 19 | + * +----------+-----------+------+ |
| 20 | + * | name | title | category | |
| 21 | + * +----------+-----------+------+ |
| 22 | + * | core/paragraph | Paragraph | text | |
| 23 | + * +----------+-----------+------+ |
| 24 | + * |
| 25 | + * @package wp-cli |
| 26 | + */ |
| 27 | +class Block_Command extends WP_CLI_Command { |
| 28 | + |
| 29 | + private $fields = array( |
| 30 | + 'name', |
| 31 | + 'title', |
| 32 | + 'description', |
| 33 | + 'category', |
| 34 | + ); |
| 35 | + |
| 36 | + /** |
| 37 | + * Lists registered block types. |
| 38 | + * |
| 39 | + * ## OPTIONS |
| 40 | + * |
| 41 | + * [--field=<field>] |
| 42 | + * : Prints the value of a single field for each block type. |
| 43 | + * |
| 44 | + * [--fields=<fields>] |
| 45 | + * : Limit the output to specific block type fields. |
| 46 | + * |
| 47 | + * [--format=<format>] |
| 48 | + * : Render output in a particular format. |
| 49 | + * --- |
| 50 | + * default: table |
| 51 | + * options: |
| 52 | + * - table |
| 53 | + * - csv |
| 54 | + * - json |
| 55 | + * - count |
| 56 | + * - yaml |
| 57 | + * --- |
| 58 | + * |
| 59 | + * ## AVAILABLE FIELDS |
| 60 | + * |
| 61 | + * These fields will be displayed by default for each block type: |
| 62 | + * |
| 63 | + * * name |
| 64 | + * * title |
| 65 | + * * description |
| 66 | + * * category |
| 67 | + * |
| 68 | + * These fields are optionally available: |
| 69 | + * |
| 70 | + * * parent |
| 71 | + * * icon |
| 72 | + * * keywords |
| 73 | + * * textdomain |
| 74 | + * * supports |
| 75 | + * * styles |
| 76 | + * * variations |
| 77 | + * * api_version |
| 78 | + * * editor_script |
| 79 | + * * editor_style |
| 80 | + * * script |
| 81 | + * * style |
| 82 | + * |
| 83 | + * ## EXAMPLES |
| 84 | + * |
| 85 | + * # List all registered block types |
| 86 | + * $ wp block list |
| 87 | + * +-------------------+-------------------+----------------------------------------+----------+ |
| 88 | + * | name | title | description | category | |
| 89 | + * +-------------------+-------------------+----------------------------------------+----------+ |
| 90 | + * | core/paragraph | Paragraph | Start with the building block of all.. | text | |
| 91 | + * | core/heading | Heading | Introduce new sections and organize... | text | |
| 92 | + * +-------------------+-------------------+----------------------------------------+----------+ |
| 93 | + * |
| 94 | + * # List all block types with 'text' category |
| 95 | + * $ wp block list --format=csv |
| 96 | + * name,title,description,category |
| 97 | + * core/paragraph,Paragraph,"Start with the building block of all narrative.",text |
| 98 | + * |
| 99 | + * @subcommand list |
| 100 | + */ |
| 101 | + public function list_( $args, $assoc_args ) { |
| 102 | + $registry = WP_Block_Type_Registry::get_instance(); |
| 103 | + $blocks = $registry->get_all_registered(); |
| 104 | + |
| 105 | + $items = array(); |
| 106 | + foreach ( $blocks as $block ) { |
| 107 | + $items[] = $this->prepare_block_for_output( $block ); |
| 108 | + } |
| 109 | + |
| 110 | + $formatter = $this->get_formatter( $assoc_args ); |
| 111 | + $formatter->display_items( $items ); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Gets details about a registered block type. |
| 116 | + * |
| 117 | + * ## OPTIONS |
| 118 | + * |
| 119 | + * <name> |
| 120 | + * : Block type name (e.g., core/paragraph). |
| 121 | + * |
| 122 | + * [--field=<field>] |
| 123 | + * : Instead of returning the whole block type, returns the value of a single field. |
| 124 | + * |
| 125 | + * [--fields=<fields>] |
| 126 | + * : Limit the output to specific fields. Defaults to all fields. |
| 127 | + * |
| 128 | + * [--format=<format>] |
| 129 | + * : Render output in a particular format. |
| 130 | + * --- |
| 131 | + * default: table |
| 132 | + * options: |
| 133 | + * - table |
| 134 | + * - csv |
| 135 | + * - json |
| 136 | + * - yaml |
| 137 | + * --- |
| 138 | + * |
| 139 | + * ## AVAILABLE FIELDS |
| 140 | + * |
| 141 | + * * name |
| 142 | + * * title |
| 143 | + * * description |
| 144 | + * * category |
| 145 | + * * parent |
| 146 | + * * icon |
| 147 | + * * keywords |
| 148 | + * * textdomain |
| 149 | + * * supports |
| 150 | + * * styles |
| 151 | + * * variations |
| 152 | + * * api_version |
| 153 | + * * editor_script |
| 154 | + * * editor_style |
| 155 | + * * script |
| 156 | + * * style |
| 157 | + * |
| 158 | + * ## EXAMPLES |
| 159 | + * |
| 160 | + * # Get details about the core/paragraph block type. |
| 161 | + * $ wp block get core/paragraph --fields=name,title,category |
| 162 | + * +----------------+-----------+----------+ |
| 163 | + * | name | title | category | |
| 164 | + * +----------------+-----------+----------+ |
| 165 | + * | core/paragraph | Paragraph | text | |
| 166 | + * +----------------+-----------+----------+ |
| 167 | + */ |
| 168 | + public function get( $args, $assoc_args ) { |
| 169 | + $block_name = $args[0]; |
| 170 | + $registry = WP_Block_Type_Registry::get_instance(); |
| 171 | + $block = $registry->get_registered( $block_name ); |
| 172 | + |
| 173 | + if ( ! $block ) { |
| 174 | + WP_CLI::error( "Block type '{$block_name}' is not registered." ); |
| 175 | + } |
| 176 | + |
| 177 | + $data = $this->prepare_block_for_output( $block ); |
| 178 | + |
| 179 | + $formatter = $this->get_formatter( $assoc_args ); |
| 180 | + $formatter->display_item( $data ); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Prepares block data for output. |
| 185 | + * |
| 186 | + * @param WP_Block_Type $block Block type object. |
| 187 | + * @return array Prepared block data. |
| 188 | + */ |
| 189 | + private function prepare_block_for_output( $block ) { |
| 190 | + return array( |
| 191 | + 'name' => $block->name, |
| 192 | + 'title' => $block->title ?? '', |
| 193 | + 'description' => $block->description ?? '', |
| 194 | + 'category' => $block->category ?? '', |
| 195 | + 'parent' => $block->parent ?? null, |
| 196 | + 'icon' => $block->icon ?? '', |
| 197 | + 'keywords' => $block->keywords ?? array(), |
| 198 | + 'textdomain' => $block->textdomain ?? '', |
| 199 | + 'supports' => $block->supports ?? array(), |
| 200 | + 'styles' => $block->styles ?? array(), |
| 201 | + 'variations' => $block->variations ?? array(), |
| 202 | + 'api_version' => $block->api_version ?? 1, |
| 203 | + 'editor_script' => $block->editor_script ?? '', |
| 204 | + 'editor_style' => $block->editor_style ?? '', |
| 205 | + 'script' => $block->script ?? '', |
| 206 | + 'style' => $block->style ?? '', |
| 207 | + ); |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Gets a formatter instance. |
| 212 | + * |
| 213 | + * @param array $assoc_args Associative arguments. |
| 214 | + * @return Formatter Formatter instance. |
| 215 | + */ |
| 216 | + private function get_formatter( &$assoc_args ) { |
| 217 | + return new Formatter( $assoc_args, $this->fields, 'block' ); |
| 218 | + } |
| 219 | +} |
0 commit comments