@@ -1,4 +1,4 @@ discard block |
||
1 | -<?php if ( ! defined( 'EVENT_ESPRESSO_VERSION' )) { exit('NO direct script access allowed'); } |
|
1 | +<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('NO direct script access allowed'); } |
|
2 | 2 | |
3 | 3 | /** |
4 | 4 | * ---------------------------------------------- |
@@ -82,10 +82,10 @@ discard block |
||
82 | 82 | * |
83 | 83 | * @param type $settings_array |
84 | 84 | */ |
85 | - public function set_settings( $settings_array ) { |
|
85 | + public function set_settings($settings_array) { |
|
86 | 86 | parent::set_settings($settings_array); |
87 | 87 | // Redirect URL. |
88 | - if ( $this->_debug_mode ) { |
|
88 | + if ($this->_debug_mode) { |
|
89 | 89 | $this->_base_gateway_url = 'https://api-3t.sandbox.paypal.com/nvp'; |
90 | 90 | } else { |
91 | 91 | $this->_base_gateway_url = 'https://api-3t.paypal.com/nvp'; |
@@ -100,19 +100,19 @@ discard block |
||
100 | 100 | * @param type $return_url |
101 | 101 | * @param type $cancel_url |
102 | 102 | */ |
103 | - public function set_redirection_info( $payment, $billing_info = array(), $return_url = NULL, $notify_url = NULL, $cancel_url = NULL ) { |
|
104 | - if ( ! $payment instanceof EEI_Payment ) { |
|
105 | - $payment->set_gateway_response( __( 'Error. No associated payment was found.', 'event_espresso' ) ); |
|
106 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
103 | + public function set_redirection_info($payment, $billing_info = array(), $return_url = NULL, $notify_url = NULL, $cancel_url = NULL) { |
|
104 | + if ( ! $payment instanceof EEI_Payment) { |
|
105 | + $payment->set_gateway_response(__('Error. No associated payment was found.', 'event_espresso')); |
|
106 | + $payment->set_status($this->_pay_model->failed_status()); |
|
107 | 107 | return $payment; |
108 | 108 | } |
109 | 109 | $transaction = $payment->transaction(); |
110 | - if ( ! $transaction instanceof EEI_Transaction ) { |
|
111 | - $payment->set_gateway_response( __( 'Could not process this payment because it has no associated transaction.', 'event_espresso' ) ); |
|
112 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
110 | + if ( ! $transaction instanceof EEI_Transaction) { |
|
111 | + $payment->set_gateway_response(__('Could not process this payment because it has no associated transaction.', 'event_espresso')); |
|
112 | + $payment->set_status($this->_pay_model->failed_status()); |
|
113 | 113 | return $payment; |
114 | 114 | } |
115 | - $order_description = substr( $this->_format_order_description($payment), 0, 127 ); |
|
115 | + $order_description = substr($this->_format_order_description($payment), 0, 127); |
|
116 | 116 | $primary_registration = $transaction->primary_registration(); |
117 | 117 | $primary_attendee = $primary_registration instanceof EE_Registration ? $primary_registration->attendee() : false; |
118 | 118 | $locale = explode('-', get_bloginfo('language')); |
@@ -126,37 +126,37 @@ discard block |
||
126 | 126 | 'RETURNURL' => $return_url, |
127 | 127 | 'CANCELURL' => $cancel_url, |
128 | 128 | 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
129 | - 'SOLUTIONTYPE' => 'Sole', // Buyer does not need to create a PayPal account to check out. This is referred to as PayPal Account Optional. |
|
130 | - 'BUTTONSOURCE' => 'EventEspresso_SP',//EE will blow up if you change this |
|
129 | + 'SOLUTIONTYPE' => 'Sole', // Buyer does not need to create a PayPal account to check out. This is referred to as PayPal Account Optional. |
|
130 | + 'BUTTONSOURCE' => 'EventEspresso_SP', //EE will blow up if you change this |
|
131 | 131 | 'LOCALECODE' => $locale[1] // Locale of the pages displayed by PayPal during Express Checkout. |
132 | 132 | ); |
133 | 133 | |
134 | 134 | // Show itemized list. |
135 | - if ( EEH_Money::compare_floats( $payment->amount(), $transaction->total(), '==' ) ) { |
|
135 | + if (EEH_Money::compare_floats($payment->amount(), $transaction->total(), '==')) { |
|
136 | 136 | $item_num = 0; |
137 | 137 | $itemized_sum = 0; |
138 | 138 | $total_line_items = $transaction->total_line_item(); |
139 | 139 | // Go through each item in the list. |
140 | - foreach ( $total_line_items->get_items() as $line_item ) { |
|
141 | - if ( $line_item instanceof EE_Line_Item ) { |
|
140 | + foreach ($total_line_items->get_items() as $line_item) { |
|
141 | + if ($line_item instanceof EE_Line_Item) { |
|
142 | 142 | // PayPal doesn't like line items with 0.00 amount, so we may skip those. |
143 | - if ( EEH_Money::compare_floats( $line_item->total(), '0.00', '==' ) ) { |
|
143 | + if (EEH_Money::compare_floats($line_item->total(), '0.00', '==')) { |
|
144 | 144 | continue; |
145 | 145 | } |
146 | 146 | |
147 | 147 | $unit_price = $line_item->unit_price(); |
148 | 148 | $line_item_quantity = $line_item->quantity(); |
149 | 149 | // This is a discount. |
150 | - if ( $line_item->is_percent() ) { |
|
150 | + if ($line_item->is_percent()) { |
|
151 | 151 | $unit_price = $line_item->total(); |
152 | 152 | $line_item_quantity = 1; |
153 | 153 | } |
154 | 154 | // Item Name. |
155 | - $token_request_dtls['L_PAYMENTREQUEST_0_NAME'.$item_num] = substr($this->_format_line_item_name( $line_item, $payment), 0, 127); |
|
155 | + $token_request_dtls['L_PAYMENTREQUEST_0_NAME'.$item_num] = substr($this->_format_line_item_name($line_item, $payment), 0, 127); |
|
156 | 156 | // Item description. |
157 | - $token_request_dtls['L_PAYMENTREQUEST_0_DESC'.$item_num] = substr($this->_format_line_item_desc( $line_item, $payment), 0, 127); |
|
157 | + $token_request_dtls['L_PAYMENTREQUEST_0_DESC'.$item_num] = substr($this->_format_line_item_desc($line_item, $payment), 0, 127); |
|
158 | 158 | // Cost of individual item. |
159 | - $token_request_dtls['L_PAYMENTREQUEST_0_AMT'.$item_num] = $this->format_currency( $unit_price ); |
|
159 | + $token_request_dtls['L_PAYMENTREQUEST_0_AMT'.$item_num] = $this->format_currency($unit_price); |
|
160 | 160 | // Item Number. |
161 | 161 | $token_request_dtls['L_PAYMENTREQUEST_0_NUMBER'.$item_num] = $item_num + 1; |
162 | 162 | // Item quantity. |
@@ -173,16 +173,16 @@ discard block |
||
173 | 173 | $token_request_dtls['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
174 | 174 | $token_request_dtls['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
175 | 175 | |
176 | - $itemized_sum_diff_from_txn_total = round( $transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), 2 ); |
|
176 | + $itemized_sum_diff_from_txn_total = round($transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), 2); |
|
177 | 177 | // If we were not able to recognize some item like promotion, surcharge or cancellation, |
178 | 178 | // add the difference as an extra line item. |
179 | - if ( $itemized_sum_diff_from_txn_total != 0 ) { |
|
179 | + if ($itemized_sum_diff_from_txn_total != 0) { |
|
180 | 180 | // Item Name. |
181 | - $token_request_dtls['L_PAYMENTREQUEST_0_NAME'.$item_num] = substr( __( 'Other (promotion/surcharge/cancellation)', 'event_espresso' ), 0, 127 ); |
|
181 | + $token_request_dtls['L_PAYMENTREQUEST_0_NAME'.$item_num] = substr(__('Other (promotion/surcharge/cancellation)', 'event_espresso'), 0, 127); |
|
182 | 182 | // Item description. |
183 | - $token_request_dtls['L_PAYMENTREQUEST_0_DESC'.$item_num] = substr( $this->_format_line_item_desc($line_item,$payment), 0, 127 ); |
|
183 | + $token_request_dtls['L_PAYMENTREQUEST_0_DESC'.$item_num] = substr($this->_format_line_item_desc($line_item, $payment), 0, 127); |
|
184 | 184 | // Cost of individual item. |
185 | - $token_request_dtls['L_PAYMENTREQUEST_0_AMT'.$item_num] = $this->format_currency( $itemized_sum_diff_from_txn_total ); |
|
185 | + $token_request_dtls['L_PAYMENTREQUEST_0_AMT'.$item_num] = $this->format_currency($itemized_sum_diff_from_txn_total); |
|
186 | 186 | // Item Number. |
187 | 187 | $token_request_dtls['L_PAYMENTREQUEST_0_NUMBER'.$item_num] = $item_num + 1; |
188 | 188 | // Item quantity. |
@@ -192,11 +192,11 @@ discard block |
||
192 | 192 | } else { |
193 | 193 | // Just one Item. |
194 | 194 | // Item Name. |
195 | - $token_request_dtls['L_PAYMENTREQUEST_0_NAME0'] = substr( $this->_format_partial_payment_line_item_name($payment), 0, 127 ); |
|
195 | + $token_request_dtls['L_PAYMENTREQUEST_0_NAME0'] = substr($this->_format_partial_payment_line_item_name($payment), 0, 127); |
|
196 | 196 | // Item description. |
197 | - $token_request_dtls['L_PAYMENTREQUEST_0_DESC0'] = substr( $this->_format_partial_payment_line_item_desc($payment), 0, 127 ); |
|
197 | + $token_request_dtls['L_PAYMENTREQUEST_0_DESC0'] = substr($this->_format_partial_payment_line_item_desc($payment), 0, 127); |
|
198 | 198 | // Cost of individual item. |
199 | - $token_request_dtls['L_PAYMENTREQUEST_0_AMT0'] = $this->format_currency( $payment->amount() ); |
|
199 | + $token_request_dtls['L_PAYMENTREQUEST_0_AMT0'] = $this->format_currency($payment->amount()); |
|
200 | 200 | // Item Number. |
201 | 201 | $token_request_dtls['L_PAYMENTREQUEST_0_NUMBER0'] = 1; |
202 | 202 | // Item quantity. |
@@ -204,14 +204,14 @@ discard block |
||
204 | 204 | // Digital item is sold. |
205 | 205 | $token_request_dtls['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Physical'; |
206 | 206 | // Item's sales S/H and tax amount. |
207 | - $token_request_dtls['PAYMENTREQUEST_0_ITEMAMT'] = $this->format_currency( $payment->amount() ); |
|
207 | + $token_request_dtls['PAYMENTREQUEST_0_ITEMAMT'] = $this->format_currency($payment->amount()); |
|
208 | 208 | $token_request_dtls['PAYMENTREQUEST_0_TAXAMT'] = '0'; |
209 | 209 | $token_request_dtls['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
210 | 210 | $token_request_dtls['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
211 | 211 | } |
212 | 212 | // Automatically filling out shipping and contact information. |
213 | - if ( $this->_request_shipping_addr && $primary_attendee instanceof EEI_Attendee ) { |
|
214 | - $token_request_dtls['NOSHIPPING'] = '2'; // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
|
213 | + if ($this->_request_shipping_addr && $primary_attendee instanceof EEI_Attendee) { |
|
214 | + $token_request_dtls['NOSHIPPING'] = '2'; // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
|
215 | 215 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET'] = $primary_attendee->address(); |
216 | 216 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $primary_attendee->address2(); |
217 | 217 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOCITY'] = $primary_attendee->city(); |
@@ -220,34 +220,34 @@ discard block |
||
220 | 220 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
221 | 221 | $token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
222 | 222 | $token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
223 | - } elseif ( ! $this->_request_shipping_addr ) { |
|
223 | + } elseif ( ! $this->_request_shipping_addr) { |
|
224 | 224 | // Do not request shipping details on the PP Checkout page. |
225 | 225 | $token_request_dtls['NOSHIPPING'] = '1'; |
226 | 226 | $token_request_dtls['REQCONFIRMSHIPPING'] = '0'; |
227 | 227 | |
228 | 228 | } |
229 | 229 | // Used a business/personal logo on the PayPal page. |
230 | - if ( ! empty($this->_image_url) ) { |
|
230 | + if ( ! empty($this->_image_url)) { |
|
231 | 231 | $token_request_dtls['LOGOIMG'] = $this->_image_url; |
232 | 232 | } |
233 | 233 | // Request PayPal token. |
234 | - $token_request_response = $this->_ppExpress_request( $token_request_dtls, 'Payment Token', $payment ); |
|
235 | - $token_rstatus = $this->_ppExpress_check_response( $token_request_response ); |
|
236 | - $response_args = ( isset($token_rstatus['args']) && is_array($token_rstatus['args']) ) ? $token_rstatus['args'] : array(); |
|
237 | - if ( $token_rstatus['status'] ) { |
|
234 | + $token_request_response = $this->_ppExpress_request($token_request_dtls, 'Payment Token', $payment); |
|
235 | + $token_rstatus = $this->_ppExpress_check_response($token_request_response); |
|
236 | + $response_args = (isset($token_rstatus['args']) && is_array($token_rstatus['args'])) ? $token_rstatus['args'] : array(); |
|
237 | + if ($token_rstatus['status']) { |
|
238 | 238 | // We got the Token so we may continue with the payment and redirect the client. |
239 | - $payment->set_details( $response_args ); |
|
239 | + $payment->set_details($response_args); |
|
240 | 240 | |
241 | - $gateway_url = ( $this->_debug_mode ) ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; |
|
242 | - $payment->set_redirect_url( $gateway_url . '/checkoutnow?useraction=commit&cmd=_express-checkout&token=' . $response_args['TOKEN'] ); |
|
241 | + $gateway_url = ($this->_debug_mode) ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; |
|
242 | + $payment->set_redirect_url($gateway_url.'/checkoutnow?useraction=commit&cmd=_express-checkout&token='.$response_args['TOKEN']); |
|
243 | 243 | } else { |
244 | - if ( isset($response_args['L_ERRORCODE']) ) { |
|
245 | - $payment->set_gateway_response( $response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE'] ); |
|
244 | + if (isset($response_args['L_ERRORCODE'])) { |
|
245 | + $payment->set_gateway_response($response_args['L_ERRORCODE'].'; '.$response_args['L_SHORTMESSAGE']); |
|
246 | 246 | } else { |
247 | - $payment->set_gateway_response( __( 'Error occurred while trying to setup the Express Checkout.', 'event_espresso' ) ); |
|
247 | + $payment->set_gateway_response(__('Error occurred while trying to setup the Express Checkout.', 'event_espresso')); |
|
248 | 248 | } |
249 | - $payment->set_details( $response_args ); |
|
250 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
249 | + $payment->set_details($response_args); |
|
250 | + $payment->set_status($this->_pay_model->failed_status()); |
|
251 | 251 | } |
252 | 252 | |
253 | 253 | return $payment; |
@@ -263,24 +263,24 @@ discard block |
||
263 | 263 | * @param type $transaction |
264 | 264 | * @return EEI_Payment |
265 | 265 | */ |
266 | - public function handle_payment_update( $update_info, $transaction ) { |
|
267 | - $payment = ( $transaction instanceof EEI_Transaction ) ? $transaction->last_payment() : null; |
|
266 | + public function handle_payment_update($update_info, $transaction) { |
|
267 | + $payment = ($transaction instanceof EEI_Transaction) ? $transaction->last_payment() : null; |
|
268 | 268 | |
269 | - if ( $payment instanceof EEI_Payment ) { |
|
270 | - $this->log( array( 'Return from Authorization' => $update_info ), $payment ); |
|
269 | + if ($payment instanceof EEI_Payment) { |
|
270 | + $this->log(array('Return from Authorization' => $update_info), $payment); |
|
271 | 271 | |
272 | 272 | $payment_details = $payment->details(); |
273 | 273 | $transaction = $payment->transaction(); |
274 | - if ( ! $transaction instanceof EEI_Transaction ) { |
|
275 | - $payment->set_gateway_response( __( 'Could not process this payment because it has no associated transaction.', 'event_espresso' ) ); |
|
276 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
274 | + if ( ! $transaction instanceof EEI_Transaction) { |
|
275 | + $payment->set_gateway_response(__('Could not process this payment because it has no associated transaction.', 'event_espresso')); |
|
276 | + $payment->set_status($this->_pay_model->failed_status()); |
|
277 | 277 | return $payment; |
278 | 278 | } |
279 | 279 | $primary_registrant = $transaction->primary_registration(); |
280 | 280 | |
281 | 281 | // Check if we still have the token. |
282 | - if ( ! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN']) ) { |
|
283 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
282 | + if ( ! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN'])) { |
|
283 | + $payment->set_status($this->_pay_model->failed_status()); |
|
284 | 284 | return $payment; |
285 | 285 | } |
286 | 286 | |
@@ -289,10 +289,10 @@ discard block |
||
289 | 289 | 'TOKEN' => $payment_details['TOKEN'] |
290 | 290 | ); |
291 | 291 | // Request Customer Details. |
292 | - $cdetails_request_response = $this->_ppExpress_request( $cdetails_request_dtls, 'Customer Details', $payment ); |
|
293 | - $cdetails_rstatus = $this->_ppExpress_check_response( $cdetails_request_response ); |
|
294 | - $cdata_response_args = ( isset($cdetails_rstatus['args']) && is_array($cdetails_rstatus['args']) ) ? $cdetails_rstatus['args'] : array(); |
|
295 | - if ( $cdetails_rstatus['status'] ) { |
|
292 | + $cdetails_request_response = $this->_ppExpress_request($cdetails_request_dtls, 'Customer Details', $payment); |
|
293 | + $cdetails_rstatus = $this->_ppExpress_check_response($cdetails_request_response); |
|
294 | + $cdata_response_args = (isset($cdetails_rstatus['args']) && is_array($cdetails_rstatus['args'])) ? $cdetails_rstatus['args'] : array(); |
|
295 | + if ($cdetails_rstatus['status']) { |
|
296 | 296 | // We got the PayerID so now we can Complete the transaction. |
297 | 297 | $docheckout_request_dtls = array( |
298 | 298 | 'METHOD' => 'DoExpressCheckoutPayment', |
@@ -303,39 +303,39 @@ discard block |
||
303 | 303 | 'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code() |
304 | 304 | ); |
305 | 305 | // Payment Checkout/Capture. |
306 | - $docheckout_request_response = $this->_ppExpress_request( $docheckout_request_dtls, 'Do Payment', $payment ); |
|
307 | - $docheckout_rstatus = $this->_ppExpress_check_response( $docheckout_request_response ); |
|
308 | - $docheckout_response_args = ( isset($docheckout_rstatus['args']) && is_array($docheckout_rstatus['args']) ) ? $docheckout_rstatus['args'] : array(); |
|
309 | - if ( $docheckout_rstatus['status'] ) { |
|
306 | + $docheckout_request_response = $this->_ppExpress_request($docheckout_request_dtls, 'Do Payment', $payment); |
|
307 | + $docheckout_rstatus = $this->_ppExpress_check_response($docheckout_request_response); |
|
308 | + $docheckout_response_args = (isset($docheckout_rstatus['args']) && is_array($docheckout_rstatus['args'])) ? $docheckout_rstatus['args'] : array(); |
|
309 | + if ($docheckout_rstatus['status']) { |
|
310 | 310 | // All is well, payment approved. |
311 | 311 | $primary_registration_code = $primary_registrant instanceof EE_Registration ? $primary_registrant->reg_code() : ''; |
312 | - $payment->set_extra_accntng( $primary_registration_code ); |
|
313 | - $payment->set_amount( isset($docheckout_response_args['PAYMENTINFO_0_AMT']) ? floatval( $docheckout_response_args['PAYMENTINFO_0_AMT'] ) : 0 ); |
|
314 | - $payment->set_txn_id_chq_nmbr( isset( $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] ) ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] : null ); |
|
315 | - $payment->set_details( $cdata_response_args ); |
|
316 | - $payment->set_gateway_response( isset( $docheckout_response_args['PAYMENTINFO_0_ACK'] ) ? $docheckout_response_args['PAYMENTINFO_0_ACK'] : '' ); |
|
317 | - $payment->set_status( $this->_pay_model->approved_status() ); |
|
312 | + $payment->set_extra_accntng($primary_registration_code); |
|
313 | + $payment->set_amount(isset($docheckout_response_args['PAYMENTINFO_0_AMT']) ? floatval($docheckout_response_args['PAYMENTINFO_0_AMT']) : 0); |
|
314 | + $payment->set_txn_id_chq_nmbr(isset($docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID']) ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] : null); |
|
315 | + $payment->set_details($cdata_response_args); |
|
316 | + $payment->set_gateway_response(isset($docheckout_response_args['PAYMENTINFO_0_ACK']) ? $docheckout_response_args['PAYMENTINFO_0_ACK'] : ''); |
|
317 | + $payment->set_status($this->_pay_model->approved_status()); |
|
318 | 318 | } else { |
319 | - if ( isset($docheckout_response_args['L_ERRORCODE']) ) { |
|
320 | - $payment->set_gateway_response( $docheckout_response_args['L_ERRORCODE'] . '; ' . $docheckout_response_args['L_SHORTMESSAGE'] ); |
|
319 | + if (isset($docheckout_response_args['L_ERRORCODE'])) { |
|
320 | + $payment->set_gateway_response($docheckout_response_args['L_ERRORCODE'].'; '.$docheckout_response_args['L_SHORTMESSAGE']); |
|
321 | 321 | } else { |
322 | - $payment->set_gateway_response( __( 'Error occurred while trying to Capture the funds.', 'event_espresso' ) ); |
|
322 | + $payment->set_gateway_response(__('Error occurred while trying to Capture the funds.', 'event_espresso')); |
|
323 | 323 | } |
324 | - $payment->set_details( $docheckout_response_args ); |
|
325 | - $payment->set_status( $this->_pay_model->declined_status() ); |
|
324 | + $payment->set_details($docheckout_response_args); |
|
325 | + $payment->set_status($this->_pay_model->declined_status()); |
|
326 | 326 | } |
327 | 327 | } else { |
328 | - if ( isset($cdata_response_args['L_ERRORCODE']) ) { |
|
329 | - $payment->set_gateway_response( $cdata_response_args['L_ERRORCODE'] . '; ' . $cdata_response_args['L_SHORTMESSAGE'] ); |
|
328 | + if (isset($cdata_response_args['L_ERRORCODE'])) { |
|
329 | + $payment->set_gateway_response($cdata_response_args['L_ERRORCODE'].'; '.$cdata_response_args['L_SHORTMESSAGE']); |
|
330 | 330 | } else { |
331 | - $payment->set_gateway_response( __( 'Error occurred while trying to get payment Details from PayPal.', 'event_espresso' ) ); |
|
331 | + $payment->set_gateway_response(__('Error occurred while trying to get payment Details from PayPal.', 'event_espresso')); |
|
332 | 332 | } |
333 | - $payment->set_details( $cdata_response_args ); |
|
334 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
333 | + $payment->set_details($cdata_response_args); |
|
334 | + $payment->set_status($this->_pay_model->failed_status()); |
|
335 | 335 | } |
336 | 336 | } else { |
337 | - $payment->set_gateway_response( __( 'Error occurred while trying to process the payment.', 'event_espresso' ) ); |
|
338 | - $payment->set_status( $this->_pay_model->failed_status() ); |
|
337 | + $payment->set_gateway_response(__('Error occurred while trying to process the payment.', 'event_espresso')); |
|
338 | + $payment->set_status($this->_pay_model->failed_status()); |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | return $payment; |
@@ -350,16 +350,16 @@ discard block |
||
350 | 350 | * @param EEI_Payment $payment |
351 | 351 | * @return mixed |
352 | 352 | */ |
353 | - public function _ppExpress_request( $request_params, $request_text, $payment ) { |
|
353 | + public function _ppExpress_request($request_params, $request_text, $payment) { |
|
354 | 354 | $request_dtls = array( |
355 | 355 | 'VERSION' => '204.0', |
356 | - 'USER' => urlencode( $this->_api_username ), |
|
357 | - 'PWD' => urlencode( $this->_api_password ), |
|
358 | - 'SIGNATURE' => urlencode( $this->_api_signature ) |
|
356 | + 'USER' => urlencode($this->_api_username), |
|
357 | + 'PWD' => urlencode($this->_api_password), |
|
358 | + 'SIGNATURE' => urlencode($this->_api_signature) |
|
359 | 359 | ); |
360 | - $dtls = array_merge( $request_dtls, $request_params ); |
|
360 | + $dtls = array_merge($request_dtls, $request_params); |
|
361 | 361 | |
362 | - $this->_log_clean_request( $dtls, $payment, $request_text . ' Request' ); |
|
362 | + $this->_log_clean_request($dtls, $payment, $request_text.' Request'); |
|
363 | 363 | // Request Customer Details. |
364 | 364 | $request_response = wp_remote_post( |
365 | 365 | $this->_base_gateway_url, |
@@ -369,11 +369,11 @@ discard block |
||
369 | 369 | 'httpversion' => '1.1', |
370 | 370 | 'cookies' => array(), |
371 | 371 | 'headers' => array(), |
372 | - 'body' => http_build_query( $dtls ) |
|
372 | + 'body' => http_build_query($dtls) |
|
373 | 373 | ) |
374 | 374 | ); |
375 | 375 | // Log the response. |
376 | - $this->log( array( $request_text . ' Response' => $request_response), $payment ); |
|
376 | + $this->log(array($request_text.' Response' => $request_response), $payment); |
|
377 | 377 | |
378 | 378 | return $request_response; |
379 | 379 | } |
@@ -385,31 +385,31 @@ discard block |
||
385 | 385 | * @param mixed $request_response |
386 | 386 | * @return array |
387 | 387 | */ |
388 | - public function _ppExpress_check_response( $request_response ) { |
|
389 | - if ( ! is_wp_error( $request_response ) && $request_response['body'] && ! empty($request_response['body'] ) ) { |
|
388 | + public function _ppExpress_check_response($request_response) { |
|
389 | + if ( ! is_wp_error($request_response) && $request_response['body'] && ! empty($request_response['body'])) { |
|
390 | 390 | $response_args = array(); |
391 | - parse_str( urldecode($request_response['body']), $response_args ); |
|
392 | - if ( isset($response_args['ACK']) |
|
391 | + parse_str(urldecode($request_response['body']), $response_args); |
|
392 | + if (isset($response_args['ACK']) |
|
393 | 393 | && $response_args['ACK'] === 'Success' |
394 | - && ( isset($response_args['PAYERID']) |
|
394 | + && (isset($response_args['PAYERID']) |
|
395 | 395 | || isset($response_args['PAYMENTINFO_0_TRANSACTIONID']) |
396 | - || ( isset($response_args['PAYMENTSTATUS']) && $response_args['PAYMENTSTATUS'] === 'Completed' ) |
|
396 | + || (isset($response_args['PAYMENTSTATUS']) && $response_args['PAYMENTSTATUS'] === 'Completed') |
|
397 | 397 | || isset($response_args['TOKEN']) |
398 | 398 | ) |
399 | 399 | ) { |
400 | 400 | // Response status OK, return response parameters for further processing. |
401 | 401 | return array('status' => true, 'args' => $response_args); |
402 | 402 | } else { |
403 | - if ( isset($response_args['ACK']) ) { |
|
403 | + if (isset($response_args['ACK'])) { |
|
404 | 404 | $errors = $this->_get_errors($response_args); |
405 | - return array( 'status' => false, 'args' => $errors ); |
|
405 | + return array('status' => false, 'args' => $errors); |
|
406 | 406 | } else { |
407 | - return array( 'status' => false, 'args' => $request_response ); |
|
407 | + return array('status' => false, 'args' => $request_response); |
|
408 | 408 | } |
409 | 409 | } |
410 | 410 | } else { |
411 | 411 | // If we got here then there was an error in this request. |
412 | - return array( 'status' => false, 'args' => $request_response ); |
|
412 | + return array('status' => false, 'args' => $request_response); |
|
413 | 413 | } |
414 | 414 | } |
415 | 415 | |
@@ -422,12 +422,12 @@ discard block |
||
422 | 422 | * @param string $info |
423 | 423 | * @return void |
424 | 424 | */ |
425 | - private function _log_clean_request( $request_prms, $payment, $info ) { |
|
425 | + private function _log_clean_request($request_prms, $payment, $info) { |
|
426 | 426 | $cleaned_request_data = $request_prms; |
427 | 427 | unset($cleaned_request_data['PWD']); |
428 | 428 | unset($cleaned_request_data['USER']); |
429 | 429 | unset($cleaned_request_data['SIGNATURE']); |
430 | - $this->log( array($info => $cleaned_request_data), $payment ); |
|
430 | + $this->log(array($info => $cleaned_request_data), $payment); |
|
431 | 431 | } |
432 | 432 | |
433 | 433 | |
@@ -437,16 +437,16 @@ discard block |
||
437 | 437 | * @param array $data_array |
438 | 438 | * @return array |
439 | 439 | */ |
440 | - private function _get_errors( $data_array ) { |
|
440 | + private function _get_errors($data_array) { |
|
441 | 441 | $errors = array(); |
442 | 442 | $n = 0; |
443 | - while ( isset($data_array['L_ERRORCODE' . $n . '']) ) { |
|
444 | - $l_error_code = isset($data_array['L_ERRORCODE' . $n . '']) ? $data_array['L_ERRORCODE' . $n . ''] : ''; |
|
445 | - $l_severity_code = isset($data_array['L_SEVERITYCODE' . $n . '']) ? $data_array['L_SEVERITYCODE' . $n . ''] : ''; |
|
446 | - $l_short_message = isset($data_array['L_SHORTMESSAGE' . $n . '']) ? $data_array['L_SHORTMESSAGE' . $n . ''] : ''; |
|
447 | - $l_long_message = isset($data_array['L_LONGMESSAGE' . $n . '']) ? $data_array['L_LONGMESSAGE' . $n . ''] : ''; |
|
443 | + while (isset($data_array['L_ERRORCODE'.$n.''])) { |
|
444 | + $l_error_code = isset($data_array['L_ERRORCODE'.$n.'']) ? $data_array['L_ERRORCODE'.$n.''] : ''; |
|
445 | + $l_severity_code = isset($data_array['L_SEVERITYCODE'.$n.'']) ? $data_array['L_SEVERITYCODE'.$n.''] : ''; |
|
446 | + $l_short_message = isset($data_array['L_SHORTMESSAGE'.$n.'']) ? $data_array['L_SHORTMESSAGE'.$n.''] : ''; |
|
447 | + $l_long_message = isset($data_array['L_LONGMESSAGE'.$n.'']) ? $data_array['L_LONGMESSAGE'.$n.''] : ''; |
|
448 | 448 | |
449 | - if ( $n == 0 ) { |
|
449 | + if ($n == 0) { |
|
450 | 450 | $errors = array( |
451 | 451 | 'L_ERRORCODE' => $l_error_code, |
452 | 452 | 'L_SHORTMESSAGE' => $l_short_message, |
@@ -454,10 +454,10 @@ discard block |
||
454 | 454 | 'L_SEVERITYCODE' => $l_severity_code |
455 | 455 | ); |
456 | 456 | } else { |
457 | - $errors['L_ERRORCODE'] .= ', ' . $l_error_code; |
|
458 | - $errors['L_SHORTMESSAGE'] .= ', ' . $l_short_message; |
|
459 | - $errors['L_LONGMESSAGE'] .= ', ' . $l_long_message; |
|
460 | - $errors['L_SEVERITYCODE'] .= ', ' . $l_severity_code; |
|
457 | + $errors['L_ERRORCODE'] .= ', '.$l_error_code; |
|
458 | + $errors['L_SHORTMESSAGE'] .= ', '.$l_short_message; |
|
459 | + $errors['L_LONGMESSAGE'] .= ', '.$l_long_message; |
|
460 | + $errors['L_SEVERITYCODE'] .= ', '.$l_severity_code; |
|
461 | 461 | } |
462 | 462 | |
463 | 463 | $n++; |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | use EventEspresso\core\libraries\rest_api\Model_Data_Translator; |
8 | 8 | |
9 | 9 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
10 | - exit('No direct script access allowed'); |
|
10 | + exit('No direct script access allowed'); |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | |
@@ -25,1106 +25,1106 @@ discard block |
||
25 | 25 | |
26 | 26 | |
27 | 27 | |
28 | - /** |
|
29 | - * @var Calculated_Model_Fields |
|
30 | - */ |
|
31 | - protected $_fields_calculator; |
|
32 | - |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * Read constructor. |
|
37 | - */ |
|
38 | - public function __construct() |
|
39 | - { |
|
40 | - parent::__construct(); |
|
41 | - $this->_fields_calculator = new Calculated_Model_Fields(); |
|
42 | - } |
|
43 | - |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
48 | - * |
|
49 | - * @param \WP_REST_Request $request |
|
50 | - * @return \WP_REST_Response|\WP_Error |
|
51 | - */ |
|
52 | - public static function handle_request_get_all(\WP_REST_Request $request) |
|
53 | - { |
|
54 | - $controller = new Read(); |
|
55 | - try { |
|
56 | - $matches = $controller->parse_route( |
|
57 | - $request->get_route(), |
|
58 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
59 | - array('version', 'model') |
|
60 | - ); |
|
61 | - $controller->set_requested_version($matches['version']); |
|
62 | - $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
63 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
64 | - return $controller->send_response( |
|
65 | - new \WP_Error( |
|
66 | - 'endpoint_parsing_error', |
|
67 | - sprintf( |
|
68 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
69 | - 'event_espresso'), |
|
70 | - $model_name_singular |
|
71 | - ) |
|
72 | - ) |
|
73 | - ); |
|
74 | - } |
|
75 | - return $controller->send_response( |
|
76 | - $controller->get_entities_from_model( |
|
77 | - $controller->get_model_version_info()->load_model($model_name_singular), |
|
78 | - $request |
|
79 | - ) |
|
80 | - ); |
|
81 | - } catch (\Exception $e) { |
|
82 | - return $controller->send_response($e); |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * Gets a single entity related to the model indicated in the path and its id |
|
90 | - * |
|
91 | - * @param \WP_REST_Request $request |
|
92 | - * @return \WP_REST_Response|\WP_Error |
|
93 | - */ |
|
94 | - public static function handle_request_get_one(\WP_REST_Request $request) |
|
95 | - { |
|
96 | - $controller = new Read(); |
|
97 | - try { |
|
98 | - $matches = $controller->parse_route( |
|
99 | - $request->get_route(), |
|
100 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
101 | - array('version', 'model', 'id')); |
|
102 | - $controller->set_requested_version($matches['version']); |
|
103 | - $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
104 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
105 | - return $controller->send_response( |
|
106 | - new \WP_Error( |
|
107 | - 'endpoint_parsing_error', |
|
108 | - sprintf( |
|
109 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
110 | - 'event_espresso'), |
|
111 | - $model_name_singular |
|
112 | - ) |
|
113 | - ) |
|
114 | - ); |
|
115 | - } |
|
116 | - return $controller->send_response( |
|
117 | - $controller->get_entity_from_model( |
|
118 | - $controller->get_model_version_info()->load_model($model_name_singular), |
|
119 | - $request |
|
120 | - ) |
|
121 | - ); |
|
122 | - } catch (\Exception $e) { |
|
123 | - return $controller->send_response($e); |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - |
|
129 | - /** |
|
130 | - * Gets all the related entities (or if its a belongs-to relation just the one) |
|
131 | - * to the item with the given id |
|
132 | - * |
|
133 | - * @param \WP_REST_Request $request |
|
134 | - * @return \WP_REST_Response|\WP_Error |
|
135 | - */ |
|
136 | - public static function handle_request_get_related(\WP_REST_Request $request) |
|
137 | - { |
|
138 | - $controller = new Read(); |
|
139 | - try { |
|
140 | - $matches = $controller->parse_route( |
|
141 | - $request->get_route(), |
|
142 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
143 | - array('version', 'model', 'id', 'related_model') |
|
144 | - ); |
|
145 | - $controller->set_requested_version($matches['version']); |
|
146 | - $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
147 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
148 | - return $controller->send_response( |
|
149 | - new \WP_Error( |
|
150 | - 'endpoint_parsing_error', |
|
151 | - sprintf( |
|
152 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
153 | - 'event_espresso'), |
|
154 | - $main_model_name_singular |
|
155 | - ) |
|
156 | - ) |
|
157 | - ); |
|
158 | - } |
|
159 | - $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
|
160 | - //assume the related model name is plural and try to find the model's name |
|
161 | - $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
|
162 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
163 | - //so the word didn't singularize well. Maybe that's just because it's a singular word? |
|
164 | - $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
|
165 | - } |
|
166 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
167 | - return $controller->send_response( |
|
168 | - new \WP_Error( |
|
169 | - 'endpoint_parsing_error', |
|
170 | - sprintf( |
|
171 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
172 | - 'event_espresso'), |
|
173 | - $related_model_name_singular |
|
174 | - ) |
|
175 | - ) |
|
176 | - ); |
|
177 | - } |
|
178 | - return $controller->send_response( |
|
179 | - $controller->get_entities_from_relation( |
|
180 | - $request->get_param('id'), |
|
181 | - $main_model->related_settings_for($related_model_name_singular), |
|
182 | - $request |
|
183 | - ) |
|
184 | - ); |
|
185 | - } catch (\Exception $e) { |
|
186 | - return $controller->send_response($e); |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - |
|
192 | - /** |
|
193 | - * Gets a collection for the given model and filters |
|
194 | - * |
|
195 | - * @param \EEM_Base $model |
|
196 | - * @param \WP_REST_Request $request |
|
197 | - * @return array|\WP_Error |
|
198 | - */ |
|
199 | - public function get_entities_from_model($model, $request) |
|
200 | - { |
|
201 | - $query_params = $this->create_model_query_params($model, $request->get_params()); |
|
202 | - if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
203 | - $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
204 | - return new \WP_Error( |
|
205 | - sprintf('rest_%s_cannot_list', $model_name_plural), |
|
206 | - sprintf( |
|
207 | - __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
208 | - $model_name_plural, |
|
209 | - Capabilities::get_missing_permissions_string($model, $query_params['caps']) |
|
210 | - ), |
|
211 | - array('status' => 403) |
|
212 | - ); |
|
213 | - } |
|
214 | - if (! $request->get_header('no_rest_headers')) { |
|
215 | - $this->_set_headers_from_query_params($model, $query_params); |
|
216 | - } |
|
217 | - /** @type array $results */ |
|
218 | - $results = $model->get_all_wpdb_results($query_params); |
|
219 | - $nice_results = array(); |
|
220 | - foreach ($results as $result) { |
|
221 | - $nice_results[] = $this->create_entity_from_wpdb_result( |
|
222 | - $model, |
|
223 | - $result, |
|
224 | - $request |
|
225 | - ); |
|
226 | - } |
|
227 | - return $nice_results; |
|
228 | - } |
|
229 | - |
|
230 | - |
|
231 | - |
|
232 | - /** |
|
233 | - * @param array $primary_model_query_params query params for finding the item from which |
|
234 | - * relations will be based |
|
235 | - * @param \EE_Model_Relation_Base $relation |
|
236 | - * @param \WP_REST_Request $request |
|
237 | - * @return \WP_Error|array |
|
238 | - */ |
|
239 | - protected function _get_entities_from_relation($primary_model_query_params, $relation, $request) |
|
240 | - { |
|
241 | - $context = $this->validate_context($request->get_param('caps')); |
|
242 | - $model = $relation->get_this_model(); |
|
243 | - $related_model = $relation->get_other_model(); |
|
244 | - if (! isset($primary_model_query_params[0])) { |
|
245 | - $primary_model_query_params[0] = array(); |
|
246 | - } |
|
247 | - //check if they can access the 1st model object |
|
248 | - $primary_model_query_params = array( |
|
249 | - 0 => $primary_model_query_params[0], |
|
250 | - 'limit' => 1, |
|
251 | - ); |
|
252 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
253 | - $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($primary_model_query_params); |
|
254 | - } |
|
255 | - $restricted_query_params = $primary_model_query_params; |
|
256 | - $restricted_query_params['caps'] = $context; |
|
257 | - $this->_set_debug_info('main model query params', $restricted_query_params); |
|
258 | - $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); |
|
259 | - if ( |
|
260 | - ! ( |
|
261 | - Capabilities::current_user_has_partial_access_to($related_model, $context) |
|
262 | - && $model->exists($restricted_query_params) |
|
263 | - ) |
|
264 | - ) { |
|
265 | - if ($relation instanceof \EE_Belongs_To_Relation) { |
|
266 | - $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
267 | - } else { |
|
268 | - $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); |
|
269 | - } |
|
270 | - return new \WP_Error( |
|
271 | - sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
272 | - sprintf( |
|
273 | - __('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
274 | - 'event_espresso'), |
|
275 | - $related_model_name_maybe_plural, |
|
276 | - $relation->get_this_model()->get_this_model_name(), |
|
277 | - implode( |
|
278 | - ',', |
|
279 | - array_keys( |
|
280 | - Capabilities::get_missing_permissions($related_model, $context) |
|
281 | - ) |
|
282 | - ) |
|
283 | - ), |
|
284 | - array('status' => 403) |
|
285 | - ); |
|
286 | - } |
|
287 | - $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); |
|
288 | - foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
289 | - $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
290 | - . '.' |
|
291 | - . $where_condition_key] = $where_condition_value; |
|
292 | - } |
|
293 | - $query_params['default_where_conditions'] = 'none'; |
|
294 | - $query_params['caps'] = $context; |
|
295 | - if (! $request->get_header('no_rest_headers')) { |
|
296 | - $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
|
297 | - } |
|
298 | - /** @type array $results */ |
|
299 | - $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
300 | - $nice_results = array(); |
|
301 | - foreach ($results as $result) { |
|
302 | - $nice_result = $this->create_entity_from_wpdb_result( |
|
303 | - $relation->get_other_model(), |
|
304 | - $result, |
|
305 | - $request |
|
306 | - ); |
|
307 | - if ($relation instanceof \EE_HABTM_Relation) { |
|
308 | - //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
309 | - //if there are conflicts we prefer the properties from the main model |
|
310 | - $join_model_result = $this->create_entity_from_wpdb_result( |
|
311 | - $relation->get_join_model(), |
|
312 | - $result, |
|
313 | - $request |
|
314 | - ); |
|
315 | - $joined_result = array_merge($nice_result, $join_model_result); |
|
316 | - //but keep the meta stuff from the main model |
|
317 | - if (isset($nice_result['meta'])) { |
|
318 | - $joined_result['meta'] = $nice_result['meta']; |
|
319 | - } |
|
320 | - $nice_result = $joined_result; |
|
321 | - } |
|
322 | - $nice_results[] = $nice_result; |
|
323 | - } |
|
324 | - if ($relation instanceof \EE_Belongs_To_Relation) { |
|
325 | - return array_shift($nice_results); |
|
326 | - } else { |
|
327 | - return $nice_results; |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - |
|
333 | - /** |
|
334 | - * Gets the collection for given relation object |
|
335 | - * The same as Read::get_entities_from_model(), except if the relation |
|
336 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
337 | - * the join-model-object into the results |
|
338 | - * |
|
339 | - * @param string $id the ID of the thing we are fetching related stuff from |
|
340 | - * @param \EE_Model_Relation_Base $relation |
|
341 | - * @param \WP_REST_Request $request |
|
342 | - * @return array|\WP_Error |
|
343 | - * @throws \EE_Error |
|
344 | - */ |
|
345 | - public function get_entities_from_relation($id, $relation, $request) |
|
346 | - { |
|
347 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
348 | - throw new \EE_Error( |
|
349 | - sprintf( |
|
350 | - __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
351 | - 'event_espresso'), |
|
352 | - $relation->get_this_model()->get_this_model_name() |
|
353 | - ) |
|
354 | - ); |
|
355 | - } |
|
356 | - return $this->_get_entities_from_relation( |
|
357 | - array( |
|
358 | - array( |
|
359 | - $relation->get_this_model()->primary_key_name() => $id, |
|
360 | - ), |
|
361 | - ), |
|
362 | - $relation, |
|
363 | - $request |
|
364 | - ); |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - |
|
369 | - /** |
|
370 | - * Sets the headers that are based on the model and query params, |
|
371 | - * like the total records. This should only be called on the original request |
|
372 | - * from the client, not on subsequent internal |
|
373 | - * |
|
374 | - * @param \EEM_Base $model |
|
375 | - * @param array $query_params |
|
376 | - * @return void |
|
377 | - */ |
|
378 | - protected function _set_headers_from_query_params($model, $query_params) |
|
379 | - { |
|
380 | - $this->_set_debug_info('model query params', $query_params); |
|
381 | - $this->_set_debug_info('missing caps', |
|
382 | - Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
|
383 | - //normally the limit to a 2-part array, where the 2nd item is the limit |
|
384 | - if (! isset($query_params['limit'])) { |
|
385 | - $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
386 | - } |
|
387 | - if (is_array($query_params['limit'])) { |
|
388 | - $limit_parts = $query_params['limit']; |
|
389 | - } else { |
|
390 | - $limit_parts = explode(',', $query_params['limit']); |
|
391 | - if (count($limit_parts) == 1) { |
|
392 | - $limit_parts = array(0, $limit_parts[0]); |
|
393 | - } |
|
394 | - } |
|
395 | - //remove the group by and having parts of the query, as those will |
|
396 | - //make the sql query return an array of values, instead of just a single value |
|
397 | - unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
398 | - $count = $model->count($query_params, null, true); |
|
399 | - $pages = $count / $limit_parts[1]; |
|
400 | - $this->_set_response_header('Total', $count, false); |
|
401 | - $this->_set_response_header('PageSize', $limit_parts[1], false); |
|
402 | - $this->_set_response_header('TotalPages', ceil($pages), false); |
|
403 | - } |
|
404 | - |
|
405 | - |
|
406 | - |
|
407 | - /** |
|
408 | - * Changes database results into REST API entities |
|
409 | - * |
|
410 | - * @param \EEM_Base $model |
|
411 | - * @param array $db_row like results from $wpdb->get_results() |
|
412 | - * @param \WP_REST_Request $rest_request |
|
413 | - * @param string $deprecated no longer used |
|
414 | - * @return array ready for being converted into json for sending to client |
|
415 | - */ |
|
416 | - public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
|
417 | - { |
|
418 | - if (! $rest_request instanceof \WP_REST_Request) { |
|
419 | - //ok so this was called in the old style, where the 3rd arg was |
|
420 | - //$include, and the 4th arg was $context |
|
421 | - //now setup the request just to avoid fatal errors, although we won't be able |
|
422 | - //to truly make use of it because it's kinda devoid of info |
|
423 | - $rest_request = new \WP_REST_Request(); |
|
424 | - $rest_request->set_param('include', $rest_request); |
|
425 | - $rest_request->set_param('caps', $deprecated); |
|
426 | - } |
|
427 | - if ($rest_request->get_param('caps') == null) { |
|
428 | - $rest_request->set_param('caps', \EEM_Base::caps_read); |
|
429 | - } |
|
430 | - $entity_array = $this->_create_bare_entity_from_wpdb_results($model, $db_row); |
|
431 | - $entity_array = $this->_add_extra_fields($model, $db_row, $entity_array); |
|
432 | - $entity_array['_links'] = $this->_get_entity_links($model, $db_row, $entity_array); |
|
433 | - $entity_array['_calculated_fields'] = $this->_get_entity_calculations($model, $db_row, $rest_request); |
|
434 | - $entity_array = $this->_include_requested_models($model, $rest_request, $entity_array, $db_row); |
|
435 | - $entity_array = apply_filters( |
|
436 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
437 | - $entity_array, |
|
438 | - $model, |
|
439 | - $rest_request->get_param('caps'), |
|
440 | - $rest_request, |
|
441 | - $this |
|
442 | - ); |
|
443 | - $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields( |
|
444 | - $entity_array, |
|
445 | - $model, |
|
446 | - $rest_request->get_param('caps'), |
|
447 | - $this->get_model_version_info(), |
|
448 | - $model->get_index_primary_key_string( |
|
449 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
450 | - ) |
|
451 | - ); |
|
452 | - $this->_set_debug_info( |
|
453 | - 'inaccessible fields', |
|
454 | - array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
455 | - ); |
|
456 | - return apply_filters( |
|
457 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
458 | - $result_without_inaccessible_fields, |
|
459 | - $model, |
|
460 | - $rest_request->get_param('caps') |
|
461 | - ); |
|
462 | - } |
|
463 | - |
|
464 | - |
|
465 | - |
|
466 | - /** |
|
467 | - * Creates a REST entity array (JSON object we're going to return in the response, but |
|
468 | - * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
469 | - * from $wpdb->get_row( $sql, ARRAY_A) |
|
470 | - * |
|
471 | - * @param \EEM_Base $model |
|
472 | - * @param array $db_row |
|
473 | - * @return array entity mostly ready for converting to JSON and sending in the response |
|
474 | - */ |
|
475 | - protected function _create_bare_entity_from_wpdb_results(\EEM_Base $model, $db_row) |
|
476 | - { |
|
477 | - $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
478 | - $result = array_intersect_key($result, |
|
479 | - $this->get_model_version_info()->fields_on_model_in_this_version($model)); |
|
480 | - foreach ($result as $field_name => $raw_field_value) { |
|
481 | - $field_obj = $model->field_settings_for($field_name); |
|
482 | - $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); |
|
483 | - if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { |
|
484 | - unset($result[$field_name]); |
|
485 | - } elseif ( |
|
486 | - $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format()) |
|
487 | - ) { |
|
488 | - $result[$field_name] = array( |
|
489 | - 'raw' => $field_obj->prepare_for_get($field_value), |
|
490 | - 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
491 | - ); |
|
492 | - } elseif ( |
|
493 | - $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format()) |
|
494 | - ) { |
|
495 | - $result[$field_name] = array( |
|
496 | - 'raw' => $field_obj->prepare_for_get($field_value), |
|
497 | - 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
498 | - ); |
|
499 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
500 | - if ($field_value instanceof \DateTime) { |
|
501 | - $timezone = $field_value->getTimezone(); |
|
502 | - $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
503 | - $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
504 | - $field_obj, |
|
505 | - $field_value, |
|
506 | - $this->get_model_version_info()->requested_version() |
|
507 | - ); |
|
508 | - $field_value->setTimezone($timezone); |
|
509 | - $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
510 | - $field_obj, |
|
511 | - $field_value, |
|
512 | - $this->get_model_version_info()->requested_version() |
|
513 | - ); |
|
514 | - } |
|
515 | - } else { |
|
516 | - $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
517 | - $field_obj, |
|
518 | - $field_obj->prepare_for_get($field_value), |
|
519 | - $this->get_model_version_info()->requested_version() |
|
520 | - ); |
|
521 | - } |
|
522 | - } |
|
523 | - return $result; |
|
524 | - } |
|
525 | - |
|
526 | - |
|
527 | - |
|
528 | - /** |
|
529 | - * Adds a few extra fields to the entity response |
|
530 | - * |
|
531 | - * @param \EEM_Base $model |
|
532 | - * @param array $db_row |
|
533 | - * @param array $entity_array |
|
534 | - * @return array modified entity |
|
535 | - */ |
|
536 | - protected function _add_extra_fields(\EEM_Base $model, $db_row, $entity_array) |
|
537 | - { |
|
538 | - if ($model instanceof \EEM_CPT_Base) { |
|
539 | - $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
540 | - } |
|
541 | - return $entity_array; |
|
542 | - } |
|
543 | - |
|
544 | - |
|
545 | - |
|
546 | - /** |
|
547 | - * Gets links we want to add to the response |
|
548 | - * |
|
549 | - * @global \WP_REST_Server $wp_rest_server |
|
550 | - * @param \EEM_Base $model |
|
551 | - * @param array $db_row |
|
552 | - * @param array $entity_array |
|
553 | - * @return array the _links item in the entity |
|
554 | - */ |
|
555 | - protected function _get_entity_links($model, $db_row, $entity_array) |
|
556 | - { |
|
557 | - //add basic links |
|
558 | - $links = array(); |
|
559 | - if ($model->has_primary_key_field()) { |
|
560 | - $links['self'] = array( |
|
561 | - array( |
|
562 | - 'href' => $this->get_versioned_link_to( |
|
563 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
564 | - . '/' |
|
565 | - . $entity_array[$model->primary_key_name()] |
|
566 | - ), |
|
567 | - ), |
|
568 | - ); |
|
569 | - } |
|
570 | - $links['collection'] = array( |
|
571 | - array( |
|
572 | - 'href' => $this->get_versioned_link_to( |
|
573 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
574 | - ), |
|
575 | - ), |
|
576 | - ); |
|
577 | - //add links to related models |
|
578 | - if ($model->has_primary_key_field()) { |
|
579 | - foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
|
580 | - $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
|
581 | - $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
582 | - array( |
|
583 | - 'href' => $this->get_versioned_link_to( |
|
584 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
585 | - . '/' |
|
586 | - . $entity_array[$model->primary_key_name()] |
|
587 | - . '/' |
|
588 | - . $related_model_part |
|
589 | - ), |
|
590 | - 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
591 | - ), |
|
592 | - ); |
|
593 | - } |
|
594 | - } |
|
595 | - return $links; |
|
596 | - } |
|
597 | - |
|
598 | - |
|
599 | - |
|
600 | - /** |
|
601 | - * Adds the included models indicated in the request to the entity provided |
|
602 | - * |
|
603 | - * @param \EEM_Base $model |
|
604 | - * @param \WP_REST_Request $rest_request |
|
605 | - * @param array $entity_array |
|
606 | - * @param array $db_row |
|
607 | - * @return array the modified entity |
|
608 | - */ |
|
609 | - protected function _include_requested_models( |
|
610 | - \EEM_Base $model, |
|
611 | - \WP_REST_Request $rest_request, |
|
612 | - $entity_array, |
|
613 | - $db_row = array() |
|
614 | - ) { |
|
615 | - //if $db_row not included, hope the entity array has what we need |
|
616 | - if (! $db_row) { |
|
617 | - $db_row = $entity_array; |
|
618 | - } |
|
619 | - $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
|
620 | - $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
|
621 | - //if they passed in * or didn't specify any includes, return everything |
|
622 | - if (! in_array('*', $includes_for_this_model) |
|
623 | - && ! empty($includes_for_this_model) |
|
624 | - ) { |
|
625 | - if ($model->has_primary_key_field()) { |
|
626 | - //always include the primary key. ya just gotta know that at least |
|
627 | - $includes_for_this_model[] = $model->primary_key_name(); |
|
628 | - } |
|
629 | - if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { |
|
630 | - $includes_for_this_model[] = '_calculated_fields'; |
|
631 | - } |
|
632 | - $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
633 | - } |
|
634 | - $relation_settings = $this->get_model_version_info()->relation_settings($model); |
|
635 | - foreach ($relation_settings as $relation_name => $relation_obj) { |
|
636 | - $related_fields_to_include = $this->explode_and_get_items_prefixed_with( |
|
637 | - $rest_request->get_param('include'), |
|
638 | - $relation_name |
|
639 | - ); |
|
640 | - $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with( |
|
641 | - $rest_request->get_param('calculate'), |
|
642 | - $relation_name |
|
643 | - ); |
|
644 | - //did they specify they wanted to include a related model, or |
|
645 | - //specific fields from a related model? |
|
646 | - //or did they specify to calculate a field from a related model? |
|
647 | - if ($related_fields_to_include || $related_fields_to_calculate) { |
|
648 | - //if so, we should include at least some part of the related model |
|
649 | - $pretend_related_request = new \WP_REST_Request(); |
|
650 | - $pretend_related_request->set_query_params( |
|
651 | - array( |
|
652 | - 'caps' => $rest_request->get_param('caps'), |
|
653 | - 'include' => $related_fields_to_include, |
|
654 | - 'calculate' => $related_fields_to_calculate, |
|
655 | - ) |
|
656 | - ); |
|
657 | - $pretend_related_request->add_header('no_rest_headers', true); |
|
658 | - $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
659 | - $model->get_index_primary_key_string( |
|
660 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
661 | - ) |
|
662 | - ); |
|
663 | - $related_results = $this->_get_entities_from_relation( |
|
664 | - $primary_model_query_params, |
|
665 | - $relation_obj, |
|
666 | - $pretend_related_request |
|
667 | - ); |
|
668 | - $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results |
|
669 | - instanceof |
|
670 | - \WP_Error |
|
671 | - ? null |
|
672 | - : $related_results; |
|
673 | - } |
|
674 | - } |
|
675 | - return $entity_array; |
|
676 | - } |
|
677 | - |
|
678 | - |
|
679 | - |
|
680 | - /** |
|
681 | - * Returns a new array with all the names of models removed. Eg |
|
682 | - * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
683 | - * |
|
684 | - * @param array $arr |
|
685 | - * @return array |
|
686 | - */ |
|
687 | - private function _remove_model_names_from_array($arr) |
|
688 | - { |
|
689 | - return array_diff($arr, array_keys(\EE_Registry::instance()->non_abstract_db_models)); |
|
690 | - } |
|
691 | - |
|
692 | - |
|
693 | - |
|
694 | - /** |
|
695 | - * Gets the calculated fields for the response |
|
696 | - * |
|
697 | - * @param \EEM_Base $model |
|
698 | - * @param array $wpdb_row |
|
699 | - * @param \WP_REST_Request $rest_request |
|
700 | - * @return \stdClass the _calculations item in the entity |
|
701 | - */ |
|
702 | - protected function _get_entity_calculations($model, $wpdb_row, $rest_request) |
|
703 | - { |
|
704 | - $calculated_fields = $this->explode_and_get_items_prefixed_with( |
|
705 | - $rest_request->get_param('calculate'), |
|
706 | - '' |
|
707 | - ); |
|
708 | - //note: setting calculate=* doesn't do anything |
|
709 | - $calculated_fields_to_return = new \stdClass(); |
|
710 | - foreach ($calculated_fields as $field_to_calculate) { |
|
711 | - try { |
|
712 | - $calculated_fields_to_return->$field_to_calculate = Model_Data_Translator::prepare_field_value_for_json( |
|
713 | - null, |
|
714 | - $this->_fields_calculator->retrieve_calculated_field_value( |
|
715 | - $model, |
|
716 | - $field_to_calculate, |
|
717 | - $wpdb_row, |
|
718 | - $rest_request, |
|
719 | - $this |
|
720 | - ), |
|
721 | - $this->get_model_version_info()->requested_version() |
|
722 | - ); |
|
723 | - } catch (Rest_Exception $e) { |
|
724 | - //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
725 | - $this->_set_response_header( |
|
726 | - 'Notices-Field-Calculation-Errors[' |
|
727 | - . $e->get_string_code() |
|
728 | - . '][' |
|
729 | - . $model->get_this_model_name() |
|
730 | - . '][' |
|
731 | - . $field_to_calculate |
|
732 | - . ']', |
|
733 | - $e->getMessage(), |
|
734 | - true |
|
735 | - ); |
|
736 | - } |
|
737 | - } |
|
738 | - return $calculated_fields_to_return; |
|
739 | - } |
|
740 | - |
|
741 | - |
|
742 | - |
|
743 | - /** |
|
744 | - * Gets the full URL to the resource, taking the requested version into account |
|
745 | - * |
|
746 | - * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
747 | - * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
748 | - */ |
|
749 | - public function get_versioned_link_to($link_part_after_version_and_slash) |
|
750 | - { |
|
751 | - return rest_url( |
|
752 | - \EED_Core_Rest_Api::ee_api_namespace |
|
753 | - . $this->get_model_version_info()->requested_version() |
|
754 | - . '/' |
|
755 | - . $link_part_after_version_and_slash |
|
756 | - ); |
|
757 | - } |
|
758 | - |
|
759 | - |
|
760 | - |
|
761 | - /** |
|
762 | - * Gets the correct lowercase name for the relation in the API according |
|
763 | - * to the relation's type |
|
764 | - * |
|
765 | - * @param string $relation_name |
|
766 | - * @param \EE_Model_Relation_Base $relation_obj |
|
767 | - * @return string |
|
768 | - */ |
|
769 | - public static function get_related_entity_name($relation_name, $relation_obj) |
|
770 | - { |
|
771 | - if ($relation_obj instanceof \EE_Belongs_To_Relation) { |
|
772 | - return strtolower($relation_name); |
|
773 | - } else { |
|
774 | - return \EEH_Inflector::pluralize_and_lower($relation_name); |
|
775 | - } |
|
776 | - } |
|
777 | - |
|
778 | - |
|
779 | - |
|
780 | - /** |
|
781 | - * Gets the one model object with the specified id for the specified model |
|
782 | - * |
|
783 | - * @param \EEM_Base $model |
|
784 | - * @param \WP_REST_Request $request |
|
785 | - * @return array|\WP_Error |
|
786 | - */ |
|
787 | - public function get_entity_from_model($model, $request) |
|
788 | - { |
|
789 | - $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
790 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
791 | - $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
792 | - } |
|
793 | - $restricted_query_params = $query_params; |
|
794 | - $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
|
795 | - $this->_set_debug_info('model query params', $restricted_query_params); |
|
796 | - $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
797 | - if (! empty ($model_rows)) { |
|
798 | - return $this->create_entity_from_wpdb_result( |
|
799 | - $model, |
|
800 | - array_shift($model_rows), |
|
801 | - $request); |
|
802 | - } else { |
|
803 | - //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
804 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
805 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
806 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
807 | - //you got shafted- it existed but we didn't want to tell you! |
|
808 | - return new \WP_Error( |
|
809 | - 'rest_user_cannot_read', |
|
810 | - sprintf( |
|
811 | - __('Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso'), |
|
812 | - strtolower($model->get_this_model_name()), |
|
813 | - Capabilities::get_missing_permissions_string( |
|
814 | - $model, |
|
815 | - $this->validate_context($request->get_param('caps'))) |
|
816 | - ), |
|
817 | - array('status' => 403) |
|
818 | - ); |
|
819 | - } else { |
|
820 | - //it's not you. It just doesn't exist |
|
821 | - return new \WP_Error( |
|
822 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
823 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
824 | - array('status' => 404) |
|
825 | - ); |
|
826 | - } |
|
827 | - } |
|
828 | - } |
|
829 | - |
|
830 | - |
|
831 | - |
|
832 | - /** |
|
833 | - * If a context is provided which isn't valid, maybe it was added in a future |
|
834 | - * version so just treat it as a default read |
|
835 | - * |
|
836 | - * @param string $context |
|
837 | - * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
838 | - */ |
|
839 | - public function validate_context($context) |
|
840 | - { |
|
841 | - if (! $context) { |
|
842 | - $context = \EEM_Base::caps_read; |
|
843 | - } |
|
844 | - $valid_contexts = \EEM_Base::valid_cap_contexts(); |
|
845 | - if (in_array($context, $valid_contexts)) { |
|
846 | - return $context; |
|
847 | - } else { |
|
848 | - return \EEM_Base::caps_read; |
|
849 | - } |
|
850 | - } |
|
851 | - |
|
852 | - |
|
853 | - |
|
854 | - /** |
|
855 | - * Verifies the passed in value is an allowable default where conditions value. |
|
856 | - * |
|
857 | - * @param $default_query_params |
|
858 | - * @return string |
|
859 | - */ |
|
860 | - public function validate_default_query_params($default_query_params) |
|
861 | - { |
|
862 | - $valid_default_where_conditions_for_api_calls = array( |
|
863 | - \EEM_Base::default_where_conditions_all, |
|
864 | - \EEM_Base::default_where_conditions_minimum_all, |
|
865 | - \EEM_Base::default_where_conditions_minimum_others, |
|
866 | - ); |
|
867 | - if (! $default_query_params) { |
|
868 | - $default_query_params = \EEM_Base::default_where_conditions_all; |
|
869 | - } |
|
870 | - if ( |
|
871 | - in_array( |
|
872 | - $default_query_params, |
|
873 | - $valid_default_where_conditions_for_api_calls, |
|
874 | - true |
|
875 | - ) |
|
876 | - ) { |
|
877 | - return $default_query_params; |
|
878 | - } else { |
|
879 | - return \EEM_Base::default_where_conditions_all; |
|
880 | - } |
|
881 | - } |
|
882 | - |
|
883 | - |
|
884 | - |
|
885 | - /** |
|
886 | - * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
887 | - * Note: right now the query parameter keys for fields (and related fields) |
|
888 | - * can be left as-is, but it's quite possible this will change someday. |
|
889 | - * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
890 | - * |
|
891 | - * @param \EEM_Base $model |
|
892 | - * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
893 | - * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
894 | - * that absolutely no results should be returned |
|
895 | - * @throws \EE_Error |
|
896 | - */ |
|
897 | - public function create_model_query_params($model, $query_parameters) |
|
898 | - { |
|
899 | - $model_query_params = array(); |
|
900 | - if (isset($query_parameters['where'])) { |
|
901 | - $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
902 | - $query_parameters['where'], |
|
903 | - $model, |
|
904 | - $this->get_model_version_info()->requested_version() |
|
905 | - ); |
|
906 | - } |
|
907 | - if (isset($query_parameters['order_by'])) { |
|
908 | - $order_by = $query_parameters['order_by']; |
|
909 | - } elseif (isset($query_parameters['orderby'])) { |
|
910 | - $order_by = $query_parameters['orderby']; |
|
911 | - } else { |
|
912 | - $order_by = null; |
|
913 | - } |
|
914 | - if ($order_by !== null) { |
|
915 | - if (is_array($order_by)) { |
|
916 | - $order_by = Model_Data_Translator::prepare_field_names_in_array_keys_from_json($order_by); |
|
917 | - } else { |
|
918 | - //it's a single item |
|
919 | - $order_by = Model_Data_Translator::prepare_field_name_from_json($order_by); |
|
920 | - } |
|
921 | - $model_query_params['order_by'] = $order_by; |
|
922 | - } |
|
923 | - if (isset($query_parameters['group_by'])) { |
|
924 | - $group_by = $query_parameters['group_by']; |
|
925 | - } elseif (isset($query_parameters['groupby'])) { |
|
926 | - $group_by = $query_parameters['groupby']; |
|
927 | - } else { |
|
928 | - $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
929 | - } |
|
930 | - //make sure they're all real names |
|
931 | - if (is_array($group_by)) { |
|
932 | - $group_by = Model_Data_Translator::prepare_field_names_from_json($group_by); |
|
933 | - } |
|
934 | - if ($group_by !== null) { |
|
935 | - $model_query_params['group_by'] = $group_by; |
|
936 | - } |
|
937 | - if (isset($query_parameters['having'])) { |
|
938 | - $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
939 | - $query_parameters['having'], |
|
940 | - $model, |
|
941 | - $this->get_model_version_info()->requested_version() |
|
942 | - ); |
|
943 | - } |
|
944 | - if (isset($query_parameters['order'])) { |
|
945 | - $model_query_params['order'] = $query_parameters['order']; |
|
946 | - } |
|
947 | - if (isset($query_parameters['mine'])) { |
|
948 | - $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
949 | - } |
|
950 | - if (isset($query_parameters['limit'])) { |
|
951 | - //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
952 | - if (! is_array($query_parameters['limit'])) { |
|
953 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
954 | - } else { |
|
955 | - $limit_array = $query_parameters['limit']; |
|
956 | - } |
|
957 | - $sanitized_limit = array(); |
|
958 | - foreach ($limit_array as $key => $limit_part) { |
|
959 | - if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
960 | - throw new \EE_Error( |
|
961 | - sprintf( |
|
962 | - __('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
963 | - 'event_espresso'), |
|
964 | - wp_json_encode($query_parameters['limit']) |
|
965 | - ) |
|
966 | - ); |
|
967 | - } |
|
968 | - $sanitized_limit[] = (int)$limit_part; |
|
969 | - } |
|
970 | - $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
971 | - } else { |
|
972 | - $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
973 | - } |
|
974 | - if (isset($query_parameters['caps'])) { |
|
975 | - $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); |
|
976 | - } else { |
|
977 | - $model_query_params['caps'] = \EEM_Base::caps_read; |
|
978 | - } |
|
979 | - if (isset($query_parameters['default_where_conditions'])) { |
|
980 | - $model_query_params['default_where_conditions'] = $this->validate_default_query_params($query_parameters['default_where_conditions']); |
|
981 | - } |
|
982 | - return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
983 | - } |
|
984 | - |
|
985 | - |
|
986 | - |
|
987 | - /** |
|
988 | - * Changes the REST-style query params for use in the models |
|
989 | - * |
|
990 | - * @deprecated |
|
991 | - * @param \EEM_Base $model |
|
992 | - * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
993 | - * @return array |
|
994 | - */ |
|
995 | - public function prepare_rest_query_params_key_for_models($model, $query_params) |
|
996 | - { |
|
997 | - $model_ready_query_params = array(); |
|
998 | - foreach ($query_params as $key => $value) { |
|
999 | - if (is_array($value)) { |
|
1000 | - $model_ready_query_params[$key] = $this->prepare_rest_query_params_key_for_models($model, $value); |
|
1001 | - } else { |
|
1002 | - $model_ready_query_params[$key] = $value; |
|
1003 | - } |
|
1004 | - } |
|
1005 | - return $model_ready_query_params; |
|
1006 | - } |
|
1007 | - |
|
1008 | - |
|
1009 | - |
|
1010 | - /** |
|
1011 | - * @deprecated |
|
1012 | - * @param $model |
|
1013 | - * @param $query_params |
|
1014 | - * @return array |
|
1015 | - */ |
|
1016 | - public function prepare_rest_query_params_values_for_models($model, $query_params) |
|
1017 | - { |
|
1018 | - $model_ready_query_params = array(); |
|
1019 | - foreach ($query_params as $key => $value) { |
|
1020 | - if (is_array($value)) { |
|
1021 | - $model_ready_query_params[$key] = $this->prepare_rest_query_params_values_for_models($model, $value); |
|
1022 | - } else { |
|
1023 | - $model_ready_query_params[$key] = $value; |
|
1024 | - } |
|
1025 | - } |
|
1026 | - return $model_ready_query_params; |
|
1027 | - } |
|
1028 | - |
|
1029 | - |
|
1030 | - |
|
1031 | - /** |
|
1032 | - * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1033 | - * If no prefix is specified, returns items with no period. |
|
1034 | - * |
|
1035 | - * @param string|array $string_to_explode eg "jibba,jabba, blah, blaabla" or array('jibba', 'jabba' ) |
|
1036 | - * @param string $prefix "Event" or "foobar" |
|
1037 | - * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1038 | - * we only return strings starting with that and a period; if no prefix was |
|
1039 | - * specified we return all items containing NO periods |
|
1040 | - */ |
|
1041 | - public function explode_and_get_items_prefixed_with($string_to_explode, $prefix) |
|
1042 | - { |
|
1043 | - if (is_string($string_to_explode)) { |
|
1044 | - $exploded_contents = explode(',', $string_to_explode); |
|
1045 | - } else if (is_array($string_to_explode)) { |
|
1046 | - $exploded_contents = $string_to_explode; |
|
1047 | - } else { |
|
1048 | - $exploded_contents = array(); |
|
1049 | - } |
|
1050 | - //if the string was empty, we want an empty array |
|
1051 | - $exploded_contents = array_filter($exploded_contents); |
|
1052 | - $contents_with_prefix = array(); |
|
1053 | - foreach ($exploded_contents as $item) { |
|
1054 | - $item = trim($item); |
|
1055 | - //if no prefix was provided, so we look for items with no "." in them |
|
1056 | - if (! $prefix) { |
|
1057 | - //does this item have a period? |
|
1058 | - if (strpos($item, '.') === false) { |
|
1059 | - //if not, then its what we're looking for |
|
1060 | - $contents_with_prefix[] = $item; |
|
1061 | - } |
|
1062 | - } else if (strpos($item, $prefix . '.') === 0) { |
|
1063 | - //this item has the prefix and a period, grab it |
|
1064 | - $contents_with_prefix[] = substr( |
|
1065 | - $item, |
|
1066 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1067 | - ); |
|
1068 | - } else if ($item === $prefix) { |
|
1069 | - //this item is JUST the prefix |
|
1070 | - //so let's grab everything after, which is a blank string |
|
1071 | - $contents_with_prefix[] = ''; |
|
1072 | - } |
|
1073 | - } |
|
1074 | - return $contents_with_prefix; |
|
1075 | - } |
|
1076 | - |
|
1077 | - |
|
1078 | - |
|
1079 | - /** |
|
1080 | - * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1081 | - * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1082 | - * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1083 | - * array('*') (when you provided a model and a model of that kind was found). |
|
1084 | - * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1085 | - * (ie have NO period in them), or for the provided model (ie start with the model |
|
1086 | - * name and then a period). |
|
1087 | - * @param string $include_string @see Read:handle_request_get_all |
|
1088 | - * @param string $model_name |
|
1089 | - * @return array of fields for this model. If $model_name is provided, then |
|
1090 | - * the fields for that model, with the model's name removed from each. |
|
1091 | - * If $include_string was blank or '*' returns an empty array |
|
1092 | - */ |
|
1093 | - public function extract_includes_for_this_model($include_string, $model_name = null) |
|
1094 | - { |
|
1095 | - if (is_array($include_string)) { |
|
1096 | - $include_string = implode(',', $include_string); |
|
1097 | - } |
|
1098 | - if ($include_string === '*' || $include_string === '') { |
|
1099 | - return array(); |
|
1100 | - } |
|
1101 | - $includes = explode(',', $include_string); |
|
1102 | - $extracted_fields_to_include = array(); |
|
1103 | - if ($model_name) { |
|
1104 | - foreach ($includes as $field_to_include) { |
|
1105 | - $field_to_include = trim($field_to_include); |
|
1106 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1107 | - //found the model name at the exact start |
|
1108 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1109 | - $extracted_fields_to_include[] = $field_sans_model_name; |
|
1110 | - } elseif ($field_to_include == $model_name) { |
|
1111 | - $extracted_fields_to_include[] = '*'; |
|
1112 | - } |
|
1113 | - } |
|
1114 | - } else { |
|
1115 | - //look for ones with no period |
|
1116 | - foreach ($includes as $field_to_include) { |
|
1117 | - $field_to_include = trim($field_to_include); |
|
1118 | - if ( |
|
1119 | - strpos($field_to_include, '.') === false |
|
1120 | - && ! $this->get_model_version_info()->is_model_name_in_this_version($field_to_include) |
|
1121 | - ) { |
|
1122 | - $extracted_fields_to_include[] = $field_to_include; |
|
1123 | - } |
|
1124 | - } |
|
1125 | - } |
|
1126 | - return $extracted_fields_to_include; |
|
1127 | - } |
|
28 | + /** |
|
29 | + * @var Calculated_Model_Fields |
|
30 | + */ |
|
31 | + protected $_fields_calculator; |
|
32 | + |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * Read constructor. |
|
37 | + */ |
|
38 | + public function __construct() |
|
39 | + { |
|
40 | + parent::__construct(); |
|
41 | + $this->_fields_calculator = new Calculated_Model_Fields(); |
|
42 | + } |
|
43 | + |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
48 | + * |
|
49 | + * @param \WP_REST_Request $request |
|
50 | + * @return \WP_REST_Response|\WP_Error |
|
51 | + */ |
|
52 | + public static function handle_request_get_all(\WP_REST_Request $request) |
|
53 | + { |
|
54 | + $controller = new Read(); |
|
55 | + try { |
|
56 | + $matches = $controller->parse_route( |
|
57 | + $request->get_route(), |
|
58 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
59 | + array('version', 'model') |
|
60 | + ); |
|
61 | + $controller->set_requested_version($matches['version']); |
|
62 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
63 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
64 | + return $controller->send_response( |
|
65 | + new \WP_Error( |
|
66 | + 'endpoint_parsing_error', |
|
67 | + sprintf( |
|
68 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
69 | + 'event_espresso'), |
|
70 | + $model_name_singular |
|
71 | + ) |
|
72 | + ) |
|
73 | + ); |
|
74 | + } |
|
75 | + return $controller->send_response( |
|
76 | + $controller->get_entities_from_model( |
|
77 | + $controller->get_model_version_info()->load_model($model_name_singular), |
|
78 | + $request |
|
79 | + ) |
|
80 | + ); |
|
81 | + } catch (\Exception $e) { |
|
82 | + return $controller->send_response($e); |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * Gets a single entity related to the model indicated in the path and its id |
|
90 | + * |
|
91 | + * @param \WP_REST_Request $request |
|
92 | + * @return \WP_REST_Response|\WP_Error |
|
93 | + */ |
|
94 | + public static function handle_request_get_one(\WP_REST_Request $request) |
|
95 | + { |
|
96 | + $controller = new Read(); |
|
97 | + try { |
|
98 | + $matches = $controller->parse_route( |
|
99 | + $request->get_route(), |
|
100 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
101 | + array('version', 'model', 'id')); |
|
102 | + $controller->set_requested_version($matches['version']); |
|
103 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
104 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
105 | + return $controller->send_response( |
|
106 | + new \WP_Error( |
|
107 | + 'endpoint_parsing_error', |
|
108 | + sprintf( |
|
109 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
110 | + 'event_espresso'), |
|
111 | + $model_name_singular |
|
112 | + ) |
|
113 | + ) |
|
114 | + ); |
|
115 | + } |
|
116 | + return $controller->send_response( |
|
117 | + $controller->get_entity_from_model( |
|
118 | + $controller->get_model_version_info()->load_model($model_name_singular), |
|
119 | + $request |
|
120 | + ) |
|
121 | + ); |
|
122 | + } catch (\Exception $e) { |
|
123 | + return $controller->send_response($e); |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + |
|
129 | + /** |
|
130 | + * Gets all the related entities (or if its a belongs-to relation just the one) |
|
131 | + * to the item with the given id |
|
132 | + * |
|
133 | + * @param \WP_REST_Request $request |
|
134 | + * @return \WP_REST_Response|\WP_Error |
|
135 | + */ |
|
136 | + public static function handle_request_get_related(\WP_REST_Request $request) |
|
137 | + { |
|
138 | + $controller = new Read(); |
|
139 | + try { |
|
140 | + $matches = $controller->parse_route( |
|
141 | + $request->get_route(), |
|
142 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
143 | + array('version', 'model', 'id', 'related_model') |
|
144 | + ); |
|
145 | + $controller->set_requested_version($matches['version']); |
|
146 | + $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
147 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
148 | + return $controller->send_response( |
|
149 | + new \WP_Error( |
|
150 | + 'endpoint_parsing_error', |
|
151 | + sprintf( |
|
152 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
153 | + 'event_espresso'), |
|
154 | + $main_model_name_singular |
|
155 | + ) |
|
156 | + ) |
|
157 | + ); |
|
158 | + } |
|
159 | + $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
|
160 | + //assume the related model name is plural and try to find the model's name |
|
161 | + $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
|
162 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
163 | + //so the word didn't singularize well. Maybe that's just because it's a singular word? |
|
164 | + $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
|
165 | + } |
|
166 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
167 | + return $controller->send_response( |
|
168 | + new \WP_Error( |
|
169 | + 'endpoint_parsing_error', |
|
170 | + sprintf( |
|
171 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
172 | + 'event_espresso'), |
|
173 | + $related_model_name_singular |
|
174 | + ) |
|
175 | + ) |
|
176 | + ); |
|
177 | + } |
|
178 | + return $controller->send_response( |
|
179 | + $controller->get_entities_from_relation( |
|
180 | + $request->get_param('id'), |
|
181 | + $main_model->related_settings_for($related_model_name_singular), |
|
182 | + $request |
|
183 | + ) |
|
184 | + ); |
|
185 | + } catch (\Exception $e) { |
|
186 | + return $controller->send_response($e); |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + |
|
192 | + /** |
|
193 | + * Gets a collection for the given model and filters |
|
194 | + * |
|
195 | + * @param \EEM_Base $model |
|
196 | + * @param \WP_REST_Request $request |
|
197 | + * @return array|\WP_Error |
|
198 | + */ |
|
199 | + public function get_entities_from_model($model, $request) |
|
200 | + { |
|
201 | + $query_params = $this->create_model_query_params($model, $request->get_params()); |
|
202 | + if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
203 | + $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
204 | + return new \WP_Error( |
|
205 | + sprintf('rest_%s_cannot_list', $model_name_plural), |
|
206 | + sprintf( |
|
207 | + __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
208 | + $model_name_plural, |
|
209 | + Capabilities::get_missing_permissions_string($model, $query_params['caps']) |
|
210 | + ), |
|
211 | + array('status' => 403) |
|
212 | + ); |
|
213 | + } |
|
214 | + if (! $request->get_header('no_rest_headers')) { |
|
215 | + $this->_set_headers_from_query_params($model, $query_params); |
|
216 | + } |
|
217 | + /** @type array $results */ |
|
218 | + $results = $model->get_all_wpdb_results($query_params); |
|
219 | + $nice_results = array(); |
|
220 | + foreach ($results as $result) { |
|
221 | + $nice_results[] = $this->create_entity_from_wpdb_result( |
|
222 | + $model, |
|
223 | + $result, |
|
224 | + $request |
|
225 | + ); |
|
226 | + } |
|
227 | + return $nice_results; |
|
228 | + } |
|
229 | + |
|
230 | + |
|
231 | + |
|
232 | + /** |
|
233 | + * @param array $primary_model_query_params query params for finding the item from which |
|
234 | + * relations will be based |
|
235 | + * @param \EE_Model_Relation_Base $relation |
|
236 | + * @param \WP_REST_Request $request |
|
237 | + * @return \WP_Error|array |
|
238 | + */ |
|
239 | + protected function _get_entities_from_relation($primary_model_query_params, $relation, $request) |
|
240 | + { |
|
241 | + $context = $this->validate_context($request->get_param('caps')); |
|
242 | + $model = $relation->get_this_model(); |
|
243 | + $related_model = $relation->get_other_model(); |
|
244 | + if (! isset($primary_model_query_params[0])) { |
|
245 | + $primary_model_query_params[0] = array(); |
|
246 | + } |
|
247 | + //check if they can access the 1st model object |
|
248 | + $primary_model_query_params = array( |
|
249 | + 0 => $primary_model_query_params[0], |
|
250 | + 'limit' => 1, |
|
251 | + ); |
|
252 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
253 | + $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($primary_model_query_params); |
|
254 | + } |
|
255 | + $restricted_query_params = $primary_model_query_params; |
|
256 | + $restricted_query_params['caps'] = $context; |
|
257 | + $this->_set_debug_info('main model query params', $restricted_query_params); |
|
258 | + $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); |
|
259 | + if ( |
|
260 | + ! ( |
|
261 | + Capabilities::current_user_has_partial_access_to($related_model, $context) |
|
262 | + && $model->exists($restricted_query_params) |
|
263 | + ) |
|
264 | + ) { |
|
265 | + if ($relation instanceof \EE_Belongs_To_Relation) { |
|
266 | + $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
267 | + } else { |
|
268 | + $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); |
|
269 | + } |
|
270 | + return new \WP_Error( |
|
271 | + sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
272 | + sprintf( |
|
273 | + __('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
274 | + 'event_espresso'), |
|
275 | + $related_model_name_maybe_plural, |
|
276 | + $relation->get_this_model()->get_this_model_name(), |
|
277 | + implode( |
|
278 | + ',', |
|
279 | + array_keys( |
|
280 | + Capabilities::get_missing_permissions($related_model, $context) |
|
281 | + ) |
|
282 | + ) |
|
283 | + ), |
|
284 | + array('status' => 403) |
|
285 | + ); |
|
286 | + } |
|
287 | + $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); |
|
288 | + foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
289 | + $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
290 | + . '.' |
|
291 | + . $where_condition_key] = $where_condition_value; |
|
292 | + } |
|
293 | + $query_params['default_where_conditions'] = 'none'; |
|
294 | + $query_params['caps'] = $context; |
|
295 | + if (! $request->get_header('no_rest_headers')) { |
|
296 | + $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
|
297 | + } |
|
298 | + /** @type array $results */ |
|
299 | + $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
300 | + $nice_results = array(); |
|
301 | + foreach ($results as $result) { |
|
302 | + $nice_result = $this->create_entity_from_wpdb_result( |
|
303 | + $relation->get_other_model(), |
|
304 | + $result, |
|
305 | + $request |
|
306 | + ); |
|
307 | + if ($relation instanceof \EE_HABTM_Relation) { |
|
308 | + //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
309 | + //if there are conflicts we prefer the properties from the main model |
|
310 | + $join_model_result = $this->create_entity_from_wpdb_result( |
|
311 | + $relation->get_join_model(), |
|
312 | + $result, |
|
313 | + $request |
|
314 | + ); |
|
315 | + $joined_result = array_merge($nice_result, $join_model_result); |
|
316 | + //but keep the meta stuff from the main model |
|
317 | + if (isset($nice_result['meta'])) { |
|
318 | + $joined_result['meta'] = $nice_result['meta']; |
|
319 | + } |
|
320 | + $nice_result = $joined_result; |
|
321 | + } |
|
322 | + $nice_results[] = $nice_result; |
|
323 | + } |
|
324 | + if ($relation instanceof \EE_Belongs_To_Relation) { |
|
325 | + return array_shift($nice_results); |
|
326 | + } else { |
|
327 | + return $nice_results; |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + |
|
333 | + /** |
|
334 | + * Gets the collection for given relation object |
|
335 | + * The same as Read::get_entities_from_model(), except if the relation |
|
336 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
337 | + * the join-model-object into the results |
|
338 | + * |
|
339 | + * @param string $id the ID of the thing we are fetching related stuff from |
|
340 | + * @param \EE_Model_Relation_Base $relation |
|
341 | + * @param \WP_REST_Request $request |
|
342 | + * @return array|\WP_Error |
|
343 | + * @throws \EE_Error |
|
344 | + */ |
|
345 | + public function get_entities_from_relation($id, $relation, $request) |
|
346 | + { |
|
347 | + if (! $relation->get_this_model()->has_primary_key_field()) { |
|
348 | + throw new \EE_Error( |
|
349 | + sprintf( |
|
350 | + __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
351 | + 'event_espresso'), |
|
352 | + $relation->get_this_model()->get_this_model_name() |
|
353 | + ) |
|
354 | + ); |
|
355 | + } |
|
356 | + return $this->_get_entities_from_relation( |
|
357 | + array( |
|
358 | + array( |
|
359 | + $relation->get_this_model()->primary_key_name() => $id, |
|
360 | + ), |
|
361 | + ), |
|
362 | + $relation, |
|
363 | + $request |
|
364 | + ); |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + |
|
369 | + /** |
|
370 | + * Sets the headers that are based on the model and query params, |
|
371 | + * like the total records. This should only be called on the original request |
|
372 | + * from the client, not on subsequent internal |
|
373 | + * |
|
374 | + * @param \EEM_Base $model |
|
375 | + * @param array $query_params |
|
376 | + * @return void |
|
377 | + */ |
|
378 | + protected function _set_headers_from_query_params($model, $query_params) |
|
379 | + { |
|
380 | + $this->_set_debug_info('model query params', $query_params); |
|
381 | + $this->_set_debug_info('missing caps', |
|
382 | + Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
|
383 | + //normally the limit to a 2-part array, where the 2nd item is the limit |
|
384 | + if (! isset($query_params['limit'])) { |
|
385 | + $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
386 | + } |
|
387 | + if (is_array($query_params['limit'])) { |
|
388 | + $limit_parts = $query_params['limit']; |
|
389 | + } else { |
|
390 | + $limit_parts = explode(',', $query_params['limit']); |
|
391 | + if (count($limit_parts) == 1) { |
|
392 | + $limit_parts = array(0, $limit_parts[0]); |
|
393 | + } |
|
394 | + } |
|
395 | + //remove the group by and having parts of the query, as those will |
|
396 | + //make the sql query return an array of values, instead of just a single value |
|
397 | + unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
398 | + $count = $model->count($query_params, null, true); |
|
399 | + $pages = $count / $limit_parts[1]; |
|
400 | + $this->_set_response_header('Total', $count, false); |
|
401 | + $this->_set_response_header('PageSize', $limit_parts[1], false); |
|
402 | + $this->_set_response_header('TotalPages', ceil($pages), false); |
|
403 | + } |
|
404 | + |
|
405 | + |
|
406 | + |
|
407 | + /** |
|
408 | + * Changes database results into REST API entities |
|
409 | + * |
|
410 | + * @param \EEM_Base $model |
|
411 | + * @param array $db_row like results from $wpdb->get_results() |
|
412 | + * @param \WP_REST_Request $rest_request |
|
413 | + * @param string $deprecated no longer used |
|
414 | + * @return array ready for being converted into json for sending to client |
|
415 | + */ |
|
416 | + public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
|
417 | + { |
|
418 | + if (! $rest_request instanceof \WP_REST_Request) { |
|
419 | + //ok so this was called in the old style, where the 3rd arg was |
|
420 | + //$include, and the 4th arg was $context |
|
421 | + //now setup the request just to avoid fatal errors, although we won't be able |
|
422 | + //to truly make use of it because it's kinda devoid of info |
|
423 | + $rest_request = new \WP_REST_Request(); |
|
424 | + $rest_request->set_param('include', $rest_request); |
|
425 | + $rest_request->set_param('caps', $deprecated); |
|
426 | + } |
|
427 | + if ($rest_request->get_param('caps') == null) { |
|
428 | + $rest_request->set_param('caps', \EEM_Base::caps_read); |
|
429 | + } |
|
430 | + $entity_array = $this->_create_bare_entity_from_wpdb_results($model, $db_row); |
|
431 | + $entity_array = $this->_add_extra_fields($model, $db_row, $entity_array); |
|
432 | + $entity_array['_links'] = $this->_get_entity_links($model, $db_row, $entity_array); |
|
433 | + $entity_array['_calculated_fields'] = $this->_get_entity_calculations($model, $db_row, $rest_request); |
|
434 | + $entity_array = $this->_include_requested_models($model, $rest_request, $entity_array, $db_row); |
|
435 | + $entity_array = apply_filters( |
|
436 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
437 | + $entity_array, |
|
438 | + $model, |
|
439 | + $rest_request->get_param('caps'), |
|
440 | + $rest_request, |
|
441 | + $this |
|
442 | + ); |
|
443 | + $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields( |
|
444 | + $entity_array, |
|
445 | + $model, |
|
446 | + $rest_request->get_param('caps'), |
|
447 | + $this->get_model_version_info(), |
|
448 | + $model->get_index_primary_key_string( |
|
449 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
450 | + ) |
|
451 | + ); |
|
452 | + $this->_set_debug_info( |
|
453 | + 'inaccessible fields', |
|
454 | + array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
455 | + ); |
|
456 | + return apply_filters( |
|
457 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
458 | + $result_without_inaccessible_fields, |
|
459 | + $model, |
|
460 | + $rest_request->get_param('caps') |
|
461 | + ); |
|
462 | + } |
|
463 | + |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * Creates a REST entity array (JSON object we're going to return in the response, but |
|
468 | + * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
469 | + * from $wpdb->get_row( $sql, ARRAY_A) |
|
470 | + * |
|
471 | + * @param \EEM_Base $model |
|
472 | + * @param array $db_row |
|
473 | + * @return array entity mostly ready for converting to JSON and sending in the response |
|
474 | + */ |
|
475 | + protected function _create_bare_entity_from_wpdb_results(\EEM_Base $model, $db_row) |
|
476 | + { |
|
477 | + $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
478 | + $result = array_intersect_key($result, |
|
479 | + $this->get_model_version_info()->fields_on_model_in_this_version($model)); |
|
480 | + foreach ($result as $field_name => $raw_field_value) { |
|
481 | + $field_obj = $model->field_settings_for($field_name); |
|
482 | + $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); |
|
483 | + if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { |
|
484 | + unset($result[$field_name]); |
|
485 | + } elseif ( |
|
486 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format()) |
|
487 | + ) { |
|
488 | + $result[$field_name] = array( |
|
489 | + 'raw' => $field_obj->prepare_for_get($field_value), |
|
490 | + 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
491 | + ); |
|
492 | + } elseif ( |
|
493 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format()) |
|
494 | + ) { |
|
495 | + $result[$field_name] = array( |
|
496 | + 'raw' => $field_obj->prepare_for_get($field_value), |
|
497 | + 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
498 | + ); |
|
499 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
500 | + if ($field_value instanceof \DateTime) { |
|
501 | + $timezone = $field_value->getTimezone(); |
|
502 | + $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
503 | + $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
504 | + $field_obj, |
|
505 | + $field_value, |
|
506 | + $this->get_model_version_info()->requested_version() |
|
507 | + ); |
|
508 | + $field_value->setTimezone($timezone); |
|
509 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
510 | + $field_obj, |
|
511 | + $field_value, |
|
512 | + $this->get_model_version_info()->requested_version() |
|
513 | + ); |
|
514 | + } |
|
515 | + } else { |
|
516 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
517 | + $field_obj, |
|
518 | + $field_obj->prepare_for_get($field_value), |
|
519 | + $this->get_model_version_info()->requested_version() |
|
520 | + ); |
|
521 | + } |
|
522 | + } |
|
523 | + return $result; |
|
524 | + } |
|
525 | + |
|
526 | + |
|
527 | + |
|
528 | + /** |
|
529 | + * Adds a few extra fields to the entity response |
|
530 | + * |
|
531 | + * @param \EEM_Base $model |
|
532 | + * @param array $db_row |
|
533 | + * @param array $entity_array |
|
534 | + * @return array modified entity |
|
535 | + */ |
|
536 | + protected function _add_extra_fields(\EEM_Base $model, $db_row, $entity_array) |
|
537 | + { |
|
538 | + if ($model instanceof \EEM_CPT_Base) { |
|
539 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
540 | + } |
|
541 | + return $entity_array; |
|
542 | + } |
|
543 | + |
|
544 | + |
|
545 | + |
|
546 | + /** |
|
547 | + * Gets links we want to add to the response |
|
548 | + * |
|
549 | + * @global \WP_REST_Server $wp_rest_server |
|
550 | + * @param \EEM_Base $model |
|
551 | + * @param array $db_row |
|
552 | + * @param array $entity_array |
|
553 | + * @return array the _links item in the entity |
|
554 | + */ |
|
555 | + protected function _get_entity_links($model, $db_row, $entity_array) |
|
556 | + { |
|
557 | + //add basic links |
|
558 | + $links = array(); |
|
559 | + if ($model->has_primary_key_field()) { |
|
560 | + $links['self'] = array( |
|
561 | + array( |
|
562 | + 'href' => $this->get_versioned_link_to( |
|
563 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
564 | + . '/' |
|
565 | + . $entity_array[$model->primary_key_name()] |
|
566 | + ), |
|
567 | + ), |
|
568 | + ); |
|
569 | + } |
|
570 | + $links['collection'] = array( |
|
571 | + array( |
|
572 | + 'href' => $this->get_versioned_link_to( |
|
573 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
574 | + ), |
|
575 | + ), |
|
576 | + ); |
|
577 | + //add links to related models |
|
578 | + if ($model->has_primary_key_field()) { |
|
579 | + foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
|
580 | + $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
|
581 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
582 | + array( |
|
583 | + 'href' => $this->get_versioned_link_to( |
|
584 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
585 | + . '/' |
|
586 | + . $entity_array[$model->primary_key_name()] |
|
587 | + . '/' |
|
588 | + . $related_model_part |
|
589 | + ), |
|
590 | + 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
591 | + ), |
|
592 | + ); |
|
593 | + } |
|
594 | + } |
|
595 | + return $links; |
|
596 | + } |
|
597 | + |
|
598 | + |
|
599 | + |
|
600 | + /** |
|
601 | + * Adds the included models indicated in the request to the entity provided |
|
602 | + * |
|
603 | + * @param \EEM_Base $model |
|
604 | + * @param \WP_REST_Request $rest_request |
|
605 | + * @param array $entity_array |
|
606 | + * @param array $db_row |
|
607 | + * @return array the modified entity |
|
608 | + */ |
|
609 | + protected function _include_requested_models( |
|
610 | + \EEM_Base $model, |
|
611 | + \WP_REST_Request $rest_request, |
|
612 | + $entity_array, |
|
613 | + $db_row = array() |
|
614 | + ) { |
|
615 | + //if $db_row not included, hope the entity array has what we need |
|
616 | + if (! $db_row) { |
|
617 | + $db_row = $entity_array; |
|
618 | + } |
|
619 | + $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
|
620 | + $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
|
621 | + //if they passed in * or didn't specify any includes, return everything |
|
622 | + if (! in_array('*', $includes_for_this_model) |
|
623 | + && ! empty($includes_for_this_model) |
|
624 | + ) { |
|
625 | + if ($model->has_primary_key_field()) { |
|
626 | + //always include the primary key. ya just gotta know that at least |
|
627 | + $includes_for_this_model[] = $model->primary_key_name(); |
|
628 | + } |
|
629 | + if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { |
|
630 | + $includes_for_this_model[] = '_calculated_fields'; |
|
631 | + } |
|
632 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
633 | + } |
|
634 | + $relation_settings = $this->get_model_version_info()->relation_settings($model); |
|
635 | + foreach ($relation_settings as $relation_name => $relation_obj) { |
|
636 | + $related_fields_to_include = $this->explode_and_get_items_prefixed_with( |
|
637 | + $rest_request->get_param('include'), |
|
638 | + $relation_name |
|
639 | + ); |
|
640 | + $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with( |
|
641 | + $rest_request->get_param('calculate'), |
|
642 | + $relation_name |
|
643 | + ); |
|
644 | + //did they specify they wanted to include a related model, or |
|
645 | + //specific fields from a related model? |
|
646 | + //or did they specify to calculate a field from a related model? |
|
647 | + if ($related_fields_to_include || $related_fields_to_calculate) { |
|
648 | + //if so, we should include at least some part of the related model |
|
649 | + $pretend_related_request = new \WP_REST_Request(); |
|
650 | + $pretend_related_request->set_query_params( |
|
651 | + array( |
|
652 | + 'caps' => $rest_request->get_param('caps'), |
|
653 | + 'include' => $related_fields_to_include, |
|
654 | + 'calculate' => $related_fields_to_calculate, |
|
655 | + ) |
|
656 | + ); |
|
657 | + $pretend_related_request->add_header('no_rest_headers', true); |
|
658 | + $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
659 | + $model->get_index_primary_key_string( |
|
660 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
661 | + ) |
|
662 | + ); |
|
663 | + $related_results = $this->_get_entities_from_relation( |
|
664 | + $primary_model_query_params, |
|
665 | + $relation_obj, |
|
666 | + $pretend_related_request |
|
667 | + ); |
|
668 | + $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results |
|
669 | + instanceof |
|
670 | + \WP_Error |
|
671 | + ? null |
|
672 | + : $related_results; |
|
673 | + } |
|
674 | + } |
|
675 | + return $entity_array; |
|
676 | + } |
|
677 | + |
|
678 | + |
|
679 | + |
|
680 | + /** |
|
681 | + * Returns a new array with all the names of models removed. Eg |
|
682 | + * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
683 | + * |
|
684 | + * @param array $arr |
|
685 | + * @return array |
|
686 | + */ |
|
687 | + private function _remove_model_names_from_array($arr) |
|
688 | + { |
|
689 | + return array_diff($arr, array_keys(\EE_Registry::instance()->non_abstract_db_models)); |
|
690 | + } |
|
691 | + |
|
692 | + |
|
693 | + |
|
694 | + /** |
|
695 | + * Gets the calculated fields for the response |
|
696 | + * |
|
697 | + * @param \EEM_Base $model |
|
698 | + * @param array $wpdb_row |
|
699 | + * @param \WP_REST_Request $rest_request |
|
700 | + * @return \stdClass the _calculations item in the entity |
|
701 | + */ |
|
702 | + protected function _get_entity_calculations($model, $wpdb_row, $rest_request) |
|
703 | + { |
|
704 | + $calculated_fields = $this->explode_and_get_items_prefixed_with( |
|
705 | + $rest_request->get_param('calculate'), |
|
706 | + '' |
|
707 | + ); |
|
708 | + //note: setting calculate=* doesn't do anything |
|
709 | + $calculated_fields_to_return = new \stdClass(); |
|
710 | + foreach ($calculated_fields as $field_to_calculate) { |
|
711 | + try { |
|
712 | + $calculated_fields_to_return->$field_to_calculate = Model_Data_Translator::prepare_field_value_for_json( |
|
713 | + null, |
|
714 | + $this->_fields_calculator->retrieve_calculated_field_value( |
|
715 | + $model, |
|
716 | + $field_to_calculate, |
|
717 | + $wpdb_row, |
|
718 | + $rest_request, |
|
719 | + $this |
|
720 | + ), |
|
721 | + $this->get_model_version_info()->requested_version() |
|
722 | + ); |
|
723 | + } catch (Rest_Exception $e) { |
|
724 | + //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
725 | + $this->_set_response_header( |
|
726 | + 'Notices-Field-Calculation-Errors[' |
|
727 | + . $e->get_string_code() |
|
728 | + . '][' |
|
729 | + . $model->get_this_model_name() |
|
730 | + . '][' |
|
731 | + . $field_to_calculate |
|
732 | + . ']', |
|
733 | + $e->getMessage(), |
|
734 | + true |
|
735 | + ); |
|
736 | + } |
|
737 | + } |
|
738 | + return $calculated_fields_to_return; |
|
739 | + } |
|
740 | + |
|
741 | + |
|
742 | + |
|
743 | + /** |
|
744 | + * Gets the full URL to the resource, taking the requested version into account |
|
745 | + * |
|
746 | + * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
747 | + * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
748 | + */ |
|
749 | + public function get_versioned_link_to($link_part_after_version_and_slash) |
|
750 | + { |
|
751 | + return rest_url( |
|
752 | + \EED_Core_Rest_Api::ee_api_namespace |
|
753 | + . $this->get_model_version_info()->requested_version() |
|
754 | + . '/' |
|
755 | + . $link_part_after_version_and_slash |
|
756 | + ); |
|
757 | + } |
|
758 | + |
|
759 | + |
|
760 | + |
|
761 | + /** |
|
762 | + * Gets the correct lowercase name for the relation in the API according |
|
763 | + * to the relation's type |
|
764 | + * |
|
765 | + * @param string $relation_name |
|
766 | + * @param \EE_Model_Relation_Base $relation_obj |
|
767 | + * @return string |
|
768 | + */ |
|
769 | + public static function get_related_entity_name($relation_name, $relation_obj) |
|
770 | + { |
|
771 | + if ($relation_obj instanceof \EE_Belongs_To_Relation) { |
|
772 | + return strtolower($relation_name); |
|
773 | + } else { |
|
774 | + return \EEH_Inflector::pluralize_and_lower($relation_name); |
|
775 | + } |
|
776 | + } |
|
777 | + |
|
778 | + |
|
779 | + |
|
780 | + /** |
|
781 | + * Gets the one model object with the specified id for the specified model |
|
782 | + * |
|
783 | + * @param \EEM_Base $model |
|
784 | + * @param \WP_REST_Request $request |
|
785 | + * @return array|\WP_Error |
|
786 | + */ |
|
787 | + public function get_entity_from_model($model, $request) |
|
788 | + { |
|
789 | + $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
790 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
791 | + $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
792 | + } |
|
793 | + $restricted_query_params = $query_params; |
|
794 | + $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
|
795 | + $this->_set_debug_info('model query params', $restricted_query_params); |
|
796 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
797 | + if (! empty ($model_rows)) { |
|
798 | + return $this->create_entity_from_wpdb_result( |
|
799 | + $model, |
|
800 | + array_shift($model_rows), |
|
801 | + $request); |
|
802 | + } else { |
|
803 | + //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
804 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
805 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
806 | + if (! empty($model_rows_found_sans_restrictions)) { |
|
807 | + //you got shafted- it existed but we didn't want to tell you! |
|
808 | + return new \WP_Error( |
|
809 | + 'rest_user_cannot_read', |
|
810 | + sprintf( |
|
811 | + __('Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso'), |
|
812 | + strtolower($model->get_this_model_name()), |
|
813 | + Capabilities::get_missing_permissions_string( |
|
814 | + $model, |
|
815 | + $this->validate_context($request->get_param('caps'))) |
|
816 | + ), |
|
817 | + array('status' => 403) |
|
818 | + ); |
|
819 | + } else { |
|
820 | + //it's not you. It just doesn't exist |
|
821 | + return new \WP_Error( |
|
822 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
823 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
824 | + array('status' => 404) |
|
825 | + ); |
|
826 | + } |
|
827 | + } |
|
828 | + } |
|
829 | + |
|
830 | + |
|
831 | + |
|
832 | + /** |
|
833 | + * If a context is provided which isn't valid, maybe it was added in a future |
|
834 | + * version so just treat it as a default read |
|
835 | + * |
|
836 | + * @param string $context |
|
837 | + * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
838 | + */ |
|
839 | + public function validate_context($context) |
|
840 | + { |
|
841 | + if (! $context) { |
|
842 | + $context = \EEM_Base::caps_read; |
|
843 | + } |
|
844 | + $valid_contexts = \EEM_Base::valid_cap_contexts(); |
|
845 | + if (in_array($context, $valid_contexts)) { |
|
846 | + return $context; |
|
847 | + } else { |
|
848 | + return \EEM_Base::caps_read; |
|
849 | + } |
|
850 | + } |
|
851 | + |
|
852 | + |
|
853 | + |
|
854 | + /** |
|
855 | + * Verifies the passed in value is an allowable default where conditions value. |
|
856 | + * |
|
857 | + * @param $default_query_params |
|
858 | + * @return string |
|
859 | + */ |
|
860 | + public function validate_default_query_params($default_query_params) |
|
861 | + { |
|
862 | + $valid_default_where_conditions_for_api_calls = array( |
|
863 | + \EEM_Base::default_where_conditions_all, |
|
864 | + \EEM_Base::default_where_conditions_minimum_all, |
|
865 | + \EEM_Base::default_where_conditions_minimum_others, |
|
866 | + ); |
|
867 | + if (! $default_query_params) { |
|
868 | + $default_query_params = \EEM_Base::default_where_conditions_all; |
|
869 | + } |
|
870 | + if ( |
|
871 | + in_array( |
|
872 | + $default_query_params, |
|
873 | + $valid_default_where_conditions_for_api_calls, |
|
874 | + true |
|
875 | + ) |
|
876 | + ) { |
|
877 | + return $default_query_params; |
|
878 | + } else { |
|
879 | + return \EEM_Base::default_where_conditions_all; |
|
880 | + } |
|
881 | + } |
|
882 | + |
|
883 | + |
|
884 | + |
|
885 | + /** |
|
886 | + * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
887 | + * Note: right now the query parameter keys for fields (and related fields) |
|
888 | + * can be left as-is, but it's quite possible this will change someday. |
|
889 | + * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
890 | + * |
|
891 | + * @param \EEM_Base $model |
|
892 | + * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
893 | + * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
894 | + * that absolutely no results should be returned |
|
895 | + * @throws \EE_Error |
|
896 | + */ |
|
897 | + public function create_model_query_params($model, $query_parameters) |
|
898 | + { |
|
899 | + $model_query_params = array(); |
|
900 | + if (isset($query_parameters['where'])) { |
|
901 | + $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
902 | + $query_parameters['where'], |
|
903 | + $model, |
|
904 | + $this->get_model_version_info()->requested_version() |
|
905 | + ); |
|
906 | + } |
|
907 | + if (isset($query_parameters['order_by'])) { |
|
908 | + $order_by = $query_parameters['order_by']; |
|
909 | + } elseif (isset($query_parameters['orderby'])) { |
|
910 | + $order_by = $query_parameters['orderby']; |
|
911 | + } else { |
|
912 | + $order_by = null; |
|
913 | + } |
|
914 | + if ($order_by !== null) { |
|
915 | + if (is_array($order_by)) { |
|
916 | + $order_by = Model_Data_Translator::prepare_field_names_in_array_keys_from_json($order_by); |
|
917 | + } else { |
|
918 | + //it's a single item |
|
919 | + $order_by = Model_Data_Translator::prepare_field_name_from_json($order_by); |
|
920 | + } |
|
921 | + $model_query_params['order_by'] = $order_by; |
|
922 | + } |
|
923 | + if (isset($query_parameters['group_by'])) { |
|
924 | + $group_by = $query_parameters['group_by']; |
|
925 | + } elseif (isset($query_parameters['groupby'])) { |
|
926 | + $group_by = $query_parameters['groupby']; |
|
927 | + } else { |
|
928 | + $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
929 | + } |
|
930 | + //make sure they're all real names |
|
931 | + if (is_array($group_by)) { |
|
932 | + $group_by = Model_Data_Translator::prepare_field_names_from_json($group_by); |
|
933 | + } |
|
934 | + if ($group_by !== null) { |
|
935 | + $model_query_params['group_by'] = $group_by; |
|
936 | + } |
|
937 | + if (isset($query_parameters['having'])) { |
|
938 | + $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
939 | + $query_parameters['having'], |
|
940 | + $model, |
|
941 | + $this->get_model_version_info()->requested_version() |
|
942 | + ); |
|
943 | + } |
|
944 | + if (isset($query_parameters['order'])) { |
|
945 | + $model_query_params['order'] = $query_parameters['order']; |
|
946 | + } |
|
947 | + if (isset($query_parameters['mine'])) { |
|
948 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
949 | + } |
|
950 | + if (isset($query_parameters['limit'])) { |
|
951 | + //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
952 | + if (! is_array($query_parameters['limit'])) { |
|
953 | + $limit_array = explode(',', (string)$query_parameters['limit']); |
|
954 | + } else { |
|
955 | + $limit_array = $query_parameters['limit']; |
|
956 | + } |
|
957 | + $sanitized_limit = array(); |
|
958 | + foreach ($limit_array as $key => $limit_part) { |
|
959 | + if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
960 | + throw new \EE_Error( |
|
961 | + sprintf( |
|
962 | + __('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
963 | + 'event_espresso'), |
|
964 | + wp_json_encode($query_parameters['limit']) |
|
965 | + ) |
|
966 | + ); |
|
967 | + } |
|
968 | + $sanitized_limit[] = (int)$limit_part; |
|
969 | + } |
|
970 | + $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
971 | + } else { |
|
972 | + $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
973 | + } |
|
974 | + if (isset($query_parameters['caps'])) { |
|
975 | + $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); |
|
976 | + } else { |
|
977 | + $model_query_params['caps'] = \EEM_Base::caps_read; |
|
978 | + } |
|
979 | + if (isset($query_parameters['default_where_conditions'])) { |
|
980 | + $model_query_params['default_where_conditions'] = $this->validate_default_query_params($query_parameters['default_where_conditions']); |
|
981 | + } |
|
982 | + return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
983 | + } |
|
984 | + |
|
985 | + |
|
986 | + |
|
987 | + /** |
|
988 | + * Changes the REST-style query params for use in the models |
|
989 | + * |
|
990 | + * @deprecated |
|
991 | + * @param \EEM_Base $model |
|
992 | + * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
993 | + * @return array |
|
994 | + */ |
|
995 | + public function prepare_rest_query_params_key_for_models($model, $query_params) |
|
996 | + { |
|
997 | + $model_ready_query_params = array(); |
|
998 | + foreach ($query_params as $key => $value) { |
|
999 | + if (is_array($value)) { |
|
1000 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_key_for_models($model, $value); |
|
1001 | + } else { |
|
1002 | + $model_ready_query_params[$key] = $value; |
|
1003 | + } |
|
1004 | + } |
|
1005 | + return $model_ready_query_params; |
|
1006 | + } |
|
1007 | + |
|
1008 | + |
|
1009 | + |
|
1010 | + /** |
|
1011 | + * @deprecated |
|
1012 | + * @param $model |
|
1013 | + * @param $query_params |
|
1014 | + * @return array |
|
1015 | + */ |
|
1016 | + public function prepare_rest_query_params_values_for_models($model, $query_params) |
|
1017 | + { |
|
1018 | + $model_ready_query_params = array(); |
|
1019 | + foreach ($query_params as $key => $value) { |
|
1020 | + if (is_array($value)) { |
|
1021 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_values_for_models($model, $value); |
|
1022 | + } else { |
|
1023 | + $model_ready_query_params[$key] = $value; |
|
1024 | + } |
|
1025 | + } |
|
1026 | + return $model_ready_query_params; |
|
1027 | + } |
|
1028 | + |
|
1029 | + |
|
1030 | + |
|
1031 | + /** |
|
1032 | + * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1033 | + * If no prefix is specified, returns items with no period. |
|
1034 | + * |
|
1035 | + * @param string|array $string_to_explode eg "jibba,jabba, blah, blaabla" or array('jibba', 'jabba' ) |
|
1036 | + * @param string $prefix "Event" or "foobar" |
|
1037 | + * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1038 | + * we only return strings starting with that and a period; if no prefix was |
|
1039 | + * specified we return all items containing NO periods |
|
1040 | + */ |
|
1041 | + public function explode_and_get_items_prefixed_with($string_to_explode, $prefix) |
|
1042 | + { |
|
1043 | + if (is_string($string_to_explode)) { |
|
1044 | + $exploded_contents = explode(',', $string_to_explode); |
|
1045 | + } else if (is_array($string_to_explode)) { |
|
1046 | + $exploded_contents = $string_to_explode; |
|
1047 | + } else { |
|
1048 | + $exploded_contents = array(); |
|
1049 | + } |
|
1050 | + //if the string was empty, we want an empty array |
|
1051 | + $exploded_contents = array_filter($exploded_contents); |
|
1052 | + $contents_with_prefix = array(); |
|
1053 | + foreach ($exploded_contents as $item) { |
|
1054 | + $item = trim($item); |
|
1055 | + //if no prefix was provided, so we look for items with no "." in them |
|
1056 | + if (! $prefix) { |
|
1057 | + //does this item have a period? |
|
1058 | + if (strpos($item, '.') === false) { |
|
1059 | + //if not, then its what we're looking for |
|
1060 | + $contents_with_prefix[] = $item; |
|
1061 | + } |
|
1062 | + } else if (strpos($item, $prefix . '.') === 0) { |
|
1063 | + //this item has the prefix and a period, grab it |
|
1064 | + $contents_with_prefix[] = substr( |
|
1065 | + $item, |
|
1066 | + strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1067 | + ); |
|
1068 | + } else if ($item === $prefix) { |
|
1069 | + //this item is JUST the prefix |
|
1070 | + //so let's grab everything after, which is a blank string |
|
1071 | + $contents_with_prefix[] = ''; |
|
1072 | + } |
|
1073 | + } |
|
1074 | + return $contents_with_prefix; |
|
1075 | + } |
|
1076 | + |
|
1077 | + |
|
1078 | + |
|
1079 | + /** |
|
1080 | + * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1081 | + * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1082 | + * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1083 | + * array('*') (when you provided a model and a model of that kind was found). |
|
1084 | + * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1085 | + * (ie have NO period in them), or for the provided model (ie start with the model |
|
1086 | + * name and then a period). |
|
1087 | + * @param string $include_string @see Read:handle_request_get_all |
|
1088 | + * @param string $model_name |
|
1089 | + * @return array of fields for this model. If $model_name is provided, then |
|
1090 | + * the fields for that model, with the model's name removed from each. |
|
1091 | + * If $include_string was blank or '*' returns an empty array |
|
1092 | + */ |
|
1093 | + public function extract_includes_for_this_model($include_string, $model_name = null) |
|
1094 | + { |
|
1095 | + if (is_array($include_string)) { |
|
1096 | + $include_string = implode(',', $include_string); |
|
1097 | + } |
|
1098 | + if ($include_string === '*' || $include_string === '') { |
|
1099 | + return array(); |
|
1100 | + } |
|
1101 | + $includes = explode(',', $include_string); |
|
1102 | + $extracted_fields_to_include = array(); |
|
1103 | + if ($model_name) { |
|
1104 | + foreach ($includes as $field_to_include) { |
|
1105 | + $field_to_include = trim($field_to_include); |
|
1106 | + if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1107 | + //found the model name at the exact start |
|
1108 | + $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1109 | + $extracted_fields_to_include[] = $field_sans_model_name; |
|
1110 | + } elseif ($field_to_include == $model_name) { |
|
1111 | + $extracted_fields_to_include[] = '*'; |
|
1112 | + } |
|
1113 | + } |
|
1114 | + } else { |
|
1115 | + //look for ones with no period |
|
1116 | + foreach ($includes as $field_to_include) { |
|
1117 | + $field_to_include = trim($field_to_include); |
|
1118 | + if ( |
|
1119 | + strpos($field_to_include, '.') === false |
|
1120 | + && ! $this->get_model_version_info()->is_model_name_in_this_version($field_to_include) |
|
1121 | + ) { |
|
1122 | + $extracted_fields_to_include[] = $field_to_include; |
|
1123 | + } |
|
1124 | + } |
|
1125 | + } |
|
1126 | + return $extracted_fields_to_include; |
|
1127 | + } |
|
1128 | 1128 | } |
1129 | 1129 | |
1130 | 1130 |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | use EventEspresso\core\libraries\rest_api\Rest_Exception; |
7 | 7 | use EventEspresso\core\libraries\rest_api\Model_Data_Translator; |
8 | 8 | |
9 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
9 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
10 | 10 | exit('No direct script access allowed'); |
11 | 11 | } |
12 | 12 | |
@@ -55,12 +55,12 @@ discard block |
||
55 | 55 | try { |
56 | 56 | $matches = $controller->parse_route( |
57 | 57 | $request->get_route(), |
58 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
58 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)~', |
|
59 | 59 | array('version', 'model') |
60 | 60 | ); |
61 | 61 | $controller->set_requested_version($matches['version']); |
62 | 62 | $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
63 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
63 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
64 | 64 | return $controller->send_response( |
65 | 65 | new \WP_Error( |
66 | 66 | 'endpoint_parsing_error', |
@@ -97,11 +97,11 @@ discard block |
||
97 | 97 | try { |
98 | 98 | $matches = $controller->parse_route( |
99 | 99 | $request->get_route(), |
100 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
100 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)~', |
|
101 | 101 | array('version', 'model', 'id')); |
102 | 102 | $controller->set_requested_version($matches['version']); |
103 | 103 | $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
104 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
104 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
105 | 105 | return $controller->send_response( |
106 | 106 | new \WP_Error( |
107 | 107 | 'endpoint_parsing_error', |
@@ -139,12 +139,12 @@ discard block |
||
139 | 139 | try { |
140 | 140 | $matches = $controller->parse_route( |
141 | 141 | $request->get_route(), |
142 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
142 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)/(.*)~', |
|
143 | 143 | array('version', 'model', 'id', 'related_model') |
144 | 144 | ); |
145 | 145 | $controller->set_requested_version($matches['version']); |
146 | 146 | $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
147 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
147 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
148 | 148 | return $controller->send_response( |
149 | 149 | new \WP_Error( |
150 | 150 | 'endpoint_parsing_error', |
@@ -159,11 +159,11 @@ discard block |
||
159 | 159 | $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
160 | 160 | //assume the related model name is plural and try to find the model's name |
161 | 161 | $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
162 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
162 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
163 | 163 | //so the word didn't singularize well. Maybe that's just because it's a singular word? |
164 | 164 | $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
165 | 165 | } |
166 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
166 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
167 | 167 | return $controller->send_response( |
168 | 168 | new \WP_Error( |
169 | 169 | 'endpoint_parsing_error', |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | public function get_entities_from_model($model, $request) |
200 | 200 | { |
201 | 201 | $query_params = $this->create_model_query_params($model, $request->get_params()); |
202 | - if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
202 | + if ( ! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
203 | 203 | $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
204 | 204 | return new \WP_Error( |
205 | 205 | sprintf('rest_%s_cannot_list', $model_name_plural), |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | array('status' => 403) |
212 | 212 | ); |
213 | 213 | } |
214 | - if (! $request->get_header('no_rest_headers')) { |
|
214 | + if ( ! $request->get_header('no_rest_headers')) { |
|
215 | 215 | $this->_set_headers_from_query_params($model, $query_params); |
216 | 216 | } |
217 | 217 | /** @type array $results */ |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | $context = $this->validate_context($request->get_param('caps')); |
242 | 242 | $model = $relation->get_this_model(); |
243 | 243 | $related_model = $relation->get_other_model(); |
244 | - if (! isset($primary_model_query_params[0])) { |
|
244 | + if ( ! isset($primary_model_query_params[0])) { |
|
245 | 245 | $primary_model_query_params[0] = array(); |
246 | 246 | } |
247 | 247 | //check if they can access the 1st model object |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | } |
293 | 293 | $query_params['default_where_conditions'] = 'none'; |
294 | 294 | $query_params['caps'] = $context; |
295 | - if (! $request->get_header('no_rest_headers')) { |
|
295 | + if ( ! $request->get_header('no_rest_headers')) { |
|
296 | 296 | $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
297 | 297 | } |
298 | 298 | /** @type array $results */ |
@@ -344,7 +344,7 @@ discard block |
||
344 | 344 | */ |
345 | 345 | public function get_entities_from_relation($id, $relation, $request) |
346 | 346 | { |
347 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
347 | + if ( ! $relation->get_this_model()->has_primary_key_field()) { |
|
348 | 348 | throw new \EE_Error( |
349 | 349 | sprintf( |
350 | 350 | __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
@@ -381,7 +381,7 @@ discard block |
||
381 | 381 | $this->_set_debug_info('missing caps', |
382 | 382 | Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
383 | 383 | //normally the limit to a 2-part array, where the 2nd item is the limit |
384 | - if (! isset($query_params['limit'])) { |
|
384 | + if ( ! isset($query_params['limit'])) { |
|
385 | 385 | $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
386 | 386 | } |
387 | 387 | if (is_array($query_params['limit'])) { |
@@ -415,7 +415,7 @@ discard block |
||
415 | 415 | */ |
416 | 416 | public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
417 | 417 | { |
418 | - if (! $rest_request instanceof \WP_REST_Request) { |
|
418 | + if ( ! $rest_request instanceof \WP_REST_Request) { |
|
419 | 419 | //ok so this was called in the old style, where the 3rd arg was |
420 | 420 | //$include, and the 4th arg was $context |
421 | 421 | //now setup the request just to avoid fatal errors, although we won't be able |
@@ -500,7 +500,7 @@ discard block |
||
500 | 500 | if ($field_value instanceof \DateTime) { |
501 | 501 | $timezone = $field_value->getTimezone(); |
502 | 502 | $field_value->setTimezone(new \DateTimeZone('UTC')); |
503 | - $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
503 | + $result[$field_name.'_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
504 | 504 | $field_obj, |
505 | 505 | $field_value, |
506 | 506 | $this->get_model_version_info()->requested_version() |
@@ -578,7 +578,7 @@ discard block |
||
578 | 578 | if ($model->has_primary_key_field()) { |
579 | 579 | foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
580 | 580 | $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
581 | - $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
581 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( |
|
582 | 582 | array( |
583 | 583 | 'href' => $this->get_versioned_link_to( |
584 | 584 | \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
@@ -613,13 +613,13 @@ discard block |
||
613 | 613 | $db_row = array() |
614 | 614 | ) { |
615 | 615 | //if $db_row not included, hope the entity array has what we need |
616 | - if (! $db_row) { |
|
616 | + if ( ! $db_row) { |
|
617 | 617 | $db_row = $entity_array; |
618 | 618 | } |
619 | 619 | $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
620 | 620 | $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
621 | 621 | //if they passed in * or didn't specify any includes, return everything |
622 | - if (! in_array('*', $includes_for_this_model) |
|
622 | + if ( ! in_array('*', $includes_for_this_model) |
|
623 | 623 | && ! empty($includes_for_this_model) |
624 | 624 | ) { |
625 | 625 | if ($model->has_primary_key_field()) { |
@@ -794,7 +794,7 @@ discard block |
||
794 | 794 | $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
795 | 795 | $this->_set_debug_info('model query params', $restricted_query_params); |
796 | 796 | $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
797 | - if (! empty ($model_rows)) { |
|
797 | + if ( ! empty ($model_rows)) { |
|
798 | 798 | return $this->create_entity_from_wpdb_result( |
799 | 799 | $model, |
800 | 800 | array_shift($model_rows), |
@@ -803,7 +803,7 @@ discard block |
||
803 | 803 | //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
804 | 804 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
805 | 805 | $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
806 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
806 | + if ( ! empty($model_rows_found_sans_restrictions)) { |
|
807 | 807 | //you got shafted- it existed but we didn't want to tell you! |
808 | 808 | return new \WP_Error( |
809 | 809 | 'rest_user_cannot_read', |
@@ -838,7 +838,7 @@ discard block |
||
838 | 838 | */ |
839 | 839 | public function validate_context($context) |
840 | 840 | { |
841 | - if (! $context) { |
|
841 | + if ( ! $context) { |
|
842 | 842 | $context = \EEM_Base::caps_read; |
843 | 843 | } |
844 | 844 | $valid_contexts = \EEM_Base::valid_cap_contexts(); |
@@ -864,7 +864,7 @@ discard block |
||
864 | 864 | \EEM_Base::default_where_conditions_minimum_all, |
865 | 865 | \EEM_Base::default_where_conditions_minimum_others, |
866 | 866 | ); |
867 | - if (! $default_query_params) { |
|
867 | + if ( ! $default_query_params) { |
|
868 | 868 | $default_query_params = \EEM_Base::default_where_conditions_all; |
869 | 869 | } |
870 | 870 | if ( |
@@ -949,14 +949,14 @@ discard block |
||
949 | 949 | } |
950 | 950 | if (isset($query_parameters['limit'])) { |
951 | 951 | //limit should be either a string like '23' or '23,43', or an array with two items in it |
952 | - if (! is_array($query_parameters['limit'])) { |
|
953 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
952 | + if ( ! is_array($query_parameters['limit'])) { |
|
953 | + $limit_array = explode(',', (string) $query_parameters['limit']); |
|
954 | 954 | } else { |
955 | 955 | $limit_array = $query_parameters['limit']; |
956 | 956 | } |
957 | 957 | $sanitized_limit = array(); |
958 | 958 | foreach ($limit_array as $key => $limit_part) { |
959 | - if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
959 | + if ($this->_debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
960 | 960 | throw new \EE_Error( |
961 | 961 | sprintf( |
962 | 962 | __('An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
@@ -965,7 +965,7 @@ discard block |
||
965 | 965 | ) |
966 | 966 | ); |
967 | 967 | } |
968 | - $sanitized_limit[] = (int)$limit_part; |
|
968 | + $sanitized_limit[] = (int) $limit_part; |
|
969 | 969 | } |
970 | 970 | $model_query_params['limit'] = implode(',', $sanitized_limit); |
971 | 971 | } else { |
@@ -1053,17 +1053,17 @@ discard block |
||
1053 | 1053 | foreach ($exploded_contents as $item) { |
1054 | 1054 | $item = trim($item); |
1055 | 1055 | //if no prefix was provided, so we look for items with no "." in them |
1056 | - if (! $prefix) { |
|
1056 | + if ( ! $prefix) { |
|
1057 | 1057 | //does this item have a period? |
1058 | 1058 | if (strpos($item, '.') === false) { |
1059 | 1059 | //if not, then its what we're looking for |
1060 | 1060 | $contents_with_prefix[] = $item; |
1061 | 1061 | } |
1062 | - } else if (strpos($item, $prefix . '.') === 0) { |
|
1062 | + } else if (strpos($item, $prefix.'.') === 0) { |
|
1063 | 1063 | //this item has the prefix and a period, grab it |
1064 | 1064 | $contents_with_prefix[] = substr( |
1065 | 1065 | $item, |
1066 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1066 | + strpos($item, $prefix.'.') + strlen($prefix.'.') |
|
1067 | 1067 | ); |
1068 | 1068 | } else if ($item === $prefix) { |
1069 | 1069 | //this item is JUST the prefix |
@@ -1103,9 +1103,9 @@ discard block |
||
1103 | 1103 | if ($model_name) { |
1104 | 1104 | foreach ($includes as $field_to_include) { |
1105 | 1105 | $field_to_include = trim($field_to_include); |
1106 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1106 | + if (strpos($field_to_include, $model_name.'.') === 0) { |
|
1107 | 1107 | //found the model name at the exact start |
1108 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1108 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); |
|
1109 | 1109 | $extracted_fields_to_include[] = $field_sans_model_name; |
1110 | 1110 | } elseif ($field_to_include == $model_name) { |
1111 | 1111 | $extracted_fields_to_include[] = '*'; |
@@ -17,216 +17,214 @@ |
||
17 | 17 | class TableManager extends \EE_Base |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var TableAnalysis $table_analysis |
|
22 | - */ |
|
23 | - private $table_analysis; |
|
24 | - |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * TableManager constructor. |
|
29 | - * |
|
30 | - * @param TableAnalysis $TableAnalysis |
|
31 | - */ |
|
32 | - public function __construct(TableAnalysis $TableAnalysis) |
|
33 | - { |
|
34 | - $this->table_analysis = $TableAnalysis; |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * Gets the injected table analyzer, or throws an exception |
|
41 | - * |
|
42 | - * @return TableAnalysis |
|
43 | - * @throws \EE_Error |
|
44 | - */ |
|
45 | - protected function getTableAnalysis() |
|
46 | - { |
|
47 | - if ($this->table_analysis instanceof TableAnalysis) { |
|
48 | - return $this->table_analysis; |
|
49 | - } else { |
|
50 | - throw new \EE_Error( |
|
51 | - sprintf( |
|
52 | - __('Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
53 | - get_class($this) |
|
54 | - ) |
|
55 | - ); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * @param string $table_name which can optionally start with $wpdb->prefix or not |
|
63 | - * @param string $column_name |
|
64 | - * @param string $column_info |
|
65 | - * @return bool|false|int |
|
66 | - */ |
|
67 | - public function addColumn($table_name, $column_name, $column_info = 'INT UNSIGNED NOT NULL') |
|
68 | - { |
|
69 | - if (apply_filters('FHEE__EEH_Activation__add_column_if_it_doesnt_exist__short_circuit', false)) { |
|
70 | - return false; |
|
71 | - } |
|
72 | - global $wpdb; |
|
73 | - $full_table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
74 | - $columns = $this->getTableColumns($table_name); |
|
75 | - if ( ! in_array($column_name, $columns)) { |
|
76 | - $alter_query = "ALTER TABLE {$full_table_name} ADD {$column_name} {$column_info}"; |
|
77 | - return $wpdb->query($alter_query); |
|
78 | - } |
|
79 | - return true; |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Gets the name of all columns on the table. $table_name can |
|
86 | - * optionally start with $wpdb->prefix or not |
|
87 | - * |
|
88 | - * @global \wpdb $wpdb |
|
89 | - * @param string $table_name |
|
90 | - * @return array |
|
91 | - */ |
|
92 | - public function getTableColumns($table_name) |
|
93 | - { |
|
94 | - global $wpdb; |
|
95 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
96 | - $field_array = array(); |
|
97 | - if ( ! empty($table_name)) { |
|
98 | - $columns = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} "); |
|
99 | - if ($columns !== false) { |
|
100 | - foreach ($columns as $column) { |
|
101 | - $field_array[] = $column->Field; |
|
102 | - } |
|
103 | - } |
|
104 | - } |
|
105 | - return $field_array; |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * Drops the specified table from the database. $table_name can |
|
112 | - * optionally start with $wpdb->prefix or not |
|
113 | - * |
|
114 | - * @global \wpdb $wpdb |
|
115 | - * @param string $table_name |
|
116 | - * @return int |
|
117 | - */ |
|
118 | - public function dropTable($table_name) |
|
119 | - { |
|
120 | - global $wpdb; |
|
121 | - if ($this->getTableAnalysis()->tableExists($table_name)) { |
|
122 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
123 | - return $wpdb->query("DROP TABLE IF EXISTS {$table_name}"); |
|
124 | - } |
|
125 | - return 0; |
|
126 | - } |
|
127 | - |
|
128 | - |
|
129 | - |
|
130 | - /** |
|
131 | - * Drops all the tables mentioned in a single MYSQL query. Double-checks |
|
132 | - * each table name provided has a wpdb prefix attached, and that it exists. |
|
133 | - * Returns the list actually deleted |
|
134 | - * |
|
135 | - * @global WPDB $wpdb |
|
136 | - * @param array $table_names |
|
137 | - * @return array of table names which we deleted |
|
138 | - */ |
|
139 | - public function dropTables($table_names) |
|
140 | - { |
|
141 | - $tables_to_delete = array(); |
|
142 | - foreach ($table_names as $table_name) { |
|
143 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
144 | - if ($this->getTableAnalysis()->tableExists($table_name)) { |
|
145 | - $tables_to_delete[] = $table_name; |
|
146 | - } |
|
147 | - } |
|
148 | - if( ! empty( $tables_to_delete ) ) { |
|
149 | - global $wpdb; |
|
150 | - $wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete)); |
|
151 | - } |
|
152 | - return $tables_to_delete; |
|
153 | - } |
|
154 | - |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * Drops the specified index from the specified table. $table_name can |
|
159 | - * optionally start with $wpdb->prefix or not |
|
160 | - |
|
161 | - * |
|
20 | + /** |
|
21 | + * @var TableAnalysis $table_analysis |
|
22 | + */ |
|
23 | + private $table_analysis; |
|
24 | + |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * TableManager constructor. |
|
29 | + * |
|
30 | + * @param TableAnalysis $TableAnalysis |
|
31 | + */ |
|
32 | + public function __construct(TableAnalysis $TableAnalysis) |
|
33 | + { |
|
34 | + $this->table_analysis = $TableAnalysis; |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * Gets the injected table analyzer, or throws an exception |
|
41 | + * |
|
42 | + * @return TableAnalysis |
|
43 | + * @throws \EE_Error |
|
44 | + */ |
|
45 | + protected function getTableAnalysis() |
|
46 | + { |
|
47 | + if ($this->table_analysis instanceof TableAnalysis) { |
|
48 | + return $this->table_analysis; |
|
49 | + } else { |
|
50 | + throw new \EE_Error( |
|
51 | + sprintf( |
|
52 | + __('Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
53 | + get_class($this) |
|
54 | + ) |
|
55 | + ); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * @param string $table_name which can optionally start with $wpdb->prefix or not |
|
63 | + * @param string $column_name |
|
64 | + * @param string $column_info |
|
65 | + * @return bool|false|int |
|
66 | + */ |
|
67 | + public function addColumn($table_name, $column_name, $column_info = 'INT UNSIGNED NOT NULL') |
|
68 | + { |
|
69 | + if (apply_filters('FHEE__EEH_Activation__add_column_if_it_doesnt_exist__short_circuit', false)) { |
|
70 | + return false; |
|
71 | + } |
|
72 | + global $wpdb; |
|
73 | + $full_table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
74 | + $columns = $this->getTableColumns($table_name); |
|
75 | + if ( ! in_array($column_name, $columns)) { |
|
76 | + $alter_query = "ALTER TABLE {$full_table_name} ADD {$column_name} {$column_info}"; |
|
77 | + return $wpdb->query($alter_query); |
|
78 | + } |
|
79 | + return true; |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Gets the name of all columns on the table. $table_name can |
|
86 | + * optionally start with $wpdb->prefix or not |
|
87 | + * |
|
88 | + * @global \wpdb $wpdb |
|
89 | + * @param string $table_name |
|
90 | + * @return array |
|
91 | + */ |
|
92 | + public function getTableColumns($table_name) |
|
93 | + { |
|
94 | + global $wpdb; |
|
95 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
96 | + $field_array = array(); |
|
97 | + if ( ! empty($table_name)) { |
|
98 | + $columns = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} "); |
|
99 | + if ($columns !== false) { |
|
100 | + foreach ($columns as $column) { |
|
101 | + $field_array[] = $column->Field; |
|
102 | + } |
|
103 | + } |
|
104 | + } |
|
105 | + return $field_array; |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * Drops the specified table from the database. $table_name can |
|
112 | + * optionally start with $wpdb->prefix or not |
|
113 | + * |
|
114 | + * @global \wpdb $wpdb |
|
115 | + * @param string $table_name |
|
116 | + * @return int |
|
117 | + */ |
|
118 | + public function dropTable($table_name) |
|
119 | + { |
|
120 | + global $wpdb; |
|
121 | + if ($this->getTableAnalysis()->tableExists($table_name)) { |
|
122 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
123 | + return $wpdb->query("DROP TABLE IF EXISTS {$table_name}"); |
|
124 | + } |
|
125 | + return 0; |
|
126 | + } |
|
127 | + |
|
128 | + |
|
129 | + |
|
130 | + /** |
|
131 | + * Drops all the tables mentioned in a single MYSQL query. Double-checks |
|
132 | + * each table name provided has a wpdb prefix attached, and that it exists. |
|
133 | + * Returns the list actually deleted |
|
134 | + * |
|
135 | + * @global WPDB $wpdb |
|
136 | + * @param array $table_names |
|
137 | + * @return array of table names which we deleted |
|
138 | + */ |
|
139 | + public function dropTables($table_names) |
|
140 | + { |
|
141 | + $tables_to_delete = array(); |
|
142 | + foreach ($table_names as $table_name) { |
|
143 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
144 | + if ($this->getTableAnalysis()->tableExists($table_name)) { |
|
145 | + $tables_to_delete[] = $table_name; |
|
146 | + } |
|
147 | + } |
|
148 | + if( ! empty( $tables_to_delete ) ) { |
|
149 | + global $wpdb; |
|
150 | + $wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete)); |
|
151 | + } |
|
152 | + return $tables_to_delete; |
|
153 | + } |
|
154 | + |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * Drops the specified index from the specified table. $table_name can |
|
159 | + * optionally start with $wpdb->prefix or not |
|
160 | + * |
|
162 | 161 | *@global \wpdb $wpdb |
163 | - * @param string $table_name |
|
164 | - * @param string $index_name |
|
165 | - * @return int |
|
166 | - */ |
|
167 | - public function dropIndex($table_name, $index_name) |
|
168 | - { |
|
169 | - if (apply_filters('FHEE__EEH_Activation__drop_index__short_circuit', false)) { |
|
170 | - return false; |
|
171 | - } |
|
172 | - global $wpdb; |
|
173 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
174 | - $index_exists_query = "SHOW INDEX FROM {$table_name} WHERE Key_name = '{$index_name}'"; |
|
175 | - if ( |
|
176 | - $this->getTableAnalysis()->tableExists($table_name) |
|
177 | - && $wpdb->get_var($index_exists_query) |
|
178 | - === $table_name //using get_var with the $index_exists_query returns the table's name |
|
179 | - ) { |
|
180 | - return $wpdb->query("ALTER TABLE {$table_name} DROP INDEX {$index_name}"); |
|
181 | - } |
|
182 | - return 0; |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * Just creates the requested table. $table_name can |
|
189 | - * optionally start with $wpdb->prefix or not |
|
190 | - |
|
191 | - * |
|
162 | + * @param string $table_name |
|
163 | + * @param string $index_name |
|
164 | + * @return int |
|
165 | + */ |
|
166 | + public function dropIndex($table_name, $index_name) |
|
167 | + { |
|
168 | + if (apply_filters('FHEE__EEH_Activation__drop_index__short_circuit', false)) { |
|
169 | + return false; |
|
170 | + } |
|
171 | + global $wpdb; |
|
172 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
173 | + $index_exists_query = "SHOW INDEX FROM {$table_name} WHERE Key_name = '{$index_name}'"; |
|
174 | + if ( |
|
175 | + $this->getTableAnalysis()->tableExists($table_name) |
|
176 | + && $wpdb->get_var($index_exists_query) |
|
177 | + === $table_name //using get_var with the $index_exists_query returns the table's name |
|
178 | + ) { |
|
179 | + return $wpdb->query("ALTER TABLE {$table_name} DROP INDEX {$index_name}"); |
|
180 | + } |
|
181 | + return 0; |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * Just creates the requested table. $table_name can |
|
188 | + * optionally start with $wpdb->prefix or not |
|
189 | + * |
|
192 | 190 | *@param string $table_name |
193 | - * @param string $create_sql defining the table's columns and indexes |
|
194 | - * @param string $engine (no need to specify "ENGINE=", that's implied) |
|
195 | - * @return void |
|
196 | - * @throws \EE_Error |
|
197 | - */ |
|
198 | - public function createTable($table_name, $create_sql, $engine = 'MyISAM') |
|
199 | - { |
|
200 | - // does $sql contain valid column information? ( LPT: https://regex101.com/ is great for working out regex patterns ) |
|
201 | - if (preg_match('((((.*?))(,\s))+)', $create_sql, $valid_column_data)) { |
|
202 | - $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
203 | - /** @var \wpdb $wpdb */ |
|
204 | - global $wpdb; |
|
205 | - $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate(); |
|
206 | - |
|
207 | - //get $wpdb to echo errors, but buffer them. This way at least WE know an error |
|
208 | - //happened. And then we can choose to tell the end user |
|
209 | - $old_show_errors_policy = $wpdb->show_errors(true); |
|
210 | - $old_error_suppression_policy = $wpdb->suppress_errors(false); |
|
211 | - ob_start(); |
|
212 | - dbDelta($SQL); |
|
213 | - $output = ob_get_contents(); |
|
214 | - ob_end_clean(); |
|
215 | - $wpdb->show_errors($old_show_errors_policy); |
|
216 | - $wpdb->suppress_errors($old_error_suppression_policy); |
|
217 | - if ( ! empty($output)) { |
|
218 | - throw new \EE_Error($output); |
|
219 | - } |
|
220 | - } else { |
|
221 | - throw new \EE_Error( |
|
222 | - sprintf( |
|
223 | - __('The following table creation SQL does not contain valid information about the table columns: %1$s %2$s', |
|
224 | - 'event_espresso'), |
|
225 | - '<br />', |
|
226 | - $create_sql |
|
227 | - ) |
|
228 | - ); |
|
229 | - } |
|
230 | - } |
|
191 | + * @param string $create_sql defining the table's columns and indexes |
|
192 | + * @param string $engine (no need to specify "ENGINE=", that's implied) |
|
193 | + * @return void |
|
194 | + * @throws \EE_Error |
|
195 | + */ |
|
196 | + public function createTable($table_name, $create_sql, $engine = 'MyISAM') |
|
197 | + { |
|
198 | + // does $sql contain valid column information? ( LPT: https://regex101.com/ is great for working out regex patterns ) |
|
199 | + if (preg_match('((((.*?))(,\s))+)', $create_sql, $valid_column_data)) { |
|
200 | + $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
|
201 | + /** @var \wpdb $wpdb */ |
|
202 | + global $wpdb; |
|
203 | + $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate(); |
|
204 | + |
|
205 | + //get $wpdb to echo errors, but buffer them. This way at least WE know an error |
|
206 | + //happened. And then we can choose to tell the end user |
|
207 | + $old_show_errors_policy = $wpdb->show_errors(true); |
|
208 | + $old_error_suppression_policy = $wpdb->suppress_errors(false); |
|
209 | + ob_start(); |
|
210 | + dbDelta($SQL); |
|
211 | + $output = ob_get_contents(); |
|
212 | + ob_end_clean(); |
|
213 | + $wpdb->show_errors($old_show_errors_policy); |
|
214 | + $wpdb->suppress_errors($old_error_suppression_policy); |
|
215 | + if ( ! empty($output)) { |
|
216 | + throw new \EE_Error($output); |
|
217 | + } |
|
218 | + } else { |
|
219 | + throw new \EE_Error( |
|
220 | + sprintf( |
|
221 | + __('The following table creation SQL does not contain valid information about the table columns: %1$s %2$s', |
|
222 | + 'event_espresso'), |
|
223 | + '<br />', |
|
224 | + $create_sql |
|
225 | + ) |
|
226 | + ); |
|
227 | + } |
|
228 | + } |
|
231 | 229 | |
232 | 230 | } |
@@ -145,9 +145,9 @@ discard block |
||
145 | 145 | $tables_to_delete[] = $table_name; |
146 | 146 | } |
147 | 147 | } |
148 | - if( ! empty( $tables_to_delete ) ) { |
|
148 | + if ( ! empty($tables_to_delete)) { |
|
149 | 149 | global $wpdb; |
150 | - $wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete)); |
|
150 | + $wpdb->query('DROP TABLE '.implode(', ', $tables_to_delete)); |
|
151 | 151 | } |
152 | 152 | return $tables_to_delete; |
153 | 153 | } |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name); |
203 | 203 | /** @var \wpdb $wpdb */ |
204 | 204 | global $wpdb; |
205 | - $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate(); |
|
205 | + $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} ".$wpdb->get_charset_collate(); |
|
206 | 206 | |
207 | 207 | //get $wpdb to echo errors, but buffer them. This way at least WE know an error |
208 | 208 | //happened. And then we can choose to tell the end user |
@@ -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 | * Datetime Model |
@@ -11,546 +11,546 @@ discard block |
||
11 | 11 | class EEM_Datetime extends EEM_Soft_Delete_Base |
12 | 12 | { |
13 | 13 | |
14 | - // private instance of the EEM_Datetime object |
|
15 | - protected static $_instance = null; |
|
16 | - |
|
17 | - |
|
18 | - |
|
19 | - /** |
|
20 | - * private constructor to prevent direct creation |
|
21 | - * |
|
22 | - * @Constructor |
|
23 | - * @access private |
|
24 | - * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
|
25 | - * incoming timezone data that gets saved). Note this just sends the timezone info to the |
|
26 | - * date time model field objects. Default is NULL (and will be assumed using the set |
|
27 | - * timezone in the 'timezone_string' wp option) |
|
28 | - */ |
|
29 | - protected function __construct($timezone) |
|
30 | - { |
|
31 | - $this->singular_item = __('Datetime', 'event_espresso'); |
|
32 | - $this->plural_item = __('Datetimes', 'event_espresso'); |
|
33 | - $this->_tables = array( |
|
34 | - 'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'), |
|
35 | - ); |
|
36 | - $this->_fields = array( |
|
37 | - 'Datetime' => array( |
|
38 | - 'DTT_ID' => new EE_Primary_Key_Int_Field('DTT_ID', __('Datetime ID', 'event_espresso')), |
|
39 | - 'EVT_ID' => new EE_Foreign_Key_Int_Field('EVT_ID', __('Event ID', 'event_espresso'), false, 0, |
|
40 | - 'Event'), |
|
41 | - 'DTT_name' => new EE_Plain_Text_Field('DTT_name', __('Datetime Name', 'event_espresso'), false, |
|
42 | - ''), |
|
43 | - 'DTT_description' => new EE_Post_Content_Field('DTT_description', |
|
44 | - __('Description for Datetime', 'event_espresso'), false, ''), |
|
45 | - 'DTT_EVT_start' => new EE_Datetime_Field('DTT_EVT_start', |
|
46 | - __('Start time/date of Event', 'event_espresso'), false, EE_Datetime_Field::now, $timezone), |
|
47 | - 'DTT_EVT_end' => new EE_Datetime_Field('DTT_EVT_end', |
|
48 | - __('End time/date of Event', 'event_espresso'), false, EE_Datetime_Field::now, $timezone), |
|
49 | - 'DTT_reg_limit' => new EE_Infinite_Integer_Field('DTT_reg_limit', |
|
50 | - __('Registration Limit for this time', 'event_espresso'), true, EE_INF), |
|
51 | - 'DTT_sold' => new EE_Integer_Field('DTT_sold', |
|
52 | - __('How many sales for this Datetime that have occurred', 'event_espresso'), true, 0), |
|
53 | - 'DTT_reserved' => new EE_Integer_Field('DTT_reserved', |
|
54 | - __('Quantity of tickets that are reserved, but not yet fully purchased', 'event_espresso'), false, |
|
55 | - 0), |
|
56 | - 'DTT_is_primary' => new EE_Boolean_Field('DTT_is_primary', |
|
57 | - __("Flag indicating datetime is primary one for event", "event_espresso"), false, false), |
|
58 | - 'DTT_order' => new EE_Integer_Field('DTT_order', |
|
59 | - __('The order in which the Datetime is displayed', 'event_espresso'), false, 0), |
|
60 | - 'DTT_parent' => new EE_Integer_Field('DTT_parent', |
|
61 | - __('Indicates what DTT_ID is the parent of this DTT_ID'), true, 0), |
|
62 | - 'DTT_deleted' => new EE_Trashed_Flag_Field('DTT_deleted', |
|
63 | - __('Flag indicating datetime is archived', 'event_espresso'), false, false), |
|
64 | - ), |
|
65 | - ); |
|
66 | - $this->_model_relations = array( |
|
67 | - 'Ticket' => new EE_HABTM_Relation('Datetime_Ticket'), |
|
68 | - 'Event' => new EE_Belongs_To_Relation(), |
|
69 | - 'Checkin' => new EE_Has_Many_Relation(), |
|
70 | - ); |
|
71 | - $this->_model_chain_to_wp_user = 'Event'; |
|
72 | - //this model is generally available for reading |
|
73 | - $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Event_Related_Public('Event'); |
|
74 | - $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Event_Related_Protected('Event'); |
|
75 | - $this->_cap_restriction_generators[EEM_Base::caps_edit] = new EE_Restriction_Generator_Event_Related_Protected('Event'); |
|
76 | - $this->_cap_restriction_generators[EEM_Base::caps_delete] = new EE_Restriction_Generator_Event_Related_Protected('Event', |
|
77 | - EEM_Base::caps_edit); |
|
78 | - parent::__construct($timezone); |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * create new blank datetime |
|
85 | - * |
|
86 | - * @access public |
|
87 | - * @return EE_Datetime[] array on success, FALSE on fail |
|
88 | - */ |
|
89 | - public function create_new_blank_datetime() |
|
90 | - { |
|
91 | - //makes sure timezone is always set. |
|
92 | - $timezone_string = $this->get_timezone(); |
|
93 | - $blank_datetime = EE_Datetime::new_instance( |
|
94 | - array( |
|
95 | - 'DTT_EVT_start' => $this->current_time_for_query('DTT_EVT_start', true) + (60 * 60 * 24 * 30), |
|
96 | - 'DTT_EVT_end' => $this->current_time_for_query('DTT_EVT_end', true) + (60 * 60 * 24 * 30), |
|
97 | - 'DTT_order' => 1, |
|
98 | - 'DTT_reg_limit' => EE_INF, |
|
99 | - ), |
|
100 | - $timezone_string |
|
101 | - ); |
|
102 | - $blank_datetime->set_start_time($this->convert_datetime_for_query('DTT_EVT_start', '8am', 'ga', |
|
103 | - $timezone_string)); |
|
104 | - $blank_datetime->set_end_time($this->convert_datetime_for_query('DTT_EVT_end', '5pm', 'ga', $timezone_string)); |
|
105 | - return array($blank_datetime); |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * get event start date from db |
|
112 | - * |
|
113 | - * @access public |
|
114 | - * @param int $EVT_ID |
|
115 | - * @return EE_Datetime[] array on success, FALSE on fail |
|
116 | - */ |
|
117 | - public function get_all_event_dates($EVT_ID = 0) |
|
118 | - { |
|
119 | - if ( ! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
120 | - return $this->create_new_blank_datetime(); |
|
121 | - } |
|
122 | - $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
|
123 | - if (empty($results)) { |
|
124 | - return $this->create_new_blank_datetime(); |
|
125 | - } |
|
126 | - return $results; |
|
127 | - } |
|
128 | - |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * get all datetimes attached to an event ordered by the DTT_order field |
|
133 | - * |
|
134 | - * @public |
|
135 | - * @param int $EVT_ID event id |
|
136 | - * @param boolean $include_expired |
|
137 | - * @param boolean $include_deleted |
|
138 | - * @param int $limit If included then limit the count of results by |
|
139 | - * the given number |
|
140 | - * @return EE_Datetime[] |
|
141 | - */ |
|
142 | - public function get_datetimes_for_event_ordered_by_DTT_order( |
|
143 | - $EVT_ID, |
|
144 | - $include_expired = true, |
|
145 | - $include_deleted = true, |
|
146 | - $limit = null |
|
147 | - ) { |
|
148 | - //sanitize EVT_ID |
|
149 | - $EVT_ID = intval($EVT_ID); |
|
150 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
151 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
152 | - $where_params = array('Event.EVT_ID' => $EVT_ID); |
|
153 | - $query_params = ! empty($limit) ? array( |
|
154 | - $where_params, |
|
155 | - 'limit' => $limit, |
|
156 | - 'order_by' => array('DTT_order' => 'ASC'), |
|
157 | - 'default_where_conditions' => 'none', |
|
158 | - ) : array($where_params, 'order_by' => array('DTT_order' => 'ASC'), 'default_where_conditions' => 'none'); |
|
159 | - if ( ! $include_expired) { |
|
160 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
161 | - } |
|
162 | - if ($include_deleted) { |
|
163 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
164 | - } |
|
165 | - $result = $this->get_all($query_params); |
|
166 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
167 | - return $result; |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * Gets the datetimes for the event (with the given limit), and orders them by "importance". By importance, we mean |
|
174 | - * that the primary datetimes are most important (DEPRECATED FOR NOW), and then the earlier datetimes are the most |
|
175 | - * important. Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
|
176 | - * |
|
177 | - * @param int $EVT_ID |
|
178 | - * @param int $limit |
|
179 | - * @return EE_Datetime[] |
|
180 | - */ |
|
181 | - public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null) |
|
182 | - { |
|
183 | - return $this->get_all(array( |
|
184 | - array('Event.EVT_ID' => $EVT_ID), |
|
185 | - 'limit' => $limit, |
|
186 | - 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
187 | - 'default_where_conditions' => 'none', |
|
188 | - )); |
|
189 | - } |
|
190 | - |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * @param int $EVT_ID |
|
195 | - * @param boolean $include_expired |
|
196 | - * @param boolean $include_deleted |
|
197 | - * @return EE_Datetime |
|
198 | - */ |
|
199 | - public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
|
200 | - { |
|
201 | - $results = $this->get_datetimes_for_event_ordered_by_start_time($EVT_ID, $include_expired, $include_deleted, 1); |
|
202 | - if ($results) { |
|
203 | - return array_shift($results); |
|
204 | - } else { |
|
205 | - return null; |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - |
|
211 | - /** |
|
212 | - * Gets the 'primary' datetime for an event. |
|
213 | - * |
|
214 | - * @param int $EVT_ID |
|
215 | - * @param bool $try_to_exclude_expired |
|
216 | - * @param bool $try_to_exclude_deleted |
|
217 | - * @return \EE_Datetime |
|
218 | - */ |
|
219 | - public function get_primary_datetime_for_event( |
|
220 | - $EVT_ID, |
|
221 | - $try_to_exclude_expired = true, |
|
222 | - $try_to_exclude_deleted = true |
|
223 | - ) { |
|
224 | - if ($try_to_exclude_expired) { |
|
225 | - $non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false); |
|
226 | - if ($non_expired) { |
|
227 | - return $non_expired; |
|
228 | - } |
|
229 | - } |
|
230 | - if ($try_to_exclude_deleted) { |
|
231 | - $expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true); |
|
232 | - if ($expired_even) { |
|
233 | - return $expired_even; |
|
234 | - } |
|
235 | - } |
|
236 | - $deleted_even = $this->get_oldest_datetime_for_event($EVT_ID, true, true); |
|
237 | - return $deleted_even; |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
|
244 | - * only by start date |
|
245 | - * |
|
246 | - * @param int $EVT_ID |
|
247 | - * @param boolean $include_expired |
|
248 | - * @param boolean $include_deleted |
|
249 | - * @param int $limit |
|
250 | - * @return EE_Datetime[] |
|
251 | - */ |
|
252 | - public function get_datetimes_for_event_ordered_by_start_time( |
|
253 | - $EVT_ID, |
|
254 | - $include_expired = true, |
|
255 | - $include_deleted = true, |
|
256 | - $limit = null |
|
257 | - ) { |
|
258 | - //sanitize EVT_ID |
|
259 | - $EVT_ID = intval($EVT_ID); |
|
260 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
261 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
262 | - $query_params = array(array('Event.EVT_ID' => $EVT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
263 | - if ( ! $include_expired) { |
|
264 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
265 | - } |
|
266 | - if ($include_deleted) { |
|
267 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
268 | - } |
|
269 | - if ($limit) { |
|
270 | - $query_params['limit'] = $limit; |
|
271 | - } |
|
272 | - $result = $this->get_all($query_params); |
|
273 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
274 | - return $result; |
|
275 | - } |
|
276 | - |
|
277 | - |
|
278 | - |
|
279 | - /** |
|
280 | - * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
|
281 | - * only by start date |
|
282 | - * |
|
283 | - * @param int $TKT_ID |
|
284 | - * @param boolean $include_expired |
|
285 | - * @param boolean $include_deleted |
|
286 | - * @param int $limit |
|
287 | - * @return EE_Datetime[] |
|
288 | - */ |
|
289 | - public function get_datetimes_for_ticket_ordered_by_start_time( |
|
290 | - $TKT_ID, |
|
291 | - $include_expired = true, |
|
292 | - $include_deleted = true, |
|
293 | - $limit = null |
|
294 | - ) { |
|
295 | - //sanitize TKT_ID |
|
296 | - $TKT_ID = intval($TKT_ID); |
|
297 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
298 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
299 | - $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
300 | - if ( ! $include_expired) { |
|
301 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
302 | - } |
|
303 | - if ($include_deleted) { |
|
304 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
305 | - } |
|
306 | - if ($limit) { |
|
307 | - $query_params['limit'] = $limit; |
|
308 | - } |
|
309 | - $result = $this->get_all($query_params); |
|
310 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
311 | - return $result; |
|
312 | - } |
|
313 | - |
|
314 | - |
|
315 | - |
|
316 | - /** |
|
317 | - * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
|
318 | - * datetimes. |
|
319 | - * |
|
320 | - * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
|
321 | - * @param boolean $include_expired whether to include expired datetimes or not |
|
322 | - * @param boolean $include_deleted whether to include trashed datetimes or not. |
|
323 | - * @param int|null $limit if null, no limit, if int then limit results by |
|
324 | - * that number |
|
325 | - * @return EE_Datetime[] |
|
326 | - */ |
|
327 | - public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
328 | - $TKT_ID, |
|
329 | - $include_expired = true, |
|
330 | - $include_deleted = true, |
|
331 | - $limit = null |
|
332 | - ) { |
|
333 | - //sanitize id. |
|
334 | - $TKT_ID = intval($TKT_ID); |
|
335 | - $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
336 | - $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
337 | - $where_params = array('Ticket.TKT_ID' => $TKT_ID); |
|
338 | - $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC')); |
|
339 | - if ( ! $include_expired) { |
|
340 | - $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
341 | - } |
|
342 | - if ($include_deleted) { |
|
343 | - $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
344 | - } |
|
345 | - if ($limit) { |
|
346 | - $query_params['limit'] = $limit; |
|
347 | - } |
|
348 | - $result = $this->get_all($query_params); |
|
349 | - $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
350 | - return $result; |
|
351 | - } |
|
352 | - |
|
353 | - |
|
354 | - |
|
355 | - /** |
|
356 | - * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
|
357 | - * reason it doesn't exist, we consider the earliest event the most important) |
|
358 | - * |
|
359 | - * @param int $EVT_ID |
|
360 | - * @return EE_Datetime |
|
361 | - */ |
|
362 | - public function get_most_important_datetime_for_event($EVT_ID) |
|
363 | - { |
|
364 | - $results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1); |
|
365 | - if ($results) { |
|
366 | - return array_shift($results); |
|
367 | - } else { |
|
368 | - return null; |
|
369 | - } |
|
370 | - } |
|
371 | - |
|
372 | - |
|
373 | - |
|
374 | - /** |
|
375 | - * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
|
376 | - * grouped by month and year. |
|
377 | - * |
|
378 | - * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
|
379 | - * @param string $evt_active_status A string representing the evt active status to filter the months by. |
|
380 | - * Can be: |
|
381 | - * - '' = no filter |
|
382 | - * - upcoming = Published events with at least one upcoming datetime. |
|
383 | - * - expired = Events with all datetimes expired. |
|
384 | - * - active = Events that are published and have at least one datetime that |
|
385 | - * starts before now and ends after now. |
|
386 | - * - inactive = Events that are either not published. |
|
387 | - * @return wpdb results array |
|
388 | - */ |
|
389 | - public function get_dtt_months_and_years($where_params, $evt_active_status = '') |
|
390 | - { |
|
391 | - $current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start'); |
|
392 | - $current_time_for_DTT_EVT_end = $this->current_time_for_query('DTT_EVT_end'); |
|
393 | - switch ($evt_active_status) { |
|
394 | - case 'upcoming' : |
|
395 | - $where_params['Event.status'] = 'publish'; |
|
396 | - //if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
397 | - if (isset($where_params['DTT_EVT_start'])) { |
|
398 | - $where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start']; |
|
399 | - } |
|
400 | - $where_params['DTT_EVT_start'] = array('>', $current_time_for_DTT_EVT_start); |
|
401 | - break; |
|
402 | - case 'expired' : |
|
403 | - if (isset($where_params['Event.status'])) { |
|
404 | - unset($where_params['Event.status']); |
|
405 | - } |
|
406 | - //get events to exclude |
|
407 | - $exclude_query[0] = array_merge($where_params, |
|
408 | - array('DTT_EVT_end' => array('>', $current_time_for_DTT_EVT_end))); |
|
409 | - //first get all events that have datetimes where its not expired. |
|
410 | - $event_ids = $this->_get_all_wpdb_results($exclude_query, OBJECT_K, 'Datetime.EVT_ID'); |
|
411 | - $event_ids = array_keys($event_ids); |
|
412 | - if (isset($where_params['DTT_EVT_end'])) { |
|
413 | - $where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
414 | - } |
|
415 | - $where_params['DTT_EVT_end'] = array('<', $current_time_for_DTT_EVT_end); |
|
416 | - $where_params['Event.EVT_ID'] = array('NOT IN', $event_ids); |
|
417 | - break; |
|
418 | - case 'active' : |
|
419 | - $where_params['Event.status'] = 'publish'; |
|
420 | - if (isset($where_params['DTT_EVT_start'])) { |
|
421 | - $where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start']; |
|
422 | - } |
|
423 | - if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
424 | - $where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end']; |
|
425 | - } |
|
426 | - $where_params['DTT_EVT_start'] = array('<', $current_time_for_DTT_EVT_start); |
|
427 | - $where_params['DTT_EVT_end'] = array('>', $current_time_for_DTT_EVT_end); |
|
428 | - break; |
|
429 | - case 'inactive' : |
|
430 | - if (isset($where_params['Event.status'])) { |
|
431 | - unset($where_params['Event.status']); |
|
432 | - } |
|
433 | - if (isset($where_params['OR'])) { |
|
434 | - $where_params['AND']['OR'] = $where_params['OR']; |
|
435 | - } |
|
436 | - if (isset($where_params['DTT_EVT_end'])) { |
|
437 | - $where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
438 | - unset($where_params['DTT_EVT_end']); |
|
439 | - } |
|
440 | - if (isset($where_params['DTT_EVT_start'])) { |
|
441 | - $where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start']; |
|
442 | - unset($where_params['DTT_EVT_start']); |
|
443 | - } |
|
444 | - $where_params['AND']['Event.status'] = array('!=', 'publish'); |
|
445 | - break; |
|
446 | - } |
|
447 | - $query_params[0] = $where_params; |
|
448 | - $query_params['group_by'] = array('dtt_year', 'dtt_month'); |
|
449 | - $query_params['order_by'] = array('DTT_EVT_start' => 'DESC'); |
|
450 | - $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'DTT_EVT_start'); |
|
451 | - $columns_to_select = array( |
|
452 | - 'dtt_year' => array('YEAR(' . $query_interval . ')', '%s'), |
|
453 | - 'dtt_month' => array('MONTHNAME(' . $query_interval . ')', '%s'), |
|
454 | - 'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'), |
|
455 | - ); |
|
456 | - return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - |
|
461 | - /** |
|
462 | - * Updates the DTT_sold attribute on each datetime (based on the registrations |
|
463 | - * for the tickets for each datetime) |
|
464 | - * |
|
465 | - * @param EE_Datetime[] $datetimes |
|
466 | - */ |
|
467 | - public function update_sold($datetimes) |
|
468 | - { |
|
469 | - foreach ($datetimes as $datetime) { |
|
470 | - $datetime->update_sold(); |
|
471 | - } |
|
472 | - } |
|
473 | - |
|
474 | - |
|
475 | - |
|
476 | - /** |
|
477 | - * Gets the total number of tickets available at a particular datetime |
|
478 | - * (does NOT take into account the datetime's spaces available) |
|
479 | - * |
|
480 | - * @param int $DTT_ID |
|
481 | - * @param array $query_params |
|
482 | - * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
|
483 | - * tickets attached to datetime then FALSE is returned. |
|
484 | - */ |
|
485 | - public function sum_tickets_currently_available_at_datetime($DTT_ID, $query_params = array()) |
|
486 | - { |
|
487 | - $datetime = $this->get_one_by_ID($DTT_ID); |
|
488 | - if ($datetime instanceof EE_Datetime) { |
|
489 | - return $datetime->tickets_remaining($query_params); |
|
490 | - } |
|
491 | - return 0; |
|
492 | - } |
|
493 | - |
|
494 | - |
|
495 | - |
|
496 | - /** |
|
497 | - * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
|
498 | - * |
|
499 | - * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
500 | - * stati you want counts for as values in the array. An empty array returns counts |
|
501 | - * for all valid stati. |
|
502 | - * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
|
503 | - * only for Datetimes connected to a specific event, or specific ticket. |
|
504 | - * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
|
505 | - * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming EE_Datetime::expired |
|
506 | - */ |
|
507 | - public function get_datetime_counts_by_status($stati_to_include = array(), $query_params = array()) |
|
508 | - { |
|
509 | - //only accept where conditions for this query. |
|
510 | - $_where = isset($query_params[0]) ? $query_params[0] : array(); |
|
511 | - $status_query_args = array( |
|
512 | - EE_Datetime::active => array_merge( |
|
513 | - $_where, |
|
514 | - array('DTT_EVT_start' => array('<', time()), 'DTT_EVT_end' => array('>', time())) |
|
515 | - ), |
|
516 | - EE_Datetime::upcoming => array_merge( |
|
517 | - $_where, |
|
518 | - array('DTT_EVT_start' => array('>', time())) |
|
519 | - ), |
|
520 | - EE_Datetime::expired => array_merge( |
|
521 | - $_where, |
|
522 | - array('DTT_EVT_end' => array('<', time())) |
|
523 | - ), |
|
524 | - ); |
|
525 | - if ( ! empty($stati_to_include)) { |
|
526 | - foreach (array_keys($status_query_args) as $status) { |
|
527 | - if ( ! in_array($status, $stati_to_include)) { |
|
528 | - unset($status_query_args[$status]); |
|
529 | - } |
|
530 | - } |
|
531 | - } |
|
532 | - //loop through and query counts for each stati. |
|
533 | - $status_query_results = array(); |
|
534 | - foreach ($status_query_args as $status => $status_where_conditions) { |
|
535 | - $status_query_results[$status] = EEM_Datetime::count(array($status_where_conditions), 'DTT_ID', true); |
|
536 | - } |
|
537 | - return $status_query_results; |
|
538 | - } |
|
539 | - |
|
540 | - |
|
541 | - |
|
542 | - /** |
|
543 | - * Returns the specific count for a given Datetime status matching any given query_params. |
|
544 | - * |
|
545 | - * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
|
546 | - * @param array $query_params |
|
547 | - * @return int |
|
548 | - */ |
|
549 | - public function get_datetime_count_for_status($status = EE_Datetime::active, $query_params = array()) |
|
550 | - { |
|
551 | - $count = $this->get_datetime_counts_by_status(array($status), $query_params); |
|
552 | - return ! empty($count[$status]) ? $count[$status] : 0; |
|
553 | - } |
|
14 | + // private instance of the EEM_Datetime object |
|
15 | + protected static $_instance = null; |
|
16 | + |
|
17 | + |
|
18 | + |
|
19 | + /** |
|
20 | + * private constructor to prevent direct creation |
|
21 | + * |
|
22 | + * @Constructor |
|
23 | + * @access private |
|
24 | + * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
|
25 | + * incoming timezone data that gets saved). Note this just sends the timezone info to the |
|
26 | + * date time model field objects. Default is NULL (and will be assumed using the set |
|
27 | + * timezone in the 'timezone_string' wp option) |
|
28 | + */ |
|
29 | + protected function __construct($timezone) |
|
30 | + { |
|
31 | + $this->singular_item = __('Datetime', 'event_espresso'); |
|
32 | + $this->plural_item = __('Datetimes', 'event_espresso'); |
|
33 | + $this->_tables = array( |
|
34 | + 'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'), |
|
35 | + ); |
|
36 | + $this->_fields = array( |
|
37 | + 'Datetime' => array( |
|
38 | + 'DTT_ID' => new EE_Primary_Key_Int_Field('DTT_ID', __('Datetime ID', 'event_espresso')), |
|
39 | + 'EVT_ID' => new EE_Foreign_Key_Int_Field('EVT_ID', __('Event ID', 'event_espresso'), false, 0, |
|
40 | + 'Event'), |
|
41 | + 'DTT_name' => new EE_Plain_Text_Field('DTT_name', __('Datetime Name', 'event_espresso'), false, |
|
42 | + ''), |
|
43 | + 'DTT_description' => new EE_Post_Content_Field('DTT_description', |
|
44 | + __('Description for Datetime', 'event_espresso'), false, ''), |
|
45 | + 'DTT_EVT_start' => new EE_Datetime_Field('DTT_EVT_start', |
|
46 | + __('Start time/date of Event', 'event_espresso'), false, EE_Datetime_Field::now, $timezone), |
|
47 | + 'DTT_EVT_end' => new EE_Datetime_Field('DTT_EVT_end', |
|
48 | + __('End time/date of Event', 'event_espresso'), false, EE_Datetime_Field::now, $timezone), |
|
49 | + 'DTT_reg_limit' => new EE_Infinite_Integer_Field('DTT_reg_limit', |
|
50 | + __('Registration Limit for this time', 'event_espresso'), true, EE_INF), |
|
51 | + 'DTT_sold' => new EE_Integer_Field('DTT_sold', |
|
52 | + __('How many sales for this Datetime that have occurred', 'event_espresso'), true, 0), |
|
53 | + 'DTT_reserved' => new EE_Integer_Field('DTT_reserved', |
|
54 | + __('Quantity of tickets that are reserved, but not yet fully purchased', 'event_espresso'), false, |
|
55 | + 0), |
|
56 | + 'DTT_is_primary' => new EE_Boolean_Field('DTT_is_primary', |
|
57 | + __("Flag indicating datetime is primary one for event", "event_espresso"), false, false), |
|
58 | + 'DTT_order' => new EE_Integer_Field('DTT_order', |
|
59 | + __('The order in which the Datetime is displayed', 'event_espresso'), false, 0), |
|
60 | + 'DTT_parent' => new EE_Integer_Field('DTT_parent', |
|
61 | + __('Indicates what DTT_ID is the parent of this DTT_ID'), true, 0), |
|
62 | + 'DTT_deleted' => new EE_Trashed_Flag_Field('DTT_deleted', |
|
63 | + __('Flag indicating datetime is archived', 'event_espresso'), false, false), |
|
64 | + ), |
|
65 | + ); |
|
66 | + $this->_model_relations = array( |
|
67 | + 'Ticket' => new EE_HABTM_Relation('Datetime_Ticket'), |
|
68 | + 'Event' => new EE_Belongs_To_Relation(), |
|
69 | + 'Checkin' => new EE_Has_Many_Relation(), |
|
70 | + ); |
|
71 | + $this->_model_chain_to_wp_user = 'Event'; |
|
72 | + //this model is generally available for reading |
|
73 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Event_Related_Public('Event'); |
|
74 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Event_Related_Protected('Event'); |
|
75 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = new EE_Restriction_Generator_Event_Related_Protected('Event'); |
|
76 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = new EE_Restriction_Generator_Event_Related_Protected('Event', |
|
77 | + EEM_Base::caps_edit); |
|
78 | + parent::__construct($timezone); |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * create new blank datetime |
|
85 | + * |
|
86 | + * @access public |
|
87 | + * @return EE_Datetime[] array on success, FALSE on fail |
|
88 | + */ |
|
89 | + public function create_new_blank_datetime() |
|
90 | + { |
|
91 | + //makes sure timezone is always set. |
|
92 | + $timezone_string = $this->get_timezone(); |
|
93 | + $blank_datetime = EE_Datetime::new_instance( |
|
94 | + array( |
|
95 | + 'DTT_EVT_start' => $this->current_time_for_query('DTT_EVT_start', true) + (60 * 60 * 24 * 30), |
|
96 | + 'DTT_EVT_end' => $this->current_time_for_query('DTT_EVT_end', true) + (60 * 60 * 24 * 30), |
|
97 | + 'DTT_order' => 1, |
|
98 | + 'DTT_reg_limit' => EE_INF, |
|
99 | + ), |
|
100 | + $timezone_string |
|
101 | + ); |
|
102 | + $blank_datetime->set_start_time($this->convert_datetime_for_query('DTT_EVT_start', '8am', 'ga', |
|
103 | + $timezone_string)); |
|
104 | + $blank_datetime->set_end_time($this->convert_datetime_for_query('DTT_EVT_end', '5pm', 'ga', $timezone_string)); |
|
105 | + return array($blank_datetime); |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * get event start date from db |
|
112 | + * |
|
113 | + * @access public |
|
114 | + * @param int $EVT_ID |
|
115 | + * @return EE_Datetime[] array on success, FALSE on fail |
|
116 | + */ |
|
117 | + public function get_all_event_dates($EVT_ID = 0) |
|
118 | + { |
|
119 | + if ( ! $EVT_ID) { // on add_new_event event_id gets set to 0 |
|
120 | + return $this->create_new_blank_datetime(); |
|
121 | + } |
|
122 | + $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID); |
|
123 | + if (empty($results)) { |
|
124 | + return $this->create_new_blank_datetime(); |
|
125 | + } |
|
126 | + return $results; |
|
127 | + } |
|
128 | + |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * get all datetimes attached to an event ordered by the DTT_order field |
|
133 | + * |
|
134 | + * @public |
|
135 | + * @param int $EVT_ID event id |
|
136 | + * @param boolean $include_expired |
|
137 | + * @param boolean $include_deleted |
|
138 | + * @param int $limit If included then limit the count of results by |
|
139 | + * the given number |
|
140 | + * @return EE_Datetime[] |
|
141 | + */ |
|
142 | + public function get_datetimes_for_event_ordered_by_DTT_order( |
|
143 | + $EVT_ID, |
|
144 | + $include_expired = true, |
|
145 | + $include_deleted = true, |
|
146 | + $limit = null |
|
147 | + ) { |
|
148 | + //sanitize EVT_ID |
|
149 | + $EVT_ID = intval($EVT_ID); |
|
150 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
151 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
152 | + $where_params = array('Event.EVT_ID' => $EVT_ID); |
|
153 | + $query_params = ! empty($limit) ? array( |
|
154 | + $where_params, |
|
155 | + 'limit' => $limit, |
|
156 | + 'order_by' => array('DTT_order' => 'ASC'), |
|
157 | + 'default_where_conditions' => 'none', |
|
158 | + ) : array($where_params, 'order_by' => array('DTT_order' => 'ASC'), 'default_where_conditions' => 'none'); |
|
159 | + if ( ! $include_expired) { |
|
160 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
161 | + } |
|
162 | + if ($include_deleted) { |
|
163 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
164 | + } |
|
165 | + $result = $this->get_all($query_params); |
|
166 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
167 | + return $result; |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * Gets the datetimes for the event (with the given limit), and orders them by "importance". By importance, we mean |
|
174 | + * that the primary datetimes are most important (DEPRECATED FOR NOW), and then the earlier datetimes are the most |
|
175 | + * important. Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet. |
|
176 | + * |
|
177 | + * @param int $EVT_ID |
|
178 | + * @param int $limit |
|
179 | + * @return EE_Datetime[] |
|
180 | + */ |
|
181 | + public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null) |
|
182 | + { |
|
183 | + return $this->get_all(array( |
|
184 | + array('Event.EVT_ID' => $EVT_ID), |
|
185 | + 'limit' => $limit, |
|
186 | + 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
187 | + 'default_where_conditions' => 'none', |
|
188 | + )); |
|
189 | + } |
|
190 | + |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * @param int $EVT_ID |
|
195 | + * @param boolean $include_expired |
|
196 | + * @param boolean $include_deleted |
|
197 | + * @return EE_Datetime |
|
198 | + */ |
|
199 | + public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false) |
|
200 | + { |
|
201 | + $results = $this->get_datetimes_for_event_ordered_by_start_time($EVT_ID, $include_expired, $include_deleted, 1); |
|
202 | + if ($results) { |
|
203 | + return array_shift($results); |
|
204 | + } else { |
|
205 | + return null; |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + |
|
211 | + /** |
|
212 | + * Gets the 'primary' datetime for an event. |
|
213 | + * |
|
214 | + * @param int $EVT_ID |
|
215 | + * @param bool $try_to_exclude_expired |
|
216 | + * @param bool $try_to_exclude_deleted |
|
217 | + * @return \EE_Datetime |
|
218 | + */ |
|
219 | + public function get_primary_datetime_for_event( |
|
220 | + $EVT_ID, |
|
221 | + $try_to_exclude_expired = true, |
|
222 | + $try_to_exclude_deleted = true |
|
223 | + ) { |
|
224 | + if ($try_to_exclude_expired) { |
|
225 | + $non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false); |
|
226 | + if ($non_expired) { |
|
227 | + return $non_expired; |
|
228 | + } |
|
229 | + } |
|
230 | + if ($try_to_exclude_deleted) { |
|
231 | + $expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true); |
|
232 | + if ($expired_even) { |
|
233 | + return $expired_even; |
|
234 | + } |
|
235 | + } |
|
236 | + $deleted_even = $this->get_oldest_datetime_for_event($EVT_ID, true, true); |
|
237 | + return $deleted_even; |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * Gets ALL the datetimes for an event (including trashed ones, for now), ordered |
|
244 | + * only by start date |
|
245 | + * |
|
246 | + * @param int $EVT_ID |
|
247 | + * @param boolean $include_expired |
|
248 | + * @param boolean $include_deleted |
|
249 | + * @param int $limit |
|
250 | + * @return EE_Datetime[] |
|
251 | + */ |
|
252 | + public function get_datetimes_for_event_ordered_by_start_time( |
|
253 | + $EVT_ID, |
|
254 | + $include_expired = true, |
|
255 | + $include_deleted = true, |
|
256 | + $limit = null |
|
257 | + ) { |
|
258 | + //sanitize EVT_ID |
|
259 | + $EVT_ID = intval($EVT_ID); |
|
260 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
261 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
262 | + $query_params = array(array('Event.EVT_ID' => $EVT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
263 | + if ( ! $include_expired) { |
|
264 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
265 | + } |
|
266 | + if ($include_deleted) { |
|
267 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
268 | + } |
|
269 | + if ($limit) { |
|
270 | + $query_params['limit'] = $limit; |
|
271 | + } |
|
272 | + $result = $this->get_all($query_params); |
|
273 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
274 | + return $result; |
|
275 | + } |
|
276 | + |
|
277 | + |
|
278 | + |
|
279 | + /** |
|
280 | + * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered |
|
281 | + * only by start date |
|
282 | + * |
|
283 | + * @param int $TKT_ID |
|
284 | + * @param boolean $include_expired |
|
285 | + * @param boolean $include_deleted |
|
286 | + * @param int $limit |
|
287 | + * @return EE_Datetime[] |
|
288 | + */ |
|
289 | + public function get_datetimes_for_ticket_ordered_by_start_time( |
|
290 | + $TKT_ID, |
|
291 | + $include_expired = true, |
|
292 | + $include_deleted = true, |
|
293 | + $limit = null |
|
294 | + ) { |
|
295 | + //sanitize TKT_ID |
|
296 | + $TKT_ID = intval($TKT_ID); |
|
297 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
298 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
299 | + $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc')); |
|
300 | + if ( ! $include_expired) { |
|
301 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
302 | + } |
|
303 | + if ($include_deleted) { |
|
304 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
305 | + } |
|
306 | + if ($limit) { |
|
307 | + $query_params['limit'] = $limit; |
|
308 | + } |
|
309 | + $result = $this->get_all($query_params); |
|
310 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
311 | + return $result; |
|
312 | + } |
|
313 | + |
|
314 | + |
|
315 | + |
|
316 | + /** |
|
317 | + * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the |
|
318 | + * datetimes. |
|
319 | + * |
|
320 | + * @param int $TKT_ID ID of ticket to retrieve the datetimes for |
|
321 | + * @param boolean $include_expired whether to include expired datetimes or not |
|
322 | + * @param boolean $include_deleted whether to include trashed datetimes or not. |
|
323 | + * @param int|null $limit if null, no limit, if int then limit results by |
|
324 | + * that number |
|
325 | + * @return EE_Datetime[] |
|
326 | + */ |
|
327 | + public function get_datetimes_for_ticket_ordered_by_DTT_order( |
|
328 | + $TKT_ID, |
|
329 | + $include_expired = true, |
|
330 | + $include_deleted = true, |
|
331 | + $limit = null |
|
332 | + ) { |
|
333 | + //sanitize id. |
|
334 | + $TKT_ID = intval($TKT_ID); |
|
335 | + $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
336 | + $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db); |
|
337 | + $where_params = array('Ticket.TKT_ID' => $TKT_ID); |
|
338 | + $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC')); |
|
339 | + if ( ! $include_expired) { |
|
340 | + $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true)); |
|
341 | + } |
|
342 | + if ($include_deleted) { |
|
343 | + $query_params[0]['DTT_deleted'] = array('IN', array(true, false)); |
|
344 | + } |
|
345 | + if ($limit) { |
|
346 | + $query_params['limit'] = $limit; |
|
347 | + } |
|
348 | + $result = $this->get_all($query_params); |
|
349 | + $this->assume_values_already_prepared_by_model_object($old_assumption); |
|
350 | + return $result; |
|
351 | + } |
|
352 | + |
|
353 | + |
|
354 | + |
|
355 | + /** |
|
356 | + * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK |
|
357 | + * reason it doesn't exist, we consider the earliest event the most important) |
|
358 | + * |
|
359 | + * @param int $EVT_ID |
|
360 | + * @return EE_Datetime |
|
361 | + */ |
|
362 | + public function get_most_important_datetime_for_event($EVT_ID) |
|
363 | + { |
|
364 | + $results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1); |
|
365 | + if ($results) { |
|
366 | + return array_shift($results); |
|
367 | + } else { |
|
368 | + return null; |
|
369 | + } |
|
370 | + } |
|
371 | + |
|
372 | + |
|
373 | + |
|
374 | + /** |
|
375 | + * This returns a wpdb->results Array of all DTT month and years matching the incoming query params and |
|
376 | + * grouped by month and year. |
|
377 | + * |
|
378 | + * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
|
379 | + * @param string $evt_active_status A string representing the evt active status to filter the months by. |
|
380 | + * Can be: |
|
381 | + * - '' = no filter |
|
382 | + * - upcoming = Published events with at least one upcoming datetime. |
|
383 | + * - expired = Events with all datetimes expired. |
|
384 | + * - active = Events that are published and have at least one datetime that |
|
385 | + * starts before now and ends after now. |
|
386 | + * - inactive = Events that are either not published. |
|
387 | + * @return wpdb results array |
|
388 | + */ |
|
389 | + public function get_dtt_months_and_years($where_params, $evt_active_status = '') |
|
390 | + { |
|
391 | + $current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start'); |
|
392 | + $current_time_for_DTT_EVT_end = $this->current_time_for_query('DTT_EVT_end'); |
|
393 | + switch ($evt_active_status) { |
|
394 | + case 'upcoming' : |
|
395 | + $where_params['Event.status'] = 'publish'; |
|
396 | + //if there are already query_params matching DTT_EVT_start then we need to modify that to add them. |
|
397 | + if (isset($where_params['DTT_EVT_start'])) { |
|
398 | + $where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start']; |
|
399 | + } |
|
400 | + $where_params['DTT_EVT_start'] = array('>', $current_time_for_DTT_EVT_start); |
|
401 | + break; |
|
402 | + case 'expired' : |
|
403 | + if (isset($where_params['Event.status'])) { |
|
404 | + unset($where_params['Event.status']); |
|
405 | + } |
|
406 | + //get events to exclude |
|
407 | + $exclude_query[0] = array_merge($where_params, |
|
408 | + array('DTT_EVT_end' => array('>', $current_time_for_DTT_EVT_end))); |
|
409 | + //first get all events that have datetimes where its not expired. |
|
410 | + $event_ids = $this->_get_all_wpdb_results($exclude_query, OBJECT_K, 'Datetime.EVT_ID'); |
|
411 | + $event_ids = array_keys($event_ids); |
|
412 | + if (isset($where_params['DTT_EVT_end'])) { |
|
413 | + $where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
414 | + } |
|
415 | + $where_params['DTT_EVT_end'] = array('<', $current_time_for_DTT_EVT_end); |
|
416 | + $where_params['Event.EVT_ID'] = array('NOT IN', $event_ids); |
|
417 | + break; |
|
418 | + case 'active' : |
|
419 | + $where_params['Event.status'] = 'publish'; |
|
420 | + if (isset($where_params['DTT_EVT_start'])) { |
|
421 | + $where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start']; |
|
422 | + } |
|
423 | + if (isset($where_params['Datetime.DTT_EVT_end'])) { |
|
424 | + $where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end']; |
|
425 | + } |
|
426 | + $where_params['DTT_EVT_start'] = array('<', $current_time_for_DTT_EVT_start); |
|
427 | + $where_params['DTT_EVT_end'] = array('>', $current_time_for_DTT_EVT_end); |
|
428 | + break; |
|
429 | + case 'inactive' : |
|
430 | + if (isset($where_params['Event.status'])) { |
|
431 | + unset($where_params['Event.status']); |
|
432 | + } |
|
433 | + if (isset($where_params['OR'])) { |
|
434 | + $where_params['AND']['OR'] = $where_params['OR']; |
|
435 | + } |
|
436 | + if (isset($where_params['DTT_EVT_end'])) { |
|
437 | + $where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end']; |
|
438 | + unset($where_params['DTT_EVT_end']); |
|
439 | + } |
|
440 | + if (isset($where_params['DTT_EVT_start'])) { |
|
441 | + $where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start']; |
|
442 | + unset($where_params['DTT_EVT_start']); |
|
443 | + } |
|
444 | + $where_params['AND']['Event.status'] = array('!=', 'publish'); |
|
445 | + break; |
|
446 | + } |
|
447 | + $query_params[0] = $where_params; |
|
448 | + $query_params['group_by'] = array('dtt_year', 'dtt_month'); |
|
449 | + $query_params['order_by'] = array('DTT_EVT_start' => 'DESC'); |
|
450 | + $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'DTT_EVT_start'); |
|
451 | + $columns_to_select = array( |
|
452 | + 'dtt_year' => array('YEAR(' . $query_interval . ')', '%s'), |
|
453 | + 'dtt_month' => array('MONTHNAME(' . $query_interval . ')', '%s'), |
|
454 | + 'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'), |
|
455 | + ); |
|
456 | + return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + |
|
461 | + /** |
|
462 | + * Updates the DTT_sold attribute on each datetime (based on the registrations |
|
463 | + * for the tickets for each datetime) |
|
464 | + * |
|
465 | + * @param EE_Datetime[] $datetimes |
|
466 | + */ |
|
467 | + public function update_sold($datetimes) |
|
468 | + { |
|
469 | + foreach ($datetimes as $datetime) { |
|
470 | + $datetime->update_sold(); |
|
471 | + } |
|
472 | + } |
|
473 | + |
|
474 | + |
|
475 | + |
|
476 | + /** |
|
477 | + * Gets the total number of tickets available at a particular datetime |
|
478 | + * (does NOT take into account the datetime's spaces available) |
|
479 | + * |
|
480 | + * @param int $DTT_ID |
|
481 | + * @param array $query_params |
|
482 | + * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF, IF there are NO |
|
483 | + * tickets attached to datetime then FALSE is returned. |
|
484 | + */ |
|
485 | + public function sum_tickets_currently_available_at_datetime($DTT_ID, $query_params = array()) |
|
486 | + { |
|
487 | + $datetime = $this->get_one_by_ID($DTT_ID); |
|
488 | + if ($datetime instanceof EE_Datetime) { |
|
489 | + return $datetime->tickets_remaining($query_params); |
|
490 | + } |
|
491 | + return 0; |
|
492 | + } |
|
493 | + |
|
494 | + |
|
495 | + |
|
496 | + /** |
|
497 | + * This returns an array of counts of datetimes in the database for each Datetime status that can be queried. |
|
498 | + * |
|
499 | + * @param array $stati_to_include If included you can restrict the statuses we return counts for by including the |
|
500 | + * stati you want counts for as values in the array. An empty array returns counts |
|
501 | + * for all valid stati. |
|
502 | + * @param array $query_params If included can be used to refine the conditions for returning the count (i.e. |
|
503 | + * only for Datetimes connected to a specific event, or specific ticket. |
|
504 | + * @return array The value returned is an array indexed by Datetime Status and the values are the counts. The |
|
505 | + * stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming EE_Datetime::expired |
|
506 | + */ |
|
507 | + public function get_datetime_counts_by_status($stati_to_include = array(), $query_params = array()) |
|
508 | + { |
|
509 | + //only accept where conditions for this query. |
|
510 | + $_where = isset($query_params[0]) ? $query_params[0] : array(); |
|
511 | + $status_query_args = array( |
|
512 | + EE_Datetime::active => array_merge( |
|
513 | + $_where, |
|
514 | + array('DTT_EVT_start' => array('<', time()), 'DTT_EVT_end' => array('>', time())) |
|
515 | + ), |
|
516 | + EE_Datetime::upcoming => array_merge( |
|
517 | + $_where, |
|
518 | + array('DTT_EVT_start' => array('>', time())) |
|
519 | + ), |
|
520 | + EE_Datetime::expired => array_merge( |
|
521 | + $_where, |
|
522 | + array('DTT_EVT_end' => array('<', time())) |
|
523 | + ), |
|
524 | + ); |
|
525 | + if ( ! empty($stati_to_include)) { |
|
526 | + foreach (array_keys($status_query_args) as $status) { |
|
527 | + if ( ! in_array($status, $stati_to_include)) { |
|
528 | + unset($status_query_args[$status]); |
|
529 | + } |
|
530 | + } |
|
531 | + } |
|
532 | + //loop through and query counts for each stati. |
|
533 | + $status_query_results = array(); |
|
534 | + foreach ($status_query_args as $status => $status_where_conditions) { |
|
535 | + $status_query_results[$status] = EEM_Datetime::count(array($status_where_conditions), 'DTT_ID', true); |
|
536 | + } |
|
537 | + return $status_query_results; |
|
538 | + } |
|
539 | + |
|
540 | + |
|
541 | + |
|
542 | + /** |
|
543 | + * Returns the specific count for a given Datetime status matching any given query_params. |
|
544 | + * |
|
545 | + * @param string $status Valid string representation for Datetime status requested. (Defaults to Active). |
|
546 | + * @param array $query_params |
|
547 | + * @return int |
|
548 | + */ |
|
549 | + public function get_datetime_count_for_status($status = EE_Datetime::active, $query_params = array()) |
|
550 | + { |
|
551 | + $count = $this->get_datetime_counts_by_status(array($status), $query_params); |
|
552 | + return ! empty($count[$status]) ? $count[$status] : 0; |
|
553 | + } |
|
554 | 554 | |
555 | 555 | |
556 | 556 |
@@ -1,16 +1,16 @@ discard block |
||
1 | 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
2 | 2 | /** |
3 | - * |
|
4 | - * Class EE_SPCO_Reg_Step_Attendee_Information |
|
5 | - * |
|
6 | - * Description |
|
7 | - * |
|
8 | - * @package Event Espresso |
|
9 | - * @subpackage core |
|
10 | - * @author Brent Christensen |
|
11 | - * @since 4.5.0 |
|
12 | - * |
|
13 | - */ |
|
3 | + * |
|
4 | + * Class EE_SPCO_Reg_Step_Attendee_Information |
|
5 | + * |
|
6 | + * Description |
|
7 | + * |
|
8 | + * @package Event Espresso |
|
9 | + * @subpackage core |
|
10 | + * @author Brent Christensen |
|
11 | + * @since 4.5.0 |
|
12 | + * |
|
13 | + */ |
|
14 | 14 | class EE_SPCO_Reg_Step_Attendee_Information extends EE_SPCO_Reg_Step { |
15 | 15 | |
16 | 16 | /** |
@@ -135,33 +135,33 @@ discard block |
||
135 | 135 | $registration instanceof EE_Registration |
136 | 136 | && $this->checkout->visit_allows_processing_of_this_registration( $registration ) |
137 | 137 | ) { |
138 | - $attendee_reg_form = $this->_registrations_reg_form($registration); |
|
139 | - if ($attendee_reg_form instanceof EE_Form_Section_Proper) { |
|
140 | - $subsections[$registration->reg_url_link()] = $attendee_reg_form; |
|
141 | - if ( ! $this->checkout->admin_request) { |
|
142 | - $template_args['registrations'][$registration->reg_url_link()] = $registration; |
|
143 | - $template_args['ticket_count'][$registration->ticket()->ID()] = isset( |
|
144 | - $template_args['ticket_count'][$registration->ticket()->ID()] |
|
145 | - ) |
|
146 | - ? $template_args['ticket_count'][$registration->ticket()->ID()] + 1 |
|
147 | - : 1; |
|
148 | - $ticket_line_item = EEH_Line_Item::get_line_items_by_object_type_and_IDs( |
|
149 | - $this->checkout->cart->get_grand_total(), |
|
150 | - 'Ticket', |
|
151 | - array($registration->ticket()->ID()) |
|
152 | - ); |
|
153 | - $ticket_line_item = is_array($ticket_line_item) |
|
154 | - ? reset($ticket_line_item) |
|
155 | - : $ticket_line_item; |
|
156 | - $template_args['ticket_line_item'][$registration->ticket() |
|
157 | - ->ID()] = $Line_Item_Display->display_line_item( |
|
158 | - $ticket_line_item |
|
159 | - ); |
|
160 | - } |
|
161 | - } |
|
162 | - if ($registration->is_primary_registrant()) { |
|
163 | - $primary_registrant = $registration->reg_url_link(); |
|
164 | - } |
|
138 | + $attendee_reg_form = $this->_registrations_reg_form($registration); |
|
139 | + if ($attendee_reg_form instanceof EE_Form_Section_Proper) { |
|
140 | + $subsections[$registration->reg_url_link()] = $attendee_reg_form; |
|
141 | + if ( ! $this->checkout->admin_request) { |
|
142 | + $template_args['registrations'][$registration->reg_url_link()] = $registration; |
|
143 | + $template_args['ticket_count'][$registration->ticket()->ID()] = isset( |
|
144 | + $template_args['ticket_count'][$registration->ticket()->ID()] |
|
145 | + ) |
|
146 | + ? $template_args['ticket_count'][$registration->ticket()->ID()] + 1 |
|
147 | + : 1; |
|
148 | + $ticket_line_item = EEH_Line_Item::get_line_items_by_object_type_and_IDs( |
|
149 | + $this->checkout->cart->get_grand_total(), |
|
150 | + 'Ticket', |
|
151 | + array($registration->ticket()->ID()) |
|
152 | + ); |
|
153 | + $ticket_line_item = is_array($ticket_line_item) |
|
154 | + ? reset($ticket_line_item) |
|
155 | + : $ticket_line_item; |
|
156 | + $template_args['ticket_line_item'][$registration->ticket() |
|
157 | + ->ID()] = $Line_Item_Display->display_line_item( |
|
158 | + $ticket_line_item |
|
159 | + ); |
|
160 | + } |
|
161 | + } |
|
162 | + if ($registration->is_primary_registrant()) { |
|
163 | + $primary_registrant = $registration->reg_url_link(); |
|
164 | + } |
|
165 | 165 | } |
166 | 166 | } |
167 | 167 | // print_copy_info ? |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | // generate hidden input |
174 | 174 | if ( |
175 | 175 | isset( $subsections[ $primary_registrant ] ) |
176 | - && $subsections[ $primary_registrant ] instanceof EE_Form_Section_Proper |
|
176 | + && $subsections[ $primary_registrant ] instanceof EE_Form_Section_Proper |
|
177 | 177 | ) { |
178 | 178 | $subsections[ $primary_registrant ]->add_subsections( $copy_options, 'primary_registrant', false ); |
179 | 179 | } |
@@ -208,35 +208,35 @@ discard block |
||
208 | 208 | */ |
209 | 209 | private function _registrations_reg_form( EE_Registration $registration ) { |
210 | 210 | static $attendee_nmbr = 1; |
211 | - $form_args = array(); |
|
211 | + $form_args = array(); |
|
212 | 212 | // verify that registration has valid event |
213 | 213 | if ( $registration->event() instanceof EE_Event ) { |
214 | 214 | $question_groups = $registration->event()->question_groups( |
215 | - array( |
|
216 | - array( |
|
217 | - 'Event.EVT_ID' => $registration->event()->ID(), |
|
218 | - 'Event_Question_Group.EQG_primary' => $registration->count() === 1 ? true : false |
|
219 | - ), |
|
220 | - 'order_by' => array('QSG_order' => 'ASC') |
|
221 | - ) |
|
222 | - ); |
|
215 | + array( |
|
216 | + array( |
|
217 | + 'Event.EVT_ID' => $registration->event()->ID(), |
|
218 | + 'Event_Question_Group.EQG_primary' => $registration->count() === 1 ? true : false |
|
219 | + ), |
|
220 | + 'order_by' => array('QSG_order' => 'ASC') |
|
221 | + ) |
|
222 | + ); |
|
223 | 223 | if ( $question_groups ) { |
224 | - // array of params to pass to parent constructor |
|
225 | - $form_args = array( |
|
226 | - 'html_id' => 'ee-registration-' . $registration->reg_url_link(), |
|
227 | - 'html_class' => 'ee-reg-form-attendee-dv', |
|
228 | - 'html_style' => $this->checkout->admin_request |
|
229 | - ? 'padding:0em 2em 1em; margin:3em 0 0; border:1px solid #ddd;' |
|
230 | - : '', |
|
231 | - 'subsections' => array(), |
|
232 | - 'layout_strategy' => new EE_Fieldset_Section_Layout( |
|
233 | - array( |
|
234 | - 'legend_class' => 'spco-attendee-lgnd smaller-text lt-grey-text', |
|
235 | - 'legend_text' => sprintf(__('Attendee %d', 'event_espresso'), $attendee_nmbr) |
|
236 | - ) |
|
237 | - ) |
|
238 | - ); |
|
239 | - foreach ( $question_groups as $question_group ) { |
|
224 | + // array of params to pass to parent constructor |
|
225 | + $form_args = array( |
|
226 | + 'html_id' => 'ee-registration-' . $registration->reg_url_link(), |
|
227 | + 'html_class' => 'ee-reg-form-attendee-dv', |
|
228 | + 'html_style' => $this->checkout->admin_request |
|
229 | + ? 'padding:0em 2em 1em; margin:3em 0 0; border:1px solid #ddd;' |
|
230 | + : '', |
|
231 | + 'subsections' => array(), |
|
232 | + 'layout_strategy' => new EE_Fieldset_Section_Layout( |
|
233 | + array( |
|
234 | + 'legend_class' => 'spco-attendee-lgnd smaller-text lt-grey-text', |
|
235 | + 'legend_text' => sprintf(__('Attendee %d', 'event_espresso'), $attendee_nmbr) |
|
236 | + ) |
|
237 | + ) |
|
238 | + ); |
|
239 | + foreach ( $question_groups as $question_group ) { |
|
240 | 240 | if ( $question_group instanceof EE_Question_Group ) { |
241 | 241 | $form_args['subsections'][ $question_group->identifier() ] = $this->_question_group_reg_form( |
242 | 242 | $registration, |
@@ -244,19 +244,19 @@ discard block |
||
244 | 244 | ); |
245 | 245 | } |
246 | 246 | } |
247 | - // add hidden input |
|
248 | - $form_args['subsections']['additional_attendee_reg_info'] = $this->_additional_attendee_reg_info_input( |
|
249 | - $registration |
|
250 | - ); |
|
251 | - // if we have question groups for additional attendees, then display the copy options |
|
247 | + // add hidden input |
|
248 | + $form_args['subsections']['additional_attendee_reg_info'] = $this->_additional_attendee_reg_info_input( |
|
249 | + $registration |
|
250 | + ); |
|
251 | + // if we have question groups for additional attendees, then display the copy options |
|
252 | 252 | $this->_print_copy_info = $attendee_nmbr > 1 ? true : $this->_print_copy_info; |
253 | - if ($registration->is_primary_registrant()) { |
|
254 | - // generate hidden input |
|
255 | - $form_args['subsections']['primary_registrant'] = $this->_additional_primary_registrant_inputs($registration); |
|
256 | - } |
|
257 | - } |
|
253 | + if ($registration->is_primary_registrant()) { |
|
254 | + // generate hidden input |
|
255 | + $form_args['subsections']['primary_registrant'] = $this->_additional_primary_registrant_inputs($registration); |
|
256 | + } |
|
257 | + } |
|
258 | 258 | } |
259 | - $attendee_nmbr++; |
|
259 | + $attendee_nmbr++; |
|
260 | 260 | return ! empty($form_args) ? new EE_Form_Section_Proper( $form_args ) : null; |
261 | 261 | } |
262 | 262 | |
@@ -889,7 +889,7 @@ discard block |
||
889 | 889 | if ( isset( $valid_data[ $reg_url_link ] ) ) { |
890 | 890 | // do we need to copy basic info from primary attendee ? |
891 | 891 | $copy_primary = isset( $valid_data[ $reg_url_link ]['additional_attendee_reg_info'] ) |
892 | - && absint( $valid_data[ $reg_url_link ]['additional_attendee_reg_info'] ) === 0 |
|
892 | + && absint( $valid_data[ $reg_url_link ]['additional_attendee_reg_info'] ) === 0 |
|
893 | 893 | ? true |
894 | 894 | : false; |
895 | 895 | // filter form input data for this registration |
@@ -1064,7 +1064,7 @@ discard block |
||
1064 | 1064 | ? $form_input |
1065 | 1065 | : $form_input . '-' . $registration->reg_url_link(); |
1066 | 1066 | $answer_is_obj = isset( $this->_registration_answers[ $answer_cache_id ] ) |
1067 | - && $this->_registration_answers[ $answer_cache_id ] instanceof EE_Answer |
|
1067 | + && $this->_registration_answers[ $answer_cache_id ] instanceof EE_Answer |
|
1068 | 1068 | ? true |
1069 | 1069 | : false; |
1070 | 1070 | //rename form_inputs if they are EE_Attendee properties |
@@ -1184,7 +1184,7 @@ discard block |
||
1184 | 1184 | // then attempt to copy them from the primary attendee |
1185 | 1185 | if ( |
1186 | 1186 | $this->checkout->primary_attendee_obj instanceof EE_Attendee |
1187 | - && ! isset( $attendee_data['ATT_fname'], $attendee_data['ATT_email'] ) |
|
1187 | + && ! isset( $attendee_data['ATT_fname'], $attendee_data['ATT_email'] ) |
|
1188 | 1188 | ) { |
1189 | 1189 | return $this->checkout->primary_attendee_obj; |
1190 | 1190 | } |
@@ -1302,7 +1302,7 @@ discard block |
||
1302 | 1302 | } |
1303 | 1303 | foreach ( $critical_attendee_details as $critical_attendee_detail ) { |
1304 | 1304 | if ( ! isset( $attendee_data[ $critical_attendee_detail ] ) |
1305 | - || empty( $attendee_data[ $critical_attendee_detail ] ) |
|
1305 | + || empty( $attendee_data[ $critical_attendee_detail ] ) |
|
1306 | 1306 | ) { |
1307 | 1307 | $attendee_data[ $critical_attendee_detail ] = $this->checkout->primary_attendee_obj->get( |
1308 | 1308 | $critical_attendee_detail |
@@ -41,21 +41,21 @@ discard block |
||
41 | 41 | * @access public |
42 | 42 | * @param EE_Checkout $checkout |
43 | 43 | */ |
44 | - public function __construct( EE_Checkout $checkout ) { |
|
44 | + public function __construct(EE_Checkout $checkout) { |
|
45 | 45 | $this->_slug = 'attendee_information'; |
46 | 46 | $this->_name = __('Attendee Information', 'event_espresso'); |
47 | - $this->_template = SPCO_REG_STEPS_PATH . $this->_slug . DS . 'attendee_info_main.template.php'; |
|
47 | + $this->_template = SPCO_REG_STEPS_PATH.$this->_slug.DS.'attendee_info_main.template.php'; |
|
48 | 48 | $this->checkout = $checkout; |
49 | 49 | $this->_reset_success_message(); |
50 | 50 | $this->set_instructions( |
51 | - __( 'Please answer the following registration questions before proceeding.', 'event_espresso' ) |
|
51 | + __('Please answer the following registration questions before proceeding.', 'event_espresso') |
|
52 | 52 | ); |
53 | 53 | } |
54 | 54 | |
55 | 55 | |
56 | 56 | |
57 | 57 | public function translate_js_strings() { |
58 | - EE_Registry::$i18n_js_strings['required_field'] = __( ' is a required question.', 'event_espresso' ); |
|
58 | + EE_Registry::$i18n_js_strings['required_field'] = __(' is a required question.', 'event_espresso'); |
|
59 | 59 | EE_Registry::$i18n_js_strings['required_multi_field'] = __( |
60 | 60 | ' is a required question. Please enter a value for at least one of the options.', |
61 | 61 | 'event_espresso' |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | // calculate taxes |
116 | 116 | $Line_Item_Display->display_line_item( |
117 | 117 | $this->checkout->cart->get_grand_total(), |
118 | - array( 'set_tax_rate' => true ) |
|
118 | + array('set_tax_rate' => true) |
|
119 | 119 | ); |
120 | 120 | /** @var $subsections EE_Form_Section_Proper[] */ |
121 | 121 | $subsections = array( |
@@ -127,13 +127,13 @@ discard block |
||
127 | 127 | 'ticket_count' => array() |
128 | 128 | ); |
129 | 129 | // grab the saved registrations from the transaction |
130 | - $registrations = $this->checkout->transaction->registrations( $this->checkout->reg_cache_where_params ); |
|
131 | - if ( $registrations ) { |
|
132 | - foreach ( $registrations as $registration ) { |
|
130 | + $registrations = $this->checkout->transaction->registrations($this->checkout->reg_cache_where_params); |
|
131 | + if ($registrations) { |
|
132 | + foreach ($registrations as $registration) { |
|
133 | 133 | // can this registration be processed during this visit ? |
134 | 134 | if ( |
135 | 135 | $registration instanceof EE_Registration |
136 | - && $this->checkout->visit_allows_processing_of_this_registration( $registration ) |
|
136 | + && $this->checkout->visit_allows_processing_of_this_registration($registration) |
|
137 | 137 | ) { |
138 | 138 | $attendee_reg_form = $this->_registrations_reg_form($registration); |
139 | 139 | if ($attendee_reg_form instanceof EE_Form_Section_Proper) { |
@@ -165,17 +165,17 @@ discard block |
||
165 | 165 | } |
166 | 166 | } |
167 | 167 | // print_copy_info ? |
168 | - if ( $primary_registrant && ! $this->checkout->admin_request && count( $registrations ) > 1 ) { |
|
168 | + if ($primary_registrant && ! $this->checkout->admin_request && count($registrations) > 1) { |
|
169 | 169 | // TODO: add admin option for toggling copy attendee info, then use that value to change $this->_print_copy_info |
170 | 170 | $copy_options['spco_copy_attendee_chk'] = $this->_print_copy_info |
171 | 171 | ? $this->_copy_attendee_info_form() |
172 | 172 | : $this->_auto_copy_attendee_info(); |
173 | 173 | // generate hidden input |
174 | 174 | if ( |
175 | - isset( $subsections[ $primary_registrant ] ) |
|
176 | - && $subsections[ $primary_registrant ] instanceof EE_Form_Section_Proper |
|
175 | + isset($subsections[$primary_registrant]) |
|
176 | + && $subsections[$primary_registrant] instanceof EE_Form_Section_Proper |
|
177 | 177 | ) { |
178 | - $subsections[ $primary_registrant ]->add_subsections( $copy_options, 'primary_registrant', false ); |
|
178 | + $subsections[$primary_registrant]->add_subsections($copy_options, 'primary_registrant', false); |
|
179 | 179 | } |
180 | 180 | } |
181 | 181 | |
@@ -187,8 +187,7 @@ discard block |
||
187 | 187 | 'html_id' => $this->reg_form_name(), |
188 | 188 | 'subsections' => $subsections, |
189 | 189 | 'layout_strategy' => $this->checkout->admin_request ? |
190 | - new EE_Div_Per_Section_Layout() : |
|
191 | - new EE_Template_Layout( |
|
190 | + new EE_Div_Per_Section_Layout() : new EE_Template_Layout( |
|
192 | 191 | array( |
193 | 192 | 'layout_template_file' => $this->_template, // layout_template |
194 | 193 | 'template_args' => $template_args |
@@ -206,11 +205,11 @@ discard block |
||
206 | 205 | * @return EE_Form_Section_Proper |
207 | 206 | * @throws \EE_Error |
208 | 207 | */ |
209 | - private function _registrations_reg_form( EE_Registration $registration ) { |
|
208 | + private function _registrations_reg_form(EE_Registration $registration) { |
|
210 | 209 | static $attendee_nmbr = 1; |
211 | 210 | $form_args = array(); |
212 | 211 | // verify that registration has valid event |
213 | - if ( $registration->event() instanceof EE_Event ) { |
|
212 | + if ($registration->event() instanceof EE_Event) { |
|
214 | 213 | $question_groups = $registration->event()->question_groups( |
215 | 214 | array( |
216 | 215 | array( |
@@ -220,10 +219,10 @@ discard block |
||
220 | 219 | 'order_by' => array('QSG_order' => 'ASC') |
221 | 220 | ) |
222 | 221 | ); |
223 | - if ( $question_groups ) { |
|
222 | + if ($question_groups) { |
|
224 | 223 | // array of params to pass to parent constructor |
225 | 224 | $form_args = array( |
226 | - 'html_id' => 'ee-registration-' . $registration->reg_url_link(), |
|
225 | + 'html_id' => 'ee-registration-'.$registration->reg_url_link(), |
|
227 | 226 | 'html_class' => 'ee-reg-form-attendee-dv', |
228 | 227 | 'html_style' => $this->checkout->admin_request |
229 | 228 | ? 'padding:0em 2em 1em; margin:3em 0 0; border:1px solid #ddd;' |
@@ -236,9 +235,9 @@ discard block |
||
236 | 235 | ) |
237 | 236 | ) |
238 | 237 | ); |
239 | - foreach ( $question_groups as $question_group ) { |
|
240 | - if ( $question_group instanceof EE_Question_Group ) { |
|
241 | - $form_args['subsections'][ $question_group->identifier() ] = $this->_question_group_reg_form( |
|
238 | + foreach ($question_groups as $question_group) { |
|
239 | + if ($question_group instanceof EE_Question_Group) { |
|
240 | + $form_args['subsections'][$question_group->identifier()] = $this->_question_group_reg_form( |
|
242 | 241 | $registration, |
243 | 242 | $question_group |
244 | 243 | ); |
@@ -257,7 +256,7 @@ discard block |
||
257 | 256 | } |
258 | 257 | } |
259 | 258 | $attendee_nmbr++; |
260 | - return ! empty($form_args) ? new EE_Form_Section_Proper( $form_args ) : null; |
|
259 | + return ! empty($form_args) ? new EE_Form_Section_Proper($form_args) : null; |
|
261 | 260 | } |
262 | 261 | |
263 | 262 | |
@@ -278,7 +277,7 @@ discard block |
||
278 | 277 | // generate hidden input |
279 | 278 | return new EE_Hidden_Input( |
280 | 279 | array( |
281 | - 'html_id' => 'additional-attendee-reg-info-' . $registration->reg_url_link(), |
|
280 | + 'html_id' => 'additional-attendee-reg-info-'.$registration->reg_url_link(), |
|
282 | 281 | 'default' => $additional_attendee_reg_info |
283 | 282 | ) |
284 | 283 | ); |
@@ -292,26 +291,26 @@ discard block |
||
292 | 291 | * @return EE_Form_Section_Proper |
293 | 292 | * @throws \EE_Error |
294 | 293 | */ |
295 | - private function _question_group_reg_form( EE_Registration $registration, EE_Question_Group $question_group ){ |
|
294 | + private function _question_group_reg_form(EE_Registration $registration, EE_Question_Group $question_group) { |
|
296 | 295 | // array of params to pass to parent constructor |
297 | 296 | $form_args = array( |
298 | - 'html_id' => 'ee-reg-form-qstn-grp-' . $question_group->identifier(), |
|
297 | + 'html_id' => 'ee-reg-form-qstn-grp-'.$question_group->identifier(), |
|
299 | 298 | 'html_class' => $this->checkout->admin_request |
300 | 299 | ? 'form-table ee-reg-form-qstn-grp-dv' |
301 | 300 | : 'ee-reg-form-qstn-grp-dv', |
302 | - 'html_label_id' => 'ee-reg-form-qstn-grp-' . $question_group->identifier() . '-lbl', |
|
301 | + 'html_label_id' => 'ee-reg-form-qstn-grp-'.$question_group->identifier().'-lbl', |
|
303 | 302 | 'subsections' => array( |
304 | - 'reg_form_qstn_grp_hdr' => $this->_question_group_header( $question_group ) |
|
303 | + 'reg_form_qstn_grp_hdr' => $this->_question_group_header($question_group) |
|
305 | 304 | ), |
306 | 305 | 'layout_strategy' => $this->checkout->admin_request |
307 | 306 | ? new EE_Admin_Two_Column_Layout() |
308 | 307 | : new EE_Div_Per_Section_Layout() |
309 | 308 | ); |
310 | 309 | // where params |
311 | - $query_params = array( 'QST_deleted' => 0 ); |
|
310 | + $query_params = array('QST_deleted' => 0); |
|
312 | 311 | // don't load admin only questions on the frontend |
313 | - if ( ! $this->checkout->admin_request ) { |
|
314 | - $query_params['QST_admin_only'] = array( '!=', true ); |
|
312 | + if ( ! $this->checkout->admin_request) { |
|
313 | + $query_params['QST_admin_only'] = array('!=', true); |
|
315 | 314 | } |
316 | 315 | $questions = $question_group->get_many_related( |
317 | 316 | 'Question', |
@@ -333,10 +332,10 @@ discard block |
||
333 | 332 | ) |
334 | 333 | ); |
335 | 334 | // loop thru questions |
336 | - foreach ( $questions as $question ) { |
|
337 | - if( $question instanceof EE_Question ){ |
|
335 | + foreach ($questions as $question) { |
|
336 | + if ($question instanceof EE_Question) { |
|
338 | 337 | $identifier = $question->is_system_question() ? $question->system_ID() : $question->ID(); |
339 | - $form_args['subsections'][ $identifier ] = $this->reg_form_question( $registration, $question ); |
|
338 | + $form_args['subsections'][$identifier] = $this->reg_form_question($registration, $question); |
|
340 | 339 | } |
341 | 340 | } |
342 | 341 | $form_args['subsections'] = apply_filters( |
@@ -357,7 +356,7 @@ discard block |
||
357 | 356 | ) |
358 | 357 | ); |
359 | 358 | // d( $form_args ); |
360 | - $question_group_reg_form = new EE_Form_Section_Proper( $form_args ); |
|
359 | + $question_group_reg_form = new EE_Form_Section_Proper($form_args); |
|
361 | 360 | return apply_filters( |
362 | 361 | 'FHEE__EE_SPCO_Reg_Step_Attendee_Information___question_group_reg_form__question_group_reg_form', |
363 | 362 | $question_group_reg_form, |
@@ -374,11 +373,11 @@ discard block |
||
374 | 373 | * @param EE_Question_Group $question_group |
375 | 374 | * @return EE_Form_Section_HTML |
376 | 375 | */ |
377 | - private function _question_group_header( EE_Question_Group $question_group ){ |
|
376 | + private function _question_group_header(EE_Question_Group $question_group) { |
|
378 | 377 | $html = ''; |
379 | 378 | // group_name |
380 | - if ( $question_group->show_group_name() && $question_group->name() !== '' ) { |
|
381 | - if ( $this->checkout->admin_request ) { |
|
379 | + if ($question_group->show_group_name() && $question_group->name() !== '') { |
|
380 | + if ($this->checkout->admin_request) { |
|
382 | 381 | $html .= EEH_HTML::br(); |
383 | 382 | $html .= EEH_HTML::h3( |
384 | 383 | $question_group->name(), |
@@ -392,7 +391,7 @@ discard block |
||
392 | 391 | } |
393 | 392 | } |
394 | 393 | // group_desc |
395 | - if ( $question_group->show_group_desc() && $question_group->desc() !== '' ) { |
|
394 | + if ($question_group->show_group_desc() && $question_group->desc() !== '') { |
|
396 | 395 | $html .= EEH_HTML::p( |
397 | 396 | $question_group->desc(), |
398 | 397 | '', |
@@ -402,7 +401,7 @@ discard block |
||
402 | 401 | ); |
403 | 402 | |
404 | 403 | } |
405 | - return new EE_Form_Section_HTML( $html ); |
|
404 | + return new EE_Form_Section_HTML($html); |
|
406 | 405 | } |
407 | 406 | |
408 | 407 | |
@@ -412,7 +411,7 @@ discard block |
||
412 | 411 | * @return EE_Form_Section_Proper |
413 | 412 | * @throws \EE_Error |
414 | 413 | */ |
415 | - private function _copy_attendee_info_form(){ |
|
414 | + private function _copy_attendee_info_form() { |
|
416 | 415 | // array of params to pass to parent constructor |
417 | 416 | return new EE_Form_Section_Proper( |
418 | 417 | array( |
@@ -441,7 +440,7 @@ discard block |
||
441 | 440 | private function _auto_copy_attendee_info() { |
442 | 441 | return new EE_Form_Section_HTML( |
443 | 442 | EEH_Template::locate_template( |
444 | - SPCO_REG_STEPS_PATH . $this->_slug . DS . '_auto_copy_attendee_info.template.php', |
|
443 | + SPCO_REG_STEPS_PATH.$this->_slug.DS.'_auto_copy_attendee_info.template.php', |
|
445 | 444 | apply_filters( |
446 | 445 | 'FHEE__EE_SPCO_Reg_Step_Attendee_Information__auto_copy_attendee_info__template_args', |
447 | 446 | array() |
@@ -465,32 +464,32 @@ discard block |
||
465 | 464 | $copy_attendee_info_inputs = array(); |
466 | 465 | $prev_ticket = NULL; |
467 | 466 | // grab the saved registrations from the transaction |
468 | - $registrations = $this->checkout->transaction->registrations( $this->checkout->reg_cache_where_params ); |
|
469 | - foreach ( $registrations as $registration ) { |
|
467 | + $registrations = $this->checkout->transaction->registrations($this->checkout->reg_cache_where_params); |
|
468 | + foreach ($registrations as $registration) { |
|
470 | 469 | // for all attendees other than the primary attendee |
471 | - if ( $registration instanceof EE_Registration && ! $registration->is_primary_registrant() ) { |
|
470 | + if ($registration instanceof EE_Registration && ! $registration->is_primary_registrant()) { |
|
472 | 471 | // if this is a new ticket OR if this is the very first additional attendee after the primary attendee |
473 | - if ( $registration->ticket()->ID() !== $prev_ticket ) { |
|
472 | + if ($registration->ticket()->ID() !== $prev_ticket) { |
|
474 | 473 | $item_name = $registration->ticket()->name(); |
475 | 474 | $item_name .= $registration->ticket()->description() !== '' |
476 | - ? ' - ' . $registration->ticket()->description() |
|
475 | + ? ' - '.$registration->ticket()->description() |
|
477 | 476 | : ''; |
478 | - $copy_attendee_info_inputs[ 'spco_copy_attendee_chk[ticket-' . $registration->ticket()->ID() . ']' ] = new EE_Form_Section_HTML( |
|
479 | - '<h6 class="spco-copy-attendee-event-hdr">' . $item_name . '</h6>' |
|
477 | + $copy_attendee_info_inputs['spco_copy_attendee_chk[ticket-'.$registration->ticket()->ID().']'] = new EE_Form_Section_HTML( |
|
478 | + '<h6 class="spco-copy-attendee-event-hdr">'.$item_name.'</h6>' |
|
480 | 479 | ); |
481 | 480 | $prev_ticket = $registration->ticket()->ID(); |
482 | 481 | } |
483 | 482 | |
484 | - $copy_attendee_info_inputs[ 'spco_copy_attendee_chk[' . $registration->ID() . ']' ] = new |
|
483 | + $copy_attendee_info_inputs['spco_copy_attendee_chk['.$registration->ID().']'] = new |
|
485 | 484 | EE_Checkbox_Multi_Input( |
486 | 485 | array( |
487 | 486 | $registration->ID() => sprintf( |
488 | - __( 'Attendee #%s', 'event_espresso' ), |
|
487 | + __('Attendee #%s', 'event_espresso'), |
|
489 | 488 | $registration->count() |
490 | 489 | ) |
491 | 490 | ), |
492 | 491 | array( |
493 | - 'html_id' => 'spco-copy-attendee-chk-' . $registration->reg_url_link(), |
|
492 | + 'html_id' => 'spco-copy-attendee-chk-'.$registration->reg_url_link(), |
|
494 | 493 | 'html_class' => 'spco-copy-attendee-chk ee-do-not-validate', |
495 | 494 | 'display_html_label_text' => false |
496 | 495 | ) |
@@ -510,7 +509,7 @@ discard block |
||
510 | 509 | * @return EE_Form_Input_Base |
511 | 510 | * @throws \EE_Error |
512 | 511 | */ |
513 | - private function _additional_primary_registrant_inputs( EE_Registration $registration ){ |
|
512 | + private function _additional_primary_registrant_inputs(EE_Registration $registration) { |
|
514 | 513 | // generate hidden input |
515 | 514 | return new EE_Hidden_Input( |
516 | 515 | array( |
@@ -529,7 +528,7 @@ discard block |
||
529 | 528 | * @return EE_Form_Input_Base |
530 | 529 | * @throws \EE_Error |
531 | 530 | */ |
532 | - public function reg_form_question( EE_Registration $registration, EE_Question $question ){ |
|
531 | + public function reg_form_question(EE_Registration $registration, EE_Question $question) { |
|
533 | 532 | |
534 | 533 | // if this question was for an attendee detail, then check for that answer |
535 | 534 | $answer_value = EEM_Answer::instance()->get_attendee_property_answer_value( |
@@ -538,32 +537,32 @@ discard block |
||
538 | 537 | ); |
539 | 538 | $answer = $answer_value === null |
540 | 539 | ? EEM_Answer::instance()->get_one( |
541 | - array( array( 'QST_ID' => $question->ID(), 'REG_ID' => $registration->ID() ) ) |
|
540 | + array(array('QST_ID' => $question->ID(), 'REG_ID' => $registration->ID())) |
|
542 | 541 | ) |
543 | 542 | : null; |
544 | 543 | // if NOT returning to edit an existing registration |
545 | 544 | // OR if this question is for an attendee property |
546 | 545 | // OR we still don't have an EE_Answer object |
547 | - if( $answer_value || ! $answer instanceof EE_Answer || ! $registration->reg_url_link() ) { |
|
546 | + if ($answer_value || ! $answer instanceof EE_Answer || ! $registration->reg_url_link()) { |
|
548 | 547 | // create an EE_Answer object for storing everything in |
549 | - $answer = EE_Answer::new_instance ( array( |
|
548 | + $answer = EE_Answer::new_instance(array( |
|
550 | 549 | 'QST_ID'=> $question->ID(), |
551 | 550 | 'REG_ID'=> $registration->ID() |
552 | 551 | )); |
553 | 552 | } |
554 | 553 | // verify instance |
555 | - if( $answer instanceof EE_Answer ){ |
|
556 | - if ( ! empty( $answer_value )) { |
|
557 | - $answer->set( 'ANS_value', $answer_value ); |
|
554 | + if ($answer instanceof EE_Answer) { |
|
555 | + if ( ! empty($answer_value)) { |
|
556 | + $answer->set('ANS_value', $answer_value); |
|
558 | 557 | } |
559 | - $answer->cache( 'Question', $question ); |
|
558 | + $answer->cache('Question', $question); |
|
560 | 559 | //remember system ID had a bug where sometimes it could be null |
561 | - $answer_cache_id =$question->is_system_question() |
|
562 | - ? $question->system_ID() . '-' . $registration->reg_url_link() |
|
563 | - : $question->ID() . '-' . $registration->reg_url_link(); |
|
564 | - $registration->cache( 'Answer', $answer, $answer_cache_id ); |
|
560 | + $answer_cache_id = $question->is_system_question() |
|
561 | + ? $question->system_ID().'-'.$registration->reg_url_link() |
|
562 | + : $question->ID().'-'.$registration->reg_url_link(); |
|
563 | + $registration->cache('Answer', $answer, $answer_cache_id); |
|
565 | 564 | } |
566 | - return $this->_generate_question_input( $registration, $question, $answer ); |
|
565 | + return $this->_generate_question_input($registration, $question, $answer); |
|
567 | 566 | |
568 | 567 | } |
569 | 568 | |
@@ -576,46 +575,46 @@ discard block |
||
576 | 575 | * @return EE_Form_Input_Base |
577 | 576 | * @throws \EE_Error |
578 | 577 | */ |
579 | - private function _generate_question_input( EE_Registration $registration, EE_Question $question, $answer ){ |
|
578 | + private function _generate_question_input(EE_Registration $registration, EE_Question $question, $answer) { |
|
580 | 579 | $identifier = $question->is_system_question() ? $question->system_ID() : $question->ID(); |
581 | - $this->_required_questions[ $identifier ] = $question->required() ? true : false; |
|
580 | + $this->_required_questions[$identifier] = $question->required() ? true : false; |
|
582 | 581 | add_filter( |
583 | 582 | 'FHEE__EE_Question__generate_form_input__country_options', |
584 | - array( $this, 'use_cached_countries_for_form_input' ), |
|
583 | + array($this, 'use_cached_countries_for_form_input'), |
|
585 | 584 | 10, |
586 | 585 | 4 |
587 | 586 | ); |
588 | 587 | add_filter( |
589 | 588 | 'FHEE__EE_Question__generate_form_input__state_options', |
590 | - array( $this, 'use_cached_states_for_form_input' ), |
|
589 | + array($this, 'use_cached_states_for_form_input'), |
|
591 | 590 | 10, |
592 | 591 | 4 |
593 | 592 | ); |
594 | 593 | $input_constructor_args = array( |
595 | - 'html_name' => 'ee_reg_qstn[' . $registration->ID() . '][' . $identifier . ']', |
|
596 | - 'html_id' => 'ee_reg_qstn-' . $registration->ID() . '-' . $identifier, |
|
597 | - 'html_class' => 'ee-reg-qstn ee-reg-qstn-' . $identifier, |
|
598 | - 'html_label_id' => 'ee_reg_qstn-' . $registration->ID() . '-' . $identifier, |
|
594 | + 'html_name' => 'ee_reg_qstn['.$registration->ID().']['.$identifier.']', |
|
595 | + 'html_id' => 'ee_reg_qstn-'.$registration->ID().'-'.$identifier, |
|
596 | + 'html_class' => 'ee-reg-qstn ee-reg-qstn-'.$identifier, |
|
597 | + 'html_label_id' => 'ee_reg_qstn-'.$registration->ID().'-'.$identifier, |
|
599 | 598 | 'html_label_class' => 'ee-reg-qstn', |
600 | 599 | ); |
601 | - $input_constructor_args['html_label_id'] .= '-lbl'; |
|
602 | - if ( $answer instanceof EE_Answer && $answer->ID() ) { |
|
603 | - $input_constructor_args[ 'html_name' ] .= '[' . $answer->ID() . ']'; |
|
604 | - $input_constructor_args[ 'html_id' ] .= '-' . $answer->ID(); |
|
605 | - $input_constructor_args[ 'html_label_id' ] .= '-' . $answer->ID(); |
|
600 | + $input_constructor_args['html_label_id'] .= '-lbl'; |
|
601 | + if ($answer instanceof EE_Answer && $answer->ID()) { |
|
602 | + $input_constructor_args['html_name'] .= '['.$answer->ID().']'; |
|
603 | + $input_constructor_args['html_id'] .= '-'.$answer->ID(); |
|
604 | + $input_constructor_args['html_label_id'] .= '-'.$answer->ID(); |
|
606 | 605 | } |
607 | - $form_input = $question->generate_form_input( |
|
606 | + $form_input = $question->generate_form_input( |
|
608 | 607 | $registration, |
609 | 608 | $answer, |
610 | 609 | $input_constructor_args |
611 | 610 | ); |
612 | 611 | remove_filter( |
613 | 612 | 'FHEE__EE_Question__generate_form_input__country_options', |
614 | - array( $this, 'use_cached_countries_for_form_input' ) |
|
613 | + array($this, 'use_cached_countries_for_form_input') |
|
615 | 614 | ); |
616 | 615 | remove_filter( |
617 | 616 | 'FHEE__EE_Question__generate_form_input__state_options', |
618 | - array( $this, 'use_cached_states_for_form_input' ) |
|
617 | + array($this, 'use_cached_states_for_form_input') |
|
619 | 618 | ); |
620 | 619 | return $form_input; |
621 | 620 | } |
@@ -637,22 +636,22 @@ discard block |
||
637 | 636 | \EE_Registration $registration = null, |
638 | 637 | \EE_Answer $answer = null |
639 | 638 | ) { |
640 | - $country_options = array( '' => '' ); |
|
639 | + $country_options = array('' => ''); |
|
641 | 640 | // get possibly cached list of countries |
642 | 641 | $countries = $this->checkout->action === 'process_reg_step' |
643 | 642 | ? EEM_Country::instance()->get_all_countries() |
644 | 643 | : EEM_Country::instance()->get_all_active_countries(); |
645 | - if ( ! empty( $countries )) { |
|
646 | - foreach( $countries as $country ){ |
|
647 | - if ( $country instanceof EE_Country ) { |
|
648 | - $country_options[ $country->ID() ] = $country->name(); |
|
644 | + if ( ! empty($countries)) { |
|
645 | + foreach ($countries as $country) { |
|
646 | + if ($country instanceof EE_Country) { |
|
647 | + $country_options[$country->ID()] = $country->name(); |
|
649 | 648 | } |
650 | 649 | } |
651 | 650 | } |
652 | - if( $question instanceof EE_Question |
|
653 | - && $registration instanceof EE_Registration ) { |
|
651 | + if ($question instanceof EE_Question |
|
652 | + && $registration instanceof EE_Registration) { |
|
654 | 653 | $answer = EEM_Answer::instance()->get_one( |
655 | - array( array( 'QST_ID' => $question->ID(), 'REG_ID' => $registration->ID() ) ) |
|
654 | + array(array('QST_ID' => $question->ID(), 'REG_ID' => $registration->ID())) |
|
656 | 655 | ); |
657 | 656 | } else { |
658 | 657 | $answer = EE_Answer::new_instance(); |
@@ -685,14 +684,14 @@ discard block |
||
685 | 684 | \EE_Registration $registration = null, |
686 | 685 | \EE_Answer $answer = null |
687 | 686 | ) { |
688 | - $state_options = array( '' => array( '' => '')); |
|
687 | + $state_options = array('' => array('' => '')); |
|
689 | 688 | $states = $this->checkout->action === 'process_reg_step' |
690 | 689 | ? EEM_State::instance()->get_all_states() |
691 | 690 | : EEM_State::instance()->get_all_active_states(); |
692 | - if ( ! empty( $states )) { |
|
693 | - foreach( $states as $state ){ |
|
694 | - if ( $state instanceof EE_State ) { |
|
695 | - $state_options[ $state->country()->name() ][ $state->ID() ] = $state->name(); |
|
691 | + if ( ! empty($states)) { |
|
692 | + foreach ($states as $state) { |
|
693 | + if ($state instanceof EE_State) { |
|
694 | + $state_options[$state->country()->name()][$state->ID()] = $state->name(); |
|
696 | 695 | } |
697 | 696 | } |
698 | 697 | } |
@@ -720,24 +719,24 @@ discard block |
||
720 | 719 | * @throws \EE_Error |
721 | 720 | */ |
722 | 721 | public function process_reg_step() { |
723 | - do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' ); |
|
722 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
724 | 723 | // grab validated data from form |
725 | 724 | $valid_data = $this->checkout->current_step->valid_data(); |
726 | 725 | // EEH_Debug_Tools::printr( $_REQUEST, '$_REQUEST', __FILE__, __LINE__ ); |
727 | 726 | // EEH_Debug_Tools::printr( $valid_data, '$valid_data', __FILE__, __LINE__ ); |
728 | 727 | // if we don't have any $valid_data then something went TERRIBLY WRONG !!! |
729 | - if ( empty( $valid_data )) { |
|
728 | + if (empty($valid_data)) { |
|
730 | 729 | EE_Error::add_error( |
731 | - __( 'No valid question responses were received.', 'event_espresso' ), |
|
730 | + __('No valid question responses were received.', 'event_espresso'), |
|
732 | 731 | __FILE__, |
733 | 732 | __FUNCTION__, |
734 | 733 | __LINE__ |
735 | 734 | ); |
736 | 735 | return false; |
737 | 736 | } |
738 | - if ( ! $this->checkout->transaction instanceof EE_Transaction || ! $this->checkout->continue_reg ) { |
|
737 | + if ( ! $this->checkout->transaction instanceof EE_Transaction || ! $this->checkout->continue_reg) { |
|
739 | 738 | EE_Error::add_error( |
740 | - __( 'A valid transaction could not be initiated for processing your registrations.', 'event_espresso' ), |
|
739 | + __('A valid transaction could not be initiated for processing your registrations.', 'event_espresso'), |
|
741 | 740 | __FILE__, |
742 | 741 | __FUNCTION__, |
743 | 742 | __LINE__ |
@@ -745,11 +744,11 @@ discard block |
||
745 | 744 | return false; |
746 | 745 | } |
747 | 746 | // get cached registrations |
748 | - $registrations = $this->checkout->transaction->registrations( $this->checkout->reg_cache_where_params ); |
|
747 | + $registrations = $this->checkout->transaction->registrations($this->checkout->reg_cache_where_params); |
|
749 | 748 | // verify we got the goods |
750 | - if ( empty( $registrations )) { |
|
749 | + if (empty($registrations)) { |
|
751 | 750 | EE_Error::add_error( |
752 | - __( 'Your form data could not be applied to any valid registrations.', 'event_espresso' ), |
|
751 | + __('Your form data could not be applied to any valid registrations.', 'event_espresso'), |
|
753 | 752 | __FILE__, |
754 | 753 | __FUNCTION__, |
755 | 754 | __LINE__ |
@@ -757,15 +756,15 @@ discard block |
||
757 | 756 | return false; |
758 | 757 | } |
759 | 758 | // extract attendee info from form data and save to model objects |
760 | - $registrations_processed = $this->_process_registrations( $registrations, $valid_data ); |
|
759 | + $registrations_processed = $this->_process_registrations($registrations, $valid_data); |
|
761 | 760 | // if first pass thru SPCO, |
762 | 761 | // then let's check processed registrations against the total number of tickets in the cart |
763 | - if ( $registrations_processed === false ) { |
|
762 | + if ($registrations_processed === false) { |
|
764 | 763 | // but return immediately if the previous step exited early due to errors |
765 | 764 | return false; |
766 | - } else if ( ! $this->checkout->revisit && $registrations_processed !== $this->checkout->total_ticket_count ) { |
|
765 | + } else if ( ! $this->checkout->revisit && $registrations_processed !== $this->checkout->total_ticket_count) { |
|
767 | 766 | // generate a correctly translated string for all possible singular/plural combinations |
768 | - if ( $this->checkout->total_ticket_count === 1 && $registrations_processed !== 1 ) { |
|
767 | + if ($this->checkout->total_ticket_count === 1 && $registrations_processed !== 1) { |
|
769 | 768 | $error_msg = sprintf( |
770 | 769 | __( |
771 | 770 | 'There was %1$d ticket in the Event Queue, but %2$ds registrations were processed', |
@@ -774,7 +773,7 @@ discard block |
||
774 | 773 | $this->checkout->total_ticket_count, |
775 | 774 | $registrations_processed |
776 | 775 | ); |
777 | - } else if ( $this->checkout->total_ticket_count !== 1 && $registrations_processed === 1 ) { |
|
776 | + } else if ($this->checkout->total_ticket_count !== 1 && $registrations_processed === 1) { |
|
778 | 777 | $error_msg = sprintf( |
779 | 778 | __( |
780 | 779 | 'There was a total of %1$d tickets in the Event Queue, but only %2$ds registration was processed', |
@@ -793,17 +792,17 @@ discard block |
||
793 | 792 | $registrations_processed |
794 | 793 | ); |
795 | 794 | } |
796 | - EE_Error::add_error( $error_msg, __FILE__, __FUNCTION__, __LINE__ ); |
|
795 | + EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
797 | 796 | return false; |
798 | 797 | } |
799 | 798 | // mark this reg step as completed |
800 | 799 | $this->set_completed(); |
801 | 800 | $this->_set_success_message( |
802 | - __( 'The Attendee Information Step has been successfully completed.', 'event_espresso' ) |
|
801 | + __('The Attendee Information Step has been successfully completed.', 'event_espresso') |
|
803 | 802 | ); |
804 | 803 | //do action in case a plugin wants to do something with the data submitted in step 1. |
805 | 804 | //passes EE_Single_Page_Checkout, and it's posted data |
806 | - do_action( 'AHEE__EE_Single_Page_Checkout__process_attendee_information__end', $this, $valid_data ); |
|
805 | + do_action('AHEE__EE_Single_Page_Checkout__process_attendee_information__end', $this, $valid_data); |
|
807 | 806 | return true; |
808 | 807 | } |
809 | 808 | |
@@ -817,9 +816,9 @@ discard block |
||
817 | 816 | * @return boolean | int |
818 | 817 | * @throws \EE_Error |
819 | 818 | */ |
820 | - private function _process_registrations( $registrations = array(), $valid_data = array() ) { |
|
819 | + private function _process_registrations($registrations = array(), $valid_data = array()) { |
|
821 | 820 | // load resources and set some defaults |
822 | - EE_Registry::instance()->load_model( 'Attendee' ); |
|
821 | + EE_Registry::instance()->load_model('Attendee'); |
|
823 | 822 | // holder for primary registrant attendee object |
824 | 823 | $this->checkout->primary_attendee_obj = NULL; |
825 | 824 | // array for tracking reg form data for the primary registrant |
@@ -836,9 +835,9 @@ discard block |
||
836 | 835 | // attendee counter |
837 | 836 | $att_nmbr = 0; |
838 | 837 | // grab the saved registrations from the transaction |
839 | - foreach ( $registrations as $registration ) { |
|
838 | + foreach ($registrations as $registration) { |
|
840 | 839 | // verify EE_Registration object |
841 | - if ( ! $registration instanceof EE_Registration ) { |
|
840 | + if ( ! $registration instanceof EE_Registration) { |
|
842 | 841 | EE_Error::add_error( |
843 | 842 | __( |
844 | 843 | 'An invalid Registration object was discovered when attempting to process your registration information.', |
@@ -853,12 +852,12 @@ discard block |
||
853 | 852 | /** @var string $reg_url_link */ |
854 | 853 | $reg_url_link = $registration->reg_url_link(); |
855 | 854 | // reg_url_link exists ? |
856 | - if ( ! empty( $reg_url_link ) ) { |
|
855 | + if ( ! empty($reg_url_link)) { |
|
857 | 856 | // should this registration be processed during this visit ? |
858 | - if ( $this->checkout->visit_allows_processing_of_this_registration( $registration ) ) { |
|
857 | + if ($this->checkout->visit_allows_processing_of_this_registration($registration)) { |
|
859 | 858 | // if NOT revisiting, then let's save the registration now, |
860 | 859 | // so that we have a REG_ID to use when generating other objects |
861 | - if ( ! $this->checkout->revisit ) { |
|
860 | + if ( ! $this->checkout->revisit) { |
|
862 | 861 | $registration->save(); |
863 | 862 | } |
864 | 863 | /** |
@@ -868,7 +867,7 @@ discard block |
||
868 | 867 | * @var bool if true is returned by the plugin then the |
869 | 868 | * registration processing is halted. |
870 | 869 | */ |
871 | - if ( apply_filters( |
|
870 | + if (apply_filters( |
|
872 | 871 | 'FHEE__EE_SPCO_Reg_Step_Attendee_Information___process_registrations__pre_registration_process', |
873 | 872 | false, |
874 | 873 | $att_nmbr, |
@@ -876,38 +875,38 @@ discard block |
||
876 | 875 | $registrations, |
877 | 876 | $valid_data, |
878 | 877 | $this |
879 | - ) ) { |
|
878 | + )) { |
|
880 | 879 | return false; |
881 | 880 | } |
882 | 881 | |
883 | 882 | // Houston, we have a registration! |
884 | 883 | $att_nmbr++; |
885 | - $this->_attendee_data[ $reg_url_link ] = array(); |
|
884 | + $this->_attendee_data[$reg_url_link] = array(); |
|
886 | 885 | // grab any existing related answer objects |
887 | 886 | $this->_registration_answers = $registration->answers(); |
888 | 887 | // unset( $valid_data[ $reg_url_link ]['additional_attendee_reg_info'] ); |
889 | - if ( isset( $valid_data[ $reg_url_link ] ) ) { |
|
888 | + if (isset($valid_data[$reg_url_link])) { |
|
890 | 889 | // do we need to copy basic info from primary attendee ? |
891 | - $copy_primary = isset( $valid_data[ $reg_url_link ]['additional_attendee_reg_info'] ) |
|
892 | - && absint( $valid_data[ $reg_url_link ]['additional_attendee_reg_info'] ) === 0 |
|
890 | + $copy_primary = isset($valid_data[$reg_url_link]['additional_attendee_reg_info']) |
|
891 | + && absint($valid_data[$reg_url_link]['additional_attendee_reg_info']) === 0 |
|
893 | 892 | ? true |
894 | 893 | : false; |
895 | 894 | // filter form input data for this registration |
896 | - $valid_data[ $reg_url_link ] = (array)apply_filters( |
|
895 | + $valid_data[$reg_url_link] = (array) apply_filters( |
|
897 | 896 | 'FHEE__EE_Single_Page_Checkout__process_attendee_information__valid_data_line_item', |
898 | - $valid_data[ $reg_url_link ] |
|
897 | + $valid_data[$reg_url_link] |
|
899 | 898 | ); |
900 | 899 | // EEH_Debug_Tools::printr( $valid_data[ $reg_url_link ], '$valid_data[ $reg_url_link ]', __FILE__, __LINE__ ); |
901 | - if ( isset( $valid_data['primary_attendee'] )) { |
|
902 | - $primary_registrant['line_item_id'] = ! empty( $valid_data['primary_attendee'] ) |
|
900 | + if (isset($valid_data['primary_attendee'])) { |
|
901 | + $primary_registrant['line_item_id'] = ! empty($valid_data['primary_attendee']) |
|
903 | 902 | ? $valid_data['primary_attendee'] |
904 | 903 | : false; |
905 | - unset( $valid_data['primary_attendee'] ); |
|
904 | + unset($valid_data['primary_attendee']); |
|
906 | 905 | } |
907 | 906 | // now loop through our array of valid post data && process attendee reg forms |
908 | - foreach ( $valid_data[ $reg_url_link ] as $form_section => $form_inputs ) { |
|
909 | - if ( ! in_array( $form_section, $non_input_form_sections )) { |
|
910 | - foreach ( $form_inputs as $form_input => $input_value ) { |
|
907 | + foreach ($valid_data[$reg_url_link] as $form_section => $form_inputs) { |
|
908 | + if ( ! in_array($form_section, $non_input_form_sections)) { |
|
909 | + foreach ($form_inputs as $form_input => $input_value) { |
|
911 | 910 | // \EEH_Debug_Tools::printr( $input_value, $form_input, __FILE__, __LINE__ ); |
912 | 911 | // check for critical inputs |
913 | 912 | if ( |
@@ -921,16 +920,16 @@ discard block |
||
921 | 920 | // store a bit of data about the primary attendee |
922 | 921 | if ( |
923 | 922 | $att_nmbr === 1 |
924 | - && ! empty( $input_value ) |
|
923 | + && ! empty($input_value) |
|
925 | 924 | && $reg_url_link === $primary_registrant['line_item_id'] |
926 | 925 | ) { |
927 | - $primary_registrant[ $form_input ] = $input_value; |
|
926 | + $primary_registrant[$form_input] = $input_value; |
|
928 | 927 | } else if ( |
929 | 928 | $copy_primary |
930 | 929 | && $input_value === null |
931 | - && isset( $primary_registrant[ $form_input ] ) |
|
930 | + && isset($primary_registrant[$form_input]) |
|
932 | 931 | ) { |
933 | - $input_value = $primary_registrant[ $form_input ]; |
|
932 | + $input_value = $primary_registrant[$form_input]; |
|
934 | 933 | } |
935 | 934 | // now attempt to save the input data |
936 | 935 | if ( |
@@ -972,55 +971,55 @@ discard block |
||
972 | 971 | // have we met before? |
973 | 972 | $attendee = $this->_find_existing_attendee( |
974 | 973 | $registration, |
975 | - $this->_attendee_data[ $reg_url_link ] |
|
974 | + $this->_attendee_data[$reg_url_link] |
|
976 | 975 | ); |
977 | 976 | // did we find an already existing record for this attendee ? |
978 | - if ( $attendee instanceof EE_Attendee ) { |
|
977 | + if ($attendee instanceof EE_Attendee) { |
|
979 | 978 | $attendee = $this->_update_existing_attendee_data( |
980 | 979 | $attendee, |
981 | - $this->_attendee_data[ $reg_url_link ] |
|
980 | + $this->_attendee_data[$reg_url_link] |
|
982 | 981 | ); |
983 | 982 | } else { |
984 | 983 | // ensure critical details are set for additional attendees |
985 | - $this->_attendee_data[ $reg_url_link ] = $att_nmbr > 1 |
|
984 | + $this->_attendee_data[$reg_url_link] = $att_nmbr > 1 |
|
986 | 985 | ? $this->_copy_critical_attendee_details_from_primary_registrant( |
987 | - $this->_attendee_data[ $reg_url_link ] |
|
986 | + $this->_attendee_data[$reg_url_link] |
|
988 | 987 | ) |
989 | - : $this->_attendee_data[ $reg_url_link ]; |
|
988 | + : $this->_attendee_data[$reg_url_link]; |
|
990 | 989 | $attendee = $this->_create_new_attendee( |
991 | 990 | $registration, |
992 | - $this->_attendee_data[ $reg_url_link ] |
|
991 | + $this->_attendee_data[$reg_url_link] |
|
993 | 992 | ); |
994 | 993 | } |
995 | 994 | // who's #1 ? |
996 | - if ( $att_nmbr === 1 ) { |
|
995 | + if ($att_nmbr === 1) { |
|
997 | 996 | $this->checkout->primary_attendee_obj = $attendee; |
998 | 997 | } |
999 | 998 | } |
1000 | 999 | // EEH_Debug_Tools::printr( $attendee, '$attendee', __FILE__, __LINE__ ); |
1001 | 1000 | // add relation to registration, set attendee ID, and cache attendee |
1002 | - $this->_associate_attendee_with_registration( $registration, $attendee ); |
|
1001 | + $this->_associate_attendee_with_registration($registration, $attendee); |
|
1003 | 1002 | // \EEH_Debug_Tools::printr( $registration, '$registration', __FILE__, __LINE__ ); |
1004 | - if ( ! $registration->attendee() instanceof EE_Attendee ) { |
|
1005 | - EE_Error::add_error( sprintf( __( 'Registration %s has an invalid or missing Attendee object.', 'event_espresso' ), $reg_url_link ), __FILE__, __FUNCTION__, __LINE__ ); |
|
1003 | + if ( ! $registration->attendee() instanceof EE_Attendee) { |
|
1004 | + EE_Error::add_error(sprintf(__('Registration %s has an invalid or missing Attendee object.', 'event_espresso'), $reg_url_link), __FILE__, __FUNCTION__, __LINE__); |
|
1006 | 1005 | return false; |
1007 | 1006 | } |
1008 | 1007 | /** @type EE_Registration_Processor $registration_processor */ |
1009 | - $registration_processor = EE_Registry::instance()->load_class( 'Registration_Processor' ); |
|
1008 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
1010 | 1009 | // at this point, we should have enough details about the registrant to consider the registration NOT incomplete |
1011 | - $registration_processor->toggle_incomplete_registration_status_to_default( $registration, false ); |
|
1010 | + $registration_processor->toggle_incomplete_registration_status_to_default($registration, false); |
|
1012 | 1011 | // we can also consider the TXN to not have been failed, so temporarily upgrade it's status to abandoned |
1013 | 1012 | $this->checkout->transaction->toggle_failed_transaction_status(); |
1014 | 1013 | // if we've gotten this far, then let's save what we have |
1015 | 1014 | $registration->save(); |
1016 | 1015 | // add relation between TXN and registration |
1017 | - $this->_associate_registration_with_transaction( $registration ); |
|
1016 | + $this->_associate_registration_with_transaction($registration); |
|
1018 | 1017 | } // end of if ( ! $this->checkout->revisit || $this->checkout->primary_revisit || ( $this->checkout->revisit && $this->checkout->reg_url_link == $reg_url_link )) { |
1019 | 1018 | |
1020 | - } else { |
|
1021 | - EE_Error::add_error( __( 'An invalid or missing line item ID was encountered while attempting to process the registration form.', 'event_espresso' ), __FILE__, __FUNCTION__, __LINE__ ); |
|
1019 | + } else { |
|
1020 | + EE_Error::add_error(__('An invalid or missing line item ID was encountered while attempting to process the registration form.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
1022 | 1021 | // remove malformed data |
1023 | - unset( $valid_data[ $reg_url_link ] ); |
|
1022 | + unset($valid_data[$reg_url_link]); |
|
1024 | 1023 | return false; |
1025 | 1024 | } |
1026 | 1025 | |
@@ -1049,26 +1048,26 @@ discard block |
||
1049 | 1048 | // \EEH_Debug_Tools::printr( $input_value, '$input_value', __FILE__, __LINE__ ); |
1050 | 1049 | // allow for plugins to hook in and do their own processing of the form input. |
1051 | 1050 | // For plugins to bypass normal processing here, they just need to return a boolean value. |
1052 | - if ( apply_filters( |
|
1051 | + if (apply_filters( |
|
1053 | 1052 | 'FHEE__EE_SPCO_Reg_Step_Attendee_Information___save_registration_form_input', |
1054 | 1053 | false, |
1055 | 1054 | $registration, |
1056 | 1055 | $form_input, |
1057 | 1056 | $input_value, |
1058 | 1057 | $this |
1059 | - ) ) { |
|
1058 | + )) { |
|
1060 | 1059 | return true; |
1061 | 1060 | } |
1062 | 1061 | // $answer_cache_id is the key used to find the EE_Answer we want |
1063 | 1062 | $answer_cache_id = $this->checkout->reg_url_link |
1064 | 1063 | ? $form_input |
1065 | - : $form_input . '-' . $registration->reg_url_link(); |
|
1066 | - $answer_is_obj = isset( $this->_registration_answers[ $answer_cache_id ] ) |
|
1067 | - && $this->_registration_answers[ $answer_cache_id ] instanceof EE_Answer |
|
1064 | + : $form_input.'-'.$registration->reg_url_link(); |
|
1065 | + $answer_is_obj = isset($this->_registration_answers[$answer_cache_id]) |
|
1066 | + && $this->_registration_answers[$answer_cache_id] instanceof EE_Answer |
|
1068 | 1067 | ? true |
1069 | 1068 | : false; |
1070 | 1069 | //rename form_inputs if they are EE_Attendee properties |
1071 | - switch( (string)$form_input ) { |
|
1070 | + switch ((string) $form_input) { |
|
1072 | 1071 | |
1073 | 1072 | case 'state' : |
1074 | 1073 | case 'STA_ID' : |
@@ -1083,32 +1082,32 @@ discard block |
||
1083 | 1082 | break; |
1084 | 1083 | |
1085 | 1084 | default : |
1086 | - $ATT_input = 'ATT_' . $form_input; |
|
1085 | + $ATT_input = 'ATT_'.$form_input; |
|
1087 | 1086 | //EEH_Debug_Tools::printr( $ATT_input, '$ATT_input', __FILE__, __LINE__ ); |
1088 | - $attendee_property = EEM_Attendee::instance()->has_field( $ATT_input ) ? true : false; |
|
1089 | - $form_input = $attendee_property ? 'ATT_' . $form_input : $form_input; |
|
1087 | + $attendee_property = EEM_Attendee::instance()->has_field($ATT_input) ? true : false; |
|
1088 | + $form_input = $attendee_property ? 'ATT_'.$form_input : $form_input; |
|
1090 | 1089 | } |
1091 | 1090 | // EEH_Debug_Tools::printr( $answer_cache_id, '$answer_cache_id', __FILE__, __LINE__ ); |
1092 | 1091 | // EEH_Debug_Tools::printr( $attendee_property, '$attendee_property', __FILE__, __LINE__ ); |
1093 | 1092 | // EEH_Debug_Tools::printr( $answer_is_obj, '$answer_is_obj', __FILE__, __LINE__ ); |
1094 | 1093 | // if this form input has a corresponding attendee property |
1095 | - if ( $attendee_property ) { |
|
1096 | - $this->_attendee_data[ $registration->reg_url_link() ][ $form_input ] = $input_value; |
|
1097 | - if ( $answer_is_obj ) { |
|
1094 | + if ($attendee_property) { |
|
1095 | + $this->_attendee_data[$registration->reg_url_link()][$form_input] = $input_value; |
|
1096 | + if ($answer_is_obj) { |
|
1098 | 1097 | // and delete the corresponding answer since we won't be storing this data in that object |
1099 | - $registration->_remove_relation_to( $this->_registration_answers[ $answer_cache_id ], 'Answer' ); |
|
1100 | - $this->_registration_answers[ $answer_cache_id ]->delete_permanently(); |
|
1098 | + $registration->_remove_relation_to($this->_registration_answers[$answer_cache_id], 'Answer'); |
|
1099 | + $this->_registration_answers[$answer_cache_id]->delete_permanently(); |
|
1101 | 1100 | } |
1102 | 1101 | return true; |
1103 | - } elseif ( $answer_is_obj ) { |
|
1102 | + } elseif ($answer_is_obj) { |
|
1104 | 1103 | // save this data to the answer object |
1105 | - $this->_registration_answers[ $answer_cache_id ]->set_value( $input_value ); |
|
1106 | - $result = $this->_registration_answers[ $answer_cache_id ]->save(); |
|
1104 | + $this->_registration_answers[$answer_cache_id]->set_value($input_value); |
|
1105 | + $result = $this->_registration_answers[$answer_cache_id]->save(); |
|
1107 | 1106 | return $result !== false ? true : false; |
1108 | 1107 | } else { |
1109 | - foreach ( $this->_registration_answers as $answer ) { |
|
1110 | - if ( $answer instanceof EE_Answer && $answer->question_ID() === $answer_cache_id ) { |
|
1111 | - $answer->set_value( $input_value ); |
|
1108 | + foreach ($this->_registration_answers as $answer) { |
|
1109 | + if ($answer instanceof EE_Answer && $answer->question_ID() === $answer_cache_id) { |
|
1110 | + $answer->set_value($input_value); |
|
1112 | 1111 | $result = $answer->save(); |
1113 | 1112 | return $result !== false ? true : false; |
1114 | 1113 | } |
@@ -1130,15 +1129,15 @@ discard block |
||
1130 | 1129 | $form_input = '', |
1131 | 1130 | $input_value = '' |
1132 | 1131 | ) { |
1133 | - if ( empty( $input_value ) ) { |
|
1132 | + if (empty($input_value)) { |
|
1134 | 1133 | // if the form input isn't marked as being required, then just return |
1135 | - if ( ! isset( $this->_required_questions[ $form_input ] ) || ! $this->_required_questions[ $form_input ] ) { |
|
1134 | + if ( ! isset($this->_required_questions[$form_input]) || ! $this->_required_questions[$form_input]) { |
|
1136 | 1135 | return true; |
1137 | 1136 | } |
1138 | - switch ( $form_input ) { |
|
1137 | + switch ($form_input) { |
|
1139 | 1138 | case 'fname' : |
1140 | 1139 | EE_Error::add_error( |
1141 | - __( 'First Name is a required value.', 'event_espresso' ), |
|
1140 | + __('First Name is a required value.', 'event_espresso'), |
|
1142 | 1141 | __FILE__, |
1143 | 1142 | __FUNCTION__, |
1144 | 1143 | __LINE__ |
@@ -1147,7 +1146,7 @@ discard block |
||
1147 | 1146 | break; |
1148 | 1147 | case 'lname' : |
1149 | 1148 | EE_Error::add_error( |
1150 | - __( 'Last Name is a required value.', 'event_espresso' ), |
|
1149 | + __('Last Name is a required value.', 'event_espresso'), |
|
1151 | 1150 | __FILE__, |
1152 | 1151 | __FUNCTION__, |
1153 | 1152 | __LINE__ |
@@ -1156,7 +1155,7 @@ discard block |
||
1156 | 1155 | break; |
1157 | 1156 | case 'email' : |
1158 | 1157 | EE_Error::add_error( |
1159 | - __( 'Please enter a valid email address.', 'event_espresso' ), |
|
1158 | + __('Please enter a valid email address.', 'event_espresso'), |
|
1160 | 1159 | __FILE__, |
1161 | 1160 | __FUNCTION__, |
1162 | 1161 | __LINE__ |
@@ -1178,30 +1177,30 @@ discard block |
||
1178 | 1177 | * @return boolean|EE_Attendee |
1179 | 1178 | * @throws \EE_Error |
1180 | 1179 | */ |
1181 | - private function _find_existing_attendee( EE_Registration $registration, $attendee_data = array() ) { |
|
1180 | + private function _find_existing_attendee(EE_Registration $registration, $attendee_data = array()) { |
|
1182 | 1181 | $existing_attendee = null; |
1183 | 1182 | // if none of the critical properties are set in the incoming attendee data... |
1184 | 1183 | // then attempt to copy them from the primary attendee |
1185 | 1184 | if ( |
1186 | 1185 | $this->checkout->primary_attendee_obj instanceof EE_Attendee |
1187 | - && ! isset( $attendee_data['ATT_fname'], $attendee_data['ATT_email'] ) |
|
1186 | + && ! isset($attendee_data['ATT_fname'], $attendee_data['ATT_email']) |
|
1188 | 1187 | ) { |
1189 | 1188 | return $this->checkout->primary_attendee_obj; |
1190 | 1189 | } |
1191 | 1190 | // does this attendee already exist in the db ? |
1192 | 1191 | // we're searching using a combination of first name, last name, AND email address |
1193 | - $ATT_fname = isset( $attendee_data['ATT_fname'] ) && ! empty( $attendee_data['ATT_fname'] ) |
|
1192 | + $ATT_fname = isset($attendee_data['ATT_fname']) && ! empty($attendee_data['ATT_fname']) |
|
1194 | 1193 | ? $attendee_data['ATT_fname'] |
1195 | 1194 | : ''; |
1196 | - $ATT_lname = isset( $attendee_data['ATT_lname'] ) && ! empty( $attendee_data['ATT_lname'] ) |
|
1195 | + $ATT_lname = isset($attendee_data['ATT_lname']) && ! empty($attendee_data['ATT_lname']) |
|
1197 | 1196 | ? $attendee_data['ATT_lname'] |
1198 | 1197 | : ''; |
1199 | - $ATT_email = isset( $attendee_data['ATT_email'] ) && ! empty( $attendee_data['ATT_email'] ) |
|
1198 | + $ATT_email = isset($attendee_data['ATT_email']) && ! empty($attendee_data['ATT_email']) |
|
1200 | 1199 | ? $attendee_data['ATT_email'] |
1201 | 1200 | : ''; |
1202 | 1201 | // but only if those have values |
1203 | - if ( $ATT_fname && $ATT_lname && $ATT_email ) { |
|
1204 | - $existing_attendee = EEM_Attendee::instance()->find_existing_attendee( array( |
|
1202 | + if ($ATT_fname && $ATT_lname && $ATT_email) { |
|
1203 | + $existing_attendee = EEM_Attendee::instance()->find_existing_attendee(array( |
|
1205 | 1204 | 'ATT_fname' => $ATT_fname, |
1206 | 1205 | 'ATT_lname' => $ATT_lname, |
1207 | 1206 | 'ATT_email' => $ATT_email |
@@ -1225,13 +1224,13 @@ discard block |
||
1225 | 1224 | * @return \EE_Attendee |
1226 | 1225 | * @throws \EE_Error |
1227 | 1226 | */ |
1228 | - private function _update_existing_attendee_data( EE_Attendee $existing_attendee, $attendee_data = array() ) { |
|
1227 | + private function _update_existing_attendee_data(EE_Attendee $existing_attendee, $attendee_data = array()) { |
|
1229 | 1228 | // first remove fname, lname, and email from attendee data |
1230 | - $dont_set = array( 'ATT_fname', 'ATT_lname', 'ATT_email' ); |
|
1229 | + $dont_set = array('ATT_fname', 'ATT_lname', 'ATT_email'); |
|
1231 | 1230 | // now loop thru what's left and add to attendee CPT |
1232 | - foreach ( $attendee_data as $property_name => $property_value ) { |
|
1233 | - if ( ! in_array( $property_name, $dont_set ) && EEM_Attendee::instance()->has_field( $property_name )) { |
|
1234 | - $existing_attendee->set( $property_name, $property_value ); |
|
1231 | + foreach ($attendee_data as $property_name => $property_value) { |
|
1232 | + if ( ! in_array($property_name, $dont_set) && EEM_Attendee::instance()->has_field($property_name)) { |
|
1233 | + $existing_attendee->set($property_name, $property_value); |
|
1235 | 1234 | } |
1236 | 1235 | } |
1237 | 1236 | // better save that now |
@@ -1249,11 +1248,11 @@ discard block |
||
1249 | 1248 | * @return void |
1250 | 1249 | * @throws \EE_Error |
1251 | 1250 | */ |
1252 | - private function _associate_attendee_with_registration( EE_Registration $registration, EE_Attendee $attendee ) { |
|
1251 | + private function _associate_attendee_with_registration(EE_Registration $registration, EE_Attendee $attendee) { |
|
1253 | 1252 | // add relation to attendee |
1254 | - $registration->_add_relation_to( $attendee, 'Attendee' ); |
|
1255 | - $registration->set_attendee_id( $attendee->ID() ); |
|
1256 | - $registration->update_cache_after_object_save( 'Attendee', $attendee ); |
|
1253 | + $registration->_add_relation_to($attendee, 'Attendee'); |
|
1254 | + $registration->set_attendee_id($attendee->ID()); |
|
1255 | + $registration->update_cache_after_object_save('Attendee', $attendee); |
|
1257 | 1256 | } |
1258 | 1257 | |
1259 | 1258 | |
@@ -1265,10 +1264,10 @@ discard block |
||
1265 | 1264 | * @return void |
1266 | 1265 | * @throws \EE_Error |
1267 | 1266 | */ |
1268 | - private function _associate_registration_with_transaction( EE_Registration $registration ) { |
|
1267 | + private function _associate_registration_with_transaction(EE_Registration $registration) { |
|
1269 | 1268 | // add relation to attendee |
1270 | - $this->checkout->transaction->_add_relation_to( $registration, 'Registration' ); |
|
1271 | - $this->checkout->transaction->update_cache_after_object_save( 'Registration', $registration ); |
|
1269 | + $this->checkout->transaction->_add_relation_to($registration, 'Registration'); |
|
1270 | + $this->checkout->transaction->update_cache_after_object_save('Registration', $registration); |
|
1272 | 1271 | } |
1273 | 1272 | |
1274 | 1273 | |
@@ -1281,14 +1280,14 @@ discard block |
||
1281 | 1280 | * @return array |
1282 | 1281 | * @throws \EE_Error |
1283 | 1282 | */ |
1284 | - private function _copy_critical_attendee_details_from_primary_registrant( $attendee_data = array() ) { |
|
1283 | + private function _copy_critical_attendee_details_from_primary_registrant($attendee_data = array()) { |
|
1285 | 1284 | // bare minimum critical details include first name, last name, email address |
1286 | - $critical_attendee_details = array( 'ATT_fname', 'ATT_lname', 'ATT_email' ); |
|
1285 | + $critical_attendee_details = array('ATT_fname', 'ATT_lname', 'ATT_email'); |
|
1287 | 1286 | // add address info to critical details? |
1288 | - if ( apply_filters( |
|
1287 | + if (apply_filters( |
|
1289 | 1288 | 'FHEE__EE_SPCO_Reg_Step_Attendee_Information__merge_address_details_with_critical_attendee_details', |
1290 | 1289 | false |
1291 | - ) ) { |
|
1290 | + )) { |
|
1292 | 1291 | $address_details = array( |
1293 | 1292 | 'ATT_address', |
1294 | 1293 | 'ATT_address2', |
@@ -1298,13 +1297,13 @@ discard block |
||
1298 | 1297 | 'ATT_zip', |
1299 | 1298 | 'ATT_phone' |
1300 | 1299 | ); |
1301 | - $critical_attendee_details = array_merge( $critical_attendee_details, $address_details ); |
|
1300 | + $critical_attendee_details = array_merge($critical_attendee_details, $address_details); |
|
1302 | 1301 | } |
1303 | - foreach ( $critical_attendee_details as $critical_attendee_detail ) { |
|
1304 | - if ( ! isset( $attendee_data[ $critical_attendee_detail ] ) |
|
1305 | - || empty( $attendee_data[ $critical_attendee_detail ] ) |
|
1302 | + foreach ($critical_attendee_details as $critical_attendee_detail) { |
|
1303 | + if ( ! isset($attendee_data[$critical_attendee_detail]) |
|
1304 | + || empty($attendee_data[$critical_attendee_detail]) |
|
1306 | 1305 | ) { |
1307 | - $attendee_data[ $critical_attendee_detail ] = $this->checkout->primary_attendee_obj->get( |
|
1306 | + $attendee_data[$critical_attendee_detail] = $this->checkout->primary_attendee_obj->get( |
|
1308 | 1307 | $critical_attendee_detail |
1309 | 1308 | ); |
1310 | 1309 | } |
@@ -1322,11 +1321,11 @@ discard block |
||
1322 | 1321 | * @return \EE_Attendee |
1323 | 1322 | * @throws \EE_Error |
1324 | 1323 | */ |
1325 | - private function _create_new_attendee( EE_Registration $registration, $attendee_data = array() ) { |
|
1324 | + private function _create_new_attendee(EE_Registration $registration, $attendee_data = array()) { |
|
1326 | 1325 | // create new attendee object |
1327 | - $new_attendee = EE_Attendee::new_instance( $attendee_data ); |
|
1326 | + $new_attendee = EE_Attendee::new_instance($attendee_data); |
|
1328 | 1327 | // set author to event creator |
1329 | - $new_attendee->set( 'ATT_author', $registration->event()->wp_user() ); |
|
1328 | + $new_attendee->set('ATT_author', $registration->event()->wp_user()); |
|
1330 | 1329 | $new_attendee->save(); |
1331 | 1330 | return $new_attendee; |
1332 | 1331 | } |
@@ -1343,7 +1342,7 @@ discard block |
||
1343 | 1342 | */ |
1344 | 1343 | public function update_reg_step() { |
1345 | 1344 | // save everything |
1346 | - if ( $this->process_reg_step() ) { |
|
1345 | + if ($this->process_reg_step()) { |
|
1347 | 1346 | $this->checkout->redirect = true; |
1348 | 1347 | $this->checkout->redirect_url = add_query_arg( |
1349 | 1348 | array( |
@@ -1352,7 +1351,7 @@ discard block |
||
1352 | 1351 | ), |
1353 | 1352 | $this->checkout->thank_you_page_url |
1354 | 1353 | ); |
1355 | - $this->checkout->json_response->set_redirect_url( $this->checkout->redirect_url ); |
|
1354 | + $this->checkout->json_response->set_redirect_url($this->checkout->redirect_url); |
|
1356 | 1355 | return true; |
1357 | 1356 | } |
1358 | 1357 | return false; |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php if ( ! defined('ABSPATH')) { |
2 | - exit('No direct script access allowed'); |
|
2 | + exit('No direct script access allowed'); |
|
3 | 3 | } |
4 | 4 | /* |
5 | 5 | Plugin Name: Event Espresso |
@@ -40,239 +40,239 @@ discard block |
||
40 | 40 | * @since 4.0 |
41 | 41 | */ |
42 | 42 | if (function_exists('espresso_version')) { |
43 | - /** |
|
44 | - * espresso_duplicate_plugin_error |
|
45 | - * displays if more than one version of EE is activated at the same time |
|
46 | - */ |
|
47 | - function espresso_duplicate_plugin_error() |
|
48 | - { |
|
49 | - ?> |
|
43 | + /** |
|
44 | + * espresso_duplicate_plugin_error |
|
45 | + * displays if more than one version of EE is activated at the same time |
|
46 | + */ |
|
47 | + function espresso_duplicate_plugin_error() |
|
48 | + { |
|
49 | + ?> |
|
50 | 50 | <div class="error"> |
51 | 51 | <p> |
52 | 52 | <?php echo esc_html__( |
53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | - 'event_espresso' |
|
55 | - ); ?> |
|
53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | + 'event_espresso' |
|
55 | + ); ?> |
|
56 | 56 | </p> |
57 | 57 | </div> |
58 | 58 | <?php |
59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | - } |
|
59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | + } |
|
61 | 61 | |
62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
63 | 63 | } else { |
64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.3.9'); |
|
65 | - if ( ! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | - /** |
|
67 | - * espresso_minimum_php_version_error |
|
68 | - * |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - function espresso_minimum_php_version_error() |
|
72 | - { |
|
73 | - ?> |
|
64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.3.9'); |
|
65 | + if ( ! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | + /** |
|
67 | + * espresso_minimum_php_version_error |
|
68 | + * |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + function espresso_minimum_php_version_error() |
|
72 | + { |
|
73 | + ?> |
|
74 | 74 | <div class="error"> |
75 | 75 | <p> |
76 | 76 | <?php |
77 | - printf( |
|
78 | - esc_html__( |
|
79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | - 'event_espresso' |
|
81 | - ), |
|
82 | - EE_MIN_PHP_VER_REQUIRED, |
|
83 | - PHP_VERSION, |
|
84 | - '<br/>', |
|
85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | - ); |
|
87 | - ?> |
|
77 | + printf( |
|
78 | + esc_html__( |
|
79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | + 'event_espresso' |
|
81 | + ), |
|
82 | + EE_MIN_PHP_VER_REQUIRED, |
|
83 | + PHP_VERSION, |
|
84 | + '<br/>', |
|
85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | + ); |
|
87 | + ?> |
|
88 | 88 | </p> |
89 | 89 | </div> |
90 | 90 | <?php |
91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | - } |
|
91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | + } |
|
93 | 93 | |
94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | - } else { |
|
96 | - /** |
|
97 | - * espresso_version |
|
98 | - * Returns the plugin version |
|
99 | - * |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - function espresso_version() |
|
103 | - { |
|
104 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.25.rc.002'); |
|
105 | - } |
|
94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | + } else { |
|
96 | + /** |
|
97 | + * espresso_version |
|
98 | + * Returns the plugin version |
|
99 | + * |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + function espresso_version() |
|
103 | + { |
|
104 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.25.rc.002'); |
|
105 | + } |
|
106 | 106 | |
107 | - // define versions |
|
108 | - define('EVENT_ESPRESSO_VERSION', espresso_version()); |
|
109 | - define('EE_MIN_WP_VER_REQUIRED', '4.1'); |
|
110 | - define('EE_MIN_WP_VER_RECOMMENDED', '4.4.2'); |
|
111 | - define('EE_MIN_PHP_VER_RECOMMENDED', '5.4.44'); |
|
112 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
113 | - //used to be DIRECTORY_SEPARATOR, but that caused issues on windows |
|
114 | - if ( ! defined('DS')) { |
|
115 | - define('DS', '/'); |
|
116 | - } |
|
117 | - if ( ! defined('PS')) { |
|
118 | - define('PS', PATH_SEPARATOR); |
|
119 | - } |
|
120 | - if ( ! defined('SP')) { |
|
121 | - define('SP', ' '); |
|
122 | - } |
|
123 | - if ( ! defined('EENL')) { |
|
124 | - define('EENL', "\n"); |
|
125 | - } |
|
126 | - define('EE_SUPPORT_EMAIL', '[email protected]'); |
|
127 | - // define the plugin directory and URL |
|
128 | - define('EE_PLUGIN_BASENAME', plugin_basename(EVENT_ESPRESSO_MAIN_FILE)); |
|
129 | - define('EE_PLUGIN_DIR_PATH', plugin_dir_path(EVENT_ESPRESSO_MAIN_FILE)); |
|
130 | - define('EE_PLUGIN_DIR_URL', plugin_dir_url(EVENT_ESPRESSO_MAIN_FILE)); |
|
131 | - // main root folder paths |
|
132 | - define('EE_ADMIN_PAGES', EE_PLUGIN_DIR_PATH . 'admin_pages' . DS); |
|
133 | - define('EE_CORE', EE_PLUGIN_DIR_PATH . 'core' . DS); |
|
134 | - define('EE_MODULES', EE_PLUGIN_DIR_PATH . 'modules' . DS); |
|
135 | - define('EE_PUBLIC', EE_PLUGIN_DIR_PATH . 'public' . DS); |
|
136 | - define('EE_SHORTCODES', EE_PLUGIN_DIR_PATH . 'shortcodes' . DS); |
|
137 | - define('EE_WIDGETS', EE_PLUGIN_DIR_PATH . 'widgets' . DS); |
|
138 | - define('EE_PAYMENT_METHODS', EE_PLUGIN_DIR_PATH . 'payment_methods' . DS); |
|
139 | - define('EE_CAFF_PATH', EE_PLUGIN_DIR_PATH . 'caffeinated' . DS); |
|
140 | - // core system paths |
|
141 | - define('EE_ADMIN', EE_CORE . 'admin' . DS); |
|
142 | - define('EE_CPTS', EE_CORE . 'CPTs' . DS); |
|
143 | - define('EE_CLASSES', EE_CORE . 'db_classes' . DS); |
|
144 | - define('EE_INTERFACES', EE_CORE . 'interfaces' . DS); |
|
145 | - define('EE_BUSINESS', EE_CORE . 'business' . DS); |
|
146 | - define('EE_MODELS', EE_CORE . 'db_models' . DS); |
|
147 | - define('EE_HELPERS', EE_CORE . 'helpers' . DS); |
|
148 | - define('EE_LIBRARIES', EE_CORE . 'libraries' . DS); |
|
149 | - define('EE_TEMPLATES', EE_CORE . 'templates' . DS); |
|
150 | - define('EE_THIRD_PARTY', EE_CORE . 'third_party_libs' . DS); |
|
151 | - define('EE_GLOBAL_ASSETS', EE_TEMPLATES . 'global_assets' . DS); |
|
152 | - define('EE_FORM_SECTIONS', EE_LIBRARIES . 'form_sections' . DS); |
|
153 | - // gateways |
|
154 | - define('EE_GATEWAYS', EE_MODULES . 'gateways' . DS); |
|
155 | - define('EE_GATEWAYS_URL', EE_PLUGIN_DIR_URL . 'modules' . DS . 'gateways' . DS); |
|
156 | - // asset URL paths |
|
157 | - define('EE_TEMPLATES_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'templates' . DS); |
|
158 | - define('EE_GLOBAL_ASSETS_URL', EE_TEMPLATES_URL . 'global_assets' . DS); |
|
159 | - define('EE_IMAGES_URL', EE_GLOBAL_ASSETS_URL . 'images' . DS); |
|
160 | - define('EE_THIRD_PARTY_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'third_party_libs' . DS); |
|
161 | - define('EE_HELPERS_ASSETS', EE_PLUGIN_DIR_URL . 'core/helpers/assets/'); |
|
162 | - define('EE_LIBRARIES_URL', EE_PLUGIN_DIR_URL . 'core/libraries/'); |
|
163 | - // define upload paths |
|
164 | - $uploads = wp_upload_dir(); |
|
165 | - // define the uploads directory and URL |
|
166 | - define('EVENT_ESPRESSO_UPLOAD_DIR', $uploads['basedir'] . DS . 'espresso' . DS); |
|
167 | - define('EVENT_ESPRESSO_UPLOAD_URL', $uploads['baseurl'] . DS . 'espresso' . DS); |
|
168 | - // define the templates directory and URL |
|
169 | - define('EVENT_ESPRESSO_TEMPLATE_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'templates' . DS); |
|
170 | - define('EVENT_ESPRESSO_TEMPLATE_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'templates' . DS); |
|
171 | - // define the gateway directory and URL |
|
172 | - define('EVENT_ESPRESSO_GATEWAY_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
173 | - define('EVENT_ESPRESSO_GATEWAY_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
174 | - // languages folder/path |
|
175 | - define('EE_LANGUAGES_SAFE_LOC', '..' . DS . 'uploads' . DS . 'espresso' . DS . 'languages' . DS); |
|
176 | - define('EE_LANGUAGES_SAFE_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'languages' . DS); |
|
177 | - //check for dompdf fonts in uploads |
|
178 | - if (file_exists(EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS)) { |
|
179 | - define('DOMPDF_FONT_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS); |
|
180 | - } |
|
181 | - //ajax constants |
|
182 | - define( |
|
183 | - 'EE_FRONT_AJAX', |
|
184 | - isset($_REQUEST['ee_front_ajax']) || isset($_REQUEST['data']['ee_front_ajax']) ? true : false |
|
185 | - ); |
|
186 | - define( |
|
187 | - 'EE_ADMIN_AJAX', |
|
188 | - isset($_REQUEST['ee_admin_ajax']) || isset($_REQUEST['data']['ee_admin_ajax']) ? true : false |
|
189 | - ); |
|
190 | - //just a handy constant occasionally needed for finding values representing infinity in the DB |
|
191 | - //you're better to use this than its straight value (currently -1) in case you ever |
|
192 | - //want to change its default value! or find when -1 means infinity |
|
193 | - define('EE_INF_IN_DB', -1); |
|
194 | - define('EE_INF', INF > (float)PHP_INT_MAX ? INF : PHP_INT_MAX); |
|
195 | - define('EE_DEBUG', false); |
|
196 | - /** |
|
197 | - * espresso_plugin_activation |
|
198 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
199 | - */ |
|
200 | - function espresso_plugin_activation() |
|
201 | - { |
|
202 | - update_option('ee_espresso_activation', true); |
|
203 | - } |
|
107 | + // define versions |
|
108 | + define('EVENT_ESPRESSO_VERSION', espresso_version()); |
|
109 | + define('EE_MIN_WP_VER_REQUIRED', '4.1'); |
|
110 | + define('EE_MIN_WP_VER_RECOMMENDED', '4.4.2'); |
|
111 | + define('EE_MIN_PHP_VER_RECOMMENDED', '5.4.44'); |
|
112 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
113 | + //used to be DIRECTORY_SEPARATOR, but that caused issues on windows |
|
114 | + if ( ! defined('DS')) { |
|
115 | + define('DS', '/'); |
|
116 | + } |
|
117 | + if ( ! defined('PS')) { |
|
118 | + define('PS', PATH_SEPARATOR); |
|
119 | + } |
|
120 | + if ( ! defined('SP')) { |
|
121 | + define('SP', ' '); |
|
122 | + } |
|
123 | + if ( ! defined('EENL')) { |
|
124 | + define('EENL', "\n"); |
|
125 | + } |
|
126 | + define('EE_SUPPORT_EMAIL', '[email protected]'); |
|
127 | + // define the plugin directory and URL |
|
128 | + define('EE_PLUGIN_BASENAME', plugin_basename(EVENT_ESPRESSO_MAIN_FILE)); |
|
129 | + define('EE_PLUGIN_DIR_PATH', plugin_dir_path(EVENT_ESPRESSO_MAIN_FILE)); |
|
130 | + define('EE_PLUGIN_DIR_URL', plugin_dir_url(EVENT_ESPRESSO_MAIN_FILE)); |
|
131 | + // main root folder paths |
|
132 | + define('EE_ADMIN_PAGES', EE_PLUGIN_DIR_PATH . 'admin_pages' . DS); |
|
133 | + define('EE_CORE', EE_PLUGIN_DIR_PATH . 'core' . DS); |
|
134 | + define('EE_MODULES', EE_PLUGIN_DIR_PATH . 'modules' . DS); |
|
135 | + define('EE_PUBLIC', EE_PLUGIN_DIR_PATH . 'public' . DS); |
|
136 | + define('EE_SHORTCODES', EE_PLUGIN_DIR_PATH . 'shortcodes' . DS); |
|
137 | + define('EE_WIDGETS', EE_PLUGIN_DIR_PATH . 'widgets' . DS); |
|
138 | + define('EE_PAYMENT_METHODS', EE_PLUGIN_DIR_PATH . 'payment_methods' . DS); |
|
139 | + define('EE_CAFF_PATH', EE_PLUGIN_DIR_PATH . 'caffeinated' . DS); |
|
140 | + // core system paths |
|
141 | + define('EE_ADMIN', EE_CORE . 'admin' . DS); |
|
142 | + define('EE_CPTS', EE_CORE . 'CPTs' . DS); |
|
143 | + define('EE_CLASSES', EE_CORE . 'db_classes' . DS); |
|
144 | + define('EE_INTERFACES', EE_CORE . 'interfaces' . DS); |
|
145 | + define('EE_BUSINESS', EE_CORE . 'business' . DS); |
|
146 | + define('EE_MODELS', EE_CORE . 'db_models' . DS); |
|
147 | + define('EE_HELPERS', EE_CORE . 'helpers' . DS); |
|
148 | + define('EE_LIBRARIES', EE_CORE . 'libraries' . DS); |
|
149 | + define('EE_TEMPLATES', EE_CORE . 'templates' . DS); |
|
150 | + define('EE_THIRD_PARTY', EE_CORE . 'third_party_libs' . DS); |
|
151 | + define('EE_GLOBAL_ASSETS', EE_TEMPLATES . 'global_assets' . DS); |
|
152 | + define('EE_FORM_SECTIONS', EE_LIBRARIES . 'form_sections' . DS); |
|
153 | + // gateways |
|
154 | + define('EE_GATEWAYS', EE_MODULES . 'gateways' . DS); |
|
155 | + define('EE_GATEWAYS_URL', EE_PLUGIN_DIR_URL . 'modules' . DS . 'gateways' . DS); |
|
156 | + // asset URL paths |
|
157 | + define('EE_TEMPLATES_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'templates' . DS); |
|
158 | + define('EE_GLOBAL_ASSETS_URL', EE_TEMPLATES_URL . 'global_assets' . DS); |
|
159 | + define('EE_IMAGES_URL', EE_GLOBAL_ASSETS_URL . 'images' . DS); |
|
160 | + define('EE_THIRD_PARTY_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'third_party_libs' . DS); |
|
161 | + define('EE_HELPERS_ASSETS', EE_PLUGIN_DIR_URL . 'core/helpers/assets/'); |
|
162 | + define('EE_LIBRARIES_URL', EE_PLUGIN_DIR_URL . 'core/libraries/'); |
|
163 | + // define upload paths |
|
164 | + $uploads = wp_upload_dir(); |
|
165 | + // define the uploads directory and URL |
|
166 | + define('EVENT_ESPRESSO_UPLOAD_DIR', $uploads['basedir'] . DS . 'espresso' . DS); |
|
167 | + define('EVENT_ESPRESSO_UPLOAD_URL', $uploads['baseurl'] . DS . 'espresso' . DS); |
|
168 | + // define the templates directory and URL |
|
169 | + define('EVENT_ESPRESSO_TEMPLATE_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'templates' . DS); |
|
170 | + define('EVENT_ESPRESSO_TEMPLATE_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'templates' . DS); |
|
171 | + // define the gateway directory and URL |
|
172 | + define('EVENT_ESPRESSO_GATEWAY_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
173 | + define('EVENT_ESPRESSO_GATEWAY_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
174 | + // languages folder/path |
|
175 | + define('EE_LANGUAGES_SAFE_LOC', '..' . DS . 'uploads' . DS . 'espresso' . DS . 'languages' . DS); |
|
176 | + define('EE_LANGUAGES_SAFE_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'languages' . DS); |
|
177 | + //check for dompdf fonts in uploads |
|
178 | + if (file_exists(EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS)) { |
|
179 | + define('DOMPDF_FONT_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS); |
|
180 | + } |
|
181 | + //ajax constants |
|
182 | + define( |
|
183 | + 'EE_FRONT_AJAX', |
|
184 | + isset($_REQUEST['ee_front_ajax']) || isset($_REQUEST['data']['ee_front_ajax']) ? true : false |
|
185 | + ); |
|
186 | + define( |
|
187 | + 'EE_ADMIN_AJAX', |
|
188 | + isset($_REQUEST['ee_admin_ajax']) || isset($_REQUEST['data']['ee_admin_ajax']) ? true : false |
|
189 | + ); |
|
190 | + //just a handy constant occasionally needed for finding values representing infinity in the DB |
|
191 | + //you're better to use this than its straight value (currently -1) in case you ever |
|
192 | + //want to change its default value! or find when -1 means infinity |
|
193 | + define('EE_INF_IN_DB', -1); |
|
194 | + define('EE_INF', INF > (float)PHP_INT_MAX ? INF : PHP_INT_MAX); |
|
195 | + define('EE_DEBUG', false); |
|
196 | + /** |
|
197 | + * espresso_plugin_activation |
|
198 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
199 | + */ |
|
200 | + function espresso_plugin_activation() |
|
201 | + { |
|
202 | + update_option('ee_espresso_activation', true); |
|
203 | + } |
|
204 | 204 | |
205 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
206 | - /** |
|
207 | - * espresso_load_error_handling |
|
208 | - * this function loads EE's class for handling exceptions and errors |
|
209 | - */ |
|
210 | - function espresso_load_error_handling() |
|
211 | - { |
|
212 | - // load debugging tools |
|
213 | - if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) { |
|
214 | - require_once(EE_HELPERS . 'EEH_Debug_Tools.helper.php'); |
|
215 | - EEH_Debug_Tools::instance(); |
|
216 | - } |
|
217 | - // load error handling |
|
218 | - if (is_readable(EE_CORE . 'EE_Error.core.php')) { |
|
219 | - require_once(EE_CORE . 'EE_Error.core.php'); |
|
220 | - } else { |
|
221 | - wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso')); |
|
222 | - } |
|
223 | - } |
|
205 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
206 | + /** |
|
207 | + * espresso_load_error_handling |
|
208 | + * this function loads EE's class for handling exceptions and errors |
|
209 | + */ |
|
210 | + function espresso_load_error_handling() |
|
211 | + { |
|
212 | + // load debugging tools |
|
213 | + if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) { |
|
214 | + require_once(EE_HELPERS . 'EEH_Debug_Tools.helper.php'); |
|
215 | + EEH_Debug_Tools::instance(); |
|
216 | + } |
|
217 | + // load error handling |
|
218 | + if (is_readable(EE_CORE . 'EE_Error.core.php')) { |
|
219 | + require_once(EE_CORE . 'EE_Error.core.php'); |
|
220 | + } else { |
|
221 | + wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso')); |
|
222 | + } |
|
223 | + } |
|
224 | 224 | |
225 | - /** |
|
226 | - * espresso_load_required |
|
227 | - * given a class name and path, this function will load that file or throw an exception |
|
228 | - * |
|
229 | - * @param string $classname |
|
230 | - * @param string $full_path_to_file |
|
231 | - * @throws EE_Error |
|
232 | - */ |
|
233 | - function espresso_load_required($classname, $full_path_to_file) |
|
234 | - { |
|
235 | - static $error_handling_loaded = false; |
|
236 | - if ( ! $error_handling_loaded) { |
|
237 | - espresso_load_error_handling(); |
|
238 | - $error_handling_loaded = true; |
|
239 | - } |
|
240 | - if (is_readable($full_path_to_file)) { |
|
241 | - require_once($full_path_to_file); |
|
242 | - } else { |
|
243 | - throw new EE_Error ( |
|
244 | - sprintf( |
|
245 | - esc_html__( |
|
246 | - 'The %s class file could not be located or is not readable due to file permissions.', |
|
247 | - 'event_espresso' |
|
248 | - ), |
|
249 | - $classname |
|
250 | - ) |
|
251 | - ); |
|
252 | - } |
|
253 | - } |
|
225 | + /** |
|
226 | + * espresso_load_required |
|
227 | + * given a class name and path, this function will load that file or throw an exception |
|
228 | + * |
|
229 | + * @param string $classname |
|
230 | + * @param string $full_path_to_file |
|
231 | + * @throws EE_Error |
|
232 | + */ |
|
233 | + function espresso_load_required($classname, $full_path_to_file) |
|
234 | + { |
|
235 | + static $error_handling_loaded = false; |
|
236 | + if ( ! $error_handling_loaded) { |
|
237 | + espresso_load_error_handling(); |
|
238 | + $error_handling_loaded = true; |
|
239 | + } |
|
240 | + if (is_readable($full_path_to_file)) { |
|
241 | + require_once($full_path_to_file); |
|
242 | + } else { |
|
243 | + throw new EE_Error ( |
|
244 | + sprintf( |
|
245 | + esc_html__( |
|
246 | + 'The %s class file could not be located or is not readable due to file permissions.', |
|
247 | + 'event_espresso' |
|
248 | + ), |
|
249 | + $classname |
|
250 | + ) |
|
251 | + ); |
|
252 | + } |
|
253 | + } |
|
254 | 254 | |
255 | - espresso_load_required('EEH_Base', EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php'); |
|
256 | - espresso_load_required('EEH_File', EE_CORE . 'helpers' . DS . 'EEH_File.helper.php'); |
|
257 | - espresso_load_required('EE_Bootstrap', EE_CORE . 'EE_Bootstrap.core.php'); |
|
258 | - new EE_Bootstrap(); |
|
259 | - } |
|
255 | + espresso_load_required('EEH_Base', EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php'); |
|
256 | + espresso_load_required('EEH_File', EE_CORE . 'helpers' . DS . 'EEH_File.helper.php'); |
|
257 | + espresso_load_required('EE_Bootstrap', EE_CORE . 'EE_Bootstrap.core.php'); |
|
258 | + new EE_Bootstrap(); |
|
259 | + } |
|
260 | 260 | } |
261 | 261 | if ( ! function_exists('espresso_deactivate_plugin')) { |
262 | - /** |
|
263 | - * deactivate_plugin |
|
264 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
265 | - * |
|
266 | - * @access public |
|
267 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
268 | - * @return void |
|
269 | - */ |
|
270 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
271 | - { |
|
272 | - if ( ! function_exists('deactivate_plugins')) { |
|
273 | - require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
|
274 | - } |
|
275 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
276 | - deactivate_plugins($plugin_basename); |
|
277 | - } |
|
262 | + /** |
|
263 | + * deactivate_plugin |
|
264 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
265 | + * |
|
266 | + * @access public |
|
267 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
268 | + * @return void |
|
269 | + */ |
|
270 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
271 | + { |
|
272 | + if ( ! function_exists('deactivate_plugins')) { |
|
273 | + require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
|
274 | + } |
|
275 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
276 | + deactivate_plugins($plugin_basename); |
|
277 | + } |
|
278 | 278 | } |