@@ -74,16 +74,14 @@ $scanner = Block_Scanner::create( $post_content );
74
74
75
75
// Find the first image block
76
76
while ( $scanner->next_delimiter() ) {
77
- if ( ! $scanner->is_block_type ( 'image' ) ) {
77
+ if ( ! $scanner->opens_block ( 'image' ) ) {
78
78
continue;
79
79
}
80
80
81
- if ( $scanner->opens_block() ) {
82
- $attributes = $scanner->allocate_and_return_parsed_attributes();
83
- if ( isset( $attributes['id'] ) ) {
84
- echo "Found image with ID: " . $attributes['id'];
85
- break;
86
- }
81
+ $attributes = $scanner->allocate_and_return_parsed_attributes();
82
+ if ( isset( $attributes['id'] ) ) {
83
+ echo "Found image with ID: " . $attributes['id'];
84
+ break;
87
85
}
88
86
}
89
87
```
@@ -130,7 +128,7 @@ Paragraph with font size: small
130
128
131
129
### Counting Block Types
132
130
133
- Get a summary of all block types in a document:
131
+ Get a count of all block types in a document:
134
132
135
133
``` php
136
134
use Automattic\Block_Scanner;
@@ -151,33 +149,33 @@ $post_content = '<!-- wp:heading {"level":2} -->
151
149
<ul ><li >Item 1</li ><li >Item 2</li ></ul >
152
150
<!-- /wp:list --> ';
153
151
154
- function get_block_types_in ( string $html ): array {
155
- $block_types = [];
152
+ function count_block_types_in ( string $html ): array {
153
+ $block_counts = [];
156
154
$scanner = Block_Scanner::create( $html );
157
155
158
156
while ( $scanner->next_delimiter() ) {
159
157
if ( $scanner->opens_block() ) {
160
- $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;
161
160
}
162
161
}
163
162
164
- $block_types = array_keys( $block_types );
165
- sort( $block_types );
166
- return $block_types;
163
+ ksort( $block_counts );
164
+ return $block_counts;
167
165
}
168
166
169
- $block_types = get_block_types_in ( $post_content );
170
- print_r( $block_types );
167
+ $block_counts = count_block_types_in ( $post_content );
168
+ print_r( $block_counts );
171
169
```
172
170
173
171
** Output:**
174
172
```
175
173
Array
176
174
(
177
- [0 ] => core/heading
178
- [1 ] => core/image
179
- [2 ] => core/list
180
- [3 ] => core/paragraph
175
+ [core/heading ] => 1
176
+ [core/image ] => 1
177
+ [core/list ] => 1
178
+ [core/paragraph ] => 2
181
179
)
182
180
```
183
181
@@ -343,7 +341,7 @@ Paragraph with font size: small
343
341
344
342
### Counting Block Types
345
343
346
- Get a summary of all block types in a document:
344
+ Get a count of all block types in a document:
347
345
348
346
``` php
349
347
use Automattic\Block_Delimiter;
@@ -364,32 +362,32 @@ $post_content = '<!-- wp:heading {"level":2} -->
364
362
<ul ><li >Item 1</li ><li >Item 2</li ></ul >
365
363
<!-- /wp:list --> ';
366
364
367
- function get_block_types_in ( string $html ): array {
368
- $block_types = [];
365
+ function count_block_types_in ( string $html ): array {
366
+ $block_counts = [];
369
367
370
368
foreach ( Block_Delimiter::scan_delimiters( $html ) as $delimiter ) {
371
369
if ( Block_Delimiter::OPENER === $delimiter->get_delimiter_type() ) {
372
- $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;
373
372
}
374
373
}
375
374
376
- $block_types = array_keys( $block_types );
377
- sort( $block_types );
378
- return $block_types;
375
+ ksort( $block_counts );
376
+ return $block_counts;
379
377
}
380
378
381
- $block_types = get_block_types_in ( $post_content );
382
- print_r( $block_types );
379
+ $block_counts = count_block_types_in ( $post_content );
380
+ print_r( $block_counts );
383
381
```
384
382
385
383
** Output:**
386
384
```
387
385
Array
388
386
(
389
- [0 ] => core/heading
390
- [1 ] => core/image
391
- [2 ] => core/list
392
- [3 ] => core/paragraph
387
+ [core/heading ] => 1
388
+ [core/image ] => 1
389
+ [core/list ] => 1
390
+ [core/paragraph ] => 2
393
391
)
394
392
```
395
393
0 commit comments