Completed
Branch FET/remove-url-validation (fbb4fb)
by
unknown
53:36 queued 44:50
created
core/services/assets/Registry.php 2 patches
Indentation   +583 added lines, -583 removed lines patch added patch discarded remove patch
@@ -23,594 +23,594 @@
 block discarded – undo
23 23
 class Registry
24 24
 {
25 25
 
26
-    const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json';
27
-
28
-    /**
29
-     * @var AssetCollection $assets
30
-     */
31
-    protected $assets;
32
-
33
-    /**
34
-     * @var I18nRegistry
35
-     */
36
-    private $i18n_registry;
37
-
38
-    /**
39
-     * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script.
40
-     *
41
-     * @var array
42
-     */
43
-    protected $jsdata = array();
44
-
45
-    /**
46
-     * This keeps track of all scripts with registered data.  It is used to prevent duplicate data objects setup in the
47
-     * page source.
48
-     *
49
-     * @var array
50
-     */
51
-    private $script_handles_with_data = array();
52
-
53
-
54
-    /**
55
-     * Holds the manifest data obtained from registered manifest files.
56
-     * Manifests are maps of asset chunk name to actual built asset file names.
57
-     * Shape of this array is:
58
-     * array(
59
-     *  'some_namespace_slug' => array(
60
-     *      'some_chunk_name' => array(
61
-     *          'js' => 'filename.js'
62
-     *          'css' => 'filename.js'
63
-     *      ),
64
-     *      'url_base' => 'https://baseurl.com/to/assets
65
-     *  )
66
-     * )
67
-     *
68
-     * @var array
69
-     */
70
-    private $manifest_data = array();
71
-
72
-
73
-    /**
74
-     * Registry constructor.
75
-     * Hooking into WP actions for script registry.
76
-     *
77
-     * @param AssetCollection $assets
78
-     * @param I18nRegistry    $i18n_registry
79
-     */
80
-    public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry)
81
-    {
82
-        $this->assets = $assets;
83
-        $this->i18n_registry = $i18n_registry;
84
-        add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1);
85
-        add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1);
86
-        add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3);
87
-        add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3);
88
-        add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4);
89
-        add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4);
90
-        add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1);
91
-        add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1);
92
-    }
93
-
94
-
95
-    /**
96
-     * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n
97
-     * translation handling.
98
-     *
99
-     * @return I18nRegistry
100
-     */
101
-    public function getI18nRegistry()
102
-    {
103
-        return $this->i18n_registry;
104
-    }
105
-
106
-
107
-    /**
108
-     * Callback for the wp_enqueue_scripts actions used to register assets.
109
-     *
110
-     * @since 4.9.62.p
111
-     * @throws Exception
112
-     */
113
-    public function registerScriptsAndStyles()
114
-    {
115
-        try {
116
-            $this->registerScripts($this->assets->getJavascriptAssets());
117
-            $this->registerStyles($this->assets->getStylesheetAssets());
118
-        } catch (Exception $exception) {
119
-            new ExceptionStackTraceDisplay($exception);
120
-        }
121
-    }
122
-
123
-
124
-    /**
125
-     * Registers JS assets with WP core
126
-     *
127
-     * @since 4.9.62.p
128
-     * @param JavascriptAsset[] $scripts
129
-     * @throws AssetRegistrationException
130
-     * @throws InvalidDataTypeException
131
-     */
132
-    public function registerScripts(array $scripts)
133
-    {
134
-        foreach ($scripts as $script) {
135
-            // skip to next script if this has already been done
136
-            if ($script->isRegistered()) {
137
-                continue;
138
-            }
139
-            do_action(
140
-                'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script',
141
-                $script
142
-            );
143
-            $registered = wp_register_script(
144
-                $script->handle(),
145
-                $script->source(),
146
-                $script->dependencies(),
147
-                $script->version(),
148
-                $script->loadInFooter()
149
-            );
150
-            if (! $registered && $this->debug()) {
151
-                throw new AssetRegistrationException($script->handle());
152
-            }
153
-            $script->setRegistered($registered);
154
-            if ($script->requiresTranslation()) {
155
-                $this->registerTranslation($script->handle());
156
-            }
157
-            do_action(
158
-                'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script',
159
-                $script
160
-            );
161
-        }
162
-    }
163
-
164
-
165
-    /**
166
-     * Registers CSS assets with WP core
167
-     *
168
-     * @since 4.9.62.p
169
-     * @param StylesheetAsset[] $styles
170
-     * @throws InvalidDataTypeException
171
-     */
172
-    public function registerStyles(array $styles)
173
-    {
174
-        foreach ($styles as $style) {
175
-            // skip to next style if this has already been done
176
-            if ($style->isRegistered()) {
177
-                continue;
178
-            }
179
-            do_action(
180
-                'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style',
181
-                $style
182
-            );
183
-            wp_register_style(
184
-                $style->handle(),
185
-                $style->source(),
186
-                $style->dependencies(),
187
-                $style->version(),
188
-                $style->media()
189
-            );
190
-            $style->setRegistered();
191
-            do_action(
192
-                'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style',
193
-                $style
194
-            );
195
-        }
196
-    }
197
-
198
-
199
-    /**
200
-     * Call back for the script print in frontend and backend.
201
-     * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point.
202
-     *
203
-     * @since 4.9.31.rc.015
204
-     */
205
-    public function enqueueData()
206
-    {
207
-        $this->removeAlreadyRegisteredDataForScriptHandles();
208
-        wp_add_inline_script(
209
-            'eejs-core',
210
-            'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)),
211
-            'before'
212
-        );
213
-        $scripts = $this->assets->getJavascriptAssetsWithData();
214
-        foreach ($scripts as $script) {
215
-            $this->addRegisteredScriptHandlesWithData($script->handle());
216
-            if ($script->hasInlineDataCallback()) {
217
-                $localize = $script->inlineDataCallback();
218
-                $localize();
219
-            }
220
-        }
221
-    }
222
-
223
-
224
-    /**
225
-     * Used to add data to eejs.data object.
226
-     * Note:  Overriding existing data is not allowed.
227
-     * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript.
228
-     * If the data you add is something like this:
229
-     *  $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) );
230
-     * It will be exposed in the page source as:
231
-     *  eejs.data.my_plugin_data.foo == gar
232
-     *
233
-     * @param string       $key   Key used to access your data
234
-     * @param string|array $value Value to attach to key
235
-     * @throws InvalidArgumentException
236
-     */
237
-    public function addData($key, $value)
238
-    {
239
-        if ($this->verifyDataNotExisting($key)) {
240
-            $this->jsdata[ $key ] = $value;
241
-        }
242
-    }
243
-
244
-
245
-    /**
246
-     * Similar to addData except this allows for users to push values to an existing key where the values on key are
247
-     * elements in an array.
248
-     *
249
-     * When you use this method, the value you include will be merged with the array on $key.
250
-     * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript
251
-     * object like this, eejs.data.test = [ my_data,
252
-     * ]
253
-     * If there has already been a scalar value attached to the data object given key (via addData for instance), then
254
-     * this will throw an exception.
255
-     *
256
-     * Caution: Only add data using this method if you are okay with the potential for additional data added on the same
257
-     * key potentially overriding the existing data on merge (specifically with associative arrays).
258
-     *
259
-     * @param string       $key   Key to attach data to.
260
-     * @param string|array $value Value being registered.
261
-     * @throws InvalidArgumentException
262
-     */
263
-    public function pushData($key, $value)
264
-    {
265
-        if (isset($this->jsdata[ $key ])
266
-            && ! is_array($this->jsdata[ $key ])
267
-        ) {
268
-            if (! $this->debug()) {
269
-                return;
270
-            }
271
-            throw new InvalidArgumentException(
272
-                sprintf(
273
-                    __(
274
-                        'The value for %1$s is already set and it is not an array. The %2$s method can only be used to
26
+	const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json';
27
+
28
+	/**
29
+	 * @var AssetCollection $assets
30
+	 */
31
+	protected $assets;
32
+
33
+	/**
34
+	 * @var I18nRegistry
35
+	 */
36
+	private $i18n_registry;
37
+
38
+	/**
39
+	 * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script.
40
+	 *
41
+	 * @var array
42
+	 */
43
+	protected $jsdata = array();
44
+
45
+	/**
46
+	 * This keeps track of all scripts with registered data.  It is used to prevent duplicate data objects setup in the
47
+	 * page source.
48
+	 *
49
+	 * @var array
50
+	 */
51
+	private $script_handles_with_data = array();
52
+
53
+
54
+	/**
55
+	 * Holds the manifest data obtained from registered manifest files.
56
+	 * Manifests are maps of asset chunk name to actual built asset file names.
57
+	 * Shape of this array is:
58
+	 * array(
59
+	 *  'some_namespace_slug' => array(
60
+	 *      'some_chunk_name' => array(
61
+	 *          'js' => 'filename.js'
62
+	 *          'css' => 'filename.js'
63
+	 *      ),
64
+	 *      'url_base' => 'https://baseurl.com/to/assets
65
+	 *  )
66
+	 * )
67
+	 *
68
+	 * @var array
69
+	 */
70
+	private $manifest_data = array();
71
+
72
+
73
+	/**
74
+	 * Registry constructor.
75
+	 * Hooking into WP actions for script registry.
76
+	 *
77
+	 * @param AssetCollection $assets
78
+	 * @param I18nRegistry    $i18n_registry
79
+	 */
80
+	public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry)
81
+	{
82
+		$this->assets = $assets;
83
+		$this->i18n_registry = $i18n_registry;
84
+		add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1);
85
+		add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1);
86
+		add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3);
87
+		add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3);
88
+		add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4);
89
+		add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4);
90
+		add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1);
91
+		add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1);
92
+	}
93
+
94
+
95
+	/**
96
+	 * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n
97
+	 * translation handling.
98
+	 *
99
+	 * @return I18nRegistry
100
+	 */
101
+	public function getI18nRegistry()
102
+	{
103
+		return $this->i18n_registry;
104
+	}
105
+
106
+
107
+	/**
108
+	 * Callback for the wp_enqueue_scripts actions used to register assets.
109
+	 *
110
+	 * @since 4.9.62.p
111
+	 * @throws Exception
112
+	 */
113
+	public function registerScriptsAndStyles()
114
+	{
115
+		try {
116
+			$this->registerScripts($this->assets->getJavascriptAssets());
117
+			$this->registerStyles($this->assets->getStylesheetAssets());
118
+		} catch (Exception $exception) {
119
+			new ExceptionStackTraceDisplay($exception);
120
+		}
121
+	}
122
+
123
+
124
+	/**
125
+	 * Registers JS assets with WP core
126
+	 *
127
+	 * @since 4.9.62.p
128
+	 * @param JavascriptAsset[] $scripts
129
+	 * @throws AssetRegistrationException
130
+	 * @throws InvalidDataTypeException
131
+	 */
132
+	public function registerScripts(array $scripts)
133
+	{
134
+		foreach ($scripts as $script) {
135
+			// skip to next script if this has already been done
136
+			if ($script->isRegistered()) {
137
+				continue;
138
+			}
139
+			do_action(
140
+				'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script',
141
+				$script
142
+			);
143
+			$registered = wp_register_script(
144
+				$script->handle(),
145
+				$script->source(),
146
+				$script->dependencies(),
147
+				$script->version(),
148
+				$script->loadInFooter()
149
+			);
150
+			if (! $registered && $this->debug()) {
151
+				throw new AssetRegistrationException($script->handle());
152
+			}
153
+			$script->setRegistered($registered);
154
+			if ($script->requiresTranslation()) {
155
+				$this->registerTranslation($script->handle());
156
+			}
157
+			do_action(
158
+				'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script',
159
+				$script
160
+			);
161
+		}
162
+	}
163
+
164
+
165
+	/**
166
+	 * Registers CSS assets with WP core
167
+	 *
168
+	 * @since 4.9.62.p
169
+	 * @param StylesheetAsset[] $styles
170
+	 * @throws InvalidDataTypeException
171
+	 */
172
+	public function registerStyles(array $styles)
173
+	{
174
+		foreach ($styles as $style) {
175
+			// skip to next style if this has already been done
176
+			if ($style->isRegistered()) {
177
+				continue;
178
+			}
179
+			do_action(
180
+				'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style',
181
+				$style
182
+			);
183
+			wp_register_style(
184
+				$style->handle(),
185
+				$style->source(),
186
+				$style->dependencies(),
187
+				$style->version(),
188
+				$style->media()
189
+			);
190
+			$style->setRegistered();
191
+			do_action(
192
+				'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style',
193
+				$style
194
+			);
195
+		}
196
+	}
197
+
198
+
199
+	/**
200
+	 * Call back for the script print in frontend and backend.
201
+	 * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point.
202
+	 *
203
+	 * @since 4.9.31.rc.015
204
+	 */
205
+	public function enqueueData()
206
+	{
207
+		$this->removeAlreadyRegisteredDataForScriptHandles();
208
+		wp_add_inline_script(
209
+			'eejs-core',
210
+			'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)),
211
+			'before'
212
+		);
213
+		$scripts = $this->assets->getJavascriptAssetsWithData();
214
+		foreach ($scripts as $script) {
215
+			$this->addRegisteredScriptHandlesWithData($script->handle());
216
+			if ($script->hasInlineDataCallback()) {
217
+				$localize = $script->inlineDataCallback();
218
+				$localize();
219
+			}
220
+		}
221
+	}
222
+
223
+
224
+	/**
225
+	 * Used to add data to eejs.data object.
226
+	 * Note:  Overriding existing data is not allowed.
227
+	 * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript.
228
+	 * If the data you add is something like this:
229
+	 *  $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) );
230
+	 * It will be exposed in the page source as:
231
+	 *  eejs.data.my_plugin_data.foo == gar
232
+	 *
233
+	 * @param string       $key   Key used to access your data
234
+	 * @param string|array $value Value to attach to key
235
+	 * @throws InvalidArgumentException
236
+	 */
237
+	public function addData($key, $value)
238
+	{
239
+		if ($this->verifyDataNotExisting($key)) {
240
+			$this->jsdata[ $key ] = $value;
241
+		}
242
+	}
243
+
244
+
245
+	/**
246
+	 * Similar to addData except this allows for users to push values to an existing key where the values on key are
247
+	 * elements in an array.
248
+	 *
249
+	 * When you use this method, the value you include will be merged with the array on $key.
250
+	 * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript
251
+	 * object like this, eejs.data.test = [ my_data,
252
+	 * ]
253
+	 * If there has already been a scalar value attached to the data object given key (via addData for instance), then
254
+	 * this will throw an exception.
255
+	 *
256
+	 * Caution: Only add data using this method if you are okay with the potential for additional data added on the same
257
+	 * key potentially overriding the existing data on merge (specifically with associative arrays).
258
+	 *
259
+	 * @param string       $key   Key to attach data to.
260
+	 * @param string|array $value Value being registered.
261
+	 * @throws InvalidArgumentException
262
+	 */
263
+	public function pushData($key, $value)
264
+	{
265
+		if (isset($this->jsdata[ $key ])
266
+			&& ! is_array($this->jsdata[ $key ])
267
+		) {
268
+			if (! $this->debug()) {
269
+				return;
270
+			}
271
+			throw new InvalidArgumentException(
272
+				sprintf(
273
+					__(
274
+						'The value for %1$s is already set and it is not an array. The %2$s method can only be used to
275 275
                          push values to this data element when it is an array.',
276
-                        'event_espresso'
277
-                    ),
278
-                    $key,
279
-                    __METHOD__
280
-                )
281
-            );
282
-        }
283
-        if ( ! isset( $this->jsdata[ $key ] ) ) {
284
-            $this->jsdata[ $key ] = is_array($value) ? $value : [$value];
285
-        } else {
286
-            $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value);
287
-        }
288
-    }
289
-
290
-
291
-    /**
292
-     * Used to set content used by javascript for a template.
293
-     * Note: Overrides of existing registered templates are not allowed.
294
-     *
295
-     * @param string $template_reference
296
-     * @param string $template_content
297
-     * @throws InvalidArgumentException
298
-     */
299
-    public function addTemplate($template_reference, $template_content)
300
-    {
301
-        if (! isset($this->jsdata['templates'])) {
302
-            $this->jsdata['templates'] = array();
303
-        }
304
-        //no overrides allowed.
305
-        if (isset($this->jsdata['templates'][ $template_reference ])) {
306
-            if (! $this->debug()) {
307
-                return;
308
-            }
309
-            throw new InvalidArgumentException(
310
-                sprintf(
311
-                    __(
312
-                        'The %1$s key already exists for the templates array in the js data array.  No overrides are allowed.',
313
-                        'event_espresso'
314
-                    ),
315
-                    $template_reference
316
-                )
317
-            );
318
-        }
319
-        $this->jsdata['templates'][ $template_reference ] = $template_content;
320
-    }
321
-
322
-
323
-    /**
324
-     * Retrieve the template content already registered for the given reference.
325
-     *
326
-     * @param string $template_reference
327
-     * @return string
328
-     */
329
-    public function getTemplate($template_reference)
330
-    {
331
-        return isset($this->jsdata['templates'][ $template_reference ])
332
-            ? $this->jsdata['templates'][ $template_reference ]
333
-            : '';
334
-    }
335
-
336
-
337
-    /**
338
-     * Retrieve registered data.
339
-     *
340
-     * @param string $key Name of key to attach data to.
341
-     * @return mixed                If there is no for the given key, then false is returned.
342
-     */
343
-    public function getData($key)
344
-    {
345
-        return isset($this->jsdata[ $key ])
346
-            ? $this->jsdata[ $key ]
347
-            : false;
348
-    }
349
-
350
-
351
-    /**
352
-     * Verifies whether the given data exists already on the jsdata array.
353
-     * Overriding data is not allowed.
354
-     *
355
-     * @param string $key Index for data.
356
-     * @return bool        If valid then return true.
357
-     * @throws InvalidArgumentException if data already exists.
358
-     */
359
-    protected function verifyDataNotExisting($key)
360
-    {
361
-        if (isset($this->jsdata[ $key ])) {
362
-            if (! $this->debug()) {
363
-                return false;
364
-            }
365
-            if (is_array($this->jsdata[ $key ])) {
366
-                throw new InvalidArgumentException(
367
-                    sprintf(
368
-                        __(
369
-                            'The value for %1$s already exists in the Registry::eejs object.
276
+						'event_espresso'
277
+					),
278
+					$key,
279
+					__METHOD__
280
+				)
281
+			);
282
+		}
283
+		if ( ! isset( $this->jsdata[ $key ] ) ) {
284
+			$this->jsdata[ $key ] = is_array($value) ? $value : [$value];
285
+		} else {
286
+			$this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value);
287
+		}
288
+	}
289
+
290
+
291
+	/**
292
+	 * Used to set content used by javascript for a template.
293
+	 * Note: Overrides of existing registered templates are not allowed.
294
+	 *
295
+	 * @param string $template_reference
296
+	 * @param string $template_content
297
+	 * @throws InvalidArgumentException
298
+	 */
299
+	public function addTemplate($template_reference, $template_content)
300
+	{
301
+		if (! isset($this->jsdata['templates'])) {
302
+			$this->jsdata['templates'] = array();
303
+		}
304
+		//no overrides allowed.
305
+		if (isset($this->jsdata['templates'][ $template_reference ])) {
306
+			if (! $this->debug()) {
307
+				return;
308
+			}
309
+			throw new InvalidArgumentException(
310
+				sprintf(
311
+					__(
312
+						'The %1$s key already exists for the templates array in the js data array.  No overrides are allowed.',
313
+						'event_espresso'
314
+					),
315
+					$template_reference
316
+				)
317
+			);
318
+		}
319
+		$this->jsdata['templates'][ $template_reference ] = $template_content;
320
+	}
321
+
322
+
323
+	/**
324
+	 * Retrieve the template content already registered for the given reference.
325
+	 *
326
+	 * @param string $template_reference
327
+	 * @return string
328
+	 */
329
+	public function getTemplate($template_reference)
330
+	{
331
+		return isset($this->jsdata['templates'][ $template_reference ])
332
+			? $this->jsdata['templates'][ $template_reference ]
333
+			: '';
334
+	}
335
+
336
+
337
+	/**
338
+	 * Retrieve registered data.
339
+	 *
340
+	 * @param string $key Name of key to attach data to.
341
+	 * @return mixed                If there is no for the given key, then false is returned.
342
+	 */
343
+	public function getData($key)
344
+	{
345
+		return isset($this->jsdata[ $key ])
346
+			? $this->jsdata[ $key ]
347
+			: false;
348
+	}
349
+
350
+
351
+	/**
352
+	 * Verifies whether the given data exists already on the jsdata array.
353
+	 * Overriding data is not allowed.
354
+	 *
355
+	 * @param string $key Index for data.
356
+	 * @return bool        If valid then return true.
357
+	 * @throws InvalidArgumentException if data already exists.
358
+	 */
359
+	protected function verifyDataNotExisting($key)
360
+	{
361
+		if (isset($this->jsdata[ $key ])) {
362
+			if (! $this->debug()) {
363
+				return false;
364
+			}
365
+			if (is_array($this->jsdata[ $key ])) {
366
+				throw new InvalidArgumentException(
367
+					sprintf(
368
+						__(
369
+							'The value for %1$s already exists in the Registry::eejs object.
370 370
                             Overrides are not allowed. Since the value of this data is an array, you may want to use the
371 371
                             %2$s method to push your value to the array.',
372
-                            'event_espresso'
373
-                        ),
374
-                        $key,
375
-                        'pushData()'
376
-                    )
377
-                );
378
-            }
379
-            throw new InvalidArgumentException(
380
-                sprintf(
381
-                    __(
382
-                        'The value for %1$s already exists in the Registry::eejs object. Overrides are not
372
+							'event_espresso'
373
+						),
374
+						$key,
375
+						'pushData()'
376
+					)
377
+				);
378
+			}
379
+			throw new InvalidArgumentException(
380
+				sprintf(
381
+					__(
382
+						'The value for %1$s already exists in the Registry::eejs object. Overrides are not
383 383
                         allowed.  Consider attaching your value to a different key',
384
-                        'event_espresso'
385
-                    ),
386
-                    $key
387
-                )
388
-            );
389
-        }
390
-        return true;
391
-    }
392
-
393
-
394
-    /**
395
-     * Get the actual asset path for asset manifests.
396
-     * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned.
397
-     *
398
-     * @param string $namespace  The namespace associated with the manifest file hosting the map of chunk_name to actual
399
-     *                           asset file location.
400
-     * @param string $chunk_name
401
-     * @param string $asset_type
402
-     * @return string
403
-     * @since 4.9.59.p
404
-     */
405
-    public function getAssetUrl($namespace, $chunk_name, $asset_type)
406
-    {
407
-        $url = isset(
408
-            $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ],
409
-            $this->manifest_data[ $namespace ]['url_base']
410
-        )
411
-            ? $this->manifest_data[ $namespace ]['url_base']
412
-              . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ]
413
-            : $chunk_name;
414
-        return apply_filters(
415
-            'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl',
416
-            $url,
417
-            $namespace,
418
-            $chunk_name,
419
-            $asset_type
420
-        );
421
-    }
422
-
423
-
424
-
425
-    /**
426
-     * Return the url to a js file for the given namespace and chunk name.
427
-     *
428
-     * @param string $namespace
429
-     * @param string $chunk_name
430
-     * @return string
431
-     */
432
-    public function getJsUrl($namespace, $chunk_name)
433
-    {
434
-        return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS);
435
-    }
436
-
437
-
438
-    /**
439
-     * Return the url to a css file for the given namespace and chunk name.
440
-     *
441
-     * @param string $namespace
442
-     * @param string $chunk_name
443
-     * @return string
444
-     */
445
-    public function getCssUrl($namespace, $chunk_name)
446
-    {
447
-        return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS);
448
-    }
449
-
450
-
451
-    /**
452
-     * @since 4.9.62.p
453
-     * @throws InvalidArgumentException
454
-     * @throws InvalidFilePathException
455
-     */
456
-    public function registerManifestFiles()
457
-    {
458
-        $manifest_files = $this->assets->getManifestFiles();
459
-        foreach ($manifest_files as $manifest_file) {
460
-            $this->registerManifestFile(
461
-                $manifest_file->assetNamespace(),
462
-                $manifest_file->urlBase(),
463
-                $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST
464
-            );
465
-        }
466
-    }
467
-
468
-
469
-    /**
470
-     * Used to register a js/css manifest file with the registered_manifest_files property.
471
-     *
472
-     * @param string $namespace     Provided to associate the manifest file with a specific namespace.
473
-     * @param string $url_base      The url base for the manifest file location.
474
-     * @param string $manifest_file The absolute path to the manifest file.
475
-     * @throws InvalidArgumentException
476
-     * @throws InvalidFilePathException
477
-     * @since 4.9.59.p
478
-     */
479
-    public function registerManifestFile($namespace, $url_base, $manifest_file)
480
-    {
481
-        if (isset($this->manifest_data[ $namespace ])) {
482
-            if (! $this->debug()) {
483
-                return;
484
-            }
485
-            throw new InvalidArgumentException(
486
-                sprintf(
487
-                    esc_html__(
488
-                        'The namespace for this manifest file has already been registered, choose a namespace other than %s',
489
-                        'event_espresso'
490
-                    ),
491
-                    $namespace
492
-                )
493
-            );
494
-        }
495
-        if (filter_var($url_base, FILTER_VALIDATE_URL) === false) {
496
-            if (is_admin()) {
497
-                EE_Error::add_error(
498
-                    sprintf(
499
-                        esc_html__(
500
-                            'The url given for %1$s assets is invalid.  The url provided was: "%2$s". This usually happens when another plugin or theme on a site is using the "%3$s" filter or has an invalid url set for the "%4$s" constant',
501
-                            'event_espresso'
502
-                        ),
503
-                        'Event Espresso',
504
-                        $url_base,
505
-                        'plugins_url',
506
-                        'WP_PLUGIN_URL'
507
-                    ),
508
-                    __FILE__,
509
-                    __FUNCTION__,
510
-                    __LINE__
511
-                );
512
-            }
513
-            return;
514
-        }
515
-        $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file);
516
-        if (! isset($this->manifest_data[ $namespace ]['url_base'])) {
517
-            $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base);
518
-        }
519
-    }
520
-
521
-
522
-    /**
523
-     * Decodes json from the provided manifest file.
524
-     *
525
-     * @since 4.9.59.p
526
-     * @param string $manifest_file Path to manifest file.
527
-     * @return array
528
-     * @throws InvalidFilePathException
529
-     */
530
-    private function decodeManifestFile($manifest_file)
531
-    {
532
-        if (! file_exists($manifest_file)) {
533
-            throw new InvalidFilePathException($manifest_file);
534
-        }
535
-        return json_decode(file_get_contents($manifest_file), true);
536
-    }
537
-
538
-
539
-    /**
540
-     * This is used to set registered script handles that have data.
541
-     *
542
-     * @param string $script_handle
543
-     */
544
-    private function addRegisteredScriptHandlesWithData($script_handle)
545
-    {
546
-        $this->script_handles_with_data[ $script_handle ] = $script_handle;
547
-    }
548
-
549
-
550
-    /**i
384
+						'event_espresso'
385
+					),
386
+					$key
387
+				)
388
+			);
389
+		}
390
+		return true;
391
+	}
392
+
393
+
394
+	/**
395
+	 * Get the actual asset path for asset manifests.
396
+	 * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned.
397
+	 *
398
+	 * @param string $namespace  The namespace associated with the manifest file hosting the map of chunk_name to actual
399
+	 *                           asset file location.
400
+	 * @param string $chunk_name
401
+	 * @param string $asset_type
402
+	 * @return string
403
+	 * @since 4.9.59.p
404
+	 */
405
+	public function getAssetUrl($namespace, $chunk_name, $asset_type)
406
+	{
407
+		$url = isset(
408
+			$this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ],
409
+			$this->manifest_data[ $namespace ]['url_base']
410
+		)
411
+			? $this->manifest_data[ $namespace ]['url_base']
412
+			  . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ]
413
+			: $chunk_name;
414
+		return apply_filters(
415
+			'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl',
416
+			$url,
417
+			$namespace,
418
+			$chunk_name,
419
+			$asset_type
420
+		);
421
+	}
422
+
423
+
424
+
425
+	/**
426
+	 * Return the url to a js file for the given namespace and chunk name.
427
+	 *
428
+	 * @param string $namespace
429
+	 * @param string $chunk_name
430
+	 * @return string
431
+	 */
432
+	public function getJsUrl($namespace, $chunk_name)
433
+	{
434
+		return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS);
435
+	}
436
+
437
+
438
+	/**
439
+	 * Return the url to a css file for the given namespace and chunk name.
440
+	 *
441
+	 * @param string $namespace
442
+	 * @param string $chunk_name
443
+	 * @return string
444
+	 */
445
+	public function getCssUrl($namespace, $chunk_name)
446
+	{
447
+		return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS);
448
+	}
449
+
450
+
451
+	/**
452
+	 * @since 4.9.62.p
453
+	 * @throws InvalidArgumentException
454
+	 * @throws InvalidFilePathException
455
+	 */
456
+	public function registerManifestFiles()
457
+	{
458
+		$manifest_files = $this->assets->getManifestFiles();
459
+		foreach ($manifest_files as $manifest_file) {
460
+			$this->registerManifestFile(
461
+				$manifest_file->assetNamespace(),
462
+				$manifest_file->urlBase(),
463
+				$manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST
464
+			);
465
+		}
466
+	}
467
+
468
+
469
+	/**
470
+	 * Used to register a js/css manifest file with the registered_manifest_files property.
471
+	 *
472
+	 * @param string $namespace     Provided to associate the manifest file with a specific namespace.
473
+	 * @param string $url_base      The url base for the manifest file location.
474
+	 * @param string $manifest_file The absolute path to the manifest file.
475
+	 * @throws InvalidArgumentException
476
+	 * @throws InvalidFilePathException
477
+	 * @since 4.9.59.p
478
+	 */
479
+	public function registerManifestFile($namespace, $url_base, $manifest_file)
480
+	{
481
+		if (isset($this->manifest_data[ $namespace ])) {
482
+			if (! $this->debug()) {
483
+				return;
484
+			}
485
+			throw new InvalidArgumentException(
486
+				sprintf(
487
+					esc_html__(
488
+						'The namespace for this manifest file has already been registered, choose a namespace other than %s',
489
+						'event_espresso'
490
+					),
491
+					$namespace
492
+				)
493
+			);
494
+		}
495
+		if (filter_var($url_base, FILTER_VALIDATE_URL) === false) {
496
+			if (is_admin()) {
497
+				EE_Error::add_error(
498
+					sprintf(
499
+						esc_html__(
500
+							'The url given for %1$s assets is invalid.  The url provided was: "%2$s". This usually happens when another plugin or theme on a site is using the "%3$s" filter or has an invalid url set for the "%4$s" constant',
501
+							'event_espresso'
502
+						),
503
+						'Event Espresso',
504
+						$url_base,
505
+						'plugins_url',
506
+						'WP_PLUGIN_URL'
507
+					),
508
+					__FILE__,
509
+					__FUNCTION__,
510
+					__LINE__
511
+				);
512
+			}
513
+			return;
514
+		}
515
+		$this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file);
516
+		if (! isset($this->manifest_data[ $namespace ]['url_base'])) {
517
+			$this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base);
518
+		}
519
+	}
520
+
521
+
522
+	/**
523
+	 * Decodes json from the provided manifest file.
524
+	 *
525
+	 * @since 4.9.59.p
526
+	 * @param string $manifest_file Path to manifest file.
527
+	 * @return array
528
+	 * @throws InvalidFilePathException
529
+	 */
530
+	private function decodeManifestFile($manifest_file)
531
+	{
532
+		if (! file_exists($manifest_file)) {
533
+			throw new InvalidFilePathException($manifest_file);
534
+		}
535
+		return json_decode(file_get_contents($manifest_file), true);
536
+	}
537
+
538
+
539
+	/**
540
+	 * This is used to set registered script handles that have data.
541
+	 *
542
+	 * @param string $script_handle
543
+	 */
544
+	private function addRegisteredScriptHandlesWithData($script_handle)
545
+	{
546
+		$this->script_handles_with_data[ $script_handle ] = $script_handle;
547
+	}
548
+
549
+
550
+	/**i
551 551
      * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the
552 552
      * Dependency stored in WP_Scripts if its set.
553 553
      */
554
-    private function removeAlreadyRegisteredDataForScriptHandles()
555
-    {
556
-        if (empty($this->script_handles_with_data)) {
557
-            return;
558
-        }
559
-        foreach ($this->script_handles_with_data as $script_handle) {
560
-            $this->removeAlreadyRegisteredDataForScriptHandle($script_handle);
561
-        }
562
-    }
563
-
564
-
565
-    /**
566
-     * Removes any data dependency registered in WP_Scripts if its set.
567
-     *
568
-     * @param string $script_handle
569
-     */
570
-    private function removeAlreadyRegisteredDataForScriptHandle($script_handle)
571
-    {
572
-        if (isset($this->script_handles_with_data[ $script_handle ])) {
573
-            global $wp_scripts;
574
-            $unset_handle = false;
575
-            if ($wp_scripts->get_data($script_handle, 'data')) {
576
-                unset($wp_scripts->registered[ $script_handle ]->extra['data']);
577
-                $unset_handle = true;
578
-            }
579
-            //deal with inline_scripts
580
-            if ($wp_scripts->get_data($script_handle, 'before')) {
581
-                unset($wp_scripts->registered[ $script_handle ]->extra['before']);
582
-                $unset_handle = true;
583
-            }
584
-            if ($wp_scripts->get_data($script_handle, 'after')) {
585
-                unset($wp_scripts->registered[ $script_handle ]->extra['after']);
586
-            }
587
-            if ($unset_handle) {
588
-                unset($this->script_handles_with_data[ $script_handle ]);
589
-            }
590
-        }
591
-    }
592
-
593
-
594
-    /**
595
-     * register translations for a registered script
596
-     *
597
-     * @param string $handle
598
-     */
599
-    public function registerTranslation($handle)
600
-    {
601
-        $this->i18n_registry->registerScriptI18n($handle);
602
-    }
603
-
604
-
605
-    /**
606
-     * @since 4.9.63.p
607
-     * @return bool
608
-     */
609
-    private function debug()
610
-    {
611
-        return apply_filters(
612
-            'FHEE__EventEspresso_core_services_assets_Registry__debug',
613
-            defined('EE_DEBUG') && EE_DEBUG
614
-        );
615
-    }
554
+	private function removeAlreadyRegisteredDataForScriptHandles()
555
+	{
556
+		if (empty($this->script_handles_with_data)) {
557
+			return;
558
+		}
559
+		foreach ($this->script_handles_with_data as $script_handle) {
560
+			$this->removeAlreadyRegisteredDataForScriptHandle($script_handle);
561
+		}
562
+	}
563
+
564
+
565
+	/**
566
+	 * Removes any data dependency registered in WP_Scripts if its set.
567
+	 *
568
+	 * @param string $script_handle
569
+	 */
570
+	private function removeAlreadyRegisteredDataForScriptHandle($script_handle)
571
+	{
572
+		if (isset($this->script_handles_with_data[ $script_handle ])) {
573
+			global $wp_scripts;
574
+			$unset_handle = false;
575
+			if ($wp_scripts->get_data($script_handle, 'data')) {
576
+				unset($wp_scripts->registered[ $script_handle ]->extra['data']);
577
+				$unset_handle = true;
578
+			}
579
+			//deal with inline_scripts
580
+			if ($wp_scripts->get_data($script_handle, 'before')) {
581
+				unset($wp_scripts->registered[ $script_handle ]->extra['before']);
582
+				$unset_handle = true;
583
+			}
584
+			if ($wp_scripts->get_data($script_handle, 'after')) {
585
+				unset($wp_scripts->registered[ $script_handle ]->extra['after']);
586
+			}
587
+			if ($unset_handle) {
588
+				unset($this->script_handles_with_data[ $script_handle ]);
589
+			}
590
+		}
591
+	}
592
+
593
+
594
+	/**
595
+	 * register translations for a registered script
596
+	 *
597
+	 * @param string $handle
598
+	 */
599
+	public function registerTranslation($handle)
600
+	{
601
+		$this->i18n_registry->registerScriptI18n($handle);
602
+	}
603
+
604
+
605
+	/**
606
+	 * @since 4.9.63.p
607
+	 * @return bool
608
+	 */
609
+	private function debug()
610
+	{
611
+		return apply_filters(
612
+			'FHEE__EventEspresso_core_services_assets_Registry__debug',
613
+			defined('EE_DEBUG') && EE_DEBUG
614
+		);
615
+	}
616 616
 }
Please login to merge, or discard this patch.
Spacing   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -147,7 +147,7 @@  discard block
 block discarded – undo
147 147
                 $script->version(),
148 148
                 $script->loadInFooter()
149 149
             );
150
-            if (! $registered && $this->debug()) {
150
+            if ( ! $registered && $this->debug()) {
151 151
                 throw new AssetRegistrationException($script->handle());
152 152
             }
153 153
             $script->setRegistered($registered);
@@ -207,7 +207,7 @@  discard block
 block discarded – undo
207 207
         $this->removeAlreadyRegisteredDataForScriptHandles();
208 208
         wp_add_inline_script(
209 209
             'eejs-core',
210
-            'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)),
210
+            'var eejsdata='.wp_json_encode(array('data' => $this->jsdata)),
211 211
             'before'
212 212
         );
213 213
         $scripts = $this->assets->getJavascriptAssetsWithData();
@@ -237,7 +237,7 @@  discard block
 block discarded – undo
237 237
     public function addData($key, $value)
238 238
     {
239 239
         if ($this->verifyDataNotExisting($key)) {
240
-            $this->jsdata[ $key ] = $value;
240
+            $this->jsdata[$key] = $value;
241 241
         }
242 242
     }
243 243
 
@@ -262,10 +262,10 @@  discard block
 block discarded – undo
262 262
      */
263 263
     public function pushData($key, $value)
264 264
     {
265
-        if (isset($this->jsdata[ $key ])
266
-            && ! is_array($this->jsdata[ $key ])
265
+        if (isset($this->jsdata[$key])
266
+            && ! is_array($this->jsdata[$key])
267 267
         ) {
268
-            if (! $this->debug()) {
268
+            if ( ! $this->debug()) {
269 269
                 return;
270 270
             }
271 271
             throw new InvalidArgumentException(
@@ -280,10 +280,10 @@  discard block
 block discarded – undo
280 280
                 )
281 281
             );
282 282
         }
283
-        if ( ! isset( $this->jsdata[ $key ] ) ) {
284
-            $this->jsdata[ $key ] = is_array($value) ? $value : [$value];
283
+        if ( ! isset($this->jsdata[$key])) {
284
+            $this->jsdata[$key] = is_array($value) ? $value : [$value];
285 285
         } else {
286
-            $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value);
286
+            $this->jsdata[$key] = array_merge($this->jsdata[$key], (array) $value);
287 287
         }
288 288
     }
289 289
 
@@ -298,12 +298,12 @@  discard block
 block discarded – undo
298 298
      */
299 299
     public function addTemplate($template_reference, $template_content)
300 300
     {
301
-        if (! isset($this->jsdata['templates'])) {
301
+        if ( ! isset($this->jsdata['templates'])) {
302 302
             $this->jsdata['templates'] = array();
303 303
         }
304 304
         //no overrides allowed.
305
-        if (isset($this->jsdata['templates'][ $template_reference ])) {
306
-            if (! $this->debug()) {
305
+        if (isset($this->jsdata['templates'][$template_reference])) {
306
+            if ( ! $this->debug()) {
307 307
                 return;
308 308
             }
309 309
             throw new InvalidArgumentException(
@@ -316,7 +316,7 @@  discard block
 block discarded – undo
316 316
                 )
317 317
             );
318 318
         }
319
-        $this->jsdata['templates'][ $template_reference ] = $template_content;
319
+        $this->jsdata['templates'][$template_reference] = $template_content;
320 320
     }
321 321
 
322 322
 
@@ -328,8 +328,8 @@  discard block
 block discarded – undo
328 328
      */
329 329
     public function getTemplate($template_reference)
330 330
     {
331
-        return isset($this->jsdata['templates'][ $template_reference ])
332
-            ? $this->jsdata['templates'][ $template_reference ]
331
+        return isset($this->jsdata['templates'][$template_reference])
332
+            ? $this->jsdata['templates'][$template_reference]
333 333
             : '';
334 334
     }
335 335
 
@@ -342,8 +342,8 @@  discard block
 block discarded – undo
342 342
      */
343 343
     public function getData($key)
344 344
     {
345
-        return isset($this->jsdata[ $key ])
346
-            ? $this->jsdata[ $key ]
345
+        return isset($this->jsdata[$key])
346
+            ? $this->jsdata[$key]
347 347
             : false;
348 348
     }
349 349
 
@@ -358,11 +358,11 @@  discard block
 block discarded – undo
358 358
      */
359 359
     protected function verifyDataNotExisting($key)
360 360
     {
361
-        if (isset($this->jsdata[ $key ])) {
362
-            if (! $this->debug()) {
361
+        if (isset($this->jsdata[$key])) {
362
+            if ( ! $this->debug()) {
363 363
                 return false;
364 364
             }
365
-            if (is_array($this->jsdata[ $key ])) {
365
+            if (is_array($this->jsdata[$key])) {
366 366
                 throw new InvalidArgumentException(
367 367
                     sprintf(
368 368
                         __(
@@ -405,11 +405,11 @@  discard block
 block discarded – undo
405 405
     public function getAssetUrl($namespace, $chunk_name, $asset_type)
406 406
     {
407 407
         $url = isset(
408
-            $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ],
409
-            $this->manifest_data[ $namespace ]['url_base']
408
+            $this->manifest_data[$namespace][$chunk_name.'.'.$asset_type],
409
+            $this->manifest_data[$namespace]['url_base']
410 410
         )
411
-            ? $this->manifest_data[ $namespace ]['url_base']
412
-              . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ]
411
+            ? $this->manifest_data[$namespace]['url_base']
412
+              . $this->manifest_data[$namespace][$chunk_name.'.'.$asset_type]
413 413
             : $chunk_name;
414 414
         return apply_filters(
415 415
             'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl',
@@ -460,7 +460,7 @@  discard block
 block discarded – undo
460 460
             $this->registerManifestFile(
461 461
                 $manifest_file->assetNamespace(),
462 462
                 $manifest_file->urlBase(),
463
-                $manifest_file->filepath() . Registry::FILE_NAME_BUILD_MANIFEST
463
+                $manifest_file->filepath().Registry::FILE_NAME_BUILD_MANIFEST
464 464
             );
465 465
         }
466 466
     }
@@ -478,8 +478,8 @@  discard block
 block discarded – undo
478 478
      */
479 479
     public function registerManifestFile($namespace, $url_base, $manifest_file)
480 480
     {
481
-        if (isset($this->manifest_data[ $namespace ])) {
482
-            if (! $this->debug()) {
481
+        if (isset($this->manifest_data[$namespace])) {
482
+            if ( ! $this->debug()) {
483 483
                 return;
484 484
             }
485 485
             throw new InvalidArgumentException(
@@ -512,9 +512,9 @@  discard block
 block discarded – undo
512 512
             }
513 513
             return;
514 514
         }
515
-        $this->manifest_data[ $namespace ] = $this->decodeManifestFile($manifest_file);
516
-        if (! isset($this->manifest_data[ $namespace ]['url_base'])) {
517
-            $this->manifest_data[ $namespace ]['url_base'] = trailingslashit($url_base);
515
+        $this->manifest_data[$namespace] = $this->decodeManifestFile($manifest_file);
516
+        if ( ! isset($this->manifest_data[$namespace]['url_base'])) {
517
+            $this->manifest_data[$namespace]['url_base'] = trailingslashit($url_base);
518 518
         }
519 519
     }
520 520
 
@@ -529,7 +529,7 @@  discard block
 block discarded – undo
529 529
      */
530 530
     private function decodeManifestFile($manifest_file)
531 531
     {
532
-        if (! file_exists($manifest_file)) {
532
+        if ( ! file_exists($manifest_file)) {
533 533
             throw new InvalidFilePathException($manifest_file);
534 534
         }
535 535
         return json_decode(file_get_contents($manifest_file), true);
@@ -543,7 +543,7 @@  discard block
 block discarded – undo
543 543
      */
544 544
     private function addRegisteredScriptHandlesWithData($script_handle)
545 545
     {
546
-        $this->script_handles_with_data[ $script_handle ] = $script_handle;
546
+        $this->script_handles_with_data[$script_handle] = $script_handle;
547 547
     }
548 548
 
549 549
 
@@ -569,23 +569,23 @@  discard block
 block discarded – undo
569 569
      */
570 570
     private function removeAlreadyRegisteredDataForScriptHandle($script_handle)
571 571
     {
572
-        if (isset($this->script_handles_with_data[ $script_handle ])) {
572
+        if (isset($this->script_handles_with_data[$script_handle])) {
573 573
             global $wp_scripts;
574 574
             $unset_handle = false;
575 575
             if ($wp_scripts->get_data($script_handle, 'data')) {
576
-                unset($wp_scripts->registered[ $script_handle ]->extra['data']);
576
+                unset($wp_scripts->registered[$script_handle]->extra['data']);
577 577
                 $unset_handle = true;
578 578
             }
579 579
             //deal with inline_scripts
580 580
             if ($wp_scripts->get_data($script_handle, 'before')) {
581
-                unset($wp_scripts->registered[ $script_handle ]->extra['before']);
581
+                unset($wp_scripts->registered[$script_handle]->extra['before']);
582 582
                 $unset_handle = true;
583 583
             }
584 584
             if ($wp_scripts->get_data($script_handle, 'after')) {
585
-                unset($wp_scripts->registered[ $script_handle ]->extra['after']);
585
+                unset($wp_scripts->registered[$script_handle]->extra['after']);
586 586
             }
587 587
             if ($unset_handle) {
588
-                unset($this->script_handles_with_data[ $script_handle ]);
588
+                unset($this->script_handles_with_data[$script_handle]);
589 589
             }
590 590
         }
591 591
     }
Please login to merge, or discard this patch.
espresso.php 1 patch
Indentation   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -38,103 +38,103 @@
 block discarded – undo
38 38
  * @since           4.0
39 39
  */
40 40
 if (function_exists('espresso_version')) {
41
-    if (! function_exists('espresso_duplicate_plugin_error')) {
42
-        /**
43
-         *    espresso_duplicate_plugin_error
44
-         *    displays if more than one version of EE is activated at the same time
45
-         */
46
-        function espresso_duplicate_plugin_error()
47
-        {
48
-            ?>
41
+	if (! function_exists('espresso_duplicate_plugin_error')) {
42
+		/**
43
+		 *    espresso_duplicate_plugin_error
44
+		 *    displays if more than one version of EE is activated at the same time
45
+		 */
46
+		function espresso_duplicate_plugin_error()
47
+		{
48
+			?>
49 49
             <div class="error">
50 50
                 <p>
51 51
                     <?php
52
-                    echo esc_html__(
53
-                        'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
-                        'event_espresso'
55
-                    ); ?>
52
+					echo esc_html__(
53
+						'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
+						'event_espresso'
55
+					); ?>
56 56
                 </p>
57 57
             </div>
58 58
             <?php
59
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
60
-        }
61
-    }
62
-    add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
59
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
60
+		}
61
+	}
62
+	add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
63 63
 } else {
64
-    define('EE_MIN_PHP_VER_REQUIRED', '5.4.0');
65
-    if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
-        /**
67
-         * espresso_minimum_php_version_error
68
-         *
69
-         * @return void
70
-         */
71
-        function espresso_minimum_php_version_error()
72
-        {
73
-            ?>
64
+	define('EE_MIN_PHP_VER_REQUIRED', '5.4.0');
65
+	if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
+		/**
67
+		 * espresso_minimum_php_version_error
68
+		 *
69
+		 * @return void
70
+		 */
71
+		function espresso_minimum_php_version_error()
72
+		{
73
+			?>
74 74
             <div class="error">
75 75
                 <p>
76 76
                     <?php
77
-                    printf(
78
-                        esc_html__(
79
-                            'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
-                            'event_espresso'
81
-                        ),
82
-                        EE_MIN_PHP_VER_REQUIRED,
83
-                        PHP_VERSION,
84
-                        '<br/>',
85
-                        '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
-                    );
87
-                    ?>
77
+					printf(
78
+						esc_html__(
79
+							'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
+							'event_espresso'
81
+						),
82
+						EE_MIN_PHP_VER_REQUIRED,
83
+						PHP_VERSION,
84
+						'<br/>',
85
+						'<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
+					);
87
+					?>
88 88
                 </p>
89 89
             </div>
90 90
             <?php
91
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
92
-        }
91
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
92
+		}
93 93
 
94
-        add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
-    } else {
96
-        define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
-        /**
98
-         * espresso_version
99
-         * Returns the plugin version
100
-         *
101
-         * @return string
102
-         */
103
-        function espresso_version()
104
-        {
105
-            return apply_filters('FHEE__espresso__espresso_version', '4.9.78.rc.021');
106
-        }
94
+		add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
+	} else {
96
+		define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
+		/**
98
+		 * espresso_version
99
+		 * Returns the plugin version
100
+		 *
101
+		 * @return string
102
+		 */
103
+		function espresso_version()
104
+		{
105
+			return apply_filters('FHEE__espresso__espresso_version', '4.9.78.rc.021');
106
+		}
107 107
 
108
-        /**
109
-         * espresso_plugin_activation
110
-         * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
-         */
112
-        function espresso_plugin_activation()
113
-        {
114
-            update_option('ee_espresso_activation', true);
115
-        }
108
+		/**
109
+		 * espresso_plugin_activation
110
+		 * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
+		 */
112
+		function espresso_plugin_activation()
113
+		{
114
+			update_option('ee_espresso_activation', true);
115
+		}
116 116
 
117
-        register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
117
+		register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
118 118
 
119
-        require_once __DIR__ . '/core/bootstrap_espresso.php';
120
-        bootstrap_espresso();
121
-    }
119
+		require_once __DIR__ . '/core/bootstrap_espresso.php';
120
+		bootstrap_espresso();
121
+	}
122 122
 }
123 123
 if (! function_exists('espresso_deactivate_plugin')) {
124
-    /**
125
-     *    deactivate_plugin
126
-     * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
-     *
128
-     * @access public
129
-     * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
-     * @return    void
131
-     */
132
-    function espresso_deactivate_plugin($plugin_basename = '')
133
-    {
134
-        if (! function_exists('deactivate_plugins')) {
135
-            require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
-        }
137
-        unset($_GET['activate'], $_REQUEST['activate']);
138
-        deactivate_plugins($plugin_basename);
139
-    }
124
+	/**
125
+	 *    deactivate_plugin
126
+	 * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
+	 *
128
+	 * @access public
129
+	 * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
+	 * @return    void
131
+	 */
132
+	function espresso_deactivate_plugin($plugin_basename = '')
133
+	{
134
+		if (! function_exists('deactivate_plugins')) {
135
+			require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
+		}
137
+		unset($_GET['activate'], $_REQUEST['activate']);
138
+		deactivate_plugins($plugin_basename);
139
+	}
140 140
 }
Please login to merge, or discard this patch.
core/db_models/EEM_Payment_Method.model.php 1 patch
Indentation   +359 added lines, -359 removed lines patch added patch discarded remove patch
@@ -20,363 +20,363 @@
 block discarded – undo
20 20
 class EEM_Payment_Method extends EEM_Base
21 21
 {
22 22
 
23
-    const scope_cart = 'CART';
24
-
25
-    const scope_admin = 'ADMIN';
26
-
27
-    const scope_api = 'API';
28
-
29
-    /**
30
-     *
31
-     * @type EEM_Payment_Method
32
-     */
33
-    protected static $_instance = null;
34
-
35
-
36
-
37
-    /**
38
-     * private constructor to prevent direct creation
39
-     * @Constructor
40
-     * @access   protected
41
-     * @return EEM_Payment_Method
42
-     */
43
-    protected function __construct($timezone = null)
44
-    {
45
-        $this->singlular_item = __('Payment Method', 'event_espresso');
46
-        $this->plural_item = __('Payment Methods', 'event_espresso');
47
-        $this->_tables = array( 'Payment_Method' => new EE_Primary_Table('esp_payment_method', 'PMD_ID') );
48
-        $this->_fields = array(
49
-            'Payment_Method' => array(
50
-                'PMD_ID' => new EE_Primary_Key_Int_Field('PMD_ID', __("ID", 'event_espresso')),
51
-                'PMD_type' => new EE_Plain_Text_Field('PMD_type', __("Payment Method Type", 'event_espresso'), false, 'Admin_Only'),
52
-                'PMD_name' => new EE_Plain_Text_Field('PMD_name', __("Name", 'event_espresso'), false),
53
-                'PMD_desc' => new EE_Post_Content_Field('PMD_desc', __("Description", 'event_espresso'), false, ''),
54
-                'PMD_admin_name' => new EE_Plain_Text_Field('PMD_admin_name', __("Admin-Only Name", 'event_espresso'), true),
55
-                'PMD_admin_desc' => new EE_Post_Content_Field('PMD_admin_desc', __("Admin-Only Description", 'event_espresso'), true),
56
-                'PMD_slug' => new EE_Slug_Field('PMD_slug', __("Slug", 'event_espresso'), false),
57
-                'PMD_order' => new EE_Integer_Field('PMD_order', __("Order", 'event_espresso'), false, 0),
58
-                'PMD_debug_mode' => new EE_Boolean_Field('PMD_debug_mode', __("Debug Mode On?", 'event_espresso'), false, false),
59
-                'PMD_wp_user' => new EE_WP_User_Field('PMD_wp_user', __("Payment Method Creator ID", 'event_espresso'), false),
60
-                'PMD_open_by_default' => new EE_Boolean_Field('PMD_open_by_default', __("Open by Default?", 'event_espresso'), false, false), 'PMD_button_url' => new EE_Plain_Text_Field('PMD_button_url', __("Button URL", 'event_espresso'), true, ''),
61
-                'PMD_scope' => new EE_Serialized_Text_Field('PMD_scope', __("Usable From?", 'event_espresso'), false, array()), // possible values currently are 'CART','ADMIN','API'
62
-        ) );
63
-        $this->_model_relations = array(
64
-            'Payment' => new EE_Has_Many_Relation(),
65
-            'Currency' => new EE_HABTM_Relation('Currency_Payment_Method'),
66
-            'Transaction' => new EE_Has_Many_Relation(),
67
-            'WP_User' => new EE_Belongs_To_Relation(),
68
-        );
69
-        parent::__construct($timezone);
70
-    }
71
-
72
-
73
-
74
-    /**
75
-     * Gets one by the slug provided
76
-     * @param string $slug
77
-     * @return EE_Payment_Method
78
-     */
79
-    public function get_one_by_slug($slug)
80
-    {
81
-        return $this->get_one(array( array( 'PMD_slug' => $slug ) ));
82
-    }
83
-
84
-
85
-
86
-    /**
87
-     * Gets all the acceptable scopes for payment methods.
88
-     * Keys are their names as store din the DB, and values are nice names for displaying them
89
-     * @return array
90
-     */
91
-    public function scopes()
92
-    {
93
-        return apply_filters(
94
-            'FHEE__EEM_Payment_Method__scopes',
95
-            array(
96
-                self::scope_cart        => __("Front-end Registration Page", 'event_espresso'),
97
-                self::scope_admin   => __("Admin Registration Page (no online processing)", 'event_espresso')
98
-            )
99
-        );
100
-    }
101
-
102
-
103
-
104
-    /**
105
-     * Determines if this is an valid scope
106
-     * @param string $scope like one of EEM_Payment_Method::instance()->scopes()
107
-     * @return boolean
108
-     */
109
-    public function is_valid_scope($scope)
110
-    {
111
-        $scopes = $this->scopes();
112
-        if (isset($scopes[ $scope ])) {
113
-            return true;
114
-        } else {
115
-            return false;
116
-        }
117
-    }
118
-
119
-
120
-
121
-    /**
122
-     * Gets all active payment methods
123
-     * @param string $scope one of
124
-     * @param array  $query_params
125
-     * @throws EE_Error
126
-     * @return EE_Payment_Method[]
127
-     */
128
-    public function get_all_active($scope = null, $query_params = array())
129
-    {
130
-        if (! isset($query_params['order_by']) && ! isset($query_params['order'])) {
131
-            $query_params['order_by'] = array( 'PMD_order' => 'ASC', 'PMD_ID' => 'ASC' );
132
-        }
133
-        return $this->get_all($this->_get_query_params_for_all_active($scope, $query_params));
134
-    }
135
-
136
-    /**
137
-     * Counts all active gateways in the specified scope
138
-     * @param string $scope one of EEM_Payment_Method::scope_*
139
-     * @param array $query_params
140
-     * @return int
141
-     */
142
-    public function count_active($scope = null, $query_params = array())
143
-    {
144
-        return $this->count($this->_get_query_params_for_all_active($scope, $query_params));
145
-    }
146
-
147
-    /**
148
-     * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params
149
-     * argument to get all active for a given scope
150
-     * @param string $scope one of the constants EEM_Payment_Method::scope_*
151
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
152
-     * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
153
-     * @throws EE_Error
154
-     */
155
-    protected function _get_query_params_for_all_active($scope = null, $query_params = array())
156
-    {
157
-        if ($scope) {
158
-            if ($this->is_valid_scope($scope)) {
159
-                return array_replace_recursive(array( array( 'PMD_scope' => array( 'LIKE', "%$scope%" ) ) ), $query_params);
160
-            } else {
161
-                throw new EE_Error(sprintf(__("'%s' is not a valid scope for a payment method", "event_espresso"), $scope));
162
-            }
163
-        } else {
164
-            $acceptable_scopes = array();
165
-            $count = 0;
166
-            foreach ($this->scopes() as $scope_name => $desc) {
167
-                $count++;
168
-                $acceptable_scopes[ 'PMD_scope*' . $count ] = array( 'LIKE', '%' . $scope_name . '%' );
169
-            }
170
-            return array_replace_recursive(array( array( 'OR*active_scope' => $acceptable_scopes ) ), $query_params);
171
-        }
172
-    }
173
-
174
-    /**
175
-     * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params
176
-     * argument to get all active for a given scope
177
-     * @param string $scope one of the constants EEM_Payment_Method::scope_*
178
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
179
-     * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
180
-     * @throws EE_Error
181
-     */
182
-    public function get_query_params_for_all_active($scope = null, $query_params = array())
183
-    {
184
-        return $this->_get_query_params_for_all_active($scope, $query_params);
185
-    }
186
-
187
-
188
-    /**
189
-     * Gets one active payment method. see @get_all_active for documentation
190
-     * @param string $scope
191
-     * @param array  $query_params
192
-     * @return EE_Payment_Method
193
-     */
194
-    public function get_one_active($scope = null, $query_params = array())
195
-    {
196
-        return $this->get_one($this->_get_query_params_for_all_active($scope, $query_params));
197
-    }
198
-
199
-
200
-
201
-    /**
202
-     * Gets one payment method of that type, regardless of whether its active or not
203
-     * @param string $type
204
-     * @return EE_Payment_Method
205
-     */
206
-    public function get_one_of_type($type)
207
-    {
208
-        return $this->get_one(array( array( 'PMD_type' => $type ) ));
209
-    }
210
-
211
-
212
-
213
-    /**
214
-     * Overrides parent ot also check by the slug
215
-     * @see EEM_Base::ensure_is_obj()
216
-     * @param string|int|EE_Payment_Method $base_class_obj_or_id
217
-     * @param boolean                      $ensure_is_in_db
218
-     * @return EE_Payment_Method
219
-     * @throws EE_Error
220
-     */
221
-    public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false)
222
-    {
223
-        // first: check if it's a slug
224
-        if (is_string($base_class_obj_or_id)) {
225
-            $obj = $this->get_one_by_slug($base_class_obj_or_id);
226
-            if ($obj) {
227
-                return $obj;
228
-            }
229
-        }
230
-        // ok so it wasn't a slug we were passed. try the usual then (ie, it's an object or an ID)
231
-        try {
232
-            return parent::ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db);
233
-        } catch (EE_Error $e) {
234
-            // handle it outside the catch
235
-        }
236
-        throw new EE_Error(sprintf(__("'%s' is neither a Payment Method ID, slug, nor object.", "event_espresso"), $base_class_obj_or_id));
237
-    }
238
-
239
-
240
-
241
-    /**
242
-     * Gets the ID of this object, or if its a string finds the object's id
243
-     * associated with that slug
244
-     * @param mixed $base_obj_or_id_or_slug
245
-     * @return int
246
-     */
247
-    public function ensure_is_ID($base_obj_or_id_or_slug)
248
-    {
249
-        if (is_string($base_obj_or_id_or_slug)) {
250
-            // assume it's a slug
251
-            $base_obj_or_id_or_slug = $this->get_one_by_slug($base_obj_or_id_or_slug);
252
-        }
253
-        return parent::ensure_is_ID($base_obj_or_id_or_slug);
254
-    }
255
-
256
-
257
-
258
-    /**
259
-     * Verifies the button urls on all the passed payment methods have a valid button url. If not, resets them to their default.
260
-     * @param EE_Payment_Method[] $payment_methods. If NULL is provided defaults to all payment methods active in the cart
261
-     */
262
-    public function verify_button_urls($payment_methods = null)
263
-    {
264
-        $payment_methods = is_array($payment_methods) ? $payment_methods : $this->get_all_active(EEM_Payment_Method::scope_cart);
265
-        foreach ($payment_methods as $payment_method) {
266
-            try {
267
-                // If there is really no button URL at all, or if the button URLs still point to decaf folder even
268
-                // though this is a caffeinated install, reset it to the default.
269
-                $current_button_url = $payment_method->button_url();
270
-                if (empty($current_button_url)
271
-                || (
272
-                        strpos($current_button_url, 'decaf') !== false
273
-                        && strpos($payment_method->type_obj()->default_button_url(), 'decaf') === false
274
-                    )
275
-                ) {
276
-                    $payment_method->save(
277
-                        [
278
-                            'PMD_button_url' => $payment_method->type_obj()->default_button_url()
279
-                        ]
280
-                    );
281
-                }
282
-            } catch (EE_Error $e) {
283
-                $payment_method->set_active(false);
284
-            }
285
-        }
286
-    }
287
-
288
-
289
-
290
-    /**
291
-     * Overrides parent to not only turn wpdb results into EE_Payment_Method objects,
292
-     * but also verifies the payment method type of each is a usable object. If not,
293
-     * deactivate it, sets a notification, and deactivates it
294
-     *
295
-     * @param array $rows
296
-     * @return EE_Payment_Method[]
297
-     * @throws InvalidDataTypeException
298
-     */
299
-    protected function _create_objects($rows = array())
300
-    {
301
-        EE_Registry::instance()->load_lib('Payment_Method_Manager');
302
-        $payment_methods = parent::_create_objects($rows);
303
-        /* @var $payment_methods EE_Payment_Method[] */
304
-        $usable_payment_methods = array();
305
-        foreach ($payment_methods as $key => $payment_method) {
306
-            if (EE_Payment_Method_Manager::instance()->payment_method_type_exists($payment_method->type())) {
307
-                $usable_payment_methods[ $key ] = $payment_method;
308
-                // some payment methods enqueue their scripts in EE_PMT_*::__construct
309
-                // which is kinda a no-no (just because it's being constructed doesn't mean we need to enqueue
310
-                // its scripts). but for backwards-compat we should continue to do that
311
-                $payment_method->type_obj();
312
-            } elseif ($payment_method->active()) {
313
-                // only deactivate and notify the admin if the payment is active somewhere
314
-                $payment_method->deactivate();
315
-                $payment_method->save();
316
-                do_action(
317
-                    'AHEE__EEM_Payment_Method___create_objects_auto_deactivated_payment_method',
318
-                    $payment_method
319
-                );
320
-                new PersistentAdminNotice(
321
-                    'auto-deactivated-' . $payment_method->type(),
322
-                    sprintf(
323
-                        __(
324
-                            'The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.',
325
-                            'event_espresso'
326
-                        ),
327
-                        $payment_method->admin_name(),
328
-                        '<br />',
329
-                        '<a href="' . admin_url('plugins.php') . '">',
330
-                        '</a>'
331
-                    ),
332
-                    true
333
-                );
334
-            }
335
-        }
336
-        return $usable_payment_methods;
337
-    }
338
-
339
-
340
-
341
-    /**
342
-     * Gets all the payment methods which can be used for transaction
343
-     * (according to the relations between payment methods and events, and
344
-     * the currencies used for the transaction and their relation to payment methods)
345
-     * @param EE_Transaction $transaction
346
-     * @param string    $scope @see EEM_Payment_Method::get_all_for_events
347
-     * @return EE_Payment_Method[]
348
-     */
349
-    public function get_all_for_transaction($transaction, $scope)
350
-    {
351
-        // give addons a chance to override what payment methods are chosen based on the transaction
352
-        return apply_filters(
353
-            'FHEE__EEM_Payment_Method__get_all_for_transaction__payment_methods',
354
-            $this->get_all_active($scope, array( 'group_by' => 'PMD_type' )),
355
-            $transaction,
356
-            $scope
357
-        );
358
-    }
359
-
360
-
361
-    /**
362
-     * Returns the payment method used for the last payment made for a registration.
363
-     *
364
-     * Note: if an offline payment method was selected on the related transaction then this will have no payment methods returned.
365
-     * It will ONLY return a payment method for a PAYMENT recorded against the registration.
366
-     *
367
-     * @param EE_Registration|int $registration_or_reg_id  Either the EE_Registration object or the id for the registration.
368
-     * @return EE_Payment|null
369
-     */
370
-    public function get_last_used_for_registration($registration_or_reg_id)
371
-    {
372
-        $registration_id = EEM_Registration::instance()->ensure_is_ID($registration_or_reg_id);
373
-
374
-        $query_params = array(
375
-            0 => array(
376
-                'Payment.Registration.REG_ID' => $registration_id,
377
-            ),
378
-            'order_by' => array( 'Payment.PAY_ID' => 'DESC' )
379
-        );
380
-        return $this->get_one($query_params);
381
-    }
23
+	const scope_cart = 'CART';
24
+
25
+	const scope_admin = 'ADMIN';
26
+
27
+	const scope_api = 'API';
28
+
29
+	/**
30
+	 *
31
+	 * @type EEM_Payment_Method
32
+	 */
33
+	protected static $_instance = null;
34
+
35
+
36
+
37
+	/**
38
+	 * private constructor to prevent direct creation
39
+	 * @Constructor
40
+	 * @access   protected
41
+	 * @return EEM_Payment_Method
42
+	 */
43
+	protected function __construct($timezone = null)
44
+	{
45
+		$this->singlular_item = __('Payment Method', 'event_espresso');
46
+		$this->plural_item = __('Payment Methods', 'event_espresso');
47
+		$this->_tables = array( 'Payment_Method' => new EE_Primary_Table('esp_payment_method', 'PMD_ID') );
48
+		$this->_fields = array(
49
+			'Payment_Method' => array(
50
+				'PMD_ID' => new EE_Primary_Key_Int_Field('PMD_ID', __("ID", 'event_espresso')),
51
+				'PMD_type' => new EE_Plain_Text_Field('PMD_type', __("Payment Method Type", 'event_espresso'), false, 'Admin_Only'),
52
+				'PMD_name' => new EE_Plain_Text_Field('PMD_name', __("Name", 'event_espresso'), false),
53
+				'PMD_desc' => new EE_Post_Content_Field('PMD_desc', __("Description", 'event_espresso'), false, ''),
54
+				'PMD_admin_name' => new EE_Plain_Text_Field('PMD_admin_name', __("Admin-Only Name", 'event_espresso'), true),
55
+				'PMD_admin_desc' => new EE_Post_Content_Field('PMD_admin_desc', __("Admin-Only Description", 'event_espresso'), true),
56
+				'PMD_slug' => new EE_Slug_Field('PMD_slug', __("Slug", 'event_espresso'), false),
57
+				'PMD_order' => new EE_Integer_Field('PMD_order', __("Order", 'event_espresso'), false, 0),
58
+				'PMD_debug_mode' => new EE_Boolean_Field('PMD_debug_mode', __("Debug Mode On?", 'event_espresso'), false, false),
59
+				'PMD_wp_user' => new EE_WP_User_Field('PMD_wp_user', __("Payment Method Creator ID", 'event_espresso'), false),
60
+				'PMD_open_by_default' => new EE_Boolean_Field('PMD_open_by_default', __("Open by Default?", 'event_espresso'), false, false), 'PMD_button_url' => new EE_Plain_Text_Field('PMD_button_url', __("Button URL", 'event_espresso'), true, ''),
61
+				'PMD_scope' => new EE_Serialized_Text_Field('PMD_scope', __("Usable From?", 'event_espresso'), false, array()), // possible values currently are 'CART','ADMIN','API'
62
+		) );
63
+		$this->_model_relations = array(
64
+			'Payment' => new EE_Has_Many_Relation(),
65
+			'Currency' => new EE_HABTM_Relation('Currency_Payment_Method'),
66
+			'Transaction' => new EE_Has_Many_Relation(),
67
+			'WP_User' => new EE_Belongs_To_Relation(),
68
+		);
69
+		parent::__construct($timezone);
70
+	}
71
+
72
+
73
+
74
+	/**
75
+	 * Gets one by the slug provided
76
+	 * @param string $slug
77
+	 * @return EE_Payment_Method
78
+	 */
79
+	public function get_one_by_slug($slug)
80
+	{
81
+		return $this->get_one(array( array( 'PMD_slug' => $slug ) ));
82
+	}
83
+
84
+
85
+
86
+	/**
87
+	 * Gets all the acceptable scopes for payment methods.
88
+	 * Keys are their names as store din the DB, and values are nice names for displaying them
89
+	 * @return array
90
+	 */
91
+	public function scopes()
92
+	{
93
+		return apply_filters(
94
+			'FHEE__EEM_Payment_Method__scopes',
95
+			array(
96
+				self::scope_cart        => __("Front-end Registration Page", 'event_espresso'),
97
+				self::scope_admin   => __("Admin Registration Page (no online processing)", 'event_espresso')
98
+			)
99
+		);
100
+	}
101
+
102
+
103
+
104
+	/**
105
+	 * Determines if this is an valid scope
106
+	 * @param string $scope like one of EEM_Payment_Method::instance()->scopes()
107
+	 * @return boolean
108
+	 */
109
+	public function is_valid_scope($scope)
110
+	{
111
+		$scopes = $this->scopes();
112
+		if (isset($scopes[ $scope ])) {
113
+			return true;
114
+		} else {
115
+			return false;
116
+		}
117
+	}
118
+
119
+
120
+
121
+	/**
122
+	 * Gets all active payment methods
123
+	 * @param string $scope one of
124
+	 * @param array  $query_params
125
+	 * @throws EE_Error
126
+	 * @return EE_Payment_Method[]
127
+	 */
128
+	public function get_all_active($scope = null, $query_params = array())
129
+	{
130
+		if (! isset($query_params['order_by']) && ! isset($query_params['order'])) {
131
+			$query_params['order_by'] = array( 'PMD_order' => 'ASC', 'PMD_ID' => 'ASC' );
132
+		}
133
+		return $this->get_all($this->_get_query_params_for_all_active($scope, $query_params));
134
+	}
135
+
136
+	/**
137
+	 * Counts all active gateways in the specified scope
138
+	 * @param string $scope one of EEM_Payment_Method::scope_*
139
+	 * @param array $query_params
140
+	 * @return int
141
+	 */
142
+	public function count_active($scope = null, $query_params = array())
143
+	{
144
+		return $this->count($this->_get_query_params_for_all_active($scope, $query_params));
145
+	}
146
+
147
+	/**
148
+	 * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params
149
+	 * argument to get all active for a given scope
150
+	 * @param string $scope one of the constants EEM_Payment_Method::scope_*
151
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
152
+	 * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
153
+	 * @throws EE_Error
154
+	 */
155
+	protected function _get_query_params_for_all_active($scope = null, $query_params = array())
156
+	{
157
+		if ($scope) {
158
+			if ($this->is_valid_scope($scope)) {
159
+				return array_replace_recursive(array( array( 'PMD_scope' => array( 'LIKE', "%$scope%" ) ) ), $query_params);
160
+			} else {
161
+				throw new EE_Error(sprintf(__("'%s' is not a valid scope for a payment method", "event_espresso"), $scope));
162
+			}
163
+		} else {
164
+			$acceptable_scopes = array();
165
+			$count = 0;
166
+			foreach ($this->scopes() as $scope_name => $desc) {
167
+				$count++;
168
+				$acceptable_scopes[ 'PMD_scope*' . $count ] = array( 'LIKE', '%' . $scope_name . '%' );
169
+			}
170
+			return array_replace_recursive(array( array( 'OR*active_scope' => $acceptable_scopes ) ), $query_params);
171
+		}
172
+	}
173
+
174
+	/**
175
+	 * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params
176
+	 * argument to get all active for a given scope
177
+	 * @param string $scope one of the constants EEM_Payment_Method::scope_*
178
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
179
+	 * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
180
+	 * @throws EE_Error
181
+	 */
182
+	public function get_query_params_for_all_active($scope = null, $query_params = array())
183
+	{
184
+		return $this->_get_query_params_for_all_active($scope, $query_params);
185
+	}
186
+
187
+
188
+	/**
189
+	 * Gets one active payment method. see @get_all_active for documentation
190
+	 * @param string $scope
191
+	 * @param array  $query_params
192
+	 * @return EE_Payment_Method
193
+	 */
194
+	public function get_one_active($scope = null, $query_params = array())
195
+	{
196
+		return $this->get_one($this->_get_query_params_for_all_active($scope, $query_params));
197
+	}
198
+
199
+
200
+
201
+	/**
202
+	 * Gets one payment method of that type, regardless of whether its active or not
203
+	 * @param string $type
204
+	 * @return EE_Payment_Method
205
+	 */
206
+	public function get_one_of_type($type)
207
+	{
208
+		return $this->get_one(array( array( 'PMD_type' => $type ) ));
209
+	}
210
+
211
+
212
+
213
+	/**
214
+	 * Overrides parent ot also check by the slug
215
+	 * @see EEM_Base::ensure_is_obj()
216
+	 * @param string|int|EE_Payment_Method $base_class_obj_or_id
217
+	 * @param boolean                      $ensure_is_in_db
218
+	 * @return EE_Payment_Method
219
+	 * @throws EE_Error
220
+	 */
221
+	public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false)
222
+	{
223
+		// first: check if it's a slug
224
+		if (is_string($base_class_obj_or_id)) {
225
+			$obj = $this->get_one_by_slug($base_class_obj_or_id);
226
+			if ($obj) {
227
+				return $obj;
228
+			}
229
+		}
230
+		// ok so it wasn't a slug we were passed. try the usual then (ie, it's an object or an ID)
231
+		try {
232
+			return parent::ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db);
233
+		} catch (EE_Error $e) {
234
+			// handle it outside the catch
235
+		}
236
+		throw new EE_Error(sprintf(__("'%s' is neither a Payment Method ID, slug, nor object.", "event_espresso"), $base_class_obj_or_id));
237
+	}
238
+
239
+
240
+
241
+	/**
242
+	 * Gets the ID of this object, or if its a string finds the object's id
243
+	 * associated with that slug
244
+	 * @param mixed $base_obj_or_id_or_slug
245
+	 * @return int
246
+	 */
247
+	public function ensure_is_ID($base_obj_or_id_or_slug)
248
+	{
249
+		if (is_string($base_obj_or_id_or_slug)) {
250
+			// assume it's a slug
251
+			$base_obj_or_id_or_slug = $this->get_one_by_slug($base_obj_or_id_or_slug);
252
+		}
253
+		return parent::ensure_is_ID($base_obj_or_id_or_slug);
254
+	}
255
+
256
+
257
+
258
+	/**
259
+	 * Verifies the button urls on all the passed payment methods have a valid button url. If not, resets them to their default.
260
+	 * @param EE_Payment_Method[] $payment_methods. If NULL is provided defaults to all payment methods active in the cart
261
+	 */
262
+	public function verify_button_urls($payment_methods = null)
263
+	{
264
+		$payment_methods = is_array($payment_methods) ? $payment_methods : $this->get_all_active(EEM_Payment_Method::scope_cart);
265
+		foreach ($payment_methods as $payment_method) {
266
+			try {
267
+				// If there is really no button URL at all, or if the button URLs still point to decaf folder even
268
+				// though this is a caffeinated install, reset it to the default.
269
+				$current_button_url = $payment_method->button_url();
270
+				if (empty($current_button_url)
271
+				|| (
272
+						strpos($current_button_url, 'decaf') !== false
273
+						&& strpos($payment_method->type_obj()->default_button_url(), 'decaf') === false
274
+					)
275
+				) {
276
+					$payment_method->save(
277
+						[
278
+							'PMD_button_url' => $payment_method->type_obj()->default_button_url()
279
+						]
280
+					);
281
+				}
282
+			} catch (EE_Error $e) {
283
+				$payment_method->set_active(false);
284
+			}
285
+		}
286
+	}
287
+
288
+
289
+
290
+	/**
291
+	 * Overrides parent to not only turn wpdb results into EE_Payment_Method objects,
292
+	 * but also verifies the payment method type of each is a usable object. If not,
293
+	 * deactivate it, sets a notification, and deactivates it
294
+	 *
295
+	 * @param array $rows
296
+	 * @return EE_Payment_Method[]
297
+	 * @throws InvalidDataTypeException
298
+	 */
299
+	protected function _create_objects($rows = array())
300
+	{
301
+		EE_Registry::instance()->load_lib('Payment_Method_Manager');
302
+		$payment_methods = parent::_create_objects($rows);
303
+		/* @var $payment_methods EE_Payment_Method[] */
304
+		$usable_payment_methods = array();
305
+		foreach ($payment_methods as $key => $payment_method) {
306
+			if (EE_Payment_Method_Manager::instance()->payment_method_type_exists($payment_method->type())) {
307
+				$usable_payment_methods[ $key ] = $payment_method;
308
+				// some payment methods enqueue their scripts in EE_PMT_*::__construct
309
+				// which is kinda a no-no (just because it's being constructed doesn't mean we need to enqueue
310
+				// its scripts). but for backwards-compat we should continue to do that
311
+				$payment_method->type_obj();
312
+			} elseif ($payment_method->active()) {
313
+				// only deactivate and notify the admin if the payment is active somewhere
314
+				$payment_method->deactivate();
315
+				$payment_method->save();
316
+				do_action(
317
+					'AHEE__EEM_Payment_Method___create_objects_auto_deactivated_payment_method',
318
+					$payment_method
319
+				);
320
+				new PersistentAdminNotice(
321
+					'auto-deactivated-' . $payment_method->type(),
322
+					sprintf(
323
+						__(
324
+							'The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.',
325
+							'event_espresso'
326
+						),
327
+						$payment_method->admin_name(),
328
+						'<br />',
329
+						'<a href="' . admin_url('plugins.php') . '">',
330
+						'</a>'
331
+					),
332
+					true
333
+				);
334
+			}
335
+		}
336
+		return $usable_payment_methods;
337
+	}
338
+
339
+
340
+
341
+	/**
342
+	 * Gets all the payment methods which can be used for transaction
343
+	 * (according to the relations between payment methods and events, and
344
+	 * the currencies used for the transaction and their relation to payment methods)
345
+	 * @param EE_Transaction $transaction
346
+	 * @param string    $scope @see EEM_Payment_Method::get_all_for_events
347
+	 * @return EE_Payment_Method[]
348
+	 */
349
+	public function get_all_for_transaction($transaction, $scope)
350
+	{
351
+		// give addons a chance to override what payment methods are chosen based on the transaction
352
+		return apply_filters(
353
+			'FHEE__EEM_Payment_Method__get_all_for_transaction__payment_methods',
354
+			$this->get_all_active($scope, array( 'group_by' => 'PMD_type' )),
355
+			$transaction,
356
+			$scope
357
+		);
358
+	}
359
+
360
+
361
+	/**
362
+	 * Returns the payment method used for the last payment made for a registration.
363
+	 *
364
+	 * Note: if an offline payment method was selected on the related transaction then this will have no payment methods returned.
365
+	 * It will ONLY return a payment method for a PAYMENT recorded against the registration.
366
+	 *
367
+	 * @param EE_Registration|int $registration_or_reg_id  Either the EE_Registration object or the id for the registration.
368
+	 * @return EE_Payment|null
369
+	 */
370
+	public function get_last_used_for_registration($registration_or_reg_id)
371
+	{
372
+		$registration_id = EEM_Registration::instance()->ensure_is_ID($registration_or_reg_id);
373
+
374
+		$query_params = array(
375
+			0 => array(
376
+				'Payment.Registration.REG_ID' => $registration_id,
377
+			),
378
+			'order_by' => array( 'Payment.PAY_ID' => 'DESC' )
379
+		);
380
+		return $this->get_one($query_params);
381
+	}
382 382
 }
Please login to merge, or discard this patch.