[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-theme-json-schema.php (source)

   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       *
  39       * @param array $theme_json The structure to migrate.
  40       *
  41       * @return array The structure in the last version.
  42       */
  43  	public static function migrate( $theme_json ) {
  44          if ( ! isset( $theme_json['version'] ) ) {
  45              $theme_json = array(
  46                  'version' => WP_Theme_JSON::LATEST_SCHEMA,
  47              );
  48          }
  49  
  50          if ( 1 === $theme_json['version'] ) {
  51              $theme_json = self::migrate_v1_to_v2( $theme_json );
  52          }
  53  
  54          return $theme_json;
  55      }
  56  
  57      /**
  58       * Removes the custom prefixes for a few properties
  59       * that were part of v1:
  60       *
  61       * 'border.customRadius'         => 'border.radius',
  62       * 'spacing.customMargin'        => 'spacing.margin',
  63       * 'spacing.customPadding'       => 'spacing.padding',
  64       * 'typography.customLineHeight' => 'typography.lineHeight',
  65       *
  66       * @since 5.9.0
  67       *
  68       * @param array $old Data to migrate.
  69       *
  70       * @return array Data without the custom prefixes.
  71       */
  72  	private static function migrate_v1_to_v2( $old ) {
  73          // Copy everything.
  74          $new = $old;
  75  
  76          // Overwrite the things that changed.
  77          if ( isset( $old['settings'] ) ) {
  78              $new['settings'] = self::rename_paths( $old['settings'], self::V1_TO_V2_RENAMED_PATHS );
  79          }
  80  
  81          // Set the new version.
  82          $new['version'] = 2;
  83  
  84          return $new;
  85      }
  86  
  87      /**
  88       * Processes the settings subtree.
  89       *
  90       * @since 5.9.0
  91       *
  92       * @param array $settings        Array to process.
  93       * @param array $paths_to_rename Paths to rename.
  94       *
  95       * @return array The settings in the new format.
  96       */
  97  	private static function rename_paths( $settings, $paths_to_rename ) {
  98          $new_settings = $settings;
  99  
 100          // Process any renamed/moved paths within default settings.
 101          self::rename_settings( $new_settings, $paths_to_rename );
 102  
 103          // Process individual block settings.
 104          if ( isset( $new_settings['blocks'] ) && is_array( $new_settings['blocks'] ) ) {
 105              foreach ( $new_settings['blocks'] as &$block_settings ) {
 106                  self::rename_settings( $block_settings, $paths_to_rename );
 107              }
 108          }
 109  
 110          return $new_settings;
 111      }
 112  
 113      /**
 114       * Processes a settings array, renaming or moving properties.
 115       *
 116       * @since 5.9.0
 117       *
 118       * @param array $settings        Reference to settings either defaults or an individual block's.
 119       * @param array $paths_to_rename Paths to rename.
 120       */
 121  	private static function rename_settings( &$settings, $paths_to_rename ) {
 122          foreach ( $paths_to_rename as $original => $renamed ) {
 123              $original_path = explode( '.', $original );
 124              $renamed_path  = explode( '.', $renamed );
 125              $current_value = _wp_array_get( $settings, $original_path, null );
 126  
 127              if ( null !== $current_value ) {
 128                  _wp_array_set( $settings, $renamed_path, $current_value );
 129                  self::unset_setting_by_path( $settings, $original_path );
 130              }
 131          }
 132      }
 133  
 134      /**
 135       * Removes a property from within the provided settings by its path.
 136       *
 137       * @since 5.9.0
 138       *
 139       * @param array $settings Reference to the current settings array.
 140       * @param array $path Path to the property to be removed.
 141       */
 142  	private static function unset_setting_by_path( &$settings, $path ) {
 143          $tmp_settings = &$settings;
 144          $last_key     = array_pop( $path );
 145          foreach ( $path as $key ) {
 146              $tmp_settings = &$tmp_settings[ $key ];
 147          }
 148  
 149          unset( $tmp_settings[ $last_key ] );
 150      }
 151  }


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref