register_block_style_handle

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

WordPress Version: 6.4

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style 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 style to register when multiple items passed.
 *                           Default 0.
 * @return string|false Style handle provided directly or created through
 *                      style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name, $index = 0)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $style_handle = $metadata[$field_name];
    if (is_array($style_handle)) {
        if (empty($style_handle[$index])) {
            return false;
        }
        $style_handle = $style_handle[$index];
    }
    $style_handle_name = generate_block_asset_handle($metadata['name'], $field_name, $index);
    // If the style handle is already registered, skip re-registering.
    if (wp_style_is($style_handle_name, 'registered')) {
        return $style_handle_name;
    }
    static $wpinc_path_norm = '';
    if (!$wpinc_path_norm) {
        $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    }
    $is_core_block = isset($metadata['file']) && str_starts_with($metadata['file'], $wpinc_path_norm);
    // Skip registering individual styles for each core block when a bundled version provided.
    if ($is_core_block && !wp_should_load_separate_core_block_assets()) {
        return false;
    }
    $style_path = remove_block_asset_path_prefix($style_handle);
    $is_style_handle = $style_handle === $style_path;
    // Allow only passing style handles for core blocks.
    if ($is_core_block && !$is_style_handle) {
        return false;
    }
    // Return the style handle unless it's the first item for every core block that requires special treatment.
    if ($is_style_handle && !($is_core_block && 0 === $index)) {
        return $style_handle;
    }
    // Check whether styles should have a ".min" suffix or not.
    $suffix = SCRIPT_DEBUG ? '' : '.min';
    if ($is_core_block) {
        $style_path = ('editorStyle' === $field_name) ? "editor{$suffix}.css" : "style{$suffix}.css";
    }
    $style_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $style_path));
    $style_uri = get_block_asset_url($style_path_norm);
    $version = (!$is_core_block && isset($metadata['version'])) ? $metadata['version'] : false;
    $result = wp_register_style($style_handle_name, $style_uri, array(), $version);
    if (!$result) {
        return false;
    }
    if ($style_uri) {
        wp_style_add_data($style_handle_name, 'path', $style_path_norm);
        if ($is_core_block) {
            $rtl_file = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm);
        } else {
            $rtl_file = str_replace('.css', '-rtl.css', $style_path_norm);
        }
        if (is_rtl() && file_exists($rtl_file)) {
            wp_style_add_data($style_handle_name, 'rtl', 'replace');
            wp_style_add_data($style_handle_name, 'suffix', $suffix);
            wp_style_add_data($style_handle_name, 'path', $rtl_file);
        }
    }
    return $style_handle_name;
}

WordPress Version: 3.2

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style 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 style to register when multiple items passed.
 *                           Default 0.
 * @return string|false Style handle provided directly or created through
 *                      style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name, $index = 0)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $style_handle = $metadata[$field_name];
    if (is_array($style_handle)) {
        if (empty($style_handle[$index])) {
            return false;
        }
        $style_handle = $style_handle[$index];
    }
    $style_handle_name = generate_block_asset_handle($metadata['name'], $field_name, $index);
    // If the style handle is already registered, skip re-registering.
    if (wp_style_is($style_handle_name, 'registered')) {
        return $style_handle_name;
    }
    static $wpinc_path_norm = '';
    if (!$wpinc_path_norm) {
        $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    }
    $is_core_block = isset($metadata['file']) && str_starts_with($metadata['file'], $wpinc_path_norm);
    // Skip registering individual styles for each core block when a bundled version provided.
    if ($is_core_block && !wp_should_load_separate_core_block_assets()) {
        return false;
    }
    $style_path = remove_block_asset_path_prefix($style_handle);
    $is_style_handle = $style_handle === $style_path;
    // Allow only passing style handles for core blocks.
    if ($is_core_block && !$is_style_handle) {
        return false;
    }
    // Return the style handle unless it's the first item for every core block that requires special treatment.
    if ($is_style_handle && !($is_core_block && 0 === $index)) {
        return $style_handle;
    }
    // Check whether styles should have a ".min" suffix or not.
    $suffix = SCRIPT_DEBUG ? '' : '.min';
    if ($is_core_block) {
        $style_path = ('editorStyle' === $field_name) ? "editor{$suffix}.css" : "style{$suffix}.css";
    }
    $style_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $style_path));
    $has_style_file = '' !== $style_path_norm;
    if ($has_style_file) {
        $style_uri = plugins_url($style_path, $metadata['file']);
        // 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());
        }
        // Determine if the block style 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($style_path_norm, trailingslashit($template_path_norm));
        $is_child_theme_block = str_starts_with($style_path_norm, trailingslashit($stylesheet_path_norm));
        $is_theme_block = $is_parent_theme_block || $is_child_theme_block;
        if ($is_core_block) {
            // All possible $style_path variants for core blocks are hard-coded above.
            $style_uri = includes_url('blocks/' . str_replace('core/', '', $metadata['name']) . '/' . $style_path);
        } elseif ($is_theme_block) {
            // Get the script path deterministically based on whether or not it was registered in a parent or child theme.
            $style_uri = $is_parent_theme_block ? get_theme_file_uri(str_replace($template_path_norm, '', $style_path_norm)) : get_theme_file_uri(str_replace($stylesheet_path_norm, '', $style_path_norm));
        }
    } else {
        $style_uri = false;
    }
    $version = (!$is_core_block && isset($metadata['version'])) ? $metadata['version'] : false;
    $result = wp_register_style($style_handle_name, $style_uri, array(), $version);
    if (!$result) {
        return false;
    }
    if ($has_style_file) {
        wp_style_add_data($style_handle_name, 'path', $style_path_norm);
        if ($is_core_block) {
            $rtl_file = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm);
        } else {
            $rtl_file = str_replace('.css', '-rtl.css', $style_path_norm);
        }
        if (is_rtl() && file_exists($rtl_file)) {
            wp_style_add_data($style_handle_name, 'rtl', 'replace');
            wp_style_add_data($style_handle_name, 'suffix', $suffix);
            wp_style_add_data($style_handle_name, 'path', $rtl_file);
        }
    }
    return $style_handle_name;
}

WordPress Version: 6.3

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style 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 style to register when multiple items passed.
 *                           Default 0.
 * @return string|false Style handle provided directly or created through
 *                      style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name, $index = 0)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $style_handle = $metadata[$field_name];
    if (is_array($style_handle)) {
        if (empty($style_handle[$index])) {
            return false;
        }
        $style_handle = $style_handle[$index];
    }
    $style_handle_name = generate_block_asset_handle($metadata['name'], $field_name, $index);
    // If the style handle is already registered, skip re-registering.
    if (wp_style_is($style_handle_name, 'registered')) {
        return $style_handle_name;
    }
    static $wpinc_path_norm = '';
    if (!$wpinc_path_norm) {
        $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    }
    $is_core_block = isset($metadata['file']) && str_starts_with($metadata['file'], $wpinc_path_norm);
    // Skip registering individual styles for each core block when a bundled version provided.
    if ($is_core_block && !wp_should_load_separate_core_block_assets()) {
        return false;
    }
    $style_path = remove_block_asset_path_prefix($style_handle);
    $is_style_handle = $style_handle === $style_path;
    // Allow only passing style handles for core blocks.
    if ($is_core_block && !$is_style_handle) {
        return false;
    }
    // Return the style handle unless it's the first item for every core block that requires special treatment.
    if ($is_style_handle && !($is_core_block && 0 === $index)) {
        return $style_handle;
    }
    // Check whether styles should have a ".min" suffix or not.
    $suffix = SCRIPT_DEBUG ? '' : '.min';
    if ($is_core_block) {
        $style_path = ('editorStyle' === $field_name) ? "editor{$suffix}.css" : "style{$suffix}.css";
    }
    $style_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $style_path));
    $has_style_file = '' !== $style_path_norm;
    if ($has_style_file) {
        $style_uri = plugins_url($style_path, $metadata['file']);
        // 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());
        }
        // Determine if the block style 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($style_path_norm, $template_path_norm);
        $is_child_theme_block = str_starts_with($style_path_norm, $stylesheet_path_norm);
        $is_theme_block = $is_parent_theme_block || $is_child_theme_block;
        if ($is_core_block) {
            // All possible $style_path variants for core blocks are hard-coded above.
            $style_uri = includes_url('blocks/' . str_replace('core/', '', $metadata['name']) . '/' . $style_path);
        } elseif ($is_theme_block) {
            // Get the script path deterministically based on whether or not it was registered in a parent or child theme.
            $style_uri = $is_parent_theme_block ? get_theme_file_uri(str_replace($template_path_norm, '', $style_path_norm)) : get_theme_file_uri(str_replace($stylesheet_path_norm, '', $style_path_norm));
        }
    } else {
        $style_uri = false;
    }
    $version = (!$is_core_block && isset($metadata['version'])) ? $metadata['version'] : false;
    $result = wp_register_style($style_handle_name, $style_uri, array(), $version);
    if (!$result) {
        return false;
    }
    if ($has_style_file) {
        wp_style_add_data($style_handle_name, 'path', $style_path_norm);
        if ($is_core_block) {
            $rtl_file = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm);
        } else {
            $rtl_file = str_replace('.css', '-rtl.css', $style_path_norm);
        }
        if (is_rtl() && file_exists($rtl_file)) {
            wp_style_add_data($style_handle_name, 'rtl', 'replace');
            wp_style_add_data($style_handle_name, 'suffix', $suffix);
            wp_style_add_data($style_handle_name, 'path', $rtl_file);
        }
    }
    return $style_handle_name;
}

WordPress Version: 2.1

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style 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 style to register when multiple items passed.
 *                           Default 0.
 * @return string|false Style handle provided directly or created through
 *                      style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name, $index = 0)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    static $wpinc_path_norm = '';
    if (!$wpinc_path_norm) {
        $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    }
    $is_core_block = isset($metadata['file']) && 0 === strpos($metadata['file'], $wpinc_path_norm);
    // Skip registering individual styles for each core block when a bundled version provided.
    if ($is_core_block && !wp_should_load_separate_core_block_assets()) {
        return false;
    }
    $style_handle = $metadata[$field_name];
    if (is_array($style_handle)) {
        if (empty($style_handle[$index])) {
            return false;
        }
        $style_handle = $style_handle[$index];
    }
    $style_path = remove_block_asset_path_prefix($style_handle);
    $is_style_handle = $style_handle === $style_path;
    // Allow only passing style handles for core blocks.
    if ($is_core_block && !$is_style_handle) {
        return false;
    }
    // Return the style handle unless it's the first item for every core block that requires special treatment.
    if ($is_style_handle && !($is_core_block && 0 === $index)) {
        return $style_handle;
    }
    // Check whether styles should have a ".min" suffix or not.
    $suffix = SCRIPT_DEBUG ? '' : '.min';
    if ($is_core_block) {
        $style_path = "style{$suffix}.css";
    }
    $style_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $style_path));
    $has_style_file = '' !== $style_path_norm;
    if ($has_style_file) {
        $style_uri = plugins_url($style_path, $metadata['file']);
        // Cache $theme_path_norm to avoid calling get_theme_file_path() multiple times.
        static $theme_path_norm = '';
        if (!$theme_path_norm) {
            $theme_path_norm = wp_normalize_path(get_theme_file_path());
        }
        $is_theme_block = str_starts_with($style_path_norm, $theme_path_norm);
        if ($is_theme_block) {
            $style_uri = get_theme_file_uri(str_replace($theme_path_norm, '', $style_path_norm));
        } elseif ($is_core_block) {
            $style_uri = includes_url('blocks/' . str_replace('core/', '', $metadata['name']) . "/style{$suffix}.css");
        }
    } else {
        $style_uri = false;
    }
    $style_handle = generate_block_asset_handle($metadata['name'], $field_name, $index);
    $version = (!$is_core_block && isset($metadata['version'])) ? $metadata['version'] : false;
    $result = wp_register_style($style_handle, $style_uri, array(), $version);
    if (!$result) {
        return false;
    }
    if ($has_style_file) {
        wp_style_add_data($style_handle, 'path', $style_path_norm);
        if ($is_core_block) {
            $rtl_file = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm);
        } else {
            $rtl_file = str_replace('.css', '-rtl.css', $style_path_norm);
        }
        if (is_rtl() && file_exists($rtl_file)) {
            wp_style_add_data($style_handle, 'rtl', 'replace');
            wp_style_add_data($style_handle, 'suffix', $suffix);
            wp_style_add_data($style_handle, 'path', $rtl_file);
        }
    }
    return $style_handle;
}

WordPress Version: 6.1

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style 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 style to register when multiple items passed.
 *                           Default 0.
 * @return string|false Style handle provided directly or created through
 *                      style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name, $index = 0)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    static $wpinc_path_norm = '';
    if (!$wpinc_path_norm) {
        $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    }
    $is_core_block = isset($metadata['file']) && 0 === strpos($metadata['file'], $wpinc_path_norm);
    // Skip registering individual styles for each core block when a bundled version provided.
    if ($is_core_block && !wp_should_load_separate_core_block_assets()) {
        return false;
    }
    $style_handle = $metadata[$field_name];
    if (is_array($style_handle)) {
        if (empty($style_handle[$index])) {
            return false;
        }
        $style_handle = $style_handle[$index];
    }
    $style_path = remove_block_asset_path_prefix($style_handle);
    $is_style_handle = $style_handle === $style_path;
    // Allow only passing style handles for core blocks.
    if ($is_core_block && !$is_style_handle) {
        return false;
    }
    // Return the style handle unless it's the first item for every core block that requires special treatment.
    if ($is_style_handle && !($is_core_block && 0 === $index)) {
        return $style_handle;
    }
    // Check whether styles should have a ".min" suffix or not.
    $suffix = SCRIPT_DEBUG ? '' : '.min';
    if ($is_core_block) {
        $style_path = "style{$suffix}.css";
    }
    $style_path_norm = wp_normalize_path(realpath(dirname($metadata['file']) . '/' . $style_path));
    $has_style_file = '' !== $style_path_norm;
    if ($has_style_file) {
        $style_uri = plugins_url($style_path, $metadata['file']);
        // Cache $theme_path_norm to avoid calling get_theme_file_path() multiple times.
        static $theme_path_norm = '';
        if (!$theme_path_norm) {
            $theme_path_norm = wp_normalize_path(get_theme_file_path());
        }
        $is_theme_block = str_starts_with($style_path_norm, $theme_path_norm);
        if ($is_theme_block) {
            $style_uri = get_theme_file_uri(str_replace($theme_path_norm, '', $style_path_norm));
        } elseif ($is_core_block) {
            $style_uri = includes_url('blocks/' . str_replace('core/', '', $metadata['name']) . "/style{$suffix}.css");
        }
    } else {
        $style_uri = false;
    }
    $style_handle = generate_block_asset_handle($metadata['name'], $field_name, $index);
    $version = (!$is_core_block && isset($metadata['version'])) ? $metadata['version'] : false;
    $result = wp_register_style($style_handle, $style_uri, array(), $version);
    if (!$result) {
        return false;
    }
    if ($has_style_file) {
        wp_style_add_data($style_handle, 'path', $style_path_norm);
        $rtl_file = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm);
        if (is_rtl() && file_exists($rtl_file)) {
            wp_style_add_data($style_handle, 'rtl', 'replace');
            wp_style_add_data($style_handle, 'suffix', $suffix);
            wp_style_add_data($style_handle, 'path', $rtl_file);
        }
    }
    return $style_handle;
}

WordPress Version: 9.3

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style handle otherwise.
 *
 * @since 5.5.0
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|false Style handle provided directly or created through
 *                      style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $wpinc_path_norm = wp_normalize_path(realpath(ABSPATH . WPINC));
    $is_core_block = isset($metadata['file']) && 0 === strpos($metadata['file'], $wpinc_path_norm);
    if ($is_core_block && !wp_should_load_separate_core_block_assets()) {
        return false;
    }
    // Check whether styles should have a ".min" suffix or not.
    $suffix = SCRIPT_DEBUG ? '' : '.min';
    $style_handle = $metadata[$field_name];
    $style_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($style_handle === $style_path && !$is_core_block) {
        return $style_handle;
    }
    $style_uri = plugins_url($style_path, $metadata['file']);
    if ($is_core_block) {
        $style_path = "style{$suffix}.css";
        $style_uri = includes_url('blocks/' . str_replace('core/', '', $metadata['name']) . "/style{$suffix}.css");
    }
    $style_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $block_dir = dirname($metadata['file']);
    $style_file = realpath("{$block_dir}/{$style_path}");
    $has_style_file = false !== $style_file;
    $version = (!$is_core_block && isset($metadata['version'])) ? $metadata['version'] : false;
    $style_uri = $has_style_file ? $style_uri : false;
    $result = wp_register_style($style_handle, $style_uri, array(), $version);
    if (file_exists(str_replace('.css', '-rtl.css', $style_file))) {
        wp_style_add_data($style_handle, 'rtl', 'replace');
    }
    if ($has_style_file) {
        wp_style_add_data($style_handle, 'path', $style_file);
    }
    $rtl_file = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $style_file);
    if (is_rtl() && file_exists($rtl_file)) {
        wp_style_add_data($style_handle, 'path', $rtl_file);
    }
    return $result ? $style_handle : false;
}

WordPress Version: 5.9

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style handle otherwise.
 *
 * @since 5.5.0
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|false Style handle provided directly or created through
 *                      style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $wpinc_path_norm = wp_normalize_path(ABSPATH . WPINC);
    $is_core_block = isset($metadata['file']) && 0 === strpos($metadata['file'], $wpinc_path_norm);
    if ($is_core_block && !wp_should_load_separate_core_block_assets()) {
        return false;
    }
    // Check whether styles should have a ".min" suffix or not.
    $suffix = SCRIPT_DEBUG ? '' : '.min';
    $style_handle = $metadata[$field_name];
    $style_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($style_handle === $style_path && !$is_core_block) {
        return $style_handle;
    }
    $style_uri = plugins_url($style_path, $metadata['file']);
    if ($is_core_block) {
        $style_path = "style{$suffix}.css";
        $style_uri = includes_url('blocks/' . str_replace('core/', '', $metadata['name']) . "/style{$suffix}.css");
    }
    $style_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $block_dir = dirname($metadata['file']);
    $style_file = realpath("{$block_dir}/{$style_path}");
    $has_style_file = false !== $style_file;
    $version = (!$is_core_block && isset($metadata['version'])) ? $metadata['version'] : false;
    $style_uri = $has_style_file ? $style_uri : false;
    $result = wp_register_style($style_handle, $style_uri, array(), $version);
    if (file_exists(str_replace('.css', '-rtl.css', $style_file))) {
        wp_style_add_data($style_handle, 'rtl', 'replace');
    }
    if ($has_style_file) {
        wp_style_add_data($style_handle, 'path', $style_file);
    }
    $rtl_file = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $style_file);
    if (is_rtl() && file_exists($rtl_file)) {
        wp_style_add_data($style_handle, 'path', $rtl_file);
    }
    return $result ? $style_handle : false;
}

WordPress Version: 5.8

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style handle otherwise.
 *
 * @since 5.5.0
 *
 * @param array  $metadata   Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|false Style handle provided directly or created through
 *                      style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $is_core_block = isset($metadata['file']) && 0 === strpos($metadata['file'], ABSPATH . WPINC);
    if ($is_core_block && !wp_should_load_separate_core_block_assets()) {
        return false;
    }
    // Check whether styles should have a ".min" suffix or not.
    $suffix = SCRIPT_DEBUG ? '' : '.min';
    $style_handle = $metadata[$field_name];
    $style_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($style_handle === $style_path && !$is_core_block) {
        return $style_handle;
    }
    $style_uri = plugins_url($style_path, $metadata['file']);
    if ($is_core_block) {
        $style_path = "style{$suffix}.css";
        $style_uri = includes_url('blocks/' . str_replace('core/', '', $metadata['name']) . "/style{$suffix}.css");
    }
    $style_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $block_dir = dirname($metadata['file']);
    $style_file = realpath("{$block_dir}/{$style_path}");
    $has_style_file = false !== $style_file;
    $version = (!$is_core_block && isset($metadata['version'])) ? $metadata['version'] : false;
    $style_uri = $has_style_file ? $style_uri : false;
    $result = wp_register_style($style_handle, $style_uri, array(), $version);
    if (file_exists(str_replace('.css', '-rtl.css', $style_file))) {
        wp_style_add_data($style_handle, 'rtl', 'replace');
    }
    if ($has_style_file) {
        wp_style_add_data($style_handle, 'path', $style_file);
    }
    $rtl_file = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $style_file);
    if (is_rtl() && file_exists($rtl_file)) {
        wp_style_add_data($style_handle, 'path', $rtl_file);
    }
    return $result ? $style_handle : false;
}

WordPress Version: 5.7

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style handle otherwise.
 *
 * @since 5.5.0
 *
 * @param array  $metadata Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|false Style handle provided directly or created through
 *                      style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $style_handle = $metadata[$field_name];
    $style_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($style_handle === $style_path) {
        return $style_handle;
    }
    $style_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $block_dir = dirname($metadata['file']);
    $style_file = realpath("{$block_dir}/{$style_path}");
    $result = wp_register_style($style_handle, plugins_url($style_path, $metadata['file']), array(), filemtime($style_file));
    if (file_exists(str_replace('.css', '-rtl.css', $style_file))) {
        wp_style_add_data($style_handle, 'rtl', 'replace');
    }
    return $result ? $style_handle : false;
}

WordPress Version: 5.5

/**
 * Finds a style handle for the block metadata field. It detects when a path
 * to file was provided and registers the style under automatically
 * generated handle name. It returns unprocessed style handle otherwise.
 *
 * @since 5.5.0
 *
 * @param array  $metadata Block metadata.
 * @param string $field_name Field name to pick from metadata.
 * @return string|boolean Style handle provided directly or created through
 *                        style's registration, or false on failure.
 */
function register_block_style_handle($metadata, $field_name)
{
    if (empty($metadata[$field_name])) {
        return false;
    }
    $style_handle = $metadata[$field_name];
    $style_path = remove_block_asset_path_prefix($metadata[$field_name]);
    if ($style_handle === $style_path) {
        return $style_handle;
    }
    $style_handle = generate_block_asset_handle($metadata['name'], $field_name);
    $block_dir = dirname($metadata['file']);
    $result = wp_register_style($style_handle, plugins_url($style_path, $metadata['file']), array(), filemtime(realpath("{$block_dir}/{$style_path}")));
    return $result ? $style_handle : false;
}