class-wp-theme-json-data.php 1.5 KB

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