1<?php
2/**
3 * WP_Theme_JSON_Data class
4 *
5 * @package WordPress
6 * @subpackage Theme
7 * @since 6.1.0
8 */
9
10/**
11 * Class to provide access to update a theme.json structure.
12 */
13#[AllowDynamicProperties]
14class WP_Theme_JSON_Data {
15
16 /**
17 * Container of the data to update.
18 *
19 * @since 6.1.0
20 * @var WP_Theme_JSON
21 */
22 private $theme_json = null;
23
24 /**
25 * The origin of the data: default, theme, user, etc.
26 *
27 * @since 6.1.0
28 * @var string
29 */
30 private $origin = '';
31
32 /**
33 * Constructor.
34 *
35 * @since 6.1.0
36 *
37 * @link https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/
38 *
39 * @param array $data Array following the theme.json specification.
40 * @param string $origin The origin of the data: default, theme, user.
41 */
42 public function __construct( $data = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA ), $origin = 'theme' ) {
43 $this->origin = $origin;
44 $this->theme_json = new WP_Theme_JSON( $data, $this->origin );
45 }
46
47 /**
48 * Updates the theme.json with the the given data.
49 *
50 * @since 6.1.0
51 *
52 * @param array $new_data Array following the theme.json specification.
53 *
54 * @return WP_Theme_JSON_Data The own instance with access to the modified data.
55 */
56 public function update_with( $new_data ) {
57 $this->theme_json->merge( new WP_Theme_JSON( $new_data, $this->origin ) );
58 return $this;
59 }
60
61 /**
62 * Returns an array containing the underlying data
63 * following the theme.json specification.
64 *
65 * @since 6.1.0
66 *
67 * @return array
68 */
69 public function get_data() {
70 return $this->theme_json->get_raw_data();
71 }
72
73 /**
74 * Returns theme JSON object.
75 *
76 * @since 6.6.0
77 *
78 * @return WP_Theme_JSON The theme JSON structure stored in this data object.
79 */
80 public function get_theme_json() {
81 return $this->theme_json;
82 }
83}
84