wpseek.com
A WordPress-centric search engine for devs and theme authors
register_block_script_handle › WordPress Function
Since5.5.0
Deprecatedn/a
› register_block_script_handle ( $metadata, $field_name, $index = 0 )
Parameters: (3) |
|
Returns: |
|
Defined at: |
|
Codex: | |
Change Log: |
|
Finds a script handle for the selected block metadata field. It detects when a path to file was provided and finds a corresponding asset file with details necessary to register the script under automatically generated handle name. It returns unprocessed script handle otherwise.
Source
function register_block_script_handle( $metadata, $field_name, $index = 0 ) { if ( empty( $metadata[ $field_name ] ) ) { return false; } $script_handle = $metadata[ $field_name ]; if ( is_array( $script_handle ) ) { if ( empty( $script_handle[ $index ] ) ) { return false; } $script_handle = $script_handle[ $index ]; } $script_path = remove_block_asset_path_prefix( $script_handle ); if ( $script_handle === $script_path ) { return $script_handle; } $path = dirname( $metadata['file'] ); $script_asset_raw_path = $path . '/' . substr_replace( $script_path, '.asset.php', - strlen( '.js' ) ); $script_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index ); $script_asset_path = wp_normalize_path( realpath( $script_asset_raw_path ) ); if ( empty( $script_asset_path ) ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: 1: Asset file location, 2: Field name, 3: Block name. */ __( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.' ), $script_asset_raw_path, $field_name, $metadata['name'] ), '5.5.0' ); return false; } $script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) ); $script_uri = get_block_asset_url( $script_path_norm ); $script_args = array(); if ( 'viewScript' === $field_name && $script_uri ) { $script_args['strategy'] = 'defer'; } $script_asset = require $script_asset_path; $script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array(); $result = wp_register_script( $script_handle, $script_uri, $script_dependencies, isset( $script_asset['version'] ) ? $script_asset['version'] : false, $script_args ); if ( ! $result ) { return false; } if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $script_dependencies, true ) ) { wp_set_script_translations( $script_handle, $metadata['textdomain'] ); } return $script_handle; }