[ 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      $tree = WP_Theme_JSON_Resolver::get_merged_data();
 107  
 108      $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support();
 109      if ( empty( $types ) && ! $supports_theme_json ) {
 110          $types = array( 'variables', 'presets' );
 111      } elseif ( empty( $types ) ) {
 112          $types = array( 'variables', 'styles', 'presets' );
 113      }
 114  
 115      /*
 116       * If variables are part of the stylesheet,
 117       * we add them for all origins (default, theme, user).
 118       * This is so themes without a theme.json still work as before 5.9:
 119       * they can override the default presets.
 120       * See https://core.trac.wordpress.org/ticket/54782
 121       */
 122      $styles_variables = '';
 123      if ( in_array( 'variables', $types, true ) ) {
 124          $styles_variables = $tree->get_stylesheet( array( 'variables' ) );
 125          $types            = array_diff( $types, array( 'variables' ) );
 126      }
 127  
 128      /*
 129       * For the remaining types (presets, styles), we do consider origins:
 130       *
 131       * - themes without theme.json: only the classes for the presets defined by core
 132       * - themes with theme.json: the presets and styles classes, both from core and the theme
 133       */
 134      $styles_rest = '';
 135      if ( ! empty( $types ) ) {
 136          $origins = array( 'default', 'theme', 'custom' );
 137          if ( ! $supports_theme_json ) {
 138              $origins = array( 'default' );
 139          }
 140          $styles_rest = $tree->get_stylesheet( $types, $origins );
 141      }
 142  
 143      $stylesheet = $styles_variables . $styles_rest;
 144  
 145      if ( $can_use_cached ) {
 146          // Cache for a minute.
 147          // This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites.
 148          set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS );
 149      }
 150  
 151      return $stylesheet;
 152  }
 153  
 154  /**
 155   * Returns a string containing the SVGs to be referenced as filters (duotone).
 156   *
 157   * @since 5.9.1
 158   *
 159   * @return string
 160   */
 161  function wp_get_global_styles_svg_filters() {
 162      // Return cached value if it can be used and exists.
 163      // It's cached by theme to make sure that theme switching clears the cache.
 164      $can_use_cached = (
 165          ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
 166          ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
 167          ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
 168          ! is_admin()
 169      );
 170      $transient_name = 'global_styles_svg_filters_' . get_stylesheet();
 171      if ( $can_use_cached ) {
 172          $cached = get_transient( $transient_name );
 173          if ( $cached ) {
 174              return $cached;
 175          }
 176      }
 177  
 178      $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support();
 179  
 180      $origins = array( 'default', 'theme', 'custom' );
 181      if ( ! $supports_theme_json ) {
 182          $origins = array( 'default' );
 183      }
 184  
 185      $tree = WP_Theme_JSON_Resolver::get_merged_data();
 186      $svgs = $tree->get_svg_filters( $origins );
 187  
 188      if ( $can_use_cached ) {
 189          // Cache for a minute, same as wp_get_global_stylesheet.
 190          set_transient( $transient_name, $svgs, MINUTE_IN_SECONDS );
 191      }
 192  
 193      return $svgs;
 194  }


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1