@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
2 | - exit('NO direct script access allowed'); |
|
2 | + exit('NO direct script access allowed'); |
|
3 | 3 | } |
4 | 4 | |
5 | 5 | |
@@ -16,680 +16,680 @@ discard block |
||
16 | 16 | */ |
17 | 17 | //Quickfix to address https://events.codebasehq.com/projects/event-espresso/tickets/11089 ASAP |
18 | 18 | if (! function_exists('mb_strcut')) { |
19 | - /** |
|
20 | - * Very simple mimic of mb_substr (which WP ensures exists in wp-includes/compat.php). Still has all the problems of mb_substr |
|
21 | - * (namely, that we might send too many characters to PayPal; however in this case they just issue a warning but nothing breaks) |
|
22 | - * @param $string |
|
23 | - * @param $start |
|
24 | - * @param $length |
|
25 | - * @return bool|string |
|
26 | - */ |
|
27 | - function mb_strcut($string, $start, $length = null) |
|
28 | - { |
|
29 | - return mb_substr($string, $start, $length); |
|
30 | - } |
|
19 | + /** |
|
20 | + * Very simple mimic of mb_substr (which WP ensures exists in wp-includes/compat.php). Still has all the problems of mb_substr |
|
21 | + * (namely, that we might send too many characters to PayPal; however in this case they just issue a warning but nothing breaks) |
|
22 | + * @param $string |
|
23 | + * @param $start |
|
24 | + * @param $length |
|
25 | + * @return bool|string |
|
26 | + */ |
|
27 | + function mb_strcut($string, $start, $length = null) |
|
28 | + { |
|
29 | + return mb_substr($string, $start, $length); |
|
30 | + } |
|
31 | 31 | } |
32 | 32 | class EEG_Paypal_Express extends EE_Offsite_Gateway |
33 | 33 | { |
34 | 34 | |
35 | - /** |
|
36 | - * Merchant API Username. |
|
37 | - * |
|
38 | - * @var string |
|
39 | - */ |
|
40 | - protected $_api_username; |
|
41 | - |
|
42 | - /** |
|
43 | - * Merchant API Password. |
|
44 | - * |
|
45 | - * @var string |
|
46 | - */ |
|
47 | - protected $_api_password; |
|
48 | - |
|
49 | - /** |
|
50 | - * API Signature. |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $_api_signature; |
|
55 | - |
|
56 | - /** |
|
57 | - * Request Shipping address on PP checkout page. |
|
58 | - * |
|
59 | - * @var string |
|
60 | - */ |
|
61 | - protected $_request_shipping_addr; |
|
62 | - |
|
63 | - /** |
|
64 | - * Business/personal logo. |
|
65 | - * |
|
66 | - * @var string |
|
67 | - */ |
|
68 | - protected $_image_url; |
|
69 | - |
|
70 | - /** |
|
71 | - * gateway URL variable |
|
72 | - * |
|
73 | - * @var string |
|
74 | - */ |
|
75 | - protected $_base_gateway_url = ''; |
|
76 | - |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * EEG_Paypal_Express constructor. |
|
81 | - */ |
|
82 | - public function __construct() |
|
83 | - { |
|
84 | - $this->_currencies_supported = array( |
|
85 | - 'USD', |
|
86 | - 'AUD', |
|
87 | - 'BRL', |
|
88 | - 'CAD', |
|
89 | - 'CZK', |
|
90 | - 'DKK', |
|
91 | - 'EUR', |
|
92 | - 'HKD', |
|
93 | - 'HUF', |
|
94 | - 'ILS', |
|
95 | - 'JPY', |
|
96 | - 'MYR', |
|
97 | - 'MXN', |
|
98 | - 'NOK', |
|
99 | - 'NZD', |
|
100 | - 'PHP', |
|
101 | - 'PLN', |
|
102 | - 'GBP', |
|
103 | - 'RUB', |
|
104 | - 'SGD', |
|
105 | - 'SEK', |
|
106 | - 'CHF', |
|
107 | - 'TWD', |
|
108 | - 'THB', |
|
109 | - 'TRY', |
|
110 | - ); |
|
111 | - parent::__construct(); |
|
112 | - } |
|
113 | - |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * Sets the gateway URL variable based on whether debug mode is enabled or not. |
|
118 | - * |
|
119 | - * @param array $settings_array |
|
120 | - */ |
|
121 | - public function set_settings($settings_array) |
|
122 | - { |
|
123 | - parent::set_settings($settings_array); |
|
124 | - // Redirect URL. |
|
125 | - $this->_base_gateway_url = $this->_debug_mode |
|
126 | - ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
127 | - : 'https://api-3t.paypal.com/nvp'; |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - |
|
132 | - /** |
|
133 | - * @param EEI_Payment $payment |
|
134 | - * @param array $billing_info |
|
135 | - * @param string $return_url |
|
136 | - * @param string $notify_url |
|
137 | - * @param string $cancel_url |
|
138 | - * @return \EE_Payment|\EEI_Payment |
|
139 | - * @throws \EE_Error |
|
140 | - */ |
|
141 | - public function set_redirection_info( |
|
142 | - $payment, |
|
143 | - $billing_info = array(), |
|
144 | - $return_url = null, |
|
145 | - $notify_url = null, |
|
146 | - $cancel_url = null |
|
147 | - ) { |
|
148 | - if (! $payment instanceof EEI_Payment) { |
|
149 | - $payment->set_gateway_response( |
|
150 | - esc_html__( |
|
151 | - 'Error. No associated payment was found.', |
|
152 | - 'event_espresso' |
|
153 | - ) |
|
154 | - ); |
|
155 | - $payment->set_status($this->_pay_model->failed_status()); |
|
156 | - return $payment; |
|
157 | - } |
|
158 | - $transaction = $payment->transaction(); |
|
159 | - if (! $transaction instanceof EEI_Transaction) { |
|
160 | - $payment->set_gateway_response( |
|
161 | - esc_html__( |
|
162 | - 'Could not process this payment because it has no associated transaction.', |
|
163 | - 'event_espresso' |
|
164 | - ) |
|
165 | - ); |
|
166 | - $payment->set_status($this->_pay_model->failed_status()); |
|
167 | - return $payment; |
|
168 | - } |
|
169 | - $gateway_formatter = $this->_get_gateway_formatter(); |
|
170 | - $order_description = mb_strcut($gateway_formatter->formatOrderDescription($payment), 0, 127); |
|
171 | - $primary_registration = $transaction->primary_registration(); |
|
172 | - $primary_attendee = $primary_registration instanceof EE_Registration |
|
173 | - ? $primary_registration->attendee() |
|
174 | - : false; |
|
175 | - $locale = explode('-', get_bloginfo('language')); |
|
176 | - // Gather request parameters. |
|
177 | - $token_request_dtls = array( |
|
178 | - 'METHOD' => 'SetExpressCheckout', |
|
179 | - 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
180 | - 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
181 | - 'PAYMENTREQUEST_0_DESC' => $order_description, |
|
182 | - 'RETURNURL' => $return_url, |
|
183 | - 'CANCELURL' => $cancel_url, |
|
184 | - 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
185 | - // Buyer does not need to create a PayPal account to check out. |
|
186 | - // This is referred to as PayPal Account Optional. |
|
187 | - 'SOLUTIONTYPE' => 'Sole', |
|
188 | - //EE will blow up if you change this |
|
189 | - 'BUTTONSOURCE' => 'EventEspresso_SP', |
|
190 | - // Locale of the pages displayed by PayPal during Express Checkout. |
|
191 | - 'LOCALECODE' => $locale[1] |
|
192 | - ); |
|
193 | - // Show itemized list. |
|
194 | - $itemized_list = $this->itemize_list($payment, $transaction); |
|
195 | - $token_request_dtls = array_merge($token_request_dtls, $itemized_list); |
|
196 | - // Automatically filling out shipping and contact information. |
|
197 | - if ($this->_request_shipping_addr && $primary_attendee instanceof EEI_Attendee) { |
|
198 | - // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
|
199 | - $token_request_dtls['NOSHIPPING'] = '2'; |
|
200 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET'] = $primary_attendee->address(); |
|
201 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $primary_attendee->address2(); |
|
202 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCITY'] = $primary_attendee->city(); |
|
203 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTATE'] = $primary_attendee->state_abbrev(); |
|
204 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $primary_attendee->country_ID(); |
|
205 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
|
206 | - $token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
|
207 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
|
208 | - } elseif (! $this->_request_shipping_addr) { |
|
209 | - // Do not request shipping details on the PP Checkout page. |
|
210 | - $token_request_dtls['NOSHIPPING'] = '1'; |
|
211 | - $token_request_dtls['REQCONFIRMSHIPPING'] = '0'; |
|
212 | - } |
|
213 | - // Used a business/personal logo on the PayPal page. |
|
214 | - if (! empty($this->_image_url)) { |
|
215 | - $token_request_dtls['LOGOIMG'] = $this->_image_url; |
|
216 | - } |
|
217 | - $token_request_dtls = apply_filters( |
|
218 | - 'FHEE__EEG_Paypal_Express__set_redirection_info__arguments', |
|
219 | - $token_request_dtls, |
|
220 | - $this |
|
221 | - ); |
|
222 | - // Request PayPal token. |
|
223 | - $token_request_response = $this->_ppExpress_request($token_request_dtls, 'Payment Token', $payment); |
|
224 | - $token_rstatus = $this->_ppExpress_check_response($token_request_response); |
|
225 | - $response_args = (isset($token_rstatus['args']) && is_array($token_rstatus['args'])) |
|
226 | - ? $token_rstatus['args'] |
|
227 | - : array(); |
|
228 | - if ($token_rstatus['status']) { |
|
229 | - // We got the Token so we may continue with the payment and redirect the client. |
|
230 | - $payment->set_details($response_args); |
|
231 | - $gateway_url = $this->_debug_mode ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; |
|
232 | - $payment->set_redirect_url( |
|
233 | - $gateway_url |
|
234 | - . '/checkoutnow?useraction=commit&cmd=_express-checkout&token=' |
|
235 | - . $response_args['TOKEN'] |
|
236 | - ); |
|
237 | - } else { |
|
238 | - if (isset($response_args['L_ERRORCODE'])) { |
|
239 | - $payment->set_gateway_response($response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE']); |
|
240 | - } else { |
|
241 | - $payment->set_gateway_response( |
|
242 | - esc_html__( |
|
243 | - 'Error occurred while trying to setup the Express Checkout.', |
|
244 | - 'event_espresso' |
|
245 | - ) |
|
246 | - ); |
|
247 | - } |
|
248 | - $payment->set_details($response_args); |
|
249 | - $payment->set_status($this->_pay_model->failed_status()); |
|
250 | - } |
|
251 | - return $payment; |
|
252 | - } |
|
253 | - |
|
254 | - |
|
255 | - |
|
256 | - /** |
|
257 | - * @param array $update_info { |
|
258 | - * @type string $gateway_txn_id |
|
259 | - * @type string status an EEMI_Payment status |
|
260 | - * } |
|
261 | - * @param EEI_Transaction $transaction |
|
262 | - * @return EEI_Payment |
|
263 | - */ |
|
264 | - public function handle_payment_update($update_info, $transaction) |
|
265 | - { |
|
266 | - $payment = $transaction instanceof EEI_Transaction ? $transaction->last_payment() : null; |
|
267 | - if ($payment instanceof EEI_Payment) { |
|
268 | - $this->log(array('Return from Authorization' => $update_info), $payment); |
|
269 | - $transaction = $payment->transaction(); |
|
270 | - if (! $transaction instanceof EEI_Transaction) { |
|
271 | - $payment->set_gateway_response( |
|
272 | - esc_html__( |
|
273 | - 'Could not process this payment because it has no associated transaction.', |
|
274 | - 'event_espresso' |
|
275 | - ) |
|
276 | - ); |
|
277 | - $payment->set_status($this->_pay_model->failed_status()); |
|
278 | - return $payment; |
|
279 | - } |
|
280 | - $primary_registrant = $transaction->primary_registration(); |
|
281 | - $payment_details = $payment->details(); |
|
282 | - // Check if we still have the token. |
|
283 | - if (! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN'])) { |
|
284 | - $payment->set_status($this->_pay_model->failed_status()); |
|
285 | - return $payment; |
|
286 | - } |
|
287 | - $cdetails_request_dtls = array( |
|
288 | - 'METHOD' => 'GetExpressCheckoutDetails', |
|
289 | - 'TOKEN' => $payment_details['TOKEN'], |
|
290 | - ); |
|
291 | - // Request Customer Details. |
|
292 | - $cdetails_request_response = $this->_ppExpress_request( |
|
293 | - $cdetails_request_dtls, |
|
294 | - 'Customer Details', |
|
295 | - $payment |
|
296 | - ); |
|
297 | - $cdetails_rstatus = $this->_ppExpress_check_response($cdetails_request_response); |
|
298 | - $cdata_response_args = (isset($cdetails_rstatus['args']) && is_array($cdetails_rstatus['args'])) |
|
299 | - ? $cdetails_rstatus['args'] |
|
300 | - : array(); |
|
301 | - if ($cdetails_rstatus['status']) { |
|
302 | - // We got the PayerID so now we can Complete the transaction. |
|
303 | - $docheckout_request_dtls = array( |
|
304 | - 'METHOD' => 'DoExpressCheckoutPayment', |
|
305 | - 'PAYERID' => $cdata_response_args['PAYERID'], |
|
306 | - 'TOKEN' => $payment_details['TOKEN'], |
|
307 | - 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
308 | - 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
309 | - 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
310 | - //EE will blow up if you change this |
|
311 | - 'BUTTONSOURCE' => 'EventEspresso_SP', |
|
312 | - ); |
|
313 | - // Include itemized list. |
|
314 | - $itemized_list = $this->itemize_list( |
|
315 | - $payment, |
|
316 | - $transaction, |
|
317 | - $cdata_response_args |
|
318 | - ); |
|
319 | - $docheckout_request_dtls = array_merge($docheckout_request_dtls, $itemized_list); |
|
320 | - // Payment Checkout/Capture. |
|
321 | - $docheckout_request_response = $this->_ppExpress_request( |
|
322 | - $docheckout_request_dtls, |
|
323 | - 'Do Payment', |
|
324 | - $payment |
|
325 | - ); |
|
326 | - $docheckout_rstatus = $this->_ppExpress_check_response($docheckout_request_response); |
|
327 | - $docheckout_response_args = (isset($docheckout_rstatus['args']) && is_array($docheckout_rstatus['args'])) |
|
328 | - ? $docheckout_rstatus['args'] |
|
329 | - : array(); |
|
330 | - if ($docheckout_rstatus['status']) { |
|
331 | - // All is well, payment approved. |
|
332 | - $primary_registration_code = $primary_registrant instanceof EE_Registration ? |
|
333 | - $primary_registrant->reg_code() |
|
334 | - : ''; |
|
335 | - $payment->set_extra_accntng($primary_registration_code); |
|
336 | - $payment->set_amount(isset($docheckout_response_args['PAYMENTINFO_0_AMT']) |
|
337 | - ? (float)$docheckout_response_args['PAYMENTINFO_0_AMT'] |
|
338 | - : 0); |
|
339 | - $payment->set_txn_id_chq_nmbr(isset($docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
340 | - ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] |
|
341 | - : null); |
|
342 | - $payment->set_details($cdata_response_args); |
|
343 | - $payment->set_gateway_response(isset($docheckout_response_args['PAYMENTINFO_0_ACK']) |
|
344 | - ? $docheckout_response_args['PAYMENTINFO_0_ACK'] |
|
345 | - : ''); |
|
346 | - $payment->set_status($this->_pay_model->approved_status()); |
|
347 | - } else { |
|
348 | - if (isset($docheckout_response_args['L_ERRORCODE'])) { |
|
349 | - $payment->set_gateway_response( |
|
350 | - $docheckout_response_args['L_ERRORCODE'] |
|
351 | - . '; ' |
|
352 | - . $docheckout_response_args['L_SHORTMESSAGE'] |
|
353 | - ); |
|
354 | - } else { |
|
355 | - $payment->set_gateway_response( |
|
356 | - esc_html__( |
|
357 | - 'Error occurred while trying to Capture the funds.', |
|
358 | - 'event_espresso' |
|
359 | - ) |
|
360 | - ); |
|
361 | - } |
|
362 | - $payment->set_details($docheckout_response_args); |
|
363 | - $payment->set_status($this->_pay_model->declined_status()); |
|
364 | - } |
|
365 | - } else { |
|
366 | - if (isset($cdata_response_args['L_ERRORCODE'])) { |
|
367 | - $payment->set_gateway_response( |
|
368 | - $cdata_response_args['L_ERRORCODE'] |
|
369 | - . '; ' |
|
370 | - . $cdata_response_args['L_SHORTMESSAGE'] |
|
371 | - ); |
|
372 | - } else { |
|
373 | - $payment->set_gateway_response( |
|
374 | - esc_html__( |
|
375 | - 'Error occurred while trying to get payment Details from PayPal.', |
|
376 | - 'event_espresso' |
|
377 | - ) |
|
378 | - ); |
|
379 | - } |
|
380 | - $payment->set_details($cdata_response_args); |
|
381 | - $payment->set_status($this->_pay_model->failed_status()); |
|
382 | - } |
|
383 | - } else { |
|
384 | - $payment->set_gateway_response( |
|
385 | - esc_html__( |
|
386 | - 'Error occurred while trying to process the payment.', |
|
387 | - 'event_espresso' |
|
388 | - ) |
|
389 | - ); |
|
390 | - $payment->set_status($this->_pay_model->failed_status()); |
|
391 | - } |
|
392 | - return $payment; |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - |
|
397 | - /** |
|
398 | - * Make a list of items that are in the giver transaction. |
|
399 | - * |
|
400 | - * @param EEI_Payment $payment |
|
401 | - * @param EEI_Transaction $transaction |
|
402 | - * @param array $request_response_args Data from a previous communication with PP. |
|
403 | - * @return array |
|
404 | - */ |
|
405 | - public function itemize_list(EEI_Payment $payment, EEI_Transaction $transaction, $request_response_args = array()) |
|
406 | - { |
|
407 | - $itemized_list = array(); |
|
408 | - $gateway_formatter = $this->_get_gateway_formatter(); |
|
409 | - // If we have data from a previous communication with PP (on this transaction) we may use that for our list... |
|
410 | - if ( |
|
411 | - ! empty($request_response_args) |
|
412 | - && array_key_exists('L_PAYMENTREQUEST_0_AMT0', $request_response_args) |
|
413 | - && array_key_exists('PAYMENTREQUEST_0_ITEMAMT', $request_response_args) |
|
414 | - ) { |
|
415 | - foreach ($request_response_args as $arg_key => $arg_val) { |
|
416 | - if ( |
|
417 | - strpos($arg_key, 'PAYMENTREQUEST_') !== false |
|
418 | - && strpos($arg_key, 'NOTIFYURL') === false |
|
419 | - ) { |
|
420 | - $itemized_list[$arg_key] = $arg_val; |
|
421 | - } |
|
422 | - } |
|
423 | - // If we got only a few Items then something is not right. |
|
424 | - if (count($itemized_list) > 2) { |
|
425 | - return $itemized_list; |
|
426 | - } else { |
|
427 | - if (WP_DEBUG) { |
|
428 | - throw new EE_Error( |
|
429 | - sprintf( |
|
430 | - esc_html__( |
|
431 | - // @codingStandardsIgnoreStart |
|
432 | - 'Unable to continue with the checkout because a proper purchase list could not be generated. The purchased list we could have sent was %1$s', |
|
433 | - // @codingStandardsIgnoreEnd |
|
434 | - 'event_espresso' |
|
435 | - ), |
|
436 | - wp_json_encode($itemized_list) |
|
437 | - ) |
|
438 | - ); |
|
439 | - } |
|
440 | - // Reset the list and log an error, maybe allow to try and generate a new list (below). |
|
441 | - $itemized_list = array(); |
|
442 | - $this->log( |
|
443 | - array( |
|
444 | - esc_html__( |
|
445 | - 'Could not generate a proper item list with:', |
|
446 | - 'event_espresso' |
|
447 | - ) => $request_response_args |
|
448 | - ), |
|
449 | - $payment |
|
450 | - ); |
|
451 | - } |
|
452 | - } |
|
453 | - // ...otherwise we generate a new list for this transaction. |
|
454 | - if ($this->_money->compare_floats($payment->amount(), $transaction->total(), '==')) { |
|
455 | - $item_num = 0; |
|
456 | - $itemized_sum = 0; |
|
457 | - $total_line_items = $transaction->total_line_item(); |
|
458 | - // Go through each item in the list. |
|
459 | - foreach ($total_line_items->get_items() as $line_item) { |
|
460 | - if ($line_item instanceof EE_Line_Item) { |
|
461 | - // PayPal doesn't like line items with 0.00 amount, so we may skip those. |
|
462 | - if (EEH_Money::compare_floats($line_item->total(), '0.00', '==')) { |
|
463 | - continue; |
|
464 | - } |
|
465 | - $unit_price = $line_item->unit_price(); |
|
466 | - $line_item_quantity = $line_item->quantity(); |
|
467 | - // This is a discount. |
|
468 | - if ($line_item->is_percent()) { |
|
469 | - $unit_price = $line_item->total(); |
|
470 | - $line_item_quantity = 1; |
|
471 | - } |
|
472 | - // Item Name. |
|
473 | - $itemized_list['L_PAYMENTREQUEST_0_NAME' . $item_num] = mb_strcut( |
|
474 | - $gateway_formatter->formatLineItemName($line_item, $payment), |
|
475 | - 0, |
|
476 | - 127 |
|
477 | - ); |
|
478 | - // Item description. |
|
479 | - $itemized_list['L_PAYMENTREQUEST_0_DESC' . $item_num] = mb_strcut( |
|
480 | - $gateway_formatter->formatLineItemDesc($line_item, $payment), |
|
481 | - 0, |
|
482 | - 127 |
|
483 | - ); |
|
484 | - // Cost of individual item. |
|
485 | - $itemized_list['L_PAYMENTREQUEST_0_AMT' . $item_num] = $gateway_formatter->formatCurrency($unit_price); |
|
486 | - // Item Number. |
|
487 | - $itemized_list['L_PAYMENTREQUEST_0_NUMBER' . $item_num] = $item_num + 1; |
|
488 | - // Item quantity. |
|
489 | - $itemized_list['L_PAYMENTREQUEST_0_QTY' . $item_num] = $line_item_quantity; |
|
490 | - // Digital item is sold. |
|
491 | - $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num] = 'Physical'; |
|
492 | - $itemized_sum += $line_item->total(); |
|
493 | - ++$item_num; |
|
494 | - } |
|
495 | - } |
|
496 | - // Item's sales S/H and tax amount. |
|
497 | - $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $total_line_items->get_items_total(); |
|
498 | - $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = $total_line_items->get_total_tax(); |
|
499 | - $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
500 | - $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
501 | - $itemized_sum_diff_from_txn_total = round( |
|
502 | - $transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), |
|
503 | - 2 |
|
504 | - ); |
|
505 | - // If we were not able to recognize some item like promotion, surcharge or cancellation, |
|
506 | - // add the difference as an extra line item. |
|
507 | - if ($this->_money->compare_floats($itemized_sum_diff_from_txn_total, 0, '!=')) { |
|
508 | - // Item Name. |
|
509 | - $itemized_list['L_PAYMENTREQUEST_0_NAME' . $item_num] = mb_strcut( |
|
510 | - esc_html__( |
|
511 | - 'Other (promotion/surcharge/cancellation)', |
|
512 | - 'event_espresso' |
|
513 | - ), |
|
514 | - 0, |
|
515 | - 127 |
|
516 | - ); |
|
517 | - // Item description. |
|
518 | - $itemized_list['L_PAYMENTREQUEST_0_DESC' . $item_num] = ''; |
|
519 | - // Cost of individual item. |
|
520 | - $itemized_list['L_PAYMENTREQUEST_0_AMT' . $item_num] = $gateway_formatter->formatCurrency( |
|
521 | - $itemized_sum_diff_from_txn_total |
|
522 | - ); |
|
523 | - // Item Number. |
|
524 | - $itemized_list['L_PAYMENTREQUEST_0_NUMBER' . $item_num] = $item_num + 1; |
|
525 | - // Item quantity. |
|
526 | - $itemized_list['L_PAYMENTREQUEST_0_QTY' . $item_num] = 1; |
|
527 | - // Digital item is sold. |
|
528 | - $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num] = 'Physical'; |
|
529 | - $item_num++; |
|
530 | - } |
|
531 | - } else { |
|
532 | - // Just one Item. |
|
533 | - // Item Name. |
|
534 | - $itemized_list['L_PAYMENTREQUEST_0_NAME0'] = mb_strcut( |
|
535 | - $gateway_formatter->formatPartialPaymentLineItemName($payment), |
|
536 | - 0, |
|
537 | - 127 |
|
538 | - ); |
|
539 | - // Item description. |
|
540 | - $itemized_list['L_PAYMENTREQUEST_0_DESC0'] = mb_strcut( |
|
541 | - $gateway_formatter->formatPartialPaymentLineItemDesc($payment), |
|
542 | - 0, |
|
543 | - 127 |
|
544 | - ); |
|
545 | - // Cost of individual item. |
|
546 | - $itemized_list['L_PAYMENTREQUEST_0_AMT0'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
547 | - // Item Number. |
|
548 | - $itemized_list['L_PAYMENTREQUEST_0_NUMBER0'] = 1; |
|
549 | - // Item quantity. |
|
550 | - $itemized_list['L_PAYMENTREQUEST_0_QTY0'] = 1; |
|
551 | - // Digital item is sold. |
|
552 | - $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Physical'; |
|
553 | - // Item's sales S/H and tax amount. |
|
554 | - $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
555 | - $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = '0'; |
|
556 | - $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
557 | - $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
558 | - } |
|
559 | - return $itemized_list; |
|
560 | - } |
|
561 | - |
|
562 | - |
|
563 | - |
|
564 | - /** |
|
565 | - * Make the Express checkout request. |
|
566 | - * |
|
567 | - * @param array $request_params |
|
568 | - * @param string $request_text |
|
569 | - * @param EEI_Payment $payment |
|
570 | - * @return mixed |
|
571 | - */ |
|
572 | - public function _ppExpress_request($request_params, $request_text, $payment) |
|
573 | - { |
|
574 | - $request_dtls = array( |
|
575 | - 'VERSION' => '204.0', |
|
576 | - 'USER' => urlencode($this->_api_username), |
|
577 | - 'PWD' => urlencode($this->_api_password), |
|
578 | - 'SIGNATURE' => urlencode($this->_api_signature), |
|
579 | - ); |
|
580 | - $dtls = array_merge($request_dtls, $request_params); |
|
581 | - $this->_log_clean_request($dtls, $payment, $request_text . ' Request'); |
|
582 | - // Request Customer Details. |
|
583 | - $request_response = wp_remote_post( |
|
584 | - $this->_base_gateway_url, |
|
585 | - array( |
|
586 | - 'method' => 'POST', |
|
587 | - 'timeout' => 45, |
|
588 | - 'httpversion' => '1.1', |
|
589 | - 'cookies' => array(), |
|
590 | - 'headers' => array(), |
|
591 | - 'body' => http_build_query($dtls, '', '&'), |
|
592 | - ) |
|
593 | - ); |
|
594 | - // Log the response. |
|
595 | - $this->log(array($request_text . ' Response' => $request_response), $payment); |
|
596 | - return $request_response; |
|
597 | - } |
|
598 | - |
|
599 | - |
|
600 | - |
|
601 | - /** |
|
602 | - * Check the response status. |
|
603 | - * |
|
604 | - * @param mixed $request_response |
|
605 | - * @return array |
|
606 | - */ |
|
607 | - public function _ppExpress_check_response($request_response) |
|
608 | - { |
|
609 | - if (is_wp_error($request_response) || empty($request_response['body'])) { |
|
610 | - // If we got here then there was an error in this request. |
|
611 | - return array('status' => false, 'args' => $request_response); |
|
612 | - } |
|
613 | - $response_args = array(); |
|
614 | - parse_str(urldecode($request_response['body']), $response_args); |
|
615 | - if (! isset($response_args['ACK'])) { |
|
616 | - return array('status' => false, 'args' => $request_response); |
|
617 | - } |
|
618 | - if ( |
|
619 | - ( |
|
620 | - isset($response_args['PAYERID']) |
|
621 | - || isset($response_args['TOKEN']) |
|
622 | - || isset($response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
623 | - || (isset($response_args['PAYMENTSTATUS']) && $response_args['PAYMENTSTATUS'] === 'Completed') |
|
624 | - ) |
|
625 | - && in_array($response_args['ACK'], array('Success', 'SuccessWithWarning'), true) |
|
626 | - ) { |
|
627 | - // Response status OK, return response parameters for further processing. |
|
628 | - return array('status' => true, 'args' => $response_args); |
|
629 | - } |
|
630 | - $errors = $this->_get_errors($response_args); |
|
631 | - return array('status' => false, 'args' => $errors); |
|
632 | - } |
|
633 | - |
|
634 | - |
|
635 | - |
|
636 | - /** |
|
637 | - * Log a "Cleared" request. |
|
638 | - * |
|
639 | - * @param array $request |
|
640 | - * @param EEI_Payment $payment |
|
641 | - * @param string $info |
|
642 | - * @return void |
|
643 | - */ |
|
644 | - private function _log_clean_request($request, $payment, $info) |
|
645 | - { |
|
646 | - $cleaned_request_data = $request; |
|
647 | - unset($cleaned_request_data['PWD'], $cleaned_request_data['USER'], $cleaned_request_data['SIGNATURE']); |
|
648 | - $this->log(array($info => $cleaned_request_data), $payment); |
|
649 | - } |
|
650 | - |
|
651 | - |
|
652 | - |
|
653 | - /** |
|
654 | - * Get error from the response data. |
|
655 | - * |
|
656 | - * @param array $data_array |
|
657 | - * @return array |
|
658 | - */ |
|
659 | - private function _get_errors($data_array) |
|
660 | - { |
|
661 | - $errors = array(); |
|
662 | - $n = 0; |
|
663 | - while (isset($data_array["L_ERRORCODE{$n}"])) { |
|
664 | - $l_error_code = isset($data_array["L_ERRORCODE{$n}"]) |
|
665 | - ? $data_array["L_ERRORCODE{$n}"] |
|
666 | - : ''; |
|
667 | - $l_severity_code = isset($data_array["L_SEVERITYCODE{$n}"]) |
|
668 | - ? $data_array["L_SEVERITYCODE{$n}"] |
|
669 | - : ''; |
|
670 | - $l_short_message = isset($data_array["L_SHORTMESSAGE{$n}"]) |
|
671 | - ? $data_array["L_SHORTMESSAGE{$n}"] |
|
672 | - : ''; |
|
673 | - $l_long_message = isset($data_array["L_LONGMESSAGE{$n}"]) |
|
674 | - ? $data_array["L_LONGMESSAGE{$n}"] |
|
675 | - : ''; |
|
676 | - if ($n === 0) { |
|
677 | - $errors = array( |
|
678 | - 'L_ERRORCODE' => $l_error_code, |
|
679 | - 'L_SHORTMESSAGE' => $l_short_message, |
|
680 | - 'L_LONGMESSAGE' => $l_long_message, |
|
681 | - 'L_SEVERITYCODE' => $l_severity_code, |
|
682 | - ); |
|
683 | - } else { |
|
684 | - $errors['L_ERRORCODE'] .= ', ' . $l_error_code; |
|
685 | - $errors['L_SHORTMESSAGE'] .= ', ' . $l_short_message; |
|
686 | - $errors['L_LONGMESSAGE'] .= ', ' . $l_long_message; |
|
687 | - $errors['L_SEVERITYCODE'] .= ', ' . $l_severity_code; |
|
688 | - } |
|
689 | - $n++; |
|
690 | - } |
|
691 | - return $errors; |
|
692 | - } |
|
35 | + /** |
|
36 | + * Merchant API Username. |
|
37 | + * |
|
38 | + * @var string |
|
39 | + */ |
|
40 | + protected $_api_username; |
|
41 | + |
|
42 | + /** |
|
43 | + * Merchant API Password. |
|
44 | + * |
|
45 | + * @var string |
|
46 | + */ |
|
47 | + protected $_api_password; |
|
48 | + |
|
49 | + /** |
|
50 | + * API Signature. |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $_api_signature; |
|
55 | + |
|
56 | + /** |
|
57 | + * Request Shipping address on PP checkout page. |
|
58 | + * |
|
59 | + * @var string |
|
60 | + */ |
|
61 | + protected $_request_shipping_addr; |
|
62 | + |
|
63 | + /** |
|
64 | + * Business/personal logo. |
|
65 | + * |
|
66 | + * @var string |
|
67 | + */ |
|
68 | + protected $_image_url; |
|
69 | + |
|
70 | + /** |
|
71 | + * gateway URL variable |
|
72 | + * |
|
73 | + * @var string |
|
74 | + */ |
|
75 | + protected $_base_gateway_url = ''; |
|
76 | + |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * EEG_Paypal_Express constructor. |
|
81 | + */ |
|
82 | + public function __construct() |
|
83 | + { |
|
84 | + $this->_currencies_supported = array( |
|
85 | + 'USD', |
|
86 | + 'AUD', |
|
87 | + 'BRL', |
|
88 | + 'CAD', |
|
89 | + 'CZK', |
|
90 | + 'DKK', |
|
91 | + 'EUR', |
|
92 | + 'HKD', |
|
93 | + 'HUF', |
|
94 | + 'ILS', |
|
95 | + 'JPY', |
|
96 | + 'MYR', |
|
97 | + 'MXN', |
|
98 | + 'NOK', |
|
99 | + 'NZD', |
|
100 | + 'PHP', |
|
101 | + 'PLN', |
|
102 | + 'GBP', |
|
103 | + 'RUB', |
|
104 | + 'SGD', |
|
105 | + 'SEK', |
|
106 | + 'CHF', |
|
107 | + 'TWD', |
|
108 | + 'THB', |
|
109 | + 'TRY', |
|
110 | + ); |
|
111 | + parent::__construct(); |
|
112 | + } |
|
113 | + |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * Sets the gateway URL variable based on whether debug mode is enabled or not. |
|
118 | + * |
|
119 | + * @param array $settings_array |
|
120 | + */ |
|
121 | + public function set_settings($settings_array) |
|
122 | + { |
|
123 | + parent::set_settings($settings_array); |
|
124 | + // Redirect URL. |
|
125 | + $this->_base_gateway_url = $this->_debug_mode |
|
126 | + ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
127 | + : 'https://api-3t.paypal.com/nvp'; |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + |
|
132 | + /** |
|
133 | + * @param EEI_Payment $payment |
|
134 | + * @param array $billing_info |
|
135 | + * @param string $return_url |
|
136 | + * @param string $notify_url |
|
137 | + * @param string $cancel_url |
|
138 | + * @return \EE_Payment|\EEI_Payment |
|
139 | + * @throws \EE_Error |
|
140 | + */ |
|
141 | + public function set_redirection_info( |
|
142 | + $payment, |
|
143 | + $billing_info = array(), |
|
144 | + $return_url = null, |
|
145 | + $notify_url = null, |
|
146 | + $cancel_url = null |
|
147 | + ) { |
|
148 | + if (! $payment instanceof EEI_Payment) { |
|
149 | + $payment->set_gateway_response( |
|
150 | + esc_html__( |
|
151 | + 'Error. No associated payment was found.', |
|
152 | + 'event_espresso' |
|
153 | + ) |
|
154 | + ); |
|
155 | + $payment->set_status($this->_pay_model->failed_status()); |
|
156 | + return $payment; |
|
157 | + } |
|
158 | + $transaction = $payment->transaction(); |
|
159 | + if (! $transaction instanceof EEI_Transaction) { |
|
160 | + $payment->set_gateway_response( |
|
161 | + esc_html__( |
|
162 | + 'Could not process this payment because it has no associated transaction.', |
|
163 | + 'event_espresso' |
|
164 | + ) |
|
165 | + ); |
|
166 | + $payment->set_status($this->_pay_model->failed_status()); |
|
167 | + return $payment; |
|
168 | + } |
|
169 | + $gateway_formatter = $this->_get_gateway_formatter(); |
|
170 | + $order_description = mb_strcut($gateway_formatter->formatOrderDescription($payment), 0, 127); |
|
171 | + $primary_registration = $transaction->primary_registration(); |
|
172 | + $primary_attendee = $primary_registration instanceof EE_Registration |
|
173 | + ? $primary_registration->attendee() |
|
174 | + : false; |
|
175 | + $locale = explode('-', get_bloginfo('language')); |
|
176 | + // Gather request parameters. |
|
177 | + $token_request_dtls = array( |
|
178 | + 'METHOD' => 'SetExpressCheckout', |
|
179 | + 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
180 | + 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
181 | + 'PAYMENTREQUEST_0_DESC' => $order_description, |
|
182 | + 'RETURNURL' => $return_url, |
|
183 | + 'CANCELURL' => $cancel_url, |
|
184 | + 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
185 | + // Buyer does not need to create a PayPal account to check out. |
|
186 | + // This is referred to as PayPal Account Optional. |
|
187 | + 'SOLUTIONTYPE' => 'Sole', |
|
188 | + //EE will blow up if you change this |
|
189 | + 'BUTTONSOURCE' => 'EventEspresso_SP', |
|
190 | + // Locale of the pages displayed by PayPal during Express Checkout. |
|
191 | + 'LOCALECODE' => $locale[1] |
|
192 | + ); |
|
193 | + // Show itemized list. |
|
194 | + $itemized_list = $this->itemize_list($payment, $transaction); |
|
195 | + $token_request_dtls = array_merge($token_request_dtls, $itemized_list); |
|
196 | + // Automatically filling out shipping and contact information. |
|
197 | + if ($this->_request_shipping_addr && $primary_attendee instanceof EEI_Attendee) { |
|
198 | + // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
|
199 | + $token_request_dtls['NOSHIPPING'] = '2'; |
|
200 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET'] = $primary_attendee->address(); |
|
201 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $primary_attendee->address2(); |
|
202 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCITY'] = $primary_attendee->city(); |
|
203 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTATE'] = $primary_attendee->state_abbrev(); |
|
204 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $primary_attendee->country_ID(); |
|
205 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
|
206 | + $token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
|
207 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
|
208 | + } elseif (! $this->_request_shipping_addr) { |
|
209 | + // Do not request shipping details on the PP Checkout page. |
|
210 | + $token_request_dtls['NOSHIPPING'] = '1'; |
|
211 | + $token_request_dtls['REQCONFIRMSHIPPING'] = '0'; |
|
212 | + } |
|
213 | + // Used a business/personal logo on the PayPal page. |
|
214 | + if (! empty($this->_image_url)) { |
|
215 | + $token_request_dtls['LOGOIMG'] = $this->_image_url; |
|
216 | + } |
|
217 | + $token_request_dtls = apply_filters( |
|
218 | + 'FHEE__EEG_Paypal_Express__set_redirection_info__arguments', |
|
219 | + $token_request_dtls, |
|
220 | + $this |
|
221 | + ); |
|
222 | + // Request PayPal token. |
|
223 | + $token_request_response = $this->_ppExpress_request($token_request_dtls, 'Payment Token', $payment); |
|
224 | + $token_rstatus = $this->_ppExpress_check_response($token_request_response); |
|
225 | + $response_args = (isset($token_rstatus['args']) && is_array($token_rstatus['args'])) |
|
226 | + ? $token_rstatus['args'] |
|
227 | + : array(); |
|
228 | + if ($token_rstatus['status']) { |
|
229 | + // We got the Token so we may continue with the payment and redirect the client. |
|
230 | + $payment->set_details($response_args); |
|
231 | + $gateway_url = $this->_debug_mode ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; |
|
232 | + $payment->set_redirect_url( |
|
233 | + $gateway_url |
|
234 | + . '/checkoutnow?useraction=commit&cmd=_express-checkout&token=' |
|
235 | + . $response_args['TOKEN'] |
|
236 | + ); |
|
237 | + } else { |
|
238 | + if (isset($response_args['L_ERRORCODE'])) { |
|
239 | + $payment->set_gateway_response($response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE']); |
|
240 | + } else { |
|
241 | + $payment->set_gateway_response( |
|
242 | + esc_html__( |
|
243 | + 'Error occurred while trying to setup the Express Checkout.', |
|
244 | + 'event_espresso' |
|
245 | + ) |
|
246 | + ); |
|
247 | + } |
|
248 | + $payment->set_details($response_args); |
|
249 | + $payment->set_status($this->_pay_model->failed_status()); |
|
250 | + } |
|
251 | + return $payment; |
|
252 | + } |
|
253 | + |
|
254 | + |
|
255 | + |
|
256 | + /** |
|
257 | + * @param array $update_info { |
|
258 | + * @type string $gateway_txn_id |
|
259 | + * @type string status an EEMI_Payment status |
|
260 | + * } |
|
261 | + * @param EEI_Transaction $transaction |
|
262 | + * @return EEI_Payment |
|
263 | + */ |
|
264 | + public function handle_payment_update($update_info, $transaction) |
|
265 | + { |
|
266 | + $payment = $transaction instanceof EEI_Transaction ? $transaction->last_payment() : null; |
|
267 | + if ($payment instanceof EEI_Payment) { |
|
268 | + $this->log(array('Return from Authorization' => $update_info), $payment); |
|
269 | + $transaction = $payment->transaction(); |
|
270 | + if (! $transaction instanceof EEI_Transaction) { |
|
271 | + $payment->set_gateway_response( |
|
272 | + esc_html__( |
|
273 | + 'Could not process this payment because it has no associated transaction.', |
|
274 | + 'event_espresso' |
|
275 | + ) |
|
276 | + ); |
|
277 | + $payment->set_status($this->_pay_model->failed_status()); |
|
278 | + return $payment; |
|
279 | + } |
|
280 | + $primary_registrant = $transaction->primary_registration(); |
|
281 | + $payment_details = $payment->details(); |
|
282 | + // Check if we still have the token. |
|
283 | + if (! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN'])) { |
|
284 | + $payment->set_status($this->_pay_model->failed_status()); |
|
285 | + return $payment; |
|
286 | + } |
|
287 | + $cdetails_request_dtls = array( |
|
288 | + 'METHOD' => 'GetExpressCheckoutDetails', |
|
289 | + 'TOKEN' => $payment_details['TOKEN'], |
|
290 | + ); |
|
291 | + // Request Customer Details. |
|
292 | + $cdetails_request_response = $this->_ppExpress_request( |
|
293 | + $cdetails_request_dtls, |
|
294 | + 'Customer Details', |
|
295 | + $payment |
|
296 | + ); |
|
297 | + $cdetails_rstatus = $this->_ppExpress_check_response($cdetails_request_response); |
|
298 | + $cdata_response_args = (isset($cdetails_rstatus['args']) && is_array($cdetails_rstatus['args'])) |
|
299 | + ? $cdetails_rstatus['args'] |
|
300 | + : array(); |
|
301 | + if ($cdetails_rstatus['status']) { |
|
302 | + // We got the PayerID so now we can Complete the transaction. |
|
303 | + $docheckout_request_dtls = array( |
|
304 | + 'METHOD' => 'DoExpressCheckoutPayment', |
|
305 | + 'PAYERID' => $cdata_response_args['PAYERID'], |
|
306 | + 'TOKEN' => $payment_details['TOKEN'], |
|
307 | + 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
308 | + 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
309 | + 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
310 | + //EE will blow up if you change this |
|
311 | + 'BUTTONSOURCE' => 'EventEspresso_SP', |
|
312 | + ); |
|
313 | + // Include itemized list. |
|
314 | + $itemized_list = $this->itemize_list( |
|
315 | + $payment, |
|
316 | + $transaction, |
|
317 | + $cdata_response_args |
|
318 | + ); |
|
319 | + $docheckout_request_dtls = array_merge($docheckout_request_dtls, $itemized_list); |
|
320 | + // Payment Checkout/Capture. |
|
321 | + $docheckout_request_response = $this->_ppExpress_request( |
|
322 | + $docheckout_request_dtls, |
|
323 | + 'Do Payment', |
|
324 | + $payment |
|
325 | + ); |
|
326 | + $docheckout_rstatus = $this->_ppExpress_check_response($docheckout_request_response); |
|
327 | + $docheckout_response_args = (isset($docheckout_rstatus['args']) && is_array($docheckout_rstatus['args'])) |
|
328 | + ? $docheckout_rstatus['args'] |
|
329 | + : array(); |
|
330 | + if ($docheckout_rstatus['status']) { |
|
331 | + // All is well, payment approved. |
|
332 | + $primary_registration_code = $primary_registrant instanceof EE_Registration ? |
|
333 | + $primary_registrant->reg_code() |
|
334 | + : ''; |
|
335 | + $payment->set_extra_accntng($primary_registration_code); |
|
336 | + $payment->set_amount(isset($docheckout_response_args['PAYMENTINFO_0_AMT']) |
|
337 | + ? (float)$docheckout_response_args['PAYMENTINFO_0_AMT'] |
|
338 | + : 0); |
|
339 | + $payment->set_txn_id_chq_nmbr(isset($docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
340 | + ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] |
|
341 | + : null); |
|
342 | + $payment->set_details($cdata_response_args); |
|
343 | + $payment->set_gateway_response(isset($docheckout_response_args['PAYMENTINFO_0_ACK']) |
|
344 | + ? $docheckout_response_args['PAYMENTINFO_0_ACK'] |
|
345 | + : ''); |
|
346 | + $payment->set_status($this->_pay_model->approved_status()); |
|
347 | + } else { |
|
348 | + if (isset($docheckout_response_args['L_ERRORCODE'])) { |
|
349 | + $payment->set_gateway_response( |
|
350 | + $docheckout_response_args['L_ERRORCODE'] |
|
351 | + . '; ' |
|
352 | + . $docheckout_response_args['L_SHORTMESSAGE'] |
|
353 | + ); |
|
354 | + } else { |
|
355 | + $payment->set_gateway_response( |
|
356 | + esc_html__( |
|
357 | + 'Error occurred while trying to Capture the funds.', |
|
358 | + 'event_espresso' |
|
359 | + ) |
|
360 | + ); |
|
361 | + } |
|
362 | + $payment->set_details($docheckout_response_args); |
|
363 | + $payment->set_status($this->_pay_model->declined_status()); |
|
364 | + } |
|
365 | + } else { |
|
366 | + if (isset($cdata_response_args['L_ERRORCODE'])) { |
|
367 | + $payment->set_gateway_response( |
|
368 | + $cdata_response_args['L_ERRORCODE'] |
|
369 | + . '; ' |
|
370 | + . $cdata_response_args['L_SHORTMESSAGE'] |
|
371 | + ); |
|
372 | + } else { |
|
373 | + $payment->set_gateway_response( |
|
374 | + esc_html__( |
|
375 | + 'Error occurred while trying to get payment Details from PayPal.', |
|
376 | + 'event_espresso' |
|
377 | + ) |
|
378 | + ); |
|
379 | + } |
|
380 | + $payment->set_details($cdata_response_args); |
|
381 | + $payment->set_status($this->_pay_model->failed_status()); |
|
382 | + } |
|
383 | + } else { |
|
384 | + $payment->set_gateway_response( |
|
385 | + esc_html__( |
|
386 | + 'Error occurred while trying to process the payment.', |
|
387 | + 'event_espresso' |
|
388 | + ) |
|
389 | + ); |
|
390 | + $payment->set_status($this->_pay_model->failed_status()); |
|
391 | + } |
|
392 | + return $payment; |
|
393 | + } |
|
394 | + |
|
395 | + |
|
396 | + |
|
397 | + /** |
|
398 | + * Make a list of items that are in the giver transaction. |
|
399 | + * |
|
400 | + * @param EEI_Payment $payment |
|
401 | + * @param EEI_Transaction $transaction |
|
402 | + * @param array $request_response_args Data from a previous communication with PP. |
|
403 | + * @return array |
|
404 | + */ |
|
405 | + public function itemize_list(EEI_Payment $payment, EEI_Transaction $transaction, $request_response_args = array()) |
|
406 | + { |
|
407 | + $itemized_list = array(); |
|
408 | + $gateway_formatter = $this->_get_gateway_formatter(); |
|
409 | + // If we have data from a previous communication with PP (on this transaction) we may use that for our list... |
|
410 | + if ( |
|
411 | + ! empty($request_response_args) |
|
412 | + && array_key_exists('L_PAYMENTREQUEST_0_AMT0', $request_response_args) |
|
413 | + && array_key_exists('PAYMENTREQUEST_0_ITEMAMT', $request_response_args) |
|
414 | + ) { |
|
415 | + foreach ($request_response_args as $arg_key => $arg_val) { |
|
416 | + if ( |
|
417 | + strpos($arg_key, 'PAYMENTREQUEST_') !== false |
|
418 | + && strpos($arg_key, 'NOTIFYURL') === false |
|
419 | + ) { |
|
420 | + $itemized_list[$arg_key] = $arg_val; |
|
421 | + } |
|
422 | + } |
|
423 | + // If we got only a few Items then something is not right. |
|
424 | + if (count($itemized_list) > 2) { |
|
425 | + return $itemized_list; |
|
426 | + } else { |
|
427 | + if (WP_DEBUG) { |
|
428 | + throw new EE_Error( |
|
429 | + sprintf( |
|
430 | + esc_html__( |
|
431 | + // @codingStandardsIgnoreStart |
|
432 | + 'Unable to continue with the checkout because a proper purchase list could not be generated. The purchased list we could have sent was %1$s', |
|
433 | + // @codingStandardsIgnoreEnd |
|
434 | + 'event_espresso' |
|
435 | + ), |
|
436 | + wp_json_encode($itemized_list) |
|
437 | + ) |
|
438 | + ); |
|
439 | + } |
|
440 | + // Reset the list and log an error, maybe allow to try and generate a new list (below). |
|
441 | + $itemized_list = array(); |
|
442 | + $this->log( |
|
443 | + array( |
|
444 | + esc_html__( |
|
445 | + 'Could not generate a proper item list with:', |
|
446 | + 'event_espresso' |
|
447 | + ) => $request_response_args |
|
448 | + ), |
|
449 | + $payment |
|
450 | + ); |
|
451 | + } |
|
452 | + } |
|
453 | + // ...otherwise we generate a new list for this transaction. |
|
454 | + if ($this->_money->compare_floats($payment->amount(), $transaction->total(), '==')) { |
|
455 | + $item_num = 0; |
|
456 | + $itemized_sum = 0; |
|
457 | + $total_line_items = $transaction->total_line_item(); |
|
458 | + // Go through each item in the list. |
|
459 | + foreach ($total_line_items->get_items() as $line_item) { |
|
460 | + if ($line_item instanceof EE_Line_Item) { |
|
461 | + // PayPal doesn't like line items with 0.00 amount, so we may skip those. |
|
462 | + if (EEH_Money::compare_floats($line_item->total(), '0.00', '==')) { |
|
463 | + continue; |
|
464 | + } |
|
465 | + $unit_price = $line_item->unit_price(); |
|
466 | + $line_item_quantity = $line_item->quantity(); |
|
467 | + // This is a discount. |
|
468 | + if ($line_item->is_percent()) { |
|
469 | + $unit_price = $line_item->total(); |
|
470 | + $line_item_quantity = 1; |
|
471 | + } |
|
472 | + // Item Name. |
|
473 | + $itemized_list['L_PAYMENTREQUEST_0_NAME' . $item_num] = mb_strcut( |
|
474 | + $gateway_formatter->formatLineItemName($line_item, $payment), |
|
475 | + 0, |
|
476 | + 127 |
|
477 | + ); |
|
478 | + // Item description. |
|
479 | + $itemized_list['L_PAYMENTREQUEST_0_DESC' . $item_num] = mb_strcut( |
|
480 | + $gateway_formatter->formatLineItemDesc($line_item, $payment), |
|
481 | + 0, |
|
482 | + 127 |
|
483 | + ); |
|
484 | + // Cost of individual item. |
|
485 | + $itemized_list['L_PAYMENTREQUEST_0_AMT' . $item_num] = $gateway_formatter->formatCurrency($unit_price); |
|
486 | + // Item Number. |
|
487 | + $itemized_list['L_PAYMENTREQUEST_0_NUMBER' . $item_num] = $item_num + 1; |
|
488 | + // Item quantity. |
|
489 | + $itemized_list['L_PAYMENTREQUEST_0_QTY' . $item_num] = $line_item_quantity; |
|
490 | + // Digital item is sold. |
|
491 | + $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num] = 'Physical'; |
|
492 | + $itemized_sum += $line_item->total(); |
|
493 | + ++$item_num; |
|
494 | + } |
|
495 | + } |
|
496 | + // Item's sales S/H and tax amount. |
|
497 | + $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $total_line_items->get_items_total(); |
|
498 | + $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = $total_line_items->get_total_tax(); |
|
499 | + $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
500 | + $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
501 | + $itemized_sum_diff_from_txn_total = round( |
|
502 | + $transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), |
|
503 | + 2 |
|
504 | + ); |
|
505 | + // If we were not able to recognize some item like promotion, surcharge or cancellation, |
|
506 | + // add the difference as an extra line item. |
|
507 | + if ($this->_money->compare_floats($itemized_sum_diff_from_txn_total, 0, '!=')) { |
|
508 | + // Item Name. |
|
509 | + $itemized_list['L_PAYMENTREQUEST_0_NAME' . $item_num] = mb_strcut( |
|
510 | + esc_html__( |
|
511 | + 'Other (promotion/surcharge/cancellation)', |
|
512 | + 'event_espresso' |
|
513 | + ), |
|
514 | + 0, |
|
515 | + 127 |
|
516 | + ); |
|
517 | + // Item description. |
|
518 | + $itemized_list['L_PAYMENTREQUEST_0_DESC' . $item_num] = ''; |
|
519 | + // Cost of individual item. |
|
520 | + $itemized_list['L_PAYMENTREQUEST_0_AMT' . $item_num] = $gateway_formatter->formatCurrency( |
|
521 | + $itemized_sum_diff_from_txn_total |
|
522 | + ); |
|
523 | + // Item Number. |
|
524 | + $itemized_list['L_PAYMENTREQUEST_0_NUMBER' . $item_num] = $item_num + 1; |
|
525 | + // Item quantity. |
|
526 | + $itemized_list['L_PAYMENTREQUEST_0_QTY' . $item_num] = 1; |
|
527 | + // Digital item is sold. |
|
528 | + $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num] = 'Physical'; |
|
529 | + $item_num++; |
|
530 | + } |
|
531 | + } else { |
|
532 | + // Just one Item. |
|
533 | + // Item Name. |
|
534 | + $itemized_list['L_PAYMENTREQUEST_0_NAME0'] = mb_strcut( |
|
535 | + $gateway_formatter->formatPartialPaymentLineItemName($payment), |
|
536 | + 0, |
|
537 | + 127 |
|
538 | + ); |
|
539 | + // Item description. |
|
540 | + $itemized_list['L_PAYMENTREQUEST_0_DESC0'] = mb_strcut( |
|
541 | + $gateway_formatter->formatPartialPaymentLineItemDesc($payment), |
|
542 | + 0, |
|
543 | + 127 |
|
544 | + ); |
|
545 | + // Cost of individual item. |
|
546 | + $itemized_list['L_PAYMENTREQUEST_0_AMT0'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
547 | + // Item Number. |
|
548 | + $itemized_list['L_PAYMENTREQUEST_0_NUMBER0'] = 1; |
|
549 | + // Item quantity. |
|
550 | + $itemized_list['L_PAYMENTREQUEST_0_QTY0'] = 1; |
|
551 | + // Digital item is sold. |
|
552 | + $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Physical'; |
|
553 | + // Item's sales S/H and tax amount. |
|
554 | + $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
555 | + $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = '0'; |
|
556 | + $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
557 | + $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
558 | + } |
|
559 | + return $itemized_list; |
|
560 | + } |
|
561 | + |
|
562 | + |
|
563 | + |
|
564 | + /** |
|
565 | + * Make the Express checkout request. |
|
566 | + * |
|
567 | + * @param array $request_params |
|
568 | + * @param string $request_text |
|
569 | + * @param EEI_Payment $payment |
|
570 | + * @return mixed |
|
571 | + */ |
|
572 | + public function _ppExpress_request($request_params, $request_text, $payment) |
|
573 | + { |
|
574 | + $request_dtls = array( |
|
575 | + 'VERSION' => '204.0', |
|
576 | + 'USER' => urlencode($this->_api_username), |
|
577 | + 'PWD' => urlencode($this->_api_password), |
|
578 | + 'SIGNATURE' => urlencode($this->_api_signature), |
|
579 | + ); |
|
580 | + $dtls = array_merge($request_dtls, $request_params); |
|
581 | + $this->_log_clean_request($dtls, $payment, $request_text . ' Request'); |
|
582 | + // Request Customer Details. |
|
583 | + $request_response = wp_remote_post( |
|
584 | + $this->_base_gateway_url, |
|
585 | + array( |
|
586 | + 'method' => 'POST', |
|
587 | + 'timeout' => 45, |
|
588 | + 'httpversion' => '1.1', |
|
589 | + 'cookies' => array(), |
|
590 | + 'headers' => array(), |
|
591 | + 'body' => http_build_query($dtls, '', '&'), |
|
592 | + ) |
|
593 | + ); |
|
594 | + // Log the response. |
|
595 | + $this->log(array($request_text . ' Response' => $request_response), $payment); |
|
596 | + return $request_response; |
|
597 | + } |
|
598 | + |
|
599 | + |
|
600 | + |
|
601 | + /** |
|
602 | + * Check the response status. |
|
603 | + * |
|
604 | + * @param mixed $request_response |
|
605 | + * @return array |
|
606 | + */ |
|
607 | + public function _ppExpress_check_response($request_response) |
|
608 | + { |
|
609 | + if (is_wp_error($request_response) || empty($request_response['body'])) { |
|
610 | + // If we got here then there was an error in this request. |
|
611 | + return array('status' => false, 'args' => $request_response); |
|
612 | + } |
|
613 | + $response_args = array(); |
|
614 | + parse_str(urldecode($request_response['body']), $response_args); |
|
615 | + if (! isset($response_args['ACK'])) { |
|
616 | + return array('status' => false, 'args' => $request_response); |
|
617 | + } |
|
618 | + if ( |
|
619 | + ( |
|
620 | + isset($response_args['PAYERID']) |
|
621 | + || isset($response_args['TOKEN']) |
|
622 | + || isset($response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
623 | + || (isset($response_args['PAYMENTSTATUS']) && $response_args['PAYMENTSTATUS'] === 'Completed') |
|
624 | + ) |
|
625 | + && in_array($response_args['ACK'], array('Success', 'SuccessWithWarning'), true) |
|
626 | + ) { |
|
627 | + // Response status OK, return response parameters for further processing. |
|
628 | + return array('status' => true, 'args' => $response_args); |
|
629 | + } |
|
630 | + $errors = $this->_get_errors($response_args); |
|
631 | + return array('status' => false, 'args' => $errors); |
|
632 | + } |
|
633 | + |
|
634 | + |
|
635 | + |
|
636 | + /** |
|
637 | + * Log a "Cleared" request. |
|
638 | + * |
|
639 | + * @param array $request |
|
640 | + * @param EEI_Payment $payment |
|
641 | + * @param string $info |
|
642 | + * @return void |
|
643 | + */ |
|
644 | + private function _log_clean_request($request, $payment, $info) |
|
645 | + { |
|
646 | + $cleaned_request_data = $request; |
|
647 | + unset($cleaned_request_data['PWD'], $cleaned_request_data['USER'], $cleaned_request_data['SIGNATURE']); |
|
648 | + $this->log(array($info => $cleaned_request_data), $payment); |
|
649 | + } |
|
650 | + |
|
651 | + |
|
652 | + |
|
653 | + /** |
|
654 | + * Get error from the response data. |
|
655 | + * |
|
656 | + * @param array $data_array |
|
657 | + * @return array |
|
658 | + */ |
|
659 | + private function _get_errors($data_array) |
|
660 | + { |
|
661 | + $errors = array(); |
|
662 | + $n = 0; |
|
663 | + while (isset($data_array["L_ERRORCODE{$n}"])) { |
|
664 | + $l_error_code = isset($data_array["L_ERRORCODE{$n}"]) |
|
665 | + ? $data_array["L_ERRORCODE{$n}"] |
|
666 | + : ''; |
|
667 | + $l_severity_code = isset($data_array["L_SEVERITYCODE{$n}"]) |
|
668 | + ? $data_array["L_SEVERITYCODE{$n}"] |
|
669 | + : ''; |
|
670 | + $l_short_message = isset($data_array["L_SHORTMESSAGE{$n}"]) |
|
671 | + ? $data_array["L_SHORTMESSAGE{$n}"] |
|
672 | + : ''; |
|
673 | + $l_long_message = isset($data_array["L_LONGMESSAGE{$n}"]) |
|
674 | + ? $data_array["L_LONGMESSAGE{$n}"] |
|
675 | + : ''; |
|
676 | + if ($n === 0) { |
|
677 | + $errors = array( |
|
678 | + 'L_ERRORCODE' => $l_error_code, |
|
679 | + 'L_SHORTMESSAGE' => $l_short_message, |
|
680 | + 'L_LONGMESSAGE' => $l_long_message, |
|
681 | + 'L_SEVERITYCODE' => $l_severity_code, |
|
682 | + ); |
|
683 | + } else { |
|
684 | + $errors['L_ERRORCODE'] .= ', ' . $l_error_code; |
|
685 | + $errors['L_SHORTMESSAGE'] .= ', ' . $l_short_message; |
|
686 | + $errors['L_LONGMESSAGE'] .= ', ' . $l_long_message; |
|
687 | + $errors['L_SEVERITYCODE'] .= ', ' . $l_severity_code; |
|
688 | + } |
|
689 | + $n++; |
|
690 | + } |
|
691 | + return $errors; |
|
692 | + } |
|
693 | 693 | |
694 | 694 | } |
695 | 695 | // End of file EEG_Paypal_Express.gateway.php |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | */ |
33 | 33 | public static function instance() { |
34 | 34 | // check if class object is instantiated |
35 | - if ( self::$_instance === NULL or ! is_object( self::$_instance ) or ! ( self::$_instance instanceof EEH_Qtip_Loader )) { |
|
35 | + if (self::$_instance === NULL or ! is_object(self::$_instance) or ! (self::$_instance instanceof EEH_Qtip_Loader)) { |
|
36 | 36 | self::$_instance = new self(); |
37 | 37 | } |
38 | 38 | return self::$_instance; |
@@ -48,8 +48,8 @@ discard block |
||
48 | 48 | */ |
49 | 49 | private function __construct() { |
50 | 50 | //let's just make sure this is instantiated in the right place. |
51 | - if ( did_action( 'wp_print_styles' ) || did_action( 'admin_head' )) { |
|
52 | - EE_Error::doing_it_wrong( 'EEH_Qtip_Loader', __('This helper must be instantiated before or within a callback for the WordPress wp_enqueue_scripts hook action hook.', 'event_espresso' ), '4.1' ); |
|
51 | + if (did_action('wp_print_styles') || did_action('admin_head')) { |
|
52 | + EE_Error::doing_it_wrong('EEH_Qtip_Loader', __('This helper must be instantiated before or within a callback for the WordPress wp_enqueue_scripts hook action hook.', 'event_espresso'), '4.1'); |
|
53 | 53 | } |
54 | 54 | } |
55 | 55 | |
@@ -61,25 +61,25 @@ discard block |
||
61 | 61 | * @return void |
62 | 62 | */ |
63 | 63 | public function register_and_enqueue() { |
64 | - $qtips_js = !defined('SCRIPT_DEBUG') ? EE_THIRD_PARTY_URL . 'qtip/jquery.qtip.min.js' : EE_THIRD_PARTY_URL . 'qtip/jquery.qtip.js'; |
|
65 | - $qtip_map = EE_THIRD_PARTY_URL . 'qtip/jquery.qtip.min.map'; |
|
66 | - $qtipcss = !defined('SCRIPT_DEBUG') ? EE_THIRD_PARTY_URL . 'qtip/jquery.qtip.min.css' : EE_THIRD_PARTY_URL . 'qtip/jquery.qtip.css'; |
|
64 | + $qtips_js = ! defined('SCRIPT_DEBUG') ? EE_THIRD_PARTY_URL . 'qtip/jquery.qtip.min.js' : EE_THIRD_PARTY_URL.'qtip/jquery.qtip.js'; |
|
65 | + $qtip_map = EE_THIRD_PARTY_URL.'qtip/jquery.qtip.min.map'; |
|
66 | + $qtipcss = ! defined('SCRIPT_DEBUG') ? EE_THIRD_PARTY_URL . 'qtip/jquery.qtip.min.css' : EE_THIRD_PARTY_URL.'qtip/jquery.qtip.css'; |
|
67 | 67 | |
68 | - wp_register_script('qtip-map', $qtip_map, array(), '3', TRUE ); |
|
69 | - wp_register_script('qtip', $qtips_js, array('jquery'), '3.0.3', TRUE ); |
|
70 | - wp_register_script('ee-qtip-helper', EE_HELPERS_ASSETS . 'ee-qtip-helper.js', array('qtip', 'jquery-cookie'), EVENT_ESPRESSO_VERSION, TRUE ); |
|
68 | + wp_register_script('qtip-map', $qtip_map, array(), '3', TRUE); |
|
69 | + wp_register_script('qtip', $qtips_js, array('jquery'), '3.0.3', TRUE); |
|
70 | + wp_register_script('ee-qtip-helper', EE_HELPERS_ASSETS.'ee-qtip-helper.js', array('qtip', 'jquery-cookie'), EVENT_ESPRESSO_VERSION, TRUE); |
|
71 | 71 | |
72 | - wp_register_style('qtip-css', $qtipcss, array(), '2.2' ); |
|
72 | + wp_register_style('qtip-css', $qtipcss, array(), '2.2'); |
|
73 | 73 | |
74 | 74 | //k now let's see if there are any registered qtips. If there are, then we need to setup the localized script for ee-qtip-helper.js (and enqueue ee-qtip-helper.js of course!) |
75 | - if ( !empty( $this->_qtips ) ) { |
|
75 | + if ( ! empty($this->_qtips)) { |
|
76 | 76 | wp_enqueue_script('ee-qtip-helper'); |
77 | 77 | wp_enqueue_style('qtip-css'); |
78 | 78 | $qtips = array(); |
79 | - foreach ( $this->_qtips as $qtip ) { |
|
79 | + foreach ($this->_qtips as $qtip) { |
|
80 | 80 | $qts = $qtip->get_tips(); |
81 | - foreach ( $qts as $qt ) { |
|
82 | - if ( ! $qt instanceof EE_Qtip ) |
|
81 | + foreach ($qts as $qt) { |
|
82 | + if ( ! $qt instanceof EE_Qtip) |
|
83 | 83 | continue; |
84 | 84 | $qtips[] = array( |
85 | 85 | 'content_id' => $qt->content_id, |
@@ -88,8 +88,8 @@ discard block |
||
88 | 88 | ); |
89 | 89 | } |
90 | 90 | } |
91 | - if ( !empty($qtips) ) |
|
92 | - wp_localize_script('ee-qtip-helper', 'EE_QTIP_HELPER', array( 'qtips' => $qtips ) ); |
|
91 | + if ( ! empty($qtips)) |
|
92 | + wp_localize_script('ee-qtip-helper', 'EE_QTIP_HELPER', array('qtips' => $qtips)); |
|
93 | 93 | |
94 | 94 | } else { |
95 | 95 | //qtips has been requested without any registration (so assuming its just directly used in the admin). |
@@ -111,24 +111,24 @@ discard block |
||
111 | 111 | * @param string|array $configname name of the Qtip class (full class name is expected and will be used for looking for file, Qtip config classes must extend EE_Qtip_Config) [if this is an array, then we loop through the array to instantiate and setup the qtips] |
112 | 112 | * @return void |
113 | 113 | */ |
114 | - public function register( $configname, $paths = array() ) { |
|
114 | + public function register($configname, $paths = array()) { |
|
115 | 115 | |
116 | 116 | //let's just make sure this is instantiated in the right place. |
117 | - if ( did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts') ) { |
|
118 | - EE_Error::doing_it_wrong( 'EEH_Qtip_Loader->register()', __('EE_Qtip_Config objects must be registered before wp_enqueue_scripts is called.', 'event_espresso' ), '4.1' ); |
|
117 | + if (did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts')) { |
|
118 | + EE_Error::doing_it_wrong('EEH_Qtip_Loader->register()', __('EE_Qtip_Config objects must be registered before wp_enqueue_scripts is called.', 'event_espresso'), '4.1'); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | $configname = (array) $configname; //typecast to array |
122 | - foreach ( $configname as $config ) { |
|
123 | - $this->_register( $config, $paths ); |
|
122 | + foreach ($configname as $config) { |
|
123 | + $this->_register($config, $paths); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | //hook into appropriate footer |
127 | 127 | $footer_action = is_admin() ? 'admin_footer' : 'wp_footer'; |
128 | - add_action($footer_action, array($this, 'setup_qtip'), 10 ); |
|
128 | + add_action($footer_action, array($this, 'setup_qtip'), 10); |
|
129 | 129 | |
130 | 130 | //make sure we "turn on" qtip js. |
131 | - add_filter('FHEE_load_qtip', '__return_true' ); |
|
131 | + add_filter('FHEE_load_qtip', '__return_true'); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | |
@@ -142,15 +142,15 @@ discard block |
||
142 | 142 | * @throws EE_Error |
143 | 143 | * @return void |
144 | 144 | */ |
145 | - private function _register( $config, $paths ) { |
|
145 | + private function _register($config, $paths) { |
|
146 | 146 | //before doing anything we have to make sure that EE_Qtip_Config parent is required. |
147 | - EE_Registry::instance()->load_lib( 'Qtip_Config', array(), TRUE ); |
|
147 | + EE_Registry::instance()->load_lib('Qtip_Config', array(), TRUE); |
|
148 | 148 | |
149 | - if ( !empty( $paths ) ) { |
|
149 | + if ( ! empty($paths)) { |
|
150 | 150 | $paths = (array) $paths; |
151 | - foreach ( $paths as $path ) { |
|
152 | - $path = $path . $config . '.lib.php'; |
|
153 | - if ( !is_readable($path ) ) { |
|
151 | + foreach ($paths as $path) { |
|
152 | + $path = $path.$config.'.lib.php'; |
|
153 | + if ( ! is_readable($path)) { |
|
154 | 154 | continue; |
155 | 155 | } else { |
156 | 156 | require_once $path; |
@@ -159,26 +159,26 @@ discard block |
||
159 | 159 | } |
160 | 160 | |
161 | 161 | //does class exist at this point? If it does then let's instantiate. If it doesn't then let's continue with other paths. |
162 | - if ( !class_exists($config) ) { |
|
163 | - $path = EE_LIBRARIES . 'qtips/' . $config . '.lib.php'; |
|
164 | - if ( !is_readable($path ) ) { |
|
165 | - throw new EE_Error( sprintf( __('Unable to load the Qtip Config registered for this page (%s) because none of the file paths attempted are readable. Please check the spelling of the paths you\'ve used in the registration', 'event_espresso'), $config ) ); |
|
162 | + if ( ! class_exists($config)) { |
|
163 | + $path = EE_LIBRARIES.'qtips/'.$config.'.lib.php'; |
|
164 | + if ( ! is_readable($path)) { |
|
165 | + throw new EE_Error(sprintf(__('Unable to load the Qtip Config registered for this page (%s) because none of the file paths attempted are readable. Please check the spelling of the paths you\'ve used in the registration', 'event_espresso'), $config)); |
|
166 | 166 | } else { |
167 | 167 | require_once $path; |
168 | 168 | } |
169 | 169 | } |
170 | 170 | |
171 | 171 | //now we attempt a class_exists one more time. |
172 | - if ( !class_exists( $config ) ) |
|
173 | - throw new EE_Error( sprintf( __('The Qtip_Config class being registered (%s) does not exist, please check the spelling.', 'event_espresso'), $config ) ); |
|
172 | + if ( ! class_exists($config)) |
|
173 | + throw new EE_Error(sprintf(__('The Qtip_Config class being registered (%s) does not exist, please check the spelling.', 'event_espresso'), $config)); |
|
174 | 174 | |
175 | 175 | //made it HERE? FINALLY, let's get things setup. |
176 | 176 | $a = new ReflectionClass($config); |
177 | 177 | $qtip = $a->newInstance(); |
178 | 178 | |
179 | 179 | //verify that $qtip is a valid object |
180 | - if ( ! $qtip instanceof EE_Qtip_Config ) |
|
181 | - throw new EE_Error( sprintf( esc_html__( 'The class given for the Qtip loader (%1$s) is not a child of the %2$sEE_Qtip_Config%3$s class. Please make sure you are extending EE_Qtip_Config.', 'event_espresso'), $config, '<strong>', '</strong>' ) ); |
|
180 | + if ( ! $qtip instanceof EE_Qtip_Config) |
|
181 | + throw new EE_Error(sprintf(esc_html__('The class given for the Qtip loader (%1$s) is not a child of the %2$sEE_Qtip_Config%3$s class. Please make sure you are extending EE_Qtip_Config.', 'event_espresso'), $config, '<strong>', '</strong>')); |
|
182 | 182 | |
183 | 183 | $this->_qtips[] = $a->newInstance(); |
184 | 184 | |
@@ -194,12 +194,12 @@ discard block |
||
194 | 194 | * @return void |
195 | 195 | */ |
196 | 196 | public function setup_qtip() { |
197 | - if ( empty( $this->_qtips ) ) |
|
197 | + if (empty($this->_qtips)) |
|
198 | 198 | return; //no qtips! |
199 | 199 | |
200 | 200 | $content = array(); |
201 | 201 | |
202 | - foreach ( $this->_qtips as $qtip ) { |
|
202 | + foreach ($this->_qtips as $qtip) { |
|
203 | 203 | $content[] = $this->_generate_content_container($qtip); |
204 | 204 | } |
205 | 205 | |
@@ -216,10 +216,10 @@ discard block |
||
216 | 216 | private function _generate_content_container($qtip) { |
217 | 217 | $qts = $qtip->get_tips(); |
218 | 218 | $content = array(); |
219 | - foreach ( $qts as $qt ) { |
|
220 | - if ( ! $qt instanceof EE_Qtip ) |
|
219 | + foreach ($qts as $qt) { |
|
220 | + if ( ! $qt instanceof EE_Qtip) |
|
221 | 221 | continue; |
222 | - $content[] = '<div class="ee-qtip-helper-content hidden" id="' . $qt->content_id . '">' . $qt->content . '</div>'; |
|
222 | + $content[] = '<div class="ee-qtip-helper-content hidden" id="'.$qt->content_id.'">'.$qt->content.'</div>'; |
|
223 | 223 | } |
224 | 224 | |
225 | 225 | return implode('<br />', $content); |
@@ -19,126 +19,126 @@ |
||
19 | 19 | abstract class DomainBase implements DomainInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * Equivalent to `__FILE__` for main plugin file. |
|
24 | - * |
|
25 | - * @var FilePath |
|
26 | - */ |
|
27 | - private $plugin_file; |
|
22 | + /** |
|
23 | + * Equivalent to `__FILE__` for main plugin file. |
|
24 | + * |
|
25 | + * @var FilePath |
|
26 | + */ |
|
27 | + private $plugin_file; |
|
28 | 28 | |
29 | - /** |
|
30 | - * String indicating version for plugin |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - private $version; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var string $plugin_basename |
|
38 | - */ |
|
39 | - private $plugin_basename; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var string $plugin_path |
|
43 | - */ |
|
44 | - private $plugin_path; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var string $plugin_url |
|
48 | - */ |
|
49 | - private $plugin_url; |
|
50 | - |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * Initializes internal properties. |
|
55 | - * |
|
56 | - * @param FilePath $plugin_file |
|
57 | - * @param Version $version |
|
58 | - */ |
|
59 | - public function __construct(FilePath $plugin_file, Version $version) |
|
60 | - { |
|
61 | - $this->plugin_file = $plugin_file; |
|
62 | - $this->version = $version; |
|
63 | - $this->plugin_basename = plugin_basename($this->pluginFile()); |
|
64 | - $this->plugin_path = plugin_dir_path($this->pluginFile()); |
|
65 | - $this->plugin_url = plugin_dir_url($this->pluginFile()); |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - public function pluginFile() |
|
73 | - { |
|
74 | - return (string) $this->plugin_file; |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function pluginBasename() |
|
83 | - { |
|
84 | - return $this->plugin_basename; |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - public function pluginPath() |
|
93 | - { |
|
94 | - return $this->plugin_path; |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - public function pluginUrl() |
|
103 | - { |
|
104 | - return $this->plugin_url; |
|
105 | - } |
|
106 | - |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - public function version() |
|
113 | - { |
|
114 | - return (string) $this->version; |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * @return Version |
|
121 | - */ |
|
122 | - public function versionValueObject() |
|
123 | - { |
|
124 | - return $this->version; |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public function distributionAssetsPath() |
|
132 | - { |
|
133 | - return $this->pluginPath() . 'assets/dist/'; |
|
134 | - } |
|
29 | + /** |
|
30 | + * String indicating version for plugin |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + private $version; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var string $plugin_basename |
|
38 | + */ |
|
39 | + private $plugin_basename; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var string $plugin_path |
|
43 | + */ |
|
44 | + private $plugin_path; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var string $plugin_url |
|
48 | + */ |
|
49 | + private $plugin_url; |
|
50 | + |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * Initializes internal properties. |
|
55 | + * |
|
56 | + * @param FilePath $plugin_file |
|
57 | + * @param Version $version |
|
58 | + */ |
|
59 | + public function __construct(FilePath $plugin_file, Version $version) |
|
60 | + { |
|
61 | + $this->plugin_file = $plugin_file; |
|
62 | + $this->version = $version; |
|
63 | + $this->plugin_basename = plugin_basename($this->pluginFile()); |
|
64 | + $this->plugin_path = plugin_dir_path($this->pluginFile()); |
|
65 | + $this->plugin_url = plugin_dir_url($this->pluginFile()); |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + public function pluginFile() |
|
73 | + { |
|
74 | + return (string) $this->plugin_file; |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function pluginBasename() |
|
83 | + { |
|
84 | + return $this->plugin_basename; |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + public function pluginPath() |
|
93 | + { |
|
94 | + return $this->plugin_path; |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + public function pluginUrl() |
|
103 | + { |
|
104 | + return $this->plugin_url; |
|
105 | + } |
|
106 | + |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + public function version() |
|
113 | + { |
|
114 | + return (string) $this->version; |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * @return Version |
|
121 | + */ |
|
122 | + public function versionValueObject() |
|
123 | + { |
|
124 | + return $this->version; |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public function distributionAssetsPath() |
|
132 | + { |
|
133 | + return $this->pluginPath() . 'assets/dist/'; |
|
134 | + } |
|
135 | 135 | |
136 | - |
|
137 | - /** |
|
138 | - * @return string |
|
139 | - */ |
|
140 | - public function distributionAssetsUrl() |
|
141 | - { |
|
142 | - return $this->pluginUrl() . 'assets/dist/'; |
|
143 | - } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return string |
|
139 | + */ |
|
140 | + public function distributionAssetsUrl() |
|
141 | + { |
|
142 | + return $this->pluginUrl() . 'assets/dist/'; |
|
143 | + } |
|
144 | 144 | } |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | */ |
131 | 131 | public function distributionAssetsPath() |
132 | 132 | { |
133 | - return $this->pluginPath() . 'assets/dist/'; |
|
133 | + return $this->pluginPath().'assets/dist/'; |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | |
@@ -139,6 +139,6 @@ discard block |
||
139 | 139 | */ |
140 | 140 | public function distributionAssetsUrl() |
141 | 141 | { |
142 | - return $this->pluginUrl() . 'assets/dist/'; |
|
142 | + return $this->pluginUrl().'assets/dist/'; |
|
143 | 143 | } |
144 | 144 | } |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | use EventEspresso\core\services\request\RequestInterface; |
8 | 8 | use EventEspresso\core\services\request\ResponseInterface; |
9 | 9 | |
10 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
10 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
11 | 11 | exit('No direct script access allowed'); |
12 | 12 | } |
13 | 13 | |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | */ |
130 | 130 | public static function instance() { |
131 | 131 | // check if class object is instantiated, and instantiated properly |
132 | - if (! self::$_instance instanceof EE_Dependency_Map) { |
|
132 | + if ( ! self::$_instance instanceof EE_Dependency_Map) { |
|
133 | 133 | self::$_instance = new EE_Dependency_Map(/*$request, $response, $legacy_request*/); |
134 | 134 | } |
135 | 135 | return self::$_instance; |
@@ -211,8 +211,8 @@ discard block |
||
211 | 211 | ) { |
212 | 212 | $class = trim($class, '\\'); |
213 | 213 | $registered = false; |
214 | - if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
215 | - self::$_instance->_dependency_map[ $class ] = array(); |
|
214 | + if (empty(self::$_instance->_dependency_map[$class])) { |
|
215 | + self::$_instance->_dependency_map[$class] = array(); |
|
216 | 216 | } |
217 | 217 | // we need to make sure that any aliases used when registering a dependency |
218 | 218 | // get resolved to the correct class name |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | $alias = self::$_instance->get_alias($dependency); |
221 | 221 | if ( |
222 | 222 | $overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
223 | - || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
223 | + || ! isset(self::$_instance->_dependency_map[$class][$alias]) |
|
224 | 224 | ) { |
225 | 225 | unset($dependencies[$dependency]); |
226 | 226 | $dependencies[$alias] = $load_source; |
@@ -233,13 +233,13 @@ discard block |
||
233 | 233 | // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
234 | 234 | // Union is way faster than array_merge() but should be used with caution... |
235 | 235 | // especially with numerically indexed arrays |
236 | - $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
236 | + $dependencies += self::$_instance->_dependency_map[$class]; |
|
237 | 237 | // now we need to ensure that the resulting dependencies |
238 | 238 | // array only has the entries that are required for the class |
239 | 239 | // so first count how many dependencies were originally registered for the class |
240 | - $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
240 | + $dependency_count = count(self::$_instance->_dependency_map[$class]); |
|
241 | 241 | // if that count is non-zero (meaning dependencies were already registered) |
242 | - self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
242 | + self::$_instance->_dependency_map[$class] = $dependency_count |
|
243 | 243 | // then truncate the final array to match that count |
244 | 244 | ? array_slice($dependencies, 0, $dependency_count) |
245 | 245 | // otherwise just take the incoming array because nothing previously existed |
@@ -257,7 +257,7 @@ discard block |
||
257 | 257 | */ |
258 | 258 | public static function register_class_loader($class_name, $loader = 'load_core') |
259 | 259 | { |
260 | - if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
260 | + if ( ! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
261 | 261 | throw new DomainException( |
262 | 262 | esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
263 | 263 | ); |
@@ -281,7 +281,7 @@ discard block |
||
281 | 281 | ); |
282 | 282 | } |
283 | 283 | $class_name = self::$_instance->get_alias($class_name); |
284 | - if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
284 | + if ( ! isset(self::$_instance->_class_loaders[$class_name])) { |
|
285 | 285 | self::$_instance->_class_loaders[$class_name] = $loader; |
286 | 286 | return true; |
287 | 287 | } |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | public function class_loader($class_name) |
367 | 367 | { |
368 | 368 | // all legacy models use load_model() |
369 | - if(strpos($class_name, 'EEM_') === 0){ |
|
369 | + if (strpos($class_name, 'EEM_') === 0) { |
|
370 | 370 | return 'load_model'; |
371 | 371 | } |
372 | 372 | $class_name = $this->get_alias($class_name); |
@@ -395,7 +395,7 @@ discard block |
||
395 | 395 | public function add_alias($class_name, $alias, $for_class = '') |
396 | 396 | { |
397 | 397 | if ($for_class !== '') { |
398 | - if (! isset($this->_aliases[$for_class])) { |
|
398 | + if ( ! isset($this->_aliases[$for_class])) { |
|
399 | 399 | $this->_aliases[$for_class] = array(); |
400 | 400 | } |
401 | 401 | $this->_aliases[$for_class][$class_name] = $alias; |
@@ -441,10 +441,10 @@ discard block |
||
441 | 441 | */ |
442 | 442 | public function get_alias($class_name = '', $for_class = '') |
443 | 443 | { |
444 | - if (! $this->has_alias($class_name, $for_class)) { |
|
444 | + if ( ! $this->has_alias($class_name, $for_class)) { |
|
445 | 445 | return $class_name; |
446 | 446 | } |
447 | - if ($for_class !== '' && isset($this->_aliases[ $for_class ][ $class_name ])) { |
|
447 | + if ($for_class !== '' && isset($this->_aliases[$for_class][$class_name])) { |
|
448 | 448 | return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
449 | 449 | } |
450 | 450 | return $this->get_alias($this->_aliases[$class_name]); |
@@ -714,13 +714,13 @@ discard block |
||
714 | 714 | 'EE_Front_Controller' => 'load_core', |
715 | 715 | 'EE_Module_Request_Router' => 'load_core', |
716 | 716 | 'EE_Registry' => 'load_core', |
717 | - 'EE_Request' => function () use (&$legacy_request) { |
|
717 | + 'EE_Request' => function() use (&$legacy_request) { |
|
718 | 718 | return $legacy_request; |
719 | 719 | }, |
720 | - 'EventEspresso\core\services\request\Request' => function () use (&$request) { |
|
720 | + 'EventEspresso\core\services\request\Request' => function() use (&$request) { |
|
721 | 721 | return $request; |
722 | 722 | }, |
723 | - 'EventEspresso\core\services\request\Response' => function () use (&$response) { |
|
723 | + 'EventEspresso\core\services\request\Response' => function() use (&$response) { |
|
724 | 724 | return $response; |
725 | 725 | }, |
726 | 726 | 'EE_Request_Handler' => 'load_core', |
@@ -742,7 +742,7 @@ discard block |
||
742 | 742 | 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
743 | 743 | 'EE_Message_Template_Group_Collection' => 'load_lib', |
744 | 744 | 'EE_Payment_Method_Manager' => 'load_lib', |
745 | - 'EE_Messages_Generator' => function () { |
|
745 | + 'EE_Messages_Generator' => function() { |
|
746 | 746 | return EE_Registry::instance()->load_lib( |
747 | 747 | 'Messages_Generator', |
748 | 748 | array(), |
@@ -750,7 +750,7 @@ discard block |
||
750 | 750 | false |
751 | 751 | ); |
752 | 752 | }, |
753 | - 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
753 | + 'EE_Messages_Template_Defaults' => function($arguments = array()) { |
|
754 | 754 | return EE_Registry::instance()->load_lib( |
755 | 755 | 'Messages_Template_Defaults', |
756 | 756 | $arguments, |
@@ -763,22 +763,22 @@ discard block |
||
763 | 763 | // 'EEM_Message_Template_Group' => 'load_model', |
764 | 764 | // 'EEM_Message_Template' => 'load_model', |
765 | 765 | //load_helper |
766 | - 'EEH_Parse_Shortcodes' => function () { |
|
766 | + 'EEH_Parse_Shortcodes' => function() { |
|
767 | 767 | if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
768 | 768 | return new EEH_Parse_Shortcodes(); |
769 | 769 | } |
770 | 770 | return null; |
771 | 771 | }, |
772 | - 'EE_Template_Config' => function () { |
|
772 | + 'EE_Template_Config' => function() { |
|
773 | 773 | return EE_Config::instance()->template_settings; |
774 | 774 | }, |
775 | - 'EE_Currency_Config' => function () { |
|
775 | + 'EE_Currency_Config' => function() { |
|
776 | 776 | return EE_Config::instance()->currency; |
777 | 777 | }, |
778 | - 'EE_Registration_Config' => function () { |
|
778 | + 'EE_Registration_Config' => function() { |
|
779 | 779 | return EE_Config::instance()->registration; |
780 | 780 | }, |
781 | - 'EventEspresso\core\services\loaders\Loader' => function () { |
|
781 | + 'EventEspresso\core\services\loaders\Loader' => function() { |
|
782 | 782 | return LoaderFactory::getLoader(); |
783 | 783 | }, |
784 | 784 | ); |
@@ -834,7 +834,7 @@ discard block |
||
834 | 834 | 'EventEspresso\core\services\request\ResponseInterface' => 'EventEspresso\core\services\request\Response', |
835 | 835 | 'EventEspresso\core\domain\DomainInterface' => 'EventEspresso\core\domain\Domain', |
836 | 836 | ); |
837 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
837 | + if ( ! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
838 | 838 | $this->_aliases['EventEspresso\core\services\notices\NoticeConverterInterface'] = 'EventEspresso\core\services\notices\ConvertNoticesToAdminNotices'; |
839 | 839 | } |
840 | 840 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | use EventEspresso\core\services\request\ResponseInterface; |
9 | 9 | |
10 | 10 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
11 | - exit('No direct script access allowed'); |
|
11 | + exit('No direct script access allowed'); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | |
@@ -25,836 +25,836 @@ discard block |
||
25 | 25 | class EE_Dependency_Map |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * This means that the requested class dependency is not present in the dependency map |
|
30 | - */ |
|
31 | - const not_registered = 0; |
|
32 | - |
|
33 | - /** |
|
34 | - * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
35 | - */ |
|
36 | - const load_new_object = 1; |
|
37 | - |
|
38 | - /** |
|
39 | - * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
40 | - * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
41 | - */ |
|
42 | - const load_from_cache = 2; |
|
43 | - |
|
44 | - /** |
|
45 | - * When registering a dependency, |
|
46 | - * this indicates to keep any existing dependencies that already exist, |
|
47 | - * and simply discard any new dependencies declared in the incoming data |
|
48 | - */ |
|
49 | - const KEEP_EXISTING_DEPENDENCIES = 0; |
|
50 | - |
|
51 | - /** |
|
52 | - * When registering a dependency, |
|
53 | - * this indicates to overwrite any existing dependencies that already exist using the incoming data |
|
54 | - */ |
|
55 | - const OVERWRITE_DEPENDENCIES = 1; |
|
56 | - |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * @type EE_Dependency_Map $_instance |
|
61 | - */ |
|
62 | - protected static $_instance; |
|
63 | - |
|
64 | - /** |
|
65 | - * @type RequestInterface $request |
|
66 | - */ |
|
67 | - protected $request; |
|
68 | - |
|
69 | - /** |
|
70 | - * @type LegacyRequestInterface $legacy_request |
|
71 | - */ |
|
72 | - protected $legacy_request; |
|
73 | - |
|
74 | - /** |
|
75 | - * @type ResponseInterface $response |
|
76 | - */ |
|
77 | - protected $response; |
|
78 | - |
|
79 | - /** |
|
80 | - * @type LoaderInterface $loader |
|
81 | - */ |
|
82 | - protected $loader; |
|
83 | - |
|
84 | - /** |
|
85 | - * @type array $_dependency_map |
|
86 | - */ |
|
87 | - protected $_dependency_map = array(); |
|
88 | - |
|
89 | - /** |
|
90 | - * @type array $_class_loaders |
|
91 | - */ |
|
92 | - protected $_class_loaders = array(); |
|
93 | - |
|
94 | - /** |
|
95 | - * @type array $_aliases |
|
96 | - */ |
|
97 | - protected $_aliases = array(); |
|
98 | - |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * EE_Dependency_Map constructor. |
|
103 | - */ |
|
104 | - protected function __construct() |
|
105 | - { |
|
106 | - // add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
107 | - do_action('EE_Dependency_Map____construct'); |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * @throws InvalidDataTypeException |
|
114 | - * @throws InvalidInterfaceException |
|
115 | - * @throws InvalidArgumentException |
|
116 | - */ |
|
117 | - public function initialize() |
|
118 | - { |
|
119 | - $this->_register_core_dependencies(); |
|
120 | - $this->_register_core_class_loaders(); |
|
121 | - $this->_register_core_aliases(); |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * @singleton method used to instantiate class object |
|
128 | - * @return EE_Dependency_Map |
|
129 | - */ |
|
130 | - public static function instance() { |
|
131 | - // check if class object is instantiated, and instantiated properly |
|
132 | - if (! self::$_instance instanceof EE_Dependency_Map) { |
|
133 | - self::$_instance = new EE_Dependency_Map(/*$request, $response, $legacy_request*/); |
|
134 | - } |
|
135 | - return self::$_instance; |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * @param RequestInterface $request |
|
141 | - */ |
|
142 | - public function setRequest(RequestInterface $request) |
|
143 | - { |
|
144 | - $this->request = $request; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * @param LegacyRequestInterface $legacy_request |
|
150 | - */ |
|
151 | - public function setLegacyRequest(LegacyRequestInterface $legacy_request) |
|
152 | - { |
|
153 | - $this->legacy_request = $legacy_request; |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * @param ResponseInterface $response |
|
159 | - */ |
|
160 | - public function setResponse(ResponseInterface $response) |
|
161 | - { |
|
162 | - $this->response = $response; |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * @param LoaderInterface $loader |
|
169 | - */ |
|
170 | - public function setLoader(LoaderInterface $loader) |
|
171 | - { |
|
172 | - $this->loader = $loader; |
|
173 | - } |
|
174 | - |
|
175 | - |
|
176 | - |
|
177 | - /** |
|
178 | - * @param string $class |
|
179 | - * @param array $dependencies |
|
180 | - * @param int $overwrite |
|
181 | - * @return bool |
|
182 | - */ |
|
183 | - public static function register_dependencies( |
|
184 | - $class, |
|
185 | - array $dependencies, |
|
186 | - $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
187 | - ) { |
|
188 | - return self::$_instance->registerDependencies($class, $dependencies, $overwrite); |
|
189 | - } |
|
190 | - |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * Assigns an array of class names and corresponding load sources (new or cached) |
|
195 | - * to the class specified by the first parameter. |
|
196 | - * IMPORTANT !!! |
|
197 | - * The order of elements in the incoming $dependencies array MUST match |
|
198 | - * the order of the constructor parameters for the class in question. |
|
199 | - * This is especially important when overriding any existing dependencies that are registered. |
|
200 | - * the third parameter controls whether any duplicate dependencies are overwritten or not. |
|
201 | - * |
|
202 | - * @param string $class |
|
203 | - * @param array $dependencies |
|
204 | - * @param int $overwrite |
|
205 | - * @return bool |
|
206 | - */ |
|
207 | - public function registerDependencies( |
|
208 | - $class, |
|
209 | - array $dependencies, |
|
210 | - $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
211 | - ) { |
|
212 | - $class = trim($class, '\\'); |
|
213 | - $registered = false; |
|
214 | - if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
215 | - self::$_instance->_dependency_map[ $class ] = array(); |
|
216 | - } |
|
217 | - // we need to make sure that any aliases used when registering a dependency |
|
218 | - // get resolved to the correct class name |
|
219 | - foreach ($dependencies as $dependency => $load_source) { |
|
220 | - $alias = self::$_instance->get_alias($dependency); |
|
221 | - if ( |
|
222 | - $overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
|
223 | - || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
224 | - ) { |
|
225 | - unset($dependencies[$dependency]); |
|
226 | - $dependencies[$alias] = $load_source; |
|
227 | - $registered = true; |
|
228 | - } |
|
229 | - } |
|
230 | - // now add our two lists of dependencies together. |
|
231 | - // using Union (+=) favours the arrays in precedence from left to right, |
|
232 | - // so $dependencies is NOT overwritten because it is listed first |
|
233 | - // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
|
234 | - // Union is way faster than array_merge() but should be used with caution... |
|
235 | - // especially with numerically indexed arrays |
|
236 | - $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
237 | - // now we need to ensure that the resulting dependencies |
|
238 | - // array only has the entries that are required for the class |
|
239 | - // so first count how many dependencies were originally registered for the class |
|
240 | - $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
241 | - // if that count is non-zero (meaning dependencies were already registered) |
|
242 | - self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
243 | - // then truncate the final array to match that count |
|
244 | - ? array_slice($dependencies, 0, $dependency_count) |
|
245 | - // otherwise just take the incoming array because nothing previously existed |
|
246 | - : $dependencies; |
|
247 | - return $registered; |
|
248 | - } |
|
249 | - |
|
250 | - |
|
251 | - |
|
252 | - /** |
|
253 | - * @param string $class_name |
|
254 | - * @param string $loader |
|
255 | - * @return bool |
|
256 | - * @throws DomainException |
|
257 | - */ |
|
258 | - public static function register_class_loader($class_name, $loader = 'load_core') |
|
259 | - { |
|
260 | - if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
261 | - throw new DomainException( |
|
262 | - esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
|
263 | - ); |
|
264 | - } |
|
265 | - // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
266 | - if ( |
|
267 | - ! is_callable($loader) |
|
268 | - && ( |
|
269 | - strpos($loader, 'load_') !== 0 |
|
270 | - || ! method_exists('EE_Registry', $loader) |
|
271 | - ) |
|
272 | - ) { |
|
273 | - throw new DomainException( |
|
274 | - sprintf( |
|
275 | - esc_html__( |
|
276 | - '"%1$s" is not a valid loader method on EE_Registry.', |
|
277 | - 'event_espresso' |
|
278 | - ), |
|
279 | - $loader |
|
280 | - ) |
|
281 | - ); |
|
282 | - } |
|
283 | - $class_name = self::$_instance->get_alias($class_name); |
|
284 | - if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
285 | - self::$_instance->_class_loaders[$class_name] = $loader; |
|
286 | - return true; |
|
287 | - } |
|
288 | - return false; |
|
289 | - } |
|
290 | - |
|
291 | - |
|
292 | - |
|
293 | - /** |
|
294 | - * @return array |
|
295 | - */ |
|
296 | - public function dependency_map() |
|
297 | - { |
|
298 | - return $this->_dependency_map; |
|
299 | - } |
|
300 | - |
|
301 | - |
|
302 | - |
|
303 | - /** |
|
304 | - * returns TRUE if dependency map contains a listing for the provided class name |
|
305 | - * |
|
306 | - * @param string $class_name |
|
307 | - * @return boolean |
|
308 | - */ |
|
309 | - public function has($class_name = '') |
|
310 | - { |
|
311 | - // all legacy models have the same dependencies |
|
312 | - if (strpos($class_name, 'EEM_') === 0) { |
|
313 | - $class_name = 'LEGACY_MODELS'; |
|
314 | - } |
|
315 | - return isset($this->_dependency_map[$class_name]) ? true : false; |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - |
|
320 | - /** |
|
321 | - * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
322 | - * |
|
323 | - * @param string $class_name |
|
324 | - * @param string $dependency |
|
325 | - * @return bool |
|
326 | - */ |
|
327 | - public function has_dependency_for_class($class_name = '', $dependency = '') |
|
328 | - { |
|
329 | - // all legacy models have the same dependencies |
|
330 | - if (strpos($class_name, 'EEM_') === 0) { |
|
331 | - $class_name = 'LEGACY_MODELS'; |
|
332 | - } |
|
333 | - $dependency = $this->get_alias($dependency); |
|
334 | - return isset($this->_dependency_map[$class_name][$dependency]) |
|
335 | - ? true |
|
336 | - : false; |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - |
|
341 | - /** |
|
342 | - * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
343 | - * |
|
344 | - * @param string $class_name |
|
345 | - * @param string $dependency |
|
346 | - * @return int |
|
347 | - */ |
|
348 | - public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
349 | - { |
|
350 | - // all legacy models have the same dependencies |
|
351 | - if (strpos($class_name, 'EEM_') === 0) { |
|
352 | - $class_name = 'LEGACY_MODELS'; |
|
353 | - } |
|
354 | - $dependency = $this->get_alias($dependency); |
|
355 | - return $this->has_dependency_for_class($class_name, $dependency) |
|
356 | - ? $this->_dependency_map[$class_name][$dependency] |
|
357 | - : EE_Dependency_Map::not_registered; |
|
358 | - } |
|
359 | - |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * @param string $class_name |
|
364 | - * @return string | Closure |
|
365 | - */ |
|
366 | - public function class_loader($class_name) |
|
367 | - { |
|
368 | - // all legacy models use load_model() |
|
369 | - if(strpos($class_name, 'EEM_') === 0){ |
|
370 | - return 'load_model'; |
|
371 | - } |
|
372 | - $class_name = $this->get_alias($class_name); |
|
373 | - return isset($this->_class_loaders[$class_name]) ? $this->_class_loaders[$class_name] : ''; |
|
374 | - } |
|
375 | - |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * @return array |
|
380 | - */ |
|
381 | - public function class_loaders() |
|
382 | - { |
|
383 | - return $this->_class_loaders; |
|
384 | - } |
|
385 | - |
|
386 | - |
|
387 | - |
|
388 | - /** |
|
389 | - * adds an alias for a classname |
|
390 | - * |
|
391 | - * @param string $class_name the class name that should be used (concrete class to replace interface) |
|
392 | - * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
393 | - * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
394 | - */ |
|
395 | - public function add_alias($class_name, $alias, $for_class = '') |
|
396 | - { |
|
397 | - if ($for_class !== '') { |
|
398 | - if (! isset($this->_aliases[$for_class])) { |
|
399 | - $this->_aliases[$for_class] = array(); |
|
400 | - } |
|
401 | - $this->_aliases[$for_class][$class_name] = $alias; |
|
402 | - } |
|
403 | - $this->_aliases[$class_name] = $alias; |
|
404 | - } |
|
405 | - |
|
406 | - |
|
407 | - |
|
408 | - /** |
|
409 | - * returns TRUE if the provided class name has an alias |
|
410 | - * |
|
411 | - * @param string $class_name |
|
412 | - * @param string $for_class |
|
413 | - * @return bool |
|
414 | - */ |
|
415 | - public function has_alias($class_name = '', $for_class = '') |
|
416 | - { |
|
417 | - return isset($this->_aliases[$for_class][$class_name]) |
|
418 | - || ( |
|
419 | - isset($this->_aliases[$class_name]) |
|
420 | - && ! is_array($this->_aliases[$class_name]) |
|
421 | - ); |
|
422 | - } |
|
423 | - |
|
424 | - |
|
425 | - |
|
426 | - /** |
|
427 | - * returns alias for class name if one exists, otherwise returns the original classname |
|
428 | - * functions recursively, so that multiple aliases can be used to drill down to a classname |
|
429 | - * for example: |
|
430 | - * if the following two entries were added to the _aliases array: |
|
431 | - * array( |
|
432 | - * 'interface_alias' => 'some\namespace\interface' |
|
433 | - * 'some\namespace\interface' => 'some\namespace\classname' |
|
434 | - * ) |
|
435 | - * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
436 | - * to load an instance of 'some\namespace\classname' |
|
437 | - * |
|
438 | - * @param string $class_name |
|
439 | - * @param string $for_class |
|
440 | - * @return string |
|
441 | - */ |
|
442 | - public function get_alias($class_name = '', $for_class = '') |
|
443 | - { |
|
444 | - if (! $this->has_alias($class_name, $for_class)) { |
|
445 | - return $class_name; |
|
446 | - } |
|
447 | - if ($for_class !== '' && isset($this->_aliases[ $for_class ][ $class_name ])) { |
|
448 | - return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
|
449 | - } |
|
450 | - return $this->get_alias($this->_aliases[$class_name]); |
|
451 | - } |
|
452 | - |
|
453 | - |
|
454 | - |
|
455 | - /** |
|
456 | - * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
457 | - * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
458 | - * This is done by using the following class constants: |
|
459 | - * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
460 | - * EE_Dependency_Map::load_new_object - generates a new object every time |
|
461 | - */ |
|
462 | - protected function _register_core_dependencies() |
|
463 | - { |
|
464 | - $this->_dependency_map = array( |
|
465 | - 'EE_Request_Handler' => array( |
|
466 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
467 | - ), |
|
468 | - 'EE_System' => array( |
|
469 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
470 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
471 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
472 | - 'EE_Maintenance_Mode' => EE_Dependency_Map::load_from_cache, |
|
473 | - ), |
|
474 | - 'EE_Session' => array( |
|
475 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
476 | - 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
477 | - 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
478 | - ), |
|
479 | - 'EE_Cart' => array( |
|
480 | - 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
481 | - ), |
|
482 | - 'EE_Front_Controller' => array( |
|
483 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
484 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
485 | - 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
486 | - ), |
|
487 | - 'EE_Messenger_Collection_Loader' => array( |
|
488 | - 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
489 | - ), |
|
490 | - 'EE_Message_Type_Collection_Loader' => array( |
|
491 | - 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
492 | - ), |
|
493 | - 'EE_Message_Resource_Manager' => array( |
|
494 | - 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
495 | - 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
496 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
497 | - ), |
|
498 | - 'EE_Message_Factory' => array( |
|
499 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
500 | - ), |
|
501 | - 'EE_messages' => array( |
|
502 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
503 | - ), |
|
504 | - 'EE_Messages_Generator' => array( |
|
505 | - 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
506 | - 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
507 | - 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
508 | - 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
509 | - ), |
|
510 | - 'EE_Messages_Processor' => array( |
|
511 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
512 | - ), |
|
513 | - 'EE_Messages_Queue' => array( |
|
514 | - 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
515 | - ), |
|
516 | - 'EE_Messages_Template_Defaults' => array( |
|
517 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
518 | - 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
519 | - ), |
|
520 | - 'EE_Message_To_Generate_From_Request' => array( |
|
521 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
522 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
523 | - ), |
|
524 | - 'EventEspresso\core\services\commands\CommandBus' => array( |
|
525 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
526 | - ), |
|
527 | - 'EventEspresso\services\commands\CommandHandler' => array( |
|
528 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
529 | - 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
530 | - ), |
|
531 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
532 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
533 | - ), |
|
534 | - 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
535 | - 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
536 | - 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
537 | - ), |
|
538 | - 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
539 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
540 | - ), |
|
541 | - 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
542 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
543 | - ), |
|
544 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
545 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
546 | - ), |
|
547 | - 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
548 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
549 | - ), |
|
550 | - 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
551 | - 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
552 | - ), |
|
553 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
554 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
555 | - ), |
|
556 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
557 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
558 | - ), |
|
559 | - 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
560 | - 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
561 | - ), |
|
562 | - 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
563 | - 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
564 | - ), |
|
565 | - 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
566 | - 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
567 | - ), |
|
568 | - 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
569 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
570 | - ), |
|
571 | - 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
572 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
573 | - ), |
|
574 | - 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler' => array( |
|
575 | - 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
576 | - ), |
|
577 | - 'EventEspresso\core\services\database\TableManager' => array( |
|
578 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
579 | - ), |
|
580 | - 'EE_Data_Migration_Class_Base' => array( |
|
581 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
582 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
583 | - ), |
|
584 | - 'EE_DMS_Core_4_1_0' => array( |
|
585 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
586 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
587 | - ), |
|
588 | - 'EE_DMS_Core_4_2_0' => array( |
|
589 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
590 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
591 | - ), |
|
592 | - 'EE_DMS_Core_4_3_0' => array( |
|
593 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
594 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
595 | - ), |
|
596 | - 'EE_DMS_Core_4_4_0' => array( |
|
597 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
598 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
599 | - ), |
|
600 | - 'EE_DMS_Core_4_5_0' => array( |
|
601 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
602 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
603 | - ), |
|
604 | - 'EE_DMS_Core_4_6_0' => array( |
|
605 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
606 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
607 | - ), |
|
608 | - 'EE_DMS_Core_4_7_0' => array( |
|
609 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
610 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
611 | - ), |
|
612 | - 'EE_DMS_Core_4_8_0' => array( |
|
613 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
614 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
615 | - ), |
|
616 | - 'EE_DMS_Core_4_9_0' => array( |
|
617 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
618 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
619 | - ), |
|
620 | - 'EventEspresso\core\services\assets\Registry' => array( |
|
621 | - 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
622 | - 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
623 | - 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache |
|
624 | - ), |
|
625 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
626 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
627 | - ), |
|
628 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
629 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
630 | - ), |
|
631 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
632 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
633 | - ), |
|
634 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
635 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
636 | - ), |
|
637 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
638 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
639 | - ), |
|
640 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
641 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
642 | - ), |
|
643 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
644 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
645 | - ), |
|
646 | - 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
647 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
648 | - ), |
|
649 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
650 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
651 | - ), |
|
652 | - 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => array( |
|
653 | - 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
654 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
655 | - ), |
|
656 | - 'EventEspresso\core\domain\values\EmailAddress' => array( |
|
657 | - null, |
|
658 | - 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => EE_Dependency_Map::load_from_cache, |
|
659 | - ), |
|
660 | - 'EventEspresso\core\services\orm\ModelFieldFactory' => array( |
|
661 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
662 | - ), |
|
663 | - 'LEGACY_MODELS' => array( |
|
664 | - null, |
|
665 | - 'EventEspresso\core\services\database\ModelFieldFactory' => EE_Dependency_Map::load_from_cache, |
|
666 | - ), |
|
667 | - 'EE_Module_Request_Router' => array( |
|
668 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
669 | - ), |
|
670 | - 'EE_Registration_Processor' => array( |
|
671 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
672 | - ), |
|
673 | - 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' => array( |
|
674 | - null, |
|
675 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
676 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
677 | - ), |
|
678 | - ); |
|
679 | - } |
|
680 | - |
|
681 | - |
|
682 | - |
|
683 | - /** |
|
684 | - * Registers how core classes are loaded. |
|
685 | - * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
686 | - * 'EE_Request_Handler' => 'load_core' |
|
687 | - * 'EE_Messages_Queue' => 'load_lib' |
|
688 | - * 'EEH_Debug_Tools' => 'load_helper' |
|
689 | - * or, if greater control is required, by providing a custom closure. For example: |
|
690 | - * 'Some_Class' => function () { |
|
691 | - * return new Some_Class(); |
|
692 | - * }, |
|
693 | - * This is required for instantiating dependencies |
|
694 | - * where an interface has been type hinted in a class constructor. For example: |
|
695 | - * 'Required_Interface' => function () { |
|
696 | - * return new A_Class_That_Implements_Required_Interface(); |
|
697 | - * }, |
|
698 | - * |
|
699 | - * @throws InvalidInterfaceException |
|
700 | - * @throws InvalidDataTypeException |
|
701 | - * @throws InvalidArgumentException |
|
702 | - */ |
|
703 | - protected function _register_core_class_loaders() |
|
704 | - { |
|
705 | - //for PHP5.3 compat, we need to register any properties called here in a variable because `$this` cannot |
|
706 | - //be used in a closure. |
|
707 | - $request = &$this->request; |
|
708 | - $response = &$this->response; |
|
709 | - $legacy_request = &$this->legacy_request; |
|
710 | - // $loader = &$this->loader; |
|
711 | - $this->_class_loaders = array( |
|
712 | - //load_core |
|
713 | - 'EE_Capabilities' => 'load_core', |
|
714 | - 'EE_Encryption' => 'load_core', |
|
715 | - 'EE_Front_Controller' => 'load_core', |
|
716 | - 'EE_Module_Request_Router' => 'load_core', |
|
717 | - 'EE_Registry' => 'load_core', |
|
718 | - 'EE_Request' => function () use (&$legacy_request) { |
|
719 | - return $legacy_request; |
|
720 | - }, |
|
721 | - 'EventEspresso\core\services\request\Request' => function () use (&$request) { |
|
722 | - return $request; |
|
723 | - }, |
|
724 | - 'EventEspresso\core\services\request\Response' => function () use (&$response) { |
|
725 | - return $response; |
|
726 | - }, |
|
727 | - 'EE_Request_Handler' => 'load_core', |
|
728 | - 'EE_Session' => 'load_core', |
|
729 | - 'EE_Cron_Tasks' => 'load_core', |
|
730 | - 'EE_System' => 'load_core', |
|
731 | - 'EE_Maintenance_Mode' => 'load_core', |
|
732 | - 'EE_Register_CPTs' => 'load_core', |
|
733 | - 'EE_Admin' => 'load_core', |
|
734 | - //load_lib |
|
735 | - 'EE_Message_Resource_Manager' => 'load_lib', |
|
736 | - 'EE_Message_Type_Collection' => 'load_lib', |
|
737 | - 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
738 | - 'EE_Messenger_Collection' => 'load_lib', |
|
739 | - 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
740 | - 'EE_Messages_Processor' => 'load_lib', |
|
741 | - 'EE_Message_Repository' => 'load_lib', |
|
742 | - 'EE_Messages_Queue' => 'load_lib', |
|
743 | - 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
744 | - 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
745 | - 'EE_Payment_Method_Manager' => 'load_lib', |
|
746 | - 'EE_Messages_Generator' => function () { |
|
747 | - return EE_Registry::instance()->load_lib( |
|
748 | - 'Messages_Generator', |
|
749 | - array(), |
|
750 | - false, |
|
751 | - false |
|
752 | - ); |
|
753 | - }, |
|
754 | - 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
755 | - return EE_Registry::instance()->load_lib( |
|
756 | - 'Messages_Template_Defaults', |
|
757 | - $arguments, |
|
758 | - false, |
|
759 | - false |
|
760 | - ); |
|
761 | - }, |
|
762 | - //load_model |
|
763 | - // 'EEM_Attendee' => 'load_model', |
|
764 | - // 'EEM_Message_Template_Group' => 'load_model', |
|
765 | - // 'EEM_Message_Template' => 'load_model', |
|
766 | - //load_helper |
|
767 | - 'EEH_Parse_Shortcodes' => function () { |
|
768 | - if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
769 | - return new EEH_Parse_Shortcodes(); |
|
770 | - } |
|
771 | - return null; |
|
772 | - }, |
|
773 | - 'EE_Template_Config' => function () { |
|
774 | - return EE_Config::instance()->template_settings; |
|
775 | - }, |
|
776 | - 'EE_Currency_Config' => function () { |
|
777 | - return EE_Config::instance()->currency; |
|
778 | - }, |
|
779 | - 'EE_Registration_Config' => function () { |
|
780 | - return EE_Config::instance()->registration; |
|
781 | - }, |
|
782 | - 'EventEspresso\core\services\loaders\Loader' => function () { |
|
783 | - return LoaderFactory::getLoader(); |
|
784 | - }, |
|
785 | - ); |
|
786 | - } |
|
787 | - |
|
788 | - |
|
789 | - |
|
790 | - /** |
|
791 | - * can be used for supplying alternate names for classes, |
|
792 | - * or for connecting interface names to instantiable classes |
|
793 | - */ |
|
794 | - protected function _register_core_aliases() |
|
795 | - { |
|
796 | - $this->_aliases = array( |
|
797 | - 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
798 | - 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
799 | - 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
800 | - 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
801 | - 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
802 | - 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
803 | - 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
804 | - 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
805 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
806 | - 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
807 | - 'CreateRegCodeCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegCodeCommand', |
|
808 | - 'CreateRegUrlLinkCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegUrlLinkCommand', |
|
809 | - 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
810 | - 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
811 | - 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
812 | - 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
813 | - 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
814 | - 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
815 | - 'CreateTransactionCommandHandler' => 'EventEspresso\core\services\commands\transaction\CreateTransactionCommandHandler', |
|
816 | - 'CreateAttendeeCommandHandler' => 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler', |
|
817 | - 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
818 | - 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
819 | - 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
820 | - 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
821 | - 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
822 | - 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
823 | - 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
824 | - 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
825 | - 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
826 | - 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
827 | - 'EventEspresso\core\domain\services\session\SessionIdentifierInterface' => 'EE_Session', |
|
828 | - 'EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface', |
|
829 | - 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidationService', |
|
830 | - 'NoticeConverterInterface' => 'EventEspresso\core\services\notices\NoticeConverterInterface', |
|
831 | - 'EventEspresso\core\services\notices\NoticeConverterInterface' => 'EventEspresso\core\services\notices\ConvertNoticesToEeErrors', |
|
832 | - 'NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainerInterface', |
|
833 | - 'EventEspresso\core\services\notices\NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainer', |
|
834 | - 'EventEspresso\core\services\request\RequestInterface' => 'EventEspresso\core\services\request\Request', |
|
835 | - 'EventEspresso\core\services\request\ResponseInterface' => 'EventEspresso\core\services\request\Response', |
|
836 | - 'EventEspresso\core\domain\DomainInterface' => 'EventEspresso\core\domain\Domain', |
|
837 | - ); |
|
838 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
839 | - $this->_aliases['EventEspresso\core\services\notices\NoticeConverterInterface'] = 'EventEspresso\core\services\notices\ConvertNoticesToAdminNotices'; |
|
840 | - } |
|
841 | - } |
|
842 | - |
|
843 | - |
|
844 | - |
|
845 | - /** |
|
846 | - * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
847 | - * request Primarily used by unit tests. |
|
848 | - * |
|
849 | - * @throws InvalidDataTypeException |
|
850 | - * @throws InvalidInterfaceException |
|
851 | - * @throws InvalidArgumentException |
|
852 | - */ |
|
853 | - public function reset() |
|
854 | - { |
|
855 | - $this->_register_core_class_loaders(); |
|
856 | - $this->_register_core_dependencies(); |
|
857 | - } |
|
28 | + /** |
|
29 | + * This means that the requested class dependency is not present in the dependency map |
|
30 | + */ |
|
31 | + const not_registered = 0; |
|
32 | + |
|
33 | + /** |
|
34 | + * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
35 | + */ |
|
36 | + const load_new_object = 1; |
|
37 | + |
|
38 | + /** |
|
39 | + * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
40 | + * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
41 | + */ |
|
42 | + const load_from_cache = 2; |
|
43 | + |
|
44 | + /** |
|
45 | + * When registering a dependency, |
|
46 | + * this indicates to keep any existing dependencies that already exist, |
|
47 | + * and simply discard any new dependencies declared in the incoming data |
|
48 | + */ |
|
49 | + const KEEP_EXISTING_DEPENDENCIES = 0; |
|
50 | + |
|
51 | + /** |
|
52 | + * When registering a dependency, |
|
53 | + * this indicates to overwrite any existing dependencies that already exist using the incoming data |
|
54 | + */ |
|
55 | + const OVERWRITE_DEPENDENCIES = 1; |
|
56 | + |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * @type EE_Dependency_Map $_instance |
|
61 | + */ |
|
62 | + protected static $_instance; |
|
63 | + |
|
64 | + /** |
|
65 | + * @type RequestInterface $request |
|
66 | + */ |
|
67 | + protected $request; |
|
68 | + |
|
69 | + /** |
|
70 | + * @type LegacyRequestInterface $legacy_request |
|
71 | + */ |
|
72 | + protected $legacy_request; |
|
73 | + |
|
74 | + /** |
|
75 | + * @type ResponseInterface $response |
|
76 | + */ |
|
77 | + protected $response; |
|
78 | + |
|
79 | + /** |
|
80 | + * @type LoaderInterface $loader |
|
81 | + */ |
|
82 | + protected $loader; |
|
83 | + |
|
84 | + /** |
|
85 | + * @type array $_dependency_map |
|
86 | + */ |
|
87 | + protected $_dependency_map = array(); |
|
88 | + |
|
89 | + /** |
|
90 | + * @type array $_class_loaders |
|
91 | + */ |
|
92 | + protected $_class_loaders = array(); |
|
93 | + |
|
94 | + /** |
|
95 | + * @type array $_aliases |
|
96 | + */ |
|
97 | + protected $_aliases = array(); |
|
98 | + |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * EE_Dependency_Map constructor. |
|
103 | + */ |
|
104 | + protected function __construct() |
|
105 | + { |
|
106 | + // add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
107 | + do_action('EE_Dependency_Map____construct'); |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * @throws InvalidDataTypeException |
|
114 | + * @throws InvalidInterfaceException |
|
115 | + * @throws InvalidArgumentException |
|
116 | + */ |
|
117 | + public function initialize() |
|
118 | + { |
|
119 | + $this->_register_core_dependencies(); |
|
120 | + $this->_register_core_class_loaders(); |
|
121 | + $this->_register_core_aliases(); |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * @singleton method used to instantiate class object |
|
128 | + * @return EE_Dependency_Map |
|
129 | + */ |
|
130 | + public static function instance() { |
|
131 | + // check if class object is instantiated, and instantiated properly |
|
132 | + if (! self::$_instance instanceof EE_Dependency_Map) { |
|
133 | + self::$_instance = new EE_Dependency_Map(/*$request, $response, $legacy_request*/); |
|
134 | + } |
|
135 | + return self::$_instance; |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * @param RequestInterface $request |
|
141 | + */ |
|
142 | + public function setRequest(RequestInterface $request) |
|
143 | + { |
|
144 | + $this->request = $request; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * @param LegacyRequestInterface $legacy_request |
|
150 | + */ |
|
151 | + public function setLegacyRequest(LegacyRequestInterface $legacy_request) |
|
152 | + { |
|
153 | + $this->legacy_request = $legacy_request; |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * @param ResponseInterface $response |
|
159 | + */ |
|
160 | + public function setResponse(ResponseInterface $response) |
|
161 | + { |
|
162 | + $this->response = $response; |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * @param LoaderInterface $loader |
|
169 | + */ |
|
170 | + public function setLoader(LoaderInterface $loader) |
|
171 | + { |
|
172 | + $this->loader = $loader; |
|
173 | + } |
|
174 | + |
|
175 | + |
|
176 | + |
|
177 | + /** |
|
178 | + * @param string $class |
|
179 | + * @param array $dependencies |
|
180 | + * @param int $overwrite |
|
181 | + * @return bool |
|
182 | + */ |
|
183 | + public static function register_dependencies( |
|
184 | + $class, |
|
185 | + array $dependencies, |
|
186 | + $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
187 | + ) { |
|
188 | + return self::$_instance->registerDependencies($class, $dependencies, $overwrite); |
|
189 | + } |
|
190 | + |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * Assigns an array of class names and corresponding load sources (new or cached) |
|
195 | + * to the class specified by the first parameter. |
|
196 | + * IMPORTANT !!! |
|
197 | + * The order of elements in the incoming $dependencies array MUST match |
|
198 | + * the order of the constructor parameters for the class in question. |
|
199 | + * This is especially important when overriding any existing dependencies that are registered. |
|
200 | + * the third parameter controls whether any duplicate dependencies are overwritten or not. |
|
201 | + * |
|
202 | + * @param string $class |
|
203 | + * @param array $dependencies |
|
204 | + * @param int $overwrite |
|
205 | + * @return bool |
|
206 | + */ |
|
207 | + public function registerDependencies( |
|
208 | + $class, |
|
209 | + array $dependencies, |
|
210 | + $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
211 | + ) { |
|
212 | + $class = trim($class, '\\'); |
|
213 | + $registered = false; |
|
214 | + if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
215 | + self::$_instance->_dependency_map[ $class ] = array(); |
|
216 | + } |
|
217 | + // we need to make sure that any aliases used when registering a dependency |
|
218 | + // get resolved to the correct class name |
|
219 | + foreach ($dependencies as $dependency => $load_source) { |
|
220 | + $alias = self::$_instance->get_alias($dependency); |
|
221 | + if ( |
|
222 | + $overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
|
223 | + || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
224 | + ) { |
|
225 | + unset($dependencies[$dependency]); |
|
226 | + $dependencies[$alias] = $load_source; |
|
227 | + $registered = true; |
|
228 | + } |
|
229 | + } |
|
230 | + // now add our two lists of dependencies together. |
|
231 | + // using Union (+=) favours the arrays in precedence from left to right, |
|
232 | + // so $dependencies is NOT overwritten because it is listed first |
|
233 | + // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
|
234 | + // Union is way faster than array_merge() but should be used with caution... |
|
235 | + // especially with numerically indexed arrays |
|
236 | + $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
237 | + // now we need to ensure that the resulting dependencies |
|
238 | + // array only has the entries that are required for the class |
|
239 | + // so first count how many dependencies were originally registered for the class |
|
240 | + $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
241 | + // if that count is non-zero (meaning dependencies were already registered) |
|
242 | + self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
243 | + // then truncate the final array to match that count |
|
244 | + ? array_slice($dependencies, 0, $dependency_count) |
|
245 | + // otherwise just take the incoming array because nothing previously existed |
|
246 | + : $dependencies; |
|
247 | + return $registered; |
|
248 | + } |
|
249 | + |
|
250 | + |
|
251 | + |
|
252 | + /** |
|
253 | + * @param string $class_name |
|
254 | + * @param string $loader |
|
255 | + * @return bool |
|
256 | + * @throws DomainException |
|
257 | + */ |
|
258 | + public static function register_class_loader($class_name, $loader = 'load_core') |
|
259 | + { |
|
260 | + if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
261 | + throw new DomainException( |
|
262 | + esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
|
263 | + ); |
|
264 | + } |
|
265 | + // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
266 | + if ( |
|
267 | + ! is_callable($loader) |
|
268 | + && ( |
|
269 | + strpos($loader, 'load_') !== 0 |
|
270 | + || ! method_exists('EE_Registry', $loader) |
|
271 | + ) |
|
272 | + ) { |
|
273 | + throw new DomainException( |
|
274 | + sprintf( |
|
275 | + esc_html__( |
|
276 | + '"%1$s" is not a valid loader method on EE_Registry.', |
|
277 | + 'event_espresso' |
|
278 | + ), |
|
279 | + $loader |
|
280 | + ) |
|
281 | + ); |
|
282 | + } |
|
283 | + $class_name = self::$_instance->get_alias($class_name); |
|
284 | + if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
285 | + self::$_instance->_class_loaders[$class_name] = $loader; |
|
286 | + return true; |
|
287 | + } |
|
288 | + return false; |
|
289 | + } |
|
290 | + |
|
291 | + |
|
292 | + |
|
293 | + /** |
|
294 | + * @return array |
|
295 | + */ |
|
296 | + public function dependency_map() |
|
297 | + { |
|
298 | + return $this->_dependency_map; |
|
299 | + } |
|
300 | + |
|
301 | + |
|
302 | + |
|
303 | + /** |
|
304 | + * returns TRUE if dependency map contains a listing for the provided class name |
|
305 | + * |
|
306 | + * @param string $class_name |
|
307 | + * @return boolean |
|
308 | + */ |
|
309 | + public function has($class_name = '') |
|
310 | + { |
|
311 | + // all legacy models have the same dependencies |
|
312 | + if (strpos($class_name, 'EEM_') === 0) { |
|
313 | + $class_name = 'LEGACY_MODELS'; |
|
314 | + } |
|
315 | + return isset($this->_dependency_map[$class_name]) ? true : false; |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + |
|
320 | + /** |
|
321 | + * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
322 | + * |
|
323 | + * @param string $class_name |
|
324 | + * @param string $dependency |
|
325 | + * @return bool |
|
326 | + */ |
|
327 | + public function has_dependency_for_class($class_name = '', $dependency = '') |
|
328 | + { |
|
329 | + // all legacy models have the same dependencies |
|
330 | + if (strpos($class_name, 'EEM_') === 0) { |
|
331 | + $class_name = 'LEGACY_MODELS'; |
|
332 | + } |
|
333 | + $dependency = $this->get_alias($dependency); |
|
334 | + return isset($this->_dependency_map[$class_name][$dependency]) |
|
335 | + ? true |
|
336 | + : false; |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + |
|
341 | + /** |
|
342 | + * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
343 | + * |
|
344 | + * @param string $class_name |
|
345 | + * @param string $dependency |
|
346 | + * @return int |
|
347 | + */ |
|
348 | + public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
349 | + { |
|
350 | + // all legacy models have the same dependencies |
|
351 | + if (strpos($class_name, 'EEM_') === 0) { |
|
352 | + $class_name = 'LEGACY_MODELS'; |
|
353 | + } |
|
354 | + $dependency = $this->get_alias($dependency); |
|
355 | + return $this->has_dependency_for_class($class_name, $dependency) |
|
356 | + ? $this->_dependency_map[$class_name][$dependency] |
|
357 | + : EE_Dependency_Map::not_registered; |
|
358 | + } |
|
359 | + |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * @param string $class_name |
|
364 | + * @return string | Closure |
|
365 | + */ |
|
366 | + public function class_loader($class_name) |
|
367 | + { |
|
368 | + // all legacy models use load_model() |
|
369 | + if(strpos($class_name, 'EEM_') === 0){ |
|
370 | + return 'load_model'; |
|
371 | + } |
|
372 | + $class_name = $this->get_alias($class_name); |
|
373 | + return isset($this->_class_loaders[$class_name]) ? $this->_class_loaders[$class_name] : ''; |
|
374 | + } |
|
375 | + |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * @return array |
|
380 | + */ |
|
381 | + public function class_loaders() |
|
382 | + { |
|
383 | + return $this->_class_loaders; |
|
384 | + } |
|
385 | + |
|
386 | + |
|
387 | + |
|
388 | + /** |
|
389 | + * adds an alias for a classname |
|
390 | + * |
|
391 | + * @param string $class_name the class name that should be used (concrete class to replace interface) |
|
392 | + * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
393 | + * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
394 | + */ |
|
395 | + public function add_alias($class_name, $alias, $for_class = '') |
|
396 | + { |
|
397 | + if ($for_class !== '') { |
|
398 | + if (! isset($this->_aliases[$for_class])) { |
|
399 | + $this->_aliases[$for_class] = array(); |
|
400 | + } |
|
401 | + $this->_aliases[$for_class][$class_name] = $alias; |
|
402 | + } |
|
403 | + $this->_aliases[$class_name] = $alias; |
|
404 | + } |
|
405 | + |
|
406 | + |
|
407 | + |
|
408 | + /** |
|
409 | + * returns TRUE if the provided class name has an alias |
|
410 | + * |
|
411 | + * @param string $class_name |
|
412 | + * @param string $for_class |
|
413 | + * @return bool |
|
414 | + */ |
|
415 | + public function has_alias($class_name = '', $for_class = '') |
|
416 | + { |
|
417 | + return isset($this->_aliases[$for_class][$class_name]) |
|
418 | + || ( |
|
419 | + isset($this->_aliases[$class_name]) |
|
420 | + && ! is_array($this->_aliases[$class_name]) |
|
421 | + ); |
|
422 | + } |
|
423 | + |
|
424 | + |
|
425 | + |
|
426 | + /** |
|
427 | + * returns alias for class name if one exists, otherwise returns the original classname |
|
428 | + * functions recursively, so that multiple aliases can be used to drill down to a classname |
|
429 | + * for example: |
|
430 | + * if the following two entries were added to the _aliases array: |
|
431 | + * array( |
|
432 | + * 'interface_alias' => 'some\namespace\interface' |
|
433 | + * 'some\namespace\interface' => 'some\namespace\classname' |
|
434 | + * ) |
|
435 | + * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
436 | + * to load an instance of 'some\namespace\classname' |
|
437 | + * |
|
438 | + * @param string $class_name |
|
439 | + * @param string $for_class |
|
440 | + * @return string |
|
441 | + */ |
|
442 | + public function get_alias($class_name = '', $for_class = '') |
|
443 | + { |
|
444 | + if (! $this->has_alias($class_name, $for_class)) { |
|
445 | + return $class_name; |
|
446 | + } |
|
447 | + if ($for_class !== '' && isset($this->_aliases[ $for_class ][ $class_name ])) { |
|
448 | + return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
|
449 | + } |
|
450 | + return $this->get_alias($this->_aliases[$class_name]); |
|
451 | + } |
|
452 | + |
|
453 | + |
|
454 | + |
|
455 | + /** |
|
456 | + * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
457 | + * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
458 | + * This is done by using the following class constants: |
|
459 | + * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
460 | + * EE_Dependency_Map::load_new_object - generates a new object every time |
|
461 | + */ |
|
462 | + protected function _register_core_dependencies() |
|
463 | + { |
|
464 | + $this->_dependency_map = array( |
|
465 | + 'EE_Request_Handler' => array( |
|
466 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
467 | + ), |
|
468 | + 'EE_System' => array( |
|
469 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
470 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
471 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
472 | + 'EE_Maintenance_Mode' => EE_Dependency_Map::load_from_cache, |
|
473 | + ), |
|
474 | + 'EE_Session' => array( |
|
475 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
476 | + 'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache, |
|
477 | + 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
478 | + ), |
|
479 | + 'EE_Cart' => array( |
|
480 | + 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
481 | + ), |
|
482 | + 'EE_Front_Controller' => array( |
|
483 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
484 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
485 | + 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
486 | + ), |
|
487 | + 'EE_Messenger_Collection_Loader' => array( |
|
488 | + 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
489 | + ), |
|
490 | + 'EE_Message_Type_Collection_Loader' => array( |
|
491 | + 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
492 | + ), |
|
493 | + 'EE_Message_Resource_Manager' => array( |
|
494 | + 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
495 | + 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
496 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
497 | + ), |
|
498 | + 'EE_Message_Factory' => array( |
|
499 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
500 | + ), |
|
501 | + 'EE_messages' => array( |
|
502 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
503 | + ), |
|
504 | + 'EE_Messages_Generator' => array( |
|
505 | + 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
506 | + 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
507 | + 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
508 | + 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
509 | + ), |
|
510 | + 'EE_Messages_Processor' => array( |
|
511 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
512 | + ), |
|
513 | + 'EE_Messages_Queue' => array( |
|
514 | + 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
515 | + ), |
|
516 | + 'EE_Messages_Template_Defaults' => array( |
|
517 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
518 | + 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
519 | + ), |
|
520 | + 'EE_Message_To_Generate_From_Request' => array( |
|
521 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
522 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
523 | + ), |
|
524 | + 'EventEspresso\core\services\commands\CommandBus' => array( |
|
525 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
526 | + ), |
|
527 | + 'EventEspresso\services\commands\CommandHandler' => array( |
|
528 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
529 | + 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
530 | + ), |
|
531 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
532 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
533 | + ), |
|
534 | + 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
535 | + 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
536 | + 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
537 | + ), |
|
538 | + 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
539 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
540 | + ), |
|
541 | + 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
542 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
543 | + ), |
|
544 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
545 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
546 | + ), |
|
547 | + 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
548 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
549 | + ), |
|
550 | + 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
551 | + 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
552 | + ), |
|
553 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
554 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
555 | + ), |
|
556 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
557 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
558 | + ), |
|
559 | + 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
560 | + 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
561 | + ), |
|
562 | + 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
563 | + 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
564 | + ), |
|
565 | + 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
566 | + 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
567 | + ), |
|
568 | + 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
569 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
570 | + ), |
|
571 | + 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
572 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
573 | + ), |
|
574 | + 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler' => array( |
|
575 | + 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
576 | + ), |
|
577 | + 'EventEspresso\core\services\database\TableManager' => array( |
|
578 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
579 | + ), |
|
580 | + 'EE_Data_Migration_Class_Base' => array( |
|
581 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
582 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
583 | + ), |
|
584 | + 'EE_DMS_Core_4_1_0' => array( |
|
585 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
586 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
587 | + ), |
|
588 | + 'EE_DMS_Core_4_2_0' => array( |
|
589 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
590 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
591 | + ), |
|
592 | + 'EE_DMS_Core_4_3_0' => array( |
|
593 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
594 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
595 | + ), |
|
596 | + 'EE_DMS_Core_4_4_0' => array( |
|
597 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
598 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
599 | + ), |
|
600 | + 'EE_DMS_Core_4_5_0' => array( |
|
601 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
602 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
603 | + ), |
|
604 | + 'EE_DMS_Core_4_6_0' => array( |
|
605 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
606 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
607 | + ), |
|
608 | + 'EE_DMS_Core_4_7_0' => array( |
|
609 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
610 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
611 | + ), |
|
612 | + 'EE_DMS_Core_4_8_0' => array( |
|
613 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
614 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
615 | + ), |
|
616 | + 'EE_DMS_Core_4_9_0' => array( |
|
617 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
618 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
619 | + ), |
|
620 | + 'EventEspresso\core\services\assets\Registry' => array( |
|
621 | + 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
622 | + 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
623 | + 'EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache |
|
624 | + ), |
|
625 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
626 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
627 | + ), |
|
628 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
629 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
630 | + ), |
|
631 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
632 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
633 | + ), |
|
634 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
635 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
636 | + ), |
|
637 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
638 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
639 | + ), |
|
640 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
641 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
642 | + ), |
|
643 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
644 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
645 | + ), |
|
646 | + 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
647 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
648 | + ), |
|
649 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
650 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
651 | + ), |
|
652 | + 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => array( |
|
653 | + 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
654 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
655 | + ), |
|
656 | + 'EventEspresso\core\domain\values\EmailAddress' => array( |
|
657 | + null, |
|
658 | + 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => EE_Dependency_Map::load_from_cache, |
|
659 | + ), |
|
660 | + 'EventEspresso\core\services\orm\ModelFieldFactory' => array( |
|
661 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
662 | + ), |
|
663 | + 'LEGACY_MODELS' => array( |
|
664 | + null, |
|
665 | + 'EventEspresso\core\services\database\ModelFieldFactory' => EE_Dependency_Map::load_from_cache, |
|
666 | + ), |
|
667 | + 'EE_Module_Request_Router' => array( |
|
668 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
669 | + ), |
|
670 | + 'EE_Registration_Processor' => array( |
|
671 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
672 | + ), |
|
673 | + 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' => array( |
|
674 | + null, |
|
675 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
676 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
677 | + ), |
|
678 | + ); |
|
679 | + } |
|
680 | + |
|
681 | + |
|
682 | + |
|
683 | + /** |
|
684 | + * Registers how core classes are loaded. |
|
685 | + * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
686 | + * 'EE_Request_Handler' => 'load_core' |
|
687 | + * 'EE_Messages_Queue' => 'load_lib' |
|
688 | + * 'EEH_Debug_Tools' => 'load_helper' |
|
689 | + * or, if greater control is required, by providing a custom closure. For example: |
|
690 | + * 'Some_Class' => function () { |
|
691 | + * return new Some_Class(); |
|
692 | + * }, |
|
693 | + * This is required for instantiating dependencies |
|
694 | + * where an interface has been type hinted in a class constructor. For example: |
|
695 | + * 'Required_Interface' => function () { |
|
696 | + * return new A_Class_That_Implements_Required_Interface(); |
|
697 | + * }, |
|
698 | + * |
|
699 | + * @throws InvalidInterfaceException |
|
700 | + * @throws InvalidDataTypeException |
|
701 | + * @throws InvalidArgumentException |
|
702 | + */ |
|
703 | + protected function _register_core_class_loaders() |
|
704 | + { |
|
705 | + //for PHP5.3 compat, we need to register any properties called here in a variable because `$this` cannot |
|
706 | + //be used in a closure. |
|
707 | + $request = &$this->request; |
|
708 | + $response = &$this->response; |
|
709 | + $legacy_request = &$this->legacy_request; |
|
710 | + // $loader = &$this->loader; |
|
711 | + $this->_class_loaders = array( |
|
712 | + //load_core |
|
713 | + 'EE_Capabilities' => 'load_core', |
|
714 | + 'EE_Encryption' => 'load_core', |
|
715 | + 'EE_Front_Controller' => 'load_core', |
|
716 | + 'EE_Module_Request_Router' => 'load_core', |
|
717 | + 'EE_Registry' => 'load_core', |
|
718 | + 'EE_Request' => function () use (&$legacy_request) { |
|
719 | + return $legacy_request; |
|
720 | + }, |
|
721 | + 'EventEspresso\core\services\request\Request' => function () use (&$request) { |
|
722 | + return $request; |
|
723 | + }, |
|
724 | + 'EventEspresso\core\services\request\Response' => function () use (&$response) { |
|
725 | + return $response; |
|
726 | + }, |
|
727 | + 'EE_Request_Handler' => 'load_core', |
|
728 | + 'EE_Session' => 'load_core', |
|
729 | + 'EE_Cron_Tasks' => 'load_core', |
|
730 | + 'EE_System' => 'load_core', |
|
731 | + 'EE_Maintenance_Mode' => 'load_core', |
|
732 | + 'EE_Register_CPTs' => 'load_core', |
|
733 | + 'EE_Admin' => 'load_core', |
|
734 | + //load_lib |
|
735 | + 'EE_Message_Resource_Manager' => 'load_lib', |
|
736 | + 'EE_Message_Type_Collection' => 'load_lib', |
|
737 | + 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
738 | + 'EE_Messenger_Collection' => 'load_lib', |
|
739 | + 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
740 | + 'EE_Messages_Processor' => 'load_lib', |
|
741 | + 'EE_Message_Repository' => 'load_lib', |
|
742 | + 'EE_Messages_Queue' => 'load_lib', |
|
743 | + 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
744 | + 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
745 | + 'EE_Payment_Method_Manager' => 'load_lib', |
|
746 | + 'EE_Messages_Generator' => function () { |
|
747 | + return EE_Registry::instance()->load_lib( |
|
748 | + 'Messages_Generator', |
|
749 | + array(), |
|
750 | + false, |
|
751 | + false |
|
752 | + ); |
|
753 | + }, |
|
754 | + 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
755 | + return EE_Registry::instance()->load_lib( |
|
756 | + 'Messages_Template_Defaults', |
|
757 | + $arguments, |
|
758 | + false, |
|
759 | + false |
|
760 | + ); |
|
761 | + }, |
|
762 | + //load_model |
|
763 | + // 'EEM_Attendee' => 'load_model', |
|
764 | + // 'EEM_Message_Template_Group' => 'load_model', |
|
765 | + // 'EEM_Message_Template' => 'load_model', |
|
766 | + //load_helper |
|
767 | + 'EEH_Parse_Shortcodes' => function () { |
|
768 | + if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
769 | + return new EEH_Parse_Shortcodes(); |
|
770 | + } |
|
771 | + return null; |
|
772 | + }, |
|
773 | + 'EE_Template_Config' => function () { |
|
774 | + return EE_Config::instance()->template_settings; |
|
775 | + }, |
|
776 | + 'EE_Currency_Config' => function () { |
|
777 | + return EE_Config::instance()->currency; |
|
778 | + }, |
|
779 | + 'EE_Registration_Config' => function () { |
|
780 | + return EE_Config::instance()->registration; |
|
781 | + }, |
|
782 | + 'EventEspresso\core\services\loaders\Loader' => function () { |
|
783 | + return LoaderFactory::getLoader(); |
|
784 | + }, |
|
785 | + ); |
|
786 | + } |
|
787 | + |
|
788 | + |
|
789 | + |
|
790 | + /** |
|
791 | + * can be used for supplying alternate names for classes, |
|
792 | + * or for connecting interface names to instantiable classes |
|
793 | + */ |
|
794 | + protected function _register_core_aliases() |
|
795 | + { |
|
796 | + $this->_aliases = array( |
|
797 | + 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
798 | + 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
799 | + 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
800 | + 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
801 | + 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
802 | + 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
803 | + 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
804 | + 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
805 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
806 | + 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
807 | + 'CreateRegCodeCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegCodeCommand', |
|
808 | + 'CreateRegUrlLinkCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegUrlLinkCommand', |
|
809 | + 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
810 | + 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
811 | + 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
812 | + 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
813 | + 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
814 | + 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
815 | + 'CreateTransactionCommandHandler' => 'EventEspresso\core\services\commands\transaction\CreateTransactionCommandHandler', |
|
816 | + 'CreateAttendeeCommandHandler' => 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler', |
|
817 | + 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
818 | + 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
819 | + 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
820 | + 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
821 | + 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
822 | + 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
823 | + 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
824 | + 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
825 | + 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
826 | + 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
827 | + 'EventEspresso\core\domain\services\session\SessionIdentifierInterface' => 'EE_Session', |
|
828 | + 'EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface', |
|
829 | + 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidationService', |
|
830 | + 'NoticeConverterInterface' => 'EventEspresso\core\services\notices\NoticeConverterInterface', |
|
831 | + 'EventEspresso\core\services\notices\NoticeConverterInterface' => 'EventEspresso\core\services\notices\ConvertNoticesToEeErrors', |
|
832 | + 'NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainerInterface', |
|
833 | + 'EventEspresso\core\services\notices\NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainer', |
|
834 | + 'EventEspresso\core\services\request\RequestInterface' => 'EventEspresso\core\services\request\Request', |
|
835 | + 'EventEspresso\core\services\request\ResponseInterface' => 'EventEspresso\core\services\request\Response', |
|
836 | + 'EventEspresso\core\domain\DomainInterface' => 'EventEspresso\core\domain\Domain', |
|
837 | + ); |
|
838 | + if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) { |
|
839 | + $this->_aliases['EventEspresso\core\services\notices\NoticeConverterInterface'] = 'EventEspresso\core\services\notices\ConvertNoticesToAdminNotices'; |
|
840 | + } |
|
841 | + } |
|
842 | + |
|
843 | + |
|
844 | + |
|
845 | + /** |
|
846 | + * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
847 | + * request Primarily used by unit tests. |
|
848 | + * |
|
849 | + * @throws InvalidDataTypeException |
|
850 | + * @throws InvalidInterfaceException |
|
851 | + * @throws InvalidArgumentException |
|
852 | + */ |
|
853 | + public function reset() |
|
854 | + { |
|
855 | + $this->_register_core_class_loaders(); |
|
856 | + $this->_register_core_dependencies(); |
|
857 | + } |
|
858 | 858 | |
859 | 859 | |
860 | 860 | } |
@@ -19,33 +19,33 @@ |
||
19 | 19 | class InvalidRequestStackMiddlewareException extends InvalidDataTypeException |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * @param mixed $middleware_app_class |
|
24 | - * @param string $message |
|
25 | - * @param int $code |
|
26 | - * @param Exception $previous |
|
27 | - */ |
|
28 | - public function __construct($middleware_app_class, $message = '', $code = 0, Exception $previous = null) |
|
29 | - { |
|
30 | - if(is_array($middleware_app_class)) { |
|
31 | - $middleware_app_class = reset($middleware_app_class); |
|
32 | - } |
|
33 | - if (empty($message)) { |
|
34 | - $message = sprintf( |
|
35 | - esc_html__( |
|
36 | - 'The supplied Request Stack Middleware class "%1$s" is invalid or could no be found.', |
|
37 | - 'event_espresso' |
|
38 | - ), |
|
39 | - $middleware_app_class |
|
40 | - ); |
|
41 | - } |
|
42 | - parent::__construct( |
|
43 | - '$middleware_app_class', |
|
44 | - $middleware_app_class, |
|
45 | - 'EventEspresso\core\services\request\middleware\Middleware', |
|
46 | - $message, |
|
47 | - $code, |
|
48 | - $previous |
|
49 | - ); |
|
50 | - } |
|
22 | + /** |
|
23 | + * @param mixed $middleware_app_class |
|
24 | + * @param string $message |
|
25 | + * @param int $code |
|
26 | + * @param Exception $previous |
|
27 | + */ |
|
28 | + public function __construct($middleware_app_class, $message = '', $code = 0, Exception $previous = null) |
|
29 | + { |
|
30 | + if(is_array($middleware_app_class)) { |
|
31 | + $middleware_app_class = reset($middleware_app_class); |
|
32 | + } |
|
33 | + if (empty($message)) { |
|
34 | + $message = sprintf( |
|
35 | + esc_html__( |
|
36 | + 'The supplied Request Stack Middleware class "%1$s" is invalid or could no be found.', |
|
37 | + 'event_espresso' |
|
38 | + ), |
|
39 | + $middleware_app_class |
|
40 | + ); |
|
41 | + } |
|
42 | + parent::__construct( |
|
43 | + '$middleware_app_class', |
|
44 | + $middleware_app_class, |
|
45 | + 'EventEspresso\core\services\request\middleware\Middleware', |
|
46 | + $message, |
|
47 | + $code, |
|
48 | + $previous |
|
49 | + ); |
|
50 | + } |
|
51 | 51 | } |
@@ -27,7 +27,7 @@ |
||
27 | 27 | */ |
28 | 28 | public function __construct($middleware_app_class, $message = '', $code = 0, Exception $previous = null) |
29 | 29 | { |
30 | - if(is_array($middleware_app_class)) { |
|
30 | + if (is_array($middleware_app_class)) { |
|
31 | 31 | $middleware_app_class = reset($middleware_app_class); |
32 | 32 | } |
33 | 33 | if (empty($message)) { |
@@ -24,97 +24,97 @@ |
||
24 | 24 | class RequestStackBuilder extends SplDoublyLinkedList |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * @type LoaderInterface $loader |
|
29 | - */ |
|
30 | - private $loader; |
|
27 | + /** |
|
28 | + * @type LoaderInterface $loader |
|
29 | + */ |
|
30 | + private $loader; |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * RequestStackBuilder constructor. |
|
35 | - * |
|
36 | - * @param LoaderInterface $loader |
|
37 | - */ |
|
38 | - public function __construct(LoaderInterface $loader) |
|
39 | - { |
|
40 | - $this->loader = $loader; |
|
41 | - $this->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
|
42 | - } |
|
33 | + /** |
|
34 | + * RequestStackBuilder constructor. |
|
35 | + * |
|
36 | + * @param LoaderInterface $loader |
|
37 | + */ |
|
38 | + public function __construct(LoaderInterface $loader) |
|
39 | + { |
|
40 | + $this->loader = $loader; |
|
41 | + $this->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
|
42 | + } |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * builds decorated middleware stack |
|
47 | - * by continuously injecting previous middleware app into the next |
|
48 | - * |
|
49 | - * @param RequestStackCoreAppInterface $application |
|
50 | - * @return RequestStack |
|
51 | - * @throws Exception |
|
52 | - */ |
|
53 | - public function resolve(RequestStackCoreAppInterface $application) |
|
54 | - { |
|
55 | - $core_app = $application; |
|
56 | - // NOW... because the RequestStack is following the decorator pattern, |
|
57 | - // the first stack app we add will end up at the center of the stack, |
|
58 | - // and will end up being the last item to actually run, but we don't want that! |
|
59 | - // Basically we're dealing with TWO stacks, and transferring items from one to the other, |
|
60 | - // BUT... we want the final stack to be in the same order as the first. |
|
61 | - // So we need to reverse the iterator mode when transferring items, |
|
62 | - // because if we don't, the second stack will end up in the incorrect order. |
|
63 | - $this->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
|
64 | - for ($this->rewind(); $this->valid(); $this->next()) { |
|
65 | - try { |
|
66 | - $middleware_app = $this->validateMiddlewareAppDetails($this->current(), true); |
|
67 | - $middleware_app_class = array_shift($middleware_app); |
|
68 | - $middleware_app_args = is_array($middleware_app) ? $middleware_app : array(); |
|
69 | - $middleware_app_args = array($application, $this->loader) + $middleware_app_args; |
|
70 | - $application = $this->loader->getShared($middleware_app_class, $middleware_app_args); |
|
71 | - } catch (InvalidRequestStackMiddlewareException $exception) { |
|
72 | - if(WP_DEBUG) { |
|
73 | - new ExceptionStackTraceDisplay($exception); |
|
74 | - continue; |
|
75 | - } |
|
76 | - error_log($exception->getMessage()); |
|
77 | - } |
|
78 | - } |
|
79 | - return new RequestStack($application, $core_app); |
|
80 | - } |
|
45 | + /** |
|
46 | + * builds decorated middleware stack |
|
47 | + * by continuously injecting previous middleware app into the next |
|
48 | + * |
|
49 | + * @param RequestStackCoreAppInterface $application |
|
50 | + * @return RequestStack |
|
51 | + * @throws Exception |
|
52 | + */ |
|
53 | + public function resolve(RequestStackCoreAppInterface $application) |
|
54 | + { |
|
55 | + $core_app = $application; |
|
56 | + // NOW... because the RequestStack is following the decorator pattern, |
|
57 | + // the first stack app we add will end up at the center of the stack, |
|
58 | + // and will end up being the last item to actually run, but we don't want that! |
|
59 | + // Basically we're dealing with TWO stacks, and transferring items from one to the other, |
|
60 | + // BUT... we want the final stack to be in the same order as the first. |
|
61 | + // So we need to reverse the iterator mode when transferring items, |
|
62 | + // because if we don't, the second stack will end up in the incorrect order. |
|
63 | + $this->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
|
64 | + for ($this->rewind(); $this->valid(); $this->next()) { |
|
65 | + try { |
|
66 | + $middleware_app = $this->validateMiddlewareAppDetails($this->current(), true); |
|
67 | + $middleware_app_class = array_shift($middleware_app); |
|
68 | + $middleware_app_args = is_array($middleware_app) ? $middleware_app : array(); |
|
69 | + $middleware_app_args = array($application, $this->loader) + $middleware_app_args; |
|
70 | + $application = $this->loader->getShared($middleware_app_class, $middleware_app_args); |
|
71 | + } catch (InvalidRequestStackMiddlewareException $exception) { |
|
72 | + if(WP_DEBUG) { |
|
73 | + new ExceptionStackTraceDisplay($exception); |
|
74 | + continue; |
|
75 | + } |
|
76 | + error_log($exception->getMessage()); |
|
77 | + } |
|
78 | + } |
|
79 | + return new RequestStack($application, $core_app); |
|
80 | + } |
|
81 | 81 | |
82 | 82 | |
83 | - /** |
|
84 | - * Ensures that the app details that have been pushed onto RequestStackBuilder |
|
85 | - * are all ordered correctly so that the middleware can be properly constructed |
|
86 | - * |
|
87 | - * @param array $middleware_app |
|
88 | - * @param bool $recurse |
|
89 | - * @return array |
|
90 | - * @throws InvalidRequestStackMiddlewareException |
|
91 | - */ |
|
92 | - protected function validateMiddlewareAppDetails(array $middleware_app, $recurse = false) |
|
93 | - { |
|
94 | - $middleware_app_class = reset($middleware_app); |
|
95 | - // is array empty ? |
|
96 | - if($middleware_app_class === false) { |
|
97 | - throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
|
98 | - } |
|
99 | - // are the class and arguments in the wrong order ? |
|
100 | - if(is_array($middleware_app_class)) { |
|
101 | - if ($recurse === true) { |
|
102 | - return $this->validateMiddlewareAppDetails(array_reverse($middleware_app)); |
|
103 | - } |
|
104 | - throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
|
105 | - } |
|
106 | - // is filter callback working like legacy middleware and sending a numerically indexed array ? |
|
107 | - if(is_int($middleware_app_class)) { |
|
108 | - if ($recurse === true) { |
|
109 | - $middleware_app = array_reverse($middleware_app); |
|
110 | - return $this->validateMiddlewareAppDetails(array(reset($middleware_app), array())); |
|
111 | - } |
|
112 | - throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
|
113 | - } |
|
114 | - // is $middleware_app_class a valid FQCN (or class is already loaded) ? |
|
115 | - if(! class_exists($middleware_app_class)) { |
|
116 | - throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
|
117 | - } |
|
118 | - return $middleware_app; |
|
119 | - } |
|
83 | + /** |
|
84 | + * Ensures that the app details that have been pushed onto RequestStackBuilder |
|
85 | + * are all ordered correctly so that the middleware can be properly constructed |
|
86 | + * |
|
87 | + * @param array $middleware_app |
|
88 | + * @param bool $recurse |
|
89 | + * @return array |
|
90 | + * @throws InvalidRequestStackMiddlewareException |
|
91 | + */ |
|
92 | + protected function validateMiddlewareAppDetails(array $middleware_app, $recurse = false) |
|
93 | + { |
|
94 | + $middleware_app_class = reset($middleware_app); |
|
95 | + // is array empty ? |
|
96 | + if($middleware_app_class === false) { |
|
97 | + throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
|
98 | + } |
|
99 | + // are the class and arguments in the wrong order ? |
|
100 | + if(is_array($middleware_app_class)) { |
|
101 | + if ($recurse === true) { |
|
102 | + return $this->validateMiddlewareAppDetails(array_reverse($middleware_app)); |
|
103 | + } |
|
104 | + throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
|
105 | + } |
|
106 | + // is filter callback working like legacy middleware and sending a numerically indexed array ? |
|
107 | + if(is_int($middleware_app_class)) { |
|
108 | + if ($recurse === true) { |
|
109 | + $middleware_app = array_reverse($middleware_app); |
|
110 | + return $this->validateMiddlewareAppDetails(array(reset($middleware_app), array())); |
|
111 | + } |
|
112 | + throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
|
113 | + } |
|
114 | + // is $middleware_app_class a valid FQCN (or class is already loaded) ? |
|
115 | + if(! class_exists($middleware_app_class)) { |
|
116 | + throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
|
117 | + } |
|
118 | + return $middleware_app; |
|
119 | + } |
|
120 | 120 | } |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | $middleware_app_args = array($application, $this->loader) + $middleware_app_args; |
70 | 70 | $application = $this->loader->getShared($middleware_app_class, $middleware_app_args); |
71 | 71 | } catch (InvalidRequestStackMiddlewareException $exception) { |
72 | - if(WP_DEBUG) { |
|
72 | + if (WP_DEBUG) { |
|
73 | 73 | new ExceptionStackTraceDisplay($exception); |
74 | 74 | continue; |
75 | 75 | } |
@@ -93,18 +93,18 @@ discard block |
||
93 | 93 | { |
94 | 94 | $middleware_app_class = reset($middleware_app); |
95 | 95 | // is array empty ? |
96 | - if($middleware_app_class === false) { |
|
96 | + if ($middleware_app_class === false) { |
|
97 | 97 | throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
98 | 98 | } |
99 | 99 | // are the class and arguments in the wrong order ? |
100 | - if(is_array($middleware_app_class)) { |
|
100 | + if (is_array($middleware_app_class)) { |
|
101 | 101 | if ($recurse === true) { |
102 | 102 | return $this->validateMiddlewareAppDetails(array_reverse($middleware_app)); |
103 | 103 | } |
104 | 104 | throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
105 | 105 | } |
106 | 106 | // is filter callback working like legacy middleware and sending a numerically indexed array ? |
107 | - if(is_int($middleware_app_class)) { |
|
107 | + if (is_int($middleware_app_class)) { |
|
108 | 108 | if ($recurse === true) { |
109 | 109 | $middleware_app = array_reverse($middleware_app); |
110 | 110 | return $this->validateMiddlewareAppDetails(array(reset($middleware_app), array())); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
113 | 113 | } |
114 | 114 | // is $middleware_app_class a valid FQCN (or class is already loaded) ? |
115 | - if(! class_exists($middleware_app_class)) { |
|
115 | + if ( ! class_exists($middleware_app_class)) { |
|
116 | 116 | throw new InvalidRequestStackMiddlewareException($middleware_app_class); |
117 | 117 | } |
118 | 118 | return $middleware_app; |
@@ -16,49 +16,49 @@ |
||
16 | 16 | interface DomainInterface extends InterminableInterface |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @return string |
|
21 | - * @throws DomainException |
|
22 | - */ |
|
23 | - public function pluginFile(); |
|
19 | + /** |
|
20 | + * @return string |
|
21 | + * @throws DomainException |
|
22 | + */ |
|
23 | + public function pluginFile(); |
|
24 | 24 | |
25 | 25 | |
26 | - /** |
|
27 | - * @return string |
|
28 | - * @throws DomainException |
|
29 | - */ |
|
30 | - public function pluginBasename(); |
|
26 | + /** |
|
27 | + * @return string |
|
28 | + * @throws DomainException |
|
29 | + */ |
|
30 | + public function pluginBasename(); |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - public function pluginPath(); |
|
33 | + /** |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + public function pluginPath(); |
|
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * @return string |
|
41 | - * @throws DomainException |
|
42 | - */ |
|
43 | - public function pluginUrl(); |
|
39 | + /** |
|
40 | + * @return string |
|
41 | + * @throws DomainException |
|
42 | + */ |
|
43 | + public function pluginUrl(); |
|
44 | 44 | |
45 | 45 | |
46 | - /** |
|
47 | - * @return string |
|
48 | - * @throws DomainException |
|
49 | - */ |
|
50 | - public function version(); |
|
46 | + /** |
|
47 | + * @return string |
|
48 | + * @throws DomainException |
|
49 | + */ |
|
50 | + public function version(); |
|
51 | 51 | |
52 | 52 | |
53 | - /** |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - public function distributionAssetsPath(); |
|
53 | + /** |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + public function distributionAssetsPath(); |
|
57 | 57 | |
58 | 58 | |
59 | - /** |
|
60 | - * @return string |
|
61 | - */ |
|
62 | - public function distributionAssetsUrl(); |
|
59 | + /** |
|
60 | + * @return string |
|
61 | + */ |
|
62 | + public function distributionAssetsUrl(); |
|
63 | 63 | |
64 | 64 | } |
@@ -14,1506 +14,1506 @@ |
||
14 | 14 | class EE_Form_Section_Proper extends EE_Form_Section_Validatable |
15 | 15 | { |
16 | 16 | |
17 | - const SUBMITTED_FORM_DATA_SSN_KEY = 'submitted_form_data'; |
|
18 | - |
|
19 | - /** |
|
20 | - * Subsections |
|
21 | - * |
|
22 | - * @var EE_Form_Section_Validatable[] |
|
23 | - */ |
|
24 | - protected $_subsections = array(); |
|
25 | - |
|
26 | - /** |
|
27 | - * Strategy for laying out the form |
|
28 | - * |
|
29 | - * @var EE_Form_Section_Layout_Base |
|
30 | - */ |
|
31 | - protected $_layout_strategy; |
|
32 | - |
|
33 | - /** |
|
34 | - * Whether or not this form has received and validated a form submission yet |
|
35 | - * |
|
36 | - * @var boolean |
|
37 | - */ |
|
38 | - protected $_received_submission = false; |
|
39 | - |
|
40 | - /** |
|
41 | - * message displayed to users upon successful form submission |
|
42 | - * |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - protected $_form_submission_success_message = ''; |
|
46 | - |
|
47 | - /** |
|
48 | - * message displayed to users upon unsuccessful form submission |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - protected $_form_submission_error_message = ''; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var array like $_REQUEST |
|
56 | - */ |
|
57 | - protected $cached_request_data; |
|
58 | - |
|
59 | - /** |
|
60 | - * Stores whether this form (and its sub-sections) were found to be valid or not. |
|
61 | - * Starts off as null, but once the form is validated, it set to either true or false |
|
62 | - * @var boolean|null |
|
63 | - */ |
|
64 | - protected $is_valid; |
|
65 | - |
|
66 | - /** |
|
67 | - * Stores all the data that will localized for form validation |
|
68 | - * |
|
69 | - * @var array |
|
70 | - */ |
|
71 | - static protected $_js_localization = array(); |
|
72 | - |
|
73 | - /** |
|
74 | - * whether or not the form's localized validation JS vars have been set |
|
75 | - * |
|
76 | - * @type boolean |
|
77 | - */ |
|
78 | - static protected $_scripts_localized = false; |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * when constructing a proper form section, calls _construct_finalize on children |
|
83 | - * so that they know who their parent is, and what name they've been given. |
|
84 | - * |
|
85 | - * @param array[] $options_array { |
|
86 | - * @type $subsections EE_Form_Section_Validatable[] where keys are the section's name |
|
87 | - * @type $include string[] numerically-indexed where values are section names to be included, |
|
88 | - * and in that order. This is handy if you want |
|
89 | - * the subsections to be ordered differently than the default, and if you override |
|
90 | - * which fields are shown |
|
91 | - * @type $exclude string[] values are subsections to be excluded. This is handy if you want |
|
92 | - * to remove certain default subsections (note: if you specify BOTH 'include' AND |
|
93 | - * 'exclude', the inclusions will be applied first, and the exclusions will exclude |
|
94 | - * items from that list of inclusions) |
|
95 | - * @type $layout_strategy EE_Form_Section_Layout_Base strategy for laying out the form |
|
96 | - * } @see EE_Form_Section_Validatable::__construct() |
|
97 | - * @throws EE_Error |
|
98 | - */ |
|
99 | - public function __construct($options_array = array()) |
|
100 | - { |
|
101 | - $options_array = (array) apply_filters( |
|
102 | - 'FHEE__EE_Form_Section_Proper___construct__options_array', |
|
103 | - $options_array, |
|
104 | - $this |
|
105 | - ); |
|
106 | - //call parent first, as it may be setting the name |
|
107 | - parent::__construct($options_array); |
|
108 | - //if they've included subsections in the constructor, add them now |
|
109 | - if (isset($options_array['include'])) { |
|
110 | - //we are going to make sure we ONLY have those subsections to include |
|
111 | - //AND we are going to make sure they're in that specified order |
|
112 | - $reordered_subsections = array(); |
|
113 | - foreach ($options_array['include'] as $input_name) { |
|
114 | - if (isset($this->_subsections[ $input_name ])) { |
|
115 | - $reordered_subsections[ $input_name ] = $this->_subsections[ $input_name ]; |
|
116 | - } |
|
117 | - } |
|
118 | - $this->_subsections = $reordered_subsections; |
|
119 | - } |
|
120 | - if (isset($options_array['exclude'])) { |
|
121 | - $exclude = $options_array['exclude']; |
|
122 | - $this->_subsections = array_diff_key($this->_subsections, array_flip($exclude)); |
|
123 | - } |
|
124 | - if (isset($options_array['layout_strategy'])) { |
|
125 | - $this->_layout_strategy = $options_array['layout_strategy']; |
|
126 | - } |
|
127 | - if (! $this->_layout_strategy) { |
|
128 | - $this->_layout_strategy = is_admin() ? new EE_Admin_Two_Column_Layout() : new EE_Two_Column_Layout(); |
|
129 | - } |
|
130 | - $this->_layout_strategy->_construct_finalize($this); |
|
131 | - //ok so we are definitely going to want the forms JS, |
|
132 | - //so enqueue it or remember to enqueue it during wp_enqueue_scripts |
|
133 | - if (did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts')) { |
|
134 | - //ok so they've constructed this object after when they should have. |
|
135 | - //just enqueue the generic form scripts and initialize the form immediately in the JS |
|
136 | - EE_Form_Section_Proper::wp_enqueue_scripts(true); |
|
137 | - } else { |
|
138 | - add_action('wp_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
139 | - add_action('admin_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
140 | - } |
|
141 | - add_action('wp_footer', array($this, 'ensure_scripts_localized'), 1); |
|
142 | - /** |
|
143 | - * Gives other plugins a chance to hook in before construct finalize is called. |
|
144 | - * The form probably doesn't yet have a parent form section. |
|
145 | - * Since 4.9.32, when this action was introduced, this is the best place to add a subsection onto a form, |
|
146 | - * assuming you don't care what the form section's name, HTML ID, or HTML name etc are. |
|
147 | - * Also see AHEE__EE_Form_Section_Proper___construct_finalize__end |
|
148 | - * |
|
149 | - * @since 4.9.32 |
|
150 | - * @param EE_Form_Section_Proper $this before __construct is done, but all of its logic, |
|
151 | - * except maybe calling _construct_finalize has been done |
|
152 | - * @param array $options_array options passed into the constructor |
|
153 | - */ |
|
154 | - do_action( |
|
155 | - 'AHEE__EE_Form_Input_Base___construct__before_construct_finalize_called', |
|
156 | - $this, |
|
157 | - $options_array |
|
158 | - ); |
|
159 | - if (isset($options_array['name'])) { |
|
160 | - $this->_construct_finalize(null, $options_array['name']); |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - /** |
|
166 | - * Finishes construction given the parent form section and this form section's name |
|
167 | - * |
|
168 | - * @param EE_Form_Section_Proper $parent_form_section |
|
169 | - * @param string $name |
|
170 | - * @throws EE_Error |
|
171 | - */ |
|
172 | - public function _construct_finalize($parent_form_section, $name) |
|
173 | - { |
|
174 | - parent::_construct_finalize($parent_form_section, $name); |
|
175 | - $this->_set_default_name_if_empty(); |
|
176 | - $this->_set_default_html_id_if_empty(); |
|
177 | - foreach ($this->_subsections as $subsection_name => $subsection) { |
|
178 | - if ($subsection instanceof EE_Form_Section_Base) { |
|
179 | - $subsection->_construct_finalize($this, $subsection_name); |
|
180 | - } else { |
|
181 | - throw new EE_Error( |
|
182 | - sprintf( |
|
183 | - esc_html__( |
|
184 | - 'Subsection "%s" is not an instanceof EE_Form_Section_Base on form "%s". It is a "%s"', |
|
185 | - 'event_espresso' |
|
186 | - ), |
|
187 | - $subsection_name, |
|
188 | - get_class($this), |
|
189 | - $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso') |
|
190 | - ) |
|
191 | - ); |
|
192 | - } |
|
193 | - } |
|
194 | - /** |
|
195 | - * Action performed just after form has been given a name (and HTML ID etc) and is fully constructed. |
|
196 | - * If you have code that should modify the form and needs it and its subsections to have a name, HTML ID |
|
197 | - * (or other attributes derived from the name like the HTML label id, etc), this is where it should be done. |
|
198 | - * This might only happen just before displaying the form, or just before it receives form submission data. |
|
199 | - * If you need to modify the form or its subsections before _construct_finalize is called on it (and we've |
|
200 | - * ensured it has a name, HTML IDs, etc |
|
201 | - * |
|
202 | - * @param EE_Form_Section_Proper $this |
|
203 | - * @param EE_Form_Section_Proper|null $parent_form_section |
|
204 | - * @param string $name |
|
205 | - */ |
|
206 | - do_action( |
|
207 | - 'AHEE__EE_Form_Section_Proper___construct_finalize__end', |
|
208 | - $this, |
|
209 | - $parent_form_section, |
|
210 | - $name |
|
211 | - ); |
|
212 | - } |
|
213 | - |
|
214 | - |
|
215 | - /** |
|
216 | - * Gets the layout strategy for this form section |
|
217 | - * |
|
218 | - * @return EE_Form_Section_Layout_Base |
|
219 | - */ |
|
220 | - public function get_layout_strategy() |
|
221 | - { |
|
222 | - return $this->_layout_strategy; |
|
223 | - } |
|
224 | - |
|
225 | - |
|
226 | - /** |
|
227 | - * Gets the HTML for a single input for this form section according |
|
228 | - * to the layout strategy |
|
229 | - * |
|
230 | - * @param EE_Form_Input_Base $input |
|
231 | - * @return string |
|
232 | - */ |
|
233 | - public function get_html_for_input($input) |
|
234 | - { |
|
235 | - return $this->_layout_strategy->layout_input($input); |
|
236 | - } |
|
237 | - |
|
238 | - |
|
239 | - /** |
|
240 | - * was_submitted - checks if form inputs are present in request data |
|
241 | - * Basically an alias for form_data_present_in() (which is used by both |
|
242 | - * proper form sections and form inputs) |
|
243 | - * |
|
244 | - * @param null $form_data |
|
245 | - * @return boolean |
|
246 | - * @throws EE_Error |
|
247 | - */ |
|
248 | - public function was_submitted($form_data = null) |
|
249 | - { |
|
250 | - return $this->form_data_present_in($form_data); |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Gets the cached request data; but if there is none, or $req_data was set with |
|
255 | - * something different, refresh the cache, and then return it |
|
256 | - * @param null $req_data |
|
257 | - * @return array |
|
258 | - */ |
|
259 | - protected function getCachedRequest($req_data = null) |
|
260 | - { |
|
261 | - if ($this->cached_request_data === null |
|
262 | - || ( |
|
263 | - $req_data !== null && |
|
264 | - $req_data !== $this->cached_request_data |
|
265 | - ) |
|
266 | - ) { |
|
267 | - $req_data = apply_filters( |
|
268 | - 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
269 | - $req_data, |
|
270 | - $this |
|
271 | - ); |
|
272 | - if ($req_data === null) { |
|
273 | - $req_data = array_merge($_GET, $_POST); |
|
274 | - } |
|
275 | - $req_data = apply_filters( |
|
276 | - 'FHEE__EE_Form_Section_Proper__receive_form_submission__request_data', |
|
277 | - $req_data, |
|
278 | - $this |
|
279 | - ); |
|
280 | - $this->cached_request_data = (array)$req_data; |
|
281 | - } |
|
282 | - return $this->cached_request_data; |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - /** |
|
287 | - * After the form section is initially created, call this to sanitize the data in the submission |
|
288 | - * which relates to this form section, validate it, and set it as properties on the form. |
|
289 | - * |
|
290 | - * @param array|null $req_data should usually be $_POST (the default). |
|
291 | - * However, you CAN supply a different array. |
|
292 | - * Consider using set_defaults() instead however. |
|
293 | - * (If you rendered the form in the page using echo $form_x->get_html() |
|
294 | - * the inputs will have the correct name in the request data for this function |
|
295 | - * to find them and populate the form with them. |
|
296 | - * If you have a flat form (with only input subsections), |
|
297 | - * you can supply a flat array where keys |
|
298 | - * are the form input names and values are their values) |
|
299 | - * @param boolean $validate whether or not to perform validation on this data. Default is, |
|
300 | - * of course, to validate that data, and set errors on the invalid values. |
|
301 | - * But if the data has already been validated |
|
302 | - * (eg you validated the data then stored it in the DB) |
|
303 | - * you may want to skip this step. |
|
304 | - * @throws InvalidArgumentException |
|
305 | - * @throws InvalidInterfaceException |
|
306 | - * @throws InvalidDataTypeException |
|
307 | - * @throws EE_Error |
|
308 | - */ |
|
309 | - public function receive_form_submission($req_data = null, $validate = true) |
|
310 | - { |
|
311 | - $req_data = $this->getCachedRequest($req_data); |
|
312 | - $this->_normalize($req_data); |
|
313 | - if ($validate) { |
|
314 | - $this->_validate(); |
|
315 | - //if it's invalid, we're going to want to re-display so remember what they submitted |
|
316 | - if (! $this->is_valid()) { |
|
317 | - $this->store_submitted_form_data_in_session(); |
|
318 | - } |
|
319 | - } |
|
320 | - if ($this->submission_error_message() === '' && ! $this->is_valid()) { |
|
321 | - $this->set_submission_error_message(); |
|
322 | - } |
|
323 | - do_action( |
|
324 | - 'AHEE__EE_Form_Section_Proper__receive_form_submission__end', |
|
325 | - $req_data, |
|
326 | - $this, |
|
327 | - $validate |
|
328 | - ); |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * caches the originally submitted input values in the session |
|
334 | - * so that they can be used to repopulate the form if it failed validation |
|
335 | - * |
|
336 | - * @return boolean whether or not the data was successfully stored in the session |
|
337 | - * @throws InvalidArgumentException |
|
338 | - * @throws InvalidInterfaceException |
|
339 | - * @throws InvalidDataTypeException |
|
340 | - * @throws EE_Error |
|
341 | - */ |
|
342 | - protected function store_submitted_form_data_in_session() |
|
343 | - { |
|
344 | - return EE_Registry::instance()->SSN->set_session_data( |
|
345 | - array( |
|
346 | - EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY => $this->submitted_values(true), |
|
347 | - ) |
|
348 | - ); |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * retrieves the originally submitted input values in the session |
|
354 | - * so that they can be used to repopulate the form if it failed validation |
|
355 | - * |
|
356 | - * @return array |
|
357 | - * @throws InvalidArgumentException |
|
358 | - * @throws InvalidInterfaceException |
|
359 | - * @throws InvalidDataTypeException |
|
360 | - */ |
|
361 | - protected function get_submitted_form_data_from_session() |
|
362 | - { |
|
363 | - $session = EE_Registry::instance()->SSN; |
|
364 | - if ($session instanceof EE_Session) { |
|
365 | - return $session->get_session_data( |
|
366 | - EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY |
|
367 | - ); |
|
368 | - } |
|
369 | - return array(); |
|
370 | - } |
|
371 | - |
|
372 | - |
|
373 | - /** |
|
374 | - * flushed the originally submitted input values from the session |
|
375 | - * |
|
376 | - * @return boolean whether or not the data was successfully removed from the session |
|
377 | - * @throws InvalidArgumentException |
|
378 | - * @throws InvalidInterfaceException |
|
379 | - * @throws InvalidDataTypeException |
|
380 | - */ |
|
381 | - protected function flush_submitted_form_data_from_session() |
|
382 | - { |
|
383 | - return EE_Registry::instance()->SSN->reset_data( |
|
384 | - array(EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY) |
|
385 | - ); |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - /** |
|
390 | - * Populates this form and its subsections with data from the session. |
|
391 | - * (Wrapper for EE_Form_Section_Proper::receive_form_submission, so it shows |
|
392 | - * validation errors when displaying too) |
|
393 | - * Returns true if the form was populated from the session, false otherwise |
|
394 | - * |
|
395 | - * @return boolean |
|
396 | - * @throws InvalidArgumentException |
|
397 | - * @throws InvalidInterfaceException |
|
398 | - * @throws InvalidDataTypeException |
|
399 | - * @throws EE_Error |
|
400 | - */ |
|
401 | - public function populate_from_session() |
|
402 | - { |
|
403 | - $form_data_in_session = $this->get_submitted_form_data_from_session(); |
|
404 | - if (empty($form_data_in_session)) { |
|
405 | - return false; |
|
406 | - } |
|
407 | - $this->receive_form_submission($form_data_in_session); |
|
408 | - $this->flush_submitted_form_data_from_session(); |
|
409 | - if ($this->form_data_present_in($form_data_in_session)) { |
|
410 | - return true; |
|
411 | - } |
|
412 | - return false; |
|
413 | - } |
|
414 | - |
|
415 | - |
|
416 | - /** |
|
417 | - * Populates the default data for the form, given an array where keys are |
|
418 | - * the input names, and values are their values (preferably normalized to be their |
|
419 | - * proper PHP types, not all strings... although that should be ok too). |
|
420 | - * Proper subsections are sub-arrays, the key being the subsection's name, and |
|
421 | - * the value being an array formatted in teh same way |
|
422 | - * |
|
423 | - * @param array $default_data |
|
424 | - * @throws EE_Error |
|
425 | - */ |
|
426 | - public function populate_defaults($default_data) |
|
427 | - { |
|
428 | - foreach ($this->subsections(false) as $subsection_name => $subsection) { |
|
429 | - if (isset($default_data[ $subsection_name ])) { |
|
430 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
431 | - $subsection->set_default($default_data[ $subsection_name ]); |
|
432 | - } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
433 | - $subsection->populate_defaults($default_data[ $subsection_name ]); |
|
434 | - } |
|
435 | - } |
|
436 | - } |
|
437 | - } |
|
438 | - |
|
439 | - |
|
440 | - /** |
|
441 | - * returns true if subsection exists |
|
442 | - * |
|
443 | - * @param string $name |
|
444 | - * @return boolean |
|
445 | - */ |
|
446 | - public function subsection_exists($name) |
|
447 | - { |
|
448 | - return isset($this->_subsections[ $name ]) ? true : false; |
|
449 | - } |
|
450 | - |
|
451 | - |
|
452 | - /** |
|
453 | - * Gets the subsection specified by its name |
|
454 | - * |
|
455 | - * @param string $name |
|
456 | - * @param boolean $require_construction_to_be_finalized most client code should leave this as TRUE |
|
457 | - * so that the inputs will be properly configured. |
|
458 | - * However, some client code may be ok |
|
459 | - * with construction finalize being called later |
|
460 | - * (realizing that the subsections' html names |
|
461 | - * might not be set yet, etc.) |
|
462 | - * @return EE_Form_Section_Base |
|
463 | - * @throws EE_Error |
|
464 | - */ |
|
465 | - public function get_subsection($name, $require_construction_to_be_finalized = true) |
|
466 | - { |
|
467 | - if ($require_construction_to_be_finalized) { |
|
468 | - $this->ensure_construct_finalized_called(); |
|
469 | - } |
|
470 | - return $this->subsection_exists($name) ? $this->_subsections[ $name ] : null; |
|
471 | - } |
|
472 | - |
|
473 | - |
|
474 | - /** |
|
475 | - * Gets all the validatable subsections of this form section |
|
476 | - * |
|
477 | - * @return EE_Form_Section_Validatable[] |
|
478 | - * @throws EE_Error |
|
479 | - */ |
|
480 | - public function get_validatable_subsections() |
|
481 | - { |
|
482 | - $validatable_subsections = array(); |
|
483 | - foreach ($this->subsections() as $name => $obj) { |
|
484 | - if ($obj instanceof EE_Form_Section_Validatable) { |
|
485 | - $validatable_subsections[ $name ] = $obj; |
|
486 | - } |
|
487 | - } |
|
488 | - return $validatable_subsections; |
|
489 | - } |
|
490 | - |
|
491 | - |
|
492 | - /** |
|
493 | - * Gets an input by the given name. If not found, or if its not an EE_FOrm_Input_Base child, |
|
494 | - * throw an EE_Error. |
|
495 | - * |
|
496 | - * @param string $name |
|
497 | - * @param boolean $require_construction_to_be_finalized most client code should |
|
498 | - * leave this as TRUE so that the inputs will be properly |
|
499 | - * configured. However, some client code may be ok with |
|
500 | - * construction finalize being called later |
|
501 | - * (realizing that the subsections' html names might not be |
|
502 | - * set yet, etc.) |
|
503 | - * @return EE_Form_Input_Base |
|
504 | - * @throws EE_Error |
|
505 | - */ |
|
506 | - public function get_input($name, $require_construction_to_be_finalized = true) |
|
507 | - { |
|
508 | - $subsection = $this->get_subsection( |
|
509 | - $name, |
|
510 | - $require_construction_to_be_finalized |
|
511 | - ); |
|
512 | - if (! $subsection instanceof EE_Form_Input_Base) { |
|
513 | - throw new EE_Error( |
|
514 | - sprintf( |
|
515 | - esc_html__( |
|
516 | - "Subsection '%s' is not an instanceof EE_Form_Input_Base on form '%s'. It is a '%s'", |
|
517 | - 'event_espresso' |
|
518 | - ), |
|
519 | - $name, |
|
520 | - get_class($this), |
|
521 | - $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso') |
|
522 | - ) |
|
523 | - ); |
|
524 | - } |
|
525 | - return $subsection; |
|
526 | - } |
|
527 | - |
|
528 | - |
|
529 | - /** |
|
530 | - * Like get_input(), gets the proper subsection of the form given the name, |
|
531 | - * otherwise throws an EE_Error |
|
532 | - * |
|
533 | - * @param string $name |
|
534 | - * @param boolean $require_construction_to_be_finalized most client code should |
|
535 | - * leave this as TRUE so that the inputs will be properly |
|
536 | - * configured. However, some client code may be ok with |
|
537 | - * construction finalize being called later |
|
538 | - * (realizing that the subsections' html names might not be |
|
539 | - * set yet, etc.) |
|
540 | - * @return EE_Form_Section_Proper |
|
541 | - * @throws EE_Error |
|
542 | - */ |
|
543 | - public function get_proper_subsection($name, $require_construction_to_be_finalized = true) |
|
544 | - { |
|
545 | - $subsection = $this->get_subsection( |
|
546 | - $name, |
|
547 | - $require_construction_to_be_finalized |
|
548 | - ); |
|
549 | - if (! $subsection instanceof EE_Form_Section_Proper) { |
|
550 | - throw new EE_Error( |
|
551 | - sprintf( |
|
552 | - esc_html__( |
|
553 | - "Subsection '%'s is not an instanceof EE_Form_Section_Proper on form '%s'", |
|
554 | - 'event_espresso' |
|
555 | - ), |
|
556 | - $name, |
|
557 | - get_class($this) |
|
558 | - ) |
|
559 | - ); |
|
560 | - } |
|
561 | - return $subsection; |
|
562 | - } |
|
563 | - |
|
564 | - |
|
565 | - /** |
|
566 | - * Gets the value of the specified input. Should be called after receive_form_submission() |
|
567 | - * or populate_defaults() on the form, where the normalized value on the input is set. |
|
568 | - * |
|
569 | - * @param string $name |
|
570 | - * @return mixed depending on the input's type and its normalization strategy |
|
571 | - * @throws EE_Error |
|
572 | - */ |
|
573 | - public function get_input_value($name) |
|
574 | - { |
|
575 | - $input = $this->get_input($name); |
|
576 | - return $input->normalized_value(); |
|
577 | - } |
|
578 | - |
|
579 | - |
|
580 | - /** |
|
581 | - * Checks if this form section itself is valid, and then checks its subsections |
|
582 | - * |
|
583 | - * @throws EE_Error |
|
584 | - * @return boolean |
|
585 | - */ |
|
586 | - public function is_valid() |
|
587 | - { |
|
588 | - if($this->is_valid === null) { |
|
589 | - if (! $this->has_received_submission()) { |
|
590 | - throw new EE_Error( |
|
591 | - sprintf( |
|
592 | - esc_html__( |
|
593 | - 'You cannot check if a form is valid before receiving the form submission using receive_form_submission', |
|
594 | - 'event_espresso' |
|
595 | - ) |
|
596 | - ) |
|
597 | - ); |
|
598 | - } |
|
599 | - if (! parent::is_valid()) { |
|
600 | - $this->is_valid = false; |
|
601 | - } else { |
|
602 | - // ok so no general errors to this entire form section. |
|
603 | - // so let's check the subsections, but only set errors if that hasn't been done yet |
|
604 | - $this->is_valid = true; |
|
605 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
606 | - if (! $subsection->is_valid()) { |
|
607 | - $this->is_valid = false; |
|
608 | - } |
|
609 | - } |
|
610 | - } |
|
611 | - } |
|
612 | - return $this->is_valid; |
|
613 | - } |
|
614 | - |
|
615 | - |
|
616 | - /** |
|
617 | - * gets the default name of this form section if none is specified |
|
618 | - * |
|
619 | - * @return void |
|
620 | - */ |
|
621 | - protected function _set_default_name_if_empty() |
|
622 | - { |
|
623 | - if (! $this->_name) { |
|
624 | - $classname = get_class($this); |
|
625 | - $default_name = str_replace('EE_', '', $classname); |
|
626 | - $this->_name = $default_name; |
|
627 | - } |
|
628 | - } |
|
629 | - |
|
630 | - |
|
631 | - /** |
|
632 | - * Returns the HTML for the form, except for the form opening and closing tags |
|
633 | - * (as the form section doesn't know where you necessarily want to send the information to), |
|
634 | - * and except for a submit button. Enqueues JS and CSS; if called early enough we will |
|
635 | - * try to enqueue them in the header, otherwise they'll be enqueued in the footer. |
|
636 | - * Not doing_it_wrong because theoretically this CAN be used properly, |
|
637 | - * provided its used during "wp_enqueue_scripts", or it doesn't need to enqueue |
|
638 | - * any CSS. |
|
639 | - * |
|
640 | - * @throws InvalidArgumentException |
|
641 | - * @throws InvalidInterfaceException |
|
642 | - * @throws InvalidDataTypeException |
|
643 | - * @throws EE_Error |
|
644 | - */ |
|
645 | - public function get_html_and_js() |
|
646 | - { |
|
647 | - $this->enqueue_js(); |
|
648 | - return $this->get_html(); |
|
649 | - } |
|
650 | - |
|
651 | - |
|
652 | - /** |
|
653 | - * returns HTML for displaying this form section. recursively calls display_section() on all subsections |
|
654 | - * |
|
655 | - * @param bool $display_previously_submitted_data |
|
656 | - * @return string |
|
657 | - * @throws InvalidArgumentException |
|
658 | - * @throws InvalidInterfaceException |
|
659 | - * @throws InvalidDataTypeException |
|
660 | - * @throws EE_Error |
|
661 | - * @throws EE_Error |
|
662 | - * @throws EE_Error |
|
663 | - */ |
|
664 | - public function get_html($display_previously_submitted_data = true) |
|
665 | - { |
|
666 | - $this->ensure_construct_finalized_called(); |
|
667 | - if ($display_previously_submitted_data) { |
|
668 | - $this->populate_from_session(); |
|
669 | - } |
|
670 | - return $this->_form_html_filter |
|
671 | - ? $this->_form_html_filter->filterHtml($this->_layout_strategy->layout_form(), $this) |
|
672 | - : $this->_layout_strategy->layout_form(); |
|
673 | - } |
|
674 | - |
|
675 | - |
|
676 | - /** |
|
677 | - * enqueues JS and CSS for the form. |
|
678 | - * It is preferred to call this before wp_enqueue_scripts so the |
|
679 | - * scripts and styles can be put in the header, but if called later |
|
680 | - * they will be put in the footer (which is OK for JS, but in HTML4 CSS should |
|
681 | - * only be in the header; but in HTML5 its ok in the body. |
|
682 | - * See http://stackoverflow.com/questions/4957446/load-external-css-file-in-body-tag. |
|
683 | - * So if your form enqueues CSS, it's preferred to call this before wp_enqueue_scripts.) |
|
684 | - * |
|
685 | - * @return void |
|
686 | - * @throws EE_Error |
|
687 | - */ |
|
688 | - public function enqueue_js() |
|
689 | - { |
|
690 | - $this->_enqueue_and_localize_form_js(); |
|
691 | - foreach ($this->subsections() as $subsection) { |
|
692 | - $subsection->enqueue_js(); |
|
693 | - } |
|
694 | - } |
|
695 | - |
|
696 | - |
|
697 | - /** |
|
698 | - * adds a filter so that jquery validate gets enqueued in EE_System::wp_enqueue_scripts(). |
|
699 | - * This must be done BEFORE wp_enqueue_scripts() gets called, which is on |
|
700 | - * the wp_enqueue_scripts hook. |
|
701 | - * However, registering the form js and localizing it can happen when we |
|
702 | - * actually output the form (which is preferred, seeing how teh form's fields |
|
703 | - * could change until it's actually outputted) |
|
704 | - * |
|
705 | - * @param boolean $init_form_validation_automatically whether or not we want the form validation |
|
706 | - * to be triggered automatically or not |
|
707 | - * @return void |
|
708 | - */ |
|
709 | - public static function wp_enqueue_scripts($init_form_validation_automatically = true) |
|
710 | - { |
|
711 | - wp_register_script( |
|
712 | - 'ee_form_section_validation', |
|
713 | - EE_GLOBAL_ASSETS_URL . 'scripts' . DS . 'form_section_validation.js', |
|
714 | - array('jquery-validate', 'jquery-ui-datepicker', 'jquery-validate-extra-methods'), |
|
715 | - EVENT_ESPRESSO_VERSION, |
|
716 | - true |
|
717 | - ); |
|
718 | - wp_localize_script( |
|
719 | - 'ee_form_section_validation', |
|
720 | - 'ee_form_section_validation_init', |
|
721 | - array('init' => $init_form_validation_automatically ? '1' : '0') |
|
722 | - ); |
|
723 | - } |
|
724 | - |
|
725 | - |
|
726 | - /** |
|
727 | - * gets the variables used by form_section_validation.js. |
|
728 | - * This needs to be called AFTER we've called $this->_enqueue_jquery_validate_script, |
|
729 | - * but before the wordpress hook wp_loaded |
|
730 | - * |
|
731 | - * @throws EE_Error |
|
732 | - */ |
|
733 | - public function _enqueue_and_localize_form_js() |
|
734 | - { |
|
735 | - $this->ensure_construct_finalized_called(); |
|
736 | - //actually, we don't want to localize just yet. There may be other forms on the page. |
|
737 | - //so we need to add our form section data to a static variable accessible by all form sections |
|
738 | - //and localize it just before the footer |
|
739 | - $this->localize_validation_rules(); |
|
740 | - add_action('wp_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'), 2); |
|
741 | - add_action('admin_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms')); |
|
742 | - } |
|
743 | - |
|
744 | - |
|
745 | - /** |
|
746 | - * add our form section data to a static variable accessible by all form sections |
|
747 | - * |
|
748 | - * @param bool $return_for_subsection |
|
749 | - * @return void |
|
750 | - * @throws EE_Error |
|
751 | - */ |
|
752 | - public function localize_validation_rules($return_for_subsection = false) |
|
753 | - { |
|
754 | - // we only want to localize vars ONCE for the entire form, |
|
755 | - // so if the form section doesn't have a parent, then it must be the top dog |
|
756 | - if ($return_for_subsection || ! $this->parent_section()) { |
|
757 | - EE_Form_Section_Proper::$_js_localization['form_data'][ $this->html_id() ] = array( |
|
758 | - 'form_section_id' => $this->html_id(true), |
|
759 | - 'validation_rules' => $this->get_jquery_validation_rules(), |
|
760 | - 'other_data' => $this->get_other_js_data(), |
|
761 | - 'errors' => $this->subsection_validation_errors_by_html_name(), |
|
762 | - ); |
|
763 | - EE_Form_Section_Proper::$_scripts_localized = true; |
|
764 | - } |
|
765 | - } |
|
766 | - |
|
767 | - |
|
768 | - /** |
|
769 | - * Gets an array of extra data that will be useful for client-side javascript. |
|
770 | - * This is primarily data added by inputs and forms in addition to any |
|
771 | - * scripts they might enqueue |
|
772 | - * |
|
773 | - * @param array $form_other_js_data |
|
774 | - * @return array |
|
775 | - * @throws EE_Error |
|
776 | - */ |
|
777 | - public function get_other_js_data($form_other_js_data = array()) |
|
778 | - { |
|
779 | - foreach ($this->subsections() as $subsection) { |
|
780 | - $form_other_js_data = $subsection->get_other_js_data($form_other_js_data); |
|
781 | - } |
|
782 | - return $form_other_js_data; |
|
783 | - } |
|
784 | - |
|
785 | - |
|
786 | - /** |
|
787 | - * Gets a flat array of inputs for this form section and its subsections. |
|
788 | - * Keys are their form names, and values are the inputs themselves |
|
789 | - * |
|
790 | - * @return EE_Form_Input_Base |
|
791 | - * @throws EE_Error |
|
792 | - */ |
|
793 | - public function inputs_in_subsections() |
|
794 | - { |
|
795 | - $inputs = array(); |
|
796 | - foreach ($this->subsections() as $subsection) { |
|
797 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
798 | - $inputs[ $subsection->html_name() ] = $subsection; |
|
799 | - } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
800 | - $inputs += $subsection->inputs_in_subsections(); |
|
801 | - } |
|
802 | - } |
|
803 | - return $inputs; |
|
804 | - } |
|
805 | - |
|
806 | - |
|
807 | - /** |
|
808 | - * Gets a flat array of all the validation errors. |
|
809 | - * Keys are html names (because those should be unique) |
|
810 | - * and values are a string of all their validation errors |
|
811 | - * |
|
812 | - * @return string[] |
|
813 | - * @throws EE_Error |
|
814 | - */ |
|
815 | - public function subsection_validation_errors_by_html_name() |
|
816 | - { |
|
817 | - $inputs = $this->inputs(); |
|
818 | - $errors = array(); |
|
819 | - foreach ($inputs as $form_input) { |
|
820 | - if ($form_input instanceof EE_Form_Input_Base && $form_input->get_validation_errors()) { |
|
821 | - $errors[ $form_input->html_name() ] = $form_input->get_validation_error_string(); |
|
822 | - } |
|
823 | - } |
|
824 | - return $errors; |
|
825 | - } |
|
826 | - |
|
827 | - |
|
828 | - /** |
|
829 | - * passes all the form data required by the JS to the JS, and enqueues the few required JS files. |
|
830 | - * Should be setup by each form during the _enqueues_and_localize_form_js |
|
831 | - * |
|
832 | - * @throws InvalidArgumentException |
|
833 | - * @throws InvalidInterfaceException |
|
834 | - * @throws InvalidDataTypeException |
|
835 | - */ |
|
836 | - public static function localize_script_for_all_forms() |
|
837 | - { |
|
838 | - //allow inputs and stuff to hook in their JS and stuff here |
|
839 | - do_action('AHEE__EE_Form_Section_Proper__localize_script_for_all_forms__begin'); |
|
840 | - EE_Form_Section_Proper::$_js_localization['localized_error_messages'] = EE_Form_Section_Proper::_get_localized_error_messages(); |
|
841 | - $email_validation_level = isset(EE_Registry::instance()->CFG->registration->email_validation_level) |
|
842 | - ? EE_Registry::instance()->CFG->registration->email_validation_level |
|
843 | - : 'wp_default'; |
|
844 | - EE_Form_Section_Proper::$_js_localization['email_validation_level'] = $email_validation_level; |
|
845 | - wp_enqueue_script('ee_form_section_validation'); |
|
846 | - wp_localize_script( |
|
847 | - 'ee_form_section_validation', |
|
848 | - 'ee_form_section_vars', |
|
849 | - EE_Form_Section_Proper::$_js_localization |
|
850 | - ); |
|
851 | - } |
|
852 | - |
|
853 | - |
|
854 | - /** |
|
855 | - * ensure_scripts_localized |
|
856 | - * |
|
857 | - * @throws EE_Error |
|
858 | - */ |
|
859 | - public function ensure_scripts_localized() |
|
860 | - { |
|
861 | - if (! EE_Form_Section_Proper::$_scripts_localized) { |
|
862 | - $this->_enqueue_and_localize_form_js(); |
|
863 | - } |
|
864 | - } |
|
865 | - |
|
866 | - |
|
867 | - /** |
|
868 | - * Gets the hard-coded validation error messages to be used in the JS. The convention |
|
869 | - * is that the key here should be the same as the custom validation rule put in the JS file |
|
870 | - * |
|
871 | - * @return array keys are custom validation rules, and values are internationalized strings |
|
872 | - */ |
|
873 | - private static function _get_localized_error_messages() |
|
874 | - { |
|
875 | - return array( |
|
876 | - 'validUrl' => esc_html__('This is not a valid absolute URL. Eg, http://domain.com/monkey.jpg', 'event_espresso'), |
|
877 | - 'regex' => esc_html__('Please check your input', 'event_espresso'), |
|
878 | - ); |
|
879 | - } |
|
880 | - |
|
881 | - |
|
882 | - /** |
|
883 | - * @return array |
|
884 | - */ |
|
885 | - public static function js_localization() |
|
886 | - { |
|
887 | - return self::$_js_localization; |
|
888 | - } |
|
889 | - |
|
890 | - |
|
891 | - /** |
|
892 | - * @return void |
|
893 | - */ |
|
894 | - public static function reset_js_localization() |
|
895 | - { |
|
896 | - self::$_js_localization = array(); |
|
897 | - } |
|
898 | - |
|
899 | - |
|
900 | - /** |
|
901 | - * Gets the JS to put inside the jquery validation rules for subsection of this form section. |
|
902 | - * See parent function for more... |
|
903 | - * |
|
904 | - * @return array |
|
905 | - * @throws EE_Error |
|
906 | - */ |
|
907 | - public function get_jquery_validation_rules() |
|
908 | - { |
|
909 | - $jquery_validation_rules = array(); |
|
910 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
911 | - $jquery_validation_rules = array_merge( |
|
912 | - $jquery_validation_rules, |
|
913 | - $subsection->get_jquery_validation_rules() |
|
914 | - ); |
|
915 | - } |
|
916 | - return $jquery_validation_rules; |
|
917 | - } |
|
918 | - |
|
919 | - |
|
920 | - /** |
|
921 | - * Sanitizes all the data and sets the sanitized value of each field |
|
922 | - * |
|
923 | - * @param array $req_data like $_POST |
|
924 | - * @return void |
|
925 | - * @throws EE_Error |
|
926 | - */ |
|
927 | - protected function _normalize($req_data) |
|
928 | - { |
|
929 | - $this->_received_submission = true; |
|
930 | - $this->_validation_errors = array(); |
|
931 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
932 | - try { |
|
933 | - $subsection->_normalize($req_data); |
|
934 | - } catch (EE_Validation_Error $e) { |
|
935 | - $subsection->add_validation_error($e); |
|
936 | - } |
|
937 | - } |
|
938 | - } |
|
939 | - |
|
940 | - |
|
941 | - /** |
|
942 | - * Performs validation on this form section and its subsections. |
|
943 | - * For each subsection, |
|
944 | - * calls _validate_{subsection_name} on THIS form (if the function exists) |
|
945 | - * and passes it the subsection, then calls _validate on that subsection. |
|
946 | - * If you need to perform validation on the form as a whole (considering multiple) |
|
947 | - * you would be best to override this _validate method, |
|
948 | - * calling parent::_validate() first. |
|
949 | - * |
|
950 | - * @throws EE_Error |
|
951 | - */ |
|
952 | - protected function _validate() |
|
953 | - { |
|
954 | - //reset the cache of whether this form is valid or not- we're re-validating it now |
|
955 | - $this->is_valid = null; |
|
956 | - foreach ($this->get_validatable_subsections() as $subsection_name => $subsection) { |
|
957 | - if (method_exists($this, '_validate_' . $subsection_name)) { |
|
958 | - call_user_func_array(array($this, '_validate_' . $subsection_name), array($subsection)); |
|
959 | - } |
|
960 | - $subsection->_validate(); |
|
961 | - } |
|
962 | - } |
|
963 | - |
|
964 | - |
|
965 | - /** |
|
966 | - * Gets all the validated inputs for the form section |
|
967 | - * |
|
968 | - * @return array |
|
969 | - * @throws EE_Error |
|
970 | - */ |
|
971 | - public function valid_data() |
|
972 | - { |
|
973 | - $inputs = array(); |
|
974 | - foreach ($this->subsections() as $subsection_name => $subsection) { |
|
975 | - if ($subsection instanceof EE_Form_Section_Proper) { |
|
976 | - $inputs[ $subsection_name ] = $subsection->valid_data(); |
|
977 | - } elseif ($subsection instanceof EE_Form_Input_Base) { |
|
978 | - $inputs[ $subsection_name ] = $subsection->normalized_value(); |
|
979 | - } |
|
980 | - } |
|
981 | - return $inputs; |
|
982 | - } |
|
983 | - |
|
984 | - |
|
985 | - /** |
|
986 | - * Gets all the inputs on this form section |
|
987 | - * |
|
988 | - * @return EE_Form_Input_Base[] |
|
989 | - * @throws EE_Error |
|
990 | - */ |
|
991 | - public function inputs() |
|
992 | - { |
|
993 | - $inputs = array(); |
|
994 | - foreach ($this->subsections() as $subsection_name => $subsection) { |
|
995 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
996 | - $inputs[ $subsection_name ] = $subsection; |
|
997 | - } |
|
998 | - } |
|
999 | - return $inputs; |
|
1000 | - } |
|
1001 | - |
|
1002 | - |
|
1003 | - /** |
|
1004 | - * Gets all the subsections which are a proper form |
|
1005 | - * |
|
1006 | - * @return EE_Form_Section_Proper[] |
|
1007 | - * @throws EE_Error |
|
1008 | - */ |
|
1009 | - public function subforms() |
|
1010 | - { |
|
1011 | - $form_sections = array(); |
|
1012 | - foreach ($this->subsections() as $name => $obj) { |
|
1013 | - if ($obj instanceof EE_Form_Section_Proper) { |
|
1014 | - $form_sections[ $name ] = $obj; |
|
1015 | - } |
|
1016 | - } |
|
1017 | - return $form_sections; |
|
1018 | - } |
|
1019 | - |
|
1020 | - |
|
1021 | - /** |
|
1022 | - * Gets all the subsections (inputs, proper subsections, or html-only sections). |
|
1023 | - * Consider using inputs() or subforms() |
|
1024 | - * if you only want form inputs or proper form sections. |
|
1025 | - * |
|
1026 | - * @param boolean $require_construction_to_be_finalized most client code should |
|
1027 | - * leave this as TRUE so that the inputs will be properly |
|
1028 | - * configured. However, some client code may be ok with |
|
1029 | - * construction finalize being called later |
|
1030 | - * (realizing that the subsections' html names might not be |
|
1031 | - * set yet, etc.) |
|
1032 | - * @return EE_Form_Section_Proper[] |
|
1033 | - * @throws EE_Error |
|
1034 | - */ |
|
1035 | - public function subsections($require_construction_to_be_finalized = true) |
|
1036 | - { |
|
1037 | - if ($require_construction_to_be_finalized) { |
|
1038 | - $this->ensure_construct_finalized_called(); |
|
1039 | - } |
|
1040 | - return $this->_subsections; |
|
1041 | - } |
|
1042 | - |
|
1043 | - |
|
1044 | - /** |
|
1045 | - * Returns whether this form has any subforms or inputs |
|
1046 | - * @return bool |
|
1047 | - */ |
|
1048 | - public function hasSubsections() |
|
1049 | - { |
|
1050 | - return ! empty($this->_subsections); |
|
1051 | - } |
|
1052 | - |
|
1053 | - |
|
1054 | - /** |
|
1055 | - * Returns a simple array where keys are input names, and values are their normalized |
|
1056 | - * values. (Similar to calling get_input_value on inputs) |
|
1057 | - * |
|
1058 | - * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
1059 | - * or just this forms' direct children inputs |
|
1060 | - * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
1061 | - * or allow multidimensional array |
|
1062 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
1063 | - * with array keys being input names |
|
1064 | - * (regardless of whether they are from a subsection or not), |
|
1065 | - * and if $flatten is FALSE it can be a multidimensional array |
|
1066 | - * where keys are always subsection names and values are either |
|
1067 | - * the input's normalized value, or an array like the top-level array |
|
1068 | - * @throws EE_Error |
|
1069 | - */ |
|
1070 | - public function input_values($include_subform_inputs = false, $flatten = false) |
|
1071 | - { |
|
1072 | - return $this->_input_values(false, $include_subform_inputs, $flatten); |
|
1073 | - } |
|
1074 | - |
|
1075 | - |
|
1076 | - /** |
|
1077 | - * Similar to EE_Form_Section_Proper::input_values(), except this returns the 'display_value' |
|
1078 | - * of each input. On some inputs (especially radio boxes or checkboxes), the value stored |
|
1079 | - * is not necessarily the value we want to display to users. This creates an array |
|
1080 | - * where keys are the input names, and values are their display values |
|
1081 | - * |
|
1082 | - * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
1083 | - * or just this forms' direct children inputs |
|
1084 | - * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
1085 | - * or allow multidimensional array |
|
1086 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
1087 | - * with array keys being input names |
|
1088 | - * (regardless of whether they are from a subsection or not), |
|
1089 | - * and if $flatten is FALSE it can be a multidimensional array |
|
1090 | - * where keys are always subsection names and values are either |
|
1091 | - * the input's normalized value, or an array like the top-level array |
|
1092 | - * @throws EE_Error |
|
1093 | - */ |
|
1094 | - public function input_pretty_values($include_subform_inputs = false, $flatten = false) |
|
1095 | - { |
|
1096 | - return $this->_input_values(true, $include_subform_inputs, $flatten); |
|
1097 | - } |
|
1098 | - |
|
1099 | - |
|
1100 | - /** |
|
1101 | - * Gets the input values from the form |
|
1102 | - * |
|
1103 | - * @param boolean $pretty Whether to retrieve the pretty value, |
|
1104 | - * or just the normalized value |
|
1105 | - * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
1106 | - * or just this forms' direct children inputs |
|
1107 | - * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
1108 | - * or allow multidimensional array |
|
1109 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array with array keys being |
|
1110 | - * input names (regardless of whether they are from a subsection or not), |
|
1111 | - * and if $flatten is FALSE it can be a multidimensional array |
|
1112 | - * where keys are always subsection names and values are either |
|
1113 | - * the input's normalized value, or an array like the top-level array |
|
1114 | - * @throws EE_Error |
|
1115 | - */ |
|
1116 | - public function _input_values($pretty = false, $include_subform_inputs = false, $flatten = false) |
|
1117 | - { |
|
1118 | - $input_values = array(); |
|
1119 | - foreach ($this->subsections() as $subsection_name => $subsection) { |
|
1120 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
1121 | - $input_values[ $subsection_name ] = $pretty |
|
1122 | - ? $subsection->pretty_value() |
|
1123 | - : $subsection->normalized_value(); |
|
1124 | - } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subform_inputs) { |
|
1125 | - $subform_input_values = $subsection->_input_values( |
|
1126 | - $pretty, |
|
1127 | - $include_subform_inputs, |
|
1128 | - $flatten |
|
1129 | - ); |
|
1130 | - if ($flatten) { |
|
1131 | - $input_values = array_merge($input_values, $subform_input_values); |
|
1132 | - } else { |
|
1133 | - $input_values[ $subsection_name ] = $subform_input_values; |
|
1134 | - } |
|
1135 | - } |
|
1136 | - } |
|
1137 | - return $input_values; |
|
1138 | - } |
|
1139 | - |
|
1140 | - |
|
1141 | - /** |
|
1142 | - * Gets the originally submitted input values from the form |
|
1143 | - * |
|
1144 | - * @param boolean $include_subforms Whether to include inputs from subforms, |
|
1145 | - * or just this forms' direct children inputs |
|
1146 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
1147 | - * with array keys being input names |
|
1148 | - * (regardless of whether they are from a subsection or not), |
|
1149 | - * and if $flatten is FALSE it can be a multidimensional array |
|
1150 | - * where keys are always subsection names and values are either |
|
1151 | - * the input's normalized value, or an array like the top-level array |
|
1152 | - * @throws EE_Error |
|
1153 | - */ |
|
1154 | - public function submitted_values($include_subforms = false) |
|
1155 | - { |
|
1156 | - $submitted_values = array(); |
|
1157 | - foreach ($this->subsections() as $subsection) { |
|
1158 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
1159 | - // is this input part of an array of inputs? |
|
1160 | - if (strpos($subsection->html_name(), '[') !== false) { |
|
1161 | - $full_input_name = EEH_Array::convert_array_values_to_keys( |
|
1162 | - explode( |
|
1163 | - '[', |
|
1164 | - str_replace(']', '', $subsection->html_name()) |
|
1165 | - ), |
|
1166 | - $subsection->raw_value() |
|
1167 | - ); |
|
1168 | - $submitted_values = array_replace_recursive($submitted_values, $full_input_name); |
|
1169 | - } else { |
|
1170 | - $submitted_values[ $subsection->html_name() ] = $subsection->raw_value(); |
|
1171 | - } |
|
1172 | - } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subforms) { |
|
1173 | - $subform_input_values = $subsection->submitted_values($include_subforms); |
|
1174 | - $submitted_values = array_replace_recursive($submitted_values, $subform_input_values); |
|
1175 | - } |
|
1176 | - } |
|
1177 | - return $submitted_values; |
|
1178 | - } |
|
1179 | - |
|
1180 | - |
|
1181 | - /** |
|
1182 | - * Indicates whether or not this form has received a submission yet |
|
1183 | - * (ie, had receive_form_submission called on it yet) |
|
1184 | - * |
|
1185 | - * @return boolean |
|
1186 | - * @throws EE_Error |
|
1187 | - */ |
|
1188 | - public function has_received_submission() |
|
1189 | - { |
|
1190 | - $this->ensure_construct_finalized_called(); |
|
1191 | - return $this->_received_submission; |
|
1192 | - } |
|
1193 | - |
|
1194 | - |
|
1195 | - /** |
|
1196 | - * Equivalent to passing 'exclude' in the constructor's options array. |
|
1197 | - * Removes the listed inputs from the form |
|
1198 | - * |
|
1199 | - * @param array $inputs_to_exclude values are the input names |
|
1200 | - * @return void |
|
1201 | - */ |
|
1202 | - public function exclude(array $inputs_to_exclude = array()) |
|
1203 | - { |
|
1204 | - foreach ($inputs_to_exclude as $input_to_exclude_name) { |
|
1205 | - unset($this->_subsections[ $input_to_exclude_name ]); |
|
1206 | - } |
|
1207 | - } |
|
1208 | - |
|
1209 | - |
|
1210 | - /** |
|
1211 | - * Changes these inputs' display strategy to be EE_Hidden_Display_Strategy. |
|
1212 | - * @param array $inputs_to_hide |
|
1213 | - * @throws EE_Error |
|
1214 | - */ |
|
1215 | - public function hide(array $inputs_to_hide = array()) |
|
1216 | - { |
|
1217 | - foreach ($inputs_to_hide as $input_to_hide) { |
|
1218 | - $input = $this->get_input($input_to_hide); |
|
1219 | - $input->set_display_strategy(new EE_Hidden_Display_Strategy()); |
|
1220 | - } |
|
1221 | - } |
|
1222 | - |
|
1223 | - |
|
1224 | - /** |
|
1225 | - * add_subsections |
|
1226 | - * Adds the listed subsections to the form section. |
|
1227 | - * If $subsection_name_to_target is provided, |
|
1228 | - * then new subsections are added before or after that subsection, |
|
1229 | - * otherwise to the start or end of the entire subsections array. |
|
1230 | - * |
|
1231 | - * @param EE_Form_Section_Base[] $new_subsections array of new form subsections |
|
1232 | - * where keys are their names |
|
1233 | - * @param string $subsection_name_to_target an existing for section that $new_subsections |
|
1234 | - * should be added before or after |
|
1235 | - * IF $subsection_name_to_target is null, |
|
1236 | - * then $new_subsections will be added to |
|
1237 | - * the beginning or end of the entire subsections array |
|
1238 | - * @param boolean $add_before whether to add $new_subsections, before or after |
|
1239 | - * $subsection_name_to_target, |
|
1240 | - * or if $subsection_name_to_target is null, |
|
1241 | - * before or after entire subsections array |
|
1242 | - * @return void |
|
1243 | - * @throws EE_Error |
|
1244 | - */ |
|
1245 | - public function add_subsections($new_subsections, $subsection_name_to_target = null, $add_before = true) |
|
1246 | - { |
|
1247 | - foreach ($new_subsections as $subsection_name => $subsection) { |
|
1248 | - if (! $subsection instanceof EE_Form_Section_Base) { |
|
1249 | - EE_Error::add_error( |
|
1250 | - sprintf( |
|
1251 | - esc_html__( |
|
1252 | - "Trying to add a %s as a subsection (it was named '%s') to the form section '%s'. It was removed.", |
|
1253 | - 'event_espresso' |
|
1254 | - ), |
|
1255 | - get_class($subsection), |
|
1256 | - $subsection_name, |
|
1257 | - $this->name() |
|
1258 | - ) |
|
1259 | - ); |
|
1260 | - unset($new_subsections[ $subsection_name ]); |
|
1261 | - } |
|
1262 | - } |
|
1263 | - $this->_subsections = EEH_Array::insert_into_array( |
|
1264 | - $this->_subsections, |
|
1265 | - $new_subsections, |
|
1266 | - $subsection_name_to_target, |
|
1267 | - $add_before |
|
1268 | - ); |
|
1269 | - if ($this->_construction_finalized) { |
|
1270 | - foreach ($this->_subsections as $name => $subsection) { |
|
1271 | - $subsection->_construct_finalize($this, $name); |
|
1272 | - } |
|
1273 | - } |
|
1274 | - } |
|
1275 | - |
|
1276 | - |
|
1277 | - /** |
|
1278 | - * Just gets all validatable subsections to clean their sensitive data |
|
1279 | - * |
|
1280 | - * @throws EE_Error |
|
1281 | - */ |
|
1282 | - public function clean_sensitive_data() |
|
1283 | - { |
|
1284 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
1285 | - $subsection->clean_sensitive_data(); |
|
1286 | - } |
|
1287 | - } |
|
1288 | - |
|
1289 | - |
|
1290 | - /** |
|
1291 | - * Sets the submission error message (aka validation error message for this form section and all sub-sections) |
|
1292 | - * @param string $form_submission_error_message |
|
1293 | - * @param EE_Form_Section_Validatable $form_section unused |
|
1294 | - * @throws EE_Error |
|
1295 | - */ |
|
1296 | - public function set_submission_error_message( |
|
1297 | - $form_submission_error_message = '' |
|
1298 | - ) { |
|
1299 | - $this->_form_submission_error_message = ! empty($form_submission_error_message) |
|
1300 | - ? $form_submission_error_message |
|
1301 | - : $this->getAllValidationErrorsString(); |
|
1302 | - } |
|
1303 | - |
|
1304 | - |
|
1305 | - /** |
|
1306 | - * Returns the cached error message. A default value is set for this during _validate(), |
|
1307 | - * (called during receive_form_submission) but it can be explicitly set using |
|
1308 | - * set_submission_error_message |
|
1309 | - * |
|
1310 | - * @return string |
|
1311 | - */ |
|
1312 | - public function submission_error_message() |
|
1313 | - { |
|
1314 | - return $this->_form_submission_error_message; |
|
1315 | - } |
|
1316 | - |
|
1317 | - |
|
1318 | - /** |
|
1319 | - * Sets a message to display if the data submitted to the form was valid. |
|
1320 | - * @param string $form_submission_success_message |
|
1321 | - */ |
|
1322 | - public function set_submission_success_message($form_submission_success_message = '') |
|
1323 | - { |
|
1324 | - $this->_form_submission_success_message = ! empty($form_submission_success_message) |
|
1325 | - ? $form_submission_success_message |
|
1326 | - : esc_html__('Form submitted successfully', 'event_espresso'); |
|
1327 | - } |
|
1328 | - |
|
1329 | - |
|
1330 | - /** |
|
1331 | - * Gets a message appropriate for display when the form is correctly submitted |
|
1332 | - * @return string |
|
1333 | - */ |
|
1334 | - public function submission_success_message() |
|
1335 | - { |
|
1336 | - return $this->_form_submission_success_message; |
|
1337 | - } |
|
1338 | - |
|
1339 | - |
|
1340 | - /** |
|
1341 | - * Returns the prefix that should be used on child of this form section for |
|
1342 | - * their html names. If this form section itself has a parent, prepends ITS |
|
1343 | - * prefix onto this form section's prefix. Used primarily by |
|
1344 | - * EE_Form_Input_Base::_set_default_html_name_if_empty |
|
1345 | - * |
|
1346 | - * @return string |
|
1347 | - * @throws EE_Error |
|
1348 | - */ |
|
1349 | - public function html_name_prefix() |
|
1350 | - { |
|
1351 | - if ($this->parent_section() instanceof EE_Form_Section_Proper) { |
|
1352 | - return $this->parent_section()->html_name_prefix() . '[' . $this->name() . ']'; |
|
1353 | - } |
|
1354 | - return $this->name(); |
|
1355 | - } |
|
1356 | - |
|
1357 | - |
|
1358 | - /** |
|
1359 | - * Gets the name, but first checks _construct_finalize has been called. If not, |
|
1360 | - * calls it (assumes there is no parent and that we want the name to be whatever |
|
1361 | - * was set, which is probably nothing, or the classname) |
|
1362 | - * |
|
1363 | - * @return string |
|
1364 | - * @throws EE_Error |
|
1365 | - */ |
|
1366 | - public function name() |
|
1367 | - { |
|
1368 | - $this->ensure_construct_finalized_called(); |
|
1369 | - return parent::name(); |
|
1370 | - } |
|
1371 | - |
|
1372 | - |
|
1373 | - /** |
|
1374 | - * @return EE_Form_Section_Proper |
|
1375 | - * @throws EE_Error |
|
1376 | - */ |
|
1377 | - public function parent_section() |
|
1378 | - { |
|
1379 | - $this->ensure_construct_finalized_called(); |
|
1380 | - return parent::parent_section(); |
|
1381 | - } |
|
1382 | - |
|
1383 | - |
|
1384 | - /** |
|
1385 | - * make sure construction finalized was called, otherwise children might not be ready |
|
1386 | - * |
|
1387 | - * @return void |
|
1388 | - * @throws EE_Error |
|
1389 | - */ |
|
1390 | - public function ensure_construct_finalized_called() |
|
1391 | - { |
|
1392 | - if (! $this->_construction_finalized) { |
|
1393 | - $this->_construct_finalize($this->_parent_section, $this->_name); |
|
1394 | - } |
|
1395 | - } |
|
1396 | - |
|
1397 | - |
|
1398 | - /** |
|
1399 | - * Checks if any of this form section's inputs, or any of its children's inputs, |
|
1400 | - * are in teh form data. If any are found, returns true. Else false |
|
1401 | - * |
|
1402 | - * @param array $req_data |
|
1403 | - * @return boolean |
|
1404 | - * @throws EE_Error |
|
1405 | - */ |
|
1406 | - public function form_data_present_in($req_data = null) |
|
1407 | - { |
|
1408 | - $req_data = $this->getCachedRequest($req_data); |
|
1409 | - foreach ($this->subsections() as $subsection) { |
|
1410 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
1411 | - if ($subsection->form_data_present_in($req_data)) { |
|
1412 | - return true; |
|
1413 | - } |
|
1414 | - } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
1415 | - if ($subsection->form_data_present_in($req_data)) { |
|
1416 | - return true; |
|
1417 | - } |
|
1418 | - } |
|
1419 | - } |
|
1420 | - return false; |
|
1421 | - } |
|
1422 | - |
|
1423 | - |
|
1424 | - /** |
|
1425 | - * Gets validation errors for this form section and subsections |
|
1426 | - * Similar to EE_Form_Section_Validatable::get_validation_errors() except this |
|
1427 | - * gets the validation errors for ALL subsection |
|
1428 | - * |
|
1429 | - * @return EE_Validation_Error[] |
|
1430 | - * @throws EE_Error |
|
1431 | - */ |
|
1432 | - public function get_validation_errors_accumulated() |
|
1433 | - { |
|
1434 | - $validation_errors = $this->get_validation_errors(); |
|
1435 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
1436 | - if ($subsection instanceof EE_Form_Section_Proper) { |
|
1437 | - $validation_errors_on_this_subsection = $subsection->get_validation_errors_accumulated(); |
|
1438 | - } else { |
|
1439 | - $validation_errors_on_this_subsection = $subsection->get_validation_errors(); |
|
1440 | - } |
|
1441 | - if ($validation_errors_on_this_subsection) { |
|
1442 | - $validation_errors = array_merge($validation_errors, $validation_errors_on_this_subsection); |
|
1443 | - } |
|
1444 | - } |
|
1445 | - return $validation_errors; |
|
1446 | - } |
|
1447 | - |
|
1448 | - /** |
|
1449 | - * Fetch validation errors from children and grandchildren and puts them in a single string. |
|
1450 | - * This traverses the form section tree to generate this, but you probably want to instead use |
|
1451 | - * get_form_submission_error_message() which is usually this message cached (or a custom validation error message) |
|
1452 | - * |
|
1453 | - * @return string |
|
1454 | - * @since $VID:$ |
|
1455 | - */ |
|
1456 | - protected function getAllValidationErrorsString() |
|
1457 | - { |
|
1458 | - $submission_error_messages = array(); |
|
1459 | - // bad, bad, bad registrant |
|
1460 | - foreach ($this->get_validation_errors_accumulated() as $validation_error) { |
|
1461 | - if ($validation_error instanceof EE_Validation_Error) { |
|
1462 | - $form_section = $validation_error->get_form_section(); |
|
1463 | - if ($form_section instanceof EE_Form_Input_Base) { |
|
1464 | - $label = $validation_error->get_form_section()->html_label_text(); |
|
1465 | - } elseif($form_section instanceof EE_Form_Section_Validatable) { |
|
1466 | - $label = $validation_error->get_form_section()->name(); |
|
1467 | - } else { |
|
1468 | - $label = esc_html__('Unknown', 'event_espresso'); |
|
1469 | - } |
|
1470 | - $submission_error_messages[] = sprintf( |
|
1471 | - __('%s : %s', 'event_espresso'), |
|
1472 | - $label, |
|
1473 | - $validation_error->getMessage() |
|
1474 | - ); |
|
1475 | - } |
|
1476 | - } |
|
1477 | - return implode('<br', $submission_error_messages); |
|
1478 | - } |
|
1479 | - |
|
1480 | - |
|
1481 | - /** |
|
1482 | - * This isn't just the name of an input, it's a path pointing to an input. The |
|
1483 | - * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
1484 | - * dot-dot-slash (../) means to ascend into the parent section. |
|
1485 | - * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
1486 | - * which will be returned. |
|
1487 | - * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
1488 | - * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
1489 | - * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
1490 | - * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
1491 | - * Etc |
|
1492 | - * |
|
1493 | - * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
1494 | - * @return EE_Form_Section_Base |
|
1495 | - * @throws EE_Error |
|
1496 | - */ |
|
1497 | - public function find_section_from_path($form_section_path) |
|
1498 | - { |
|
1499 | - //check if we can find the input from purely going straight up the tree |
|
1500 | - $input = parent::find_section_from_path($form_section_path); |
|
1501 | - if ($input instanceof EE_Form_Section_Base) { |
|
1502 | - return $input; |
|
1503 | - } |
|
1504 | - $next_slash_pos = strpos($form_section_path, '/'); |
|
1505 | - if ($next_slash_pos !== false) { |
|
1506 | - $child_section_name = substr($form_section_path, 0, $next_slash_pos); |
|
1507 | - $subpath = substr($form_section_path, $next_slash_pos + 1); |
|
1508 | - } else { |
|
1509 | - $child_section_name = $form_section_path; |
|
1510 | - $subpath = ''; |
|
1511 | - } |
|
1512 | - $child_section = $this->get_subsection($child_section_name); |
|
1513 | - if ($child_section instanceof EE_Form_Section_Base) { |
|
1514 | - return $child_section->find_section_from_path($subpath); |
|
1515 | - } |
|
1516 | - return null; |
|
1517 | - } |
|
17 | + const SUBMITTED_FORM_DATA_SSN_KEY = 'submitted_form_data'; |
|
18 | + |
|
19 | + /** |
|
20 | + * Subsections |
|
21 | + * |
|
22 | + * @var EE_Form_Section_Validatable[] |
|
23 | + */ |
|
24 | + protected $_subsections = array(); |
|
25 | + |
|
26 | + /** |
|
27 | + * Strategy for laying out the form |
|
28 | + * |
|
29 | + * @var EE_Form_Section_Layout_Base |
|
30 | + */ |
|
31 | + protected $_layout_strategy; |
|
32 | + |
|
33 | + /** |
|
34 | + * Whether or not this form has received and validated a form submission yet |
|
35 | + * |
|
36 | + * @var boolean |
|
37 | + */ |
|
38 | + protected $_received_submission = false; |
|
39 | + |
|
40 | + /** |
|
41 | + * message displayed to users upon successful form submission |
|
42 | + * |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + protected $_form_submission_success_message = ''; |
|
46 | + |
|
47 | + /** |
|
48 | + * message displayed to users upon unsuccessful form submission |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + protected $_form_submission_error_message = ''; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var array like $_REQUEST |
|
56 | + */ |
|
57 | + protected $cached_request_data; |
|
58 | + |
|
59 | + /** |
|
60 | + * Stores whether this form (and its sub-sections) were found to be valid or not. |
|
61 | + * Starts off as null, but once the form is validated, it set to either true or false |
|
62 | + * @var boolean|null |
|
63 | + */ |
|
64 | + protected $is_valid; |
|
65 | + |
|
66 | + /** |
|
67 | + * Stores all the data that will localized for form validation |
|
68 | + * |
|
69 | + * @var array |
|
70 | + */ |
|
71 | + static protected $_js_localization = array(); |
|
72 | + |
|
73 | + /** |
|
74 | + * whether or not the form's localized validation JS vars have been set |
|
75 | + * |
|
76 | + * @type boolean |
|
77 | + */ |
|
78 | + static protected $_scripts_localized = false; |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * when constructing a proper form section, calls _construct_finalize on children |
|
83 | + * so that they know who their parent is, and what name they've been given. |
|
84 | + * |
|
85 | + * @param array[] $options_array { |
|
86 | + * @type $subsections EE_Form_Section_Validatable[] where keys are the section's name |
|
87 | + * @type $include string[] numerically-indexed where values are section names to be included, |
|
88 | + * and in that order. This is handy if you want |
|
89 | + * the subsections to be ordered differently than the default, and if you override |
|
90 | + * which fields are shown |
|
91 | + * @type $exclude string[] values are subsections to be excluded. This is handy if you want |
|
92 | + * to remove certain default subsections (note: if you specify BOTH 'include' AND |
|
93 | + * 'exclude', the inclusions will be applied first, and the exclusions will exclude |
|
94 | + * items from that list of inclusions) |
|
95 | + * @type $layout_strategy EE_Form_Section_Layout_Base strategy for laying out the form |
|
96 | + * } @see EE_Form_Section_Validatable::__construct() |
|
97 | + * @throws EE_Error |
|
98 | + */ |
|
99 | + public function __construct($options_array = array()) |
|
100 | + { |
|
101 | + $options_array = (array) apply_filters( |
|
102 | + 'FHEE__EE_Form_Section_Proper___construct__options_array', |
|
103 | + $options_array, |
|
104 | + $this |
|
105 | + ); |
|
106 | + //call parent first, as it may be setting the name |
|
107 | + parent::__construct($options_array); |
|
108 | + //if they've included subsections in the constructor, add them now |
|
109 | + if (isset($options_array['include'])) { |
|
110 | + //we are going to make sure we ONLY have those subsections to include |
|
111 | + //AND we are going to make sure they're in that specified order |
|
112 | + $reordered_subsections = array(); |
|
113 | + foreach ($options_array['include'] as $input_name) { |
|
114 | + if (isset($this->_subsections[ $input_name ])) { |
|
115 | + $reordered_subsections[ $input_name ] = $this->_subsections[ $input_name ]; |
|
116 | + } |
|
117 | + } |
|
118 | + $this->_subsections = $reordered_subsections; |
|
119 | + } |
|
120 | + if (isset($options_array['exclude'])) { |
|
121 | + $exclude = $options_array['exclude']; |
|
122 | + $this->_subsections = array_diff_key($this->_subsections, array_flip($exclude)); |
|
123 | + } |
|
124 | + if (isset($options_array['layout_strategy'])) { |
|
125 | + $this->_layout_strategy = $options_array['layout_strategy']; |
|
126 | + } |
|
127 | + if (! $this->_layout_strategy) { |
|
128 | + $this->_layout_strategy = is_admin() ? new EE_Admin_Two_Column_Layout() : new EE_Two_Column_Layout(); |
|
129 | + } |
|
130 | + $this->_layout_strategy->_construct_finalize($this); |
|
131 | + //ok so we are definitely going to want the forms JS, |
|
132 | + //so enqueue it or remember to enqueue it during wp_enqueue_scripts |
|
133 | + if (did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts')) { |
|
134 | + //ok so they've constructed this object after when they should have. |
|
135 | + //just enqueue the generic form scripts and initialize the form immediately in the JS |
|
136 | + EE_Form_Section_Proper::wp_enqueue_scripts(true); |
|
137 | + } else { |
|
138 | + add_action('wp_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
139 | + add_action('admin_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
140 | + } |
|
141 | + add_action('wp_footer', array($this, 'ensure_scripts_localized'), 1); |
|
142 | + /** |
|
143 | + * Gives other plugins a chance to hook in before construct finalize is called. |
|
144 | + * The form probably doesn't yet have a parent form section. |
|
145 | + * Since 4.9.32, when this action was introduced, this is the best place to add a subsection onto a form, |
|
146 | + * assuming you don't care what the form section's name, HTML ID, or HTML name etc are. |
|
147 | + * Also see AHEE__EE_Form_Section_Proper___construct_finalize__end |
|
148 | + * |
|
149 | + * @since 4.9.32 |
|
150 | + * @param EE_Form_Section_Proper $this before __construct is done, but all of its logic, |
|
151 | + * except maybe calling _construct_finalize has been done |
|
152 | + * @param array $options_array options passed into the constructor |
|
153 | + */ |
|
154 | + do_action( |
|
155 | + 'AHEE__EE_Form_Input_Base___construct__before_construct_finalize_called', |
|
156 | + $this, |
|
157 | + $options_array |
|
158 | + ); |
|
159 | + if (isset($options_array['name'])) { |
|
160 | + $this->_construct_finalize(null, $options_array['name']); |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + /** |
|
166 | + * Finishes construction given the parent form section and this form section's name |
|
167 | + * |
|
168 | + * @param EE_Form_Section_Proper $parent_form_section |
|
169 | + * @param string $name |
|
170 | + * @throws EE_Error |
|
171 | + */ |
|
172 | + public function _construct_finalize($parent_form_section, $name) |
|
173 | + { |
|
174 | + parent::_construct_finalize($parent_form_section, $name); |
|
175 | + $this->_set_default_name_if_empty(); |
|
176 | + $this->_set_default_html_id_if_empty(); |
|
177 | + foreach ($this->_subsections as $subsection_name => $subsection) { |
|
178 | + if ($subsection instanceof EE_Form_Section_Base) { |
|
179 | + $subsection->_construct_finalize($this, $subsection_name); |
|
180 | + } else { |
|
181 | + throw new EE_Error( |
|
182 | + sprintf( |
|
183 | + esc_html__( |
|
184 | + 'Subsection "%s" is not an instanceof EE_Form_Section_Base on form "%s". It is a "%s"', |
|
185 | + 'event_espresso' |
|
186 | + ), |
|
187 | + $subsection_name, |
|
188 | + get_class($this), |
|
189 | + $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso') |
|
190 | + ) |
|
191 | + ); |
|
192 | + } |
|
193 | + } |
|
194 | + /** |
|
195 | + * Action performed just after form has been given a name (and HTML ID etc) and is fully constructed. |
|
196 | + * If you have code that should modify the form and needs it and its subsections to have a name, HTML ID |
|
197 | + * (or other attributes derived from the name like the HTML label id, etc), this is where it should be done. |
|
198 | + * This might only happen just before displaying the form, or just before it receives form submission data. |
|
199 | + * If you need to modify the form or its subsections before _construct_finalize is called on it (and we've |
|
200 | + * ensured it has a name, HTML IDs, etc |
|
201 | + * |
|
202 | + * @param EE_Form_Section_Proper $this |
|
203 | + * @param EE_Form_Section_Proper|null $parent_form_section |
|
204 | + * @param string $name |
|
205 | + */ |
|
206 | + do_action( |
|
207 | + 'AHEE__EE_Form_Section_Proper___construct_finalize__end', |
|
208 | + $this, |
|
209 | + $parent_form_section, |
|
210 | + $name |
|
211 | + ); |
|
212 | + } |
|
213 | + |
|
214 | + |
|
215 | + /** |
|
216 | + * Gets the layout strategy for this form section |
|
217 | + * |
|
218 | + * @return EE_Form_Section_Layout_Base |
|
219 | + */ |
|
220 | + public function get_layout_strategy() |
|
221 | + { |
|
222 | + return $this->_layout_strategy; |
|
223 | + } |
|
224 | + |
|
225 | + |
|
226 | + /** |
|
227 | + * Gets the HTML for a single input for this form section according |
|
228 | + * to the layout strategy |
|
229 | + * |
|
230 | + * @param EE_Form_Input_Base $input |
|
231 | + * @return string |
|
232 | + */ |
|
233 | + public function get_html_for_input($input) |
|
234 | + { |
|
235 | + return $this->_layout_strategy->layout_input($input); |
|
236 | + } |
|
237 | + |
|
238 | + |
|
239 | + /** |
|
240 | + * was_submitted - checks if form inputs are present in request data |
|
241 | + * Basically an alias for form_data_present_in() (which is used by both |
|
242 | + * proper form sections and form inputs) |
|
243 | + * |
|
244 | + * @param null $form_data |
|
245 | + * @return boolean |
|
246 | + * @throws EE_Error |
|
247 | + */ |
|
248 | + public function was_submitted($form_data = null) |
|
249 | + { |
|
250 | + return $this->form_data_present_in($form_data); |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Gets the cached request data; but if there is none, or $req_data was set with |
|
255 | + * something different, refresh the cache, and then return it |
|
256 | + * @param null $req_data |
|
257 | + * @return array |
|
258 | + */ |
|
259 | + protected function getCachedRequest($req_data = null) |
|
260 | + { |
|
261 | + if ($this->cached_request_data === null |
|
262 | + || ( |
|
263 | + $req_data !== null && |
|
264 | + $req_data !== $this->cached_request_data |
|
265 | + ) |
|
266 | + ) { |
|
267 | + $req_data = apply_filters( |
|
268 | + 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
269 | + $req_data, |
|
270 | + $this |
|
271 | + ); |
|
272 | + if ($req_data === null) { |
|
273 | + $req_data = array_merge($_GET, $_POST); |
|
274 | + } |
|
275 | + $req_data = apply_filters( |
|
276 | + 'FHEE__EE_Form_Section_Proper__receive_form_submission__request_data', |
|
277 | + $req_data, |
|
278 | + $this |
|
279 | + ); |
|
280 | + $this->cached_request_data = (array)$req_data; |
|
281 | + } |
|
282 | + return $this->cached_request_data; |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + /** |
|
287 | + * After the form section is initially created, call this to sanitize the data in the submission |
|
288 | + * which relates to this form section, validate it, and set it as properties on the form. |
|
289 | + * |
|
290 | + * @param array|null $req_data should usually be $_POST (the default). |
|
291 | + * However, you CAN supply a different array. |
|
292 | + * Consider using set_defaults() instead however. |
|
293 | + * (If you rendered the form in the page using echo $form_x->get_html() |
|
294 | + * the inputs will have the correct name in the request data for this function |
|
295 | + * to find them and populate the form with them. |
|
296 | + * If you have a flat form (with only input subsections), |
|
297 | + * you can supply a flat array where keys |
|
298 | + * are the form input names and values are their values) |
|
299 | + * @param boolean $validate whether or not to perform validation on this data. Default is, |
|
300 | + * of course, to validate that data, and set errors on the invalid values. |
|
301 | + * But if the data has already been validated |
|
302 | + * (eg you validated the data then stored it in the DB) |
|
303 | + * you may want to skip this step. |
|
304 | + * @throws InvalidArgumentException |
|
305 | + * @throws InvalidInterfaceException |
|
306 | + * @throws InvalidDataTypeException |
|
307 | + * @throws EE_Error |
|
308 | + */ |
|
309 | + public function receive_form_submission($req_data = null, $validate = true) |
|
310 | + { |
|
311 | + $req_data = $this->getCachedRequest($req_data); |
|
312 | + $this->_normalize($req_data); |
|
313 | + if ($validate) { |
|
314 | + $this->_validate(); |
|
315 | + //if it's invalid, we're going to want to re-display so remember what they submitted |
|
316 | + if (! $this->is_valid()) { |
|
317 | + $this->store_submitted_form_data_in_session(); |
|
318 | + } |
|
319 | + } |
|
320 | + if ($this->submission_error_message() === '' && ! $this->is_valid()) { |
|
321 | + $this->set_submission_error_message(); |
|
322 | + } |
|
323 | + do_action( |
|
324 | + 'AHEE__EE_Form_Section_Proper__receive_form_submission__end', |
|
325 | + $req_data, |
|
326 | + $this, |
|
327 | + $validate |
|
328 | + ); |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * caches the originally submitted input values in the session |
|
334 | + * so that they can be used to repopulate the form if it failed validation |
|
335 | + * |
|
336 | + * @return boolean whether or not the data was successfully stored in the session |
|
337 | + * @throws InvalidArgumentException |
|
338 | + * @throws InvalidInterfaceException |
|
339 | + * @throws InvalidDataTypeException |
|
340 | + * @throws EE_Error |
|
341 | + */ |
|
342 | + protected function store_submitted_form_data_in_session() |
|
343 | + { |
|
344 | + return EE_Registry::instance()->SSN->set_session_data( |
|
345 | + array( |
|
346 | + EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY => $this->submitted_values(true), |
|
347 | + ) |
|
348 | + ); |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * retrieves the originally submitted input values in the session |
|
354 | + * so that they can be used to repopulate the form if it failed validation |
|
355 | + * |
|
356 | + * @return array |
|
357 | + * @throws InvalidArgumentException |
|
358 | + * @throws InvalidInterfaceException |
|
359 | + * @throws InvalidDataTypeException |
|
360 | + */ |
|
361 | + protected function get_submitted_form_data_from_session() |
|
362 | + { |
|
363 | + $session = EE_Registry::instance()->SSN; |
|
364 | + if ($session instanceof EE_Session) { |
|
365 | + return $session->get_session_data( |
|
366 | + EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY |
|
367 | + ); |
|
368 | + } |
|
369 | + return array(); |
|
370 | + } |
|
371 | + |
|
372 | + |
|
373 | + /** |
|
374 | + * flushed the originally submitted input values from the session |
|
375 | + * |
|
376 | + * @return boolean whether or not the data was successfully removed from the session |
|
377 | + * @throws InvalidArgumentException |
|
378 | + * @throws InvalidInterfaceException |
|
379 | + * @throws InvalidDataTypeException |
|
380 | + */ |
|
381 | + protected function flush_submitted_form_data_from_session() |
|
382 | + { |
|
383 | + return EE_Registry::instance()->SSN->reset_data( |
|
384 | + array(EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY) |
|
385 | + ); |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + /** |
|
390 | + * Populates this form and its subsections with data from the session. |
|
391 | + * (Wrapper for EE_Form_Section_Proper::receive_form_submission, so it shows |
|
392 | + * validation errors when displaying too) |
|
393 | + * Returns true if the form was populated from the session, false otherwise |
|
394 | + * |
|
395 | + * @return boolean |
|
396 | + * @throws InvalidArgumentException |
|
397 | + * @throws InvalidInterfaceException |
|
398 | + * @throws InvalidDataTypeException |
|
399 | + * @throws EE_Error |
|
400 | + */ |
|
401 | + public function populate_from_session() |
|
402 | + { |
|
403 | + $form_data_in_session = $this->get_submitted_form_data_from_session(); |
|
404 | + if (empty($form_data_in_session)) { |
|
405 | + return false; |
|
406 | + } |
|
407 | + $this->receive_form_submission($form_data_in_session); |
|
408 | + $this->flush_submitted_form_data_from_session(); |
|
409 | + if ($this->form_data_present_in($form_data_in_session)) { |
|
410 | + return true; |
|
411 | + } |
|
412 | + return false; |
|
413 | + } |
|
414 | + |
|
415 | + |
|
416 | + /** |
|
417 | + * Populates the default data for the form, given an array where keys are |
|
418 | + * the input names, and values are their values (preferably normalized to be their |
|
419 | + * proper PHP types, not all strings... although that should be ok too). |
|
420 | + * Proper subsections are sub-arrays, the key being the subsection's name, and |
|
421 | + * the value being an array formatted in teh same way |
|
422 | + * |
|
423 | + * @param array $default_data |
|
424 | + * @throws EE_Error |
|
425 | + */ |
|
426 | + public function populate_defaults($default_data) |
|
427 | + { |
|
428 | + foreach ($this->subsections(false) as $subsection_name => $subsection) { |
|
429 | + if (isset($default_data[ $subsection_name ])) { |
|
430 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
431 | + $subsection->set_default($default_data[ $subsection_name ]); |
|
432 | + } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
433 | + $subsection->populate_defaults($default_data[ $subsection_name ]); |
|
434 | + } |
|
435 | + } |
|
436 | + } |
|
437 | + } |
|
438 | + |
|
439 | + |
|
440 | + /** |
|
441 | + * returns true if subsection exists |
|
442 | + * |
|
443 | + * @param string $name |
|
444 | + * @return boolean |
|
445 | + */ |
|
446 | + public function subsection_exists($name) |
|
447 | + { |
|
448 | + return isset($this->_subsections[ $name ]) ? true : false; |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + /** |
|
453 | + * Gets the subsection specified by its name |
|
454 | + * |
|
455 | + * @param string $name |
|
456 | + * @param boolean $require_construction_to_be_finalized most client code should leave this as TRUE |
|
457 | + * so that the inputs will be properly configured. |
|
458 | + * However, some client code may be ok |
|
459 | + * with construction finalize being called later |
|
460 | + * (realizing that the subsections' html names |
|
461 | + * might not be set yet, etc.) |
|
462 | + * @return EE_Form_Section_Base |
|
463 | + * @throws EE_Error |
|
464 | + */ |
|
465 | + public function get_subsection($name, $require_construction_to_be_finalized = true) |
|
466 | + { |
|
467 | + if ($require_construction_to_be_finalized) { |
|
468 | + $this->ensure_construct_finalized_called(); |
|
469 | + } |
|
470 | + return $this->subsection_exists($name) ? $this->_subsections[ $name ] : null; |
|
471 | + } |
|
472 | + |
|
473 | + |
|
474 | + /** |
|
475 | + * Gets all the validatable subsections of this form section |
|
476 | + * |
|
477 | + * @return EE_Form_Section_Validatable[] |
|
478 | + * @throws EE_Error |
|
479 | + */ |
|
480 | + public function get_validatable_subsections() |
|
481 | + { |
|
482 | + $validatable_subsections = array(); |
|
483 | + foreach ($this->subsections() as $name => $obj) { |
|
484 | + if ($obj instanceof EE_Form_Section_Validatable) { |
|
485 | + $validatable_subsections[ $name ] = $obj; |
|
486 | + } |
|
487 | + } |
|
488 | + return $validatable_subsections; |
|
489 | + } |
|
490 | + |
|
491 | + |
|
492 | + /** |
|
493 | + * Gets an input by the given name. If not found, or if its not an EE_FOrm_Input_Base child, |
|
494 | + * throw an EE_Error. |
|
495 | + * |
|
496 | + * @param string $name |
|
497 | + * @param boolean $require_construction_to_be_finalized most client code should |
|
498 | + * leave this as TRUE so that the inputs will be properly |
|
499 | + * configured. However, some client code may be ok with |
|
500 | + * construction finalize being called later |
|
501 | + * (realizing that the subsections' html names might not be |
|
502 | + * set yet, etc.) |
|
503 | + * @return EE_Form_Input_Base |
|
504 | + * @throws EE_Error |
|
505 | + */ |
|
506 | + public function get_input($name, $require_construction_to_be_finalized = true) |
|
507 | + { |
|
508 | + $subsection = $this->get_subsection( |
|
509 | + $name, |
|
510 | + $require_construction_to_be_finalized |
|
511 | + ); |
|
512 | + if (! $subsection instanceof EE_Form_Input_Base) { |
|
513 | + throw new EE_Error( |
|
514 | + sprintf( |
|
515 | + esc_html__( |
|
516 | + "Subsection '%s' is not an instanceof EE_Form_Input_Base on form '%s'. It is a '%s'", |
|
517 | + 'event_espresso' |
|
518 | + ), |
|
519 | + $name, |
|
520 | + get_class($this), |
|
521 | + $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso') |
|
522 | + ) |
|
523 | + ); |
|
524 | + } |
|
525 | + return $subsection; |
|
526 | + } |
|
527 | + |
|
528 | + |
|
529 | + /** |
|
530 | + * Like get_input(), gets the proper subsection of the form given the name, |
|
531 | + * otherwise throws an EE_Error |
|
532 | + * |
|
533 | + * @param string $name |
|
534 | + * @param boolean $require_construction_to_be_finalized most client code should |
|
535 | + * leave this as TRUE so that the inputs will be properly |
|
536 | + * configured. However, some client code may be ok with |
|
537 | + * construction finalize being called later |
|
538 | + * (realizing that the subsections' html names might not be |
|
539 | + * set yet, etc.) |
|
540 | + * @return EE_Form_Section_Proper |
|
541 | + * @throws EE_Error |
|
542 | + */ |
|
543 | + public function get_proper_subsection($name, $require_construction_to_be_finalized = true) |
|
544 | + { |
|
545 | + $subsection = $this->get_subsection( |
|
546 | + $name, |
|
547 | + $require_construction_to_be_finalized |
|
548 | + ); |
|
549 | + if (! $subsection instanceof EE_Form_Section_Proper) { |
|
550 | + throw new EE_Error( |
|
551 | + sprintf( |
|
552 | + esc_html__( |
|
553 | + "Subsection '%'s is not an instanceof EE_Form_Section_Proper on form '%s'", |
|
554 | + 'event_espresso' |
|
555 | + ), |
|
556 | + $name, |
|
557 | + get_class($this) |
|
558 | + ) |
|
559 | + ); |
|
560 | + } |
|
561 | + return $subsection; |
|
562 | + } |
|
563 | + |
|
564 | + |
|
565 | + /** |
|
566 | + * Gets the value of the specified input. Should be called after receive_form_submission() |
|
567 | + * or populate_defaults() on the form, where the normalized value on the input is set. |
|
568 | + * |
|
569 | + * @param string $name |
|
570 | + * @return mixed depending on the input's type and its normalization strategy |
|
571 | + * @throws EE_Error |
|
572 | + */ |
|
573 | + public function get_input_value($name) |
|
574 | + { |
|
575 | + $input = $this->get_input($name); |
|
576 | + return $input->normalized_value(); |
|
577 | + } |
|
578 | + |
|
579 | + |
|
580 | + /** |
|
581 | + * Checks if this form section itself is valid, and then checks its subsections |
|
582 | + * |
|
583 | + * @throws EE_Error |
|
584 | + * @return boolean |
|
585 | + */ |
|
586 | + public function is_valid() |
|
587 | + { |
|
588 | + if($this->is_valid === null) { |
|
589 | + if (! $this->has_received_submission()) { |
|
590 | + throw new EE_Error( |
|
591 | + sprintf( |
|
592 | + esc_html__( |
|
593 | + 'You cannot check if a form is valid before receiving the form submission using receive_form_submission', |
|
594 | + 'event_espresso' |
|
595 | + ) |
|
596 | + ) |
|
597 | + ); |
|
598 | + } |
|
599 | + if (! parent::is_valid()) { |
|
600 | + $this->is_valid = false; |
|
601 | + } else { |
|
602 | + // ok so no general errors to this entire form section. |
|
603 | + // so let's check the subsections, but only set errors if that hasn't been done yet |
|
604 | + $this->is_valid = true; |
|
605 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
606 | + if (! $subsection->is_valid()) { |
|
607 | + $this->is_valid = false; |
|
608 | + } |
|
609 | + } |
|
610 | + } |
|
611 | + } |
|
612 | + return $this->is_valid; |
|
613 | + } |
|
614 | + |
|
615 | + |
|
616 | + /** |
|
617 | + * gets the default name of this form section if none is specified |
|
618 | + * |
|
619 | + * @return void |
|
620 | + */ |
|
621 | + protected function _set_default_name_if_empty() |
|
622 | + { |
|
623 | + if (! $this->_name) { |
|
624 | + $classname = get_class($this); |
|
625 | + $default_name = str_replace('EE_', '', $classname); |
|
626 | + $this->_name = $default_name; |
|
627 | + } |
|
628 | + } |
|
629 | + |
|
630 | + |
|
631 | + /** |
|
632 | + * Returns the HTML for the form, except for the form opening and closing tags |
|
633 | + * (as the form section doesn't know where you necessarily want to send the information to), |
|
634 | + * and except for a submit button. Enqueues JS and CSS; if called early enough we will |
|
635 | + * try to enqueue them in the header, otherwise they'll be enqueued in the footer. |
|
636 | + * Not doing_it_wrong because theoretically this CAN be used properly, |
|
637 | + * provided its used during "wp_enqueue_scripts", or it doesn't need to enqueue |
|
638 | + * any CSS. |
|
639 | + * |
|
640 | + * @throws InvalidArgumentException |
|
641 | + * @throws InvalidInterfaceException |
|
642 | + * @throws InvalidDataTypeException |
|
643 | + * @throws EE_Error |
|
644 | + */ |
|
645 | + public function get_html_and_js() |
|
646 | + { |
|
647 | + $this->enqueue_js(); |
|
648 | + return $this->get_html(); |
|
649 | + } |
|
650 | + |
|
651 | + |
|
652 | + /** |
|
653 | + * returns HTML for displaying this form section. recursively calls display_section() on all subsections |
|
654 | + * |
|
655 | + * @param bool $display_previously_submitted_data |
|
656 | + * @return string |
|
657 | + * @throws InvalidArgumentException |
|
658 | + * @throws InvalidInterfaceException |
|
659 | + * @throws InvalidDataTypeException |
|
660 | + * @throws EE_Error |
|
661 | + * @throws EE_Error |
|
662 | + * @throws EE_Error |
|
663 | + */ |
|
664 | + public function get_html($display_previously_submitted_data = true) |
|
665 | + { |
|
666 | + $this->ensure_construct_finalized_called(); |
|
667 | + if ($display_previously_submitted_data) { |
|
668 | + $this->populate_from_session(); |
|
669 | + } |
|
670 | + return $this->_form_html_filter |
|
671 | + ? $this->_form_html_filter->filterHtml($this->_layout_strategy->layout_form(), $this) |
|
672 | + : $this->_layout_strategy->layout_form(); |
|
673 | + } |
|
674 | + |
|
675 | + |
|
676 | + /** |
|
677 | + * enqueues JS and CSS for the form. |
|
678 | + * It is preferred to call this before wp_enqueue_scripts so the |
|
679 | + * scripts and styles can be put in the header, but if called later |
|
680 | + * they will be put in the footer (which is OK for JS, but in HTML4 CSS should |
|
681 | + * only be in the header; but in HTML5 its ok in the body. |
|
682 | + * See http://stackoverflow.com/questions/4957446/load-external-css-file-in-body-tag. |
|
683 | + * So if your form enqueues CSS, it's preferred to call this before wp_enqueue_scripts.) |
|
684 | + * |
|
685 | + * @return void |
|
686 | + * @throws EE_Error |
|
687 | + */ |
|
688 | + public function enqueue_js() |
|
689 | + { |
|
690 | + $this->_enqueue_and_localize_form_js(); |
|
691 | + foreach ($this->subsections() as $subsection) { |
|
692 | + $subsection->enqueue_js(); |
|
693 | + } |
|
694 | + } |
|
695 | + |
|
696 | + |
|
697 | + /** |
|
698 | + * adds a filter so that jquery validate gets enqueued in EE_System::wp_enqueue_scripts(). |
|
699 | + * This must be done BEFORE wp_enqueue_scripts() gets called, which is on |
|
700 | + * the wp_enqueue_scripts hook. |
|
701 | + * However, registering the form js and localizing it can happen when we |
|
702 | + * actually output the form (which is preferred, seeing how teh form's fields |
|
703 | + * could change until it's actually outputted) |
|
704 | + * |
|
705 | + * @param boolean $init_form_validation_automatically whether or not we want the form validation |
|
706 | + * to be triggered automatically or not |
|
707 | + * @return void |
|
708 | + */ |
|
709 | + public static function wp_enqueue_scripts($init_form_validation_automatically = true) |
|
710 | + { |
|
711 | + wp_register_script( |
|
712 | + 'ee_form_section_validation', |
|
713 | + EE_GLOBAL_ASSETS_URL . 'scripts' . DS . 'form_section_validation.js', |
|
714 | + array('jquery-validate', 'jquery-ui-datepicker', 'jquery-validate-extra-methods'), |
|
715 | + EVENT_ESPRESSO_VERSION, |
|
716 | + true |
|
717 | + ); |
|
718 | + wp_localize_script( |
|
719 | + 'ee_form_section_validation', |
|
720 | + 'ee_form_section_validation_init', |
|
721 | + array('init' => $init_form_validation_automatically ? '1' : '0') |
|
722 | + ); |
|
723 | + } |
|
724 | + |
|
725 | + |
|
726 | + /** |
|
727 | + * gets the variables used by form_section_validation.js. |
|
728 | + * This needs to be called AFTER we've called $this->_enqueue_jquery_validate_script, |
|
729 | + * but before the wordpress hook wp_loaded |
|
730 | + * |
|
731 | + * @throws EE_Error |
|
732 | + */ |
|
733 | + public function _enqueue_and_localize_form_js() |
|
734 | + { |
|
735 | + $this->ensure_construct_finalized_called(); |
|
736 | + //actually, we don't want to localize just yet. There may be other forms on the page. |
|
737 | + //so we need to add our form section data to a static variable accessible by all form sections |
|
738 | + //and localize it just before the footer |
|
739 | + $this->localize_validation_rules(); |
|
740 | + add_action('wp_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'), 2); |
|
741 | + add_action('admin_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms')); |
|
742 | + } |
|
743 | + |
|
744 | + |
|
745 | + /** |
|
746 | + * add our form section data to a static variable accessible by all form sections |
|
747 | + * |
|
748 | + * @param bool $return_for_subsection |
|
749 | + * @return void |
|
750 | + * @throws EE_Error |
|
751 | + */ |
|
752 | + public function localize_validation_rules($return_for_subsection = false) |
|
753 | + { |
|
754 | + // we only want to localize vars ONCE for the entire form, |
|
755 | + // so if the form section doesn't have a parent, then it must be the top dog |
|
756 | + if ($return_for_subsection || ! $this->parent_section()) { |
|
757 | + EE_Form_Section_Proper::$_js_localization['form_data'][ $this->html_id() ] = array( |
|
758 | + 'form_section_id' => $this->html_id(true), |
|
759 | + 'validation_rules' => $this->get_jquery_validation_rules(), |
|
760 | + 'other_data' => $this->get_other_js_data(), |
|
761 | + 'errors' => $this->subsection_validation_errors_by_html_name(), |
|
762 | + ); |
|
763 | + EE_Form_Section_Proper::$_scripts_localized = true; |
|
764 | + } |
|
765 | + } |
|
766 | + |
|
767 | + |
|
768 | + /** |
|
769 | + * Gets an array of extra data that will be useful for client-side javascript. |
|
770 | + * This is primarily data added by inputs and forms in addition to any |
|
771 | + * scripts they might enqueue |
|
772 | + * |
|
773 | + * @param array $form_other_js_data |
|
774 | + * @return array |
|
775 | + * @throws EE_Error |
|
776 | + */ |
|
777 | + public function get_other_js_data($form_other_js_data = array()) |
|
778 | + { |
|
779 | + foreach ($this->subsections() as $subsection) { |
|
780 | + $form_other_js_data = $subsection->get_other_js_data($form_other_js_data); |
|
781 | + } |
|
782 | + return $form_other_js_data; |
|
783 | + } |
|
784 | + |
|
785 | + |
|
786 | + /** |
|
787 | + * Gets a flat array of inputs for this form section and its subsections. |
|
788 | + * Keys are their form names, and values are the inputs themselves |
|
789 | + * |
|
790 | + * @return EE_Form_Input_Base |
|
791 | + * @throws EE_Error |
|
792 | + */ |
|
793 | + public function inputs_in_subsections() |
|
794 | + { |
|
795 | + $inputs = array(); |
|
796 | + foreach ($this->subsections() as $subsection) { |
|
797 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
798 | + $inputs[ $subsection->html_name() ] = $subsection; |
|
799 | + } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
800 | + $inputs += $subsection->inputs_in_subsections(); |
|
801 | + } |
|
802 | + } |
|
803 | + return $inputs; |
|
804 | + } |
|
805 | + |
|
806 | + |
|
807 | + /** |
|
808 | + * Gets a flat array of all the validation errors. |
|
809 | + * Keys are html names (because those should be unique) |
|
810 | + * and values are a string of all their validation errors |
|
811 | + * |
|
812 | + * @return string[] |
|
813 | + * @throws EE_Error |
|
814 | + */ |
|
815 | + public function subsection_validation_errors_by_html_name() |
|
816 | + { |
|
817 | + $inputs = $this->inputs(); |
|
818 | + $errors = array(); |
|
819 | + foreach ($inputs as $form_input) { |
|
820 | + if ($form_input instanceof EE_Form_Input_Base && $form_input->get_validation_errors()) { |
|
821 | + $errors[ $form_input->html_name() ] = $form_input->get_validation_error_string(); |
|
822 | + } |
|
823 | + } |
|
824 | + return $errors; |
|
825 | + } |
|
826 | + |
|
827 | + |
|
828 | + /** |
|
829 | + * passes all the form data required by the JS to the JS, and enqueues the few required JS files. |
|
830 | + * Should be setup by each form during the _enqueues_and_localize_form_js |
|
831 | + * |
|
832 | + * @throws InvalidArgumentException |
|
833 | + * @throws InvalidInterfaceException |
|
834 | + * @throws InvalidDataTypeException |
|
835 | + */ |
|
836 | + public static function localize_script_for_all_forms() |
|
837 | + { |
|
838 | + //allow inputs and stuff to hook in their JS and stuff here |
|
839 | + do_action('AHEE__EE_Form_Section_Proper__localize_script_for_all_forms__begin'); |
|
840 | + EE_Form_Section_Proper::$_js_localization['localized_error_messages'] = EE_Form_Section_Proper::_get_localized_error_messages(); |
|
841 | + $email_validation_level = isset(EE_Registry::instance()->CFG->registration->email_validation_level) |
|
842 | + ? EE_Registry::instance()->CFG->registration->email_validation_level |
|
843 | + : 'wp_default'; |
|
844 | + EE_Form_Section_Proper::$_js_localization['email_validation_level'] = $email_validation_level; |
|
845 | + wp_enqueue_script('ee_form_section_validation'); |
|
846 | + wp_localize_script( |
|
847 | + 'ee_form_section_validation', |
|
848 | + 'ee_form_section_vars', |
|
849 | + EE_Form_Section_Proper::$_js_localization |
|
850 | + ); |
|
851 | + } |
|
852 | + |
|
853 | + |
|
854 | + /** |
|
855 | + * ensure_scripts_localized |
|
856 | + * |
|
857 | + * @throws EE_Error |
|
858 | + */ |
|
859 | + public function ensure_scripts_localized() |
|
860 | + { |
|
861 | + if (! EE_Form_Section_Proper::$_scripts_localized) { |
|
862 | + $this->_enqueue_and_localize_form_js(); |
|
863 | + } |
|
864 | + } |
|
865 | + |
|
866 | + |
|
867 | + /** |
|
868 | + * Gets the hard-coded validation error messages to be used in the JS. The convention |
|
869 | + * is that the key here should be the same as the custom validation rule put in the JS file |
|
870 | + * |
|
871 | + * @return array keys are custom validation rules, and values are internationalized strings |
|
872 | + */ |
|
873 | + private static function _get_localized_error_messages() |
|
874 | + { |
|
875 | + return array( |
|
876 | + 'validUrl' => esc_html__('This is not a valid absolute URL. Eg, http://domain.com/monkey.jpg', 'event_espresso'), |
|
877 | + 'regex' => esc_html__('Please check your input', 'event_espresso'), |
|
878 | + ); |
|
879 | + } |
|
880 | + |
|
881 | + |
|
882 | + /** |
|
883 | + * @return array |
|
884 | + */ |
|
885 | + public static function js_localization() |
|
886 | + { |
|
887 | + return self::$_js_localization; |
|
888 | + } |
|
889 | + |
|
890 | + |
|
891 | + /** |
|
892 | + * @return void |
|
893 | + */ |
|
894 | + public static function reset_js_localization() |
|
895 | + { |
|
896 | + self::$_js_localization = array(); |
|
897 | + } |
|
898 | + |
|
899 | + |
|
900 | + /** |
|
901 | + * Gets the JS to put inside the jquery validation rules for subsection of this form section. |
|
902 | + * See parent function for more... |
|
903 | + * |
|
904 | + * @return array |
|
905 | + * @throws EE_Error |
|
906 | + */ |
|
907 | + public function get_jquery_validation_rules() |
|
908 | + { |
|
909 | + $jquery_validation_rules = array(); |
|
910 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
911 | + $jquery_validation_rules = array_merge( |
|
912 | + $jquery_validation_rules, |
|
913 | + $subsection->get_jquery_validation_rules() |
|
914 | + ); |
|
915 | + } |
|
916 | + return $jquery_validation_rules; |
|
917 | + } |
|
918 | + |
|
919 | + |
|
920 | + /** |
|
921 | + * Sanitizes all the data and sets the sanitized value of each field |
|
922 | + * |
|
923 | + * @param array $req_data like $_POST |
|
924 | + * @return void |
|
925 | + * @throws EE_Error |
|
926 | + */ |
|
927 | + protected function _normalize($req_data) |
|
928 | + { |
|
929 | + $this->_received_submission = true; |
|
930 | + $this->_validation_errors = array(); |
|
931 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
932 | + try { |
|
933 | + $subsection->_normalize($req_data); |
|
934 | + } catch (EE_Validation_Error $e) { |
|
935 | + $subsection->add_validation_error($e); |
|
936 | + } |
|
937 | + } |
|
938 | + } |
|
939 | + |
|
940 | + |
|
941 | + /** |
|
942 | + * Performs validation on this form section and its subsections. |
|
943 | + * For each subsection, |
|
944 | + * calls _validate_{subsection_name} on THIS form (if the function exists) |
|
945 | + * and passes it the subsection, then calls _validate on that subsection. |
|
946 | + * If you need to perform validation on the form as a whole (considering multiple) |
|
947 | + * you would be best to override this _validate method, |
|
948 | + * calling parent::_validate() first. |
|
949 | + * |
|
950 | + * @throws EE_Error |
|
951 | + */ |
|
952 | + protected function _validate() |
|
953 | + { |
|
954 | + //reset the cache of whether this form is valid or not- we're re-validating it now |
|
955 | + $this->is_valid = null; |
|
956 | + foreach ($this->get_validatable_subsections() as $subsection_name => $subsection) { |
|
957 | + if (method_exists($this, '_validate_' . $subsection_name)) { |
|
958 | + call_user_func_array(array($this, '_validate_' . $subsection_name), array($subsection)); |
|
959 | + } |
|
960 | + $subsection->_validate(); |
|
961 | + } |
|
962 | + } |
|
963 | + |
|
964 | + |
|
965 | + /** |
|
966 | + * Gets all the validated inputs for the form section |
|
967 | + * |
|
968 | + * @return array |
|
969 | + * @throws EE_Error |
|
970 | + */ |
|
971 | + public function valid_data() |
|
972 | + { |
|
973 | + $inputs = array(); |
|
974 | + foreach ($this->subsections() as $subsection_name => $subsection) { |
|
975 | + if ($subsection instanceof EE_Form_Section_Proper) { |
|
976 | + $inputs[ $subsection_name ] = $subsection->valid_data(); |
|
977 | + } elseif ($subsection instanceof EE_Form_Input_Base) { |
|
978 | + $inputs[ $subsection_name ] = $subsection->normalized_value(); |
|
979 | + } |
|
980 | + } |
|
981 | + return $inputs; |
|
982 | + } |
|
983 | + |
|
984 | + |
|
985 | + /** |
|
986 | + * Gets all the inputs on this form section |
|
987 | + * |
|
988 | + * @return EE_Form_Input_Base[] |
|
989 | + * @throws EE_Error |
|
990 | + */ |
|
991 | + public function inputs() |
|
992 | + { |
|
993 | + $inputs = array(); |
|
994 | + foreach ($this->subsections() as $subsection_name => $subsection) { |
|
995 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
996 | + $inputs[ $subsection_name ] = $subsection; |
|
997 | + } |
|
998 | + } |
|
999 | + return $inputs; |
|
1000 | + } |
|
1001 | + |
|
1002 | + |
|
1003 | + /** |
|
1004 | + * Gets all the subsections which are a proper form |
|
1005 | + * |
|
1006 | + * @return EE_Form_Section_Proper[] |
|
1007 | + * @throws EE_Error |
|
1008 | + */ |
|
1009 | + public function subforms() |
|
1010 | + { |
|
1011 | + $form_sections = array(); |
|
1012 | + foreach ($this->subsections() as $name => $obj) { |
|
1013 | + if ($obj instanceof EE_Form_Section_Proper) { |
|
1014 | + $form_sections[ $name ] = $obj; |
|
1015 | + } |
|
1016 | + } |
|
1017 | + return $form_sections; |
|
1018 | + } |
|
1019 | + |
|
1020 | + |
|
1021 | + /** |
|
1022 | + * Gets all the subsections (inputs, proper subsections, or html-only sections). |
|
1023 | + * Consider using inputs() or subforms() |
|
1024 | + * if you only want form inputs or proper form sections. |
|
1025 | + * |
|
1026 | + * @param boolean $require_construction_to_be_finalized most client code should |
|
1027 | + * leave this as TRUE so that the inputs will be properly |
|
1028 | + * configured. However, some client code may be ok with |
|
1029 | + * construction finalize being called later |
|
1030 | + * (realizing that the subsections' html names might not be |
|
1031 | + * set yet, etc.) |
|
1032 | + * @return EE_Form_Section_Proper[] |
|
1033 | + * @throws EE_Error |
|
1034 | + */ |
|
1035 | + public function subsections($require_construction_to_be_finalized = true) |
|
1036 | + { |
|
1037 | + if ($require_construction_to_be_finalized) { |
|
1038 | + $this->ensure_construct_finalized_called(); |
|
1039 | + } |
|
1040 | + return $this->_subsections; |
|
1041 | + } |
|
1042 | + |
|
1043 | + |
|
1044 | + /** |
|
1045 | + * Returns whether this form has any subforms or inputs |
|
1046 | + * @return bool |
|
1047 | + */ |
|
1048 | + public function hasSubsections() |
|
1049 | + { |
|
1050 | + return ! empty($this->_subsections); |
|
1051 | + } |
|
1052 | + |
|
1053 | + |
|
1054 | + /** |
|
1055 | + * Returns a simple array where keys are input names, and values are their normalized |
|
1056 | + * values. (Similar to calling get_input_value on inputs) |
|
1057 | + * |
|
1058 | + * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
1059 | + * or just this forms' direct children inputs |
|
1060 | + * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
1061 | + * or allow multidimensional array |
|
1062 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
1063 | + * with array keys being input names |
|
1064 | + * (regardless of whether they are from a subsection or not), |
|
1065 | + * and if $flatten is FALSE it can be a multidimensional array |
|
1066 | + * where keys are always subsection names and values are either |
|
1067 | + * the input's normalized value, or an array like the top-level array |
|
1068 | + * @throws EE_Error |
|
1069 | + */ |
|
1070 | + public function input_values($include_subform_inputs = false, $flatten = false) |
|
1071 | + { |
|
1072 | + return $this->_input_values(false, $include_subform_inputs, $flatten); |
|
1073 | + } |
|
1074 | + |
|
1075 | + |
|
1076 | + /** |
|
1077 | + * Similar to EE_Form_Section_Proper::input_values(), except this returns the 'display_value' |
|
1078 | + * of each input. On some inputs (especially radio boxes or checkboxes), the value stored |
|
1079 | + * is not necessarily the value we want to display to users. This creates an array |
|
1080 | + * where keys are the input names, and values are their display values |
|
1081 | + * |
|
1082 | + * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
1083 | + * or just this forms' direct children inputs |
|
1084 | + * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
1085 | + * or allow multidimensional array |
|
1086 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
1087 | + * with array keys being input names |
|
1088 | + * (regardless of whether they are from a subsection or not), |
|
1089 | + * and if $flatten is FALSE it can be a multidimensional array |
|
1090 | + * where keys are always subsection names and values are either |
|
1091 | + * the input's normalized value, or an array like the top-level array |
|
1092 | + * @throws EE_Error |
|
1093 | + */ |
|
1094 | + public function input_pretty_values($include_subform_inputs = false, $flatten = false) |
|
1095 | + { |
|
1096 | + return $this->_input_values(true, $include_subform_inputs, $flatten); |
|
1097 | + } |
|
1098 | + |
|
1099 | + |
|
1100 | + /** |
|
1101 | + * Gets the input values from the form |
|
1102 | + * |
|
1103 | + * @param boolean $pretty Whether to retrieve the pretty value, |
|
1104 | + * or just the normalized value |
|
1105 | + * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
1106 | + * or just this forms' direct children inputs |
|
1107 | + * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
1108 | + * or allow multidimensional array |
|
1109 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array with array keys being |
|
1110 | + * input names (regardless of whether they are from a subsection or not), |
|
1111 | + * and if $flatten is FALSE it can be a multidimensional array |
|
1112 | + * where keys are always subsection names and values are either |
|
1113 | + * the input's normalized value, or an array like the top-level array |
|
1114 | + * @throws EE_Error |
|
1115 | + */ |
|
1116 | + public function _input_values($pretty = false, $include_subform_inputs = false, $flatten = false) |
|
1117 | + { |
|
1118 | + $input_values = array(); |
|
1119 | + foreach ($this->subsections() as $subsection_name => $subsection) { |
|
1120 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
1121 | + $input_values[ $subsection_name ] = $pretty |
|
1122 | + ? $subsection->pretty_value() |
|
1123 | + : $subsection->normalized_value(); |
|
1124 | + } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subform_inputs) { |
|
1125 | + $subform_input_values = $subsection->_input_values( |
|
1126 | + $pretty, |
|
1127 | + $include_subform_inputs, |
|
1128 | + $flatten |
|
1129 | + ); |
|
1130 | + if ($flatten) { |
|
1131 | + $input_values = array_merge($input_values, $subform_input_values); |
|
1132 | + } else { |
|
1133 | + $input_values[ $subsection_name ] = $subform_input_values; |
|
1134 | + } |
|
1135 | + } |
|
1136 | + } |
|
1137 | + return $input_values; |
|
1138 | + } |
|
1139 | + |
|
1140 | + |
|
1141 | + /** |
|
1142 | + * Gets the originally submitted input values from the form |
|
1143 | + * |
|
1144 | + * @param boolean $include_subforms Whether to include inputs from subforms, |
|
1145 | + * or just this forms' direct children inputs |
|
1146 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
1147 | + * with array keys being input names |
|
1148 | + * (regardless of whether they are from a subsection or not), |
|
1149 | + * and if $flatten is FALSE it can be a multidimensional array |
|
1150 | + * where keys are always subsection names and values are either |
|
1151 | + * the input's normalized value, or an array like the top-level array |
|
1152 | + * @throws EE_Error |
|
1153 | + */ |
|
1154 | + public function submitted_values($include_subforms = false) |
|
1155 | + { |
|
1156 | + $submitted_values = array(); |
|
1157 | + foreach ($this->subsections() as $subsection) { |
|
1158 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
1159 | + // is this input part of an array of inputs? |
|
1160 | + if (strpos($subsection->html_name(), '[') !== false) { |
|
1161 | + $full_input_name = EEH_Array::convert_array_values_to_keys( |
|
1162 | + explode( |
|
1163 | + '[', |
|
1164 | + str_replace(']', '', $subsection->html_name()) |
|
1165 | + ), |
|
1166 | + $subsection->raw_value() |
|
1167 | + ); |
|
1168 | + $submitted_values = array_replace_recursive($submitted_values, $full_input_name); |
|
1169 | + } else { |
|
1170 | + $submitted_values[ $subsection->html_name() ] = $subsection->raw_value(); |
|
1171 | + } |
|
1172 | + } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subforms) { |
|
1173 | + $subform_input_values = $subsection->submitted_values($include_subforms); |
|
1174 | + $submitted_values = array_replace_recursive($submitted_values, $subform_input_values); |
|
1175 | + } |
|
1176 | + } |
|
1177 | + return $submitted_values; |
|
1178 | + } |
|
1179 | + |
|
1180 | + |
|
1181 | + /** |
|
1182 | + * Indicates whether or not this form has received a submission yet |
|
1183 | + * (ie, had receive_form_submission called on it yet) |
|
1184 | + * |
|
1185 | + * @return boolean |
|
1186 | + * @throws EE_Error |
|
1187 | + */ |
|
1188 | + public function has_received_submission() |
|
1189 | + { |
|
1190 | + $this->ensure_construct_finalized_called(); |
|
1191 | + return $this->_received_submission; |
|
1192 | + } |
|
1193 | + |
|
1194 | + |
|
1195 | + /** |
|
1196 | + * Equivalent to passing 'exclude' in the constructor's options array. |
|
1197 | + * Removes the listed inputs from the form |
|
1198 | + * |
|
1199 | + * @param array $inputs_to_exclude values are the input names |
|
1200 | + * @return void |
|
1201 | + */ |
|
1202 | + public function exclude(array $inputs_to_exclude = array()) |
|
1203 | + { |
|
1204 | + foreach ($inputs_to_exclude as $input_to_exclude_name) { |
|
1205 | + unset($this->_subsections[ $input_to_exclude_name ]); |
|
1206 | + } |
|
1207 | + } |
|
1208 | + |
|
1209 | + |
|
1210 | + /** |
|
1211 | + * Changes these inputs' display strategy to be EE_Hidden_Display_Strategy. |
|
1212 | + * @param array $inputs_to_hide |
|
1213 | + * @throws EE_Error |
|
1214 | + */ |
|
1215 | + public function hide(array $inputs_to_hide = array()) |
|
1216 | + { |
|
1217 | + foreach ($inputs_to_hide as $input_to_hide) { |
|
1218 | + $input = $this->get_input($input_to_hide); |
|
1219 | + $input->set_display_strategy(new EE_Hidden_Display_Strategy()); |
|
1220 | + } |
|
1221 | + } |
|
1222 | + |
|
1223 | + |
|
1224 | + /** |
|
1225 | + * add_subsections |
|
1226 | + * Adds the listed subsections to the form section. |
|
1227 | + * If $subsection_name_to_target is provided, |
|
1228 | + * then new subsections are added before or after that subsection, |
|
1229 | + * otherwise to the start or end of the entire subsections array. |
|
1230 | + * |
|
1231 | + * @param EE_Form_Section_Base[] $new_subsections array of new form subsections |
|
1232 | + * where keys are their names |
|
1233 | + * @param string $subsection_name_to_target an existing for section that $new_subsections |
|
1234 | + * should be added before or after |
|
1235 | + * IF $subsection_name_to_target is null, |
|
1236 | + * then $new_subsections will be added to |
|
1237 | + * the beginning or end of the entire subsections array |
|
1238 | + * @param boolean $add_before whether to add $new_subsections, before or after |
|
1239 | + * $subsection_name_to_target, |
|
1240 | + * or if $subsection_name_to_target is null, |
|
1241 | + * before or after entire subsections array |
|
1242 | + * @return void |
|
1243 | + * @throws EE_Error |
|
1244 | + */ |
|
1245 | + public function add_subsections($new_subsections, $subsection_name_to_target = null, $add_before = true) |
|
1246 | + { |
|
1247 | + foreach ($new_subsections as $subsection_name => $subsection) { |
|
1248 | + if (! $subsection instanceof EE_Form_Section_Base) { |
|
1249 | + EE_Error::add_error( |
|
1250 | + sprintf( |
|
1251 | + esc_html__( |
|
1252 | + "Trying to add a %s as a subsection (it was named '%s') to the form section '%s'. It was removed.", |
|
1253 | + 'event_espresso' |
|
1254 | + ), |
|
1255 | + get_class($subsection), |
|
1256 | + $subsection_name, |
|
1257 | + $this->name() |
|
1258 | + ) |
|
1259 | + ); |
|
1260 | + unset($new_subsections[ $subsection_name ]); |
|
1261 | + } |
|
1262 | + } |
|
1263 | + $this->_subsections = EEH_Array::insert_into_array( |
|
1264 | + $this->_subsections, |
|
1265 | + $new_subsections, |
|
1266 | + $subsection_name_to_target, |
|
1267 | + $add_before |
|
1268 | + ); |
|
1269 | + if ($this->_construction_finalized) { |
|
1270 | + foreach ($this->_subsections as $name => $subsection) { |
|
1271 | + $subsection->_construct_finalize($this, $name); |
|
1272 | + } |
|
1273 | + } |
|
1274 | + } |
|
1275 | + |
|
1276 | + |
|
1277 | + /** |
|
1278 | + * Just gets all validatable subsections to clean their sensitive data |
|
1279 | + * |
|
1280 | + * @throws EE_Error |
|
1281 | + */ |
|
1282 | + public function clean_sensitive_data() |
|
1283 | + { |
|
1284 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
1285 | + $subsection->clean_sensitive_data(); |
|
1286 | + } |
|
1287 | + } |
|
1288 | + |
|
1289 | + |
|
1290 | + /** |
|
1291 | + * Sets the submission error message (aka validation error message for this form section and all sub-sections) |
|
1292 | + * @param string $form_submission_error_message |
|
1293 | + * @param EE_Form_Section_Validatable $form_section unused |
|
1294 | + * @throws EE_Error |
|
1295 | + */ |
|
1296 | + public function set_submission_error_message( |
|
1297 | + $form_submission_error_message = '' |
|
1298 | + ) { |
|
1299 | + $this->_form_submission_error_message = ! empty($form_submission_error_message) |
|
1300 | + ? $form_submission_error_message |
|
1301 | + : $this->getAllValidationErrorsString(); |
|
1302 | + } |
|
1303 | + |
|
1304 | + |
|
1305 | + /** |
|
1306 | + * Returns the cached error message. A default value is set for this during _validate(), |
|
1307 | + * (called during receive_form_submission) but it can be explicitly set using |
|
1308 | + * set_submission_error_message |
|
1309 | + * |
|
1310 | + * @return string |
|
1311 | + */ |
|
1312 | + public function submission_error_message() |
|
1313 | + { |
|
1314 | + return $this->_form_submission_error_message; |
|
1315 | + } |
|
1316 | + |
|
1317 | + |
|
1318 | + /** |
|
1319 | + * Sets a message to display if the data submitted to the form was valid. |
|
1320 | + * @param string $form_submission_success_message |
|
1321 | + */ |
|
1322 | + public function set_submission_success_message($form_submission_success_message = '') |
|
1323 | + { |
|
1324 | + $this->_form_submission_success_message = ! empty($form_submission_success_message) |
|
1325 | + ? $form_submission_success_message |
|
1326 | + : esc_html__('Form submitted successfully', 'event_espresso'); |
|
1327 | + } |
|
1328 | + |
|
1329 | + |
|
1330 | + /** |
|
1331 | + * Gets a message appropriate for display when the form is correctly submitted |
|
1332 | + * @return string |
|
1333 | + */ |
|
1334 | + public function submission_success_message() |
|
1335 | + { |
|
1336 | + return $this->_form_submission_success_message; |
|
1337 | + } |
|
1338 | + |
|
1339 | + |
|
1340 | + /** |
|
1341 | + * Returns the prefix that should be used on child of this form section for |
|
1342 | + * their html names. If this form section itself has a parent, prepends ITS |
|
1343 | + * prefix onto this form section's prefix. Used primarily by |
|
1344 | + * EE_Form_Input_Base::_set_default_html_name_if_empty |
|
1345 | + * |
|
1346 | + * @return string |
|
1347 | + * @throws EE_Error |
|
1348 | + */ |
|
1349 | + public function html_name_prefix() |
|
1350 | + { |
|
1351 | + if ($this->parent_section() instanceof EE_Form_Section_Proper) { |
|
1352 | + return $this->parent_section()->html_name_prefix() . '[' . $this->name() . ']'; |
|
1353 | + } |
|
1354 | + return $this->name(); |
|
1355 | + } |
|
1356 | + |
|
1357 | + |
|
1358 | + /** |
|
1359 | + * Gets the name, but first checks _construct_finalize has been called. If not, |
|
1360 | + * calls it (assumes there is no parent and that we want the name to be whatever |
|
1361 | + * was set, which is probably nothing, or the classname) |
|
1362 | + * |
|
1363 | + * @return string |
|
1364 | + * @throws EE_Error |
|
1365 | + */ |
|
1366 | + public function name() |
|
1367 | + { |
|
1368 | + $this->ensure_construct_finalized_called(); |
|
1369 | + return parent::name(); |
|
1370 | + } |
|
1371 | + |
|
1372 | + |
|
1373 | + /** |
|
1374 | + * @return EE_Form_Section_Proper |
|
1375 | + * @throws EE_Error |
|
1376 | + */ |
|
1377 | + public function parent_section() |
|
1378 | + { |
|
1379 | + $this->ensure_construct_finalized_called(); |
|
1380 | + return parent::parent_section(); |
|
1381 | + } |
|
1382 | + |
|
1383 | + |
|
1384 | + /** |
|
1385 | + * make sure construction finalized was called, otherwise children might not be ready |
|
1386 | + * |
|
1387 | + * @return void |
|
1388 | + * @throws EE_Error |
|
1389 | + */ |
|
1390 | + public function ensure_construct_finalized_called() |
|
1391 | + { |
|
1392 | + if (! $this->_construction_finalized) { |
|
1393 | + $this->_construct_finalize($this->_parent_section, $this->_name); |
|
1394 | + } |
|
1395 | + } |
|
1396 | + |
|
1397 | + |
|
1398 | + /** |
|
1399 | + * Checks if any of this form section's inputs, or any of its children's inputs, |
|
1400 | + * are in teh form data. If any are found, returns true. Else false |
|
1401 | + * |
|
1402 | + * @param array $req_data |
|
1403 | + * @return boolean |
|
1404 | + * @throws EE_Error |
|
1405 | + */ |
|
1406 | + public function form_data_present_in($req_data = null) |
|
1407 | + { |
|
1408 | + $req_data = $this->getCachedRequest($req_data); |
|
1409 | + foreach ($this->subsections() as $subsection) { |
|
1410 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
1411 | + if ($subsection->form_data_present_in($req_data)) { |
|
1412 | + return true; |
|
1413 | + } |
|
1414 | + } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
1415 | + if ($subsection->form_data_present_in($req_data)) { |
|
1416 | + return true; |
|
1417 | + } |
|
1418 | + } |
|
1419 | + } |
|
1420 | + return false; |
|
1421 | + } |
|
1422 | + |
|
1423 | + |
|
1424 | + /** |
|
1425 | + * Gets validation errors for this form section and subsections |
|
1426 | + * Similar to EE_Form_Section_Validatable::get_validation_errors() except this |
|
1427 | + * gets the validation errors for ALL subsection |
|
1428 | + * |
|
1429 | + * @return EE_Validation_Error[] |
|
1430 | + * @throws EE_Error |
|
1431 | + */ |
|
1432 | + public function get_validation_errors_accumulated() |
|
1433 | + { |
|
1434 | + $validation_errors = $this->get_validation_errors(); |
|
1435 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
1436 | + if ($subsection instanceof EE_Form_Section_Proper) { |
|
1437 | + $validation_errors_on_this_subsection = $subsection->get_validation_errors_accumulated(); |
|
1438 | + } else { |
|
1439 | + $validation_errors_on_this_subsection = $subsection->get_validation_errors(); |
|
1440 | + } |
|
1441 | + if ($validation_errors_on_this_subsection) { |
|
1442 | + $validation_errors = array_merge($validation_errors, $validation_errors_on_this_subsection); |
|
1443 | + } |
|
1444 | + } |
|
1445 | + return $validation_errors; |
|
1446 | + } |
|
1447 | + |
|
1448 | + /** |
|
1449 | + * Fetch validation errors from children and grandchildren and puts them in a single string. |
|
1450 | + * This traverses the form section tree to generate this, but you probably want to instead use |
|
1451 | + * get_form_submission_error_message() which is usually this message cached (or a custom validation error message) |
|
1452 | + * |
|
1453 | + * @return string |
|
1454 | + * @since $VID:$ |
|
1455 | + */ |
|
1456 | + protected function getAllValidationErrorsString() |
|
1457 | + { |
|
1458 | + $submission_error_messages = array(); |
|
1459 | + // bad, bad, bad registrant |
|
1460 | + foreach ($this->get_validation_errors_accumulated() as $validation_error) { |
|
1461 | + if ($validation_error instanceof EE_Validation_Error) { |
|
1462 | + $form_section = $validation_error->get_form_section(); |
|
1463 | + if ($form_section instanceof EE_Form_Input_Base) { |
|
1464 | + $label = $validation_error->get_form_section()->html_label_text(); |
|
1465 | + } elseif($form_section instanceof EE_Form_Section_Validatable) { |
|
1466 | + $label = $validation_error->get_form_section()->name(); |
|
1467 | + } else { |
|
1468 | + $label = esc_html__('Unknown', 'event_espresso'); |
|
1469 | + } |
|
1470 | + $submission_error_messages[] = sprintf( |
|
1471 | + __('%s : %s', 'event_espresso'), |
|
1472 | + $label, |
|
1473 | + $validation_error->getMessage() |
|
1474 | + ); |
|
1475 | + } |
|
1476 | + } |
|
1477 | + return implode('<br', $submission_error_messages); |
|
1478 | + } |
|
1479 | + |
|
1480 | + |
|
1481 | + /** |
|
1482 | + * This isn't just the name of an input, it's a path pointing to an input. The |
|
1483 | + * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
1484 | + * dot-dot-slash (../) means to ascend into the parent section. |
|
1485 | + * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
1486 | + * which will be returned. |
|
1487 | + * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
1488 | + * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
1489 | + * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
1490 | + * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
1491 | + * Etc |
|
1492 | + * |
|
1493 | + * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
1494 | + * @return EE_Form_Section_Base |
|
1495 | + * @throws EE_Error |
|
1496 | + */ |
|
1497 | + public function find_section_from_path($form_section_path) |
|
1498 | + { |
|
1499 | + //check if we can find the input from purely going straight up the tree |
|
1500 | + $input = parent::find_section_from_path($form_section_path); |
|
1501 | + if ($input instanceof EE_Form_Section_Base) { |
|
1502 | + return $input; |
|
1503 | + } |
|
1504 | + $next_slash_pos = strpos($form_section_path, '/'); |
|
1505 | + if ($next_slash_pos !== false) { |
|
1506 | + $child_section_name = substr($form_section_path, 0, $next_slash_pos); |
|
1507 | + $subpath = substr($form_section_path, $next_slash_pos + 1); |
|
1508 | + } else { |
|
1509 | + $child_section_name = $form_section_path; |
|
1510 | + $subpath = ''; |
|
1511 | + } |
|
1512 | + $child_section = $this->get_subsection($child_section_name); |
|
1513 | + if ($child_section instanceof EE_Form_Section_Base) { |
|
1514 | + return $child_section->find_section_from_path($subpath); |
|
1515 | + } |
|
1516 | + return null; |
|
1517 | + } |
|
1518 | 1518 | } |
1519 | 1519 |
@@ -24,459 +24,459 @@ |
||
24 | 24 | class Registry |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * @var EE_Template_Config $template_config |
|
29 | - */ |
|
30 | - protected $template_config; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var EE_Currency_Config $currency_config |
|
34 | - */ |
|
35 | - protected $currency_config; |
|
36 | - |
|
37 | - /** |
|
38 | - * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
39 | - * |
|
40 | - * @var array |
|
41 | - */ |
|
42 | - protected $jsdata = array(); |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
47 | - * page source. |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - protected $script_handles_with_data = array(); |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * @var DomainInterface |
|
55 | - */ |
|
56 | - protected $domain; |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * Registry constructor. |
|
61 | - * Hooking into WP actions for script registry. |
|
62 | - * |
|
63 | - * @param EE_Template_Config $template_config |
|
64 | - * @param EE_Currency_Config $currency_config |
|
65 | - * @param DomainInterface $domain |
|
66 | - */ |
|
67 | - public function __construct( |
|
68 | - EE_Template_Config $template_config, |
|
69 | - EE_Currency_Config $currency_config, |
|
70 | - DomainInterface $domain |
|
71 | - ) { |
|
72 | - $this->template_config = $template_config; |
|
73 | - $this->currency_config = $currency_config; |
|
74 | - $this->domain = $domain; |
|
75 | - add_action('wp_enqueue_scripts', array($this, 'scripts'), 1); |
|
76 | - add_action('admin_enqueue_scripts', array($this, 'scripts'), 1); |
|
77 | - add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 2); |
|
78 | - add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 2); |
|
79 | - add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
80 | - add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * Callback for the WP script actions. |
|
87 | - * Used to register globally accessible core scripts. |
|
88 | - * Also used to add the eejs.data object to the source for any js having eejs-core as a dependency. |
|
89 | - */ |
|
90 | - public function scripts() |
|
91 | - { |
|
92 | - global $wp_version; |
|
93 | - wp_register_script( |
|
94 | - 'eejs-core', |
|
95 | - EE_PLUGIN_DIR_URL . 'core/services/assets/core_assets/eejs-core.js', |
|
96 | - array(), |
|
97 | - EVENT_ESPRESSO_VERSION, |
|
98 | - true |
|
99 | - ); |
|
100 | - //only run this if WordPress 4.4.0 > is in use. |
|
101 | - if (version_compare($wp_version, '4.4.0', '>')) { |
|
102 | - //js.api |
|
103 | - wp_register_script( |
|
104 | - 'eejs-api', |
|
105 | - EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js', |
|
106 | - array('underscore', 'eejs-core'), |
|
107 | - EVENT_ESPRESSO_VERSION, |
|
108 | - true |
|
109 | - ); |
|
110 | - $this->jsdata['eejs_api_nonce'] = wp_create_nonce('wp_rest'); |
|
111 | - $this->jsdata['paths'] = array('rest_route' => rest_url('ee/v4.8.36/')); |
|
112 | - } |
|
113 | - if (! is_admin()) { |
|
114 | - $this->loadCoreCss(); |
|
115 | - } |
|
116 | - $this->loadCoreJs(); |
|
117 | - $this->loadJqueryValidate(); |
|
118 | - $this->loadAccountingJs(); |
|
119 | - $this->loadQtipJs(); |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * Call back for the script print in frontend and backend. |
|
126 | - * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
127 | - * |
|
128 | - * @since 4.9.31.rc.015 |
|
129 | - */ |
|
130 | - public function enqueueData() |
|
131 | - { |
|
132 | - $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
133 | - wp_localize_script('eejs-core', 'eejs', array('data' => $this->jsdata)); |
|
134 | - wp_localize_script('espresso_core', 'eei18n', EE_Registry::$i18n_js_strings); |
|
135 | - $this->localizeAccountingJs(); |
|
136 | - $this->addRegisteredScriptHandlesWithData('eejs-core'); |
|
137 | - $this->addRegisteredScriptHandlesWithData('espresso_core'); |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * Used to add data to eejs.data object. |
|
144 | - * Note: Overriding existing data is not allowed. |
|
145 | - * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
146 | - * If the data you add is something like this: |
|
147 | - * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
148 | - * It will be exposed in the page source as: |
|
149 | - * eejs.data.my_plugin_data.foo == gar |
|
150 | - * |
|
151 | - * @param string $key Key used to access your data |
|
152 | - * @param string|array $value Value to attach to key |
|
153 | - * @throws InvalidArgumentException |
|
154 | - */ |
|
155 | - public function addData($key, $value) |
|
156 | - { |
|
157 | - if ($this->verifyDataNotExisting($key)) { |
|
158 | - $this->jsdata[$key] = $value; |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
166 | - * elements in an array. |
|
167 | - * When you use this method, the value you include will be appended to the end of an array on $key. |
|
168 | - * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
|
169 | - * object like this, eejs.data.test = [ my_data, |
|
170 | - * ] |
|
171 | - * If there has already been a scalar value attached to the data object given key, then |
|
172 | - * this will throw an exception. |
|
173 | - * |
|
174 | - * @param string $key Key to attach data to. |
|
175 | - * @param string|array $value Value being registered. |
|
176 | - * @throws InvalidArgumentException |
|
177 | - */ |
|
178 | - public function pushData($key, $value) |
|
179 | - { |
|
180 | - if (isset($this->jsdata[$key]) |
|
181 | - && ! is_array($this->jsdata[$key]) |
|
182 | - ) { |
|
183 | - throw new invalidArgumentException( |
|
184 | - sprintf( |
|
185 | - __( |
|
186 | - 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
27 | + /** |
|
28 | + * @var EE_Template_Config $template_config |
|
29 | + */ |
|
30 | + protected $template_config; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var EE_Currency_Config $currency_config |
|
34 | + */ |
|
35 | + protected $currency_config; |
|
36 | + |
|
37 | + /** |
|
38 | + * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
|
39 | + * |
|
40 | + * @var array |
|
41 | + */ |
|
42 | + protected $jsdata = array(); |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
|
47 | + * page source. |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + protected $script_handles_with_data = array(); |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * @var DomainInterface |
|
55 | + */ |
|
56 | + protected $domain; |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * Registry constructor. |
|
61 | + * Hooking into WP actions for script registry. |
|
62 | + * |
|
63 | + * @param EE_Template_Config $template_config |
|
64 | + * @param EE_Currency_Config $currency_config |
|
65 | + * @param DomainInterface $domain |
|
66 | + */ |
|
67 | + public function __construct( |
|
68 | + EE_Template_Config $template_config, |
|
69 | + EE_Currency_Config $currency_config, |
|
70 | + DomainInterface $domain |
|
71 | + ) { |
|
72 | + $this->template_config = $template_config; |
|
73 | + $this->currency_config = $currency_config; |
|
74 | + $this->domain = $domain; |
|
75 | + add_action('wp_enqueue_scripts', array($this, 'scripts'), 1); |
|
76 | + add_action('admin_enqueue_scripts', array($this, 'scripts'), 1); |
|
77 | + add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 2); |
|
78 | + add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 2); |
|
79 | + add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
80 | + add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * Callback for the WP script actions. |
|
87 | + * Used to register globally accessible core scripts. |
|
88 | + * Also used to add the eejs.data object to the source for any js having eejs-core as a dependency. |
|
89 | + */ |
|
90 | + public function scripts() |
|
91 | + { |
|
92 | + global $wp_version; |
|
93 | + wp_register_script( |
|
94 | + 'eejs-core', |
|
95 | + EE_PLUGIN_DIR_URL . 'core/services/assets/core_assets/eejs-core.js', |
|
96 | + array(), |
|
97 | + EVENT_ESPRESSO_VERSION, |
|
98 | + true |
|
99 | + ); |
|
100 | + //only run this if WordPress 4.4.0 > is in use. |
|
101 | + if (version_compare($wp_version, '4.4.0', '>')) { |
|
102 | + //js.api |
|
103 | + wp_register_script( |
|
104 | + 'eejs-api', |
|
105 | + EE_LIBRARIES_URL . 'rest_api/assets/js/eejs-api.min.js', |
|
106 | + array('underscore', 'eejs-core'), |
|
107 | + EVENT_ESPRESSO_VERSION, |
|
108 | + true |
|
109 | + ); |
|
110 | + $this->jsdata['eejs_api_nonce'] = wp_create_nonce('wp_rest'); |
|
111 | + $this->jsdata['paths'] = array('rest_route' => rest_url('ee/v4.8.36/')); |
|
112 | + } |
|
113 | + if (! is_admin()) { |
|
114 | + $this->loadCoreCss(); |
|
115 | + } |
|
116 | + $this->loadCoreJs(); |
|
117 | + $this->loadJqueryValidate(); |
|
118 | + $this->loadAccountingJs(); |
|
119 | + $this->loadQtipJs(); |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * Call back for the script print in frontend and backend. |
|
126 | + * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
|
127 | + * |
|
128 | + * @since 4.9.31.rc.015 |
|
129 | + */ |
|
130 | + public function enqueueData() |
|
131 | + { |
|
132 | + $this->removeAlreadyRegisteredDataForScriptHandles(); |
|
133 | + wp_localize_script('eejs-core', 'eejs', array('data' => $this->jsdata)); |
|
134 | + wp_localize_script('espresso_core', 'eei18n', EE_Registry::$i18n_js_strings); |
|
135 | + $this->localizeAccountingJs(); |
|
136 | + $this->addRegisteredScriptHandlesWithData('eejs-core'); |
|
137 | + $this->addRegisteredScriptHandlesWithData('espresso_core'); |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * Used to add data to eejs.data object. |
|
144 | + * Note: Overriding existing data is not allowed. |
|
145 | + * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
|
146 | + * If the data you add is something like this: |
|
147 | + * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
|
148 | + * It will be exposed in the page source as: |
|
149 | + * eejs.data.my_plugin_data.foo == gar |
|
150 | + * |
|
151 | + * @param string $key Key used to access your data |
|
152 | + * @param string|array $value Value to attach to key |
|
153 | + * @throws InvalidArgumentException |
|
154 | + */ |
|
155 | + public function addData($key, $value) |
|
156 | + { |
|
157 | + if ($this->verifyDataNotExisting($key)) { |
|
158 | + $this->jsdata[$key] = $value; |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * Similar to addData except this allows for users to push values to an existing key where the values on key are |
|
166 | + * elements in an array. |
|
167 | + * When you use this method, the value you include will be appended to the end of an array on $key. |
|
168 | + * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript |
|
169 | + * object like this, eejs.data.test = [ my_data, |
|
170 | + * ] |
|
171 | + * If there has already been a scalar value attached to the data object given key, then |
|
172 | + * this will throw an exception. |
|
173 | + * |
|
174 | + * @param string $key Key to attach data to. |
|
175 | + * @param string|array $value Value being registered. |
|
176 | + * @throws InvalidArgumentException |
|
177 | + */ |
|
178 | + public function pushData($key, $value) |
|
179 | + { |
|
180 | + if (isset($this->jsdata[$key]) |
|
181 | + && ! is_array($this->jsdata[$key]) |
|
182 | + ) { |
|
183 | + throw new invalidArgumentException( |
|
184 | + sprintf( |
|
185 | + __( |
|
186 | + 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
|
187 | 187 | push values to this data element when it is an array.', |
188 | - 'event_espresso' |
|
189 | - ), |
|
190 | - $key, |
|
191 | - __METHOD__ |
|
192 | - ) |
|
193 | - ); |
|
194 | - } |
|
195 | - $this->jsdata[$key][] = $value; |
|
196 | - } |
|
197 | - |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * Used to set content used by javascript for a template. |
|
202 | - * Note: Overrides of existing registered templates are not allowed. |
|
203 | - * |
|
204 | - * @param string $template_reference |
|
205 | - * @param string $template_content |
|
206 | - * @throws InvalidArgumentException |
|
207 | - */ |
|
208 | - public function addTemplate($template_reference, $template_content) |
|
209 | - { |
|
210 | - if (! isset($this->jsdata['templates'])) { |
|
211 | - $this->jsdata['templates'] = array(); |
|
212 | - } |
|
213 | - //no overrides allowed. |
|
214 | - if (isset($this->jsdata['templates'][$template_reference])) { |
|
215 | - throw new invalidArgumentException( |
|
216 | - sprintf( |
|
217 | - __( |
|
218 | - 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
219 | - 'event_espresso' |
|
220 | - ), |
|
221 | - $template_reference |
|
222 | - ) |
|
223 | - ); |
|
224 | - } |
|
225 | - $this->jsdata['templates'][$template_reference] = $template_content; |
|
226 | - } |
|
227 | - |
|
228 | - |
|
229 | - |
|
230 | - /** |
|
231 | - * Retrieve the template content already registered for the given reference. |
|
232 | - * |
|
233 | - * @param string $template_reference |
|
234 | - * @return string |
|
235 | - */ |
|
236 | - public function getTemplate($template_reference) |
|
237 | - { |
|
238 | - return isset($this->jsdata['templates'], $this->jsdata['templates'][$template_reference]) |
|
239 | - ? $this->jsdata['templates'][$template_reference] |
|
240 | - : ''; |
|
241 | - } |
|
242 | - |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * Retrieve registered data. |
|
247 | - * |
|
248 | - * @param string $key Name of key to attach data to. |
|
249 | - * @return mixed If there is no for the given key, then false is returned. |
|
250 | - */ |
|
251 | - public function getData($key) |
|
252 | - { |
|
253 | - return isset($this->jsdata[$key]) |
|
254 | - ? $this->jsdata[$key] |
|
255 | - : false; |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - |
|
260 | - /** |
|
261 | - * Verifies whether the given data exists already on the jsdata array. |
|
262 | - * Overriding data is not allowed. |
|
263 | - * |
|
264 | - * @param string $key Index for data. |
|
265 | - * @return bool If valid then return true. |
|
266 | - * @throws InvalidArgumentException if data already exists. |
|
267 | - */ |
|
268 | - protected function verifyDataNotExisting($key) |
|
269 | - { |
|
270 | - if (isset($this->jsdata[$key])) { |
|
271 | - if (is_array($this->jsdata[$key])) { |
|
272 | - throw new InvalidArgumentException( |
|
273 | - sprintf( |
|
274 | - __( |
|
275 | - 'The value for %1$s already exists in the Registry::eejs object. |
|
188 | + 'event_espresso' |
|
189 | + ), |
|
190 | + $key, |
|
191 | + __METHOD__ |
|
192 | + ) |
|
193 | + ); |
|
194 | + } |
|
195 | + $this->jsdata[$key][] = $value; |
|
196 | + } |
|
197 | + |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * Used to set content used by javascript for a template. |
|
202 | + * Note: Overrides of existing registered templates are not allowed. |
|
203 | + * |
|
204 | + * @param string $template_reference |
|
205 | + * @param string $template_content |
|
206 | + * @throws InvalidArgumentException |
|
207 | + */ |
|
208 | + public function addTemplate($template_reference, $template_content) |
|
209 | + { |
|
210 | + if (! isset($this->jsdata['templates'])) { |
|
211 | + $this->jsdata['templates'] = array(); |
|
212 | + } |
|
213 | + //no overrides allowed. |
|
214 | + if (isset($this->jsdata['templates'][$template_reference])) { |
|
215 | + throw new invalidArgumentException( |
|
216 | + sprintf( |
|
217 | + __( |
|
218 | + 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
|
219 | + 'event_espresso' |
|
220 | + ), |
|
221 | + $template_reference |
|
222 | + ) |
|
223 | + ); |
|
224 | + } |
|
225 | + $this->jsdata['templates'][$template_reference] = $template_content; |
|
226 | + } |
|
227 | + |
|
228 | + |
|
229 | + |
|
230 | + /** |
|
231 | + * Retrieve the template content already registered for the given reference. |
|
232 | + * |
|
233 | + * @param string $template_reference |
|
234 | + * @return string |
|
235 | + */ |
|
236 | + public function getTemplate($template_reference) |
|
237 | + { |
|
238 | + return isset($this->jsdata['templates'], $this->jsdata['templates'][$template_reference]) |
|
239 | + ? $this->jsdata['templates'][$template_reference] |
|
240 | + : ''; |
|
241 | + } |
|
242 | + |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * Retrieve registered data. |
|
247 | + * |
|
248 | + * @param string $key Name of key to attach data to. |
|
249 | + * @return mixed If there is no for the given key, then false is returned. |
|
250 | + */ |
|
251 | + public function getData($key) |
|
252 | + { |
|
253 | + return isset($this->jsdata[$key]) |
|
254 | + ? $this->jsdata[$key] |
|
255 | + : false; |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + |
|
260 | + /** |
|
261 | + * Verifies whether the given data exists already on the jsdata array. |
|
262 | + * Overriding data is not allowed. |
|
263 | + * |
|
264 | + * @param string $key Index for data. |
|
265 | + * @return bool If valid then return true. |
|
266 | + * @throws InvalidArgumentException if data already exists. |
|
267 | + */ |
|
268 | + protected function verifyDataNotExisting($key) |
|
269 | + { |
|
270 | + if (isset($this->jsdata[$key])) { |
|
271 | + if (is_array($this->jsdata[$key])) { |
|
272 | + throw new InvalidArgumentException( |
|
273 | + sprintf( |
|
274 | + __( |
|
275 | + 'The value for %1$s already exists in the Registry::eejs object. |
|
276 | 276 | Overrides are not allowed. Since the value of this data is an array, you may want to use the |
277 | 277 | %2$s method to push your value to the array.', |
278 | - 'event_espresso' |
|
279 | - ), |
|
280 | - $key, |
|
281 | - 'pushData()' |
|
282 | - ) |
|
283 | - ); |
|
284 | - } |
|
285 | - throw new InvalidArgumentException( |
|
286 | - sprintf( |
|
287 | - __( |
|
288 | - 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
278 | + 'event_espresso' |
|
279 | + ), |
|
280 | + $key, |
|
281 | + 'pushData()' |
|
282 | + ) |
|
283 | + ); |
|
284 | + } |
|
285 | + throw new InvalidArgumentException( |
|
286 | + sprintf( |
|
287 | + __( |
|
288 | + 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
|
289 | 289 | allowed. Consider attaching your value to a different key', |
290 | - 'event_espresso' |
|
291 | - ), |
|
292 | - $key |
|
293 | - ) |
|
294 | - ); |
|
295 | - } |
|
296 | - return true; |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - |
|
301 | - /** |
|
302 | - * registers core default stylesheets |
|
303 | - */ |
|
304 | - private function loadCoreCss() |
|
305 | - { |
|
306 | - if ($this->template_config->enable_default_style) { |
|
307 | - $default_stylesheet_path = is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css') |
|
308 | - ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css' |
|
309 | - : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css'; |
|
310 | - wp_register_style( |
|
311 | - 'espresso_default', |
|
312 | - $default_stylesheet_path, |
|
313 | - array('dashicons'), |
|
314 | - EVENT_ESPRESSO_VERSION |
|
315 | - ); |
|
316 | - //Load custom style sheet if available |
|
317 | - if ($this->template_config->custom_style_sheet !== null) { |
|
318 | - wp_register_style( |
|
319 | - 'espresso_custom_css', |
|
320 | - EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet, |
|
321 | - array('espresso_default'), |
|
322 | - EVENT_ESPRESSO_VERSION |
|
323 | - ); |
|
324 | - } |
|
325 | - } |
|
326 | - } |
|
327 | - |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * registers core default javascript |
|
332 | - */ |
|
333 | - private function loadCoreJs() |
|
334 | - { |
|
335 | - // load core js |
|
336 | - wp_register_script( |
|
337 | - 'espresso_core', |
|
338 | - EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
339 | - array('jquery'), |
|
340 | - EVENT_ESPRESSO_VERSION, |
|
341 | - true |
|
342 | - ); |
|
343 | - } |
|
344 | - |
|
345 | - |
|
346 | - |
|
347 | - /** |
|
348 | - * registers jQuery Validate for form validation |
|
349 | - */ |
|
350 | - private function loadJqueryValidate() |
|
351 | - { |
|
352 | - // register jQuery Validate and additional methods |
|
353 | - wp_register_script( |
|
354 | - 'jquery-validate', |
|
355 | - EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js', |
|
356 | - array('jquery'), |
|
357 | - '1.15.0', |
|
358 | - true |
|
359 | - ); |
|
360 | - wp_register_script( |
|
361 | - 'jquery-validate-extra-methods', |
|
362 | - EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js', |
|
363 | - array('jquery', 'jquery-validate'), |
|
364 | - '1.15.0', |
|
365 | - true |
|
366 | - ); |
|
367 | - } |
|
368 | - |
|
369 | - |
|
370 | - |
|
371 | - /** |
|
372 | - * registers accounting.js for performing client-side calculations |
|
373 | - */ |
|
374 | - private function loadAccountingJs() |
|
375 | - { |
|
376 | - //accounting.js library |
|
377 | - // @link http://josscrowcroft.github.io/accounting.js/ |
|
378 | - wp_register_script( |
|
379 | - 'ee-accounting-core', |
|
380 | - EE_THIRD_PARTY_URL . 'accounting/accounting.js', |
|
381 | - array('underscore'), |
|
382 | - '0.3.2', |
|
383 | - true |
|
384 | - ); |
|
385 | - wp_register_script( |
|
386 | - 'ee-accounting', |
|
387 | - EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js', |
|
388 | - array('ee-accounting-core'), |
|
389 | - EVENT_ESPRESSO_VERSION, |
|
390 | - true |
|
391 | - ); |
|
392 | - } |
|
393 | - |
|
394 | - |
|
395 | - |
|
396 | - /** |
|
397 | - * registers accounting.js for performing client-side calculations |
|
398 | - */ |
|
399 | - private function localizeAccountingJs() |
|
400 | - { |
|
401 | - wp_localize_script( |
|
402 | - 'ee-accounting', |
|
403 | - 'EE_ACCOUNTING_CFG', |
|
404 | - array( |
|
405 | - 'currency' => array( |
|
406 | - 'symbol' => $this->currency_config->sign, |
|
407 | - 'format' => array( |
|
408 | - 'pos' => $this->currency_config->sign_b4 ? '%s%v' : '%v%s', |
|
409 | - 'neg' => $this->currency_config->sign_b4 ? '- %s%v' : '- %v%s', |
|
410 | - 'zero' => $this->currency_config->sign_b4 ? '%s--' : '--%s', |
|
411 | - ), |
|
412 | - 'decimal' => $this->currency_config->dec_mrk, |
|
413 | - 'thousand' => $this->currency_config->thsnds, |
|
414 | - 'precision' => $this->currency_config->dec_plc, |
|
415 | - ), |
|
416 | - 'number' => array( |
|
417 | - 'precision' => $this->currency_config->dec_plc, |
|
418 | - 'thousand' => $this->currency_config->thsnds, |
|
419 | - 'decimal' => $this->currency_config->dec_mrk, |
|
420 | - ), |
|
421 | - ) |
|
422 | - ); |
|
423 | - $this->addRegisteredScriptHandlesWithData('ee-accounting'); |
|
424 | - } |
|
425 | - |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * registers assets for cleaning your ears |
|
430 | - */ |
|
431 | - private function loadQtipJs() |
|
432 | - { |
|
433 | - // qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook, |
|
434 | - // can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' ); |
|
435 | - if (apply_filters('FHEE_load_qtip', false)) { |
|
436 | - EEH_Qtip_Loader::instance()->register_and_enqueue(); |
|
437 | - } |
|
438 | - } |
|
439 | - |
|
440 | - |
|
441 | - /** |
|
442 | - * This is used to set registered script handles that have data. |
|
443 | - * @param string $script_handle |
|
444 | - */ |
|
445 | - private function addRegisteredScriptHandlesWithData($script_handle) |
|
446 | - { |
|
447 | - $this->script_handles_with_data[$script_handle] = $script_handle; |
|
448 | - } |
|
449 | - |
|
450 | - |
|
451 | - /** |
|
452 | - * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
|
453 | - * Dependency stored in WP_Scripts if its set. |
|
454 | - */ |
|
455 | - private function removeAlreadyRegisteredDataForScriptHandles() |
|
456 | - { |
|
457 | - if (empty($this->script_handles_with_data)) { |
|
458 | - return; |
|
459 | - } |
|
460 | - foreach ($this->script_handles_with_data as $script_handle) { |
|
461 | - $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
462 | - } |
|
463 | - } |
|
464 | - |
|
465 | - |
|
466 | - /** |
|
467 | - * Removes any data dependency registered in WP_Scripts if its set. |
|
468 | - * @param string $script_handle |
|
469 | - */ |
|
470 | - private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
471 | - { |
|
472 | - if (isset($this->script_handles_with_data[$script_handle])) { |
|
473 | - global $wp_scripts; |
|
474 | - if ($wp_scripts->get_data($script_handle, 'data')) { |
|
475 | - unset($wp_scripts->registered[$script_handle]->extra['data']); |
|
476 | - unset($this->script_handles_with_data[$script_handle]); |
|
477 | - } |
|
478 | - } |
|
479 | - } |
|
290 | + 'event_espresso' |
|
291 | + ), |
|
292 | + $key |
|
293 | + ) |
|
294 | + ); |
|
295 | + } |
|
296 | + return true; |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + |
|
301 | + /** |
|
302 | + * registers core default stylesheets |
|
303 | + */ |
|
304 | + private function loadCoreCss() |
|
305 | + { |
|
306 | + if ($this->template_config->enable_default_style) { |
|
307 | + $default_stylesheet_path = is_readable(EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css') |
|
308 | + ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css' |
|
309 | + : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css'; |
|
310 | + wp_register_style( |
|
311 | + 'espresso_default', |
|
312 | + $default_stylesheet_path, |
|
313 | + array('dashicons'), |
|
314 | + EVENT_ESPRESSO_VERSION |
|
315 | + ); |
|
316 | + //Load custom style sheet if available |
|
317 | + if ($this->template_config->custom_style_sheet !== null) { |
|
318 | + wp_register_style( |
|
319 | + 'espresso_custom_css', |
|
320 | + EVENT_ESPRESSO_UPLOAD_URL . 'css/' . $this->template_config->custom_style_sheet, |
|
321 | + array('espresso_default'), |
|
322 | + EVENT_ESPRESSO_VERSION |
|
323 | + ); |
|
324 | + } |
|
325 | + } |
|
326 | + } |
|
327 | + |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * registers core default javascript |
|
332 | + */ |
|
333 | + private function loadCoreJs() |
|
334 | + { |
|
335 | + // load core js |
|
336 | + wp_register_script( |
|
337 | + 'espresso_core', |
|
338 | + EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
339 | + array('jquery'), |
|
340 | + EVENT_ESPRESSO_VERSION, |
|
341 | + true |
|
342 | + ); |
|
343 | + } |
|
344 | + |
|
345 | + |
|
346 | + |
|
347 | + /** |
|
348 | + * registers jQuery Validate for form validation |
|
349 | + */ |
|
350 | + private function loadJqueryValidate() |
|
351 | + { |
|
352 | + // register jQuery Validate and additional methods |
|
353 | + wp_register_script( |
|
354 | + 'jquery-validate', |
|
355 | + EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js', |
|
356 | + array('jquery'), |
|
357 | + '1.15.0', |
|
358 | + true |
|
359 | + ); |
|
360 | + wp_register_script( |
|
361 | + 'jquery-validate-extra-methods', |
|
362 | + EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.additional-methods.min.js', |
|
363 | + array('jquery', 'jquery-validate'), |
|
364 | + '1.15.0', |
|
365 | + true |
|
366 | + ); |
|
367 | + } |
|
368 | + |
|
369 | + |
|
370 | + |
|
371 | + /** |
|
372 | + * registers accounting.js for performing client-side calculations |
|
373 | + */ |
|
374 | + private function loadAccountingJs() |
|
375 | + { |
|
376 | + //accounting.js library |
|
377 | + // @link http://josscrowcroft.github.io/accounting.js/ |
|
378 | + wp_register_script( |
|
379 | + 'ee-accounting-core', |
|
380 | + EE_THIRD_PARTY_URL . 'accounting/accounting.js', |
|
381 | + array('underscore'), |
|
382 | + '0.3.2', |
|
383 | + true |
|
384 | + ); |
|
385 | + wp_register_script( |
|
386 | + 'ee-accounting', |
|
387 | + EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js', |
|
388 | + array('ee-accounting-core'), |
|
389 | + EVENT_ESPRESSO_VERSION, |
|
390 | + true |
|
391 | + ); |
|
392 | + } |
|
393 | + |
|
394 | + |
|
395 | + |
|
396 | + /** |
|
397 | + * registers accounting.js for performing client-side calculations |
|
398 | + */ |
|
399 | + private function localizeAccountingJs() |
|
400 | + { |
|
401 | + wp_localize_script( |
|
402 | + 'ee-accounting', |
|
403 | + 'EE_ACCOUNTING_CFG', |
|
404 | + array( |
|
405 | + 'currency' => array( |
|
406 | + 'symbol' => $this->currency_config->sign, |
|
407 | + 'format' => array( |
|
408 | + 'pos' => $this->currency_config->sign_b4 ? '%s%v' : '%v%s', |
|
409 | + 'neg' => $this->currency_config->sign_b4 ? '- %s%v' : '- %v%s', |
|
410 | + 'zero' => $this->currency_config->sign_b4 ? '%s--' : '--%s', |
|
411 | + ), |
|
412 | + 'decimal' => $this->currency_config->dec_mrk, |
|
413 | + 'thousand' => $this->currency_config->thsnds, |
|
414 | + 'precision' => $this->currency_config->dec_plc, |
|
415 | + ), |
|
416 | + 'number' => array( |
|
417 | + 'precision' => $this->currency_config->dec_plc, |
|
418 | + 'thousand' => $this->currency_config->thsnds, |
|
419 | + 'decimal' => $this->currency_config->dec_mrk, |
|
420 | + ), |
|
421 | + ) |
|
422 | + ); |
|
423 | + $this->addRegisteredScriptHandlesWithData('ee-accounting'); |
|
424 | + } |
|
425 | + |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * registers assets for cleaning your ears |
|
430 | + */ |
|
431 | + private function loadQtipJs() |
|
432 | + { |
|
433 | + // qtip is turned OFF by default, but prior to the wp_enqueue_scripts hook, |
|
434 | + // can be turned back on again via: add_filter('FHEE_load_qtip', '__return_true' ); |
|
435 | + if (apply_filters('FHEE_load_qtip', false)) { |
|
436 | + EEH_Qtip_Loader::instance()->register_and_enqueue(); |
|
437 | + } |
|
438 | + } |
|
439 | + |
|
440 | + |
|
441 | + /** |
|
442 | + * This is used to set registered script handles that have data. |
|
443 | + * @param string $script_handle |
|
444 | + */ |
|
445 | + private function addRegisteredScriptHandlesWithData($script_handle) |
|
446 | + { |
|
447 | + $this->script_handles_with_data[$script_handle] = $script_handle; |
|
448 | + } |
|
449 | + |
|
450 | + |
|
451 | + /** |
|
452 | + * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
|
453 | + * Dependency stored in WP_Scripts if its set. |
|
454 | + */ |
|
455 | + private function removeAlreadyRegisteredDataForScriptHandles() |
|
456 | + { |
|
457 | + if (empty($this->script_handles_with_data)) { |
|
458 | + return; |
|
459 | + } |
|
460 | + foreach ($this->script_handles_with_data as $script_handle) { |
|
461 | + $this->removeAlreadyRegisteredDataForScriptHandle($script_handle); |
|
462 | + } |
|
463 | + } |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * Removes any data dependency registered in WP_Scripts if its set. |
|
468 | + * @param string $script_handle |
|
469 | + */ |
|
470 | + private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
|
471 | + { |
|
472 | + if (isset($this->script_handles_with_data[$script_handle])) { |
|
473 | + global $wp_scripts; |
|
474 | + if ($wp_scripts->get_data($script_handle, 'data')) { |
|
475 | + unset($wp_scripts->registered[$script_handle]->extra['data']); |
|
476 | + unset($this->script_handles_with_data[$script_handle]); |
|
477 | + } |
|
478 | + } |
|
479 | + } |
|
480 | 480 | |
481 | 481 | |
482 | 482 | } |