WP_Theme_JSON::get_root_layout_rules( string $selector, array $block_metadata ): string

In this article

Outputs the CSS for layout rules on the root.

Parameters

$selectorstringrequired
The root node selector.
$block_metadataarrayrequired
The metadata for the root block.

Return

string The additional root rules CSS.

Source

		$clean_style_variation_selector = trim( $style_variation['selector'] );

		// Generate any feature/subfeature style declarations for the current style variation.
		$variation_declarations = static::get_feature_declarations_for_node( $block_metadata, $style_variation_node );

		// Combine selectors with style variation's selector and add to overall style variation declarations.
		foreach ( $variation_declarations as $current_selector => $new_declarations ) {
			// If current selector includes block classname, remove it but leave the whitespace in.
			$shortened_selector = str_replace( $block_metadata['selector'] . ' ', ' ', $current_selector );

			// Prepend the variation selector to the current selector.
			$split_selectors    = explode( ',', $shortened_selector );
			$updated_selectors  = array_map(
				static function ( $split_selector ) use ( $clean_style_variation_selector ) {
					return $clean_style_variation_selector . $split_selector;
				},
				$split_selectors
			);
			$combined_selectors = implode( ',', $updated_selectors );

			// Add the new declarations to the overall results under the modified selector.
			$style_variation_declarations[ $combined_selectors ] = $new_declarations;
		}

		// Compute declarations for remaining styles not covered by feature level selectors.
		$style_variation_declarations[ $style_variation['selector'] ] = static::compute_style_properties( $style_variation_node, $settings, null, $this->theme_json );
	}
}
/*
 * Get a reference to element name from path.
 * $block_metadata['path'] = array( 'styles','elements','link' );
 * Make sure that $block_metadata['path'] describes an element node, like [ 'styles', 'element', 'link' ].
 * Skip non-element paths like just ['styles'].
 */
$is_processing_element = in_array( 'elements', $block_metadata['path'], true );

$current_element = $is_processing_element ? $block_metadata['path'][ count( $block_metadata['path'] ) - 1 ] : null;

$element_pseudo_allowed = array();

if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ] ) ) {
	$element_pseudo_allowed = static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ];
}

/*
 * Check for allowed pseudo classes (e.g. ":hover") from the $selector ("a:hover").
 * This also resets the array keys.
 */
$pseudo_matches = array_values(
	array_filter(
		$element_pseudo_allowed,
		static function ( $pseudo_selector ) use ( $selector ) {
			return str_contains( $selector, $pseudo_selector );
		}
	)
);

$pseudo_selector = isset( $pseudo_matches[0] ) ? $pseudo_matches[0] : null;

/*
 * If the current selector is a pseudo selector that's defined in the allow list for the current
 * element then compute the style properties for it.
 * Otherwise just compute the styles for the default selector as normal.
 */
if ( $pseudo_selector && isset( $node[ $pseudo_selector ] ) &&
	isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ] )

Changelog

VersionDescription
6.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.