@@ -26,494 +26,494 @@ |
||
26 | 26 | class EE_Form_Section extends EE_Base_Class |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * @var Attributes |
|
31 | - */ |
|
32 | - private $attributes; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var EE_Form_Element[] |
|
36 | - */ |
|
37 | - private $form_elements = []; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var FormLabel |
|
41 | - */ |
|
42 | - private $label; |
|
43 | - |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * @param array $props_n_values |
|
48 | - * @return EE_Form_Section |
|
49 | - * @throws EE_Error |
|
50 | - * @throws ReflectionException |
|
51 | - */ |
|
52 | - public static function new_instance(array $props_n_values = []): EE_Form_Section |
|
53 | - { |
|
54 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
55 | - return $has_object ?: new self($props_n_values); |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * @param array $props_n_values |
|
61 | - * @return EE_Form_Section |
|
62 | - * @throws EE_Error |
|
63 | - * @throws ReflectionException |
|
64 | - */ |
|
65 | - public static function new_instance_from_db(array $props_n_values = []): EE_Form_Section |
|
66 | - { |
|
67 | - return new self($props_n_values, true); |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * Form Section UUID (universally unique identifier) |
|
73 | - * |
|
74 | - * @return string |
|
75 | - * @throws EE_Error |
|
76 | - * @throws ReflectionException |
|
77 | - */ |
|
78 | - public function UUID(): string |
|
79 | - { |
|
80 | - return $this->get('FSC_UUID'); |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * last 8 characters of the UUID |
|
86 | - * |
|
87 | - * @return string |
|
88 | - * @throws EE_Error |
|
89 | - * @throws ReflectionException |
|
90 | - */ |
|
91 | - public function uuidSlug(): string |
|
92 | - { |
|
93 | - return substr($this->UUID(), -8); |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - /** |
|
98 | - * @param string $UUID |
|
99 | - * @throws EE_Error |
|
100 | - * @throws ReflectionException |
|
101 | - */ |
|
102 | - public function setUUID(string $UUID) |
|
103 | - { |
|
104 | - if (! Cuid::isCuid($UUID)) { |
|
105 | - throw new InvalidArgumentException( |
|
106 | - sprintf( |
|
107 | - /* translators: 1: UUID value, 2: UUID generator function. */ |
|
108 | - esc_html__( |
|
109 | - 'The supplied UUID "%1$s" is invalid or missing. Please use %2$s to generate a valid one.', |
|
110 | - 'event_espresso' |
|
111 | - ), |
|
112 | - $UUID, |
|
113 | - "`Cuid::cuid()`" |
|
114 | - ) |
|
115 | - ); |
|
116 | - } |
|
117 | - $this->set('FSC_UUID', $UUID); |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * Form user types that this form section should be presented to. |
|
123 | - * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
124 | - * |
|
125 | - * @return string |
|
126 | - * @throws EE_Error |
|
127 | - * @throws ReflectionException |
|
128 | - */ |
|
129 | - public function appliesTo(): string |
|
130 | - { |
|
131 | - return $this->get('FSC_appliesTo'); |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * Form user types that this form section should be presented to. |
|
137 | - * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
138 | - * |
|
139 | - * @param EE_Registration|string $registrant |
|
140 | - * @return bool |
|
141 | - * @throws EE_Error |
|
142 | - * @throws ReflectionException |
|
143 | - */ |
|
144 | - public function appliesToRegistrant($registrant): bool |
|
145 | - { |
|
146 | - switch ($this->appliesTo()) { |
|
147 | - case EEM_Form_Section::APPLIES_TO_PRIMARY: |
|
148 | - return $registrant instanceof EE_Registration && $registrant->is_primary_registrant(); |
|
149 | - case EEM_Form_Section::APPLIES_TO_PURCHASER: |
|
150 | - return $registrant === 'purchaser'; |
|
151 | - case EEM_Form_Section::APPLIES_TO_REGISTRANTS: |
|
152 | - return $registrant instanceof EE_Registration && ! $registrant->is_primary_registrant(); |
|
153 | - case EEM_Form_Section::APPLIES_TO_ALL: |
|
154 | - default: |
|
155 | - return true; |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * @param string $user_type |
|
162 | - * @throws EE_Error |
|
163 | - * @throws ReflectionException |
|
164 | - */ |
|
165 | - public function setAppliesTo(string $user_type) |
|
166 | - { |
|
167 | - $this->set('FSC_appliesTo', $user_type); |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * JSON string of HTML attributes, such as class, to be applied to this form section\'s container. |
|
173 | - * |
|
174 | - * @return Attributes |
|
175 | - * @throws EE_Error |
|
176 | - * @throws ReflectionException |
|
177 | - */ |
|
178 | - public function attributes(): ?Attributes |
|
179 | - { |
|
180 | - if (! $this->attributes instanceof Attributes) { |
|
181 | - $this->attributes = Attributes::fromJson($this->get('FSC_attributes')); |
|
182 | - } |
|
183 | - return $this->attributes; |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * @param Attributes $attributes |
|
189 | - * @throws EE_Error |
|
190 | - * @throws ReflectionException |
|
191 | - */ |
|
192 | - public function setAttributes(Attributes $attributes) |
|
193 | - { |
|
194 | - // set local object |
|
195 | - $this->attributes = $attributes; |
|
196 | - // then pass to model as an array which will get converted to JSON by the model field |
|
197 | - $this->set('FSC_attributes', $attributes->toArray()); |
|
198 | - } |
|
199 | - |
|
200 | - |
|
201 | - /** |
|
202 | - * UUID or ID of related entity this form section belongs to. |
|
203 | - * |
|
204 | - * @return string |
|
205 | - * @throws EE_Error |
|
206 | - * @throws ReflectionException |
|
207 | - */ |
|
208 | - public function belongsTo(): ?string |
|
209 | - { |
|
210 | - return $this->get('FSC_belongsTo'); |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * @param string $parent_UUID |
|
216 | - * @throws EE_Error |
|
217 | - * @throws ReflectionException |
|
218 | - */ |
|
219 | - public function setBelongsTo(string $parent_UUID) |
|
220 | - { |
|
221 | - $this->set('FSC_belongsTo', $parent_UUID); |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - /** |
|
226 | - * @return EE_Form_Element[] |
|
227 | - * @throws EE_Error |
|
228 | - * @throws ReflectionException |
|
229 | - */ |
|
230 | - public function formElements(): array |
|
231 | - { |
|
232 | - return $this->form_elements ?: $this->getFormElements(); |
|
233 | - } |
|
234 | - |
|
235 | - |
|
236 | - /** |
|
237 | - * @return EE_Form_Element[] |
|
238 | - * @throws EE_Error |
|
239 | - * @throws ReflectionException |
|
240 | - */ |
|
241 | - private function getFormElements(): array |
|
242 | - { |
|
243 | - $form_elements = $this->get_many_related('Form_Element', ['order_by' => ['FIN_order' => 'ASC']]); |
|
244 | - foreach ($form_elements as $form_element) { |
|
245 | - if ($form_element instanceof EE_Form_Element) { |
|
246 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
247 | - } |
|
248 | - } |
|
249 | - return $this->form_elements; |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - /** |
|
254 | - * @param EE_Form_Element[] $form_elements |
|
255 | - * @throws EE_Error |
|
256 | - * @throws ReflectionException |
|
257 | - */ |
|
258 | - public function setFormElements(array $form_elements): void |
|
259 | - { |
|
260 | - foreach ($form_elements as $form_element) { |
|
261 | - if ($form_element instanceof EE_Form_Element) { |
|
262 | - $this->_add_relation_to($form_element->UUID(), 'Form_Element'); |
|
263 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
264 | - } |
|
265 | - } |
|
266 | - } |
|
267 | - |
|
268 | - |
|
269 | - /** |
|
270 | - * @param EE_Form_Element[] $all_form_elements |
|
271 | - * @return EE_Form_Element[] |
|
272 | - * @throws EE_Error |
|
273 | - * @throws ReflectionException |
|
274 | - */ |
|
275 | - public function filterFormElements(array $all_form_elements): array |
|
276 | - { |
|
277 | - return array_filter($all_form_elements, $this->formElementFilter()); |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * returns a closure that can be used to filter form elements for this form section |
|
283 | - * usage: |
|
284 | - * $filter = EEM_Form_Element::formElementFilter(); |
|
285 | - * $filtered_form_elements = array_filter( $all_form_elements, $filter ); |
|
286 | - * |
|
287 | - * @return Closure |
|
288 | - * @throws EE_Error |
|
289 | - * @throws ReflectionException |
|
290 | - */ |
|
291 | - public function formElementFilter(): Closure |
|
292 | - { |
|
293 | - $FSC_UUID = strtolower($this->UUID()); |
|
294 | - return function ($form_element) use ($FSC_UUID) { |
|
295 | - return $form_element instanceof EE_Form_Element && strtolower($form_element->belongsTo()) === $FSC_UUID; |
|
296 | - }; |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - /** |
|
301 | - * returns a FormLabel object for managing form section labels, ie: the form section heading |
|
302 | - * |
|
303 | - * @return FormLabel |
|
304 | - * @throws EE_Error |
|
305 | - * @throws ReflectionException |
|
306 | - */ |
|
307 | - public function label(): ?FormLabel |
|
308 | - { |
|
309 | - if (! $this->label instanceof FormLabel) { |
|
310 | - $this->label = FormLabel::fromJson($this->get('FSC_label')); |
|
311 | - } |
|
312 | - return $this->label; |
|
313 | - } |
|
314 | - |
|
315 | - |
|
316 | - /** |
|
317 | - * @param FormLabel $label |
|
318 | - * @throws EE_Error |
|
319 | - * @throws ReflectionException |
|
320 | - */ |
|
321 | - public function setLabel(FormLabel $label) |
|
322 | - { |
|
323 | - // set local object |
|
324 | - $this->label = $label; |
|
325 | - // then pass to model as an array which will get converted to JSON by the model field |
|
326 | - $this->set('FSC_label', $label->toJson()); |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * Order in which form section appears in a form. |
|
332 | - * |
|
333 | - * @return int |
|
334 | - * @throws EE_Error |
|
335 | - * @throws ReflectionException |
|
336 | - */ |
|
337 | - public function order(): int |
|
338 | - { |
|
339 | - return $this->get('FSC_order'); |
|
340 | - } |
|
341 | - |
|
342 | - |
|
343 | - /** |
|
344 | - * @param int $order |
|
345 | - * @throws EE_Error |
|
346 | - * @throws ReflectionException |
|
347 | - */ |
|
348 | - public function setOrder(int $order) |
|
349 | - { |
|
350 | - $this->set('FSC_order', $order); |
|
351 | - } |
|
352 | - |
|
353 | - |
|
354 | - /** |
|
355 | - * combination of public label and UUID slug for use in identifiers |
|
356 | - * |
|
357 | - * @return string |
|
358 | - * @throws EE_Error |
|
359 | - * @throws ReflectionException |
|
360 | - */ |
|
361 | - public function slug(): ?string |
|
362 | - { |
|
363 | - $label = sanitize_title($this->label()->publicLabel()); |
|
364 | - return "{$label}-{$this->uuidSlug()}"; |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - /** |
|
369 | - * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
370 | - * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
371 | - * |
|
372 | - * @return string |
|
373 | - * @throws EE_Error |
|
374 | - * @throws ReflectionException |
|
375 | - */ |
|
376 | - public function status(): ?string |
|
377 | - { |
|
378 | - return $this->get('FSC_status'); |
|
379 | - } |
|
380 | - |
|
381 | - |
|
382 | - /** |
|
383 | - * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
384 | - * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
385 | - * |
|
386 | - * @param string $status |
|
387 | - * @throws EE_Error |
|
388 | - * @throws ReflectionException |
|
389 | - */ |
|
390 | - public function setStatus(string $status) |
|
391 | - { |
|
392 | - $this->set('FSC_status', $status); |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - /** |
|
397 | - * returns the id the wordpress user who created this question |
|
398 | - * |
|
399 | - * @return int |
|
400 | - * @throws EE_Error |
|
401 | - * @throws ReflectionException |
|
402 | - */ |
|
403 | - public function wp_user(): int |
|
404 | - { |
|
405 | - return $this->get('FSC_wpUser'); |
|
406 | - } |
|
407 | - |
|
408 | - |
|
409 | - /** |
|
410 | - * @param int $wp_user |
|
411 | - * @throws EE_Error |
|
412 | - * @throws ReflectionException |
|
413 | - */ |
|
414 | - public function setWpUser(int $wp_user) |
|
415 | - { |
|
416 | - $this->set('FSC_wpUser', $wp_user); |
|
417 | - } |
|
418 | - |
|
419 | - |
|
420 | - /** |
|
421 | - * @param array $set_cols_n_values |
|
422 | - * @return bool|int|string |
|
423 | - * @throws EE_Error |
|
424 | - * @throws ReflectionException |
|
425 | - */ |
|
426 | - public function save($set_cols_n_values = []) |
|
427 | - { |
|
428 | - // make sure internal versions for all composite objects are updated |
|
429 | - $this->set('FSC_attributes', $this->attributes()->toArray()); |
|
430 | - $this->set('FSC_label', $this->label()->toArray()); |
|
431 | - return parent::save($set_cols_n_values); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Whether the section is active. |
|
436 | - * |
|
437 | - * @return boolean TRUE if is active, FALSE if not. |
|
438 | - * @throws ReflectionException |
|
439 | - * @throws InvalidArgumentException |
|
440 | - * @throws InvalidInterfaceException |
|
441 | - * @throws InvalidDataTypeException |
|
442 | - * @throws EE_Error |
|
443 | - */ |
|
444 | - public function isActive(): bool |
|
445 | - { |
|
446 | - return $this->status() === FormStatus::ACTIVE; |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Whether the section is archived. |
|
451 | - * |
|
452 | - * @return boolean TRUE if is archived, FALSE if not. |
|
453 | - * @throws ReflectionException |
|
454 | - * @throws InvalidArgumentException |
|
455 | - * @throws InvalidInterfaceException |
|
456 | - * @throws InvalidDataTypeException |
|
457 | - * @throws EE_Error |
|
458 | - */ |
|
459 | - public function isArchived(): bool |
|
460 | - { |
|
461 | - return $this->status() === FormStatus::ARCHIVED; |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Whether the section is a default one. |
|
466 | - * |
|
467 | - * @return boolean TRUE if is default, FALSE if not. |
|
468 | - * @throws ReflectionException |
|
469 | - * @throws InvalidArgumentException |
|
470 | - * @throws InvalidInterfaceException |
|
471 | - * @throws InvalidDataTypeException |
|
472 | - * @throws EE_Error |
|
473 | - */ |
|
474 | - public function isDefault(): bool |
|
475 | - { |
|
476 | - return $this->status() === FormStatus::DEFAULT; |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Whether the section is a shared one. |
|
481 | - * |
|
482 | - * @return boolean TRUE if is shared, FALSE if not. |
|
483 | - * @throws ReflectionException |
|
484 | - * @throws InvalidArgumentException |
|
485 | - * @throws InvalidInterfaceException |
|
486 | - * @throws InvalidDataTypeException |
|
487 | - * @throws EE_Error |
|
488 | - */ |
|
489 | - public function isShared(): bool |
|
490 | - { |
|
491 | - return $this->status() === FormStatus::SHARED; |
|
492 | - } |
|
493 | - |
|
494 | - /** |
|
495 | - * Whether the section is trashed. |
|
496 | - * |
|
497 | - * @return boolean TRUE if is trashed, FALSE if not. |
|
498 | - * @throws ReflectionException |
|
499 | - * @throws InvalidArgumentException |
|
500 | - * @throws InvalidInterfaceException |
|
501 | - * @throws InvalidDataTypeException |
|
502 | - * @throws EE_Error |
|
503 | - */ |
|
504 | - public function isTrashed(): bool |
|
505 | - { |
|
506 | - return $this->status() === FormStatus::TRASHED; |
|
507 | - } |
|
508 | - |
|
509 | - |
|
510 | - /** |
|
511 | - * @return bool |
|
512 | - * @throws EE_Error |
|
513 | - * @throws ReflectionException |
|
514 | - */ |
|
515 | - public function isTopLevelFormSection(): bool |
|
516 | - { |
|
517 | - return empty($this->belongsTo()); |
|
518 | - } |
|
29 | + /** |
|
30 | + * @var Attributes |
|
31 | + */ |
|
32 | + private $attributes; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var EE_Form_Element[] |
|
36 | + */ |
|
37 | + private $form_elements = []; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var FormLabel |
|
41 | + */ |
|
42 | + private $label; |
|
43 | + |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * @param array $props_n_values |
|
48 | + * @return EE_Form_Section |
|
49 | + * @throws EE_Error |
|
50 | + * @throws ReflectionException |
|
51 | + */ |
|
52 | + public static function new_instance(array $props_n_values = []): EE_Form_Section |
|
53 | + { |
|
54 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
55 | + return $has_object ?: new self($props_n_values); |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * @param array $props_n_values |
|
61 | + * @return EE_Form_Section |
|
62 | + * @throws EE_Error |
|
63 | + * @throws ReflectionException |
|
64 | + */ |
|
65 | + public static function new_instance_from_db(array $props_n_values = []): EE_Form_Section |
|
66 | + { |
|
67 | + return new self($props_n_values, true); |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * Form Section UUID (universally unique identifier) |
|
73 | + * |
|
74 | + * @return string |
|
75 | + * @throws EE_Error |
|
76 | + * @throws ReflectionException |
|
77 | + */ |
|
78 | + public function UUID(): string |
|
79 | + { |
|
80 | + return $this->get('FSC_UUID'); |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * last 8 characters of the UUID |
|
86 | + * |
|
87 | + * @return string |
|
88 | + * @throws EE_Error |
|
89 | + * @throws ReflectionException |
|
90 | + */ |
|
91 | + public function uuidSlug(): string |
|
92 | + { |
|
93 | + return substr($this->UUID(), -8); |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + /** |
|
98 | + * @param string $UUID |
|
99 | + * @throws EE_Error |
|
100 | + * @throws ReflectionException |
|
101 | + */ |
|
102 | + public function setUUID(string $UUID) |
|
103 | + { |
|
104 | + if (! Cuid::isCuid($UUID)) { |
|
105 | + throw new InvalidArgumentException( |
|
106 | + sprintf( |
|
107 | + /* translators: 1: UUID value, 2: UUID generator function. */ |
|
108 | + esc_html__( |
|
109 | + 'The supplied UUID "%1$s" is invalid or missing. Please use %2$s to generate a valid one.', |
|
110 | + 'event_espresso' |
|
111 | + ), |
|
112 | + $UUID, |
|
113 | + "`Cuid::cuid()`" |
|
114 | + ) |
|
115 | + ); |
|
116 | + } |
|
117 | + $this->set('FSC_UUID', $UUID); |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * Form user types that this form section should be presented to. |
|
123 | + * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
124 | + * |
|
125 | + * @return string |
|
126 | + * @throws EE_Error |
|
127 | + * @throws ReflectionException |
|
128 | + */ |
|
129 | + public function appliesTo(): string |
|
130 | + { |
|
131 | + return $this->get('FSC_appliesTo'); |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * Form user types that this form section should be presented to. |
|
137 | + * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
138 | + * |
|
139 | + * @param EE_Registration|string $registrant |
|
140 | + * @return bool |
|
141 | + * @throws EE_Error |
|
142 | + * @throws ReflectionException |
|
143 | + */ |
|
144 | + public function appliesToRegistrant($registrant): bool |
|
145 | + { |
|
146 | + switch ($this->appliesTo()) { |
|
147 | + case EEM_Form_Section::APPLIES_TO_PRIMARY: |
|
148 | + return $registrant instanceof EE_Registration && $registrant->is_primary_registrant(); |
|
149 | + case EEM_Form_Section::APPLIES_TO_PURCHASER: |
|
150 | + return $registrant === 'purchaser'; |
|
151 | + case EEM_Form_Section::APPLIES_TO_REGISTRANTS: |
|
152 | + return $registrant instanceof EE_Registration && ! $registrant->is_primary_registrant(); |
|
153 | + case EEM_Form_Section::APPLIES_TO_ALL: |
|
154 | + default: |
|
155 | + return true; |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * @param string $user_type |
|
162 | + * @throws EE_Error |
|
163 | + * @throws ReflectionException |
|
164 | + */ |
|
165 | + public function setAppliesTo(string $user_type) |
|
166 | + { |
|
167 | + $this->set('FSC_appliesTo', $user_type); |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * JSON string of HTML attributes, such as class, to be applied to this form section\'s container. |
|
173 | + * |
|
174 | + * @return Attributes |
|
175 | + * @throws EE_Error |
|
176 | + * @throws ReflectionException |
|
177 | + */ |
|
178 | + public function attributes(): ?Attributes |
|
179 | + { |
|
180 | + if (! $this->attributes instanceof Attributes) { |
|
181 | + $this->attributes = Attributes::fromJson($this->get('FSC_attributes')); |
|
182 | + } |
|
183 | + return $this->attributes; |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * @param Attributes $attributes |
|
189 | + * @throws EE_Error |
|
190 | + * @throws ReflectionException |
|
191 | + */ |
|
192 | + public function setAttributes(Attributes $attributes) |
|
193 | + { |
|
194 | + // set local object |
|
195 | + $this->attributes = $attributes; |
|
196 | + // then pass to model as an array which will get converted to JSON by the model field |
|
197 | + $this->set('FSC_attributes', $attributes->toArray()); |
|
198 | + } |
|
199 | + |
|
200 | + |
|
201 | + /** |
|
202 | + * UUID or ID of related entity this form section belongs to. |
|
203 | + * |
|
204 | + * @return string |
|
205 | + * @throws EE_Error |
|
206 | + * @throws ReflectionException |
|
207 | + */ |
|
208 | + public function belongsTo(): ?string |
|
209 | + { |
|
210 | + return $this->get('FSC_belongsTo'); |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * @param string $parent_UUID |
|
216 | + * @throws EE_Error |
|
217 | + * @throws ReflectionException |
|
218 | + */ |
|
219 | + public function setBelongsTo(string $parent_UUID) |
|
220 | + { |
|
221 | + $this->set('FSC_belongsTo', $parent_UUID); |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + /** |
|
226 | + * @return EE_Form_Element[] |
|
227 | + * @throws EE_Error |
|
228 | + * @throws ReflectionException |
|
229 | + */ |
|
230 | + public function formElements(): array |
|
231 | + { |
|
232 | + return $this->form_elements ?: $this->getFormElements(); |
|
233 | + } |
|
234 | + |
|
235 | + |
|
236 | + /** |
|
237 | + * @return EE_Form_Element[] |
|
238 | + * @throws EE_Error |
|
239 | + * @throws ReflectionException |
|
240 | + */ |
|
241 | + private function getFormElements(): array |
|
242 | + { |
|
243 | + $form_elements = $this->get_many_related('Form_Element', ['order_by' => ['FIN_order' => 'ASC']]); |
|
244 | + foreach ($form_elements as $form_element) { |
|
245 | + if ($form_element instanceof EE_Form_Element) { |
|
246 | + $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
247 | + } |
|
248 | + } |
|
249 | + return $this->form_elements; |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + /** |
|
254 | + * @param EE_Form_Element[] $form_elements |
|
255 | + * @throws EE_Error |
|
256 | + * @throws ReflectionException |
|
257 | + */ |
|
258 | + public function setFormElements(array $form_elements): void |
|
259 | + { |
|
260 | + foreach ($form_elements as $form_element) { |
|
261 | + if ($form_element instanceof EE_Form_Element) { |
|
262 | + $this->_add_relation_to($form_element->UUID(), 'Form_Element'); |
|
263 | + $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
264 | + } |
|
265 | + } |
|
266 | + } |
|
267 | + |
|
268 | + |
|
269 | + /** |
|
270 | + * @param EE_Form_Element[] $all_form_elements |
|
271 | + * @return EE_Form_Element[] |
|
272 | + * @throws EE_Error |
|
273 | + * @throws ReflectionException |
|
274 | + */ |
|
275 | + public function filterFormElements(array $all_form_elements): array |
|
276 | + { |
|
277 | + return array_filter($all_form_elements, $this->formElementFilter()); |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * returns a closure that can be used to filter form elements for this form section |
|
283 | + * usage: |
|
284 | + * $filter = EEM_Form_Element::formElementFilter(); |
|
285 | + * $filtered_form_elements = array_filter( $all_form_elements, $filter ); |
|
286 | + * |
|
287 | + * @return Closure |
|
288 | + * @throws EE_Error |
|
289 | + * @throws ReflectionException |
|
290 | + */ |
|
291 | + public function formElementFilter(): Closure |
|
292 | + { |
|
293 | + $FSC_UUID = strtolower($this->UUID()); |
|
294 | + return function ($form_element) use ($FSC_UUID) { |
|
295 | + return $form_element instanceof EE_Form_Element && strtolower($form_element->belongsTo()) === $FSC_UUID; |
|
296 | + }; |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + /** |
|
301 | + * returns a FormLabel object for managing form section labels, ie: the form section heading |
|
302 | + * |
|
303 | + * @return FormLabel |
|
304 | + * @throws EE_Error |
|
305 | + * @throws ReflectionException |
|
306 | + */ |
|
307 | + public function label(): ?FormLabel |
|
308 | + { |
|
309 | + if (! $this->label instanceof FormLabel) { |
|
310 | + $this->label = FormLabel::fromJson($this->get('FSC_label')); |
|
311 | + } |
|
312 | + return $this->label; |
|
313 | + } |
|
314 | + |
|
315 | + |
|
316 | + /** |
|
317 | + * @param FormLabel $label |
|
318 | + * @throws EE_Error |
|
319 | + * @throws ReflectionException |
|
320 | + */ |
|
321 | + public function setLabel(FormLabel $label) |
|
322 | + { |
|
323 | + // set local object |
|
324 | + $this->label = $label; |
|
325 | + // then pass to model as an array which will get converted to JSON by the model field |
|
326 | + $this->set('FSC_label', $label->toJson()); |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * Order in which form section appears in a form. |
|
332 | + * |
|
333 | + * @return int |
|
334 | + * @throws EE_Error |
|
335 | + * @throws ReflectionException |
|
336 | + */ |
|
337 | + public function order(): int |
|
338 | + { |
|
339 | + return $this->get('FSC_order'); |
|
340 | + } |
|
341 | + |
|
342 | + |
|
343 | + /** |
|
344 | + * @param int $order |
|
345 | + * @throws EE_Error |
|
346 | + * @throws ReflectionException |
|
347 | + */ |
|
348 | + public function setOrder(int $order) |
|
349 | + { |
|
350 | + $this->set('FSC_order', $order); |
|
351 | + } |
|
352 | + |
|
353 | + |
|
354 | + /** |
|
355 | + * combination of public label and UUID slug for use in identifiers |
|
356 | + * |
|
357 | + * @return string |
|
358 | + * @throws EE_Error |
|
359 | + * @throws ReflectionException |
|
360 | + */ |
|
361 | + public function slug(): ?string |
|
362 | + { |
|
363 | + $label = sanitize_title($this->label()->publicLabel()); |
|
364 | + return "{$label}-{$this->uuidSlug()}"; |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + /** |
|
369 | + * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
370 | + * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
371 | + * |
|
372 | + * @return string |
|
373 | + * @throws EE_Error |
|
374 | + * @throws ReflectionException |
|
375 | + */ |
|
376 | + public function status(): ?string |
|
377 | + { |
|
378 | + return $this->get('FSC_status'); |
|
379 | + } |
|
380 | + |
|
381 | + |
|
382 | + /** |
|
383 | + * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
384 | + * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
385 | + * |
|
386 | + * @param string $status |
|
387 | + * @throws EE_Error |
|
388 | + * @throws ReflectionException |
|
389 | + */ |
|
390 | + public function setStatus(string $status) |
|
391 | + { |
|
392 | + $this->set('FSC_status', $status); |
|
393 | + } |
|
394 | + |
|
395 | + |
|
396 | + /** |
|
397 | + * returns the id the wordpress user who created this question |
|
398 | + * |
|
399 | + * @return int |
|
400 | + * @throws EE_Error |
|
401 | + * @throws ReflectionException |
|
402 | + */ |
|
403 | + public function wp_user(): int |
|
404 | + { |
|
405 | + return $this->get('FSC_wpUser'); |
|
406 | + } |
|
407 | + |
|
408 | + |
|
409 | + /** |
|
410 | + * @param int $wp_user |
|
411 | + * @throws EE_Error |
|
412 | + * @throws ReflectionException |
|
413 | + */ |
|
414 | + public function setWpUser(int $wp_user) |
|
415 | + { |
|
416 | + $this->set('FSC_wpUser', $wp_user); |
|
417 | + } |
|
418 | + |
|
419 | + |
|
420 | + /** |
|
421 | + * @param array $set_cols_n_values |
|
422 | + * @return bool|int|string |
|
423 | + * @throws EE_Error |
|
424 | + * @throws ReflectionException |
|
425 | + */ |
|
426 | + public function save($set_cols_n_values = []) |
|
427 | + { |
|
428 | + // make sure internal versions for all composite objects are updated |
|
429 | + $this->set('FSC_attributes', $this->attributes()->toArray()); |
|
430 | + $this->set('FSC_label', $this->label()->toArray()); |
|
431 | + return parent::save($set_cols_n_values); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Whether the section is active. |
|
436 | + * |
|
437 | + * @return boolean TRUE if is active, FALSE if not. |
|
438 | + * @throws ReflectionException |
|
439 | + * @throws InvalidArgumentException |
|
440 | + * @throws InvalidInterfaceException |
|
441 | + * @throws InvalidDataTypeException |
|
442 | + * @throws EE_Error |
|
443 | + */ |
|
444 | + public function isActive(): bool |
|
445 | + { |
|
446 | + return $this->status() === FormStatus::ACTIVE; |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Whether the section is archived. |
|
451 | + * |
|
452 | + * @return boolean TRUE if is archived, FALSE if not. |
|
453 | + * @throws ReflectionException |
|
454 | + * @throws InvalidArgumentException |
|
455 | + * @throws InvalidInterfaceException |
|
456 | + * @throws InvalidDataTypeException |
|
457 | + * @throws EE_Error |
|
458 | + */ |
|
459 | + public function isArchived(): bool |
|
460 | + { |
|
461 | + return $this->status() === FormStatus::ARCHIVED; |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Whether the section is a default one. |
|
466 | + * |
|
467 | + * @return boolean TRUE if is default, FALSE if not. |
|
468 | + * @throws ReflectionException |
|
469 | + * @throws InvalidArgumentException |
|
470 | + * @throws InvalidInterfaceException |
|
471 | + * @throws InvalidDataTypeException |
|
472 | + * @throws EE_Error |
|
473 | + */ |
|
474 | + public function isDefault(): bool |
|
475 | + { |
|
476 | + return $this->status() === FormStatus::DEFAULT; |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Whether the section is a shared one. |
|
481 | + * |
|
482 | + * @return boolean TRUE if is shared, FALSE if not. |
|
483 | + * @throws ReflectionException |
|
484 | + * @throws InvalidArgumentException |
|
485 | + * @throws InvalidInterfaceException |
|
486 | + * @throws InvalidDataTypeException |
|
487 | + * @throws EE_Error |
|
488 | + */ |
|
489 | + public function isShared(): bool |
|
490 | + { |
|
491 | + return $this->status() === FormStatus::SHARED; |
|
492 | + } |
|
493 | + |
|
494 | + /** |
|
495 | + * Whether the section is trashed. |
|
496 | + * |
|
497 | + * @return boolean TRUE if is trashed, FALSE if not. |
|
498 | + * @throws ReflectionException |
|
499 | + * @throws InvalidArgumentException |
|
500 | + * @throws InvalidInterfaceException |
|
501 | + * @throws InvalidDataTypeException |
|
502 | + * @throws EE_Error |
|
503 | + */ |
|
504 | + public function isTrashed(): bool |
|
505 | + { |
|
506 | + return $this->status() === FormStatus::TRASHED; |
|
507 | + } |
|
508 | + |
|
509 | + |
|
510 | + /** |
|
511 | + * @return bool |
|
512 | + * @throws EE_Error |
|
513 | + * @throws ReflectionException |
|
514 | + */ |
|
515 | + public function isTopLevelFormSection(): bool |
|
516 | + { |
|
517 | + return empty($this->belongsTo()); |
|
518 | + } |
|
519 | 519 | } |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | */ |
102 | 102 | public function setUUID(string $UUID) |
103 | 103 | { |
104 | - if (! Cuid::isCuid($UUID)) { |
|
104 | + if ( ! Cuid::isCuid($UUID)) { |
|
105 | 105 | throw new InvalidArgumentException( |
106 | 106 | sprintf( |
107 | 107 | /* translators: 1: UUID value, 2: UUID generator function. */ |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | */ |
178 | 178 | public function attributes(): ?Attributes |
179 | 179 | { |
180 | - if (! $this->attributes instanceof Attributes) { |
|
180 | + if ( ! $this->attributes instanceof Attributes) { |
|
181 | 181 | $this->attributes = Attributes::fromJson($this->get('FSC_attributes')); |
182 | 182 | } |
183 | 183 | return $this->attributes; |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | $form_elements = $this->get_many_related('Form_Element', ['order_by' => ['FIN_order' => 'ASC']]); |
244 | 244 | foreach ($form_elements as $form_element) { |
245 | 245 | if ($form_element instanceof EE_Form_Element) { |
246 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
246 | + $this->form_elements[$form_element->UUID()] = $form_element; |
|
247 | 247 | } |
248 | 248 | } |
249 | 249 | return $this->form_elements; |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | foreach ($form_elements as $form_element) { |
261 | 261 | if ($form_element instanceof EE_Form_Element) { |
262 | 262 | $this->_add_relation_to($form_element->UUID(), 'Form_Element'); |
263 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
263 | + $this->form_elements[$form_element->UUID()] = $form_element; |
|
264 | 264 | } |
265 | 265 | } |
266 | 266 | } |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | public function formElementFilter(): Closure |
292 | 292 | { |
293 | 293 | $FSC_UUID = strtolower($this->UUID()); |
294 | - return function ($form_element) use ($FSC_UUID) { |
|
294 | + return function($form_element) use ($FSC_UUID) { |
|
295 | 295 | return $form_element instanceof EE_Form_Element && strtolower($form_element->belongsTo()) === $FSC_UUID; |
296 | 296 | }; |
297 | 297 | } |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | */ |
307 | 307 | public function label(): ?FormLabel |
308 | 308 | { |
309 | - if (! $this->label instanceof FormLabel) { |
|
309 | + if ( ! $this->label instanceof FormLabel) { |
|
310 | 310 | $this->label = FormLabel::fromJson($this->get('FSC_label')); |
311 | 311 | } |
312 | 312 | return $this->label; |
@@ -30,122 +30,122 @@ |
||
30 | 30 | class NewEventDefaultEntities extends EventEditorData |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * @var DefaultDatetimes |
|
35 | - */ |
|
36 | - protected $default_datetimes; |
|
33 | + /** |
|
34 | + * @var DefaultDatetimes |
|
35 | + */ |
|
36 | + protected $default_datetimes; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @var DefaultFormSections |
|
40 | - */ |
|
41 | - protected $default_form_sections; |
|
38 | + /** |
|
39 | + * @var DefaultFormSections |
|
40 | + */ |
|
41 | + protected $default_form_sections; |
|
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * NewEventDefaultEntities constructor. |
|
46 | - * |
|
47 | - * @param DefaultDatetimes $default_datetimes |
|
48 | - * @param DefaultFormSections $default_form_sections |
|
49 | - * @param EEM_Datetime $datetime_model |
|
50 | - * @param EEM_Event $event_model |
|
51 | - * @param EEM_Price $price_model |
|
52 | - * @param EEM_Price_Type $price_type_model |
|
53 | - * @param EEM_Ticket $ticket_model |
|
54 | - * @param Utilities $utilities |
|
55 | - */ |
|
56 | - public function __construct( |
|
57 | - DefaultDatetimes $default_datetimes, |
|
58 | - DefaultFormSections $default_form_sections, |
|
59 | - EEM_Datetime $datetime_model, |
|
60 | - EEM_Event $event_model, |
|
61 | - EEM_Price $price_model, |
|
62 | - EEM_Price_Type $price_type_model, |
|
63 | - EEM_Ticket $ticket_model, |
|
64 | - Utilities $utilities |
|
65 | - ) { |
|
66 | - $this->default_datetimes = $default_datetimes; |
|
67 | - $this->default_form_sections = $default_form_sections; |
|
68 | - parent::__construct( |
|
69 | - $datetime_model, |
|
70 | - $event_model, |
|
71 | - $price_model, |
|
72 | - $price_type_model, |
|
73 | - $ticket_model, |
|
74 | - $utilities |
|
75 | - ); |
|
76 | - } |
|
44 | + /** |
|
45 | + * NewEventDefaultEntities constructor. |
|
46 | + * |
|
47 | + * @param DefaultDatetimes $default_datetimes |
|
48 | + * @param DefaultFormSections $default_form_sections |
|
49 | + * @param EEM_Datetime $datetime_model |
|
50 | + * @param EEM_Event $event_model |
|
51 | + * @param EEM_Price $price_model |
|
52 | + * @param EEM_Price_Type $price_type_model |
|
53 | + * @param EEM_Ticket $ticket_model |
|
54 | + * @param Utilities $utilities |
|
55 | + */ |
|
56 | + public function __construct( |
|
57 | + DefaultDatetimes $default_datetimes, |
|
58 | + DefaultFormSections $default_form_sections, |
|
59 | + EEM_Datetime $datetime_model, |
|
60 | + EEM_Event $event_model, |
|
61 | + EEM_Price $price_model, |
|
62 | + EEM_Price_Type $price_type_model, |
|
63 | + EEM_Ticket $ticket_model, |
|
64 | + Utilities $utilities |
|
65 | + ) { |
|
66 | + $this->default_datetimes = $default_datetimes; |
|
67 | + $this->default_form_sections = $default_form_sections; |
|
68 | + parent::__construct( |
|
69 | + $datetime_model, |
|
70 | + $event_model, |
|
71 | + $price_model, |
|
72 | + $price_type_model, |
|
73 | + $ticket_model, |
|
74 | + $utilities |
|
75 | + ); |
|
76 | + } |
|
77 | 77 | |
78 | 78 | |
79 | - /** |
|
80 | - * @param int $eventId |
|
81 | - * @return EE_Datetime[] |
|
82 | - * @throws EE_Error |
|
83 | - * @throws InvalidArgumentException |
|
84 | - * @throws InvalidEntityException |
|
85 | - * @throws ReflectionException |
|
86 | - * @since $VID:$ |
|
87 | - */ |
|
88 | - public function getData(int $eventId): array |
|
89 | - { |
|
90 | - $EVT_ID = absint($eventId); |
|
91 | - if ($EVT_ID < 1) { |
|
92 | - throw new InvalidArgumentException( |
|
93 | - esc_html__( |
|
94 | - 'A missing or invalid event ID was received.', |
|
95 | - 'event_espresso' |
|
96 | - ) |
|
97 | - ); |
|
98 | - } |
|
99 | - $event = $this->event_model->get_one_by_ID($EVT_ID); |
|
100 | - if (! $event instanceof EE_Event) { |
|
101 | - throw new InvalidEntityException($event, 'EE_Event'); |
|
102 | - } |
|
103 | - $new_event = isset($_REQUEST['action']) && $_REQUEST['action'] === 'create_new'; |
|
104 | - return [ |
|
105 | - 'datetimes' => $this->createDefaultDatetimes($event, $new_event), |
|
106 | - 'form_sections' => $this->createDefaultFormSections($event, $new_event) |
|
107 | - ]; |
|
108 | - } |
|
79 | + /** |
|
80 | + * @param int $eventId |
|
81 | + * @return EE_Datetime[] |
|
82 | + * @throws EE_Error |
|
83 | + * @throws InvalidArgumentException |
|
84 | + * @throws InvalidEntityException |
|
85 | + * @throws ReflectionException |
|
86 | + * @since $VID:$ |
|
87 | + */ |
|
88 | + public function getData(int $eventId): array |
|
89 | + { |
|
90 | + $EVT_ID = absint($eventId); |
|
91 | + if ($EVT_ID < 1) { |
|
92 | + throw new InvalidArgumentException( |
|
93 | + esc_html__( |
|
94 | + 'A missing or invalid event ID was received.', |
|
95 | + 'event_espresso' |
|
96 | + ) |
|
97 | + ); |
|
98 | + } |
|
99 | + $event = $this->event_model->get_one_by_ID($EVT_ID); |
|
100 | + if (! $event instanceof EE_Event) { |
|
101 | + throw new InvalidEntityException($event, 'EE_Event'); |
|
102 | + } |
|
103 | + $new_event = isset($_REQUEST['action']) && $_REQUEST['action'] === 'create_new'; |
|
104 | + return [ |
|
105 | + 'datetimes' => $this->createDefaultDatetimes($event, $new_event), |
|
106 | + 'form_sections' => $this->createDefaultFormSections($event, $new_event) |
|
107 | + ]; |
|
108 | + } |
|
109 | 109 | |
110 | 110 | |
111 | - /** |
|
112 | - * @param EE_Event $event |
|
113 | - * @param bool $new_event |
|
114 | - * @return EE_Datetime[] |
|
115 | - * @throws EE_Error |
|
116 | - * @throws ReflectionException |
|
117 | - */ |
|
118 | - private function createDefaultDatetimes(EE_Event $event, bool $new_event): array |
|
119 | - { |
|
120 | - $datetime_count = $this->datetime_model->count( |
|
121 | - [ |
|
122 | - [ |
|
123 | - 'EVT_ID' => $event->ID(), |
|
124 | - 'DTT_deleted' => ['IN', [true, false]], |
|
125 | - ], |
|
126 | - 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
127 | - ], |
|
128 | - 'EVT_ID' |
|
129 | - ); |
|
130 | - return $new_event || $datetime_count === 0 |
|
131 | - ? $this->default_datetimes->create($event) |
|
132 | - : []; |
|
133 | - } |
|
111 | + /** |
|
112 | + * @param EE_Event $event |
|
113 | + * @param bool $new_event |
|
114 | + * @return EE_Datetime[] |
|
115 | + * @throws EE_Error |
|
116 | + * @throws ReflectionException |
|
117 | + */ |
|
118 | + private function createDefaultDatetimes(EE_Event $event, bool $new_event): array |
|
119 | + { |
|
120 | + $datetime_count = $this->datetime_model->count( |
|
121 | + [ |
|
122 | + [ |
|
123 | + 'EVT_ID' => $event->ID(), |
|
124 | + 'DTT_deleted' => ['IN', [true, false]], |
|
125 | + ], |
|
126 | + 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
127 | + ], |
|
128 | + 'EVT_ID' |
|
129 | + ); |
|
130 | + return $new_event || $datetime_count === 0 |
|
131 | + ? $this->default_datetimes->create($event) |
|
132 | + : []; |
|
133 | + } |
|
134 | 134 | |
135 | 135 | |
136 | - /** |
|
137 | - * @param EE_Event $event |
|
138 | - * @param bool $new_event |
|
139 | - * @return EE_Form_Section[] |
|
140 | - * @throws EE_Error |
|
141 | - * @throws ReflectionException |
|
142 | - */ |
|
143 | - private function createDefaultFormSections(EE_Event $event, bool $new_event): array |
|
144 | - { |
|
145 | - $reg_form_UUID = $event->registrationFormUuid(); |
|
146 | - // if it's a new event and defaults have not been created yet, OR if there is no reg form at all... |
|
147 | - return ($new_event && ! $reg_form_UUID) || ! $reg_form_UUID |
|
148 | - ? $this->default_form_sections->create($event) |
|
149 | - : []; |
|
150 | - } |
|
136 | + /** |
|
137 | + * @param EE_Event $event |
|
138 | + * @param bool $new_event |
|
139 | + * @return EE_Form_Section[] |
|
140 | + * @throws EE_Error |
|
141 | + * @throws ReflectionException |
|
142 | + */ |
|
143 | + private function createDefaultFormSections(EE_Event $event, bool $new_event): array |
|
144 | + { |
|
145 | + $reg_form_UUID = $event->registrationFormUuid(); |
|
146 | + // if it's a new event and defaults have not been created yet, OR if there is no reg form at all... |
|
147 | + return ($new_event && ! $reg_form_UUID) || ! $reg_form_UUID |
|
148 | + ? $this->default_form_sections->create($event) |
|
149 | + : []; |
|
150 | + } |
|
151 | 151 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | ); |
98 | 98 | } |
99 | 99 | $event = $this->event_model->get_one_by_ID($EVT_ID); |
100 | - if (! $event instanceof EE_Event) { |
|
100 | + if ( ! $event instanceof EE_Event) { |
|
101 | 101 | throw new InvalidEntityException($event, 'EE_Event'); |
102 | 102 | } |
103 | 103 | $new_event = isset($_REQUEST['action']) && $_REQUEST['action'] === 'create_new'; |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | { |
145 | 145 | $reg_form_UUID = $event->registrationFormUuid(); |
146 | 146 | // if it's a new event and defaults have not been created yet, OR if there is no reg form at all... |
147 | - return ($new_event && ! $reg_form_UUID) || ! $reg_form_UUID |
|
147 | + return ($new_event && ! $reg_form_UUID) || ! $reg_form_UUID |
|
148 | 148 | ? $this->default_form_sections->create($event) |
149 | 149 | : []; |
150 | 150 | } |
@@ -21,56 +21,56 @@ |
||
21 | 21 | class DefaultDatetimes implements DefaultEntityGeneratorInterface |
22 | 22 | { |
23 | 23 | |
24 | - /** |
|
25 | - * @var DefaultTickets $default_tickets |
|
26 | - */ |
|
27 | - protected $default_tickets; |
|
24 | + /** |
|
25 | + * @var DefaultTickets $default_tickets |
|
26 | + */ |
|
27 | + protected $default_tickets; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var EEM_Datetime $datetime_model |
|
31 | - */ |
|
32 | - protected $datetime_model; |
|
29 | + /** |
|
30 | + * @var EEM_Datetime $datetime_model |
|
31 | + */ |
|
32 | + protected $datetime_model; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @param DefaultTickets $default_tickets |
|
36 | - * @param EEM_Datetime $datetime_model |
|
37 | - */ |
|
38 | - public function __construct(DefaultTickets $default_tickets, EEM_Datetime $datetime_model) |
|
39 | - { |
|
40 | - $this->default_tickets = $default_tickets; |
|
41 | - $this->datetime_model = $datetime_model; |
|
42 | - } |
|
34 | + /** |
|
35 | + * @param DefaultTickets $default_tickets |
|
36 | + * @param EEM_Datetime $datetime_model |
|
37 | + */ |
|
38 | + public function __construct(DefaultTickets $default_tickets, EEM_Datetime $datetime_model) |
|
39 | + { |
|
40 | + $this->default_tickets = $default_tickets; |
|
41 | + $this->datetime_model = $datetime_model; |
|
42 | + } |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * @param EE_Event|EE_Base_Class $entity |
|
47 | - * @return EE_Datetime[] |
|
48 | - * @throws EE_Error |
|
49 | - * @throws InvalidEntityException |
|
50 | - * @throws ReflectionException |
|
51 | - * @since $VID:$ |
|
52 | - */ |
|
53 | - public function create(EE_Base_Class $entity): array |
|
54 | - { |
|
55 | - if (! $entity instanceof EE_Event) { |
|
56 | - throw new InvalidEntityException($entity, 'EE_Event'); |
|
57 | - } |
|
58 | - $default_dates = []; |
|
59 | - $blank_dates = $this->datetime_model->create_new_blank_datetime(); |
|
60 | - if (is_array($blank_dates)) { |
|
61 | - foreach ($blank_dates as $blank_date) { |
|
62 | - if (! $blank_date instanceof EE_Datetime) { |
|
63 | - throw new InvalidEntityException($blank_date, 'EE_Datetime'); |
|
64 | - } |
|
65 | - // clone date, strip out ID, then save to get a new ID |
|
66 | - $default_date = clone $blank_date; |
|
67 | - $default_date->set('DTT_ID', null); |
|
68 | - $default_date->save(); |
|
69 | - $default_date->_add_relation_to($entity->ID(), 'Event'); |
|
70 | - $this->default_tickets->create($default_date); |
|
71 | - $default_dates[ $default_date->ID() ] = $default_date; |
|
72 | - } |
|
73 | - } |
|
74 | - return $default_dates; |
|
75 | - } |
|
45 | + /** |
|
46 | + * @param EE_Event|EE_Base_Class $entity |
|
47 | + * @return EE_Datetime[] |
|
48 | + * @throws EE_Error |
|
49 | + * @throws InvalidEntityException |
|
50 | + * @throws ReflectionException |
|
51 | + * @since $VID:$ |
|
52 | + */ |
|
53 | + public function create(EE_Base_Class $entity): array |
|
54 | + { |
|
55 | + if (! $entity instanceof EE_Event) { |
|
56 | + throw new InvalidEntityException($entity, 'EE_Event'); |
|
57 | + } |
|
58 | + $default_dates = []; |
|
59 | + $blank_dates = $this->datetime_model->create_new_blank_datetime(); |
|
60 | + if (is_array($blank_dates)) { |
|
61 | + foreach ($blank_dates as $blank_date) { |
|
62 | + if (! $blank_date instanceof EE_Datetime) { |
|
63 | + throw new InvalidEntityException($blank_date, 'EE_Datetime'); |
|
64 | + } |
|
65 | + // clone date, strip out ID, then save to get a new ID |
|
66 | + $default_date = clone $blank_date; |
|
67 | + $default_date->set('DTT_ID', null); |
|
68 | + $default_date->save(); |
|
69 | + $default_date->_add_relation_to($entity->ID(), 'Event'); |
|
70 | + $this->default_tickets->create($default_date); |
|
71 | + $default_dates[ $default_date->ID() ] = $default_date; |
|
72 | + } |
|
73 | + } |
|
74 | + return $default_dates; |
|
75 | + } |
|
76 | 76 | } |
@@ -52,14 +52,14 @@ discard block |
||
52 | 52 | */ |
53 | 53 | public function create(EE_Base_Class $entity): array |
54 | 54 | { |
55 | - if (! $entity instanceof EE_Event) { |
|
55 | + if ( ! $entity instanceof EE_Event) { |
|
56 | 56 | throw new InvalidEntityException($entity, 'EE_Event'); |
57 | 57 | } |
58 | 58 | $default_dates = []; |
59 | 59 | $blank_dates = $this->datetime_model->create_new_blank_datetime(); |
60 | 60 | if (is_array($blank_dates)) { |
61 | 61 | foreach ($blank_dates as $blank_date) { |
62 | - if (! $blank_date instanceof EE_Datetime) { |
|
62 | + if ( ! $blank_date instanceof EE_Datetime) { |
|
63 | 63 | throw new InvalidEntityException($blank_date, 'EE_Datetime'); |
64 | 64 | } |
65 | 65 | // clone date, strip out ID, then save to get a new ID |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | $default_date->save(); |
69 | 69 | $default_date->_add_relation_to($entity->ID(), 'Event'); |
70 | 70 | $this->default_tickets->create($default_date); |
71 | - $default_dates[ $default_date->ID() ] = $default_date; |
|
71 | + $default_dates[$default_date->ID()] = $default_date; |
|
72 | 72 | } |
73 | 73 | } |
74 | 74 | return $default_dates; |
@@ -25,97 +25,97 @@ |
||
25 | 25 | class DefaultFormSections implements DefaultEntityGeneratorInterface |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * @var EEM_Form_Element $form_element_model |
|
30 | - */ |
|
31 | - protected $form_element_model; |
|
28 | + /** |
|
29 | + * @var EEM_Form_Element $form_element_model |
|
30 | + */ |
|
31 | + protected $form_element_model; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @var EEM_Form_Section $form_section_model |
|
35 | - */ |
|
36 | - protected $form_section_model; |
|
33 | + /** |
|
34 | + * @var EEM_Form_Section $form_section_model |
|
35 | + */ |
|
36 | + protected $form_section_model; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param EEM_Form_Element $form_element_model |
|
40 | - * @param EEM_Form_Section $form_section_model |
|
41 | - */ |
|
42 | - public function __construct(EEM_Form_Element $form_element_model, EEM_Form_Section $form_section_model) |
|
43 | - { |
|
44 | - $this->form_element_model = $form_element_model; |
|
45 | - $this->form_section_model = $form_section_model; |
|
46 | - } |
|
38 | + /** |
|
39 | + * @param EEM_Form_Element $form_element_model |
|
40 | + * @param EEM_Form_Section $form_section_model |
|
41 | + */ |
|
42 | + public function __construct(EEM_Form_Element $form_element_model, EEM_Form_Section $form_section_model) |
|
43 | + { |
|
44 | + $this->form_element_model = $form_element_model; |
|
45 | + $this->form_section_model = $form_section_model; |
|
46 | + } |
|
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * @param EE_Event|EE_Base_Class $entity |
|
51 | - * @return EE_Form_Section[] |
|
52 | - * @throws EE_Error |
|
53 | - * @throws InvalidEntityException |
|
54 | - * @throws ReflectionException |
|
55 | - */ |
|
56 | - public function create(EE_Base_Class $entity): array |
|
57 | - { |
|
58 | - if (! $entity instanceof EE_Event) { |
|
59 | - throw new InvalidEntityException($entity, 'EE_Event'); |
|
60 | - } |
|
61 | - /** @var EE_Form_Section[] $new_form_sections */ |
|
62 | - $new_form_sections = []; |
|
63 | - $default_form_sections = $this->form_section_model->getDefaultFormSections(); |
|
64 | - if (is_array($default_form_sections)) { |
|
65 | - foreach ($default_form_sections as $default_form_section) { |
|
66 | - if (! $default_form_section instanceof EE_Form_Section) { |
|
67 | - throw new InvalidEntityException($default_form_section, 'EE_Form_Section'); |
|
68 | - } |
|
69 | - // we're calling this inside the loop, because this might get set the first time around |
|
70 | - // and it would always be blank for a new event if we were to call it before the loop, |
|
71 | - // and then we couldn't set the "BelongsTo" for any child form sections |
|
72 | - $top_level_form_section = $entity->registrationFormUuid(); |
|
73 | - $default_form_elements = $default_form_section->formElements(); |
|
74 | - // clone form_section, generate a new UUID, reset the status, then save it |
|
75 | - $new_form_section = clone $default_form_section; |
|
76 | - $UUID = Cuid::cuid(); |
|
77 | - $new_form_section->setUUID($UUID); |
|
78 | - $new_form_section->setStatus(FormStatus::ACTIVE); |
|
79 | - if ($top_level_form_section) { |
|
80 | - $new_form_section->setBelongsTo($top_level_form_section); |
|
81 | - } |
|
82 | - $new_form_section->save(); |
|
83 | - $new_form_sections[ $UUID ] = $new_form_section; |
|
84 | - // now retrieve, clone, and save all of the form elements |
|
85 | - $this->createDefaultFormElements($new_form_section, $default_form_elements); |
|
86 | - // save form section UUID on event if it is the top-level form section |
|
87 | - if ($new_form_section->isTopLevelFormSection()) { |
|
88 | - $entity->setRegistrationFormUuid($UUID); |
|
89 | - $entity->save(); |
|
90 | - } |
|
91 | - } |
|
92 | - } |
|
93 | - return $new_form_sections; |
|
94 | - } |
|
49 | + /** |
|
50 | + * @param EE_Event|EE_Base_Class $entity |
|
51 | + * @return EE_Form_Section[] |
|
52 | + * @throws EE_Error |
|
53 | + * @throws InvalidEntityException |
|
54 | + * @throws ReflectionException |
|
55 | + */ |
|
56 | + public function create(EE_Base_Class $entity): array |
|
57 | + { |
|
58 | + if (! $entity instanceof EE_Event) { |
|
59 | + throw new InvalidEntityException($entity, 'EE_Event'); |
|
60 | + } |
|
61 | + /** @var EE_Form_Section[] $new_form_sections */ |
|
62 | + $new_form_sections = []; |
|
63 | + $default_form_sections = $this->form_section_model->getDefaultFormSections(); |
|
64 | + if (is_array($default_form_sections)) { |
|
65 | + foreach ($default_form_sections as $default_form_section) { |
|
66 | + if (! $default_form_section instanceof EE_Form_Section) { |
|
67 | + throw new InvalidEntityException($default_form_section, 'EE_Form_Section'); |
|
68 | + } |
|
69 | + // we're calling this inside the loop, because this might get set the first time around |
|
70 | + // and it would always be blank for a new event if we were to call it before the loop, |
|
71 | + // and then we couldn't set the "BelongsTo" for any child form sections |
|
72 | + $top_level_form_section = $entity->registrationFormUuid(); |
|
73 | + $default_form_elements = $default_form_section->formElements(); |
|
74 | + // clone form_section, generate a new UUID, reset the status, then save it |
|
75 | + $new_form_section = clone $default_form_section; |
|
76 | + $UUID = Cuid::cuid(); |
|
77 | + $new_form_section->setUUID($UUID); |
|
78 | + $new_form_section->setStatus(FormStatus::ACTIVE); |
|
79 | + if ($top_level_form_section) { |
|
80 | + $new_form_section->setBelongsTo($top_level_form_section); |
|
81 | + } |
|
82 | + $new_form_section->save(); |
|
83 | + $new_form_sections[ $UUID ] = $new_form_section; |
|
84 | + // now retrieve, clone, and save all of the form elements |
|
85 | + $this->createDefaultFormElements($new_form_section, $default_form_elements); |
|
86 | + // save form section UUID on event if it is the top-level form section |
|
87 | + if ($new_form_section->isTopLevelFormSection()) { |
|
88 | + $entity->setRegistrationFormUuid($UUID); |
|
89 | + $entity->save(); |
|
90 | + } |
|
91 | + } |
|
92 | + } |
|
93 | + return $new_form_sections; |
|
94 | + } |
|
95 | 95 | |
96 | 96 | |
97 | - /** |
|
98 | - * @throws EE_Error |
|
99 | - * @throws ReflectionException |
|
100 | - */ |
|
101 | - private function createDefaultFormElements(EE_Form_Section $new_form_section, array $default_form_elements) |
|
102 | - { |
|
103 | - /** @var EE_Form_Element[] $new_form_sections */ |
|
104 | - $new_form_elements = []; |
|
105 | - foreach ($default_form_elements as $default_form_element) { |
|
106 | - if (! $default_form_element instanceof EE_Form_Element) { |
|
107 | - throw new InvalidEntityException($default_form_element, 'EE_Form_Element'); |
|
108 | - } |
|
109 | - // clone form_element, generate a new UUID, reset the status, then save it |
|
110 | - $new_form_element = clone $default_form_element; |
|
111 | - // generate a new UUID for this form section then save it |
|
112 | - $UUID = Cuid::cuid(); |
|
113 | - $new_form_element->setUUID($UUID); |
|
114 | - $new_form_element->setBelongsTo($new_form_section->UUID()); |
|
115 | - $new_form_element->setStatus(FormStatus::ACTIVE); |
|
116 | - $new_form_element->save(); |
|
117 | - $new_form_elements[ $UUID ] = $new_form_element; |
|
118 | - } |
|
119 | - $new_form_section->setFormElements($new_form_elements); |
|
120 | - } |
|
97 | + /** |
|
98 | + * @throws EE_Error |
|
99 | + * @throws ReflectionException |
|
100 | + */ |
|
101 | + private function createDefaultFormElements(EE_Form_Section $new_form_section, array $default_form_elements) |
|
102 | + { |
|
103 | + /** @var EE_Form_Element[] $new_form_sections */ |
|
104 | + $new_form_elements = []; |
|
105 | + foreach ($default_form_elements as $default_form_element) { |
|
106 | + if (! $default_form_element instanceof EE_Form_Element) { |
|
107 | + throw new InvalidEntityException($default_form_element, 'EE_Form_Element'); |
|
108 | + } |
|
109 | + // clone form_element, generate a new UUID, reset the status, then save it |
|
110 | + $new_form_element = clone $default_form_element; |
|
111 | + // generate a new UUID for this form section then save it |
|
112 | + $UUID = Cuid::cuid(); |
|
113 | + $new_form_element->setUUID($UUID); |
|
114 | + $new_form_element->setBelongsTo($new_form_section->UUID()); |
|
115 | + $new_form_element->setStatus(FormStatus::ACTIVE); |
|
116 | + $new_form_element->save(); |
|
117 | + $new_form_elements[ $UUID ] = $new_form_element; |
|
118 | + } |
|
119 | + $new_form_section->setFormElements($new_form_elements); |
|
120 | + } |
|
121 | 121 | } |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | */ |
56 | 56 | public function create(EE_Base_Class $entity): array |
57 | 57 | { |
58 | - if (! $entity instanceof EE_Event) { |
|
58 | + if ( ! $entity instanceof EE_Event) { |
|
59 | 59 | throw new InvalidEntityException($entity, 'EE_Event'); |
60 | 60 | } |
61 | 61 | /** @var EE_Form_Section[] $new_form_sections */ |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | $default_form_sections = $this->form_section_model->getDefaultFormSections(); |
64 | 64 | if (is_array($default_form_sections)) { |
65 | 65 | foreach ($default_form_sections as $default_form_section) { |
66 | - if (! $default_form_section instanceof EE_Form_Section) { |
|
66 | + if ( ! $default_form_section instanceof EE_Form_Section) { |
|
67 | 67 | throw new InvalidEntityException($default_form_section, 'EE_Form_Section'); |
68 | 68 | } |
69 | 69 | // we're calling this inside the loop, because this might get set the first time around |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | $new_form_section->setBelongsTo($top_level_form_section); |
81 | 81 | } |
82 | 82 | $new_form_section->save(); |
83 | - $new_form_sections[ $UUID ] = $new_form_section; |
|
83 | + $new_form_sections[$UUID] = $new_form_section; |
|
84 | 84 | // now retrieve, clone, and save all of the form elements |
85 | 85 | $this->createDefaultFormElements($new_form_section, $default_form_elements); |
86 | 86 | // save form section UUID on event if it is the top-level form section |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | /** @var EE_Form_Element[] $new_form_sections */ |
104 | 104 | $new_form_elements = []; |
105 | 105 | foreach ($default_form_elements as $default_form_element) { |
106 | - if (! $default_form_element instanceof EE_Form_Element) { |
|
106 | + if ( ! $default_form_element instanceof EE_Form_Element) { |
|
107 | 107 | throw new InvalidEntityException($default_form_element, 'EE_Form_Element'); |
108 | 108 | } |
109 | 109 | // clone form_element, generate a new UUID, reset the status, then save it |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | $new_form_element->setBelongsTo($new_form_section->UUID()); |
115 | 115 | $new_form_element->setStatus(FormStatus::ACTIVE); |
116 | 116 | $new_form_element->save(); |
117 | - $new_form_elements[ $UUID ] = $new_form_element; |
|
117 | + $new_form_elements[$UUID] = $new_form_element; |
|
118 | 118 | } |
119 | 119 | $new_form_section->setFormElements($new_form_elements); |
120 | 120 | } |
@@ -17,12 +17,12 @@ |
||
17 | 17 | interface DefaultEntityGeneratorInterface |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @param EE_Base_Class $entity |
|
22 | - * @return EE_Base_Class[] |
|
23 | - * @throws EE_Error |
|
24 | - * @throws ReflectionException |
|
25 | - * @since $VID:$ |
|
26 | - */ |
|
27 | - public function create(EE_Base_Class $entity): array; |
|
20 | + /** |
|
21 | + * @param EE_Base_Class $entity |
|
22 | + * @return EE_Base_Class[] |
|
23 | + * @throws EE_Error |
|
24 | + * @throws ReflectionException |
|
25 | + * @since $VID:$ |
|
26 | + */ |
|
27 | + public function create(EE_Base_Class $entity): array; |
|
28 | 28 | } |
@@ -17,41 +17,41 @@ |
||
17 | 17 | { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * @type Psr4Autoloader |
|
22 | - */ |
|
23 | - protected static $psr4_loader; |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * @return void |
|
28 | - * @throws EE_Error |
|
29 | - */ |
|
30 | - public function initializeAutoloader() |
|
31 | - { |
|
32 | - static $initialized = false; |
|
33 | - if (! $initialized) { |
|
34 | - // instantiate PSR4 autoloader |
|
35 | - espresso_load_required('Psr4Autoloader', EE_CORE . 'Psr4Autoloader.php'); |
|
36 | - EE_Psr4AutoloaderInit::$psr4_loader = new Psr4Autoloader(); |
|
37 | - // register the autoloader |
|
38 | - EE_Psr4AutoloaderInit::$psr4_loader->register(); |
|
39 | - // register the base directories for the namespace prefix |
|
40 | - EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspresso', EE_PLUGIN_DIR_PATH); |
|
41 | - EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspressoBatchRequest', EE_LIBRARIES . 'batch'); |
|
42 | - EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspressoVendor', EE_THIRD_PARTY); |
|
43 | - EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EETests', EE_PLUGIN_DIR_PATH . 'tests'); |
|
44 | - EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EndyJasmi', EE_THIRD_PARTY . 'cuid'); |
|
45 | - $initialized = true; |
|
46 | - } |
|
47 | - } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * @return Psr4Autoloader |
|
52 | - */ |
|
53 | - public static function psr4_loader(): Psr4Autoloader |
|
54 | - { |
|
55 | - return self::$psr4_loader; |
|
56 | - } |
|
20 | + /** |
|
21 | + * @type Psr4Autoloader |
|
22 | + */ |
|
23 | + protected static $psr4_loader; |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * @return void |
|
28 | + * @throws EE_Error |
|
29 | + */ |
|
30 | + public function initializeAutoloader() |
|
31 | + { |
|
32 | + static $initialized = false; |
|
33 | + if (! $initialized) { |
|
34 | + // instantiate PSR4 autoloader |
|
35 | + espresso_load_required('Psr4Autoloader', EE_CORE . 'Psr4Autoloader.php'); |
|
36 | + EE_Psr4AutoloaderInit::$psr4_loader = new Psr4Autoloader(); |
|
37 | + // register the autoloader |
|
38 | + EE_Psr4AutoloaderInit::$psr4_loader->register(); |
|
39 | + // register the base directories for the namespace prefix |
|
40 | + EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspresso', EE_PLUGIN_DIR_PATH); |
|
41 | + EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspressoBatchRequest', EE_LIBRARIES . 'batch'); |
|
42 | + EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspressoVendor', EE_THIRD_PARTY); |
|
43 | + EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EETests', EE_PLUGIN_DIR_PATH . 'tests'); |
|
44 | + EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EndyJasmi', EE_THIRD_PARTY . 'cuid'); |
|
45 | + $initialized = true; |
|
46 | + } |
|
47 | + } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * @return Psr4Autoloader |
|
52 | + */ |
|
53 | + public static function psr4_loader(): Psr4Autoloader |
|
54 | + { |
|
55 | + return self::$psr4_loader; |
|
56 | + } |
|
57 | 57 | } |
@@ -30,18 +30,18 @@ |
||
30 | 30 | public function initializeAutoloader() |
31 | 31 | { |
32 | 32 | static $initialized = false; |
33 | - if (! $initialized) { |
|
33 | + if ( ! $initialized) { |
|
34 | 34 | // instantiate PSR4 autoloader |
35 | - espresso_load_required('Psr4Autoloader', EE_CORE . 'Psr4Autoloader.php'); |
|
35 | + espresso_load_required('Psr4Autoloader', EE_CORE.'Psr4Autoloader.php'); |
|
36 | 36 | EE_Psr4AutoloaderInit::$psr4_loader = new Psr4Autoloader(); |
37 | 37 | // register the autoloader |
38 | 38 | EE_Psr4AutoloaderInit::$psr4_loader->register(); |
39 | 39 | // register the base directories for the namespace prefix |
40 | 40 | EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspresso', EE_PLUGIN_DIR_PATH); |
41 | - EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspressoBatchRequest', EE_LIBRARIES . 'batch'); |
|
41 | + EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspressoBatchRequest', EE_LIBRARIES.'batch'); |
|
42 | 42 | EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EventEspressoVendor', EE_THIRD_PARTY); |
43 | - EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EETests', EE_PLUGIN_DIR_PATH . 'tests'); |
|
44 | - EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EndyJasmi', EE_THIRD_PARTY . 'cuid'); |
|
43 | + EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EETests', EE_PLUGIN_DIR_PATH.'tests'); |
|
44 | + EE_Psr4AutoloaderInit::$psr4_loader->addNamespace('EndyJasmi', EE_THIRD_PARTY.'cuid'); |
|
45 | 45 | $initialized = true; |
46 | 46 | } |
47 | 47 | } |
@@ -11,194 +11,194 @@ |
||
11 | 11 | */ |
12 | 12 | class EE_Register_Model implements EEI_Plugin_API |
13 | 13 | { |
14 | - /** |
|
15 | - * |
|
16 | - * @var array keys are the model_id used to register with, values are the array provided to register them, exactly |
|
17 | - * like EE_Register_Model::register()'s 2nd arg |
|
18 | - */ |
|
19 | - protected static $_model_registry; |
|
14 | + /** |
|
15 | + * |
|
16 | + * @var array keys are the model_id used to register with, values are the array provided to register them, exactly |
|
17 | + * like EE_Register_Model::register()'s 2nd arg |
|
18 | + */ |
|
19 | + protected static $_model_registry; |
|
20 | 20 | |
21 | - /** |
|
22 | - * |
|
23 | - * @var array keys are model names, values are their class names. Stored on registration and used |
|
24 | - * on a hook |
|
25 | - */ |
|
26 | - protected static $_model_name_to_classname_map; |
|
21 | + /** |
|
22 | + * |
|
23 | + * @var array keys are model names, values are their class names. Stored on registration and used |
|
24 | + * on a hook |
|
25 | + */ |
|
26 | + protected static $_model_name_to_classname_map; |
|
27 | 27 | |
28 | 28 | |
29 | - /** |
|
30 | - * @param string $addon_name unique id for it |
|
31 | - * @param array $setup_args { |
|
32 | - * @type array $model_paths array of folders containing DB models, where each file follows the models naming |
|
33 | - * convention, which is: EEM_{model_name}.model.php which contains a single class called |
|
34 | - * EEM_{model_name}. Eg. you could pass |
|
35 | - * "public_html/wp-content/plugins/my_addon/db_models" (with or without trailing slash) |
|
36 | - * and in that folder put each of your model files, like "EEM_Food.model.php" which |
|
37 | - * contains the class "EEM_Food" and |
|
38 | - * "EEM_Monkey.model.php" which contains the class "EEM_Monkey". These will be |
|
39 | - * autoloaded and added to the EE registry so they can be used like ordinary models. The |
|
40 | - * class contained in each file should extend EEM_Base. |
|
41 | - * @type array $class_paths array of folders containing DB classes, where each file follows the model class |
|
42 | - * naming convention, which is EE_{model_name}.class.php. The class contained in each |
|
43 | - * file should extend EE_Base_Class |
|
44 | - * |
|
45 | - * } |
|
46 | - * @return bool |
|
47 | - * @throws EE_Error |
|
48 | - */ |
|
49 | - public static function register(string $addon_name = '', array $setup_args = []): bool |
|
50 | - { |
|
51 | - // required fields MUST be present, so let's make sure they are. |
|
52 | - if (empty($addon_name) || ! is_array($setup_args) || empty($setup_args['model_paths'])) { |
|
53 | - throw new EE_Error( |
|
54 | - __( |
|
55 | - 'In order to register Models with EE_Register_Model::register(), you must include a "model_id" (a unique identifier for this set of models), and an array containing the following keys: "model_paths" (an array of full server paths to folders that contain models)', |
|
56 | - 'event_espresso' |
|
57 | - ) |
|
58 | - ); |
|
59 | - } |
|
29 | + /** |
|
30 | + * @param string $addon_name unique id for it |
|
31 | + * @param array $setup_args { |
|
32 | + * @type array $model_paths array of folders containing DB models, where each file follows the models naming |
|
33 | + * convention, which is: EEM_{model_name}.model.php which contains a single class called |
|
34 | + * EEM_{model_name}. Eg. you could pass |
|
35 | + * "public_html/wp-content/plugins/my_addon/db_models" (with or without trailing slash) |
|
36 | + * and in that folder put each of your model files, like "EEM_Food.model.php" which |
|
37 | + * contains the class "EEM_Food" and |
|
38 | + * "EEM_Monkey.model.php" which contains the class "EEM_Monkey". These will be |
|
39 | + * autoloaded and added to the EE registry so they can be used like ordinary models. The |
|
40 | + * class contained in each file should extend EEM_Base. |
|
41 | + * @type array $class_paths array of folders containing DB classes, where each file follows the model class |
|
42 | + * naming convention, which is EE_{model_name}.class.php. The class contained in each |
|
43 | + * file should extend EE_Base_Class |
|
44 | + * |
|
45 | + * } |
|
46 | + * @return bool |
|
47 | + * @throws EE_Error |
|
48 | + */ |
|
49 | + public static function register(string $addon_name = '', array $setup_args = []): bool |
|
50 | + { |
|
51 | + // required fields MUST be present, so let's make sure they are. |
|
52 | + if (empty($addon_name) || ! is_array($setup_args) || empty($setup_args['model_paths'])) { |
|
53 | + throw new EE_Error( |
|
54 | + __( |
|
55 | + 'In order to register Models with EE_Register_Model::register(), you must include a "model_id" (a unique identifier for this set of models), and an array containing the following keys: "model_paths" (an array of full server paths to folders that contain models)', |
|
56 | + 'event_espresso' |
|
57 | + ) |
|
58 | + ); |
|
59 | + } |
|
60 | 60 | |
61 | - // make sure we don't register twice |
|
62 | - if (isset(self::$_model_registry[ $addon_name ])) { |
|
63 | - return true; |
|
64 | - } |
|
61 | + // make sure we don't register twice |
|
62 | + if (isset(self::$_model_registry[ $addon_name ])) { |
|
63 | + return true; |
|
64 | + } |
|
65 | 65 | |
66 | - if ( |
|
67 | - ! did_action('AHEE__EE_System__load_espresso_addons') |
|
68 | - || did_action('FHEE__EE_System__parse_model_names') |
|
69 | - || did_action('FHEE__EE_System__parse_implemented_model_names') |
|
70 | - ) { |
|
71 | - EE_Error::doing_it_wrong( |
|
72 | - __METHOD__, |
|
73 | - sprintf( |
|
74 | - __( |
|
75 | - 'An attempt was made to register "%s" as a group models has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_espresso_addons" hook to register models.', |
|
76 | - 'event_espresso' |
|
77 | - ), |
|
78 | - $addon_name |
|
79 | - ), |
|
80 | - '4.5' |
|
81 | - ); |
|
82 | - } |
|
83 | - self::$_model_registry[ $addon_name ] = $setup_args; |
|
66 | + if ( |
|
67 | + ! did_action('AHEE__EE_System__load_espresso_addons') |
|
68 | + || did_action('FHEE__EE_System__parse_model_names') |
|
69 | + || did_action('FHEE__EE_System__parse_implemented_model_names') |
|
70 | + ) { |
|
71 | + EE_Error::doing_it_wrong( |
|
72 | + __METHOD__, |
|
73 | + sprintf( |
|
74 | + __( |
|
75 | + 'An attempt was made to register "%s" as a group models has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_espresso_addons" hook to register models.', |
|
76 | + 'event_espresso' |
|
77 | + ), |
|
78 | + $addon_name |
|
79 | + ), |
|
80 | + '4.5' |
|
81 | + ); |
|
82 | + } |
|
83 | + self::$_model_registry[ $addon_name ] = $setup_args; |
|
84 | 84 | |
85 | - if ( |
|
86 | - ( |
|
87 | - isset($setup_args['model_paths']) && ! isset($setup_args['class_paths']) |
|
88 | - ) |
|
89 | - || (! isset($setup_args['model_paths']) && isset($setup_args['class_paths'])) |
|
90 | - ) { |
|
91 | - throw new EE_Error( |
|
92 | - sprintf( |
|
93 | - __( |
|
94 | - 'You must register both "model_paths" AND "class_paths", not just one or the other You provided %s', |
|
95 | - 'event_espresso' |
|
96 | - ), |
|
97 | - implode(", ", array_keys($setup_args)) |
|
98 | - ) |
|
99 | - ); |
|
100 | - } |
|
101 | - if (isset($setup_args['model_paths'])) { |
|
102 | - // make sure they passed in an array |
|
103 | - if (! is_array($setup_args['model_paths'])) { |
|
104 | - $setup_args['model_paths'] = [$setup_args['model_paths']]; |
|
105 | - } |
|
106 | - // we want to add this as a model folder |
|
107 | - // and autoload them all |
|
108 | - $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['model_paths']); |
|
109 | - EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
110 | - $model_name_to_classname_map = []; |
|
111 | - foreach (array_keys($class_to_filepath_map) as $classname) { |
|
112 | - $model_name_to_classname_map[ str_replace("EEM_", "", $classname) ] = $classname; |
|
113 | - } |
|
114 | - self::$_model_name_to_classname_map[ $addon_name ] = $model_name_to_classname_map; |
|
115 | - add_filter('FHEE__EE_System__parse_model_names', ['EE_Register_Model', 'add_addon_models']); |
|
116 | - add_filter( |
|
117 | - 'FHEE__EE_System__parse_implemented_model_names', |
|
118 | - ['EE_Register_Model', 'add_addon_models'] |
|
119 | - ); |
|
120 | - add_filter('FHEE__EE_Registry__load_model__paths', ['EE_Register_Model', 'add_model_folders']); |
|
121 | - unset($setup_args['model_paths']); |
|
122 | - } |
|
123 | - if (isset($setup_args['class_paths'])) { |
|
124 | - // make sure they passed in an array |
|
125 | - if (! is_array($setup_args['class_paths'])) { |
|
126 | - $setup_args['class_paths'] = [$setup_args['class_paths']]; |
|
127 | - } |
|
128 | - $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['class_paths']); |
|
129 | - EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
130 | - add_filter('FHEE__EE_Registry__load_class__paths', ['EE_Register_Model', 'add_class_folders']); |
|
131 | - unset($setup_args['class_paths']); |
|
132 | - } |
|
133 | - foreach ($setup_args as $unknown_key => $unknown_config) { |
|
134 | - self::deregister($addon_name); |
|
135 | - throw new EE_Error( |
|
136 | - sprintf(__("The key '%s' is not a known key for registering a model", "event_espresso"), $unknown_key) |
|
137 | - ); |
|
138 | - } |
|
139 | - return true; |
|
140 | - } |
|
85 | + if ( |
|
86 | + ( |
|
87 | + isset($setup_args['model_paths']) && ! isset($setup_args['class_paths']) |
|
88 | + ) |
|
89 | + || (! isset($setup_args['model_paths']) && isset($setup_args['class_paths'])) |
|
90 | + ) { |
|
91 | + throw new EE_Error( |
|
92 | + sprintf( |
|
93 | + __( |
|
94 | + 'You must register both "model_paths" AND "class_paths", not just one or the other You provided %s', |
|
95 | + 'event_espresso' |
|
96 | + ), |
|
97 | + implode(", ", array_keys($setup_args)) |
|
98 | + ) |
|
99 | + ); |
|
100 | + } |
|
101 | + if (isset($setup_args['model_paths'])) { |
|
102 | + // make sure they passed in an array |
|
103 | + if (! is_array($setup_args['model_paths'])) { |
|
104 | + $setup_args['model_paths'] = [$setup_args['model_paths']]; |
|
105 | + } |
|
106 | + // we want to add this as a model folder |
|
107 | + // and autoload them all |
|
108 | + $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['model_paths']); |
|
109 | + EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
110 | + $model_name_to_classname_map = []; |
|
111 | + foreach (array_keys($class_to_filepath_map) as $classname) { |
|
112 | + $model_name_to_classname_map[ str_replace("EEM_", "", $classname) ] = $classname; |
|
113 | + } |
|
114 | + self::$_model_name_to_classname_map[ $addon_name ] = $model_name_to_classname_map; |
|
115 | + add_filter('FHEE__EE_System__parse_model_names', ['EE_Register_Model', 'add_addon_models']); |
|
116 | + add_filter( |
|
117 | + 'FHEE__EE_System__parse_implemented_model_names', |
|
118 | + ['EE_Register_Model', 'add_addon_models'] |
|
119 | + ); |
|
120 | + add_filter('FHEE__EE_Registry__load_model__paths', ['EE_Register_Model', 'add_model_folders']); |
|
121 | + unset($setup_args['model_paths']); |
|
122 | + } |
|
123 | + if (isset($setup_args['class_paths'])) { |
|
124 | + // make sure they passed in an array |
|
125 | + if (! is_array($setup_args['class_paths'])) { |
|
126 | + $setup_args['class_paths'] = [$setup_args['class_paths']]; |
|
127 | + } |
|
128 | + $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['class_paths']); |
|
129 | + EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
130 | + add_filter('FHEE__EE_Registry__load_class__paths', ['EE_Register_Model', 'add_class_folders']); |
|
131 | + unset($setup_args['class_paths']); |
|
132 | + } |
|
133 | + foreach ($setup_args as $unknown_key => $unknown_config) { |
|
134 | + self::deregister($addon_name); |
|
135 | + throw new EE_Error( |
|
136 | + sprintf(__("The key '%s' is not a known key for registering a model", "event_espresso"), $unknown_key) |
|
137 | + ); |
|
138 | + } |
|
139 | + return true; |
|
140 | + } |
|
141 | 141 | |
142 | 142 | |
143 | - /** |
|
144 | - * Filters the core list of models |
|
145 | - * |
|
146 | - * @param array $core_models |
|
147 | - * @return array keys are model names (eg 'Event') and values are their classes (eg 'EE_Event') |
|
148 | - */ |
|
149 | - public static function add_addon_models(array $core_models = []): array |
|
150 | - { |
|
151 | - $models = []; |
|
152 | - foreach (self::$_model_name_to_classname_map as $model_map) { |
|
153 | - $models[] = $model_map; |
|
154 | - } |
|
155 | - return array_merge($core_models, ...$models); |
|
156 | - } |
|
143 | + /** |
|
144 | + * Filters the core list of models |
|
145 | + * |
|
146 | + * @param array $core_models |
|
147 | + * @return array keys are model names (eg 'Event') and values are their classes (eg 'EE_Event') |
|
148 | + */ |
|
149 | + public static function add_addon_models(array $core_models = []): array |
|
150 | + { |
|
151 | + $models = []; |
|
152 | + foreach (self::$_model_name_to_classname_map as $model_map) { |
|
153 | + $models[] = $model_map; |
|
154 | + } |
|
155 | + return array_merge($core_models, ...$models); |
|
156 | + } |
|
157 | 157 | |
158 | 158 | |
159 | - /** |
|
160 | - * Filters the list of model folders |
|
161 | - * |
|
162 | - * @param array $folders |
|
163 | - * @return array of folder paths |
|
164 | - */ |
|
165 | - public static function add_model_folders(array $folders = []): array |
|
166 | - { |
|
167 | - $model_folders = []; |
|
168 | - foreach (self::$_model_registry as $setup_args) { |
|
169 | - if (isset($setup_args['model_paths'])) { |
|
170 | - $model_folders[] = (array) $setup_args['model_paths']; |
|
171 | - } |
|
172 | - } |
|
173 | - return array_merge($folders, ...$model_folders); |
|
174 | - } |
|
159 | + /** |
|
160 | + * Filters the list of model folders |
|
161 | + * |
|
162 | + * @param array $folders |
|
163 | + * @return array of folder paths |
|
164 | + */ |
|
165 | + public static function add_model_folders(array $folders = []): array |
|
166 | + { |
|
167 | + $model_folders = []; |
|
168 | + foreach (self::$_model_registry as $setup_args) { |
|
169 | + if (isset($setup_args['model_paths'])) { |
|
170 | + $model_folders[] = (array) $setup_args['model_paths']; |
|
171 | + } |
|
172 | + } |
|
173 | + return array_merge($folders, ...$model_folders); |
|
174 | + } |
|
175 | 175 | |
176 | 176 | |
177 | - /** |
|
178 | - * Filters the array of model class paths |
|
179 | - * |
|
180 | - * @param array $folders |
|
181 | - * @return array of folder paths |
|
182 | - */ |
|
183 | - public static function add_class_folders(array $folders = []): array |
|
184 | - { |
|
185 | - $class_folders = []; |
|
186 | - foreach (self::$_model_registry as $setup_args) { |
|
187 | - if (isset($setup_args['class_paths'])) { |
|
188 | - $class_folders[] = (array) $setup_args['class_paths']; |
|
189 | - } |
|
190 | - } |
|
191 | - return array_merge($folders, ...$class_folders); |
|
192 | - } |
|
177 | + /** |
|
178 | + * Filters the array of model class paths |
|
179 | + * |
|
180 | + * @param array $folders |
|
181 | + * @return array of folder paths |
|
182 | + */ |
|
183 | + public static function add_class_folders(array $folders = []): array |
|
184 | + { |
|
185 | + $class_folders = []; |
|
186 | + foreach (self::$_model_registry as $setup_args) { |
|
187 | + if (isset($setup_args['class_paths'])) { |
|
188 | + $class_folders[] = (array) $setup_args['class_paths']; |
|
189 | + } |
|
190 | + } |
|
191 | + return array_merge($folders, ...$class_folders); |
|
192 | + } |
|
193 | 193 | |
194 | 194 | |
195 | - /** |
|
196 | - * deregister |
|
197 | - * |
|
198 | - * @param string $addon_name |
|
199 | - */ |
|
200 | - public static function deregister(string $addon_name = '') |
|
201 | - { |
|
202 | - unset(self::$_model_registry[ $addon_name ], self::$_model_name_to_classname_map[ $addon_name ]); |
|
203 | - } |
|
195 | + /** |
|
196 | + * deregister |
|
197 | + * |
|
198 | + * @param string $addon_name |
|
199 | + */ |
|
200 | + public static function deregister(string $addon_name = '') |
|
201 | + { |
|
202 | + unset(self::$_model_registry[ $addon_name ], self::$_model_name_to_classname_map[ $addon_name ]); |
|
203 | + } |
|
204 | 204 | } |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | } |
60 | 60 | |
61 | 61 | // make sure we don't register twice |
62 | - if (isset(self::$_model_registry[ $addon_name ])) { |
|
62 | + if (isset(self::$_model_registry[$addon_name])) { |
|
63 | 63 | return true; |
64 | 64 | } |
65 | 65 | |
@@ -80,13 +80,13 @@ discard block |
||
80 | 80 | '4.5' |
81 | 81 | ); |
82 | 82 | } |
83 | - self::$_model_registry[ $addon_name ] = $setup_args; |
|
83 | + self::$_model_registry[$addon_name] = $setup_args; |
|
84 | 84 | |
85 | 85 | if ( |
86 | 86 | ( |
87 | 87 | isset($setup_args['model_paths']) && ! isset($setup_args['class_paths']) |
88 | 88 | ) |
89 | - || (! isset($setup_args['model_paths']) && isset($setup_args['class_paths'])) |
|
89 | + || ( ! isset($setup_args['model_paths']) && isset($setup_args['class_paths'])) |
|
90 | 90 | ) { |
91 | 91 | throw new EE_Error( |
92 | 92 | sprintf( |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | } |
101 | 101 | if (isset($setup_args['model_paths'])) { |
102 | 102 | // make sure they passed in an array |
103 | - if (! is_array($setup_args['model_paths'])) { |
|
103 | + if ( ! is_array($setup_args['model_paths'])) { |
|
104 | 104 | $setup_args['model_paths'] = [$setup_args['model_paths']]; |
105 | 105 | } |
106 | 106 | // we want to add this as a model folder |
@@ -109,9 +109,9 @@ discard block |
||
109 | 109 | EEH_Autoloader::register_autoloader($class_to_filepath_map); |
110 | 110 | $model_name_to_classname_map = []; |
111 | 111 | foreach (array_keys($class_to_filepath_map) as $classname) { |
112 | - $model_name_to_classname_map[ str_replace("EEM_", "", $classname) ] = $classname; |
|
112 | + $model_name_to_classname_map[str_replace("EEM_", "", $classname)] = $classname; |
|
113 | 113 | } |
114 | - self::$_model_name_to_classname_map[ $addon_name ] = $model_name_to_classname_map; |
|
114 | + self::$_model_name_to_classname_map[$addon_name] = $model_name_to_classname_map; |
|
115 | 115 | add_filter('FHEE__EE_System__parse_model_names', ['EE_Register_Model', 'add_addon_models']); |
116 | 116 | add_filter( |
117 | 117 | 'FHEE__EE_System__parse_implemented_model_names', |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | } |
123 | 123 | if (isset($setup_args['class_paths'])) { |
124 | 124 | // make sure they passed in an array |
125 | - if (! is_array($setup_args['class_paths'])) { |
|
125 | + if ( ! is_array($setup_args['class_paths'])) { |
|
126 | 126 | $setup_args['class_paths'] = [$setup_args['class_paths']]; |
127 | 127 | } |
128 | 128 | $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['class_paths']); |
@@ -199,6 +199,6 @@ discard block |
||
199 | 199 | */ |
200 | 200 | public static function deregister(string $addon_name = '') |
201 | 201 | { |
202 | - unset(self::$_model_registry[ $addon_name ], self::$_model_name_to_classname_map[ $addon_name ]); |
|
202 | + unset(self::$_model_registry[$addon_name], self::$_model_name_to_classname_map[$addon_name]); |
|
203 | 203 | } |
204 | 204 | } |
@@ -20,163 +20,163 @@ |
||
20 | 20 | class EE_Register_Shortcode implements EEI_Plugin_API |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * Holds values for registered shortcodes |
|
25 | - * |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected static $_settings = []; |
|
23 | + /** |
|
24 | + * Holds values for registered shortcodes |
|
25 | + * |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected static $_settings = []; |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * Method for registering new EE_Shortcodes |
|
33 | - * |
|
34 | - * @param string $addon_name a unique identifier for this set of modules Required. |
|
35 | - * @param array $setup_args an array of arguments provided for registering shortcodes Required. |
|
36 | - * @type array shortcode_paths an array of full server paths to folders containing any EES_Shortcodes |
|
37 | - * @type array shortcode_fqcns an array of fully qualified class names for any new shortcode classes to register. |
|
38 | - * Shortcode classes should extend EspressoShortcode |
|
39 | - * and be properly namespaced so they are autoloaded. |
|
40 | - * @return bool |
|
41 | - * @throws EE_Error |
|
42 | - * @since 4.3.0 |
|
43 | - * @since 4.9.46.rc.025 for the new `shortcode_fqcns` array argument. |
|
44 | - */ |
|
45 | - public static function register(string $addon_name = '', array $setup_args = []): bool |
|
46 | - { |
|
47 | - // required fields MUST be present, so let's make sure they are. |
|
48 | - if ( |
|
49 | - empty($addon_name) |
|
50 | - || ! is_array($setup_args) |
|
51 | - || ( |
|
52 | - empty($setup_args['shortcode_paths']) |
|
53 | - ) |
|
54 | - && empty($setup_args['shortcode_fqcns']) |
|
55 | - ) { |
|
56 | - throw new EE_Error( |
|
57 | - esc_html__( |
|
58 | - 'In order to register Modules with EE_Register_Shortcode::register(), you must include a "shortcode_id" (a unique identifier for this set of shortcodes), and an array containing the following keys: "shortcode_paths" (an array of full server paths to folders that contain shortcodes, or to the shortcode files themselves)', |
|
59 | - 'event_espresso' |
|
60 | - ) |
|
61 | - ); |
|
62 | - } |
|
31 | + /** |
|
32 | + * Method for registering new EE_Shortcodes |
|
33 | + * |
|
34 | + * @param string $addon_name a unique identifier for this set of modules Required. |
|
35 | + * @param array $setup_args an array of arguments provided for registering shortcodes Required. |
|
36 | + * @type array shortcode_paths an array of full server paths to folders containing any EES_Shortcodes |
|
37 | + * @type array shortcode_fqcns an array of fully qualified class names for any new shortcode classes to register. |
|
38 | + * Shortcode classes should extend EspressoShortcode |
|
39 | + * and be properly namespaced so they are autoloaded. |
|
40 | + * @return bool |
|
41 | + * @throws EE_Error |
|
42 | + * @since 4.3.0 |
|
43 | + * @since 4.9.46.rc.025 for the new `shortcode_fqcns` array argument. |
|
44 | + */ |
|
45 | + public static function register(string $addon_name = '', array $setup_args = []): bool |
|
46 | + { |
|
47 | + // required fields MUST be present, so let's make sure they are. |
|
48 | + if ( |
|
49 | + empty($addon_name) |
|
50 | + || ! is_array($setup_args) |
|
51 | + || ( |
|
52 | + empty($setup_args['shortcode_paths']) |
|
53 | + ) |
|
54 | + && empty($setup_args['shortcode_fqcns']) |
|
55 | + ) { |
|
56 | + throw new EE_Error( |
|
57 | + esc_html__( |
|
58 | + 'In order to register Modules with EE_Register_Shortcode::register(), you must include a "shortcode_id" (a unique identifier for this set of shortcodes), and an array containing the following keys: "shortcode_paths" (an array of full server paths to folders that contain shortcodes, or to the shortcode files themselves)', |
|
59 | + 'event_espresso' |
|
60 | + ) |
|
61 | + ); |
|
62 | + } |
|
63 | 63 | |
64 | - // make sure we don't register twice |
|
65 | - if (isset(self::$_settings[ $addon_name ])) { |
|
66 | - return true; |
|
67 | - } |
|
64 | + // make sure we don't register twice |
|
65 | + if (isset(self::$_settings[ $addon_name ])) { |
|
66 | + return true; |
|
67 | + } |
|
68 | 68 | |
69 | - // make sure this was called in the right place! |
|
70 | - if ( |
|
71 | - ! did_action('AHEE__EE_System__load_espresso_addons') |
|
72 | - || did_action('AHEE__EE_System__register_shortcodes_modules_and_widgets') |
|
73 | - ) { |
|
74 | - EE_Error::doing_it_wrong( |
|
75 | - __METHOD__, |
|
76 | - esc_html__( |
|
77 | - 'An attempt to register shortcodes has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__register_shortcodes_modules_and_widgets" hook to register shortcodes.', |
|
78 | - 'event_espresso' |
|
79 | - ), |
|
80 | - '4.3.0' |
|
81 | - ); |
|
82 | - } |
|
83 | - // setup $_settings array from incoming values. |
|
84 | - self::$_settings[ $addon_name ] = [ |
|
85 | - // array of full server paths to any EES_Shortcodes used by the shortcode |
|
86 | - 'shortcode_paths' => isset($setup_args['shortcode_paths']) |
|
87 | - ? (array) $setup_args['shortcode_paths'] |
|
88 | - : [], |
|
89 | - 'shortcode_fqcns' => isset($setup_args['shortcode_fqcns']) |
|
90 | - ? (array) $setup_args['shortcode_fqcns'] |
|
91 | - : [], |
|
92 | - ]; |
|
93 | - // add to list of shortcodes to be registered |
|
94 | - add_filter( |
|
95 | - 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', |
|
96 | - ['EE_Register_Shortcode', 'add_shortcodes'] |
|
97 | - ); |
|
69 | + // make sure this was called in the right place! |
|
70 | + if ( |
|
71 | + ! did_action('AHEE__EE_System__load_espresso_addons') |
|
72 | + || did_action('AHEE__EE_System__register_shortcodes_modules_and_widgets') |
|
73 | + ) { |
|
74 | + EE_Error::doing_it_wrong( |
|
75 | + __METHOD__, |
|
76 | + esc_html__( |
|
77 | + 'An attempt to register shortcodes has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__register_shortcodes_modules_and_widgets" hook to register shortcodes.', |
|
78 | + 'event_espresso' |
|
79 | + ), |
|
80 | + '4.3.0' |
|
81 | + ); |
|
82 | + } |
|
83 | + // setup $_settings array from incoming values. |
|
84 | + self::$_settings[ $addon_name ] = [ |
|
85 | + // array of full server paths to any EES_Shortcodes used by the shortcode |
|
86 | + 'shortcode_paths' => isset($setup_args['shortcode_paths']) |
|
87 | + ? (array) $setup_args['shortcode_paths'] |
|
88 | + : [], |
|
89 | + 'shortcode_fqcns' => isset($setup_args['shortcode_fqcns']) |
|
90 | + ? (array) $setup_args['shortcode_fqcns'] |
|
91 | + : [], |
|
92 | + ]; |
|
93 | + // add to list of shortcodes to be registered |
|
94 | + add_filter( |
|
95 | + 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', |
|
96 | + ['EE_Register_Shortcode', 'add_shortcodes'] |
|
97 | + ); |
|
98 | 98 | |
99 | - add_filter( |
|
100 | - 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
|
101 | - ['EE_Register_Shortcode', 'instantiateAndAddToShortcodeCollection'] |
|
102 | - ); |
|
103 | - return true; |
|
104 | - } |
|
99 | + add_filter( |
|
100 | + 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
|
101 | + ['EE_Register_Shortcode', 'instantiateAndAddToShortcodeCollection'] |
|
102 | + ); |
|
103 | + return true; |
|
104 | + } |
|
105 | 105 | |
106 | 106 | |
107 | - /** |
|
108 | - * Filters the list of shortcodes to add ours. |
|
109 | - * and they're just full filepaths to FOLDERS containing a shortcode class file. Eg. |
|
110 | - * array('espresso_monkey'=>'/public_html/wonder-site/wp-content/plugins/ee4/shortcodes/espresso_monkey'...) |
|
111 | - * |
|
112 | - * @param array $shortcodes_to_register array of paths to all shortcodes that require registering |
|
113 | - * @return array |
|
114 | - */ |
|
115 | - public static function add_shortcodes(array $shortcodes_to_register): array |
|
116 | - { |
|
117 | - $shortcode_paths = []; |
|
118 | - foreach (self::$_settings as $settings) { |
|
119 | - $shortcode_paths[] = $settings['shortcode_paths']; |
|
120 | - } |
|
121 | - return array_merge($shortcodes_to_register, ...$shortcode_paths); |
|
122 | - } |
|
107 | + /** |
|
108 | + * Filters the list of shortcodes to add ours. |
|
109 | + * and they're just full filepaths to FOLDERS containing a shortcode class file. Eg. |
|
110 | + * array('espresso_monkey'=>'/public_html/wonder-site/wp-content/plugins/ee4/shortcodes/espresso_monkey'...) |
|
111 | + * |
|
112 | + * @param array $shortcodes_to_register array of paths to all shortcodes that require registering |
|
113 | + * @return array |
|
114 | + */ |
|
115 | + public static function add_shortcodes(array $shortcodes_to_register): array |
|
116 | + { |
|
117 | + $shortcode_paths = []; |
|
118 | + foreach (self::$_settings as $settings) { |
|
119 | + $shortcode_paths[] = $settings['shortcode_paths']; |
|
120 | + } |
|
121 | + return array_merge($shortcodes_to_register, ...$shortcode_paths); |
|
122 | + } |
|
123 | 123 | |
124 | 124 | |
125 | - /** |
|
126 | - * Hooks into |
|
127 | - * FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection and |
|
128 | - * registers any provided shortcode fully qualified class names. |
|
129 | - * |
|
130 | - * @param CollectionInterface $shortcodes_collection |
|
131 | - * @return CollectionInterface |
|
132 | - * @throws InvalidArgumentException |
|
133 | - * @throws InvalidClassException |
|
134 | - * @throws InvalidDataTypeException |
|
135 | - * @throws InvalidInterfaceException |
|
136 | - */ |
|
137 | - public static function instantiateAndAddToShortcodeCollection( |
|
138 | - CollectionInterface $shortcodes_collection |
|
139 | - ): CollectionInterface { |
|
140 | - foreach (self::$_settings as $settings) { |
|
141 | - if (! empty($settings['shortcode_fqcns'])) { |
|
142 | - foreach ($settings['shortcode_fqcns'] as $shortcode_fqcn) { |
|
143 | - if (! class_exists($shortcode_fqcn)) { |
|
144 | - throw new InvalidClassException( |
|
145 | - sprintf( |
|
146 | - esc_html__( |
|
147 | - 'Are you sure %s is the right fully qualified class name for the shortcode class?', |
|
148 | - 'event_espresso' |
|
149 | - ), |
|
150 | - $shortcode_fqcn |
|
151 | - ) |
|
152 | - ); |
|
153 | - } |
|
154 | - if (! EE_Dependency_Map::instance()->has_dependency_for_class($shortcode_fqcn)) { |
|
155 | - // register dependencies |
|
156 | - EE_Dependency_Map::register_dependencies( |
|
157 | - $shortcode_fqcn, |
|
158 | - [ |
|
159 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
160 | - ] |
|
161 | - ); |
|
162 | - } |
|
163 | - $shortcodes_collection->add(LoaderFactory::getLoader()->getShared($shortcode_fqcn)); |
|
164 | - } |
|
165 | - } |
|
166 | - } |
|
167 | - return $shortcodes_collection; |
|
168 | - } |
|
125 | + /** |
|
126 | + * Hooks into |
|
127 | + * FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection and |
|
128 | + * registers any provided shortcode fully qualified class names. |
|
129 | + * |
|
130 | + * @param CollectionInterface $shortcodes_collection |
|
131 | + * @return CollectionInterface |
|
132 | + * @throws InvalidArgumentException |
|
133 | + * @throws InvalidClassException |
|
134 | + * @throws InvalidDataTypeException |
|
135 | + * @throws InvalidInterfaceException |
|
136 | + */ |
|
137 | + public static function instantiateAndAddToShortcodeCollection( |
|
138 | + CollectionInterface $shortcodes_collection |
|
139 | + ): CollectionInterface { |
|
140 | + foreach (self::$_settings as $settings) { |
|
141 | + if (! empty($settings['shortcode_fqcns'])) { |
|
142 | + foreach ($settings['shortcode_fqcns'] as $shortcode_fqcn) { |
|
143 | + if (! class_exists($shortcode_fqcn)) { |
|
144 | + throw new InvalidClassException( |
|
145 | + sprintf( |
|
146 | + esc_html__( |
|
147 | + 'Are you sure %s is the right fully qualified class name for the shortcode class?', |
|
148 | + 'event_espresso' |
|
149 | + ), |
|
150 | + $shortcode_fqcn |
|
151 | + ) |
|
152 | + ); |
|
153 | + } |
|
154 | + if (! EE_Dependency_Map::instance()->has_dependency_for_class($shortcode_fqcn)) { |
|
155 | + // register dependencies |
|
156 | + EE_Dependency_Map::register_dependencies( |
|
157 | + $shortcode_fqcn, |
|
158 | + [ |
|
159 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
160 | + ] |
|
161 | + ); |
|
162 | + } |
|
163 | + $shortcodes_collection->add(LoaderFactory::getLoader()->getShared($shortcode_fqcn)); |
|
164 | + } |
|
165 | + } |
|
166 | + } |
|
167 | + return $shortcodes_collection; |
|
168 | + } |
|
169 | 169 | |
170 | 170 | |
171 | - /** |
|
172 | - * This deregisters a shortcode that was previously registered with a specific $addon_name. |
|
173 | - * |
|
174 | - * @param string $addon_name the name for the shortcode that was previously registered |
|
175 | - * @return void |
|
176 | - * @since 4.3.0 |
|
177 | - */ |
|
178 | - public static function deregister(string $addon_name = '') |
|
179 | - { |
|
180 | - unset(self::$_settings[ $addon_name ]); |
|
181 | - } |
|
171 | + /** |
|
172 | + * This deregisters a shortcode that was previously registered with a specific $addon_name. |
|
173 | + * |
|
174 | + * @param string $addon_name the name for the shortcode that was previously registered |
|
175 | + * @return void |
|
176 | + * @since 4.3.0 |
|
177 | + */ |
|
178 | + public static function deregister(string $addon_name = '') |
|
179 | + { |
|
180 | + unset(self::$_settings[ $addon_name ]); |
|
181 | + } |
|
182 | 182 | } |
@@ -12,202 +12,202 @@ |
||
12 | 12 | { |
13 | 13 | |
14 | 14 | |
15 | - /** |
|
16 | - * Holds values for registered template pack |
|
17 | - * |
|
18 | - * @since 4.5.0 |
|
19 | - * |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected static $_registry = []; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * Used to register a new template pack with the messages system. |
|
27 | - * |
|
28 | - * Template packs are primarily defined via class extending EE_Messages_Template_Pack and are typically used to |
|
29 | - * change entire layouts for a set of message templates. This method is used to register the new template pack and |
|
30 | - * automatically have it loaded in the appropriate places. |
|
31 | - * |
|
32 | - * This registry also verifies that there isn't already a template pack registered with the same name and if there |
|
33 | - * is then it will add an EE_Error notice. |
|
34 | - * |
|
35 | - * Note that this only handles registering the your Template Pack class with the message template pack system. |
|
36 | - * However, there is also a naming schema you must follow for templates you are providing with your template pack. |
|
37 | - * |
|
38 | - * @param string $addon_name The internal reference used to refer to this template pack. Note, this is first come, |
|
39 | - * first serve. If there is already a template pack registered with this name then the |
|
40 | - * registry will assign a unique reference for it so it can still be activated (but this |
|
41 | - * makes it harder to deregister as it will be unique per load - so its best to try to |
|
42 | - * make this a unique string!) |
|
43 | - * @param array $setup_args array { |
|
44 | - * An array of required values for registering the template pack. |
|
45 | - * @type string $path The path for the new template pack class. |
|
46 | - * @type string $classname The name of the new Template Pack Class. |
|
47 | - * } |
|
48 | - * @return bool |
|
49 | - * @throws EE_Error |
|
50 | - * |
|
51 | - * @see core/libraries/messages/defaults/default/* for all the example templates the default template pack |
|
52 | - * supports. |
|
53 | - * |
|
54 | - * |
|
55 | - * @since 4.5.0 |
|
56 | - * @see EE_Messages_Template_Pack_Default for an example class |
|
57 | - */ |
|
58 | - public static function register(string $addon_name = '', array $setup_args = []): bool |
|
59 | - { |
|
60 | - |
|
61 | - // check for required params |
|
62 | - if (empty($addon_name) || empty($setup_args['path']) || empty($setup_args['classname'])) { |
|
63 | - throw new EE_Error( |
|
64 | - __( |
|
65 | - 'In order to register a new template pack for the EE Messages system, you must include a value to reference the template pack being registered and the setup_args must have the path for the new template pack class as well as the classname for the new Template Pack Class. ', |
|
66 | - 'event_espresso' |
|
67 | - ) |
|
68 | - ); |
|
69 | - } |
|
70 | - |
|
71 | - // make sure we don't register twice |
|
72 | - if (isset(self::$_registry[ $addon_name ])) { |
|
73 | - return true; |
|
74 | - } |
|
75 | - |
|
76 | - // check that incoming $addon_name doesn't already exist. If it does then we'll create a unique reference for this template pack. |
|
77 | - if (isset(self::$_registry[ $addon_name ])) { |
|
78 | - $addon_name = uniqid() . '_' . $addon_name; |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - // make sure this was called in the right place! |
|
83 | - if ( |
|
84 | - ! did_action('EE_Brewing_Regular___messages_caf') |
|
85 | - || did_action('AHEE__EE_System__perform_activations_upgrades_and_migrations') |
|
86 | - ) { |
|
87 | - EE_Error::doing_it_wrong( |
|
88 | - __METHOD__, |
|
89 | - sprintf( |
|
90 | - __( |
|
91 | - 'A EE Messages Template Pack given the reference "%s" has been attempted to be registered with the EE Messages System. It may or may not work because it should be only called on the "EE_Brewing_Regular__messages_caf" hook.', |
|
92 | - 'event_espresso' |
|
93 | - ), |
|
94 | - $addon_name |
|
95 | - ), |
|
96 | - '4.5.0' |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - if (self::_verify_class_not_exist($setup_args['classname'])) { |
|
101 | - self::$_registry[ $addon_name ] = [ |
|
102 | - 'path' => (string) $setup_args['path'], |
|
103 | - 'classname' => (string) $setup_args['classname'], |
|
104 | - ]; |
|
105 | - } |
|
106 | - |
|
107 | - // hook into the system |
|
108 | - add_filter( |
|
109 | - 'FHEE__EED_Messages___set_messages_paths___MSG_PATHS', |
|
110 | - ['EE_Register_Messages_Template_Pack', 'set_template_pack_path'], |
|
111 | - 10 |
|
112 | - ); |
|
113 | - add_filter( |
|
114 | - 'FHEE__EED_Messages__get_template_packs__template_packs', |
|
115 | - ['EE_Register_Messages_Template_Pack', 'set_template_pack'], |
|
116 | - 10 |
|
117 | - ); |
|
118 | - return true; |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * Callback for the FHEE__EED_Messages___set_messages_paths___MSG_PATHS filter. This adds this template packs path |
|
124 | - * to the messages autoloader paths. |
|
125 | - * |
|
126 | - * @param array $paths Array of paths already registered with the messages autoloader |
|
127 | - * |
|
128 | - * @return array |
|
129 | - * @since 4.5.0 |
|
130 | - * |
|
131 | - */ |
|
132 | - public static function set_template_pack_path(array $paths): array |
|
133 | - { |
|
134 | - foreach (self::$_registry as $args) { |
|
135 | - $paths[] = $args['path']; |
|
136 | - } |
|
137 | - return $paths; |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * Callback for the FHEE__EED_Messages__get_template_packs__template_packs filter. This adds the instantiated, |
|
143 | - * registered template pack to the template packs array when requested by client code. |
|
144 | - * |
|
145 | - * @param EE_Messages_Template_Pack[] $template_packs |
|
146 | - * @return EE_Messages_Template_Pack[] |
|
147 | - * @since 4.5.0 |
|
148 | - * |
|
149 | - */ |
|
150 | - public static function set_template_pack(array $template_packs): array |
|
151 | - { |
|
152 | - foreach (self::$_registry as $args) { |
|
153 | - // verify class_exists |
|
154 | - if (! class_exists($args['classname'])) { |
|
155 | - require_once($args['path'] . '/' . $args['classname'] . '.class.php'); |
|
156 | - } |
|
157 | - |
|
158 | - // check again! |
|
159 | - if (class_exists($args['classname'])) { |
|
160 | - $template_pack = new $args['classname'](); |
|
161 | - $template_packs[ $template_pack->dbref ] = $template_pack; |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - return $template_packs; |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * This verifies that the classes for each registered template pack are unique names. |
|
171 | - * |
|
172 | - * @param string $classname The classname being checked |
|
173 | - * |
|
174 | - * @return bool |
|
175 | - */ |
|
176 | - private static function _verify_class_not_exist(string $classname): bool |
|
177 | - { |
|
178 | - // loop through the existing registry and see if the classname is already present. |
|
179 | - foreach (self::$_registry as $args) { |
|
180 | - if ($args['classname'] == $classname) { |
|
181 | - EE_Error::add_error( |
|
182 | - sprintf( |
|
183 | - __( |
|
184 | - 'The %s template pack that you just activated cannot be registered with the messages system because there is already a template pack active using the same classname. Contact the author of this template pack to let them know of the conflict. To stop seeing this message you will need to deactivate this template pack.', |
|
185 | - 'event_espresso' |
|
186 | - ), |
|
187 | - $classname |
|
188 | - ), |
|
189 | - __FILE__, |
|
190 | - __LINE__, |
|
191 | - __FUNCTION__ |
|
192 | - ); |
|
193 | - return false; |
|
194 | - } |
|
195 | - } |
|
196 | - return true; |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * This deregisters a variation set that was previously registered with the given slug. |
|
202 | - * |
|
203 | - * @param string $addon_name The name for the variation set that was previously registered. |
|
204 | - * |
|
205 | - * @return void |
|
206 | - * @since 4.5.0 |
|
207 | - * |
|
208 | - */ |
|
209 | - public static function deregister(string $addon_name = '') |
|
210 | - { |
|
211 | - unset(self::$_registry[ $addon_name ]); |
|
212 | - } |
|
15 | + /** |
|
16 | + * Holds values for registered template pack |
|
17 | + * |
|
18 | + * @since 4.5.0 |
|
19 | + * |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected static $_registry = []; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * Used to register a new template pack with the messages system. |
|
27 | + * |
|
28 | + * Template packs are primarily defined via class extending EE_Messages_Template_Pack and are typically used to |
|
29 | + * change entire layouts for a set of message templates. This method is used to register the new template pack and |
|
30 | + * automatically have it loaded in the appropriate places. |
|
31 | + * |
|
32 | + * This registry also verifies that there isn't already a template pack registered with the same name and if there |
|
33 | + * is then it will add an EE_Error notice. |
|
34 | + * |
|
35 | + * Note that this only handles registering the your Template Pack class with the message template pack system. |
|
36 | + * However, there is also a naming schema you must follow for templates you are providing with your template pack. |
|
37 | + * |
|
38 | + * @param string $addon_name The internal reference used to refer to this template pack. Note, this is first come, |
|
39 | + * first serve. If there is already a template pack registered with this name then the |
|
40 | + * registry will assign a unique reference for it so it can still be activated (but this |
|
41 | + * makes it harder to deregister as it will be unique per load - so its best to try to |
|
42 | + * make this a unique string!) |
|
43 | + * @param array $setup_args array { |
|
44 | + * An array of required values for registering the template pack. |
|
45 | + * @type string $path The path for the new template pack class. |
|
46 | + * @type string $classname The name of the new Template Pack Class. |
|
47 | + * } |
|
48 | + * @return bool |
|
49 | + * @throws EE_Error |
|
50 | + * |
|
51 | + * @see core/libraries/messages/defaults/default/* for all the example templates the default template pack |
|
52 | + * supports. |
|
53 | + * |
|
54 | + * |
|
55 | + * @since 4.5.0 |
|
56 | + * @see EE_Messages_Template_Pack_Default for an example class |
|
57 | + */ |
|
58 | + public static function register(string $addon_name = '', array $setup_args = []): bool |
|
59 | + { |
|
60 | + |
|
61 | + // check for required params |
|
62 | + if (empty($addon_name) || empty($setup_args['path']) || empty($setup_args['classname'])) { |
|
63 | + throw new EE_Error( |
|
64 | + __( |
|
65 | + 'In order to register a new template pack for the EE Messages system, you must include a value to reference the template pack being registered and the setup_args must have the path for the new template pack class as well as the classname for the new Template Pack Class. ', |
|
66 | + 'event_espresso' |
|
67 | + ) |
|
68 | + ); |
|
69 | + } |
|
70 | + |
|
71 | + // make sure we don't register twice |
|
72 | + if (isset(self::$_registry[ $addon_name ])) { |
|
73 | + return true; |
|
74 | + } |
|
75 | + |
|
76 | + // check that incoming $addon_name doesn't already exist. If it does then we'll create a unique reference for this template pack. |
|
77 | + if (isset(self::$_registry[ $addon_name ])) { |
|
78 | + $addon_name = uniqid() . '_' . $addon_name; |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + // make sure this was called in the right place! |
|
83 | + if ( |
|
84 | + ! did_action('EE_Brewing_Regular___messages_caf') |
|
85 | + || did_action('AHEE__EE_System__perform_activations_upgrades_and_migrations') |
|
86 | + ) { |
|
87 | + EE_Error::doing_it_wrong( |
|
88 | + __METHOD__, |
|
89 | + sprintf( |
|
90 | + __( |
|
91 | + 'A EE Messages Template Pack given the reference "%s" has been attempted to be registered with the EE Messages System. It may or may not work because it should be only called on the "EE_Brewing_Regular__messages_caf" hook.', |
|
92 | + 'event_espresso' |
|
93 | + ), |
|
94 | + $addon_name |
|
95 | + ), |
|
96 | + '4.5.0' |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + if (self::_verify_class_not_exist($setup_args['classname'])) { |
|
101 | + self::$_registry[ $addon_name ] = [ |
|
102 | + 'path' => (string) $setup_args['path'], |
|
103 | + 'classname' => (string) $setup_args['classname'], |
|
104 | + ]; |
|
105 | + } |
|
106 | + |
|
107 | + // hook into the system |
|
108 | + add_filter( |
|
109 | + 'FHEE__EED_Messages___set_messages_paths___MSG_PATHS', |
|
110 | + ['EE_Register_Messages_Template_Pack', 'set_template_pack_path'], |
|
111 | + 10 |
|
112 | + ); |
|
113 | + add_filter( |
|
114 | + 'FHEE__EED_Messages__get_template_packs__template_packs', |
|
115 | + ['EE_Register_Messages_Template_Pack', 'set_template_pack'], |
|
116 | + 10 |
|
117 | + ); |
|
118 | + return true; |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * Callback for the FHEE__EED_Messages___set_messages_paths___MSG_PATHS filter. This adds this template packs path |
|
124 | + * to the messages autoloader paths. |
|
125 | + * |
|
126 | + * @param array $paths Array of paths already registered with the messages autoloader |
|
127 | + * |
|
128 | + * @return array |
|
129 | + * @since 4.5.0 |
|
130 | + * |
|
131 | + */ |
|
132 | + public static function set_template_pack_path(array $paths): array |
|
133 | + { |
|
134 | + foreach (self::$_registry as $args) { |
|
135 | + $paths[] = $args['path']; |
|
136 | + } |
|
137 | + return $paths; |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * Callback for the FHEE__EED_Messages__get_template_packs__template_packs filter. This adds the instantiated, |
|
143 | + * registered template pack to the template packs array when requested by client code. |
|
144 | + * |
|
145 | + * @param EE_Messages_Template_Pack[] $template_packs |
|
146 | + * @return EE_Messages_Template_Pack[] |
|
147 | + * @since 4.5.0 |
|
148 | + * |
|
149 | + */ |
|
150 | + public static function set_template_pack(array $template_packs): array |
|
151 | + { |
|
152 | + foreach (self::$_registry as $args) { |
|
153 | + // verify class_exists |
|
154 | + if (! class_exists($args['classname'])) { |
|
155 | + require_once($args['path'] . '/' . $args['classname'] . '.class.php'); |
|
156 | + } |
|
157 | + |
|
158 | + // check again! |
|
159 | + if (class_exists($args['classname'])) { |
|
160 | + $template_pack = new $args['classname'](); |
|
161 | + $template_packs[ $template_pack->dbref ] = $template_pack; |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + return $template_packs; |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * This verifies that the classes for each registered template pack are unique names. |
|
171 | + * |
|
172 | + * @param string $classname The classname being checked |
|
173 | + * |
|
174 | + * @return bool |
|
175 | + */ |
|
176 | + private static function _verify_class_not_exist(string $classname): bool |
|
177 | + { |
|
178 | + // loop through the existing registry and see if the classname is already present. |
|
179 | + foreach (self::$_registry as $args) { |
|
180 | + if ($args['classname'] == $classname) { |
|
181 | + EE_Error::add_error( |
|
182 | + sprintf( |
|
183 | + __( |
|
184 | + 'The %s template pack that you just activated cannot be registered with the messages system because there is already a template pack active using the same classname. Contact the author of this template pack to let them know of the conflict. To stop seeing this message you will need to deactivate this template pack.', |
|
185 | + 'event_espresso' |
|
186 | + ), |
|
187 | + $classname |
|
188 | + ), |
|
189 | + __FILE__, |
|
190 | + __LINE__, |
|
191 | + __FUNCTION__ |
|
192 | + ); |
|
193 | + return false; |
|
194 | + } |
|
195 | + } |
|
196 | + return true; |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * This deregisters a variation set that was previously registered with the given slug. |
|
202 | + * |
|
203 | + * @param string $addon_name The name for the variation set that was previously registered. |
|
204 | + * |
|
205 | + * @return void |
|
206 | + * @since 4.5.0 |
|
207 | + * |
|
208 | + */ |
|
209 | + public static function deregister(string $addon_name = '') |
|
210 | + { |
|
211 | + unset(self::$_registry[ $addon_name ]); |
|
212 | + } |
|
213 | 213 | } |
@@ -69,13 +69,13 @@ discard block |
||
69 | 69 | } |
70 | 70 | |
71 | 71 | // make sure we don't register twice |
72 | - if (isset(self::$_registry[ $addon_name ])) { |
|
72 | + if (isset(self::$_registry[$addon_name])) { |
|
73 | 73 | return true; |
74 | 74 | } |
75 | 75 | |
76 | 76 | // check that incoming $addon_name doesn't already exist. If it does then we'll create a unique reference for this template pack. |
77 | - if (isset(self::$_registry[ $addon_name ])) { |
|
78 | - $addon_name = uniqid() . '_' . $addon_name; |
|
77 | + if (isset(self::$_registry[$addon_name])) { |
|
78 | + $addon_name = uniqid().'_'.$addon_name; |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | } |
99 | 99 | |
100 | 100 | if (self::_verify_class_not_exist($setup_args['classname'])) { |
101 | - self::$_registry[ $addon_name ] = [ |
|
101 | + self::$_registry[$addon_name] = [ |
|
102 | 102 | 'path' => (string) $setup_args['path'], |
103 | 103 | 'classname' => (string) $setup_args['classname'], |
104 | 104 | ]; |
@@ -151,14 +151,14 @@ discard block |
||
151 | 151 | { |
152 | 152 | foreach (self::$_registry as $args) { |
153 | 153 | // verify class_exists |
154 | - if (! class_exists($args['classname'])) { |
|
155 | - require_once($args['path'] . '/' . $args['classname'] . '.class.php'); |
|
154 | + if ( ! class_exists($args['classname'])) { |
|
155 | + require_once($args['path'].'/'.$args['classname'].'.class.php'); |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | // check again! |
159 | 159 | if (class_exists($args['classname'])) { |
160 | 160 | $template_pack = new $args['classname'](); |
161 | - $template_packs[ $template_pack->dbref ] = $template_pack; |
|
161 | + $template_packs[$template_pack->dbref] = $template_pack; |
|
162 | 162 | } |
163 | 163 | } |
164 | 164 | |
@@ -208,6 +208,6 @@ discard block |
||
208 | 208 | */ |
209 | 209 | public static function deregister(string $addon_name = '') |
210 | 210 | { |
211 | - unset(self::$_registry[ $addon_name ]); |
|
211 | + unset(self::$_registry[$addon_name]); |
|
212 | 212 | } |
213 | 213 | } |