@@ -362,62 +362,62 @@ |
||
362 | 362 | |
363 | 363 | |
364 | 364 | |
365 | - /** |
|
366 | - * @return EE_Base_Class[]|EE_Line_Item[] |
|
367 | - * @throws \EE_Error |
|
368 | - */ |
|
369 | - public function get_total_line_items_with_no_transaction() |
|
370 | - { |
|
371 | - return $this->get_total_line_items_for_carts(); |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - |
|
376 | - /** |
|
377 | - * @return EE_Base_Class[]|EE_Line_Item[] |
|
378 | - * @throws \EE_Error |
|
379 | - */ |
|
380 | - public function get_total_line_items_for_active_carts() |
|
381 | - { |
|
382 | - return $this->get_total_line_items_for_carts(false); |
|
383 | - } |
|
384 | - |
|
385 | - |
|
386 | - |
|
387 | - /** |
|
388 | - * @return EE_Base_Class[]|EE_Line_Item[] |
|
389 | - * @throws \EE_Error |
|
390 | - */ |
|
391 | - public function get_total_line_items_for_expired_carts() |
|
392 | - { |
|
393 | - return $this->get_total_line_items_for_carts(true); |
|
394 | - } |
|
395 | - |
|
396 | - |
|
397 | - |
|
398 | - /** |
|
399 | - * Returns an array of grand total line items where the TXN_ID is 0. |
|
400 | - * If $expired is set to true, then only line items for expired sessions will be returned. |
|
401 | - * If $expired is set to false, then only line items for active sessions will be returned. |
|
402 | - * |
|
403 | - * @param bool|null $expired |
|
404 | - * @return EE_Base_Class[]|EE_Line_Item[] |
|
405 | - * @throws \EE_Error |
|
406 | - */ |
|
407 | - private function get_total_line_items_for_carts($expired = null) |
|
408 | - { |
|
409 | - $where_params = array( |
|
410 | - 'TXN_ID' => 0, |
|
411 | - 'LIN_type' => 'total', |
|
412 | - ); |
|
413 | - if ($expired !== null) { |
|
414 | - $where_params['LIN_timestamp'] = array( |
|
415 | - $expired ? '<=' : '>', |
|
416 | - time() - EE_Registry::instance()->SSN->lifespan(), |
|
417 | - ); |
|
418 | - } |
|
419 | - return $this->get_all(array($where_params)); |
|
420 | - } |
|
365 | + /** |
|
366 | + * @return EE_Base_Class[]|EE_Line_Item[] |
|
367 | + * @throws \EE_Error |
|
368 | + */ |
|
369 | + public function get_total_line_items_with_no_transaction() |
|
370 | + { |
|
371 | + return $this->get_total_line_items_for_carts(); |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + |
|
376 | + /** |
|
377 | + * @return EE_Base_Class[]|EE_Line_Item[] |
|
378 | + * @throws \EE_Error |
|
379 | + */ |
|
380 | + public function get_total_line_items_for_active_carts() |
|
381 | + { |
|
382 | + return $this->get_total_line_items_for_carts(false); |
|
383 | + } |
|
384 | + |
|
385 | + |
|
386 | + |
|
387 | + /** |
|
388 | + * @return EE_Base_Class[]|EE_Line_Item[] |
|
389 | + * @throws \EE_Error |
|
390 | + */ |
|
391 | + public function get_total_line_items_for_expired_carts() |
|
392 | + { |
|
393 | + return $this->get_total_line_items_for_carts(true); |
|
394 | + } |
|
395 | + |
|
396 | + |
|
397 | + |
|
398 | + /** |
|
399 | + * Returns an array of grand total line items where the TXN_ID is 0. |
|
400 | + * If $expired is set to true, then only line items for expired sessions will be returned. |
|
401 | + * If $expired is set to false, then only line items for active sessions will be returned. |
|
402 | + * |
|
403 | + * @param bool|null $expired |
|
404 | + * @return EE_Base_Class[]|EE_Line_Item[] |
|
405 | + * @throws \EE_Error |
|
406 | + */ |
|
407 | + private function get_total_line_items_for_carts($expired = null) |
|
408 | + { |
|
409 | + $where_params = array( |
|
410 | + 'TXN_ID' => 0, |
|
411 | + 'LIN_type' => 'total', |
|
412 | + ); |
|
413 | + if ($expired !== null) { |
|
414 | + $where_params['LIN_timestamp'] = array( |
|
415 | + $expired ? '<=' : '>', |
|
416 | + time() - EE_Registry::instance()->SSN->lifespan(), |
|
417 | + ); |
|
418 | + } |
|
419 | + return $this->get_all(array($where_params)); |
|
420 | + } |
|
421 | 421 | |
422 | 422 | |
423 | 423 |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | } |
104 | 104 | $order = apply_filters( 'FHEE__EEG_Mijireh__set_redirection_info__order_arguments', $order, $payment, $primary_registrant ); |
105 | 105 | do_action( 'AHEE_log', __FILE__, __FUNCTION__, serialize(get_object_vars($this)) ); |
106 | - $order = $this->_get_unsupported_character_remover()->formatArray($order); |
|
106 | + $order = $this->_get_unsupported_character_remover()->formatArray($order); |
|
107 | 107 | |
108 | 108 | $args = array( |
109 | 109 | 'headers' => array( |
@@ -113,45 +113,45 @@ discard block |
||
113 | 113 | 'body'=> wp_json_encode($order) |
114 | 114 | ); |
115 | 115 | $response = wp_remote_post( $this->_mijireh_api_orders_url, $args ); |
116 | - $problems_string = false; |
|
116 | + $problems_string = false; |
|
117 | 117 | $this->log(array('get checkout url request_args' => $args, 'response' => $response ), $payment); |
118 | 118 | if( ! $response instanceof WP_Error ){ |
119 | 119 | $response_body = json_decode($response['body']); |
120 | 120 | if($response_body && isset($response_body->checkout_url)){ |
121 | - $payment->set_redirect_url($response_body->checkout_url); |
|
122 | - $payment->set_txn_id_chq_nmbr($response_body->order_number); |
|
123 | - $payment->set_details($response['body']); |
|
121 | + $payment->set_redirect_url($response_body->checkout_url); |
|
122 | + $payment->set_txn_id_chq_nmbr($response_body->order_number); |
|
123 | + $payment->set_details($response['body']); |
|
124 | 124 | } else { |
125 | - if( is_array( $response_body ) || is_object( $response_body)){ |
|
126 | - $response_body_as_array = (array)$response_body; |
|
127 | - foreach($response_body_as_array as $problem_parameter => $problems){ |
|
128 | - $problems_string.= sprintf(__('\nProblems with %s: %s','event_espresso'),$problem_parameter,implode(", ",$problems)); |
|
129 | - } |
|
130 | - }else{ |
|
131 | - $problems_string = $response['body']; |
|
132 | - } |
|
133 | - if( ! $problems_string ) { |
|
134 | - //no message to show? wack |
|
135 | - if( isset( $response[ 'headers' ][ 'status' ] ) ){ |
|
136 | - $problems_string = $response[ 'headers' ][ 'status' ]; |
|
137 | - }else{ |
|
138 | - $problems_string = __( 'No response from Mijireh', 'event_espresso' ); |
|
139 | - } |
|
140 | - } |
|
141 | - } |
|
125 | + if( is_array( $response_body ) || is_object( $response_body)){ |
|
126 | + $response_body_as_array = (array)$response_body; |
|
127 | + foreach($response_body_as_array as $problem_parameter => $problems){ |
|
128 | + $problems_string.= sprintf(__('\nProblems with %s: %s','event_espresso'),$problem_parameter,implode(", ",$problems)); |
|
129 | + } |
|
130 | + }else{ |
|
131 | + $problems_string = $response['body']; |
|
132 | + } |
|
133 | + if( ! $problems_string ) { |
|
134 | + //no message to show? wack |
|
135 | + if( isset( $response[ 'headers' ][ 'status' ] ) ){ |
|
136 | + $problems_string = $response[ 'headers' ][ 'status' ]; |
|
137 | + }else{ |
|
138 | + $problems_string = __( 'No response from Mijireh', 'event_espresso' ); |
|
139 | + } |
|
140 | + } |
|
141 | + } |
|
142 | 142 | }else{ |
143 | - $problems_string = implode( ",", $response->get_error_messages() ); |
|
143 | + $problems_string = implode( ",", $response->get_error_messages() ); |
|
144 | 144 | } |
145 | 145 | |
146 | - if( $problems_string ) { |
|
147 | - $payment->set_gateway_response( sprintf( __( 'Errors occurred communicating with Mijireh: %1$s', 'event_espresso'), $problems_string ) ); |
|
148 | - $payment->set_details( $response ); |
|
149 | - $payment->set_redirect_url( null ); |
|
150 | - //even though the payment's status is failed at this point anyways, |
|
151 | - //let's be explicit about it. The fact that the redirect url is null |
|
152 | - //should be enough to client code that they can't redirect the user |
|
153 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
154 | - } |
|
146 | + if( $problems_string ) { |
|
147 | + $payment->set_gateway_response( sprintf( __( 'Errors occurred communicating with Mijireh: %1$s', 'event_espresso'), $problems_string ) ); |
|
148 | + $payment->set_details( $response ); |
|
149 | + $payment->set_redirect_url( null ); |
|
150 | + //even though the payment's status is failed at this point anyways, |
|
151 | + //let's be explicit about it. The fact that the redirect url is null |
|
152 | + //should be enough to client code that they can't redirect the user |
|
153 | + $payment->set_status( $this->_pay_model->failed_status() ); |
|
154 | + } |
|
155 | 155 | return $payment; |
156 | 156 | } |
157 | 157 | |
@@ -188,70 +188,70 @@ discard block |
||
188 | 188 | * @param array $update_info unused. We just use the $transaction |
189 | 189 | * @param EEI_Transaction $transaction |
190 | 190 | * @return \EEI_Payment |
191 | - * @throws EE_Error |
|
191 | + * @throws EE_Error |
|
192 | 192 | */ |
193 | 193 | public function handle_payment_update($update_info, $transaction) { |
194 | - foreach( $transaction->pending_payments() as $payment){ |
|
195 | - $payment = $this->check_payment_in_mijireh($payment); |
|
196 | - if( $payment->status() === $this->_pay_model->approved_status()){ |
|
197 | - return $payment; |
|
198 | - } |
|
199 | - } |
|
200 | - $payment = $transaction instanceof EEI_Transaction ? $transaction->last_payment() : NULL; |
|
194 | + foreach( $transaction->pending_payments() as $payment){ |
|
195 | + $payment = $this->check_payment_in_mijireh($payment); |
|
196 | + if( $payment->status() === $this->_pay_model->approved_status()){ |
|
197 | + return $payment; |
|
198 | + } |
|
199 | + } |
|
200 | + $payment = $transaction instanceof EEI_Transaction ? $transaction->last_payment() : NULL; |
|
201 | 201 | |
202 | - if ( ! $payment instanceof EEI_Payment ){ |
|
203 | - throw new EE_Error( sprintf( __( "Could not find Mijireh payment for transaction %s", 'event_espresso' ), $transaction->ID() ) ); |
|
204 | - } |
|
205 | - return $payment; |
|
202 | + if ( ! $payment instanceof EEI_Payment ){ |
|
203 | + throw new EE_Error( sprintf( __( "Could not find Mijireh payment for transaction %s", 'event_espresso' ), $transaction->ID() ) ); |
|
204 | + } |
|
205 | + return $payment; |
|
206 | 206 | } |
207 | 207 | |
208 | 208 | |
209 | 209 | |
210 | - /** |
|
211 | - * Checks the payment's status in Mijireh for this specific payment |
|
212 | - * @param \EEI_Payment $payment |
|
213 | - * @return \EEI_Payment |
|
214 | - */ |
|
210 | + /** |
|
211 | + * Checks the payment's status in Mijireh for this specific payment |
|
212 | + * @param \EEI_Payment $payment |
|
213 | + * @return \EEI_Payment |
|
214 | + */ |
|
215 | 215 | public function check_payment_in_mijireh( EEI_Payment $payment ){ |
216 | - $request_args = array( |
|
217 | - 'headers' => array( |
|
218 | - 'Authorization' => 'Basic ' . base64_encode( $this->_access_key . ':' ), |
|
219 | - 'Accept'=>'application/json' |
|
220 | - ) |
|
221 | - ); |
|
216 | + $request_args = array( |
|
217 | + 'headers' => array( |
|
218 | + 'Authorization' => 'Basic ' . base64_encode( $this->_access_key . ':' ), |
|
219 | + 'Accept'=>'application/json' |
|
220 | + ) |
|
221 | + ); |
|
222 | 222 | |
223 | - $response = wp_remote_get( |
|
224 | - $this->_mijireh_api_orders_url . '/' . $payment->txn_id_chq_nmbr(), |
|
225 | - $request_args |
|
226 | - ); |
|
223 | + $response = wp_remote_get( |
|
224 | + $this->_mijireh_api_orders_url . '/' . $payment->txn_id_chq_nmbr(), |
|
225 | + $request_args |
|
226 | + ); |
|
227 | 227 | |
228 | - $this->log( |
|
229 | - array( 'get payment status request_args' => $request_args, 'response' => $response ), |
|
230 | - $payment |
|
231 | - ); |
|
232 | - // validate response |
|
233 | - $response_body = isset( $response[ 'body' ] ) ? json_decode( $response[ 'body' ] ) : ''; |
|
234 | - if( $response && $response_body ){ |
|
235 | - switch( $response_body->status ){ |
|
236 | - case 'paid': |
|
237 | - $payment->set_status($this->_pay_model->approved_status()); |
|
238 | - break; |
|
239 | - case 'pending': |
|
240 | - $payment->set_status($this->_pay_model->pending_status()); |
|
241 | - break; |
|
242 | - default: |
|
243 | - $payment->set_status($this->_pay_model->declined_status()); |
|
244 | - } |
|
228 | + $this->log( |
|
229 | + array( 'get payment status request_args' => $request_args, 'response' => $response ), |
|
230 | + $payment |
|
231 | + ); |
|
232 | + // validate response |
|
233 | + $response_body = isset( $response[ 'body' ] ) ? json_decode( $response[ 'body' ] ) : ''; |
|
234 | + if( $response && $response_body ){ |
|
235 | + switch( $response_body->status ){ |
|
236 | + case 'paid': |
|
237 | + $payment->set_status($this->_pay_model->approved_status()); |
|
238 | + break; |
|
239 | + case 'pending': |
|
240 | + $payment->set_status($this->_pay_model->pending_status()); |
|
241 | + break; |
|
242 | + default: |
|
243 | + $payment->set_status($this->_pay_model->declined_status()); |
|
244 | + } |
|
245 | 245 | |
246 | - } else { |
|
247 | - $payment->set_gateway_response( __( 'Response from Mijireh could not be understood.', 'event_espresso' ) ); |
|
248 | - $payment->set_details( $response ); |
|
249 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
250 | - } |
|
251 | - // the following is ONLY for testing the Mijireh IPN and should NEVER be uncommented for real usage |
|
246 | + } else { |
|
247 | + $payment->set_gateway_response( __( 'Response from Mijireh could not be understood.', 'event_espresso' ) ); |
|
248 | + $payment->set_details( $response ); |
|
249 | + $payment->set_status( $this->_pay_model->failed_status() ); |
|
250 | + } |
|
251 | + // the following is ONLY for testing the Mijireh IPN and should NEVER be uncommented for real usage |
|
252 | 252 | // $payment->set_status( $this->_pay_model->pending_status() ); |
253 | - return $payment; |
|
254 | - } |
|
253 | + return $payment; |
|
254 | + } |
|
255 | 255 | |
256 | 256 | } |
257 | 257 |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @author Mike Nelson |
9 | 9 | * |
10 | 10 | */ |
11 | -class EEG_Mijireh extends EE_Offsite_Gateway{ |
|
11 | +class EEG_Mijireh extends EE_Offsite_Gateway { |
|
12 | 12 | |
13 | 13 | protected $_access_key; |
14 | 14 | |
@@ -37,14 +37,14 @@ discard block |
||
37 | 37 | $primary_attendee = $primary_registrant->attendee(); |
38 | 38 | $items = array(); |
39 | 39 | //if we're are charging for the full amount, show the normal line items |
40 | - if( $this->_can_easily_itemize_transaction_for( $payment )){ |
|
40 | + if ($this->_can_easily_itemize_transaction_for($payment)) { |
|
41 | 41 | $total_line_item = $transaction->total_line_item(); |
42 | 42 | $tax_total = $total_line_item->get_total_tax(); |
43 | - foreach($total_line_item->get_items() as $line_item){ |
|
43 | + foreach ($total_line_item->get_items() as $line_item) { |
|
44 | 44 | $items[] = array( |
45 | 45 | 'name'=>apply_filters( |
46 | 46 | 'FHEE__EEG_Mijireh__set_redirection_info__full_amount_line_item_name', |
47 | - $this->_format_line_item_name( $line_item, $payment ), |
|
47 | + $this->_format_line_item_name($line_item, $payment), |
|
48 | 48 | $line_item, |
49 | 49 | $payment, |
50 | 50 | $primary_registrant |
@@ -54,13 +54,13 @@ discard block |
||
54 | 54 | 'quantity'=>$line_item->quantity() |
55 | 55 | ); |
56 | 56 | } |
57 | - }else{//its a partial payment |
|
57 | + } else {//its a partial payment |
|
58 | 58 | $tax_total = 0; |
59 | 59 | //partial payment, so just add 1 item |
60 | 60 | $items[] = array( |
61 | 61 | 'name'=> apply_filters( |
62 | 62 | 'FHEE__EEG_Mijireh__set_redirection_info__partial_amount_line_item_name', |
63 | - $this->_format_partial_payment_line_item_name( $payment ), |
|
63 | + $this->_format_partial_payment_line_item_name($payment), |
|
64 | 64 | $payment, |
65 | 65 | $primary_registrant |
66 | 66 | ), |
@@ -72,18 +72,18 @@ discard block |
||
72 | 72 | $order = array( |
73 | 73 | 'total'=>$this->format_currency($payment->amount()), |
74 | 74 | 'return_url'=>$return_url, |
75 | - 'items'=>$this->_prepare_for_mijireh( $items ), |
|
75 | + 'items'=>$this->_prepare_for_mijireh($items), |
|
76 | 76 | 'email'=>$primary_attendee->email(), |
77 | 77 | 'first_name'=>$primary_attendee->fname(), |
78 | 78 | 'last_name'=>$primary_attendee->lname(), |
79 | 79 | 'tax'=>$this->format_currency($tax_total), |
80 | 80 | 'partner_id'=>'ee'); |
81 | 81 | //setup address? |
82 | - if( $primary_attendee->address() && |
|
83 | - $primary_attendee->city() && |
|
84 | - $primary_attendee->state_ID() && |
|
85 | - $primary_attendee->country_ID() && |
|
86 | - $primary_attendee->zip() ){ |
|
82 | + if ($primary_attendee->address() && |
|
83 | + $primary_attendee->city() && |
|
84 | + $primary_attendee->state_ID() && |
|
85 | + $primary_attendee->country_ID() && |
|
86 | + $primary_attendee->zip()) { |
|
87 | 87 | $shipping_address = array( |
88 | 88 | 'first_name'=>$primary_attendee->fname(), |
89 | 89 | 'last_name'=>$primary_attendee->lname(), |
@@ -93,64 +93,64 @@ discard block |
||
93 | 93 | 'zip_code' => $primary_attendee->zip(), |
94 | 94 | 'country' => $primary_attendee->country_ID() |
95 | 95 | ); |
96 | - if( $primary_attendee->address2() ){ |
|
97 | - $shipping_address[ 'apt_suite' ] = $primary_attendee->address2(); |
|
96 | + if ($primary_attendee->address2()) { |
|
97 | + $shipping_address['apt_suite'] = $primary_attendee->address2(); |
|
98 | 98 | } |
99 | - if( $primary_attendee->phone() ){ |
|
100 | - $shipping_address[ 'phone' ] = $primary_attendee->phone(); |
|
99 | + if ($primary_attendee->phone()) { |
|
100 | + $shipping_address['phone'] = $primary_attendee->phone(); |
|
101 | 101 | } |
102 | - $order[ 'shipping_address' ] = $shipping_address; |
|
102 | + $order['shipping_address'] = $shipping_address; |
|
103 | 103 | } |
104 | - $order = apply_filters( 'FHEE__EEG_Mijireh__set_redirection_info__order_arguments', $order, $payment, $primary_registrant ); |
|
105 | - do_action( 'AHEE_log', __FILE__, __FUNCTION__, serialize(get_object_vars($this)) ); |
|
104 | + $order = apply_filters('FHEE__EEG_Mijireh__set_redirection_info__order_arguments', $order, $payment, $primary_registrant); |
|
105 | + do_action('AHEE_log', __FILE__, __FUNCTION__, serialize(get_object_vars($this))); |
|
106 | 106 | $order = $this->_get_unsupported_character_remover()->formatArray($order); |
107 | 107 | |
108 | 108 | $args = array( |
109 | 109 | 'headers' => array( |
110 | - 'Authorization' => 'Basic ' . base64_encode( $this->_access_key . ':' ), |
|
110 | + 'Authorization' => 'Basic '.base64_encode($this->_access_key.':'), |
|
111 | 111 | 'Accept'=>'application/json' |
112 | 112 | ), |
113 | 113 | 'body'=> wp_json_encode($order) |
114 | 114 | ); |
115 | - $response = wp_remote_post( $this->_mijireh_api_orders_url, $args ); |
|
115 | + $response = wp_remote_post($this->_mijireh_api_orders_url, $args); |
|
116 | 116 | $problems_string = false; |
117 | - $this->log(array('get checkout url request_args' => $args, 'response' => $response ), $payment); |
|
118 | - if( ! $response instanceof WP_Error ){ |
|
117 | + $this->log(array('get checkout url request_args' => $args, 'response' => $response), $payment); |
|
118 | + if ( ! $response instanceof WP_Error) { |
|
119 | 119 | $response_body = json_decode($response['body']); |
120 | - if($response_body && isset($response_body->checkout_url)){ |
|
120 | + if ($response_body && isset($response_body->checkout_url)) { |
|
121 | 121 | $payment->set_redirect_url($response_body->checkout_url); |
122 | 122 | $payment->set_txn_id_chq_nmbr($response_body->order_number); |
123 | 123 | $payment->set_details($response['body']); |
124 | 124 | } else { |
125 | - if( is_array( $response_body ) || is_object( $response_body)){ |
|
126 | - $response_body_as_array = (array)$response_body; |
|
127 | - foreach($response_body_as_array as $problem_parameter => $problems){ |
|
128 | - $problems_string.= sprintf(__('\nProblems with %s: %s','event_espresso'),$problem_parameter,implode(", ",$problems)); |
|
125 | + if (is_array($response_body) || is_object($response_body)) { |
|
126 | + $response_body_as_array = (array) $response_body; |
|
127 | + foreach ($response_body_as_array as $problem_parameter => $problems) { |
|
128 | + $problems_string .= sprintf(__('\nProblems with %s: %s', 'event_espresso'), $problem_parameter, implode(", ", $problems)); |
|
129 | 129 | } |
130 | - }else{ |
|
130 | + } else { |
|
131 | 131 | $problems_string = $response['body']; |
132 | 132 | } |
133 | - if( ! $problems_string ) { |
|
133 | + if ( ! $problems_string) { |
|
134 | 134 | //no message to show? wack |
135 | - if( isset( $response[ 'headers' ][ 'status' ] ) ){ |
|
136 | - $problems_string = $response[ 'headers' ][ 'status' ]; |
|
137 | - }else{ |
|
138 | - $problems_string = __( 'No response from Mijireh', 'event_espresso' ); |
|
135 | + if (isset($response['headers']['status'])) { |
|
136 | + $problems_string = $response['headers']['status']; |
|
137 | + } else { |
|
138 | + $problems_string = __('No response from Mijireh', 'event_espresso'); |
|
139 | 139 | } |
140 | 140 | } |
141 | 141 | } |
142 | - }else{ |
|
143 | - $problems_string = implode( ",", $response->get_error_messages() ); |
|
142 | + } else { |
|
143 | + $problems_string = implode(",", $response->get_error_messages()); |
|
144 | 144 | } |
145 | 145 | |
146 | - if( $problems_string ) { |
|
147 | - $payment->set_gateway_response( sprintf( __( 'Errors occurred communicating with Mijireh: %1$s', 'event_espresso'), $problems_string ) ); |
|
148 | - $payment->set_details( $response ); |
|
149 | - $payment->set_redirect_url( null ); |
|
146 | + if ($problems_string) { |
|
147 | + $payment->set_gateway_response(sprintf(__('Errors occurred communicating with Mijireh: %1$s', 'event_espresso'), $problems_string)); |
|
148 | + $payment->set_details($response); |
|
149 | + $payment->set_redirect_url(null); |
|
150 | 150 | //even though the payment's status is failed at this point anyways, |
151 | 151 | //let's be explicit about it. The fact that the redirect url is null |
152 | 152 | //should be enough to client code that they can't redirect the user |
153 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
153 | + $payment->set_status($this->_pay_model->failed_status()); |
|
154 | 154 | } |
155 | 155 | return $payment; |
156 | 156 | } |
@@ -163,16 +163,16 @@ discard block |
||
163 | 163 | * @param mixed $data |
164 | 164 | * @return mixed same type as $data |
165 | 165 | */ |
166 | - private function _prepare_for_mijireh( $data ){ |
|
167 | - if( is_array( $data ) ){ |
|
166 | + private function _prepare_for_mijireh($data) { |
|
167 | + if (is_array($data)) { |
|
168 | 168 | $prepared_data = array(); |
169 | - foreach($data as $key => $datum ){ |
|
170 | - $prepared_data[ $key ] = $this->_prepare_for_mijireh( $datum ); |
|
169 | + foreach ($data as $key => $datum) { |
|
170 | + $prepared_data[$key] = $this->_prepare_for_mijireh($datum); |
|
171 | 171 | } |
172 | 172 | return $prepared_data; |
173 | - }elseif(is_string( $data ) ){ |
|
174 | - return str_replace( '%', 'percent', $data ); |
|
175 | - }else{ |
|
173 | + }elseif (is_string($data)) { |
|
174 | + return str_replace('%', 'percent', $data); |
|
175 | + } else { |
|
176 | 176 | return $data; |
177 | 177 | } |
178 | 178 | } |
@@ -191,16 +191,16 @@ discard block |
||
191 | 191 | * @throws EE_Error |
192 | 192 | */ |
193 | 193 | public function handle_payment_update($update_info, $transaction) { |
194 | - foreach( $transaction->pending_payments() as $payment){ |
|
194 | + foreach ($transaction->pending_payments() as $payment) { |
|
195 | 195 | $payment = $this->check_payment_in_mijireh($payment); |
196 | - if( $payment->status() === $this->_pay_model->approved_status()){ |
|
196 | + if ($payment->status() === $this->_pay_model->approved_status()) { |
|
197 | 197 | return $payment; |
198 | 198 | } |
199 | 199 | } |
200 | 200 | $payment = $transaction instanceof EEI_Transaction ? $transaction->last_payment() : NULL; |
201 | 201 | |
202 | - if ( ! $payment instanceof EEI_Payment ){ |
|
203 | - throw new EE_Error( sprintf( __( "Could not find Mijireh payment for transaction %s", 'event_espresso' ), $transaction->ID() ) ); |
|
202 | + if ( ! $payment instanceof EEI_Payment) { |
|
203 | + throw new EE_Error(sprintf(__("Could not find Mijireh payment for transaction %s", 'event_espresso'), $transaction->ID())); |
|
204 | 204 | } |
205 | 205 | return $payment; |
206 | 206 | } |
@@ -212,27 +212,27 @@ discard block |
||
212 | 212 | * @param \EEI_Payment $payment |
213 | 213 | * @return \EEI_Payment |
214 | 214 | */ |
215 | - public function check_payment_in_mijireh( EEI_Payment $payment ){ |
|
215 | + public function check_payment_in_mijireh(EEI_Payment $payment) { |
|
216 | 216 | $request_args = array( |
217 | 217 | 'headers' => array( |
218 | - 'Authorization' => 'Basic ' . base64_encode( $this->_access_key . ':' ), |
|
218 | + 'Authorization' => 'Basic '.base64_encode($this->_access_key.':'), |
|
219 | 219 | 'Accept'=>'application/json' |
220 | 220 | ) |
221 | 221 | ); |
222 | 222 | |
223 | 223 | $response = wp_remote_get( |
224 | - $this->_mijireh_api_orders_url . '/' . $payment->txn_id_chq_nmbr(), |
|
224 | + $this->_mijireh_api_orders_url.'/'.$payment->txn_id_chq_nmbr(), |
|
225 | 225 | $request_args |
226 | 226 | ); |
227 | 227 | |
228 | 228 | $this->log( |
229 | - array( 'get payment status request_args' => $request_args, 'response' => $response ), |
|
229 | + array('get payment status request_args' => $request_args, 'response' => $response), |
|
230 | 230 | $payment |
231 | 231 | ); |
232 | 232 | // validate response |
233 | - $response_body = isset( $response[ 'body' ] ) ? json_decode( $response[ 'body' ] ) : ''; |
|
234 | - if( $response && $response_body ){ |
|
235 | - switch( $response_body->status ){ |
|
233 | + $response_body = isset($response['body']) ? json_decode($response['body']) : ''; |
|
234 | + if ($response && $response_body) { |
|
235 | + switch ($response_body->status) { |
|
236 | 236 | case 'paid': |
237 | 237 | $payment->set_status($this->_pay_model->approved_status()); |
238 | 238 | break; |
@@ -244,9 +244,9 @@ discard block |
||
244 | 244 | } |
245 | 245 | |
246 | 246 | } else { |
247 | - $payment->set_gateway_response( __( 'Response from Mijireh could not be understood.', 'event_espresso' ) ); |
|
248 | - $payment->set_details( $response ); |
|
249 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
247 | + $payment->set_gateway_response(__('Response from Mijireh could not be understood.', 'event_espresso')); |
|
248 | + $payment->set_details($response); |
|
249 | + $payment->set_status($this->_pay_model->failed_status()); |
|
250 | 250 | } |
251 | 251 | // the following is ONLY for testing the Mijireh IPN and should NEVER be uncommented for real usage |
252 | 252 | // $payment->set_status( $this->_pay_model->pending_status() ); |
@@ -18,27 +18,27 @@ |
||
18 | 18 | abstract class FormatterBase implements FormatterInterface |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * Recursively applies the formatting to all VALUES in this multi-dimensional array |
|
23 | - * |
|
24 | - * @param array $input |
|
25 | - * @return array |
|
26 | - * @throws InvalidDataTypeException if $input is not an array |
|
27 | - */ |
|
28 | - public function formatArray($input) |
|
29 | - { |
|
30 | - if (! is_array($input)) { |
|
31 | - throw new InvalidDataTypeException('input', $input, 'array'); |
|
32 | - } |
|
33 | - //we can use $this inside the closure in PHP 5.3, so pass in a variable pointing to this instead |
|
34 | - $formatter = $this; |
|
35 | - array_walk_recursive( |
|
36 | - $input, |
|
37 | - function (&$value, $key) use ($formatter) { |
|
38 | - $value = $formatter->format($value); |
|
39 | - } |
|
40 | - ); |
|
41 | - return $input; |
|
42 | - }} |
|
21 | + /** |
|
22 | + * Recursively applies the formatting to all VALUES in this multi-dimensional array |
|
23 | + * |
|
24 | + * @param array $input |
|
25 | + * @return array |
|
26 | + * @throws InvalidDataTypeException if $input is not an array |
|
27 | + */ |
|
28 | + public function formatArray($input) |
|
29 | + { |
|
30 | + if (! is_array($input)) { |
|
31 | + throw new InvalidDataTypeException('input', $input, 'array'); |
|
32 | + } |
|
33 | + //we can use $this inside the closure in PHP 5.3, so pass in a variable pointing to this instead |
|
34 | + $formatter = $this; |
|
35 | + array_walk_recursive( |
|
36 | + $input, |
|
37 | + function (&$value, $key) use ($formatter) { |
|
38 | + $value = $formatter->format($value); |
|
39 | + } |
|
40 | + ); |
|
41 | + return $input; |
|
42 | + }} |
|
43 | 43 | // End of file FormatterBase.php |
44 | 44 | // Location: core\services\formatters/FormatterBase.php |
45 | 45 | \ No newline at end of file |
@@ -27,14 +27,14 @@ |
||
27 | 27 | */ |
28 | 28 | public function formatArray($input) |
29 | 29 | { |
30 | - if (! is_array($input)) { |
|
30 | + if ( ! is_array($input)) { |
|
31 | 31 | throw new InvalidDataTypeException('input', $input, 'array'); |
32 | 32 | } |
33 | 33 | //we can use $this inside the closure in PHP 5.3, so pass in a variable pointing to this instead |
34 | 34 | $formatter = $this; |
35 | 35 | array_walk_recursive( |
36 | 36 | $input, |
37 | - function (&$value, $key) use ($formatter) { |
|
37 | + function(&$value, $key) use ($formatter) { |
|
38 | 38 | $value = $formatter->format($value); |
39 | 39 | } |
40 | 40 | ); |
@@ -18,807 +18,807 @@ |
||
18 | 18 | class EED_Core_Rest_Api extends \EED_Module |
19 | 19 | { |
20 | 20 | |
21 | - const ee_api_namespace = 'ee/v'; |
|
21 | + const ee_api_namespace = 'ee/v'; |
|
22 | 22 | |
23 | - const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
23 | + const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
24 | 24 | |
25 | - const saved_routes_option_names = 'ee_core_routes'; |
|
25 | + const saved_routes_option_names = 'ee_core_routes'; |
|
26 | 26 | |
27 | - /** |
|
28 | - * string used in _links response bodies to make them globally unique. |
|
29 | - * |
|
30 | - * @see http://v2.wp-api.org/extending/linking/ |
|
31 | - */ |
|
32 | - const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
27 | + /** |
|
28 | + * string used in _links response bodies to make them globally unique. |
|
29 | + * |
|
30 | + * @see http://v2.wp-api.org/extending/linking/ |
|
31 | + */ |
|
32 | + const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var Calculated_Model_Fields |
|
36 | - */ |
|
37 | - protected static $_field_calculator = null; |
|
34 | + /** |
|
35 | + * @var Calculated_Model_Fields |
|
36 | + */ |
|
37 | + protected static $_field_calculator = null; |
|
38 | 38 | |
39 | 39 | |
40 | 40 | |
41 | - /** |
|
42 | - * @return EED_Core_Rest_Api |
|
43 | - */ |
|
44 | - public static function instance() |
|
45 | - { |
|
46 | - self::$_field_calculator = new Calculated_Model_Fields(); |
|
47 | - return parent::get_instance(__CLASS__); |
|
48 | - } |
|
41 | + /** |
|
42 | + * @return EED_Core_Rest_Api |
|
43 | + */ |
|
44 | + public static function instance() |
|
45 | + { |
|
46 | + self::$_field_calculator = new Calculated_Model_Fields(); |
|
47 | + return parent::get_instance(__CLASS__); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
54 | - * |
|
55 | - * @access public |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public static function set_hooks() |
|
59 | - { |
|
60 | - self::set_hooks_both(); |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
67 | - * |
|
68 | - * @access public |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public static function set_hooks_admin() |
|
72 | - { |
|
73 | - self::set_hooks_both(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - |
|
78 | - public static function set_hooks_both() |
|
79 | - { |
|
80 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
81 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
82 | - add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
83 | - add_filter('rest_index', |
|
84 | - array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filter_ee_metadata_into_index')); |
|
85 | - EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * sets up hooks which only need to be included as part of REST API requests; |
|
92 | - * other requests like to the frontend or admin etc don't need them |
|
93 | - */ |
|
94 | - public static function set_hooks_rest_api() |
|
95 | - { |
|
96 | - //set hooks which account for changes made to the API |
|
97 | - EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * public wrapper of _set_hooks_for_changes. |
|
104 | - * Loads all the hooks which make requests to old versions of the API |
|
105 | - * appear the same as they always did |
|
106 | - */ |
|
107 | - public static function set_hooks_for_changes() |
|
108 | - { |
|
109 | - self::_set_hooks_for_changes(); |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - |
|
114 | - |
|
115 | - |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * Loads all the hooks which make requests to old versions of the API |
|
120 | - * appear the same as they always did |
|
121 | - */ |
|
122 | - protected static function _set_hooks_for_changes() |
|
123 | - { |
|
124 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
125 | - foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
126 | - //ignore the base parent class |
|
127 | - if ($classname_in_namespace === 'Changes_In_Base') { |
|
128 | - continue; |
|
129 | - } |
|
130 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
131 | - if (class_exists($full_classname)) { |
|
132 | - $instance_of_class = new $full_classname; |
|
133 | - if ($instance_of_class instanceof Changes_In_Base) { |
|
134 | - $instance_of_class->set_hooks(); |
|
135 | - } |
|
136 | - } |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
144 | - * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
145 | - */ |
|
146 | - public static function register_routes() |
|
147 | - { |
|
148 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
149 | - foreach ($relative_urls as $endpoint => $routes) { |
|
150 | - foreach ($routes as $route) { |
|
151 | - $route_args = array( |
|
152 | - array( |
|
153 | - 'callback' => $route['callback'], |
|
154 | - 'methods' => $route['methods'], |
|
155 | - 'args' => isset($route['args']) ? $route['args'] : array(), |
|
156 | - ) |
|
157 | - ); |
|
158 | - if (isset($route['schema_callback'])) { |
|
159 | - $model_name = isset($route['schema_callback'][0]) |
|
160 | - ? $route['schema_callback'][0] |
|
161 | - : ''; |
|
162 | - $version = isset( $route['schema_callback'][1]) |
|
163 | - ? $route['schema_callback'][1] |
|
164 | - : ''; |
|
165 | - if (! empty($model_name) && ! empty($version)) { |
|
166 | - $route_args['schema'] = function () use ($model_name, $version) { |
|
167 | - return ModelRead::handle_schema_request( |
|
168 | - $model_name, |
|
169 | - $version |
|
170 | - ); |
|
171 | - }; |
|
172 | - } |
|
173 | - } |
|
174 | - register_rest_route( |
|
175 | - $namespace, |
|
176 | - $endpoint, |
|
177 | - $route_args |
|
178 | - ); |
|
179 | - } |
|
180 | - } |
|
181 | - } |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * Checks if there was a version change or something that merits invalidating the cached |
|
188 | - * route data. If so, invalidates the cached route data so that it gets refreshed |
|
189 | - * next time the WP API is used |
|
190 | - */ |
|
191 | - public static function invalidate_cached_route_data_on_version_change() |
|
192 | - { |
|
193 | - if (EE_System::instance()->detect_req_type() != EE_System::req_type_normal) { |
|
194 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
195 | - } |
|
196 | - foreach (EE_Registry::instance()->addons as $addon) { |
|
197 | - if ($addon instanceof EE_Addon && $addon->detect_req_type() != EE_System::req_type_normal) { |
|
198 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
199 | - } |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - |
|
205 | - /** |
|
206 | - * Removes the cached route data so it will get refreshed next time the WP API is used |
|
207 | - */ |
|
208 | - public static function invalidate_cached_route_data() |
|
209 | - { |
|
210 | - //delete the saved EE REST API routes |
|
211 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
212 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - |
|
217 | - |
|
218 | - /** |
|
219 | - * Gets the EE route data |
|
220 | - * |
|
221 | - * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
222 | - * @type string|array $callback |
|
223 | - * @type string $methods |
|
224 | - * @type boolean $hidden_endpoint |
|
225 | - * } |
|
226 | - */ |
|
227 | - public static function get_ee_route_data() |
|
228 | - { |
|
229 | - $ee_routes = array(); |
|
230 | - foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
231 | - $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version($version, |
|
232 | - $hidden_endpoints); |
|
233 | - } |
|
234 | - return $ee_routes; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - |
|
239 | - /** |
|
240 | - * Gets the EE route data from the wp options if it exists already, |
|
241 | - * otherwise re-generates it and saves it to the option |
|
242 | - * |
|
243 | - * @param string $version |
|
244 | - * @param boolean $hidden_endpoints |
|
245 | - * @return array |
|
246 | - */ |
|
247 | - protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
248 | - { |
|
249 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
250 | - if ( ! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
251 | - $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
252 | - } |
|
253 | - return $ee_routes; |
|
254 | - } |
|
255 | - |
|
256 | - |
|
257 | - |
|
258 | - /** |
|
259 | - * Saves the EE REST API route data to a wp option and returns it |
|
260 | - * |
|
261 | - * @param string $version |
|
262 | - * @param boolean $hidden_endpoints |
|
263 | - * @return mixed|null|void |
|
264 | - */ |
|
265 | - protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
266 | - { |
|
267 | - $instance = self::instance(); |
|
268 | - $routes = apply_filters( |
|
269 | - 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
270 | - array_replace_recursive( |
|
271 | - $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
272 | - $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
273 | - $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
274 | - $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
275 | - ) |
|
276 | - ); |
|
277 | - $option_name = self::saved_routes_option_names . $version; |
|
278 | - if (get_option($option_name)) { |
|
279 | - update_option($option_name, $routes, true); |
|
280 | - } else { |
|
281 | - add_option($option_name, $routes, null, 'no'); |
|
282 | - } |
|
283 | - return $routes; |
|
284 | - } |
|
285 | - |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * Calculates all the EE routes and saves it to a wordpress option so we don't |
|
290 | - * need to calculate it on every request |
|
291 | - * |
|
292 | - * @deprecated since version 4.9.1 |
|
293 | - * @return void |
|
294 | - */ |
|
295 | - public static function save_ee_routes() |
|
296 | - { |
|
297 | - if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
298 | - $instance = self::instance(); |
|
299 | - $routes = apply_filters( |
|
300 | - 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
301 | - array_replace_recursive( |
|
302 | - $instance->_register_config_routes(), |
|
303 | - $instance->_register_meta_routes(), |
|
304 | - $instance->_register_model_routes(), |
|
305 | - $instance->_register_rpc_routes() |
|
306 | - ) |
|
307 | - ); |
|
308 | - update_option(self::saved_routes_option_names, $routes, true); |
|
309 | - } |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - |
|
314 | - /** |
|
315 | - * Gets all the route information relating to EE models |
|
316 | - * |
|
317 | - * @return array @see get_ee_route_data |
|
318 | - * @deprecated since version 4.9.1 |
|
319 | - */ |
|
320 | - protected function _register_model_routes() |
|
321 | - { |
|
322 | - $model_routes = array(); |
|
323 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
324 | - $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
325 | - . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
326 | - } |
|
327 | - return $model_routes; |
|
328 | - } |
|
329 | - |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * Gets the route data for EE models in the specified version |
|
334 | - * |
|
335 | - * @param string $version |
|
336 | - * @param boolean $hidden_endpoint |
|
337 | - * @return array |
|
338 | - */ |
|
339 | - protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
340 | - { |
|
341 | - $model_version_info = new Model_Version_Info($version); |
|
342 | - $models_to_register = apply_filters( |
|
343 | - 'FHEE__EED_Core_REST_API___register_model_routes', |
|
344 | - $model_version_info->models_for_requested_version() |
|
345 | - ); |
|
346 | - //let's not bother having endpoints for extra metas |
|
347 | - unset($models_to_register['Extra_Meta']); |
|
348 | - unset($models_to_register['Extra_Join']); |
|
349 | - $model_routes = array(); |
|
350 | - foreach ($models_to_register as $model_name => $model_classname) { |
|
351 | - $model = \EE_Registry::instance()->load_model($model_name); |
|
352 | - |
|
353 | - //if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
354 | - if (! $model instanceof EEM_Base) { |
|
355 | - continue; |
|
356 | - } |
|
357 | - |
|
358 | - //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
359 | - $plural_model_route = EEH_Inflector::pluralize_and_lower($model_name); |
|
360 | - $singular_model_route = $plural_model_route . '/(?P<id>\d+)'; |
|
361 | - $model_routes[$plural_model_route] = array( |
|
362 | - array( |
|
363 | - 'callback' => array( |
|
364 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
365 | - 'handle_request_get_all', |
|
366 | - ), |
|
367 | - 'methods' => WP_REST_Server::READABLE, |
|
368 | - 'hidden_endpoint' => $hidden_endpoint, |
|
369 | - 'args' => $this->_get_read_query_params($model, $version), |
|
370 | - '_links' => array( |
|
371 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
372 | - ), |
|
373 | - 'schema_callback' => array($model_name, $version) |
|
374 | - ), |
|
375 | - // array( |
|
376 | - // 'callback' => array( |
|
377 | - // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
378 | - // 'handle_request_create_one' ), |
|
379 | - // 'methods' => WP_REST_Server::CREATABLE, |
|
380 | - // 'hidden_endpoint' => $hidden_endpoint |
|
381 | - // ) |
|
382 | - ); |
|
383 | - $model_routes[$singular_model_route] = array( |
|
384 | - array( |
|
385 | - 'callback' => array( |
|
386 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
387 | - 'handle_request_get_one', |
|
388 | - ), |
|
389 | - 'methods' => WP_REST_Server::READABLE, |
|
390 | - 'hidden_endpoint' => $hidden_endpoint, |
|
391 | - 'args' => $this->_get_response_selection_query_params($model, $version), |
|
392 | - ), |
|
393 | - // array( |
|
394 | - // 'callback' => array( |
|
395 | - // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
396 | - // 'handle_request_edit_one' ), |
|
397 | - // 'methods' => WP_REST_Server::EDITABLE, |
|
398 | - // 'hidden_endpoint' => $hidden_endpoint |
|
399 | - // ), |
|
400 | - ); |
|
401 | - //@todo: also handle DELETE for a single item |
|
402 | - foreach ($model_version_info->relation_settings($model) as $relation_name => $relation_obj) { |
|
403 | - $related_model_name_endpoint_part = ModelRead::get_related_entity_name( |
|
404 | - $relation_name, |
|
405 | - $relation_obj |
|
406 | - ); |
|
407 | - $model_routes[$singular_model_route . '/' . $related_model_name_endpoint_part] = array( |
|
408 | - array( |
|
409 | - 'callback' => array( |
|
410 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
411 | - 'handle_request_get_related', |
|
412 | - ), |
|
413 | - 'methods' => WP_REST_Server::READABLE, |
|
414 | - 'hidden_endpoint' => $hidden_endpoint, |
|
415 | - 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
416 | - ), |
|
417 | - // array( |
|
418 | - // 'callback' => array( |
|
419 | - // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
420 | - // 'handle_request_create_or_update_related' ), |
|
421 | - // 'methods' => WP_REST_Server::EDITABLE, |
|
422 | - // 'hidden_endpoint' => $hidden_endpoint |
|
423 | - // ) |
|
424 | - ); |
|
425 | - //@todo: handle delete related and possibly remove relation (not sure hwo to distinguish) |
|
426 | - } |
|
427 | - } |
|
428 | - return $model_routes; |
|
429 | - } |
|
430 | - |
|
431 | - |
|
432 | - |
|
433 | - /** |
|
434 | - * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
435 | - * routes that don't conform to the traditional REST CRUD-style). |
|
436 | - * |
|
437 | - * @deprecated since 4.9.1 |
|
438 | - */ |
|
439 | - protected function _register_rpc_routes() |
|
440 | - { |
|
441 | - $routes = array(); |
|
442 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
443 | - $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version($version, |
|
444 | - $hidden_endpoint); |
|
445 | - } |
|
446 | - return $routes; |
|
447 | - } |
|
448 | - |
|
449 | - |
|
450 | - |
|
451 | - /** |
|
452 | - * @param string $version |
|
453 | - * @param boolean $hidden_endpoint |
|
454 | - * @return array |
|
455 | - */ |
|
456 | - protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
457 | - { |
|
458 | - $this_versions_routes = array(); |
|
459 | - //checkin endpoint |
|
460 | - $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
461 | - array( |
|
462 | - 'callback' => array( |
|
463 | - 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
464 | - 'handle_request_toggle_checkin', |
|
465 | - ), |
|
466 | - 'methods' => WP_REST_Server::CREATABLE, |
|
467 | - 'hidden_endpoint' => $hidden_endpoint, |
|
468 | - 'args' => array( |
|
469 | - 'force' => array( |
|
470 | - 'required' => false, |
|
471 | - 'default' => false, |
|
472 | - 'description' => __('Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
473 | - 'event_espresso'), |
|
474 | - ), |
|
475 | - ), |
|
476 | - ), |
|
477 | - ); |
|
478 | - return apply_filters( |
|
479 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
480 | - $this_versions_routes, |
|
481 | - $version, |
|
482 | - $hidden_endpoint |
|
483 | - ); |
|
484 | - } |
|
485 | - |
|
486 | - |
|
487 | - |
|
488 | - /** |
|
489 | - * Gets the query params that can be used when request one or many |
|
490 | - * |
|
491 | - * @param EEM_Base $model |
|
492 | - * @param string $version |
|
493 | - * @return array |
|
494 | - */ |
|
495 | - protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
496 | - { |
|
497 | - return apply_filters( |
|
498 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
499 | - array( |
|
500 | - 'include' => array( |
|
501 | - 'required' => false, |
|
502 | - 'default' => '*', |
|
503 | - ), |
|
504 | - 'calculate' => array( |
|
505 | - 'required' => false, |
|
506 | - 'default' => '', |
|
507 | - 'enum' => self::$_field_calculator->retrieve_calculated_fields_for_model($model), |
|
508 | - ), |
|
509 | - ), |
|
510 | - $model, |
|
511 | - $version |
|
512 | - ); |
|
513 | - } |
|
514 | - |
|
515 | - |
|
516 | - |
|
517 | - /** |
|
518 | - * Gets info about reading query params that are acceptable |
|
519 | - * |
|
520 | - * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
521 | - * @param string $version |
|
522 | - * @return array describing the args acceptable when querying this model |
|
523 | - * @throws \EE_Error |
|
524 | - */ |
|
525 | - protected function _get_read_query_params(\EEM_Base $model, $version) |
|
526 | - { |
|
527 | - $default_orderby = array(); |
|
528 | - foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
529 | - $default_orderby[$key_field->get_name()] = 'ASC'; |
|
530 | - } |
|
531 | - return array_merge( |
|
532 | - $this->_get_response_selection_query_params($model, $version), |
|
533 | - array( |
|
534 | - 'where' => array( |
|
535 | - 'required' => false, |
|
536 | - 'default' => array(), |
|
537 | - ), |
|
538 | - 'limit' => array( |
|
539 | - 'required' => false, |
|
540 | - 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
541 | - ), |
|
542 | - 'order_by' => array( |
|
543 | - 'required' => false, |
|
544 | - 'default' => $default_orderby, |
|
545 | - ), |
|
546 | - 'group_by' => array( |
|
547 | - 'required' => false, |
|
548 | - 'default' => null, |
|
549 | - ), |
|
550 | - 'having' => array( |
|
551 | - 'required' => false, |
|
552 | - 'default' => null, |
|
553 | - ), |
|
554 | - 'caps' => array( |
|
555 | - 'required' => false, |
|
556 | - 'default' => EEM_Base::caps_read, |
|
557 | - ), |
|
558 | - ) |
|
559 | - ); |
|
560 | - } |
|
561 | - |
|
562 | - |
|
563 | - |
|
564 | - /** |
|
565 | - * Gets routes for the config |
|
566 | - * |
|
567 | - * @return array @see _register_model_routes |
|
568 | - * @deprecated since version 4.9.1 |
|
569 | - */ |
|
570 | - protected function _register_config_routes() |
|
571 | - { |
|
572 | - $config_routes = array(); |
|
573 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
574 | - $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version($version, |
|
575 | - $hidden_endpoint); |
|
576 | - } |
|
577 | - return $config_routes; |
|
578 | - } |
|
579 | - |
|
580 | - |
|
581 | - |
|
582 | - /** |
|
583 | - * Gets routes for the config for the specified version |
|
584 | - * |
|
585 | - * @param string $version |
|
586 | - * @param boolean $hidden_endpoint |
|
587 | - * @return array |
|
588 | - */ |
|
589 | - protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
590 | - { |
|
591 | - return array( |
|
592 | - 'config' => array( |
|
593 | - array( |
|
594 | - 'callback' => array( |
|
595 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
596 | - 'handle_request', |
|
597 | - ), |
|
598 | - 'methods' => WP_REST_Server::READABLE, |
|
599 | - 'hidden_endpoint' => $hidden_endpoint, |
|
600 | - ), |
|
601 | - ), |
|
602 | - 'site_info' => array( |
|
603 | - array( |
|
604 | - 'callback' => array( |
|
605 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
606 | - 'handle_request_site_info', |
|
607 | - ), |
|
608 | - 'methods' => WP_REST_Server::READABLE, |
|
609 | - 'hidden_endpoint' => $hidden_endpoint, |
|
610 | - ), |
|
611 | - ), |
|
612 | - ); |
|
613 | - } |
|
614 | - |
|
615 | - |
|
616 | - |
|
617 | - /** |
|
618 | - * Gets the meta info routes |
|
619 | - * |
|
620 | - * @return array @see _register_model_routes |
|
621 | - * @deprecated since version 4.9.1 |
|
622 | - */ |
|
623 | - protected function _register_meta_routes() |
|
624 | - { |
|
625 | - $meta_routes = array(); |
|
626 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
627 | - $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version($version, |
|
628 | - $hidden_endpoint); |
|
629 | - } |
|
630 | - return $meta_routes; |
|
631 | - } |
|
632 | - |
|
633 | - |
|
634 | - |
|
635 | - /** |
|
636 | - * @param string $version |
|
637 | - * @param boolean $hidden_endpoint |
|
638 | - * @return array |
|
639 | - */ |
|
640 | - protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
641 | - { |
|
642 | - return array( |
|
643 | - 'resources' => array( |
|
644 | - array( |
|
645 | - 'callback' => array( |
|
646 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
647 | - 'handle_request_models_meta', |
|
648 | - ), |
|
649 | - 'methods' => WP_REST_Server::READABLE, |
|
650 | - 'hidden_endpoint' => $hidden_endpoint, |
|
651 | - ), |
|
652 | - ), |
|
653 | - ); |
|
654 | - } |
|
655 | - |
|
656 | - |
|
657 | - |
|
658 | - /** |
|
659 | - * Tries to hide old 4.6 endpoints from the |
|
660 | - * |
|
661 | - * @param array $route_data |
|
662 | - * @return array |
|
663 | - */ |
|
664 | - public static function hide_old_endpoints($route_data) |
|
665 | - { |
|
666 | - //allow API clients to override which endpoints get hidden, in case |
|
667 | - //they want to discover particular endpoints |
|
668 | - //also, we don't have access to the request so we have to just grab it from the superglobal |
|
669 | - $force_show_ee_namespace = ltrim( |
|
670 | - EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
671 | - '/' |
|
672 | - ); |
|
673 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
674 | - foreach ($relative_urls as $endpoint => $routes) { |
|
675 | - foreach ($routes as $route) { |
|
676 | - //by default, hide "hidden_endpoint"s, unless the request indicates |
|
677 | - //to $force_show_ee_namespace, in which case only show that one |
|
678 | - //namespace's endpoints (and hide all others) |
|
679 | - if (($route['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
680 | - || ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
681 | - ) { |
|
682 | - $full_route = '/' . ltrim($namespace, '/') . '/' . ltrim($endpoint, '/'); |
|
683 | - unset($route_data[$full_route]); |
|
684 | - } |
|
685 | - } |
|
686 | - } |
|
687 | - } |
|
688 | - return $route_data; |
|
689 | - } |
|
690 | - |
|
691 | - |
|
692 | - |
|
693 | - /** |
|
694 | - * Returns an array describing which versions of core support serving requests for. |
|
695 | - * Keys are core versions' major and minor version, and values are the |
|
696 | - * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
697 | - * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
698 | - * the answers table entirely, in which case it would be very difficult for |
|
699 | - * it to serve 4.6-style responses. |
|
700 | - * Versions of core that are missing from this array are unknowns. |
|
701 | - * previous ver |
|
702 | - * |
|
703 | - * @return array |
|
704 | - */ |
|
705 | - public static function version_compatibilities() |
|
706 | - { |
|
707 | - return apply_filters( |
|
708 | - 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
709 | - array( |
|
710 | - '4.8.29' => '4.8.29', |
|
711 | - '4.8.33' => '4.8.29', |
|
712 | - '4.8.34' => '4.8.29', |
|
713 | - '4.8.36' => '4.8.29', |
|
714 | - ) |
|
715 | - ); |
|
716 | - } |
|
717 | - |
|
718 | - |
|
719 | - |
|
720 | - /** |
|
721 | - * Gets the latest API version served. Eg if there |
|
722 | - * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
723 | - * we are on core version 4.8.34, it will return the string "4.8.32" |
|
724 | - * |
|
725 | - * @return string |
|
726 | - */ |
|
727 | - public static function latest_rest_api_version() |
|
728 | - { |
|
729 | - $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
730 | - $versions_served_keys = array_keys($versions_served); |
|
731 | - return end($versions_served_keys); |
|
732 | - } |
|
733 | - |
|
734 | - |
|
735 | - |
|
736 | - /** |
|
737 | - * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
738 | - * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
739 | - * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
740 | - * We also indicate whether or not this version should be put in the index or not |
|
741 | - * |
|
742 | - * @return array keys are API version numbers (just major and minor numbers), and values |
|
743 | - * are whether or not they should be hidden |
|
744 | - */ |
|
745 | - public static function versions_served() |
|
746 | - { |
|
747 | - $versions_served = array(); |
|
748 | - $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
749 | - $lowest_compatible_version = end($possibly_served_versions); |
|
750 | - reset($possibly_served_versions); |
|
751 | - $versions_served_historically = array_keys($possibly_served_versions); |
|
752 | - $latest_version = end($versions_served_historically); |
|
753 | - reset($versions_served_historically); |
|
754 | - //for each version of core we have ever served: |
|
755 | - foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
756 | - //if it's not above the current core version, and it's compatible with the current version of core |
|
757 | - if ($key_versioned_endpoint == $latest_version) { |
|
758 | - //don't hide the latest version in the index |
|
759 | - $versions_served[$key_versioned_endpoint] = false; |
|
760 | - } else if ( |
|
761 | - $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
762 | - && $key_versioned_endpoint >= $lowest_compatible_version |
|
763 | - ) { |
|
764 | - //include, but hide, previous versions which are still supported |
|
765 | - $versions_served[$key_versioned_endpoint] = true; |
|
766 | - } elseif ( |
|
767 | - apply_filters( |
|
768 | - 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
769 | - false, |
|
770 | - $possibly_served_versions |
|
771 | - ) |
|
772 | - ) { |
|
773 | - //if a version is no longer supported, don't include it in index or list of versions served |
|
774 | - $versions_served[$key_versioned_endpoint] = true; |
|
775 | - } |
|
776 | - } |
|
777 | - return $versions_served; |
|
778 | - } |
|
779 | - |
|
780 | - |
|
781 | - |
|
782 | - /** |
|
783 | - * Gets the major and minor version of EE core's version string |
|
784 | - * |
|
785 | - * @return string |
|
786 | - */ |
|
787 | - public static function core_version() |
|
788 | - { |
|
789 | - return apply_filters('FHEE__EED_Core_REST_API__core_version', |
|
790 | - implode('.', array_slice(explode('.', espresso_version()), 0, 3))); |
|
791 | - } |
|
792 | - |
|
793 | - |
|
794 | - |
|
795 | - /** |
|
796 | - * Gets the default limit that should be used when querying for resources |
|
797 | - * |
|
798 | - * @return int |
|
799 | - */ |
|
800 | - public static function get_default_query_limit() |
|
801 | - { |
|
802 | - //we actually don't use a const because we want folks to always use |
|
803 | - //this method, not the const directly |
|
804 | - return apply_filters( |
|
805 | - 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
806 | - 50 |
|
807 | - ); |
|
808 | - } |
|
809 | - |
|
810 | - |
|
811 | - |
|
812 | - /** |
|
813 | - * run - initial module setup |
|
814 | - * |
|
815 | - * @access public |
|
816 | - * @param WP $WP |
|
817 | - * @return void |
|
818 | - */ |
|
819 | - public function run($WP) |
|
820 | - { |
|
821 | - } |
|
52 | + /** |
|
53 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
54 | + * |
|
55 | + * @access public |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public static function set_hooks() |
|
59 | + { |
|
60 | + self::set_hooks_both(); |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
67 | + * |
|
68 | + * @access public |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public static function set_hooks_admin() |
|
72 | + { |
|
73 | + self::set_hooks_both(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + |
|
78 | + public static function set_hooks_both() |
|
79 | + { |
|
80 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
81 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
82 | + add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
83 | + add_filter('rest_index', |
|
84 | + array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filter_ee_metadata_into_index')); |
|
85 | + EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * sets up hooks which only need to be included as part of REST API requests; |
|
92 | + * other requests like to the frontend or admin etc don't need them |
|
93 | + */ |
|
94 | + public static function set_hooks_rest_api() |
|
95 | + { |
|
96 | + //set hooks which account for changes made to the API |
|
97 | + EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * public wrapper of _set_hooks_for_changes. |
|
104 | + * Loads all the hooks which make requests to old versions of the API |
|
105 | + * appear the same as they always did |
|
106 | + */ |
|
107 | + public static function set_hooks_for_changes() |
|
108 | + { |
|
109 | + self::_set_hooks_for_changes(); |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + |
|
114 | + |
|
115 | + |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * Loads all the hooks which make requests to old versions of the API |
|
120 | + * appear the same as they always did |
|
121 | + */ |
|
122 | + protected static function _set_hooks_for_changes() |
|
123 | + { |
|
124 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
125 | + foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
126 | + //ignore the base parent class |
|
127 | + if ($classname_in_namespace === 'Changes_In_Base') { |
|
128 | + continue; |
|
129 | + } |
|
130 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
131 | + if (class_exists($full_classname)) { |
|
132 | + $instance_of_class = new $full_classname; |
|
133 | + if ($instance_of_class instanceof Changes_In_Base) { |
|
134 | + $instance_of_class->set_hooks(); |
|
135 | + } |
|
136 | + } |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
144 | + * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
145 | + */ |
|
146 | + public static function register_routes() |
|
147 | + { |
|
148 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
149 | + foreach ($relative_urls as $endpoint => $routes) { |
|
150 | + foreach ($routes as $route) { |
|
151 | + $route_args = array( |
|
152 | + array( |
|
153 | + 'callback' => $route['callback'], |
|
154 | + 'methods' => $route['methods'], |
|
155 | + 'args' => isset($route['args']) ? $route['args'] : array(), |
|
156 | + ) |
|
157 | + ); |
|
158 | + if (isset($route['schema_callback'])) { |
|
159 | + $model_name = isset($route['schema_callback'][0]) |
|
160 | + ? $route['schema_callback'][0] |
|
161 | + : ''; |
|
162 | + $version = isset( $route['schema_callback'][1]) |
|
163 | + ? $route['schema_callback'][1] |
|
164 | + : ''; |
|
165 | + if (! empty($model_name) && ! empty($version)) { |
|
166 | + $route_args['schema'] = function () use ($model_name, $version) { |
|
167 | + return ModelRead::handle_schema_request( |
|
168 | + $model_name, |
|
169 | + $version |
|
170 | + ); |
|
171 | + }; |
|
172 | + } |
|
173 | + } |
|
174 | + register_rest_route( |
|
175 | + $namespace, |
|
176 | + $endpoint, |
|
177 | + $route_args |
|
178 | + ); |
|
179 | + } |
|
180 | + } |
|
181 | + } |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * Checks if there was a version change or something that merits invalidating the cached |
|
188 | + * route data. If so, invalidates the cached route data so that it gets refreshed |
|
189 | + * next time the WP API is used |
|
190 | + */ |
|
191 | + public static function invalidate_cached_route_data_on_version_change() |
|
192 | + { |
|
193 | + if (EE_System::instance()->detect_req_type() != EE_System::req_type_normal) { |
|
194 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
195 | + } |
|
196 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
197 | + if ($addon instanceof EE_Addon && $addon->detect_req_type() != EE_System::req_type_normal) { |
|
198 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
199 | + } |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + |
|
205 | + /** |
|
206 | + * Removes the cached route data so it will get refreshed next time the WP API is used |
|
207 | + */ |
|
208 | + public static function invalidate_cached_route_data() |
|
209 | + { |
|
210 | + //delete the saved EE REST API routes |
|
211 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
212 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + |
|
217 | + |
|
218 | + /** |
|
219 | + * Gets the EE route data |
|
220 | + * |
|
221 | + * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
222 | + * @type string|array $callback |
|
223 | + * @type string $methods |
|
224 | + * @type boolean $hidden_endpoint |
|
225 | + * } |
|
226 | + */ |
|
227 | + public static function get_ee_route_data() |
|
228 | + { |
|
229 | + $ee_routes = array(); |
|
230 | + foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
231 | + $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version($version, |
|
232 | + $hidden_endpoints); |
|
233 | + } |
|
234 | + return $ee_routes; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + |
|
239 | + /** |
|
240 | + * Gets the EE route data from the wp options if it exists already, |
|
241 | + * otherwise re-generates it and saves it to the option |
|
242 | + * |
|
243 | + * @param string $version |
|
244 | + * @param boolean $hidden_endpoints |
|
245 | + * @return array |
|
246 | + */ |
|
247 | + protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
248 | + { |
|
249 | + $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
250 | + if ( ! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
251 | + $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
252 | + } |
|
253 | + return $ee_routes; |
|
254 | + } |
|
255 | + |
|
256 | + |
|
257 | + |
|
258 | + /** |
|
259 | + * Saves the EE REST API route data to a wp option and returns it |
|
260 | + * |
|
261 | + * @param string $version |
|
262 | + * @param boolean $hidden_endpoints |
|
263 | + * @return mixed|null|void |
|
264 | + */ |
|
265 | + protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
266 | + { |
|
267 | + $instance = self::instance(); |
|
268 | + $routes = apply_filters( |
|
269 | + 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
270 | + array_replace_recursive( |
|
271 | + $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
272 | + $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
273 | + $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
274 | + $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
275 | + ) |
|
276 | + ); |
|
277 | + $option_name = self::saved_routes_option_names . $version; |
|
278 | + if (get_option($option_name)) { |
|
279 | + update_option($option_name, $routes, true); |
|
280 | + } else { |
|
281 | + add_option($option_name, $routes, null, 'no'); |
|
282 | + } |
|
283 | + return $routes; |
|
284 | + } |
|
285 | + |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * Calculates all the EE routes and saves it to a wordpress option so we don't |
|
290 | + * need to calculate it on every request |
|
291 | + * |
|
292 | + * @deprecated since version 4.9.1 |
|
293 | + * @return void |
|
294 | + */ |
|
295 | + public static function save_ee_routes() |
|
296 | + { |
|
297 | + if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
298 | + $instance = self::instance(); |
|
299 | + $routes = apply_filters( |
|
300 | + 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
301 | + array_replace_recursive( |
|
302 | + $instance->_register_config_routes(), |
|
303 | + $instance->_register_meta_routes(), |
|
304 | + $instance->_register_model_routes(), |
|
305 | + $instance->_register_rpc_routes() |
|
306 | + ) |
|
307 | + ); |
|
308 | + update_option(self::saved_routes_option_names, $routes, true); |
|
309 | + } |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + |
|
314 | + /** |
|
315 | + * Gets all the route information relating to EE models |
|
316 | + * |
|
317 | + * @return array @see get_ee_route_data |
|
318 | + * @deprecated since version 4.9.1 |
|
319 | + */ |
|
320 | + protected function _register_model_routes() |
|
321 | + { |
|
322 | + $model_routes = array(); |
|
323 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
324 | + $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
325 | + . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
326 | + } |
|
327 | + return $model_routes; |
|
328 | + } |
|
329 | + |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * Gets the route data for EE models in the specified version |
|
334 | + * |
|
335 | + * @param string $version |
|
336 | + * @param boolean $hidden_endpoint |
|
337 | + * @return array |
|
338 | + */ |
|
339 | + protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
340 | + { |
|
341 | + $model_version_info = new Model_Version_Info($version); |
|
342 | + $models_to_register = apply_filters( |
|
343 | + 'FHEE__EED_Core_REST_API___register_model_routes', |
|
344 | + $model_version_info->models_for_requested_version() |
|
345 | + ); |
|
346 | + //let's not bother having endpoints for extra metas |
|
347 | + unset($models_to_register['Extra_Meta']); |
|
348 | + unset($models_to_register['Extra_Join']); |
|
349 | + $model_routes = array(); |
|
350 | + foreach ($models_to_register as $model_name => $model_classname) { |
|
351 | + $model = \EE_Registry::instance()->load_model($model_name); |
|
352 | + |
|
353 | + //if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
354 | + if (! $model instanceof EEM_Base) { |
|
355 | + continue; |
|
356 | + } |
|
357 | + |
|
358 | + //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
359 | + $plural_model_route = EEH_Inflector::pluralize_and_lower($model_name); |
|
360 | + $singular_model_route = $plural_model_route . '/(?P<id>\d+)'; |
|
361 | + $model_routes[$plural_model_route] = array( |
|
362 | + array( |
|
363 | + 'callback' => array( |
|
364 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
365 | + 'handle_request_get_all', |
|
366 | + ), |
|
367 | + 'methods' => WP_REST_Server::READABLE, |
|
368 | + 'hidden_endpoint' => $hidden_endpoint, |
|
369 | + 'args' => $this->_get_read_query_params($model, $version), |
|
370 | + '_links' => array( |
|
371 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
372 | + ), |
|
373 | + 'schema_callback' => array($model_name, $version) |
|
374 | + ), |
|
375 | + // array( |
|
376 | + // 'callback' => array( |
|
377 | + // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
378 | + // 'handle_request_create_one' ), |
|
379 | + // 'methods' => WP_REST_Server::CREATABLE, |
|
380 | + // 'hidden_endpoint' => $hidden_endpoint |
|
381 | + // ) |
|
382 | + ); |
|
383 | + $model_routes[$singular_model_route] = array( |
|
384 | + array( |
|
385 | + 'callback' => array( |
|
386 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
387 | + 'handle_request_get_one', |
|
388 | + ), |
|
389 | + 'methods' => WP_REST_Server::READABLE, |
|
390 | + 'hidden_endpoint' => $hidden_endpoint, |
|
391 | + 'args' => $this->_get_response_selection_query_params($model, $version), |
|
392 | + ), |
|
393 | + // array( |
|
394 | + // 'callback' => array( |
|
395 | + // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
396 | + // 'handle_request_edit_one' ), |
|
397 | + // 'methods' => WP_REST_Server::EDITABLE, |
|
398 | + // 'hidden_endpoint' => $hidden_endpoint |
|
399 | + // ), |
|
400 | + ); |
|
401 | + //@todo: also handle DELETE for a single item |
|
402 | + foreach ($model_version_info->relation_settings($model) as $relation_name => $relation_obj) { |
|
403 | + $related_model_name_endpoint_part = ModelRead::get_related_entity_name( |
|
404 | + $relation_name, |
|
405 | + $relation_obj |
|
406 | + ); |
|
407 | + $model_routes[$singular_model_route . '/' . $related_model_name_endpoint_part] = array( |
|
408 | + array( |
|
409 | + 'callback' => array( |
|
410 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
411 | + 'handle_request_get_related', |
|
412 | + ), |
|
413 | + 'methods' => WP_REST_Server::READABLE, |
|
414 | + 'hidden_endpoint' => $hidden_endpoint, |
|
415 | + 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
416 | + ), |
|
417 | + // array( |
|
418 | + // 'callback' => array( |
|
419 | + // 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
420 | + // 'handle_request_create_or_update_related' ), |
|
421 | + // 'methods' => WP_REST_Server::EDITABLE, |
|
422 | + // 'hidden_endpoint' => $hidden_endpoint |
|
423 | + // ) |
|
424 | + ); |
|
425 | + //@todo: handle delete related and possibly remove relation (not sure hwo to distinguish) |
|
426 | + } |
|
427 | + } |
|
428 | + return $model_routes; |
|
429 | + } |
|
430 | + |
|
431 | + |
|
432 | + |
|
433 | + /** |
|
434 | + * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
435 | + * routes that don't conform to the traditional REST CRUD-style). |
|
436 | + * |
|
437 | + * @deprecated since 4.9.1 |
|
438 | + */ |
|
439 | + protected function _register_rpc_routes() |
|
440 | + { |
|
441 | + $routes = array(); |
|
442 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
443 | + $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version($version, |
|
444 | + $hidden_endpoint); |
|
445 | + } |
|
446 | + return $routes; |
|
447 | + } |
|
448 | + |
|
449 | + |
|
450 | + |
|
451 | + /** |
|
452 | + * @param string $version |
|
453 | + * @param boolean $hidden_endpoint |
|
454 | + * @return array |
|
455 | + */ |
|
456 | + protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
457 | + { |
|
458 | + $this_versions_routes = array(); |
|
459 | + //checkin endpoint |
|
460 | + $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
461 | + array( |
|
462 | + 'callback' => array( |
|
463 | + 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
464 | + 'handle_request_toggle_checkin', |
|
465 | + ), |
|
466 | + 'methods' => WP_REST_Server::CREATABLE, |
|
467 | + 'hidden_endpoint' => $hidden_endpoint, |
|
468 | + 'args' => array( |
|
469 | + 'force' => array( |
|
470 | + 'required' => false, |
|
471 | + 'default' => false, |
|
472 | + 'description' => __('Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
473 | + 'event_espresso'), |
|
474 | + ), |
|
475 | + ), |
|
476 | + ), |
|
477 | + ); |
|
478 | + return apply_filters( |
|
479 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
480 | + $this_versions_routes, |
|
481 | + $version, |
|
482 | + $hidden_endpoint |
|
483 | + ); |
|
484 | + } |
|
485 | + |
|
486 | + |
|
487 | + |
|
488 | + /** |
|
489 | + * Gets the query params that can be used when request one or many |
|
490 | + * |
|
491 | + * @param EEM_Base $model |
|
492 | + * @param string $version |
|
493 | + * @return array |
|
494 | + */ |
|
495 | + protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
496 | + { |
|
497 | + return apply_filters( |
|
498 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
499 | + array( |
|
500 | + 'include' => array( |
|
501 | + 'required' => false, |
|
502 | + 'default' => '*', |
|
503 | + ), |
|
504 | + 'calculate' => array( |
|
505 | + 'required' => false, |
|
506 | + 'default' => '', |
|
507 | + 'enum' => self::$_field_calculator->retrieve_calculated_fields_for_model($model), |
|
508 | + ), |
|
509 | + ), |
|
510 | + $model, |
|
511 | + $version |
|
512 | + ); |
|
513 | + } |
|
514 | + |
|
515 | + |
|
516 | + |
|
517 | + /** |
|
518 | + * Gets info about reading query params that are acceptable |
|
519 | + * |
|
520 | + * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
521 | + * @param string $version |
|
522 | + * @return array describing the args acceptable when querying this model |
|
523 | + * @throws \EE_Error |
|
524 | + */ |
|
525 | + protected function _get_read_query_params(\EEM_Base $model, $version) |
|
526 | + { |
|
527 | + $default_orderby = array(); |
|
528 | + foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
529 | + $default_orderby[$key_field->get_name()] = 'ASC'; |
|
530 | + } |
|
531 | + return array_merge( |
|
532 | + $this->_get_response_selection_query_params($model, $version), |
|
533 | + array( |
|
534 | + 'where' => array( |
|
535 | + 'required' => false, |
|
536 | + 'default' => array(), |
|
537 | + ), |
|
538 | + 'limit' => array( |
|
539 | + 'required' => false, |
|
540 | + 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
541 | + ), |
|
542 | + 'order_by' => array( |
|
543 | + 'required' => false, |
|
544 | + 'default' => $default_orderby, |
|
545 | + ), |
|
546 | + 'group_by' => array( |
|
547 | + 'required' => false, |
|
548 | + 'default' => null, |
|
549 | + ), |
|
550 | + 'having' => array( |
|
551 | + 'required' => false, |
|
552 | + 'default' => null, |
|
553 | + ), |
|
554 | + 'caps' => array( |
|
555 | + 'required' => false, |
|
556 | + 'default' => EEM_Base::caps_read, |
|
557 | + ), |
|
558 | + ) |
|
559 | + ); |
|
560 | + } |
|
561 | + |
|
562 | + |
|
563 | + |
|
564 | + /** |
|
565 | + * Gets routes for the config |
|
566 | + * |
|
567 | + * @return array @see _register_model_routes |
|
568 | + * @deprecated since version 4.9.1 |
|
569 | + */ |
|
570 | + protected function _register_config_routes() |
|
571 | + { |
|
572 | + $config_routes = array(); |
|
573 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
574 | + $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version($version, |
|
575 | + $hidden_endpoint); |
|
576 | + } |
|
577 | + return $config_routes; |
|
578 | + } |
|
579 | + |
|
580 | + |
|
581 | + |
|
582 | + /** |
|
583 | + * Gets routes for the config for the specified version |
|
584 | + * |
|
585 | + * @param string $version |
|
586 | + * @param boolean $hidden_endpoint |
|
587 | + * @return array |
|
588 | + */ |
|
589 | + protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
590 | + { |
|
591 | + return array( |
|
592 | + 'config' => array( |
|
593 | + array( |
|
594 | + 'callback' => array( |
|
595 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
596 | + 'handle_request', |
|
597 | + ), |
|
598 | + 'methods' => WP_REST_Server::READABLE, |
|
599 | + 'hidden_endpoint' => $hidden_endpoint, |
|
600 | + ), |
|
601 | + ), |
|
602 | + 'site_info' => array( |
|
603 | + array( |
|
604 | + 'callback' => array( |
|
605 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
606 | + 'handle_request_site_info', |
|
607 | + ), |
|
608 | + 'methods' => WP_REST_Server::READABLE, |
|
609 | + 'hidden_endpoint' => $hidden_endpoint, |
|
610 | + ), |
|
611 | + ), |
|
612 | + ); |
|
613 | + } |
|
614 | + |
|
615 | + |
|
616 | + |
|
617 | + /** |
|
618 | + * Gets the meta info routes |
|
619 | + * |
|
620 | + * @return array @see _register_model_routes |
|
621 | + * @deprecated since version 4.9.1 |
|
622 | + */ |
|
623 | + protected function _register_meta_routes() |
|
624 | + { |
|
625 | + $meta_routes = array(); |
|
626 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
627 | + $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version($version, |
|
628 | + $hidden_endpoint); |
|
629 | + } |
|
630 | + return $meta_routes; |
|
631 | + } |
|
632 | + |
|
633 | + |
|
634 | + |
|
635 | + /** |
|
636 | + * @param string $version |
|
637 | + * @param boolean $hidden_endpoint |
|
638 | + * @return array |
|
639 | + */ |
|
640 | + protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
641 | + { |
|
642 | + return array( |
|
643 | + 'resources' => array( |
|
644 | + array( |
|
645 | + 'callback' => array( |
|
646 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
647 | + 'handle_request_models_meta', |
|
648 | + ), |
|
649 | + 'methods' => WP_REST_Server::READABLE, |
|
650 | + 'hidden_endpoint' => $hidden_endpoint, |
|
651 | + ), |
|
652 | + ), |
|
653 | + ); |
|
654 | + } |
|
655 | + |
|
656 | + |
|
657 | + |
|
658 | + /** |
|
659 | + * Tries to hide old 4.6 endpoints from the |
|
660 | + * |
|
661 | + * @param array $route_data |
|
662 | + * @return array |
|
663 | + */ |
|
664 | + public static function hide_old_endpoints($route_data) |
|
665 | + { |
|
666 | + //allow API clients to override which endpoints get hidden, in case |
|
667 | + //they want to discover particular endpoints |
|
668 | + //also, we don't have access to the request so we have to just grab it from the superglobal |
|
669 | + $force_show_ee_namespace = ltrim( |
|
670 | + EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
671 | + '/' |
|
672 | + ); |
|
673 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
674 | + foreach ($relative_urls as $endpoint => $routes) { |
|
675 | + foreach ($routes as $route) { |
|
676 | + //by default, hide "hidden_endpoint"s, unless the request indicates |
|
677 | + //to $force_show_ee_namespace, in which case only show that one |
|
678 | + //namespace's endpoints (and hide all others) |
|
679 | + if (($route['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
680 | + || ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
681 | + ) { |
|
682 | + $full_route = '/' . ltrim($namespace, '/') . '/' . ltrim($endpoint, '/'); |
|
683 | + unset($route_data[$full_route]); |
|
684 | + } |
|
685 | + } |
|
686 | + } |
|
687 | + } |
|
688 | + return $route_data; |
|
689 | + } |
|
690 | + |
|
691 | + |
|
692 | + |
|
693 | + /** |
|
694 | + * Returns an array describing which versions of core support serving requests for. |
|
695 | + * Keys are core versions' major and minor version, and values are the |
|
696 | + * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
697 | + * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
698 | + * the answers table entirely, in which case it would be very difficult for |
|
699 | + * it to serve 4.6-style responses. |
|
700 | + * Versions of core that are missing from this array are unknowns. |
|
701 | + * previous ver |
|
702 | + * |
|
703 | + * @return array |
|
704 | + */ |
|
705 | + public static function version_compatibilities() |
|
706 | + { |
|
707 | + return apply_filters( |
|
708 | + 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
709 | + array( |
|
710 | + '4.8.29' => '4.8.29', |
|
711 | + '4.8.33' => '4.8.29', |
|
712 | + '4.8.34' => '4.8.29', |
|
713 | + '4.8.36' => '4.8.29', |
|
714 | + ) |
|
715 | + ); |
|
716 | + } |
|
717 | + |
|
718 | + |
|
719 | + |
|
720 | + /** |
|
721 | + * Gets the latest API version served. Eg if there |
|
722 | + * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
723 | + * we are on core version 4.8.34, it will return the string "4.8.32" |
|
724 | + * |
|
725 | + * @return string |
|
726 | + */ |
|
727 | + public static function latest_rest_api_version() |
|
728 | + { |
|
729 | + $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
730 | + $versions_served_keys = array_keys($versions_served); |
|
731 | + return end($versions_served_keys); |
|
732 | + } |
|
733 | + |
|
734 | + |
|
735 | + |
|
736 | + /** |
|
737 | + * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
738 | + * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
739 | + * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
740 | + * We also indicate whether or not this version should be put in the index or not |
|
741 | + * |
|
742 | + * @return array keys are API version numbers (just major and minor numbers), and values |
|
743 | + * are whether or not they should be hidden |
|
744 | + */ |
|
745 | + public static function versions_served() |
|
746 | + { |
|
747 | + $versions_served = array(); |
|
748 | + $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
749 | + $lowest_compatible_version = end($possibly_served_versions); |
|
750 | + reset($possibly_served_versions); |
|
751 | + $versions_served_historically = array_keys($possibly_served_versions); |
|
752 | + $latest_version = end($versions_served_historically); |
|
753 | + reset($versions_served_historically); |
|
754 | + //for each version of core we have ever served: |
|
755 | + foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
756 | + //if it's not above the current core version, and it's compatible with the current version of core |
|
757 | + if ($key_versioned_endpoint == $latest_version) { |
|
758 | + //don't hide the latest version in the index |
|
759 | + $versions_served[$key_versioned_endpoint] = false; |
|
760 | + } else if ( |
|
761 | + $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
762 | + && $key_versioned_endpoint >= $lowest_compatible_version |
|
763 | + ) { |
|
764 | + //include, but hide, previous versions which are still supported |
|
765 | + $versions_served[$key_versioned_endpoint] = true; |
|
766 | + } elseif ( |
|
767 | + apply_filters( |
|
768 | + 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
769 | + false, |
|
770 | + $possibly_served_versions |
|
771 | + ) |
|
772 | + ) { |
|
773 | + //if a version is no longer supported, don't include it in index or list of versions served |
|
774 | + $versions_served[$key_versioned_endpoint] = true; |
|
775 | + } |
|
776 | + } |
|
777 | + return $versions_served; |
|
778 | + } |
|
779 | + |
|
780 | + |
|
781 | + |
|
782 | + /** |
|
783 | + * Gets the major and minor version of EE core's version string |
|
784 | + * |
|
785 | + * @return string |
|
786 | + */ |
|
787 | + public static function core_version() |
|
788 | + { |
|
789 | + return apply_filters('FHEE__EED_Core_REST_API__core_version', |
|
790 | + implode('.', array_slice(explode('.', espresso_version()), 0, 3))); |
|
791 | + } |
|
792 | + |
|
793 | + |
|
794 | + |
|
795 | + /** |
|
796 | + * Gets the default limit that should be used when querying for resources |
|
797 | + * |
|
798 | + * @return int |
|
799 | + */ |
|
800 | + public static function get_default_query_limit() |
|
801 | + { |
|
802 | + //we actually don't use a const because we want folks to always use |
|
803 | + //this method, not the const directly |
|
804 | + return apply_filters( |
|
805 | + 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
806 | + 50 |
|
807 | + ); |
|
808 | + } |
|
809 | + |
|
810 | + |
|
811 | + |
|
812 | + /** |
|
813 | + * run - initial module setup |
|
814 | + * |
|
815 | + * @access public |
|
816 | + * @param WP $WP |
|
817 | + * @return void |
|
818 | + */ |
|
819 | + public function run($WP) |
|
820 | + { |
|
821 | + } |
|
822 | 822 | } |
823 | 823 | |
824 | 824 | // End of file EED_Core_Rest_Api.module.php |
@@ -6,21 +6,21 @@ discard block |
||
6 | 6 | * @subpackage |
7 | 7 | * @author Mike Nelson |
8 | 8 | */ |
9 | -class EE_Datepicker_Input extends EE_Form_Input_Base{ |
|
9 | +class EE_Datepicker_Input extends EE_Form_Input_Base { |
|
10 | 10 | |
11 | 11 | /** |
12 | 12 | * @param array $input_settings |
13 | 13 | */ |
14 | - function __construct( $input_settings = array() ){ |
|
15 | - $this->_set_display_strategy( new EE_Text_Input_Display_Strategy( 'datepicker' ) ); |
|
16 | - $this->_set_normalization_strategy( new EE_Text_Normalization() ); |
|
14 | + function __construct($input_settings = array()) { |
|
15 | + $this->_set_display_strategy(new EE_Text_Input_Display_Strategy('datepicker')); |
|
16 | + $this->_set_normalization_strategy(new EE_Text_Normalization()); |
|
17 | 17 | //we could do better for validation, but at least verify its plaintext |
18 | - $this->_add_validation_strategy( new EE_Plaintext_Validation_Strategy( isset( $input_settings[ 'validation_error_message' ] ) ? $input_settings[ 'validation_error_message' ] : NULL ) ); |
|
19 | - parent::__construct( $input_settings ); |
|
20 | - $this->set_html_class( $this->html_class() . ' datepicker' ); |
|
18 | + $this->_add_validation_strategy(new EE_Plaintext_Validation_Strategy(isset($input_settings['validation_error_message']) ? $input_settings['validation_error_message'] : NULL)); |
|
19 | + parent::__construct($input_settings); |
|
20 | + $this->set_html_class($this->html_class().' datepicker'); |
|
21 | 21 | // add some style and make it dance |
22 | - add_action( 'wp_enqueue_scripts', array( 'EE_Datepicker_Input', 'enqueue_styles_and_scripts' )); |
|
23 | - add_action( 'admin_enqueue_scripts', array( 'EE_Datepicker_Input', 'enqueue_styles_and_scripts' )); |
|
22 | + add_action('wp_enqueue_scripts', array('EE_Datepicker_Input', 'enqueue_styles_and_scripts')); |
|
23 | + add_action('admin_enqueue_scripts', array('EE_Datepicker_Input', 'enqueue_styles_and_scripts')); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | |
@@ -33,8 +33,8 @@ discard block |
||
33 | 33 | */ |
34 | 34 | public static function enqueue_styles_and_scripts() { |
35 | 35 | // load css |
36 | - wp_register_style( 'espresso-ui-theme', EE_GLOBAL_ASSETS_URL . 'css/espresso-ui-theme/jquery-ui-1.10.3.custom.min.css', array(), EVENT_ESPRESSO_VERSION ); |
|
37 | - wp_enqueue_style( 'espresso-ui-theme'); |
|
36 | + wp_register_style('espresso-ui-theme', EE_GLOBAL_ASSETS_URL.'css/espresso-ui-theme/jquery-ui-1.10.3.custom.min.css', array(), EVENT_ESPRESSO_VERSION); |
|
37 | + wp_enqueue_style('espresso-ui-theme'); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 |
@@ -17,67 +17,67 @@ discard block |
||
17 | 17 | class EEH_Schema { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * generates JSON-based linked data for an event |
|
22 | - * |
|
23 | - * @param \EE_Event $event |
|
24 | - */ |
|
25 | - public static function add_json_linked_data_for_event(\EE_Event $event) |
|
26 | - { |
|
27 | - //Check we have a valid datetime for the event |
|
28 | - if(! $event->primary_datetime() instanceof EE_Datetime) { |
|
29 | - return; |
|
30 | - } |
|
31 | - |
|
32 | - $template_args = array( |
|
33 | - 'event_permalink' => '', |
|
34 | - 'event_name' => '', |
|
35 | - 'event_description' => '', |
|
36 | - 'event_start' => '', |
|
37 | - 'event_end' => '', |
|
38 | - 'currency' => '', |
|
39 | - 'event_tickets' => array(), |
|
40 | - 'venue_name' => '', |
|
41 | - 'venue_url' => '', |
|
42 | - 'venue_locality' => '', |
|
43 | - 'venue_region' => '', |
|
44 | - 'event_image' => '', |
|
45 | - ); |
|
46 | - $template_args['event_permalink'] = $event->get_permalink(); |
|
47 | - $template_args['event_name'] = $event->name(); |
|
48 | - $template_args['event_description'] = wp_strip_all_tags($event->short_description(200)); |
|
49 | - $template_args['event_start'] = $event->primary_datetime()->start_date(DateTime::ATOM); |
|
50 | - $template_args['event_end'] = $event->primary_datetime()->end_date(DateTime::ATOM); |
|
51 | - $template_args['currency'] = EE_Registry::instance()->CFG->currency->code; |
|
52 | - foreach ($event->tickets() as $ticket) { |
|
53 | - $ID = $ticket->ID(); |
|
54 | - $template_args['event_tickets'][$ID]['start_date'] = $ticket->start_date(DateTime::ATOM, null); |
|
55 | - $template_args['event_tickets'][$ID]['end_date'] = $ticket->end_date(DateTime::ATOM, null); |
|
56 | - $template_args['event_tickets'][$ID]['price'] = number_format( |
|
57 | - $ticket->price(), |
|
58 | - EE_Registry::instance()->CFG->currency->dec_plc, |
|
59 | - EE_Registry::instance()->CFG->currency->dec_mrk, |
|
60 | - EE_Registry::instance()->CFG->currency->thsnds |
|
61 | - ); |
|
62 | - } |
|
63 | - $VNU_ID = espresso_venue_id(); |
|
64 | - if ( ! empty($VNU_ID) && ! espresso_is_venue_private($VNU_ID)) { |
|
65 | - $venue = EEH_Venue_View::get_venue($VNU_ID); |
|
66 | - $template_args['venue_name'] = get_the_title($VNU_ID); |
|
67 | - $template_args['venue_url'] = get_permalink($VNU_ID); |
|
68 | - $template_args['venue_locality'] = $venue->city(); |
|
69 | - $template_args['venue_region'] = $venue->state_name(); |
|
70 | - } |
|
71 | - $template_args['event_image'] = $event->feature_image_url(); |
|
72 | - $template_args = apply_filters( |
|
73 | - 'FHEE__EEH_Schema__add_json_linked_data_for_event__template_args', |
|
74 | - $template_args, |
|
75 | - $event, |
|
76 | - $VNU_ID |
|
77 | - ); |
|
78 | - extract($template_args, EXTR_OVERWRITE); |
|
79 | - include EE_TEMPLATES . 'json_linked_data_for_event.template.php'; |
|
80 | - } |
|
20 | + /** |
|
21 | + * generates JSON-based linked data for an event |
|
22 | + * |
|
23 | + * @param \EE_Event $event |
|
24 | + */ |
|
25 | + public static function add_json_linked_data_for_event(\EE_Event $event) |
|
26 | + { |
|
27 | + //Check we have a valid datetime for the event |
|
28 | + if(! $event->primary_datetime() instanceof EE_Datetime) { |
|
29 | + return; |
|
30 | + } |
|
31 | + |
|
32 | + $template_args = array( |
|
33 | + 'event_permalink' => '', |
|
34 | + 'event_name' => '', |
|
35 | + 'event_description' => '', |
|
36 | + 'event_start' => '', |
|
37 | + 'event_end' => '', |
|
38 | + 'currency' => '', |
|
39 | + 'event_tickets' => array(), |
|
40 | + 'venue_name' => '', |
|
41 | + 'venue_url' => '', |
|
42 | + 'venue_locality' => '', |
|
43 | + 'venue_region' => '', |
|
44 | + 'event_image' => '', |
|
45 | + ); |
|
46 | + $template_args['event_permalink'] = $event->get_permalink(); |
|
47 | + $template_args['event_name'] = $event->name(); |
|
48 | + $template_args['event_description'] = wp_strip_all_tags($event->short_description(200)); |
|
49 | + $template_args['event_start'] = $event->primary_datetime()->start_date(DateTime::ATOM); |
|
50 | + $template_args['event_end'] = $event->primary_datetime()->end_date(DateTime::ATOM); |
|
51 | + $template_args['currency'] = EE_Registry::instance()->CFG->currency->code; |
|
52 | + foreach ($event->tickets() as $ticket) { |
|
53 | + $ID = $ticket->ID(); |
|
54 | + $template_args['event_tickets'][$ID]['start_date'] = $ticket->start_date(DateTime::ATOM, null); |
|
55 | + $template_args['event_tickets'][$ID]['end_date'] = $ticket->end_date(DateTime::ATOM, null); |
|
56 | + $template_args['event_tickets'][$ID]['price'] = number_format( |
|
57 | + $ticket->price(), |
|
58 | + EE_Registry::instance()->CFG->currency->dec_plc, |
|
59 | + EE_Registry::instance()->CFG->currency->dec_mrk, |
|
60 | + EE_Registry::instance()->CFG->currency->thsnds |
|
61 | + ); |
|
62 | + } |
|
63 | + $VNU_ID = espresso_venue_id(); |
|
64 | + if ( ! empty($VNU_ID) && ! espresso_is_venue_private($VNU_ID)) { |
|
65 | + $venue = EEH_Venue_View::get_venue($VNU_ID); |
|
66 | + $template_args['venue_name'] = get_the_title($VNU_ID); |
|
67 | + $template_args['venue_url'] = get_permalink($VNU_ID); |
|
68 | + $template_args['venue_locality'] = $venue->city(); |
|
69 | + $template_args['venue_region'] = $venue->state_name(); |
|
70 | + } |
|
71 | + $template_args['event_image'] = $event->feature_image_url(); |
|
72 | + $template_args = apply_filters( |
|
73 | + 'FHEE__EEH_Schema__add_json_linked_data_for_event__template_args', |
|
74 | + $template_args, |
|
75 | + $event, |
|
76 | + $VNU_ID |
|
77 | + ); |
|
78 | + extract($template_args, EXTR_OVERWRITE); |
|
79 | + include EE_TEMPLATES . 'json_linked_data_for_event.template.php'; |
|
80 | + } |
|
81 | 81 | |
82 | 82 | |
83 | 83 | /** |
@@ -91,8 +91,8 @@ discard block |
||
91 | 91 | */ |
92 | 92 | public static function location( $location = null ) { |
93 | 93 | return ! empty( $location ) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">' |
94 | - . $location |
|
95 | - . '</div>' : ''; |
|
94 | + . $location |
|
95 | + . '</div>' : ''; |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | |
@@ -212,8 +212,8 @@ discard block |
||
212 | 212 | */ |
213 | 213 | public static function postalCode( EEI_Address $obj_with_address = null ) { |
214 | 214 | return $obj_with_address->zip() !== null && $obj_with_address->zip() !== '' ? '<span itemprop="postalCode">' |
215 | - . $obj_with_address->zip() |
|
216 | - . '</span>' : ''; |
|
215 | + . $obj_with_address->zip() |
|
216 | + . '</span>' : ''; |
|
217 | 217 | } |
218 | 218 | |
219 | 219 | |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | //Check the URL includes a scheme |
248 | 248 | $parsed_url = parse_url($url); |
249 | 249 | if ( empty($parsed_url['scheme']) ) { |
250 | - $url = 'http://' . ltrim($url, '/'); |
|
250 | + $url = 'http://' . ltrim($url, '/'); |
|
251 | 251 | } |
252 | 252 | |
253 | 253 | $atts = ''; |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
4 | - exit( 'No direct script access allowed' ); |
|
3 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
4 | + exit('No direct script access allowed'); |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | public static function add_json_linked_data_for_event(\EE_Event $event) |
26 | 26 | { |
27 | 27 | //Check we have a valid datetime for the event |
28 | - if(! $event->primary_datetime() instanceof EE_Datetime) { |
|
28 | + if ( ! $event->primary_datetime() instanceof EE_Datetime) { |
|
29 | 29 | return; |
30 | 30 | } |
31 | 31 | |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | $VNU_ID |
77 | 77 | ); |
78 | 78 | extract($template_args, EXTR_OVERWRITE); |
79 | - include EE_TEMPLATES . 'json_linked_data_for_event.template.php'; |
|
79 | + include EE_TEMPLATES.'json_linked_data_for_event.template.php'; |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | |
@@ -89,8 +89,8 @@ discard block |
||
89 | 89 | * @param string $location |
90 | 90 | * @return string |
91 | 91 | */ |
92 | - public static function location( $location = null ) { |
|
93 | - return ! empty( $location ) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">' |
|
92 | + public static function location($location = null) { |
|
93 | + return ! empty($location) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">' |
|
94 | 94 | . $location |
95 | 95 | . '</div>' : ''; |
96 | 96 | } |
@@ -105,8 +105,8 @@ discard block |
||
105 | 105 | * @param string $name |
106 | 106 | * @return string |
107 | 107 | */ |
108 | - public static function name( $name = null ) { |
|
109 | - return ! empty( $name ) ? '<span itemprop="name">' . $name . '</span>' : ''; |
|
108 | + public static function name($name = null) { |
|
109 | + return ! empty($name) ? '<span itemprop="name">'.$name.'</span>' : ''; |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | |
@@ -119,9 +119,9 @@ discard block |
||
119 | 119 | * @param EEI_Address $obj_with_address |
120 | 120 | * @return string |
121 | 121 | */ |
122 | - public static function streetAddress( EEI_Address $obj_with_address = null ) { |
|
122 | + public static function streetAddress(EEI_Address $obj_with_address = null) { |
|
123 | 123 | return $obj_with_address->address() !== null && $obj_with_address->address() !== '' |
124 | - ? '<span itemprop="streetAddress">' . $obj_with_address->address() . '</span>' : ''; |
|
124 | + ? '<span itemprop="streetAddress">'.$obj_with_address->address().'</span>' : ''; |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | |
@@ -134,14 +134,14 @@ discard block |
||
134 | 134 | * @param EEI_Address $obj_with_address |
135 | 135 | * @return string |
136 | 136 | */ |
137 | - public static function postOfficeBoxNumber( EEI_Address $obj_with_address = null ) { |
|
137 | + public static function postOfficeBoxNumber(EEI_Address $obj_with_address = null) { |
|
138 | 138 | // regex check for some form of PO Box or P.O. Box, etc, etc, etc |
139 | - if ( preg_match( |
|
139 | + if (preg_match( |
|
140 | 140 | "/^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i", |
141 | 141 | $obj_with_address->address2() |
142 | - ) ) { |
|
142 | + )) { |
|
143 | 143 | return $obj_with_address->address2() !== null && $obj_with_address->address2() !== '' |
144 | - ? '<span itemprop="postOfficeBoxNumber">' . $obj_with_address->address2() . '</span>' : ''; |
|
144 | + ? '<span itemprop="postOfficeBoxNumber">'.$obj_with_address->address2().'</span>' : ''; |
|
145 | 145 | } else { |
146 | 146 | return $obj_with_address->address2(); |
147 | 147 | } |
@@ -157,9 +157,9 @@ discard block |
||
157 | 157 | * @param EEI_Address $obj_with_address |
158 | 158 | * @return string |
159 | 159 | */ |
160 | - public static function addressLocality( EEI_Address $obj_with_address = null ) { |
|
160 | + public static function addressLocality(EEI_Address $obj_with_address = null) { |
|
161 | 161 | return $obj_with_address->city() !== null && $obj_with_address->city() !== '' |
162 | - ? '<span itemprop="addressLocality">' . $obj_with_address->city() . '</span>' : ''; |
|
162 | + ? '<span itemprop="addressLocality">'.$obj_with_address->city().'</span>' : ''; |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | |
@@ -172,10 +172,10 @@ discard block |
||
172 | 172 | * @param EEI_Address $obj_with_address |
173 | 173 | * @return string |
174 | 174 | */ |
175 | - public static function addressRegion( EEI_Address $obj_with_address = null ) { |
|
175 | + public static function addressRegion(EEI_Address $obj_with_address = null) { |
|
176 | 176 | $state = $obj_with_address->state_name(); |
177 | - if ( ! empty( $state ) ) { |
|
178 | - return '<span itemprop="addressRegion">' . $state . '</span>'; |
|
177 | + if ( ! empty($state)) { |
|
178 | + return '<span itemprop="addressRegion">'.$state.'</span>'; |
|
179 | 179 | } else { |
180 | 180 | return ''; |
181 | 181 | } |
@@ -191,10 +191,10 @@ discard block |
||
191 | 191 | * @param EEI_Address $obj_with_address |
192 | 192 | * @return string |
193 | 193 | */ |
194 | - public static function addressCountry( EEI_Address $obj_with_address = null ) { |
|
194 | + public static function addressCountry(EEI_Address $obj_with_address = null) { |
|
195 | 195 | $country = $obj_with_address->country_name(); |
196 | - if ( ! empty( $country ) ) { |
|
197 | - return '<span itemprop="addressCountry">' . $country . '</span>'; |
|
196 | + if ( ! empty($country)) { |
|
197 | + return '<span itemprop="addressCountry">'.$country.'</span>'; |
|
198 | 198 | } else { |
199 | 199 | return ''; |
200 | 200 | } |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | * @param EEI_Address $obj_with_address |
211 | 211 | * @return string |
212 | 212 | */ |
213 | - public static function postalCode( EEI_Address $obj_with_address = null ) { |
|
213 | + public static function postalCode(EEI_Address $obj_with_address = null) { |
|
214 | 214 | return $obj_with_address->zip() !== null && $obj_with_address->zip() !== '' ? '<span itemprop="postalCode">' |
215 | 215 | . $obj_with_address->zip() |
216 | 216 | . '</span>' : ''; |
@@ -226,8 +226,8 @@ discard block |
||
226 | 226 | * @param string $phone_nmbr |
227 | 227 | * @return string |
228 | 228 | */ |
229 | - public static function telephone( $phone_nmbr = null ) { |
|
230 | - return $phone_nmbr !== null && $phone_nmbr !== '' ? '<span itemprop="telephone">' . $phone_nmbr . '</span>' |
|
229 | + public static function telephone($phone_nmbr = null) { |
|
230 | + return $phone_nmbr !== null && $phone_nmbr !== '' ? '<span itemprop="telephone">'.$phone_nmbr.'</span>' |
|
231 | 231 | : ''; |
232 | 232 | } |
233 | 233 | |
@@ -243,19 +243,19 @@ discard block |
||
243 | 243 | * @param array $attributes - array of additional link attributes in attribute_name => value pairs. ie: array( 'title' => 'click here', 'class' => 'link-class' ) |
244 | 244 | * @return string (link) |
245 | 245 | */ |
246 | - public static function url( $url = null, $text = null, $attributes = array() ) { |
|
246 | + public static function url($url = null, $text = null, $attributes = array()) { |
|
247 | 247 | //Check the URL includes a scheme |
248 | 248 | $parsed_url = parse_url($url); |
249 | - if ( empty($parsed_url['scheme']) ) { |
|
250 | - $url = 'http://' . ltrim($url, '/'); |
|
249 | + if (empty($parsed_url['scheme'])) { |
|
250 | + $url = 'http://'.ltrim($url, '/'); |
|
251 | 251 | } |
252 | 252 | |
253 | 253 | $atts = ''; |
254 | - foreach ( $attributes as $attribute => $value ) { |
|
255 | - $atts .= ' ' . $attribute . '="' . $value . '"'; |
|
254 | + foreach ($attributes as $attribute => $value) { |
|
255 | + $atts .= ' '.$attribute.'="'.$value.'"'; |
|
256 | 256 | } |
257 | 257 | $text = $text !== null && $text !== '' ? $text : $url; |
258 | - return $url !== null && $url !== '' ? '<a itemprop="url" href="' . $url . '"' . $atts . '>' . $text . '</a>' |
|
258 | + return $url !== null && $url !== '' ? '<a itemprop="url" href="'.$url.'"'.$atts.'>'.$text.'</a>' |
|
259 | 259 | : ''; |
260 | 260 | } |
261 | 261 |
@@ -11,1386 +11,1386 @@ |
||
11 | 11 | class EE_Form_Section_Proper extends EE_Form_Section_Validatable |
12 | 12 | { |
13 | 13 | |
14 | - const SUBMITTED_FORM_DATA_SSN_KEY = 'submitted_form_data'; |
|
15 | - |
|
16 | - /** |
|
17 | - * Subsections |
|
18 | - * |
|
19 | - * @var EE_Form_Section_Validatable[] |
|
20 | - */ |
|
21 | - protected $_subsections = array(); |
|
22 | - |
|
23 | - /** |
|
24 | - * Strategy for laying out the form |
|
25 | - * |
|
26 | - * @var EE_Form_Section_Layout_Base |
|
27 | - */ |
|
28 | - protected $_layout_strategy; |
|
29 | - |
|
30 | - /** |
|
31 | - * Whether or not this form has received and validated a form submission yet |
|
32 | - * |
|
33 | - * @var boolean |
|
34 | - */ |
|
35 | - protected $_received_submission = false; |
|
36 | - |
|
37 | - /** |
|
38 | - * message displayed to users upon successful form submission |
|
39 | - * |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected $_form_submission_success_message = ''; |
|
43 | - |
|
44 | - /** |
|
45 | - * message displayed to users upon unsuccessful form submission |
|
46 | - * |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - protected $_form_submission_error_message = ''; |
|
50 | - |
|
51 | - /** |
|
52 | - * Stores all the data that will localized for form validation |
|
53 | - * |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - static protected $_js_localization = array(); |
|
57 | - |
|
58 | - /** |
|
59 | - * whether or not the form's localized validation JS vars have been set |
|
60 | - * |
|
61 | - * @type boolean |
|
62 | - */ |
|
63 | - static protected $_scripts_localized = false; |
|
64 | - |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * when constructing a proper form section, calls _construct_finalize on children |
|
69 | - * so that they know who their parent is, and what name they've been given. |
|
70 | - * |
|
71 | - * @param array $options_array { |
|
72 | - * @type $subsections EE_Form_Section_Validatable[] where keys are the section's name |
|
73 | - * @type $include string[] numerically-indexed where values are section names to be included, |
|
74 | - * and in that order. This is handy if you want |
|
75 | - * the subsections to be ordered differently than the default, and if you override |
|
76 | - * which fields are shown |
|
77 | - * @type $exclude string[] values are subsections to be excluded. This is handy if you want |
|
78 | - * to remove certain default subsections (note: if you specify BOTH 'include' AND |
|
79 | - * 'exclude', the inclusions will be applied first, and the exclusions will exclude |
|
80 | - * items from that list of inclusions) |
|
81 | - * @type $layout_strategy EE_Form_Section_Layout_Base strategy for laying out the form |
|
82 | - * } @see EE_Form_Section_Validatable::__construct() |
|
83 | - * @throws \EE_Error |
|
84 | - */ |
|
85 | - public function __construct($options_array = array()) |
|
86 | - { |
|
87 | - $options_array = (array)apply_filters('FHEE__EE_Form_Section_Proper___construct__options_array', $options_array, |
|
88 | - $this); |
|
89 | - //call parent first, as it may be setting the name |
|
90 | - parent::__construct($options_array); |
|
91 | - //if they've included subsections in the constructor, add them now |
|
92 | - if (isset($options_array['include'])) { |
|
93 | - //we are going to make sure we ONLY have those subsections to include |
|
94 | - //AND we are going to make sure they're in that specified order |
|
95 | - $reordered_subsections = array(); |
|
96 | - foreach ($options_array['include'] as $input_name) { |
|
97 | - if (isset($this->_subsections[$input_name])) { |
|
98 | - $reordered_subsections[$input_name] = $this->_subsections[$input_name]; |
|
99 | - } |
|
100 | - } |
|
101 | - $this->_subsections = $reordered_subsections; |
|
102 | - } |
|
103 | - if (isset($options_array['exclude'])) { |
|
104 | - $exclude = $options_array['exclude']; |
|
105 | - $this->_subsections = array_diff_key($this->_subsections, array_flip($exclude)); |
|
106 | - } |
|
107 | - if (isset($options_array['layout_strategy'])) { |
|
108 | - $this->_layout_strategy = $options_array['layout_strategy']; |
|
109 | - } |
|
110 | - if (! $this->_layout_strategy) { |
|
111 | - $this->_layout_strategy = is_admin() ? new EE_Admin_Two_Column_Layout() : new EE_Two_Column_Layout(); |
|
112 | - } |
|
113 | - $this->_layout_strategy->_construct_finalize($this); |
|
114 | - //ok so we are definitely going to want the forms JS, |
|
115 | - //so enqueue it or remember to enqueue it during wp_enqueue_scripts |
|
116 | - if (did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts')) { |
|
117 | - //ok so they've constructed this object after when they should have. |
|
118 | - //just enqueue the generic form scripts and initialize the form immediately in the JS |
|
119 | - \EE_Form_Section_Proper::wp_enqueue_scripts(true); |
|
120 | - } else { |
|
121 | - add_action('wp_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
122 | - add_action('admin_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
123 | - } |
|
124 | - add_action('wp_footer', array($this, 'ensure_scripts_localized'), 1); |
|
125 | - |
|
126 | - /** |
|
127 | - * Gives other plugins a chance to hook in before construct finalize is called. The form probably doesn't |
|
128 | - * yet have a parent form section. Since 4.9.32, when this action was introduced, this is the best place to |
|
129 | - * add a subsection onto a form, assuming you don't care what the form section's name, HTML ID, or HTML name etc are. |
|
130 | - * Also see AHEE__EE_Form_Section_Proper___construct_finalize__end |
|
131 | - * @since 4.9.32 |
|
132 | - * @param EE_Form_Section_Proper $this before __construct is done, but all of its logic, except maybe calling |
|
133 | - * _construct_finalize has been done |
|
134 | - * @param array $options_array options passed into the constructor |
|
135 | - */ |
|
136 | - do_action('AHEE__EE_Form_Input_Base___construct__before_construct_finalize_called', $this, $options_array); |
|
137 | - |
|
138 | - if (isset($options_array['name'])) { |
|
139 | - $this->_construct_finalize(null, $options_array['name']); |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - |
|
145 | - /** |
|
146 | - * Finishes construction given the parent form section and this form section's name |
|
147 | - * |
|
148 | - * @param EE_Form_Section_Proper $parent_form_section |
|
149 | - * @param string $name |
|
150 | - * @throws \EE_Error |
|
151 | - */ |
|
152 | - public function _construct_finalize($parent_form_section, $name) |
|
153 | - { |
|
154 | - parent::_construct_finalize($parent_form_section, $name); |
|
155 | - $this->_set_default_name_if_empty(); |
|
156 | - $this->_set_default_html_id_if_empty(); |
|
157 | - foreach ($this->_subsections as $subsection_name => $subsection) { |
|
158 | - if ($subsection instanceof EE_Form_Section_Base) { |
|
159 | - $subsection->_construct_finalize($this, $subsection_name); |
|
160 | - } else { |
|
161 | - throw new EE_Error( |
|
162 | - sprintf( |
|
163 | - __('Subsection "%s" is not an instanceof EE_Form_Section_Base on form "%s". It is a "%s"', |
|
164 | - 'event_espresso'), |
|
165 | - $subsection_name, |
|
166 | - get_class($this), |
|
167 | - $subsection ? get_class($subsection) : __('NULL', 'event_espresso') |
|
168 | - ) |
|
169 | - ); |
|
170 | - } |
|
171 | - } |
|
172 | - /** |
|
173 | - * Action performed just after form has been given a name (and HTML ID etc) and is fully constructed. |
|
174 | - * If you have code that should modify the form and needs it and its subsections to have a name, HTML ID (or other attributes derived |
|
175 | - * from the name like the HTML label id, etc), this is where it should be done. |
|
176 | - * This might only happen just before displaying the form, or just before it receives form submission data. |
|
177 | - * If you need to modify the form or its subsections before _construct_finalize is called on it (and we've |
|
178 | - * ensured it has a name, HTML IDs, etc |
|
179 | - * @param EE_Form_Section_Proper $this |
|
180 | - * @param EE_Form_Section_Proper|null $parent_form_section |
|
181 | - * @param string $name |
|
182 | - */ |
|
183 | - do_action('AHEE__EE_Form_Section_Proper___construct_finalize__end', $this, $parent_form_section, $name); |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - |
|
188 | - /** |
|
189 | - * Gets the layout strategy for this form section |
|
190 | - * |
|
191 | - * @return EE_Form_Section_Layout_Base |
|
192 | - */ |
|
193 | - public function get_layout_strategy() |
|
194 | - { |
|
195 | - return $this->_layout_strategy; |
|
196 | - } |
|
197 | - |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * Gets the HTML for a single input for this form section according |
|
202 | - * to the layout strategy |
|
203 | - * |
|
204 | - * @param EE_Form_Input_Base $input |
|
205 | - * @return string |
|
206 | - */ |
|
207 | - public function get_html_for_input($input) |
|
208 | - { |
|
209 | - return $this->_layout_strategy->layout_input($input); |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * was_submitted - checks if form inputs are present in request data |
|
216 | - * Basically an alias for form_data_present_in() (which is used by both |
|
217 | - * proper form sections and form inputs) |
|
218 | - * |
|
219 | - * @param null $form_data |
|
220 | - * @return boolean |
|
221 | - */ |
|
222 | - public function was_submitted($form_data = null) |
|
223 | - { |
|
224 | - return $this->form_data_present_in($form_data); |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - |
|
229 | - /** |
|
230 | - * After the form section is initially created, call this to sanitize the data in the submission |
|
231 | - * which relates to this form section, validate it, and set it as properties on the form. |
|
232 | - * |
|
233 | - * @param array|null $req_data should usually be $_POST (the default). |
|
234 | - * However, you CAN supply a different array. |
|
235 | - * Consider using set_defaults() instead however. |
|
236 | - * (If you rendered the form in the page using echo $form_x->get_html() |
|
237 | - * the inputs will have the correct name in the request data for this function |
|
238 | - * to find them and populate the form with them. |
|
239 | - * If you have a flat form (with only input subsections), |
|
240 | - * you can supply a flat array where keys |
|
241 | - * are the form input names and values are their values) |
|
242 | - * @param boolean $validate whether or not to perform validation on this data. Default is, |
|
243 | - * of course, to validate that data, and set errors on the invalid values. |
|
244 | - * But if the data has already been validated |
|
245 | - * (eg you validated the data then stored it in the DB) |
|
246 | - * you may want to skip this step. |
|
247 | - */ |
|
248 | - public function receive_form_submission($req_data = null, $validate = true) |
|
249 | - { |
|
250 | - $req_data = apply_filters('FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', $req_data, $this, |
|
251 | - $validate); |
|
252 | - if ($req_data === null) { |
|
253 | - $req_data = array_merge($_GET, $_POST); |
|
254 | - } |
|
255 | - $req_data = apply_filters('FHEE__EE_Form_Section_Proper__receive_form_submission__request_data', $req_data, |
|
256 | - $this); |
|
257 | - $this->_normalize($req_data); |
|
258 | - if ($validate) { |
|
259 | - $this->_validate(); |
|
260 | - //if it's invalid, we're going to want to re-display so remember what they submitted |
|
261 | - if (! $this->is_valid()) { |
|
262 | - $this->store_submitted_form_data_in_session(); |
|
263 | - } |
|
264 | - } |
|
265 | - do_action('AHEE__EE_Form_Section_Proper__receive_form_submission__end', $req_data, $this, $validate); |
|
266 | - } |
|
267 | - |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * caches the originally submitted input values in the session |
|
272 | - * so that they can be used to repopulate the form if it failed validation |
|
273 | - * |
|
274 | - * @return boolean whether or not the data was successfully stored in the session |
|
275 | - */ |
|
276 | - protected function store_submitted_form_data_in_session() |
|
277 | - { |
|
278 | - return EE_Registry::instance()->SSN->set_session_data( |
|
279 | - array( |
|
280 | - \EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY => $this->submitted_values(true), |
|
281 | - ) |
|
282 | - ); |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - |
|
287 | - /** |
|
288 | - * retrieves the originally submitted input values in the session |
|
289 | - * so that they can be used to repopulate the form if it failed validation |
|
290 | - * |
|
291 | - * @return array |
|
292 | - */ |
|
293 | - protected function get_submitted_form_data_from_session() |
|
294 | - { |
|
295 | - $session = EE_Registry::instance()->SSN; |
|
296 | - if ($session instanceof EE_Session) { |
|
297 | - return $session->get_session_data( |
|
298 | - \EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY |
|
299 | - ); |
|
300 | - } else { |
|
301 | - return array(); |
|
302 | - } |
|
303 | - } |
|
304 | - |
|
305 | - |
|
306 | - |
|
307 | - /** |
|
308 | - * flushed the originally submitted input values from the session |
|
309 | - * |
|
310 | - * @return boolean whether or not the data was successfully removed from the session |
|
311 | - */ |
|
312 | - protected function flush_submitted_form_data_from_session() |
|
313 | - { |
|
314 | - return EE_Registry::instance()->SSN->reset_data( |
|
315 | - array(\EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY) |
|
316 | - ); |
|
317 | - } |
|
318 | - |
|
319 | - |
|
320 | - |
|
321 | - /** |
|
322 | - * Populates this form and its subsections with data from the session. |
|
323 | - * (Wrapper for EE_Form_Section_Proper::receive_form_submission, so it shows |
|
324 | - * validation errors when displaying too) |
|
325 | - * Returns true if the form was populated from the session, false otherwise |
|
326 | - * |
|
327 | - * @return boolean |
|
328 | - */ |
|
329 | - public function populate_from_session() |
|
330 | - { |
|
331 | - $form_data_in_session = $this->get_submitted_form_data_from_session(); |
|
332 | - if (empty($form_data_in_session)) { |
|
333 | - return false; |
|
334 | - } |
|
335 | - $this->receive_form_submission($form_data_in_session); |
|
336 | - $this->flush_submitted_form_data_from_session(); |
|
337 | - if ($this->form_data_present_in($form_data_in_session)) { |
|
338 | - return true; |
|
339 | - } else { |
|
340 | - return false; |
|
341 | - } |
|
342 | - } |
|
343 | - |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * Populates the default data for the form, given an array where keys are |
|
348 | - * the input names, and values are their values (preferably normalized to be their |
|
349 | - * proper PHP types, not all strings... although that should be ok too). |
|
350 | - * Proper subsections are sub-arrays, the key being the subsection's name, and |
|
351 | - * the value being an array formatted in teh same way |
|
352 | - * |
|
353 | - * @param array $default_data |
|
354 | - */ |
|
355 | - public function populate_defaults($default_data) |
|
356 | - { |
|
357 | - foreach ($this->subsections(false) as $subsection_name => $subsection) { |
|
358 | - if (isset($default_data[$subsection_name])) { |
|
359 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
360 | - $subsection->set_default($default_data[$subsection_name]); |
|
361 | - } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
362 | - $subsection->populate_defaults($default_data[$subsection_name]); |
|
363 | - } |
|
364 | - } |
|
365 | - } |
|
366 | - } |
|
367 | - |
|
368 | - |
|
369 | - |
|
370 | - /** |
|
371 | - * returns true if subsection exists |
|
372 | - * |
|
373 | - * @param string $name |
|
374 | - * @return boolean |
|
375 | - */ |
|
376 | - public function subsection_exists($name) |
|
377 | - { |
|
378 | - return isset($this->_subsections[$name]) ? true : false; |
|
379 | - } |
|
380 | - |
|
381 | - |
|
382 | - |
|
383 | - /** |
|
384 | - * Gets the subsection specified by its name |
|
385 | - * |
|
386 | - * @param string $name |
|
387 | - * @param boolean $require_construction_to_be_finalized most client code should leave this as TRUE |
|
388 | - * so that the inputs will be properly configured. |
|
389 | - * However, some client code may be ok |
|
390 | - * with construction finalize being called later |
|
391 | - * (realizing that the subsections' html names |
|
392 | - * might not be set yet, etc.) |
|
393 | - * @return EE_Form_Section_Base |
|
394 | - * @throws \EE_Error |
|
395 | - */ |
|
396 | - public function get_subsection($name, $require_construction_to_be_finalized = true) |
|
397 | - { |
|
398 | - if ($require_construction_to_be_finalized) { |
|
399 | - $this->ensure_construct_finalized_called(); |
|
400 | - } |
|
401 | - return $this->subsection_exists($name) ? $this->_subsections[$name] : null; |
|
402 | - } |
|
403 | - |
|
404 | - |
|
405 | - |
|
406 | - /** |
|
407 | - * Gets all the validatable subsections of this form section |
|
408 | - * |
|
409 | - * @return EE_Form_Section_Validatable[] |
|
410 | - */ |
|
411 | - public function get_validatable_subsections() |
|
412 | - { |
|
413 | - $validatable_subsections = array(); |
|
414 | - foreach ($this->subsections() as $name => $obj) { |
|
415 | - if ($obj instanceof EE_Form_Section_Validatable) { |
|
416 | - $validatable_subsections[$name] = $obj; |
|
417 | - } |
|
418 | - } |
|
419 | - return $validatable_subsections; |
|
420 | - } |
|
421 | - |
|
422 | - |
|
423 | - |
|
424 | - /** |
|
425 | - * Gets an input by the given name. If not found, or if its not an EE_FOrm_Input_Base child, |
|
426 | - * throw an EE_Error. |
|
427 | - * |
|
428 | - * @param string $name |
|
429 | - * @param boolean $require_construction_to_be_finalized most client code should |
|
430 | - * leave this as TRUE so that the inputs will be properly |
|
431 | - * configured. However, some client code may be ok with |
|
432 | - * construction finalize being called later |
|
433 | - * (realizing that the subsections' html names might not be |
|
434 | - * set yet, etc.) |
|
435 | - * @return EE_Form_Input_Base |
|
436 | - * @throws EE_Error |
|
437 | - */ |
|
438 | - public function get_input($name, $require_construction_to_be_finalized = true) |
|
439 | - { |
|
440 | - $subsection = $this->get_subsection($name, $require_construction_to_be_finalized); |
|
441 | - if (! $subsection instanceof EE_Form_Input_Base) { |
|
442 | - throw new EE_Error( |
|
443 | - sprintf( |
|
444 | - __( |
|
445 | - "Subsection '%s' is not an instanceof EE_Form_Input_Base on form '%s'. It is a '%s'", |
|
446 | - 'event_espresso' |
|
447 | - ), |
|
448 | - $name, |
|
449 | - get_class($this), |
|
450 | - $subsection ? get_class($subsection) : __("NULL", 'event_espresso') |
|
451 | - ) |
|
452 | - ); |
|
453 | - } |
|
454 | - return $subsection; |
|
455 | - } |
|
456 | - |
|
457 | - |
|
458 | - |
|
459 | - /** |
|
460 | - * Like get_input(), gets the proper subsection of the form given the name, |
|
461 | - * otherwise throws an EE_Error |
|
462 | - * |
|
463 | - * @param string $name |
|
464 | - * @param boolean $require_construction_to_be_finalized most client code should |
|
465 | - * leave this as TRUE so that the inputs will be properly |
|
466 | - * configured. However, some client code may be ok with |
|
467 | - * construction finalize being called later |
|
468 | - * (realizing that the subsections' html names might not be |
|
469 | - * set yet, etc.) |
|
470 | - * @return EE_Form_Section_Proper |
|
471 | - * @throws EE_Error |
|
472 | - */ |
|
473 | - public function get_proper_subsection($name, $require_construction_to_be_finalized = true) |
|
474 | - { |
|
475 | - $subsection = $this->get_subsection($name, $require_construction_to_be_finalized); |
|
476 | - if (! $subsection instanceof EE_Form_Section_Proper) { |
|
477 | - throw new EE_Error( |
|
478 | - sprintf( |
|
479 | - __("Subsection '%'s is not an instanceof EE_Form_Section_Proper on form '%s'", 'event_espresso'), |
|
480 | - $name, |
|
481 | - get_class($this) |
|
482 | - ) |
|
483 | - ); |
|
484 | - } |
|
485 | - return $subsection; |
|
486 | - } |
|
487 | - |
|
488 | - |
|
489 | - |
|
490 | - /** |
|
491 | - * Gets the value of the specified input. Should be called after receive_form_submission() |
|
492 | - * or populate_defaults() on the form, where the normalized value on the input is set. |
|
493 | - * |
|
494 | - * @param string $name |
|
495 | - * @return mixed depending on the input's type and its normalization strategy |
|
496 | - * @throws \EE_Error |
|
497 | - */ |
|
498 | - public function get_input_value($name) |
|
499 | - { |
|
500 | - $input = $this->get_input($name); |
|
501 | - return $input->normalized_value(); |
|
502 | - } |
|
503 | - |
|
504 | - |
|
505 | - |
|
506 | - /** |
|
507 | - * Checks if this form section itself is valid, and then checks its subsections |
|
508 | - * |
|
509 | - * @throws EE_Error |
|
510 | - * @return boolean |
|
511 | - */ |
|
512 | - public function is_valid() |
|
513 | - { |
|
514 | - if (! $this->has_received_submission()) { |
|
515 | - throw new EE_Error( |
|
516 | - sprintf( |
|
517 | - __( |
|
518 | - "You cannot check if a form is valid before receiving the form submission using receive_form_submission", |
|
519 | - "event_espresso" |
|
520 | - ) |
|
521 | - ) |
|
522 | - ); |
|
523 | - } |
|
524 | - if (! parent::is_valid()) { |
|
525 | - return false; |
|
526 | - } |
|
527 | - // ok so no general errors to this entire form section. |
|
528 | - // so let's check the subsections, but only set errors if that hasn't been done yet |
|
529 | - $set_submission_errors = $this->submission_error_message() === '' ? true : false; |
|
530 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
531 | - if (! $subsection->is_valid() || $subsection->get_validation_error_string() !== '') { |
|
532 | - if ($set_submission_errors) { |
|
533 | - $this->set_submission_error_message($subsection->get_validation_error_string()); |
|
534 | - } |
|
535 | - return false; |
|
536 | - } |
|
537 | - } |
|
538 | - return true; |
|
539 | - } |
|
540 | - |
|
541 | - |
|
542 | - |
|
543 | - /** |
|
544 | - * gets teh default name of this form section if none is specified |
|
545 | - * |
|
546 | - * @return string |
|
547 | - */ |
|
548 | - protected function _set_default_name_if_empty() |
|
549 | - { |
|
550 | - if (! $this->_name) { |
|
551 | - $classname = get_class($this); |
|
552 | - $default_name = str_replace("EE_", "", $classname); |
|
553 | - $this->_name = $default_name; |
|
554 | - } |
|
555 | - } |
|
556 | - |
|
557 | - |
|
558 | - |
|
559 | - /** |
|
560 | - * Returns the HTML for the form, except for the form opening and closing tags |
|
561 | - * (as the form section doesn't know where you necessarily want to send the information to), |
|
562 | - * and except for a submit button. Enqueus JS and CSS; if called early enough we will |
|
563 | - * try to enqueue them in the header, otherwise they'll be enqueued in the footer. |
|
564 | - * Not doing_it_wrong because theoretically this CAN be used properly, |
|
565 | - * provided its used during "wp_enqueue_scripts", or it doesn't need to enqueue |
|
566 | - * any CSS. |
|
567 | - * |
|
568 | - * @throws \EE_Error |
|
569 | - */ |
|
570 | - public function get_html_and_js() |
|
571 | - { |
|
572 | - $this->enqueue_js(); |
|
573 | - return $this->get_html(); |
|
574 | - } |
|
575 | - |
|
576 | - |
|
577 | - |
|
578 | - /** |
|
579 | - * returns HTML for displaying this form section. recursively calls display_section() on all subsections |
|
580 | - * |
|
581 | - * @param bool $display_previously_submitted_data |
|
582 | - * @return string |
|
583 | - */ |
|
584 | - public function get_html($display_previously_submitted_data = true) |
|
585 | - { |
|
586 | - $this->ensure_construct_finalized_called(); |
|
587 | - if ($display_previously_submitted_data) { |
|
588 | - $this->populate_from_session(); |
|
589 | - } |
|
590 | - return $this->_form_html_filter |
|
591 | - ? $this->_form_html_filter->filterHtml($this->_layout_strategy->layout_form(), $this) |
|
592 | - : $this->_layout_strategy->layout_form(); |
|
593 | - } |
|
594 | - |
|
595 | - |
|
596 | - |
|
597 | - /** |
|
598 | - * enqueues JS and CSS for the form. |
|
599 | - * It is preferred to call this before wp_enqueue_scripts so the |
|
600 | - * scripts and styles can be put in the header, but if called later |
|
601 | - * they will be put in the footer (which is OK for JS, but in HTML4 CSS should |
|
602 | - * only be in the header; but in HTML5 its ok in the body. |
|
603 | - * See http://stackoverflow.com/questions/4957446/load-external-css-file-in-body-tag. |
|
604 | - * So if your form enqueues CSS, it's preferred to call this before wp_enqueue_scripts.) |
|
605 | - * |
|
606 | - * @return string |
|
607 | - * @throws \EE_Error |
|
608 | - */ |
|
609 | - public function enqueue_js() |
|
610 | - { |
|
611 | - $this->_enqueue_and_localize_form_js(); |
|
612 | - foreach ($this->subsections() as $subsection) { |
|
613 | - $subsection->enqueue_js(); |
|
614 | - } |
|
615 | - } |
|
616 | - |
|
617 | - |
|
618 | - |
|
619 | - /** |
|
620 | - * adds a filter so that jquery validate gets enqueued in EE_System::wp_enqueue_scripts(). |
|
621 | - * This must be done BEFORE wp_enqueue_scripts() gets called, which is on |
|
622 | - * the wp_enqueue_scripts hook. |
|
623 | - * However, registering the form js and localizing it can happen when we |
|
624 | - * actually output the form (which is preferred, seeing how teh form's fields |
|
625 | - * could change until it's actually outputted) |
|
626 | - * |
|
627 | - * @param boolean $init_form_validation_automatically whether or not we want the form validation |
|
628 | - * to be triggered automatically or not |
|
629 | - * @return void |
|
630 | - */ |
|
631 | - public static function wp_enqueue_scripts($init_form_validation_automatically = true) |
|
632 | - { |
|
633 | - add_filter('FHEE_load_jquery_validate', '__return_true'); |
|
634 | - wp_register_script( |
|
635 | - 'ee_form_section_validation', |
|
636 | - EE_GLOBAL_ASSETS_URL . 'scripts' . DS . 'form_section_validation.js', |
|
637 | - array('jquery-validate', 'jquery-ui-datepicker', 'jquery-validate-extra-methods'), |
|
638 | - EVENT_ESPRESSO_VERSION, |
|
639 | - true |
|
640 | - ); |
|
641 | - wp_localize_script( |
|
642 | - 'ee_form_section_validation', |
|
643 | - 'ee_form_section_validation_init', |
|
644 | - array('init' => $init_form_validation_automatically ? true : false) |
|
645 | - ); |
|
646 | - } |
|
647 | - |
|
648 | - |
|
649 | - |
|
650 | - /** |
|
651 | - * gets the variables used by form_section_validation.js. |
|
652 | - * This needs to be called AFTER we've called $this->_enqueue_jquery_validate_script, |
|
653 | - * but before the wordpress hook wp_loaded |
|
654 | - * |
|
655 | - * @throws \EE_Error |
|
656 | - */ |
|
657 | - public function _enqueue_and_localize_form_js() |
|
658 | - { |
|
659 | - $this->ensure_construct_finalized_called(); |
|
660 | - //actually, we don't want to localize just yet. There may be other forms on the page. |
|
661 | - //so we need to add our form section data to a static variable accessible by all form sections |
|
662 | - //and localize it just before the footer |
|
663 | - $this->localize_validation_rules(); |
|
664 | - add_action('wp_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'), 2); |
|
665 | - add_action('admin_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms')); |
|
666 | - } |
|
667 | - |
|
668 | - |
|
669 | - |
|
670 | - /** |
|
671 | - * add our form section data to a static variable accessible by all form sections |
|
672 | - * |
|
673 | - * @param bool $return_for_subsection |
|
674 | - * @return void |
|
675 | - * @throws \EE_Error |
|
676 | - */ |
|
677 | - public function localize_validation_rules($return_for_subsection = false) |
|
678 | - { |
|
679 | - // we only want to localize vars ONCE for the entire form, |
|
680 | - // so if the form section doesn't have a parent, then it must be the top dog |
|
681 | - if ($return_for_subsection || ! $this->parent_section()) { |
|
682 | - EE_Form_Section_Proper::$_js_localization['form_data'][$this->html_id()] = array( |
|
683 | - 'form_section_id' => $this->html_id(true), |
|
684 | - 'validation_rules' => $this->get_jquery_validation_rules(), |
|
685 | - 'other_data' => $this->get_other_js_data(), |
|
686 | - 'errors' => $this->subsection_validation_errors_by_html_name(), |
|
687 | - ); |
|
688 | - EE_Form_Section_Proper::$_scripts_localized = true; |
|
689 | - } |
|
690 | - } |
|
691 | - |
|
692 | - |
|
693 | - |
|
694 | - /** |
|
695 | - * Gets an array of extra data that will be useful for client-side javascript. |
|
696 | - * This is primarily data added by inputs and forms in addition to any |
|
697 | - * scripts they might enqueue |
|
698 | - * |
|
699 | - * @param array $form_other_js_data |
|
700 | - * @return array |
|
701 | - */ |
|
702 | - public function get_other_js_data($form_other_js_data = array()) |
|
703 | - { |
|
704 | - foreach ($this->subsections() as $subsection) { |
|
705 | - $form_other_js_data = $subsection->get_other_js_data($form_other_js_data); |
|
706 | - } |
|
707 | - return $form_other_js_data; |
|
708 | - } |
|
709 | - |
|
710 | - |
|
711 | - |
|
712 | - /** |
|
713 | - * Gets a flat array of inputs for this form section and its subsections. |
|
714 | - * Keys are their form names, and values are the inputs themselves |
|
715 | - * |
|
716 | - * @return EE_Form_Input_Base |
|
717 | - */ |
|
718 | - public function inputs_in_subsections() |
|
719 | - { |
|
720 | - $inputs = array(); |
|
721 | - foreach ($this->subsections() as $subsection) { |
|
722 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
723 | - $inputs[$subsection->html_name()] = $subsection; |
|
724 | - } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
725 | - $inputs += $subsection->inputs_in_subsections(); |
|
726 | - } |
|
727 | - } |
|
728 | - return $inputs; |
|
729 | - } |
|
730 | - |
|
731 | - |
|
732 | - |
|
733 | - /** |
|
734 | - * Gets a flat array of all the validation errors. |
|
735 | - * Keys are html names (because those should be unique) |
|
736 | - * and values are a string of all their validation errors |
|
737 | - * |
|
738 | - * @return string[] |
|
739 | - */ |
|
740 | - public function subsection_validation_errors_by_html_name() |
|
741 | - { |
|
742 | - $inputs = $this->inputs(); |
|
743 | - $errors = array(); |
|
744 | - foreach ($inputs as $form_input) { |
|
745 | - if ($form_input instanceof EE_Form_Input_Base && $form_input->get_validation_errors()) { |
|
746 | - $errors[$form_input->html_name()] = $form_input->get_validation_error_string(); |
|
747 | - } |
|
748 | - } |
|
749 | - return $errors; |
|
750 | - } |
|
751 | - |
|
752 | - |
|
753 | - |
|
754 | - /** |
|
755 | - * passes all the form data required by the JS to the JS, and enqueues the few required JS files. |
|
756 | - * Should be setup by each form during the _enqueues_and_localize_form_js |
|
757 | - */ |
|
758 | - public static function localize_script_for_all_forms() |
|
759 | - { |
|
760 | - //allow inputs and stuff to hook in their JS and stuff here |
|
761 | - do_action('AHEE__EE_Form_Section_Proper__localize_script_for_all_forms__begin'); |
|
762 | - EE_Form_Section_Proper::$_js_localization['localized_error_messages'] = EE_Form_Section_Proper::_get_localized_error_messages(); |
|
763 | - $email_validation_level = isset(EE_Registry::instance()->CFG->registration->email_validation_level) |
|
764 | - ? EE_Registry::instance()->CFG->registration->email_validation_level |
|
765 | - : 'wp_default'; |
|
766 | - EE_Form_Section_Proper::$_js_localization['email_validation_level'] = $email_validation_level; |
|
767 | - wp_enqueue_script('ee_form_section_validation'); |
|
768 | - wp_localize_script( |
|
769 | - 'ee_form_section_validation', |
|
770 | - 'ee_form_section_vars', |
|
771 | - EE_Form_Section_Proper::$_js_localization |
|
772 | - ); |
|
773 | - } |
|
774 | - |
|
775 | - |
|
776 | - |
|
777 | - /** |
|
778 | - * ensure_scripts_localized |
|
779 | - */ |
|
780 | - public function ensure_scripts_localized() |
|
781 | - { |
|
782 | - if (! EE_Form_Section_Proper::$_scripts_localized) { |
|
783 | - $this->_enqueue_and_localize_form_js(); |
|
784 | - } |
|
785 | - } |
|
786 | - |
|
787 | - |
|
788 | - |
|
789 | - /** |
|
790 | - * Gets the hard-coded validation error messages to be used in the JS. The convention |
|
791 | - * is that the key here should be the same as the custom validation rule put in the JS file |
|
792 | - * |
|
793 | - * @return array keys are custom validation rules, and values are internationalized strings |
|
794 | - */ |
|
795 | - private static function _get_localized_error_messages() |
|
796 | - { |
|
797 | - return array( |
|
798 | - 'validUrl' => __("This is not a valid absolute URL. Eg, http://domain.com/monkey.jpg", "event_espresso"), |
|
799 | - 'regex' => __('Please check your input', 'event_espresso'), |
|
800 | - ); |
|
801 | - } |
|
802 | - |
|
803 | - |
|
804 | - |
|
805 | - /** |
|
806 | - * @return array |
|
807 | - */ |
|
808 | - public static function js_localization() |
|
809 | - { |
|
810 | - return self::$_js_localization; |
|
811 | - } |
|
812 | - |
|
813 | - |
|
814 | - |
|
815 | - /** |
|
816 | - * @return array |
|
817 | - */ |
|
818 | - public static function reset_js_localization() |
|
819 | - { |
|
820 | - self::$_js_localization = array(); |
|
821 | - } |
|
822 | - |
|
823 | - |
|
824 | - |
|
825 | - /** |
|
826 | - * Gets the JS to put inside the jquery validation rules for subsection of this form section. |
|
827 | - * See parent function for more... |
|
828 | - * |
|
829 | - * @return array |
|
830 | - */ |
|
831 | - public function get_jquery_validation_rules() |
|
832 | - { |
|
833 | - $jquery_validation_rules = array(); |
|
834 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
835 | - $jquery_validation_rules = array_merge( |
|
836 | - $jquery_validation_rules, |
|
837 | - $subsection->get_jquery_validation_rules() |
|
838 | - ); |
|
839 | - } |
|
840 | - return $jquery_validation_rules; |
|
841 | - } |
|
842 | - |
|
843 | - |
|
844 | - |
|
845 | - /** |
|
846 | - * Sanitizes all the data and sets the sanitized value of each field |
|
847 | - * |
|
848 | - * @param array $req_data like $_POST |
|
849 | - * @return void |
|
850 | - */ |
|
851 | - protected function _normalize($req_data) |
|
852 | - { |
|
853 | - $this->_received_submission = true; |
|
854 | - $this->_validation_errors = array(); |
|
855 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
856 | - try { |
|
857 | - $subsection->_normalize($req_data); |
|
858 | - } catch (EE_Validation_Error $e) { |
|
859 | - $subsection->add_validation_error($e); |
|
860 | - } |
|
861 | - } |
|
862 | - } |
|
863 | - |
|
864 | - |
|
865 | - |
|
866 | - /** |
|
867 | - * Performs validation on this form section and its subsections. |
|
868 | - * For each subsection, |
|
869 | - * calls _validate_{subsection_name} on THIS form (if the function exists) |
|
870 | - * and passes it the subsection, then calls _validate on that subsection. |
|
871 | - * If you need to perform validation on the form as a whole (considering multiple) |
|
872 | - * you would be best to override this _validate method, |
|
873 | - * calling parent::_validate() first. |
|
874 | - */ |
|
875 | - protected function _validate() |
|
876 | - { |
|
877 | - foreach ($this->get_validatable_subsections() as $subsection_name => $subsection) { |
|
878 | - if (method_exists($this, '_validate_' . $subsection_name)) { |
|
879 | - call_user_func_array(array($this, '_validate_' . $subsection_name), array($subsection)); |
|
880 | - } |
|
881 | - $subsection->_validate(); |
|
882 | - } |
|
883 | - } |
|
884 | - |
|
885 | - |
|
886 | - |
|
887 | - /** |
|
888 | - * Gets all the validated inputs for the form section |
|
889 | - * |
|
890 | - * @return array |
|
891 | - */ |
|
892 | - public function valid_data() |
|
893 | - { |
|
894 | - $inputs = array(); |
|
895 | - foreach ($this->subsections() as $subsection_name => $subsection) { |
|
896 | - if ($subsection instanceof EE_Form_Section_Proper) { |
|
897 | - $inputs[$subsection_name] = $subsection->valid_data(); |
|
898 | - } else if ($subsection instanceof EE_Form_Input_Base) { |
|
899 | - $inputs[$subsection_name] = $subsection->normalized_value(); |
|
900 | - } |
|
901 | - } |
|
902 | - return $inputs; |
|
903 | - } |
|
904 | - |
|
905 | - |
|
906 | - |
|
907 | - /** |
|
908 | - * Gets all the inputs on this form section |
|
909 | - * |
|
910 | - * @return EE_Form_Input_Base[] |
|
911 | - */ |
|
912 | - public function inputs() |
|
913 | - { |
|
914 | - $inputs = array(); |
|
915 | - foreach ($this->subsections() as $subsection_name => $subsection) { |
|
916 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
917 | - $inputs[$subsection_name] = $subsection; |
|
918 | - } |
|
919 | - } |
|
920 | - return $inputs; |
|
921 | - } |
|
922 | - |
|
923 | - |
|
924 | - |
|
925 | - /** |
|
926 | - * Gets all the subsections which are a proper form |
|
927 | - * |
|
928 | - * @return EE_Form_Section_Proper[] |
|
929 | - */ |
|
930 | - public function subforms() |
|
931 | - { |
|
932 | - $form_sections = array(); |
|
933 | - foreach ($this->subsections() as $name => $obj) { |
|
934 | - if ($obj instanceof EE_Form_Section_Proper) { |
|
935 | - $form_sections[$name] = $obj; |
|
936 | - } |
|
937 | - } |
|
938 | - return $form_sections; |
|
939 | - } |
|
940 | - |
|
941 | - |
|
942 | - |
|
943 | - /** |
|
944 | - * Gets all the subsections (inputs, proper subsections, or html-only sections). |
|
945 | - * Consider using inputs() or subforms() |
|
946 | - * if you only want form inputs or proper form sections. |
|
947 | - * |
|
948 | - * @param boolean $require_construction_to_be_finalized most client code should |
|
949 | - * leave this as TRUE so that the inputs will be properly |
|
950 | - * configured. However, some client code may be ok with |
|
951 | - * construction finalize being called later |
|
952 | - * (realizing that the subsections' html names might not be |
|
953 | - * set yet, etc.) |
|
954 | - * @return EE_Form_Section_Proper[] |
|
955 | - */ |
|
956 | - public function subsections($require_construction_to_be_finalized = true) |
|
957 | - { |
|
958 | - if ($require_construction_to_be_finalized) { |
|
959 | - $this->ensure_construct_finalized_called(); |
|
960 | - } |
|
961 | - return $this->_subsections; |
|
962 | - } |
|
963 | - |
|
964 | - |
|
965 | - |
|
966 | - /** |
|
967 | - * Returns a simple array where keys are input names, and values are their normalized |
|
968 | - * values. (Similar to calling get_input_value on inputs) |
|
969 | - * |
|
970 | - * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
971 | - * or just this forms' direct children inputs |
|
972 | - * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
973 | - * or allow multidimensional array |
|
974 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
975 | - * with array keys being input names |
|
976 | - * (regardless of whether they are from a subsection or not), |
|
977 | - * and if $flatten is FALSE it can be a multidimensional array |
|
978 | - * where keys are always subsection names and values are either |
|
979 | - * the input's normalized value, or an array like the top-level array |
|
980 | - */ |
|
981 | - public function input_values($include_subform_inputs = false, $flatten = false) |
|
982 | - { |
|
983 | - return $this->_input_values(false, $include_subform_inputs, $flatten); |
|
984 | - } |
|
985 | - |
|
986 | - |
|
987 | - |
|
988 | - /** |
|
989 | - * Similar to EE_Form_Section_Proper::input_values(), except this returns the 'display_value' |
|
990 | - * of each input. On some inputs (especially radio boxes or checkboxes), the value stored |
|
991 | - * is not necessarily the value we want to display to users. This creates an array |
|
992 | - * where keys are the input names, and values are their display values |
|
993 | - * |
|
994 | - * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
995 | - * or just this forms' direct children inputs |
|
996 | - * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
997 | - * or allow multidimensional array |
|
998 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
999 | - * with array keys being input names |
|
1000 | - * (regardless of whether they are from a subsection or not), |
|
1001 | - * and if $flatten is FALSE it can be a multidimensional array |
|
1002 | - * where keys are always subsection names and values are either |
|
1003 | - * the input's normalized value, or an array like the top-level array |
|
1004 | - */ |
|
1005 | - public function input_pretty_values($include_subform_inputs = false, $flatten = false) |
|
1006 | - { |
|
1007 | - return $this->_input_values(true, $include_subform_inputs, $flatten); |
|
1008 | - } |
|
1009 | - |
|
1010 | - |
|
1011 | - |
|
1012 | - /** |
|
1013 | - * Gets the input values from the form |
|
1014 | - * |
|
1015 | - * @param boolean $pretty Whether to retrieve the pretty value, |
|
1016 | - * or just the normalized value |
|
1017 | - * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
1018 | - * or just this forms' direct children inputs |
|
1019 | - * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
1020 | - * or allow multidimensional array |
|
1021 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array with array keys being |
|
1022 | - * input names (regardless of whether they are from a subsection or not), |
|
1023 | - * and if $flatten is FALSE it can be a multidimensional array |
|
1024 | - * where keys are always subsection names and values are either |
|
1025 | - * the input's normalized value, or an array like the top-level array |
|
1026 | - */ |
|
1027 | - public function _input_values($pretty = false, $include_subform_inputs = false, $flatten = false) |
|
1028 | - { |
|
1029 | - $input_values = array(); |
|
1030 | - foreach ($this->subsections() as $subsection_name => $subsection) { |
|
1031 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
1032 | - $input_values[$subsection_name] = $pretty |
|
1033 | - ? $subsection->pretty_value() |
|
1034 | - : $subsection->normalized_value(); |
|
1035 | - } else if ($subsection instanceof EE_Form_Section_Proper && $include_subform_inputs) { |
|
1036 | - $subform_input_values = $subsection->_input_values($pretty, $include_subform_inputs, $flatten); |
|
1037 | - if ($flatten) { |
|
1038 | - $input_values = array_merge($input_values, $subform_input_values); |
|
1039 | - } else { |
|
1040 | - $input_values[$subsection_name] = $subform_input_values; |
|
1041 | - } |
|
1042 | - } |
|
1043 | - } |
|
1044 | - return $input_values; |
|
1045 | - } |
|
1046 | - |
|
1047 | - |
|
1048 | - |
|
1049 | - /** |
|
1050 | - * Gets the originally submitted input values from the form |
|
1051 | - * |
|
1052 | - * @param boolean $include_subforms Whether to include inputs from subforms, |
|
1053 | - * or just this forms' direct children inputs |
|
1054 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
1055 | - * with array keys being input names |
|
1056 | - * (regardless of whether they are from a subsection or not), |
|
1057 | - * and if $flatten is FALSE it can be a multidimensional array |
|
1058 | - * where keys are always subsection names and values are either |
|
1059 | - * the input's normalized value, or an array like the top-level array |
|
1060 | - */ |
|
1061 | - public function submitted_values($include_subforms = false) |
|
1062 | - { |
|
1063 | - $submitted_values = array(); |
|
1064 | - foreach ($this->subsections() as $subsection) { |
|
1065 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
1066 | - // is this input part of an array of inputs? |
|
1067 | - if (strpos($subsection->html_name(), '[') !== false) { |
|
1068 | - $full_input_name = \EEH_Array::convert_array_values_to_keys( |
|
1069 | - explode('[', str_replace(']', '', $subsection->html_name())), |
|
1070 | - $subsection->raw_value() |
|
1071 | - ); |
|
1072 | - $submitted_values = array_replace_recursive($submitted_values, $full_input_name); |
|
1073 | - } else { |
|
1074 | - $submitted_values[$subsection->html_name()] = $subsection->raw_value(); |
|
1075 | - } |
|
1076 | - } else if ($subsection instanceof EE_Form_Section_Proper && $include_subforms) { |
|
1077 | - $subform_input_values = $subsection->submitted_values($include_subforms); |
|
1078 | - $submitted_values = array_replace_recursive($submitted_values, $subform_input_values); |
|
1079 | - } |
|
1080 | - } |
|
1081 | - return $submitted_values; |
|
1082 | - } |
|
1083 | - |
|
1084 | - |
|
1085 | - |
|
1086 | - /** |
|
1087 | - * Indicates whether or not this form has received a submission yet |
|
1088 | - * (ie, had receive_form_submission called on it yet) |
|
1089 | - * |
|
1090 | - * @return boolean |
|
1091 | - * @throws \EE_Error |
|
1092 | - */ |
|
1093 | - public function has_received_submission() |
|
1094 | - { |
|
1095 | - $this->ensure_construct_finalized_called(); |
|
1096 | - return $this->_received_submission; |
|
1097 | - } |
|
1098 | - |
|
1099 | - |
|
1100 | - |
|
1101 | - /** |
|
1102 | - * Equivalent to passing 'exclude' in the constructor's options array. |
|
1103 | - * Removes the listed inputs from the form |
|
1104 | - * |
|
1105 | - * @param array $inputs_to_exclude values are the input names |
|
1106 | - * @return void |
|
1107 | - */ |
|
1108 | - public function exclude(array $inputs_to_exclude = array()) |
|
1109 | - { |
|
1110 | - foreach ($inputs_to_exclude as $input_to_exclude_name) { |
|
1111 | - unset($this->_subsections[$input_to_exclude_name]); |
|
1112 | - } |
|
1113 | - } |
|
1114 | - |
|
1115 | - |
|
1116 | - |
|
1117 | - /** |
|
1118 | - * @param array $inputs_to_hide |
|
1119 | - * @throws \EE_Error |
|
1120 | - */ |
|
1121 | - public function hide(array $inputs_to_hide = array()) |
|
1122 | - { |
|
1123 | - foreach ($inputs_to_hide as $input_to_hide) { |
|
1124 | - $input = $this->get_input($input_to_hide); |
|
1125 | - $input->set_display_strategy(new EE_Hidden_Display_Strategy()); |
|
1126 | - } |
|
1127 | - } |
|
1128 | - |
|
1129 | - |
|
1130 | - |
|
1131 | - /** |
|
1132 | - * add_subsections |
|
1133 | - * Adds the listed subsections to the form section. |
|
1134 | - * If $subsection_name_to_target is provided, |
|
1135 | - * then new subsections are added before or after that subsection, |
|
1136 | - * otherwise to the start or end of the entire subsections array. |
|
1137 | - * |
|
1138 | - * @param EE_Form_Section_Base[] $new_subsections array of new form subsections |
|
1139 | - * where keys are their names |
|
1140 | - * @param string $subsection_name_to_target an existing for section that $new_subsections |
|
1141 | - * should be added before or after |
|
1142 | - * IF $subsection_name_to_target is null, |
|
1143 | - * then $new_subsections will be added to |
|
1144 | - * the beginning or end of the entire subsections array |
|
1145 | - * @param boolean $add_before whether to add $new_subsections, before or after |
|
1146 | - * $subsection_name_to_target, |
|
1147 | - * or if $subsection_name_to_target is null, |
|
1148 | - * before or after entire subsections array |
|
1149 | - * @return void |
|
1150 | - * @throws \EE_Error |
|
1151 | - */ |
|
1152 | - public function add_subsections($new_subsections, $subsection_name_to_target = null, $add_before = true) |
|
1153 | - { |
|
1154 | - foreach ($new_subsections as $subsection_name => $subsection) { |
|
1155 | - if (! $subsection instanceof EE_Form_Section_Base) { |
|
1156 | - EE_Error::add_error( |
|
1157 | - sprintf( |
|
1158 | - __( |
|
1159 | - "Trying to add a %s as a subsection (it was named '%s') to the form section '%s'. It was removed.", |
|
1160 | - "event_espresso" |
|
1161 | - ), |
|
1162 | - get_class($subsection), |
|
1163 | - $subsection_name, |
|
1164 | - $this->name() |
|
1165 | - ) |
|
1166 | - ); |
|
1167 | - unset($new_subsections[$subsection_name]); |
|
1168 | - } |
|
1169 | - } |
|
1170 | - $this->_subsections = EEH_Array::insert_into_array( |
|
1171 | - $this->_subsections, |
|
1172 | - $new_subsections, |
|
1173 | - $subsection_name_to_target, |
|
1174 | - $add_before |
|
1175 | - ); |
|
1176 | - if ($this->_construction_finalized) { |
|
1177 | - foreach ($this->_subsections as $name => $subsection) { |
|
1178 | - $subsection->_construct_finalize($this, $name); |
|
1179 | - } |
|
1180 | - } |
|
1181 | - } |
|
1182 | - |
|
1183 | - |
|
1184 | - |
|
1185 | - /** |
|
1186 | - * Just gets all validatable subsections to clean their sensitive data |
|
1187 | - */ |
|
1188 | - public function clean_sensitive_data() |
|
1189 | - { |
|
1190 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
1191 | - $subsection->clean_sensitive_data(); |
|
1192 | - } |
|
1193 | - } |
|
1194 | - |
|
1195 | - |
|
1196 | - |
|
1197 | - /** |
|
1198 | - * @param string $form_submission_error_message |
|
1199 | - */ |
|
1200 | - public function set_submission_error_message($form_submission_error_message = '') |
|
1201 | - { |
|
1202 | - $this->_form_submission_error_message .= ! empty($form_submission_error_message) |
|
1203 | - ? $form_submission_error_message |
|
1204 | - : __('Form submission failed due to errors', 'event_espresso'); |
|
1205 | - } |
|
1206 | - |
|
1207 | - |
|
1208 | - |
|
1209 | - /** |
|
1210 | - * @return string |
|
1211 | - */ |
|
1212 | - public function submission_error_message() |
|
1213 | - { |
|
1214 | - return $this->_form_submission_error_message; |
|
1215 | - } |
|
1216 | - |
|
1217 | - |
|
1218 | - |
|
1219 | - /** |
|
1220 | - * @param string $form_submission_success_message |
|
1221 | - */ |
|
1222 | - public function set_submission_success_message($form_submission_success_message) |
|
1223 | - { |
|
1224 | - $this->_form_submission_success_message .= ! empty($form_submission_success_message) |
|
1225 | - ? $form_submission_success_message |
|
1226 | - : __('Form submitted successfully', 'event_espresso'); |
|
1227 | - } |
|
1228 | - |
|
1229 | - |
|
1230 | - |
|
1231 | - /** |
|
1232 | - * @return string |
|
1233 | - */ |
|
1234 | - public function submission_success_message() |
|
1235 | - { |
|
1236 | - return $this->_form_submission_success_message; |
|
1237 | - } |
|
1238 | - |
|
1239 | - |
|
1240 | - |
|
1241 | - /** |
|
1242 | - * Returns the prefix that should be used on child of this form section for |
|
1243 | - * their html names. If this form section itself has a parent, prepends ITS |
|
1244 | - * prefix onto this form section's prefix. Used primarily by |
|
1245 | - * EE_Form_Input_Base::_set_default_html_name_if_empty |
|
1246 | - * |
|
1247 | - * @return string |
|
1248 | - * @throws \EE_Error |
|
1249 | - */ |
|
1250 | - public function html_name_prefix() |
|
1251 | - { |
|
1252 | - if ($this->parent_section() instanceof EE_Form_Section_Proper) { |
|
1253 | - return $this->parent_section()->html_name_prefix() . '[' . $this->name() . ']'; |
|
1254 | - } else { |
|
1255 | - return $this->name(); |
|
1256 | - } |
|
1257 | - } |
|
1258 | - |
|
1259 | - |
|
1260 | - |
|
1261 | - /** |
|
1262 | - * Gets the name, but first checks _construct_finalize has been called. If not, |
|
1263 | - * calls it (assumes there is no parent and that we want the name to be whatever |
|
1264 | - * was set, which is probably nothing, or the classname) |
|
1265 | - * |
|
1266 | - * @return string |
|
1267 | - * @throws \EE_Error |
|
1268 | - */ |
|
1269 | - public function name() |
|
1270 | - { |
|
1271 | - $this->ensure_construct_finalized_called(); |
|
1272 | - return parent::name(); |
|
1273 | - } |
|
1274 | - |
|
1275 | - |
|
1276 | - |
|
1277 | - /** |
|
1278 | - * @return EE_Form_Section_Proper |
|
1279 | - * @throws \EE_Error |
|
1280 | - */ |
|
1281 | - public function parent_section() |
|
1282 | - { |
|
1283 | - $this->ensure_construct_finalized_called(); |
|
1284 | - return parent::parent_section(); |
|
1285 | - } |
|
1286 | - |
|
1287 | - |
|
1288 | - |
|
1289 | - /** |
|
1290 | - * make sure construction finalized was called, otherwise children might not be ready |
|
1291 | - * |
|
1292 | - * @return void |
|
1293 | - * @throws \EE_Error |
|
1294 | - */ |
|
1295 | - public function ensure_construct_finalized_called() |
|
1296 | - { |
|
1297 | - if (! $this->_construction_finalized) { |
|
1298 | - $this->_construct_finalize($this->_parent_section, $this->_name); |
|
1299 | - } |
|
1300 | - } |
|
1301 | - |
|
1302 | - |
|
1303 | - |
|
1304 | - /** |
|
1305 | - * Checks if any of this form section's inputs, or any of its children's inputs, |
|
1306 | - * are in teh form data. If any are found, returns true. Else false |
|
1307 | - * |
|
1308 | - * @param array $req_data |
|
1309 | - * @return boolean |
|
1310 | - */ |
|
1311 | - public function form_data_present_in($req_data = null) |
|
1312 | - { |
|
1313 | - if ($req_data === null) { |
|
1314 | - $req_data = $_POST; |
|
1315 | - } |
|
1316 | - foreach ($this->subsections() as $subsection) { |
|
1317 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
1318 | - if ($subsection->form_data_present_in($req_data)) { |
|
1319 | - return true; |
|
1320 | - } |
|
1321 | - } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
1322 | - if ($subsection->form_data_present_in($req_data)) { |
|
1323 | - return true; |
|
1324 | - } |
|
1325 | - } |
|
1326 | - } |
|
1327 | - return false; |
|
1328 | - } |
|
1329 | - |
|
1330 | - |
|
1331 | - |
|
1332 | - /** |
|
1333 | - * Gets validation errors for this form section and subsections |
|
1334 | - * Similar to EE_Form_Section_Validatable::get_validation_errors() except this |
|
1335 | - * gets the validation errors for ALL subsection |
|
1336 | - * |
|
1337 | - * @return EE_Validation_Error[] |
|
1338 | - */ |
|
1339 | - public function get_validation_errors_accumulated() |
|
1340 | - { |
|
1341 | - $validation_errors = $this->get_validation_errors(); |
|
1342 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
1343 | - if ($subsection instanceof EE_Form_Section_Proper) { |
|
1344 | - $validation_errors_on_this_subsection = $subsection->get_validation_errors_accumulated(); |
|
1345 | - } else { |
|
1346 | - $validation_errors_on_this_subsection = $subsection->get_validation_errors(); |
|
1347 | - } |
|
1348 | - if ($validation_errors_on_this_subsection) { |
|
1349 | - $validation_errors = array_merge($validation_errors, $validation_errors_on_this_subsection); |
|
1350 | - } |
|
1351 | - } |
|
1352 | - return $validation_errors; |
|
1353 | - } |
|
1354 | - |
|
1355 | - |
|
1356 | - |
|
1357 | - /** |
|
1358 | - * This isn't just the name of an input, it's a path pointing to an input. The |
|
1359 | - * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
1360 | - * dot-dot-slash (../) means to ascend into the parent section. |
|
1361 | - * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
1362 | - * which will be returned. |
|
1363 | - * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
1364 | - * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
1365 | - * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
1366 | - * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
1367 | - * Etc |
|
1368 | - * |
|
1369 | - * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
1370 | - * @return EE_Form_Section_Base |
|
1371 | - */ |
|
1372 | - public function find_section_from_path($form_section_path) |
|
1373 | - { |
|
1374 | - //check if we can find the input from purely going straight up the tree |
|
1375 | - $input = parent::find_section_from_path($form_section_path); |
|
1376 | - if ($input instanceof EE_Form_Section_Base) { |
|
1377 | - return $input; |
|
1378 | - } |
|
1379 | - $next_slash_pos = strpos($form_section_path, '/'); |
|
1380 | - if ($next_slash_pos !== false) { |
|
1381 | - $child_section_name = substr($form_section_path, 0, $next_slash_pos); |
|
1382 | - $subpath = substr($form_section_path, $next_slash_pos + 1); |
|
1383 | - } else { |
|
1384 | - $child_section_name = $form_section_path; |
|
1385 | - $subpath = ''; |
|
1386 | - } |
|
1387 | - $child_section = $this->get_subsection($child_section_name); |
|
1388 | - if ($child_section instanceof EE_Form_Section_Base) { |
|
1389 | - return $child_section->find_section_from_path($subpath); |
|
1390 | - } else { |
|
1391 | - return null; |
|
1392 | - } |
|
1393 | - } |
|
14 | + const SUBMITTED_FORM_DATA_SSN_KEY = 'submitted_form_data'; |
|
15 | + |
|
16 | + /** |
|
17 | + * Subsections |
|
18 | + * |
|
19 | + * @var EE_Form_Section_Validatable[] |
|
20 | + */ |
|
21 | + protected $_subsections = array(); |
|
22 | + |
|
23 | + /** |
|
24 | + * Strategy for laying out the form |
|
25 | + * |
|
26 | + * @var EE_Form_Section_Layout_Base |
|
27 | + */ |
|
28 | + protected $_layout_strategy; |
|
29 | + |
|
30 | + /** |
|
31 | + * Whether or not this form has received and validated a form submission yet |
|
32 | + * |
|
33 | + * @var boolean |
|
34 | + */ |
|
35 | + protected $_received_submission = false; |
|
36 | + |
|
37 | + /** |
|
38 | + * message displayed to users upon successful form submission |
|
39 | + * |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected $_form_submission_success_message = ''; |
|
43 | + |
|
44 | + /** |
|
45 | + * message displayed to users upon unsuccessful form submission |
|
46 | + * |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + protected $_form_submission_error_message = ''; |
|
50 | + |
|
51 | + /** |
|
52 | + * Stores all the data that will localized for form validation |
|
53 | + * |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + static protected $_js_localization = array(); |
|
57 | + |
|
58 | + /** |
|
59 | + * whether or not the form's localized validation JS vars have been set |
|
60 | + * |
|
61 | + * @type boolean |
|
62 | + */ |
|
63 | + static protected $_scripts_localized = false; |
|
64 | + |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * when constructing a proper form section, calls _construct_finalize on children |
|
69 | + * so that they know who their parent is, and what name they've been given. |
|
70 | + * |
|
71 | + * @param array $options_array { |
|
72 | + * @type $subsections EE_Form_Section_Validatable[] where keys are the section's name |
|
73 | + * @type $include string[] numerically-indexed where values are section names to be included, |
|
74 | + * and in that order. This is handy if you want |
|
75 | + * the subsections to be ordered differently than the default, and if you override |
|
76 | + * which fields are shown |
|
77 | + * @type $exclude string[] values are subsections to be excluded. This is handy if you want |
|
78 | + * to remove certain default subsections (note: if you specify BOTH 'include' AND |
|
79 | + * 'exclude', the inclusions will be applied first, and the exclusions will exclude |
|
80 | + * items from that list of inclusions) |
|
81 | + * @type $layout_strategy EE_Form_Section_Layout_Base strategy for laying out the form |
|
82 | + * } @see EE_Form_Section_Validatable::__construct() |
|
83 | + * @throws \EE_Error |
|
84 | + */ |
|
85 | + public function __construct($options_array = array()) |
|
86 | + { |
|
87 | + $options_array = (array)apply_filters('FHEE__EE_Form_Section_Proper___construct__options_array', $options_array, |
|
88 | + $this); |
|
89 | + //call parent first, as it may be setting the name |
|
90 | + parent::__construct($options_array); |
|
91 | + //if they've included subsections in the constructor, add them now |
|
92 | + if (isset($options_array['include'])) { |
|
93 | + //we are going to make sure we ONLY have those subsections to include |
|
94 | + //AND we are going to make sure they're in that specified order |
|
95 | + $reordered_subsections = array(); |
|
96 | + foreach ($options_array['include'] as $input_name) { |
|
97 | + if (isset($this->_subsections[$input_name])) { |
|
98 | + $reordered_subsections[$input_name] = $this->_subsections[$input_name]; |
|
99 | + } |
|
100 | + } |
|
101 | + $this->_subsections = $reordered_subsections; |
|
102 | + } |
|
103 | + if (isset($options_array['exclude'])) { |
|
104 | + $exclude = $options_array['exclude']; |
|
105 | + $this->_subsections = array_diff_key($this->_subsections, array_flip($exclude)); |
|
106 | + } |
|
107 | + if (isset($options_array['layout_strategy'])) { |
|
108 | + $this->_layout_strategy = $options_array['layout_strategy']; |
|
109 | + } |
|
110 | + if (! $this->_layout_strategy) { |
|
111 | + $this->_layout_strategy = is_admin() ? new EE_Admin_Two_Column_Layout() : new EE_Two_Column_Layout(); |
|
112 | + } |
|
113 | + $this->_layout_strategy->_construct_finalize($this); |
|
114 | + //ok so we are definitely going to want the forms JS, |
|
115 | + //so enqueue it or remember to enqueue it during wp_enqueue_scripts |
|
116 | + if (did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts')) { |
|
117 | + //ok so they've constructed this object after when they should have. |
|
118 | + //just enqueue the generic form scripts and initialize the form immediately in the JS |
|
119 | + \EE_Form_Section_Proper::wp_enqueue_scripts(true); |
|
120 | + } else { |
|
121 | + add_action('wp_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
122 | + add_action('admin_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
123 | + } |
|
124 | + add_action('wp_footer', array($this, 'ensure_scripts_localized'), 1); |
|
125 | + |
|
126 | + /** |
|
127 | + * Gives other plugins a chance to hook in before construct finalize is called. The form probably doesn't |
|
128 | + * yet have a parent form section. Since 4.9.32, when this action was introduced, this is the best place to |
|
129 | + * add a subsection onto a form, assuming you don't care what the form section's name, HTML ID, or HTML name etc are. |
|
130 | + * Also see AHEE__EE_Form_Section_Proper___construct_finalize__end |
|
131 | + * @since 4.9.32 |
|
132 | + * @param EE_Form_Section_Proper $this before __construct is done, but all of its logic, except maybe calling |
|
133 | + * _construct_finalize has been done |
|
134 | + * @param array $options_array options passed into the constructor |
|
135 | + */ |
|
136 | + do_action('AHEE__EE_Form_Input_Base___construct__before_construct_finalize_called', $this, $options_array); |
|
137 | + |
|
138 | + if (isset($options_array['name'])) { |
|
139 | + $this->_construct_finalize(null, $options_array['name']); |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + |
|
145 | + /** |
|
146 | + * Finishes construction given the parent form section and this form section's name |
|
147 | + * |
|
148 | + * @param EE_Form_Section_Proper $parent_form_section |
|
149 | + * @param string $name |
|
150 | + * @throws \EE_Error |
|
151 | + */ |
|
152 | + public function _construct_finalize($parent_form_section, $name) |
|
153 | + { |
|
154 | + parent::_construct_finalize($parent_form_section, $name); |
|
155 | + $this->_set_default_name_if_empty(); |
|
156 | + $this->_set_default_html_id_if_empty(); |
|
157 | + foreach ($this->_subsections as $subsection_name => $subsection) { |
|
158 | + if ($subsection instanceof EE_Form_Section_Base) { |
|
159 | + $subsection->_construct_finalize($this, $subsection_name); |
|
160 | + } else { |
|
161 | + throw new EE_Error( |
|
162 | + sprintf( |
|
163 | + __('Subsection "%s" is not an instanceof EE_Form_Section_Base on form "%s". It is a "%s"', |
|
164 | + 'event_espresso'), |
|
165 | + $subsection_name, |
|
166 | + get_class($this), |
|
167 | + $subsection ? get_class($subsection) : __('NULL', 'event_espresso') |
|
168 | + ) |
|
169 | + ); |
|
170 | + } |
|
171 | + } |
|
172 | + /** |
|
173 | + * Action performed just after form has been given a name (and HTML ID etc) and is fully constructed. |
|
174 | + * If you have code that should modify the form and needs it and its subsections to have a name, HTML ID (or other attributes derived |
|
175 | + * from the name like the HTML label id, etc), this is where it should be done. |
|
176 | + * This might only happen just before displaying the form, or just before it receives form submission data. |
|
177 | + * If you need to modify the form or its subsections before _construct_finalize is called on it (and we've |
|
178 | + * ensured it has a name, HTML IDs, etc |
|
179 | + * @param EE_Form_Section_Proper $this |
|
180 | + * @param EE_Form_Section_Proper|null $parent_form_section |
|
181 | + * @param string $name |
|
182 | + */ |
|
183 | + do_action('AHEE__EE_Form_Section_Proper___construct_finalize__end', $this, $parent_form_section, $name); |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + |
|
188 | + /** |
|
189 | + * Gets the layout strategy for this form section |
|
190 | + * |
|
191 | + * @return EE_Form_Section_Layout_Base |
|
192 | + */ |
|
193 | + public function get_layout_strategy() |
|
194 | + { |
|
195 | + return $this->_layout_strategy; |
|
196 | + } |
|
197 | + |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * Gets the HTML for a single input for this form section according |
|
202 | + * to the layout strategy |
|
203 | + * |
|
204 | + * @param EE_Form_Input_Base $input |
|
205 | + * @return string |
|
206 | + */ |
|
207 | + public function get_html_for_input($input) |
|
208 | + { |
|
209 | + return $this->_layout_strategy->layout_input($input); |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * was_submitted - checks if form inputs are present in request data |
|
216 | + * Basically an alias for form_data_present_in() (which is used by both |
|
217 | + * proper form sections and form inputs) |
|
218 | + * |
|
219 | + * @param null $form_data |
|
220 | + * @return boolean |
|
221 | + */ |
|
222 | + public function was_submitted($form_data = null) |
|
223 | + { |
|
224 | + return $this->form_data_present_in($form_data); |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + |
|
229 | + /** |
|
230 | + * After the form section is initially created, call this to sanitize the data in the submission |
|
231 | + * which relates to this form section, validate it, and set it as properties on the form. |
|
232 | + * |
|
233 | + * @param array|null $req_data should usually be $_POST (the default). |
|
234 | + * However, you CAN supply a different array. |
|
235 | + * Consider using set_defaults() instead however. |
|
236 | + * (If you rendered the form in the page using echo $form_x->get_html() |
|
237 | + * the inputs will have the correct name in the request data for this function |
|
238 | + * to find them and populate the form with them. |
|
239 | + * If you have a flat form (with only input subsections), |
|
240 | + * you can supply a flat array where keys |
|
241 | + * are the form input names and values are their values) |
|
242 | + * @param boolean $validate whether or not to perform validation on this data. Default is, |
|
243 | + * of course, to validate that data, and set errors on the invalid values. |
|
244 | + * But if the data has already been validated |
|
245 | + * (eg you validated the data then stored it in the DB) |
|
246 | + * you may want to skip this step. |
|
247 | + */ |
|
248 | + public function receive_form_submission($req_data = null, $validate = true) |
|
249 | + { |
|
250 | + $req_data = apply_filters('FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', $req_data, $this, |
|
251 | + $validate); |
|
252 | + if ($req_data === null) { |
|
253 | + $req_data = array_merge($_GET, $_POST); |
|
254 | + } |
|
255 | + $req_data = apply_filters('FHEE__EE_Form_Section_Proper__receive_form_submission__request_data', $req_data, |
|
256 | + $this); |
|
257 | + $this->_normalize($req_data); |
|
258 | + if ($validate) { |
|
259 | + $this->_validate(); |
|
260 | + //if it's invalid, we're going to want to re-display so remember what they submitted |
|
261 | + if (! $this->is_valid()) { |
|
262 | + $this->store_submitted_form_data_in_session(); |
|
263 | + } |
|
264 | + } |
|
265 | + do_action('AHEE__EE_Form_Section_Proper__receive_form_submission__end', $req_data, $this, $validate); |
|
266 | + } |
|
267 | + |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * caches the originally submitted input values in the session |
|
272 | + * so that they can be used to repopulate the form if it failed validation |
|
273 | + * |
|
274 | + * @return boolean whether or not the data was successfully stored in the session |
|
275 | + */ |
|
276 | + protected function store_submitted_form_data_in_session() |
|
277 | + { |
|
278 | + return EE_Registry::instance()->SSN->set_session_data( |
|
279 | + array( |
|
280 | + \EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY => $this->submitted_values(true), |
|
281 | + ) |
|
282 | + ); |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + |
|
287 | + /** |
|
288 | + * retrieves the originally submitted input values in the session |
|
289 | + * so that they can be used to repopulate the form if it failed validation |
|
290 | + * |
|
291 | + * @return array |
|
292 | + */ |
|
293 | + protected function get_submitted_form_data_from_session() |
|
294 | + { |
|
295 | + $session = EE_Registry::instance()->SSN; |
|
296 | + if ($session instanceof EE_Session) { |
|
297 | + return $session->get_session_data( |
|
298 | + \EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY |
|
299 | + ); |
|
300 | + } else { |
|
301 | + return array(); |
|
302 | + } |
|
303 | + } |
|
304 | + |
|
305 | + |
|
306 | + |
|
307 | + /** |
|
308 | + * flushed the originally submitted input values from the session |
|
309 | + * |
|
310 | + * @return boolean whether or not the data was successfully removed from the session |
|
311 | + */ |
|
312 | + protected function flush_submitted_form_data_from_session() |
|
313 | + { |
|
314 | + return EE_Registry::instance()->SSN->reset_data( |
|
315 | + array(\EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY) |
|
316 | + ); |
|
317 | + } |
|
318 | + |
|
319 | + |
|
320 | + |
|
321 | + /** |
|
322 | + * Populates this form and its subsections with data from the session. |
|
323 | + * (Wrapper for EE_Form_Section_Proper::receive_form_submission, so it shows |
|
324 | + * validation errors when displaying too) |
|
325 | + * Returns true if the form was populated from the session, false otherwise |
|
326 | + * |
|
327 | + * @return boolean |
|
328 | + */ |
|
329 | + public function populate_from_session() |
|
330 | + { |
|
331 | + $form_data_in_session = $this->get_submitted_form_data_from_session(); |
|
332 | + if (empty($form_data_in_session)) { |
|
333 | + return false; |
|
334 | + } |
|
335 | + $this->receive_form_submission($form_data_in_session); |
|
336 | + $this->flush_submitted_form_data_from_session(); |
|
337 | + if ($this->form_data_present_in($form_data_in_session)) { |
|
338 | + return true; |
|
339 | + } else { |
|
340 | + return false; |
|
341 | + } |
|
342 | + } |
|
343 | + |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * Populates the default data for the form, given an array where keys are |
|
348 | + * the input names, and values are their values (preferably normalized to be their |
|
349 | + * proper PHP types, not all strings... although that should be ok too). |
|
350 | + * Proper subsections are sub-arrays, the key being the subsection's name, and |
|
351 | + * the value being an array formatted in teh same way |
|
352 | + * |
|
353 | + * @param array $default_data |
|
354 | + */ |
|
355 | + public function populate_defaults($default_data) |
|
356 | + { |
|
357 | + foreach ($this->subsections(false) as $subsection_name => $subsection) { |
|
358 | + if (isset($default_data[$subsection_name])) { |
|
359 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
360 | + $subsection->set_default($default_data[$subsection_name]); |
|
361 | + } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
362 | + $subsection->populate_defaults($default_data[$subsection_name]); |
|
363 | + } |
|
364 | + } |
|
365 | + } |
|
366 | + } |
|
367 | + |
|
368 | + |
|
369 | + |
|
370 | + /** |
|
371 | + * returns true if subsection exists |
|
372 | + * |
|
373 | + * @param string $name |
|
374 | + * @return boolean |
|
375 | + */ |
|
376 | + public function subsection_exists($name) |
|
377 | + { |
|
378 | + return isset($this->_subsections[$name]) ? true : false; |
|
379 | + } |
|
380 | + |
|
381 | + |
|
382 | + |
|
383 | + /** |
|
384 | + * Gets the subsection specified by its name |
|
385 | + * |
|
386 | + * @param string $name |
|
387 | + * @param boolean $require_construction_to_be_finalized most client code should leave this as TRUE |
|
388 | + * so that the inputs will be properly configured. |
|
389 | + * However, some client code may be ok |
|
390 | + * with construction finalize being called later |
|
391 | + * (realizing that the subsections' html names |
|
392 | + * might not be set yet, etc.) |
|
393 | + * @return EE_Form_Section_Base |
|
394 | + * @throws \EE_Error |
|
395 | + */ |
|
396 | + public function get_subsection($name, $require_construction_to_be_finalized = true) |
|
397 | + { |
|
398 | + if ($require_construction_to_be_finalized) { |
|
399 | + $this->ensure_construct_finalized_called(); |
|
400 | + } |
|
401 | + return $this->subsection_exists($name) ? $this->_subsections[$name] : null; |
|
402 | + } |
|
403 | + |
|
404 | + |
|
405 | + |
|
406 | + /** |
|
407 | + * Gets all the validatable subsections of this form section |
|
408 | + * |
|
409 | + * @return EE_Form_Section_Validatable[] |
|
410 | + */ |
|
411 | + public function get_validatable_subsections() |
|
412 | + { |
|
413 | + $validatable_subsections = array(); |
|
414 | + foreach ($this->subsections() as $name => $obj) { |
|
415 | + if ($obj instanceof EE_Form_Section_Validatable) { |
|
416 | + $validatable_subsections[$name] = $obj; |
|
417 | + } |
|
418 | + } |
|
419 | + return $validatable_subsections; |
|
420 | + } |
|
421 | + |
|
422 | + |
|
423 | + |
|
424 | + /** |
|
425 | + * Gets an input by the given name. If not found, or if its not an EE_FOrm_Input_Base child, |
|
426 | + * throw an EE_Error. |
|
427 | + * |
|
428 | + * @param string $name |
|
429 | + * @param boolean $require_construction_to_be_finalized most client code should |
|
430 | + * leave this as TRUE so that the inputs will be properly |
|
431 | + * configured. However, some client code may be ok with |
|
432 | + * construction finalize being called later |
|
433 | + * (realizing that the subsections' html names might not be |
|
434 | + * set yet, etc.) |
|
435 | + * @return EE_Form_Input_Base |
|
436 | + * @throws EE_Error |
|
437 | + */ |
|
438 | + public function get_input($name, $require_construction_to_be_finalized = true) |
|
439 | + { |
|
440 | + $subsection = $this->get_subsection($name, $require_construction_to_be_finalized); |
|
441 | + if (! $subsection instanceof EE_Form_Input_Base) { |
|
442 | + throw new EE_Error( |
|
443 | + sprintf( |
|
444 | + __( |
|
445 | + "Subsection '%s' is not an instanceof EE_Form_Input_Base on form '%s'. It is a '%s'", |
|
446 | + 'event_espresso' |
|
447 | + ), |
|
448 | + $name, |
|
449 | + get_class($this), |
|
450 | + $subsection ? get_class($subsection) : __("NULL", 'event_espresso') |
|
451 | + ) |
|
452 | + ); |
|
453 | + } |
|
454 | + return $subsection; |
|
455 | + } |
|
456 | + |
|
457 | + |
|
458 | + |
|
459 | + /** |
|
460 | + * Like get_input(), gets the proper subsection of the form given the name, |
|
461 | + * otherwise throws an EE_Error |
|
462 | + * |
|
463 | + * @param string $name |
|
464 | + * @param boolean $require_construction_to_be_finalized most client code should |
|
465 | + * leave this as TRUE so that the inputs will be properly |
|
466 | + * configured. However, some client code may be ok with |
|
467 | + * construction finalize being called later |
|
468 | + * (realizing that the subsections' html names might not be |
|
469 | + * set yet, etc.) |
|
470 | + * @return EE_Form_Section_Proper |
|
471 | + * @throws EE_Error |
|
472 | + */ |
|
473 | + public function get_proper_subsection($name, $require_construction_to_be_finalized = true) |
|
474 | + { |
|
475 | + $subsection = $this->get_subsection($name, $require_construction_to_be_finalized); |
|
476 | + if (! $subsection instanceof EE_Form_Section_Proper) { |
|
477 | + throw new EE_Error( |
|
478 | + sprintf( |
|
479 | + __("Subsection '%'s is not an instanceof EE_Form_Section_Proper on form '%s'", 'event_espresso'), |
|
480 | + $name, |
|
481 | + get_class($this) |
|
482 | + ) |
|
483 | + ); |
|
484 | + } |
|
485 | + return $subsection; |
|
486 | + } |
|
487 | + |
|
488 | + |
|
489 | + |
|
490 | + /** |
|
491 | + * Gets the value of the specified input. Should be called after receive_form_submission() |
|
492 | + * or populate_defaults() on the form, where the normalized value on the input is set. |
|
493 | + * |
|
494 | + * @param string $name |
|
495 | + * @return mixed depending on the input's type and its normalization strategy |
|
496 | + * @throws \EE_Error |
|
497 | + */ |
|
498 | + public function get_input_value($name) |
|
499 | + { |
|
500 | + $input = $this->get_input($name); |
|
501 | + return $input->normalized_value(); |
|
502 | + } |
|
503 | + |
|
504 | + |
|
505 | + |
|
506 | + /** |
|
507 | + * Checks if this form section itself is valid, and then checks its subsections |
|
508 | + * |
|
509 | + * @throws EE_Error |
|
510 | + * @return boolean |
|
511 | + */ |
|
512 | + public function is_valid() |
|
513 | + { |
|
514 | + if (! $this->has_received_submission()) { |
|
515 | + throw new EE_Error( |
|
516 | + sprintf( |
|
517 | + __( |
|
518 | + "You cannot check if a form is valid before receiving the form submission using receive_form_submission", |
|
519 | + "event_espresso" |
|
520 | + ) |
|
521 | + ) |
|
522 | + ); |
|
523 | + } |
|
524 | + if (! parent::is_valid()) { |
|
525 | + return false; |
|
526 | + } |
|
527 | + // ok so no general errors to this entire form section. |
|
528 | + // so let's check the subsections, but only set errors if that hasn't been done yet |
|
529 | + $set_submission_errors = $this->submission_error_message() === '' ? true : false; |
|
530 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
531 | + if (! $subsection->is_valid() || $subsection->get_validation_error_string() !== '') { |
|
532 | + if ($set_submission_errors) { |
|
533 | + $this->set_submission_error_message($subsection->get_validation_error_string()); |
|
534 | + } |
|
535 | + return false; |
|
536 | + } |
|
537 | + } |
|
538 | + return true; |
|
539 | + } |
|
540 | + |
|
541 | + |
|
542 | + |
|
543 | + /** |
|
544 | + * gets teh default name of this form section if none is specified |
|
545 | + * |
|
546 | + * @return string |
|
547 | + */ |
|
548 | + protected function _set_default_name_if_empty() |
|
549 | + { |
|
550 | + if (! $this->_name) { |
|
551 | + $classname = get_class($this); |
|
552 | + $default_name = str_replace("EE_", "", $classname); |
|
553 | + $this->_name = $default_name; |
|
554 | + } |
|
555 | + } |
|
556 | + |
|
557 | + |
|
558 | + |
|
559 | + /** |
|
560 | + * Returns the HTML for the form, except for the form opening and closing tags |
|
561 | + * (as the form section doesn't know where you necessarily want to send the information to), |
|
562 | + * and except for a submit button. Enqueus JS and CSS; if called early enough we will |
|
563 | + * try to enqueue them in the header, otherwise they'll be enqueued in the footer. |
|
564 | + * Not doing_it_wrong because theoretically this CAN be used properly, |
|
565 | + * provided its used during "wp_enqueue_scripts", or it doesn't need to enqueue |
|
566 | + * any CSS. |
|
567 | + * |
|
568 | + * @throws \EE_Error |
|
569 | + */ |
|
570 | + public function get_html_and_js() |
|
571 | + { |
|
572 | + $this->enqueue_js(); |
|
573 | + return $this->get_html(); |
|
574 | + } |
|
575 | + |
|
576 | + |
|
577 | + |
|
578 | + /** |
|
579 | + * returns HTML for displaying this form section. recursively calls display_section() on all subsections |
|
580 | + * |
|
581 | + * @param bool $display_previously_submitted_data |
|
582 | + * @return string |
|
583 | + */ |
|
584 | + public function get_html($display_previously_submitted_data = true) |
|
585 | + { |
|
586 | + $this->ensure_construct_finalized_called(); |
|
587 | + if ($display_previously_submitted_data) { |
|
588 | + $this->populate_from_session(); |
|
589 | + } |
|
590 | + return $this->_form_html_filter |
|
591 | + ? $this->_form_html_filter->filterHtml($this->_layout_strategy->layout_form(), $this) |
|
592 | + : $this->_layout_strategy->layout_form(); |
|
593 | + } |
|
594 | + |
|
595 | + |
|
596 | + |
|
597 | + /** |
|
598 | + * enqueues JS and CSS for the form. |
|
599 | + * It is preferred to call this before wp_enqueue_scripts so the |
|
600 | + * scripts and styles can be put in the header, but if called later |
|
601 | + * they will be put in the footer (which is OK for JS, but in HTML4 CSS should |
|
602 | + * only be in the header; but in HTML5 its ok in the body. |
|
603 | + * See http://stackoverflow.com/questions/4957446/load-external-css-file-in-body-tag. |
|
604 | + * So if your form enqueues CSS, it's preferred to call this before wp_enqueue_scripts.) |
|
605 | + * |
|
606 | + * @return string |
|
607 | + * @throws \EE_Error |
|
608 | + */ |
|
609 | + public function enqueue_js() |
|
610 | + { |
|
611 | + $this->_enqueue_and_localize_form_js(); |
|
612 | + foreach ($this->subsections() as $subsection) { |
|
613 | + $subsection->enqueue_js(); |
|
614 | + } |
|
615 | + } |
|
616 | + |
|
617 | + |
|
618 | + |
|
619 | + /** |
|
620 | + * adds a filter so that jquery validate gets enqueued in EE_System::wp_enqueue_scripts(). |
|
621 | + * This must be done BEFORE wp_enqueue_scripts() gets called, which is on |
|
622 | + * the wp_enqueue_scripts hook. |
|
623 | + * However, registering the form js and localizing it can happen when we |
|
624 | + * actually output the form (which is preferred, seeing how teh form's fields |
|
625 | + * could change until it's actually outputted) |
|
626 | + * |
|
627 | + * @param boolean $init_form_validation_automatically whether or not we want the form validation |
|
628 | + * to be triggered automatically or not |
|
629 | + * @return void |
|
630 | + */ |
|
631 | + public static function wp_enqueue_scripts($init_form_validation_automatically = true) |
|
632 | + { |
|
633 | + add_filter('FHEE_load_jquery_validate', '__return_true'); |
|
634 | + wp_register_script( |
|
635 | + 'ee_form_section_validation', |
|
636 | + EE_GLOBAL_ASSETS_URL . 'scripts' . DS . 'form_section_validation.js', |
|
637 | + array('jquery-validate', 'jquery-ui-datepicker', 'jquery-validate-extra-methods'), |
|
638 | + EVENT_ESPRESSO_VERSION, |
|
639 | + true |
|
640 | + ); |
|
641 | + wp_localize_script( |
|
642 | + 'ee_form_section_validation', |
|
643 | + 'ee_form_section_validation_init', |
|
644 | + array('init' => $init_form_validation_automatically ? true : false) |
|
645 | + ); |
|
646 | + } |
|
647 | + |
|
648 | + |
|
649 | + |
|
650 | + /** |
|
651 | + * gets the variables used by form_section_validation.js. |
|
652 | + * This needs to be called AFTER we've called $this->_enqueue_jquery_validate_script, |
|
653 | + * but before the wordpress hook wp_loaded |
|
654 | + * |
|
655 | + * @throws \EE_Error |
|
656 | + */ |
|
657 | + public function _enqueue_and_localize_form_js() |
|
658 | + { |
|
659 | + $this->ensure_construct_finalized_called(); |
|
660 | + //actually, we don't want to localize just yet. There may be other forms on the page. |
|
661 | + //so we need to add our form section data to a static variable accessible by all form sections |
|
662 | + //and localize it just before the footer |
|
663 | + $this->localize_validation_rules(); |
|
664 | + add_action('wp_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'), 2); |
|
665 | + add_action('admin_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms')); |
|
666 | + } |
|
667 | + |
|
668 | + |
|
669 | + |
|
670 | + /** |
|
671 | + * add our form section data to a static variable accessible by all form sections |
|
672 | + * |
|
673 | + * @param bool $return_for_subsection |
|
674 | + * @return void |
|
675 | + * @throws \EE_Error |
|
676 | + */ |
|
677 | + public function localize_validation_rules($return_for_subsection = false) |
|
678 | + { |
|
679 | + // we only want to localize vars ONCE for the entire form, |
|
680 | + // so if the form section doesn't have a parent, then it must be the top dog |
|
681 | + if ($return_for_subsection || ! $this->parent_section()) { |
|
682 | + EE_Form_Section_Proper::$_js_localization['form_data'][$this->html_id()] = array( |
|
683 | + 'form_section_id' => $this->html_id(true), |
|
684 | + 'validation_rules' => $this->get_jquery_validation_rules(), |
|
685 | + 'other_data' => $this->get_other_js_data(), |
|
686 | + 'errors' => $this->subsection_validation_errors_by_html_name(), |
|
687 | + ); |
|
688 | + EE_Form_Section_Proper::$_scripts_localized = true; |
|
689 | + } |
|
690 | + } |
|
691 | + |
|
692 | + |
|
693 | + |
|
694 | + /** |
|
695 | + * Gets an array of extra data that will be useful for client-side javascript. |
|
696 | + * This is primarily data added by inputs and forms in addition to any |
|
697 | + * scripts they might enqueue |
|
698 | + * |
|
699 | + * @param array $form_other_js_data |
|
700 | + * @return array |
|
701 | + */ |
|
702 | + public function get_other_js_data($form_other_js_data = array()) |
|
703 | + { |
|
704 | + foreach ($this->subsections() as $subsection) { |
|
705 | + $form_other_js_data = $subsection->get_other_js_data($form_other_js_data); |
|
706 | + } |
|
707 | + return $form_other_js_data; |
|
708 | + } |
|
709 | + |
|
710 | + |
|
711 | + |
|
712 | + /** |
|
713 | + * Gets a flat array of inputs for this form section and its subsections. |
|
714 | + * Keys are their form names, and values are the inputs themselves |
|
715 | + * |
|
716 | + * @return EE_Form_Input_Base |
|
717 | + */ |
|
718 | + public function inputs_in_subsections() |
|
719 | + { |
|
720 | + $inputs = array(); |
|
721 | + foreach ($this->subsections() as $subsection) { |
|
722 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
723 | + $inputs[$subsection->html_name()] = $subsection; |
|
724 | + } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
725 | + $inputs += $subsection->inputs_in_subsections(); |
|
726 | + } |
|
727 | + } |
|
728 | + return $inputs; |
|
729 | + } |
|
730 | + |
|
731 | + |
|
732 | + |
|
733 | + /** |
|
734 | + * Gets a flat array of all the validation errors. |
|
735 | + * Keys are html names (because those should be unique) |
|
736 | + * and values are a string of all their validation errors |
|
737 | + * |
|
738 | + * @return string[] |
|
739 | + */ |
|
740 | + public function subsection_validation_errors_by_html_name() |
|
741 | + { |
|
742 | + $inputs = $this->inputs(); |
|
743 | + $errors = array(); |
|
744 | + foreach ($inputs as $form_input) { |
|
745 | + if ($form_input instanceof EE_Form_Input_Base && $form_input->get_validation_errors()) { |
|
746 | + $errors[$form_input->html_name()] = $form_input->get_validation_error_string(); |
|
747 | + } |
|
748 | + } |
|
749 | + return $errors; |
|
750 | + } |
|
751 | + |
|
752 | + |
|
753 | + |
|
754 | + /** |
|
755 | + * passes all the form data required by the JS to the JS, and enqueues the few required JS files. |
|
756 | + * Should be setup by each form during the _enqueues_and_localize_form_js |
|
757 | + */ |
|
758 | + public static function localize_script_for_all_forms() |
|
759 | + { |
|
760 | + //allow inputs and stuff to hook in their JS and stuff here |
|
761 | + do_action('AHEE__EE_Form_Section_Proper__localize_script_for_all_forms__begin'); |
|
762 | + EE_Form_Section_Proper::$_js_localization['localized_error_messages'] = EE_Form_Section_Proper::_get_localized_error_messages(); |
|
763 | + $email_validation_level = isset(EE_Registry::instance()->CFG->registration->email_validation_level) |
|
764 | + ? EE_Registry::instance()->CFG->registration->email_validation_level |
|
765 | + : 'wp_default'; |
|
766 | + EE_Form_Section_Proper::$_js_localization['email_validation_level'] = $email_validation_level; |
|
767 | + wp_enqueue_script('ee_form_section_validation'); |
|
768 | + wp_localize_script( |
|
769 | + 'ee_form_section_validation', |
|
770 | + 'ee_form_section_vars', |
|
771 | + EE_Form_Section_Proper::$_js_localization |
|
772 | + ); |
|
773 | + } |
|
774 | + |
|
775 | + |
|
776 | + |
|
777 | + /** |
|
778 | + * ensure_scripts_localized |
|
779 | + */ |
|
780 | + public function ensure_scripts_localized() |
|
781 | + { |
|
782 | + if (! EE_Form_Section_Proper::$_scripts_localized) { |
|
783 | + $this->_enqueue_and_localize_form_js(); |
|
784 | + } |
|
785 | + } |
|
786 | + |
|
787 | + |
|
788 | + |
|
789 | + /** |
|
790 | + * Gets the hard-coded validation error messages to be used in the JS. The convention |
|
791 | + * is that the key here should be the same as the custom validation rule put in the JS file |
|
792 | + * |
|
793 | + * @return array keys are custom validation rules, and values are internationalized strings |
|
794 | + */ |
|
795 | + private static function _get_localized_error_messages() |
|
796 | + { |
|
797 | + return array( |
|
798 | + 'validUrl' => __("This is not a valid absolute URL. Eg, http://domain.com/monkey.jpg", "event_espresso"), |
|
799 | + 'regex' => __('Please check your input', 'event_espresso'), |
|
800 | + ); |
|
801 | + } |
|
802 | + |
|
803 | + |
|
804 | + |
|
805 | + /** |
|
806 | + * @return array |
|
807 | + */ |
|
808 | + public static function js_localization() |
|
809 | + { |
|
810 | + return self::$_js_localization; |
|
811 | + } |
|
812 | + |
|
813 | + |
|
814 | + |
|
815 | + /** |
|
816 | + * @return array |
|
817 | + */ |
|
818 | + public static function reset_js_localization() |
|
819 | + { |
|
820 | + self::$_js_localization = array(); |
|
821 | + } |
|
822 | + |
|
823 | + |
|
824 | + |
|
825 | + /** |
|
826 | + * Gets the JS to put inside the jquery validation rules for subsection of this form section. |
|
827 | + * See parent function for more... |
|
828 | + * |
|
829 | + * @return array |
|
830 | + */ |
|
831 | + public function get_jquery_validation_rules() |
|
832 | + { |
|
833 | + $jquery_validation_rules = array(); |
|
834 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
835 | + $jquery_validation_rules = array_merge( |
|
836 | + $jquery_validation_rules, |
|
837 | + $subsection->get_jquery_validation_rules() |
|
838 | + ); |
|
839 | + } |
|
840 | + return $jquery_validation_rules; |
|
841 | + } |
|
842 | + |
|
843 | + |
|
844 | + |
|
845 | + /** |
|
846 | + * Sanitizes all the data and sets the sanitized value of each field |
|
847 | + * |
|
848 | + * @param array $req_data like $_POST |
|
849 | + * @return void |
|
850 | + */ |
|
851 | + protected function _normalize($req_data) |
|
852 | + { |
|
853 | + $this->_received_submission = true; |
|
854 | + $this->_validation_errors = array(); |
|
855 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
856 | + try { |
|
857 | + $subsection->_normalize($req_data); |
|
858 | + } catch (EE_Validation_Error $e) { |
|
859 | + $subsection->add_validation_error($e); |
|
860 | + } |
|
861 | + } |
|
862 | + } |
|
863 | + |
|
864 | + |
|
865 | + |
|
866 | + /** |
|
867 | + * Performs validation on this form section and its subsections. |
|
868 | + * For each subsection, |
|
869 | + * calls _validate_{subsection_name} on THIS form (if the function exists) |
|
870 | + * and passes it the subsection, then calls _validate on that subsection. |
|
871 | + * If you need to perform validation on the form as a whole (considering multiple) |
|
872 | + * you would be best to override this _validate method, |
|
873 | + * calling parent::_validate() first. |
|
874 | + */ |
|
875 | + protected function _validate() |
|
876 | + { |
|
877 | + foreach ($this->get_validatable_subsections() as $subsection_name => $subsection) { |
|
878 | + if (method_exists($this, '_validate_' . $subsection_name)) { |
|
879 | + call_user_func_array(array($this, '_validate_' . $subsection_name), array($subsection)); |
|
880 | + } |
|
881 | + $subsection->_validate(); |
|
882 | + } |
|
883 | + } |
|
884 | + |
|
885 | + |
|
886 | + |
|
887 | + /** |
|
888 | + * Gets all the validated inputs for the form section |
|
889 | + * |
|
890 | + * @return array |
|
891 | + */ |
|
892 | + public function valid_data() |
|
893 | + { |
|
894 | + $inputs = array(); |
|
895 | + foreach ($this->subsections() as $subsection_name => $subsection) { |
|
896 | + if ($subsection instanceof EE_Form_Section_Proper) { |
|
897 | + $inputs[$subsection_name] = $subsection->valid_data(); |
|
898 | + } else if ($subsection instanceof EE_Form_Input_Base) { |
|
899 | + $inputs[$subsection_name] = $subsection->normalized_value(); |
|
900 | + } |
|
901 | + } |
|
902 | + return $inputs; |
|
903 | + } |
|
904 | + |
|
905 | + |
|
906 | + |
|
907 | + /** |
|
908 | + * Gets all the inputs on this form section |
|
909 | + * |
|
910 | + * @return EE_Form_Input_Base[] |
|
911 | + */ |
|
912 | + public function inputs() |
|
913 | + { |
|
914 | + $inputs = array(); |
|
915 | + foreach ($this->subsections() as $subsection_name => $subsection) { |
|
916 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
917 | + $inputs[$subsection_name] = $subsection; |
|
918 | + } |
|
919 | + } |
|
920 | + return $inputs; |
|
921 | + } |
|
922 | + |
|
923 | + |
|
924 | + |
|
925 | + /** |
|
926 | + * Gets all the subsections which are a proper form |
|
927 | + * |
|
928 | + * @return EE_Form_Section_Proper[] |
|
929 | + */ |
|
930 | + public function subforms() |
|
931 | + { |
|
932 | + $form_sections = array(); |
|
933 | + foreach ($this->subsections() as $name => $obj) { |
|
934 | + if ($obj instanceof EE_Form_Section_Proper) { |
|
935 | + $form_sections[$name] = $obj; |
|
936 | + } |
|
937 | + } |
|
938 | + return $form_sections; |
|
939 | + } |
|
940 | + |
|
941 | + |
|
942 | + |
|
943 | + /** |
|
944 | + * Gets all the subsections (inputs, proper subsections, or html-only sections). |
|
945 | + * Consider using inputs() or subforms() |
|
946 | + * if you only want form inputs or proper form sections. |
|
947 | + * |
|
948 | + * @param boolean $require_construction_to_be_finalized most client code should |
|
949 | + * leave this as TRUE so that the inputs will be properly |
|
950 | + * configured. However, some client code may be ok with |
|
951 | + * construction finalize being called later |
|
952 | + * (realizing that the subsections' html names might not be |
|
953 | + * set yet, etc.) |
|
954 | + * @return EE_Form_Section_Proper[] |
|
955 | + */ |
|
956 | + public function subsections($require_construction_to_be_finalized = true) |
|
957 | + { |
|
958 | + if ($require_construction_to_be_finalized) { |
|
959 | + $this->ensure_construct_finalized_called(); |
|
960 | + } |
|
961 | + return $this->_subsections; |
|
962 | + } |
|
963 | + |
|
964 | + |
|
965 | + |
|
966 | + /** |
|
967 | + * Returns a simple array where keys are input names, and values are their normalized |
|
968 | + * values. (Similar to calling get_input_value on inputs) |
|
969 | + * |
|
970 | + * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
971 | + * or just this forms' direct children inputs |
|
972 | + * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
973 | + * or allow multidimensional array |
|
974 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
975 | + * with array keys being input names |
|
976 | + * (regardless of whether they are from a subsection or not), |
|
977 | + * and if $flatten is FALSE it can be a multidimensional array |
|
978 | + * where keys are always subsection names and values are either |
|
979 | + * the input's normalized value, or an array like the top-level array |
|
980 | + */ |
|
981 | + public function input_values($include_subform_inputs = false, $flatten = false) |
|
982 | + { |
|
983 | + return $this->_input_values(false, $include_subform_inputs, $flatten); |
|
984 | + } |
|
985 | + |
|
986 | + |
|
987 | + |
|
988 | + /** |
|
989 | + * Similar to EE_Form_Section_Proper::input_values(), except this returns the 'display_value' |
|
990 | + * of each input. On some inputs (especially radio boxes or checkboxes), the value stored |
|
991 | + * is not necessarily the value we want to display to users. This creates an array |
|
992 | + * where keys are the input names, and values are their display values |
|
993 | + * |
|
994 | + * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
995 | + * or just this forms' direct children inputs |
|
996 | + * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
997 | + * or allow multidimensional array |
|
998 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
999 | + * with array keys being input names |
|
1000 | + * (regardless of whether they are from a subsection or not), |
|
1001 | + * and if $flatten is FALSE it can be a multidimensional array |
|
1002 | + * where keys are always subsection names and values are either |
|
1003 | + * the input's normalized value, or an array like the top-level array |
|
1004 | + */ |
|
1005 | + public function input_pretty_values($include_subform_inputs = false, $flatten = false) |
|
1006 | + { |
|
1007 | + return $this->_input_values(true, $include_subform_inputs, $flatten); |
|
1008 | + } |
|
1009 | + |
|
1010 | + |
|
1011 | + |
|
1012 | + /** |
|
1013 | + * Gets the input values from the form |
|
1014 | + * |
|
1015 | + * @param boolean $pretty Whether to retrieve the pretty value, |
|
1016 | + * or just the normalized value |
|
1017 | + * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
1018 | + * or just this forms' direct children inputs |
|
1019 | + * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
1020 | + * or allow multidimensional array |
|
1021 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array with array keys being |
|
1022 | + * input names (regardless of whether they are from a subsection or not), |
|
1023 | + * and if $flatten is FALSE it can be a multidimensional array |
|
1024 | + * where keys are always subsection names and values are either |
|
1025 | + * the input's normalized value, or an array like the top-level array |
|
1026 | + */ |
|
1027 | + public function _input_values($pretty = false, $include_subform_inputs = false, $flatten = false) |
|
1028 | + { |
|
1029 | + $input_values = array(); |
|
1030 | + foreach ($this->subsections() as $subsection_name => $subsection) { |
|
1031 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
1032 | + $input_values[$subsection_name] = $pretty |
|
1033 | + ? $subsection->pretty_value() |
|
1034 | + : $subsection->normalized_value(); |
|
1035 | + } else if ($subsection instanceof EE_Form_Section_Proper && $include_subform_inputs) { |
|
1036 | + $subform_input_values = $subsection->_input_values($pretty, $include_subform_inputs, $flatten); |
|
1037 | + if ($flatten) { |
|
1038 | + $input_values = array_merge($input_values, $subform_input_values); |
|
1039 | + } else { |
|
1040 | + $input_values[$subsection_name] = $subform_input_values; |
|
1041 | + } |
|
1042 | + } |
|
1043 | + } |
|
1044 | + return $input_values; |
|
1045 | + } |
|
1046 | + |
|
1047 | + |
|
1048 | + |
|
1049 | + /** |
|
1050 | + * Gets the originally submitted input values from the form |
|
1051 | + * |
|
1052 | + * @param boolean $include_subforms Whether to include inputs from subforms, |
|
1053 | + * or just this forms' direct children inputs |
|
1054 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
1055 | + * with array keys being input names |
|
1056 | + * (regardless of whether they are from a subsection or not), |
|
1057 | + * and if $flatten is FALSE it can be a multidimensional array |
|
1058 | + * where keys are always subsection names and values are either |
|
1059 | + * the input's normalized value, or an array like the top-level array |
|
1060 | + */ |
|
1061 | + public function submitted_values($include_subforms = false) |
|
1062 | + { |
|
1063 | + $submitted_values = array(); |
|
1064 | + foreach ($this->subsections() as $subsection) { |
|
1065 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
1066 | + // is this input part of an array of inputs? |
|
1067 | + if (strpos($subsection->html_name(), '[') !== false) { |
|
1068 | + $full_input_name = \EEH_Array::convert_array_values_to_keys( |
|
1069 | + explode('[', str_replace(']', '', $subsection->html_name())), |
|
1070 | + $subsection->raw_value() |
|
1071 | + ); |
|
1072 | + $submitted_values = array_replace_recursive($submitted_values, $full_input_name); |
|
1073 | + } else { |
|
1074 | + $submitted_values[$subsection->html_name()] = $subsection->raw_value(); |
|
1075 | + } |
|
1076 | + } else if ($subsection instanceof EE_Form_Section_Proper && $include_subforms) { |
|
1077 | + $subform_input_values = $subsection->submitted_values($include_subforms); |
|
1078 | + $submitted_values = array_replace_recursive($submitted_values, $subform_input_values); |
|
1079 | + } |
|
1080 | + } |
|
1081 | + return $submitted_values; |
|
1082 | + } |
|
1083 | + |
|
1084 | + |
|
1085 | + |
|
1086 | + /** |
|
1087 | + * Indicates whether or not this form has received a submission yet |
|
1088 | + * (ie, had receive_form_submission called on it yet) |
|
1089 | + * |
|
1090 | + * @return boolean |
|
1091 | + * @throws \EE_Error |
|
1092 | + */ |
|
1093 | + public function has_received_submission() |
|
1094 | + { |
|
1095 | + $this->ensure_construct_finalized_called(); |
|
1096 | + return $this->_received_submission; |
|
1097 | + } |
|
1098 | + |
|
1099 | + |
|
1100 | + |
|
1101 | + /** |
|
1102 | + * Equivalent to passing 'exclude' in the constructor's options array. |
|
1103 | + * Removes the listed inputs from the form |
|
1104 | + * |
|
1105 | + * @param array $inputs_to_exclude values are the input names |
|
1106 | + * @return void |
|
1107 | + */ |
|
1108 | + public function exclude(array $inputs_to_exclude = array()) |
|
1109 | + { |
|
1110 | + foreach ($inputs_to_exclude as $input_to_exclude_name) { |
|
1111 | + unset($this->_subsections[$input_to_exclude_name]); |
|
1112 | + } |
|
1113 | + } |
|
1114 | + |
|
1115 | + |
|
1116 | + |
|
1117 | + /** |
|
1118 | + * @param array $inputs_to_hide |
|
1119 | + * @throws \EE_Error |
|
1120 | + */ |
|
1121 | + public function hide(array $inputs_to_hide = array()) |
|
1122 | + { |
|
1123 | + foreach ($inputs_to_hide as $input_to_hide) { |
|
1124 | + $input = $this->get_input($input_to_hide); |
|
1125 | + $input->set_display_strategy(new EE_Hidden_Display_Strategy()); |
|
1126 | + } |
|
1127 | + } |
|
1128 | + |
|
1129 | + |
|
1130 | + |
|
1131 | + /** |
|
1132 | + * add_subsections |
|
1133 | + * Adds the listed subsections to the form section. |
|
1134 | + * If $subsection_name_to_target is provided, |
|
1135 | + * then new subsections are added before or after that subsection, |
|
1136 | + * otherwise to the start or end of the entire subsections array. |
|
1137 | + * |
|
1138 | + * @param EE_Form_Section_Base[] $new_subsections array of new form subsections |
|
1139 | + * where keys are their names |
|
1140 | + * @param string $subsection_name_to_target an existing for section that $new_subsections |
|
1141 | + * should be added before or after |
|
1142 | + * IF $subsection_name_to_target is null, |
|
1143 | + * then $new_subsections will be added to |
|
1144 | + * the beginning or end of the entire subsections array |
|
1145 | + * @param boolean $add_before whether to add $new_subsections, before or after |
|
1146 | + * $subsection_name_to_target, |
|
1147 | + * or if $subsection_name_to_target is null, |
|
1148 | + * before or after entire subsections array |
|
1149 | + * @return void |
|
1150 | + * @throws \EE_Error |
|
1151 | + */ |
|
1152 | + public function add_subsections($new_subsections, $subsection_name_to_target = null, $add_before = true) |
|
1153 | + { |
|
1154 | + foreach ($new_subsections as $subsection_name => $subsection) { |
|
1155 | + if (! $subsection instanceof EE_Form_Section_Base) { |
|
1156 | + EE_Error::add_error( |
|
1157 | + sprintf( |
|
1158 | + __( |
|
1159 | + "Trying to add a %s as a subsection (it was named '%s') to the form section '%s'. It was removed.", |
|
1160 | + "event_espresso" |
|
1161 | + ), |
|
1162 | + get_class($subsection), |
|
1163 | + $subsection_name, |
|
1164 | + $this->name() |
|
1165 | + ) |
|
1166 | + ); |
|
1167 | + unset($new_subsections[$subsection_name]); |
|
1168 | + } |
|
1169 | + } |
|
1170 | + $this->_subsections = EEH_Array::insert_into_array( |
|
1171 | + $this->_subsections, |
|
1172 | + $new_subsections, |
|
1173 | + $subsection_name_to_target, |
|
1174 | + $add_before |
|
1175 | + ); |
|
1176 | + if ($this->_construction_finalized) { |
|
1177 | + foreach ($this->_subsections as $name => $subsection) { |
|
1178 | + $subsection->_construct_finalize($this, $name); |
|
1179 | + } |
|
1180 | + } |
|
1181 | + } |
|
1182 | + |
|
1183 | + |
|
1184 | + |
|
1185 | + /** |
|
1186 | + * Just gets all validatable subsections to clean their sensitive data |
|
1187 | + */ |
|
1188 | + public function clean_sensitive_data() |
|
1189 | + { |
|
1190 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
1191 | + $subsection->clean_sensitive_data(); |
|
1192 | + } |
|
1193 | + } |
|
1194 | + |
|
1195 | + |
|
1196 | + |
|
1197 | + /** |
|
1198 | + * @param string $form_submission_error_message |
|
1199 | + */ |
|
1200 | + public function set_submission_error_message($form_submission_error_message = '') |
|
1201 | + { |
|
1202 | + $this->_form_submission_error_message .= ! empty($form_submission_error_message) |
|
1203 | + ? $form_submission_error_message |
|
1204 | + : __('Form submission failed due to errors', 'event_espresso'); |
|
1205 | + } |
|
1206 | + |
|
1207 | + |
|
1208 | + |
|
1209 | + /** |
|
1210 | + * @return string |
|
1211 | + */ |
|
1212 | + public function submission_error_message() |
|
1213 | + { |
|
1214 | + return $this->_form_submission_error_message; |
|
1215 | + } |
|
1216 | + |
|
1217 | + |
|
1218 | + |
|
1219 | + /** |
|
1220 | + * @param string $form_submission_success_message |
|
1221 | + */ |
|
1222 | + public function set_submission_success_message($form_submission_success_message) |
|
1223 | + { |
|
1224 | + $this->_form_submission_success_message .= ! empty($form_submission_success_message) |
|
1225 | + ? $form_submission_success_message |
|
1226 | + : __('Form submitted successfully', 'event_espresso'); |
|
1227 | + } |
|
1228 | + |
|
1229 | + |
|
1230 | + |
|
1231 | + /** |
|
1232 | + * @return string |
|
1233 | + */ |
|
1234 | + public function submission_success_message() |
|
1235 | + { |
|
1236 | + return $this->_form_submission_success_message; |
|
1237 | + } |
|
1238 | + |
|
1239 | + |
|
1240 | + |
|
1241 | + /** |
|
1242 | + * Returns the prefix that should be used on child of this form section for |
|
1243 | + * their html names. If this form section itself has a parent, prepends ITS |
|
1244 | + * prefix onto this form section's prefix. Used primarily by |
|
1245 | + * EE_Form_Input_Base::_set_default_html_name_if_empty |
|
1246 | + * |
|
1247 | + * @return string |
|
1248 | + * @throws \EE_Error |
|
1249 | + */ |
|
1250 | + public function html_name_prefix() |
|
1251 | + { |
|
1252 | + if ($this->parent_section() instanceof EE_Form_Section_Proper) { |
|
1253 | + return $this->parent_section()->html_name_prefix() . '[' . $this->name() . ']'; |
|
1254 | + } else { |
|
1255 | + return $this->name(); |
|
1256 | + } |
|
1257 | + } |
|
1258 | + |
|
1259 | + |
|
1260 | + |
|
1261 | + /** |
|
1262 | + * Gets the name, but first checks _construct_finalize has been called. If not, |
|
1263 | + * calls it (assumes there is no parent and that we want the name to be whatever |
|
1264 | + * was set, which is probably nothing, or the classname) |
|
1265 | + * |
|
1266 | + * @return string |
|
1267 | + * @throws \EE_Error |
|
1268 | + */ |
|
1269 | + public function name() |
|
1270 | + { |
|
1271 | + $this->ensure_construct_finalized_called(); |
|
1272 | + return parent::name(); |
|
1273 | + } |
|
1274 | + |
|
1275 | + |
|
1276 | + |
|
1277 | + /** |
|
1278 | + * @return EE_Form_Section_Proper |
|
1279 | + * @throws \EE_Error |
|
1280 | + */ |
|
1281 | + public function parent_section() |
|
1282 | + { |
|
1283 | + $this->ensure_construct_finalized_called(); |
|
1284 | + return parent::parent_section(); |
|
1285 | + } |
|
1286 | + |
|
1287 | + |
|
1288 | + |
|
1289 | + /** |
|
1290 | + * make sure construction finalized was called, otherwise children might not be ready |
|
1291 | + * |
|
1292 | + * @return void |
|
1293 | + * @throws \EE_Error |
|
1294 | + */ |
|
1295 | + public function ensure_construct_finalized_called() |
|
1296 | + { |
|
1297 | + if (! $this->_construction_finalized) { |
|
1298 | + $this->_construct_finalize($this->_parent_section, $this->_name); |
|
1299 | + } |
|
1300 | + } |
|
1301 | + |
|
1302 | + |
|
1303 | + |
|
1304 | + /** |
|
1305 | + * Checks if any of this form section's inputs, or any of its children's inputs, |
|
1306 | + * are in teh form data. If any are found, returns true. Else false |
|
1307 | + * |
|
1308 | + * @param array $req_data |
|
1309 | + * @return boolean |
|
1310 | + */ |
|
1311 | + public function form_data_present_in($req_data = null) |
|
1312 | + { |
|
1313 | + if ($req_data === null) { |
|
1314 | + $req_data = $_POST; |
|
1315 | + } |
|
1316 | + foreach ($this->subsections() as $subsection) { |
|
1317 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
1318 | + if ($subsection->form_data_present_in($req_data)) { |
|
1319 | + return true; |
|
1320 | + } |
|
1321 | + } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
1322 | + if ($subsection->form_data_present_in($req_data)) { |
|
1323 | + return true; |
|
1324 | + } |
|
1325 | + } |
|
1326 | + } |
|
1327 | + return false; |
|
1328 | + } |
|
1329 | + |
|
1330 | + |
|
1331 | + |
|
1332 | + /** |
|
1333 | + * Gets validation errors for this form section and subsections |
|
1334 | + * Similar to EE_Form_Section_Validatable::get_validation_errors() except this |
|
1335 | + * gets the validation errors for ALL subsection |
|
1336 | + * |
|
1337 | + * @return EE_Validation_Error[] |
|
1338 | + */ |
|
1339 | + public function get_validation_errors_accumulated() |
|
1340 | + { |
|
1341 | + $validation_errors = $this->get_validation_errors(); |
|
1342 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
1343 | + if ($subsection instanceof EE_Form_Section_Proper) { |
|
1344 | + $validation_errors_on_this_subsection = $subsection->get_validation_errors_accumulated(); |
|
1345 | + } else { |
|
1346 | + $validation_errors_on_this_subsection = $subsection->get_validation_errors(); |
|
1347 | + } |
|
1348 | + if ($validation_errors_on_this_subsection) { |
|
1349 | + $validation_errors = array_merge($validation_errors, $validation_errors_on_this_subsection); |
|
1350 | + } |
|
1351 | + } |
|
1352 | + return $validation_errors; |
|
1353 | + } |
|
1354 | + |
|
1355 | + |
|
1356 | + |
|
1357 | + /** |
|
1358 | + * This isn't just the name of an input, it's a path pointing to an input. The |
|
1359 | + * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
1360 | + * dot-dot-slash (../) means to ascend into the parent section. |
|
1361 | + * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
1362 | + * which will be returned. |
|
1363 | + * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
1364 | + * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
1365 | + * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
1366 | + * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
1367 | + * Etc |
|
1368 | + * |
|
1369 | + * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
1370 | + * @return EE_Form_Section_Base |
|
1371 | + */ |
|
1372 | + public function find_section_from_path($form_section_path) |
|
1373 | + { |
|
1374 | + //check if we can find the input from purely going straight up the tree |
|
1375 | + $input = parent::find_section_from_path($form_section_path); |
|
1376 | + if ($input instanceof EE_Form_Section_Base) { |
|
1377 | + return $input; |
|
1378 | + } |
|
1379 | + $next_slash_pos = strpos($form_section_path, '/'); |
|
1380 | + if ($next_slash_pos !== false) { |
|
1381 | + $child_section_name = substr($form_section_path, 0, $next_slash_pos); |
|
1382 | + $subpath = substr($form_section_path, $next_slash_pos + 1); |
|
1383 | + } else { |
|
1384 | + $child_section_name = $form_section_path; |
|
1385 | + $subpath = ''; |
|
1386 | + } |
|
1387 | + $child_section = $this->get_subsection($child_section_name); |
|
1388 | + if ($child_section instanceof EE_Form_Section_Base) { |
|
1389 | + return $child_section->find_section_from_path($subpath); |
|
1390 | + } else { |
|
1391 | + return null; |
|
1392 | + } |
|
1393 | + } |
|
1394 | 1394 | |
1395 | 1395 | } |
1396 | 1396 |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
2 | - exit('No direct script access allowed'); |
|
2 | + exit('No direct script access allowed'); |
|
3 | 3 | } |
4 | 4 | |
5 | 5 | |
@@ -16,1130 +16,1130 @@ discard block |
||
16 | 16 | abstract class EE_Form_Input_Base extends EE_Form_Section_Validatable |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * the input's name attribute |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $_html_name; |
|
25 | - |
|
26 | - /** |
|
27 | - * id for the html label tag |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - protected $_html_label_id; |
|
32 | - |
|
33 | - /** |
|
34 | - * class for teh html label tag |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $_html_label_class; |
|
39 | - |
|
40 | - /** |
|
41 | - * any additional html attributes that you may want to add |
|
42 | - * |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - protected $_html_other_attributes; |
|
46 | - |
|
47 | - /** |
|
48 | - * style for teh html label tag |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - protected $_html_label_style; |
|
53 | - |
|
54 | - /** |
|
55 | - * text to be placed in the html label |
|
56 | - * |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - protected $_html_label_text; |
|
60 | - |
|
61 | - /** |
|
62 | - * the full html label. If used, all other html_label_* properties are invalid |
|
63 | - * |
|
64 | - * @var string |
|
65 | - */ |
|
66 | - protected $_html_label; |
|
67 | - |
|
68 | - /** |
|
69 | - * HTML to use for help text (normally placed below form input), in a span which normally |
|
70 | - * has a class of 'description' |
|
71 | - * |
|
72 | - * @var string |
|
73 | - */ |
|
74 | - protected $_html_help_text; |
|
75 | - |
|
76 | - /** |
|
77 | - * CSS classes for displaying the help span |
|
78 | - * |
|
79 | - * @var string |
|
80 | - */ |
|
81 | - protected $_html_help_class = 'description'; |
|
82 | - |
|
83 | - /** |
|
84 | - * CSS to put in the style attribute on the help span |
|
85 | - * |
|
86 | - * @var string |
|
87 | - */ |
|
88 | - protected $_html_help_style; |
|
89 | - |
|
90 | - /** |
|
91 | - * Stores whether or not this input's response is required. |
|
92 | - * Because certain styling elements may also want to know that this |
|
93 | - * input is required etc. |
|
94 | - * |
|
95 | - * @var boolean |
|
96 | - */ |
|
97 | - protected $_required; |
|
98 | - |
|
99 | - /** |
|
100 | - * css class added to required inputs |
|
101 | - * |
|
102 | - * @var string |
|
103 | - */ |
|
104 | - protected $_required_css_class = 'ee-required'; |
|
105 | - |
|
106 | - /** |
|
107 | - * css styles applied to button type inputs |
|
108 | - * |
|
109 | - * @var string |
|
110 | - */ |
|
111 | - protected $_button_css_attributes; |
|
112 | - |
|
113 | - /** |
|
114 | - * The raw data submitted for this, like in the $_POST super global. |
|
115 | - * Generally unsafe for usage in client code |
|
116 | - * |
|
117 | - * @var mixed string or array |
|
118 | - */ |
|
119 | - protected $_raw_value; |
|
120 | - |
|
121 | - /** |
|
122 | - * Value normalized according to the input's normalization strategy. |
|
123 | - * The normalization strategy dictates whether this is a string, int, float, |
|
124 | - * boolean, or array of any of those. |
|
125 | - * |
|
126 | - * @var mixed |
|
127 | - */ |
|
128 | - protected $_normalized_value; |
|
129 | - |
|
130 | - /** |
|
131 | - * Strategy used for displaying this field. |
|
132 | - * Child classes must use _get_display_strategy to access it. |
|
133 | - * |
|
134 | - * @var EE_Display_Strategy_Base |
|
135 | - */ |
|
136 | - private $_display_strategy; |
|
137 | - |
|
138 | - /** |
|
139 | - * Gets all the validation strategies used on this field |
|
140 | - * |
|
141 | - * @var EE_Validation_Strategy_Base[] |
|
142 | - */ |
|
143 | - private $_validation_strategies = array(); |
|
144 | - |
|
145 | - /** |
|
146 | - * The normalization strategy for this field |
|
147 | - * |
|
148 | - * @var EE_Normalization_Strategy_Base |
|
149 | - */ |
|
150 | - private $_normalization_strategy; |
|
151 | - |
|
152 | - /** |
|
153 | - * Strategy for removing sensitive data after we're done with the form input |
|
154 | - * |
|
155 | - * @var EE_Sensitive_Data_Removal_Base |
|
156 | - */ |
|
157 | - protected $_sensitive_data_removal_strategy; |
|
158 | - |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * @param array $input_args { |
|
163 | - * @type string $html_name the html name for the input |
|
164 | - * @type string $html_label_id the id attribute to give to the html label tag |
|
165 | - * @type string $html_label_class the class attribute to give to the html label tag |
|
166 | - * @type string $html_label_style the style attribute to give ot teh label tag |
|
167 | - * @type string $html_label_text the text to put in the label tag |
|
168 | - * @type string $html_label the full html label. If used, |
|
169 | - * all other html_label_* args are invalid |
|
170 | - * @type string $html_help_text text to put in help element |
|
171 | - * @type string $html_help_style style attribute to give to teh help element |
|
172 | - * @type string $html_help_class class attribute to give to the help element |
|
173 | - * @type string $default default value NORMALIZED (eg, if providing the default |
|
174 | - * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
175 | - * @type EE_Display_Strategy_Base $display strategy |
|
176 | - * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
177 | - * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
178 | - * } |
|
179 | - */ |
|
180 | - public function __construct($input_args = array()) |
|
181 | - { |
|
182 | - $input_args = (array)apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
183 | - // the following properties must be cast as arrays |
|
184 | - if (isset($input_args['validation_strategies'])) { |
|
185 | - foreach ((array)$input_args['validation_strategies'] as $validation_strategy) { |
|
186 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
187 | - $this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy; |
|
188 | - } |
|
189 | - } |
|
190 | - unset($input_args['validation_strategies']); |
|
191 | - } |
|
192 | - // loop thru incoming options |
|
193 | - foreach ($input_args as $key => $value) { |
|
194 | - // add underscore to $key to match property names |
|
195 | - $_key = '_' . $key; |
|
196 | - if (property_exists($this, $_key)) { |
|
197 | - $this->{$_key} = $value; |
|
198 | - } |
|
199 | - } |
|
200 | - // ensure that "required" is set correctly |
|
201 | - $this->set_required( |
|
202 | - $this->_required, isset($input_args['required_validation_error_message']) |
|
203 | - ? $input_args['required_validation_error_message'] |
|
204 | - : null |
|
205 | - ); |
|
206 | - //$this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
207 | - $this->_display_strategy->_construct_finalize($this); |
|
208 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
209 | - $validation_strategy->_construct_finalize($this); |
|
210 | - } |
|
211 | - if (! $this->_normalization_strategy) { |
|
212 | - $this->_normalization_strategy = new EE_Text_Normalization(); |
|
213 | - } |
|
214 | - $this->_normalization_strategy->_construct_finalize($this); |
|
215 | - //at least we can use the normalization strategy to populate the default |
|
216 | - if (isset($input_args['default'])) { |
|
217 | - $this->set_default($input_args['default']); |
|
218 | - } |
|
219 | - if (! $this->_sensitive_data_removal_strategy) { |
|
220 | - $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
221 | - } |
|
222 | - $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
223 | - parent::__construct($input_args); |
|
224 | - } |
|
225 | - |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * Sets the html_name to its default value, if none was specified in teh constructor. |
|
230 | - * Calculation involves using the name and the parent's html_name |
|
231 | - * |
|
232 | - * @throws \EE_Error |
|
233 | - */ |
|
234 | - protected function _set_default_html_name_if_empty() |
|
235 | - { |
|
236 | - if (! $this->_html_name) { |
|
237 | - $this->_html_name = $this->name(); |
|
238 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
239 | - $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
240 | - } |
|
241 | - } |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - |
|
246 | - /** |
|
247 | - * @param $parent_form_section |
|
248 | - * @param $name |
|
249 | - * @throws \EE_Error |
|
250 | - */ |
|
251 | - public function _construct_finalize($parent_form_section, $name) |
|
252 | - { |
|
253 | - parent::_construct_finalize($parent_form_section, $name); |
|
254 | - if ($this->_html_label === null && $this->_html_label_text === null) { |
|
255 | - $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
256 | - } |
|
257 | - do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
264 | - * |
|
265 | - * @return EE_Display_Strategy_Base |
|
266 | - * @throws EE_Error |
|
267 | - */ |
|
268 | - protected function _get_display_strategy() |
|
269 | - { |
|
270 | - $this->ensure_construct_finalized_called(); |
|
271 | - if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
272 | - throw new EE_Error( |
|
273 | - sprintf( |
|
274 | - __( |
|
275 | - "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
276 | - "event_espresso" |
|
277 | - ), |
|
278 | - $this->html_name(), |
|
279 | - $this->html_id() |
|
280 | - ) |
|
281 | - ); |
|
282 | - } else { |
|
283 | - return $this->_display_strategy; |
|
284 | - } |
|
285 | - } |
|
286 | - |
|
287 | - |
|
288 | - |
|
289 | - /** |
|
290 | - * Sets the display strategy. |
|
291 | - * |
|
292 | - * @param EE_Display_Strategy_Base $strategy |
|
293 | - */ |
|
294 | - protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
295 | - { |
|
296 | - $this->_display_strategy = $strategy; |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - |
|
301 | - /** |
|
302 | - * Sets the sanitization strategy |
|
303 | - * |
|
304 | - * @param EE_Normalization_Strategy_Base $strategy |
|
305 | - */ |
|
306 | - protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
307 | - { |
|
308 | - $this->_normalization_strategy = $strategy; |
|
309 | - } |
|
310 | - |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * Gets sensitive_data_removal_strategy |
|
315 | - * |
|
316 | - * @return EE_Sensitive_Data_Removal_Base |
|
317 | - */ |
|
318 | - public function get_sensitive_data_removal_strategy() |
|
319 | - { |
|
320 | - return $this->_sensitive_data_removal_strategy; |
|
321 | - } |
|
322 | - |
|
323 | - |
|
324 | - |
|
325 | - /** |
|
326 | - * Sets sensitive_data_removal_strategy |
|
327 | - * |
|
328 | - * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
329 | - * @return boolean |
|
330 | - */ |
|
331 | - public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
332 | - { |
|
333 | - $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
334 | - } |
|
335 | - |
|
336 | - |
|
337 | - |
|
338 | - /** |
|
339 | - * Gets the display strategy for this input |
|
340 | - * |
|
341 | - * @return EE_Display_Strategy_Base |
|
342 | - */ |
|
343 | - public function get_display_strategy() |
|
344 | - { |
|
345 | - return $this->_display_strategy; |
|
346 | - } |
|
347 | - |
|
348 | - |
|
349 | - |
|
350 | - /** |
|
351 | - * Overwrites the display strategy |
|
352 | - * |
|
353 | - * @param EE_Display_Strategy_Base $display_strategy |
|
354 | - */ |
|
355 | - public function set_display_strategy($display_strategy) |
|
356 | - { |
|
357 | - $this->_display_strategy = $display_strategy; |
|
358 | - $this->_display_strategy->_construct_finalize($this); |
|
359 | - } |
|
360 | - |
|
361 | - |
|
362 | - |
|
363 | - /** |
|
364 | - * Gets the normalization strategy set on this input |
|
365 | - * |
|
366 | - * @return EE_Normalization_Strategy_Base |
|
367 | - */ |
|
368 | - public function get_normalization_strategy() |
|
369 | - { |
|
370 | - return $this->_normalization_strategy; |
|
371 | - } |
|
372 | - |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * Overwrites the normalization strategy |
|
377 | - * |
|
378 | - * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
379 | - */ |
|
380 | - public function set_normalization_strategy($normalization_strategy) |
|
381 | - { |
|
382 | - $this->_normalization_strategy = $normalization_strategy; |
|
383 | - $this->_normalization_strategy->_construct_finalize($this); |
|
384 | - } |
|
385 | - |
|
386 | - |
|
387 | - |
|
388 | - /** |
|
389 | - * Returns all teh validation strategies which apply to this field, numerically indexed |
|
390 | - * |
|
391 | - * @return EE_Validation_Strategy_Base[] |
|
392 | - */ |
|
393 | - public function get_validation_strategies() |
|
394 | - { |
|
395 | - return $this->_validation_strategies; |
|
396 | - } |
|
397 | - |
|
398 | - |
|
399 | - |
|
400 | - /** |
|
401 | - * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
402 | - * |
|
403 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
404 | - * @return void |
|
405 | - */ |
|
406 | - protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
407 | - { |
|
408 | - $validation_strategy->_construct_finalize($this); |
|
409 | - $this->_validation_strategies[] = $validation_strategy; |
|
410 | - } |
|
411 | - |
|
412 | - |
|
413 | - |
|
414 | - /** |
|
415 | - * Adds a new validation strategy onto the form input |
|
416 | - * |
|
417 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
418 | - * @return void |
|
419 | - */ |
|
420 | - public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
421 | - { |
|
422 | - $this->_add_validation_strategy($validation_strategy); |
|
423 | - } |
|
424 | - |
|
425 | - |
|
426 | - |
|
427 | - /** |
|
428 | - * The classname of the validation strategy to remove |
|
429 | - * |
|
430 | - * @param string $validation_strategy_classname |
|
431 | - */ |
|
432 | - public function remove_validation_strategy($validation_strategy_classname) |
|
433 | - { |
|
434 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
435 | - if ( |
|
436 | - $validation_strategy instanceof $validation_strategy_classname |
|
437 | - || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
438 | - ) { |
|
439 | - unset($this->_validation_strategies[$key]); |
|
440 | - } |
|
441 | - } |
|
442 | - } |
|
443 | - |
|
444 | - |
|
445 | - |
|
446 | - /** |
|
447 | - * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
448 | - * |
|
449 | - * @param array $validation_strategy_classnames |
|
450 | - * @return bool |
|
451 | - */ |
|
452 | - public function has_validation_strategy($validation_strategy_classnames) |
|
453 | - { |
|
454 | - $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
455 | - ? $validation_strategy_classnames |
|
456 | - : array($validation_strategy_classnames); |
|
457 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
458 | - if (in_array($key, $validation_strategy_classnames)) { |
|
459 | - return true; |
|
460 | - } |
|
461 | - } |
|
462 | - return false; |
|
463 | - } |
|
464 | - |
|
465 | - |
|
466 | - |
|
467 | - /** |
|
468 | - * Gets the HTML |
|
469 | - * |
|
470 | - * @return string |
|
471 | - */ |
|
472 | - public function get_html() |
|
473 | - { |
|
474 | - return $this->_parent_section->get_html_for_input($this); |
|
475 | - } |
|
476 | - |
|
477 | - |
|
478 | - |
|
479 | - /** |
|
480 | - * Gets the HTML for the input itself (no label or errors) according to the |
|
481 | - * input's display strategy |
|
482 | - * Makes sure the JS and CSS are enqueued for it |
|
483 | - * |
|
484 | - * @return string |
|
485 | - * @throws \EE_Error |
|
486 | - */ |
|
487 | - public function get_html_for_input() |
|
488 | - { |
|
489 | - return $this->_form_html_filter |
|
490 | - ? $this->_form_html_filter->filterHtml( |
|
491 | - $this->_get_display_strategy()->display(), |
|
492 | - $this |
|
493 | - ) |
|
494 | - : $this->_get_display_strategy()->display(); |
|
495 | - } |
|
496 | - |
|
497 | - |
|
498 | - |
|
499 | - /** |
|
500 | - * @return string |
|
501 | - */ |
|
502 | - public function html_other_attributes() |
|
503 | - { |
|
504 | - return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : ''; |
|
505 | - } |
|
506 | - |
|
507 | - |
|
508 | - |
|
509 | - /** |
|
510 | - * @param string $html_other_attributes |
|
511 | - */ |
|
512 | - public function set_html_other_attributes($html_other_attributes) |
|
513 | - { |
|
514 | - $this->_html_other_attributes = $html_other_attributes; |
|
515 | - } |
|
516 | - |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * Gets the HTML for displaying the label for this form input |
|
521 | - * according to the form section's layout strategy |
|
522 | - * |
|
523 | - * @return string |
|
524 | - */ |
|
525 | - public function get_html_for_label() |
|
526 | - { |
|
527 | - return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
528 | - } |
|
529 | - |
|
530 | - |
|
531 | - |
|
532 | - /** |
|
533 | - * Gets the HTML for displaying the errors section for this form input |
|
534 | - * according to the form section's layout strategy |
|
535 | - * |
|
536 | - * @return string |
|
537 | - */ |
|
538 | - public function get_html_for_errors() |
|
539 | - { |
|
540 | - return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
541 | - } |
|
542 | - |
|
543 | - |
|
544 | - |
|
545 | - /** |
|
546 | - * Gets the HTML for displaying the help text for this form input |
|
547 | - * according to the form section's layout strategy |
|
548 | - * |
|
549 | - * @return string |
|
550 | - */ |
|
551 | - public function get_html_for_help() |
|
552 | - { |
|
553 | - return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
554 | - } |
|
555 | - |
|
556 | - |
|
557 | - |
|
558 | - /** |
|
559 | - * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
560 | - * and returns whether or not the form input's submitted value is value |
|
561 | - * |
|
562 | - * @return boolean |
|
563 | - */ |
|
564 | - protected function _validate() |
|
565 | - { |
|
566 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
567 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
568 | - try { |
|
569 | - $validation_strategy->validate($this->normalized_value()); |
|
570 | - } catch (EE_Validation_Error $e) { |
|
571 | - $this->add_validation_error($e); |
|
572 | - } |
|
573 | - } |
|
574 | - } |
|
575 | - if ($this->get_validation_errors()) { |
|
576 | - return false; |
|
577 | - } else { |
|
578 | - return true; |
|
579 | - } |
|
580 | - } |
|
581 | - |
|
582 | - |
|
583 | - |
|
584 | - /** |
|
585 | - * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
586 | - * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
587 | - * |
|
588 | - * @param string $value |
|
589 | - * @return null|string |
|
590 | - */ |
|
591 | - private function _sanitize($value) |
|
592 | - { |
|
593 | - return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null; |
|
594 | - } |
|
595 | - |
|
596 | - |
|
597 | - |
|
598 | - /** |
|
599 | - * Picks out the form value that relates to this form input, |
|
600 | - * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
601 | - * Returns whether or not any validation errors occurred |
|
602 | - * |
|
603 | - * @param array $req_data like $_POST |
|
604 | - * @return boolean whether or not there was an error |
|
605 | - * @throws \EE_Error |
|
606 | - */ |
|
607 | - protected function _normalize($req_data) |
|
608 | - { |
|
609 | - //any existing validation errors don't apply so clear them |
|
610 | - $this->_validation_errors = array(); |
|
611 | - try { |
|
612 | - $raw_input = $this->find_form_data_for_this_section($req_data); |
|
613 | - //super simple sanitization for now |
|
614 | - if (is_array($raw_input)) { |
|
615 | - $raw_value = array(); |
|
616 | - foreach ($raw_input as $key => $value) { |
|
617 | - $raw_value[$key] = $this->_sanitize($value); |
|
618 | - } |
|
619 | - $this->_set_raw_value($raw_value); |
|
620 | - } else { |
|
621 | - $this->_set_raw_value($this->_sanitize($raw_input)); |
|
622 | - } |
|
623 | - //we want to mostly leave the input alone in case we need to re-display it to the user |
|
624 | - $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
625 | - } catch (EE_Validation_Error $e) { |
|
626 | - $this->add_validation_error($e); |
|
627 | - } |
|
628 | - } |
|
629 | - |
|
630 | - |
|
631 | - |
|
632 | - /** |
|
633 | - * @return string |
|
634 | - */ |
|
635 | - public function html_name() |
|
636 | - { |
|
637 | - $this->_set_default_html_name_if_empty(); |
|
638 | - return $this->_html_name; |
|
639 | - } |
|
640 | - |
|
641 | - |
|
642 | - |
|
643 | - /** |
|
644 | - * @return string |
|
645 | - */ |
|
646 | - public function html_label_id() |
|
647 | - { |
|
648 | - return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->_html_id . '-lbl'; |
|
649 | - } |
|
650 | - |
|
651 | - |
|
652 | - |
|
653 | - /** |
|
654 | - * @return string |
|
655 | - */ |
|
656 | - public function html_label_class() |
|
657 | - { |
|
658 | - return $this->_html_label_class; |
|
659 | - } |
|
660 | - |
|
661 | - |
|
662 | - |
|
663 | - /** |
|
664 | - * @return string |
|
665 | - */ |
|
666 | - public function html_label_style() |
|
667 | - { |
|
668 | - return $this->_html_label_style; |
|
669 | - } |
|
670 | - |
|
671 | - |
|
672 | - |
|
673 | - /** |
|
674 | - * @return string |
|
675 | - */ |
|
676 | - public function html_label_text() |
|
677 | - { |
|
678 | - return $this->_html_label_text; |
|
679 | - } |
|
680 | - |
|
681 | - |
|
682 | - |
|
683 | - /** |
|
684 | - * @return string |
|
685 | - */ |
|
686 | - public function html_help_text() |
|
687 | - { |
|
688 | - return $this->_html_help_text; |
|
689 | - } |
|
690 | - |
|
691 | - |
|
692 | - |
|
693 | - /** |
|
694 | - * @return string |
|
695 | - */ |
|
696 | - public function html_help_class() |
|
697 | - { |
|
698 | - return $this->_html_help_class; |
|
699 | - } |
|
700 | - |
|
701 | - |
|
702 | - |
|
703 | - /** |
|
704 | - * @return string |
|
705 | - */ |
|
706 | - public function html_help_style() |
|
707 | - { |
|
708 | - return $this->_html_style; |
|
709 | - } |
|
710 | - |
|
711 | - |
|
712 | - |
|
713 | - /** |
|
714 | - * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
715 | - * Please note that almost all client code should instead use the normalized_value; |
|
716 | - * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
717 | - * mostly by escaping quotes) |
|
718 | - * Note, we do not store the exact original value sent in the user's request because |
|
719 | - * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
720 | - * in which case, we would have stored the malicious content to our database. |
|
721 | - * |
|
722 | - * @return string |
|
723 | - */ |
|
724 | - public function raw_value() |
|
725 | - { |
|
726 | - return $this->_raw_value; |
|
727 | - } |
|
728 | - |
|
729 | - |
|
730 | - |
|
731 | - /** |
|
732 | - * Returns a string safe to usage in form inputs when displaying, because |
|
733 | - * it escapes all html entities |
|
734 | - * |
|
735 | - * @return string |
|
736 | - */ |
|
737 | - public function raw_value_in_form() |
|
738 | - { |
|
739 | - return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
740 | - } |
|
741 | - |
|
742 | - |
|
743 | - |
|
744 | - /** |
|
745 | - * returns the value after it's been sanitized, and then converted into it's proper type |
|
746 | - * in PHP. Eg, a string, an int, an array, |
|
747 | - * |
|
748 | - * @return mixed |
|
749 | - */ |
|
750 | - public function normalized_value() |
|
751 | - { |
|
752 | - return $this->_normalized_value; |
|
753 | - } |
|
754 | - |
|
755 | - |
|
756 | - |
|
757 | - /** |
|
758 | - * Returns the normalized value is a presentable way. By default this is just |
|
759 | - * the normalized value by itself, but it can be overridden for when that's not |
|
760 | - * the best thing to display |
|
761 | - * |
|
762 | - * @return string |
|
763 | - */ |
|
764 | - public function pretty_value() |
|
765 | - { |
|
766 | - return $this->_normalized_value; |
|
767 | - } |
|
768 | - |
|
769 | - |
|
770 | - |
|
771 | - /** |
|
772 | - * When generating the JS for the jquery validation rules like<br> |
|
773 | - * <code>$( "#myform" ).validate({ |
|
774 | - * rules: { |
|
775 | - * password: "required", |
|
776 | - * password_again: { |
|
777 | - * equalTo: "#password" |
|
778 | - * } |
|
779 | - * } |
|
780 | - * });</code> |
|
781 | - * if this field had the name 'password_again', it should return |
|
782 | - * <br><code>password_again: { |
|
783 | - * equalTo: "#password" |
|
784 | - * }</code> |
|
785 | - * |
|
786 | - * @return array |
|
787 | - */ |
|
788 | - public function get_jquery_validation_rules() |
|
789 | - { |
|
790 | - $jquery_validation_js = array(); |
|
791 | - $jquery_validation_rules = array(); |
|
792 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
793 | - $jquery_validation_rules = array_replace_recursive( |
|
794 | - $jquery_validation_rules, |
|
795 | - $validation_strategy->get_jquery_validation_rule_array() |
|
796 | - ); |
|
797 | - } |
|
798 | - if (! empty($jquery_validation_rules)) { |
|
799 | - foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
800 | - $jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules; |
|
801 | - } |
|
802 | - } |
|
803 | - return $jquery_validation_js; |
|
804 | - } |
|
805 | - |
|
806 | - |
|
807 | - |
|
808 | - /** |
|
809 | - * Sets the input's default value for use in displaying in the form. Note: value should be |
|
810 | - * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
811 | - * |
|
812 | - * @param mixed $value |
|
813 | - * @return void |
|
814 | - */ |
|
815 | - public function set_default($value) |
|
816 | - { |
|
817 | - $this->_set_normalized_value($value); |
|
818 | - $this->_set_raw_value($value); |
|
819 | - } |
|
820 | - |
|
821 | - |
|
822 | - |
|
823 | - /** |
|
824 | - * Sets the normalized value on this input |
|
825 | - * |
|
826 | - * @param mixed $value |
|
827 | - */ |
|
828 | - protected function _set_normalized_value($value) |
|
829 | - { |
|
830 | - $this->_normalized_value = $value; |
|
831 | - } |
|
832 | - |
|
833 | - |
|
834 | - |
|
835 | - /** |
|
836 | - * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
837 | - * |
|
838 | - * @param mixed $value |
|
839 | - */ |
|
840 | - protected function _set_raw_value($value) |
|
841 | - { |
|
842 | - $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
843 | - } |
|
844 | - |
|
845 | - |
|
846 | - |
|
847 | - /** |
|
848 | - * Sets the HTML label text after it has already been defined |
|
849 | - * |
|
850 | - * @param string $label |
|
851 | - * @return void |
|
852 | - */ |
|
853 | - public function set_html_label_text($label) |
|
854 | - { |
|
855 | - $this->_html_label_text = $label; |
|
856 | - } |
|
857 | - |
|
858 | - |
|
859 | - |
|
860 | - /** |
|
861 | - * Sets whether or not this field is required, and adjusts the validation strategy. |
|
862 | - * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
863 | - * please add it as a validation strategy using add_validation_strategy as normal |
|
864 | - * |
|
865 | - * @param boolean $required boolean |
|
866 | - * @param null $required_text |
|
867 | - */ |
|
868 | - public function set_required($required = true, $required_text = null) |
|
869 | - { |
|
870 | - $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
871 | - //whether $required is a string or a boolean, we want to add a required validation strategy |
|
872 | - if ($required) { |
|
873 | - $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
874 | - } else { |
|
875 | - $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
876 | - } |
|
877 | - $this->_required = $required; |
|
878 | - } |
|
879 | - |
|
880 | - |
|
881 | - |
|
882 | - /** |
|
883 | - * Returns whether or not this field is required |
|
884 | - * |
|
885 | - * @return boolean |
|
886 | - */ |
|
887 | - public function required() |
|
888 | - { |
|
889 | - return $this->_required; |
|
890 | - } |
|
891 | - |
|
892 | - |
|
893 | - |
|
894 | - /** |
|
895 | - * @param string $required_css_class |
|
896 | - */ |
|
897 | - public function set_required_css_class($required_css_class) |
|
898 | - { |
|
899 | - $this->_required_css_class = $required_css_class; |
|
900 | - } |
|
901 | - |
|
902 | - |
|
903 | - |
|
904 | - /** |
|
905 | - * @return string |
|
906 | - */ |
|
907 | - public function required_css_class() |
|
908 | - { |
|
909 | - return $this->_required_css_class; |
|
910 | - } |
|
911 | - |
|
912 | - |
|
913 | - |
|
914 | - /** |
|
915 | - * Sets the help text, in case |
|
916 | - * |
|
917 | - * @param string $text |
|
918 | - */ |
|
919 | - public function set_html_help_text($text) |
|
920 | - { |
|
921 | - $this->_html_help_text = $text; |
|
922 | - } |
|
923 | - |
|
924 | - |
|
925 | - |
|
926 | - /** |
|
927 | - * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
928 | - * input. If there is any kind of sensitive data removal on this input, we clear |
|
929 | - * out the raw value completely |
|
930 | - * |
|
931 | - * @return void |
|
932 | - */ |
|
933 | - public function clean_sensitive_data() |
|
934 | - { |
|
935 | - //if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
936 | - //if we need more logic than this we'll make a strategy for it |
|
937 | - if ($this->_sensitive_data_removal_strategy |
|
938 | - && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
939 | - ) { |
|
940 | - $this->_set_raw_value(null); |
|
941 | - } |
|
942 | - //and clean the normalized value according to the appropriate strategy |
|
943 | - $this->_set_normalized_value( |
|
944 | - $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
945 | - $this->_normalized_value |
|
946 | - ) |
|
947 | - ); |
|
948 | - } |
|
949 | - |
|
950 | - |
|
951 | - |
|
952 | - /** |
|
953 | - * @param bool $primary |
|
954 | - * @param string $button_size |
|
955 | - * @param string $other_attributes |
|
956 | - */ |
|
957 | - public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
958 | - { |
|
959 | - $button_css_attributes = 'button'; |
|
960 | - $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
961 | - switch ($button_size) { |
|
962 | - case 'xs' : |
|
963 | - case 'extra-small' : |
|
964 | - $button_css_attributes .= ' button-xs'; |
|
965 | - break; |
|
966 | - case 'sm' : |
|
967 | - case 'small' : |
|
968 | - $button_css_attributes .= ' button-sm'; |
|
969 | - break; |
|
970 | - case 'lg' : |
|
971 | - case 'large' : |
|
972 | - $button_css_attributes .= ' button-lg'; |
|
973 | - break; |
|
974 | - case 'block' : |
|
975 | - $button_css_attributes .= ' button-block'; |
|
976 | - break; |
|
977 | - case 'md' : |
|
978 | - case 'medium' : |
|
979 | - default : |
|
980 | - $button_css_attributes .= ''; |
|
981 | - } |
|
982 | - $this->_button_css_attributes .= ! empty($other_attributes) |
|
983 | - ? $button_css_attributes . ' ' . $other_attributes |
|
984 | - : $button_css_attributes; |
|
985 | - } |
|
986 | - |
|
987 | - |
|
988 | - |
|
989 | - /** |
|
990 | - * @return string |
|
991 | - */ |
|
992 | - public function button_css_attributes() |
|
993 | - { |
|
994 | - if (empty($this->_button_css_attributes)) { |
|
995 | - $this->set_button_css_attributes(); |
|
996 | - } |
|
997 | - return $this->_button_css_attributes; |
|
998 | - } |
|
999 | - |
|
1000 | - |
|
1001 | - |
|
1002 | - /** |
|
1003 | - * find_form_data_for_this_section |
|
1004 | - * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1005 | - * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1006 | - * then it's value should be in $_REQUEST at $_REQUEST['my_form']['subform']['form_input_1']. |
|
1007 | - * (If that doesn't exist, we also check for this subsection's name |
|
1008 | - * at the TOP LEVEL of the request data. Eg $_REQUEST['form_input_1'].) |
|
1009 | - * This function finds its value in the form. |
|
1010 | - * |
|
1011 | - * @param array $req_data |
|
1012 | - * @return mixed whatever the raw value of this form section is in the request data |
|
1013 | - * @throws \EE_Error |
|
1014 | - */ |
|
1015 | - public function find_form_data_for_this_section($req_data) |
|
1016 | - { |
|
1017 | - // break up the html name by "[]" |
|
1018 | - if (strpos($this->html_name(), '[') !== false) { |
|
1019 | - $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1020 | - } else { |
|
1021 | - $before_any_brackets = $this->html_name(); |
|
1022 | - } |
|
1023 | - // grab all of the segments |
|
1024 | - preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1025 | - if (isset($matches[1]) && is_array($matches[1])) { |
|
1026 | - $name_parts = $matches[1]; |
|
1027 | - array_unshift($name_parts, $before_any_brackets); |
|
1028 | - } else { |
|
1029 | - $name_parts = array($before_any_brackets); |
|
1030 | - } |
|
1031 | - // now get the value for the input |
|
1032 | - $value = $this->_find_form_data_for_this_section_using_name_parts($name_parts, $req_data); |
|
1033 | - // check if this thing's name is at the TOP level of the request data |
|
1034 | - if ($value === null && isset($req_data[$this->name()])) { |
|
1035 | - $value = $req_data[$this->name()]; |
|
1036 | - } |
|
1037 | - return $value; |
|
1038 | - } |
|
1039 | - |
|
1040 | - |
|
1041 | - |
|
1042 | - /** |
|
1043 | - * @param array $html_name_parts |
|
1044 | - * @param array $req_data |
|
1045 | - * @return array | NULL |
|
1046 | - */ |
|
1047 | - public function _find_form_data_for_this_section_using_name_parts($html_name_parts, $req_data) |
|
1048 | - { |
|
1049 | - $first_part_to_consider = array_shift($html_name_parts); |
|
1050 | - if (isset($req_data[$first_part_to_consider])) { |
|
1051 | - if (empty($html_name_parts)) { |
|
1052 | - return $req_data[$first_part_to_consider]; |
|
1053 | - } else { |
|
1054 | - return $this->_find_form_data_for_this_section_using_name_parts( |
|
1055 | - $html_name_parts, |
|
1056 | - $req_data[$first_part_to_consider] |
|
1057 | - ); |
|
1058 | - } |
|
1059 | - } else { |
|
1060 | - return null; |
|
1061 | - } |
|
1062 | - } |
|
1063 | - |
|
1064 | - |
|
1065 | - |
|
1066 | - /** |
|
1067 | - * Checks if this form input's data is in the request data |
|
1068 | - * |
|
1069 | - * @param array $req_data like $_POST |
|
1070 | - * @return boolean |
|
1071 | - * @throws \EE_Error |
|
1072 | - */ |
|
1073 | - public function form_data_present_in($req_data = null) |
|
1074 | - { |
|
1075 | - if ($req_data === null) { |
|
1076 | - $req_data = $_POST; |
|
1077 | - } |
|
1078 | - $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1079 | - if ($checked_value !== null) { |
|
1080 | - return true; |
|
1081 | - } else { |
|
1082 | - return false; |
|
1083 | - } |
|
1084 | - } |
|
1085 | - |
|
1086 | - |
|
1087 | - |
|
1088 | - /** |
|
1089 | - * Overrides parent to add js data from validation and display strategies |
|
1090 | - * |
|
1091 | - * @param array $form_other_js_data |
|
1092 | - * @return array |
|
1093 | - */ |
|
1094 | - public function get_other_js_data($form_other_js_data = array()) |
|
1095 | - { |
|
1096 | - $form_other_js_data = $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1097 | - return $form_other_js_data; |
|
1098 | - } |
|
1099 | - |
|
1100 | - |
|
1101 | - |
|
1102 | - /** |
|
1103 | - * Gets other JS data for localization from this input's strategies, like |
|
1104 | - * the validation strategies and the display strategy |
|
1105 | - * |
|
1106 | - * @param array $form_other_js_data |
|
1107 | - * @return array |
|
1108 | - */ |
|
1109 | - public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
1110 | - { |
|
1111 | - $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1112 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1113 | - $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1114 | - } |
|
1115 | - return $form_other_js_data; |
|
1116 | - } |
|
1117 | - |
|
1118 | - |
|
1119 | - |
|
1120 | - /** |
|
1121 | - * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1122 | - * |
|
1123 | - * @return void |
|
1124 | - */ |
|
1125 | - public function enqueue_js() |
|
1126 | - { |
|
1127 | - //ask our display strategy and validation strategies if they have js to enqueue |
|
1128 | - $this->enqueue_js_from_strategies(); |
|
1129 | - } |
|
1130 | - |
|
1131 | - |
|
1132 | - |
|
1133 | - /** |
|
1134 | - * Tells strategies when its ok to enqueue their js and css |
|
1135 | - * |
|
1136 | - * @return void |
|
1137 | - */ |
|
1138 | - public function enqueue_js_from_strategies() |
|
1139 | - { |
|
1140 | - $this->get_display_strategy()->enqueue_js(); |
|
1141 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1142 | - $validation_strategy->enqueue_js(); |
|
1143 | - } |
|
1144 | - } |
|
19 | + /** |
|
20 | + * the input's name attribute |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $_html_name; |
|
25 | + |
|
26 | + /** |
|
27 | + * id for the html label tag |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + protected $_html_label_id; |
|
32 | + |
|
33 | + /** |
|
34 | + * class for teh html label tag |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $_html_label_class; |
|
39 | + |
|
40 | + /** |
|
41 | + * any additional html attributes that you may want to add |
|
42 | + * |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + protected $_html_other_attributes; |
|
46 | + |
|
47 | + /** |
|
48 | + * style for teh html label tag |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + protected $_html_label_style; |
|
53 | + |
|
54 | + /** |
|
55 | + * text to be placed in the html label |
|
56 | + * |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + protected $_html_label_text; |
|
60 | + |
|
61 | + /** |
|
62 | + * the full html label. If used, all other html_label_* properties are invalid |
|
63 | + * |
|
64 | + * @var string |
|
65 | + */ |
|
66 | + protected $_html_label; |
|
67 | + |
|
68 | + /** |
|
69 | + * HTML to use for help text (normally placed below form input), in a span which normally |
|
70 | + * has a class of 'description' |
|
71 | + * |
|
72 | + * @var string |
|
73 | + */ |
|
74 | + protected $_html_help_text; |
|
75 | + |
|
76 | + /** |
|
77 | + * CSS classes for displaying the help span |
|
78 | + * |
|
79 | + * @var string |
|
80 | + */ |
|
81 | + protected $_html_help_class = 'description'; |
|
82 | + |
|
83 | + /** |
|
84 | + * CSS to put in the style attribute on the help span |
|
85 | + * |
|
86 | + * @var string |
|
87 | + */ |
|
88 | + protected $_html_help_style; |
|
89 | + |
|
90 | + /** |
|
91 | + * Stores whether or not this input's response is required. |
|
92 | + * Because certain styling elements may also want to know that this |
|
93 | + * input is required etc. |
|
94 | + * |
|
95 | + * @var boolean |
|
96 | + */ |
|
97 | + protected $_required; |
|
98 | + |
|
99 | + /** |
|
100 | + * css class added to required inputs |
|
101 | + * |
|
102 | + * @var string |
|
103 | + */ |
|
104 | + protected $_required_css_class = 'ee-required'; |
|
105 | + |
|
106 | + /** |
|
107 | + * css styles applied to button type inputs |
|
108 | + * |
|
109 | + * @var string |
|
110 | + */ |
|
111 | + protected $_button_css_attributes; |
|
112 | + |
|
113 | + /** |
|
114 | + * The raw data submitted for this, like in the $_POST super global. |
|
115 | + * Generally unsafe for usage in client code |
|
116 | + * |
|
117 | + * @var mixed string or array |
|
118 | + */ |
|
119 | + protected $_raw_value; |
|
120 | + |
|
121 | + /** |
|
122 | + * Value normalized according to the input's normalization strategy. |
|
123 | + * The normalization strategy dictates whether this is a string, int, float, |
|
124 | + * boolean, or array of any of those. |
|
125 | + * |
|
126 | + * @var mixed |
|
127 | + */ |
|
128 | + protected $_normalized_value; |
|
129 | + |
|
130 | + /** |
|
131 | + * Strategy used for displaying this field. |
|
132 | + * Child classes must use _get_display_strategy to access it. |
|
133 | + * |
|
134 | + * @var EE_Display_Strategy_Base |
|
135 | + */ |
|
136 | + private $_display_strategy; |
|
137 | + |
|
138 | + /** |
|
139 | + * Gets all the validation strategies used on this field |
|
140 | + * |
|
141 | + * @var EE_Validation_Strategy_Base[] |
|
142 | + */ |
|
143 | + private $_validation_strategies = array(); |
|
144 | + |
|
145 | + /** |
|
146 | + * The normalization strategy for this field |
|
147 | + * |
|
148 | + * @var EE_Normalization_Strategy_Base |
|
149 | + */ |
|
150 | + private $_normalization_strategy; |
|
151 | + |
|
152 | + /** |
|
153 | + * Strategy for removing sensitive data after we're done with the form input |
|
154 | + * |
|
155 | + * @var EE_Sensitive_Data_Removal_Base |
|
156 | + */ |
|
157 | + protected $_sensitive_data_removal_strategy; |
|
158 | + |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * @param array $input_args { |
|
163 | + * @type string $html_name the html name for the input |
|
164 | + * @type string $html_label_id the id attribute to give to the html label tag |
|
165 | + * @type string $html_label_class the class attribute to give to the html label tag |
|
166 | + * @type string $html_label_style the style attribute to give ot teh label tag |
|
167 | + * @type string $html_label_text the text to put in the label tag |
|
168 | + * @type string $html_label the full html label. If used, |
|
169 | + * all other html_label_* args are invalid |
|
170 | + * @type string $html_help_text text to put in help element |
|
171 | + * @type string $html_help_style style attribute to give to teh help element |
|
172 | + * @type string $html_help_class class attribute to give to the help element |
|
173 | + * @type string $default default value NORMALIZED (eg, if providing the default |
|
174 | + * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
175 | + * @type EE_Display_Strategy_Base $display strategy |
|
176 | + * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
177 | + * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
178 | + * } |
|
179 | + */ |
|
180 | + public function __construct($input_args = array()) |
|
181 | + { |
|
182 | + $input_args = (array)apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
183 | + // the following properties must be cast as arrays |
|
184 | + if (isset($input_args['validation_strategies'])) { |
|
185 | + foreach ((array)$input_args['validation_strategies'] as $validation_strategy) { |
|
186 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
187 | + $this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy; |
|
188 | + } |
|
189 | + } |
|
190 | + unset($input_args['validation_strategies']); |
|
191 | + } |
|
192 | + // loop thru incoming options |
|
193 | + foreach ($input_args as $key => $value) { |
|
194 | + // add underscore to $key to match property names |
|
195 | + $_key = '_' . $key; |
|
196 | + if (property_exists($this, $_key)) { |
|
197 | + $this->{$_key} = $value; |
|
198 | + } |
|
199 | + } |
|
200 | + // ensure that "required" is set correctly |
|
201 | + $this->set_required( |
|
202 | + $this->_required, isset($input_args['required_validation_error_message']) |
|
203 | + ? $input_args['required_validation_error_message'] |
|
204 | + : null |
|
205 | + ); |
|
206 | + //$this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
207 | + $this->_display_strategy->_construct_finalize($this); |
|
208 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
209 | + $validation_strategy->_construct_finalize($this); |
|
210 | + } |
|
211 | + if (! $this->_normalization_strategy) { |
|
212 | + $this->_normalization_strategy = new EE_Text_Normalization(); |
|
213 | + } |
|
214 | + $this->_normalization_strategy->_construct_finalize($this); |
|
215 | + //at least we can use the normalization strategy to populate the default |
|
216 | + if (isset($input_args['default'])) { |
|
217 | + $this->set_default($input_args['default']); |
|
218 | + } |
|
219 | + if (! $this->_sensitive_data_removal_strategy) { |
|
220 | + $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
221 | + } |
|
222 | + $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
223 | + parent::__construct($input_args); |
|
224 | + } |
|
225 | + |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * Sets the html_name to its default value, if none was specified in teh constructor. |
|
230 | + * Calculation involves using the name and the parent's html_name |
|
231 | + * |
|
232 | + * @throws \EE_Error |
|
233 | + */ |
|
234 | + protected function _set_default_html_name_if_empty() |
|
235 | + { |
|
236 | + if (! $this->_html_name) { |
|
237 | + $this->_html_name = $this->name(); |
|
238 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
239 | + $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
240 | + } |
|
241 | + } |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + |
|
246 | + /** |
|
247 | + * @param $parent_form_section |
|
248 | + * @param $name |
|
249 | + * @throws \EE_Error |
|
250 | + */ |
|
251 | + public function _construct_finalize($parent_form_section, $name) |
|
252 | + { |
|
253 | + parent::_construct_finalize($parent_form_section, $name); |
|
254 | + if ($this->_html_label === null && $this->_html_label_text === null) { |
|
255 | + $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
256 | + } |
|
257 | + do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
264 | + * |
|
265 | + * @return EE_Display_Strategy_Base |
|
266 | + * @throws EE_Error |
|
267 | + */ |
|
268 | + protected function _get_display_strategy() |
|
269 | + { |
|
270 | + $this->ensure_construct_finalized_called(); |
|
271 | + if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
272 | + throw new EE_Error( |
|
273 | + sprintf( |
|
274 | + __( |
|
275 | + "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
276 | + "event_espresso" |
|
277 | + ), |
|
278 | + $this->html_name(), |
|
279 | + $this->html_id() |
|
280 | + ) |
|
281 | + ); |
|
282 | + } else { |
|
283 | + return $this->_display_strategy; |
|
284 | + } |
|
285 | + } |
|
286 | + |
|
287 | + |
|
288 | + |
|
289 | + /** |
|
290 | + * Sets the display strategy. |
|
291 | + * |
|
292 | + * @param EE_Display_Strategy_Base $strategy |
|
293 | + */ |
|
294 | + protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
295 | + { |
|
296 | + $this->_display_strategy = $strategy; |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + |
|
301 | + /** |
|
302 | + * Sets the sanitization strategy |
|
303 | + * |
|
304 | + * @param EE_Normalization_Strategy_Base $strategy |
|
305 | + */ |
|
306 | + protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
307 | + { |
|
308 | + $this->_normalization_strategy = $strategy; |
|
309 | + } |
|
310 | + |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * Gets sensitive_data_removal_strategy |
|
315 | + * |
|
316 | + * @return EE_Sensitive_Data_Removal_Base |
|
317 | + */ |
|
318 | + public function get_sensitive_data_removal_strategy() |
|
319 | + { |
|
320 | + return $this->_sensitive_data_removal_strategy; |
|
321 | + } |
|
322 | + |
|
323 | + |
|
324 | + |
|
325 | + /** |
|
326 | + * Sets sensitive_data_removal_strategy |
|
327 | + * |
|
328 | + * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
329 | + * @return boolean |
|
330 | + */ |
|
331 | + public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
332 | + { |
|
333 | + $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
334 | + } |
|
335 | + |
|
336 | + |
|
337 | + |
|
338 | + /** |
|
339 | + * Gets the display strategy for this input |
|
340 | + * |
|
341 | + * @return EE_Display_Strategy_Base |
|
342 | + */ |
|
343 | + public function get_display_strategy() |
|
344 | + { |
|
345 | + return $this->_display_strategy; |
|
346 | + } |
|
347 | + |
|
348 | + |
|
349 | + |
|
350 | + /** |
|
351 | + * Overwrites the display strategy |
|
352 | + * |
|
353 | + * @param EE_Display_Strategy_Base $display_strategy |
|
354 | + */ |
|
355 | + public function set_display_strategy($display_strategy) |
|
356 | + { |
|
357 | + $this->_display_strategy = $display_strategy; |
|
358 | + $this->_display_strategy->_construct_finalize($this); |
|
359 | + } |
|
360 | + |
|
361 | + |
|
362 | + |
|
363 | + /** |
|
364 | + * Gets the normalization strategy set on this input |
|
365 | + * |
|
366 | + * @return EE_Normalization_Strategy_Base |
|
367 | + */ |
|
368 | + public function get_normalization_strategy() |
|
369 | + { |
|
370 | + return $this->_normalization_strategy; |
|
371 | + } |
|
372 | + |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * Overwrites the normalization strategy |
|
377 | + * |
|
378 | + * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
379 | + */ |
|
380 | + public function set_normalization_strategy($normalization_strategy) |
|
381 | + { |
|
382 | + $this->_normalization_strategy = $normalization_strategy; |
|
383 | + $this->_normalization_strategy->_construct_finalize($this); |
|
384 | + } |
|
385 | + |
|
386 | + |
|
387 | + |
|
388 | + /** |
|
389 | + * Returns all teh validation strategies which apply to this field, numerically indexed |
|
390 | + * |
|
391 | + * @return EE_Validation_Strategy_Base[] |
|
392 | + */ |
|
393 | + public function get_validation_strategies() |
|
394 | + { |
|
395 | + return $this->_validation_strategies; |
|
396 | + } |
|
397 | + |
|
398 | + |
|
399 | + |
|
400 | + /** |
|
401 | + * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
402 | + * |
|
403 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
404 | + * @return void |
|
405 | + */ |
|
406 | + protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
407 | + { |
|
408 | + $validation_strategy->_construct_finalize($this); |
|
409 | + $this->_validation_strategies[] = $validation_strategy; |
|
410 | + } |
|
411 | + |
|
412 | + |
|
413 | + |
|
414 | + /** |
|
415 | + * Adds a new validation strategy onto the form input |
|
416 | + * |
|
417 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
418 | + * @return void |
|
419 | + */ |
|
420 | + public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
421 | + { |
|
422 | + $this->_add_validation_strategy($validation_strategy); |
|
423 | + } |
|
424 | + |
|
425 | + |
|
426 | + |
|
427 | + /** |
|
428 | + * The classname of the validation strategy to remove |
|
429 | + * |
|
430 | + * @param string $validation_strategy_classname |
|
431 | + */ |
|
432 | + public function remove_validation_strategy($validation_strategy_classname) |
|
433 | + { |
|
434 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
435 | + if ( |
|
436 | + $validation_strategy instanceof $validation_strategy_classname |
|
437 | + || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
438 | + ) { |
|
439 | + unset($this->_validation_strategies[$key]); |
|
440 | + } |
|
441 | + } |
|
442 | + } |
|
443 | + |
|
444 | + |
|
445 | + |
|
446 | + /** |
|
447 | + * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
448 | + * |
|
449 | + * @param array $validation_strategy_classnames |
|
450 | + * @return bool |
|
451 | + */ |
|
452 | + public function has_validation_strategy($validation_strategy_classnames) |
|
453 | + { |
|
454 | + $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
455 | + ? $validation_strategy_classnames |
|
456 | + : array($validation_strategy_classnames); |
|
457 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
458 | + if (in_array($key, $validation_strategy_classnames)) { |
|
459 | + return true; |
|
460 | + } |
|
461 | + } |
|
462 | + return false; |
|
463 | + } |
|
464 | + |
|
465 | + |
|
466 | + |
|
467 | + /** |
|
468 | + * Gets the HTML |
|
469 | + * |
|
470 | + * @return string |
|
471 | + */ |
|
472 | + public function get_html() |
|
473 | + { |
|
474 | + return $this->_parent_section->get_html_for_input($this); |
|
475 | + } |
|
476 | + |
|
477 | + |
|
478 | + |
|
479 | + /** |
|
480 | + * Gets the HTML for the input itself (no label or errors) according to the |
|
481 | + * input's display strategy |
|
482 | + * Makes sure the JS and CSS are enqueued for it |
|
483 | + * |
|
484 | + * @return string |
|
485 | + * @throws \EE_Error |
|
486 | + */ |
|
487 | + public function get_html_for_input() |
|
488 | + { |
|
489 | + return $this->_form_html_filter |
|
490 | + ? $this->_form_html_filter->filterHtml( |
|
491 | + $this->_get_display_strategy()->display(), |
|
492 | + $this |
|
493 | + ) |
|
494 | + : $this->_get_display_strategy()->display(); |
|
495 | + } |
|
496 | + |
|
497 | + |
|
498 | + |
|
499 | + /** |
|
500 | + * @return string |
|
501 | + */ |
|
502 | + public function html_other_attributes() |
|
503 | + { |
|
504 | + return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : ''; |
|
505 | + } |
|
506 | + |
|
507 | + |
|
508 | + |
|
509 | + /** |
|
510 | + * @param string $html_other_attributes |
|
511 | + */ |
|
512 | + public function set_html_other_attributes($html_other_attributes) |
|
513 | + { |
|
514 | + $this->_html_other_attributes = $html_other_attributes; |
|
515 | + } |
|
516 | + |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * Gets the HTML for displaying the label for this form input |
|
521 | + * according to the form section's layout strategy |
|
522 | + * |
|
523 | + * @return string |
|
524 | + */ |
|
525 | + public function get_html_for_label() |
|
526 | + { |
|
527 | + return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
528 | + } |
|
529 | + |
|
530 | + |
|
531 | + |
|
532 | + /** |
|
533 | + * Gets the HTML for displaying the errors section for this form input |
|
534 | + * according to the form section's layout strategy |
|
535 | + * |
|
536 | + * @return string |
|
537 | + */ |
|
538 | + public function get_html_for_errors() |
|
539 | + { |
|
540 | + return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
541 | + } |
|
542 | + |
|
543 | + |
|
544 | + |
|
545 | + /** |
|
546 | + * Gets the HTML for displaying the help text for this form input |
|
547 | + * according to the form section's layout strategy |
|
548 | + * |
|
549 | + * @return string |
|
550 | + */ |
|
551 | + public function get_html_for_help() |
|
552 | + { |
|
553 | + return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
554 | + } |
|
555 | + |
|
556 | + |
|
557 | + |
|
558 | + /** |
|
559 | + * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
560 | + * and returns whether or not the form input's submitted value is value |
|
561 | + * |
|
562 | + * @return boolean |
|
563 | + */ |
|
564 | + protected function _validate() |
|
565 | + { |
|
566 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
567 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
568 | + try { |
|
569 | + $validation_strategy->validate($this->normalized_value()); |
|
570 | + } catch (EE_Validation_Error $e) { |
|
571 | + $this->add_validation_error($e); |
|
572 | + } |
|
573 | + } |
|
574 | + } |
|
575 | + if ($this->get_validation_errors()) { |
|
576 | + return false; |
|
577 | + } else { |
|
578 | + return true; |
|
579 | + } |
|
580 | + } |
|
581 | + |
|
582 | + |
|
583 | + |
|
584 | + /** |
|
585 | + * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
586 | + * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
587 | + * |
|
588 | + * @param string $value |
|
589 | + * @return null|string |
|
590 | + */ |
|
591 | + private function _sanitize($value) |
|
592 | + { |
|
593 | + return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null; |
|
594 | + } |
|
595 | + |
|
596 | + |
|
597 | + |
|
598 | + /** |
|
599 | + * Picks out the form value that relates to this form input, |
|
600 | + * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
601 | + * Returns whether or not any validation errors occurred |
|
602 | + * |
|
603 | + * @param array $req_data like $_POST |
|
604 | + * @return boolean whether or not there was an error |
|
605 | + * @throws \EE_Error |
|
606 | + */ |
|
607 | + protected function _normalize($req_data) |
|
608 | + { |
|
609 | + //any existing validation errors don't apply so clear them |
|
610 | + $this->_validation_errors = array(); |
|
611 | + try { |
|
612 | + $raw_input = $this->find_form_data_for_this_section($req_data); |
|
613 | + //super simple sanitization for now |
|
614 | + if (is_array($raw_input)) { |
|
615 | + $raw_value = array(); |
|
616 | + foreach ($raw_input as $key => $value) { |
|
617 | + $raw_value[$key] = $this->_sanitize($value); |
|
618 | + } |
|
619 | + $this->_set_raw_value($raw_value); |
|
620 | + } else { |
|
621 | + $this->_set_raw_value($this->_sanitize($raw_input)); |
|
622 | + } |
|
623 | + //we want to mostly leave the input alone in case we need to re-display it to the user |
|
624 | + $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
625 | + } catch (EE_Validation_Error $e) { |
|
626 | + $this->add_validation_error($e); |
|
627 | + } |
|
628 | + } |
|
629 | + |
|
630 | + |
|
631 | + |
|
632 | + /** |
|
633 | + * @return string |
|
634 | + */ |
|
635 | + public function html_name() |
|
636 | + { |
|
637 | + $this->_set_default_html_name_if_empty(); |
|
638 | + return $this->_html_name; |
|
639 | + } |
|
640 | + |
|
641 | + |
|
642 | + |
|
643 | + /** |
|
644 | + * @return string |
|
645 | + */ |
|
646 | + public function html_label_id() |
|
647 | + { |
|
648 | + return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->_html_id . '-lbl'; |
|
649 | + } |
|
650 | + |
|
651 | + |
|
652 | + |
|
653 | + /** |
|
654 | + * @return string |
|
655 | + */ |
|
656 | + public function html_label_class() |
|
657 | + { |
|
658 | + return $this->_html_label_class; |
|
659 | + } |
|
660 | + |
|
661 | + |
|
662 | + |
|
663 | + /** |
|
664 | + * @return string |
|
665 | + */ |
|
666 | + public function html_label_style() |
|
667 | + { |
|
668 | + return $this->_html_label_style; |
|
669 | + } |
|
670 | + |
|
671 | + |
|
672 | + |
|
673 | + /** |
|
674 | + * @return string |
|
675 | + */ |
|
676 | + public function html_label_text() |
|
677 | + { |
|
678 | + return $this->_html_label_text; |
|
679 | + } |
|
680 | + |
|
681 | + |
|
682 | + |
|
683 | + /** |
|
684 | + * @return string |
|
685 | + */ |
|
686 | + public function html_help_text() |
|
687 | + { |
|
688 | + return $this->_html_help_text; |
|
689 | + } |
|
690 | + |
|
691 | + |
|
692 | + |
|
693 | + /** |
|
694 | + * @return string |
|
695 | + */ |
|
696 | + public function html_help_class() |
|
697 | + { |
|
698 | + return $this->_html_help_class; |
|
699 | + } |
|
700 | + |
|
701 | + |
|
702 | + |
|
703 | + /** |
|
704 | + * @return string |
|
705 | + */ |
|
706 | + public function html_help_style() |
|
707 | + { |
|
708 | + return $this->_html_style; |
|
709 | + } |
|
710 | + |
|
711 | + |
|
712 | + |
|
713 | + /** |
|
714 | + * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
715 | + * Please note that almost all client code should instead use the normalized_value; |
|
716 | + * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
717 | + * mostly by escaping quotes) |
|
718 | + * Note, we do not store the exact original value sent in the user's request because |
|
719 | + * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
720 | + * in which case, we would have stored the malicious content to our database. |
|
721 | + * |
|
722 | + * @return string |
|
723 | + */ |
|
724 | + public function raw_value() |
|
725 | + { |
|
726 | + return $this->_raw_value; |
|
727 | + } |
|
728 | + |
|
729 | + |
|
730 | + |
|
731 | + /** |
|
732 | + * Returns a string safe to usage in form inputs when displaying, because |
|
733 | + * it escapes all html entities |
|
734 | + * |
|
735 | + * @return string |
|
736 | + */ |
|
737 | + public function raw_value_in_form() |
|
738 | + { |
|
739 | + return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
740 | + } |
|
741 | + |
|
742 | + |
|
743 | + |
|
744 | + /** |
|
745 | + * returns the value after it's been sanitized, and then converted into it's proper type |
|
746 | + * in PHP. Eg, a string, an int, an array, |
|
747 | + * |
|
748 | + * @return mixed |
|
749 | + */ |
|
750 | + public function normalized_value() |
|
751 | + { |
|
752 | + return $this->_normalized_value; |
|
753 | + } |
|
754 | + |
|
755 | + |
|
756 | + |
|
757 | + /** |
|
758 | + * Returns the normalized value is a presentable way. By default this is just |
|
759 | + * the normalized value by itself, but it can be overridden for when that's not |
|
760 | + * the best thing to display |
|
761 | + * |
|
762 | + * @return string |
|
763 | + */ |
|
764 | + public function pretty_value() |
|
765 | + { |
|
766 | + return $this->_normalized_value; |
|
767 | + } |
|
768 | + |
|
769 | + |
|
770 | + |
|
771 | + /** |
|
772 | + * When generating the JS for the jquery validation rules like<br> |
|
773 | + * <code>$( "#myform" ).validate({ |
|
774 | + * rules: { |
|
775 | + * password: "required", |
|
776 | + * password_again: { |
|
777 | + * equalTo: "#password" |
|
778 | + * } |
|
779 | + * } |
|
780 | + * });</code> |
|
781 | + * if this field had the name 'password_again', it should return |
|
782 | + * <br><code>password_again: { |
|
783 | + * equalTo: "#password" |
|
784 | + * }</code> |
|
785 | + * |
|
786 | + * @return array |
|
787 | + */ |
|
788 | + public function get_jquery_validation_rules() |
|
789 | + { |
|
790 | + $jquery_validation_js = array(); |
|
791 | + $jquery_validation_rules = array(); |
|
792 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
793 | + $jquery_validation_rules = array_replace_recursive( |
|
794 | + $jquery_validation_rules, |
|
795 | + $validation_strategy->get_jquery_validation_rule_array() |
|
796 | + ); |
|
797 | + } |
|
798 | + if (! empty($jquery_validation_rules)) { |
|
799 | + foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
800 | + $jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules; |
|
801 | + } |
|
802 | + } |
|
803 | + return $jquery_validation_js; |
|
804 | + } |
|
805 | + |
|
806 | + |
|
807 | + |
|
808 | + /** |
|
809 | + * Sets the input's default value for use in displaying in the form. Note: value should be |
|
810 | + * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
811 | + * |
|
812 | + * @param mixed $value |
|
813 | + * @return void |
|
814 | + */ |
|
815 | + public function set_default($value) |
|
816 | + { |
|
817 | + $this->_set_normalized_value($value); |
|
818 | + $this->_set_raw_value($value); |
|
819 | + } |
|
820 | + |
|
821 | + |
|
822 | + |
|
823 | + /** |
|
824 | + * Sets the normalized value on this input |
|
825 | + * |
|
826 | + * @param mixed $value |
|
827 | + */ |
|
828 | + protected function _set_normalized_value($value) |
|
829 | + { |
|
830 | + $this->_normalized_value = $value; |
|
831 | + } |
|
832 | + |
|
833 | + |
|
834 | + |
|
835 | + /** |
|
836 | + * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
837 | + * |
|
838 | + * @param mixed $value |
|
839 | + */ |
|
840 | + protected function _set_raw_value($value) |
|
841 | + { |
|
842 | + $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
843 | + } |
|
844 | + |
|
845 | + |
|
846 | + |
|
847 | + /** |
|
848 | + * Sets the HTML label text after it has already been defined |
|
849 | + * |
|
850 | + * @param string $label |
|
851 | + * @return void |
|
852 | + */ |
|
853 | + public function set_html_label_text($label) |
|
854 | + { |
|
855 | + $this->_html_label_text = $label; |
|
856 | + } |
|
857 | + |
|
858 | + |
|
859 | + |
|
860 | + /** |
|
861 | + * Sets whether or not this field is required, and adjusts the validation strategy. |
|
862 | + * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
863 | + * please add it as a validation strategy using add_validation_strategy as normal |
|
864 | + * |
|
865 | + * @param boolean $required boolean |
|
866 | + * @param null $required_text |
|
867 | + */ |
|
868 | + public function set_required($required = true, $required_text = null) |
|
869 | + { |
|
870 | + $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
871 | + //whether $required is a string or a boolean, we want to add a required validation strategy |
|
872 | + if ($required) { |
|
873 | + $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
874 | + } else { |
|
875 | + $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
876 | + } |
|
877 | + $this->_required = $required; |
|
878 | + } |
|
879 | + |
|
880 | + |
|
881 | + |
|
882 | + /** |
|
883 | + * Returns whether or not this field is required |
|
884 | + * |
|
885 | + * @return boolean |
|
886 | + */ |
|
887 | + public function required() |
|
888 | + { |
|
889 | + return $this->_required; |
|
890 | + } |
|
891 | + |
|
892 | + |
|
893 | + |
|
894 | + /** |
|
895 | + * @param string $required_css_class |
|
896 | + */ |
|
897 | + public function set_required_css_class($required_css_class) |
|
898 | + { |
|
899 | + $this->_required_css_class = $required_css_class; |
|
900 | + } |
|
901 | + |
|
902 | + |
|
903 | + |
|
904 | + /** |
|
905 | + * @return string |
|
906 | + */ |
|
907 | + public function required_css_class() |
|
908 | + { |
|
909 | + return $this->_required_css_class; |
|
910 | + } |
|
911 | + |
|
912 | + |
|
913 | + |
|
914 | + /** |
|
915 | + * Sets the help text, in case |
|
916 | + * |
|
917 | + * @param string $text |
|
918 | + */ |
|
919 | + public function set_html_help_text($text) |
|
920 | + { |
|
921 | + $this->_html_help_text = $text; |
|
922 | + } |
|
923 | + |
|
924 | + |
|
925 | + |
|
926 | + /** |
|
927 | + * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
928 | + * input. If there is any kind of sensitive data removal on this input, we clear |
|
929 | + * out the raw value completely |
|
930 | + * |
|
931 | + * @return void |
|
932 | + */ |
|
933 | + public function clean_sensitive_data() |
|
934 | + { |
|
935 | + //if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
936 | + //if we need more logic than this we'll make a strategy for it |
|
937 | + if ($this->_sensitive_data_removal_strategy |
|
938 | + && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
939 | + ) { |
|
940 | + $this->_set_raw_value(null); |
|
941 | + } |
|
942 | + //and clean the normalized value according to the appropriate strategy |
|
943 | + $this->_set_normalized_value( |
|
944 | + $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
945 | + $this->_normalized_value |
|
946 | + ) |
|
947 | + ); |
|
948 | + } |
|
949 | + |
|
950 | + |
|
951 | + |
|
952 | + /** |
|
953 | + * @param bool $primary |
|
954 | + * @param string $button_size |
|
955 | + * @param string $other_attributes |
|
956 | + */ |
|
957 | + public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
958 | + { |
|
959 | + $button_css_attributes = 'button'; |
|
960 | + $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
961 | + switch ($button_size) { |
|
962 | + case 'xs' : |
|
963 | + case 'extra-small' : |
|
964 | + $button_css_attributes .= ' button-xs'; |
|
965 | + break; |
|
966 | + case 'sm' : |
|
967 | + case 'small' : |
|
968 | + $button_css_attributes .= ' button-sm'; |
|
969 | + break; |
|
970 | + case 'lg' : |
|
971 | + case 'large' : |
|
972 | + $button_css_attributes .= ' button-lg'; |
|
973 | + break; |
|
974 | + case 'block' : |
|
975 | + $button_css_attributes .= ' button-block'; |
|
976 | + break; |
|
977 | + case 'md' : |
|
978 | + case 'medium' : |
|
979 | + default : |
|
980 | + $button_css_attributes .= ''; |
|
981 | + } |
|
982 | + $this->_button_css_attributes .= ! empty($other_attributes) |
|
983 | + ? $button_css_attributes . ' ' . $other_attributes |
|
984 | + : $button_css_attributes; |
|
985 | + } |
|
986 | + |
|
987 | + |
|
988 | + |
|
989 | + /** |
|
990 | + * @return string |
|
991 | + */ |
|
992 | + public function button_css_attributes() |
|
993 | + { |
|
994 | + if (empty($this->_button_css_attributes)) { |
|
995 | + $this->set_button_css_attributes(); |
|
996 | + } |
|
997 | + return $this->_button_css_attributes; |
|
998 | + } |
|
999 | + |
|
1000 | + |
|
1001 | + |
|
1002 | + /** |
|
1003 | + * find_form_data_for_this_section |
|
1004 | + * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1005 | + * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1006 | + * then it's value should be in $_REQUEST at $_REQUEST['my_form']['subform']['form_input_1']. |
|
1007 | + * (If that doesn't exist, we also check for this subsection's name |
|
1008 | + * at the TOP LEVEL of the request data. Eg $_REQUEST['form_input_1'].) |
|
1009 | + * This function finds its value in the form. |
|
1010 | + * |
|
1011 | + * @param array $req_data |
|
1012 | + * @return mixed whatever the raw value of this form section is in the request data |
|
1013 | + * @throws \EE_Error |
|
1014 | + */ |
|
1015 | + public function find_form_data_for_this_section($req_data) |
|
1016 | + { |
|
1017 | + // break up the html name by "[]" |
|
1018 | + if (strpos($this->html_name(), '[') !== false) { |
|
1019 | + $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1020 | + } else { |
|
1021 | + $before_any_brackets = $this->html_name(); |
|
1022 | + } |
|
1023 | + // grab all of the segments |
|
1024 | + preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1025 | + if (isset($matches[1]) && is_array($matches[1])) { |
|
1026 | + $name_parts = $matches[1]; |
|
1027 | + array_unshift($name_parts, $before_any_brackets); |
|
1028 | + } else { |
|
1029 | + $name_parts = array($before_any_brackets); |
|
1030 | + } |
|
1031 | + // now get the value for the input |
|
1032 | + $value = $this->_find_form_data_for_this_section_using_name_parts($name_parts, $req_data); |
|
1033 | + // check if this thing's name is at the TOP level of the request data |
|
1034 | + if ($value === null && isset($req_data[$this->name()])) { |
|
1035 | + $value = $req_data[$this->name()]; |
|
1036 | + } |
|
1037 | + return $value; |
|
1038 | + } |
|
1039 | + |
|
1040 | + |
|
1041 | + |
|
1042 | + /** |
|
1043 | + * @param array $html_name_parts |
|
1044 | + * @param array $req_data |
|
1045 | + * @return array | NULL |
|
1046 | + */ |
|
1047 | + public function _find_form_data_for_this_section_using_name_parts($html_name_parts, $req_data) |
|
1048 | + { |
|
1049 | + $first_part_to_consider = array_shift($html_name_parts); |
|
1050 | + if (isset($req_data[$first_part_to_consider])) { |
|
1051 | + if (empty($html_name_parts)) { |
|
1052 | + return $req_data[$first_part_to_consider]; |
|
1053 | + } else { |
|
1054 | + return $this->_find_form_data_for_this_section_using_name_parts( |
|
1055 | + $html_name_parts, |
|
1056 | + $req_data[$first_part_to_consider] |
|
1057 | + ); |
|
1058 | + } |
|
1059 | + } else { |
|
1060 | + return null; |
|
1061 | + } |
|
1062 | + } |
|
1063 | + |
|
1064 | + |
|
1065 | + |
|
1066 | + /** |
|
1067 | + * Checks if this form input's data is in the request data |
|
1068 | + * |
|
1069 | + * @param array $req_data like $_POST |
|
1070 | + * @return boolean |
|
1071 | + * @throws \EE_Error |
|
1072 | + */ |
|
1073 | + public function form_data_present_in($req_data = null) |
|
1074 | + { |
|
1075 | + if ($req_data === null) { |
|
1076 | + $req_data = $_POST; |
|
1077 | + } |
|
1078 | + $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1079 | + if ($checked_value !== null) { |
|
1080 | + return true; |
|
1081 | + } else { |
|
1082 | + return false; |
|
1083 | + } |
|
1084 | + } |
|
1085 | + |
|
1086 | + |
|
1087 | + |
|
1088 | + /** |
|
1089 | + * Overrides parent to add js data from validation and display strategies |
|
1090 | + * |
|
1091 | + * @param array $form_other_js_data |
|
1092 | + * @return array |
|
1093 | + */ |
|
1094 | + public function get_other_js_data($form_other_js_data = array()) |
|
1095 | + { |
|
1096 | + $form_other_js_data = $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1097 | + return $form_other_js_data; |
|
1098 | + } |
|
1099 | + |
|
1100 | + |
|
1101 | + |
|
1102 | + /** |
|
1103 | + * Gets other JS data for localization from this input's strategies, like |
|
1104 | + * the validation strategies and the display strategy |
|
1105 | + * |
|
1106 | + * @param array $form_other_js_data |
|
1107 | + * @return array |
|
1108 | + */ |
|
1109 | + public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
1110 | + { |
|
1111 | + $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1112 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1113 | + $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1114 | + } |
|
1115 | + return $form_other_js_data; |
|
1116 | + } |
|
1117 | + |
|
1118 | + |
|
1119 | + |
|
1120 | + /** |
|
1121 | + * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1122 | + * |
|
1123 | + * @return void |
|
1124 | + */ |
|
1125 | + public function enqueue_js() |
|
1126 | + { |
|
1127 | + //ask our display strategy and validation strategies if they have js to enqueue |
|
1128 | + $this->enqueue_js_from_strategies(); |
|
1129 | + } |
|
1130 | + |
|
1131 | + |
|
1132 | + |
|
1133 | + /** |
|
1134 | + * Tells strategies when its ok to enqueue their js and css |
|
1135 | + * |
|
1136 | + * @return void |
|
1137 | + */ |
|
1138 | + public function enqueue_js_from_strategies() |
|
1139 | + { |
|
1140 | + $this->get_display_strategy()->enqueue_js(); |
|
1141 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1142 | + $validation_strategy->enqueue_js(); |
|
1143 | + } |
|
1144 | + } |
|
1145 | 1145 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | * the interaction of EE_Transaction and EE_Registration model objects |
8 | 8 | * Provides methods for manipulating and processing changes to an EE_Transaction |
9 | 9 | * and it's related EE_Registrations with regards to the checkout/registration process |
10 | - |
|
11 | -* |
|
10 | + * |
|
12 | 11 | *@package Event Espresso |
13 | 12 | * @subpackage core |
14 | 13 | * @author Brent Christensen |
@@ -177,24 +176,24 @@ discard block |
||
177 | 176 | |
178 | 177 | |
179 | 178 | |
180 | - /** |
|
181 | - * update_transaction_and_registrations_after_checkout_or_payment |
|
182 | - * cycles thru related registrations and calls update_registration_after_checkout_or_payment() on each |
|
183 | - * |
|
184 | - * @param EE_Transaction $transaction |
|
185 | - * @param \EE_Payment | NULL $payment |
|
186 | - * @param array $registration_query_params array of query WHERE params to use |
|
187 | - * when retrieving cached registrations from a transaction |
|
188 | - * @param bool $trigger_notifications whether or not to call |
|
189 | - * \EE_Registration_Processor::trigger_registration_update_notifications() |
|
190 | - * @return array |
|
191 | - * @throws \EE_Error |
|
192 | - */ |
|
179 | + /** |
|
180 | + * update_transaction_and_registrations_after_checkout_or_payment |
|
181 | + * cycles thru related registrations and calls update_registration_after_checkout_or_payment() on each |
|
182 | + * |
|
183 | + * @param EE_Transaction $transaction |
|
184 | + * @param \EE_Payment | NULL $payment |
|
185 | + * @param array $registration_query_params array of query WHERE params to use |
|
186 | + * when retrieving cached registrations from a transaction |
|
187 | + * @param bool $trigger_notifications whether or not to call |
|
188 | + * \EE_Registration_Processor::trigger_registration_update_notifications() |
|
189 | + * @return array |
|
190 | + * @throws \EE_Error |
|
191 | + */ |
|
193 | 192 | public function update_transaction_and_registrations_after_checkout_or_payment( |
194 | 193 | EE_Transaction $transaction, |
195 | 194 | $payment = null, |
196 | 195 | $registration_query_params = array(), |
197 | - $trigger_notifications = true |
|
196 | + $trigger_notifications = true |
|
198 | 197 | ) { |
199 | 198 | // make sure some query params are set for retrieving registrations |
200 | 199 | $this->_set_registration_query_params( $registration_query_params ); |
@@ -224,15 +223,15 @@ discard block |
||
224 | 223 | $update_params |
225 | 224 | ); |
226 | 225 | if ($trigger_notifications) { |
227 | - // send messages |
|
228 | - /** @type EE_Registration_Processor $registration_processor */ |
|
229 | - $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
230 | - $registration_processor->trigger_registration_update_notifications( |
|
231 | - $transaction->primary_registration(), |
|
232 | - $update_params |
|
233 | - ); |
|
234 | - } |
|
235 | - do_action( |
|
226 | + // send messages |
|
227 | + /** @type EE_Registration_Processor $registration_processor */ |
|
228 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
229 | + $registration_processor->trigger_registration_update_notifications( |
|
230 | + $transaction->primary_registration(), |
|
231 | + $update_params |
|
232 | + ); |
|
233 | + } |
|
234 | + do_action( |
|
236 | 235 | 'AHEE__EE_Transaction_Processor__update_transaction_and_registrations_after_checkout_or_payment', |
237 | 236 | $transaction, |
238 | 237 | $update_params |
@@ -242,17 +241,17 @@ discard block |
||
242 | 241 | |
243 | 242 | |
244 | 243 | |
245 | - /** |
|
246 | - * update_transaction_after_registration_reopened |
|
247 | - * readjusts TXN and Line Item totals after a registration is changed from |
|
248 | - * cancelled or declined to another reg status such as pending payment or approved |
|
249 | - * |
|
250 | - * @param \EE_Registration $registration |
|
251 | - * @param array $closed_reg_statuses |
|
252 | - * @param bool $update_txn |
|
253 | - * @return bool |
|
254 | - * @throws \EE_Error |
|
255 | - */ |
|
244 | + /** |
|
245 | + * update_transaction_after_registration_reopened |
|
246 | + * readjusts TXN and Line Item totals after a registration is changed from |
|
247 | + * cancelled or declined to another reg status such as pending payment or approved |
|
248 | + * |
|
249 | + * @param \EE_Registration $registration |
|
250 | + * @param array $closed_reg_statuses |
|
251 | + * @param bool $update_txn |
|
252 | + * @return bool |
|
253 | + * @throws \EE_Error |
|
254 | + */ |
|
256 | 255 | public function update_transaction_after_reinstating_canceled_registration( |
257 | 256 | EE_Registration $registration, |
258 | 257 | $closed_reg_statuses = array(), |
@@ -314,15 +313,15 @@ discard block |
||
314 | 313 | try { |
315 | 314 | $transaction = $this->get_transaction_for_registration( $registration ); |
316 | 315 | if ( |
317 | - apply_filters( |
|
318 | - 'FHEE__EE_Transaction_Processor__update_transaction_after_canceled_or_declined_registration__cancel_ticket_line_item', |
|
319 | - true, |
|
320 | - $registration, |
|
321 | - $transaction |
|
322 | - ) |
|
323 | - ){ |
|
324 | - $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration( $transaction, $registration ); |
|
325 | - EEH_Line_Item::cancel_ticket_line_item( $ticket_line_item ); |
|
316 | + apply_filters( |
|
317 | + 'FHEE__EE_Transaction_Processor__update_transaction_after_canceled_or_declined_registration__cancel_ticket_line_item', |
|
318 | + true, |
|
319 | + $registration, |
|
320 | + $transaction |
|
321 | + ) |
|
322 | + ){ |
|
323 | + $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration( $transaction, $registration ); |
|
324 | + EEH_Line_Item::cancel_ticket_line_item( $ticket_line_item ); |
|
326 | 325 | } |
327 | 326 | } catch ( EE_Error $e ) { |
328 | 327 | EE_Error::add_error( |
@@ -532,7 +531,7 @@ discard block |
||
532 | 531 | foreach ( $available_payment_methods as $available_payment_method ) { |
533 | 532 | if ( |
534 | 533 | $available_payment_method instanceof EE_Payment_Method |
535 | - && $available_payment_method->open_by_default() |
|
534 | + && $available_payment_method->open_by_default() |
|
536 | 535 | ) { |
537 | 536 | $PMD_ID = $available_payment_method->ID(); |
538 | 537 | break; |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
2 | -EE_Registry::instance()->load_class( 'Processor_Base' ); |
|
2 | +EE_Registry::instance()->load_class('Processor_Base'); |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * Class EE_Transaction_Processor |
@@ -50,10 +50,10 @@ discard block |
||
50 | 50 | * @param array $registration_query_params |
51 | 51 | *@return EE_Transaction_Processor instance |
52 | 52 | */ |
53 | - public static function instance( $registration_query_params = array() ) { |
|
53 | + public static function instance($registration_query_params = array()) { |
|
54 | 54 | // check if class object is instantiated |
55 | - if ( ! self::$_instance instanceof EE_Transaction_Processor ) { |
|
56 | - self::$_instance = new self( $registration_query_params ); |
|
55 | + if ( ! self::$_instance instanceof EE_Transaction_Processor) { |
|
56 | + self::$_instance = new self($registration_query_params); |
|
57 | 57 | } |
58 | 58 | return self::$_instance; |
59 | 59 | } |
@@ -63,9 +63,9 @@ discard block |
||
63 | 63 | /** |
64 | 64 | * @param array $registration_query_params |
65 | 65 | */ |
66 | - private function __construct( $registration_query_params = array() ) { |
|
66 | + private function __construct($registration_query_params = array()) { |
|
67 | 67 | // make sure some query params are set for retrieving registrations |
68 | - $this->_set_registration_query_params( $registration_query_params ); |
|
68 | + $this->_set_registration_query_params($registration_query_params); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | |
@@ -74,8 +74,8 @@ discard block |
||
74 | 74 | * @access private |
75 | 75 | * @param array $registration_query_params |
76 | 76 | */ |
77 | - private function _set_registration_query_params( $registration_query_params ) { |
|
78 | - $this->_registration_query_params = ! empty( $registration_query_params ) ? $registration_query_params : array( 'order_by' => array( 'REG_count' => 'ASC' )); |
|
77 | + private function _set_registration_query_params($registration_query_params) { |
|
78 | + $this->_registration_query_params = ! empty($registration_query_params) ? $registration_query_params : array('order_by' => array('REG_count' => 'ASC')); |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | |
@@ -104,10 +104,10 @@ discard block |
||
104 | 104 | ); |
105 | 105 | // send messages |
106 | 106 | /** @type EE_Registration_Processor $registration_processor */ |
107 | - $registration_processor = EE_Registry::instance()->load_class( 'Registration_Processor' ); |
|
107 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
108 | 108 | $registration_processor->trigger_registration_update_notifications( |
109 | 109 | $transaction->primary_registration(), |
110 | - array( 'manually_updated' => true ) |
|
110 | + array('manually_updated' => true) |
|
111 | 111 | ); |
112 | 112 | do_action( |
113 | 113 | 'AHEE__EE_Transaction_Processor__manually_update_registration_statuses', |
@@ -197,13 +197,13 @@ discard block |
||
197 | 197 | $trigger_notifications = true |
198 | 198 | ) { |
199 | 199 | // make sure some query params are set for retrieving registrations |
200 | - $this->_set_registration_query_params( $registration_query_params ); |
|
200 | + $this->_set_registration_query_params($registration_query_params); |
|
201 | 201 | // get final reg step status |
202 | 202 | $finalized = $transaction->final_reg_step_completed(); |
203 | 203 | // if the 'finalize_registration' step has been initiated (has a timestamp) |
204 | 204 | // but has not yet been fully completed (TRUE) |
205 | - if ( is_int( $finalized ) && $finalized !== false && $finalized !== true ) { |
|
206 | - $transaction->set_reg_step_completed( 'finalize_registration' ); |
|
205 | + if (is_int($finalized) && $finalized !== false && $finalized !== true) { |
|
206 | + $transaction->set_reg_step_completed('finalize_registration'); |
|
207 | 207 | $finalized = true; |
208 | 208 | } |
209 | 209 | $transaction->save(); |
@@ -259,22 +259,22 @@ discard block |
||
259 | 259 | $update_txn = true |
260 | 260 | ) { |
261 | 261 | // these reg statuses should not be considered in any calculations involving monies owing |
262 | - $closed_reg_statuses = ! empty( $closed_reg_statuses ) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
263 | - if ( in_array( $registration->status_ID(), $closed_reg_statuses, true ) ) { |
|
262 | + $closed_reg_statuses = ! empty($closed_reg_statuses) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
263 | + if (in_array($registration->status_ID(), $closed_reg_statuses, true)) { |
|
264 | 264 | return false; |
265 | 265 | } |
266 | 266 | try { |
267 | - $transaction = $this->get_transaction_for_registration( $registration ); |
|
267 | + $transaction = $this->get_transaction_for_registration($registration); |
|
268 | 268 | $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration( |
269 | 269 | $transaction, |
270 | 270 | $registration |
271 | 271 | ); |
272 | 272 | // un-cancel the ticket |
273 | - $success = EEH_Line_Item::reinstate_canceled_ticket_line_item( $ticket_line_item ); |
|
274 | - } catch ( EE_Error $e ) { |
|
273 | + $success = EEH_Line_Item::reinstate_canceled_ticket_line_item($ticket_line_item); |
|
274 | + } catch (EE_Error $e) { |
|
275 | 275 | EE_Error::add_error( |
276 | 276 | sprintf( |
277 | - __( 'The Ticket Line Item for Registration %1$d could not be reinstated because :%2$s%3$s', 'event_espresso' ), |
|
277 | + __('The Ticket Line Item for Registration %1$d could not be reinstated because :%2$s%3$s', 'event_espresso'), |
|
278 | 278 | $registration->ID(), |
279 | 279 | '<br />', |
280 | 280 | $e->getMessage() |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | ); |
284 | 284 | return false; |
285 | 285 | } |
286 | - if ( $update_txn ) { |
|
286 | + if ($update_txn) { |
|
287 | 287 | return $transaction->save() ? $success : false; |
288 | 288 | } |
289 | 289 | return $success; |
@@ -307,12 +307,12 @@ discard block |
||
307 | 307 | $update_txn = true |
308 | 308 | ) { |
309 | 309 | // these reg statuses should not be considered in any calculations involving monies owing |
310 | - $closed_reg_statuses = ! empty( $closed_reg_statuses ) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
311 | - if ( ! in_array( $registration->status_ID(), $closed_reg_statuses, true ) ) { |
|
310 | + $closed_reg_statuses = ! empty($closed_reg_statuses) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
311 | + if ( ! in_array($registration->status_ID(), $closed_reg_statuses, true)) { |
|
312 | 312 | return false; |
313 | 313 | } |
314 | 314 | try { |
315 | - $transaction = $this->get_transaction_for_registration( $registration ); |
|
315 | + $transaction = $this->get_transaction_for_registration($registration); |
|
316 | 316 | if ( |
317 | 317 | apply_filters( |
318 | 318 | 'FHEE__EE_Transaction_Processor__update_transaction_after_canceled_or_declined_registration__cancel_ticket_line_item', |
@@ -320,14 +320,14 @@ discard block |
||
320 | 320 | $registration, |
321 | 321 | $transaction |
322 | 322 | ) |
323 | - ){ |
|
324 | - $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration( $transaction, $registration ); |
|
325 | - EEH_Line_Item::cancel_ticket_line_item( $ticket_line_item ); |
|
323 | + ) { |
|
324 | + $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration($transaction, $registration); |
|
325 | + EEH_Line_Item::cancel_ticket_line_item($ticket_line_item); |
|
326 | 326 | } |
327 | - } catch ( EE_Error $e ) { |
|
327 | + } catch (EE_Error $e) { |
|
328 | 328 | EE_Error::add_error( |
329 | 329 | sprintf( |
330 | - __( 'The Ticket Line Item for Registration %1$d could not be cancelled because :%2$s%3$s', 'event_espresso' ), |
|
330 | + __('The Ticket Line Item for Registration %1$d could not be cancelled because :%2$s%3$s', 'event_espresso'), |
|
331 | 331 | $registration->ID(), |
332 | 332 | '<br />', |
333 | 333 | $e->getMessage() |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | ); |
337 | 337 | return false; |
338 | 338 | } |
339 | - if ( $update_txn ) { |
|
339 | + if ($update_txn) { |
|
340 | 340 | return $transaction->save() ? true : false; |
341 | 341 | } |
342 | 342 | return true; |
@@ -352,12 +352,12 @@ discard block |
||
352 | 352 | * @return EE_Transaction |
353 | 353 | * @throws EE_Error |
354 | 354 | */ |
355 | - public function get_transaction_for_registration( EE_Registration $registration ) { |
|
355 | + public function get_transaction_for_registration(EE_Registration $registration) { |
|
356 | 356 | $transaction = $registration->transaction(); |
357 | - if ( ! $transaction instanceof EE_Transaction ) { |
|
357 | + if ( ! $transaction instanceof EE_Transaction) { |
|
358 | 358 | throw new EE_Error( |
359 | 359 | sprintf( |
360 | - __( 'The Transaction for Registration %1$d was not found or is invalid.', 'event_espresso' ), |
|
360 | + __('The Transaction for Registration %1$d was not found or is invalid.', 'event_espresso'), |
|
361 | 361 | $registration->ID() |
362 | 362 | ) |
363 | 363 | ); |
@@ -380,16 +380,16 @@ discard block |
||
380 | 380 | EE_Transaction $transaction, |
381 | 381 | EE_Registration $registration |
382 | 382 | ) { |
383 | - EE_Registry::instance()->load_helper( 'Line_Item' ); |
|
383 | + EE_Registry::instance()->load_helper('Line_Item'); |
|
384 | 384 | $ticket_line_item = EEM_Line_Item::instance()->get_ticket_line_item_for_transaction( |
385 | 385 | $transaction->ID(), |
386 | 386 | $registration->ticket_ID() |
387 | 387 | ); |
388 | - if ( ! $ticket_line_item instanceof EE_Line_Item ) { |
|
388 | + if ( ! $ticket_line_item instanceof EE_Line_Item) { |
|
389 | 389 | throw new EE_Error( |
390 | 390 | sprintf( |
391 | - __( 'The Line Item for Transaction %1$d and Ticket %2$d was not found or is invalid.', |
|
392 | - 'event_espresso' ), |
|
391 | + __('The Line Item for Transaction %1$d and Ticket %2$d was not found or is invalid.', |
|
392 | + 'event_espresso'), |
|
393 | 393 | $transaction->ID(), |
394 | 394 | $registration->ticket_ID() |
395 | 395 | ) |
@@ -422,22 +422,22 @@ discard block |
||
422 | 422 | $update_txn = true |
423 | 423 | ) { |
424 | 424 | // make sure some query params are set for retrieving registrations |
425 | - $this->_set_registration_query_params( $registration_query_params ); |
|
425 | + $this->_set_registration_query_params($registration_query_params); |
|
426 | 426 | // these reg statuses should not be considered in any calculations involving monies owing |
427 | - $closed_reg_statuses = ! empty( $closed_reg_statuses ) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
427 | + $closed_reg_statuses = ! empty($closed_reg_statuses) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
428 | 428 | // loop through cached registrations |
429 | - foreach ( $transaction->registrations( $this->_registration_query_params ) as $registration ) { |
|
429 | + foreach ($transaction->registrations($this->_registration_query_params) as $registration) { |
|
430 | 430 | if ( |
431 | 431 | $registration instanceof EE_Registration |
432 | - && ! in_array( $registration->status_ID(), $closed_reg_statuses ) |
|
432 | + && ! in_array($registration->status_ID(), $closed_reg_statuses) |
|
433 | 433 | ) { |
434 | 434 | return false; |
435 | 435 | } |
436 | 436 | } |
437 | - if ( in_array( $new_TXN_status, EEM_Transaction::txn_status_array() ) ) { |
|
438 | - $transaction->set_status( $new_TXN_status ); |
|
437 | + if (in_array($new_TXN_status, EEM_Transaction::txn_status_array())) { |
|
438 | + $transaction->set_status($new_TXN_status); |
|
439 | 439 | } |
440 | - if ( $update_txn ) { |
|
440 | + if ($update_txn) { |
|
441 | 441 | return $transaction->save() ? true : false; |
442 | 442 | } |
443 | 443 | return true; |
@@ -466,22 +466,22 @@ discard block |
||
466 | 466 | ) { |
467 | 467 | $response = false; |
468 | 468 | /** @type EE_Registration_Processor $registration_processor */ |
469 | - $registration_processor = EE_Registry::instance()->load_class( 'Registration_Processor' ); |
|
469 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
470 | 470 | // check that method exists |
471 | - if ( ! method_exists( $registration_processor, $method_name )) { |
|
472 | - throw new EE_Error( __( 'Method does not exist.', 'event_espresso' )); |
|
471 | + if ( ! method_exists($registration_processor, $method_name)) { |
|
472 | + throw new EE_Error(__('Method does not exist.', 'event_espresso')); |
|
473 | 473 | } |
474 | 474 | // make sure some query params are set for retrieving registrations |
475 | - $this->_set_registration_query_params( $registration_query_params ); |
|
475 | + $this->_set_registration_query_params($registration_query_params); |
|
476 | 476 | // loop through cached registrations |
477 | - foreach ( $transaction->registrations( $this->_registration_query_params ) as $registration ) { |
|
478 | - if ( $registration instanceof EE_Registration ) { |
|
479 | - if ( $additional_param ) { |
|
480 | - $response = $registration_processor->{$method_name}( $registration, $additional_param ) |
|
477 | + foreach ($transaction->registrations($this->_registration_query_params) as $registration) { |
|
478 | + if ($registration instanceof EE_Registration) { |
|
479 | + if ($additional_param) { |
|
480 | + $response = $registration_processor->{$method_name}($registration, $additional_param) |
|
481 | 481 | ? true |
482 | 482 | : $response; |
483 | 483 | } else { |
484 | - $response = $registration_processor->{$method_name}( $registration ) |
|
484 | + $response = $registration_processor->{$method_name}($registration) |
|
485 | 485 | ? true |
486 | 486 | : $response; |
487 | 487 | } |
@@ -508,28 +508,28 @@ discard block |
||
508 | 508 | public function set_transaction_payment_method_based_on_registration_statuses( |
509 | 509 | EE_Registration $edited_registration |
510 | 510 | ) { |
511 | - if ( $edited_registration instanceof EE_Registration ) { |
|
511 | + if ($edited_registration instanceof EE_Registration) { |
|
512 | 512 | $transaction = $edited_registration->transaction(); |
513 | - if ( $transaction instanceof EE_Transaction ) { |
|
513 | + if ($transaction instanceof EE_Transaction) { |
|
514 | 514 | $all_not_approved = true; |
515 | - foreach ( $transaction->registrations() as $registration ) { |
|
516 | - if ( $registration instanceof EE_Registration ) { |
|
515 | + foreach ($transaction->registrations() as $registration) { |
|
516 | + if ($registration instanceof EE_Registration) { |
|
517 | 517 | // if any REG != "Not Approved" then toggle to false |
518 | 518 | $all_not_approved = $registration->is_not_approved() ? $all_not_approved : false; |
519 | 519 | } |
520 | 520 | } |
521 | 521 | // if ALL Registrations are "Not Approved" |
522 | - if ( $all_not_approved ) { |
|
523 | - $transaction->set_payment_method_ID( null ); |
|
522 | + if ($all_not_approved) { |
|
523 | + $transaction->set_payment_method_ID(null); |
|
524 | 524 | $transaction->save(); |
525 | 525 | } else { |
526 | 526 | $available_payment_methods = EEM_Payment_Method::instance()->get_all_for_transaction( |
527 | 527 | $transaction, |
528 | 528 | EEM_Payment_Method::scope_cart |
529 | 529 | ); |
530 | - if ( ! empty( $available_payment_methods ) ) { |
|
530 | + if ( ! empty($available_payment_methods)) { |
|
531 | 531 | $PMD_ID = 0; |
532 | - foreach ( $available_payment_methods as $available_payment_method ) { |
|
532 | + foreach ($available_payment_methods as $available_payment_method) { |
|
533 | 533 | if ( |
534 | 534 | $available_payment_method instanceof EE_Payment_Method |
535 | 535 | && $available_payment_method->open_by_default() |
@@ -538,22 +538,22 @@ discard block |
||
538 | 538 | break; |
539 | 539 | } |
540 | 540 | } |
541 | - if ( ! $PMD_ID ) { |
|
542 | - $first_payment_method = reset( $available_payment_methods ); |
|
543 | - if ( $first_payment_method instanceof EE_Payment_Method ) { |
|
541 | + if ( ! $PMD_ID) { |
|
542 | + $first_payment_method = reset($available_payment_methods); |
|
543 | + if ($first_payment_method instanceof EE_Payment_Method) { |
|
544 | 544 | $PMD_ID = $first_payment_method->ID(); |
545 | 545 | } else { |
546 | 546 | EE_Error::add_error( |
547 | - __( 'A valid Payment Method could not be determined. Please ensure that at least one Payment Method is activated.', 'event_espresso' ), |
|
547 | + __('A valid Payment Method could not be determined. Please ensure that at least one Payment Method is activated.', 'event_espresso'), |
|
548 | 548 | __FILE__, __LINE__, __FUNCTION__ |
549 | 549 | ); |
550 | 550 | } |
551 | 551 | } |
552 | - $transaction->set_payment_method_ID( $PMD_ID ); |
|
552 | + $transaction->set_payment_method_ID($PMD_ID); |
|
553 | 553 | $transaction->save(); |
554 | 554 | } else { |
555 | 555 | EE_Error::add_error( |
556 | - __( 'Please activate at least one Payment Method in order for things to operate correctly.', 'event_espresso' ), |
|
556 | + __('Please activate at least one Payment Method in order for things to operate correctly.', 'event_espresso'), |
|
557 | 557 | __FILE__, __LINE__, __FUNCTION__ |
558 | 558 | ); |
559 | 559 | } |
@@ -590,7 +590,7 @@ discard block |
||
590 | 590 | * @deprecated 4.9.12 |
591 | 591 | * @param string $old_txn_status |
592 | 592 | */ |
593 | - public function set_old_txn_status( $old_txn_status ) { |
|
593 | + public function set_old_txn_status($old_txn_status) { |
|
594 | 594 | EE_Error::doing_it_wrong( |
595 | 595 | __METHOD__, |
596 | 596 | esc_html__( |
@@ -600,7 +600,7 @@ discard block |
||
600 | 600 | '4.9.12' |
601 | 601 | ); |
602 | 602 | // only set the first time |
603 | - if ( $this->_old_txn_status === null ) { |
|
603 | + if ($this->_old_txn_status === null) { |
|
604 | 604 | $this->_old_txn_status = $old_txn_status; |
605 | 605 | } |
606 | 606 | } |
@@ -629,7 +629,7 @@ discard block |
||
629 | 629 | * @deprecated 4.9.12 |
630 | 630 | * @param string $new_txn_status |
631 | 631 | */ |
632 | - public function set_new_txn_status( $new_txn_status ) { |
|
632 | + public function set_new_txn_status($new_txn_status) { |
|
633 | 633 | EE_Error::doing_it_wrong( |
634 | 634 | __METHOD__, |
635 | 635 | esc_html__( |
@@ -673,7 +673,7 @@ discard block |
||
673 | 673 | * @param EE_Transaction $transaction |
674 | 674 | * @return boolean |
675 | 675 | */ |
676 | - public function all_reg_steps_completed( EE_Transaction $transaction ) { |
|
676 | + public function all_reg_steps_completed(EE_Transaction $transaction) { |
|
677 | 677 | EE_Error::doing_it_wrong( |
678 | 678 | __METHOD__, |
679 | 679 | esc_html__( |
@@ -700,7 +700,7 @@ discard block |
||
700 | 700 | * @param string $exception |
701 | 701 | * @return boolean |
702 | 702 | */ |
703 | - public function all_reg_steps_completed_except( EE_Transaction $transaction, $exception = '' ) { |
|
703 | + public function all_reg_steps_completed_except(EE_Transaction $transaction, $exception = '') { |
|
704 | 704 | EE_Error::doing_it_wrong( |
705 | 705 | __METHOD__, |
706 | 706 | esc_html__( |
@@ -710,7 +710,7 @@ discard block |
||
710 | 710 | '4.9.12', |
711 | 711 | '5.0.0' |
712 | 712 | ); |
713 | - return $transaction->all_reg_steps_completed_except( $exception ); |
|
713 | + return $transaction->all_reg_steps_completed_except($exception); |
|
714 | 714 | } |
715 | 715 | |
716 | 716 | |
@@ -726,7 +726,7 @@ discard block |
||
726 | 726 | * @param EE_Transaction $transaction |
727 | 727 | * @return boolean |
728 | 728 | */ |
729 | - public function all_reg_steps_completed_except_final_step( EE_Transaction $transaction ) { |
|
729 | + public function all_reg_steps_completed_except_final_step(EE_Transaction $transaction) { |
|
730 | 730 | EE_Error::doing_it_wrong( |
731 | 731 | __METHOD__, |
732 | 732 | esc_html__( |
@@ -753,7 +753,7 @@ discard block |
||
753 | 753 | * @param string $reg_step_slug |
754 | 754 | * @return boolean | int |
755 | 755 | */ |
756 | - public function reg_step_completed( EE_Transaction $transaction, $reg_step_slug ) { |
|
756 | + public function reg_step_completed(EE_Transaction $transaction, $reg_step_slug) { |
|
757 | 757 | EE_Error::doing_it_wrong( |
758 | 758 | __METHOD__, |
759 | 759 | esc_html__( |
@@ -763,7 +763,7 @@ discard block |
||
763 | 763 | '4.9.12', |
764 | 764 | '5.0.0' |
765 | 765 | ); |
766 | - return $transaction->reg_step_completed( $reg_step_slug ); |
|
766 | + return $transaction->reg_step_completed($reg_step_slug); |
|
767 | 767 | } |
768 | 768 | |
769 | 769 | |
@@ -779,7 +779,7 @@ discard block |
||
779 | 779 | * @param EE_Transaction $transaction |
780 | 780 | * @return boolean | int |
781 | 781 | */ |
782 | - public function final_reg_step_completed( EE_Transaction $transaction ) { |
|
782 | + public function final_reg_step_completed(EE_Transaction $transaction) { |
|
783 | 783 | EE_Error::doing_it_wrong( |
784 | 784 | __METHOD__, |
785 | 785 | esc_html__( |
@@ -805,7 +805,7 @@ discard block |
||
805 | 805 | * @return boolean |
806 | 806 | * @throws \EE_Error |
807 | 807 | */ |
808 | - public function set_reg_step_initiated( EE_Transaction $transaction, $reg_step_slug ) { |
|
808 | + public function set_reg_step_initiated(EE_Transaction $transaction, $reg_step_slug) { |
|
809 | 809 | EE_Error::doing_it_wrong( |
810 | 810 | __METHOD__, |
811 | 811 | esc_html__( |
@@ -815,7 +815,7 @@ discard block |
||
815 | 815 | '4.9.12', |
816 | 816 | '5.0.0' |
817 | 817 | ); |
818 | - return $transaction->set_reg_step_initiated( $reg_step_slug ); |
|
818 | + return $transaction->set_reg_step_initiated($reg_step_slug); |
|
819 | 819 | } |
820 | 820 | |
821 | 821 | |
@@ -831,7 +831,7 @@ discard block |
||
831 | 831 | * @return boolean |
832 | 832 | * @throws \EE_Error |
833 | 833 | */ |
834 | - public function set_reg_step_completed( EE_Transaction $transaction, $reg_step_slug ) { |
|
834 | + public function set_reg_step_completed(EE_Transaction $transaction, $reg_step_slug) { |
|
835 | 835 | EE_Error::doing_it_wrong( |
836 | 836 | __METHOD__, |
837 | 837 | esc_html__( |
@@ -841,7 +841,7 @@ discard block |
||
841 | 841 | '4.9.12', |
842 | 842 | '5.0.0' |
843 | 843 | ); |
844 | - return $transaction->set_reg_step_completed( $reg_step_slug ); |
|
844 | + return $transaction->set_reg_step_completed($reg_step_slug); |
|
845 | 845 | } |
846 | 846 | |
847 | 847 | |
@@ -857,7 +857,7 @@ discard block |
||
857 | 857 | * @return boolean |
858 | 858 | * @throws \EE_Error |
859 | 859 | */ |
860 | - public function set_reg_step_not_completed( EE_Transaction $transaction, $reg_step_slug ) { |
|
860 | + public function set_reg_step_not_completed(EE_Transaction $transaction, $reg_step_slug) { |
|
861 | 861 | EE_Error::doing_it_wrong( |
862 | 862 | __METHOD__, |
863 | 863 | esc_html__( |
@@ -867,7 +867,7 @@ discard block |
||
867 | 867 | '4.9.12', |
868 | 868 | '5.0.0' |
869 | 869 | ); |
870 | - return $transaction->set_reg_step_not_completed( $reg_step_slug ); |
|
870 | + return $transaction->set_reg_step_not_completed($reg_step_slug); |
|
871 | 871 | } |
872 | 872 | |
873 | 873 | |
@@ -884,7 +884,7 @@ discard block |
||
884 | 884 | * @param string $reg_step_slug |
885 | 885 | * @return void |
886 | 886 | */ |
887 | - public function remove_reg_step( EE_Transaction $transaction, $reg_step_slug ) { |
|
887 | + public function remove_reg_step(EE_Transaction $transaction, $reg_step_slug) { |
|
888 | 888 | EE_Error::doing_it_wrong( |
889 | 889 | __METHOD__, |
890 | 890 | esc_html__( |
@@ -894,7 +894,7 @@ discard block |
||
894 | 894 | '4.9.12', |
895 | 895 | '5.0.0' |
896 | 896 | ); |
897 | - $transaction->remove_reg_step( $reg_step_slug ); |
|
897 | + $transaction->remove_reg_step($reg_step_slug); |
|
898 | 898 | } |
899 | 899 | |
900 | 900 | |
@@ -910,7 +910,7 @@ discard block |
||
910 | 910 | * @return boolean |
911 | 911 | * @throws \EE_Error |
912 | 912 | */ |
913 | - public function toggle_failed_transaction_status( EE_Transaction $transaction ) { |
|
913 | + public function toggle_failed_transaction_status(EE_Transaction $transaction) { |
|
914 | 914 | EE_Error::doing_it_wrong( |
915 | 915 | __METHOD__, |
916 | 916 | esc_html__( |
@@ -934,7 +934,7 @@ discard block |
||
934 | 934 | * @param EE_Transaction $transaction |
935 | 935 | * @return boolean |
936 | 936 | */ |
937 | - public function toggle_abandoned_transaction_status( EE_Transaction $transaction ) { |
|
937 | + public function toggle_abandoned_transaction_status(EE_Transaction $transaction) { |
|
938 | 938 | EE_Error::doing_it_wrong( |
939 | 939 | __METHOD__, |
940 | 940 | esc_html__( |