@@ -128,7 +128,7 @@ Paragraph with font size: small
128
128
129
129
### Counting Block Types
130
130
131
- Get a summary of all block types in a document:
131
+ Get a count of all block types in a document:
132
132
133
133
``` php
134
134
use Automattic\Block_Scanner;
@@ -149,33 +149,33 @@ $post_content = '<!-- wp:heading {"level":2} -->
149
149
<ul ><li >Item 1</li ><li >Item 2</li ></ul >
150
150
<!-- /wp:list --> ';
151
151
152
- function get_block_types_in ( string $html ): array {
153
- $block_types = [];
152
+ function count_block_types_in ( string $html ): array {
153
+ $block_counts = [];
154
154
$scanner = Block_Scanner::create( $html );
155
155
156
156
while ( $scanner->next_delimiter() ) {
157
157
if ( $scanner->opens_block() ) {
158
- $block_types[ $scanner->get_block_type() ] = true;
158
+ $block_type = $scanner->get_block_type();
159
+ $block_counts[ $block_type ] = ( $block_counts[ $block_type ] ?? 0 ) + 1;
159
160
}
160
161
}
161
162
162
- $block_types = array_keys( $block_types );
163
- sort( $block_types );
164
- return $block_types;
163
+ ksort( $block_counts );
164
+ return $block_counts;
165
165
}
166
166
167
- $block_types = get_block_types_in ( $post_content );
168
- print_r( $block_types );
167
+ $block_counts = count_block_types_in ( $post_content );
168
+ print_r( $block_counts );
169
169
```
170
170
171
171
** Output:**
172
172
```
173
173
Array
174
174
(
175
- [0 ] => core/heading
176
- [1 ] => core/image
177
- [2 ] => core/list
178
- [3 ] => core/paragraph
175
+ [core/heading ] => 1
176
+ [core/image ] => 1
177
+ [core/list ] => 1
178
+ [core/paragraph ] => 2
179
179
)
180
180
```
181
181
@@ -341,7 +341,7 @@ Paragraph with font size: small
341
341
342
342
### Counting Block Types
343
343
344
- Get a summary of all block types in a document:
344
+ Get a count of all block types in a document:
345
345
346
346
``` php
347
347
use Automattic\Block_Delimiter;
@@ -362,32 +362,32 @@ $post_content = '<!-- wp:heading {"level":2} -->
362
362
<ul ><li >Item 1</li ><li >Item 2</li ></ul >
363
363
<!-- /wp:list --> ';
364
364
365
- function get_block_types_in ( string $html ): array {
366
- $block_types = [];
365
+ function count_block_types_in ( string $html ): array {
366
+ $block_counts = [];
367
367
368
368
foreach ( Block_Delimiter::scan_delimiters( $html ) as $delimiter ) {
369
369
if ( Block_Delimiter::OPENER === $delimiter->get_delimiter_type() ) {
370
- $block_types[ $delimiter->allocate_and_return_block_type() ] = true;
370
+ $block_type = $delimiter->allocate_and_return_block_type();
371
+ $block_counts[ $block_type ] = ( $block_counts[ $block_type ] ?? 0 ) + 1;
371
372
}
372
373
}
373
374
374
- $block_types = array_keys( $block_types );
375
- sort( $block_types );
376
- return $block_types;
375
+ ksort( $block_counts );
376
+ return $block_counts;
377
377
}
378
378
379
- $block_types = get_block_types_in ( $post_content );
380
- print_r( $block_types );
379
+ $block_counts = count_block_types_in ( $post_content );
380
+ print_r( $block_counts );
381
381
```
382
382
383
383
** Output:**
384
384
```
385
385
Array
386
386
(
387
- [0 ] => core/heading
388
- [1 ] => core/image
389
- [2 ] => core/list
390
- [3 ] => core/paragraph
387
+ [core/heading ] => 1
388
+ [core/image ] => 1
389
+ [core/list ] => 1
390
+ [core/paragraph ] => 2
391
391
)
392
392
```
393
393
0 commit comments