Skip to content

Commit

Permalink
Refactor block registration processing to use new properties
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Sep 9, 2022
1 parent f48b1c1 commit 709ed1b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
26 changes: 13 additions & 13 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,15 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
}

$script_fields = array(
'editorScript' => 'editor_script',
'script' => 'script',
'viewScript' => 'view_script',
'editorScript' => 'editor_script_handles',
'script' => 'script_handles',
'viewScript' => 'view_script_handles',
);
foreach ( $script_fields as $metadata_field_name => $settings_field_name ) {
if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
$scripts = $metadata[ $metadata_field_name ];
$scripts = $metadata[ $metadata_field_name ];
$processed_scripts = array();
if ( is_array( $scripts ) ) {
$processed_scripts = array();
for ( $index = 0; $index < count( $scripts ); $index++ ) {
$result = register_block_script_handle(
$metadata,
Expand All @@ -360,28 +360,28 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$processed_scripts[] = $result;
}
}
$settings[ $settings_field_name ] = $processed_scripts;
} else {
$result = register_block_script_handle(
$metadata,
$metadata_field_name
);
if ( $result ) {
$settings[ $settings_field_name ] = $result;
$processed_scripts[] = $result;
}
}
$settings[ $settings_field_name ] = $processed_scripts;
}
}

$style_fields = array(
'editorStyle' => 'editor_style',
'style' => 'style',
'editorStyle' => 'editor_style_handles',
'style' => 'style_handles',
);
foreach ( $style_fields as $metadata_field_name => $settings_field_name ) {
if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
$styles = $metadata[ $metadata_field_name ];
$styles = $metadata[ $metadata_field_name ];
$processed_styles = array();
if ( is_array( $styles ) ) {
$processed_styles = array();
for ( $index = 0; $index < count( $styles ); $index++ ) {
$result = register_block_style_handle(
$metadata,
Expand All @@ -392,16 +392,16 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$processed_styles[] = $result;
}
}
$settings[ $settings_field_name ] = $processed_styles;
} else {
$result = register_block_style_handle(
$metadata,
$metadata_field_name
);
if ( $result ) {
$settings[ $settings_field_name ] = $result;
$processed_styles[] = $result;
}
}
$settings[ $settings_field_name ] = $processed_styles;
}
}

Expand Down
19 changes: 14 additions & 5 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,16 +496,25 @@ public function test_block_registers_with_metadata_fixture() {
),
$result->example
);
$this->assertSame( 'tests-notice-editor-script', $result->editor_script );
$this->assertSame( 'tests-notice-script', $result->script );
$this->assertSameSets(
array( 'tests-notice-editor-script' ),
$result->editor_script_handles
);
$this->assertSameSets(
array( 'tests-notice-script' ),
$result->script_handles
);
$this->assertSameSets(
array( 'tests-notice-view-script', 'tests-notice-view-script-2' ),
$result->view_script
$result->view_script_handles
);
$this->assertSameSets(
array( 'tests-notice-editor-style' ),
$result->editor_style_handles
);
$this->assertSame( 'tests-notice-editor-style', $result->editor_style );
$this->assertSameSets(
array( 'tests-notice-style', 'tests-notice-style-2' ),
$result->style
$result->style_handles
);

// @ticket 50328
Expand Down
16 changes: 8 additions & 8 deletions tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ public function test_get_item_invalid() {
$this->assertFalse( $data['is_dynamic'] );
$this->assertSameSets( array( array() ), $data['variations'] );
// Deprecated properties.
$this->assertNull( $data['editor_script'] );
$this->assertNull( $data['script'] );
$this->assertNull( $data['view_script'] );
$this->assertNull( $data['editor_style'] );
$this->assertNull( $data['style'] );
$this->assertNull( $data['editor_script'] );
$this->assertNull( $data['script'] );
$this->assertNull( $data['view_script'] );
$this->assertNull( $data['editor_style'] );
$this->assertNull( $data['style'] );

}

Expand Down Expand Up @@ -333,9 +333,9 @@ public function test_get_item_defaults() {
// Deprecated properties.
$this->assertNull( $data['editor_script'] );
$this->assertNull( $data['script'] );
$this->assertNull( $data['view_script'] );
$this->assertNull( $data['editor_style'] );
$this->assertNull( $data['style'] );
$this->assertNull( $data['view_script'] );
$this->assertNull( $data['editor_style'] );
$this->assertNull( $data['style'] );
}

public function test_get_variation() {
Expand Down

0 comments on commit 709ed1b

Please sign in to comment.