@@ -20,8 +20,8 @@ |
||
20 | 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 | 21 | * (namely, that we might send too many characters to PayPal; however in this case they just issue a warning but nothing breaks) |
22 | 22 | * @param $string |
23 | - * @param $start |
|
24 | - * @param $length |
|
23 | + * @param integer $start |
|
24 | + * @param integer $length |
|
25 | 25 | * @return bool|string |
26 | 26 | */ |
27 | 27 | function mb_strcut($string, $start, $length = null) |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | * ---------------------------------------------- |
11 | 11 | */ |
12 | 12 | // Quickfix to address https://events.codebasehq.com/projects/event-espresso/tickets/11089 ASAP |
13 | -if (! function_exists('mb_strcut')) { |
|
13 | +if ( ! function_exists('mb_strcut')) { |
|
14 | 14 | /** |
15 | 15 | * Very simple mimic of mb_substr (which WP ensures exists in wp-includes/compat.php). Still has all the problems of mb_substr |
16 | 16 | * (namely, that we might send too many characters to PayPal; however in this case they just issue a warning but nothing breaks) |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | $notify_url = null, |
142 | 142 | $cancel_url = null |
143 | 143 | ) { |
144 | - if (! $payment instanceof EEI_Payment) { |
|
144 | + if ( ! $payment instanceof EEI_Payment) { |
|
145 | 145 | $payment->set_gateway_response( |
146 | 146 | esc_html__( |
147 | 147 | 'Error. No associated payment was found.', |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | return $payment; |
153 | 153 | } |
154 | 154 | $transaction = $payment->transaction(); |
155 | - if (! $transaction instanceof EEI_Transaction) { |
|
155 | + if ( ! $transaction instanceof EEI_Transaction) { |
|
156 | 156 | $payment->set_gateway_response( |
157 | 157 | esc_html__( |
158 | 158 | 'Could not process this payment because it has no associated transaction.', |
@@ -199,13 +199,13 @@ discard block |
||
199 | 199 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
200 | 200 | $token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
201 | 201 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
202 | - } elseif (! $this->_request_shipping_addr) { |
|
202 | + } elseif ( ! $this->_request_shipping_addr) { |
|
203 | 203 | // Do not request shipping details on the PP Checkout page. |
204 | 204 | $token_request_dtls['NOSHIPPING'] = '1'; |
205 | 205 | $token_request_dtls['REQCONFIRMSHIPPING'] = '0'; |
206 | 206 | } |
207 | 207 | // Used a business/personal logo on the PayPal page. |
208 | - if (! empty($this->_image_url)) { |
|
208 | + if ( ! empty($this->_image_url)) { |
|
209 | 209 | $token_request_dtls['LOGOIMG'] = $this->_image_url; |
210 | 210 | } |
211 | 211 | $token_request_dtls = apply_filters( |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | ); |
231 | 231 | } else { |
232 | 232 | if (isset($response_args['L_ERRORCODE'])) { |
233 | - $payment->set_gateway_response($response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE']); |
|
233 | + $payment->set_gateway_response($response_args['L_ERRORCODE'].'; '.$response_args['L_SHORTMESSAGE']); |
|
234 | 234 | } else { |
235 | 235 | $payment->set_gateway_response( |
236 | 236 | esc_html__( |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | if ($payment instanceof EEI_Payment) { |
262 | 262 | $this->log(array('Return from Authorization' => $update_info), $payment); |
263 | 263 | $transaction = $payment->transaction(); |
264 | - if (! $transaction instanceof EEI_Transaction) { |
|
264 | + if ( ! $transaction instanceof EEI_Transaction) { |
|
265 | 265 | $payment->set_gateway_response( |
266 | 266 | esc_html__( |
267 | 267 | 'Could not process this payment because it has no associated transaction.', |
@@ -274,7 +274,7 @@ discard block |
||
274 | 274 | $primary_registrant = $transaction->primary_registration(); |
275 | 275 | $payment_details = $payment->details(); |
276 | 276 | // Check if we still have the token. |
277 | - if (! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN'])) { |
|
277 | + if ( ! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN'])) { |
|
278 | 278 | $payment->set_status($this->_pay_model->failed_status()); |
279 | 279 | return $payment; |
280 | 280 | } |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | $itemized_list = array(); |
400 | 400 | $gateway_formatter = $this->_get_gateway_formatter(); |
401 | 401 | // If we have data from a previous communication with PP (on this transaction) we may use that for our list... |
402 | - if (! empty($request_response_args) |
|
402 | + if ( ! empty($request_response_args) |
|
403 | 403 | && array_key_exists('L_PAYMENTREQUEST_0_AMT0', $request_response_args) |
404 | 404 | && array_key_exists('PAYMENTREQUEST_0_ITEMAMT', $request_response_args) |
405 | 405 | ) { |
@@ -407,7 +407,7 @@ discard block |
||
407 | 407 | if (strpos($arg_key, 'PAYMENTREQUEST_') !== false |
408 | 408 | && strpos($arg_key, 'NOTIFYURL') === false |
409 | 409 | ) { |
410 | - $itemized_list[ $arg_key ] = $arg_val; |
|
410 | + $itemized_list[$arg_key] = $arg_val; |
|
411 | 411 | } |
412 | 412 | } |
413 | 413 | // If we got only a few Items then something is not right. |
@@ -460,25 +460,25 @@ discard block |
||
460 | 460 | $line_item_quantity = 1; |
461 | 461 | } |
462 | 462 | // Item Name. |
463 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
463 | + $itemized_list['L_PAYMENTREQUEST_0_NAME'.$item_num] = mb_strcut( |
|
464 | 464 | $gateway_formatter->formatLineItemName($line_item, $payment), |
465 | 465 | 0, |
466 | 466 | 127 |
467 | 467 | ); |
468 | 468 | // Item description. |
469 | - $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = mb_strcut( |
|
469 | + $itemized_list['L_PAYMENTREQUEST_0_DESC'.$item_num] = mb_strcut( |
|
470 | 470 | $gateway_formatter->formatLineItemDesc($line_item, $payment), |
471 | 471 | 0, |
472 | 472 | 127 |
473 | 473 | ); |
474 | 474 | // Cost of individual item. |
475 | - $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency($unit_price); |
|
475 | + $itemized_list['L_PAYMENTREQUEST_0_AMT'.$item_num] = $gateway_formatter->formatCurrency($unit_price); |
|
476 | 476 | // Item Number. |
477 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
477 | + $itemized_list['L_PAYMENTREQUEST_0_NUMBER'.$item_num] = $item_num + 1; |
|
478 | 478 | // Item quantity. |
479 | - $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = $line_item_quantity; |
|
479 | + $itemized_list['L_PAYMENTREQUEST_0_QTY'.$item_num] = $line_item_quantity; |
|
480 | 480 | // Digital item is sold. |
481 | - $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
481 | + $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY'.$item_num] = 'Physical'; |
|
482 | 482 | $itemized_sum += $line_item->total(); |
483 | 483 | ++$item_num; |
484 | 484 | } |
@@ -496,7 +496,7 @@ discard block |
||
496 | 496 | // add the difference as an extra line item. |
497 | 497 | if ($this->_money->compare_floats($itemized_sum_diff_from_txn_total, 0, '!=')) { |
498 | 498 | // Item Name. |
499 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
499 | + $itemized_list['L_PAYMENTREQUEST_0_NAME'.$item_num] = mb_strcut( |
|
500 | 500 | esc_html__( |
501 | 501 | 'Other (promotion/surcharge/cancellation)', |
502 | 502 | 'event_espresso' |
@@ -505,17 +505,17 @@ discard block |
||
505 | 505 | 127 |
506 | 506 | ); |
507 | 507 | // Item description. |
508 | - $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = ''; |
|
508 | + $itemized_list['L_PAYMENTREQUEST_0_DESC'.$item_num] = ''; |
|
509 | 509 | // Cost of individual item. |
510 | - $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency( |
|
510 | + $itemized_list['L_PAYMENTREQUEST_0_AMT'.$item_num] = $gateway_formatter->formatCurrency( |
|
511 | 511 | $itemized_sum_diff_from_txn_total |
512 | 512 | ); |
513 | 513 | // Item Number. |
514 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
514 | + $itemized_list['L_PAYMENTREQUEST_0_NUMBER'.$item_num] = $item_num + 1; |
|
515 | 515 | // Item quantity. |
516 | - $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = 1; |
|
516 | + $itemized_list['L_PAYMENTREQUEST_0_QTY'.$item_num] = 1; |
|
517 | 517 | // Digital item is sold. |
518 | - $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
518 | + $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY'.$item_num] = 'Physical'; |
|
519 | 519 | $item_num++; |
520 | 520 | } |
521 | 521 | } else { |
@@ -570,7 +570,7 @@ discard block |
||
570 | 570 | 'BUTTONSOURCE' => 'EventEspresso_SP', |
571 | 571 | ); |
572 | 572 | $dtls = array_merge($request_dtls, $request_params); |
573 | - $this->_log_clean_request($dtls, $payment, $request_text . ' Request'); |
|
573 | + $this->_log_clean_request($dtls, $payment, $request_text.' Request'); |
|
574 | 574 | // Request Customer Details. |
575 | 575 | $request_response = wp_remote_post( |
576 | 576 | $this->_base_gateway_url, |
@@ -584,7 +584,7 @@ discard block |
||
584 | 584 | ) |
585 | 585 | ); |
586 | 586 | // Log the response. |
587 | - $this->log(array($request_text . ' Response' => $request_response), $payment); |
|
587 | + $this->log(array($request_text.' Response' => $request_response), $payment); |
|
588 | 588 | return $request_response; |
589 | 589 | } |
590 | 590 | |
@@ -604,7 +604,7 @@ discard block |
||
604 | 604 | } |
605 | 605 | $response_args = array(); |
606 | 606 | parse_str(urldecode($request_response['body']), $response_args); |
607 | - if (! isset($response_args['ACK'])) { |
|
607 | + if ( ! isset($response_args['ACK'])) { |
|
608 | 608 | return array('status' => false, 'args' => $request_response); |
609 | 609 | } |
610 | 610 | if (( |
@@ -651,18 +651,18 @@ discard block |
||
651 | 651 | { |
652 | 652 | $errors = array(); |
653 | 653 | $n = 0; |
654 | - while (isset($data_array[ "L_ERRORCODE{$n}" ])) { |
|
655 | - $l_error_code = isset($data_array[ "L_ERRORCODE{$n}" ]) |
|
656 | - ? $data_array[ "L_ERRORCODE{$n}" ] |
|
654 | + while (isset($data_array["L_ERRORCODE{$n}"])) { |
|
655 | + $l_error_code = isset($data_array["L_ERRORCODE{$n}"]) |
|
656 | + ? $data_array["L_ERRORCODE{$n}"] |
|
657 | 657 | : ''; |
658 | - $l_severity_code = isset($data_array[ "L_SEVERITYCODE{$n}" ]) |
|
659 | - ? $data_array[ "L_SEVERITYCODE{$n}" ] |
|
658 | + $l_severity_code = isset($data_array["L_SEVERITYCODE{$n}"]) |
|
659 | + ? $data_array["L_SEVERITYCODE{$n}"] |
|
660 | 660 | : ''; |
661 | - $l_short_message = isset($data_array[ "L_SHORTMESSAGE{$n}" ]) |
|
662 | - ? $data_array[ "L_SHORTMESSAGE{$n}" ] |
|
661 | + $l_short_message = isset($data_array["L_SHORTMESSAGE{$n}"]) |
|
662 | + ? $data_array["L_SHORTMESSAGE{$n}"] |
|
663 | 663 | : ''; |
664 | - $l_long_message = isset($data_array[ "L_LONGMESSAGE{$n}" ]) |
|
665 | - ? $data_array[ "L_LONGMESSAGE{$n}" ] |
|
664 | + $l_long_message = isset($data_array["L_LONGMESSAGE{$n}"]) |
|
665 | + ? $data_array["L_LONGMESSAGE{$n}"] |
|
666 | 666 | : ''; |
667 | 667 | if ($n === 0) { |
668 | 668 | $errors = array( |
@@ -672,10 +672,10 @@ discard block |
||
672 | 672 | 'L_SEVERITYCODE' => $l_severity_code, |
673 | 673 | ); |
674 | 674 | } else { |
675 | - $errors['L_ERRORCODE'] .= ', ' . $l_error_code; |
|
676 | - $errors['L_SHORTMESSAGE'] .= ', ' . $l_short_message; |
|
677 | - $errors['L_LONGMESSAGE'] .= ', ' . $l_long_message; |
|
678 | - $errors['L_SEVERITYCODE'] .= ', ' . $l_severity_code; |
|
675 | + $errors['L_ERRORCODE'] .= ', '.$l_error_code; |
|
676 | + $errors['L_SHORTMESSAGE'] .= ', '.$l_short_message; |
|
677 | + $errors['L_LONGMESSAGE'] .= ', '.$l_long_message; |
|
678 | + $errors['L_SEVERITYCODE'] .= ', '.$l_severity_code; |
|
679 | 679 | } |
680 | 680 | $n++; |
681 | 681 | } |
@@ -11,674 +11,674 @@ |
||
11 | 11 | */ |
12 | 12 | // Quickfix to address https://events.codebasehq.com/projects/event-espresso/tickets/11089 ASAP |
13 | 13 | if (! function_exists('mb_strcut')) { |
14 | - /** |
|
15 | - * Very simple mimic of mb_substr (which WP ensures exists in wp-includes/compat.php). Still has all the problems of mb_substr |
|
16 | - * (namely, that we might send too many characters to PayPal; however in this case they just issue a warning but nothing breaks) |
|
17 | - * @param $string |
|
18 | - * @param $start |
|
19 | - * @param $length |
|
20 | - * @return bool|string |
|
21 | - */ |
|
22 | - function mb_strcut($string, $start, $length = null) |
|
23 | - { |
|
24 | - return mb_substr($string, $start, $length); |
|
25 | - } |
|
14 | + /** |
|
15 | + * Very simple mimic of mb_substr (which WP ensures exists in wp-includes/compat.php). Still has all the problems of mb_substr |
|
16 | + * (namely, that we might send too many characters to PayPal; however in this case they just issue a warning but nothing breaks) |
|
17 | + * @param $string |
|
18 | + * @param $start |
|
19 | + * @param $length |
|
20 | + * @return bool|string |
|
21 | + */ |
|
22 | + function mb_strcut($string, $start, $length = null) |
|
23 | + { |
|
24 | + return mb_substr($string, $start, $length); |
|
25 | + } |
|
26 | 26 | } |
27 | 27 | class EEG_Paypal_Express extends EE_Offsite_Gateway |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Merchant API Username. |
|
32 | - * |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - protected $_api_username; |
|
36 | - |
|
37 | - /** |
|
38 | - * Merchant API Password. |
|
39 | - * |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected $_api_password; |
|
43 | - |
|
44 | - /** |
|
45 | - * API Signature. |
|
46 | - * |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - protected $_api_signature; |
|
50 | - |
|
51 | - /** |
|
52 | - * Request Shipping address on PP checkout page. |
|
53 | - * |
|
54 | - * @var string |
|
55 | - */ |
|
56 | - protected $_request_shipping_addr; |
|
57 | - |
|
58 | - /** |
|
59 | - * Business/personal logo. |
|
60 | - * |
|
61 | - * @var string |
|
62 | - */ |
|
63 | - protected $_image_url; |
|
64 | - |
|
65 | - /** |
|
66 | - * gateway URL variable |
|
67 | - * |
|
68 | - * @var string |
|
69 | - */ |
|
70 | - protected $_base_gateway_url = ''; |
|
71 | - |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * EEG_Paypal_Express constructor. |
|
76 | - */ |
|
77 | - public function __construct() |
|
78 | - { |
|
79 | - $this->_currencies_supported = array( |
|
80 | - 'USD', |
|
81 | - 'AUD', |
|
82 | - 'BRL', |
|
83 | - 'CAD', |
|
84 | - 'CZK', |
|
85 | - 'DKK', |
|
86 | - 'EUR', |
|
87 | - 'HKD', |
|
88 | - 'HUF', |
|
89 | - 'ILS', |
|
90 | - 'JPY', |
|
91 | - 'MYR', |
|
92 | - 'MXN', |
|
93 | - 'NOK', |
|
94 | - 'NZD', |
|
95 | - 'PHP', |
|
96 | - 'PLN', |
|
97 | - 'GBP', |
|
98 | - 'RUB', |
|
99 | - 'SGD', |
|
100 | - 'SEK', |
|
101 | - 'CHF', |
|
102 | - 'TWD', |
|
103 | - 'THB', |
|
104 | - 'TRY', |
|
105 | - 'INR', |
|
106 | - ); |
|
107 | - parent::__construct(); |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * Sets the gateway URL variable based on whether debug mode is enabled or not. |
|
114 | - * |
|
115 | - * @param array $settings_array |
|
116 | - */ |
|
117 | - public function set_settings($settings_array) |
|
118 | - { |
|
119 | - parent::set_settings($settings_array); |
|
120 | - // Redirect URL. |
|
121 | - $this->_base_gateway_url = $this->_debug_mode |
|
122 | - ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
123 | - : 'https://api-3t.paypal.com/nvp'; |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * @param EEI_Payment $payment |
|
130 | - * @param array $billing_info |
|
131 | - * @param string $return_url |
|
132 | - * @param string $notify_url |
|
133 | - * @param string $cancel_url |
|
134 | - * @return \EE_Payment|\EEI_Payment |
|
135 | - * @throws \EE_Error |
|
136 | - */ |
|
137 | - public function set_redirection_info( |
|
138 | - $payment, |
|
139 | - $billing_info = array(), |
|
140 | - $return_url = null, |
|
141 | - $notify_url = null, |
|
142 | - $cancel_url = null |
|
143 | - ) { |
|
144 | - if (! $payment instanceof EEI_Payment) { |
|
145 | - $payment->set_gateway_response( |
|
146 | - esc_html__( |
|
147 | - 'Error. No associated payment was found.', |
|
148 | - 'event_espresso' |
|
149 | - ) |
|
150 | - ); |
|
151 | - $payment->set_status($this->_pay_model->failed_status()); |
|
152 | - return $payment; |
|
153 | - } |
|
154 | - $transaction = $payment->transaction(); |
|
155 | - if (! $transaction instanceof EEI_Transaction) { |
|
156 | - $payment->set_gateway_response( |
|
157 | - esc_html__( |
|
158 | - 'Could not process this payment because it has no associated transaction.', |
|
159 | - 'event_espresso' |
|
160 | - ) |
|
161 | - ); |
|
162 | - $payment->set_status($this->_pay_model->failed_status()); |
|
163 | - return $payment; |
|
164 | - } |
|
165 | - $gateway_formatter = $this->_get_gateway_formatter(); |
|
166 | - $order_description = mb_strcut($gateway_formatter->formatOrderDescription($payment), 0, 127); |
|
167 | - $primary_registration = $transaction->primary_registration(); |
|
168 | - $primary_attendee = $primary_registration instanceof EE_Registration |
|
169 | - ? $primary_registration->attendee() |
|
170 | - : false; |
|
171 | - $locale = explode('-', get_bloginfo('language')); |
|
172 | - // Gather request parameters. |
|
173 | - $token_request_dtls = array( |
|
174 | - 'METHOD' => 'SetExpressCheckout', |
|
175 | - 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
176 | - 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
177 | - 'PAYMENTREQUEST_0_DESC' => $order_description, |
|
178 | - 'RETURNURL' => $return_url, |
|
179 | - 'CANCELURL' => $cancel_url, |
|
180 | - 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
181 | - // Buyer does not need to create a PayPal account to check out. |
|
182 | - // This is referred to as PayPal Account Optional. |
|
183 | - 'SOLUTIONTYPE' => 'Sole', |
|
184 | - // Locale of the pages displayed by PayPal during Express Checkout. |
|
185 | - 'LOCALECODE' => $locale[1] |
|
186 | - ); |
|
187 | - // Show itemized list. |
|
188 | - $itemized_list = $this->itemize_list($payment, $transaction); |
|
189 | - $token_request_dtls = array_merge($token_request_dtls, $itemized_list); |
|
190 | - // Automatically filling out shipping and contact information. |
|
191 | - if ($this->_request_shipping_addr && $primary_attendee instanceof EEI_Attendee) { |
|
192 | - // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
|
193 | - $token_request_dtls['NOSHIPPING'] = '2'; |
|
194 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET'] = $primary_attendee->address(); |
|
195 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $primary_attendee->address2(); |
|
196 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCITY'] = $primary_attendee->city(); |
|
197 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTATE'] = $primary_attendee->state_abbrev(); |
|
198 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $primary_attendee->country_ID(); |
|
199 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
|
200 | - $token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
|
201 | - $token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
|
202 | - } elseif (! $this->_request_shipping_addr) { |
|
203 | - // Do not request shipping details on the PP Checkout page. |
|
204 | - $token_request_dtls['NOSHIPPING'] = '1'; |
|
205 | - $token_request_dtls['REQCONFIRMSHIPPING'] = '0'; |
|
206 | - } |
|
207 | - // Used a business/personal logo on the PayPal page. |
|
208 | - if (! empty($this->_image_url)) { |
|
209 | - $token_request_dtls['LOGOIMG'] = $this->_image_url; |
|
210 | - } |
|
211 | - $token_request_dtls = apply_filters( |
|
212 | - 'FHEE__EEG_Paypal_Express__set_redirection_info__arguments', |
|
213 | - $token_request_dtls, |
|
214 | - $this |
|
215 | - ); |
|
216 | - // Request PayPal token. |
|
217 | - $token_request_response = $this->_ppExpress_request($token_request_dtls, 'Payment Token', $payment); |
|
218 | - $token_rstatus = $this->_ppExpress_check_response($token_request_response); |
|
219 | - $response_args = (isset($token_rstatus['args']) && is_array($token_rstatus['args'])) |
|
220 | - ? $token_rstatus['args'] |
|
221 | - : array(); |
|
222 | - if ($token_rstatus['status']) { |
|
223 | - // We got the Token so we may continue with the payment and redirect the client. |
|
224 | - $payment->set_details($response_args); |
|
225 | - $gateway_url = $this->_debug_mode ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; |
|
226 | - $payment->set_redirect_url( |
|
227 | - $gateway_url |
|
228 | - . '/checkoutnow?useraction=commit&cmd=_express-checkout&token=' |
|
229 | - . $response_args['TOKEN'] |
|
230 | - ); |
|
231 | - } else { |
|
232 | - if (isset($response_args['L_ERRORCODE'])) { |
|
233 | - $payment->set_gateway_response($response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE']); |
|
234 | - } else { |
|
235 | - $payment->set_gateway_response( |
|
236 | - esc_html__( |
|
237 | - 'Error occurred while trying to setup the Express Checkout.', |
|
238 | - 'event_espresso' |
|
239 | - ) |
|
240 | - ); |
|
241 | - } |
|
242 | - $payment->set_details($response_args); |
|
243 | - $payment->set_status($this->_pay_model->failed_status()); |
|
244 | - } |
|
245 | - return $payment; |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * @param array $update_info { |
|
252 | - * @type string $gateway_txn_id |
|
253 | - * @type string status an EEMI_Payment status |
|
254 | - * } |
|
255 | - * @param EEI_Transaction $transaction |
|
256 | - * @return EEI_Payment |
|
257 | - */ |
|
258 | - public function handle_payment_update($update_info, $transaction) |
|
259 | - { |
|
260 | - $payment = $transaction instanceof EEI_Transaction ? $transaction->last_payment() : null; |
|
261 | - if ($payment instanceof EEI_Payment) { |
|
262 | - $this->log(array('Return from Authorization' => $update_info), $payment); |
|
263 | - $transaction = $payment->transaction(); |
|
264 | - if (! $transaction instanceof EEI_Transaction) { |
|
265 | - $payment->set_gateway_response( |
|
266 | - esc_html__( |
|
267 | - 'Could not process this payment because it has no associated transaction.', |
|
268 | - 'event_espresso' |
|
269 | - ) |
|
270 | - ); |
|
271 | - $payment->set_status($this->_pay_model->failed_status()); |
|
272 | - return $payment; |
|
273 | - } |
|
274 | - $primary_registrant = $transaction->primary_registration(); |
|
275 | - $payment_details = $payment->details(); |
|
276 | - // Check if we still have the token. |
|
277 | - if (! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN'])) { |
|
278 | - $payment->set_status($this->_pay_model->failed_status()); |
|
279 | - return $payment; |
|
280 | - } |
|
281 | - $cdetails_request_dtls = array( |
|
282 | - 'METHOD' => 'GetExpressCheckoutDetails', |
|
283 | - 'TOKEN' => $payment_details['TOKEN'], |
|
284 | - ); |
|
285 | - // Request Customer Details. |
|
286 | - $cdetails_request_response = $this->_ppExpress_request( |
|
287 | - $cdetails_request_dtls, |
|
288 | - 'Customer Details', |
|
289 | - $payment |
|
290 | - ); |
|
291 | - $cdetails_rstatus = $this->_ppExpress_check_response($cdetails_request_response); |
|
292 | - $cdata_response_args = (isset($cdetails_rstatus['args']) && is_array($cdetails_rstatus['args'])) |
|
293 | - ? $cdetails_rstatus['args'] |
|
294 | - : array(); |
|
295 | - if ($cdetails_rstatus['status']) { |
|
296 | - // We got the PayerID so now we can Complete the transaction. |
|
297 | - $docheckout_request_dtls = array( |
|
298 | - 'METHOD' => 'DoExpressCheckoutPayment', |
|
299 | - 'PAYERID' => $cdata_response_args['PAYERID'], |
|
300 | - 'TOKEN' => $payment_details['TOKEN'], |
|
301 | - 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
302 | - 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
303 | - 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
304 | - ); |
|
305 | - // Include itemized list. |
|
306 | - $itemized_list = $this->itemize_list( |
|
307 | - $payment, |
|
308 | - $transaction, |
|
309 | - $cdata_response_args |
|
310 | - ); |
|
311 | - $docheckout_request_dtls = array_merge($docheckout_request_dtls, $itemized_list); |
|
312 | - // Payment Checkout/Capture. |
|
313 | - $docheckout_request_response = $this->_ppExpress_request( |
|
314 | - $docheckout_request_dtls, |
|
315 | - 'Do Payment', |
|
316 | - $payment |
|
317 | - ); |
|
318 | - $docheckout_rstatus = $this->_ppExpress_check_response($docheckout_request_response); |
|
319 | - $docheckout_response_args = (isset($docheckout_rstatus['args']) && is_array($docheckout_rstatus['args'])) |
|
320 | - ? $docheckout_rstatus['args'] |
|
321 | - : array(); |
|
322 | - if ($docheckout_rstatus['status']) { |
|
323 | - // All is well, payment approved. |
|
324 | - $primary_registration_code = $primary_registrant instanceof EE_Registration ? |
|
325 | - $primary_registrant->reg_code() |
|
326 | - : ''; |
|
327 | - $payment->set_extra_accntng($primary_registration_code); |
|
328 | - $payment->set_amount(isset($docheckout_response_args['PAYMENTINFO_0_AMT']) |
|
329 | - ? (float) $docheckout_response_args['PAYMENTINFO_0_AMT'] |
|
330 | - : 0); |
|
331 | - $payment->set_txn_id_chq_nmbr(isset($docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
332 | - ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] |
|
333 | - : null); |
|
334 | - $payment->set_details($cdata_response_args); |
|
335 | - $payment->set_gateway_response(isset($docheckout_response_args['PAYMENTINFO_0_ACK']) |
|
336 | - ? $docheckout_response_args['PAYMENTINFO_0_ACK'] |
|
337 | - : ''); |
|
338 | - $payment->set_status($this->_pay_model->approved_status()); |
|
339 | - } else { |
|
340 | - if (isset($docheckout_response_args['L_ERRORCODE'])) { |
|
341 | - $payment->set_gateway_response( |
|
342 | - $docheckout_response_args['L_ERRORCODE'] |
|
343 | - . '; ' |
|
344 | - . $docheckout_response_args['L_SHORTMESSAGE'] |
|
345 | - ); |
|
346 | - } else { |
|
347 | - $payment->set_gateway_response( |
|
348 | - esc_html__( |
|
349 | - 'Error occurred while trying to Capture the funds.', |
|
350 | - 'event_espresso' |
|
351 | - ) |
|
352 | - ); |
|
353 | - } |
|
354 | - $payment->set_details($docheckout_response_args); |
|
355 | - $payment->set_status($this->_pay_model->declined_status()); |
|
356 | - } |
|
357 | - } else { |
|
358 | - if (isset($cdata_response_args['L_ERRORCODE'])) { |
|
359 | - $payment->set_gateway_response( |
|
360 | - $cdata_response_args['L_ERRORCODE'] |
|
361 | - . '; ' |
|
362 | - . $cdata_response_args['L_SHORTMESSAGE'] |
|
363 | - ); |
|
364 | - } else { |
|
365 | - $payment->set_gateway_response( |
|
366 | - esc_html__( |
|
367 | - 'Error occurred while trying to get payment Details from PayPal.', |
|
368 | - 'event_espresso' |
|
369 | - ) |
|
370 | - ); |
|
371 | - } |
|
372 | - $payment->set_details($cdata_response_args); |
|
373 | - $payment->set_status($this->_pay_model->failed_status()); |
|
374 | - } |
|
375 | - } else { |
|
376 | - $payment->set_gateway_response( |
|
377 | - esc_html__( |
|
378 | - 'Error occurred while trying to process the payment.', |
|
379 | - 'event_espresso' |
|
380 | - ) |
|
381 | - ); |
|
382 | - $payment->set_status($this->_pay_model->failed_status()); |
|
383 | - } |
|
384 | - return $payment; |
|
385 | - } |
|
386 | - |
|
387 | - |
|
388 | - |
|
389 | - /** |
|
390 | - * Make a list of items that are in the giver transaction. |
|
391 | - * |
|
392 | - * @param EEI_Payment $payment |
|
393 | - * @param EEI_Transaction $transaction |
|
394 | - * @param array $request_response_args Data from a previous communication with PP. |
|
395 | - * @return array |
|
396 | - */ |
|
397 | - public function itemize_list(EEI_Payment $payment, EEI_Transaction $transaction, $request_response_args = array()) |
|
398 | - { |
|
399 | - $itemized_list = array(); |
|
400 | - $gateway_formatter = $this->_get_gateway_formatter(); |
|
401 | - // If we have data from a previous communication with PP (on this transaction) we may use that for our list... |
|
402 | - if (! empty($request_response_args) |
|
403 | - && array_key_exists('L_PAYMENTREQUEST_0_AMT0', $request_response_args) |
|
404 | - && array_key_exists('PAYMENTREQUEST_0_ITEMAMT', $request_response_args) |
|
405 | - ) { |
|
406 | - foreach ($request_response_args as $arg_key => $arg_val) { |
|
407 | - if (strpos($arg_key, 'PAYMENTREQUEST_') !== false |
|
408 | - && strpos($arg_key, 'NOTIFYURL') === false |
|
409 | - ) { |
|
410 | - $itemized_list[ $arg_key ] = $arg_val; |
|
411 | - } |
|
412 | - } |
|
413 | - // If we got only a few Items then something is not right. |
|
414 | - if (count($itemized_list) > 2) { |
|
415 | - return $itemized_list; |
|
416 | - } else { |
|
417 | - if (WP_DEBUG) { |
|
418 | - throw new EE_Error( |
|
419 | - sprintf( |
|
420 | - esc_html__( |
|
421 | - // @codingStandardsIgnoreStart |
|
422 | - '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', |
|
423 | - // @codingStandardsIgnoreEnd |
|
424 | - 'event_espresso' |
|
425 | - ), |
|
426 | - wp_json_encode($itemized_list) |
|
427 | - ) |
|
428 | - ); |
|
429 | - } |
|
430 | - // Reset the list and log an error, maybe allow to try and generate a new list (below). |
|
431 | - $itemized_list = array(); |
|
432 | - $this->log( |
|
433 | - array( |
|
434 | - (string) esc_html__( |
|
435 | - 'Could not generate a proper item list with:', |
|
436 | - 'event_espresso' |
|
437 | - ) => $request_response_args |
|
438 | - ), |
|
439 | - $payment |
|
440 | - ); |
|
441 | - } |
|
442 | - } |
|
443 | - // ...otherwise we generate a new list for this transaction. |
|
444 | - if ($this->_money->compare_floats($payment->amount(), $transaction->total(), '==')) { |
|
445 | - $item_num = 0; |
|
446 | - $itemized_sum = 0; |
|
447 | - $total_line_items = $transaction->total_line_item(); |
|
448 | - // Go through each item in the list. |
|
449 | - foreach ($total_line_items->get_items() as $line_item) { |
|
450 | - if ($line_item instanceof EE_Line_Item) { |
|
451 | - // PayPal doesn't like line items with 0.00 amount, so we may skip those. |
|
452 | - if (EEH_Money::compare_floats($line_item->total(), '0.00', '==')) { |
|
453 | - continue; |
|
454 | - } |
|
455 | - $unit_price = $line_item->unit_price(); |
|
456 | - $line_item_quantity = $line_item->quantity(); |
|
457 | - // This is a discount. |
|
458 | - if ($line_item->is_percent()) { |
|
459 | - $unit_price = $line_item->total(); |
|
460 | - $line_item_quantity = 1; |
|
461 | - } |
|
462 | - // Item Name. |
|
463 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
464 | - $gateway_formatter->formatLineItemName($line_item, $payment), |
|
465 | - 0, |
|
466 | - 127 |
|
467 | - ); |
|
468 | - // Item description. |
|
469 | - $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = mb_strcut( |
|
470 | - $gateway_formatter->formatLineItemDesc($line_item, $payment), |
|
471 | - 0, |
|
472 | - 127 |
|
473 | - ); |
|
474 | - // Cost of individual item. |
|
475 | - $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency($unit_price); |
|
476 | - // Item Number. |
|
477 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
478 | - // Item quantity. |
|
479 | - $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = $line_item_quantity; |
|
480 | - // Digital item is sold. |
|
481 | - $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
482 | - $itemized_sum += $line_item->total(); |
|
483 | - ++$item_num; |
|
484 | - } |
|
485 | - } |
|
486 | - // Item's sales S/H and tax amount. |
|
487 | - $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $total_line_items->get_items_total(); |
|
488 | - $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = $total_line_items->get_total_tax(); |
|
489 | - $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
490 | - $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
491 | - $itemized_sum_diff_from_txn_total = round( |
|
492 | - $transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), |
|
493 | - 2 |
|
494 | - ); |
|
495 | - // If we were not able to recognize some item like promotion, surcharge or cancellation, |
|
496 | - // add the difference as an extra line item. |
|
497 | - if ($this->_money->compare_floats($itemized_sum_diff_from_txn_total, 0, '!=')) { |
|
498 | - // Item Name. |
|
499 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
500 | - esc_html__( |
|
501 | - 'Other (promotion/surcharge/cancellation)', |
|
502 | - 'event_espresso' |
|
503 | - ), |
|
504 | - 0, |
|
505 | - 127 |
|
506 | - ); |
|
507 | - // Item description. |
|
508 | - $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = ''; |
|
509 | - // Cost of individual item. |
|
510 | - $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency( |
|
511 | - $itemized_sum_diff_from_txn_total |
|
512 | - ); |
|
513 | - // Item Number. |
|
514 | - $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
515 | - // Item quantity. |
|
516 | - $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = 1; |
|
517 | - // Digital item is sold. |
|
518 | - $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
519 | - $item_num++; |
|
520 | - } |
|
521 | - } else { |
|
522 | - // Just one Item. |
|
523 | - // Item Name. |
|
524 | - $itemized_list['L_PAYMENTREQUEST_0_NAME0'] = mb_strcut( |
|
525 | - $gateway_formatter->formatPartialPaymentLineItemName($payment), |
|
526 | - 0, |
|
527 | - 127 |
|
528 | - ); |
|
529 | - // Item description. |
|
530 | - $itemized_list['L_PAYMENTREQUEST_0_DESC0'] = mb_strcut( |
|
531 | - $gateway_formatter->formatPartialPaymentLineItemDesc($payment), |
|
532 | - 0, |
|
533 | - 127 |
|
534 | - ); |
|
535 | - // Cost of individual item. |
|
536 | - $itemized_list['L_PAYMENTREQUEST_0_AMT0'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
537 | - // Item Number. |
|
538 | - $itemized_list['L_PAYMENTREQUEST_0_NUMBER0'] = 1; |
|
539 | - // Item quantity. |
|
540 | - $itemized_list['L_PAYMENTREQUEST_0_QTY0'] = 1; |
|
541 | - // Digital item is sold. |
|
542 | - $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Physical'; |
|
543 | - // Item's sales S/H and tax amount. |
|
544 | - $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
545 | - $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = '0'; |
|
546 | - $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
547 | - $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
548 | - } |
|
549 | - return $itemized_list; |
|
550 | - } |
|
551 | - |
|
552 | - |
|
553 | - |
|
554 | - /** |
|
555 | - * Make the Express checkout request. |
|
556 | - * |
|
557 | - * @param array $request_params |
|
558 | - * @param string $request_text |
|
559 | - * @param EEI_Payment $payment |
|
560 | - * @return mixed |
|
561 | - */ |
|
562 | - public function _ppExpress_request($request_params, $request_text, $payment) |
|
563 | - { |
|
564 | - $request_dtls = array( |
|
565 | - 'VERSION' => '204.0', |
|
566 | - 'USER' => $this->_api_username, |
|
567 | - 'PWD' => $this->_api_password, |
|
568 | - 'SIGNATURE' => $this->_api_signature, |
|
569 | - // EE will blow up if you change this |
|
570 | - 'BUTTONSOURCE' => 'EventEspresso_SP', |
|
571 | - ); |
|
572 | - $dtls = array_merge($request_dtls, $request_params); |
|
573 | - $this->_log_clean_request($dtls, $payment, $request_text . ' Request'); |
|
574 | - // Request Customer Details. |
|
575 | - $request_response = wp_remote_post( |
|
576 | - $this->_base_gateway_url, |
|
577 | - array( |
|
578 | - 'method' => 'POST', |
|
579 | - 'timeout' => 45, |
|
580 | - 'httpversion' => '1.1', |
|
581 | - 'cookies' => array(), |
|
582 | - 'headers' => array(), |
|
583 | - 'body' => http_build_query($dtls, '', '&'), |
|
584 | - ) |
|
585 | - ); |
|
586 | - // Log the response. |
|
587 | - $this->log(array($request_text . ' Response' => $request_response), $payment); |
|
588 | - return $request_response; |
|
589 | - } |
|
590 | - |
|
591 | - |
|
592 | - |
|
593 | - /** |
|
594 | - * Check the response status. |
|
595 | - * |
|
596 | - * @param mixed $request_response |
|
597 | - * @return array |
|
598 | - */ |
|
599 | - public function _ppExpress_check_response($request_response) |
|
600 | - { |
|
601 | - if (is_wp_error($request_response) || empty($request_response['body'])) { |
|
602 | - // If we got here then there was an error in this request. |
|
603 | - return array('status' => false, 'args' => $request_response); |
|
604 | - } |
|
605 | - $response_args = array(); |
|
606 | - parse_str(urldecode($request_response['body']), $response_args); |
|
607 | - if (! isset($response_args['ACK'])) { |
|
608 | - return array('status' => false, 'args' => $request_response); |
|
609 | - } |
|
610 | - if (( |
|
611 | - isset($response_args['PAYERID']) |
|
612 | - || isset($response_args['TOKEN']) |
|
613 | - || isset($response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
614 | - || (isset($response_args['PAYMENTSTATUS']) && $response_args['PAYMENTSTATUS'] === 'Completed') |
|
615 | - ) |
|
616 | - && in_array($response_args['ACK'], array('Success', 'SuccessWithWarning'), true) |
|
617 | - ) { |
|
618 | - // Response status OK, return response parameters for further processing. |
|
619 | - return array('status' => true, 'args' => $response_args); |
|
620 | - } |
|
621 | - $errors = $this->_get_errors($response_args); |
|
622 | - return array('status' => false, 'args' => $errors); |
|
623 | - } |
|
624 | - |
|
625 | - |
|
626 | - |
|
627 | - /** |
|
628 | - * Log a "Cleared" request. |
|
629 | - * |
|
630 | - * @param array $request |
|
631 | - * @param EEI_Payment $payment |
|
632 | - * @param string $info |
|
633 | - * @return void |
|
634 | - */ |
|
635 | - private function _log_clean_request($request, $payment, $info) |
|
636 | - { |
|
637 | - $cleaned_request_data = $request; |
|
638 | - unset($cleaned_request_data['PWD'], $cleaned_request_data['USER'], $cleaned_request_data['SIGNATURE']); |
|
639 | - $this->log(array($info => $cleaned_request_data), $payment); |
|
640 | - } |
|
641 | - |
|
642 | - |
|
643 | - |
|
644 | - /** |
|
645 | - * Get error from the response data. |
|
646 | - * |
|
647 | - * @param array $data_array |
|
648 | - * @return array |
|
649 | - */ |
|
650 | - private function _get_errors($data_array) |
|
651 | - { |
|
652 | - $errors = array(); |
|
653 | - $n = 0; |
|
654 | - while (isset($data_array[ "L_ERRORCODE{$n}" ])) { |
|
655 | - $l_error_code = isset($data_array[ "L_ERRORCODE{$n}" ]) |
|
656 | - ? $data_array[ "L_ERRORCODE{$n}" ] |
|
657 | - : ''; |
|
658 | - $l_severity_code = isset($data_array[ "L_SEVERITYCODE{$n}" ]) |
|
659 | - ? $data_array[ "L_SEVERITYCODE{$n}" ] |
|
660 | - : ''; |
|
661 | - $l_short_message = isset($data_array[ "L_SHORTMESSAGE{$n}" ]) |
|
662 | - ? $data_array[ "L_SHORTMESSAGE{$n}" ] |
|
663 | - : ''; |
|
664 | - $l_long_message = isset($data_array[ "L_LONGMESSAGE{$n}" ]) |
|
665 | - ? $data_array[ "L_LONGMESSAGE{$n}" ] |
|
666 | - : ''; |
|
667 | - if ($n === 0) { |
|
668 | - $errors = array( |
|
669 | - 'L_ERRORCODE' => $l_error_code, |
|
670 | - 'L_SHORTMESSAGE' => $l_short_message, |
|
671 | - 'L_LONGMESSAGE' => $l_long_message, |
|
672 | - 'L_SEVERITYCODE' => $l_severity_code, |
|
673 | - ); |
|
674 | - } else { |
|
675 | - $errors['L_ERRORCODE'] .= ', ' . $l_error_code; |
|
676 | - $errors['L_SHORTMESSAGE'] .= ', ' . $l_short_message; |
|
677 | - $errors['L_LONGMESSAGE'] .= ', ' . $l_long_message; |
|
678 | - $errors['L_SEVERITYCODE'] .= ', ' . $l_severity_code; |
|
679 | - } |
|
680 | - $n++; |
|
681 | - } |
|
682 | - return $errors; |
|
683 | - } |
|
30 | + /** |
|
31 | + * Merchant API Username. |
|
32 | + * |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + protected $_api_username; |
|
36 | + |
|
37 | + /** |
|
38 | + * Merchant API Password. |
|
39 | + * |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected $_api_password; |
|
43 | + |
|
44 | + /** |
|
45 | + * API Signature. |
|
46 | + * |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + protected $_api_signature; |
|
50 | + |
|
51 | + /** |
|
52 | + * Request Shipping address on PP checkout page. |
|
53 | + * |
|
54 | + * @var string |
|
55 | + */ |
|
56 | + protected $_request_shipping_addr; |
|
57 | + |
|
58 | + /** |
|
59 | + * Business/personal logo. |
|
60 | + * |
|
61 | + * @var string |
|
62 | + */ |
|
63 | + protected $_image_url; |
|
64 | + |
|
65 | + /** |
|
66 | + * gateway URL variable |
|
67 | + * |
|
68 | + * @var string |
|
69 | + */ |
|
70 | + protected $_base_gateway_url = ''; |
|
71 | + |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * EEG_Paypal_Express constructor. |
|
76 | + */ |
|
77 | + public function __construct() |
|
78 | + { |
|
79 | + $this->_currencies_supported = array( |
|
80 | + 'USD', |
|
81 | + 'AUD', |
|
82 | + 'BRL', |
|
83 | + 'CAD', |
|
84 | + 'CZK', |
|
85 | + 'DKK', |
|
86 | + 'EUR', |
|
87 | + 'HKD', |
|
88 | + 'HUF', |
|
89 | + 'ILS', |
|
90 | + 'JPY', |
|
91 | + 'MYR', |
|
92 | + 'MXN', |
|
93 | + 'NOK', |
|
94 | + 'NZD', |
|
95 | + 'PHP', |
|
96 | + 'PLN', |
|
97 | + 'GBP', |
|
98 | + 'RUB', |
|
99 | + 'SGD', |
|
100 | + 'SEK', |
|
101 | + 'CHF', |
|
102 | + 'TWD', |
|
103 | + 'THB', |
|
104 | + 'TRY', |
|
105 | + 'INR', |
|
106 | + ); |
|
107 | + parent::__construct(); |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * Sets the gateway URL variable based on whether debug mode is enabled or not. |
|
114 | + * |
|
115 | + * @param array $settings_array |
|
116 | + */ |
|
117 | + public function set_settings($settings_array) |
|
118 | + { |
|
119 | + parent::set_settings($settings_array); |
|
120 | + // Redirect URL. |
|
121 | + $this->_base_gateway_url = $this->_debug_mode |
|
122 | + ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
123 | + : 'https://api-3t.paypal.com/nvp'; |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * @param EEI_Payment $payment |
|
130 | + * @param array $billing_info |
|
131 | + * @param string $return_url |
|
132 | + * @param string $notify_url |
|
133 | + * @param string $cancel_url |
|
134 | + * @return \EE_Payment|\EEI_Payment |
|
135 | + * @throws \EE_Error |
|
136 | + */ |
|
137 | + public function set_redirection_info( |
|
138 | + $payment, |
|
139 | + $billing_info = array(), |
|
140 | + $return_url = null, |
|
141 | + $notify_url = null, |
|
142 | + $cancel_url = null |
|
143 | + ) { |
|
144 | + if (! $payment instanceof EEI_Payment) { |
|
145 | + $payment->set_gateway_response( |
|
146 | + esc_html__( |
|
147 | + 'Error. No associated payment was found.', |
|
148 | + 'event_espresso' |
|
149 | + ) |
|
150 | + ); |
|
151 | + $payment->set_status($this->_pay_model->failed_status()); |
|
152 | + return $payment; |
|
153 | + } |
|
154 | + $transaction = $payment->transaction(); |
|
155 | + if (! $transaction instanceof EEI_Transaction) { |
|
156 | + $payment->set_gateway_response( |
|
157 | + esc_html__( |
|
158 | + 'Could not process this payment because it has no associated transaction.', |
|
159 | + 'event_espresso' |
|
160 | + ) |
|
161 | + ); |
|
162 | + $payment->set_status($this->_pay_model->failed_status()); |
|
163 | + return $payment; |
|
164 | + } |
|
165 | + $gateway_formatter = $this->_get_gateway_formatter(); |
|
166 | + $order_description = mb_strcut($gateway_formatter->formatOrderDescription($payment), 0, 127); |
|
167 | + $primary_registration = $transaction->primary_registration(); |
|
168 | + $primary_attendee = $primary_registration instanceof EE_Registration |
|
169 | + ? $primary_registration->attendee() |
|
170 | + : false; |
|
171 | + $locale = explode('-', get_bloginfo('language')); |
|
172 | + // Gather request parameters. |
|
173 | + $token_request_dtls = array( |
|
174 | + 'METHOD' => 'SetExpressCheckout', |
|
175 | + 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
176 | + 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
177 | + 'PAYMENTREQUEST_0_DESC' => $order_description, |
|
178 | + 'RETURNURL' => $return_url, |
|
179 | + 'CANCELURL' => $cancel_url, |
|
180 | + 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
181 | + // Buyer does not need to create a PayPal account to check out. |
|
182 | + // This is referred to as PayPal Account Optional. |
|
183 | + 'SOLUTIONTYPE' => 'Sole', |
|
184 | + // Locale of the pages displayed by PayPal during Express Checkout. |
|
185 | + 'LOCALECODE' => $locale[1] |
|
186 | + ); |
|
187 | + // Show itemized list. |
|
188 | + $itemized_list = $this->itemize_list($payment, $transaction); |
|
189 | + $token_request_dtls = array_merge($token_request_dtls, $itemized_list); |
|
190 | + // Automatically filling out shipping and contact information. |
|
191 | + if ($this->_request_shipping_addr && $primary_attendee instanceof EEI_Attendee) { |
|
192 | + // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
|
193 | + $token_request_dtls['NOSHIPPING'] = '2'; |
|
194 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET'] = $primary_attendee->address(); |
|
195 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $primary_attendee->address2(); |
|
196 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCITY'] = $primary_attendee->city(); |
|
197 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTATE'] = $primary_attendee->state_abbrev(); |
|
198 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $primary_attendee->country_ID(); |
|
199 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
|
200 | + $token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
|
201 | + $token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
|
202 | + } elseif (! $this->_request_shipping_addr) { |
|
203 | + // Do not request shipping details on the PP Checkout page. |
|
204 | + $token_request_dtls['NOSHIPPING'] = '1'; |
|
205 | + $token_request_dtls['REQCONFIRMSHIPPING'] = '0'; |
|
206 | + } |
|
207 | + // Used a business/personal logo on the PayPal page. |
|
208 | + if (! empty($this->_image_url)) { |
|
209 | + $token_request_dtls['LOGOIMG'] = $this->_image_url; |
|
210 | + } |
|
211 | + $token_request_dtls = apply_filters( |
|
212 | + 'FHEE__EEG_Paypal_Express__set_redirection_info__arguments', |
|
213 | + $token_request_dtls, |
|
214 | + $this |
|
215 | + ); |
|
216 | + // Request PayPal token. |
|
217 | + $token_request_response = $this->_ppExpress_request($token_request_dtls, 'Payment Token', $payment); |
|
218 | + $token_rstatus = $this->_ppExpress_check_response($token_request_response); |
|
219 | + $response_args = (isset($token_rstatus['args']) && is_array($token_rstatus['args'])) |
|
220 | + ? $token_rstatus['args'] |
|
221 | + : array(); |
|
222 | + if ($token_rstatus['status']) { |
|
223 | + // We got the Token so we may continue with the payment and redirect the client. |
|
224 | + $payment->set_details($response_args); |
|
225 | + $gateway_url = $this->_debug_mode ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; |
|
226 | + $payment->set_redirect_url( |
|
227 | + $gateway_url |
|
228 | + . '/checkoutnow?useraction=commit&cmd=_express-checkout&token=' |
|
229 | + . $response_args['TOKEN'] |
|
230 | + ); |
|
231 | + } else { |
|
232 | + if (isset($response_args['L_ERRORCODE'])) { |
|
233 | + $payment->set_gateway_response($response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE']); |
|
234 | + } else { |
|
235 | + $payment->set_gateway_response( |
|
236 | + esc_html__( |
|
237 | + 'Error occurred while trying to setup the Express Checkout.', |
|
238 | + 'event_espresso' |
|
239 | + ) |
|
240 | + ); |
|
241 | + } |
|
242 | + $payment->set_details($response_args); |
|
243 | + $payment->set_status($this->_pay_model->failed_status()); |
|
244 | + } |
|
245 | + return $payment; |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * @param array $update_info { |
|
252 | + * @type string $gateway_txn_id |
|
253 | + * @type string status an EEMI_Payment status |
|
254 | + * } |
|
255 | + * @param EEI_Transaction $transaction |
|
256 | + * @return EEI_Payment |
|
257 | + */ |
|
258 | + public function handle_payment_update($update_info, $transaction) |
|
259 | + { |
|
260 | + $payment = $transaction instanceof EEI_Transaction ? $transaction->last_payment() : null; |
|
261 | + if ($payment instanceof EEI_Payment) { |
|
262 | + $this->log(array('Return from Authorization' => $update_info), $payment); |
|
263 | + $transaction = $payment->transaction(); |
|
264 | + if (! $transaction instanceof EEI_Transaction) { |
|
265 | + $payment->set_gateway_response( |
|
266 | + esc_html__( |
|
267 | + 'Could not process this payment because it has no associated transaction.', |
|
268 | + 'event_espresso' |
|
269 | + ) |
|
270 | + ); |
|
271 | + $payment->set_status($this->_pay_model->failed_status()); |
|
272 | + return $payment; |
|
273 | + } |
|
274 | + $primary_registrant = $transaction->primary_registration(); |
|
275 | + $payment_details = $payment->details(); |
|
276 | + // Check if we still have the token. |
|
277 | + if (! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN'])) { |
|
278 | + $payment->set_status($this->_pay_model->failed_status()); |
|
279 | + return $payment; |
|
280 | + } |
|
281 | + $cdetails_request_dtls = array( |
|
282 | + 'METHOD' => 'GetExpressCheckoutDetails', |
|
283 | + 'TOKEN' => $payment_details['TOKEN'], |
|
284 | + ); |
|
285 | + // Request Customer Details. |
|
286 | + $cdetails_request_response = $this->_ppExpress_request( |
|
287 | + $cdetails_request_dtls, |
|
288 | + 'Customer Details', |
|
289 | + $payment |
|
290 | + ); |
|
291 | + $cdetails_rstatus = $this->_ppExpress_check_response($cdetails_request_response); |
|
292 | + $cdata_response_args = (isset($cdetails_rstatus['args']) && is_array($cdetails_rstatus['args'])) |
|
293 | + ? $cdetails_rstatus['args'] |
|
294 | + : array(); |
|
295 | + if ($cdetails_rstatus['status']) { |
|
296 | + // We got the PayerID so now we can Complete the transaction. |
|
297 | + $docheckout_request_dtls = array( |
|
298 | + 'METHOD' => 'DoExpressCheckoutPayment', |
|
299 | + 'PAYERID' => $cdata_response_args['PAYERID'], |
|
300 | + 'TOKEN' => $payment_details['TOKEN'], |
|
301 | + 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
302 | + 'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
303 | + 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
304 | + ); |
|
305 | + // Include itemized list. |
|
306 | + $itemized_list = $this->itemize_list( |
|
307 | + $payment, |
|
308 | + $transaction, |
|
309 | + $cdata_response_args |
|
310 | + ); |
|
311 | + $docheckout_request_dtls = array_merge($docheckout_request_dtls, $itemized_list); |
|
312 | + // Payment Checkout/Capture. |
|
313 | + $docheckout_request_response = $this->_ppExpress_request( |
|
314 | + $docheckout_request_dtls, |
|
315 | + 'Do Payment', |
|
316 | + $payment |
|
317 | + ); |
|
318 | + $docheckout_rstatus = $this->_ppExpress_check_response($docheckout_request_response); |
|
319 | + $docheckout_response_args = (isset($docheckout_rstatus['args']) && is_array($docheckout_rstatus['args'])) |
|
320 | + ? $docheckout_rstatus['args'] |
|
321 | + : array(); |
|
322 | + if ($docheckout_rstatus['status']) { |
|
323 | + // All is well, payment approved. |
|
324 | + $primary_registration_code = $primary_registrant instanceof EE_Registration ? |
|
325 | + $primary_registrant->reg_code() |
|
326 | + : ''; |
|
327 | + $payment->set_extra_accntng($primary_registration_code); |
|
328 | + $payment->set_amount(isset($docheckout_response_args['PAYMENTINFO_0_AMT']) |
|
329 | + ? (float) $docheckout_response_args['PAYMENTINFO_0_AMT'] |
|
330 | + : 0); |
|
331 | + $payment->set_txn_id_chq_nmbr(isset($docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
332 | + ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] |
|
333 | + : null); |
|
334 | + $payment->set_details($cdata_response_args); |
|
335 | + $payment->set_gateway_response(isset($docheckout_response_args['PAYMENTINFO_0_ACK']) |
|
336 | + ? $docheckout_response_args['PAYMENTINFO_0_ACK'] |
|
337 | + : ''); |
|
338 | + $payment->set_status($this->_pay_model->approved_status()); |
|
339 | + } else { |
|
340 | + if (isset($docheckout_response_args['L_ERRORCODE'])) { |
|
341 | + $payment->set_gateway_response( |
|
342 | + $docheckout_response_args['L_ERRORCODE'] |
|
343 | + . '; ' |
|
344 | + . $docheckout_response_args['L_SHORTMESSAGE'] |
|
345 | + ); |
|
346 | + } else { |
|
347 | + $payment->set_gateway_response( |
|
348 | + esc_html__( |
|
349 | + 'Error occurred while trying to Capture the funds.', |
|
350 | + 'event_espresso' |
|
351 | + ) |
|
352 | + ); |
|
353 | + } |
|
354 | + $payment->set_details($docheckout_response_args); |
|
355 | + $payment->set_status($this->_pay_model->declined_status()); |
|
356 | + } |
|
357 | + } else { |
|
358 | + if (isset($cdata_response_args['L_ERRORCODE'])) { |
|
359 | + $payment->set_gateway_response( |
|
360 | + $cdata_response_args['L_ERRORCODE'] |
|
361 | + . '; ' |
|
362 | + . $cdata_response_args['L_SHORTMESSAGE'] |
|
363 | + ); |
|
364 | + } else { |
|
365 | + $payment->set_gateway_response( |
|
366 | + esc_html__( |
|
367 | + 'Error occurred while trying to get payment Details from PayPal.', |
|
368 | + 'event_espresso' |
|
369 | + ) |
|
370 | + ); |
|
371 | + } |
|
372 | + $payment->set_details($cdata_response_args); |
|
373 | + $payment->set_status($this->_pay_model->failed_status()); |
|
374 | + } |
|
375 | + } else { |
|
376 | + $payment->set_gateway_response( |
|
377 | + esc_html__( |
|
378 | + 'Error occurred while trying to process the payment.', |
|
379 | + 'event_espresso' |
|
380 | + ) |
|
381 | + ); |
|
382 | + $payment->set_status($this->_pay_model->failed_status()); |
|
383 | + } |
|
384 | + return $payment; |
|
385 | + } |
|
386 | + |
|
387 | + |
|
388 | + |
|
389 | + /** |
|
390 | + * Make a list of items that are in the giver transaction. |
|
391 | + * |
|
392 | + * @param EEI_Payment $payment |
|
393 | + * @param EEI_Transaction $transaction |
|
394 | + * @param array $request_response_args Data from a previous communication with PP. |
|
395 | + * @return array |
|
396 | + */ |
|
397 | + public function itemize_list(EEI_Payment $payment, EEI_Transaction $transaction, $request_response_args = array()) |
|
398 | + { |
|
399 | + $itemized_list = array(); |
|
400 | + $gateway_formatter = $this->_get_gateway_formatter(); |
|
401 | + // If we have data from a previous communication with PP (on this transaction) we may use that for our list... |
|
402 | + if (! empty($request_response_args) |
|
403 | + && array_key_exists('L_PAYMENTREQUEST_0_AMT0', $request_response_args) |
|
404 | + && array_key_exists('PAYMENTREQUEST_0_ITEMAMT', $request_response_args) |
|
405 | + ) { |
|
406 | + foreach ($request_response_args as $arg_key => $arg_val) { |
|
407 | + if (strpos($arg_key, 'PAYMENTREQUEST_') !== false |
|
408 | + && strpos($arg_key, 'NOTIFYURL') === false |
|
409 | + ) { |
|
410 | + $itemized_list[ $arg_key ] = $arg_val; |
|
411 | + } |
|
412 | + } |
|
413 | + // If we got only a few Items then something is not right. |
|
414 | + if (count($itemized_list) > 2) { |
|
415 | + return $itemized_list; |
|
416 | + } else { |
|
417 | + if (WP_DEBUG) { |
|
418 | + throw new EE_Error( |
|
419 | + sprintf( |
|
420 | + esc_html__( |
|
421 | + // @codingStandardsIgnoreStart |
|
422 | + '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', |
|
423 | + // @codingStandardsIgnoreEnd |
|
424 | + 'event_espresso' |
|
425 | + ), |
|
426 | + wp_json_encode($itemized_list) |
|
427 | + ) |
|
428 | + ); |
|
429 | + } |
|
430 | + // Reset the list and log an error, maybe allow to try and generate a new list (below). |
|
431 | + $itemized_list = array(); |
|
432 | + $this->log( |
|
433 | + array( |
|
434 | + (string) esc_html__( |
|
435 | + 'Could not generate a proper item list with:', |
|
436 | + 'event_espresso' |
|
437 | + ) => $request_response_args |
|
438 | + ), |
|
439 | + $payment |
|
440 | + ); |
|
441 | + } |
|
442 | + } |
|
443 | + // ...otherwise we generate a new list for this transaction. |
|
444 | + if ($this->_money->compare_floats($payment->amount(), $transaction->total(), '==')) { |
|
445 | + $item_num = 0; |
|
446 | + $itemized_sum = 0; |
|
447 | + $total_line_items = $transaction->total_line_item(); |
|
448 | + // Go through each item in the list. |
|
449 | + foreach ($total_line_items->get_items() as $line_item) { |
|
450 | + if ($line_item instanceof EE_Line_Item) { |
|
451 | + // PayPal doesn't like line items with 0.00 amount, so we may skip those. |
|
452 | + if (EEH_Money::compare_floats($line_item->total(), '0.00', '==')) { |
|
453 | + continue; |
|
454 | + } |
|
455 | + $unit_price = $line_item->unit_price(); |
|
456 | + $line_item_quantity = $line_item->quantity(); |
|
457 | + // This is a discount. |
|
458 | + if ($line_item->is_percent()) { |
|
459 | + $unit_price = $line_item->total(); |
|
460 | + $line_item_quantity = 1; |
|
461 | + } |
|
462 | + // Item Name. |
|
463 | + $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
464 | + $gateway_formatter->formatLineItemName($line_item, $payment), |
|
465 | + 0, |
|
466 | + 127 |
|
467 | + ); |
|
468 | + // Item description. |
|
469 | + $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = mb_strcut( |
|
470 | + $gateway_formatter->formatLineItemDesc($line_item, $payment), |
|
471 | + 0, |
|
472 | + 127 |
|
473 | + ); |
|
474 | + // Cost of individual item. |
|
475 | + $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency($unit_price); |
|
476 | + // Item Number. |
|
477 | + $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
478 | + // Item quantity. |
|
479 | + $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = $line_item_quantity; |
|
480 | + // Digital item is sold. |
|
481 | + $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
482 | + $itemized_sum += $line_item->total(); |
|
483 | + ++$item_num; |
|
484 | + } |
|
485 | + } |
|
486 | + // Item's sales S/H and tax amount. |
|
487 | + $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $total_line_items->get_items_total(); |
|
488 | + $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = $total_line_items->get_total_tax(); |
|
489 | + $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
490 | + $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
491 | + $itemized_sum_diff_from_txn_total = round( |
|
492 | + $transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), |
|
493 | + 2 |
|
494 | + ); |
|
495 | + // If we were not able to recognize some item like promotion, surcharge or cancellation, |
|
496 | + // add the difference as an extra line item. |
|
497 | + if ($this->_money->compare_floats($itemized_sum_diff_from_txn_total, 0, '!=')) { |
|
498 | + // Item Name. |
|
499 | + $itemized_list[ 'L_PAYMENTREQUEST_0_NAME' . $item_num ] = mb_strcut( |
|
500 | + esc_html__( |
|
501 | + 'Other (promotion/surcharge/cancellation)', |
|
502 | + 'event_espresso' |
|
503 | + ), |
|
504 | + 0, |
|
505 | + 127 |
|
506 | + ); |
|
507 | + // Item description. |
|
508 | + $itemized_list[ 'L_PAYMENTREQUEST_0_DESC' . $item_num ] = ''; |
|
509 | + // Cost of individual item. |
|
510 | + $itemized_list[ 'L_PAYMENTREQUEST_0_AMT' . $item_num ] = $gateway_formatter->formatCurrency( |
|
511 | + $itemized_sum_diff_from_txn_total |
|
512 | + ); |
|
513 | + // Item Number. |
|
514 | + $itemized_list[ 'L_PAYMENTREQUEST_0_NUMBER' . $item_num ] = $item_num + 1; |
|
515 | + // Item quantity. |
|
516 | + $itemized_list[ 'L_PAYMENTREQUEST_0_QTY' . $item_num ] = 1; |
|
517 | + // Digital item is sold. |
|
518 | + $itemized_list[ 'L_PAYMENTREQUEST_0_ITEMCATEGORY' . $item_num ] = 'Physical'; |
|
519 | + $item_num++; |
|
520 | + } |
|
521 | + } else { |
|
522 | + // Just one Item. |
|
523 | + // Item Name. |
|
524 | + $itemized_list['L_PAYMENTREQUEST_0_NAME0'] = mb_strcut( |
|
525 | + $gateway_formatter->formatPartialPaymentLineItemName($payment), |
|
526 | + 0, |
|
527 | + 127 |
|
528 | + ); |
|
529 | + // Item description. |
|
530 | + $itemized_list['L_PAYMENTREQUEST_0_DESC0'] = mb_strcut( |
|
531 | + $gateway_formatter->formatPartialPaymentLineItemDesc($payment), |
|
532 | + 0, |
|
533 | + 127 |
|
534 | + ); |
|
535 | + // Cost of individual item. |
|
536 | + $itemized_list['L_PAYMENTREQUEST_0_AMT0'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
537 | + // Item Number. |
|
538 | + $itemized_list['L_PAYMENTREQUEST_0_NUMBER0'] = 1; |
|
539 | + // Item quantity. |
|
540 | + $itemized_list['L_PAYMENTREQUEST_0_QTY0'] = 1; |
|
541 | + // Digital item is sold. |
|
542 | + $itemized_list['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Physical'; |
|
543 | + // Item's sales S/H and tax amount. |
|
544 | + $itemized_list['PAYMENTREQUEST_0_ITEMAMT'] = $gateway_formatter->formatCurrency($payment->amount()); |
|
545 | + $itemized_list['PAYMENTREQUEST_0_TAXAMT'] = '0'; |
|
546 | + $itemized_list['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
547 | + $itemized_list['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
548 | + } |
|
549 | + return $itemized_list; |
|
550 | + } |
|
551 | + |
|
552 | + |
|
553 | + |
|
554 | + /** |
|
555 | + * Make the Express checkout request. |
|
556 | + * |
|
557 | + * @param array $request_params |
|
558 | + * @param string $request_text |
|
559 | + * @param EEI_Payment $payment |
|
560 | + * @return mixed |
|
561 | + */ |
|
562 | + public function _ppExpress_request($request_params, $request_text, $payment) |
|
563 | + { |
|
564 | + $request_dtls = array( |
|
565 | + 'VERSION' => '204.0', |
|
566 | + 'USER' => $this->_api_username, |
|
567 | + 'PWD' => $this->_api_password, |
|
568 | + 'SIGNATURE' => $this->_api_signature, |
|
569 | + // EE will blow up if you change this |
|
570 | + 'BUTTONSOURCE' => 'EventEspresso_SP', |
|
571 | + ); |
|
572 | + $dtls = array_merge($request_dtls, $request_params); |
|
573 | + $this->_log_clean_request($dtls, $payment, $request_text . ' Request'); |
|
574 | + // Request Customer Details. |
|
575 | + $request_response = wp_remote_post( |
|
576 | + $this->_base_gateway_url, |
|
577 | + array( |
|
578 | + 'method' => 'POST', |
|
579 | + 'timeout' => 45, |
|
580 | + 'httpversion' => '1.1', |
|
581 | + 'cookies' => array(), |
|
582 | + 'headers' => array(), |
|
583 | + 'body' => http_build_query($dtls, '', '&'), |
|
584 | + ) |
|
585 | + ); |
|
586 | + // Log the response. |
|
587 | + $this->log(array($request_text . ' Response' => $request_response), $payment); |
|
588 | + return $request_response; |
|
589 | + } |
|
590 | + |
|
591 | + |
|
592 | + |
|
593 | + /** |
|
594 | + * Check the response status. |
|
595 | + * |
|
596 | + * @param mixed $request_response |
|
597 | + * @return array |
|
598 | + */ |
|
599 | + public function _ppExpress_check_response($request_response) |
|
600 | + { |
|
601 | + if (is_wp_error($request_response) || empty($request_response['body'])) { |
|
602 | + // If we got here then there was an error in this request. |
|
603 | + return array('status' => false, 'args' => $request_response); |
|
604 | + } |
|
605 | + $response_args = array(); |
|
606 | + parse_str(urldecode($request_response['body']), $response_args); |
|
607 | + if (! isset($response_args['ACK'])) { |
|
608 | + return array('status' => false, 'args' => $request_response); |
|
609 | + } |
|
610 | + if (( |
|
611 | + isset($response_args['PAYERID']) |
|
612 | + || isset($response_args['TOKEN']) |
|
613 | + || isset($response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
614 | + || (isset($response_args['PAYMENTSTATUS']) && $response_args['PAYMENTSTATUS'] === 'Completed') |
|
615 | + ) |
|
616 | + && in_array($response_args['ACK'], array('Success', 'SuccessWithWarning'), true) |
|
617 | + ) { |
|
618 | + // Response status OK, return response parameters for further processing. |
|
619 | + return array('status' => true, 'args' => $response_args); |
|
620 | + } |
|
621 | + $errors = $this->_get_errors($response_args); |
|
622 | + return array('status' => false, 'args' => $errors); |
|
623 | + } |
|
624 | + |
|
625 | + |
|
626 | + |
|
627 | + /** |
|
628 | + * Log a "Cleared" request. |
|
629 | + * |
|
630 | + * @param array $request |
|
631 | + * @param EEI_Payment $payment |
|
632 | + * @param string $info |
|
633 | + * @return void |
|
634 | + */ |
|
635 | + private function _log_clean_request($request, $payment, $info) |
|
636 | + { |
|
637 | + $cleaned_request_data = $request; |
|
638 | + unset($cleaned_request_data['PWD'], $cleaned_request_data['USER'], $cleaned_request_data['SIGNATURE']); |
|
639 | + $this->log(array($info => $cleaned_request_data), $payment); |
|
640 | + } |
|
641 | + |
|
642 | + |
|
643 | + |
|
644 | + /** |
|
645 | + * Get error from the response data. |
|
646 | + * |
|
647 | + * @param array $data_array |
|
648 | + * @return array |
|
649 | + */ |
|
650 | + private function _get_errors($data_array) |
|
651 | + { |
|
652 | + $errors = array(); |
|
653 | + $n = 0; |
|
654 | + while (isset($data_array[ "L_ERRORCODE{$n}" ])) { |
|
655 | + $l_error_code = isset($data_array[ "L_ERRORCODE{$n}" ]) |
|
656 | + ? $data_array[ "L_ERRORCODE{$n}" ] |
|
657 | + : ''; |
|
658 | + $l_severity_code = isset($data_array[ "L_SEVERITYCODE{$n}" ]) |
|
659 | + ? $data_array[ "L_SEVERITYCODE{$n}" ] |
|
660 | + : ''; |
|
661 | + $l_short_message = isset($data_array[ "L_SHORTMESSAGE{$n}" ]) |
|
662 | + ? $data_array[ "L_SHORTMESSAGE{$n}" ] |
|
663 | + : ''; |
|
664 | + $l_long_message = isset($data_array[ "L_LONGMESSAGE{$n}" ]) |
|
665 | + ? $data_array[ "L_LONGMESSAGE{$n}" ] |
|
666 | + : ''; |
|
667 | + if ($n === 0) { |
|
668 | + $errors = array( |
|
669 | + 'L_ERRORCODE' => $l_error_code, |
|
670 | + 'L_SHORTMESSAGE' => $l_short_message, |
|
671 | + 'L_LONGMESSAGE' => $l_long_message, |
|
672 | + 'L_SEVERITYCODE' => $l_severity_code, |
|
673 | + ); |
|
674 | + } else { |
|
675 | + $errors['L_ERRORCODE'] .= ', ' . $l_error_code; |
|
676 | + $errors['L_SHORTMESSAGE'] .= ', ' . $l_short_message; |
|
677 | + $errors['L_LONGMESSAGE'] .= ', ' . $l_long_message; |
|
678 | + $errors['L_SEVERITYCODE'] .= ', ' . $l_severity_code; |
|
679 | + } |
|
680 | + $n++; |
|
681 | + } |
|
682 | + return $errors; |
|
683 | + } |
|
684 | 684 | } |
@@ -12,32 +12,32 @@ |
||
12 | 12 | class EE_Button_Display_Strategy extends EE_Display_Strategy_Base |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * @return string of html to display the input |
|
17 | - */ |
|
18 | - public function display() |
|
19 | - { |
|
20 | - $default_value = $this->_input->get_default(); |
|
21 | - if ($this->_input->get_normalization_strategy() instanceof EE_Normalization_Strategy_Base) { |
|
22 | - $default_value = $this->_input->get_normalization_strategy()->unnormalize($default_value); |
|
23 | - } |
|
24 | - $html = $this->_opening_tag('button'); |
|
25 | - $html .= $this->_attributes_string( |
|
26 | - array_merge( |
|
27 | - $this->_standard_attributes_array(), |
|
28 | - array( |
|
29 | - 'value' => $default_value, |
|
30 | - ) |
|
31 | - ) |
|
32 | - ); |
|
33 | - if ($this->_input instanceof EE_Button_Input) { |
|
34 | - $button_content = $this->_input->button_content(); |
|
35 | - } else { |
|
36 | - $button_content = $this->_input->get_default(); |
|
37 | - } |
|
38 | - $html .= '>'; |
|
39 | - $html .= $button_content; |
|
40 | - $html .= $this->_closing_tag(); |
|
41 | - return $html; |
|
42 | - } |
|
15 | + /** |
|
16 | + * @return string of html to display the input |
|
17 | + */ |
|
18 | + public function display() |
|
19 | + { |
|
20 | + $default_value = $this->_input->get_default(); |
|
21 | + if ($this->_input->get_normalization_strategy() instanceof EE_Normalization_Strategy_Base) { |
|
22 | + $default_value = $this->_input->get_normalization_strategy()->unnormalize($default_value); |
|
23 | + } |
|
24 | + $html = $this->_opening_tag('button'); |
|
25 | + $html .= $this->_attributes_string( |
|
26 | + array_merge( |
|
27 | + $this->_standard_attributes_array(), |
|
28 | + array( |
|
29 | + 'value' => $default_value, |
|
30 | + ) |
|
31 | + ) |
|
32 | + ); |
|
33 | + if ($this->_input instanceof EE_Button_Input) { |
|
34 | + $button_content = $this->_input->button_content(); |
|
35 | + } else { |
|
36 | + $button_content = $this->_input->get_default(); |
|
37 | + } |
|
38 | + $html .= '>'; |
|
39 | + $html .= $button_content; |
|
40 | + $html .= $this->_closing_tag(); |
|
41 | + return $html; |
|
42 | + } |
|
43 | 43 | } |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public static function getShared(FullyQualifiedName $domain_fqcn, array $arguments) |
42 | 42 | { |
43 | - if (! isset($arguments[0], $arguments[1])) { |
|
43 | + if ( ! isset($arguments[0], $arguments[1])) { |
|
44 | 44 | throw new InvalidArgumentException( |
45 | 45 | esc_html__( |
46 | 46 | 'You need to pass at least two arguments, representing the addon plugin file and version, in order to generate a Domain class', |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | ); |
50 | 50 | } |
51 | 51 | $domain = LoaderFactory::getLoader()->getShared($domain_fqcn, $arguments); |
52 | - if (! $domain instanceof $domain_fqcn && ! $domain instanceof DomainBase) { |
|
52 | + if ( ! $domain instanceof $domain_fqcn && ! $domain instanceof DomainBase) { |
|
53 | 53 | throw new DomainException( |
54 | 54 | sprintf( |
55 | 55 | esc_html__( |
@@ -28,63 +28,63 @@ |
||
28 | 28 | class DomainFactory |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * @param FullyQualifiedName $domain_fqcn [required] Fully Qualified Class Name for the Domain class |
|
33 | - * @param array $arguments [required] array of arguments to be passed to the Domain class |
|
34 | - * constructor. Must at least include the following two value objects: |
|
35 | - * array( |
|
36 | - * EventEspresso\core\domain\values\FilePath $plugin_file |
|
37 | - * EventEspresso\core\domain\values\Version $version |
|
38 | - * ) |
|
39 | - * @return mixed |
|
40 | - * @throws DomainException |
|
41 | - * @throws InvalidArgumentException |
|
42 | - * @throws InvalidDataTypeException |
|
43 | - * @throws InvalidInterfaceException |
|
44 | - */ |
|
45 | - public static function getShared(FullyQualifiedName $domain_fqcn, array $arguments) |
|
46 | - { |
|
47 | - if (! isset($arguments[0], $arguments[1])) { |
|
48 | - throw new InvalidArgumentException( |
|
49 | - esc_html__( |
|
50 | - 'You need to pass at least two arguments, representing the addon plugin file and version, in order to generate a Domain class', |
|
51 | - 'event_espresso' |
|
52 | - ) |
|
53 | - ); |
|
54 | - } |
|
55 | - $domain = LoaderFactory::getLoader()->getShared($domain_fqcn, $arguments); |
|
56 | - if (! $domain instanceof $domain_fqcn && ! $domain instanceof DomainBase) { |
|
57 | - throw new DomainException( |
|
58 | - sprintf( |
|
59 | - esc_html__( |
|
60 | - 'The requested Domain class "%1$s" could not be loaded.', |
|
61 | - 'event_espresso' |
|
62 | - ), |
|
63 | - $domain_fqcn |
|
64 | - ) |
|
65 | - ); |
|
66 | - } |
|
67 | - return $domain; |
|
68 | - } |
|
31 | + /** |
|
32 | + * @param FullyQualifiedName $domain_fqcn [required] Fully Qualified Class Name for the Domain class |
|
33 | + * @param array $arguments [required] array of arguments to be passed to the Domain class |
|
34 | + * constructor. Must at least include the following two value objects: |
|
35 | + * array( |
|
36 | + * EventEspresso\core\domain\values\FilePath $plugin_file |
|
37 | + * EventEspresso\core\domain\values\Version $version |
|
38 | + * ) |
|
39 | + * @return mixed |
|
40 | + * @throws DomainException |
|
41 | + * @throws InvalidArgumentException |
|
42 | + * @throws InvalidDataTypeException |
|
43 | + * @throws InvalidInterfaceException |
|
44 | + */ |
|
45 | + public static function getShared(FullyQualifiedName $domain_fqcn, array $arguments) |
|
46 | + { |
|
47 | + if (! isset($arguments[0], $arguments[1])) { |
|
48 | + throw new InvalidArgumentException( |
|
49 | + esc_html__( |
|
50 | + 'You need to pass at least two arguments, representing the addon plugin file and version, in order to generate a Domain class', |
|
51 | + 'event_espresso' |
|
52 | + ) |
|
53 | + ); |
|
54 | + } |
|
55 | + $domain = LoaderFactory::getLoader()->getShared($domain_fqcn, $arguments); |
|
56 | + if (! $domain instanceof $domain_fqcn && ! $domain instanceof DomainBase) { |
|
57 | + throw new DomainException( |
|
58 | + sprintf( |
|
59 | + esc_html__( |
|
60 | + 'The requested Domain class "%1$s" could not be loaded.', |
|
61 | + 'event_espresso' |
|
62 | + ), |
|
63 | + $domain_fqcn |
|
64 | + ) |
|
65 | + ); |
|
66 | + } |
|
67 | + return $domain; |
|
68 | + } |
|
69 | 69 | |
70 | 70 | |
71 | - /** |
|
72 | - * @return Domain |
|
73 | - * @throws DomainException |
|
74 | - * @throws InvalidArgumentException |
|
75 | - * @throws InvalidDataTypeException |
|
76 | - * @throws InvalidFilePathException |
|
77 | - * @throws InvalidInterfaceException |
|
78 | - */ |
|
79 | - public static function getEventEspressoCoreDomain() |
|
80 | - { |
|
81 | - $domain = new Domain( |
|
82 | - new FilePath(EVENT_ESPRESSO_MAIN_FILE), |
|
83 | - Version::fromString(espresso_version()) |
|
84 | - ); |
|
85 | - LoaderFactory::getLoader()->share('EventEspresso\core\domain\Domain', $domain); |
|
86 | - return $domain; |
|
87 | - } |
|
71 | + /** |
|
72 | + * @return Domain |
|
73 | + * @throws DomainException |
|
74 | + * @throws InvalidArgumentException |
|
75 | + * @throws InvalidDataTypeException |
|
76 | + * @throws InvalidFilePathException |
|
77 | + * @throws InvalidInterfaceException |
|
78 | + */ |
|
79 | + public static function getEventEspressoCoreDomain() |
|
80 | + { |
|
81 | + $domain = new Domain( |
|
82 | + new FilePath(EVENT_ESPRESSO_MAIN_FILE), |
|
83 | + Version::fromString(espresso_version()) |
|
84 | + ); |
|
85 | + LoaderFactory::getLoader()->share('EventEspresso\core\domain\Domain', $domain); |
|
86 | + return $domain; |
|
87 | + } |
|
88 | 88 | } |
89 | 89 | |
90 | 90 |
@@ -20,42 +20,42 @@ |
||
20 | 20 | class FilePath |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * @var string file_path |
|
25 | - */ |
|
26 | - private $file_path; |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * FilePath constructor. |
|
31 | - * |
|
32 | - * @param string $file_path |
|
33 | - * @throws InvalidDataTypeException |
|
34 | - * @throws InvalidFilePathException |
|
35 | - */ |
|
36 | - public function __construct($file_path) |
|
37 | - { |
|
38 | - if (! is_string($file_path)) { |
|
39 | - throw new InvalidDataTypeException( |
|
40 | - '$file_path', |
|
41 | - $file_path, |
|
42 | - 'string' |
|
43 | - ); |
|
44 | - } |
|
45 | - if (! is_readable($file_path)) { |
|
46 | - throw new InvalidFilePathException($file_path); |
|
47 | - } |
|
48 | - $this->file_path = $file_path; |
|
49 | - } |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - public function __toString() |
|
56 | - { |
|
57 | - return $this->file_path; |
|
58 | - } |
|
23 | + /** |
|
24 | + * @var string file_path |
|
25 | + */ |
|
26 | + private $file_path; |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * FilePath constructor. |
|
31 | + * |
|
32 | + * @param string $file_path |
|
33 | + * @throws InvalidDataTypeException |
|
34 | + * @throws InvalidFilePathException |
|
35 | + */ |
|
36 | + public function __construct($file_path) |
|
37 | + { |
|
38 | + if (! is_string($file_path)) { |
|
39 | + throw new InvalidDataTypeException( |
|
40 | + '$file_path', |
|
41 | + $file_path, |
|
42 | + 'string' |
|
43 | + ); |
|
44 | + } |
|
45 | + if (! is_readable($file_path)) { |
|
46 | + throw new InvalidFilePathException($file_path); |
|
47 | + } |
|
48 | + $this->file_path = $file_path; |
|
49 | + } |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + public function __toString() |
|
56 | + { |
|
57 | + return $this->file_path; |
|
58 | + } |
|
59 | 59 | |
60 | 60 | |
61 | 61 | } |
@@ -35,14 +35,14 @@ |
||
35 | 35 | */ |
36 | 36 | public function __construct($file_path) |
37 | 37 | { |
38 | - if (! is_string($file_path)) { |
|
38 | + if ( ! is_string($file_path)) { |
|
39 | 39 | throw new InvalidDataTypeException( |
40 | 40 | '$file_path', |
41 | 41 | $file_path, |
42 | 42 | 'string' |
43 | 43 | ); |
44 | 44 | } |
45 | - if (! is_readable($file_path)) { |
|
45 | + if ( ! is_readable($file_path)) { |
|
46 | 46 | throw new InvalidFilePathException($file_path); |
47 | 47 | } |
48 | 48 | $this->file_path = $file_path; |
@@ -37,14 +37,14 @@ |
||
37 | 37 | */ |
38 | 38 | public function __construct($fully_qualified_name) |
39 | 39 | { |
40 | - if (! is_string($fully_qualified_name)) { |
|
40 | + if ( ! is_string($fully_qualified_name)) { |
|
41 | 41 | throw new InvalidDataTypeException( |
42 | 42 | '$fully_qualified_name', |
43 | 43 | $fully_qualified_name, |
44 | 44 | 'string' |
45 | 45 | ); |
46 | 46 | } |
47 | - if (! class_exists($fully_qualified_name) && ! interface_exists($fully_qualified_name)) { |
|
47 | + if ( ! class_exists($fully_qualified_name) && ! interface_exists($fully_qualified_name)) { |
|
48 | 48 | if (strpos($fully_qualified_name, 'Interface') !== false) { |
49 | 49 | throw new InvalidInterfaceException($fully_qualified_name); |
50 | 50 | } |
@@ -21,54 +21,54 @@ |
||
21 | 21 | class FullyQualifiedName |
22 | 22 | { |
23 | 23 | |
24 | - /** |
|
25 | - * @var string $fully_qualified_name |
|
26 | - */ |
|
27 | - private $fully_qualified_name; |
|
24 | + /** |
|
25 | + * @var string $fully_qualified_name |
|
26 | + */ |
|
27 | + private $fully_qualified_name; |
|
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * FullyQualifiedName constructor. |
|
32 | - * |
|
33 | - * @param string $fully_qualified_name |
|
34 | - * @throws InvalidClassException |
|
35 | - * @throws InvalidInterfaceException |
|
36 | - * @throws InvalidDataTypeException |
|
37 | - */ |
|
38 | - public function __construct($fully_qualified_name) |
|
39 | - { |
|
40 | - if (! is_string($fully_qualified_name)) { |
|
41 | - throw new InvalidDataTypeException( |
|
42 | - '$fully_qualified_name', |
|
43 | - $fully_qualified_name, |
|
44 | - 'string' |
|
45 | - ); |
|
46 | - } |
|
47 | - if (! class_exists($fully_qualified_name) && ! interface_exists($fully_qualified_name)) { |
|
48 | - if (strpos($fully_qualified_name, 'Interface') !== false) { |
|
49 | - throw new InvalidInterfaceException($fully_qualified_name); |
|
50 | - } |
|
51 | - throw new InvalidClassException($fully_qualified_name); |
|
52 | - } |
|
53 | - $this->fully_qualified_name = $fully_qualified_name; |
|
54 | - } |
|
30 | + /** |
|
31 | + * FullyQualifiedName constructor. |
|
32 | + * |
|
33 | + * @param string $fully_qualified_name |
|
34 | + * @throws InvalidClassException |
|
35 | + * @throws InvalidInterfaceException |
|
36 | + * @throws InvalidDataTypeException |
|
37 | + */ |
|
38 | + public function __construct($fully_qualified_name) |
|
39 | + { |
|
40 | + if (! is_string($fully_qualified_name)) { |
|
41 | + throw new InvalidDataTypeException( |
|
42 | + '$fully_qualified_name', |
|
43 | + $fully_qualified_name, |
|
44 | + 'string' |
|
45 | + ); |
|
46 | + } |
|
47 | + if (! class_exists($fully_qualified_name) && ! interface_exists($fully_qualified_name)) { |
|
48 | + if (strpos($fully_qualified_name, 'Interface') !== false) { |
|
49 | + throw new InvalidInterfaceException($fully_qualified_name); |
|
50 | + } |
|
51 | + throw new InvalidClassException($fully_qualified_name); |
|
52 | + } |
|
53 | + $this->fully_qualified_name = $fully_qualified_name; |
|
54 | + } |
|
55 | 55 | |
56 | 56 | |
57 | - /** |
|
58 | - * @return string |
|
59 | - */ |
|
60 | - public function string() |
|
61 | - { |
|
62 | - return $this->fully_qualified_name; |
|
63 | - } |
|
57 | + /** |
|
58 | + * @return string |
|
59 | + */ |
|
60 | + public function string() |
|
61 | + { |
|
62 | + return $this->fully_qualified_name; |
|
63 | + } |
|
64 | 64 | |
65 | 65 | |
66 | - /** |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function __toString() |
|
70 | - { |
|
71 | - return $this->fully_qualified_name; |
|
72 | - } |
|
66 | + /** |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function __toString() |
|
70 | + { |
|
71 | + return $this->fully_qualified_name; |
|
72 | + } |
|
73 | 73 | |
74 | 74 | } |
@@ -91,7 +91,7 @@ |
||
91 | 91 | public function __construct($slug, $description) |
92 | 92 | { |
93 | 93 | parent::__construct($slug, $description); |
94 | - if (! in_array($this->slug(), $this->validRequestTypes(), true)) { |
|
94 | + if ( ! in_array($this->slug(), $this->validRequestTypes(), true)) { |
|
95 | 95 | throw new InvalidArgumentException( |
96 | 96 | sprintf( |
97 | 97 | esc_html__( |
@@ -16,155 +16,155 @@ |
||
16 | 16 | class RequestTypeContext extends Context |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * indicates that the current request involves some form of activation |
|
21 | - */ |
|
22 | - const ACTIVATION = 'activation-request'; |
|
23 | - |
|
24 | - /** |
|
25 | - * indicates that the current request is for the admin but is not being made via AJAX |
|
26 | - */ |
|
27 | - const ADMIN = 'non-ajax-admin-request'; |
|
28 | - |
|
29 | - /** |
|
30 | - * indicates that the current request is for the admin AND is being made via AJAX |
|
31 | - */ |
|
32 | - const AJAX_ADMIN = 'admin-ajax-request'; |
|
33 | - |
|
34 | - /** |
|
35 | - * indicates that the current request is for the frontend AND is being made via AJAX |
|
36 | - */ |
|
37 | - const AJAX_FRONT = 'frontend-ajax-request'; |
|
38 | - |
|
39 | - /** |
|
40 | - * indicates that the current request is for the WP Heartbeat |
|
41 | - */ |
|
42 | - const AJAX_HEARTBEAT = 'admin-ajax-heartbeat'; |
|
43 | - |
|
44 | - /** |
|
45 | - * indicates that the current request is being made via AJAX, but is NOT for EE |
|
46 | - */ |
|
47 | - const AJAX_OTHER = 'other-ajax-request'; |
|
48 | - |
|
49 | - /** |
|
50 | - * indicates that the current request is for the EE REST API |
|
51 | - */ |
|
52 | - const API = 'rest-api'; |
|
53 | - |
|
54 | - /** |
|
55 | - * indicates that the current request is from the command line |
|
56 | - */ |
|
57 | - const CLI = 'command-line'; |
|
58 | - |
|
59 | - /** |
|
60 | - * indicates that the current request is for a WP_Cron |
|
61 | - */ |
|
62 | - const CRON = 'wp-cron'; |
|
63 | - |
|
64 | - /** |
|
65 | - * indicates that the current request is for a feed (ie: RSS) |
|
66 | - */ |
|
67 | - const FEED = 'feed-request'; |
|
68 | - |
|
69 | - /** |
|
70 | - * indicates that the current request is for the frontend but is not being made via AJAX |
|
71 | - */ |
|
72 | - const FRONTEND = 'non-ajax-frontend-request'; |
|
73 | - |
|
74 | - /** |
|
75 | - * indicates that the current request is for content that is to be displayed within an iframe |
|
76 | - */ |
|
77 | - const IFRAME = 'iframe-request'; |
|
78 | - |
|
79 | - /** |
|
80 | - * indicates that the current request is for the WP REST API |
|
81 | - */ |
|
82 | - const WP_API = 'wp-rest-api'; |
|
83 | - |
|
84 | - /** |
|
85 | - * indicates that the current request is a loopback sent from WP core to test for errors |
|
86 | - */ |
|
87 | - const WP_SCRAPE = 'wordpress-scrape'; |
|
88 | - |
|
89 | - /** |
|
90 | - * @var boolean $is_activation |
|
91 | - */ |
|
92 | - private $is_activation = false; |
|
93 | - |
|
94 | - /** |
|
95 | - * @var array $valid_request_types |
|
96 | - */ |
|
97 | - private $valid_request_types = array(); |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * RequestTypeContext constructor. |
|
102 | - * |
|
103 | - * @param string $slug |
|
104 | - * @param string $description |
|
105 | - * @throws InvalidArgumentException |
|
106 | - */ |
|
107 | - public function __construct($slug, $description) |
|
108 | - { |
|
109 | - parent::__construct($slug, $description); |
|
110 | - if (! in_array($this->slug(), $this->validRequestTypes(), true)) { |
|
111 | - throw new InvalidArgumentException( |
|
112 | - sprintf( |
|
113 | - esc_html__( |
|
114 | - 'The RequestTypeContext slug must be one of the following values: %1$s %2$s', |
|
115 | - 'event_espresso' |
|
116 | - ), |
|
117 | - var_export($this->validRequestTypes(), true) |
|
118 | - ) |
|
119 | - ); |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * @return array |
|
126 | - */ |
|
127 | - public function validRequestTypes() |
|
128 | - { |
|
129 | - if (empty($this->valid_request_types)) { |
|
130 | - $this->valid_request_types = apply_filters( |
|
131 | - 'FHEE__EventEspresso_core_domain_entities_contexts_RequestTypeContext__validRequestTypes', |
|
132 | - array( |
|
133 | - RequestTypeContext::ACTIVATION, |
|
134 | - RequestTypeContext::ADMIN, |
|
135 | - RequestTypeContext::AJAX_ADMIN, |
|
136 | - RequestTypeContext::AJAX_FRONT, |
|
137 | - RequestTypeContext::AJAX_HEARTBEAT, |
|
138 | - RequestTypeContext::AJAX_OTHER, |
|
139 | - RequestTypeContext::API, |
|
140 | - RequestTypeContext::CLI, |
|
141 | - RequestTypeContext::CRON, |
|
142 | - RequestTypeContext::FEED, |
|
143 | - RequestTypeContext::FRONTEND, |
|
144 | - RequestTypeContext::IFRAME, |
|
145 | - RequestTypeContext::WP_API, |
|
146 | - RequestTypeContext::WP_SCRAPE, |
|
147 | - ) |
|
148 | - ); |
|
149 | - } |
|
150 | - return $this->valid_request_types; |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * @return bool |
|
156 | - */ |
|
157 | - public function isActivation() |
|
158 | - { |
|
159 | - return $this->is_activation; |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * @param bool $is_activation |
|
165 | - */ |
|
166 | - public function setIsActivation($is_activation) |
|
167 | - { |
|
168 | - $this->is_activation = filter_var($is_activation, FILTER_VALIDATE_BOOLEAN); |
|
169 | - } |
|
19 | + /** |
|
20 | + * indicates that the current request involves some form of activation |
|
21 | + */ |
|
22 | + const ACTIVATION = 'activation-request'; |
|
23 | + |
|
24 | + /** |
|
25 | + * indicates that the current request is for the admin but is not being made via AJAX |
|
26 | + */ |
|
27 | + const ADMIN = 'non-ajax-admin-request'; |
|
28 | + |
|
29 | + /** |
|
30 | + * indicates that the current request is for the admin AND is being made via AJAX |
|
31 | + */ |
|
32 | + const AJAX_ADMIN = 'admin-ajax-request'; |
|
33 | + |
|
34 | + /** |
|
35 | + * indicates that the current request is for the frontend AND is being made via AJAX |
|
36 | + */ |
|
37 | + const AJAX_FRONT = 'frontend-ajax-request'; |
|
38 | + |
|
39 | + /** |
|
40 | + * indicates that the current request is for the WP Heartbeat |
|
41 | + */ |
|
42 | + const AJAX_HEARTBEAT = 'admin-ajax-heartbeat'; |
|
43 | + |
|
44 | + /** |
|
45 | + * indicates that the current request is being made via AJAX, but is NOT for EE |
|
46 | + */ |
|
47 | + const AJAX_OTHER = 'other-ajax-request'; |
|
48 | + |
|
49 | + /** |
|
50 | + * indicates that the current request is for the EE REST API |
|
51 | + */ |
|
52 | + const API = 'rest-api'; |
|
53 | + |
|
54 | + /** |
|
55 | + * indicates that the current request is from the command line |
|
56 | + */ |
|
57 | + const CLI = 'command-line'; |
|
58 | + |
|
59 | + /** |
|
60 | + * indicates that the current request is for a WP_Cron |
|
61 | + */ |
|
62 | + const CRON = 'wp-cron'; |
|
63 | + |
|
64 | + /** |
|
65 | + * indicates that the current request is for a feed (ie: RSS) |
|
66 | + */ |
|
67 | + const FEED = 'feed-request'; |
|
68 | + |
|
69 | + /** |
|
70 | + * indicates that the current request is for the frontend but is not being made via AJAX |
|
71 | + */ |
|
72 | + const FRONTEND = 'non-ajax-frontend-request'; |
|
73 | + |
|
74 | + /** |
|
75 | + * indicates that the current request is for content that is to be displayed within an iframe |
|
76 | + */ |
|
77 | + const IFRAME = 'iframe-request'; |
|
78 | + |
|
79 | + /** |
|
80 | + * indicates that the current request is for the WP REST API |
|
81 | + */ |
|
82 | + const WP_API = 'wp-rest-api'; |
|
83 | + |
|
84 | + /** |
|
85 | + * indicates that the current request is a loopback sent from WP core to test for errors |
|
86 | + */ |
|
87 | + const WP_SCRAPE = 'wordpress-scrape'; |
|
88 | + |
|
89 | + /** |
|
90 | + * @var boolean $is_activation |
|
91 | + */ |
|
92 | + private $is_activation = false; |
|
93 | + |
|
94 | + /** |
|
95 | + * @var array $valid_request_types |
|
96 | + */ |
|
97 | + private $valid_request_types = array(); |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * RequestTypeContext constructor. |
|
102 | + * |
|
103 | + * @param string $slug |
|
104 | + * @param string $description |
|
105 | + * @throws InvalidArgumentException |
|
106 | + */ |
|
107 | + public function __construct($slug, $description) |
|
108 | + { |
|
109 | + parent::__construct($slug, $description); |
|
110 | + if (! in_array($this->slug(), $this->validRequestTypes(), true)) { |
|
111 | + throw new InvalidArgumentException( |
|
112 | + sprintf( |
|
113 | + esc_html__( |
|
114 | + 'The RequestTypeContext slug must be one of the following values: %1$s %2$s', |
|
115 | + 'event_espresso' |
|
116 | + ), |
|
117 | + var_export($this->validRequestTypes(), true) |
|
118 | + ) |
|
119 | + ); |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * @return array |
|
126 | + */ |
|
127 | + public function validRequestTypes() |
|
128 | + { |
|
129 | + if (empty($this->valid_request_types)) { |
|
130 | + $this->valid_request_types = apply_filters( |
|
131 | + 'FHEE__EventEspresso_core_domain_entities_contexts_RequestTypeContext__validRequestTypes', |
|
132 | + array( |
|
133 | + RequestTypeContext::ACTIVATION, |
|
134 | + RequestTypeContext::ADMIN, |
|
135 | + RequestTypeContext::AJAX_ADMIN, |
|
136 | + RequestTypeContext::AJAX_FRONT, |
|
137 | + RequestTypeContext::AJAX_HEARTBEAT, |
|
138 | + RequestTypeContext::AJAX_OTHER, |
|
139 | + RequestTypeContext::API, |
|
140 | + RequestTypeContext::CLI, |
|
141 | + RequestTypeContext::CRON, |
|
142 | + RequestTypeContext::FEED, |
|
143 | + RequestTypeContext::FRONTEND, |
|
144 | + RequestTypeContext::IFRAME, |
|
145 | + RequestTypeContext::WP_API, |
|
146 | + RequestTypeContext::WP_SCRAPE, |
|
147 | + ) |
|
148 | + ); |
|
149 | + } |
|
150 | + return $this->valid_request_types; |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * @return bool |
|
156 | + */ |
|
157 | + public function isActivation() |
|
158 | + { |
|
159 | + return $this->is_activation; |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * @param bool $is_activation |
|
165 | + */ |
|
166 | + public function setIsActivation($is_activation) |
|
167 | + { |
|
168 | + $this->is_activation = filter_var($is_activation, FILTER_VALIDATE_BOOLEAN); |
|
169 | + } |
|
170 | 170 | } |
@@ -505,7 +505,7 @@ |
||
505 | 505 | * |
506 | 506 | * @param string $value string to evaluate |
507 | 507 | * @param array $valid_shortcodes array of shortcodes that are acceptable. |
508 | - * @return mixed (bool|string) return either a list of invalid shortcodes OR false if the shortcodes validate. |
|
508 | + * @return false|string (bool|string) return either a list of invalid shortcodes OR false if the shortcodes validate. |
|
509 | 509 | */ |
510 | 510 | protected function _invalid_shortcodes($value, $valid_shortcodes) |
511 | 511 | { |
@@ -18,626 +18,626 @@ |
||
18 | 18 | { |
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * These properties just hold the name for the Messenger and Message Type (defined by child classes). |
|
23 | - * These are used for retrieving objects etc. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $_m_name; |
|
28 | - protected $_mt_name; |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * This will hold any error messages from the validation process. |
|
33 | - * The _errors property holds an associative array of error messages |
|
34 | - * listing the field as the key and the message as the value. |
|
35 | - * |
|
36 | - * @var array() |
|
37 | - */ |
|
38 | - private $_errors = array(); |
|
39 | - |
|
40 | - |
|
41 | - /** |
|
42 | - * holds an array of fields being validated |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - protected $_fields; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * this will hold the incoming context |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $_context; |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * this holds an array of fields and the relevant validation information |
|
59 | - * that the incoming fields data get validated against. |
|
60 | - * This gets setup in the _set_props() method. |
|
61 | - * |
|
62 | - * @var array |
|
63 | - */ |
|
64 | - protected $_validators; |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * holds the messenger object |
|
69 | - * |
|
70 | - * @var object |
|
71 | - */ |
|
72 | - protected $_messenger; |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * holds the message type object |
|
77 | - * |
|
78 | - * @var object |
|
79 | - */ |
|
80 | - protected $_message_type; |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * will hold any valid_shortcode modifications made by the _modify_validator() method. |
|
85 | - * |
|
86 | - * @var array |
|
87 | - */ |
|
88 | - protected $_valid_shortcodes_modifier; |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * There may be times where a message type wants to include a shortcode group but exclude specific |
|
93 | - * shortcodes. If that's the case then it can set this property as an array of shortcodes to exclude and |
|
94 | - * they will not be allowed. |
|
95 | - * Array should be indexed by field and values are an array of specific shortcodes to exclude. |
|
96 | - * |
|
97 | - * @var array |
|
98 | - */ |
|
99 | - protected $_specific_shortcode_excludes = array(); |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * Runs the validator using the incoming fields array as the fields/values to check. |
|
104 | - * |
|
105 | - * @param array $fields The fields sent by the EEM object. |
|
106 | - * @param $context |
|
107 | - * @throws EE_Error |
|
108 | - * @throws ReflectionException |
|
109 | - */ |
|
110 | - public function __construct($fields, $context) |
|
111 | - { |
|
112 | - // check that _m_name and _mt_name have been set by child class otherwise we get out. |
|
113 | - if (empty($this->_m_name) || empty($this->_mt_name)) { |
|
114 | - throw new EE_Error( |
|
115 | - esc_html__( |
|
116 | - 'EE_Messages_Validator child classes MUST set the $_m_name and $_mt_name property. Check that the child class is doing this', |
|
117 | - 'event_espresso' |
|
118 | - ) |
|
119 | - ); |
|
120 | - } |
|
121 | - $this->_fields = $fields; |
|
122 | - $this->_context = $context; |
|
123 | - |
|
124 | - // load messenger and message_type objects and the related shortcode objects. |
|
125 | - $this->_load_objects(); |
|
126 | - |
|
127 | - |
|
128 | - // modify any messenger/message_type specific validation instructions. This is what child classes define. |
|
129 | - $this->_modify_validator(); |
|
130 | - |
|
131 | - |
|
132 | - // let's set validators property |
|
133 | - $this->_set_validators(); |
|
134 | - } |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * Child classes instantiate this and use it to modify the _validator_config array property |
|
139 | - * for the messenger using messengers set_validate_config() method. |
|
140 | - * This is so we can specify specific validation instructions for a messenger/message_type combo |
|
141 | - * that aren't handled by the defaults setup in the messenger. |
|
142 | - * |
|
143 | - * @abstract |
|
144 | - * @access protected |
|
145 | - * @return void |
|
146 | - */ |
|
147 | - abstract protected function _modify_validator(); |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * loads all objects used by validator |
|
152 | - * |
|
153 | - * @access private |
|
154 | - * @throws \EE_Error |
|
155 | - */ |
|
156 | - private function _load_objects() |
|
157 | - { |
|
158 | - // load messenger |
|
159 | - $messenger = ucwords(str_replace('_', ' ', $this->_m_name)); |
|
160 | - $messenger = str_replace(' ', '_', $messenger); |
|
161 | - $messenger = 'EE_' . $messenger . '_messenger'; |
|
162 | - |
|
163 | - if (! class_exists($messenger)) { |
|
164 | - throw new EE_Error( |
|
165 | - sprintf( |
|
166 | - esc_html__('There is no messenger class for the given string (%s)', 'event_espresso'), |
|
167 | - $this->_m_name |
|
168 | - ) |
|
169 | - ); |
|
170 | - } |
|
171 | - |
|
172 | - $this->_messenger = new $messenger(); |
|
173 | - |
|
174 | - // load message type |
|
175 | - $message_type = ucwords(str_replace('_', ' ', $this->_mt_name)); |
|
176 | - $message_type = str_replace(' ', '_', $message_type); |
|
177 | - $message_type = 'EE_' . $message_type . '_message_type'; |
|
178 | - |
|
179 | - if (! class_exists($message_type)) { |
|
180 | - throw new EE_Error( |
|
181 | - sprintf( |
|
182 | - esc_html__('There is no message type class for the given string (%s)', 'event_espresso'), |
|
183 | - $this->_mt_name |
|
184 | - ) |
|
185 | - ); |
|
186 | - } |
|
187 | - |
|
188 | - $this->_message_type = new $message_type(); |
|
189 | - } |
|
190 | - |
|
191 | - |
|
192 | - /** |
|
193 | - * used to set the $_validators property |
|
194 | - * |
|
195 | - * @access private |
|
196 | - * @return void |
|
197 | - * @throws ReflectionException |
|
198 | - */ |
|
199 | - private function _set_validators() |
|
200 | - { |
|
201 | - // let's get all valid shortcodes from mt and message type |
|
202 | - // (messenger will have its set in the _validator_config property for the messenger) |
|
203 | - $mt_codes = $this->_message_type->get_valid_shortcodes(); |
|
204 | - |
|
205 | - |
|
206 | - // get messenger validator_config |
|
207 | - $msgr_validator = $this->_messenger->get_validator_config(); |
|
208 | - |
|
209 | - |
|
210 | - // we only want the valid shortcodes for the given context! |
|
211 | - $context = $this->_context; |
|
212 | - $mt_codes = $mt_codes[ $context ]; |
|
213 | - |
|
214 | - // in this first loop we're just getting all shortcode group indexes from the msgr_validator |
|
215 | - // into a single array (so we can get the appropriate shortcode objects for the groups) |
|
216 | - $shortcode_groups = $mt_codes; |
|
217 | - $groups_per_field = array(); |
|
218 | - |
|
219 | - foreach ($msgr_validator as $field => $config) { |
|
220 | - if (empty($config) || ! isset($config['shortcodes'])) { |
|
221 | - continue; |
|
222 | - } //Nothing to see here. |
|
223 | - $groups_per_field[ $field ] = array_intersect($config['shortcodes'], $mt_codes); |
|
224 | - $shortcode_groups = array_merge($config['shortcodes'], $shortcode_groups); |
|
225 | - } |
|
226 | - |
|
227 | - $shortcode_groups = array_unique($shortcode_groups); |
|
228 | - |
|
229 | - // okay now we've got our groups. |
|
230 | - // Let's get the codes from the objects into an array indexed by group for easy retrieval later. |
|
231 | - $codes_from_objs = array(); |
|
232 | - |
|
233 | - foreach ($shortcode_groups as $group) { |
|
234 | - $ref = ucwords(str_replace('_', ' ', $group)); |
|
235 | - $ref = str_replace(' ', '_', $ref); |
|
236 | - $classname = 'EE_' . $ref . '_Shortcodes'; |
|
237 | - if (class_exists($classname)) { |
|
238 | - $a = new ReflectionClass($classname); |
|
239 | - $obj = $a->newInstance(); |
|
240 | - $codes_from_objs[ $group ] = $obj->get_shortcodes(); |
|
241 | - } |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - // let's just replace the $mt shortcode group indexes with the actual shortcodes (unique) |
|
246 | - $final_mt_codes = array(); |
|
247 | - foreach ($mt_codes as $group) { |
|
248 | - $final_mt_codes = array_merge($final_mt_codes, $codes_from_objs[ $group ]); |
|
249 | - } |
|
250 | - |
|
251 | - $mt_codes = $final_mt_codes; |
|
252 | - |
|
253 | - |
|
254 | - // k now in this next loop we're going to loop through $msgr_validator again |
|
255 | - // and setup the _validators property from the data we've setup so far. |
|
256 | - foreach ($msgr_validator as $field => $config) { |
|
257 | - // if required shortcode is not in our list of codes for the given field, then we skip this field. |
|
258 | - $required = isset($config['required']) |
|
259 | - ? array_intersect($config['required'], array_keys($mt_codes)) |
|
260 | - : true; |
|
261 | - if (empty($required)) { |
|
262 | - continue; |
|
263 | - } |
|
264 | - |
|
265 | - // If we have an override then we use it to indicate the codes we want. |
|
266 | - if (isset($this->_valid_shortcodes_modifier[ $context ][ $field ])) { |
|
267 | - $this->_validators[ $field ]['shortcodes'] = $this->_reassemble_valid_shortcodes_from_group( |
|
268 | - $this->_valid_shortcodes_modifier[ $context ][ $field ], |
|
269 | - $codes_from_objs |
|
270 | - ); |
|
271 | - } //if we have specific shortcodes for a field then we need to use them |
|
272 | - elseif (isset($groups_per_field[ $field ])) { |
|
273 | - $this->_validators[ $field ]['shortcodes'] = $this->_reassemble_valid_shortcodes_from_group( |
|
274 | - $groups_per_field[ $field ], |
|
275 | - $codes_from_objs |
|
276 | - ); |
|
277 | - } //if empty config then we're assuming we're just going to use the shortcodes from the message type context |
|
278 | - elseif (empty($config)) { |
|
279 | - $this->_validators[ $field ]['shortcodes'] = $mt_codes; |
|
280 | - } //if we have specific shortcodes then we need to use them |
|
281 | - elseif (isset($config['specific_shortcodes'])) { |
|
282 | - $this->_validators[ $field ]['shortcodes'] = $config['specific_shortcodes']; |
|
283 | - } //otherwise the shortcodes are what is set by the messenger for that field |
|
284 | - else { |
|
285 | - foreach ($config['shortcodes'] as $group) { |
|
286 | - $this->_validators[ $field ]['shortcodes'] = isset($this->_validators[ $field ]['shortcodes']) |
|
287 | - ? array_merge($this->_validators[ $field ]['shortcodes'], $codes_from_objs[ $group ]) |
|
288 | - : $codes_from_objs[ $group ]; |
|
289 | - } |
|
290 | - } |
|
291 | - |
|
292 | - // now let's just make sure that any excluded specific shortcodes are removed. |
|
293 | - $specific_excludes = $this->get_specific_shortcode_excludes(); |
|
294 | - if (isset($specific_excludes[ $field ])) { |
|
295 | - foreach ($specific_excludes[ $field ] as $sex) { |
|
296 | - if (isset($this->_validators[ $field ]['shortcodes'][ $sex ])) { |
|
297 | - unset($this->_validators[ $field ]['shortcodes'][ $sex ]); |
|
298 | - } |
|
299 | - } |
|
300 | - } |
|
301 | - |
|
302 | - // hey! don't forget to include the type if present! |
|
303 | - $this->_validators[ $field ]['type'] = isset($config['type']) ? $config['type'] : null; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - |
|
308 | - /** |
|
309 | - * This just returns the validators property that contains information |
|
310 | - * about the various shortcodes and their availability with each field |
|
311 | - * |
|
312 | - * @return array |
|
313 | - */ |
|
314 | - public function get_validators() |
|
315 | - { |
|
316 | - return $this->_validators; |
|
317 | - } |
|
318 | - |
|
319 | - |
|
320 | - /** |
|
321 | - * This simply returns the specific shortcode_excludes property that is set. |
|
322 | - * |
|
323 | - * @since 4.5.0 |
|
324 | - * @return array |
|
325 | - */ |
|
326 | - public function get_specific_shortcode_excludes() |
|
327 | - { |
|
328 | - // specific validator filter |
|
329 | - $shortcode_excludes = apply_filters( |
|
330 | - 'FHEE__' . get_class($this) . '__get_specific_shortcode_excludes;', |
|
331 | - $this->_specific_shortcode_excludes, |
|
332 | - $this->_context |
|
333 | - ); |
|
334 | - // global filter |
|
335 | - return apply_filters( |
|
336 | - 'FHEE__EE_Messages_Validator__get_specific_shortcode_excludes', |
|
337 | - $shortcode_excludes, |
|
338 | - $this->_context, |
|
339 | - $this |
|
340 | - ); |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * This is the main method that handles validation |
|
346 | - * What it does is loop through the _fields (the ones that get validated) |
|
347 | - * and checks them against the shortcodes array for the field and the 'type' indicated by the |
|
348 | - * |
|
349 | - * @access public |
|
350 | - * @return mixed (bool|array) if errors present we return the array otherwise true |
|
351 | - */ |
|
352 | - public function validate() |
|
353 | - { |
|
354 | - // some defaults |
|
355 | - $template_fields = $this->_messenger->get_template_fields(); |
|
356 | - // loop through the fields and check! |
|
357 | - foreach ($this->_fields as $field => $value) { |
|
358 | - $this->_errors[ $field ] = array(); |
|
359 | - $err_msg = ''; |
|
360 | - $field_label = ''; |
|
361 | - // if field is not present in the _validators array then we continue |
|
362 | - if (! isset($this->_validators[ $field ])) { |
|
363 | - unset($this->_errors[ $field ]); |
|
364 | - continue; |
|
365 | - } |
|
366 | - |
|
367 | - // get the translated field label! |
|
368 | - // first check if it's in the main fields list |
|
369 | - if (isset($template_fields[ $field ])) { |
|
370 | - if (empty($template_fields[ $field ])) { |
|
371 | - $field_label = $field; |
|
372 | - } //most likely the field is found in the 'extra' array. |
|
373 | - else { |
|
374 | - $field_label = $template_fields[ $field ]['label']; |
|
375 | - } |
|
376 | - } |
|
377 | - |
|
378 | - // if field label is empty OR is equal to the current field |
|
379 | - // then we need to loop through the 'extra' fields in the template_fields config (if present) |
|
380 | - if (isset($template_fields['extra']) && (empty($field_label) || $field_label === $field)) { |
|
381 | - foreach ($template_fields['extra'] as $main_field => $secondary_field) { |
|
382 | - foreach ($secondary_field as $name => $values) { |
|
383 | - if ($name === $field) { |
|
384 | - $field_label = $values['label']; |
|
385 | - } |
|
386 | - |
|
387 | - // if we've got a 'main' secondary field, let's see if that matches what field we're on |
|
388 | - // which means it contains the label for this field. |
|
389 | - if ($name === 'main' && $main_field === $field_label) { |
|
390 | - $field_label = $values['label']; |
|
391 | - } |
|
392 | - } |
|
393 | - } |
|
394 | - } |
|
395 | - |
|
396 | - // field is present. Let's validate shortcodes first (but only if shortcodes present). |
|
397 | - if (isset($this->_validators[ $field ]['shortcodes']) |
|
398 | - && ! empty($this->_validators[ $field ]['shortcodes']) |
|
399 | - ) { |
|
400 | - $invalid_shortcodes = $this->_invalid_shortcodes($value, $this->_validators[ $field ]['shortcodes']); |
|
401 | - // if true then that means there is a returned error message |
|
402 | - // that we'll need to add to the _errors array for this field. |
|
403 | - if ($invalid_shortcodes) { |
|
404 | - $v_s = array_keys($this->_validators[ $field ]['shortcodes']); |
|
405 | - $err_msg = sprintf( |
|
406 | - esc_html__( |
|
407 | - '%3$sThe following shortcodes were found in the "%1$s" field that ARE not valid: %2$s%4$s', |
|
408 | - 'event_espresso' |
|
409 | - ), |
|
410 | - '<strong>' . $field_label . '</strong>', |
|
411 | - $invalid_shortcodes, |
|
412 | - '<p>', |
|
413 | - '</p >' |
|
414 | - ); |
|
415 | - $err_msg .= sprintf( |
|
416 | - esc_html__('%2$sValid shortcodes for this field are: %1$s%3$s', 'event_espresso'), |
|
417 | - implode(', ', $v_s), |
|
418 | - '<strong>', |
|
419 | - '</strong>' |
|
420 | - ); |
|
421 | - } |
|
422 | - } |
|
423 | - |
|
424 | - // if there's a "type" to be validated then let's do that too. |
|
425 | - if (isset($this->_validators[ $field ]['type']) && ! empty($this->_validators[ $field ]['type'])) { |
|
426 | - switch ($this->_validators[ $field ]['type']) { |
|
427 | - case 'number': |
|
428 | - if (! is_numeric($value)) { |
|
429 | - $err_msg .= sprintf( |
|
430 | - esc_html__( |
|
431 | - '%3$sThe %1$s field is supposed to be a number. The value given (%2$s) is not. Please double-check and make sure the field contains a number%4$s', |
|
432 | - 'event_espresso' |
|
433 | - ), |
|
434 | - $field_label, |
|
435 | - $value, |
|
436 | - '<p>', |
|
437 | - '</p >' |
|
438 | - ); |
|
439 | - } |
|
440 | - break; |
|
441 | - case 'email': |
|
442 | - $valid_email = $this->_validate_email($value); |
|
443 | - if (! $valid_email) { |
|
444 | - $err_msg .= htmlentities( |
|
445 | - sprintf( |
|
446 | - esc_html__( |
|
447 | - 'The %1$s field has at least one string that is not a valid email address record. Valid emails are in the format: "Name <[email protected]>" or "[email protected]" and multiple emails can be separated by a comma.', |
|
448 | - 'event_espresso' |
|
449 | - ), |
|
450 | - $field_label |
|
451 | - ) |
|
452 | - ); |
|
453 | - } |
|
454 | - break; |
|
455 | - default: |
|
456 | - break; |
|
457 | - } |
|
458 | - } |
|
459 | - |
|
460 | - // if $err_msg isn't empty let's setup the _errors array for this field. |
|
461 | - if (! empty($err_msg)) { |
|
462 | - $this->_errors[ $field ]['msg'] = $err_msg; |
|
463 | - } else { |
|
464 | - unset($this->_errors[ $field ]); |
|
465 | - } |
|
466 | - } |
|
467 | - |
|
468 | - // if we have ANY errors, then we want to make sure we return the values |
|
469 | - // for ALL the fields so the user doesn't have to retype them all. |
|
470 | - if (! empty($this->_errors)) { |
|
471 | - foreach ($this->_fields as $field => $value) { |
|
472 | - $this->_errors[ $field ]['value'] = stripslashes($value); |
|
473 | - } |
|
474 | - } |
|
475 | - |
|
476 | - // return any errors or just TRUE if everything validates |
|
477 | - return empty($this->_errors) ? true : $this->_errors; |
|
478 | - } |
|
479 | - |
|
480 | - |
|
481 | - /** |
|
482 | - * Reassembles and returns an array of valid shortcodes |
|
483 | - * given the array of groups and array of shortcodes indexed by group. |
|
484 | - * |
|
485 | - * @param array $groups array of shortcode groups that we want shortcodes for |
|
486 | - * @param array $codes_from_objs All the codes available. |
|
487 | - * @return array an array of actual shortcodes (that will be used for validation). |
|
488 | - */ |
|
489 | - private function _reassemble_valid_shortcodes_from_group($groups, $codes_from_objs) |
|
490 | - { |
|
491 | - $shortcodes = array(); |
|
492 | - foreach ($groups as $group) { |
|
493 | - $shortcodes = array_merge($shortcodes, $codes_from_objs[ $group ]); |
|
494 | - } |
|
495 | - return $shortcodes; |
|
496 | - } |
|
497 | - |
|
498 | - |
|
499 | - /** |
|
500 | - * Validates a string against a list of accepted shortcodes |
|
501 | - * This function takes in an array of shortcodes |
|
502 | - * and makes sure that the given string ONLY contains shortcodes in that array. |
|
503 | - * |
|
504 | - * @param string $value string to evaluate |
|
505 | - * @param array $valid_shortcodes array of shortcodes that are acceptable. |
|
506 | - * @return mixed (bool|string) return either a list of invalid shortcodes OR false if the shortcodes validate. |
|
507 | - */ |
|
508 | - protected function _invalid_shortcodes($value, $valid_shortcodes) |
|
509 | - { |
|
510 | - // first we need to go through the string and get the shortcodes in the string |
|
511 | - preg_match_all('/(\[.+?\])/', $value, $matches); |
|
512 | - $incoming_shortcodes = (array) $matches[0]; |
|
513 | - |
|
514 | - // get a diff of the shortcodes in the string vs the valid shortcodes |
|
515 | - $diff = array_diff($incoming_shortcodes, array_keys($valid_shortcodes)); |
|
516 | - |
|
517 | - // we need to account for custom codes so let's loop through the diff and remove any of those type of codes |
|
518 | - foreach ($diff as $ind => $code) { |
|
519 | - if (preg_match('/(\[[A-Za-z0-9\_]+_\*)/', $code)) { |
|
520 | - // strip the shortcode so we just have the BASE string (i.e. [ANSWER_*] ) |
|
521 | - $dynamic_sc = preg_replace('/(_\*+.+)/', '_*]', $code); |
|
522 | - // does this exist in the $valid_shortcodes? If so then unset. |
|
523 | - if (isset($valid_shortcodes[ $dynamic_sc ])) { |
|
524 | - unset($diff[ $ind ]); |
|
525 | - } |
|
526 | - } |
|
527 | - } |
|
528 | - |
|
529 | - if (empty($diff)) { |
|
530 | - return false; |
|
531 | - } //there is no diff, we have no invalid shortcodes, so return |
|
532 | - |
|
533 | - // made it here? then let's assemble the error message |
|
534 | - $invalid_shortcodes = implode('</strong>,<strong>', $diff); |
|
535 | - $invalid_shortcodes = '<strong>' . $invalid_shortcodes . '</strong>'; |
|
536 | - return $invalid_shortcodes; |
|
537 | - } |
|
538 | - |
|
539 | - |
|
540 | - /** |
|
541 | - * Validates an incoming string and makes sure we have valid emails in the string. |
|
542 | - * |
|
543 | - * @param string $value incoming value to validate |
|
544 | - * @return bool true if the string validates, false if it doesn't |
|
545 | - */ |
|
546 | - protected function _validate_email($value) |
|
547 | - { |
|
548 | - $validate = true; |
|
549 | - $or_val = $value; |
|
550 | - |
|
551 | - // empty strings will validate because this is how a message template |
|
552 | - // for a particular context can be "turned off" (if there is no email then no message) |
|
553 | - if (empty($value)) { |
|
554 | - return $validate; |
|
555 | - } |
|
556 | - |
|
557 | - // first determine if there ARE any shortcodes. |
|
558 | - // If there are shortcodes and then later we find that there were no other valid emails |
|
559 | - // but the field isn't empty... |
|
560 | - // that means we've got extra commas that were left after stripping out shortcodes so probably still valid. |
|
561 | - $has_shortcodes = preg_match('/(\[.+?\])/', $value); |
|
562 | - |
|
563 | - // first we need to strip out all the shortcodes! |
|
564 | - $value = preg_replace('/(\[.+?\])/', '', $value); |
|
565 | - |
|
566 | - // if original value is not empty and new value is, then we've parsed out a shortcode |
|
567 | - // and we now have an empty string which DOES validate. |
|
568 | - // We also validate complete empty field for email because |
|
569 | - // its possible that this message is being "turned off" for a particular context |
|
570 | - |
|
571 | - |
|
572 | - if (! empty($or_val) && empty($value)) { |
|
573 | - return $validate; |
|
574 | - } |
|
575 | - |
|
576 | - // trim any commas from beginning and end of string ( after whitespace trimmed ); |
|
577 | - $value = trim(trim($value), ','); |
|
578 | - |
|
579 | - |
|
580 | - // next we need to split up the string if its comma delimited. |
|
581 | - $emails = explode(',', $value); |
|
582 | - $empty = false; // used to indicate that there is an empty comma. |
|
583 | - // now let's loop through the emails and do our checks |
|
584 | - foreach ($emails as $email) { |
|
585 | - if (empty($email)) { |
|
586 | - $empty = true; |
|
587 | - continue; |
|
588 | - } |
|
589 | - |
|
590 | - // trim whitespace |
|
591 | - $email = trim($email); |
|
592 | - // either its of type "[email protected]", or its of type "fname lname <[email protected]>" |
|
593 | - if (is_email($email)) { |
|
594 | - continue; |
|
595 | - } |
|
596 | - $matches = array(); |
|
597 | - $validate = preg_match('/(.*)<(.+)>/', $email, $matches) ? true : false; |
|
598 | - if ($validate && is_email($matches[2])) { |
|
599 | - continue; |
|
600 | - } |
|
601 | - return false; |
|
602 | - } |
|
603 | - |
|
604 | - $validate = $empty && ! $has_shortcodes ? false : $validate; |
|
605 | - |
|
606 | - return $validate; |
|
607 | - } |
|
608 | - |
|
609 | - |
|
610 | - /** |
|
611 | - * Magic getter |
|
612 | - * Using this to provide back compat with add-ons referencing deprecated properties. |
|
613 | - * |
|
614 | - * @param string $property Property being requested |
|
615 | - * @throws Exception |
|
616 | - * @return mixed |
|
617 | - */ |
|
618 | - public function __get($property) |
|
619 | - { |
|
620 | - $expected_properties_map = array( |
|
621 | - /** |
|
622 | - * @deprecated 4.9.0 |
|
623 | - */ |
|
624 | - '_MSGR' => '_messenger', |
|
625 | - /** |
|
626 | - * @deprecated 4.9.0 |
|
627 | - */ |
|
628 | - '_MSGTYP' => '_message_type', |
|
629 | - ); |
|
630 | - |
|
631 | - if (isset($expected_properties_map[ $property ])) { |
|
632 | - return $this->{$expected_properties_map[ $property ]}; |
|
633 | - } |
|
634 | - |
|
635 | - throw new Exception( |
|
636 | - sprintf( |
|
637 | - esc_html__('The property %1$s being requested on %2$s does not exist', 'event_espresso'), |
|
638 | - $property, |
|
639 | - get_class($this) |
|
640 | - ) |
|
641 | - ); |
|
642 | - } |
|
21 | + /** |
|
22 | + * These properties just hold the name for the Messenger and Message Type (defined by child classes). |
|
23 | + * These are used for retrieving objects etc. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $_m_name; |
|
28 | + protected $_mt_name; |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * This will hold any error messages from the validation process. |
|
33 | + * The _errors property holds an associative array of error messages |
|
34 | + * listing the field as the key and the message as the value. |
|
35 | + * |
|
36 | + * @var array() |
|
37 | + */ |
|
38 | + private $_errors = array(); |
|
39 | + |
|
40 | + |
|
41 | + /** |
|
42 | + * holds an array of fields being validated |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + protected $_fields; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * this will hold the incoming context |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $_context; |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * this holds an array of fields and the relevant validation information |
|
59 | + * that the incoming fields data get validated against. |
|
60 | + * This gets setup in the _set_props() method. |
|
61 | + * |
|
62 | + * @var array |
|
63 | + */ |
|
64 | + protected $_validators; |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * holds the messenger object |
|
69 | + * |
|
70 | + * @var object |
|
71 | + */ |
|
72 | + protected $_messenger; |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * holds the message type object |
|
77 | + * |
|
78 | + * @var object |
|
79 | + */ |
|
80 | + protected $_message_type; |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * will hold any valid_shortcode modifications made by the _modify_validator() method. |
|
85 | + * |
|
86 | + * @var array |
|
87 | + */ |
|
88 | + protected $_valid_shortcodes_modifier; |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * There may be times where a message type wants to include a shortcode group but exclude specific |
|
93 | + * shortcodes. If that's the case then it can set this property as an array of shortcodes to exclude and |
|
94 | + * they will not be allowed. |
|
95 | + * Array should be indexed by field and values are an array of specific shortcodes to exclude. |
|
96 | + * |
|
97 | + * @var array |
|
98 | + */ |
|
99 | + protected $_specific_shortcode_excludes = array(); |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * Runs the validator using the incoming fields array as the fields/values to check. |
|
104 | + * |
|
105 | + * @param array $fields The fields sent by the EEM object. |
|
106 | + * @param $context |
|
107 | + * @throws EE_Error |
|
108 | + * @throws ReflectionException |
|
109 | + */ |
|
110 | + public function __construct($fields, $context) |
|
111 | + { |
|
112 | + // check that _m_name and _mt_name have been set by child class otherwise we get out. |
|
113 | + if (empty($this->_m_name) || empty($this->_mt_name)) { |
|
114 | + throw new EE_Error( |
|
115 | + esc_html__( |
|
116 | + 'EE_Messages_Validator child classes MUST set the $_m_name and $_mt_name property. Check that the child class is doing this', |
|
117 | + 'event_espresso' |
|
118 | + ) |
|
119 | + ); |
|
120 | + } |
|
121 | + $this->_fields = $fields; |
|
122 | + $this->_context = $context; |
|
123 | + |
|
124 | + // load messenger and message_type objects and the related shortcode objects. |
|
125 | + $this->_load_objects(); |
|
126 | + |
|
127 | + |
|
128 | + // modify any messenger/message_type specific validation instructions. This is what child classes define. |
|
129 | + $this->_modify_validator(); |
|
130 | + |
|
131 | + |
|
132 | + // let's set validators property |
|
133 | + $this->_set_validators(); |
|
134 | + } |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * Child classes instantiate this and use it to modify the _validator_config array property |
|
139 | + * for the messenger using messengers set_validate_config() method. |
|
140 | + * This is so we can specify specific validation instructions for a messenger/message_type combo |
|
141 | + * that aren't handled by the defaults setup in the messenger. |
|
142 | + * |
|
143 | + * @abstract |
|
144 | + * @access protected |
|
145 | + * @return void |
|
146 | + */ |
|
147 | + abstract protected function _modify_validator(); |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * loads all objects used by validator |
|
152 | + * |
|
153 | + * @access private |
|
154 | + * @throws \EE_Error |
|
155 | + */ |
|
156 | + private function _load_objects() |
|
157 | + { |
|
158 | + // load messenger |
|
159 | + $messenger = ucwords(str_replace('_', ' ', $this->_m_name)); |
|
160 | + $messenger = str_replace(' ', '_', $messenger); |
|
161 | + $messenger = 'EE_' . $messenger . '_messenger'; |
|
162 | + |
|
163 | + if (! class_exists($messenger)) { |
|
164 | + throw new EE_Error( |
|
165 | + sprintf( |
|
166 | + esc_html__('There is no messenger class for the given string (%s)', 'event_espresso'), |
|
167 | + $this->_m_name |
|
168 | + ) |
|
169 | + ); |
|
170 | + } |
|
171 | + |
|
172 | + $this->_messenger = new $messenger(); |
|
173 | + |
|
174 | + // load message type |
|
175 | + $message_type = ucwords(str_replace('_', ' ', $this->_mt_name)); |
|
176 | + $message_type = str_replace(' ', '_', $message_type); |
|
177 | + $message_type = 'EE_' . $message_type . '_message_type'; |
|
178 | + |
|
179 | + if (! class_exists($message_type)) { |
|
180 | + throw new EE_Error( |
|
181 | + sprintf( |
|
182 | + esc_html__('There is no message type class for the given string (%s)', 'event_espresso'), |
|
183 | + $this->_mt_name |
|
184 | + ) |
|
185 | + ); |
|
186 | + } |
|
187 | + |
|
188 | + $this->_message_type = new $message_type(); |
|
189 | + } |
|
190 | + |
|
191 | + |
|
192 | + /** |
|
193 | + * used to set the $_validators property |
|
194 | + * |
|
195 | + * @access private |
|
196 | + * @return void |
|
197 | + * @throws ReflectionException |
|
198 | + */ |
|
199 | + private function _set_validators() |
|
200 | + { |
|
201 | + // let's get all valid shortcodes from mt and message type |
|
202 | + // (messenger will have its set in the _validator_config property for the messenger) |
|
203 | + $mt_codes = $this->_message_type->get_valid_shortcodes(); |
|
204 | + |
|
205 | + |
|
206 | + // get messenger validator_config |
|
207 | + $msgr_validator = $this->_messenger->get_validator_config(); |
|
208 | + |
|
209 | + |
|
210 | + // we only want the valid shortcodes for the given context! |
|
211 | + $context = $this->_context; |
|
212 | + $mt_codes = $mt_codes[ $context ]; |
|
213 | + |
|
214 | + // in this first loop we're just getting all shortcode group indexes from the msgr_validator |
|
215 | + // into a single array (so we can get the appropriate shortcode objects for the groups) |
|
216 | + $shortcode_groups = $mt_codes; |
|
217 | + $groups_per_field = array(); |
|
218 | + |
|
219 | + foreach ($msgr_validator as $field => $config) { |
|
220 | + if (empty($config) || ! isset($config['shortcodes'])) { |
|
221 | + continue; |
|
222 | + } //Nothing to see here. |
|
223 | + $groups_per_field[ $field ] = array_intersect($config['shortcodes'], $mt_codes); |
|
224 | + $shortcode_groups = array_merge($config['shortcodes'], $shortcode_groups); |
|
225 | + } |
|
226 | + |
|
227 | + $shortcode_groups = array_unique($shortcode_groups); |
|
228 | + |
|
229 | + // okay now we've got our groups. |
|
230 | + // Let's get the codes from the objects into an array indexed by group for easy retrieval later. |
|
231 | + $codes_from_objs = array(); |
|
232 | + |
|
233 | + foreach ($shortcode_groups as $group) { |
|
234 | + $ref = ucwords(str_replace('_', ' ', $group)); |
|
235 | + $ref = str_replace(' ', '_', $ref); |
|
236 | + $classname = 'EE_' . $ref . '_Shortcodes'; |
|
237 | + if (class_exists($classname)) { |
|
238 | + $a = new ReflectionClass($classname); |
|
239 | + $obj = $a->newInstance(); |
|
240 | + $codes_from_objs[ $group ] = $obj->get_shortcodes(); |
|
241 | + } |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + // let's just replace the $mt shortcode group indexes with the actual shortcodes (unique) |
|
246 | + $final_mt_codes = array(); |
|
247 | + foreach ($mt_codes as $group) { |
|
248 | + $final_mt_codes = array_merge($final_mt_codes, $codes_from_objs[ $group ]); |
|
249 | + } |
|
250 | + |
|
251 | + $mt_codes = $final_mt_codes; |
|
252 | + |
|
253 | + |
|
254 | + // k now in this next loop we're going to loop through $msgr_validator again |
|
255 | + // and setup the _validators property from the data we've setup so far. |
|
256 | + foreach ($msgr_validator as $field => $config) { |
|
257 | + // if required shortcode is not in our list of codes for the given field, then we skip this field. |
|
258 | + $required = isset($config['required']) |
|
259 | + ? array_intersect($config['required'], array_keys($mt_codes)) |
|
260 | + : true; |
|
261 | + if (empty($required)) { |
|
262 | + continue; |
|
263 | + } |
|
264 | + |
|
265 | + // If we have an override then we use it to indicate the codes we want. |
|
266 | + if (isset($this->_valid_shortcodes_modifier[ $context ][ $field ])) { |
|
267 | + $this->_validators[ $field ]['shortcodes'] = $this->_reassemble_valid_shortcodes_from_group( |
|
268 | + $this->_valid_shortcodes_modifier[ $context ][ $field ], |
|
269 | + $codes_from_objs |
|
270 | + ); |
|
271 | + } //if we have specific shortcodes for a field then we need to use them |
|
272 | + elseif (isset($groups_per_field[ $field ])) { |
|
273 | + $this->_validators[ $field ]['shortcodes'] = $this->_reassemble_valid_shortcodes_from_group( |
|
274 | + $groups_per_field[ $field ], |
|
275 | + $codes_from_objs |
|
276 | + ); |
|
277 | + } //if empty config then we're assuming we're just going to use the shortcodes from the message type context |
|
278 | + elseif (empty($config)) { |
|
279 | + $this->_validators[ $field ]['shortcodes'] = $mt_codes; |
|
280 | + } //if we have specific shortcodes then we need to use them |
|
281 | + elseif (isset($config['specific_shortcodes'])) { |
|
282 | + $this->_validators[ $field ]['shortcodes'] = $config['specific_shortcodes']; |
|
283 | + } //otherwise the shortcodes are what is set by the messenger for that field |
|
284 | + else { |
|
285 | + foreach ($config['shortcodes'] as $group) { |
|
286 | + $this->_validators[ $field ]['shortcodes'] = isset($this->_validators[ $field ]['shortcodes']) |
|
287 | + ? array_merge($this->_validators[ $field ]['shortcodes'], $codes_from_objs[ $group ]) |
|
288 | + : $codes_from_objs[ $group ]; |
|
289 | + } |
|
290 | + } |
|
291 | + |
|
292 | + // now let's just make sure that any excluded specific shortcodes are removed. |
|
293 | + $specific_excludes = $this->get_specific_shortcode_excludes(); |
|
294 | + if (isset($specific_excludes[ $field ])) { |
|
295 | + foreach ($specific_excludes[ $field ] as $sex) { |
|
296 | + if (isset($this->_validators[ $field ]['shortcodes'][ $sex ])) { |
|
297 | + unset($this->_validators[ $field ]['shortcodes'][ $sex ]); |
|
298 | + } |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + // hey! don't forget to include the type if present! |
|
303 | + $this->_validators[ $field ]['type'] = isset($config['type']) ? $config['type'] : null; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + |
|
308 | + /** |
|
309 | + * This just returns the validators property that contains information |
|
310 | + * about the various shortcodes and their availability with each field |
|
311 | + * |
|
312 | + * @return array |
|
313 | + */ |
|
314 | + public function get_validators() |
|
315 | + { |
|
316 | + return $this->_validators; |
|
317 | + } |
|
318 | + |
|
319 | + |
|
320 | + /** |
|
321 | + * This simply returns the specific shortcode_excludes property that is set. |
|
322 | + * |
|
323 | + * @since 4.5.0 |
|
324 | + * @return array |
|
325 | + */ |
|
326 | + public function get_specific_shortcode_excludes() |
|
327 | + { |
|
328 | + // specific validator filter |
|
329 | + $shortcode_excludes = apply_filters( |
|
330 | + 'FHEE__' . get_class($this) . '__get_specific_shortcode_excludes;', |
|
331 | + $this->_specific_shortcode_excludes, |
|
332 | + $this->_context |
|
333 | + ); |
|
334 | + // global filter |
|
335 | + return apply_filters( |
|
336 | + 'FHEE__EE_Messages_Validator__get_specific_shortcode_excludes', |
|
337 | + $shortcode_excludes, |
|
338 | + $this->_context, |
|
339 | + $this |
|
340 | + ); |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * This is the main method that handles validation |
|
346 | + * What it does is loop through the _fields (the ones that get validated) |
|
347 | + * and checks them against the shortcodes array for the field and the 'type' indicated by the |
|
348 | + * |
|
349 | + * @access public |
|
350 | + * @return mixed (bool|array) if errors present we return the array otherwise true |
|
351 | + */ |
|
352 | + public function validate() |
|
353 | + { |
|
354 | + // some defaults |
|
355 | + $template_fields = $this->_messenger->get_template_fields(); |
|
356 | + // loop through the fields and check! |
|
357 | + foreach ($this->_fields as $field => $value) { |
|
358 | + $this->_errors[ $field ] = array(); |
|
359 | + $err_msg = ''; |
|
360 | + $field_label = ''; |
|
361 | + // if field is not present in the _validators array then we continue |
|
362 | + if (! isset($this->_validators[ $field ])) { |
|
363 | + unset($this->_errors[ $field ]); |
|
364 | + continue; |
|
365 | + } |
|
366 | + |
|
367 | + // get the translated field label! |
|
368 | + // first check if it's in the main fields list |
|
369 | + if (isset($template_fields[ $field ])) { |
|
370 | + if (empty($template_fields[ $field ])) { |
|
371 | + $field_label = $field; |
|
372 | + } //most likely the field is found in the 'extra' array. |
|
373 | + else { |
|
374 | + $field_label = $template_fields[ $field ]['label']; |
|
375 | + } |
|
376 | + } |
|
377 | + |
|
378 | + // if field label is empty OR is equal to the current field |
|
379 | + // then we need to loop through the 'extra' fields in the template_fields config (if present) |
|
380 | + if (isset($template_fields['extra']) && (empty($field_label) || $field_label === $field)) { |
|
381 | + foreach ($template_fields['extra'] as $main_field => $secondary_field) { |
|
382 | + foreach ($secondary_field as $name => $values) { |
|
383 | + if ($name === $field) { |
|
384 | + $field_label = $values['label']; |
|
385 | + } |
|
386 | + |
|
387 | + // if we've got a 'main' secondary field, let's see if that matches what field we're on |
|
388 | + // which means it contains the label for this field. |
|
389 | + if ($name === 'main' && $main_field === $field_label) { |
|
390 | + $field_label = $values['label']; |
|
391 | + } |
|
392 | + } |
|
393 | + } |
|
394 | + } |
|
395 | + |
|
396 | + // field is present. Let's validate shortcodes first (but only if shortcodes present). |
|
397 | + if (isset($this->_validators[ $field ]['shortcodes']) |
|
398 | + && ! empty($this->_validators[ $field ]['shortcodes']) |
|
399 | + ) { |
|
400 | + $invalid_shortcodes = $this->_invalid_shortcodes($value, $this->_validators[ $field ]['shortcodes']); |
|
401 | + // if true then that means there is a returned error message |
|
402 | + // that we'll need to add to the _errors array for this field. |
|
403 | + if ($invalid_shortcodes) { |
|
404 | + $v_s = array_keys($this->_validators[ $field ]['shortcodes']); |
|
405 | + $err_msg = sprintf( |
|
406 | + esc_html__( |
|
407 | + '%3$sThe following shortcodes were found in the "%1$s" field that ARE not valid: %2$s%4$s', |
|
408 | + 'event_espresso' |
|
409 | + ), |
|
410 | + '<strong>' . $field_label . '</strong>', |
|
411 | + $invalid_shortcodes, |
|
412 | + '<p>', |
|
413 | + '</p >' |
|
414 | + ); |
|
415 | + $err_msg .= sprintf( |
|
416 | + esc_html__('%2$sValid shortcodes for this field are: %1$s%3$s', 'event_espresso'), |
|
417 | + implode(', ', $v_s), |
|
418 | + '<strong>', |
|
419 | + '</strong>' |
|
420 | + ); |
|
421 | + } |
|
422 | + } |
|
423 | + |
|
424 | + // if there's a "type" to be validated then let's do that too. |
|
425 | + if (isset($this->_validators[ $field ]['type']) && ! empty($this->_validators[ $field ]['type'])) { |
|
426 | + switch ($this->_validators[ $field ]['type']) { |
|
427 | + case 'number': |
|
428 | + if (! is_numeric($value)) { |
|
429 | + $err_msg .= sprintf( |
|
430 | + esc_html__( |
|
431 | + '%3$sThe %1$s field is supposed to be a number. The value given (%2$s) is not. Please double-check and make sure the field contains a number%4$s', |
|
432 | + 'event_espresso' |
|
433 | + ), |
|
434 | + $field_label, |
|
435 | + $value, |
|
436 | + '<p>', |
|
437 | + '</p >' |
|
438 | + ); |
|
439 | + } |
|
440 | + break; |
|
441 | + case 'email': |
|
442 | + $valid_email = $this->_validate_email($value); |
|
443 | + if (! $valid_email) { |
|
444 | + $err_msg .= htmlentities( |
|
445 | + sprintf( |
|
446 | + esc_html__( |
|
447 | + 'The %1$s field has at least one string that is not a valid email address record. Valid emails are in the format: "Name <[email protected]>" or "[email protected]" and multiple emails can be separated by a comma.', |
|
448 | + 'event_espresso' |
|
449 | + ), |
|
450 | + $field_label |
|
451 | + ) |
|
452 | + ); |
|
453 | + } |
|
454 | + break; |
|
455 | + default: |
|
456 | + break; |
|
457 | + } |
|
458 | + } |
|
459 | + |
|
460 | + // if $err_msg isn't empty let's setup the _errors array for this field. |
|
461 | + if (! empty($err_msg)) { |
|
462 | + $this->_errors[ $field ]['msg'] = $err_msg; |
|
463 | + } else { |
|
464 | + unset($this->_errors[ $field ]); |
|
465 | + } |
|
466 | + } |
|
467 | + |
|
468 | + // if we have ANY errors, then we want to make sure we return the values |
|
469 | + // for ALL the fields so the user doesn't have to retype them all. |
|
470 | + if (! empty($this->_errors)) { |
|
471 | + foreach ($this->_fields as $field => $value) { |
|
472 | + $this->_errors[ $field ]['value'] = stripslashes($value); |
|
473 | + } |
|
474 | + } |
|
475 | + |
|
476 | + // return any errors or just TRUE if everything validates |
|
477 | + return empty($this->_errors) ? true : $this->_errors; |
|
478 | + } |
|
479 | + |
|
480 | + |
|
481 | + /** |
|
482 | + * Reassembles and returns an array of valid shortcodes |
|
483 | + * given the array of groups and array of shortcodes indexed by group. |
|
484 | + * |
|
485 | + * @param array $groups array of shortcode groups that we want shortcodes for |
|
486 | + * @param array $codes_from_objs All the codes available. |
|
487 | + * @return array an array of actual shortcodes (that will be used for validation). |
|
488 | + */ |
|
489 | + private function _reassemble_valid_shortcodes_from_group($groups, $codes_from_objs) |
|
490 | + { |
|
491 | + $shortcodes = array(); |
|
492 | + foreach ($groups as $group) { |
|
493 | + $shortcodes = array_merge($shortcodes, $codes_from_objs[ $group ]); |
|
494 | + } |
|
495 | + return $shortcodes; |
|
496 | + } |
|
497 | + |
|
498 | + |
|
499 | + /** |
|
500 | + * Validates a string against a list of accepted shortcodes |
|
501 | + * This function takes in an array of shortcodes |
|
502 | + * and makes sure that the given string ONLY contains shortcodes in that array. |
|
503 | + * |
|
504 | + * @param string $value string to evaluate |
|
505 | + * @param array $valid_shortcodes array of shortcodes that are acceptable. |
|
506 | + * @return mixed (bool|string) return either a list of invalid shortcodes OR false if the shortcodes validate. |
|
507 | + */ |
|
508 | + protected function _invalid_shortcodes($value, $valid_shortcodes) |
|
509 | + { |
|
510 | + // first we need to go through the string and get the shortcodes in the string |
|
511 | + preg_match_all('/(\[.+?\])/', $value, $matches); |
|
512 | + $incoming_shortcodes = (array) $matches[0]; |
|
513 | + |
|
514 | + // get a diff of the shortcodes in the string vs the valid shortcodes |
|
515 | + $diff = array_diff($incoming_shortcodes, array_keys($valid_shortcodes)); |
|
516 | + |
|
517 | + // we need to account for custom codes so let's loop through the diff and remove any of those type of codes |
|
518 | + foreach ($diff as $ind => $code) { |
|
519 | + if (preg_match('/(\[[A-Za-z0-9\_]+_\*)/', $code)) { |
|
520 | + // strip the shortcode so we just have the BASE string (i.e. [ANSWER_*] ) |
|
521 | + $dynamic_sc = preg_replace('/(_\*+.+)/', '_*]', $code); |
|
522 | + // does this exist in the $valid_shortcodes? If so then unset. |
|
523 | + if (isset($valid_shortcodes[ $dynamic_sc ])) { |
|
524 | + unset($diff[ $ind ]); |
|
525 | + } |
|
526 | + } |
|
527 | + } |
|
528 | + |
|
529 | + if (empty($diff)) { |
|
530 | + return false; |
|
531 | + } //there is no diff, we have no invalid shortcodes, so return |
|
532 | + |
|
533 | + // made it here? then let's assemble the error message |
|
534 | + $invalid_shortcodes = implode('</strong>,<strong>', $diff); |
|
535 | + $invalid_shortcodes = '<strong>' . $invalid_shortcodes . '</strong>'; |
|
536 | + return $invalid_shortcodes; |
|
537 | + } |
|
538 | + |
|
539 | + |
|
540 | + /** |
|
541 | + * Validates an incoming string and makes sure we have valid emails in the string. |
|
542 | + * |
|
543 | + * @param string $value incoming value to validate |
|
544 | + * @return bool true if the string validates, false if it doesn't |
|
545 | + */ |
|
546 | + protected function _validate_email($value) |
|
547 | + { |
|
548 | + $validate = true; |
|
549 | + $or_val = $value; |
|
550 | + |
|
551 | + // empty strings will validate because this is how a message template |
|
552 | + // for a particular context can be "turned off" (if there is no email then no message) |
|
553 | + if (empty($value)) { |
|
554 | + return $validate; |
|
555 | + } |
|
556 | + |
|
557 | + // first determine if there ARE any shortcodes. |
|
558 | + // If there are shortcodes and then later we find that there were no other valid emails |
|
559 | + // but the field isn't empty... |
|
560 | + // that means we've got extra commas that were left after stripping out shortcodes so probably still valid. |
|
561 | + $has_shortcodes = preg_match('/(\[.+?\])/', $value); |
|
562 | + |
|
563 | + // first we need to strip out all the shortcodes! |
|
564 | + $value = preg_replace('/(\[.+?\])/', '', $value); |
|
565 | + |
|
566 | + // if original value is not empty and new value is, then we've parsed out a shortcode |
|
567 | + // and we now have an empty string which DOES validate. |
|
568 | + // We also validate complete empty field for email because |
|
569 | + // its possible that this message is being "turned off" for a particular context |
|
570 | + |
|
571 | + |
|
572 | + if (! empty($or_val) && empty($value)) { |
|
573 | + return $validate; |
|
574 | + } |
|
575 | + |
|
576 | + // trim any commas from beginning and end of string ( after whitespace trimmed ); |
|
577 | + $value = trim(trim($value), ','); |
|
578 | + |
|
579 | + |
|
580 | + // next we need to split up the string if its comma delimited. |
|
581 | + $emails = explode(',', $value); |
|
582 | + $empty = false; // used to indicate that there is an empty comma. |
|
583 | + // now let's loop through the emails and do our checks |
|
584 | + foreach ($emails as $email) { |
|
585 | + if (empty($email)) { |
|
586 | + $empty = true; |
|
587 | + continue; |
|
588 | + } |
|
589 | + |
|
590 | + // trim whitespace |
|
591 | + $email = trim($email); |
|
592 | + // either its of type "[email protected]", or its of type "fname lname <[email protected]>" |
|
593 | + if (is_email($email)) { |
|
594 | + continue; |
|
595 | + } |
|
596 | + $matches = array(); |
|
597 | + $validate = preg_match('/(.*)<(.+)>/', $email, $matches) ? true : false; |
|
598 | + if ($validate && is_email($matches[2])) { |
|
599 | + continue; |
|
600 | + } |
|
601 | + return false; |
|
602 | + } |
|
603 | + |
|
604 | + $validate = $empty && ! $has_shortcodes ? false : $validate; |
|
605 | + |
|
606 | + return $validate; |
|
607 | + } |
|
608 | + |
|
609 | + |
|
610 | + /** |
|
611 | + * Magic getter |
|
612 | + * Using this to provide back compat with add-ons referencing deprecated properties. |
|
613 | + * |
|
614 | + * @param string $property Property being requested |
|
615 | + * @throws Exception |
|
616 | + * @return mixed |
|
617 | + */ |
|
618 | + public function __get($property) |
|
619 | + { |
|
620 | + $expected_properties_map = array( |
|
621 | + /** |
|
622 | + * @deprecated 4.9.0 |
|
623 | + */ |
|
624 | + '_MSGR' => '_messenger', |
|
625 | + /** |
|
626 | + * @deprecated 4.9.0 |
|
627 | + */ |
|
628 | + '_MSGTYP' => '_message_type', |
|
629 | + ); |
|
630 | + |
|
631 | + if (isset($expected_properties_map[ $property ])) { |
|
632 | + return $this->{$expected_properties_map[ $property ]}; |
|
633 | + } |
|
634 | + |
|
635 | + throw new Exception( |
|
636 | + sprintf( |
|
637 | + esc_html__('The property %1$s being requested on %2$s does not exist', 'event_espresso'), |
|
638 | + $property, |
|
639 | + get_class($this) |
|
640 | + ) |
|
641 | + ); |
|
642 | + } |
|
643 | 643 | } |
@@ -158,9 +158,9 @@ discard block |
||
158 | 158 | // load messenger |
159 | 159 | $messenger = ucwords(str_replace('_', ' ', $this->_m_name)); |
160 | 160 | $messenger = str_replace(' ', '_', $messenger); |
161 | - $messenger = 'EE_' . $messenger . '_messenger'; |
|
161 | + $messenger = 'EE_'.$messenger.'_messenger'; |
|
162 | 162 | |
163 | - if (! class_exists($messenger)) { |
|
163 | + if ( ! class_exists($messenger)) { |
|
164 | 164 | throw new EE_Error( |
165 | 165 | sprintf( |
166 | 166 | esc_html__('There is no messenger class for the given string (%s)', 'event_espresso'), |
@@ -174,9 +174,9 @@ discard block |
||
174 | 174 | // load message type |
175 | 175 | $message_type = ucwords(str_replace('_', ' ', $this->_mt_name)); |
176 | 176 | $message_type = str_replace(' ', '_', $message_type); |
177 | - $message_type = 'EE_' . $message_type . '_message_type'; |
|
177 | + $message_type = 'EE_'.$message_type.'_message_type'; |
|
178 | 178 | |
179 | - if (! class_exists($message_type)) { |
|
179 | + if ( ! class_exists($message_type)) { |
|
180 | 180 | throw new EE_Error( |
181 | 181 | sprintf( |
182 | 182 | esc_html__('There is no message type class for the given string (%s)', 'event_espresso'), |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | |
210 | 210 | // we only want the valid shortcodes for the given context! |
211 | 211 | $context = $this->_context; |
212 | - $mt_codes = $mt_codes[ $context ]; |
|
212 | + $mt_codes = $mt_codes[$context]; |
|
213 | 213 | |
214 | 214 | // in this first loop we're just getting all shortcode group indexes from the msgr_validator |
215 | 215 | // into a single array (so we can get the appropriate shortcode objects for the groups) |
@@ -220,8 +220,8 @@ discard block |
||
220 | 220 | if (empty($config) || ! isset($config['shortcodes'])) { |
221 | 221 | continue; |
222 | 222 | } //Nothing to see here. |
223 | - $groups_per_field[ $field ] = array_intersect($config['shortcodes'], $mt_codes); |
|
224 | - $shortcode_groups = array_merge($config['shortcodes'], $shortcode_groups); |
|
223 | + $groups_per_field[$field] = array_intersect($config['shortcodes'], $mt_codes); |
|
224 | + $shortcode_groups = array_merge($config['shortcodes'], $shortcode_groups); |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | $shortcode_groups = array_unique($shortcode_groups); |
@@ -233,11 +233,11 @@ discard block |
||
233 | 233 | foreach ($shortcode_groups as $group) { |
234 | 234 | $ref = ucwords(str_replace('_', ' ', $group)); |
235 | 235 | $ref = str_replace(' ', '_', $ref); |
236 | - $classname = 'EE_' . $ref . '_Shortcodes'; |
|
236 | + $classname = 'EE_'.$ref.'_Shortcodes'; |
|
237 | 237 | if (class_exists($classname)) { |
238 | 238 | $a = new ReflectionClass($classname); |
239 | 239 | $obj = $a->newInstance(); |
240 | - $codes_from_objs[ $group ] = $obj->get_shortcodes(); |
|
240 | + $codes_from_objs[$group] = $obj->get_shortcodes(); |
|
241 | 241 | } |
242 | 242 | } |
243 | 243 | |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | // let's just replace the $mt shortcode group indexes with the actual shortcodes (unique) |
246 | 246 | $final_mt_codes = array(); |
247 | 247 | foreach ($mt_codes as $group) { |
248 | - $final_mt_codes = array_merge($final_mt_codes, $codes_from_objs[ $group ]); |
|
248 | + $final_mt_codes = array_merge($final_mt_codes, $codes_from_objs[$group]); |
|
249 | 249 | } |
250 | 250 | |
251 | 251 | $mt_codes = $final_mt_codes; |
@@ -263,44 +263,44 @@ discard block |
||
263 | 263 | } |
264 | 264 | |
265 | 265 | // If we have an override then we use it to indicate the codes we want. |
266 | - if (isset($this->_valid_shortcodes_modifier[ $context ][ $field ])) { |
|
267 | - $this->_validators[ $field ]['shortcodes'] = $this->_reassemble_valid_shortcodes_from_group( |
|
268 | - $this->_valid_shortcodes_modifier[ $context ][ $field ], |
|
266 | + if (isset($this->_valid_shortcodes_modifier[$context][$field])) { |
|
267 | + $this->_validators[$field]['shortcodes'] = $this->_reassemble_valid_shortcodes_from_group( |
|
268 | + $this->_valid_shortcodes_modifier[$context][$field], |
|
269 | 269 | $codes_from_objs |
270 | 270 | ); |
271 | 271 | } //if we have specific shortcodes for a field then we need to use them |
272 | - elseif (isset($groups_per_field[ $field ])) { |
|
273 | - $this->_validators[ $field ]['shortcodes'] = $this->_reassemble_valid_shortcodes_from_group( |
|
274 | - $groups_per_field[ $field ], |
|
272 | + elseif (isset($groups_per_field[$field])) { |
|
273 | + $this->_validators[$field]['shortcodes'] = $this->_reassemble_valid_shortcodes_from_group( |
|
274 | + $groups_per_field[$field], |
|
275 | 275 | $codes_from_objs |
276 | 276 | ); |
277 | 277 | } //if empty config then we're assuming we're just going to use the shortcodes from the message type context |
278 | 278 | elseif (empty($config)) { |
279 | - $this->_validators[ $field ]['shortcodes'] = $mt_codes; |
|
279 | + $this->_validators[$field]['shortcodes'] = $mt_codes; |
|
280 | 280 | } //if we have specific shortcodes then we need to use them |
281 | 281 | elseif (isset($config['specific_shortcodes'])) { |
282 | - $this->_validators[ $field ]['shortcodes'] = $config['specific_shortcodes']; |
|
282 | + $this->_validators[$field]['shortcodes'] = $config['specific_shortcodes']; |
|
283 | 283 | } //otherwise the shortcodes are what is set by the messenger for that field |
284 | 284 | else { |
285 | 285 | foreach ($config['shortcodes'] as $group) { |
286 | - $this->_validators[ $field ]['shortcodes'] = isset($this->_validators[ $field ]['shortcodes']) |
|
287 | - ? array_merge($this->_validators[ $field ]['shortcodes'], $codes_from_objs[ $group ]) |
|
288 | - : $codes_from_objs[ $group ]; |
|
286 | + $this->_validators[$field]['shortcodes'] = isset($this->_validators[$field]['shortcodes']) |
|
287 | + ? array_merge($this->_validators[$field]['shortcodes'], $codes_from_objs[$group]) |
|
288 | + : $codes_from_objs[$group]; |
|
289 | 289 | } |
290 | 290 | } |
291 | 291 | |
292 | 292 | // now let's just make sure that any excluded specific shortcodes are removed. |
293 | 293 | $specific_excludes = $this->get_specific_shortcode_excludes(); |
294 | - if (isset($specific_excludes[ $field ])) { |
|
295 | - foreach ($specific_excludes[ $field ] as $sex) { |
|
296 | - if (isset($this->_validators[ $field ]['shortcodes'][ $sex ])) { |
|
297 | - unset($this->_validators[ $field ]['shortcodes'][ $sex ]); |
|
294 | + if (isset($specific_excludes[$field])) { |
|
295 | + foreach ($specific_excludes[$field] as $sex) { |
|
296 | + if (isset($this->_validators[$field]['shortcodes'][$sex])) { |
|
297 | + unset($this->_validators[$field]['shortcodes'][$sex]); |
|
298 | 298 | } |
299 | 299 | } |
300 | 300 | } |
301 | 301 | |
302 | 302 | // hey! don't forget to include the type if present! |
303 | - $this->_validators[ $field ]['type'] = isset($config['type']) ? $config['type'] : null; |
|
303 | + $this->_validators[$field]['type'] = isset($config['type']) ? $config['type'] : null; |
|
304 | 304 | } |
305 | 305 | } |
306 | 306 | |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | { |
328 | 328 | // specific validator filter |
329 | 329 | $shortcode_excludes = apply_filters( |
330 | - 'FHEE__' . get_class($this) . '__get_specific_shortcode_excludes;', |
|
330 | + 'FHEE__'.get_class($this).'__get_specific_shortcode_excludes;', |
|
331 | 331 | $this->_specific_shortcode_excludes, |
332 | 332 | $this->_context |
333 | 333 | ); |
@@ -355,23 +355,23 @@ discard block |
||
355 | 355 | $template_fields = $this->_messenger->get_template_fields(); |
356 | 356 | // loop through the fields and check! |
357 | 357 | foreach ($this->_fields as $field => $value) { |
358 | - $this->_errors[ $field ] = array(); |
|
358 | + $this->_errors[$field] = array(); |
|
359 | 359 | $err_msg = ''; |
360 | 360 | $field_label = ''; |
361 | 361 | // if field is not present in the _validators array then we continue |
362 | - if (! isset($this->_validators[ $field ])) { |
|
363 | - unset($this->_errors[ $field ]); |
|
362 | + if ( ! isset($this->_validators[$field])) { |
|
363 | + unset($this->_errors[$field]); |
|
364 | 364 | continue; |
365 | 365 | } |
366 | 366 | |
367 | 367 | // get the translated field label! |
368 | 368 | // first check if it's in the main fields list |
369 | - if (isset($template_fields[ $field ])) { |
|
370 | - if (empty($template_fields[ $field ])) { |
|
369 | + if (isset($template_fields[$field])) { |
|
370 | + if (empty($template_fields[$field])) { |
|
371 | 371 | $field_label = $field; |
372 | 372 | } //most likely the field is found in the 'extra' array. |
373 | 373 | else { |
374 | - $field_label = $template_fields[ $field ]['label']; |
|
374 | + $field_label = $template_fields[$field]['label']; |
|
375 | 375 | } |
376 | 376 | } |
377 | 377 | |
@@ -394,20 +394,20 @@ discard block |
||
394 | 394 | } |
395 | 395 | |
396 | 396 | // field is present. Let's validate shortcodes first (but only if shortcodes present). |
397 | - if (isset($this->_validators[ $field ]['shortcodes']) |
|
398 | - && ! empty($this->_validators[ $field ]['shortcodes']) |
|
397 | + if (isset($this->_validators[$field]['shortcodes']) |
|
398 | + && ! empty($this->_validators[$field]['shortcodes']) |
|
399 | 399 | ) { |
400 | - $invalid_shortcodes = $this->_invalid_shortcodes($value, $this->_validators[ $field ]['shortcodes']); |
|
400 | + $invalid_shortcodes = $this->_invalid_shortcodes($value, $this->_validators[$field]['shortcodes']); |
|
401 | 401 | // if true then that means there is a returned error message |
402 | 402 | // that we'll need to add to the _errors array for this field. |
403 | 403 | if ($invalid_shortcodes) { |
404 | - $v_s = array_keys($this->_validators[ $field ]['shortcodes']); |
|
404 | + $v_s = array_keys($this->_validators[$field]['shortcodes']); |
|
405 | 405 | $err_msg = sprintf( |
406 | 406 | esc_html__( |
407 | 407 | '%3$sThe following shortcodes were found in the "%1$s" field that ARE not valid: %2$s%4$s', |
408 | 408 | 'event_espresso' |
409 | 409 | ), |
410 | - '<strong>' . $field_label . '</strong>', |
|
410 | + '<strong>'.$field_label.'</strong>', |
|
411 | 411 | $invalid_shortcodes, |
412 | 412 | '<p>', |
413 | 413 | '</p >' |
@@ -422,10 +422,10 @@ discard block |
||
422 | 422 | } |
423 | 423 | |
424 | 424 | // if there's a "type" to be validated then let's do that too. |
425 | - if (isset($this->_validators[ $field ]['type']) && ! empty($this->_validators[ $field ]['type'])) { |
|
426 | - switch ($this->_validators[ $field ]['type']) { |
|
425 | + if (isset($this->_validators[$field]['type']) && ! empty($this->_validators[$field]['type'])) { |
|
426 | + switch ($this->_validators[$field]['type']) { |
|
427 | 427 | case 'number': |
428 | - if (! is_numeric($value)) { |
|
428 | + if ( ! is_numeric($value)) { |
|
429 | 429 | $err_msg .= sprintf( |
430 | 430 | esc_html__( |
431 | 431 | '%3$sThe %1$s field is supposed to be a number. The value given (%2$s) is not. Please double-check and make sure the field contains a number%4$s', |
@@ -440,7 +440,7 @@ discard block |
||
440 | 440 | break; |
441 | 441 | case 'email': |
442 | 442 | $valid_email = $this->_validate_email($value); |
443 | - if (! $valid_email) { |
|
443 | + if ( ! $valid_email) { |
|
444 | 444 | $err_msg .= htmlentities( |
445 | 445 | sprintf( |
446 | 446 | esc_html__( |
@@ -458,18 +458,18 @@ discard block |
||
458 | 458 | } |
459 | 459 | |
460 | 460 | // if $err_msg isn't empty let's setup the _errors array for this field. |
461 | - if (! empty($err_msg)) { |
|
462 | - $this->_errors[ $field ]['msg'] = $err_msg; |
|
461 | + if ( ! empty($err_msg)) { |
|
462 | + $this->_errors[$field]['msg'] = $err_msg; |
|
463 | 463 | } else { |
464 | - unset($this->_errors[ $field ]); |
|
464 | + unset($this->_errors[$field]); |
|
465 | 465 | } |
466 | 466 | } |
467 | 467 | |
468 | 468 | // if we have ANY errors, then we want to make sure we return the values |
469 | 469 | // for ALL the fields so the user doesn't have to retype them all. |
470 | - if (! empty($this->_errors)) { |
|
470 | + if ( ! empty($this->_errors)) { |
|
471 | 471 | foreach ($this->_fields as $field => $value) { |
472 | - $this->_errors[ $field ]['value'] = stripslashes($value); |
|
472 | + $this->_errors[$field]['value'] = stripslashes($value); |
|
473 | 473 | } |
474 | 474 | } |
475 | 475 | |
@@ -490,7 +490,7 @@ discard block |
||
490 | 490 | { |
491 | 491 | $shortcodes = array(); |
492 | 492 | foreach ($groups as $group) { |
493 | - $shortcodes = array_merge($shortcodes, $codes_from_objs[ $group ]); |
|
493 | + $shortcodes = array_merge($shortcodes, $codes_from_objs[$group]); |
|
494 | 494 | } |
495 | 495 | return $shortcodes; |
496 | 496 | } |
@@ -520,8 +520,8 @@ discard block |
||
520 | 520 | // strip the shortcode so we just have the BASE string (i.e. [ANSWER_*] ) |
521 | 521 | $dynamic_sc = preg_replace('/(_\*+.+)/', '_*]', $code); |
522 | 522 | // does this exist in the $valid_shortcodes? If so then unset. |
523 | - if (isset($valid_shortcodes[ $dynamic_sc ])) { |
|
524 | - unset($diff[ $ind ]); |
|
523 | + if (isset($valid_shortcodes[$dynamic_sc])) { |
|
524 | + unset($diff[$ind]); |
|
525 | 525 | } |
526 | 526 | } |
527 | 527 | } |
@@ -532,7 +532,7 @@ discard block |
||
532 | 532 | |
533 | 533 | // made it here? then let's assemble the error message |
534 | 534 | $invalid_shortcodes = implode('</strong>,<strong>', $diff); |
535 | - $invalid_shortcodes = '<strong>' . $invalid_shortcodes . '</strong>'; |
|
535 | + $invalid_shortcodes = '<strong>'.$invalid_shortcodes.'</strong>'; |
|
536 | 536 | return $invalid_shortcodes; |
537 | 537 | } |
538 | 538 | |
@@ -569,7 +569,7 @@ discard block |
||
569 | 569 | // its possible that this message is being "turned off" for a particular context |
570 | 570 | |
571 | 571 | |
572 | - if (! empty($or_val) && empty($value)) { |
|
572 | + if ( ! empty($or_val) && empty($value)) { |
|
573 | 573 | return $validate; |
574 | 574 | } |
575 | 575 | |
@@ -628,8 +628,8 @@ discard block |
||
628 | 628 | '_MSGTYP' => '_message_type', |
629 | 629 | ); |
630 | 630 | |
631 | - if (isset($expected_properties_map[ $property ])) { |
|
632 | - return $this->{$expected_properties_map[ $property ]}; |
|
631 | + if (isset($expected_properties_map[$property])) { |
|
632 | + return $this->{$expected_properties_map[$property]}; |
|
633 | 633 | } |
634 | 634 | |
635 | 635 | throw new Exception( |
@@ -11,14 +11,14 @@ |
||
11 | 11 | |
12 | 12 | |
13 | 13 | |
14 | - /** |
|
15 | - * removes all tags which a WP Post wouldn't allow in its content normally |
|
16 | - * |
|
17 | - * @param string $value |
|
18 | - * @return string |
|
19 | - */ |
|
20 | - public function prepare_for_set($value) |
|
21 | - { |
|
22 | - return parent::prepare_for_set(wp_kses("$value", EEH_HTML::get_simple_tags())); |
|
23 | - } |
|
14 | + /** |
|
15 | + * removes all tags which a WP Post wouldn't allow in its content normally |
|
16 | + * |
|
17 | + * @param string $value |
|
18 | + * @return string |
|
19 | + */ |
|
20 | + public function prepare_for_set($value) |
|
21 | + { |
|
22 | + return parent::prepare_for_set(wp_kses("$value", EEH_HTML::get_simple_tags())); |
|
23 | + } |
|
24 | 24 | } |
@@ -51,7 +51,7 @@ |
||
51 | 51 | */ |
52 | 52 | public function wp_user_obj() |
53 | 53 | { |
54 | - if (! $this->_wp_user_obj) { |
|
54 | + if ( ! $this->_wp_user_obj) { |
|
55 | 55 | $this->_wp_user_obj = get_user_by('ID', $this->ID()); |
56 | 56 | } |
57 | 57 | return $this->_wp_user_obj; |
@@ -11,91 +11,91 @@ |
||
11 | 11 | class EE_WP_User extends EE_Base_Class implements EEI_Admin_Links |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * @var WP_User |
|
16 | - */ |
|
17 | - protected $_wp_user_obj; |
|
14 | + /** |
|
15 | + * @var WP_User |
|
16 | + */ |
|
17 | + protected $_wp_user_obj; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param array $props_n_values |
|
21 | - * @return EE_WP_User|mixed |
|
22 | - */ |
|
23 | - public static function new_instance($props_n_values = array()) |
|
24 | - { |
|
25 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
26 | - return $has_object ? $has_object : new self($props_n_values); |
|
27 | - } |
|
19 | + /** |
|
20 | + * @param array $props_n_values |
|
21 | + * @return EE_WP_User|mixed |
|
22 | + */ |
|
23 | + public static function new_instance($props_n_values = array()) |
|
24 | + { |
|
25 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
26 | + return $has_object ? $has_object : new self($props_n_values); |
|
27 | + } |
|
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * @param array $props_n_values |
|
32 | - * @return EE_WP_User |
|
33 | - */ |
|
34 | - public static function new_instance_from_db($props_n_values = array()) |
|
35 | - { |
|
36 | - return new self($props_n_values, true); |
|
37 | - } |
|
30 | + /** |
|
31 | + * @param array $props_n_values |
|
32 | + * @return EE_WP_User |
|
33 | + */ |
|
34 | + public static function new_instance_from_db($props_n_values = array()) |
|
35 | + { |
|
36 | + return new self($props_n_values, true); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Return a normal WP_User object (caches the object for future calls) |
|
41 | - * |
|
42 | - * @return WP_User |
|
43 | - */ |
|
44 | - public function wp_user_obj() |
|
45 | - { |
|
46 | - if (! $this->_wp_user_obj) { |
|
47 | - $this->_wp_user_obj = get_user_by('ID', $this->ID()); |
|
48 | - } |
|
49 | - return $this->_wp_user_obj; |
|
50 | - } |
|
39 | + /** |
|
40 | + * Return a normal WP_User object (caches the object for future calls) |
|
41 | + * |
|
42 | + * @return WP_User |
|
43 | + */ |
|
44 | + public function wp_user_obj() |
|
45 | + { |
|
46 | + if (! $this->_wp_user_obj) { |
|
47 | + $this->_wp_user_obj = get_user_by('ID', $this->ID()); |
|
48 | + } |
|
49 | + return $this->_wp_user_obj; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Return the link to the admin details for the object. |
|
54 | - * |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public function get_admin_details_link() |
|
58 | - { |
|
59 | - return $this->get_admin_edit_link(); |
|
60 | - } |
|
52 | + /** |
|
53 | + * Return the link to the admin details for the object. |
|
54 | + * |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public function get_admin_details_link() |
|
58 | + { |
|
59 | + return $this->get_admin_edit_link(); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Returns the link to the editor for the object. Sometimes this is the same as the details. |
|
64 | - * |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public function get_admin_edit_link() |
|
68 | - { |
|
69 | - return esc_url( |
|
70 | - add_query_arg( |
|
71 | - 'wp_http_referer', |
|
72 | - urlencode( |
|
73 | - wp_unslash( |
|
74 | - $_SERVER['REQUEST_URI'] |
|
75 | - ) |
|
76 | - ), |
|
77 | - get_edit_user_link($this->ID()) |
|
78 | - ) |
|
79 | - ); |
|
80 | - } |
|
62 | + /** |
|
63 | + * Returns the link to the editor for the object. Sometimes this is the same as the details. |
|
64 | + * |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public function get_admin_edit_link() |
|
68 | + { |
|
69 | + return esc_url( |
|
70 | + add_query_arg( |
|
71 | + 'wp_http_referer', |
|
72 | + urlencode( |
|
73 | + wp_unslash( |
|
74 | + $_SERVER['REQUEST_URI'] |
|
75 | + ) |
|
76 | + ), |
|
77 | + get_edit_user_link($this->ID()) |
|
78 | + ) |
|
79 | + ); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Returns the link to a settings page for the object. |
|
84 | - * |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - public function get_admin_settings_link() |
|
88 | - { |
|
89 | - return $this->get_admin_edit_link(); |
|
90 | - } |
|
82 | + /** |
|
83 | + * Returns the link to a settings page for the object. |
|
84 | + * |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + public function get_admin_settings_link() |
|
88 | + { |
|
89 | + return $this->get_admin_edit_link(); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Returns the link to the "overview" for the object (typically the "list table" view). |
|
94 | - * |
|
95 | - * @return string |
|
96 | - */ |
|
97 | - public function get_admin_overview_link() |
|
98 | - { |
|
99 | - return admin_url('users.php'); |
|
100 | - } |
|
92 | + /** |
|
93 | + * Returns the link to the "overview" for the object (typically the "list table" view). |
|
94 | + * |
|
95 | + * @return string |
|
96 | + */ |
|
97 | + public function get_admin_overview_link() |
|
98 | + { |
|
99 | + return admin_url('users.php'); |
|
100 | + } |
|
101 | 101 | } |