@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | ); |
60 | 60 | } |
61 | 61 | // make sure we don't register twice |
62 | - if (isset(self::$_settings[ $addon_name ])) { |
|
62 | + if (isset(self::$_settings[$addon_name])) { |
|
63 | 63 | return true; |
64 | 64 | } |
65 | 65 | // make sure this was called in the right place! |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | ); |
78 | 78 | } |
79 | 79 | // setup $_settings array from incoming values. |
80 | - self::$_settings[ $addon_name ] = [ |
|
80 | + self::$_settings[$addon_name] = [ |
|
81 | 81 | // array of full server paths to any EE_PMT_Base children used |
82 | 82 | 'payment_method_paths' => isset($setup_args['payment_method_paths']) |
83 | 83 | ? (array) $setup_args['payment_method_paths'] |
@@ -93,12 +93,12 @@ discard block |
||
93 | 93 | if (did_action('FHEE__EE_Payment_Method_Manager__register_payment_methods__registered_payment_methods')) { |
94 | 94 | $payment_method_manager = LoaderFactory::getLoader()->getShared('EE_Payment_Method_Manager'); |
95 | 95 | // register payment methods directly |
96 | - foreach (self::$_settings[ $addon_name ]['payment_method_paths'] as $payment_method_path) { |
|
96 | + foreach (self::$_settings[$addon_name]['payment_method_paths'] as $payment_method_path) { |
|
97 | 97 | $payment_method_manager->register_payment_method($payment_method_path); |
98 | 98 | } |
99 | 99 | $capabilities = LoaderFactory::getLoader()->getShared('EE_Capabilities'); |
100 | 100 | $capabilities->addCaps( |
101 | - self::getPaymentMethodCapabilities(self::$_settings[ $addon_name ]) |
|
101 | + self::getPaymentMethodCapabilities(self::$_settings[$addon_name]) |
|
102 | 102 | ); |
103 | 103 | } |
104 | 104 | return true; |
@@ -135,13 +135,13 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public static function deregister(string $addon_name = '') |
137 | 137 | { |
138 | - if (isset(self::$_settings[ $addon_name ])) { |
|
138 | + if (isset(self::$_settings[$addon_name])) { |
|
139 | 139 | // set action for just this module id to delay deregistration until core is loaded and ready. |
140 | - $module_settings = self::$_settings[ $addon_name ]; |
|
141 | - unset(self::$_settings[ $addon_name ]); |
|
140 | + $module_settings = self::$_settings[$addon_name]; |
|
141 | + unset(self::$_settings[$addon_name]); |
|
142 | 142 | add_action( |
143 | 143 | 'AHEE__EE_System__core_loaded_and_ready', |
144 | - function () use ($module_settings) { |
|
144 | + function() use ($module_settings) { |
|
145 | 145 | $capabilities = LoaderFactory::getLoader()->getShared('EE_Capabilities'); |
146 | 146 | $capabilities->removeCaps( |
147 | 147 | EE_Register_Payment_Method::getPaymentMethodCapabilities($module_settings) |
@@ -18,169 +18,169 @@ |
||
18 | 18 | */ |
19 | 19 | class EE_Register_Payment_Method implements EEI_Plugin_API |
20 | 20 | { |
21 | - /** |
|
22 | - * Holds values for registered payment methods |
|
23 | - * |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected static $_settings = []; |
|
21 | + /** |
|
22 | + * Holds values for registered payment methods |
|
23 | + * |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected static $_settings = []; |
|
27 | 27 | |
28 | 28 | |
29 | - /** |
|
30 | - * Method for registering new EE_PMT_Base children |
|
31 | - * |
|
32 | - * @param string $addon_name a unique identifier for this set of modules Required. |
|
33 | - * @param array $setup_args an array of arguments provided for registering modules Required.{ |
|
34 | - * @type string[] $payment_method_paths each element is the folder containing the EE_PMT_Base child class |
|
35 | - * (eg, 'public_html/wp-content/plugins/my_plugin/Payomatic/' which contains |
|
36 | - * the files EE_PMT_Payomatic.pm.php) |
|
37 | - * } |
|
38 | - * @return bool |
|
39 | - * @throws EE_Error |
|
40 | - * @type array payment_method_paths an array of full server paths to folders containing any EE_PMT_Base |
|
41 | - * children, or to the EED_Module files themselves |
|
42 | - * @throws InvalidDataTypeException |
|
43 | - * @throws DomainException |
|
44 | - * @throws InvalidArgumentException |
|
45 | - * @throws InvalidInterfaceException |
|
46 | - * @throws InvalidDataTypeException |
|
47 | - * @since 4.5.0 |
|
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['payment_method_paths'])) { |
|
53 | - throw new EE_Error( |
|
54 | - esc_html__( |
|
55 | - 'In order to register Payment Methods with EE_Register_Payment_Method::register(), you must include a "payment_method_id" (a unique identifier for this set of modules), and an array containing the following keys: "payment_method_paths" (an array of full server paths to folders that contain modules, or to the module files themselves)', |
|
56 | - 'event_espresso' |
|
57 | - ) |
|
58 | - ); |
|
59 | - } |
|
60 | - // make sure we don't register twice |
|
61 | - if (isset(self::$_settings[ $addon_name ])) { |
|
62 | - return true; |
|
63 | - } |
|
64 | - // make sure this was called in the right place! |
|
65 | - if ( |
|
66 | - ! did_action('AHEE__EE_System__load_espresso_addons') |
|
67 | - || did_action('AHEE__EE_System__register_shortcodes_modules_and_widgets') |
|
68 | - ) { |
|
69 | - EE_Error::doing_it_wrong( |
|
70 | - __METHOD__, |
|
71 | - esc_html__( |
|
72 | - 'An attempt to register modules 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 modules.', |
|
73 | - 'event_espresso' |
|
74 | - ), |
|
75 | - '4.3.0' |
|
76 | - ); |
|
77 | - } |
|
78 | - // setup $_settings array from incoming values. |
|
79 | - self::$_settings[ $addon_name ] = [ |
|
80 | - // array of full server paths to any EE_PMT_Base children used |
|
81 | - 'payment_method_paths' => isset($setup_args['payment_method_paths']) |
|
82 | - ? (array) $setup_args['payment_method_paths'] |
|
83 | - : [], |
|
84 | - ]; |
|
85 | - // add to list of modules to be registered |
|
86 | - add_filter( |
|
87 | - 'FHEE__EE_Payment_Method_Manager__register_payment_methods__payment_methods_to_register', |
|
88 | - ['EE_Register_Payment_Method', 'add_payment_methods'] |
|
89 | - ); |
|
90 | - // If EE_Payment_Method_Manager::register_payment_methods has already been called, |
|
91 | - // then we need to add our caps for this payment method manually |
|
92 | - if (did_action('FHEE__EE_Payment_Method_Manager__register_payment_methods__registered_payment_methods')) { |
|
93 | - $payment_method_manager = LoaderFactory::getLoader()->getShared('EE_Payment_Method_Manager'); |
|
94 | - // register payment methods directly |
|
95 | - foreach (self::$_settings[ $addon_name ]['payment_method_paths'] as $payment_method_path) { |
|
96 | - $payment_method_manager->register_payment_method($payment_method_path); |
|
97 | - } |
|
98 | - $capabilities = LoaderFactory::getLoader()->getShared('EE_Capabilities'); |
|
99 | - $capabilities->addCaps( |
|
100 | - self::getPaymentMethodCapabilities(self::$_settings[ $addon_name ]) |
|
101 | - ); |
|
102 | - } |
|
103 | - return true; |
|
104 | - } |
|
29 | + /** |
|
30 | + * Method for registering new EE_PMT_Base children |
|
31 | + * |
|
32 | + * @param string $addon_name a unique identifier for this set of modules Required. |
|
33 | + * @param array $setup_args an array of arguments provided for registering modules Required.{ |
|
34 | + * @type string[] $payment_method_paths each element is the folder containing the EE_PMT_Base child class |
|
35 | + * (eg, 'public_html/wp-content/plugins/my_plugin/Payomatic/' which contains |
|
36 | + * the files EE_PMT_Payomatic.pm.php) |
|
37 | + * } |
|
38 | + * @return bool |
|
39 | + * @throws EE_Error |
|
40 | + * @type array payment_method_paths an array of full server paths to folders containing any EE_PMT_Base |
|
41 | + * children, or to the EED_Module files themselves |
|
42 | + * @throws InvalidDataTypeException |
|
43 | + * @throws DomainException |
|
44 | + * @throws InvalidArgumentException |
|
45 | + * @throws InvalidInterfaceException |
|
46 | + * @throws InvalidDataTypeException |
|
47 | + * @since 4.5.0 |
|
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['payment_method_paths'])) { |
|
53 | + throw new EE_Error( |
|
54 | + esc_html__( |
|
55 | + 'In order to register Payment Methods with EE_Register_Payment_Method::register(), you must include a "payment_method_id" (a unique identifier for this set of modules), and an array containing the following keys: "payment_method_paths" (an array of full server paths to folders that contain modules, or to the module files themselves)', |
|
56 | + 'event_espresso' |
|
57 | + ) |
|
58 | + ); |
|
59 | + } |
|
60 | + // make sure we don't register twice |
|
61 | + if (isset(self::$_settings[ $addon_name ])) { |
|
62 | + return true; |
|
63 | + } |
|
64 | + // make sure this was called in the right place! |
|
65 | + if ( |
|
66 | + ! did_action('AHEE__EE_System__load_espresso_addons') |
|
67 | + || did_action('AHEE__EE_System__register_shortcodes_modules_and_widgets') |
|
68 | + ) { |
|
69 | + EE_Error::doing_it_wrong( |
|
70 | + __METHOD__, |
|
71 | + esc_html__( |
|
72 | + 'An attempt to register modules 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 modules.', |
|
73 | + 'event_espresso' |
|
74 | + ), |
|
75 | + '4.3.0' |
|
76 | + ); |
|
77 | + } |
|
78 | + // setup $_settings array from incoming values. |
|
79 | + self::$_settings[ $addon_name ] = [ |
|
80 | + // array of full server paths to any EE_PMT_Base children used |
|
81 | + 'payment_method_paths' => isset($setup_args['payment_method_paths']) |
|
82 | + ? (array) $setup_args['payment_method_paths'] |
|
83 | + : [], |
|
84 | + ]; |
|
85 | + // add to list of modules to be registered |
|
86 | + add_filter( |
|
87 | + 'FHEE__EE_Payment_Method_Manager__register_payment_methods__payment_methods_to_register', |
|
88 | + ['EE_Register_Payment_Method', 'add_payment_methods'] |
|
89 | + ); |
|
90 | + // If EE_Payment_Method_Manager::register_payment_methods has already been called, |
|
91 | + // then we need to add our caps for this payment method manually |
|
92 | + if (did_action('FHEE__EE_Payment_Method_Manager__register_payment_methods__registered_payment_methods')) { |
|
93 | + $payment_method_manager = LoaderFactory::getLoader()->getShared('EE_Payment_Method_Manager'); |
|
94 | + // register payment methods directly |
|
95 | + foreach (self::$_settings[ $addon_name ]['payment_method_paths'] as $payment_method_path) { |
|
96 | + $payment_method_manager->register_payment_method($payment_method_path); |
|
97 | + } |
|
98 | + $capabilities = LoaderFactory::getLoader()->getShared('EE_Capabilities'); |
|
99 | + $capabilities->addCaps( |
|
100 | + self::getPaymentMethodCapabilities(self::$_settings[ $addon_name ]) |
|
101 | + ); |
|
102 | + } |
|
103 | + return true; |
|
104 | + } |
|
105 | 105 | |
106 | 106 | |
107 | - /** |
|
108 | - * Filters the list of payment methods to add ours. |
|
109 | - * and they're just full filepaths to FOLDERS containing a payment method class file. Eg. |
|
110 | - * |
|
111 | - * @param array $payment_method_folders array of paths to all payment methods that require registering |
|
112 | - * @return array |
|
113 | - */ |
|
114 | - public static function add_payment_methods(array $payment_method_folders): array |
|
115 | - { |
|
116 | - $payment_method_paths = []; |
|
117 | - foreach (self::$_settings as $settings) { |
|
118 | - $payment_method_paths[] = $settings['payment_method_paths']; |
|
119 | - } |
|
120 | - return array_merge($payment_method_folders, ...$payment_method_paths); |
|
121 | - } |
|
107 | + /** |
|
108 | + * Filters the list of payment methods to add ours. |
|
109 | + * and they're just full filepaths to FOLDERS containing a payment method class file. Eg. |
|
110 | + * |
|
111 | + * @param array $payment_method_folders array of paths to all payment methods that require registering |
|
112 | + * @return array |
|
113 | + */ |
|
114 | + public static function add_payment_methods(array $payment_method_folders): array |
|
115 | + { |
|
116 | + $payment_method_paths = []; |
|
117 | + foreach (self::$_settings as $settings) { |
|
118 | + $payment_method_paths[] = $settings['payment_method_paths']; |
|
119 | + } |
|
120 | + return array_merge($payment_method_folders, ...$payment_method_paths); |
|
121 | + } |
|
122 | 122 | |
123 | 123 | |
124 | - /** |
|
125 | - * This deregisters a module that was previously registered with a specific $addon_name. |
|
126 | - * |
|
127 | - * @param string $addon_name the name for the module that was previously registered |
|
128 | - * @return void |
|
129 | - * @throws DomainException |
|
130 | - * @throws InvalidArgumentException |
|
131 | - * @throws InvalidInterfaceException |
|
132 | - * @throws InvalidDataTypeException |
|
133 | - * @since 4.3.0 |
|
134 | - */ |
|
135 | - public static function deregister(string $addon_name = '') |
|
136 | - { |
|
137 | - if (isset(self::$_settings[ $addon_name ])) { |
|
138 | - // set action for just this module id to delay deregistration until core is loaded and ready. |
|
139 | - $module_settings = self::$_settings[ $addon_name ]; |
|
140 | - unset(self::$_settings[ $addon_name ]); |
|
141 | - add_action( |
|
142 | - 'AHEE__EE_System__core_loaded_and_ready', |
|
143 | - function () use ($module_settings) { |
|
144 | - $capabilities = LoaderFactory::getLoader()->getShared('EE_Capabilities'); |
|
145 | - $capabilities->removeCaps( |
|
146 | - EE_Register_Payment_Method::getPaymentMethodCapabilities($module_settings) |
|
147 | - ); |
|
148 | - } |
|
149 | - ); |
|
150 | - } |
|
151 | - } |
|
124 | + /** |
|
125 | + * This deregisters a module that was previously registered with a specific $addon_name. |
|
126 | + * |
|
127 | + * @param string $addon_name the name for the module that was previously registered |
|
128 | + * @return void |
|
129 | + * @throws DomainException |
|
130 | + * @throws InvalidArgumentException |
|
131 | + * @throws InvalidInterfaceException |
|
132 | + * @throws InvalidDataTypeException |
|
133 | + * @since 4.3.0 |
|
134 | + */ |
|
135 | + public static function deregister(string $addon_name = '') |
|
136 | + { |
|
137 | + if (isset(self::$_settings[ $addon_name ])) { |
|
138 | + // set action for just this module id to delay deregistration until core is loaded and ready. |
|
139 | + $module_settings = self::$_settings[ $addon_name ]; |
|
140 | + unset(self::$_settings[ $addon_name ]); |
|
141 | + add_action( |
|
142 | + 'AHEE__EE_System__core_loaded_and_ready', |
|
143 | + function () use ($module_settings) { |
|
144 | + $capabilities = LoaderFactory::getLoader()->getShared('EE_Capabilities'); |
|
145 | + $capabilities->removeCaps( |
|
146 | + EE_Register_Payment_Method::getPaymentMethodCapabilities($module_settings) |
|
147 | + ); |
|
148 | + } |
|
149 | + ); |
|
150 | + } |
|
151 | + } |
|
152 | 152 | |
153 | 153 | |
154 | - /** |
|
155 | - * returns an array of the caps that get added when a Payment Method is registered |
|
156 | - * |
|
157 | - * @param array $settings |
|
158 | - * @return array |
|
159 | - * @throws DomainException |
|
160 | - * @throws InvalidArgumentException |
|
161 | - * @throws InvalidInterfaceException |
|
162 | - * @throws InvalidDataTypeException |
|
163 | - * @access private Developers do NOT use this method. It's only public for PHP5.3 closure support (see deregister) |
|
164 | - * When we drop support for PHP5.3 this will be made private again. You have been warned. |
|
165 | - */ |
|
166 | - public static function getPaymentMethodCapabilities(array $settings): array |
|
167 | - { |
|
168 | - $payment_method_manager = LoaderFactory::getLoader()->getShared('EE_Payment_Method_Manager'); |
|
169 | - $payment_method_caps = ['administrator' => []]; |
|
170 | - if (isset($settings['payment_method_paths'])) { |
|
171 | - foreach ($settings['payment_method_paths'] as $payment_method_path) { |
|
172 | - $payment_method_caps = $payment_method_manager->addPaymentMethodCap( |
|
173 | - strtolower(basename($payment_method_path)), |
|
174 | - $payment_method_caps |
|
175 | - ); |
|
176 | - } |
|
177 | - } |
|
178 | - return $payment_method_caps; |
|
179 | - } |
|
154 | + /** |
|
155 | + * returns an array of the caps that get added when a Payment Method is registered |
|
156 | + * |
|
157 | + * @param array $settings |
|
158 | + * @return array |
|
159 | + * @throws DomainException |
|
160 | + * @throws InvalidArgumentException |
|
161 | + * @throws InvalidInterfaceException |
|
162 | + * @throws InvalidDataTypeException |
|
163 | + * @access private Developers do NOT use this method. It's only public for PHP5.3 closure support (see deregister) |
|
164 | + * When we drop support for PHP5.3 this will be made private again. You have been warned. |
|
165 | + */ |
|
166 | + public static function getPaymentMethodCapabilities(array $settings): array |
|
167 | + { |
|
168 | + $payment_method_manager = LoaderFactory::getLoader()->getShared('EE_Payment_Method_Manager'); |
|
169 | + $payment_method_caps = ['administrator' => []]; |
|
170 | + if (isset($settings['payment_method_paths'])) { |
|
171 | + foreach ($settings['payment_method_paths'] as $payment_method_path) { |
|
172 | + $payment_method_caps = $payment_method_manager->addPaymentMethodCap( |
|
173 | + strtolower(basename($payment_method_path)), |
|
174 | + $payment_method_caps |
|
175 | + ); |
|
176 | + } |
|
177 | + } |
|
178 | + return $payment_method_caps; |
|
179 | + } |
|
180 | 180 | |
181 | 181 | |
182 | - public static function reset(): void |
|
183 | - { |
|
184 | - self::$_settings = []; |
|
185 | - } |
|
182 | + public static function reset(): void |
|
183 | + { |
|
184 | + self::$_settings = []; |
|
185 | + } |
|
186 | 186 | } |
@@ -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; |
@@ -25,494 +25,494 @@ |
||
25 | 25 | */ |
26 | 26 | class EE_Form_Section extends EE_Base_Class |
27 | 27 | { |
28 | - /** |
|
29 | - * @var Attributes |
|
30 | - */ |
|
31 | - private $attributes; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var EE_Form_Element[] |
|
35 | - */ |
|
36 | - private $form_elements = []; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var FormLabel |
|
40 | - */ |
|
41 | - private $label; |
|
42 | - |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * @param array $props_n_values |
|
47 | - * @return EE_Form_Section |
|
48 | - * @throws EE_Error |
|
49 | - * @throws ReflectionException |
|
50 | - */ |
|
51 | - public static function new_instance(array $props_n_values = []): EE_Form_Section |
|
52 | - { |
|
53 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
54 | - return $has_object ?: new self($props_n_values); |
|
55 | - } |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * @param array $props_n_values |
|
60 | - * @return EE_Form_Section |
|
61 | - * @throws EE_Error |
|
62 | - * @throws ReflectionException |
|
63 | - */ |
|
64 | - public static function new_instance_from_db(array $props_n_values = []): EE_Form_Section |
|
65 | - { |
|
66 | - return new self($props_n_values, true); |
|
67 | - } |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * Form Section UUID (universally unique identifier) |
|
72 | - * |
|
73 | - * @return string |
|
74 | - * @throws EE_Error |
|
75 | - * @throws ReflectionException |
|
76 | - */ |
|
77 | - public function UUID(): string |
|
78 | - { |
|
79 | - return $this->get('FSC_UUID'); |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * last 8 characters of the UUID |
|
85 | - * |
|
86 | - * @return string |
|
87 | - * @throws EE_Error |
|
88 | - * @throws ReflectionException |
|
89 | - */ |
|
90 | - public function uuidSlug(): string |
|
91 | - { |
|
92 | - return substr($this->UUID(), -8); |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * @param string $UUID |
|
98 | - * @throws EE_Error |
|
99 | - * @throws ReflectionException |
|
100 | - */ |
|
101 | - public function setUUID(string $UUID) |
|
102 | - { |
|
103 | - if (! Cuid::isCuid($UUID)) { |
|
104 | - throw new InvalidArgumentException( |
|
105 | - sprintf( |
|
106 | - /* translators: 1: UUID value, 2: UUID generator function. */ |
|
107 | - esc_html__( |
|
108 | - 'The supplied UUID "%1$s" is invalid or missing. Please use %2$s to generate a valid one.', |
|
109 | - 'event_espresso' |
|
110 | - ), |
|
111 | - $UUID, |
|
112 | - "`Cuid::cuid()`" |
|
113 | - ) |
|
114 | - ); |
|
115 | - } |
|
116 | - $this->set('FSC_UUID', $UUID); |
|
117 | - } |
|
118 | - |
|
119 | - |
|
120 | - /** |
|
121 | - * Form user types that this form section should be presented to. |
|
122 | - * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
123 | - * |
|
124 | - * @return string |
|
125 | - * @throws EE_Error |
|
126 | - * @throws ReflectionException |
|
127 | - */ |
|
128 | - public function appliesTo(): string |
|
129 | - { |
|
130 | - return $this->get('FSC_appliesTo'); |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * Form user types that this form section should be presented to. |
|
136 | - * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
137 | - * |
|
138 | - * @param EE_Registration|string $registrant |
|
139 | - * @return bool |
|
140 | - * @throws EE_Error |
|
141 | - * @throws ReflectionException |
|
142 | - */ |
|
143 | - public function appliesToRegistrant($registrant): bool |
|
144 | - { |
|
145 | - switch ($this->appliesTo()) { |
|
146 | - case EEM_Form_Section::APPLIES_TO_PRIMARY: |
|
147 | - return $registrant instanceof EE_Registration && $registrant->is_primary_registrant(); |
|
148 | - case EEM_Form_Section::APPLIES_TO_PURCHASER: |
|
149 | - return $registrant === 'purchaser'; |
|
150 | - case EEM_Form_Section::APPLIES_TO_REGISTRANTS: |
|
151 | - return $registrant instanceof EE_Registration && ! $registrant->is_primary_registrant(); |
|
152 | - case EEM_Form_Section::APPLIES_TO_ALL: |
|
153 | - default: |
|
154 | - return true; |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * @param string $user_type |
|
161 | - * @throws EE_Error |
|
162 | - * @throws ReflectionException |
|
163 | - */ |
|
164 | - public function setAppliesTo(string $user_type) |
|
165 | - { |
|
166 | - $this->set('FSC_appliesTo', $user_type); |
|
167 | - } |
|
168 | - |
|
169 | - |
|
170 | - /** |
|
171 | - * JSON string of HTML attributes, such as class, to be applied to this form section\'s container. |
|
172 | - * |
|
173 | - * @return Attributes |
|
174 | - * @throws EE_Error |
|
175 | - * @throws ReflectionException |
|
176 | - */ |
|
177 | - public function attributes(): ?Attributes |
|
178 | - { |
|
179 | - if (! $this->attributes instanceof Attributes) { |
|
180 | - $this->attributes = Attributes::fromJson($this->get('FSC_attributes')); |
|
181 | - } |
|
182 | - return $this->attributes; |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * @param Attributes $attributes |
|
188 | - * @throws EE_Error |
|
189 | - * @throws ReflectionException |
|
190 | - */ |
|
191 | - public function setAttributes(Attributes $attributes) |
|
192 | - { |
|
193 | - // set local object |
|
194 | - $this->attributes = $attributes; |
|
195 | - // then pass to model as an array which will get converted to JSON by the model field |
|
196 | - $this->set('FSC_attributes', $attributes->toArray()); |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * UUID or ID of related entity this form section belongs to. |
|
202 | - * |
|
203 | - * @return string |
|
204 | - * @throws EE_Error |
|
205 | - * @throws ReflectionException |
|
206 | - */ |
|
207 | - public function belongsTo(): ?string |
|
208 | - { |
|
209 | - return $this->get('FSC_belongsTo'); |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - /** |
|
214 | - * @param string $parent_UUID |
|
215 | - * @throws EE_Error |
|
216 | - * @throws ReflectionException |
|
217 | - */ |
|
218 | - public function setBelongsTo(string $parent_UUID) |
|
219 | - { |
|
220 | - $this->set('FSC_belongsTo', $parent_UUID); |
|
221 | - } |
|
222 | - |
|
223 | - |
|
224 | - /** |
|
225 | - * @return EE_Form_Element[] |
|
226 | - * @throws EE_Error |
|
227 | - * @throws ReflectionException |
|
228 | - */ |
|
229 | - public function formElements(): array |
|
230 | - { |
|
231 | - return $this->form_elements ?: $this->getFormElements(); |
|
232 | - } |
|
233 | - |
|
234 | - |
|
235 | - /** |
|
236 | - * @return EE_Form_Element[] |
|
237 | - * @throws EE_Error |
|
238 | - * @throws ReflectionException |
|
239 | - */ |
|
240 | - private function getFormElements(): array |
|
241 | - { |
|
242 | - $form_elements = $this->get_many_related('Form_Element', ['order_by' => ['FIN_order' => 'ASC']]); |
|
243 | - foreach ($form_elements as $form_element) { |
|
244 | - if ($form_element instanceof EE_Form_Element) { |
|
245 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
246 | - } |
|
247 | - } |
|
248 | - return $this->form_elements; |
|
249 | - } |
|
250 | - |
|
251 | - |
|
252 | - /** |
|
253 | - * @param EE_Form_Element[] $form_elements |
|
254 | - * @throws EE_Error |
|
255 | - * @throws ReflectionException |
|
256 | - */ |
|
257 | - public function setFormElements(array $form_elements): void |
|
258 | - { |
|
259 | - foreach ($form_elements as $form_element) { |
|
260 | - if ($form_element instanceof EE_Form_Element) { |
|
261 | - $this->_add_relation_to($form_element->UUID(), 'Form_Element'); |
|
262 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
263 | - } |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - |
|
268 | - /** |
|
269 | - * @param EE_Form_Element[] $all_form_elements |
|
270 | - * @return EE_Form_Element[] |
|
271 | - * @throws EE_Error |
|
272 | - * @throws ReflectionException |
|
273 | - */ |
|
274 | - public function filterFormElements(array $all_form_elements): array |
|
275 | - { |
|
276 | - return array_filter($all_form_elements, $this->formElementFilter()); |
|
277 | - } |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * returns a closure that can be used to filter form elements for this form section |
|
282 | - * usage: |
|
283 | - * $filter = EEM_Form_Element::formElementFilter(); |
|
284 | - * $filtered_form_elements = array_filter( $all_form_elements, $filter ); |
|
285 | - * |
|
286 | - * @return Closure |
|
287 | - * @throws EE_Error |
|
288 | - * @throws ReflectionException |
|
289 | - */ |
|
290 | - public function formElementFilter(): Closure |
|
291 | - { |
|
292 | - $FSC_UUID = strtolower($this->UUID()); |
|
293 | - return function ($form_element) use ($FSC_UUID) { |
|
294 | - return $form_element instanceof EE_Form_Element && strtolower($form_element->belongsTo()) === $FSC_UUID; |
|
295 | - }; |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * returns a FormLabel object for managing form section labels, ie: the form section heading |
|
301 | - * |
|
302 | - * @return FormLabel |
|
303 | - * @throws EE_Error |
|
304 | - * @throws ReflectionException |
|
305 | - */ |
|
306 | - public function label(): ?FormLabel |
|
307 | - { |
|
308 | - if (! $this->label instanceof FormLabel) { |
|
309 | - $this->label = FormLabel::fromJson($this->get('FSC_label')); |
|
310 | - } |
|
311 | - return $this->label; |
|
312 | - } |
|
313 | - |
|
314 | - |
|
315 | - /** |
|
316 | - * @param FormLabel $label |
|
317 | - * @throws EE_Error |
|
318 | - * @throws ReflectionException |
|
319 | - */ |
|
320 | - public function setLabel(FormLabel $label) |
|
321 | - { |
|
322 | - // set local object |
|
323 | - $this->label = $label; |
|
324 | - // then pass to model as an array which will get converted to JSON by the model field |
|
325 | - $this->set('FSC_label', $label->toJson()); |
|
326 | - } |
|
327 | - |
|
328 | - |
|
329 | - /** |
|
330 | - * Order in which form section appears in a form. |
|
331 | - * |
|
332 | - * @return int |
|
333 | - * @throws EE_Error |
|
334 | - * @throws ReflectionException |
|
335 | - */ |
|
336 | - public function order(): int |
|
337 | - { |
|
338 | - return $this->get('FSC_order'); |
|
339 | - } |
|
340 | - |
|
341 | - |
|
342 | - /** |
|
343 | - * @param int $order |
|
344 | - * @throws EE_Error |
|
345 | - * @throws ReflectionException |
|
346 | - */ |
|
347 | - public function setOrder(int $order) |
|
348 | - { |
|
349 | - $this->set('FSC_order', $order); |
|
350 | - } |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * combination of public label and UUID slug for use in identifiers |
|
355 | - * |
|
356 | - * @return string |
|
357 | - * @throws EE_Error |
|
358 | - * @throws ReflectionException |
|
359 | - */ |
|
360 | - public function slug(): ?string |
|
361 | - { |
|
362 | - $label = sanitize_title($this->label()->publicLabel()); |
|
363 | - return "{$label}-{$this->uuidSlug()}"; |
|
364 | - } |
|
365 | - |
|
366 | - |
|
367 | - /** |
|
368 | - * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
369 | - * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
370 | - * |
|
371 | - * @return string |
|
372 | - * @throws EE_Error |
|
373 | - * @throws ReflectionException |
|
374 | - */ |
|
375 | - public function status(): ?string |
|
376 | - { |
|
377 | - return $this->get('FSC_status'); |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - /** |
|
382 | - * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
383 | - * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
384 | - * |
|
385 | - * @param string $status |
|
386 | - * @throws EE_Error |
|
387 | - * @throws ReflectionException |
|
388 | - */ |
|
389 | - public function setStatus(string $status) |
|
390 | - { |
|
391 | - $this->set('FSC_status', $status); |
|
392 | - } |
|
393 | - |
|
394 | - |
|
395 | - /** |
|
396 | - * returns the id the wordpress user who created this question |
|
397 | - * |
|
398 | - * @return int |
|
399 | - * @throws EE_Error |
|
400 | - * @throws ReflectionException |
|
401 | - */ |
|
402 | - public function wp_user(): int |
|
403 | - { |
|
404 | - return $this->get('FSC_wpUser'); |
|
405 | - } |
|
406 | - |
|
407 | - |
|
408 | - /** |
|
409 | - * @param int $wp_user |
|
410 | - * @throws EE_Error |
|
411 | - * @throws ReflectionException |
|
412 | - */ |
|
413 | - public function setWpUser(int $wp_user) |
|
414 | - { |
|
415 | - $this->set('FSC_wpUser', $wp_user); |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - /** |
|
420 | - * @param array $set_cols_n_values |
|
421 | - * @return bool|int|string |
|
422 | - * @throws EE_Error |
|
423 | - * @throws ReflectionException |
|
424 | - */ |
|
425 | - public function save($set_cols_n_values = []) |
|
426 | - { |
|
427 | - // make sure internal versions for all composite objects are updated |
|
428 | - $this->set('FSC_attributes', $this->attributes()->toArray()); |
|
429 | - $this->set('FSC_label', $this->label()->toArray()); |
|
430 | - return parent::save($set_cols_n_values); |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Whether the section is active. |
|
435 | - * |
|
436 | - * @return boolean TRUE if is active, FALSE if not. |
|
437 | - * @throws ReflectionException |
|
438 | - * @throws InvalidArgumentException |
|
439 | - * @throws InvalidInterfaceException |
|
440 | - * @throws InvalidDataTypeException |
|
441 | - * @throws EE_Error |
|
442 | - */ |
|
443 | - public function isActive(): bool |
|
444 | - { |
|
445 | - return $this->status() === FormStatus::ACTIVE; |
|
446 | - } |
|
447 | - |
|
448 | - /** |
|
449 | - * Whether the section is archived. |
|
450 | - * |
|
451 | - * @return boolean TRUE if is archived, FALSE if not. |
|
452 | - * @throws ReflectionException |
|
453 | - * @throws InvalidArgumentException |
|
454 | - * @throws InvalidInterfaceException |
|
455 | - * @throws InvalidDataTypeException |
|
456 | - * @throws EE_Error |
|
457 | - */ |
|
458 | - public function isArchived(): bool |
|
459 | - { |
|
460 | - return $this->status() === FormStatus::ARCHIVED; |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * Whether the section is a default one. |
|
465 | - * |
|
466 | - * @return boolean TRUE if is default, FALSE if not. |
|
467 | - * @throws ReflectionException |
|
468 | - * @throws InvalidArgumentException |
|
469 | - * @throws InvalidInterfaceException |
|
470 | - * @throws InvalidDataTypeException |
|
471 | - * @throws EE_Error |
|
472 | - */ |
|
473 | - public function isDefault(): bool |
|
474 | - { |
|
475 | - return $this->status() === FormStatus::DEFAULT; |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Whether the section is a shared one. |
|
480 | - * |
|
481 | - * @return boolean TRUE if is shared, FALSE if not. |
|
482 | - * @throws ReflectionException |
|
483 | - * @throws InvalidArgumentException |
|
484 | - * @throws InvalidInterfaceException |
|
485 | - * @throws InvalidDataTypeException |
|
486 | - * @throws EE_Error |
|
487 | - */ |
|
488 | - public function isShared(): bool |
|
489 | - { |
|
490 | - return $this->status() === FormStatus::SHARED; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * Whether the section is trashed. |
|
495 | - * |
|
496 | - * @return boolean TRUE if is trashed, FALSE if not. |
|
497 | - * @throws ReflectionException |
|
498 | - * @throws InvalidArgumentException |
|
499 | - * @throws InvalidInterfaceException |
|
500 | - * @throws InvalidDataTypeException |
|
501 | - * @throws EE_Error |
|
502 | - */ |
|
503 | - public function isTrashed(): bool |
|
504 | - { |
|
505 | - return $this->status() === FormStatus::TRASHED; |
|
506 | - } |
|
507 | - |
|
508 | - |
|
509 | - /** |
|
510 | - * @return bool |
|
511 | - * @throws EE_Error |
|
512 | - * @throws ReflectionException |
|
513 | - */ |
|
514 | - public function isTopLevelFormSection(): bool |
|
515 | - { |
|
516 | - return empty($this->belongsTo()); |
|
517 | - } |
|
28 | + /** |
|
29 | + * @var Attributes |
|
30 | + */ |
|
31 | + private $attributes; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var EE_Form_Element[] |
|
35 | + */ |
|
36 | + private $form_elements = []; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var FormLabel |
|
40 | + */ |
|
41 | + private $label; |
|
42 | + |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * @param array $props_n_values |
|
47 | + * @return EE_Form_Section |
|
48 | + * @throws EE_Error |
|
49 | + * @throws ReflectionException |
|
50 | + */ |
|
51 | + public static function new_instance(array $props_n_values = []): EE_Form_Section |
|
52 | + { |
|
53 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
54 | + return $has_object ?: new self($props_n_values); |
|
55 | + } |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * @param array $props_n_values |
|
60 | + * @return EE_Form_Section |
|
61 | + * @throws EE_Error |
|
62 | + * @throws ReflectionException |
|
63 | + */ |
|
64 | + public static function new_instance_from_db(array $props_n_values = []): EE_Form_Section |
|
65 | + { |
|
66 | + return new self($props_n_values, true); |
|
67 | + } |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * Form Section UUID (universally unique identifier) |
|
72 | + * |
|
73 | + * @return string |
|
74 | + * @throws EE_Error |
|
75 | + * @throws ReflectionException |
|
76 | + */ |
|
77 | + public function UUID(): string |
|
78 | + { |
|
79 | + return $this->get('FSC_UUID'); |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * last 8 characters of the UUID |
|
85 | + * |
|
86 | + * @return string |
|
87 | + * @throws EE_Error |
|
88 | + * @throws ReflectionException |
|
89 | + */ |
|
90 | + public function uuidSlug(): string |
|
91 | + { |
|
92 | + return substr($this->UUID(), -8); |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * @param string $UUID |
|
98 | + * @throws EE_Error |
|
99 | + * @throws ReflectionException |
|
100 | + */ |
|
101 | + public function setUUID(string $UUID) |
|
102 | + { |
|
103 | + if (! Cuid::isCuid($UUID)) { |
|
104 | + throw new InvalidArgumentException( |
|
105 | + sprintf( |
|
106 | + /* translators: 1: UUID value, 2: UUID generator function. */ |
|
107 | + esc_html__( |
|
108 | + 'The supplied UUID "%1$s" is invalid or missing. Please use %2$s to generate a valid one.', |
|
109 | + 'event_espresso' |
|
110 | + ), |
|
111 | + $UUID, |
|
112 | + "`Cuid::cuid()`" |
|
113 | + ) |
|
114 | + ); |
|
115 | + } |
|
116 | + $this->set('FSC_UUID', $UUID); |
|
117 | + } |
|
118 | + |
|
119 | + |
|
120 | + /** |
|
121 | + * Form user types that this form section should be presented to. |
|
122 | + * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
123 | + * |
|
124 | + * @return string |
|
125 | + * @throws EE_Error |
|
126 | + * @throws ReflectionException |
|
127 | + */ |
|
128 | + public function appliesTo(): string |
|
129 | + { |
|
130 | + return $this->get('FSC_appliesTo'); |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * Form user types that this form section should be presented to. |
|
136 | + * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
137 | + * |
|
138 | + * @param EE_Registration|string $registrant |
|
139 | + * @return bool |
|
140 | + * @throws EE_Error |
|
141 | + * @throws ReflectionException |
|
142 | + */ |
|
143 | + public function appliesToRegistrant($registrant): bool |
|
144 | + { |
|
145 | + switch ($this->appliesTo()) { |
|
146 | + case EEM_Form_Section::APPLIES_TO_PRIMARY: |
|
147 | + return $registrant instanceof EE_Registration && $registrant->is_primary_registrant(); |
|
148 | + case EEM_Form_Section::APPLIES_TO_PURCHASER: |
|
149 | + return $registrant === 'purchaser'; |
|
150 | + case EEM_Form_Section::APPLIES_TO_REGISTRANTS: |
|
151 | + return $registrant instanceof EE_Registration && ! $registrant->is_primary_registrant(); |
|
152 | + case EEM_Form_Section::APPLIES_TO_ALL: |
|
153 | + default: |
|
154 | + return true; |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * @param string $user_type |
|
161 | + * @throws EE_Error |
|
162 | + * @throws ReflectionException |
|
163 | + */ |
|
164 | + public function setAppliesTo(string $user_type) |
|
165 | + { |
|
166 | + $this->set('FSC_appliesTo', $user_type); |
|
167 | + } |
|
168 | + |
|
169 | + |
|
170 | + /** |
|
171 | + * JSON string of HTML attributes, such as class, to be applied to this form section\'s container. |
|
172 | + * |
|
173 | + * @return Attributes |
|
174 | + * @throws EE_Error |
|
175 | + * @throws ReflectionException |
|
176 | + */ |
|
177 | + public function attributes(): ?Attributes |
|
178 | + { |
|
179 | + if (! $this->attributes instanceof Attributes) { |
|
180 | + $this->attributes = Attributes::fromJson($this->get('FSC_attributes')); |
|
181 | + } |
|
182 | + return $this->attributes; |
|
183 | + } |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * @param Attributes $attributes |
|
188 | + * @throws EE_Error |
|
189 | + * @throws ReflectionException |
|
190 | + */ |
|
191 | + public function setAttributes(Attributes $attributes) |
|
192 | + { |
|
193 | + // set local object |
|
194 | + $this->attributes = $attributes; |
|
195 | + // then pass to model as an array which will get converted to JSON by the model field |
|
196 | + $this->set('FSC_attributes', $attributes->toArray()); |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * UUID or ID of related entity this form section belongs to. |
|
202 | + * |
|
203 | + * @return string |
|
204 | + * @throws EE_Error |
|
205 | + * @throws ReflectionException |
|
206 | + */ |
|
207 | + public function belongsTo(): ?string |
|
208 | + { |
|
209 | + return $this->get('FSC_belongsTo'); |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + /** |
|
214 | + * @param string $parent_UUID |
|
215 | + * @throws EE_Error |
|
216 | + * @throws ReflectionException |
|
217 | + */ |
|
218 | + public function setBelongsTo(string $parent_UUID) |
|
219 | + { |
|
220 | + $this->set('FSC_belongsTo', $parent_UUID); |
|
221 | + } |
|
222 | + |
|
223 | + |
|
224 | + /** |
|
225 | + * @return EE_Form_Element[] |
|
226 | + * @throws EE_Error |
|
227 | + * @throws ReflectionException |
|
228 | + */ |
|
229 | + public function formElements(): array |
|
230 | + { |
|
231 | + return $this->form_elements ?: $this->getFormElements(); |
|
232 | + } |
|
233 | + |
|
234 | + |
|
235 | + /** |
|
236 | + * @return EE_Form_Element[] |
|
237 | + * @throws EE_Error |
|
238 | + * @throws ReflectionException |
|
239 | + */ |
|
240 | + private function getFormElements(): array |
|
241 | + { |
|
242 | + $form_elements = $this->get_many_related('Form_Element', ['order_by' => ['FIN_order' => 'ASC']]); |
|
243 | + foreach ($form_elements as $form_element) { |
|
244 | + if ($form_element instanceof EE_Form_Element) { |
|
245 | + $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
246 | + } |
|
247 | + } |
|
248 | + return $this->form_elements; |
|
249 | + } |
|
250 | + |
|
251 | + |
|
252 | + /** |
|
253 | + * @param EE_Form_Element[] $form_elements |
|
254 | + * @throws EE_Error |
|
255 | + * @throws ReflectionException |
|
256 | + */ |
|
257 | + public function setFormElements(array $form_elements): void |
|
258 | + { |
|
259 | + foreach ($form_elements as $form_element) { |
|
260 | + if ($form_element instanceof EE_Form_Element) { |
|
261 | + $this->_add_relation_to($form_element->UUID(), 'Form_Element'); |
|
262 | + $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
263 | + } |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + /** |
|
269 | + * @param EE_Form_Element[] $all_form_elements |
|
270 | + * @return EE_Form_Element[] |
|
271 | + * @throws EE_Error |
|
272 | + * @throws ReflectionException |
|
273 | + */ |
|
274 | + public function filterFormElements(array $all_form_elements): array |
|
275 | + { |
|
276 | + return array_filter($all_form_elements, $this->formElementFilter()); |
|
277 | + } |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * returns a closure that can be used to filter form elements for this form section |
|
282 | + * usage: |
|
283 | + * $filter = EEM_Form_Element::formElementFilter(); |
|
284 | + * $filtered_form_elements = array_filter( $all_form_elements, $filter ); |
|
285 | + * |
|
286 | + * @return Closure |
|
287 | + * @throws EE_Error |
|
288 | + * @throws ReflectionException |
|
289 | + */ |
|
290 | + public function formElementFilter(): Closure |
|
291 | + { |
|
292 | + $FSC_UUID = strtolower($this->UUID()); |
|
293 | + return function ($form_element) use ($FSC_UUID) { |
|
294 | + return $form_element instanceof EE_Form_Element && strtolower($form_element->belongsTo()) === $FSC_UUID; |
|
295 | + }; |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * returns a FormLabel object for managing form section labels, ie: the form section heading |
|
301 | + * |
|
302 | + * @return FormLabel |
|
303 | + * @throws EE_Error |
|
304 | + * @throws ReflectionException |
|
305 | + */ |
|
306 | + public function label(): ?FormLabel |
|
307 | + { |
|
308 | + if (! $this->label instanceof FormLabel) { |
|
309 | + $this->label = FormLabel::fromJson($this->get('FSC_label')); |
|
310 | + } |
|
311 | + return $this->label; |
|
312 | + } |
|
313 | + |
|
314 | + |
|
315 | + /** |
|
316 | + * @param FormLabel $label |
|
317 | + * @throws EE_Error |
|
318 | + * @throws ReflectionException |
|
319 | + */ |
|
320 | + public function setLabel(FormLabel $label) |
|
321 | + { |
|
322 | + // set local object |
|
323 | + $this->label = $label; |
|
324 | + // then pass to model as an array which will get converted to JSON by the model field |
|
325 | + $this->set('FSC_label', $label->toJson()); |
|
326 | + } |
|
327 | + |
|
328 | + |
|
329 | + /** |
|
330 | + * Order in which form section appears in a form. |
|
331 | + * |
|
332 | + * @return int |
|
333 | + * @throws EE_Error |
|
334 | + * @throws ReflectionException |
|
335 | + */ |
|
336 | + public function order(): int |
|
337 | + { |
|
338 | + return $this->get('FSC_order'); |
|
339 | + } |
|
340 | + |
|
341 | + |
|
342 | + /** |
|
343 | + * @param int $order |
|
344 | + * @throws EE_Error |
|
345 | + * @throws ReflectionException |
|
346 | + */ |
|
347 | + public function setOrder(int $order) |
|
348 | + { |
|
349 | + $this->set('FSC_order', $order); |
|
350 | + } |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * combination of public label and UUID slug for use in identifiers |
|
355 | + * |
|
356 | + * @return string |
|
357 | + * @throws EE_Error |
|
358 | + * @throws ReflectionException |
|
359 | + */ |
|
360 | + public function slug(): ?string |
|
361 | + { |
|
362 | + $label = sanitize_title($this->label()->publicLabel()); |
|
363 | + return "{$label}-{$this->uuidSlug()}"; |
|
364 | + } |
|
365 | + |
|
366 | + |
|
367 | + /** |
|
368 | + * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
369 | + * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
370 | + * |
|
371 | + * @return string |
|
372 | + * @throws EE_Error |
|
373 | + * @throws ReflectionException |
|
374 | + */ |
|
375 | + public function status(): ?string |
|
376 | + { |
|
377 | + return $this->get('FSC_status'); |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + /** |
|
382 | + * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
383 | + * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
384 | + * |
|
385 | + * @param string $status |
|
386 | + * @throws EE_Error |
|
387 | + * @throws ReflectionException |
|
388 | + */ |
|
389 | + public function setStatus(string $status) |
|
390 | + { |
|
391 | + $this->set('FSC_status', $status); |
|
392 | + } |
|
393 | + |
|
394 | + |
|
395 | + /** |
|
396 | + * returns the id the wordpress user who created this question |
|
397 | + * |
|
398 | + * @return int |
|
399 | + * @throws EE_Error |
|
400 | + * @throws ReflectionException |
|
401 | + */ |
|
402 | + public function wp_user(): int |
|
403 | + { |
|
404 | + return $this->get('FSC_wpUser'); |
|
405 | + } |
|
406 | + |
|
407 | + |
|
408 | + /** |
|
409 | + * @param int $wp_user |
|
410 | + * @throws EE_Error |
|
411 | + * @throws ReflectionException |
|
412 | + */ |
|
413 | + public function setWpUser(int $wp_user) |
|
414 | + { |
|
415 | + $this->set('FSC_wpUser', $wp_user); |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + /** |
|
420 | + * @param array $set_cols_n_values |
|
421 | + * @return bool|int|string |
|
422 | + * @throws EE_Error |
|
423 | + * @throws ReflectionException |
|
424 | + */ |
|
425 | + public function save($set_cols_n_values = []) |
|
426 | + { |
|
427 | + // make sure internal versions for all composite objects are updated |
|
428 | + $this->set('FSC_attributes', $this->attributes()->toArray()); |
|
429 | + $this->set('FSC_label', $this->label()->toArray()); |
|
430 | + return parent::save($set_cols_n_values); |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Whether the section is active. |
|
435 | + * |
|
436 | + * @return boolean TRUE if is active, FALSE if not. |
|
437 | + * @throws ReflectionException |
|
438 | + * @throws InvalidArgumentException |
|
439 | + * @throws InvalidInterfaceException |
|
440 | + * @throws InvalidDataTypeException |
|
441 | + * @throws EE_Error |
|
442 | + */ |
|
443 | + public function isActive(): bool |
|
444 | + { |
|
445 | + return $this->status() === FormStatus::ACTIVE; |
|
446 | + } |
|
447 | + |
|
448 | + /** |
|
449 | + * Whether the section is archived. |
|
450 | + * |
|
451 | + * @return boolean TRUE if is archived, FALSE if not. |
|
452 | + * @throws ReflectionException |
|
453 | + * @throws InvalidArgumentException |
|
454 | + * @throws InvalidInterfaceException |
|
455 | + * @throws InvalidDataTypeException |
|
456 | + * @throws EE_Error |
|
457 | + */ |
|
458 | + public function isArchived(): bool |
|
459 | + { |
|
460 | + return $this->status() === FormStatus::ARCHIVED; |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * Whether the section is a default one. |
|
465 | + * |
|
466 | + * @return boolean TRUE if is default, FALSE if not. |
|
467 | + * @throws ReflectionException |
|
468 | + * @throws InvalidArgumentException |
|
469 | + * @throws InvalidInterfaceException |
|
470 | + * @throws InvalidDataTypeException |
|
471 | + * @throws EE_Error |
|
472 | + */ |
|
473 | + public function isDefault(): bool |
|
474 | + { |
|
475 | + return $this->status() === FormStatus::DEFAULT; |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Whether the section is a shared one. |
|
480 | + * |
|
481 | + * @return boolean TRUE if is shared, FALSE if not. |
|
482 | + * @throws ReflectionException |
|
483 | + * @throws InvalidArgumentException |
|
484 | + * @throws InvalidInterfaceException |
|
485 | + * @throws InvalidDataTypeException |
|
486 | + * @throws EE_Error |
|
487 | + */ |
|
488 | + public function isShared(): bool |
|
489 | + { |
|
490 | + return $this->status() === FormStatus::SHARED; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * Whether the section is trashed. |
|
495 | + * |
|
496 | + * @return boolean TRUE if is trashed, FALSE if not. |
|
497 | + * @throws ReflectionException |
|
498 | + * @throws InvalidArgumentException |
|
499 | + * @throws InvalidInterfaceException |
|
500 | + * @throws InvalidDataTypeException |
|
501 | + * @throws EE_Error |
|
502 | + */ |
|
503 | + public function isTrashed(): bool |
|
504 | + { |
|
505 | + return $this->status() === FormStatus::TRASHED; |
|
506 | + } |
|
507 | + |
|
508 | + |
|
509 | + /** |
|
510 | + * @return bool |
|
511 | + * @throws EE_Error |
|
512 | + * @throws ReflectionException |
|
513 | + */ |
|
514 | + public function isTopLevelFormSection(): bool |
|
515 | + { |
|
516 | + return empty($this->belongsTo()); |
|
517 | + } |
|
518 | 518 | } |
@@ -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; |
@@ -20,56 +20,56 @@ |
||
20 | 20 | */ |
21 | 21 | class DefaultDatetimes implements DefaultEntityGeneratorInterface |
22 | 22 | { |
23 | - /** |
|
24 | - * @var DefaultTickets $default_tickets |
|
25 | - */ |
|
26 | - protected $default_tickets; |
|
23 | + /** |
|
24 | + * @var DefaultTickets $default_tickets |
|
25 | + */ |
|
26 | + protected $default_tickets; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @var EEM_Datetime $datetime_model |
|
30 | - */ |
|
31 | - protected $datetime_model; |
|
28 | + /** |
|
29 | + * @var EEM_Datetime $datetime_model |
|
30 | + */ |
|
31 | + protected $datetime_model; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param DefaultTickets $default_tickets |
|
35 | - * @param EEM_Datetime $datetime_model |
|
36 | - */ |
|
37 | - public function __construct(DefaultTickets $default_tickets, EEM_Datetime $datetime_model) |
|
38 | - { |
|
39 | - $this->default_tickets = $default_tickets; |
|
40 | - $this->datetime_model = $datetime_model; |
|
41 | - } |
|
33 | + /** |
|
34 | + * @param DefaultTickets $default_tickets |
|
35 | + * @param EEM_Datetime $datetime_model |
|
36 | + */ |
|
37 | + public function __construct(DefaultTickets $default_tickets, EEM_Datetime $datetime_model) |
|
38 | + { |
|
39 | + $this->default_tickets = $default_tickets; |
|
40 | + $this->datetime_model = $datetime_model; |
|
41 | + } |
|
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * @param EE_Event|EE_Base_Class $entity |
|
46 | - * @return EE_Datetime[] |
|
47 | - * @throws EE_Error |
|
48 | - * @throws InvalidEntityException |
|
49 | - * @throws ReflectionException |
|
50 | - * @since 5.0.0.p |
|
51 | - */ |
|
52 | - public function create(EE_Base_Class $entity): array |
|
53 | - { |
|
54 | - if (! $entity instanceof EE_Event) { |
|
55 | - throw new InvalidEntityException($entity, 'EE_Event'); |
|
56 | - } |
|
57 | - $default_dates = []; |
|
58 | - $blank_dates = $this->datetime_model->create_new_blank_datetime(); |
|
59 | - if (is_array($blank_dates)) { |
|
60 | - foreach ($blank_dates as $blank_date) { |
|
61 | - if (! $blank_date instanceof EE_Datetime) { |
|
62 | - throw new InvalidEntityException($blank_date, 'EE_Datetime'); |
|
63 | - } |
|
64 | - // clone date, strip out ID, then save to get a new ID |
|
65 | - $default_date = clone $blank_date; |
|
66 | - $default_date->set('DTT_ID', null); |
|
67 | - $default_date->save(); |
|
68 | - $default_date->_add_relation_to($entity->ID(), 'Event'); |
|
69 | - $this->default_tickets->create($default_date); |
|
70 | - $default_dates[ $default_date->ID() ] = $default_date; |
|
71 | - } |
|
72 | - } |
|
73 | - return $default_dates; |
|
74 | - } |
|
44 | + /** |
|
45 | + * @param EE_Event|EE_Base_Class $entity |
|
46 | + * @return EE_Datetime[] |
|
47 | + * @throws EE_Error |
|
48 | + * @throws InvalidEntityException |
|
49 | + * @throws ReflectionException |
|
50 | + * @since 5.0.0.p |
|
51 | + */ |
|
52 | + public function create(EE_Base_Class $entity): array |
|
53 | + { |
|
54 | + if (! $entity instanceof EE_Event) { |
|
55 | + throw new InvalidEntityException($entity, 'EE_Event'); |
|
56 | + } |
|
57 | + $default_dates = []; |
|
58 | + $blank_dates = $this->datetime_model->create_new_blank_datetime(); |
|
59 | + if (is_array($blank_dates)) { |
|
60 | + foreach ($blank_dates as $blank_date) { |
|
61 | + if (! $blank_date instanceof EE_Datetime) { |
|
62 | + throw new InvalidEntityException($blank_date, 'EE_Datetime'); |
|
63 | + } |
|
64 | + // clone date, strip out ID, then save to get a new ID |
|
65 | + $default_date = clone $blank_date; |
|
66 | + $default_date->set('DTT_ID', null); |
|
67 | + $default_date->save(); |
|
68 | + $default_date->_add_relation_to($entity->ID(), 'Event'); |
|
69 | + $this->default_tickets->create($default_date); |
|
70 | + $default_dates[ $default_date->ID() ] = $default_date; |
|
71 | + } |
|
72 | + } |
|
73 | + return $default_dates; |
|
74 | + } |
|
75 | 75 | } |
@@ -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 | } |
@@ -24,97 +24,97 @@ |
||
24 | 24 | */ |
25 | 25 | class DefaultFormSections implements DefaultEntityGeneratorInterface |
26 | 26 | { |
27 | - /** |
|
28 | - * @var EEM_Form_Element $form_element_model |
|
29 | - */ |
|
30 | - protected $form_element_model; |
|
27 | + /** |
|
28 | + * @var EEM_Form_Element $form_element_model |
|
29 | + */ |
|
30 | + protected $form_element_model; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @var EEM_Form_Section $form_section_model |
|
34 | - */ |
|
35 | - protected $form_section_model; |
|
32 | + /** |
|
33 | + * @var EEM_Form_Section $form_section_model |
|
34 | + */ |
|
35 | + protected $form_section_model; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param EEM_Form_Element $form_element_model |
|
39 | - * @param EEM_Form_Section $form_section_model |
|
40 | - */ |
|
41 | - public function __construct(EEM_Form_Element $form_element_model, EEM_Form_Section $form_section_model) |
|
42 | - { |
|
43 | - $this->form_element_model = $form_element_model; |
|
44 | - $this->form_section_model = $form_section_model; |
|
45 | - } |
|
37 | + /** |
|
38 | + * @param EEM_Form_Element $form_element_model |
|
39 | + * @param EEM_Form_Section $form_section_model |
|
40 | + */ |
|
41 | + public function __construct(EEM_Form_Element $form_element_model, EEM_Form_Section $form_section_model) |
|
42 | + { |
|
43 | + $this->form_element_model = $form_element_model; |
|
44 | + $this->form_section_model = $form_section_model; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | - /** |
|
49 | - * @param EE_Event|EE_Base_Class $entity |
|
50 | - * @return EE_Form_Section[] |
|
51 | - * @throws EE_Error |
|
52 | - * @throws InvalidEntityException |
|
53 | - * @throws ReflectionException |
|
54 | - */ |
|
55 | - public function create(EE_Base_Class $entity): array |
|
56 | - { |
|
57 | - if (! $entity instanceof EE_Event) { |
|
58 | - throw new InvalidEntityException($entity, 'EE_Event'); |
|
59 | - } |
|
60 | - /** @var EE_Form_Section[] $new_form_sections */ |
|
61 | - $new_form_sections = []; |
|
62 | - $default_form_sections = $this->form_section_model->getDefaultFormSections(); |
|
63 | - if (is_array($default_form_sections)) { |
|
64 | - foreach ($default_form_sections as $default_form_section) { |
|
65 | - if (! $default_form_section instanceof EE_Form_Section) { |
|
66 | - throw new InvalidEntityException($default_form_section, 'EE_Form_Section'); |
|
67 | - } |
|
68 | - // we're calling this inside the loop, because this might get set the first time around |
|
69 | - // and it would always be blank for a new event if we were to call it before the loop, |
|
70 | - // and then we couldn't set the "BelongsTo" for any child form sections |
|
71 | - $top_level_form_section = $entity->registrationFormUuid(); |
|
72 | - $default_form_elements = $default_form_section->formElements(); |
|
73 | - // clone form_section, generate a new UUID, reset the status, then save it |
|
74 | - $new_form_section = clone $default_form_section; |
|
75 | - $UUID = Cuid::cuid(); |
|
76 | - $new_form_section->setUUID($UUID); |
|
77 | - $new_form_section->setStatus(FormStatus::ACTIVE); |
|
78 | - if ($top_level_form_section) { |
|
79 | - $new_form_section->setBelongsTo($top_level_form_section); |
|
80 | - } |
|
81 | - $new_form_section->save(); |
|
82 | - $new_form_sections[ $UUID ] = $new_form_section; |
|
83 | - // now retrieve, clone, and save all of the form elements |
|
84 | - $this->createDefaultFormElements($new_form_section, $default_form_elements); |
|
85 | - // save form section UUID on event if it is the top-level form section |
|
86 | - if ($new_form_section->isTopLevelFormSection()) { |
|
87 | - $entity->setRegistrationFormUuid($UUID); |
|
88 | - $entity->save(); |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - return $new_form_sections; |
|
93 | - } |
|
48 | + /** |
|
49 | + * @param EE_Event|EE_Base_Class $entity |
|
50 | + * @return EE_Form_Section[] |
|
51 | + * @throws EE_Error |
|
52 | + * @throws InvalidEntityException |
|
53 | + * @throws ReflectionException |
|
54 | + */ |
|
55 | + public function create(EE_Base_Class $entity): array |
|
56 | + { |
|
57 | + if (! $entity instanceof EE_Event) { |
|
58 | + throw new InvalidEntityException($entity, 'EE_Event'); |
|
59 | + } |
|
60 | + /** @var EE_Form_Section[] $new_form_sections */ |
|
61 | + $new_form_sections = []; |
|
62 | + $default_form_sections = $this->form_section_model->getDefaultFormSections(); |
|
63 | + if (is_array($default_form_sections)) { |
|
64 | + foreach ($default_form_sections as $default_form_section) { |
|
65 | + if (! $default_form_section instanceof EE_Form_Section) { |
|
66 | + throw new InvalidEntityException($default_form_section, 'EE_Form_Section'); |
|
67 | + } |
|
68 | + // we're calling this inside the loop, because this might get set the first time around |
|
69 | + // and it would always be blank for a new event if we were to call it before the loop, |
|
70 | + // and then we couldn't set the "BelongsTo" for any child form sections |
|
71 | + $top_level_form_section = $entity->registrationFormUuid(); |
|
72 | + $default_form_elements = $default_form_section->formElements(); |
|
73 | + // clone form_section, generate a new UUID, reset the status, then save it |
|
74 | + $new_form_section = clone $default_form_section; |
|
75 | + $UUID = Cuid::cuid(); |
|
76 | + $new_form_section->setUUID($UUID); |
|
77 | + $new_form_section->setStatus(FormStatus::ACTIVE); |
|
78 | + if ($top_level_form_section) { |
|
79 | + $new_form_section->setBelongsTo($top_level_form_section); |
|
80 | + } |
|
81 | + $new_form_section->save(); |
|
82 | + $new_form_sections[ $UUID ] = $new_form_section; |
|
83 | + // now retrieve, clone, and save all of the form elements |
|
84 | + $this->createDefaultFormElements($new_form_section, $default_form_elements); |
|
85 | + // save form section UUID on event if it is the top-level form section |
|
86 | + if ($new_form_section->isTopLevelFormSection()) { |
|
87 | + $entity->setRegistrationFormUuid($UUID); |
|
88 | + $entity->save(); |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + return $new_form_sections; |
|
93 | + } |
|
94 | 94 | |
95 | 95 | |
96 | - /** |
|
97 | - * @throws EE_Error |
|
98 | - * @throws ReflectionException |
|
99 | - */ |
|
100 | - private function createDefaultFormElements(EE_Form_Section $new_form_section, array $default_form_elements) |
|
101 | - { |
|
102 | - /** @var EE_Form_Element[] $new_form_sections */ |
|
103 | - $new_form_elements = []; |
|
104 | - foreach ($default_form_elements as $default_form_element) { |
|
105 | - if (! $default_form_element instanceof EE_Form_Element) { |
|
106 | - throw new InvalidEntityException($default_form_element, 'EE_Form_Element'); |
|
107 | - } |
|
108 | - // clone form_element, generate a new UUID, reset the status, then save it |
|
109 | - $new_form_element = clone $default_form_element; |
|
110 | - // generate a new UUID for this form section then save it |
|
111 | - $UUID = Cuid::cuid(); |
|
112 | - $new_form_element->setUUID($UUID); |
|
113 | - $new_form_element->setBelongsTo($new_form_section->UUID()); |
|
114 | - $new_form_element->setStatus(FormStatus::ACTIVE); |
|
115 | - $new_form_element->save(); |
|
116 | - $new_form_elements[ $UUID ] = $new_form_element; |
|
117 | - } |
|
118 | - $new_form_section->setFormElements($new_form_elements); |
|
119 | - } |
|
96 | + /** |
|
97 | + * @throws EE_Error |
|
98 | + * @throws ReflectionException |
|
99 | + */ |
|
100 | + private function createDefaultFormElements(EE_Form_Section $new_form_section, array $default_form_elements) |
|
101 | + { |
|
102 | + /** @var EE_Form_Element[] $new_form_sections */ |
|
103 | + $new_form_elements = []; |
|
104 | + foreach ($default_form_elements as $default_form_element) { |
|
105 | + if (! $default_form_element instanceof EE_Form_Element) { |
|
106 | + throw new InvalidEntityException($default_form_element, 'EE_Form_Element'); |
|
107 | + } |
|
108 | + // clone form_element, generate a new UUID, reset the status, then save it |
|
109 | + $new_form_element = clone $default_form_element; |
|
110 | + // generate a new UUID for this form section then save it |
|
111 | + $UUID = Cuid::cuid(); |
|
112 | + $new_form_element->setUUID($UUID); |
|
113 | + $new_form_element->setBelongsTo($new_form_section->UUID()); |
|
114 | + $new_form_element->setStatus(FormStatus::ACTIVE); |
|
115 | + $new_form_element->save(); |
|
116 | + $new_form_elements[ $UUID ] = $new_form_element; |
|
117 | + } |
|
118 | + $new_form_section->setFormElements($new_form_elements); |
|
119 | + } |
|
120 | 120 | } |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | $UUID = $section->UUID(); |
56 | 56 | |
57 | 57 | // Avoid duplicates, if any |
58 | - $sections[ $UUID ] = [ |
|
58 | + $sections[$UUID] = [ |
|
59 | 59 | 'id' => $UUID, |
60 | 60 | 'appliesTo' => GQLUtils::formatEnumKey($section->appliesTo()), |
61 | 61 | 'attributes' => $section->attributes()->toJson(), |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | $UUID = $element->UUID(); |
86 | 86 | |
87 | 87 | // Avoid duplicates |
88 | - $elements[ $UUID ] = [ |
|
88 | + $elements[$UUID] = [ |
|
89 | 89 | 'id' => $UUID, |
90 | 90 | 'adminOnly' => $element->adminOnly(), |
91 | 91 | 'attributes' => $element->attributes()->toJson(), |
@@ -23,87 +23,87 @@ |
||
23 | 23 | */ |
24 | 24 | class FormBuilder implements EventEditorDataInterface |
25 | 25 | { |
26 | - /** |
|
27 | - * @param int $eventId |
|
28 | - * @return array |
|
29 | - * @throws EE_Error |
|
30 | - * @throws ReflectionException |
|
31 | - */ |
|
32 | - public function getData(int $eventId): array |
|
33 | - { |
|
34 | - /** @var EE_Event $event */ |
|
35 | - $event = EEM_Event::instance()->get_one_by_ID($eventId); |
|
36 | - $topLevelSectionId = $event->registrationFormUuid(); |
|
26 | + /** |
|
27 | + * @param int $eventId |
|
28 | + * @return array |
|
29 | + * @throws EE_Error |
|
30 | + * @throws ReflectionException |
|
31 | + */ |
|
32 | + public function getData(int $eventId): array |
|
33 | + { |
|
34 | + /** @var EE_Event $event */ |
|
35 | + $event = EEM_Event::instance()->get_one_by_ID($eventId); |
|
36 | + $topLevelSectionId = $event->registrationFormUuid(); |
|
37 | 37 | |
38 | - /** @var EE_Form_Section[] $form_sections */ |
|
39 | - $form_sections = EEM_Form_Section::instance()->get_all([ |
|
40 | - [ |
|
41 | - 'OR' => [ |
|
42 | - 'FSC_UUID' => $topLevelSectionId, // top level form |
|
43 | - 'FSC_belongsTo' => $topLevelSectionId, // child form sections |
|
44 | - 'FSC_status' => FormStatus::SHARED, // shared form sections |
|
45 | - ] |
|
46 | - ], |
|
47 | - 'order_by' => ['FSC_order' => 'ASC'], |
|
48 | - ]); |
|
38 | + /** @var EE_Form_Section[] $form_sections */ |
|
39 | + $form_sections = EEM_Form_Section::instance()->get_all([ |
|
40 | + [ |
|
41 | + 'OR' => [ |
|
42 | + 'FSC_UUID' => $topLevelSectionId, // top level form |
|
43 | + 'FSC_belongsTo' => $topLevelSectionId, // child form sections |
|
44 | + 'FSC_status' => FormStatus::SHARED, // shared form sections |
|
45 | + ] |
|
46 | + ], |
|
47 | + 'order_by' => ['FSC_order' => 'ASC'], |
|
48 | + ]); |
|
49 | 49 | |
50 | - $sections = []; |
|
51 | - foreach ($form_sections as $section) { |
|
52 | - if ($section instanceof EE_Form_Section) { |
|
53 | - $UUID = $section->UUID(); |
|
50 | + $sections = []; |
|
51 | + foreach ($form_sections as $section) { |
|
52 | + if ($section instanceof EE_Form_Section) { |
|
53 | + $UUID = $section->UUID(); |
|
54 | 54 | |
55 | - // Avoid duplicates, if any |
|
56 | - $sections[ $UUID ] = [ |
|
57 | - 'id' => $UUID, |
|
58 | - 'appliesTo' => GQLUtils::formatEnumKey($section->appliesTo()), |
|
59 | - 'attributes' => $section->attributes()->toJson(), |
|
60 | - 'belongsTo' => $section->belongsTo(), |
|
61 | - 'isActive' => $section->isActive(), |
|
62 | - 'isArchived' => $section->isArchived(), |
|
63 | - 'isDefault' => $section->isDefault(), |
|
64 | - 'isShared' => $section->isShared(), |
|
65 | - 'isTrashed' => $section->isTrashed(), |
|
66 | - 'label' => $section->label()->toJson(), |
|
67 | - 'order' => $section->order(), |
|
68 | - 'status' => GQLUtils::formatEnumKey($section->status()), |
|
69 | - ]; |
|
70 | - } |
|
71 | - } |
|
55 | + // Avoid duplicates, if any |
|
56 | + $sections[ $UUID ] = [ |
|
57 | + 'id' => $UUID, |
|
58 | + 'appliesTo' => GQLUtils::formatEnumKey($section->appliesTo()), |
|
59 | + 'attributes' => $section->attributes()->toJson(), |
|
60 | + 'belongsTo' => $section->belongsTo(), |
|
61 | + 'isActive' => $section->isActive(), |
|
62 | + 'isArchived' => $section->isArchived(), |
|
63 | + 'isDefault' => $section->isDefault(), |
|
64 | + 'isShared' => $section->isShared(), |
|
65 | + 'isTrashed' => $section->isTrashed(), |
|
66 | + 'label' => $section->label()->toJson(), |
|
67 | + 'order' => $section->order(), |
|
68 | + 'status' => GQLUtils::formatEnumKey($section->status()), |
|
69 | + ]; |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - /** @var EE_Form_Element[] $form_elements */ |
|
74 | - $form_elements = EEM_Form_Element::instance()->get_all([ |
|
75 | - [ |
|
76 | - 'FSC_UUID' => ['IN', array_keys($sections)] |
|
77 | - ] |
|
78 | - ]); |
|
73 | + /** @var EE_Form_Element[] $form_elements */ |
|
74 | + $form_elements = EEM_Form_Element::instance()->get_all([ |
|
75 | + [ |
|
76 | + 'FSC_UUID' => ['IN', array_keys($sections)] |
|
77 | + ] |
|
78 | + ]); |
|
79 | 79 | |
80 | - $elements = []; |
|
81 | - foreach ($form_elements as $element) { |
|
82 | - if ($element instanceof EE_Form_Element) { |
|
83 | - $UUID = $element->UUID(); |
|
80 | + $elements = []; |
|
81 | + foreach ($form_elements as $element) { |
|
82 | + if ($element instanceof EE_Form_Element) { |
|
83 | + $UUID = $element->UUID(); |
|
84 | 84 | |
85 | - // Avoid duplicates |
|
86 | - $elements[ $UUID ] = [ |
|
87 | - 'id' => $UUID, |
|
88 | - 'adminOnly' => $element->adminOnly(), |
|
89 | - 'attributes' => $element->attributes()->toJson(), |
|
90 | - 'belongsTo' => $element->belongsTo(), |
|
91 | - 'helpText' => $element->helpText()->toJson(), |
|
92 | - 'label' => $element->label()->toJson(), |
|
93 | - 'mapsTo' => $element->mapsTo(), |
|
94 | - 'options' => $element->options()->toJson(), |
|
95 | - 'order' => $element->order(), |
|
96 | - 'required' => $element->required()->toJson(), |
|
97 | - 'status' => GQLUtils::formatEnumKey($element->status()), |
|
98 | - 'type' => GQLUtils::formatEnumKey($element->type()), |
|
99 | - ]; |
|
100 | - } |
|
101 | - } |
|
85 | + // Avoid duplicates |
|
86 | + $elements[ $UUID ] = [ |
|
87 | + 'id' => $UUID, |
|
88 | + 'adminOnly' => $element->adminOnly(), |
|
89 | + 'attributes' => $element->attributes()->toJson(), |
|
90 | + 'belongsTo' => $element->belongsTo(), |
|
91 | + 'helpText' => $element->helpText()->toJson(), |
|
92 | + 'label' => $element->label()->toJson(), |
|
93 | + 'mapsTo' => $element->mapsTo(), |
|
94 | + 'options' => $element->options()->toJson(), |
|
95 | + 'order' => $element->order(), |
|
96 | + 'required' => $element->required()->toJson(), |
|
97 | + 'status' => GQLUtils::formatEnumKey($element->status()), |
|
98 | + 'type' => GQLUtils::formatEnumKey($element->type()), |
|
99 | + ]; |
|
100 | + } |
|
101 | + } |
|
102 | 102 | |
103 | - return [ |
|
104 | - 'elements' => array_values($elements), |
|
105 | - 'sections' => array_values($sections), |
|
106 | - 'topLevelSectionId' => $event->registrationFormUuid() |
|
107 | - ]; |
|
108 | - } |
|
103 | + return [ |
|
104 | + 'elements' => array_values($elements), |
|
105 | + 'sections' => array_values($sections), |
|
106 | + 'topLevelSectionId' => $event->registrationFormUuid() |
|
107 | + ]; |
|
108 | + } |
|
109 | 109 | } |
@@ -4,61 +4,61 @@ |
||
4 | 4 | |
5 | 5 | class Text |
6 | 6 | { |
7 | - /** |
|
8 | - * indicates that the HTML input type is 'email' |
|
9 | - */ |
|
10 | - public const TYPE_EMAIL = 'email'; |
|
7 | + /** |
|
8 | + * indicates that the HTML input type is 'email' |
|
9 | + */ |
|
10 | + public const TYPE_EMAIL = 'email'; |
|
11 | 11 | |
12 | - /** |
|
13 | - * indicates that the input is used to confirm an email address |
|
14 | - */ |
|
15 | - public const TYPE_EMAIL_CONFIRMATION = 'email-confirmation'; |
|
12 | + /** |
|
13 | + * indicates that the input is used to confirm an email address |
|
14 | + */ |
|
15 | + public const TYPE_EMAIL_CONFIRMATION = 'email-confirmation'; |
|
16 | 16 | |
17 | - /** |
|
18 | - * indicates that the HTML input type is 'text' |
|
19 | - */ |
|
20 | - public const TYPE_TEXT = 'text'; |
|
17 | + /** |
|
18 | + * indicates that the HTML input type is 'text' |
|
19 | + */ |
|
20 | + public const TYPE_TEXT = 'text'; |
|
21 | 21 | |
22 | - /** |
|
23 | - * indicates that the input is a TEXTAREA that only allows plain text |
|
24 | - */ |
|
25 | - public const TYPE_TEXTAREA = 'textarea'; |
|
22 | + /** |
|
23 | + * indicates that the input is a TEXTAREA that only allows plain text |
|
24 | + */ |
|
25 | + public const TYPE_TEXTAREA = 'textarea'; |
|
26 | 26 | |
27 | - /** |
|
28 | - * indicates that the input is a TEXTAREA that allows simple html |
|
29 | - */ |
|
30 | - public const TYPE_TEXTAREA_HTML = 'textarea-html'; |
|
27 | + /** |
|
28 | + * indicates that the input is a TEXTAREA that allows simple html |
|
29 | + */ |
|
30 | + public const TYPE_TEXTAREA_HTML = 'textarea-html'; |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * @var array |
|
35 | - */ |
|
36 | - private $valid_type_options; |
|
33 | + /** |
|
34 | + * @var array |
|
35 | + */ |
|
36 | + private $valid_type_options; |
|
37 | 37 | |
38 | 38 | |
39 | - public function __construct() |
|
40 | - { |
|
41 | - $this->valid_type_options = apply_filters( |
|
42 | - 'FHEE__EventEspresso_core_services_form_meta_inputs_Text__valid_type_options', |
|
43 | - [ |
|
44 | - Text::TYPE_EMAIL => esc_html__('Email', 'event_espresso'), |
|
45 | - Text::TYPE_EMAIL_CONFIRMATION => esc_html__('Email Confirmation', 'event_espresso'), |
|
46 | - Text::TYPE_TEXT => esc_html__('Plain Text', 'event_espresso'), |
|
47 | - Text::TYPE_TEXTAREA => esc_html__('Plain Textarea', 'event_espresso'), |
|
48 | - Text::TYPE_TEXTAREA_HTML => esc_html__('Simple HTML Textarea', 'event_espresso'), |
|
49 | - ] |
|
50 | - ); |
|
51 | - } |
|
39 | + public function __construct() |
|
40 | + { |
|
41 | + $this->valid_type_options = apply_filters( |
|
42 | + 'FHEE__EventEspresso_core_services_form_meta_inputs_Text__valid_type_options', |
|
43 | + [ |
|
44 | + Text::TYPE_EMAIL => esc_html__('Email', 'event_espresso'), |
|
45 | + Text::TYPE_EMAIL_CONFIRMATION => esc_html__('Email Confirmation', 'event_espresso'), |
|
46 | + Text::TYPE_TEXT => esc_html__('Plain Text', 'event_espresso'), |
|
47 | + Text::TYPE_TEXTAREA => esc_html__('Plain Textarea', 'event_espresso'), |
|
48 | + Text::TYPE_TEXTAREA_HTML => esc_html__('Simple HTML Textarea', 'event_espresso'), |
|
49 | + ] |
|
50 | + ); |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * @param bool $constants_only |
|
56 | - * @return array |
|
57 | - */ |
|
58 | - public function validTypeOptions(bool $constants_only = false): array |
|
59 | - { |
|
60 | - return $constants_only |
|
61 | - ? array_keys($this->valid_type_options) |
|
62 | - : $this->valid_type_options; |
|
63 | - } |
|
54 | + /** |
|
55 | + * @param bool $constants_only |
|
56 | + * @return array |
|
57 | + */ |
|
58 | + public function validTypeOptions(bool $constants_only = false): array |
|
59 | + { |
|
60 | + return $constants_only |
|
61 | + ? array_keys($this->valid_type_options) |
|
62 | + : $this->valid_type_options; |
|
63 | + } |
|
64 | 64 | } |
@@ -5,90 +5,90 @@ |
||
5 | 5 | class Input |
6 | 6 | { |
7 | 7 | |
8 | - /** |
|
9 | - * indicates that the HTML input type is 'checkbox' |
|
10 | - */ |
|
11 | - public const TYPE_CHECKBOX = 'checkbox'; |
|
12 | - |
|
13 | - /** |
|
14 | - * indicates that the HTML input type is 'multi checkbox' |
|
15 | - */ |
|
16 | - public const TYPE_CHECKBOX_MULTI = 'checkbox-multi'; |
|
17 | - |
|
18 | - /** |
|
19 | - * indicates that the HTML input type is 'color' |
|
20 | - */ |
|
21 | - public const TYPE_COLOR = 'color'; |
|
22 | - |
|
23 | - /** |
|
24 | - * indicates that the HTML input type is 'file' |
|
25 | - */ |
|
26 | - public const TYPE_FILE = 'file'; |
|
27 | - |
|
28 | - /** |
|
29 | - * indicates that the HTML input type is 'hidden' |
|
30 | - */ |
|
31 | - public const TYPE_HIDDEN = 'hidden'; |
|
32 | - |
|
33 | - /** |
|
34 | - * indicates that the HTML input type is 'image' |
|
35 | - */ |
|
36 | - public const TYPE_IMAGE = 'image'; |
|
37 | - |
|
38 | - /** |
|
39 | - * indicates that the HTML input type is 'password' |
|
40 | - */ |
|
41 | - public const TYPE_PASSWORD = 'password'; |
|
42 | - |
|
43 | - /** |
|
44 | - * indicates that the input is used to confirm a password |
|
45 | - */ |
|
46 | - public const TYPE_PASSWORD_CONFIRMATION = 'password-confirmation'; |
|
47 | - |
|
48 | - /** |
|
49 | - * indicates that the HTML input type is 'radio' |
|
50 | - */ |
|
51 | - public const TYPE_RADIO = 'radio'; |
|
52 | - |
|
53 | - /** |
|
54 | - * indicates that the HTML input type is 'url' |
|
55 | - */ |
|
56 | - public const TYPE_URL = 'url'; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - private $valid_type_options; |
|
62 | - |
|
63 | - |
|
64 | - public function __construct() |
|
65 | - { |
|
66 | - $this->valid_type_options = apply_filters( |
|
67 | - 'FHEE__EventEspresso_core_services_form_meta_inputs_Input__valid_type_options', |
|
68 | - [ |
|
69 | - Input::TYPE_CHECKBOX => esc_html__('Checkbox', 'event_espresso'), |
|
70 | - Input::TYPE_CHECKBOX_MULTI => esc_html__('Multi Checkbox', 'event_espresso'), |
|
71 | - Input::TYPE_COLOR => esc_html__('Color Picker', 'event_espresso'), |
|
72 | - Input::TYPE_FILE => esc_html__('File Upload', 'event_espresso'), |
|
73 | - Input::TYPE_HIDDEN => esc_html__('Hidden', 'event_espresso'), |
|
74 | - Input::TYPE_IMAGE => esc_html__('Image Upload', 'event_espresso'), |
|
75 | - Input::TYPE_PASSWORD => esc_html__('Password', 'event_espresso'), |
|
76 | - Input::TYPE_PASSWORD_CONFIRMATION => esc_html__('Password Confirmation', 'event_espresso'), |
|
77 | - Input::TYPE_RADIO => esc_html__('Radio Button', 'event_espresso'), |
|
78 | - Input::TYPE_URL => esc_html__('URL', 'event_espresso'), |
|
79 | - ] |
|
80 | - ); |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * @param bool $constants_only |
|
86 | - * @return array |
|
87 | - */ |
|
88 | - public function validTypeOptions(bool $constants_only = false): array |
|
89 | - { |
|
90 | - return $constants_only |
|
91 | - ? array_keys($this->valid_type_options) |
|
92 | - : $this->valid_type_options; |
|
93 | - } |
|
8 | + /** |
|
9 | + * indicates that the HTML input type is 'checkbox' |
|
10 | + */ |
|
11 | + public const TYPE_CHECKBOX = 'checkbox'; |
|
12 | + |
|
13 | + /** |
|
14 | + * indicates that the HTML input type is 'multi checkbox' |
|
15 | + */ |
|
16 | + public const TYPE_CHECKBOX_MULTI = 'checkbox-multi'; |
|
17 | + |
|
18 | + /** |
|
19 | + * indicates that the HTML input type is 'color' |
|
20 | + */ |
|
21 | + public const TYPE_COLOR = 'color'; |
|
22 | + |
|
23 | + /** |
|
24 | + * indicates that the HTML input type is 'file' |
|
25 | + */ |
|
26 | + public const TYPE_FILE = 'file'; |
|
27 | + |
|
28 | + /** |
|
29 | + * indicates that the HTML input type is 'hidden' |
|
30 | + */ |
|
31 | + public const TYPE_HIDDEN = 'hidden'; |
|
32 | + |
|
33 | + /** |
|
34 | + * indicates that the HTML input type is 'image' |
|
35 | + */ |
|
36 | + public const TYPE_IMAGE = 'image'; |
|
37 | + |
|
38 | + /** |
|
39 | + * indicates that the HTML input type is 'password' |
|
40 | + */ |
|
41 | + public const TYPE_PASSWORD = 'password'; |
|
42 | + |
|
43 | + /** |
|
44 | + * indicates that the input is used to confirm a password |
|
45 | + */ |
|
46 | + public const TYPE_PASSWORD_CONFIRMATION = 'password-confirmation'; |
|
47 | + |
|
48 | + /** |
|
49 | + * indicates that the HTML input type is 'radio' |
|
50 | + */ |
|
51 | + public const TYPE_RADIO = 'radio'; |
|
52 | + |
|
53 | + /** |
|
54 | + * indicates that the HTML input type is 'url' |
|
55 | + */ |
|
56 | + public const TYPE_URL = 'url'; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + private $valid_type_options; |
|
62 | + |
|
63 | + |
|
64 | + public function __construct() |
|
65 | + { |
|
66 | + $this->valid_type_options = apply_filters( |
|
67 | + 'FHEE__EventEspresso_core_services_form_meta_inputs_Input__valid_type_options', |
|
68 | + [ |
|
69 | + Input::TYPE_CHECKBOX => esc_html__('Checkbox', 'event_espresso'), |
|
70 | + Input::TYPE_CHECKBOX_MULTI => esc_html__('Multi Checkbox', 'event_espresso'), |
|
71 | + Input::TYPE_COLOR => esc_html__('Color Picker', 'event_espresso'), |
|
72 | + Input::TYPE_FILE => esc_html__('File Upload', 'event_espresso'), |
|
73 | + Input::TYPE_HIDDEN => esc_html__('Hidden', 'event_espresso'), |
|
74 | + Input::TYPE_IMAGE => esc_html__('Image Upload', 'event_espresso'), |
|
75 | + Input::TYPE_PASSWORD => esc_html__('Password', 'event_espresso'), |
|
76 | + Input::TYPE_PASSWORD_CONFIRMATION => esc_html__('Password Confirmation', 'event_espresso'), |
|
77 | + Input::TYPE_RADIO => esc_html__('Radio Button', 'event_espresso'), |
|
78 | + Input::TYPE_URL => esc_html__('URL', 'event_espresso'), |
|
79 | + ] |
|
80 | + ); |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * @param bool $constants_only |
|
86 | + * @return array |
|
87 | + */ |
|
88 | + public function validTypeOptions(bool $constants_only = false): array |
|
89 | + { |
|
90 | + return $constants_only |
|
91 | + ? array_keys($this->valid_type_options) |
|
92 | + : $this->valid_type_options; |
|
93 | + } |
|
94 | 94 | } |
@@ -75,11 +75,11 @@ discard block |
||
75 | 75 | $this->setDisplayStrategy($display_strategy_name); |
76 | 76 | $this->setDefaultStep($default_step); |
77 | 77 | $this->setFormStepUrlKey($form_step_url_key); |
78 | - if (! $collection instanceof CollectionInterface) { |
|
78 | + if ( ! $collection instanceof CollectionInterface) { |
|
79 | 79 | $collection = new Collection('\EventEspresso\core\services\progress_steps\ProgressStepInterface'); |
80 | 80 | } |
81 | 81 | $this->collection = $collection; |
82 | - if (! $request instanceof RequestInterface) { |
|
82 | + if ( ! $request instanceof RequestInterface) { |
|
83 | 83 | /** @var RequestInterface $request */ |
84 | 84 | $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
85 | 85 | } |
@@ -94,23 +94,23 @@ discard block |
||
94 | 94 | */ |
95 | 95 | protected function setDisplayStrategy($display_strategy_name = 'number_bubbles') |
96 | 96 | { |
97 | - if (! is_string($display_strategy_name)) { |
|
97 | + if ( ! is_string($display_strategy_name)) { |
|
98 | 98 | throw new InvalidDataTypeException('$display_strategy_name', $display_strategy_name, 'string'); |
99 | 99 | } |
100 | 100 | // build up FQCN from incoming display strategy folder name |
101 | 101 | $display_strategy_class = 'EventEspresso\core\services\progress_steps\display_strategies\\'; |
102 | - $display_strategy_class .= $display_strategy_name . '\\'; |
|
102 | + $display_strategy_class .= $display_strategy_name.'\\'; |
|
103 | 103 | $display_strategy_class .= str_replace(' ', '', ucwords(str_replace('_', ' ', $display_strategy_name))); |
104 | 104 | $display_strategy_class .= 'ProgressStepsDisplay'; |
105 | 105 | $display_strategy_class = apply_filters( |
106 | 106 | 'FHEE__ProgressStepManager__setDisplayStrategy__display_strategy_class', |
107 | 107 | $display_strategy_class |
108 | 108 | ); |
109 | - if (! class_exists($display_strategy_class)) { |
|
109 | + if ( ! class_exists($display_strategy_class)) { |
|
110 | 110 | throw new InvalidClassException($display_strategy_class); |
111 | 111 | } |
112 | 112 | $display_strategy = new $display_strategy_class(); |
113 | - if (! $display_strategy instanceof ProgressStepsDisplayInterface) { |
|
113 | + if ( ! $display_strategy instanceof ProgressStepsDisplayInterface) { |
|
114 | 114 | throw new InvalidClassException( |
115 | 115 | $display_strategy_class, |
116 | 116 | sprintf( |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | */ |
131 | 131 | public function setDefaultStep($default_step) |
132 | 132 | { |
133 | - if (! is_string($default_step)) { |
|
133 | + if ( ! is_string($default_step)) { |
|
134 | 134 | throw new InvalidDataTypeException('$default_step', $default_step, 'string'); |
135 | 135 | } |
136 | 136 | $this->default_step = $default_step; |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | */ |
144 | 144 | public function setFormStepUrlKey($form_step_url_key = 'ee-form-step') |
145 | 145 | { |
146 | - if (! is_string($form_step_url_key)) { |
|
146 | + if ( ! is_string($form_step_url_key)) { |
|
147 | 147 | throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string'); |
148 | 148 | } |
149 | 149 | $this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step'; |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | // grab the step previously known as current, in case we need to revert |
164 | 164 | $current_current_step = $this->collection->current(); |
165 | 165 | // verify that requested step exists |
166 | - if (! $this->collection->has($step)) { |
|
166 | + if ( ! $this->collection->has($step)) { |
|
167 | 167 | throw new InvalidIdentifierException($step, $this->default_step); |
168 | 168 | } |
169 | 169 | if ($this->collection->setCurrent($step)) { |
@@ -26,225 +26,225 @@ |
||
26 | 26 | */ |
27 | 27 | class ProgressStepManager |
28 | 28 | { |
29 | - /** |
|
30 | - * @var ProgressStepInterface[] |
|
31 | - */ |
|
32 | - private $collection; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - private $default_step; |
|
38 | - |
|
39 | - /** |
|
40 | - * the key used for the URL param that denotes the current form step |
|
41 | - * defaults to 'ee-form-step' |
|
42 | - * |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - private $form_step_url_key = ''; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var ProgressStepsDisplayInterface |
|
49 | - */ |
|
50 | - private $display_strategy; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var RequestInterface |
|
54 | - */ |
|
55 | - private $request; |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * ProgressStepManager constructor |
|
60 | - * |
|
61 | - * @param string $display_strategy_name |
|
62 | - * @param string $default_step |
|
63 | - * @param string $form_step_url_key |
|
64 | - * @param CollectionInterface $collection |
|
65 | - * @param RequestInterface $request |
|
66 | - */ |
|
67 | - public function __construct( |
|
68 | - $display_strategy_name = 'number_bubbles', |
|
69 | - $default_step = '', |
|
70 | - $form_step_url_key = '', |
|
71 | - CollectionInterface $collection = null, |
|
72 | - RequestInterface $request = null |
|
73 | - ) { |
|
74 | - $this->setDisplayStrategy($display_strategy_name); |
|
75 | - $this->setDefaultStep($default_step); |
|
76 | - $this->setFormStepUrlKey($form_step_url_key); |
|
77 | - if (! $collection instanceof CollectionInterface) { |
|
78 | - $collection = new Collection('\EventEspresso\core\services\progress_steps\ProgressStepInterface'); |
|
79 | - } |
|
80 | - $this->collection = $collection; |
|
81 | - if (! $request instanceof RequestInterface) { |
|
82 | - /** @var RequestInterface $request */ |
|
83 | - $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
|
84 | - } |
|
85 | - $this->request = $request; |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * @param string $display_strategy_name |
|
91 | - * @throws InvalidDataTypeException |
|
92 | - * @throws InvalidClassException |
|
93 | - */ |
|
94 | - protected function setDisplayStrategy($display_strategy_name = 'number_bubbles') |
|
95 | - { |
|
96 | - if (! is_string($display_strategy_name)) { |
|
97 | - throw new InvalidDataTypeException('$display_strategy_name', $display_strategy_name, 'string'); |
|
98 | - } |
|
99 | - // build up FQCN from incoming display strategy folder name |
|
100 | - $display_strategy_class = 'EventEspresso\core\services\progress_steps\display_strategies\\'; |
|
101 | - $display_strategy_class .= $display_strategy_name . '\\'; |
|
102 | - $display_strategy_class .= str_replace(' ', '', ucwords(str_replace('_', ' ', $display_strategy_name))); |
|
103 | - $display_strategy_class .= 'ProgressStepsDisplay'; |
|
104 | - $display_strategy_class = apply_filters( |
|
105 | - 'FHEE__ProgressStepManager__setDisplayStrategy__display_strategy_class', |
|
106 | - $display_strategy_class |
|
107 | - ); |
|
108 | - if (! class_exists($display_strategy_class)) { |
|
109 | - throw new InvalidClassException($display_strategy_class); |
|
110 | - } |
|
111 | - $display_strategy = new $display_strategy_class(); |
|
112 | - if (! $display_strategy instanceof ProgressStepsDisplayInterface) { |
|
113 | - throw new InvalidClassException( |
|
114 | - $display_strategy_class, |
|
115 | - sprintf( |
|
116 | - esc_html__('The "%1$s" Class needs to be an implementation of the "%1$s" Interface.', 'event_espresso'), |
|
117 | - $display_strategy_class, |
|
118 | - '\EventEspresso\core\services\progress_steps\display_strategies\ProgressStepsDisplayInterface' |
|
119 | - ) |
|
120 | - ); |
|
121 | - } |
|
122 | - $this->display_strategy = $display_strategy; |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * @param string $default_step |
|
128 | - * @throws InvalidDataTypeException |
|
129 | - */ |
|
130 | - public function setDefaultStep($default_step) |
|
131 | - { |
|
132 | - if (! is_string($default_step)) { |
|
133 | - throw new InvalidDataTypeException('$default_step', $default_step, 'string'); |
|
134 | - } |
|
135 | - $this->default_step = $default_step; |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * @param string $form_step_url_key |
|
141 | - * @throws InvalidDataTypeException |
|
142 | - */ |
|
143 | - public function setFormStepUrlKey($form_step_url_key = 'ee-form-step') |
|
144 | - { |
|
145 | - if (! is_string($form_step_url_key)) { |
|
146 | - throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string'); |
|
147 | - } |
|
148 | - $this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step'; |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * @param string $step |
|
154 | - * @throws InvalidIdentifierException |
|
155 | - */ |
|
156 | - public function setCurrentStep($step = '') |
|
157 | - { |
|
158 | - // use incoming value if it's set, otherwise use request param if it's set, otherwise use default |
|
159 | - $step = ! empty($step) |
|
160 | - ? $step |
|
161 | - : $this->request->getRequestParam($this->form_step_url_key, $this->default_step); |
|
162 | - // grab the step previously known as current, in case we need to revert |
|
163 | - $current_current_step = $this->collection->current(); |
|
164 | - // verify that requested step exists |
|
165 | - if (! $this->collection->has($step)) { |
|
166 | - throw new InvalidIdentifierException($step, $this->default_step); |
|
167 | - } |
|
168 | - if ($this->collection->setCurrent($step)) { |
|
169 | - // if the old boss is the same as the new boss, then nothing changes |
|
170 | - if ($this->collection->current() !== $current_current_step) { |
|
171 | - $current_current_step->setIsCurrent(false); |
|
172 | - } |
|
173 | - $this->collection->current()->setIsCurrent(); |
|
174 | - } else { |
|
175 | - $this->collection->setCurrent($current_current_step->id()); |
|
176 | - $current_current_step->setIsCurrent(true); |
|
177 | - } |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * setPreviousStepsCompleted |
|
183 | - */ |
|
184 | - public function setPreviousStepsCompleted() |
|
185 | - { |
|
186 | - $current_current_step = $this->collection->current(); |
|
187 | - $this->collection->rewind(); |
|
188 | - while ($this->collection->valid()) { |
|
189 | - if ($this->collection->current() === $current_current_step) { |
|
190 | - break; |
|
191 | - } |
|
192 | - $this->setCurrentStepCompleted(); |
|
193 | - $this->collection->next(); |
|
194 | - } |
|
195 | - $this->collection->setCurrentUsingObject($current_current_step); |
|
196 | - return false; |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * @return ProgressStepInterface |
|
202 | - */ |
|
203 | - public function currentStep() |
|
204 | - { |
|
205 | - return $this->collection->current(); |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - /** |
|
210 | - * @return ProgressStepInterface |
|
211 | - */ |
|
212 | - public function nextStep() |
|
213 | - { |
|
214 | - return $this->collection->next(); |
|
215 | - } |
|
216 | - |
|
217 | - |
|
218 | - /** |
|
219 | - * @return void |
|
220 | - */ |
|
221 | - public function enqueueStylesAndScripts() |
|
222 | - { |
|
223 | - $this->display_strategy->enqueueStylesAndScripts(); |
|
224 | - } |
|
225 | - |
|
226 | - |
|
227 | - /** |
|
228 | - * echos out HTML |
|
229 | - * |
|
230 | - * @return string |
|
231 | - */ |
|
232 | - public function displaySteps() |
|
233 | - { |
|
234 | - return EEH_Template::display_template( |
|
235 | - $this->display_strategy->getTemplate(), |
|
236 | - array('progress_steps' => $this->collection), |
|
237 | - true |
|
238 | - ); |
|
239 | - } |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * @param bool $completed |
|
244 | - * @return ProgressStepInterface |
|
245 | - */ |
|
246 | - public function setCurrentStepCompleted($completed = true) |
|
247 | - { |
|
248 | - return $this->collection->current()->setIsCompleted($completed); |
|
249 | - } |
|
29 | + /** |
|
30 | + * @var ProgressStepInterface[] |
|
31 | + */ |
|
32 | + private $collection; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + private $default_step; |
|
38 | + |
|
39 | + /** |
|
40 | + * the key used for the URL param that denotes the current form step |
|
41 | + * defaults to 'ee-form-step' |
|
42 | + * |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + private $form_step_url_key = ''; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var ProgressStepsDisplayInterface |
|
49 | + */ |
|
50 | + private $display_strategy; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var RequestInterface |
|
54 | + */ |
|
55 | + private $request; |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * ProgressStepManager constructor |
|
60 | + * |
|
61 | + * @param string $display_strategy_name |
|
62 | + * @param string $default_step |
|
63 | + * @param string $form_step_url_key |
|
64 | + * @param CollectionInterface $collection |
|
65 | + * @param RequestInterface $request |
|
66 | + */ |
|
67 | + public function __construct( |
|
68 | + $display_strategy_name = 'number_bubbles', |
|
69 | + $default_step = '', |
|
70 | + $form_step_url_key = '', |
|
71 | + CollectionInterface $collection = null, |
|
72 | + RequestInterface $request = null |
|
73 | + ) { |
|
74 | + $this->setDisplayStrategy($display_strategy_name); |
|
75 | + $this->setDefaultStep($default_step); |
|
76 | + $this->setFormStepUrlKey($form_step_url_key); |
|
77 | + if (! $collection instanceof CollectionInterface) { |
|
78 | + $collection = new Collection('\EventEspresso\core\services\progress_steps\ProgressStepInterface'); |
|
79 | + } |
|
80 | + $this->collection = $collection; |
|
81 | + if (! $request instanceof RequestInterface) { |
|
82 | + /** @var RequestInterface $request */ |
|
83 | + $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
|
84 | + } |
|
85 | + $this->request = $request; |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * @param string $display_strategy_name |
|
91 | + * @throws InvalidDataTypeException |
|
92 | + * @throws InvalidClassException |
|
93 | + */ |
|
94 | + protected function setDisplayStrategy($display_strategy_name = 'number_bubbles') |
|
95 | + { |
|
96 | + if (! is_string($display_strategy_name)) { |
|
97 | + throw new InvalidDataTypeException('$display_strategy_name', $display_strategy_name, 'string'); |
|
98 | + } |
|
99 | + // build up FQCN from incoming display strategy folder name |
|
100 | + $display_strategy_class = 'EventEspresso\core\services\progress_steps\display_strategies\\'; |
|
101 | + $display_strategy_class .= $display_strategy_name . '\\'; |
|
102 | + $display_strategy_class .= str_replace(' ', '', ucwords(str_replace('_', ' ', $display_strategy_name))); |
|
103 | + $display_strategy_class .= 'ProgressStepsDisplay'; |
|
104 | + $display_strategy_class = apply_filters( |
|
105 | + 'FHEE__ProgressStepManager__setDisplayStrategy__display_strategy_class', |
|
106 | + $display_strategy_class |
|
107 | + ); |
|
108 | + if (! class_exists($display_strategy_class)) { |
|
109 | + throw new InvalidClassException($display_strategy_class); |
|
110 | + } |
|
111 | + $display_strategy = new $display_strategy_class(); |
|
112 | + if (! $display_strategy instanceof ProgressStepsDisplayInterface) { |
|
113 | + throw new InvalidClassException( |
|
114 | + $display_strategy_class, |
|
115 | + sprintf( |
|
116 | + esc_html__('The "%1$s" Class needs to be an implementation of the "%1$s" Interface.', 'event_espresso'), |
|
117 | + $display_strategy_class, |
|
118 | + '\EventEspresso\core\services\progress_steps\display_strategies\ProgressStepsDisplayInterface' |
|
119 | + ) |
|
120 | + ); |
|
121 | + } |
|
122 | + $this->display_strategy = $display_strategy; |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * @param string $default_step |
|
128 | + * @throws InvalidDataTypeException |
|
129 | + */ |
|
130 | + public function setDefaultStep($default_step) |
|
131 | + { |
|
132 | + if (! is_string($default_step)) { |
|
133 | + throw new InvalidDataTypeException('$default_step', $default_step, 'string'); |
|
134 | + } |
|
135 | + $this->default_step = $default_step; |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * @param string $form_step_url_key |
|
141 | + * @throws InvalidDataTypeException |
|
142 | + */ |
|
143 | + public function setFormStepUrlKey($form_step_url_key = 'ee-form-step') |
|
144 | + { |
|
145 | + if (! is_string($form_step_url_key)) { |
|
146 | + throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string'); |
|
147 | + } |
|
148 | + $this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step'; |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * @param string $step |
|
154 | + * @throws InvalidIdentifierException |
|
155 | + */ |
|
156 | + public function setCurrentStep($step = '') |
|
157 | + { |
|
158 | + // use incoming value if it's set, otherwise use request param if it's set, otherwise use default |
|
159 | + $step = ! empty($step) |
|
160 | + ? $step |
|
161 | + : $this->request->getRequestParam($this->form_step_url_key, $this->default_step); |
|
162 | + // grab the step previously known as current, in case we need to revert |
|
163 | + $current_current_step = $this->collection->current(); |
|
164 | + // verify that requested step exists |
|
165 | + if (! $this->collection->has($step)) { |
|
166 | + throw new InvalidIdentifierException($step, $this->default_step); |
|
167 | + } |
|
168 | + if ($this->collection->setCurrent($step)) { |
|
169 | + // if the old boss is the same as the new boss, then nothing changes |
|
170 | + if ($this->collection->current() !== $current_current_step) { |
|
171 | + $current_current_step->setIsCurrent(false); |
|
172 | + } |
|
173 | + $this->collection->current()->setIsCurrent(); |
|
174 | + } else { |
|
175 | + $this->collection->setCurrent($current_current_step->id()); |
|
176 | + $current_current_step->setIsCurrent(true); |
|
177 | + } |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * setPreviousStepsCompleted |
|
183 | + */ |
|
184 | + public function setPreviousStepsCompleted() |
|
185 | + { |
|
186 | + $current_current_step = $this->collection->current(); |
|
187 | + $this->collection->rewind(); |
|
188 | + while ($this->collection->valid()) { |
|
189 | + if ($this->collection->current() === $current_current_step) { |
|
190 | + break; |
|
191 | + } |
|
192 | + $this->setCurrentStepCompleted(); |
|
193 | + $this->collection->next(); |
|
194 | + } |
|
195 | + $this->collection->setCurrentUsingObject($current_current_step); |
|
196 | + return false; |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * @return ProgressStepInterface |
|
202 | + */ |
|
203 | + public function currentStep() |
|
204 | + { |
|
205 | + return $this->collection->current(); |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + /** |
|
210 | + * @return ProgressStepInterface |
|
211 | + */ |
|
212 | + public function nextStep() |
|
213 | + { |
|
214 | + return $this->collection->next(); |
|
215 | + } |
|
216 | + |
|
217 | + |
|
218 | + /** |
|
219 | + * @return void |
|
220 | + */ |
|
221 | + public function enqueueStylesAndScripts() |
|
222 | + { |
|
223 | + $this->display_strategy->enqueueStylesAndScripts(); |
|
224 | + } |
|
225 | + |
|
226 | + |
|
227 | + /** |
|
228 | + * echos out HTML |
|
229 | + * |
|
230 | + * @return string |
|
231 | + */ |
|
232 | + public function displaySteps() |
|
233 | + { |
|
234 | + return EEH_Template::display_template( |
|
235 | + $this->display_strategy->getTemplate(), |
|
236 | + array('progress_steps' => $this->collection), |
|
237 | + true |
|
238 | + ); |
|
239 | + } |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * @param bool $completed |
|
244 | + * @return ProgressStepInterface |
|
245 | + */ |
|
246 | + public function setCurrentStepCompleted($completed = true) |
|
247 | + { |
|
248 | + return $this->collection->current()->setIsCompleted($completed); |
|
249 | + } |
|
250 | 250 | } |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | */ |
141 | 141 | protected function setBaseUrl($base_url) |
142 | 142 | { |
143 | - if (! is_string($base_url)) { |
|
143 | + if ( ! is_string($base_url)) { |
|
144 | 144 | throw new InvalidDataTypeException('$base_url', $base_url, 'string'); |
145 | 145 | } |
146 | 146 | if (empty($base_url)) { |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | */ |
172 | 172 | public function setFormStepUrlKey($form_step_url_key = 'ee-form-step') |
173 | 173 | { |
174 | - if (! is_string($form_step_url_key)) { |
|
174 | + if ( ! is_string($form_step_url_key)) { |
|
175 | 175 | throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string'); |
176 | 176 | } |
177 | 177 | $this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step'; |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | protected function setDefaultFormStep($default_form_step) |
195 | 195 | { |
196 | - if (! is_string($default_form_step)) { |
|
196 | + if ( ! is_string($default_form_step)) { |
|
197 | 197 | throw new InvalidDataTypeException('$default_form_step', $default_form_step, 'string'); |
198 | 198 | } |
199 | 199 | $this->default_form_step = $default_form_step; |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | protected function setCurrentStepFromRequest() |
209 | 209 | { |
210 | 210 | $current_step_slug = $this->request()->getRequestParam($this->formStepUrlKey(), $this->defaultFormStep()); |
211 | - if (! $this->form_steps->setCurrent($current_step_slug)) { |
|
211 | + if ( ! $this->form_steps->setCurrent($current_step_slug)) { |
|
212 | 212 | throw new InvalidIdentifierException( |
213 | 213 | $current_step_slug, |
214 | 214 | $this->defaultFormStep(), |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | */ |
228 | 228 | public function getCurrentStep() |
229 | 229 | { |
230 | - if (! $this->form_steps->current() instanceof SequentialStepForm) { |
|
230 | + if ( ! $this->form_steps->current() instanceof SequentialStepForm) { |
|
231 | 231 | throw new InvalidFormHandlerException($this->form_steps->current()); |
232 | 232 | } |
233 | 233 | return $this->form_steps->current(); |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | */ |
241 | 241 | public function formAction() |
242 | 242 | { |
243 | - if (! is_string($this->form_action) || empty($this->form_action)) { |
|
243 | + if ( ! is_string($this->form_action) || empty($this->form_action)) { |
|
244 | 244 | $this->form_action = $this->baseUrl(); |
245 | 245 | } |
246 | 246 | return $this->form_action; |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | */ |
254 | 254 | public function setFormAction($form_action) |
255 | 255 | { |
256 | - if (! is_string($form_action)) { |
|
256 | + if ( ! is_string($form_action)) { |
|
257 | 257 | throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
258 | 258 | } |
259 | 259 | $this->form_action = $form_action; |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | */ |
268 | 268 | public function addFormActionArgs($form_action_args = array()) |
269 | 269 | { |
270 | - if (! is_array($form_action_args)) { |
|
270 | + if ( ! is_array($form_action_args)) { |
|
271 | 271 | throw new InvalidDataTypeException('$form_action_args', $form_action_args, 'array'); |
272 | 272 | } |
273 | 273 | $form_action_args = ! empty($form_action_args) |
@@ -332,7 +332,7 @@ discard block |
||
332 | 332 | protected function getProgressStepsCollection() |
333 | 333 | { |
334 | 334 | static $collection = null; |
335 | - if (! $collection instanceof ProgressStepCollection) { |
|
335 | + if ( ! $collection instanceof ProgressStepCollection) { |
|
336 | 336 | $collection = new ProgressStepCollection(); |
337 | 337 | } |
338 | 338 | return $collection; |
@@ -354,7 +354,7 @@ discard block |
||
354 | 354 | /** @var SequentialStepForm $form_step */ |
355 | 355 | foreach ($this->form_steps as $form_step) { |
356 | 356 | // is this step active ? |
357 | - if (! $form_step->initialize()) { |
|
357 | + if ( ! $form_step->initialize()) { |
|
358 | 358 | continue; |
359 | 359 | } |
360 | 360 | $progress_steps_collection->add( |
@@ -423,7 +423,7 @@ discard block |
||
423 | 423 | { |
424 | 424 | $form_step = $this->buildCurrentStepForm(); |
425 | 425 | // no displayable content ? then skip straight to processing |
426 | - if (! $form_step->displayable()) { |
|
426 | + if ( ! $form_step->displayable()) { |
|
427 | 427 | $this->addFormActionArgs(); |
428 | 428 | $form_step->setFormAction($this->formAction()); |
429 | 429 | wp_safe_redirect($form_step->formAction()); |
@@ -499,7 +499,7 @@ discard block |
||
499 | 499 | public function displayProgressSteps($return_as_string = true) |
500 | 500 | { |
501 | 501 | $form_step = $this->getCurrentStep(); |
502 | - if (! $form_step->displayable()) { |
|
502 | + if ( ! $form_step->displayable()) { |
|
503 | 503 | return ''; |
504 | 504 | } |
505 | 505 | $progress_steps = apply_filters( |
@@ -30,583 +30,583 @@ |
||
30 | 30 | */ |
31 | 31 | abstract class SequentialStepFormManager |
32 | 32 | { |
33 | - /** |
|
34 | - * a simplified URL with no form related parameters |
|
35 | - * that will be used to build the form's redirect URLs |
|
36 | - * |
|
37 | - * @var string $base_url |
|
38 | - */ |
|
39 | - private $base_url = ''; |
|
40 | - |
|
41 | - /** |
|
42 | - * the key used for the URL param that denotes the current form step |
|
43 | - * defaults to 'ee-form-step' |
|
44 | - * |
|
45 | - * @var string $form_step_url_key |
|
46 | - */ |
|
47 | - private $form_step_url_key = ''; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var string $default_form_step |
|
51 | - */ |
|
52 | - private $default_form_step = ''; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var string $form_action |
|
56 | - */ |
|
57 | - private $form_action; |
|
58 | - |
|
59 | - /** |
|
60 | - * value of one of the string constant above |
|
61 | - * |
|
62 | - * @var string $form_config |
|
63 | - */ |
|
64 | - private $form_config; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var string $progress_step_style |
|
68 | - */ |
|
69 | - private $progress_step_style = ''; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var RequestInterface $request |
|
73 | - */ |
|
74 | - private $request; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var Collection $form_steps |
|
78 | - */ |
|
79 | - protected $form_steps; |
|
80 | - |
|
81 | - /** |
|
82 | - * @var ProgressStepManager $progress_step_manager |
|
83 | - */ |
|
84 | - protected $progress_step_manager; |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * @return Collection|null |
|
89 | - */ |
|
90 | - abstract protected function getFormStepsCollection(); |
|
91 | - |
|
92 | - // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
93 | - /** |
|
94 | - * StepsManager constructor |
|
95 | - * |
|
96 | - * @param string $base_url |
|
97 | - * @param string $default_form_step |
|
98 | - * @param string $form_action |
|
99 | - * @param string $form_config |
|
100 | - * @param EE_Request|RequestInterface|null $request |
|
101 | - * @param string $progress_step_style |
|
102 | - * @throws InvalidDataTypeException |
|
103 | - * @throws InvalidArgumentException |
|
104 | - */ |
|
105 | - public function __construct( |
|
106 | - $base_url, |
|
107 | - $default_form_step, |
|
108 | - $form_action = '', |
|
109 | - $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
110 | - $progress_step_style = 'number_bubbles', |
|
111 | - $request = null |
|
112 | - ) { |
|
113 | - $this->setBaseUrl($base_url); |
|
114 | - $this->setDefaultFormStep($default_form_step); |
|
115 | - $this->setFormAction($form_action); |
|
116 | - $this->setFormConfig($form_config); |
|
117 | - $this->setProgressStepStyle($progress_step_style); |
|
118 | - $this->request = $request instanceof RequestInterface |
|
119 | - ? $request |
|
120 | - : LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
121 | - } |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * @return string |
|
126 | - * @throws InvalidFormHandlerException |
|
127 | - */ |
|
128 | - public function baseUrl() |
|
129 | - { |
|
130 | - if (strpos($this->base_url, $this->getCurrentStep()->slug()) === false) { |
|
131 | - add_query_arg( |
|
132 | - array($this->form_step_url_key => $this->getCurrentStep()->slug()), |
|
133 | - $this->base_url |
|
134 | - ); |
|
135 | - } |
|
136 | - return $this->base_url; |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * @param string $base_url |
|
142 | - * @throws InvalidDataTypeException |
|
143 | - * @throws InvalidArgumentException |
|
144 | - */ |
|
145 | - protected function setBaseUrl($base_url) |
|
146 | - { |
|
147 | - if (! is_string($base_url)) { |
|
148 | - throw new InvalidDataTypeException('$base_url', $base_url, 'string'); |
|
149 | - } |
|
150 | - if (empty($base_url)) { |
|
151 | - throw new InvalidArgumentException( |
|
152 | - esc_html__('The base URL can not be an empty string.', 'event_espresso') |
|
153 | - ); |
|
154 | - } |
|
155 | - $this->base_url = $base_url; |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * @return string |
|
161 | - * @throws InvalidDataTypeException |
|
162 | - */ |
|
163 | - public function formStepUrlKey() |
|
164 | - { |
|
165 | - if (empty($this->form_step_url_key)) { |
|
166 | - $this->setFormStepUrlKey(); |
|
167 | - } |
|
168 | - return $this->form_step_url_key; |
|
169 | - } |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * @param string $form_step_url_key |
|
174 | - * @throws InvalidDataTypeException |
|
175 | - */ |
|
176 | - public function setFormStepUrlKey($form_step_url_key = 'ee-form-step') |
|
177 | - { |
|
178 | - if (! is_string($form_step_url_key)) { |
|
179 | - throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string'); |
|
180 | - } |
|
181 | - $this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step'; |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * @return string |
|
187 | - */ |
|
188 | - public function defaultFormStep() |
|
189 | - { |
|
190 | - return $this->default_form_step; |
|
191 | - } |
|
192 | - |
|
193 | - |
|
194 | - /** |
|
195 | - * @param $default_form_step |
|
196 | - * @throws InvalidDataTypeException |
|
197 | - */ |
|
198 | - protected function setDefaultFormStep($default_form_step) |
|
199 | - { |
|
200 | - if (! is_string($default_form_step)) { |
|
201 | - throw new InvalidDataTypeException('$default_form_step', $default_form_step, 'string'); |
|
202 | - } |
|
203 | - $this->default_form_step = $default_form_step; |
|
204 | - } |
|
205 | - |
|
206 | - |
|
207 | - /** |
|
208 | - * @return void |
|
209 | - * @throws InvalidIdentifierException |
|
210 | - * @throws InvalidDataTypeException |
|
211 | - */ |
|
212 | - protected function setCurrentStepFromRequest() |
|
213 | - { |
|
214 | - $current_step_slug = $this->request()->getRequestParam($this->formStepUrlKey(), $this->defaultFormStep()); |
|
215 | - if (! $this->form_steps->setCurrent($current_step_slug)) { |
|
216 | - throw new InvalidIdentifierException( |
|
217 | - $current_step_slug, |
|
218 | - $this->defaultFormStep(), |
|
219 | - sprintf( |
|
220 | - esc_html__('The "%1$s" form step could not be set.', 'event_espresso'), |
|
221 | - $current_step_slug |
|
222 | - ) |
|
223 | - ); |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * @return SequentialStepFormInterface|object |
|
230 | - * @throws InvalidFormHandlerException |
|
231 | - */ |
|
232 | - public function getCurrentStep() |
|
233 | - { |
|
234 | - if (! $this->form_steps->current() instanceof SequentialStepForm) { |
|
235 | - throw new InvalidFormHandlerException($this->form_steps->current()); |
|
236 | - } |
|
237 | - return $this->form_steps->current(); |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * @return string |
|
243 | - * @throws InvalidFormHandlerException |
|
244 | - */ |
|
245 | - public function formAction() |
|
246 | - { |
|
247 | - if (! is_string($this->form_action) || empty($this->form_action)) { |
|
248 | - $this->form_action = $this->baseUrl(); |
|
249 | - } |
|
250 | - return $this->form_action; |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * @param string $form_action |
|
256 | - * @throws InvalidDataTypeException |
|
257 | - */ |
|
258 | - public function setFormAction($form_action) |
|
259 | - { |
|
260 | - if (! is_string($form_action)) { |
|
261 | - throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
262 | - } |
|
263 | - $this->form_action = $form_action; |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * @param array $form_action_args |
|
269 | - * @throws InvalidDataTypeException |
|
270 | - * @throws InvalidFormHandlerException |
|
271 | - */ |
|
272 | - public function addFormActionArgs($form_action_args = array()) |
|
273 | - { |
|
274 | - if (! is_array($form_action_args)) { |
|
275 | - throw new InvalidDataTypeException('$form_action_args', $form_action_args, 'array'); |
|
276 | - } |
|
277 | - $form_action_args = ! empty($form_action_args) |
|
278 | - ? $form_action_args |
|
279 | - : array($this->formStepUrlKey() => $this->form_steps->current()->slug()); |
|
280 | - $this->getCurrentStep()->setFormAction( |
|
281 | - add_query_arg($form_action_args, $this->formAction()) |
|
282 | - ); |
|
283 | - $this->form_action = $this->getCurrentStep()->formAction(); |
|
284 | - } |
|
285 | - |
|
286 | - |
|
287 | - /** |
|
288 | - * @return string |
|
289 | - */ |
|
290 | - public function formConfig() |
|
291 | - { |
|
292 | - return $this->form_config; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * @param string $form_config |
|
298 | - */ |
|
299 | - public function setFormConfig($form_config) |
|
300 | - { |
|
301 | - $this->form_config = $form_config; |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * @return string |
|
307 | - */ |
|
308 | - public function progressStepStyle() |
|
309 | - { |
|
310 | - return $this->progress_step_style; |
|
311 | - } |
|
312 | - |
|
313 | - |
|
314 | - /** |
|
315 | - * @param string $progress_step_style |
|
316 | - */ |
|
317 | - public function setProgressStepStyle($progress_step_style) |
|
318 | - { |
|
319 | - $this->progress_step_style = $progress_step_style; |
|
320 | - } |
|
321 | - |
|
322 | - |
|
323 | - /** |
|
324 | - * @return RequestInterface |
|
325 | - */ |
|
326 | - public function request() |
|
327 | - { |
|
328 | - return $this->request; |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * @return Collection|null |
|
334 | - * @throws InvalidInterfaceException |
|
335 | - */ |
|
336 | - protected function getProgressStepsCollection() |
|
337 | - { |
|
338 | - static $collection = null; |
|
339 | - if (! $collection instanceof ProgressStepCollection) { |
|
340 | - $collection = new ProgressStepCollection(); |
|
341 | - } |
|
342 | - return $collection; |
|
343 | - } |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * @param Collection $progress_steps_collection |
|
348 | - * @return ProgressStepManager |
|
349 | - * @throws InvalidInterfaceException |
|
350 | - * @throws InvalidClassException |
|
351 | - * @throws InvalidDataTypeException |
|
352 | - * @throws InvalidEntityException |
|
353 | - * @throws InvalidFormHandlerException |
|
354 | - */ |
|
355 | - protected function generateProgressSteps(Collection $progress_steps_collection) |
|
356 | - { |
|
357 | - $current_step = $this->getCurrentStep(); |
|
358 | - /** @var SequentialStepForm $form_step */ |
|
359 | - foreach ($this->form_steps as $form_step) { |
|
360 | - // is this step active ? |
|
361 | - if (! $form_step->initialize()) { |
|
362 | - continue; |
|
363 | - } |
|
364 | - $progress_steps_collection->add( |
|
365 | - new ProgressStep( |
|
366 | - $form_step->order(), |
|
367 | - $form_step->slug(), |
|
368 | - $form_step->slug(), |
|
369 | - $form_step->formName() |
|
370 | - ), |
|
371 | - $form_step->slug() |
|
372 | - ); |
|
373 | - } |
|
374 | - // set collection pointer back to current step |
|
375 | - $this->form_steps->setCurrentUsingObject($current_step); |
|
376 | - return new ProgressStepManager( |
|
377 | - $this->progressStepStyle(), |
|
378 | - $this->defaultFormStep(), |
|
379 | - $this->formStepUrlKey(), |
|
380 | - $progress_steps_collection |
|
381 | - ); |
|
382 | - } |
|
383 | - |
|
384 | - |
|
385 | - /** |
|
386 | - * @throws InvalidClassException |
|
387 | - * @throws InvalidDataTypeException |
|
388 | - * @throws InvalidEntityException |
|
389 | - * @throws InvalidIdentifierException |
|
390 | - * @throws InvalidInterfaceException |
|
391 | - * @throws InvalidArgumentException |
|
392 | - * @throws InvalidFormHandlerException |
|
393 | - */ |
|
394 | - public function buildForm() |
|
395 | - { |
|
396 | - $this->buildCurrentStepFormForDisplay(); |
|
397 | - } |
|
398 | - |
|
399 | - |
|
400 | - /** |
|
401 | - * @param array $form_data |
|
402 | - * @throws InvalidArgumentException |
|
403 | - * @throws InvalidClassException |
|
404 | - * @throws InvalidDataTypeException |
|
405 | - * @throws InvalidEntityException |
|
406 | - * @throws InvalidFormHandlerException |
|
407 | - * @throws InvalidIdentifierException |
|
408 | - * @throws InvalidInterfaceException |
|
409 | - */ |
|
410 | - public function processForm($form_data = array()) |
|
411 | - { |
|
412 | - $this->buildCurrentStepFormForProcessing(); |
|
413 | - $this->processCurrentStepForm($form_data); |
|
414 | - } |
|
415 | - |
|
416 | - |
|
417 | - /** |
|
418 | - * @throws InvalidClassException |
|
419 | - * @throws InvalidDataTypeException |
|
420 | - * @throws InvalidEntityException |
|
421 | - * @throws InvalidInterfaceException |
|
422 | - * @throws InvalidIdentifierException |
|
423 | - * @throws InvalidArgumentException |
|
424 | - * @throws InvalidFormHandlerException |
|
425 | - */ |
|
426 | - public function buildCurrentStepFormForDisplay() |
|
427 | - { |
|
428 | - $form_step = $this->buildCurrentStepForm(); |
|
429 | - // no displayable content ? then skip straight to processing |
|
430 | - if (! $form_step->displayable()) { |
|
431 | - $this->addFormActionArgs(); |
|
432 | - $form_step->setFormAction($this->formAction()); |
|
433 | - wp_safe_redirect($form_step->formAction()); |
|
434 | - } |
|
435 | - } |
|
436 | - |
|
437 | - |
|
438 | - /** |
|
439 | - * @throws InvalidClassException |
|
440 | - * @throws InvalidDataTypeException |
|
441 | - * @throws InvalidEntityException |
|
442 | - * @throws InvalidInterfaceException |
|
443 | - * @throws InvalidIdentifierException |
|
444 | - * @throws InvalidArgumentException |
|
445 | - * @throws InvalidFormHandlerException |
|
446 | - */ |
|
447 | - public function buildCurrentStepFormForProcessing() |
|
448 | - { |
|
449 | - $this->buildCurrentStepForm(false); |
|
450 | - } |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * @param bool $for_display |
|
455 | - * @return SequentialStepFormInterface |
|
456 | - * @throws InvalidArgumentException |
|
457 | - * @throws InvalidClassException |
|
458 | - * @throws InvalidDataTypeException |
|
459 | - * @throws InvalidEntityException |
|
460 | - * @throws InvalidFormHandlerException |
|
461 | - * @throws InvalidIdentifierException |
|
462 | - * @throws InvalidInterfaceException |
|
463 | - */ |
|
464 | - private function buildCurrentStepForm($for_display = true) |
|
465 | - { |
|
466 | - $this->form_steps = $this->getFormStepsCollection(); |
|
467 | - $this->setCurrentStepFromRequest(); |
|
468 | - $form_step = $this->getCurrentStep(); |
|
469 | - if ($form_step->submitBtnText() === esc_html__('Submit', 'event_espresso')) { |
|
470 | - $form_step->setSubmitBtnText(esc_html__('Next Step', 'event_espresso')); |
|
471 | - } |
|
472 | - if ($for_display && $form_step->displayable()) { |
|
473 | - $this->progress_step_manager = $this->generateProgressSteps( |
|
474 | - $this->getProgressStepsCollection() |
|
475 | - ); |
|
476 | - $this->progress_step_manager->setCurrentStep( |
|
477 | - $form_step->slug() |
|
478 | - ); |
|
479 | - // mark all previous progress steps as completed |
|
480 | - $this->progress_step_manager->setPreviousStepsCompleted(); |
|
481 | - $this->progress_step_manager->enqueueStylesAndScripts(); |
|
482 | - $this->addFormActionArgs(); |
|
483 | - $form_step->setFormAction($this->formAction()); |
|
484 | - } else { |
|
485 | - $form_step->setRedirectUrl($this->baseUrl()); |
|
486 | - $form_step->addRedirectArgs( |
|
487 | - array($this->formStepUrlKey() => $this->form_steps->current()->slug()) |
|
488 | - ); |
|
489 | - } |
|
490 | - $form_step->generate(); |
|
491 | - if ($for_display) { |
|
492 | - $form_step->enqueueStylesAndScripts(); |
|
493 | - } |
|
494 | - return $form_step; |
|
495 | - } |
|
496 | - |
|
497 | - |
|
498 | - /** |
|
499 | - * @param bool $return_as_string |
|
500 | - * @return string |
|
501 | - * @throws InvalidFormHandlerException |
|
502 | - */ |
|
503 | - public function displayProgressSteps($return_as_string = true) |
|
504 | - { |
|
505 | - $form_step = $this->getCurrentStep(); |
|
506 | - if (! $form_step->displayable()) { |
|
507 | - return ''; |
|
508 | - } |
|
509 | - $progress_steps = apply_filters( |
|
510 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__before_steps', |
|
511 | - '' |
|
512 | - ); |
|
513 | - $progress_steps .= $this->progress_step_manager->displaySteps(); |
|
514 | - $progress_steps .= apply_filters( |
|
515 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__after_steps', |
|
516 | - '' |
|
517 | - ); |
|
518 | - if ($return_as_string) { |
|
519 | - return $progress_steps; |
|
520 | - } |
|
521 | - echo wp_kses($progress_steps, AllowedTags::getWithFormTags()); |
|
522 | - return ''; |
|
523 | - } |
|
524 | - |
|
525 | - |
|
526 | - /** |
|
527 | - * @param bool $return_as_string |
|
528 | - * @return string |
|
529 | - * @throws InvalidFormHandlerException |
|
530 | - */ |
|
531 | - public function displayCurrentStepForm($return_as_string = true) |
|
532 | - { |
|
533 | - if ($return_as_string) { |
|
534 | - return $this->getCurrentStep()->display(); |
|
535 | - } |
|
536 | - echo wp_kses($this->getCurrentStep()->display(), AllowedTags::getWithFormTags()); |
|
537 | - return ''; |
|
538 | - } |
|
539 | - |
|
540 | - |
|
541 | - /** |
|
542 | - * @param array $form_data |
|
543 | - * @return void |
|
544 | - * @throws InvalidArgumentException |
|
545 | - * @throws InvalidDataTypeException |
|
546 | - * @throws InvalidFormHandlerException |
|
547 | - */ |
|
548 | - public function processCurrentStepForm($form_data = array()) |
|
549 | - { |
|
550 | - // grab instance of current step because after calling next() below, |
|
551 | - // any calls to getCurrentStep() will return the "next" step because we advanced |
|
552 | - $current_step = $this->getCurrentStep(); |
|
553 | - try { |
|
554 | - // form processing should either throw exceptions or return true |
|
555 | - $current_step->process($form_data); |
|
556 | - } catch (Exception $e) { |
|
557 | - // something went wrong, convert the Exception to an EE_Error |
|
558 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
559 | - // prevent redirect to next step or other if exception was thrown |
|
560 | - if ( |
|
561 | - $current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_NEXT_STEP |
|
562 | - || $current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_OTHER |
|
563 | - ) { |
|
564 | - $current_step->setRedirectTo(SequentialStepForm::REDIRECT_TO_CURRENT_STEP); |
|
565 | - } |
|
566 | - } |
|
567 | - // save notices to a transient so that when we redirect back |
|
568 | - // to the display portion for this step |
|
569 | - // those notices can be displayed |
|
570 | - EE_Error::get_notices(false, true); |
|
571 | - $this->redirectForm($current_step); |
|
572 | - } |
|
573 | - |
|
574 | - |
|
575 | - /** |
|
576 | - * handles where to go to next |
|
577 | - * |
|
578 | - * @param SequentialStepFormInterface $current_step |
|
579 | - * @throws InvalidArgumentException |
|
580 | - * @throws InvalidDataTypeException |
|
581 | - * @throws InvalidFormHandlerException |
|
582 | - */ |
|
583 | - public function redirectForm(SequentialStepFormInterface $current_step) |
|
584 | - { |
|
585 | - $redirect_step = $current_step; |
|
586 | - switch ($current_step->redirectTo()) { |
|
587 | - case SequentialStepForm::REDIRECT_TO_OTHER: |
|
588 | - // going somewhere else, so just check out now |
|
589 | - wp_safe_redirect($redirect_step->redirectUrl()); |
|
590 | - exit(); |
|
591 | - case SequentialStepForm::REDIRECT_TO_PREV_STEP: |
|
592 | - $redirect_step = $this->form_steps->previous(); |
|
593 | - break; |
|
594 | - case SequentialStepForm::REDIRECT_TO_NEXT_STEP: |
|
595 | - $this->form_steps->next(); |
|
596 | - if ($this->form_steps->valid()) { |
|
597 | - $redirect_step = $this->form_steps->current(); |
|
598 | - } |
|
599 | - break; |
|
600 | - case SequentialStepForm::REDIRECT_TO_CURRENT_STEP: |
|
601 | - default: |
|
602 | - // $redirect_step is already set |
|
603 | - } |
|
604 | - $current_step->setRedirectUrl($this->baseUrl()); |
|
605 | - $current_step->addRedirectArgs( |
|
606 | - // use the slug for whatever step we are redirecting too |
|
607 | - array($this->formStepUrlKey() => $redirect_step->slug()) |
|
608 | - ); |
|
609 | - wp_safe_redirect($current_step->redirectUrl()); |
|
610 | - exit(); |
|
611 | - } |
|
33 | + /** |
|
34 | + * a simplified URL with no form related parameters |
|
35 | + * that will be used to build the form's redirect URLs |
|
36 | + * |
|
37 | + * @var string $base_url |
|
38 | + */ |
|
39 | + private $base_url = ''; |
|
40 | + |
|
41 | + /** |
|
42 | + * the key used for the URL param that denotes the current form step |
|
43 | + * defaults to 'ee-form-step' |
|
44 | + * |
|
45 | + * @var string $form_step_url_key |
|
46 | + */ |
|
47 | + private $form_step_url_key = ''; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var string $default_form_step |
|
51 | + */ |
|
52 | + private $default_form_step = ''; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var string $form_action |
|
56 | + */ |
|
57 | + private $form_action; |
|
58 | + |
|
59 | + /** |
|
60 | + * value of one of the string constant above |
|
61 | + * |
|
62 | + * @var string $form_config |
|
63 | + */ |
|
64 | + private $form_config; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var string $progress_step_style |
|
68 | + */ |
|
69 | + private $progress_step_style = ''; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var RequestInterface $request |
|
73 | + */ |
|
74 | + private $request; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var Collection $form_steps |
|
78 | + */ |
|
79 | + protected $form_steps; |
|
80 | + |
|
81 | + /** |
|
82 | + * @var ProgressStepManager $progress_step_manager |
|
83 | + */ |
|
84 | + protected $progress_step_manager; |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * @return Collection|null |
|
89 | + */ |
|
90 | + abstract protected function getFormStepsCollection(); |
|
91 | + |
|
92 | + // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
93 | + /** |
|
94 | + * StepsManager constructor |
|
95 | + * |
|
96 | + * @param string $base_url |
|
97 | + * @param string $default_form_step |
|
98 | + * @param string $form_action |
|
99 | + * @param string $form_config |
|
100 | + * @param EE_Request|RequestInterface|null $request |
|
101 | + * @param string $progress_step_style |
|
102 | + * @throws InvalidDataTypeException |
|
103 | + * @throws InvalidArgumentException |
|
104 | + */ |
|
105 | + public function __construct( |
|
106 | + $base_url, |
|
107 | + $default_form_step, |
|
108 | + $form_action = '', |
|
109 | + $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
110 | + $progress_step_style = 'number_bubbles', |
|
111 | + $request = null |
|
112 | + ) { |
|
113 | + $this->setBaseUrl($base_url); |
|
114 | + $this->setDefaultFormStep($default_form_step); |
|
115 | + $this->setFormAction($form_action); |
|
116 | + $this->setFormConfig($form_config); |
|
117 | + $this->setProgressStepStyle($progress_step_style); |
|
118 | + $this->request = $request instanceof RequestInterface |
|
119 | + ? $request |
|
120 | + : LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
121 | + } |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * @return string |
|
126 | + * @throws InvalidFormHandlerException |
|
127 | + */ |
|
128 | + public function baseUrl() |
|
129 | + { |
|
130 | + if (strpos($this->base_url, $this->getCurrentStep()->slug()) === false) { |
|
131 | + add_query_arg( |
|
132 | + array($this->form_step_url_key => $this->getCurrentStep()->slug()), |
|
133 | + $this->base_url |
|
134 | + ); |
|
135 | + } |
|
136 | + return $this->base_url; |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * @param string $base_url |
|
142 | + * @throws InvalidDataTypeException |
|
143 | + * @throws InvalidArgumentException |
|
144 | + */ |
|
145 | + protected function setBaseUrl($base_url) |
|
146 | + { |
|
147 | + if (! is_string($base_url)) { |
|
148 | + throw new InvalidDataTypeException('$base_url', $base_url, 'string'); |
|
149 | + } |
|
150 | + if (empty($base_url)) { |
|
151 | + throw new InvalidArgumentException( |
|
152 | + esc_html__('The base URL can not be an empty string.', 'event_espresso') |
|
153 | + ); |
|
154 | + } |
|
155 | + $this->base_url = $base_url; |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * @return string |
|
161 | + * @throws InvalidDataTypeException |
|
162 | + */ |
|
163 | + public function formStepUrlKey() |
|
164 | + { |
|
165 | + if (empty($this->form_step_url_key)) { |
|
166 | + $this->setFormStepUrlKey(); |
|
167 | + } |
|
168 | + return $this->form_step_url_key; |
|
169 | + } |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * @param string $form_step_url_key |
|
174 | + * @throws InvalidDataTypeException |
|
175 | + */ |
|
176 | + public function setFormStepUrlKey($form_step_url_key = 'ee-form-step') |
|
177 | + { |
|
178 | + if (! is_string($form_step_url_key)) { |
|
179 | + throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string'); |
|
180 | + } |
|
181 | + $this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step'; |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * @return string |
|
187 | + */ |
|
188 | + public function defaultFormStep() |
|
189 | + { |
|
190 | + return $this->default_form_step; |
|
191 | + } |
|
192 | + |
|
193 | + |
|
194 | + /** |
|
195 | + * @param $default_form_step |
|
196 | + * @throws InvalidDataTypeException |
|
197 | + */ |
|
198 | + protected function setDefaultFormStep($default_form_step) |
|
199 | + { |
|
200 | + if (! is_string($default_form_step)) { |
|
201 | + throw new InvalidDataTypeException('$default_form_step', $default_form_step, 'string'); |
|
202 | + } |
|
203 | + $this->default_form_step = $default_form_step; |
|
204 | + } |
|
205 | + |
|
206 | + |
|
207 | + /** |
|
208 | + * @return void |
|
209 | + * @throws InvalidIdentifierException |
|
210 | + * @throws InvalidDataTypeException |
|
211 | + */ |
|
212 | + protected function setCurrentStepFromRequest() |
|
213 | + { |
|
214 | + $current_step_slug = $this->request()->getRequestParam($this->formStepUrlKey(), $this->defaultFormStep()); |
|
215 | + if (! $this->form_steps->setCurrent($current_step_slug)) { |
|
216 | + throw new InvalidIdentifierException( |
|
217 | + $current_step_slug, |
|
218 | + $this->defaultFormStep(), |
|
219 | + sprintf( |
|
220 | + esc_html__('The "%1$s" form step could not be set.', 'event_espresso'), |
|
221 | + $current_step_slug |
|
222 | + ) |
|
223 | + ); |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * @return SequentialStepFormInterface|object |
|
230 | + * @throws InvalidFormHandlerException |
|
231 | + */ |
|
232 | + public function getCurrentStep() |
|
233 | + { |
|
234 | + if (! $this->form_steps->current() instanceof SequentialStepForm) { |
|
235 | + throw new InvalidFormHandlerException($this->form_steps->current()); |
|
236 | + } |
|
237 | + return $this->form_steps->current(); |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * @return string |
|
243 | + * @throws InvalidFormHandlerException |
|
244 | + */ |
|
245 | + public function formAction() |
|
246 | + { |
|
247 | + if (! is_string($this->form_action) || empty($this->form_action)) { |
|
248 | + $this->form_action = $this->baseUrl(); |
|
249 | + } |
|
250 | + return $this->form_action; |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * @param string $form_action |
|
256 | + * @throws InvalidDataTypeException |
|
257 | + */ |
|
258 | + public function setFormAction($form_action) |
|
259 | + { |
|
260 | + if (! is_string($form_action)) { |
|
261 | + throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
262 | + } |
|
263 | + $this->form_action = $form_action; |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * @param array $form_action_args |
|
269 | + * @throws InvalidDataTypeException |
|
270 | + * @throws InvalidFormHandlerException |
|
271 | + */ |
|
272 | + public function addFormActionArgs($form_action_args = array()) |
|
273 | + { |
|
274 | + if (! is_array($form_action_args)) { |
|
275 | + throw new InvalidDataTypeException('$form_action_args', $form_action_args, 'array'); |
|
276 | + } |
|
277 | + $form_action_args = ! empty($form_action_args) |
|
278 | + ? $form_action_args |
|
279 | + : array($this->formStepUrlKey() => $this->form_steps->current()->slug()); |
|
280 | + $this->getCurrentStep()->setFormAction( |
|
281 | + add_query_arg($form_action_args, $this->formAction()) |
|
282 | + ); |
|
283 | + $this->form_action = $this->getCurrentStep()->formAction(); |
|
284 | + } |
|
285 | + |
|
286 | + |
|
287 | + /** |
|
288 | + * @return string |
|
289 | + */ |
|
290 | + public function formConfig() |
|
291 | + { |
|
292 | + return $this->form_config; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * @param string $form_config |
|
298 | + */ |
|
299 | + public function setFormConfig($form_config) |
|
300 | + { |
|
301 | + $this->form_config = $form_config; |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * @return string |
|
307 | + */ |
|
308 | + public function progressStepStyle() |
|
309 | + { |
|
310 | + return $this->progress_step_style; |
|
311 | + } |
|
312 | + |
|
313 | + |
|
314 | + /** |
|
315 | + * @param string $progress_step_style |
|
316 | + */ |
|
317 | + public function setProgressStepStyle($progress_step_style) |
|
318 | + { |
|
319 | + $this->progress_step_style = $progress_step_style; |
|
320 | + } |
|
321 | + |
|
322 | + |
|
323 | + /** |
|
324 | + * @return RequestInterface |
|
325 | + */ |
|
326 | + public function request() |
|
327 | + { |
|
328 | + return $this->request; |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * @return Collection|null |
|
334 | + * @throws InvalidInterfaceException |
|
335 | + */ |
|
336 | + protected function getProgressStepsCollection() |
|
337 | + { |
|
338 | + static $collection = null; |
|
339 | + if (! $collection instanceof ProgressStepCollection) { |
|
340 | + $collection = new ProgressStepCollection(); |
|
341 | + } |
|
342 | + return $collection; |
|
343 | + } |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * @param Collection $progress_steps_collection |
|
348 | + * @return ProgressStepManager |
|
349 | + * @throws InvalidInterfaceException |
|
350 | + * @throws InvalidClassException |
|
351 | + * @throws InvalidDataTypeException |
|
352 | + * @throws InvalidEntityException |
|
353 | + * @throws InvalidFormHandlerException |
|
354 | + */ |
|
355 | + protected function generateProgressSteps(Collection $progress_steps_collection) |
|
356 | + { |
|
357 | + $current_step = $this->getCurrentStep(); |
|
358 | + /** @var SequentialStepForm $form_step */ |
|
359 | + foreach ($this->form_steps as $form_step) { |
|
360 | + // is this step active ? |
|
361 | + if (! $form_step->initialize()) { |
|
362 | + continue; |
|
363 | + } |
|
364 | + $progress_steps_collection->add( |
|
365 | + new ProgressStep( |
|
366 | + $form_step->order(), |
|
367 | + $form_step->slug(), |
|
368 | + $form_step->slug(), |
|
369 | + $form_step->formName() |
|
370 | + ), |
|
371 | + $form_step->slug() |
|
372 | + ); |
|
373 | + } |
|
374 | + // set collection pointer back to current step |
|
375 | + $this->form_steps->setCurrentUsingObject($current_step); |
|
376 | + return new ProgressStepManager( |
|
377 | + $this->progressStepStyle(), |
|
378 | + $this->defaultFormStep(), |
|
379 | + $this->formStepUrlKey(), |
|
380 | + $progress_steps_collection |
|
381 | + ); |
|
382 | + } |
|
383 | + |
|
384 | + |
|
385 | + /** |
|
386 | + * @throws InvalidClassException |
|
387 | + * @throws InvalidDataTypeException |
|
388 | + * @throws InvalidEntityException |
|
389 | + * @throws InvalidIdentifierException |
|
390 | + * @throws InvalidInterfaceException |
|
391 | + * @throws InvalidArgumentException |
|
392 | + * @throws InvalidFormHandlerException |
|
393 | + */ |
|
394 | + public function buildForm() |
|
395 | + { |
|
396 | + $this->buildCurrentStepFormForDisplay(); |
|
397 | + } |
|
398 | + |
|
399 | + |
|
400 | + /** |
|
401 | + * @param array $form_data |
|
402 | + * @throws InvalidArgumentException |
|
403 | + * @throws InvalidClassException |
|
404 | + * @throws InvalidDataTypeException |
|
405 | + * @throws InvalidEntityException |
|
406 | + * @throws InvalidFormHandlerException |
|
407 | + * @throws InvalidIdentifierException |
|
408 | + * @throws InvalidInterfaceException |
|
409 | + */ |
|
410 | + public function processForm($form_data = array()) |
|
411 | + { |
|
412 | + $this->buildCurrentStepFormForProcessing(); |
|
413 | + $this->processCurrentStepForm($form_data); |
|
414 | + } |
|
415 | + |
|
416 | + |
|
417 | + /** |
|
418 | + * @throws InvalidClassException |
|
419 | + * @throws InvalidDataTypeException |
|
420 | + * @throws InvalidEntityException |
|
421 | + * @throws InvalidInterfaceException |
|
422 | + * @throws InvalidIdentifierException |
|
423 | + * @throws InvalidArgumentException |
|
424 | + * @throws InvalidFormHandlerException |
|
425 | + */ |
|
426 | + public function buildCurrentStepFormForDisplay() |
|
427 | + { |
|
428 | + $form_step = $this->buildCurrentStepForm(); |
|
429 | + // no displayable content ? then skip straight to processing |
|
430 | + if (! $form_step->displayable()) { |
|
431 | + $this->addFormActionArgs(); |
|
432 | + $form_step->setFormAction($this->formAction()); |
|
433 | + wp_safe_redirect($form_step->formAction()); |
|
434 | + } |
|
435 | + } |
|
436 | + |
|
437 | + |
|
438 | + /** |
|
439 | + * @throws InvalidClassException |
|
440 | + * @throws InvalidDataTypeException |
|
441 | + * @throws InvalidEntityException |
|
442 | + * @throws InvalidInterfaceException |
|
443 | + * @throws InvalidIdentifierException |
|
444 | + * @throws InvalidArgumentException |
|
445 | + * @throws InvalidFormHandlerException |
|
446 | + */ |
|
447 | + public function buildCurrentStepFormForProcessing() |
|
448 | + { |
|
449 | + $this->buildCurrentStepForm(false); |
|
450 | + } |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * @param bool $for_display |
|
455 | + * @return SequentialStepFormInterface |
|
456 | + * @throws InvalidArgumentException |
|
457 | + * @throws InvalidClassException |
|
458 | + * @throws InvalidDataTypeException |
|
459 | + * @throws InvalidEntityException |
|
460 | + * @throws InvalidFormHandlerException |
|
461 | + * @throws InvalidIdentifierException |
|
462 | + * @throws InvalidInterfaceException |
|
463 | + */ |
|
464 | + private function buildCurrentStepForm($for_display = true) |
|
465 | + { |
|
466 | + $this->form_steps = $this->getFormStepsCollection(); |
|
467 | + $this->setCurrentStepFromRequest(); |
|
468 | + $form_step = $this->getCurrentStep(); |
|
469 | + if ($form_step->submitBtnText() === esc_html__('Submit', 'event_espresso')) { |
|
470 | + $form_step->setSubmitBtnText(esc_html__('Next Step', 'event_espresso')); |
|
471 | + } |
|
472 | + if ($for_display && $form_step->displayable()) { |
|
473 | + $this->progress_step_manager = $this->generateProgressSteps( |
|
474 | + $this->getProgressStepsCollection() |
|
475 | + ); |
|
476 | + $this->progress_step_manager->setCurrentStep( |
|
477 | + $form_step->slug() |
|
478 | + ); |
|
479 | + // mark all previous progress steps as completed |
|
480 | + $this->progress_step_manager->setPreviousStepsCompleted(); |
|
481 | + $this->progress_step_manager->enqueueStylesAndScripts(); |
|
482 | + $this->addFormActionArgs(); |
|
483 | + $form_step->setFormAction($this->formAction()); |
|
484 | + } else { |
|
485 | + $form_step->setRedirectUrl($this->baseUrl()); |
|
486 | + $form_step->addRedirectArgs( |
|
487 | + array($this->formStepUrlKey() => $this->form_steps->current()->slug()) |
|
488 | + ); |
|
489 | + } |
|
490 | + $form_step->generate(); |
|
491 | + if ($for_display) { |
|
492 | + $form_step->enqueueStylesAndScripts(); |
|
493 | + } |
|
494 | + return $form_step; |
|
495 | + } |
|
496 | + |
|
497 | + |
|
498 | + /** |
|
499 | + * @param bool $return_as_string |
|
500 | + * @return string |
|
501 | + * @throws InvalidFormHandlerException |
|
502 | + */ |
|
503 | + public function displayProgressSteps($return_as_string = true) |
|
504 | + { |
|
505 | + $form_step = $this->getCurrentStep(); |
|
506 | + if (! $form_step->displayable()) { |
|
507 | + return ''; |
|
508 | + } |
|
509 | + $progress_steps = apply_filters( |
|
510 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__before_steps', |
|
511 | + '' |
|
512 | + ); |
|
513 | + $progress_steps .= $this->progress_step_manager->displaySteps(); |
|
514 | + $progress_steps .= apply_filters( |
|
515 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__after_steps', |
|
516 | + '' |
|
517 | + ); |
|
518 | + if ($return_as_string) { |
|
519 | + return $progress_steps; |
|
520 | + } |
|
521 | + echo wp_kses($progress_steps, AllowedTags::getWithFormTags()); |
|
522 | + return ''; |
|
523 | + } |
|
524 | + |
|
525 | + |
|
526 | + /** |
|
527 | + * @param bool $return_as_string |
|
528 | + * @return string |
|
529 | + * @throws InvalidFormHandlerException |
|
530 | + */ |
|
531 | + public function displayCurrentStepForm($return_as_string = true) |
|
532 | + { |
|
533 | + if ($return_as_string) { |
|
534 | + return $this->getCurrentStep()->display(); |
|
535 | + } |
|
536 | + echo wp_kses($this->getCurrentStep()->display(), AllowedTags::getWithFormTags()); |
|
537 | + return ''; |
|
538 | + } |
|
539 | + |
|
540 | + |
|
541 | + /** |
|
542 | + * @param array $form_data |
|
543 | + * @return void |
|
544 | + * @throws InvalidArgumentException |
|
545 | + * @throws InvalidDataTypeException |
|
546 | + * @throws InvalidFormHandlerException |
|
547 | + */ |
|
548 | + public function processCurrentStepForm($form_data = array()) |
|
549 | + { |
|
550 | + // grab instance of current step because after calling next() below, |
|
551 | + // any calls to getCurrentStep() will return the "next" step because we advanced |
|
552 | + $current_step = $this->getCurrentStep(); |
|
553 | + try { |
|
554 | + // form processing should either throw exceptions or return true |
|
555 | + $current_step->process($form_data); |
|
556 | + } catch (Exception $e) { |
|
557 | + // something went wrong, convert the Exception to an EE_Error |
|
558 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
559 | + // prevent redirect to next step or other if exception was thrown |
|
560 | + if ( |
|
561 | + $current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_NEXT_STEP |
|
562 | + || $current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_OTHER |
|
563 | + ) { |
|
564 | + $current_step->setRedirectTo(SequentialStepForm::REDIRECT_TO_CURRENT_STEP); |
|
565 | + } |
|
566 | + } |
|
567 | + // save notices to a transient so that when we redirect back |
|
568 | + // to the display portion for this step |
|
569 | + // those notices can be displayed |
|
570 | + EE_Error::get_notices(false, true); |
|
571 | + $this->redirectForm($current_step); |
|
572 | + } |
|
573 | + |
|
574 | + |
|
575 | + /** |
|
576 | + * handles where to go to next |
|
577 | + * |
|
578 | + * @param SequentialStepFormInterface $current_step |
|
579 | + * @throws InvalidArgumentException |
|
580 | + * @throws InvalidDataTypeException |
|
581 | + * @throws InvalidFormHandlerException |
|
582 | + */ |
|
583 | + public function redirectForm(SequentialStepFormInterface $current_step) |
|
584 | + { |
|
585 | + $redirect_step = $current_step; |
|
586 | + switch ($current_step->redirectTo()) { |
|
587 | + case SequentialStepForm::REDIRECT_TO_OTHER: |
|
588 | + // going somewhere else, so just check out now |
|
589 | + wp_safe_redirect($redirect_step->redirectUrl()); |
|
590 | + exit(); |
|
591 | + case SequentialStepForm::REDIRECT_TO_PREV_STEP: |
|
592 | + $redirect_step = $this->form_steps->previous(); |
|
593 | + break; |
|
594 | + case SequentialStepForm::REDIRECT_TO_NEXT_STEP: |
|
595 | + $this->form_steps->next(); |
|
596 | + if ($this->form_steps->valid()) { |
|
597 | + $redirect_step = $this->form_steps->current(); |
|
598 | + } |
|
599 | + break; |
|
600 | + case SequentialStepForm::REDIRECT_TO_CURRENT_STEP: |
|
601 | + default: |
|
602 | + // $redirect_step is already set |
|
603 | + } |
|
604 | + $current_step->setRedirectUrl($this->baseUrl()); |
|
605 | + $current_step->addRedirectArgs( |
|
606 | + // use the slug for whatever step we are redirecting too |
|
607 | + array($this->formStepUrlKey() => $redirect_step->slug()) |
|
608 | + ); |
|
609 | + wp_safe_redirect($current_step->redirectUrl()); |
|
610 | + exit(); |
|
611 | + } |
|
612 | 612 | } |