Completed
Branch BUG-9951-10331-8793-pue-fixes (9f33f1)
by
unknown
25:58 queued 13:29
created

Registry::addTemplate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 2
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\services\assets;
3
4
use InvalidArgumentException;
5
6
defined('EVENT_ESPRESSO_VERSION') || exit;
7
8
/**
9
 * Used for registering assets used in EE.
10
 *
11
 * @package    EventEspresso
12
 * @subpackage services\assets
13
 * @author     Darren Ethier
14
 * @since      4.9.24.rc.004
15
 */
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()
31
    {
32
        add_action('wp_enqueue_scripts', array($this, 'scripts'), 100);
33
        add_action('admin_enqueue_scripts', array($this, 'scripts'), 100);
34
        add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1);
35
        add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1);
36
    }
37
38
39
    /**
40
     * Callback for the WP script actions.
41
     * Used to register globally accessible core scripts.
42
     * Also used to add the eejs.data object to the source for any js having eejs-core as a dependency.
43
     */
44
    public function scripts()
45
    {
46
        global $wp_version;
47
        wp_register_script(
48
            'eejs-core',
49
            EE_PLUGIN_DIR_URL . 'core/services/assets/core_assets/eejs-core.js',
50
            array(),
51
            espresso_version(),
52
            true
53
        );
54
        //only run this if WordPress 4.4.0 > is in use.
55
        if (version_compare($wp_version, '4.4.0', '>')) {
56
            //js.api
57
            wp_register_script(
58
                'eejs-api',
59
                EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js',
60
                array('underscore', 'eejs-core'),
61
                espresso_version(),
62
                true
63
            );
64
            $this->jsdata['paths'] = array('rest_route' => rest_url('ee/v4.8.36/'));
65
        }
66
    }
67
68
69
    /**
70
     * Call back for the script print in frontend and backend.
71
     * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hookpoint.
72
     * @since 4.9.31.rc.015
73
     */
74
    public function enqueueData()
75
    {
76
        wp_localize_script('eejs-core', 'eejs', array('data' => $this->jsdata));
77
    }
78
79
80
    /**
81
     * Used to add data to eejs.data object.
82
     *
83
     * Note:  Overriding existing data is not allowed.
84
     *
85
     * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript.
86
     * If the data you add is something like this:
87
     *
88
     *  $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) );
89
     *
90
     * It will be exposed in the page source as:
91
     *
92
     *  eejs.data.my_plugin_data.foo == gar
93
     *
94
     * @param string       $key   Key used to access your data
95
     * @param string|array $value Value to attach to key
96
     * @throws InvalidArgumentException
97
     */
98
    public function addData($key, $value)
99
    {
100
        if ($this->verifyDataNotExisting($key)) {
101
            $this->jsdata[$key] = $value;
102
        }
103
    }
104
105
106
    /**
107
     * Similar to addData except this allows for users to push values to an existing key where the values on key are
108
     * elements in an array.
109
     *
110
     * When you use this method, the value you include will be appended to the end of an array on $key.
111
     *
112
     * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object
113
     * like this,
114
     *
115
     * eejs.data.test = [
116
     *     my_data,
117
     * ]
118
     *
119
     * If there has already been a scalar value attached to the data object given key, then
120
     * this will throw an exception.
121
     *
122
     * @param string $key          Key to attach data to.
123
     * @param string|array $value  Value being registered.
124
     * @throws InvalidArgumentException
125
     */
126
    public function pushData($key, $value)
127
    {
128
        if (isset($this->jsdata[$key])
129
            && ! is_array($this->jsdata[$key])
130
        ) {
131
            throw new invalidArgumentException(
132
                sprintf(
133
                    __(
134
                        'The value for %1$s is already set and it is not an array. The %2$s method can only be used to
135
                         push values to this data element when it is an array.',
136
                        'event_espresso'
137
                    ),
138
                    $key,
139
                    __METHOD__
140
                )
141
            );
142
        }
143
144
        $this->jsdata[$key][] = $value;
145
    }
146
147
148
    /**
149
     * Used to set content used by javascript for a template.
150
     * Note: Overrides of existing registered templates are not allowed.
151
     *
152
     * @param string $template_reference
153
     * @param string $template_content
154
     * @throws InvalidArgumentException
155
     */
156
    public function addTemplate($template_reference, $template_content)
157
    {
158
        if (! isset($this->jsdata['templates'])) {
159
            $this->jsdata['templates'] = array();
160
        }
161
162
        //no overrides allowed.
163
        if (isset($this->jsdata['templates'][$template_reference])) {
164
            throw new invalidArgumentException(
165
                sprintf(
166
                    __(
167
                        'The %1$s key already exists for the templates array in the js data array.  No overrides are allowed.',
168
                        'event_espresso'
169
                    ),
170
                    $template_reference
171
                )
172
            );
173
        } else {
174
            $this->jsdata['templates'][$template_reference] = $template_content;
175
        }
176
    }
177
178
179
    /**
180
     * Retrieve the template content already registered for the given reference.
181
     * @param string $template_reference
182
     * @return string
183
     */
184
    public function getTemplate($template_reference)
185
    {
186
        return isset($this->jsdata['templates'], $this->jsdata['templates'][$template_reference])
187
            ? $this->jsdata['templates'][$template_reference]
188
            : '';
189
    }
190
191
192
    /**
193
     * Retrieve registered data.
194
     *
195
     * @param string $key           Name of key to attach data to.
196
     * @return mixed                If there is no for the given key, then false is returned.
197
     */
198
    public function getData($key)
199
    {
200
        return isset($this->jsdata[$key])
201
            ? $this->jsdata[$key]
202
            : false;
203
    }
204
205
206
207
208
    /**
209
     * Verifies whether the given data exists already on the jsdata array.
210
     *
211
     * Overriding data is not allowed.
212
     *
213
     * @param string       $key          Index for data.
214
     * @return bool        If valid then return true.
215
     * @throws InvalidArgumentException if data already exists.
216
     */
217
    protected function verifyDataNotExisting($key)
218
    {
219
        if (isset($this->jsdata[$key])) {
220
            if (is_array($this->jsdata[$key])) {
221
                throw new InvalidArgumentException(
222
                    sprintf(
223
                        __(
224
                            'The value for %1$s already exists in the Registry::eejs object.
225
                            Overrides are not allowed. Since the value of this data is an array, you may want to use the
226
                            %2$s method to push your value to the array.',
227
                            'event_espresso'
228
                        ),
229
                        $key,
230
                        'pushData()'
231
                    )
232
                );
233
            } else {
234
                throw new InvalidArgumentException(
235
                    sprintf(
236
                        __(
237
                            'The value for %1$s already exists in the Registry::eejs object. Overrides are not
238
                            allowed.  Consider attaching your value to a different key',
239
                            'event_espresso'
240
                        ),
241
                        $key
242
                    )
243
                );
244
            }
245
        }
246
        return true;
247
    }
248
}
249