Changeset 50967
- Timestamp:
- 05/24/2021 01:24:05 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json-resolver.php
r50959 r50967 1 1 <?php 2 2 /** 3 * Process the different data sources for site-level 4 * config and offers and API to work with them. 3 * WP_Theme_JSON_Resolver class 5 4 * 6 5 * @package WordPress 6 * @subpackage Theme 7 * @since 5.8.0 7 8 */ 8 9 9 10 /** 10 * Class that abstracts the processing 11 * of the different data sources.11 * Class that abstracts the processing of the different data sources 12 * for site-level config and offers an API to work with them. 12 13 * 13 14 * @access private … … 18 19 * Container for data coming from core. 19 20 * 21 * @since 5.8.0 20 22 * @var WP_Theme_JSON 21 23 */ … … 25 27 * Container for data coming from the theme. 26 28 * 29 * @since 5.8.0 27 30 * @var WP_Theme_JSON 28 31 */ … … 32 35 * Whether or not the theme supports theme.json. 33 36 * 34 * @var boolean 37 * @since 5.8.0 38 * @var bool 35 39 */ 36 40 private static $theme_has_support = null; … … 39 43 * Structure to hold i18n metadata. 40 44 * 41 * @var Array 45 * @since 5.8.0 46 * @var array 42 47 */ 43 48 private static $theme_json_i18n = null; 44 49 45 50 /** 46 * Processes a file that adheres to the theme.json 47 * schema and returns an array with its contents, 48 * or a void array if none found. 51 * Processes a file that adheres to the theme.json schema 52 * and returns an array with its contents, or a void array if none found. 53 * 54 * @since 5.8.0 49 55 * 50 56 * @param string $file_path Path to file. Empty if no file. 51 *52 57 * @return array Contents that adhere to the theme.json schema. 53 58 */ … … 79 84 * For example, given this input: 80 85 * 81 * { 82 * "settings": { 83 * "*": { 84 * "typography": { 85 * "fontSizes": [ { "name": "Font size name" } ], 86 * "fontStyles": [ { "name": "Font size name" } ] 86 * { 87 * "settings": { 88 * "*": { 89 * "typography": { 90 * "fontSizes": [ { "name": "Font size name" } ], 91 * "fontStyles": [ { "name": "Font size name" } ] 92 * } 93 * } 87 94 * } 88 95 * } 89 * }90 * }91 96 * 92 97 * will return this output: 93 98 * 94 * [ 95 * 0 => [ 96 * 'path' => [ 'settings', '*', 'typography', 'fontSizes' ], 97 * 'key' => 'name', 98 * 'context' => 'Font size name' 99 * ], 100 * 1 => [ 101 * 'path' => [ 'settings', '*', 'typography', 'fontStyles' ], 102 * 'key' => 'name', 103 * 'context' => 'Font style name' 104 * ] 105 * ] 99 * [ 100 * 0 => [ 101 * 'path' => [ 'settings', '*', 'typography', 'fontSizes' ], 102 * 'key' => 'name', 103 * 'context' => 'Font size name' 104 * ], 105 * 1 => [ 106 * 'path' => [ 'settings', '*', 'typography', 'fontStyles' ], 107 * 'key' => 'name', 108 * 'context' => 'Font style name' 109 * ] 110 * ] 111 * 112 * @since 5.8.0 106 113 * 107 114 * @param array $i18n_partial A tree that follows the format of i18n-theme.json. 108 115 * @param array $current_path Keeps track of the path as we walk down the given tree. 109 *110 116 * @return array A linear array containing the paths to translate. 111 117 */ … … 135 141 * Returns a data structure used in theme.json translation. 136 142 * 137 * @return array An array of theme.json fields that are translatable and the keys that are translatable 143 * @since 5.8.0 144 * 145 * @return array An array of theme.json fields that are translatable and the keys that are translatable. 138 146 */ 139 147 public static function get_fields_to_translate() { … … 148 156 * Translates a chunk of the loaded theme.json structure. 149 157 * 158 * @since 5.8.0 159 * 150 160 * @param array $array_to_translate The chunk of theme.json to translate. 151 161 * @param string $key The key of the field that contains the string to translate. 152 162 * @param string $context The context to apply in the translation call. 153 163 * @param string $domain Text domain. Unique identifier for retrieving translated strings. 154 *155 164 * @return array Returns the modified $theme_json chunk. 156 165 */ … … 169 178 170 179 /** 171 * Given a theme.json structure modifies it in place 172 * to update certain values by its translated strings 173 * according to the language set by the user. 180 * Given a theme.json structure modifies it in place to update certain values 181 * by its translated strings according to the language set by the user. 182 * 183 * @since 5.8.0 174 184 * 175 185 * @param array $theme_json The theme.json to translate. 176 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 177 * Default 'default'. 178 * 186 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 187 * Default 'default'. 179 188 * @return array Returns the modified $theme_json_structure. 180 189 */ … … 229 238 * Return core's origin config. 230 239 * 240 * @since 5.8.0 241 * 231 242 * @return WP_Theme_JSON Entity that holds core data. 232 243 */ … … 246 257 * Returns the theme's data. 247 258 * 248 * Data from theme.json can be augmented via the 249 * $theme_support_data variable. This is useful, for example, 250 * to backfill the gaps in theme.json that a theme has declared 251 * via add_theme_supports. 252 * 253 * Note that if the same data is present in theme.json 254 * and in $theme_support_data, the theme.json's is not overwritten. 259 * Data from theme.json can be augmented via the $theme_support_data variable. 260 * This is useful, for example, to backfill the gaps in theme.json that a theme 261 * has declared via add_theme_supports. 262 * 263 * Note that if the same data is present in theme.json and in $theme_support_data, 264 * the theme.json's is not overwritten. 265 * 266 * @since 5.8.0 255 267 * 256 268 * @param array $theme_support_data Theme support data in theme.json format. 257 *258 269 * @return WP_Theme_JSON Entity that holds theme data. 259 270 */ … … 280 291 281 292 /** 282 * There are different sources of data for a site: 283 * core and theme. 284 * 285 * While the getters {@link get_core_data}, 286 * {@link get_theme_data} return the raw data 287 * from the respective origins, this method merges them 288 * all together. 293 * There are different sources of data for a site: core and theme. 294 * 295 * While the getters {@link get_core_data}, {@link get_theme_data} return the raw data 296 * from the respective origins, this method merges them all together. 289 297 * 290 298 * If the same piece of data is declared in different origins (core and theme), 291 * the last origin overrides the previous. For example, 292 * if core disables custom colors but a theme enables them, 293 * the theme config wins. 299 * the last origin overrides the previous. For example, if core disables custom colors 300 * but a theme enables them, the theme config wins. 301 * 302 * @since 5.8.0 294 303 * 295 304 * @param array $settings Existing block editor settings. 296 305 * Empty array by default. 297 *298 306 * @return WP_Theme_JSON 299 307 */ … … 322 330 323 331 /** 324 * Builds the path to the given file 325 * and checks that it is readable.326 * 327 * If it isn't, returns an empty string,328 * otherwise returns the whole file path.332 * Builds the path to the given file and checks that it is readable. 333 * 334 * If it isn't, returns an empty string, otherwise returns the whole file path. 335 * 336 * @since 5.8.0 329 337 * 330 338 * @param string $file_name Name of the file. … … 332 340 */ 333 341 private static function get_file_path_from_theme( $file_name ) { 334 // This used to be a locate_template call. 335 // However, that method proved problematic 336 // due to its use of constants (STYLESHEETPATH) 337 // that threw errors in some scenarios. 338 // 339 // When the theme.json merge algorithm properly supports 340 // child themes, this should also fallback 341 // to the template path, as locate_template did. 342 /* 343 * This used to be a locate_template call. However, that method proved problematic 344 * due to its use of constants (STYLESHEETPATH) that threw errors in some scenarios. 345 * 346 * When the theme.json merge algorithm properly supports child themes, 347 * this should also fall back to the template path, as locate_template did. 348 */ 342 349 $located = ''; 343 350 $candidate = get_stylesheet_directory() . '/' . $file_name; … … 350 357 /** 351 358 * Cleans the cached data so it can be recalculated. 359 * 360 * @since 5.8.0 352 361 */ 353 362 public static function clean_cached_data() { 354 self::$core 355 self::$theme 356 self::$theme_has_support 357 self::$theme_json_i18n 363 self::$core = null; 364 self::$theme = null; 365 self::$theme_has_support = null; 366 self::$theme_json_i18n = null; 358 367 } 359 368 -
trunk/src/wp-includes/class-wp-theme-json.php
r50959 r50967 1 1 <?php 2 2 /** 3 * Process of structures that adhere to the theme.json schema.3 * WP_Theme_JSON class 4 4 * 5 5 * @package WordPress 6 * @subpackage Theme 7 * @since 5.8.0 6 8 */ 7 9 8 10 /** 9 * Class that encapsulates the processing of 10 * structures that adhere to the theme.json spec. 11 * Class that encapsulates the processing of structures that adhere to the theme.json spec. 11 12 * 12 13 * @access private … … 17 18 * Container of data in theme.json format. 18 19 * 20 * @since 5.8.0 19 21 * @var array 20 22 */ … … 25 27 * Shared among all instances so we only process it once. 26 28 * 29 * @since 5.8.0 27 30 * @var array 28 31 */ … … 63 66 * Constructor. 64 67 * 68 * @since 5.8.0 69 * 65 70 * @param array $theme_json A structure that follows the theme.json schema. 66 71 */ … … 71 76 } 72 77 73 $this->theme_json 78 $this->theme_json = self::sanitize( $theme_json ); 74 79 } 75 80 76 81 /** 77 82 * Returns the allowed block names. 83 * 84 * @since 5.8.0 78 85 * 79 86 * @return array … … 92 99 * Sanitizes the input according to the schemas. 93 100 * 101 * @since 5.8.0 102 * 94 103 * @param array $input Structure to sanitize. 95 *96 104 * @return array The sanitized output. 97 105 */ … … 144 152 * It is recursive and modifies the input in-place. 145 153 * 154 * @since 5.8.0 155 * 146 156 * @param array $tree Input to process. 147 157 * @param array $schema Schema to adhere to. 148 *149 158 * @return array Returns the modified $tree. 150 159 */ … … 176 185 * Example: 177 186 * 178 * { 179 * 'root': { 180 * 'color': { 181 * 'custom': true 187 * { 188 * 'root': { 189 * 'color': { 190 * 'custom': true 191 * } 192 * }, 193 * 'core/paragraph': { 194 * 'spacing': { 195 * 'customPadding': true 196 * } 197 * } 182 198 * } 183 * }, 184 * 'core/paragraph': { 185 * 'spacing': { 186 * 'customPadding': true 187 * } 188 * } 189 * } 199 * 200 * @since 5.8.0 190 201 * 191 202 * @return array Settings per block. … … 202 213 * Builds metadata for the setting nodes, which returns in the form of: 203 214 * 204 * [ 205 * [ 206 * 'path' => ['path', 'to', 'some', 'node' ] 207 * ], 208 * [ 209 * 'path' => [ 'path', 'to', 'other', 'node' ] 210 * ], 211 * ] 215 * [ 216 * [ 217 * 'path' => ['path', 'to', 'some', 'node' ] 218 * ], 219 * [ 220 * 'path' => [ 'path', 'to', 'other', 'node' ] 221 * ], 222 * ] 223 * 224 * @since 5.8.0 212 225 * 213 226 * @param array $theme_json The tree to extract setting nodes from. 214 *215 227 * @return array 216 228 */ … … 243 255 * Merge new incoming data. 244 256 * 257 * @since 5.8.0 258 * 245 259 * @param WP_Theme_JSON $incoming Data to merge. 246 260 */ … … 249 263 $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); 250 264 251 // The array_replace_recursive algorithm merges at the leaf level. 252 // For leaf values that are arrays it will use the numeric indexes for replacement. 253 // In those cases, what we want is to use the incoming value, if it exists. 254 // 255 // These are the cases that have array values at the leaf levels. 265 /* 266 * The array_replace_recursive algorithm merges at the leaf level. 267 * For leaf values that are arrays it will use the numeric indexes for replacement. 268 * In those cases, what we want is to use the incoming value, if it exists. 269 * 270 * These are the cases that have array values at the leaf levels. 271 */ 256 272 $properties = array(); 257 273 $properties[] = array( 'color', 'palette' ); … … 278 294 * Returns the raw data. 279 295 * 296 * @since 5.8.0 297 * 280 298 * @return array Raw data. 281 299 */ … … 285 303 286 304 /** 287 *288 305 * Transforms the given editor settings according the 289 306 * add_theme_support format to the theme.json format. 290 307 * 308 * @since 5.8.0 309 * 291 310 * @param array $settings Existing editor settings. 292 *293 311 * @return array Config that adheres to the theme.json schema. 294 312 */ … … 365 383 } 366 384 367 // This allows to make the plugin work with WordPress 5.7 beta 368 // as well as lower versions. The second check can be removed 369 // as soon as the minimum WordPress version for the plugin 370 // is bumped to 5.7. 385 /* 386 * This allows to make the plugin work with WordPress 5.8 beta 387 * as well as lower versions. The second check can be removed 388 * as soon as the minimum WordPress version for the plugin 389 * is bumped to 5.8. 390 */ 371 391 if ( isset( $settings['enableCustomSpacing'] ) ) { 372 392 if ( ! isset( $theme_settings['settings']['spacing'] ) ) { -
trunk/tests/phpunit/tests/theme/wpTheme.php
r50966 r50967 1 1 <?php 2 2 /** 3 /** 4 * Test WP_Theme class. 5 * 6 * @package WordPress 7 * @subpackage Theme 8 * 3 9 * @group themes 4 10 */ 5 class Tests_Theme_ WPTheme extends WP_UnitTestCase {11 class Tests_Theme_wpTheme extends WP_UnitTestCase { 6 12 function setUp() { 7 13 parent::setUp(); -
trunk/tests/phpunit/tests/theme/wpThemeJson.php
r50966 r50967 5 5 * 6 6 * @package WordPress 7 * @subpackage Theme 7 8 * @since 5.8.0 9 * 10 * @group themes 8 11 */ 9 12 10 class WP_Theme_JSON_Testextends WP_UnitTestCase {13 class Tests_Theme_wpThemeJson extends WP_UnitTestCase { 11 14 12 15 /** -
trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php
r50966 r50967 5 5 * 6 6 * @package WordPress 7 * @subpackage Theme 7 8 * @since 5.8.0 9 * 10 * @group themes 8 11 */ 9 10 class WP_Theme_JSON_Resolver_Test extends WP_UnitTestCase { 12 class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase { 11 13 12 14 function setUp() {
Note: See TracChangeset
for help on using the changeset viewer.