WP_Theme_JSON::get_settings_values_by_slug( array $settings, array $preset_metadata, array $origins )

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


Description

$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',
// );

Top ↑

Parameters

$settings

(array) (Required) Settings to process.

$preset_metadata

(array) (Required) One of the PRESETS_METADATA values.

$origins

(array) (Required) List of origins to process.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/class-wp-theme-json.php

	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;
	}


Top ↑

Changelog

Changelog
Version Description
5.9.0 Introduced.

Top ↑

User Contributed Notes

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