1 | <?php |
||
16 | class Registry |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $jsdata = array(); |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Registry constructor. |
||
28 | * Hooking into WP actions for script registry. |
||
29 | */ |
||
30 | public function __construct() |
||
35 | |||
36 | |||
37 | /** |
||
38 | * Callback for the WP script actions. |
||
39 | * Used to register globally accessible core scripts. |
||
40 | * Also used to add the eejs.data object to the source for any js having eejs-core as a dependency. |
||
41 | */ |
||
42 | public function scripts() |
||
43 | { |
||
44 | wp_register_script( |
||
45 | 'eejs-core', |
||
46 | EE_PLUGIN_DIR_URL . 'core/services/assets/core_assets/eejs-core.js', |
||
47 | array(), |
||
48 | espresso_version(), |
||
49 | true |
||
50 | ); |
||
51 | wp_localize_script('eejs-core', 'eejs', array('data'=>$this->jsdata)); |
||
52 | } |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Used to add data to eejs.data object. |
||
57 | * |
||
58 | * Note: Overriding existing data is not allowed. |
||
59 | * |
||
60 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
61 | * If the data you add is something like this: |
||
62 | * |
||
63 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
64 | * |
||
65 | * It will be exposed in the page source as: |
||
66 | * |
||
67 | * eejs.data.my_plugin_data.foo == gar |
||
68 | * |
||
69 | * @param string $key Key used to access your data |
||
70 | * @param string|array $value Value to attach to key |
||
71 | * @throws \InvalidArgumentException |
||
72 | */ |
||
73 | public function addData($key, $value) |
||
74 | { |
||
75 | if ($this->verifyDataNotExisting($key)) { |
||
76 | $this->jsdata[$key] = $value; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
83 | * elements in an array. |
||
84 | * |
||
85 | * When you use this method, the value you include will be appended to the end of an array on $key. |
||
86 | * |
||
87 | * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object |
||
88 | * like this, |
||
89 | * |
||
90 | * eejs.data.test = [ |
||
91 | * my_data, |
||
92 | * ] |
||
93 | * |
||
94 | * If there has already been a scalar value attached to the data object given key, then |
||
95 | * this will throw an exception. |
||
96 | * |
||
97 | * @param string $key Key to attach data to. |
||
98 | * @param string|array $value Value being registered. |
||
99 | * @throws InvalidArgumentException |
||
100 | */ |
||
101 | public function pushData($key, $value) |
||
102 | { |
||
103 | if (isset($this->jsdata[$key]) |
||
104 | && ! is_array($this->jsdata[$key]) |
||
105 | ) { |
||
106 | throw new invalidArgumentException( |
||
107 | sprintf( |
||
108 | __( |
||
109 | 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
||
110 | push values to this data element when it is an array.', |
||
111 | 'event_espresso' |
||
112 | ), |
||
113 | $key, |
||
114 | __METHOD__ |
||
115 | ) |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | $this->jsdata[$key][] = $value; |
||
120 | } |
||
121 | |||
122 | |||
123 | /** |
||
124 | * Retrieve registered data. |
||
125 | * |
||
126 | * @param string $key Name of key to attach data to. |
||
127 | * @return mixed If there is no for the given key, then false is returned. |
||
128 | */ |
||
129 | public function getData($key) |
||
130 | { |
||
131 | return isset($this->jsdata[$key]) |
||
132 | ? $this->jsdata[$key] |
||
133 | : false; |
||
134 | } |
||
135 | |||
136 | |||
137 | |||
138 | |||
139 | /** |
||
140 | * Verifies whether the given data exists already on the jsdata array. |
||
141 | * |
||
142 | * Overriding data is not allowed. |
||
143 | * |
||
144 | * @param string $key Index for data. |
||
145 | * @return bool If valid then return true. |
||
146 | * @throws InvalidArgumentException if data already exists. |
||
147 | */ |
||
148 | protected function verifyDataNotExisting($key) |
||
179 | } |
||
180 |