global-styles-and-settings.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * APIs to interact with global settings & styles.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Gets 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. * @return array The settings to retrieve.
  24. */
  25. function wp_get_global_settings( $path = array(), $context = array() ) {
  26. if ( ! empty( $context['block_name'] ) ) {
  27. $path = array_merge( array( 'blocks', $context['block_name'] ), $path );
  28. }
  29. $origin = 'custom';
  30. if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
  31. $origin = 'theme';
  32. }
  33. $settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
  34. return _wp_array_get( $settings, $path, $settings );
  35. }
  36. /**
  37. * Gets the styles resulting of merging core, theme, and user data.
  38. *
  39. * @since 5.9.0
  40. *
  41. * @param array $path Path to the specific style to retrieve. Optional.
  42. * If empty, will return all styles.
  43. * @param array $context {
  44. * Metadata to know where to retrieve the $path from. Optional.
  45. *
  46. * @type string $block_name Which block to retrieve the styles from.
  47. * If empty, it'll return the styles for the global context.
  48. * @type string $origin Which origin to take data from.
  49. * Valid values are 'all' (core, theme, and user) or 'base' (core and theme).
  50. * If empty or unknown, 'all' is used.
  51. * }
  52. * @return array The styles to retrieve.
  53. */
  54. function wp_get_global_styles( $path = array(), $context = array() ) {
  55. if ( ! empty( $context['block_name'] ) ) {
  56. $path = array_merge( array( 'blocks', $context['block_name'] ), $path );
  57. }
  58. $origin = 'custom';
  59. if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
  60. $origin = 'theme';
  61. }
  62. $styles = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles'];
  63. return _wp_array_get( $styles, $path, $styles );
  64. }
  65. /**
  66. * Returns the stylesheet resulting of merging core, theme, and user data.
  67. *
  68. * @since 5.9.0
  69. *
  70. * @param array $types Types of styles to load. Optional.
  71. * It accepts 'variables', 'styles', 'presets' as values.
  72. * If empty, it'll load all for themes with theme.json support
  73. * and only [ 'variables', 'presets' ] for themes without theme.json support.
  74. * @return string Stylesheet.
  75. */
  76. function wp_get_global_stylesheet( $types = array() ) {
  77. // Return cached value if it can be used and exists.
  78. // It's cached by theme to make sure that theme switching clears the cache.
  79. $can_use_cached = (
  80. ( empty( $types ) ) &&
  81. ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
  82. ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
  83. ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
  84. ! is_admin()
  85. );
  86. $transient_name = 'global_styles_' . get_stylesheet();
  87. if ( $can_use_cached ) {
  88. $cached = get_transient( $transient_name );
  89. if ( $cached ) {
  90. return $cached;
  91. }
  92. }
  93. $tree = WP_Theme_JSON_Resolver::get_merged_data();
  94. $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support();
  95. if ( empty( $types ) && ! $supports_theme_json ) {
  96. $types = array( 'variables', 'presets', 'base-layout-styles' );
  97. } elseif ( empty( $types ) ) {
  98. $types = array( 'variables', 'styles', 'presets' );
  99. }
  100. /*
  101. * If variables are part of the stylesheet, then add them.
  102. * This is so themes without a theme.json still work as before 5.9:
  103. * they can override the default presets.
  104. * See https://core.trac.wordpress.org/ticket/54782
  105. */
  106. $styles_variables = '';
  107. if ( in_array( 'variables', $types, true ) ) {
  108. /*
  109. * Only use the default, theme, and custom origins. Why?
  110. * Because styles for `blocks` origin are added at a later phase
  111. * (i.e. in the render cycle). Here, only the ones in use are rendered.
  112. * @see wp_add_global_styles_for_blocks
  113. */
  114. $origins = array( 'default', 'theme', 'custom' );
  115. $styles_variables = $tree->get_stylesheet( array( 'variables' ), $origins );
  116. $types = array_diff( $types, array( 'variables' ) );
  117. }
  118. /*
  119. * For the remaining types (presets, styles), we do consider origins:
  120. *
  121. * - themes without theme.json: only the classes for the presets defined by core
  122. * - themes with theme.json: the presets and styles classes, both from core and the theme
  123. */
  124. $styles_rest = '';
  125. if ( ! empty( $types ) ) {
  126. /*
  127. * Only use the default, theme, and custom origins. Why?
  128. * Because styles for `blocks` origin are added at a later phase
  129. * (i.e. in the render cycle). Here, only the ones in use are rendered.
  130. * @see wp_add_global_styles_for_blocks
  131. */
  132. $origins = array( 'default', 'theme', 'custom' );
  133. if ( ! $supports_theme_json ) {
  134. $origins = array( 'default' );
  135. }
  136. $styles_rest = $tree->get_stylesheet( $types, $origins );
  137. }
  138. $stylesheet = $styles_variables . $styles_rest;
  139. if ( $can_use_cached ) {
  140. // Cache for a minute.
  141. // This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites.
  142. set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS );
  143. }
  144. return $stylesheet;
  145. }
  146. /**
  147. * Returns a string containing the SVGs to be referenced as filters (duotone).
  148. *
  149. * @since 5.9.1
  150. *
  151. * @return string
  152. */
  153. function wp_get_global_styles_svg_filters() {
  154. // Return cached value if it can be used and exists.
  155. // It's cached by theme to make sure that theme switching clears the cache.
  156. $can_use_cached = (
  157. ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
  158. ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
  159. ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
  160. ! is_admin()
  161. );
  162. $transient_name = 'global_styles_svg_filters_' . get_stylesheet();
  163. if ( $can_use_cached ) {
  164. $cached = get_transient( $transient_name );
  165. if ( $cached ) {
  166. return $cached;
  167. }
  168. }
  169. $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support();
  170. $origins = array( 'default', 'theme', 'custom' );
  171. if ( ! $supports_theme_json ) {
  172. $origins = array( 'default' );
  173. }
  174. $tree = WP_Theme_JSON_Resolver::get_merged_data();
  175. $svgs = $tree->get_svg_filters( $origins );
  176. if ( $can_use_cached ) {
  177. // Cache for a minute, same as wp_get_global_stylesheet.
  178. set_transient( $transient_name, $svgs, MINUTE_IN_SECONDS );
  179. }
  180. return $svgs;
  181. }
  182. /**
  183. * Adds global style rules to the inline style for each block.
  184. *
  185. * @since 6.1.0
  186. */
  187. function wp_add_global_styles_for_blocks() {
  188. $tree = WP_Theme_JSON_Resolver::get_merged_data();
  189. $block_nodes = $tree->get_styles_block_nodes();
  190. foreach ( $block_nodes as $metadata ) {
  191. $block_css = $tree->get_styles_for_block( $metadata );
  192. if ( ! wp_should_load_separate_core_block_assets() ) {
  193. wp_add_inline_style( 'global-styles', $block_css );
  194. continue;
  195. }
  196. $stylesheet_handle = 'global-styles';
  197. if ( isset( $metadata['name'] ) ) {
  198. /*
  199. * These block styles are added on block_render.
  200. * This hooks inline CSS to them so that they are loaded conditionally
  201. * based on whether or not the block is used on the page.
  202. */
  203. if ( str_starts_with( $metadata['name'], 'core/' ) ) {
  204. $block_name = str_replace( 'core/', '', $metadata['name'] );
  205. $stylesheet_handle = 'wp-block-' . $block_name;
  206. }
  207. wp_add_inline_style( $stylesheet_handle, $block_css );
  208. }
  209. // The likes of block element styles from theme.json do not have $metadata['name'] set.
  210. if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) {
  211. $result = array_values(
  212. array_filter(
  213. $metadata['path'],
  214. function ( $item ) {
  215. if ( strpos( $item, 'core/' ) !== false ) {
  216. return true;
  217. }
  218. return false;
  219. }
  220. )
  221. );
  222. if ( isset( $result[0] ) ) {
  223. if ( str_starts_with( $result[0], 'core/' ) ) {
  224. $block_name = str_replace( 'core/', '', $result[0] );
  225. $stylesheet_handle = 'wp-block-' . $block_name;
  226. }
  227. wp_add_inline_style( $stylesheet_handle, $block_css );
  228. }
  229. }
  230. }
  231. }