[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> global-styles-and-settings.php (source)

   1  <?php
   2  /**
   3   * APIs to interact with global settings & styles.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Function to get the settings resulting of merging core, theme, and user data.
  10   *
  11   * @since 5.9.0
  12   *
  13   * @param array $path    Path to the specific setting to retrieve. Optional.
  14   *                       If empty, will return all settings.
  15   * @param array $context {
  16   *     Metadata to know where to retrieve the $path from. Optional.
  17   *
  18   *     @type string $block_name Which block to retrieve the settings from.
  19   *                              If empty, it'll return the settings for the global context.
  20   *     @type string $origin     Which origin to take data from.
  21   *                              Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
  22   *                              If empty or unknown, 'all' is used.
  23   * }
  24   *
  25   * @return array The settings to retrieve.
  26   */
  27  function wp_get_global_settings( $path = array(), $context = array() ) {
  28      if ( ! empty( $context['block_name'] ) ) {
  29          $path = array_merge( array( 'blocks', $context['block_name'] ), $path );
  30      }
  31  
  32      $origin = 'custom';
  33      if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
  34          $origin = 'theme';
  35      }
  36  
  37      $settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
  38  
  39      return _wp_array_get( $settings, $path, $settings );
  40  }
  41  
  42  /**
  43   * Function to get the styles resulting of merging core, theme, and user data.
  44   *
  45   * @since 5.9.0
  46   *
  47   * @param array $path    Path to the specific style to retrieve. Optional.
  48   *                       If empty, will return all styles.
  49   * @param array $context {
  50   *     Metadata to know where to retrieve the $path from. Optional.
  51   *
  52   *     @type string $block_name Which block to retrieve the styles from.
  53   *                              If empty, it'll return the styles for the global context.
  54   *     @type string $origin     Which origin to take data from.
  55   *                              Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
  56   *                              If empty or unknown, 'all' is used.
  57   * }
  58   *
  59   * @return array The styles to retrieve.
  60   */
  61  function wp_get_global_styles( $path = array(), $context = array() ) {
  62      if ( ! empty( $context['block_name'] ) ) {
  63          $path = array_merge( array( 'blocks', $context['block_name'] ), $path );
  64      }
  65  
  66      $origin = 'custom';
  67      if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
  68          $origin = 'theme';
  69      }
  70  
  71      $styles = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles'];
  72  
  73      return _wp_array_get( $styles, $path, $styles );
  74  }
  75  
  76  /**
  77   * Returns the stylesheet resulting of merging core, theme, and user data.
  78   *
  79   * @since 5.9.0
  80   *
  81   * @param array $types Types of styles to load. Optional.
  82   *                     It accepts 'variables', 'styles', 'presets' as values.
  83   *                     If empty, it'll load all for themes with theme.json support
  84   *                     and only [ 'variables', 'presets' ] for themes without theme.json support.
  85   *
  86   * @return string Stylesheet.
  87   */
  88  function wp_get_global_stylesheet( $types = array() ) {
  89      // Return cached value if it can be used and exists.
  90      // It's cached by theme to make sure that theme switching clears the cache.
  91      $can_use_cached = (
  92          ( empty( $types ) ) &&
  93          ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
  94          ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
  95          ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
  96          ! is_admin()
  97      );
  98      $transient_name = 'global_styles_' . get_stylesheet();
  99      if ( $can_use_cached ) {
 100          $cached = get_transient( $transient_name );
 101          if ( $cached ) {
 102              return $cached;
 103          }
 104      }
 105  
 106      $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support();
 107      $supports_link_color = get_theme_support( 'experimental-link-color' );
 108      if ( empty( $types ) && ! $supports_theme_json ) {
 109          $types = array( 'variables', 'presets' );
 110      } elseif ( empty( $types ) ) {
 111          $types = array( 'variables', 'styles', 'presets' );
 112      }
 113  
 114      $origins = array( 'default', 'theme', 'custom' );
 115      if ( ! $supports_theme_json && ! $supports_link_color ) {
 116          // In this case we only enqueue the core presets (CSS Custom Properties + the classes).
 117          $origins = array( 'default' );
 118      } elseif ( ! $supports_theme_json && $supports_link_color ) {
 119          // For the legacy link color feature to work, the CSS Custom Properties
 120          // should be in scope (either the core or the theme ones).
 121          $origins = array( 'default', 'theme' );
 122      }
 123  
 124      $tree       = WP_Theme_JSON_Resolver::get_merged_data();
 125      $stylesheet = $tree->get_stylesheet( $types, $origins );
 126  
 127      if ( $can_use_cached ) {
 128          // Cache for a minute.
 129          // This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites.
 130          set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS );
 131      }
 132  
 133      return $stylesheet;
 134  }


Generated: Wed Jan 19 01:00:04 2022 Cross-referenced by PHPXref 0.7.1