Make WordPress Core

Changeset 57026

Timestamp:
10/28/2023 01:00:14 AM (9 months ago)
Author:
SergeyBiryukov
Message:

Blocks: Parse the arguments earlier in register_block_type_from_metadata().

This makes it possible to register a block by passing an array of arguments, without the presence of a block.json file.

Follow-up to [48141], [49948].

Props aristath, spacedmonkey, mukesh27, costdev, audrasjb, oglekler, felipeelia, hellofromTonya.
Fixes #56865.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r56970 r57026  
    353353
    354354    $is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC );
    355 
    356     if ( ! $is_core_block && ! file_exists( $metadata_file ) ) {
     355    // If the block is not a core block, the metadata file must exist.
     356    $metadata_file_exists = $is_core_block || file_exists( $metadata_file );
     357    if ( ! $metadata_file_exists && empty( $args['name'] ) ) {
    357358        return false;
    358359    }
    359360
    360361    // Try to get metadata from the static cache for core blocks.
    361     $metadata = false;
     362    $metadata = ;
    362363    if ( $is_core_block ) {
    363364        $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder );
     
    368369
    369370    // If metadata is not found in the static cache, read it from the file.
    370     if ( ! $metadata ) {
     371    if ( ) {
    371372        $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
    372373    }
    373374
    374     if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
     375    if ( ! is_array( $metadata ) || ) ) {
    375376        return false;
    376377    }
    377     $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) );
     378
     379    $metadata['file'] = $metadata_file_exists ? wp_normalize_path( realpath( $metadata_file ) ) : null;
    378380
    379381    /**
     
    405407    $property_mappings = array(
    406408        'apiVersion'      => 'api_version',
     409
    407410        'title'           => 'title',
    408411        'category'        => 'category',
     
    427430        if ( isset( $metadata[ $key ] ) ) {
    428431            $settings[ $mapped_key ] = $metadata[ $key ];
    429             if ( $textdomain && isset( $i18n_schema->$key ) ) {
     432            if ( $textdomain && isset( $i18n_schema->$key ) ) {
    430433                $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain );
    431434            }
    432         }
    433     }
    434 
    435     $script_fields = array(
    436         'editorScript' => 'editor_script_handles',
    437         'script'       => 'script_handles',
    438         'viewScript'   => 'view_script_handles',
    439     );
    440     foreach ( $script_fields as $metadata_field_name => $settings_field_name ) {
    441         if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
    442             $scripts           = $metadata[ $metadata_field_name ];
    443             $processed_scripts = array();
    444             if ( is_array( $scripts ) ) {
    445                 for ( $index = 0; $index < count( $scripts ); $index++ ) {
    446                     $result = register_block_script_handle(
    447                         $metadata,
    448                         $metadata_field_name,
    449                         $index
    450                     );
    451                     if ( $result ) {
    452                         $processed_scripts[] = $result;
    453                     }
    454                 }
    455             } else {
    456                 $result = register_block_script_handle(
    457                     $metadata,
    458                     $metadata_field_name
    459                 );
    460                 if ( $result ) {
    461                     $processed_scripts[] = $result;
    462                 }
    463             }
    464             $settings[ $settings_field_name ] = $processed_scripts;
    465         }
    466     }
    467 
    468     $style_fields = array(
    469         'editorStyle' => 'editor_style_handles',
    470         'style'       => 'style_handles',
    471     );
    472     foreach ( $style_fields as $metadata_field_name => $settings_field_name ) {
    473         if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
    474             $styles           = $metadata[ $metadata_field_name ];
    475             $processed_styles = array();
    476             if ( is_array( $styles ) ) {
    477                 for ( $index = 0; $index < count( $styles ); $index++ ) {
    478                     $result = register_block_style_handle(
    479                         $metadata,
    480                         $metadata_field_name,
    481                         $index
    482                     );
    483                     if ( $result ) {
    484                         $processed_styles[] = $result;
    485                     }
    486                 }
    487             } else {
    488                 $result = register_block_style_handle(
    489                     $metadata,
    490                     $metadata_field_name
    491                 );
    492                 if ( $result ) {
    493                     $processed_styles[] = $result;
    494                 }
    495             }
    496             $settings[ $settings_field_name ] = $processed_styles;
    497         }
    498     }
    499 
    500     if ( ! empty( $metadata['blockHooks'] ) ) {
    501         /**
    502          * Map camelCased position string (from block.json) to snake_cased block type position.
    503          *
    504          * @var array
    505          */
    506         $position_mappings = array(
    507             'before'     => 'before',
    508             'after'      => 'after',
    509             'firstChild' => 'first_child',
    510             'lastChild'  => 'last_child',
    511         );
    512 
    513         $settings['block_hooks'] = array();
    514         foreach ( $metadata['blockHooks'] as $anchor_block_name => $position ) {
    515             // Avoid infinite recursion (hooking to itself).
    516             if ( $metadata['name'] === $anchor_block_name ) {
    517                 _doing_it_wrong(
    518                     __METHOD__,
    519                     __( 'Cannot hook block to itself.' ),
    520                     '6.4.0'
    521                 );
    522                 continue;
    523             }
    524 
    525             if ( ! isset( $position_mappings[ $position ] ) ) {
    526                 continue;
    527             }
    528 
    529             $settings['block_hooks'][ $anchor_block_name ] = $position_mappings[ $position ];
    530435        }
    531436    }
     
    558463    }
    559464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
     527
     528
     529
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
     546
     547
     548
     549
     550
     551
     552
     553
     554
     555
     556
     557
     558
     559
     560
     561
     562
     563
     564
     565
     566
     567
     568
     569
     570
    560571    /**
    561572     * Filters the settings determined from the block type metadata.
     
    566577     * @param array $metadata Metadata provided for registering a block type.
    567578     */
    568     $settings = apply_filters(
    569         'block_type_metadata_settings',
    570         array_merge(
    571             $settings,
    572             $args
    573         ),
    574         $metadata
    575     );
     579    $settings = apply_filters( 'block_type_metadata_settings', $settings, $metadata );
     580
     581    $metadata['name'] = ! empty( $settings['name'] ) ? $settings['name'] : $metadata['name'];
    576582
    577583    return WP_Block_Type_Registry::get_instance()->register(
  • trunk/tests/phpunit/tests/blocks/register.php

    r56607 r57026  
    601601
    602602    /**
     603
     604
     605
     606
     607
     608
     609
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     646
     647
     648
     649
     650
     651
     652
     653
     654
     655
     656
     657
     658
     659
     660
     661
     662
     663
     664
     665
     666
     667
     668
     669
     670
     671
     672
     673
     674
     675
     676
     677
     678
     679
     680
     681
     682
     683
     684
     685
     686
     687
     688
     689
     690
     691
     692
     693
     694
     695
     696
     697
     698
     699
     700
     701
     702
     703
     704
     705
     706
     707
     708
     709
     710
     711
     712
     713
     714
     715
     716
     717
     718
     719
     720
     721
     722
    603723     * Tests that the function returns the registered block when the `block.json`
    604724     * is found in the fixtures directory.
Note: See TracChangeset for help on using the changeset viewer.