WP_Theme_JSON::get_feature_declarations_for_node( object $metadata, object $node ): array

In this article

Generates style declarations for a node’s features e.g., color, border, typography etc. that have custom selectors in their related block’s metadata.

Parameters

$metadataobjectrequired
The related block metadata containing selectors.
$nodeobjectrequired
A merged theme.json node for block or variation.

Return

array The style declarations for the node’s features with custom selectors.

Source

			: ( $spacing_scale['increment'] >= 1 ? $current_step * $spacing_scale['increment'] : $current_step / $spacing_scale['increment'] );

		$above_sizes[] = array(
			/* translators: %s: Digit to indicate multiple of sizing, eg. 2X-Large. */
			'name' => 0 === $above_midpoint_count ? __( 'Large' ) : sprintf( __( '%sX-Large' ), (string) $x_large_count ),
			'slug' => (string) $slug,
			'size' => round( $current_step, 2 ) . $unit,
		);

		if ( 1 === $above_midpoint_count ) {
			$x_large_count = 2;
		}

		if ( $above_midpoint_count > 1 ) {
			++$x_large_count;
		}

		$slug += 10;
	}

	$spacing_sizes = $below_sizes;
	foreach ( $above_sizes as $above_sizes_item ) {
		$spacing_sizes[] = $above_sizes_item;
	}

	// If there are 7 or fewer steps in the scale revert to numbers for labels instead of t-shirt sizes.
	if ( $spacing_scale['steps'] <= 7 ) {
		for ( $spacing_sizes_count = 0; $spacing_sizes_count < count( $spacing_sizes ); $spacing_sizes_count++ ) {
			$spacing_sizes[ $spacing_sizes_count ]['name'] = (string) ( $spacing_sizes_count + 1 );
		}
	}

	_wp_array_set( $this->theme_json, array( 'settings', 'spacing', 'spacingSizes', 'default' ), $spacing_sizes );
}

/**
 * This is used to convert the internal representation of variables to the CSS representation.
 * For example, `var:preset|color|vivid-green-cyan` becomes `var(--wp--preset--color--vivid-green-cyan)`.
 *
 * @since 6.3.0
 * @param string $value The variable such as var:preset|color|vivid-green-cyan to convert.
 * @return string The converted variable.
 */
private static function convert_custom_properties( $value ) {
	$prefix     = 'var:';
	$prefix_len = strlen( $prefix );
	$token_in   = '|';
	$token_out  = '--';
	if ( str_starts_with( $value, $prefix ) ) {
		$unwrapped_name = str_replace(
			$token_in,
			$token_out,
			substr( $value, $prefix_len )
		);
		$value          = "var(--wp--$unwrapped_name)";
	}

	return $value;
}

/**
 * Given a tree, converts the internal representation of variables to the CSS representation.
 * It is recursive and modifies the input in-place.
 *
 * @since 6.3.0
 * @param array $tree   Input to process.
 * @return array The modified $tree.
 */
private static function resolve_custom_css_format( $tree ) {
	$prefix = 'var:';

	foreach ( $tree as $key => $data ) {
		if ( is_string( $data ) && str_starts_with( $data, $prefix ) ) {
			$tree[ $key ] = self::convert_custom_properties( $data );
		} elseif ( is_array( $data ) ) {
			$tree[ $key ] = self::resolve_custom_css_format( $data );
		}
	}

	return $tree;
}

/**
 * Returns the selectors metadata for a block.
 *
 * @since 6.3.0
 *
 * @param object $block_type    The block type.
 * @param string $root_selector The block's root selector.
 *
 * @return array The custom selectors set by the block.
 */
protected static function get_block_selectors( $block_type, $root_selector ) {
	if ( ! empty( $block_type->selectors ) ) {
		return $block_type->selectors;
	}

Changelog

VersionDescription
6.3.0Introduced.

User Contributed Notes

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