[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WP_Theme_JSON_Resolver class
   4   *
   5   * @package WordPress
   6   * @subpackage Theme
   7   * @since 5.8.0
   8   */
   9  
  10  /**
  11   * Class that abstracts the processing of the different data sources
  12   * for site-level config and offers an API to work with them.
  13   *
  14   * @access private
  15   */
  16  class WP_Theme_JSON_Resolver {
  17  
  18      /**
  19       * Container for data coming from core.
  20       *
  21       * @since 5.8.0
  22       * @var WP_Theme_JSON
  23       */
  24      private static $core = null;
  25  
  26      /**
  27       * Container for data coming from the theme.
  28       *
  29       * @since 5.8.0
  30       * @var WP_Theme_JSON
  31       */
  32      private static $theme = null;
  33  
  34      /**
  35       * Whether or not the theme supports theme.json.
  36       *
  37       * @since 5.8.0
  38       * @var bool
  39       */
  40      private static $theme_has_support = null;
  41  
  42      /**
  43       * Container to keep loaded i18n schema for `theme.json`.
  44       *
  45       * @since 5.9.0
  46       * @var array
  47       */
  48      private static $i18n_schema = null;
  49  
  50      /**
  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
  55       *
  56       * @param string $file_path Path to file. Empty if no file.
  57       * @return array Contents that adhere to the theme.json schema.
  58       */
  59  	private static function read_json_file( $file_path ) {
  60          $config = array();
  61          if ( $file_path ) {
  62              $decoded_file = wp_json_file_decode( $file_path, array( 'associative' => true ) );
  63              if ( is_array( $decoded_file ) ) {
  64                  $config = $decoded_file;
  65              }
  66          }
  67          return $config;
  68      }
  69  
  70      /**
  71       * Returns a data structure used in theme.json translation.
  72       *
  73       * @since 5.8.0
  74       * @deprecated 5.9.0
  75       *
  76       * @return array An array of theme.json fields that are translatable and the keys that are translatable.
  77       */
  78  	public static function get_fields_to_translate() {
  79          _deprecated_function( __METHOD__, '5.9.0' );
  80          return array();
  81      }
  82  
  83      /**
  84       * Given a theme.json structure modifies it in place to update certain values
  85       * by its translated strings according to the language set by the user.
  86       *
  87       * @since 5.8.0
  88       *
  89       * @param array  $theme_json The theme.json to translate.
  90       * @param string $domain     Optional. Text domain. Unique identifier for retrieving translated strings.
  91       *                           Default 'default'.
  92       * @return array Returns the modified $theme_json_structure.
  93       */
  94  	private static function translate( $theme_json, $domain = 'default' ) {
  95          if ( null === self::$i18n_schema ) {
  96              $i18n_schema       = wp_json_file_decode( __DIR__ . '/theme-i18n.json' );
  97              self::$i18n_schema = null === $i18n_schema ? array() : $i18n_schema;
  98          }
  99  
 100          return translate_settings_using_i18n_schema( self::$i18n_schema, $theme_json, $domain );
 101      }
 102  
 103      /**
 104       * Return core's origin config.
 105       *
 106       * @since 5.8.0
 107       *
 108       * @return WP_Theme_JSON Entity that holds core data.
 109       */
 110  	public static function get_core_data() {
 111          if ( null !== self::$core ) {
 112              return self::$core;
 113          }
 114  
 115          $config     = self::read_json_file( __DIR__ . '/theme.json' );
 116          $config     = self::translate( $config );
 117          self::$core = new WP_Theme_JSON( $config, 'core' );
 118  
 119          return self::$core;
 120      }
 121  
 122      /**
 123       * Returns the theme's data.
 124       *
 125       * Data from theme.json can be augmented via the $theme_support_data variable.
 126       * This is useful, for example, to backfill the gaps in theme.json that a theme
 127       * has declared via add_theme_supports.
 128       *
 129       * Note that if the same data is present in theme.json and in $theme_support_data,
 130       * the theme.json's is not overwritten.
 131       *
 132       * @since 5.8.0
 133       *
 134       * @param array $theme_support_data Optional. Theme support data in theme.json format.
 135       *                                  Default empty array.
 136       * @return WP_Theme_JSON Entity that holds theme data.
 137       */
 138  	public static function get_theme_data( $theme_support_data = array() ) {
 139          if ( null === self::$theme ) {
 140              $theme_json_data = self::read_json_file( self::get_file_path_from_theme( 'theme.json' ) );
 141              $theme_json_data = self::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
 142              self::$theme     = new WP_Theme_JSON( $theme_json_data );
 143          }
 144  
 145          if ( empty( $theme_support_data ) ) {
 146              return self::$theme;
 147          }
 148  
 149          /*
 150           * We want the presets and settings declared in theme.json
 151           * to override the ones declared via add_theme_support.
 152           */
 153          $with_theme_supports = new WP_Theme_JSON( $theme_support_data );
 154          $with_theme_supports->merge( self::$theme );
 155  
 156          return $with_theme_supports;
 157      }
 158  
 159      /**
 160       * There are different sources of data for a site: core and theme.
 161       *
 162       * While the getters {@link get_core_data}, {@link get_theme_data} return the raw data
 163       * from the respective origins, this method merges them all together.
 164       *
 165       * If the same piece of data is declared in different origins (core and theme),
 166       * the last origin overrides the previous. For example, if core disables custom colors
 167       * but a theme enables them, the theme config wins.
 168       *
 169       * @since 5.8.0
 170       *
 171       * @param array $settings Optional. Existing block editor settings. Default empty array.
 172       * @return WP_Theme_JSON
 173       */
 174  	public static function get_merged_data( $settings = array() ) {
 175          $theme_support_data = WP_Theme_JSON::get_from_editor_settings( $settings );
 176  
 177          $result = new WP_Theme_JSON();
 178          $result->merge( self::get_core_data() );
 179          $result->merge( self::get_theme_data( $theme_support_data ) );
 180  
 181          return $result;
 182      }
 183  
 184      /**
 185       * Whether the current theme has a theme.json file.
 186       *
 187       * @since 5.8.0
 188       *
 189       * @return bool
 190       */
 191  	public static function theme_has_support() {
 192          if ( ! isset( self::$theme_has_support ) ) {
 193              self::$theme_has_support = (bool) self::get_file_path_from_theme( 'theme.json' );
 194          }
 195  
 196          return self::$theme_has_support;
 197      }
 198  
 199      /**
 200       * Builds the path to the given file and checks that it is readable.
 201       *
 202       * If it isn't, returns an empty string, otherwise returns the whole file path.
 203       *
 204       * @since 5.8.0
 205       *
 206       * @param string $file_name Name of the file.
 207       * @return string The whole file path or empty if the file doesn't exist.
 208       */
 209  	private static function get_file_path_from_theme( $file_name ) {
 210          /*
 211           * This used to be a locate_template call. However, that method proved problematic
 212           * due to its use of constants (STYLESHEETPATH) that threw errors in some scenarios.
 213           *
 214           * When the theme.json merge algorithm properly supports child themes,
 215           * this should also fall back to the template path, as locate_template did.
 216           */
 217          $located   = '';
 218          $candidate = get_stylesheet_directory() . '/' . $file_name;
 219          if ( is_readable( $candidate ) ) {
 220              $located = $candidate;
 221          }
 222          return $located;
 223      }
 224  
 225      /**
 226       * Cleans the cached data so it can be recalculated.
 227       *
 228       * @since 5.8.0
 229       */
 230  	public static function clean_cached_data() {
 231          self::$core              = null;
 232          self::$theme             = null;
 233          self::$theme_has_support = null;
 234      }
 235  
 236  }


Generated: Wed Oct 27 01:00:04 2021 Cross-referenced by PHPXref 0.7.1