Açıklama Yok

class-wp-theme-json-schema.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * WP_Theme_JSON_Schema class
  4. *
  5. * @package WordPress
  6. * @subpackage Theme
  7. * @since 5.9.0
  8. */
  9. /**
  10. * Class that migrates a given theme.json structure to the latest schema.
  11. *
  12. * This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
  13. * This is a low-level API that may need to do breaking changes. Please,
  14. * use get_global_settings, get_global_styles, and get_global_stylesheet instead.
  15. *
  16. * @since 5.9.0
  17. * @access private
  18. */
  19. class WP_Theme_JSON_Schema {
  20. /**
  21. * Maps old properties to their new location within the schema's settings.
  22. * This will be applied at both the defaults and individual block levels.
  23. */
  24. const V1_TO_V2_RENAMED_PATHS = array(
  25. 'border.customRadius' => 'border.radius',
  26. 'spacing.customMargin' => 'spacing.margin',
  27. 'spacing.customPadding' => 'spacing.padding',
  28. 'typography.customLineHeight' => 'typography.lineHeight',
  29. );
  30. /**
  31. * Function that migrates a given theme.json structure to the last version.
  32. *
  33. * @since 5.9.0
  34. *
  35. * @param array $theme_json The structure to migrate.
  36. *
  37. * @return array The structure in the last version.
  38. */
  39. public static function migrate( $theme_json ) {
  40. if ( ! isset( $theme_json['version'] ) ) {
  41. $theme_json = array(
  42. 'version' => WP_Theme_JSON::LATEST_SCHEMA,
  43. );
  44. }
  45. if ( 1 === $theme_json['version'] ) {
  46. $theme_json = self::migrate_v1_to_v2( $theme_json );
  47. }
  48. return $theme_json;
  49. }
  50. /**
  51. * Removes the custom prefixes for a few properties
  52. * that were part of v1:
  53. *
  54. * 'border.customRadius' => 'border.radius',
  55. * 'spacing.customMargin' => 'spacing.margin',
  56. * 'spacing.customPadding' => 'spacing.padding',
  57. * 'typography.customLineHeight' => 'typography.lineHeight',
  58. *
  59. * @since 5.9.0
  60. *
  61. * @param array $old Data to migrate.
  62. *
  63. * @return array Data without the custom prefixes.
  64. */
  65. private static function migrate_v1_to_v2( $old ) {
  66. // Copy everything.
  67. $new = $old;
  68. // Overwrite the things that changed.
  69. if ( isset( $old['settings'] ) ) {
  70. $new['settings'] = self::rename_paths( $old['settings'], self::V1_TO_V2_RENAMED_PATHS );
  71. }
  72. // Set the new version.
  73. $new['version'] = 2;
  74. return $new;
  75. }
  76. /**
  77. * Processes the settings subtree.
  78. *
  79. * @since 5.9.0
  80. *
  81. * @param array $settings Array to process.
  82. * @param array $paths_to_rename Paths to rename.
  83. *
  84. * @return array The settings in the new format.
  85. */
  86. private static function rename_paths( $settings, $paths_to_rename ) {
  87. $new_settings = $settings;
  88. // Process any renamed/moved paths within default settings.
  89. self::rename_settings( $new_settings, $paths_to_rename );
  90. // Process individual block settings.
  91. if ( isset( $new_settings['blocks'] ) && is_array( $new_settings['blocks'] ) ) {
  92. foreach ( $new_settings['blocks'] as &$block_settings ) {
  93. self::rename_settings( $block_settings, $paths_to_rename );
  94. }
  95. }
  96. return $new_settings;
  97. }
  98. /**
  99. * Processes a settings array, renaming or moving properties.
  100. *
  101. * @since 5.9.0
  102. *
  103. * @param array $settings Reference to settings either defaults or an individual block's.
  104. * @param array $paths_to_rename Paths to rename.
  105. */
  106. private static function rename_settings( &$settings, $paths_to_rename ) {
  107. foreach ( $paths_to_rename as $original => $renamed ) {
  108. $original_path = explode( '.', $original );
  109. $renamed_path = explode( '.', $renamed );
  110. $current_value = _wp_array_get( $settings, $original_path, null );
  111. if ( null !== $current_value ) {
  112. _wp_array_set( $settings, $renamed_path, $current_value );
  113. self::unset_setting_by_path( $settings, $original_path );
  114. }
  115. }
  116. }
  117. /**
  118. * Removes a property from within the provided settings by its path.
  119. *
  120. * @since 5.9.0
  121. *
  122. * @param array $settings Reference to the current settings array.
  123. * @param array $path Path to the property to be removed.
  124. *
  125. * @return void
  126. */
  127. private static function unset_setting_by_path( &$settings, $path ) {
  128. $tmp_settings = &$settings; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  129. $last_key = array_pop( $path );
  130. foreach ( $path as $key ) {
  131. $tmp_settings = &$tmp_settings[ $key ];
  132. }
  133. unset( $tmp_settings[ $last_key ] );
  134. }
  135. }