register_block_script_handle

The timeline below displays how wordpress function register_block_script_handle has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Finds a script handle for the selected block metadata field. It detects
 * when a path to file was provided and optionally finds a corresponding asset
 * file with details necessary to register the script under automatically
 * generated handle name. It returns unprocessed script handle otherwise.
 *
 * @since 5.5.0
 * @since 6.1.0 Added `$index` parameter.
 * @since 6.5.0 The asset file is optional. Added script handle support in the asset file.
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @param int    $index      Optional. Index of the script to register when multiple items passed.
 *                           Default 0.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
function register_block_script_handle($metadata, $field_name, $index = 0)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $script_handle_or_path = $metadata[$field_name];
    if (is_array($script_handle_or_path)) {
        if (empty($script_handle_or_path[$index])) {
            return false;
        }
        $script_handle_or_path = $script_handle_or_path[$index];
    }
    $script_path = remove_block_asset_path_prefix($script_handle_or_path);
    if ($script_handle_or_path === $script_path) {
        return $script_handle_or_path;
    }
    $path = dirname($metadata['file']);
    $script_asset_raw_path = $path . '/' . substr_replace($script_path, '.asset.php', -strlen('.js'));
    $script_asset_path = wp_normalize_path(realpath($script_asset_raw_path));
    // Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
    $script_asset = (!empty($script_asset_path)) ? require $script_asset_path : array();
    $script_handle = isset($script_asset['handle']) ? $script_asset['handle'] : generate_block_asset_handle($metadata['name'], $field_name, $index);
    if (wp_script_is($script_handle, 'registered')) {
        return $script_handle;
    }
    $script_path_norm = wp_normalize_path(realpath($path . '/' . $script_path));
    $script_uri = get_block_asset_url($script_path_norm);
    $script_dependencies = isset($script_asset['dependencies']) ? $script_asset['dependencies'] : array();
    $block_version = isset($metadata['version']) ? $metadata['version'] : false;
    $script_version = isset($script_asset['version']) ? $script_asset['version'] : $block_version;
    $script_args = array();
    if ('viewScript' === $field_name && $script_uri) {
        $script_args['strategy'] = 'defer';
    }
    $result = wp_register_script($script_handle, $script_uri, $script_dependencies, $script_version, $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;
}

WordPress Version: 6.4

/**
 * 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.
 *
 * @since 5.5.0
 * @since 6.1.0 Added `$index` parameter.
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @param int    $index      Optional. Index of the script to register when multiple items passed.
 *                           Default 0.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
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;
}

WordPress Version: 3.2

/**
 * 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.
 *
 * @since 5.5.0
 * @since 6.1.0 Added `$index` parameter.
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @param int    $index      Optional. Index of the script to register when multiple items passed.
 *                           Default 0.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
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;
    }
    $script_asset_raw_path = dirname($metadata['file']) . '/' . 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;
    }
    // Path needs to be normalized to work in Windows env.
    static $wpinc_path_norm = '';
    if (!$wpinc_path_norm) {
        $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    }
    // Cache $template_path_norm and $stylesheet_path_norm to avoid unnecessary additional calls.
    static $template_path_norm = '';
    static $stylesheet_path_norm = '';
    if (!$template_path_norm || !$stylesheet_path_norm) {
        $template_path_norm = wp_normalize_path(get_template_directory());
        $stylesheet_path_norm = wp_normalize_path(get_stylesheet_directory());
    }
    $script_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $script_path));
    $is_core_block = isset($metadata['file']) && str_starts_with($metadata['file'], $wpinc_path_norm);
    /*
     * Determine if the block script was registered in a theme, by checking if the script path starts with either
     * the parent (template) or child (stylesheet) directory path.
     */
    $is_parent_theme_block = str_starts_with($script_path_norm, trailingslashit($template_path_norm));
    $is_child_theme_block = str_starts_with($script_path_norm, trailingslashit($stylesheet_path_norm));
    $is_theme_block = $is_parent_theme_block || $is_child_theme_block;
    $script_uri = plugins_url($script_path, $metadata['file']);
    if ($is_core_block) {
        $script_uri = includes_url(str_replace($wpinc_path_norm, '', $script_path_norm));
    } elseif ($is_theme_block) {
        // Get the script path deterministically based on whether or not it was registered in a parent or child theme.
        $script_uri = $is_parent_theme_block ? get_theme_file_uri(str_replace($template_path_norm, '', $script_path_norm)) : get_theme_file_uri(str_replace($stylesheet_path_norm, '', $script_path_norm));
    }
    $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);
    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;
}

WordPress Version: 6.3

/**
 * 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.
 *
 * @since 5.5.0
 * @since 6.1.0 Added `$index` parameter.
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @param int    $index      Optional. Index of the script to register when multiple items passed.
 *                           Default 0.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
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;
    }
    $script_asset_raw_path = dirname($metadata['file']) . '/' . 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;
    }
    // Path needs to be normalized to work in Windows env.
    static $wpinc_path_norm = '';
    if (!$wpinc_path_norm) {
        $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    }
    // Cache $template_path_norm and $stylesheet_path_norm to avoid unnecessary additional calls.
    static $template_path_norm = '';
    static $stylesheet_path_norm = '';
    if (!$template_path_norm || !$stylesheet_path_norm) {
        $template_path_norm = wp_normalize_path(get_template_directory());
        $stylesheet_path_norm = wp_normalize_path(get_stylesheet_directory());
    }
    $script_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $script_path));
    $is_core_block = isset($metadata['file']) && str_starts_with($metadata['file'], $wpinc_path_norm);
    /*
     * Determine if the block script was registered in a theme, by checking if the script path starts with either
     * the parent (template) or child (stylesheet) directory path.
     */
    $is_parent_theme_block = str_starts_with($script_path_norm, $template_path_norm);
    $is_child_theme_block = str_starts_with($script_path_norm, $stylesheet_path_norm);
    $is_theme_block = $is_parent_theme_block || $is_child_theme_block;
    $script_uri = plugins_url($script_path, $metadata['file']);
    if ($is_core_block) {
        $script_uri = includes_url(str_replace($wpinc_path_norm, '', $script_path_norm));
    } elseif ($is_theme_block) {
        // Get the script path deterministically based on whether or not it was registered in a parent or child theme.
        $script_uri = $is_parent_theme_block ? get_theme_file_uri(str_replace($template_path_norm, '', $script_path_norm)) : get_theme_file_uri(str_replace($stylesheet_path_norm, '', $script_path_norm));
    }
    $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);
    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;
}

WordPress Version: 6.2

/**
 * 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.
 *
 * @since 5.5.0
 * @since 6.1.0 Added `$index` parameter.
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @param int    $index      Optional. Index of the script to register when multiple items passed.
 *                           Default 0.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
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;
    }
    $script_asset_raw_path = dirname($metadata['file']) . '/' . 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;
    }
    // Path needs to be normalized to work in Windows env.
    static $wpinc_path_norm = '';
    if (!$wpinc_path_norm) {
        $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    }
    $theme_path_norm = wp_normalize_path(get_theme_file_path());
    $script_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $script_path));
    $is_core_block = isset($metadata['file']) && 0 === strpos($metadata['file'], $wpinc_path_norm);
    $is_theme_block = 0 === strpos($script_path_norm, $theme_path_norm);
    $script_uri = plugins_url($script_path, $metadata['file']);
    if ($is_core_block) {
        $script_uri = includes_url(str_replace($wpinc_path_norm, '', $script_path_norm));
    } elseif ($is_theme_block) {
        $script_uri = get_theme_file_uri(str_replace($theme_path_norm, '', $script_path_norm));
    }
    $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);
    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;
}

WordPress Version: 6.1

/**
 * 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.
 *
 * @since 5.5.0
 * @since 6.1.0 Added `$index` parameter.
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @param int    $index      Optional. Index of the script to register when multiple items passed.
 *                           Default 0.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
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;
    }
    $script_handle = generate_block_asset_handle($metadata['name'], $field_name, $index);
    $script_asset_path = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . substr_replace($script_path, '.asset.php', -strlen('.js'))));
    if (!file_exists($script_asset_path)) {
        _doing_it_wrong(__FUNCTION__, sprintf(
            /* translators: 1: Field name, 2: Block name. */
            __('The asset file for the "%1$s" defined in "%2$s" block definition is missing.'),
            $field_name,
            $metadata['name']
        ), '5.5.0');
        return false;
    }
    // Path needs to be normalized to work in Windows env.
    static $wpinc_path_norm = '';
    if (!$wpinc_path_norm) {
        $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    }
    $theme_path_norm = wp_normalize_path(get_theme_file_path());
    $script_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $script_path));
    $is_core_block = isset($metadata['file']) && 0 === strpos($metadata['file'], $wpinc_path_norm);
    $is_theme_block = 0 === strpos($script_path_norm, $theme_path_norm);
    $script_uri = plugins_url($script_path, $metadata['file']);
    if ($is_core_block) {
        $script_uri = includes_url(str_replace($wpinc_path_norm, '', $script_path_norm));
    } elseif ($is_theme_block) {
        $script_uri = get_theme_file_uri(str_replace($theme_path_norm, '', $script_path_norm));
    }
    $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);
    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;
}

WordPress Version: 9.3

/**
 * 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.
 *
 * @since 5.5.0
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
function register_block_script_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $script_handle = $metadata[$field_name];
    $script_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($script_handle === $script_path) {
        return $script_handle;
    }
    $script_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $script_asset_path = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . substr_replace($script_path, '.asset.php', -strlen('.js'))));
    if (!file_exists($script_asset_path)) {
        _doing_it_wrong(__FUNCTION__, sprintf(
            /* translators: 1: Field name, 2: Block name. */
            __('The asset file for the "%1$s" defined in "%2$s" block definition is missing.'),
            $field_name,
            $metadata['name']
        ), '5.5.0');
        return false;
    }
    // Path needs to be normalized to work in Windows env.
    $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    $script_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $script_path));
    $is_core_block = isset($metadata['file']) && 0 === strpos($metadata['file'], $wpinc_path_norm);
    $script_uri = $is_core_block ? includes_url(str_replace($wpinc_path_norm, '', $script_path_norm)) : plugins_url($script_path, $metadata['file']);
    $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);
    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;
}

WordPress Version: 5.9

/**
 * 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.
 *
 * @since 5.5.0
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
function register_block_script_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $script_handle = $metadata[$field_name];
    $script_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($script_handle === $script_path) {
        return $script_handle;
    }
    $script_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $script_asset_path = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . substr_replace($script_path, '.asset.php', -strlen('.js'))));
    if (!file_exists($script_asset_path)) {
        _doing_it_wrong(__FUNCTION__, sprintf(
            /* translators: 1: Field name, 2: Block name. */
            __('The asset file for the "%1$s" defined in "%2$s" block definition is missing.'),
            $field_name,
            $metadata['name']
        ), '5.5.0');
        return false;
    }
    // Path needs to be normalized to work in Windows env.
    $wpinc_path_norm = wp_normalize_path(ABSPATH . WPINC);
    $script_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $script_path));
    $is_core_block = isset($metadata['file']) && 0 === strpos($metadata['file'], $wpinc_path_norm);
    $script_uri = $is_core_block ? includes_url(str_replace($wpinc_path_norm, '', $script_path_norm)) : plugins_url($script_path, $metadata['file']);
    $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);
    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;
}

WordPress Version: 5.8

/**
 * 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.
 *
 * @since 5.5.0
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
function register_block_script_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $script_handle = $metadata[$field_name];
    $script_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($script_handle === $script_path) {
        return $script_handle;
    }
    $script_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $script_asset_path = realpath(dirname($metadata['file']) . '/' . substr_replace($script_path, '.asset.php', -strlen('.js')));
    if (!file_exists($script_asset_path)) {
        _doing_it_wrong(__FUNCTION__, sprintf(
            /* translators: 1: Field name, 2: Block name. */
            __('The asset file for the "%1$s" defined in "%2$s" block definition is missing.'),
            $field_name,
            $metadata['name']
        ), '5.5.0');
        return false;
    }
    $script_asset = require $script_asset_path;
    $result = wp_register_script($script_handle, plugins_url($script_path, $metadata['file']), $script_asset['dependencies'], $script_asset['version']);
    if (!$result) {
        return false;
    }
    if (!empty($metadata['textdomain'])) {
        wp_set_script_translations($script_handle, $metadata['textdomain']);
    }
    return $script_handle;
}

WordPress Version: 5.7

/**
 * 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.
 *
 * @since 5.5.0
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|false Script handle provided directly or created through
 *                      script's registration, or false on failure.
 */
function register_block_script_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $script_handle = $metadata[$field_name];
    $script_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($script_handle === $script_path) {
        return $script_handle;
    }
    $script_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $script_asset_path = realpath(dirname($metadata['file']) . '/' . substr_replace($script_path, '.asset.php', -strlen('.js')));
    if (!file_exists($script_asset_path)) {
        $message = sprintf(
            /* translators: %1: field name. %2: block name */
            __('The asset file for the "%1$s" defined in "%2$s" block definition is missing.', 'default'),
            $field_name,
            $metadata['name']
        );
        _doing_it_wrong(__FUNCTION__, $message, '5.5.0');
        return false;
    }
    $script_asset = require $script_asset_path;
    $result = wp_register_script($script_handle, plugins_url($script_path, $metadata['file']), $script_asset['dependencies'], $script_asset['version']);
    if (!$result) {
        return false;
    }
    if (!empty($metadata['textdomain'])) {
        wp_set_script_translations($script_handle, $metadata['textdomain']);
    }
    return $script_handle;
}

WordPress Version: 5.5

/**
 * 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.
 *
 * @since 5.5.0
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|bool Script handle provided directly or created through
 *                     script's registration, or false on failure.
 */
function register_block_script_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $script_handle = $metadata[$field_name];
    $script_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($script_handle === $script_path) {
        return $script_handle;
    }
    $script_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $script_asset_path = realpath(dirname($metadata['file']) . '/' . substr_replace($script_path, '.asset.php', -strlen('.js')));
    if (!file_exists($script_asset_path)) {
        $message = sprintf(
            /* translators: %1: field name. %2: block name */
            __('The asset file for the "%1$s" defined in "%2$s" block definition is missing.', 'default'),
            $field_name,
            $metadata['name']
        );
        _doing_it_wrong(__FUNCTION__, $message, '5.5.0');
        return false;
    }
    $script_asset = require $script_asset_path;
    $result = wp_register_script($script_handle, plugins_url($script_path, $metadata['file']), $script_asset['dependencies'], $script_asset['version']);
    return $result ? $script_handle : false;
}