Completed
Branch FET-10306-introduce_Scripts_cl... (be58f2)
by
unknown
21:17 queued 11:07
created
core/services/assets/Registry.php 2 patches
Indentation   +222 added lines, -223 removed lines patch added patch discarded remove patch
@@ -16,232 +16,231 @@
 block discarded – undo
16 16
 class Registry
17 17
 {
18 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/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.  To help enforce uniqueness, key/value pairs are added as an element
59
-     * of the method/function name for the caller.  So for instance, if this coded is called from a class, something like
60
-     * this would be done:
61
-
62
-     * EE_Registry::instance()->AssetsRegistry->addData(__METHOD__, 'my_key', 'my_value');
63
-     *
64
-     * If this is being called within a function then you'd do:
65
-     *
66
-     * EE_Registry::instance()->AssetsRegistry->addData(__FUNCTION__, 'my_key', 'my_value');
67
-     *
68
-     * This method WILL validate whether the incoming method/function name is an actual function or method.
69
-     *
70
-     * Since `::` and `\` is not accessible via javascript object dot notation. Umlauts (::) will be escaped with a
71
-     * double underscore (__) and if the method belongs to a namespaced class, the backslashes will be escaped with a
72
-     * single underscore (_).  So if your method was a part of namespaced class resulting in something like this:
73
-     * EventEspresso\some\namespace\Class::myMethod
74
-     *
75
-     * ...then you would be able to access all the data attached to it in your js via:
76
-     *
77
-     * var myData = eejs.data.EventEspresso_some_namespace_Class__myMethod
78
-     *
79
-     *
80
-     * @param string $method_name  Name of actual method or function.
81
-     * @param string $key          Key used to access your data
82
-     * @param string|array $value  Value to attach to key
83
-     * @throws \InvalidArgumentException
84
-     */
85
-    public function addData($method_name, $key, $value)
86
-    {
87
-        $this->verifyMethod($method_name);
88
-
89
-        //convert method name into js friendly string
90
-        $method_name = $this->methodNameToJsFriendlyString($method_name);
91
-        if ($this->verifyDataNotExisting($method_name, $key, $value)) {
92
-            $this->jsdata[$method_name][$key] = $value;
93
-        }
94
-    }
95
-
96
-
97
-    /**
98
-     * Similar to addData except this allows for users to push values to an existing key where the values on key are
99
-     * elements in an array.
100
-     *
101
-     * When you use this method, the value you include will be appended to the end of an array on $key.
102
-     *
103
-     * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object
104
-     * like this,
105
-     *
106
-     * eejs.data.function_name.test = [
107
-     *     my_data,
108
-     * ]
109
-     *
110
-     * If there has already been a scalar value attached to the data object for the given method_name, then
111
-     * this will throw an exception.
112
-     *
113
-     * @see Registry::addData for more info on purpose of $method_name
114
-     *
115
-     * @param string $method_name  Name of actual method or function.
116
-     * @param string $key          Key to attach data to.
117
-     * @param string|array $value  Value being registered.
118
-     * @throws InvalidArgumentException
119
-     */
120
-    public function pushData($method_name, $key, $value)
121
-    {
122
-        $this->verifyMethod($method_name);
123
-        $method_name = $this->methodNameToJsFriendlyString($method_name);
124
-
125
-        if (isset($this->jsdata[$method_name], $this->jsdata[$method_name][$key])
126
-            && ! is_array($this->jsdata[$method_name][$key])
127
-        ) {
128
-            throw new invalidArgumentException(
129
-                sprintf(
130
-                    __(
131
-                        'The value for %1$s is already set and it is not an array. The %2$s method can only be used to
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/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.  To help enforce uniqueness, key/value pairs are added as an element
59
+	 * of the method/function name for the caller.  So for instance, if this coded is called from a class, something like
60
+	 * this would be done:
61
+	 * EE_Registry::instance()->AssetsRegistry->addData(__METHOD__, 'my_key', 'my_value');
62
+	 *
63
+	 * If this is being called within a function then you'd do:
64
+	 *
65
+	 * EE_Registry::instance()->AssetsRegistry->addData(__FUNCTION__, 'my_key', 'my_value');
66
+	 *
67
+	 * This method WILL validate whether the incoming method/function name is an actual function or method.
68
+	 *
69
+	 * Since `::` and `\` is not accessible via javascript object dot notation. Umlauts (::) will be escaped with a
70
+	 * double underscore (__) and if the method belongs to a namespaced class, the backslashes will be escaped with a
71
+	 * single underscore (_).  So if your method was a part of namespaced class resulting in something like this:
72
+	 * EventEspresso\some\namespace\Class::myMethod
73
+	 *
74
+	 * ...then you would be able to access all the data attached to it in your js via:
75
+	 *
76
+	 * var myData = eejs.data.EventEspresso_some_namespace_Class__myMethod
77
+	 *
78
+	 *
79
+	 * @param string $method_name  Name of actual method or function.
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($method_name, $key, $value)
85
+	{
86
+		$this->verifyMethod($method_name);
87
+
88
+		//convert method name into js friendly string
89
+		$method_name = $this->methodNameToJsFriendlyString($method_name);
90
+		if ($this->verifyDataNotExisting($method_name, $key, $value)) {
91
+			$this->jsdata[$method_name][$key] = $value;
92
+		}
93
+	}
94
+
95
+
96
+	/**
97
+	 * Similar to addData except this allows for users to push values to an existing key where the values on key are
98
+	 * elements in an array.
99
+	 *
100
+	 * When you use this method, the value you include will be appended to the end of an array on $key.
101
+	 *
102
+	 * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object
103
+	 * like this,
104
+	 *
105
+	 * eejs.data.function_name.test = [
106
+	 *     my_data,
107
+	 * ]
108
+	 *
109
+	 * If there has already been a scalar value attached to the data object for the given method_name, then
110
+	 * this will throw an exception.
111
+	 *
112
+	 * @see Registry::addData for more info on purpose of $method_name
113
+	 *
114
+	 * @param string $method_name  Name of actual method or function.
115
+	 * @param string $key          Key to attach data to.
116
+	 * @param string|array $value  Value being registered.
117
+	 * @throws InvalidArgumentException
118
+	 */
119
+	public function pushData($method_name, $key, $value)
120
+	{
121
+		$this->verifyMethod($method_name);
122
+		$method_name = $this->methodNameToJsFriendlyString($method_name);
123
+
124
+		if (isset($this->jsdata[$method_name], $this->jsdata[$method_name][$key])
125
+			&& ! is_array($this->jsdata[$method_name][$key])
126
+		) {
127
+			throw new invalidArgumentException(
128
+				sprintf(
129
+					__(
130
+						'The value for %1$s is already set and it is not an array. The %2$s method can only be used to
132 131
                          push values to this data element when it is an array.',
133
-                        'event_espresso'
134
-                    ),
135
-                    $key,
136
-                    __METHOD__
137
-                )
138
-            );
139
-        }
140
-
141
-        $this->jsdata[$method_name][$key][] = $value;
142
-    }
143
-
144
-
145
-    /**
146
-     * Retrieve registered data.
147
-     *
148
-     * @param string $method_name   Name of actual method or function.
149
-     * @param string $key           Name of key to attach data to.
150
-     * @return mixed                If there is no for the given key, then false is returned.
151
-     */
152
-    public function getData($method_name, $key)
153
-    {
154
-        $method_name = $this->methodNameToJsFriendlyString($method_name);
155
-        return isset($this->jsdata[$method_name], $this->jsdata[$method_name][$key])
156
-            ? $this->jsdata[$method_name][$key]
157
-            : false;
158
-    }
159
-
160
-
161
-    /**
162
-     * Verifies the given string represents an actual function or method.
163
-     * @param string $method_name
164
-     * @throws InvalidArgumentException
165
-     */
166
-    protected function verifyMethod($method_name)
167
-    {
168
-        if (! is_callable($method_name)) {
169
-            throw new InvalidArgumentException(
170
-                sprintf(
171
-                    __('The value for $method_name (%1$s) is not valid.  You must send in a valid function/method name that 
132
+						'event_espresso'
133
+					),
134
+					$key,
135
+					__METHOD__
136
+				)
137
+			);
138
+		}
139
+
140
+		$this->jsdata[$method_name][$key][] = $value;
141
+	}
142
+
143
+
144
+	/**
145
+	 * Retrieve registered data.
146
+	 *
147
+	 * @param string $method_name   Name of actual method or function.
148
+	 * @param string $key           Name of key to attach data to.
149
+	 * @return mixed                If there is no for the given key, then false is returned.
150
+	 */
151
+	public function getData($method_name, $key)
152
+	{
153
+		$method_name = $this->methodNameToJsFriendlyString($method_name);
154
+		return isset($this->jsdata[$method_name], $this->jsdata[$method_name][$key])
155
+			? $this->jsdata[$method_name][$key]
156
+			: false;
157
+	}
158
+
159
+
160
+	/**
161
+	 * Verifies the given string represents an actual function or method.
162
+	 * @param string $method_name
163
+	 * @throws InvalidArgumentException
164
+	 */
165
+	protected function verifyMethod($method_name)
166
+	{
167
+		if (! is_callable($method_name)) {
168
+			throw new InvalidArgumentException(
169
+				sprintf(
170
+					__('The value for $method_name (%1$s) is not valid.  You must send in a valid function/method name that 
172 171
                     your key value pair for the data will be attached to.', 'event_espresso'),
173
-                    $method_name
174
-                )
175
-            );
176
-        }
177
-    }
178
-
179
-
180
-
181
-
182
-    /**
183
-     * Verifies whether the given data exists already on the jsdata array.
184
-     *
185
-     * Overriding data is not allowed.
186
-     *
187
-     * @param string       $method_name  Name representing actual function or method.
188
-     * @param string       $key          Index for data.
189
-     * @param string|array $value        Value being stored.
190
-     * @return bool        If valid then return true.
191
-     * @throws InvalidArgumentException if data already exists.
192
-     */
193
-    protected function verifyDataNotExisting($method_name, $key, $value)
194
-    {
195
-        if (isset($this->jsdata[$method_name], $this->jsdata[$method_name][$key])) {
196
-            if (is_array($this->jsdata[$method_name][$key])) {
197
-                throw new InvalidArgumentException(
198
-                    sprintf(
199
-                        __(
200
-                            'The value for %1$s already exists in the Registry::eejs object for %2$s.
172
+					$method_name
173
+				)
174
+			);
175
+		}
176
+	}
177
+
178
+
179
+
180
+
181
+	/**
182
+	 * Verifies whether the given data exists already on the jsdata array.
183
+	 *
184
+	 * Overriding data is not allowed.
185
+	 *
186
+	 * @param string       $method_name  Name representing actual function or method.
187
+	 * @param string       $key          Index for data.
188
+	 * @param string|array $value        Value being stored.
189
+	 * @return bool        If valid then return true.
190
+	 * @throws InvalidArgumentException if data already exists.
191
+	 */
192
+	protected function verifyDataNotExisting($method_name, $key, $value)
193
+	{
194
+		if (isset($this->jsdata[$method_name], $this->jsdata[$method_name][$key])) {
195
+			if (is_array($this->jsdata[$method_name][$key])) {
196
+				throw new InvalidArgumentException(
197
+					sprintf(
198
+						__(
199
+							'The value for %1$s already exists in the Registry::eejs object for %2$s.
201 200
                             Overrides are not allowed. Since the value of this data is an array, you may want to use the
202 201
                             %2$s method to push your value to the array.',
203
-                            'event_espresso'
204
-                        ),
205
-                        $key,
206
-                        $method_name,
207
-                        'pushData'
208
-                    )
209
-                );
210
-            } else {
211
-                throw new InvalidArgumentException(
212
-                    sprintf(
213
-                        __(
214
-                            'The value for %1$s already exists in the Registry::eejs object for %2$s. Overrides are not
202
+							'event_espresso'
203
+						),
204
+						$key,
205
+						$method_name,
206
+						'pushData'
207
+					)
208
+				);
209
+			} else {
210
+				throw new InvalidArgumentException(
211
+					sprintf(
212
+						__(
213
+							'The value for %1$s already exists in the Registry::eejs object for %2$s. Overrides are not
215 214
                             allowed.  Consider attaching your value to a different key',
216
-                            'event_espresso'
217
-                        ),
218
-                        $key,
219
-                        $method_name
220
-                    )
221
-                );
222
-            }
223
-        }
224
-        return true;
225
-    }
226
-
227
-
228
-
229
-
230
-    /**
231
-     * Escapes backslash and umlauts existing in given string.
232
-     *
233
-     * '::' becomes '__'
234
-     * '\'  becomes '_'
235
-     *
236
-     * @param string $method_name  The string to escape.
237
-     * @return string
238
-     */
239
-    protected function methodNameToJsFriendlyString($method_name)
240
-    {
241
-        //all `\` from namespaces gets converted to single underscore
242
-        $method_name = str_replace('\\', '_', $method_name);
243
-
244
-        //all `::` for class::method formats gets converted to double underscore.
245
-        return str_replace('::', '__', $method_name);
246
-    }
215
+							'event_espresso'
216
+						),
217
+						$key,
218
+						$method_name
219
+					)
220
+				);
221
+			}
222
+		}
223
+		return true;
224
+	}
225
+
226
+
227
+
228
+
229
+	/**
230
+	 * Escapes backslash and umlauts existing in given string.
231
+	 *
232
+	 * '::' becomes '__'
233
+	 * '\'  becomes '_'
234
+	 *
235
+	 * @param string $method_name  The string to escape.
236
+	 * @return string
237
+	 */
238
+	protected function methodNameToJsFriendlyString($method_name)
239
+	{
240
+		//all `\` from namespaces gets converted to single underscore
241
+		$method_name = str_replace('\\', '_', $method_name);
242
+
243
+		//all `::` for class::method formats gets converted to double underscore.
244
+		return str_replace('::', '__', $method_name);
245
+	}
247 246
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
     {
44 44
         wp_register_script(
45 45
             'eejs-core',
46
-            EE_PLUGIN_DIR_URL . 'core/services/core_assets/eejs-core.js',
46
+            EE_PLUGIN_DIR_URL.'core/services/core_assets/eejs-core.js',
47 47
             array(),
48 48
             espresso_version(),
49 49
             true
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
      */
166 166
     protected function verifyMethod($method_name)
167 167
     {
168
-        if (! is_callable($method_name)) {
168
+        if ( ! is_callable($method_name)) {
169 169
             throw new InvalidArgumentException(
170 170
                 sprintf(
171 171
                     __('The value for $method_name (%1$s) is not valid.  You must send in a valid function/method name that 
Please login to merge, or discard this patch.