Completed
Branch FET-10304-welcome-to-vue (781cd5)
by
unknown
167:16 queued 156:05
created

Registry::scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 1
b 0
f 1
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
    }
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
52
        //js.api
53
        wp_register_script(
54
            'eejs-api',
55
            EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js',
56
            array('underscore','eejs-core'),
57
            espresso_version(),
58
            true
59
        );
60
        $this->jsdata['paths'] = array('rest_route' => rest_url('ee/v4.8.36/'));
61
62
        wp_localize_script('eejs-core', 'eejs', array('data'=>$this->jsdata));
63
    }
64
65
66
    /**
67
     * Used to add data to eejs.data object.
68
     *
69
     * Note:  Overriding existing data is not allowed.
70
     *
71
     * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript.
72
     * If the data you add is something like this:
73
     *
74
     * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) );
75
     *
76
     * It will be exposed in the page source as:
77
     *
78
     * eejs.data.my_plugin_data.foo == gar
79
     *
80
     * @param string $key          Key used to access your data
81
     * @param string|array $value  Value to attach to key
82
     * @throws \InvalidArgumentException
83
     */
84
    public function addData($key, $value)
85
    {
86
        if ($this->verifyDataNotExisting($key)) {
87
            $this->jsdata[$key] = $value;
88
        }
89
    }
90
91
92
    /**
93
     * Similar to addData except this allows for users to push values to an existing key where the values on key are
94
     * elements in an array.
95
     *
96
     * When you use this method, the value you include will be appended to the end of an array on $key.
97
     *
98
     * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object
99
     * like this,
100
     *
101
     * eejs.data.test = [
102
     *     my_data,
103
     * ]
104
     *
105
     * If there has already been a scalar value attached to the data object given key, then
106
     * this will throw an exception.
107
     *
108
     * @param string $key          Key to attach data to.
109
     * @param string|array $value  Value being registered.
110
     * @throws InvalidArgumentException
111
     */
112
    public function pushData($key, $value)
113
    {
114
        if (isset($this->jsdata[$key])
115
            && ! is_array($this->jsdata[$key])
116
        ) {
117
            throw new invalidArgumentException(
118
                sprintf(
119
                    __(
120
                        'The value for %1$s is already set and it is not an array. The %2$s method can only be used to
121
                         push values to this data element when it is an array.',
122
                        'event_espresso'
123
                    ),
124
                    $key,
125
                    __METHOD__
126
                )
127
            );
128
        }
129
130
        $this->jsdata[$key][] = $value;
131
    }
132
133
134
    /**
135
     * Used to set content used by javascript for a template.
136
     *
137
     * Note: Overrides of existing registered templates are not allowed.
138
     *
139
     * @param string $template_reference
140
     * @param string $template_content
141
     */
142
    public function addTemplate($template_reference, $template_content)
143
    {
144
        if (! isset($this->jsdata['templates'])) {
145
            $this->jsdata['templates'] = array();
146
        }
147
148
        //no overrides allowed.
149
        if (isset($this->jsdata['templates'][$template_reference])) {
150
            throw new invalidArgumentException(
151
                sprintf(
152
                    __(
153
                        'The %1$s key already exists for the templates array in the js data array.  No overrides are allowed.',
154
                        'event_espresso'
155
                    ),
156
                    $template_reference
157
                )
158
            );
159
        } else {
160
            $this->jsdata['templates'][$template_reference] = $template_content;
161
        }
162
    }
163
164
165
    /**
166
     * Retrieve the template content already registered for the given reference.
167
     * @param string $template_reference
168
     * @return ''
0 ignored issues
show
Documentation introduced by
The doc-type '' could not be parsed: Unknown type name "''" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
169
     */
170
    public function getTemplate($template_reference)
171
    {
172
        return isset($this->jsdata['templates'], $this->jsdata['templates'][$template_reference])
173
            ? $this->jsdata['templates'][$template_reference]
174
            : '';
175
    }
176
177
178
    /**
179
     * Retrieve registered data.
180
     *
181
     * @param string $key           Name of key to attach data to.
182
     * @return mixed                If there is no for the given key, then false is returned.
183
     */
184
    public function getData($key)
185
    {
186
        return isset($this->jsdata[$key])
187
            ? $this->jsdata[$key]
188
            : false;
189
    }
190
191
192
193
194
    /**
195
     * Verifies whether the given data exists already on the jsdata array.
196
     *
197
     * Overriding data is not allowed.
198
     *
199
     * @param string       $key          Index for data.
200
     * @return bool        If valid then return true.
201
     * @throws InvalidArgumentException if data already exists.
202
     */
203
    protected function verifyDataNotExisting($key)
204
    {
205
        if (isset($this->jsdata[$key])) {
206
            if (is_array($this->jsdata[$key])) {
207
                throw new InvalidArgumentException(
208
                    sprintf(
209
                        __(
210
                            'The value for %1$s already exists in the Registry::eejs object.
211
                            Overrides are not allowed. Since the value of this data is an array, you may want to use the
212
                            %2$s method to push your value to the array.',
213
                            'event_espresso'
214
                        ),
215
                        $key,
216
                        'pushData()'
217
                    )
218
                );
219
            } else {
220
                throw new InvalidArgumentException(
221
                    sprintf(
222
                        __(
223
                            'The value for %1$s already exists in the Registry::eejs object. Overrides are not
224
                            allowed.  Consider attaching your value to a different key',
225
                            'event_espresso'
226
                        ),
227
                        $key
228
                    )
229
                );
230
            }
231
        }
232
        return true;
233
    }
234
}
235