| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WP_Theme_JSON_Schema class 4 * 5 * @package WordPress 6 * @subpackage Theme 7 * @since 5.9.0 8 */ 9 10 /** 11 * Class that migrates a given theme.json structure to the latest schema. 12 * 13 * This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes). 14 * This is a low-level API that may need to do breaking changes. Please, 15 * use get_global_settings, get_global_styles, and get_global_stylesheet instead. 16 * 17 * @since 5.9.0 18 * @access private 19 */ 20 #[AllowDynamicProperties] 21 class WP_Theme_JSON_Schema { 22 23 /** 24 * Maps old properties to their new location within the schema's settings. 25 * This will be applied at both the defaults and individual block levels. 26 */ 27 const V1_TO_V2_RENAMED_PATHS = array( 28 'border.customRadius' => 'border.radius', 29 'spacing.customMargin' => 'spacing.margin', 30 'spacing.customPadding' => 'spacing.padding', 31 'typography.customLineHeight' => 'typography.lineHeight', 32 ); 33 34 /** 35 * Function that migrates a given theme.json structure to the last version. 36 * 37 * @since 5.9.0 38 * @since 6.6.0 Migrate up to v3 and add $origin parameter. 39 * 40 * @param array $theme_json The structure to migrate. 41 * @param string $origin Optional. What source of data this object represents. 42 * One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'. 43 * @return array The structure in the last version. 44 */ 45 public static function migrate( $theme_json, $origin = 'theme' ) { 46 if ( ! isset( $theme_json['version'] ) ) { 47 $theme_json = array( 48 'version' => WP_Theme_JSON::LATEST_SCHEMA, 49 ); 50 } 51 52 // Migrate each version in order starting with the current version. 53 switch ( $theme_json['version'] ) { 54 case 1: 55 $theme_json = self::migrate_v1_to_v2( $theme_json ); 56 // Deliberate fall through. Once migrated to v2, also migrate to v3. 57 case 2: 58 $theme_json = self::migrate_v2_to_v3( $theme_json, $origin ); 59 } 60 61 return $theme_json; 62 } 63 64 /** 65 * Removes the custom prefixes for a few properties 66 * that were part of v1: 67 * 68 * 'border.customRadius' => 'border.radius', 69 * 'spacing.customMargin' => 'spacing.margin', 70 * 'spacing.customPadding' => 'spacing.padding', 71 * 'typography.customLineHeight' => 'typography.lineHeight', 72 * 73 * @since 5.9.0 74 * 75 * @param array $old Data to migrate. 76 * @return array Data without the custom prefixes. 77 */ 78 private static function migrate_v1_to_v2( $old ) { 79 // Copy everything. 80 $new = $old; 81 82 // Overwrite the things that changed. 83 if ( isset( $old['settings'] ) ) { 84 $new['settings'] = self::rename_paths( $old['settings'], self::V1_TO_V2_RENAMED_PATHS ); 85 } 86 87 // Set the new version. 88 $new['version'] = 2; 89 90 return $new; 91 } 92 93 /** 94 * Migrates from v2 to v3. 95 * 96 * - Sets settings.typography.defaultFontSizes to false if settings.typography.fontSizes are defined. 97 * - Sets settings.spacing.defaultSpacingSizes to false if settings.spacing.spacingSizes are defined. 98 * - Prevents settings.spacing.spacingSizes from merging with settings.spacing.spacingScale by 99 * unsetting spacingScale when spacingSizes are defined. 100 * 101 * @since 6.6.0 102 * 103 * @param array $old Data to migrate. 104 * @param string $origin What source of data this object represents. 105 * One of 'blocks', 'default', 'theme', or 'custom'. 106 * @return array Data with defaultFontSizes set to false. 107 */ 108 private static function migrate_v2_to_v3( $old, $origin ) { 109 // Copy everything. 110 $new = $old; 111 112 // Set the new version. 113 $new['version'] = 3; 114 115 /* 116 * Remaining changes do not need to be applied to the custom origin, 117 * as they should take on the value of the theme origin. 118 */ 119 if ( 'custom' === $origin ) { 120 return $new; 121 } 122 123 /* 124 * Even though defaultFontSizes and defaultSpacingSizes are new 125 * settings, we need to migrate them as they each control 126 * PRESETS_METADATA prevent_override values which were previously 127 * hardcoded to false. This only needs to happen when the theme provides 128 * fontSizes or spacingSizes as they could match the default ones and 129 * affect the generated CSS. 130 */ 131 if ( isset( $old['settings']['typography']['fontSizes'] ) ) { 132 $new['settings']['typography']['defaultFontSizes'] = false; 133 } 134 135 /* 136 * Similarly to defaultFontSizes, we need to migrate defaultSpacingSizes 137 * as it controls the PRESETS_METADATA prevent_override which was 138 * previously hardcoded to false. This only needs to happen when the 139 * theme provided spacing sizes via spacingSizes or spacingScale. 140 */ 141 if ( 142 isset( $old['settings']['spacing']['spacingSizes'] ) || 143 isset( $old['settings']['spacing']['spacingScale'] ) 144 ) { 145 $new['settings']['spacing']['defaultSpacingSizes'] = false; 146 } 147 148 /* 149 * In v3 spacingSizes is merged with the generated spacingScale sizes 150 * instead of completely replacing them. The v3 behavior is what was 151 * documented for the v2 schema, but the code never actually did work 152 * that way. Instead of surprising users with a behavior change two 153 * years after the fact at the same time as a v3 update is introduced, 154 * we'll continue using the "bugged" behavior for v2 themes. And treat 155 * the "bug fix" as a breaking change for v3. 156 */ 157 if ( isset( $old['settings']['spacing']['spacingSizes'] ) ) { 158 unset( $new['settings']['spacing']['spacingScale'] ); 159 } 160 161 return $new; 162 } 163 164 /** 165 * Processes the settings subtree. 166 * 167 * @since 5.9.0 168 * 169 * @param array $settings Array to process. 170 * @param array $paths_to_rename Paths to rename. 171 * @return array The settings in the new format. 172 */ 173 private static function rename_paths( $settings, $paths_to_rename ) { 174 $new_settings = $settings; 175 176 // Process any renamed/moved paths within default settings. 177 self::rename_settings( $new_settings, $paths_to_rename ); 178 179 // Process individual block settings. 180 if ( isset( $new_settings['blocks'] ) && is_array( $new_settings['blocks'] ) ) { 181 foreach ( $new_settings['blocks'] as &$block_settings ) { 182 self::rename_settings( $block_settings, $paths_to_rename ); 183 } 184 } 185 186 return $new_settings; 187 } 188 189 /** 190 * Processes a settings array, renaming or moving properties. 191 * 192 * @since 5.9.0 193 * 194 * @param array $settings Reference to settings either defaults or an individual block's. 195 * @param array $paths_to_rename Paths to rename. 196 */ 197 private static function rename_settings( &$settings, $paths_to_rename ) { 198 foreach ( $paths_to_rename as $original => $renamed ) { 199 $original_path = explode( '.', $original ); 200 $renamed_path = explode( '.', $renamed ); 201 $current_value = _wp_array_get( $settings, $original_path, null ); 202 203 if ( null !== $current_value ) { 204 _wp_array_set( $settings, $renamed_path, $current_value ); 205 self::unset_setting_by_path( $settings, $original_path ); 206 } 207 } 208 } 209 210 /** 211 * Removes a property from within the provided settings by its path. 212 * 213 * @since 5.9.0 214 * 215 * @param array $settings Reference to the current settings array. 216 * @param array $path Path to the property to be removed. 217 */ 218 private static function unset_setting_by_path( &$settings, $path ) { 219 $tmp_settings = &$settings; 220 $last_key = array_pop( $path ); 221 foreach ( $path as $key ) { 222 $tmp_settings = &$tmp_settings[ $key ]; 223 } 224 225 unset( $tmp_settings[ $last_key ] ); 226 } 227 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Aug 18 08:20:25 2026 | Cross-referenced by PHPXref |