Bez popisu

class-wp-theme-json-resolver.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. /**
  3. * WP_Theme_JSON_Resolver class
  4. *
  5. * @package WordPress
  6. * @subpackage Theme
  7. * @since 5.8.0
  8. */
  9. /**
  10. * Class that abstracts the processing of the different data sources
  11. * for site-level config and offers an API to work with them.
  12. *
  13. * @access private
  14. */
  15. class WP_Theme_JSON_Resolver {
  16. /**
  17. * Container for data coming from core.
  18. *
  19. * @since 5.8.0
  20. * @var WP_Theme_JSON
  21. */
  22. private static $core = null;
  23. /**
  24. * Container for data coming from the theme.
  25. *
  26. * @since 5.8.0
  27. * @var WP_Theme_JSON
  28. */
  29. private static $theme = null;
  30. /**
  31. * Whether or not the theme supports theme.json.
  32. *
  33. * @since 5.8.0
  34. * @var bool
  35. */
  36. private static $theme_has_support = null;
  37. /**
  38. * Structure to hold i18n metadata.
  39. *
  40. * @since 5.8.0
  41. * @var array
  42. */
  43. private static $theme_json_i18n = null;
  44. /**
  45. * Processes a file that adheres to the theme.json schema
  46. * and returns an array with its contents, or a void array if none found.
  47. *
  48. * @since 5.8.0
  49. *
  50. * @param string $file_path Path to file. Empty if no file.
  51. * @return array Contents that adhere to the theme.json schema.
  52. */
  53. private static function read_json_file( $file_path ) {
  54. $config = array();
  55. if ( $file_path ) {
  56. $decoded_file = json_decode(
  57. file_get_contents( $file_path ),
  58. true
  59. );
  60. $json_decoding_error = json_last_error();
  61. if ( JSON_ERROR_NONE !== $json_decoding_error ) {
  62. trigger_error( "Error when decoding a theme.json schema at path $file_path " . json_last_error_msg() );
  63. return $config;
  64. }
  65. if ( is_array( $decoded_file ) ) {
  66. $config = $decoded_file;
  67. }
  68. }
  69. return $config;
  70. }
  71. /**
  72. * Converts a tree as in i18n-theme.json into a linear array
  73. * containing metadata to translate a theme.json file.
  74. *
  75. * For example, given this input:
  76. *
  77. * {
  78. * "settings": {
  79. * "*": {
  80. * "typography": {
  81. * "fontSizes": [ { "name": "Font size name" } ],
  82. * "fontStyles": [ { "name": "Font size name" } ]
  83. * }
  84. * }
  85. * }
  86. * }
  87. *
  88. * will return this output:
  89. *
  90. * [
  91. * 0 => [
  92. * 'path' => [ 'settings', '*', 'typography', 'fontSizes' ],
  93. * 'key' => 'name',
  94. * 'context' => 'Font size name'
  95. * ],
  96. * 1 => [
  97. * 'path' => [ 'settings', '*', 'typography', 'fontStyles' ],
  98. * 'key' => 'name',
  99. * 'context' => 'Font style name'
  100. * ]
  101. * ]
  102. *
  103. * @since 5.8.0
  104. *
  105. * @param array $i18n_partial A tree that follows the format of i18n-theme.json.
  106. * @param array $current_path Optional. Keeps track of the path as we walk down the given tree.
  107. * Default empty array.
  108. * @return array A linear array containing the paths to translate.
  109. */
  110. private static function extract_paths_to_translate( $i18n_partial, $current_path = array() ) {
  111. $result = array();
  112. foreach ( $i18n_partial as $property => $partial_child ) {
  113. if ( is_numeric( $property ) ) {
  114. foreach ( $partial_child as $key => $context ) {
  115. $result[] = array(
  116. 'path' => $current_path,
  117. 'key' => $key,
  118. 'context' => $context,
  119. );
  120. }
  121. return $result;
  122. }
  123. $result = array_merge(
  124. $result,
  125. self::extract_paths_to_translate( $partial_child, array_merge( $current_path, array( $property ) ) )
  126. );
  127. }
  128. return $result;
  129. }
  130. /**
  131. * Returns a data structure used in theme.json translation.
  132. *
  133. * @since 5.8.0
  134. *
  135. * @return array An array of theme.json fields that are translatable and the keys that are translatable.
  136. */
  137. public static function get_fields_to_translate() {
  138. if ( null === self::$theme_json_i18n ) {
  139. $file_structure = self::read_json_file( __DIR__ . '/theme-i18n.json' );
  140. self::$theme_json_i18n = self::extract_paths_to_translate( $file_structure );
  141. }
  142. return self::$theme_json_i18n;
  143. }
  144. /**
  145. * Translates a chunk of the loaded theme.json structure.
  146. *
  147. * @since 5.8.0
  148. *
  149. * @param array $array_to_translate The chunk of theme.json to translate.
  150. * @param string $key The key of the field that contains the string to translate.
  151. * @param string $context The context to apply in the translation call.
  152. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  153. * @return array Returns the modified $theme_json chunk.
  154. */
  155. private static function translate_theme_json_chunk( array $array_to_translate, $key, $context, $domain ) {
  156. foreach ( $array_to_translate as $item_key => $item_to_translate ) {
  157. if ( empty( $item_to_translate[ $key ] ) ) {
  158. continue;
  159. }
  160. // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain
  161. $array_to_translate[ $item_key ][ $key ] = translate_with_gettext_context( $array_to_translate[ $item_key ][ $key ], $context, $domain );
  162. }
  163. return $array_to_translate;
  164. }
  165. /**
  166. * Given a theme.json structure modifies it in place to update certain values
  167. * by its translated strings according to the language set by the user.
  168. *
  169. * @since 5.8.0
  170. *
  171. * @param array $theme_json The theme.json to translate.
  172. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
  173. * Default 'default'.
  174. * @return array Returns the modified $theme_json_structure.
  175. */
  176. private static function translate( $theme_json, $domain = 'default' ) {
  177. $fields = self::get_fields_to_translate();
  178. foreach ( $fields as $field ) {
  179. $path = $field['path'];
  180. $key = $field['key'];
  181. $context = $field['context'];
  182. /*
  183. * We need to process the paths that include '*' separately.
  184. * One example of such a path would be:
  185. * [ 'settings', 'blocks', '*', 'color', 'palette' ]
  186. */
  187. $nodes_to_iterate = array_keys( $path, '*', true );
  188. if ( ! empty( $nodes_to_iterate ) ) {
  189. /*
  190. * At the moment, we only need to support one '*' in the path, so take it directly.
  191. * - base will be [ 'settings', 'blocks' ]
  192. * - data will be [ 'color', 'palette' ]
  193. */
  194. $base_path = array_slice( $path, 0, $nodes_to_iterate[0] );
  195. $data_path = array_slice( $path, $nodes_to_iterate[0] + 1 );
  196. $base_tree = _wp_array_get( $theme_json, $base_path, array() );
  197. foreach ( $base_tree as $node_name => $node_data ) {
  198. $array_to_translate = _wp_array_get( $node_data, $data_path, null );
  199. if ( is_null( $array_to_translate ) ) {
  200. continue;
  201. }
  202. // Whole path will be [ 'settings', 'blocks', 'core/paragraph', 'color', 'palette' ].
  203. $whole_path = array_merge( $base_path, array( $node_name ), $data_path );
  204. $translated_array = self::translate_theme_json_chunk( $array_to_translate, $key, $context, $domain );
  205. _wp_array_set( $theme_json, $whole_path, $translated_array );
  206. }
  207. } else {
  208. $array_to_translate = _wp_array_get( $theme_json, $path, null );
  209. if ( is_null( $array_to_translate ) ) {
  210. continue;
  211. }
  212. $translated_array = self::translate_theme_json_chunk( $array_to_translate, $key, $context, $domain );
  213. _wp_array_set( $theme_json, $path, $translated_array );
  214. }
  215. }
  216. return $theme_json;
  217. }
  218. /**
  219. * Return core's origin config.
  220. *
  221. * @since 5.8.0
  222. *
  223. * @return WP_Theme_JSON Entity that holds core data.
  224. */
  225. public static function get_core_data() {
  226. if ( null !== self::$core ) {
  227. return self::$core;
  228. }
  229. $config = self::read_json_file( __DIR__ . '/theme.json' );
  230. $config = self::translate( $config );
  231. self::$core = new WP_Theme_JSON( $config, 'core' );
  232. return self::$core;
  233. }
  234. /**
  235. * Returns the theme's data.
  236. *
  237. * Data from theme.json can be augmented via the $theme_support_data variable.
  238. * This is useful, for example, to backfill the gaps in theme.json that a theme
  239. * has declared via add_theme_supports.
  240. *
  241. * Note that if the same data is present in theme.json and in $theme_support_data,
  242. * the theme.json's is not overwritten.
  243. *
  244. * @since 5.8.0
  245. *
  246. * @param array $theme_support_data Optional. Theme support data in theme.json format.
  247. * Default empty array.
  248. * @return WP_Theme_JSON Entity that holds theme data.
  249. */
  250. public static function get_theme_data( $theme_support_data = array() ) {
  251. if ( null === self::$theme ) {
  252. $theme_json_data = self::read_json_file( self::get_file_path_from_theme( 'theme.json' ) );
  253. $theme_json_data = self::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
  254. self::$theme = new WP_Theme_JSON( $theme_json_data );
  255. }
  256. if ( empty( $theme_support_data ) ) {
  257. return self::$theme;
  258. }
  259. /*
  260. * We want the presets and settings declared in theme.json
  261. * to override the ones declared via add_theme_support.
  262. */
  263. $with_theme_supports = new WP_Theme_JSON( $theme_support_data );
  264. $with_theme_supports->merge( self::$theme );
  265. return $with_theme_supports;
  266. }
  267. /**
  268. * There are different sources of data for a site: core and theme.
  269. *
  270. * While the getters {@link get_core_data}, {@link get_theme_data} return the raw data
  271. * from the respective origins, this method merges them all together.
  272. *
  273. * If the same piece of data is declared in different origins (core and theme),
  274. * the last origin overrides the previous. For example, if core disables custom colors
  275. * but a theme enables them, the theme config wins.
  276. *
  277. * @since 5.8.0
  278. *
  279. * @param array $settings Optional. Existing block editor settings. Default empty array.
  280. * @return WP_Theme_JSON
  281. */
  282. public static function get_merged_data( $settings = array() ) {
  283. $theme_support_data = WP_Theme_JSON::get_from_editor_settings( $settings );
  284. $result = new WP_Theme_JSON();
  285. $result->merge( self::get_core_data() );
  286. $result->merge( self::get_theme_data( $theme_support_data ) );
  287. return $result;
  288. }
  289. /**
  290. * Whether the current theme has a theme.json file.
  291. *
  292. * @since 5.8.0
  293. *
  294. * @return bool
  295. */
  296. public static function theme_has_support() {
  297. if ( ! isset( self::$theme_has_support ) ) {
  298. self::$theme_has_support = (bool) self::get_file_path_from_theme( 'theme.json' );
  299. }
  300. return self::$theme_has_support;
  301. }
  302. /**
  303. * Builds the path to the given file and checks that it is readable.
  304. *
  305. * If it isn't, returns an empty string, otherwise returns the whole file path.
  306. *
  307. * @since 5.8.0
  308. *
  309. * @param string $file_name Name of the file.
  310. * @return string The whole file path or empty if the file doesn't exist.
  311. */
  312. private static function get_file_path_from_theme( $file_name ) {
  313. /*
  314. * This used to be a locate_template call. However, that method proved problematic
  315. * due to its use of constants (STYLESHEETPATH) that threw errors in some scenarios.
  316. *
  317. * When the theme.json merge algorithm properly supports child themes,
  318. * this should also fall back to the template path, as locate_template did.
  319. */
  320. $located = '';
  321. $candidate = get_stylesheet_directory() . '/' . $file_name;
  322. if ( is_readable( $candidate ) ) {
  323. $located = $candidate;
  324. }
  325. return $located;
  326. }
  327. /**
  328. * Cleans the cached data so it can be recalculated.
  329. *
  330. * @since 5.8.0
  331. */
  332. public static function clean_cached_data() {
  333. self::$core = null;
  334. self::$theme = null;
  335. self::$theme_has_support = null;
  336. self::$theme_json_i18n = null;
  337. }
  338. }