WP_Theme_JSON::get_layout_styles( array $block_metadata ): string

In this article

Gets the CSS layout rules for a particular block from theme.json layout definitions.

Parameters

$block_metadataarrayrequired
Metadata about the block to get styles for.

Return

string Layout styles for the block.

Source

					}
				}
			} else {
				// If associative, process as a single object.
				$tree[ $key ] = self::remove_keys_not_in_schema( $value, $schema[ $key ] );

				if ( empty( $tree[ $key ] ) ) {
					unset( $tree[ $key ] );
				}
			}
		}
	}
	return $tree;
}

/**
 * Returns the existing settings for each block.
 *
 * Example:
 *
 *     {
 *       'root': {
 *         'color': {
 *           'custom': true
 *         }
 *       },
 *       'core/paragraph': {
 *         'spacing': {
 *           'customPadding': true
 *         }
 *       }
 *     }
 *
 * @since 5.8.0
 *
 * @return array Settings per block.
 */
public function get_settings() {
	if ( ! isset( $this->theme_json['settings'] ) ) {
		return array();
	} else {
		return $this->theme_json['settings'];
	}
}

/**
 * Returns the stylesheet that results of processing
 * the theme.json structure this object represents.
 *
 * @since 5.8.0
 * @since 5.9.0 Removed the `$type` parameter, added the `$types` and `$origins` parameters.
 * @since 6.3.0 Add fallback layout styles for Post Template when block gap support isn't available.
 * @since 6.6.0 Added boolean `skip_root_layout_styles` and `include_block_style_variations` options
 *              to control styles output as desired.
 *
 * @param string[] $types   Types of styles to load. Will load all by default. It accepts:
 *                          - `variables`: only the CSS Custom Properties for presets & custom ones.
 *                          - `styles`: only the styles section in theme.json.
 *                          - `presets`: only the classes for the presets.
 * @param string[] $origins A list of origins to include. By default it includes VALID_ORIGINS.
 * @param array    $options {
 *     Optional. An array of options for now used for internal purposes only (may change without notice).
 *
 *     @type string $scope                           Makes sure all style are scoped to a given selector
 *     @type string $root_selector                   Overwrites and forces a given selector to be used on the root node
 *     @type bool   $skip_root_layout_styles         Omits root layout styles from the generated stylesheet. Default false.
 *     @type bool   $include_block_style_variations  Includes styles for block style variations in the generated stylesheet. Default false.
 * }
 * @return string The resulting stylesheet.
 */
public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' ), $origins = null, $options = array() ) {
	if ( null === $origins ) {
		$origins = static::VALID_ORIGINS;
	}

	if ( is_string( $types ) ) {
		// Dispatch error and map old arguments to new ones.
		_deprecated_argument( __FUNCTION__, '5.9.0' );
		if ( 'block_styles' === $types ) {
			$types = array( 'styles', 'presets' );
		} elseif ( 'css_variables' === $types ) {
			$types = array( 'variables' );
		} else {
			$types = array( 'variables', 'styles', 'presets' );
		}
	}

	$blocks_metadata = static::get_blocks_metadata();
	$style_nodes     = static::get_style_nodes( $this->theme_json, $blocks_metadata, $options );
	$setting_nodes   = static::get_setting_nodes( $this->theme_json, $blocks_metadata );

	$root_style_key    = array_search( static::ROOT_BLOCK_SELECTOR, array_column( $style_nodes, 'selector' ), true );
	$root_settings_key = array_search( static::ROOT_BLOCK_SELECTOR, array_column( $setting_nodes, 'selector' ), true );

	if ( ! empty( $options['scope'] ) ) {
		foreach ( $setting_nodes as &$node ) {
			$node['selector'] = static::scope_selector( $options['scope'], $node['selector'] );
		}
		foreach ( $style_nodes as &$node ) {
			$node = static::scope_style_node_selectors( $options['scope'], $node );
		}
		unset( $node );
	}

	if ( ! empty( $options['root_selector'] ) ) {
		if ( false !== $root_settings_key ) {
			$setting_nodes[ $root_settings_key ]['selector'] = $options['root_selector'];
		}
		if ( false !== $root_style_key ) {
			$style_nodes[ $root_style_key ]['selector'] = $options['root_selector'];
		}
	}

	$stylesheet = '';

	if ( in_array( 'variables', $types, true ) ) {
		$stylesheet .= $this->get_css_variables( $setting_nodes, $origins );
	}

	if ( in_array( 'styles', $types, true ) ) {
		if ( false !== $root_style_key && empty( $options['skip_root_layout_styles'] ) ) {
			$stylesheet .= $this->get_root_layout_rules( $style_nodes[ $root_style_key ]['selector'], $style_nodes[ $root_style_key ] );
		}
		$stylesheet .= $this->get_block_classes( $style_nodes );
	} elseif ( in_array( 'base-layout-styles', $types, true ) ) {
		$root_selector          = static::ROOT_BLOCK_SELECTOR;
		$columns_selector       = '.wp-block-columns';
		$post_template_selector = '.wp-block-post-template';
		if ( ! empty( $options['scope'] ) ) {
			$root_selector          = static::scope_selector( $options['scope'], $root_selector );
			$columns_selector       = static::scope_selector( $options['scope'], $columns_selector );
			$post_template_selector = static::scope_selector( $options['scope'], $post_template_selector );
		}
		if ( ! empty( $options['root_selector'] ) ) {
			$root_selector = $options['root_selector'];
		}
		/*
		 * Base layout styles are provided as part of `styles`, so only output separately if explicitly requested.
		 * For backwards compatibility, the Columns block is explicitly included, to support a different default gap value.
		 */
		$base_styles_nodes = array(
			array(
				'path'     => array( 'styles' ),
				'selector' => $root_selector,
			),
			array(
				'path'     => array( 'styles', 'blocks', 'core/columns' ),
				'selector' => $columns_selector,
				'name'     => 'core/columns',
			),
			array(
				'path'     => array( 'styles', 'blocks', 'core/post-template' ),
				'selector' => $post_template_selector,
				'name'     => 'core/post-template',
			),
		);

		foreach ( $base_styles_nodes as $base_style_node ) {
			$stylesheet .= $this->get_layout_styles( $base_style_node, $types );
		}
	}

	if ( in_array( 'presets', $types, true ) ) {
		$stylesheet .= $this->get_preset_classes( $setting_nodes, $origins );
	}

	// Load the custom CSS last so it has the highest specificity.
	if ( in_array( 'custom-css', $types, true ) ) {
		// Add the global styles root CSS.
		$stylesheet .= _wp_array_get( $this->theme_json, array( 'styles', 'css' ) );
	}

	return $stylesheet;
}

/**

Changelog

VersionDescription
6.3.0Reduced specificity for layout margin rules.
6.1.0Introduced.

User Contributed Notes

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