Changeset 57559
- Timestamp:
- 02/08/2024 08:16:59 AM (8 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r57550 r57559 125 125 /** 126 126 * Finds a script handle for the selected block metadata field. It detects 127 * when a path to file was provided and finds a corresponding asset file128 * with details necessary to register the script under automatically127 * when a path to file was provided and optionally finds a corresponding asset 128 * file with details necessary to register the script under automatically 129 129 * generated handle name. It returns unprocessed script handle otherwise. 130 130 * 131 131 * @since 5.5.0 132 132 * @since 6.1.0 Added `$index` parameter. 133 * @since 6.5.0 The asset file is optional. 133 134 * 134 135 * @param array $metadata Block metadata. … … 164 165 ); 165 166 166 if ( empty( $script_asset_path ) ) {167 _doing_it_wrong(168 __FUNCTION__,169 sprintf(170 /* translators: 1: Asset file location, 2: Field name, 3: Block name. */171 __( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.' ),172 $script_asset_raw_path,173 $field_name,174 $metadata['name']175 ),176 '5.5.0'177 );178 return false;179 }180 181 167 $script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) ); 182 168 $script_uri = get_block_asset_url( $script_path_norm ); … … 187 173 } 188 174 189 $script_asset = require $script_asset_path; 175 // Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460. 176 $script_asset = ! empty( $script_asset_path ) ? require $script_asset_path : array(); 190 177 $script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array(); 191 178 $result = wp_register_script( -
trunk/tests/phpunit/tests/blocks/register.php
r57493 r57559 61 61 } 62 62 63 foreach ( wp_scripts()->registered as $script_handle => $script ) { 64 if ( str_starts_with( $script_handle, 'unit-tests-' ) ) { 65 wp_deregister_script( $script_handle ); 66 } 67 } 68 63 69 parent::tear_down(); 64 70 } … … 227 233 228 234 /** 229 * @expectedIncorrectUsage register_block_script_handle 230 * @ticket 50263 231 */ 232 public function test_missing_asset_file_register_block_script_handle() { 235 * @ticket 50263 236 */ 237 public function test_handle_passed_register_block_script_handle() { 238 $metadata = array( 239 'script' => 'test-script-handle', 240 ); 241 $result = register_block_script_handle( $metadata, 'script' ); 242 243 $this->assertSame( 'test-script-handle', $result ); 244 } 245 246 public function test_handles_passed_register_block_script_handles() { 247 $metadata = array( 248 'script' => array( 'test-script-handle', 'test-script-handle-2' ), 249 ); 250 251 $result = register_block_script_handle( $metadata, 'script' ); 252 $this->assertSame( 'test-script-handle', $result ); 253 254 $result = register_block_script_handle( $metadata, 'script', 1 ); 255 $this->assertSame( 'test-script-handle-2', $result, 1 ); 256 } 257 258 /** 259 * @ticket 50263 260 * @ticket 60460 261 */ 262 public function test_missing_asset_file_register_block_script_handle_with_default_settings() { 233 263 $metadata = array( 234 264 'file' => __FILE__, … … 238 268 $result = register_block_script_handle( $metadata, 'script' ); 239 269 240 $this->assertFalse( $result ); 241 } 242 243 /** 244 * @ticket 50263 245 */ 246 public function test_handle_passed_register_block_script_handle() { 247 $metadata = array( 248 'script' => 'test-script-handle', 249 ); 250 $result = register_block_script_handle( $metadata, 'script' ); 251 252 $this->assertSame( 'test-script-handle', $result ); 253 } 254 255 public function test_handles_passed_register_block_script_handles() { 256 $metadata = array( 257 'script' => array( 'test-script-handle', 'test-script-handle-2' ), 258 ); 259 260 $result = register_block_script_handle( $metadata, 'script' ); 261 $this->assertSame( 'test-script-handle', $result ); 262 263 $result = register_block_script_handle( $metadata, 'script', 1 ); 264 $this->assertSame( 'test-script-handle-2', $result, 1 ); 270 $this->assertSame( 'unit-tests-test-block-script', $result ); 265 271 } 266 272
Note: See TracChangeset
for help on using the changeset viewer.