WP_Theme_JSON::get_settings_values_by_slug()protected staticWP 5.9.0

Gets preset values keyed by slugs based on settings and metadata.

$settings = array(
	'typography' => array(
		'fontFamilies' => array(
			array(
				'slug'       => 'sansSerif',
				'fontFamily' => '"Helvetica Neue", sans-serif',
			),
			array(
				'slug'   => 'serif',
				'colors' => 'Georgia, serif',
			)
		),
	),
);
$meta = array(
   'path'      => array( 'typography', 'fontFamilies' ),
   'value_key' => 'fontFamily',
);
$values_by_slug = get_settings_values_by_slug();
// $values_by_slug === array(
//   'sans-serif' => '"Helvetica Neue", sans-serif',
//   'serif'      => 'Georgia, serif',
// );

{} It's a method of the class: WP_Theme_JSON{}

No Hooks.

Return

Array. Array of presets where each key is a slug and each value is the preset value.

Usage

$result = WP_Theme_JSON::get_settings_values_by_slug( $settings, $preset_metadata, $origins );
$settings(array) (required)
Settings to process.
$preset_metadata(array) (required)
One of the PRESETS_METADATA values.
$origins(array) (required)
List of origins to process.

Changelog

Since 5.9.0 Introduced.

Code of WP_Theme_JSON::get_settings_values_by_slug() WP 6.0

protected static function get_settings_values_by_slug( $settings, $preset_metadata, $origins ) {
	$preset_per_origin = _wp_array_get( $settings, $preset_metadata['path'], array() );

	$result = array();
	foreach ( $origins as $origin ) {
		if ( ! isset( $preset_per_origin[ $origin ] ) ) {
			continue;
		}
		foreach ( $preset_per_origin[ $origin ] as $preset ) {
			$slug = _wp_to_kebab_case( $preset['slug'] );

			$value = '';
			if ( isset( $preset_metadata['value_key'], $preset[ $preset_metadata['value_key'] ] ) ) {
				$value_key = $preset_metadata['value_key'];
				$value     = $preset[ $value_key ];
			} elseif (
				isset( $preset_metadata['value_func'] ) &&
				is_callable( $preset_metadata['value_func'] )
			) {
				$value_func = $preset_metadata['value_func'];
				$value      = call_user_func( $value_func, $preset );
			} else {
				// If we don't have a value, then don't add it to the result.
				continue;
			}

			$result[ $slug ] = $value;
		}
	}
	return $result;
}