Нет описания

global-styles-and-settings.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * APIs to interact with global settings & styles.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Function to get the settings resulting of merging core, theme, and user data.
  9. *
  10. * @since 5.9.0
  11. *
  12. * @param array $path Path to the specific setting to retrieve. Optional.
  13. * If empty, will return all settings.
  14. * @param array $context {
  15. * Metadata to know where to retrieve the $path from. Optional.
  16. *
  17. * @type string $block_name Which block to retrieve the settings from.
  18. * If empty, it'll return the settings for the global context.
  19. * @type string $origin Which origin to take data from.
  20. * Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
  21. * If empty or unknown, 'all' is used.
  22. * }
  23. *
  24. * @return array The settings to retrieve.
  25. */
  26. function wp_get_global_settings( $path = array(), $context = array() ) {
  27. if ( ! empty( $context['block_name'] ) ) {
  28. $path = array_merge( array( 'blocks', $context['block_name'] ), $path );
  29. }
  30. $origin = 'custom';
  31. if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
  32. $origin = 'theme';
  33. }
  34. $settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
  35. return _wp_array_get( $settings, $path, $settings );
  36. }
  37. /**
  38. * Function to get the styles resulting of merging core, theme, and user data.
  39. *
  40. * @since 5.9.0
  41. *
  42. * @param array $path Path to the specific style to retrieve. Optional.
  43. * If empty, will return all styles.
  44. * @param array $context {
  45. * Metadata to know where to retrieve the $path from. Optional.
  46. *
  47. * @type string $block_name Which block to retrieve the styles from.
  48. * If empty, it'll return the styles for the global context.
  49. * @type string $origin Which origin to take data from.
  50. * Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
  51. * If empty or unknown, 'all' is used.
  52. * }
  53. *
  54. * @return array The styles to retrieve.
  55. */
  56. function wp_get_global_styles( $path = array(), $context = array() ) {
  57. if ( ! empty( $context['block_name'] ) ) {
  58. $path = array_merge( array( 'blocks', $context['block_name'] ), $path );
  59. }
  60. $origin = 'custom';
  61. if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
  62. $origin = 'theme';
  63. }
  64. $styles = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles'];
  65. return _wp_array_get( $styles, $path, $styles );
  66. }
  67. /**
  68. * Returns the stylesheet resulting of merging core, theme, and user data.
  69. *
  70. * @since 5.9.0
  71. *
  72. * @param array $types Types of styles to load. Optional.
  73. * It accepts 'variables', 'styles', 'presets' as values.
  74. * If empty, it'll load all for themes with theme.json support
  75. * and only [ 'variables', 'presets' ] for themes without theme.json support.
  76. *
  77. * @return string Stylesheet.
  78. */
  79. function wp_get_global_stylesheet( $types = array() ) {
  80. // Return cached value if it can be used and exists.
  81. // It's cached by theme to make sure that theme switching clears the cache.
  82. $can_use_cached = (
  83. ( empty( $types ) ) &&
  84. ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
  85. ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
  86. ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
  87. ! is_admin()
  88. );
  89. $transient_name = 'global_styles_' . get_stylesheet();
  90. if ( $can_use_cached ) {
  91. $cached = get_transient( $transient_name );
  92. if ( $cached ) {
  93. return $cached;
  94. }
  95. }
  96. $tree = WP_Theme_JSON_Resolver::get_merged_data();
  97. $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support();
  98. if ( empty( $types ) && ! $supports_theme_json ) {
  99. $types = array( 'variables', 'presets' );
  100. } elseif ( empty( $types ) ) {
  101. $types = array( 'variables', 'styles', 'presets' );
  102. }
  103. /*
  104. * If variables are part of the stylesheet,
  105. * we add them for all origins (default, theme, user).
  106. * This is so themes without a theme.json still work as before 5.9:
  107. * they can override the default presets.
  108. * See https://core.trac.wordpress.org/ticket/54782
  109. */
  110. $styles_variables = '';
  111. if ( in_array( 'variables', $types, true ) ) {
  112. $styles_variables = $tree->get_stylesheet( array( 'variables' ) );
  113. $types = array_diff( $types, array( 'variables' ) );
  114. }
  115. /*
  116. * For the remaining types (presets, styles), we do consider origins:
  117. *
  118. * - themes without theme.json: only the classes for the presets defined by core
  119. * - themes with theme.json: the presets and styles classes, both from core and the theme
  120. */
  121. $styles_rest = '';
  122. if ( ! empty( $types ) ) {
  123. $origins = array( 'default', 'theme', 'custom' );
  124. if ( ! $supports_theme_json ) {
  125. $origins = array( 'default' );
  126. }
  127. $styles_rest = $tree->get_stylesheet( $types, $origins );
  128. }
  129. $stylesheet = $styles_variables . $styles_rest;
  130. if ( $can_use_cached ) {
  131. // Cache for a minute.
  132. // This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites.
  133. set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS );
  134. }
  135. return $stylesheet;
  136. }
  137. /**
  138. * Returns a string containing the SVGs to be referenced as filters (duotone).
  139. *
  140. * @since 5.9.1
  141. *
  142. * @return string
  143. */
  144. function wp_get_global_styles_svg_filters() {
  145. // Return cached value if it can be used and exists.
  146. // It's cached by theme to make sure that theme switching clears the cache.
  147. $can_use_cached = (
  148. ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
  149. ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
  150. ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
  151. ! is_admin()
  152. );
  153. $transient_name = 'global_styles_svg_filters_' . get_stylesheet();
  154. if ( $can_use_cached ) {
  155. $cached = get_transient( $transient_name );
  156. if ( $cached ) {
  157. return $cached;
  158. }
  159. }
  160. $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support();
  161. $origins = array( 'default', 'theme', 'custom' );
  162. if ( ! $supports_theme_json ) {
  163. $origins = array( 'default' );
  164. }
  165. $tree = WP_Theme_JSON_Resolver::get_merged_data();
  166. $svgs = $tree->get_svg_filters( $origins );
  167. if ( $can_use_cached ) {
  168. // Cache for a minute, same as wp_get_global_stylesheet.
  169. set_transient( $transient_name, $svgs, MINUTE_IN_SECONDS );
  170. }
  171. return $svgs;
  172. }