wpseek.com
A WordPress-centric search engine for devs and theme authors



register_block_style_handle › WordPress Function

Since5.5.0
Deprecatedn/a
register_block_style_handle ( $metadata, $field_name, $index = 0 )
Parameters: (3)
  • (array) $metadata Block metadata.
    Required: Yes
  • (string) $field_name Field name to pick from metadata.
    Required: Yes
  • (int) $index Optional. Index of the style to register when multiple items passed. Default 0.
    Required: No
    Default:
Returns:
  • (string|false) Style handle provided directly or created through style's registration, or false on failure.
Defined at:
Codex:
Change Log:
  • 6.1.0

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.



Source

function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
	if ( empty( $metadata[ $field_name ] ) ) {
		return false;
	}

	$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
	$theme_path_norm = wp_normalize_path( get_theme_file_path() );
	$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';
	$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_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
	$is_theme_block  = 0 === strpos( $style_path_norm, $theme_path_norm );

	if ( $is_theme_block ) {
		$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
	}

	$style_handle   = generate_block_asset_handle( $metadata['name'], $field_name, $index );
	$has_style_file = false !== $style_path_norm;
	$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_path_norm ) ) ) {
		wp_style_add_data( $style_handle, 'rtl', 'replace' );
	}
	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, 'path', $rtl_file );
	}

	return $result ? $style_handle : false;
}