WP_Theme_JSON::get_settings_values_by_slug() – Gets preset values keyed by slugs based on settings and metadata.

You appear to be a bot. Output may be restricted

Description

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

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

Usage

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

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.

Returns

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

Source

File name: wordpress/wp-includes/class-wp-theme-json.php
Lines:

1 to 31 of 31
  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;
  }
 

 View on GitHub View on Trac