@@ -361,46 +361,32 @@ function twenty_twenty_one_get_non_latin_css( $type = 'front-end' ) {
361361 * @return bool Returns true if a block was located & printed, otherwise false.
362362 */
363363function twenty_twenty_one_print_first_instance_of_block ( $ block_name , $ content = null , $ instances = 1 ) {
364- $ instances_count = 0 ;
365364 $ blocks_content = '' ;
366365
367366 if ( ! $ content ) {
368367 $ content = get_the_content ();
369368 }
370369
371- // Parse blocks in the content.
372- $ blocks = parse_blocks ( $ content ) ;
370+ $ processor = new WP_Block_Processor ( $ content );
371+ $ instance_count = 0 ;
373372
374- // Loop blocks.
375- foreach ( $ blocks as $ block ) {
373+ if ( str_ends_with ( $ block_name , '* ' ) ) {
374+ // Scan for blocks whose block type matches the prefix.
375+ $ prefix = rtrim ( $ block_name , '* ' );
376376
377- // Confidence check.
378- if ( ! isset ( $ block ['blockName ' ] ) ) {
379- continue ;
380- }
381-
382- // Check if this the block matches the $block_name.
383- $ is_matching_block = false ;
384-
385- // If the block ends with *, try to match the first portion.
386- if ( '* ' === $ block_name [-1 ] ) {
387- $ is_matching_block = 0 === strpos ( $ block ['blockName ' ], rtrim ( $ block_name , '* ' ) );
388- } else {
389- $ is_matching_block = $ block_name === $ block ['blockName ' ];
390- }
391-
392- if ( $ is_matching_block ) {
393- // Increment count.
394- ++$ instances_count ;
395-
396- // Add the block HTML.
397- $ blocks_content .= render_block ( $ block );
398-
399- // Break the loop if the $instances count was reached.
400- if ( $ instances_count >= $ instances ) {
401- break ;
377+ while ( $ instance_count < $ instances && $ processor ->next_block () ) {
378+ $ matched_block_type = $ processor ->get_printable_block_type ();
379+ if ( str_starts_with ( $ matched_block_type , $ prefix ) ) {
380+ $ blocks_content .= render_block ( $ processor ->extract_full_block_and_advance () );
381+ ++$ instance_count ;
402382 }
403383 }
384+ } else {
385+ // Scan for blocks of the exact block type.
386+ while ( $ instance_count < $ instances && $ processor ->next_block ( $ block_name ) ) {
387+ $ blocks_content .= render_block ( $ processor ->extract_full_block_and_advance () );
388+ ++$ instance_count ;
389+ }
404390 }
405391
406392 if ( $ blocks_content ) {
0 commit comments