PluginProbe
Gutenberg / 17.0.3
Gutenberg v17.0.3
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / experimental / interactivity-api / class-wp-interactivity-store.php

class-wp-interactivity-store.php in Gutenberg 17.0.3, at lib/experimental/interactivity-api/class-wp-interactivity-store.php

70 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP_Interactivity_Store class
4 *
5 * Manages the initial state of the Interactivity API store in the server and
6 * its serialization so it can be restored in the browser upon hydration.
7 *
8 * @package Gutenberg
9 * @subpackage Interactivity API
10 */
11
12 if ( class_exists( 'WP_Interactivity_Store' ) ) {
13 return;
14 }
15
16 /**
17 * Manages the initial state of the Interactivity API store in the server and
18 * its serialization so it can be restored in the browser upon hydration.
19 *
20 * It's a private class, exposed by other functions, like `wp_store`.
21 *
22 * @access private
23 */
24 class WP_Interactivity_Store {
25 /**
26 * Store.
27 *
28 * @var array
29 */
30 private static $store = array();
31
32 /**
33 * Get store data.
34 *
35 * @return array
36 */
37 public static function get_data() {
38 return self::$store;
39 }
40
41 /**
42 * Merge data.
43 *
44 * @param array $data The data that will be merged with the existing store.
45 */
46 public static function merge_data( $data ) {
47 self::$store = array_replace_recursive( self::$store, $data );
48 }
49
50 /**
51 * Reset the store data.
52 */
53 public static function reset() {
54 self::$store = array();
55 }
56
57 /**
58 * Render the store data.
59 */
60 public static function render() {
61 if ( empty( self::$store ) ) {
62 return;
63 }
64 echo sprintf(
65 '<script id="wp-interactivity-store-data" type="application/json">%s</script>',
66 wp_json_encode( self::$store, JSON_HEX_TAG | JSON_HEX_AMP )
67 );
68 }
69 }
70