wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



wp_generate_custom_css_class_name › WordPress Function

Seit7.2.0
Veraltetn/v
› wp_generate_custom_css_class_name ( $parsed_block )
Zugriff:
  • private
Parameter:
  • (array) $parsed_block The parsed block.
    Erforderlich: Ja
Gibt zurück:
  • (string|null) The generated class name, or null if the block has no custom CSS to render.
Definiert in:
Codex:

Generates the custom CSS class name for a block and registers its stylesheet.



Quellcode

function wp_generate_custom_css_class_name( $parsed_block ) {
	$style = $parsed_block['attrs']['style'] ?? null;
	if ( empty( $style ) || ! is_array( $style ) ) {
		return null;
	}

	$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );
	if ( ! block_has_support( $block_type, 'customCSS', true ) ) {
		return null;
	}

	$viewport_settings        = wp_get_global_settings( array( 'viewport' ) );
	$responsive_media_queries = WP_Theme_JSON::get_viewport_media_queries( $viewport_settings );
	$state_entries            = wp_get_custom_css_state_entries( $style, $parsed_block['blockName'], $responsive_media_queries );
	if ( empty( $state_entries ) ) {
		return null;
	}

	// Generate a unique class name for this block instance.
	$class_name    = wp_unique_id_from_values( $parsed_block, 'wp-custom-css-' );
	$processed_css = wp_process_custom_css_state_entries( $state_entries, '.' . $class_name );
	if ( '' === $processed_css ) {
		return null;
	}

	/**
	 * Reuse one handle so identical custom CSS is enqueued only once via
	 * {@see wp_unique_id_from_values()}. Explicitly declare the `wp-block-library`
	 * dependency so `global-styles` is guaranteed to print after it, preventing
	 * block default styles from unintentionally overriding global styles.
	 */
	$handle = 'wp-block-custom-css';
	if ( ! wp_style_is( $handle, 'registered' ) ) {
		wp_register_style( $handle, false, array( 'wp-block-library', 'global-styles' ) );
	}
	$after_styles = wp_styles()->get_data( $handle, 'after' );
	if ( ! is_array( $after_styles ) ) {
		$after_styles = array();
	}
	if ( ! in_array( $processed_css, $after_styles, true ) ) {
		wp_add_inline_style( $handle, $processed_css );
	}

	return $class_name;
}