@@ -105,41 +105,41 @@ discard block |
||
105 | 105 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved). Note this just sends the timezone info to the date time model field objects. Default is NULL (and will be assumed using the set timezone in the 'timezone_string' wp option) |
106 | 106 | * @return \EEM_Line_Item |
107 | 107 | */ |
108 | - protected function __construct( $timezone ) { |
|
109 | - $this->singular_item = __('Line Item','event_espresso'); |
|
110 | - $this->plural_item = __('Line Items','event_espresso'); |
|
108 | + protected function __construct($timezone) { |
|
109 | + $this->singular_item = __('Line Item', 'event_espresso'); |
|
110 | + $this->plural_item = __('Line Items', 'event_espresso'); |
|
111 | 111 | |
112 | 112 | $this->_tables = array( |
113 | - 'Line_Item'=>new EE_Primary_Table('esp_line_item','LIN_ID') |
|
113 | + 'Line_Item'=>new EE_Primary_Table('esp_line_item', 'LIN_ID') |
|
114 | 114 | ); |
115 | - $line_items_can_be_for = apply_filters( 'FHEE__EEM_Line_Item__line_items_can_be_for', array('Ticket','Price', 'Event' ) ); |
|
115 | + $line_items_can_be_for = apply_filters('FHEE__EEM_Line_Item__line_items_can_be_for', array('Ticket', 'Price', 'Event')); |
|
116 | 116 | $this->_fields = array( |
117 | 117 | 'Line_Item' => array( |
118 | - 'LIN_ID' => new EE_Primary_Key_Int_Field( 'LIN_ID', __( "ID", "event_espresso" ) ), |
|
119 | - 'LIN_code' => new EE_Slug_Field( 'LIN_code', __( "Code for index into Cart", "event_espresso" ), TRUE ), |
|
120 | - 'TXN_ID' => new EE_Foreign_Key_Int_Field( 'TXN_ID', __( "Transaction ID", "event_espresso" ), TRUE, NULL, 'Transaction' ), |
|
121 | - 'LIN_name' => new EE_Full_HTML_Field( 'LIN_name', __( "Line Item Name", "event_espresso" ), FALSE, '' ), |
|
122 | - 'LIN_desc' => new EE_Full_HTML_Field( 'LIN_desc', __( "Line Item Description", "event_espresso" ), TRUE ), |
|
123 | - 'LIN_unit_price' => new EE_Money_Field( 'LIN_unit_price', __( "Unit Price", "event_espresso" ), FALSE, 0 ), |
|
124 | - 'LIN_percent' => new EE_Float_Field( 'LIN_percent', __( "Percent", "event_espresso" ), FALSE, 0 ), |
|
125 | - 'LIN_is_taxable' => new EE_Boolean_Field( 'LIN_is_taxable', __( "Taxable", "event_espresso" ), FALSE, FALSE ), |
|
126 | - 'LIN_order' => new EE_Integer_Field( 'LIN_order', __( "Order of Application towards total of parent", "event_espresso" ), FALSE, 1 ), |
|
127 | - 'LIN_total' => new EE_Money_Field( 'LIN_total', __( "Total (unit price x quantity)", "event_espresso" ), FALSE, 0 ), |
|
128 | - 'LIN_quantity' => new EE_Integer_Field( 'LIN_quantity', __( "Quantity", "event_espresso" ), TRUE, 1 ), |
|
129 | - 'LIN_parent' => new EE_Integer_Field( 'LIN_parent', __( "Parent ID (this item goes towards that Line Item's total)", "event_espresso" ), TRUE, NULL ), |
|
130 | - 'LIN_type' => new EE_Enum_Text_Field( 'LIN_type', __( "Type", "event_espresso" ), FALSE, 'line-item', array( |
|
118 | + 'LIN_ID' => new EE_Primary_Key_Int_Field('LIN_ID', __("ID", "event_espresso")), |
|
119 | + 'LIN_code' => new EE_Slug_Field('LIN_code', __("Code for index into Cart", "event_espresso"), TRUE), |
|
120 | + 'TXN_ID' => new EE_Foreign_Key_Int_Field('TXN_ID', __("Transaction ID", "event_espresso"), TRUE, NULL, 'Transaction'), |
|
121 | + 'LIN_name' => new EE_Full_HTML_Field('LIN_name', __("Line Item Name", "event_espresso"), FALSE, ''), |
|
122 | + 'LIN_desc' => new EE_Full_HTML_Field('LIN_desc', __("Line Item Description", "event_espresso"), TRUE), |
|
123 | + 'LIN_unit_price' => new EE_Money_Field('LIN_unit_price', __("Unit Price", "event_espresso"), FALSE, 0), |
|
124 | + 'LIN_percent' => new EE_Float_Field('LIN_percent', __("Percent", "event_espresso"), FALSE, 0), |
|
125 | + 'LIN_is_taxable' => new EE_Boolean_Field('LIN_is_taxable', __("Taxable", "event_espresso"), FALSE, FALSE), |
|
126 | + 'LIN_order' => new EE_Integer_Field('LIN_order', __("Order of Application towards total of parent", "event_espresso"), FALSE, 1), |
|
127 | + 'LIN_total' => new EE_Money_Field('LIN_total', __("Total (unit price x quantity)", "event_espresso"), FALSE, 0), |
|
128 | + 'LIN_quantity' => new EE_Integer_Field('LIN_quantity', __("Quantity", "event_espresso"), TRUE, 1), |
|
129 | + 'LIN_parent' => new EE_Integer_Field('LIN_parent', __("Parent ID (this item goes towards that Line Item's total)", "event_espresso"), TRUE, NULL), |
|
130 | + 'LIN_type' => new EE_Enum_Text_Field('LIN_type', __("Type", "event_espresso"), FALSE, 'line-item', array( |
|
131 | 131 | self::type_line_item => __("Line Item", "event_espresso"), |
132 | 132 | self::type_sub_line_item => __("Sub-Item", "event_espresso"), |
133 | 133 | self::type_sub_total => __("Subtotal", "event_espresso"), |
134 | 134 | self::type_tax_sub_total => __("Tax Subtotal", "event_espresso"), |
135 | 135 | self::type_tax => __("Tax", "event_espresso"), |
136 | 136 | self::type_total => __("Total", "event_espresso"), |
137 | - self::type_cancellation => __( 'Cancellation', 'event_espresso' ) |
|
137 | + self::type_cancellation => __('Cancellation', 'event_espresso') |
|
138 | 138 | ) |
139 | 139 | ), |
140 | - 'OBJ_ID' => new EE_Foreign_Key_Int_Field( 'OBJ_ID', __( 'ID of Item purchased.', 'event_espresso' ), TRUE, NULL, $line_items_can_be_for ), |
|
141 | - 'OBJ_type' =>new EE_Any_Foreign_Model_Name_Field( 'OBJ_type', __( "Model Name this Line Item is for", "event_espresso" ), TRUE, NULL, $line_items_can_be_for ), |
|
142 | - 'LIN_timestamp' => new EE_Datetime_Field('LIN_timestamp', __('When the line item was created','event_espresso'), false, EE_Datetime_Field::now, $timezone ), |
|
140 | + 'OBJ_ID' => new EE_Foreign_Key_Int_Field('OBJ_ID', __('ID of Item purchased.', 'event_espresso'), TRUE, NULL, $line_items_can_be_for), |
|
141 | + 'OBJ_type' =>new EE_Any_Foreign_Model_Name_Field('OBJ_type', __("Model Name this Line Item is for", "event_espresso"), TRUE, NULL, $line_items_can_be_for), |
|
142 | + 'LIN_timestamp' => new EE_Datetime_Field('LIN_timestamp', __('When the line item was created', 'event_espresso'), false, EE_Datetime_Field::now, $timezone), |
|
143 | 143 | ) |
144 | 144 | ); |
145 | 145 | $this->_model_relations = array( |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | ); |
151 | 151 | $this->_model_chain_to_wp_user = 'Transaction.Registration.Event'; |
152 | 152 | $this->_caps_slug = 'transactions'; |
153 | - parent::__construct( $timezone ); |
|
153 | + parent::__construct($timezone); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | |
@@ -161,9 +161,9 @@ discard block |
||
161 | 161 | * @param EE_Transaction|int $transaction |
162 | 162 | * @return EE_Line_Item[] |
163 | 163 | */ |
164 | - public function get_all_of_type_for_transaction( $line_item_type, $transaction ){ |
|
165 | - $transaction = EEM_Transaction::instance()->ensure_is_ID( $transaction ); |
|
166 | - return $this->get_all( array( array( |
|
164 | + public function get_all_of_type_for_transaction($line_item_type, $transaction) { |
|
165 | + $transaction = EEM_Transaction::instance()->ensure_is_ID($transaction); |
|
166 | + return $this->get_all(array(array( |
|
167 | 167 | 'LIN_type' => $line_item_type, |
168 | 168 | 'TXN_ID' => $transaction |
169 | 169 | ))); |
@@ -177,14 +177,14 @@ discard block |
||
177 | 177 | * @param EE_Transaction|int $transaction |
178 | 178 | * @return EE_Line_Item[] |
179 | 179 | */ |
180 | - public function get_all_non_ticket_line_items_for_transaction( $transaction ) { |
|
181 | - $transaction = EEM_Transaction::instance()->ensure_is_ID( $transaction ); |
|
182 | - return $this->get_all( array( array( |
|
180 | + public function get_all_non_ticket_line_items_for_transaction($transaction) { |
|
181 | + $transaction = EEM_Transaction::instance()->ensure_is_ID($transaction); |
|
182 | + return $this->get_all(array(array( |
|
183 | 183 | 'LIN_type' => self::type_line_item, |
184 | 184 | 'TXN_ID' => $transaction, |
185 | 185 | 'OR' => array( |
186 | - 'OBJ_type*notticket' => array( '!=', 'Ticket'), |
|
187 | - 'OBJ_type*null' => array( 'IS_NULL' )) |
|
186 | + 'OBJ_type*notticket' => array('!=', 'Ticket'), |
|
187 | + 'OBJ_type*null' => array('IS_NULL')) |
|
188 | 188 | ))); |
189 | 189 | } |
190 | 190 | |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | * because if there are spam bots afoot there will be LOTS of line items |
195 | 195 | * @return int count of how many deleted |
196 | 196 | */ |
197 | - public function delete_line_items_with_no_transaction(){ |
|
197 | + public function delete_line_items_with_no_transaction() { |
|
198 | 198 | /** @type WPDB $wpdb */ |
199 | 199 | global $wpdb; |
200 | 200 | $time_to_leave_alone = apply_filters( |
@@ -202,13 +202,13 @@ discard block |
||
202 | 202 | ); |
203 | 203 | $query = $wpdb->prepare( |
204 | 204 | 'DELETE li |
205 | - FROM ' . $this->table() . ' li |
|
206 | - LEFT JOIN ' . EEM_Transaction::instance()->table(). ' t ON li.TXN_ID = t.TXN_ID |
|
205 | + FROM ' . $this->table().' li |
|
206 | + LEFT JOIN ' . EEM_Transaction::instance()->table().' t ON li.TXN_ID = t.TXN_ID |
|
207 | 207 | WHERE t.TXN_ID IS NULL AND li.LIN_timestamp < %s', |
208 | 208 | // use GMT time because that's what TXN_timestamps are in |
209 | - date( 'Y-m-d H:i:s', time() - $time_to_leave_alone ) |
|
209 | + date('Y-m-d H:i:s', time() - $time_to_leave_alone) |
|
210 | 210 | ); |
211 | - return $wpdb->query( $query ); |
|
211 | + return $wpdb->query($query); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | |
@@ -221,10 +221,10 @@ discard block |
||
221 | 221 | * @param \EE_Base_Class $object |
222 | 222 | * @return EE_Line_Item[] |
223 | 223 | */ |
224 | - public function get_line_item_for_transaction_object( $TXN_ID, EE_Base_Class $object ){ |
|
225 | - return $this->get_all( array( array( |
|
224 | + public function get_line_item_for_transaction_object($TXN_ID, EE_Base_Class $object) { |
|
225 | + return $this->get_all(array(array( |
|
226 | 226 | 'TXN_ID' => $TXN_ID, |
227 | - 'OBJ_type' => str_replace( 'EE_', '', get_class( $object )), |
|
227 | + 'OBJ_type' => str_replace('EE_', '', get_class($object)), |
|
228 | 228 | 'OBJ_ID' => $object->ID() |
229 | 229 | ))); |
230 | 230 | } |
@@ -240,16 +240,16 @@ discard block |
||
240 | 240 | * @param array $OBJ_IDs |
241 | 241 | * @return EE_Line_Item[] |
242 | 242 | */ |
243 | - public function get_object_line_items_for_transaction( $TXN_ID, $OBJ_type = 'Event', $OBJ_IDs = array() ){ |
|
243 | + public function get_object_line_items_for_transaction($TXN_ID, $OBJ_type = 'Event', $OBJ_IDs = array()) { |
|
244 | 244 | $query_params = array( |
245 | 245 | 'OBJ_type' => $OBJ_type, |
246 | 246 | // if incoming $OBJ_IDs is an array, then make sure it is formatted correctly for the query |
247 | - 'OBJ_ID' => is_array( $OBJ_IDs ) && ! isset( $OBJ_IDs['IN'] ) ? array( 'IN', $OBJ_IDs ) : $OBJ_IDs |
|
247 | + 'OBJ_ID' => is_array($OBJ_IDs) && ! isset($OBJ_IDs['IN']) ? array('IN', $OBJ_IDs) : $OBJ_IDs |
|
248 | 248 | ); |
249 | - if ( $TXN_ID ) { |
|
249 | + if ($TXN_ID) { |
|
250 | 250 | $query_params['TXN_ID'] = $TXN_ID; |
251 | 251 | } |
252 | - return $this->get_all( array( $query_params )); |
|
252 | + return $this->get_all(array($query_params)); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | |
@@ -260,13 +260,13 @@ discard block |
||
260 | 260 | * @param EE_Transaction $transaction |
261 | 261 | * @return EE_Line_Item[] |
262 | 262 | */ |
263 | - public function get_all_ticket_line_items_for_transaction( EE_Transaction $transaction ) { |
|
264 | - return $this->get_all( array( |
|
263 | + public function get_all_ticket_line_items_for_transaction(EE_Transaction $transaction) { |
|
264 | + return $this->get_all(array( |
|
265 | 265 | array( |
266 | 266 | 'TXN_ID' => $transaction->ID(), |
267 | 267 | 'OBJ_type' => 'Ticket', |
268 | 268 | ) |
269 | - ) ); |
|
269 | + )); |
|
270 | 270 | } |
271 | 271 | |
272 | 272 | |
@@ -278,14 +278,14 @@ discard block |
||
278 | 278 | * @param int $TKT_ID |
279 | 279 | * @return \EE_Line_Item |
280 | 280 | */ |
281 | - public function get_ticket_line_item_for_transaction( $TXN_ID, $TKT_ID ) { |
|
282 | - return $this->get_one( array( |
|
281 | + public function get_ticket_line_item_for_transaction($TXN_ID, $TKT_ID) { |
|
282 | + return $this->get_one(array( |
|
283 | 283 | array( |
284 | - 'TXN_ID' => EEM_Transaction::instance()->ensure_is_ID( $TXN_ID ), |
|
284 | + 'TXN_ID' => EEM_Transaction::instance()->ensure_is_ID($TXN_ID), |
|
285 | 285 | 'OBJ_ID' => $TKT_ID, |
286 | 286 | 'OBJ_type' => 'Ticket', |
287 | 287 | ) |
288 | - ) ); |
|
288 | + )); |
|
289 | 289 | } |
290 | 290 | |
291 | 291 | |
@@ -300,8 +300,8 @@ discard block |
||
300 | 300 | * @param EE_Promotion $promotion |
301 | 301 | * @return EE_Line_Item |
302 | 302 | */ |
303 | - public function get_existing_promotion_line_item( EE_Line_Item $parent_line_item, EE_Promotion $promotion ) { |
|
304 | - return $this->get_one( array( |
|
303 | + public function get_existing_promotion_line_item(EE_Line_Item $parent_line_item, EE_Promotion $promotion) { |
|
304 | + return $this->get_one(array( |
|
305 | 305 | array( |
306 | 306 | 'TXN_ID' => $parent_line_item->TXN_ID(), |
307 | 307 | 'LIN_parent' => $parent_line_item->ID(), |
@@ -322,8 +322,8 @@ discard block |
||
322 | 322 | * @param EE_Line_Item $parent_line_item |
323 | 323 | * @return EE_Line_Item[] |
324 | 324 | */ |
325 | - public function get_all_promotion_line_items( EE_Line_Item $parent_line_item ) { |
|
326 | - return $this->get_all( array( |
|
325 | + public function get_all_promotion_line_items(EE_Line_Item $parent_line_item) { |
|
326 | + return $this->get_all(array( |
|
327 | 327 | array( |
328 | 328 | 'TXN_ID' => $parent_line_item->TXN_ID(), |
329 | 329 | 'LIN_parent' => $parent_line_item->ID(), |
@@ -340,8 +340,8 @@ discard block |
||
340 | 340 | * @param EE_Registration $registration |
341 | 341 | * @return EE_Line_ITem |
342 | 342 | */ |
343 | - public function get_line_item_for_registration( EE_Registration $registration ) { |
|
344 | - return $this->get_one( $this->line_item_for_registration_query_params( $registration )); |
|
343 | + public function get_line_item_for_registration(EE_Registration $registration) { |
|
344 | + return $this->get_one($this->line_item_for_registration_query_params($registration)); |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | /** |
@@ -350,14 +350,14 @@ discard block |
||
350 | 350 | * @param array $original_query_params any extra query params you'd like to be merged with |
351 | 351 | * @return array like EEM_Base::get_all()'s $query_params |
352 | 352 | */ |
353 | - public function line_item_for_registration_query_params( EE_Registration $registration, $original_query_params = array() ) { |
|
354 | - return array_replace_recursive( $original_query_params, array( |
|
353 | + public function line_item_for_registration_query_params(EE_Registration $registration, $original_query_params = array()) { |
|
354 | + return array_replace_recursive($original_query_params, array( |
|
355 | 355 | array( |
356 | 356 | 'OBJ_ID' => $registration->ticket_ID(), |
357 | 357 | 'OBJ_type' => 'Ticket', |
358 | 358 | 'TXN_ID' => $registration->transaction_ID() |
359 | 359 | ) |
360 | - ) ); |
|
360 | + )); |
|
361 | 361 | } |
362 | 362 | |
363 | 363 |
@@ -362,62 +362,62 @@ |
||
362 | 362 | |
363 | 363 | |
364 | 364 | |
365 | - /** |
|
366 | - * @return EE_Base_Class[]|EE_Line_Item[] |
|
367 | - * @throws \EE_Error |
|
368 | - */ |
|
369 | - public function get_total_line_items_with_no_transaction() |
|
370 | - { |
|
371 | - return $this->get_total_line_items_for_carts(); |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - |
|
376 | - /** |
|
377 | - * @return EE_Base_Class[]|EE_Line_Item[] |
|
378 | - * @throws \EE_Error |
|
379 | - */ |
|
380 | - public function get_total_line_items_for_active_carts() |
|
381 | - { |
|
382 | - return $this->get_total_line_items_for_carts(false); |
|
383 | - } |
|
384 | - |
|
385 | - |
|
386 | - |
|
387 | - /** |
|
388 | - * @return EE_Base_Class[]|EE_Line_Item[] |
|
389 | - * @throws \EE_Error |
|
390 | - */ |
|
391 | - public function get_total_line_items_for_expired_carts() |
|
392 | - { |
|
393 | - return $this->get_total_line_items_for_carts(true); |
|
394 | - } |
|
395 | - |
|
396 | - |
|
397 | - |
|
398 | - /** |
|
399 | - * Returns an array of grand total line items where the TXN_ID is 0. |
|
400 | - * If $expired is set to true, then only line items for expired sessions will be returned. |
|
401 | - * If $expired is set to false, then only line items for active sessions will be returned. |
|
402 | - * |
|
403 | - * @param bool|null $expired |
|
404 | - * @return EE_Base_Class[]|EE_Line_Item[] |
|
405 | - * @throws \EE_Error |
|
406 | - */ |
|
407 | - private function get_total_line_items_for_carts($expired = null) |
|
408 | - { |
|
409 | - $where_params = array( |
|
410 | - 'TXN_ID' => 0, |
|
411 | - 'LIN_type' => 'total', |
|
412 | - ); |
|
413 | - if ($expired !== null) { |
|
414 | - $where_params['LIN_timestamp'] = array( |
|
415 | - $expired ? '<=' : '>', |
|
416 | - time() - EE_Registry::instance()->SSN->lifespan(), |
|
417 | - ); |
|
418 | - } |
|
419 | - return $this->get_all(array($where_params)); |
|
420 | - } |
|
365 | + /** |
|
366 | + * @return EE_Base_Class[]|EE_Line_Item[] |
|
367 | + * @throws \EE_Error |
|
368 | + */ |
|
369 | + public function get_total_line_items_with_no_transaction() |
|
370 | + { |
|
371 | + return $this->get_total_line_items_for_carts(); |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + |
|
376 | + /** |
|
377 | + * @return EE_Base_Class[]|EE_Line_Item[] |
|
378 | + * @throws \EE_Error |
|
379 | + */ |
|
380 | + public function get_total_line_items_for_active_carts() |
|
381 | + { |
|
382 | + return $this->get_total_line_items_for_carts(false); |
|
383 | + } |
|
384 | + |
|
385 | + |
|
386 | + |
|
387 | + /** |
|
388 | + * @return EE_Base_Class[]|EE_Line_Item[] |
|
389 | + * @throws \EE_Error |
|
390 | + */ |
|
391 | + public function get_total_line_items_for_expired_carts() |
|
392 | + { |
|
393 | + return $this->get_total_line_items_for_carts(true); |
|
394 | + } |
|
395 | + |
|
396 | + |
|
397 | + |
|
398 | + /** |
|
399 | + * Returns an array of grand total line items where the TXN_ID is 0. |
|
400 | + * If $expired is set to true, then only line items for expired sessions will be returned. |
|
401 | + * If $expired is set to false, then only line items for active sessions will be returned. |
|
402 | + * |
|
403 | + * @param bool|null $expired |
|
404 | + * @return EE_Base_Class[]|EE_Line_Item[] |
|
405 | + * @throws \EE_Error |
|
406 | + */ |
|
407 | + private function get_total_line_items_for_carts($expired = null) |
|
408 | + { |
|
409 | + $where_params = array( |
|
410 | + 'TXN_ID' => 0, |
|
411 | + 'LIN_type' => 'total', |
|
412 | + ); |
|
413 | + if ($expired !== null) { |
|
414 | + $where_params['LIN_timestamp'] = array( |
|
415 | + $expired ? '<=' : '>', |
|
416 | + time() - EE_Registry::instance()->SSN->lifespan(), |
|
417 | + ); |
|
418 | + } |
|
419 | + return $this->get_all(array($where_params)); |
|
420 | + } |
|
421 | 421 | |
422 | 422 | |
423 | 423 |
@@ -129,13 +129,13 @@ discard block |
||
129 | 129 | |
130 | 130 | |
131 | 131 | |
132 | - /** |
|
133 | - * Updates the TKT_sold quantity on all the tickets matching $query_params |
|
134 | - * |
|
135 | - * @param EE_Ticket[] $tickets |
|
136 | - * @return void |
|
137 | - * @throws \EE_Error |
|
138 | - */ |
|
132 | + /** |
|
133 | + * Updates the TKT_sold quantity on all the tickets matching $query_params |
|
134 | + * |
|
135 | + * @param EE_Ticket[] $tickets |
|
136 | + * @return void |
|
137 | + * @throws \EE_Error |
|
138 | + */ |
|
139 | 139 | public function update_tickets_sold($tickets){ |
140 | 140 | foreach($tickets as $ticket){ |
141 | 141 | /* @var $ticket EE_Ticket */ |
@@ -145,41 +145,41 @@ discard block |
||
145 | 145 | |
146 | 146 | |
147 | 147 | |
148 | - /** |
|
149 | - * returns an array of EE_Ticket objects with a non-zero value for TKT_reserved |
|
150 | - * |
|
151 | - * @return EE_Base_Class[]|EE_Ticket[] |
|
152 | - * @throws \EE_Error |
|
153 | - */ |
|
154 | - public function get_tickets_with_reservations() |
|
155 | - { |
|
156 | - return $this->get_all( |
|
157 | - array( |
|
158 | - array( |
|
159 | - 'TKT_reserved' => array( '>', 0 ) |
|
160 | - ) |
|
161 | - ) |
|
162 | - ); |
|
148 | + /** |
|
149 | + * returns an array of EE_Ticket objects with a non-zero value for TKT_reserved |
|
150 | + * |
|
151 | + * @return EE_Base_Class[]|EE_Ticket[] |
|
152 | + * @throws \EE_Error |
|
153 | + */ |
|
154 | + public function get_tickets_with_reservations() |
|
155 | + { |
|
156 | + return $this->get_all( |
|
157 | + array( |
|
158 | + array( |
|
159 | + 'TKT_reserved' => array( '>', 0 ) |
|
160 | + ) |
|
161 | + ) |
|
162 | + ); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | |
166 | 166 | |
167 | - /** |
|
168 | - * returns an array of EE_Ticket objects matching the supplied list of IDs |
|
169 | - * |
|
170 | - * @param array $ticket_IDs |
|
171 | - * @return EE_Base_Class[]|EE_Ticket[] |
|
172 | - * @throws \EE_Error |
|
173 | - */ |
|
174 | - public function get_tickets_with_IDs(array $ticket_IDs) |
|
175 | - { |
|
176 | - return $this->get_all( |
|
177 | - array( |
|
178 | - array( |
|
179 | - 'TKT_ID' => array( 'IN', $ticket_IDs ) |
|
180 | - ) |
|
181 | - ) |
|
182 | - ); |
|
167 | + /** |
|
168 | + * returns an array of EE_Ticket objects matching the supplied list of IDs |
|
169 | + * |
|
170 | + * @param array $ticket_IDs |
|
171 | + * @return EE_Base_Class[]|EE_Ticket[] |
|
172 | + * @throws \EE_Error |
|
173 | + */ |
|
174 | + public function get_tickets_with_IDs(array $ticket_IDs) |
|
175 | + { |
|
176 | + return $this->get_all( |
|
177 | + array( |
|
178 | + array( |
|
179 | + 'TKT_ID' => array( 'IN', $ticket_IDs ) |
|
180 | + ) |
|
181 | + ) |
|
182 | + ); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 |
@@ -21,8 +21,8 @@ discard block |
||
21 | 21 | * |
22 | 22 | * ------------------------------------------------------------------------ |
23 | 23 | */ |
24 | -require_once ( EE_MODELS . 'EEM_Soft_Delete_Base.model.php' ); |
|
25 | -require_once ( EE_CLASSES . 'EE_Ticket.class.php' ); |
|
24 | +require_once (EE_MODELS.'EEM_Soft_Delete_Base.model.php'); |
|
25 | +require_once (EE_CLASSES.'EE_Ticket.class.php'); |
|
26 | 26 | |
27 | 27 | class EEM_Ticket extends EEM_Soft_Delete_Base { |
28 | 28 | |
@@ -35,36 +35,36 @@ discard block |
||
35 | 35 | * @access private |
36 | 36 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved). Note this just sends the timezone info to the date time model field objects. Default is NULL (and will be assumed using the set timezone in the 'timezone_string' wp option) |
37 | 37 | */ |
38 | - protected function __construct( $timezone ) { |
|
39 | - $this->singular_item = __('Ticket','event_espresso'); |
|
40 | - $this->plural_item = __('Tickets','event_espresso'); |
|
38 | + protected function __construct($timezone) { |
|
39 | + $this->singular_item = __('Ticket', 'event_espresso'); |
|
40 | + $this->plural_item = __('Tickets', 'event_espresso'); |
|
41 | 41 | |
42 | 42 | $this->_tables = array( |
43 | 43 | 'Ticket'=> new EE_Primary_Table('esp_ticket', 'TKT_ID') |
44 | 44 | ); |
45 | 45 | $this->_fields = array( |
46 | 46 | 'Ticket'=>array( |
47 | - 'TKT_ID'=> new EE_Primary_Key_Int_Field('TKT_ID', __('Ticket ID','event_espresso')), |
|
48 | - 'TTM_ID'=>new EE_Foreign_Key_Int_Field('TTM_ID', __('Ticket Template ID','event_espresso'), false, 0, 'Ticket_Template'), |
|
47 | + 'TKT_ID'=> new EE_Primary_Key_Int_Field('TKT_ID', __('Ticket ID', 'event_espresso')), |
|
48 | + 'TTM_ID'=>new EE_Foreign_Key_Int_Field('TTM_ID', __('Ticket Template ID', 'event_espresso'), false, 0, 'Ticket_Template'), |
|
49 | 49 | 'TKT_name'=>new EE_Plain_Text_Field('TKT_name', __('Ticket Name', 'event_espresso'), false, ''), |
50 | - 'TKT_description'=>new EE_Post_Content_Field('TKT_description', __('Description of Ticket', 'event_espresso'), false, '' ), |
|
51 | - 'TKT_start_date'=>new EE_Datetime_Field('TKT_start_date', __('Start time/date of Ticket','event_espresso'), false, EE_Datetime_Field::now, $timezone ), |
|
52 | - 'TKT_end_date'=>new EE_Datetime_Field('TKT_end_date', __('End time/date of Ticket','event_espresso'), false, EE_Datetime_Field::now, $timezone ), |
|
53 | - 'TKT_min'=>new EE_Integer_Field('TKT_min', __('Minimum quantity of this ticket that must be purchased', 'event_espresso'), false, 0 ), |
|
54 | - 'TKT_max'=>new EE_Infinite_Integer_Field('TKT_max', __('Maximum quantity of this ticket that can be purchased in one transaction', 'event_espresso'), false, EE_INF ), |
|
50 | + 'TKT_description'=>new EE_Post_Content_Field('TKT_description', __('Description of Ticket', 'event_espresso'), false, ''), |
|
51 | + 'TKT_start_date'=>new EE_Datetime_Field('TKT_start_date', __('Start time/date of Ticket', 'event_espresso'), false, EE_Datetime_Field::now, $timezone), |
|
52 | + 'TKT_end_date'=>new EE_Datetime_Field('TKT_end_date', __('End time/date of Ticket', 'event_espresso'), false, EE_Datetime_Field::now, $timezone), |
|
53 | + 'TKT_min'=>new EE_Integer_Field('TKT_min', __('Minimum quantity of this ticket that must be purchased', 'event_espresso'), false, 0), |
|
54 | + 'TKT_max'=>new EE_Infinite_Integer_Field('TKT_max', __('Maximum quantity of this ticket that can be purchased in one transaction', 'event_espresso'), false, EE_INF), |
|
55 | 55 | 'TKT_price'=> new EE_Money_Field('TKT_price', 'Final calculated price for ticket', false, 0), |
56 | 56 | 'TKT_sold' => new EE_Integer_Field('TKT_sold', __('Number of this ticket sold', 'event_espresso'), false, 0), |
57 | - 'TKT_qty'=>new EE_Infinite_Integer_Field('TKT_qty', __('Quantity of this ticket that is available','event_espresso'), false, EE_INF), |
|
58 | - 'TKT_reserved'=>new EE_Integer_Field('TKT_reserved', __('Quantity of this ticket that is reserved, but not yet fully purchased','event_espresso'), false, 0 ), |
|
59 | - 'TKT_uses'=>new EE_Infinite_Integer_Field('TKT_uses', __('Number of datetimes this ticket can be used at', 'event_espresso'), false, EE_INF ), |
|
60 | - 'TKT_required'=>new EE_Boolean_Field('TKT_required', __("Flag indicating whether this ticket must be purchased with a transaction", "event_espresso"), false, false ), |
|
61 | - 'TKT_taxable'=>new EE_Boolean_Field('TKT_taxable', __("Flag indicating whether there is tax applied on this ticket", "event_espresso"), false,false), |
|
62 | - 'TKT_is_default'=>new EE_Boolean_Field('TKT_is_default', __('Flag indicating that this ticket is a default ticket', 'event_espresso'), false, false ), |
|
57 | + 'TKT_qty'=>new EE_Infinite_Integer_Field('TKT_qty', __('Quantity of this ticket that is available', 'event_espresso'), false, EE_INF), |
|
58 | + 'TKT_reserved'=>new EE_Integer_Field('TKT_reserved', __('Quantity of this ticket that is reserved, but not yet fully purchased', 'event_espresso'), false, 0), |
|
59 | + 'TKT_uses'=>new EE_Infinite_Integer_Field('TKT_uses', __('Number of datetimes this ticket can be used at', 'event_espresso'), false, EE_INF), |
|
60 | + 'TKT_required'=>new EE_Boolean_Field('TKT_required', __("Flag indicating whether this ticket must be purchased with a transaction", "event_espresso"), false, false), |
|
61 | + 'TKT_taxable'=>new EE_Boolean_Field('TKT_taxable', __("Flag indicating whether there is tax applied on this ticket", "event_espresso"), false, false), |
|
62 | + 'TKT_is_default'=>new EE_Boolean_Field('TKT_is_default', __('Flag indicating that this ticket is a default ticket', 'event_espresso'), false, false), |
|
63 | 63 | 'TKT_order' => new EE_Integer_Field('TKT_order', __('The order in which the Ticket is displayed in the editor (used for autosaves when the form doesn\'t have the ticket ID yet)', 'event_espresso'), false, 0), |
64 | - 'TKT_row' => new EE_Integer_Field('TKT_row', __('How tickets are displayed in the ui', 'event_espresso'), false, 0 ), |
|
64 | + 'TKT_row' => new EE_Integer_Field('TKT_row', __('How tickets are displayed in the ui', 'event_espresso'), false, 0), |
|
65 | 65 | 'TKT_deleted' => new EE_Trashed_Flag_Field('TKT_deleted', __('Flag indicating if this has been archived or not', 'event_espresso'), false, false), |
66 | - 'TKT_wp_user' => new EE_WP_User_Field('TKT_wp_user', __('Ticket Creator ID', 'event_espresso'), FALSE ), |
|
67 | - 'TKT_parent' => new EE_Integer_Field('TKT_parent', __('Indicates what TKT_ID is the parent of this TKT_ID (used in autosaves/revisions)'), true, 0 ) |
|
66 | + 'TKT_wp_user' => new EE_WP_User_Field('TKT_wp_user', __('Ticket Creator ID', 'event_espresso'), FALSE), |
|
67 | + 'TKT_parent' => new EE_Integer_Field('TKT_parent', __('Indicates what TKT_ID is the parent of this TKT_ID (used in autosaves/revisions)'), true, 0) |
|
68 | 68 | )); |
69 | 69 | $this->_model_relations = array( |
70 | 70 | 'Datetime'=>new EE_HABTM_Relation('Datetime_Ticket'), |
@@ -76,12 +76,12 @@ discard block |
||
76 | 76 | ); |
77 | 77 | |
78 | 78 | //this model is generally available for reading |
79 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Default_Public('TKT_is_default', 'Datetime.Event'); |
|
79 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Default_Public('TKT_is_default', 'Datetime.Event'); |
|
80 | 80 | //account for default tickets in the caps |
81 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Default_Protected( 'TKT_is_default', 'Datetime.Event' ); |
|
82 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Default_Protected( 'TKT_is_default', 'Datetime.Event' ); |
|
83 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Default_Protected( 'TKT_is_default', 'Datetime.Event' ); |
|
84 | - parent::__construct( $timezone ); |
|
81 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Default_Protected('TKT_is_default', 'Datetime.Event'); |
|
82 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = new EE_Restriction_Generator_Default_Protected('TKT_is_default', 'Datetime.Event'); |
|
83 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = new EE_Restriction_Generator_Default_Protected('TKT_is_default', 'Datetime.Event'); |
|
84 | + parent::__construct($timezone); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | |
@@ -92,9 +92,9 @@ discard block |
||
92 | 92 | */ |
93 | 93 | public function get_all_default_tickets() { |
94 | 94 | /** @type EE_Ticket[] $tickets */ |
95 | - $tickets = $this->get_all( array( array('TKT_is_default' => 1), 'order_by' => array('TKT_ID' => 'ASC')) ); |
|
95 | + $tickets = $this->get_all(array(array('TKT_is_default' => 1), 'order_by' => array('TKT_ID' => 'ASC'))); |
|
96 | 96 | //we need to set the start date and end date to today's date and the start of the default dtt |
97 | - return $this->_set_default_dates( $tickets ); |
|
97 | + return $this->_set_default_dates($tickets); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | |
@@ -104,11 +104,11 @@ discard block |
||
104 | 104 | * @param EE_Ticket[] $tickets |
105 | 105 | * @return EE_Ticket[] |
106 | 106 | */ |
107 | - private function _set_default_dates( $tickets ) { |
|
108 | - foreach ( $tickets as $ticket ) { |
|
109 | - $ticket->set('TKT_start_date', (int)$this->current_time_for_query('TKT_start_date', true) ); |
|
110 | - $ticket->set('TKT_end_date', (int)$this->current_time_for_query( 'TKT_end_date', true ) + (60 * 60 * 24 * 30 ) ); |
|
111 | - $ticket->set_end_time( $this->convert_datetime_for_query( 'TKT_end_date', '11:59 pm', 'g:i a', $this->_timezone ) ); |
|
107 | + private function _set_default_dates($tickets) { |
|
108 | + foreach ($tickets as $ticket) { |
|
109 | + $ticket->set('TKT_start_date', (int) $this->current_time_for_query('TKT_start_date', true)); |
|
110 | + $ticket->set('TKT_end_date', (int) $this->current_time_for_query('TKT_end_date', true) + (60 * 60 * 24 * 30)); |
|
111 | + $ticket->set_end_time($this->convert_datetime_for_query('TKT_end_date', '11:59 pm', 'g:i a', $this->_timezone)); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | return $tickets; |
@@ -123,8 +123,8 @@ discard block |
||
123 | 123 | * @param array $query_params |
124 | 124 | * @return int |
125 | 125 | */ |
126 | - public function sum_tickets_currently_available_at_datetime($DTT_ID, $query_params = array()){ |
|
127 | - return EEM_Datetime::instance()->sum_tickets_currently_available_at_datetime( $DTT_ID, $query_params ); |
|
126 | + public function sum_tickets_currently_available_at_datetime($DTT_ID, $query_params = array()) { |
|
127 | + return EEM_Datetime::instance()->sum_tickets_currently_available_at_datetime($DTT_ID, $query_params); |
|
128 | 128 | } |
129 | 129 | |
130 | 130 | |
@@ -136,8 +136,8 @@ discard block |
||
136 | 136 | * @return void |
137 | 137 | * @throws \EE_Error |
138 | 138 | */ |
139 | - public function update_tickets_sold($tickets){ |
|
140 | - foreach($tickets as $ticket){ |
|
139 | + public function update_tickets_sold($tickets) { |
|
140 | + foreach ($tickets as $ticket) { |
|
141 | 141 | /* @var $ticket EE_Ticket */ |
142 | 142 | $ticket->update_tickets_sold(); |
143 | 143 | } |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | return $this->get_all( |
157 | 157 | array( |
158 | 158 | array( |
159 | - 'TKT_reserved' => array( '>', 0 ) |
|
159 | + 'TKT_reserved' => array('>', 0) |
|
160 | 160 | ) |
161 | 161 | ) |
162 | 162 | ); |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | return $this->get_all( |
177 | 177 | array( |
178 | 178 | array( |
179 | - 'TKT_ID' => array( 'IN', $ticket_IDs ) |
|
179 | + 'TKT_ID' => array('IN', $ticket_IDs) |
|
180 | 180 | ) |
181 | 181 | ) |
182 | 182 | ); |
@@ -1,5 +1,5 @@ discard block |
||
1 | -<?php if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
2 | - exit( 'No direct script access allowed' ); |
|
1 | +<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
2 | + exit('No direct script access allowed'); |
|
3 | 3 | } |
4 | 4 | /** |
5 | 5 | * Event Espresso |
@@ -50,9 +50,9 @@ discard block |
||
50 | 50 | * date_format and the second value is the time format |
51 | 51 | * @return EE_Line_Item |
52 | 52 | */ |
53 | - public static function new_instance( $props_n_values = array(), $timezone = null, $date_formats = array() ) { |
|
54 | - $has_object = parent::_check_for_object( $props_n_values, __CLASS__, $timezone, $date_formats ); |
|
55 | - return $has_object ? $has_object : new self( $props_n_values, false, $timezone, $date_formats ); |
|
53 | + public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array()) { |
|
54 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats); |
|
55 | + return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | * the website will be used. |
64 | 64 | * @return EE_Line_Item |
65 | 65 | */ |
66 | - public static function new_instance_from_db( $props_n_values = array(), $timezone = null ) { |
|
67 | - return new self( $props_n_values, TRUE, $timezone ); |
|
66 | + public static function new_instance_from_db($props_n_values = array(), $timezone = null) { |
|
67 | + return new self($props_n_values, TRUE, $timezone); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | |
@@ -75,10 +75,10 @@ discard block |
||
75 | 75 | * @param bool $bydb |
76 | 76 | * @param string $timezone |
77 | 77 | */ |
78 | - protected function __construct( $fieldValues = array(), $bydb = FALSE, $timezone = '' ) { |
|
79 | - parent::__construct( $fieldValues, $bydb, $timezone ); |
|
80 | - if ( ! $this->get( 'LIN_code' ) ) { |
|
81 | - $this->set_code( $this->generate_code() ); |
|
78 | + protected function __construct($fieldValues = array(), $bydb = FALSE, $timezone = '') { |
|
79 | + parent::__construct($fieldValues, $bydb, $timezone); |
|
80 | + if ( ! $this->get('LIN_code')) { |
|
81 | + $this->set_code($this->generate_code()); |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 | |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | * @return int |
90 | 90 | */ |
91 | 91 | public function ID() { |
92 | - return $this->get( 'LIN_ID' ); |
|
92 | + return $this->get('LIN_ID'); |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | * @return int |
100 | 100 | */ |
101 | 101 | public function TXN_ID() { |
102 | - return $this->get( 'TXN_ID' ); |
|
102 | + return $this->get('TXN_ID'); |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | |
@@ -108,8 +108,8 @@ discard block |
||
108 | 108 | * Sets TXN_ID |
109 | 109 | * @param int $TXN_ID |
110 | 110 | */ |
111 | - public function set_TXN_ID( $TXN_ID ) { |
|
112 | - $this->set( 'TXN_ID', $TXN_ID ); |
|
111 | + public function set_TXN_ID($TXN_ID) { |
|
112 | + $this->set('TXN_ID', $TXN_ID); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | |
@@ -119,9 +119,9 @@ discard block |
||
119 | 119 | * @return string |
120 | 120 | */ |
121 | 121 | public function name() { |
122 | - $name = $this->get( 'LIN_name' ); |
|
123 | - if( ! $name ){ |
|
124 | - $name = ucwords( str_replace( '-', ' ', $this->type() ) ); |
|
122 | + $name = $this->get('LIN_name'); |
|
123 | + if ( ! $name) { |
|
124 | + $name = ucwords(str_replace('-', ' ', $this->type())); |
|
125 | 125 | } |
126 | 126 | return $name; |
127 | 127 | } |
@@ -132,8 +132,8 @@ discard block |
||
132 | 132 | * Sets name |
133 | 133 | * @param string $name |
134 | 134 | */ |
135 | - public function set_name( $name ) { |
|
136 | - $this->set( 'LIN_name', $name ); |
|
135 | + public function set_name($name) { |
|
136 | + $this->set('LIN_name', $name); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | * @return string |
144 | 144 | */ |
145 | 145 | public function desc() { |
146 | - return $this->get( 'LIN_desc' ); |
|
146 | + return $this->get('LIN_desc'); |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | |
@@ -152,8 +152,8 @@ discard block |
||
152 | 152 | * Sets desc |
153 | 153 | * @param string $desc |
154 | 154 | */ |
155 | - public function set_desc( $desc ) { |
|
156 | - $this->set( 'LIN_desc', $desc ); |
|
155 | + public function set_desc($desc) { |
|
156 | + $this->set('LIN_desc', $desc); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | * @return int |
164 | 164 | */ |
165 | 165 | public function quantity() { |
166 | - return $this->get( 'LIN_quantity' ); |
|
166 | + return $this->get('LIN_quantity'); |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | |
@@ -172,8 +172,8 @@ discard block |
||
172 | 172 | * Sets quantity |
173 | 173 | * @param int $quantity |
174 | 174 | */ |
175 | - public function set_quantity( $quantity ) { |
|
176 | - $this->set( 'LIN_quantity', max( $quantity, 0 ) ); |
|
175 | + public function set_quantity($quantity) { |
|
176 | + $this->set('LIN_quantity', max($quantity, 0)); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | * @return string |
184 | 184 | */ |
185 | 185 | public function OBJ_ID() { |
186 | - return $this->get( 'OBJ_ID' ); |
|
186 | + return $this->get('OBJ_ID'); |
|
187 | 187 | } |
188 | 188 | |
189 | 189 | |
@@ -192,8 +192,8 @@ discard block |
||
192 | 192 | * Sets item_id |
193 | 193 | * @param string $item_id |
194 | 194 | */ |
195 | - public function set_OBJ_ID( $item_id ) { |
|
196 | - $this->set( 'OBJ_ID', $item_id ); |
|
195 | + public function set_OBJ_ID($item_id) { |
|
196 | + $this->set('OBJ_ID', $item_id); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | * @return string |
204 | 204 | */ |
205 | 205 | public function OBJ_type() { |
206 | - return $this->get( 'OBJ_type' ); |
|
206 | + return $this->get('OBJ_type'); |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | |
@@ -240,8 +240,8 @@ discard block |
||
240 | 240 | * Sets item_type |
241 | 241 | * @param string $OBJ_type |
242 | 242 | */ |
243 | - public function set_OBJ_type( $OBJ_type ) { |
|
244 | - $this->set( 'OBJ_type', $OBJ_type ); |
|
243 | + public function set_OBJ_type($OBJ_type) { |
|
244 | + $this->set('OBJ_type', $OBJ_type); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | * @return float |
252 | 252 | */ |
253 | 253 | public function unit_price() { |
254 | - return $this->get( 'LIN_unit_price' ); |
|
254 | + return $this->get('LIN_unit_price'); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | |
@@ -261,8 +261,8 @@ discard block |
||
261 | 261 | * |
262 | 262 | * @param float $unit_price |
263 | 263 | */ |
264 | - public function set_unit_price( $unit_price ) { |
|
265 | - $this->set( 'LIN_unit_price', $unit_price ); |
|
264 | + public function set_unit_price($unit_price) { |
|
265 | + $this->set('LIN_unit_price', $unit_price); |
|
266 | 266 | } |
267 | 267 | |
268 | 268 | |
@@ -273,19 +273,19 @@ discard block |
||
273 | 273 | * @return boolean |
274 | 274 | */ |
275 | 275 | public function is_percent() { |
276 | - if( $this->is_tax_sub_total() ) { |
|
276 | + if ($this->is_tax_sub_total()) { |
|
277 | 277 | //tax subtotals HAVE a percent on them, that percentage only applies |
278 | 278 | //to taxable items, so its' an exception. Treat it like a flat line item |
279 | 279 | return false; |
280 | 280 | } |
281 | - $unit_price = abs( $this->get( 'LIN_unit_price' ) ); |
|
282 | - $percent = abs( $this->get( 'LIN_percent' ) ); |
|
283 | - if ( $unit_price < .001 && $percent ) { |
|
281 | + $unit_price = abs($this->get('LIN_unit_price')); |
|
282 | + $percent = abs($this->get('LIN_percent')); |
|
283 | + if ($unit_price < .001 && $percent) { |
|
284 | 284 | return TRUE; |
285 | - } elseif ( $unit_price >= .001 && !$percent ) { |
|
285 | + } elseif ($unit_price >= .001 && ! $percent) { |
|
286 | 286 | return FALSE; |
287 | - } elseif ( $unit_price >= .001 && $percent ) { |
|
288 | - throw new EE_Error( sprintf( __( "A Line Item can not have a unit price of (%s) AND a percent (%s)!", "event_espresso" ), $unit_price, $percent ) ); |
|
287 | + } elseif ($unit_price >= .001 && $percent) { |
|
288 | + throw new EE_Error(sprintf(__("A Line Item can not have a unit price of (%s) AND a percent (%s)!", "event_espresso"), $unit_price, $percent)); |
|
289 | 289 | } else { |
290 | 290 | // if they're both 0, assume its not a percent item |
291 | 291 | return FALSE; |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | * @return float |
300 | 300 | */ |
301 | 301 | public function percent() { |
302 | - return $this->get( 'LIN_percent' ); |
|
302 | + return $this->get('LIN_percent'); |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | |
@@ -308,8 +308,8 @@ discard block |
||
308 | 308 | * Sets percent (between 100-0.01) |
309 | 309 | * @param float $percent |
310 | 310 | */ |
311 | - public function set_percent( $percent ) { |
|
312 | - $this->set( 'LIN_percent', $percent ); |
|
311 | + public function set_percent($percent) { |
|
312 | + $this->set('LIN_percent', $percent); |
|
313 | 313 | } |
314 | 314 | |
315 | 315 | |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | * @return float |
320 | 320 | */ |
321 | 321 | public function total() { |
322 | - return $this->get( 'LIN_total' ); |
|
322 | + return $this->get('LIN_total'); |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | |
@@ -328,8 +328,8 @@ discard block |
||
328 | 328 | * Sets total |
329 | 329 | * @param float $total |
330 | 330 | */ |
331 | - public function set_total( $total ) { |
|
332 | - $this->set( 'LIN_total', $total ); |
|
331 | + public function set_total($total) { |
|
332 | + $this->set('LIN_total', $total); |
|
333 | 333 | } |
334 | 334 | |
335 | 335 | |
@@ -339,7 +339,7 @@ discard block |
||
339 | 339 | * @return int |
340 | 340 | */ |
341 | 341 | public function order() { |
342 | - return $this->get( 'LIN_order' ); |
|
342 | + return $this->get('LIN_order'); |
|
343 | 343 | } |
344 | 344 | |
345 | 345 | |
@@ -348,8 +348,8 @@ discard block |
||
348 | 348 | * Sets order |
349 | 349 | * @param int $order |
350 | 350 | */ |
351 | - public function set_order( $order ) { |
|
352 | - $this->set( 'LIN_order', $order ); |
|
351 | + public function set_order($order) { |
|
352 | + $this->set('LIN_order', $order); |
|
353 | 353 | } |
354 | 354 | |
355 | 355 | |
@@ -359,7 +359,7 @@ discard block |
||
359 | 359 | * @return int |
360 | 360 | */ |
361 | 361 | public function parent_ID() { |
362 | - return $this->get( 'LIN_parent' ); |
|
362 | + return $this->get('LIN_parent'); |
|
363 | 363 | } |
364 | 364 | |
365 | 365 | |
@@ -368,8 +368,8 @@ discard block |
||
368 | 368 | * Sets parent |
369 | 369 | * @param int $parent |
370 | 370 | */ |
371 | - public function set_parent_ID( $parent ) { |
|
372 | - $this->set( 'LIN_parent', $parent ); |
|
371 | + public function set_parent_ID($parent) { |
|
372 | + $this->set('LIN_parent', $parent); |
|
373 | 373 | } |
374 | 374 | |
375 | 375 | |
@@ -379,7 +379,7 @@ discard block |
||
379 | 379 | * @return string |
380 | 380 | */ |
381 | 381 | public function type() { |
382 | - return $this->get( 'LIN_type' ); |
|
382 | + return $this->get('LIN_type'); |
|
383 | 383 | } |
384 | 384 | |
385 | 385 | |
@@ -388,8 +388,8 @@ discard block |
||
388 | 388 | * Sets type |
389 | 389 | * @param string $type |
390 | 390 | */ |
391 | - public function set_type( $type ) { |
|
392 | - $this->set( 'LIN_type', $type ); |
|
391 | + public function set_type($type) { |
|
392 | + $this->set('LIN_type', $type); |
|
393 | 393 | } |
394 | 394 | |
395 | 395 | |
@@ -402,8 +402,8 @@ discard block |
||
402 | 402 | * @return EE_Line_Item |
403 | 403 | */ |
404 | 404 | public function parent() { |
405 | - if( $this->ID() ) { |
|
406 | - return $this->get_model()->get_one_by_ID( $this->parent_ID() ); |
|
405 | + if ($this->ID()) { |
|
406 | + return $this->get_model()->get_one_by_ID($this->parent_ID()); |
|
407 | 407 | } else { |
408 | 408 | return $this->_parent; |
409 | 409 | } |
@@ -416,13 +416,13 @@ discard block |
||
416 | 416 | * @return EE_Line_Item[] |
417 | 417 | */ |
418 | 418 | public function children() { |
419 | - if ( $this->ID() ) { |
|
419 | + if ($this->ID()) { |
|
420 | 420 | return $this->get_model()->get_all( |
421 | 421 | array( |
422 | - array( 'LIN_parent' => $this->ID() ), |
|
423 | - 'order_by' => array( 'LIN_order' => 'ASC' ) ) ); |
|
422 | + array('LIN_parent' => $this->ID()), |
|
423 | + 'order_by' => array('LIN_order' => 'ASC') ) ); |
|
424 | 424 | } else { |
425 | - if ( ! is_array( $this->_children ) ) { |
|
425 | + if ( ! is_array($this->_children)) { |
|
426 | 426 | $this->_children = array(); |
427 | 427 | } |
428 | 428 | return $this->_children; |
@@ -436,7 +436,7 @@ discard block |
||
436 | 436 | * @return string |
437 | 437 | */ |
438 | 438 | public function code() { |
439 | - return $this->get( 'LIN_code' ); |
|
439 | + return $this->get('LIN_code'); |
|
440 | 440 | } |
441 | 441 | |
442 | 442 | |
@@ -445,8 +445,8 @@ discard block |
||
445 | 445 | * Sets code |
446 | 446 | * @param string $code |
447 | 447 | */ |
448 | - public function set_code( $code ) { |
|
449 | - $this->set( 'LIN_code', $code ); |
|
448 | + public function set_code($code) { |
|
449 | + $this->set('LIN_code', $code); |
|
450 | 450 | } |
451 | 451 | |
452 | 452 | |
@@ -456,7 +456,7 @@ discard block |
||
456 | 456 | * @return boolean |
457 | 457 | */ |
458 | 458 | public function is_taxable() { |
459 | - return $this->get( 'LIN_is_taxable' ); |
|
459 | + return $this->get('LIN_is_taxable'); |
|
460 | 460 | } |
461 | 461 | |
462 | 462 | |
@@ -465,8 +465,8 @@ discard block |
||
465 | 465 | * Sets is_taxable |
466 | 466 | * @param boolean $is_taxable |
467 | 467 | */ |
468 | - public function set_is_taxable( $is_taxable ) { |
|
469 | - $this->set( 'LIN_is_taxable', $is_taxable ); |
|
468 | + public function set_is_taxable($is_taxable) { |
|
469 | + $this->set('LIN_is_taxable', $is_taxable); |
|
470 | 470 | } |
471 | 471 | |
472 | 472 | |
@@ -481,7 +481,7 @@ discard block |
||
481 | 481 | */ |
482 | 482 | public function get_object() { |
483 | 483 | $model_name_of_related_obj = $this->OBJ_type(); |
484 | - return $this->get_model()->has_relation( $model_name_of_related_obj ) ? $this->get_first_related( $model_name_of_related_obj ) : NULL; |
|
484 | + return $this->get_model()->has_relation($model_name_of_related_obj) ? $this->get_first_related($model_name_of_related_obj) : NULL; |
|
485 | 485 | } |
486 | 486 | |
487 | 487 | |
@@ -492,11 +492,11 @@ discard block |
||
492 | 492 | * @param array $query_params |
493 | 493 | * @return EE_Ticket |
494 | 494 | */ |
495 | - public function ticket( $query_params = array() ) { |
|
495 | + public function ticket($query_params = array()) { |
|
496 | 496 | //we're going to assume that when this method is called we always want to receive the attached ticket EVEN if that ticket is archived. This can be overridden via the incoming $query_params argument |
497 | - $remove_defaults = array( 'default_where_conditions' => 'none' ); |
|
498 | - $query_params = array_merge( $remove_defaults, $query_params ); |
|
499 | - return $this->get_first_related( 'Ticket', $query_params ); |
|
497 | + $remove_defaults = array('default_where_conditions' => 'none'); |
|
498 | + $query_params = array_merge($remove_defaults, $query_params); |
|
499 | + return $this->get_first_related('Ticket', $query_params); |
|
500 | 500 | } |
501 | 501 | |
502 | 502 | |
@@ -506,11 +506,11 @@ discard block |
||
506 | 506 | * @return EE_Datetime | NULL |
507 | 507 | */ |
508 | 508 | public function get_ticket_datetime() { |
509 | - if ( $this->OBJ_type() === 'Ticket' ) { |
|
509 | + if ($this->OBJ_type() === 'Ticket') { |
|
510 | 510 | $ticket = $this->ticket(); |
511 | - if ( $ticket instanceof EE_Ticket ) { |
|
511 | + if ($ticket instanceof EE_Ticket) { |
|
512 | 512 | $datetime = $ticket->first_datetime(); |
513 | - if ( $datetime instanceof EE_Datetime ) { |
|
513 | + if ($datetime instanceof EE_Datetime) { |
|
514 | 514 | return $datetime; |
515 | 515 | } |
516 | 516 | } |
@@ -526,9 +526,9 @@ discard block |
||
526 | 526 | * @return string |
527 | 527 | */ |
528 | 528 | public function ticket_event_name() { |
529 | - $event_name = __( "Unknown", "event_espresso" ); |
|
529 | + $event_name = __("Unknown", "event_espresso"); |
|
530 | 530 | $event = $this->ticket_event(); |
531 | - if ( $event instanceof EE_Event ) { |
|
531 | + if ($event instanceof EE_Event) { |
|
532 | 532 | $event_name = $event->name(); |
533 | 533 | } |
534 | 534 | return $event_name; |
@@ -542,9 +542,9 @@ discard block |
||
542 | 542 | public function ticket_event() { |
543 | 543 | $event = null; |
544 | 544 | $ticket = $this->ticket(); |
545 | - if ( $ticket instanceof EE_Ticket ) { |
|
545 | + if ($ticket instanceof EE_Ticket) { |
|
546 | 546 | $datetime = $ticket->first_datetime(); |
547 | - if ( $datetime instanceof EE_Datetime ) { |
|
547 | + if ($datetime instanceof EE_Datetime) { |
|
548 | 548 | $event = $datetime->event(); |
549 | 549 | } |
550 | 550 | } |
@@ -559,11 +559,11 @@ discard block |
||
559 | 559 | * @param string $time_format |
560 | 560 | * @return string |
561 | 561 | */ |
562 | - public function ticket_datetime_start( $date_format = '', $time_format = '' ) { |
|
563 | - $first_datetime_string = __( "Unknown", "event_espresso" ); |
|
562 | + public function ticket_datetime_start($date_format = '', $time_format = '') { |
|
563 | + $first_datetime_string = __("Unknown", "event_espresso"); |
|
564 | 564 | $datetime = $this->get_ticket_datetime(); |
565 | - if ( $datetime ) { |
|
566 | - $first_datetime_string = $datetime->start_date_and_time( $date_format, $time_format ); |
|
565 | + if ($datetime) { |
|
566 | + $first_datetime_string = $datetime->start_date_and_time($date_format, $time_format); |
|
567 | 567 | } |
568 | 568 | return $first_datetime_string; |
569 | 569 | } |
@@ -578,26 +578,26 @@ discard block |
||
578 | 578 | * @return bool success |
579 | 579 | * @throws \EE_Error |
580 | 580 | */ |
581 | - public function add_child_line_item( EEI_Line_Item $line_item, $set_order = true ) { |
|
581 | + public function add_child_line_item(EEI_Line_Item $line_item, $set_order = true) { |
|
582 | 582 | // should we calculate the LIN_order for this line item ? |
583 | - if ( $set_order || $line_item->order() === null ) { |
|
584 | - $line_item->set_order( count( $this->children() ) ); |
|
583 | + if ($set_order || $line_item->order() === null) { |
|
584 | + $line_item->set_order(count($this->children())); |
|
585 | 585 | } |
586 | - if ( $this->ID() ) { |
|
586 | + if ($this->ID()) { |
|
587 | 587 | //check for any duplicate line items (with the same code), if so, this replaces it |
588 | - $line_item_with_same_code = $this->get_child_line_item( $line_item->code() ); |
|
589 | - if( $line_item_with_same_code instanceof EE_Line_Item && $line_item_with_same_code !== $line_item ) { |
|
590 | - $this->delete_child_line_item( $line_item_with_same_code->code() ); |
|
588 | + $line_item_with_same_code = $this->get_child_line_item($line_item->code()); |
|
589 | + if ($line_item_with_same_code instanceof EE_Line_Item && $line_item_with_same_code !== $line_item) { |
|
590 | + $this->delete_child_line_item($line_item_with_same_code->code()); |
|
591 | 591 | } |
592 | - $line_item->set_parent_ID( $this->ID() ); |
|
593 | - if( $this->TXN_ID() ){ |
|
594 | - $line_item->set_TXN_ID( $this->TXN_ID() ); |
|
592 | + $line_item->set_parent_ID($this->ID()); |
|
593 | + if ($this->TXN_ID()) { |
|
594 | + $line_item->set_TXN_ID($this->TXN_ID()); |
|
595 | 595 | } |
596 | 596 | return $line_item->save(); |
597 | 597 | } else { |
598 | - $this->_children[ $line_item->code() ] = $line_item; |
|
599 | - if( $line_item->parent() != $this ) { |
|
600 | - $line_item->set_parent( $this ); |
|
598 | + $this->_children[$line_item->code()] = $line_item; |
|
599 | + if ($line_item->parent() != $this) { |
|
600 | + $line_item->set_parent($this); |
|
601 | 601 | } |
602 | 602 | return TRUE; |
603 | 603 | } |
@@ -611,16 +611,16 @@ discard block |
||
611 | 611 | * @param EE_Line_Item $line_item |
612 | 612 | * |
613 | 613 | */ |
614 | - public function set_parent( $line_item ) { |
|
615 | - if ( $this->ID() ) { |
|
616 | - if( ! $line_item->ID() ) { |
|
614 | + public function set_parent($line_item) { |
|
615 | + if ($this->ID()) { |
|
616 | + if ( ! $line_item->ID()) { |
|
617 | 617 | $line_item->save(); |
618 | 618 | } |
619 | - $this->set_parent_ID( $line_item->ID() ); |
|
619 | + $this->set_parent_ID($line_item->ID()); |
|
620 | 620 | $this->save(); |
621 | 621 | } else { |
622 | 622 | $this->_parent = $line_item; |
623 | - $this->set_parent_ID( $line_item->ID() ); |
|
623 | + $this->set_parent_ID($line_item->ID()); |
|
624 | 624 | } |
625 | 625 | } |
626 | 626 | |
@@ -633,11 +633,11 @@ discard block |
||
633 | 633 | * @param string $code |
634 | 634 | * @return EE_Line_Item |
635 | 635 | */ |
636 | - public function get_child_line_item( $code ) { |
|
637 | - if ( $this->ID() ) { |
|
638 | - return $this->get_model()->get_one( array( array( 'LIN_parent' => $this->ID(), 'LIN_code' => $code ) ) ); |
|
636 | + public function get_child_line_item($code) { |
|
637 | + if ($this->ID()) { |
|
638 | + return $this->get_model()->get_one(array(array('LIN_parent' => $this->ID(), 'LIN_code' => $code))); |
|
639 | 639 | } else { |
640 | - return isset( $this->_children[ $code ] ) ? $this->_children[ $code ] : null; |
|
640 | + return isset($this->_children[$code]) ? $this->_children[$code] : null; |
|
641 | 641 | } |
642 | 642 | } |
643 | 643 | |
@@ -648,10 +648,10 @@ discard block |
||
648 | 648 | * @return int |
649 | 649 | */ |
650 | 650 | public function delete_children_line_items() { |
651 | - if ( $this->ID() ) { |
|
652 | - return $this->get_model()->delete( array( array( 'LIN_parent' => $this->ID() ) ) ); |
|
651 | + if ($this->ID()) { |
|
652 | + return $this->get_model()->delete(array(array('LIN_parent' => $this->ID()))); |
|
653 | 653 | } else { |
654 | - $count = count( $this->_children ); |
|
654 | + $count = count($this->_children); |
|
655 | 655 | $this->_children = array(); |
656 | 656 | return $count; |
657 | 657 | } |
@@ -668,25 +668,25 @@ discard block |
||
668 | 668 | * @param bool $stop_search_once_found |
669 | 669 | * @return int count of items deleted (or simply removed from the line item's cache, if not has not been saved to the DB yet) |
670 | 670 | */ |
671 | - public function delete_child_line_item( $code, $stop_search_once_found = true ) { |
|
672 | - if ( $this->ID() ) { |
|
671 | + public function delete_child_line_item($code, $stop_search_once_found = true) { |
|
672 | + if ($this->ID()) { |
|
673 | 673 | $items_deleted = 0; |
674 | - if( $this->code() == $code ) { |
|
675 | - $items_deleted += EEH_Line_Item::delete_all_child_items( $this ); |
|
674 | + if ($this->code() == $code) { |
|
675 | + $items_deleted += EEH_Line_Item::delete_all_child_items($this); |
|
676 | 676 | $items_deleted += (int) $this->delete(); |
677 | - if( $stop_search_once_found ){ |
|
677 | + if ($stop_search_once_found) { |
|
678 | 678 | return $items_deleted; |
679 | 679 | } |
680 | 680 | } |
681 | - foreach( $this->children() as $child_line_item ) { |
|
682 | - $items_deleted += $child_line_item->delete_child_line_item( $code, $stop_search_once_found ); |
|
681 | + foreach ($this->children() as $child_line_item) { |
|
682 | + $items_deleted += $child_line_item->delete_child_line_item($code, $stop_search_once_found); |
|
683 | 683 | } |
684 | 684 | return $items_deleted; |
685 | 685 | } else { |
686 | - if( isset( $this->_children[ $code ] ) ) { |
|
687 | - unset( $this->_children[ $code ] ); |
|
686 | + if (isset($this->_children[$code])) { |
|
687 | + unset($this->_children[$code]); |
|
688 | 688 | return 1; |
689 | - }else{ |
|
689 | + } else { |
|
690 | 690 | return 0; |
691 | 691 | } |
692 | 692 | } |
@@ -699,9 +699,9 @@ discard block |
||
699 | 699 | * @return boolean |
700 | 700 | */ |
701 | 701 | public function delete_if_childless_subtotal() { |
702 | - if( $this->ID() && |
|
702 | + if ($this->ID() && |
|
703 | 703 | $this->type() == EEM_Line_Item::type_sub_total && |
704 | - ! $this->children() ) { |
|
704 | + ! $this->children()) { |
|
705 | 705 | return $this->delete(); |
706 | 706 | } else { |
707 | 707 | return false; |
@@ -716,7 +716,7 @@ discard block |
||
716 | 716 | */ |
717 | 717 | public function generate_code() { |
718 | 718 | // each line item in the cart requires a unique identifier |
719 | - return md5( $this->get( 'OBJ_type' ) . $this->get( 'OBJ_ID' ) . microtime() ); |
|
719 | + return md5($this->get('OBJ_type').$this->get('OBJ_ID').microtime()); |
|
720 | 720 | } |
721 | 721 | |
722 | 722 | |
@@ -799,7 +799,7 @@ discard block |
||
799 | 799 | * @return string like '2, 004.00', formatted according to the localized currency |
800 | 800 | */ |
801 | 801 | public function unit_price_no_code() { |
802 | - return $this->get_pretty( 'LIN_unit_price', 'no_currency_code' ); |
|
802 | + return $this->get_pretty('LIN_unit_price', 'no_currency_code'); |
|
803 | 803 | } |
804 | 804 | |
805 | 805 | |
@@ -809,7 +809,7 @@ discard block |
||
809 | 809 | * @return string like '2, 004.00', formatted according to the localized currency |
810 | 810 | */ |
811 | 811 | public function total_no_code() { |
812 | - return $this->get_pretty( 'LIN_total', 'no_currency_code' ); |
|
812 | + return $this->get_pretty('LIN_total', 'no_currency_code'); |
|
813 | 813 | } |
814 | 814 | |
815 | 815 | |
@@ -829,17 +829,17 @@ discard block |
||
829 | 829 | $tax_total = $this->recalculate_taxes_and_tax_total(); |
830 | 830 | $total = $pre_tax_total + $tax_total; |
831 | 831 | // no negative totals plz |
832 | - $total = max( $total, 0 ); |
|
833 | - $this->set_total( $total ); |
|
832 | + $total = max($total, 0); |
|
833 | + $this->set_total($total); |
|
834 | 834 | //only update the related transaction's total |
835 | 835 | //if we intend to save this line item and its a grand total |
836 | - if( |
|
836 | + if ( |
|
837 | 837 | $this->allow_persist() && |
838 | 838 | $this->type() === EEM_Line_Item::type_total && |
839 | 839 | $this->transaction() instanceof EE_Transaction |
840 | - ){ |
|
841 | - $this->transaction()->set_total( $total ); |
|
842 | - if ( $this->transaction()->ID() ) { |
|
840 | + ) { |
|
841 | + $this->transaction()->set_total($total); |
|
842 | + if ($this->transaction()->ID()) { |
|
843 | 843 | $this->transaction()->save(); |
844 | 844 | } |
845 | 845 | } |
@@ -858,47 +858,47 @@ discard block |
||
858 | 858 | public function recalculate_pre_tax_total() { |
859 | 859 | $total = 0; |
860 | 860 | $my_children = $this->children(); |
861 | - $has_children = ! empty( $my_children ); |
|
862 | - if ( $has_children && $this->is_line_item() ) { |
|
863 | - $total = $this->_recalculate_pretax_total_for_line_item( $total, $my_children ); |
|
864 | - } elseif ( ! $has_children && ( $this->is_sub_line_item() || $this->is_line_item() ) ) { |
|
861 | + $has_children = ! empty($my_children); |
|
862 | + if ($has_children && $this->is_line_item()) { |
|
863 | + $total = $this->_recalculate_pretax_total_for_line_item($total, $my_children); |
|
864 | + } elseif ( ! $has_children && ($this->is_sub_line_item() || $this->is_line_item())) { |
|
865 | 865 | $total = $this->unit_price() * $this->quantity(); |
866 | - } elseif( $this->is_sub_total() || $this->is_total() ) { |
|
867 | - $total = $this->_recalculate_pretax_total_for_subtotal( $total, $my_children ); |
|
868 | - } elseif ( $this->is_tax_sub_total() || $this->is_tax() || $this->is_cancelled() ) { |
|
866 | + } elseif ($this->is_sub_total() || $this->is_total()) { |
|
867 | + $total = $this->_recalculate_pretax_total_for_subtotal($total, $my_children); |
|
868 | + } elseif ($this->is_tax_sub_total() || $this->is_tax() || $this->is_cancelled()) { |
|
869 | 869 | // completely ignore tax totals, tax sub-totals, and cancelled line items, when calculating the pre-tax-total |
870 | 870 | return 0; |
871 | 871 | } |
872 | 872 | // ensure all non-line items and non-sub-line-items have a quantity of 1 (except for Events) |
873 | - if( |
|
873 | + if ( |
|
874 | 874 | ! $this->is_line_item() && |
875 | 875 | ! $this->is_sub_line_item() && |
876 | 876 | ! $this->is_cancellation() |
877 | 877 | ) { |
878 | - if ( $this->OBJ_type() !== 'Event' ) { |
|
879 | - $this->set_quantity( 1 ); |
|
878 | + if ($this->OBJ_type() !== 'Event') { |
|
879 | + $this->set_quantity(1); |
|
880 | 880 | } |
881 | - if( ! $this->is_percent() ) { |
|
882 | - $this->set_unit_price( $this->total() ); |
|
881 | + if ( ! $this->is_percent()) { |
|
882 | + $this->set_unit_price($this->total()); |
|
883 | 883 | } |
884 | 884 | } |
885 | 885 | |
886 | 886 | //we don't want to bother saving grand totals, because that needs to factor in taxes anyways |
887 | 887 | //so it ought to be |
888 | - if( ! $this->is_total() ) { |
|
889 | - $this->set_total( $total ); |
|
888 | + if ( ! $this->is_total()) { |
|
889 | + $this->set_total($total); |
|
890 | 890 | //if not a percent line item, make sure we keep the unit price in sync |
891 | - if( |
|
891 | + if ( |
|
892 | 892 | $has_children |
893 | 893 | && $this->is_line_item() |
894 | 894 | && ! $this->is_percent() |
895 | 895 | ) { |
896 | - if( $this->quantity() === 0 ){ |
|
896 | + if ($this->quantity() === 0) { |
|
897 | 897 | $new_unit_price = 0; |
898 | 898 | } else { |
899 | 899 | $new_unit_price = $this->total() / $this->quantity(); |
900 | 900 | } |
901 | - $this->set_unit_price( $new_unit_price ); |
|
901 | + $this->set_unit_price($new_unit_price); |
|
902 | 902 | } |
903 | 903 | $this->maybe_save(); |
904 | 904 | } |
@@ -918,39 +918,39 @@ discard block |
||
918 | 918 | * @return float |
919 | 919 | * @throws \EE_Error |
920 | 920 | */ |
921 | - protected function _recalculate_pretax_total_for_subtotal( $calculated_total_so_far, $my_children = null ) { |
|
922 | - if( $my_children === null ) { |
|
921 | + protected function _recalculate_pretax_total_for_subtotal($calculated_total_so_far, $my_children = null) { |
|
922 | + if ($my_children === null) { |
|
923 | 923 | $my_children = $this->children(); |
924 | 924 | } |
925 | 925 | //get the total of all its children |
926 | - foreach ( $my_children as $child_line_item ) { |
|
927 | - if ( $child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation() ) { |
|
926 | + foreach ($my_children as $child_line_item) { |
|
927 | + if ($child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation()) { |
|
928 | 928 | // percentage line items are based on total so far |
929 | - if ( $child_line_item->is_percent() ) { |
|
929 | + if ($child_line_item->is_percent()) { |
|
930 | 930 | //round as we go so that the line items add up ok |
931 | 931 | $percent_total = round( |
932 | 932 | $calculated_total_so_far * $child_line_item->percent() / 100, |
933 | 933 | EE_Registry::instance()->CFG->currency->dec_plc |
934 | 934 | ); |
935 | - $child_line_item->set_total( $percent_total ); |
|
935 | + $child_line_item->set_total($percent_total); |
|
936 | 936 | //so far all percent line items should have a quantity of 1 |
937 | 937 | //(ie, no double percent discounts. Although that might be requested someday) |
938 | - $child_line_item->set_quantity( 1 ); |
|
938 | + $child_line_item->set_quantity(1); |
|
939 | 939 | $child_line_item->maybe_save(); |
940 | 940 | $calculated_total_so_far += $percent_total; |
941 | 941 | } else { |
942 | 942 | //verify flat sub-line-item quantities match their parent |
943 | - if( $child_line_item->is_sub_line_item() ) { |
|
944 | - $child_line_item->set_quantity( $this->quantity() ); |
|
943 | + if ($child_line_item->is_sub_line_item()) { |
|
944 | + $child_line_item->set_quantity($this->quantity()); |
|
945 | 945 | } |
946 | 946 | $calculated_total_so_far += $child_line_item->recalculate_pre_tax_total(); |
947 | 947 | } |
948 | 948 | } |
949 | 949 | } |
950 | 950 | |
951 | - if( $this->is_sub_total() ){ |
|
951 | + if ($this->is_sub_total()) { |
|
952 | 952 | // no negative totals plz |
953 | - $calculated_total_so_far = max( $calculated_total_so_far, 0 ); |
|
953 | + $calculated_total_so_far = max($calculated_total_so_far, 0); |
|
954 | 954 | } |
955 | 955 | return $calculated_total_so_far; |
956 | 956 | } |
@@ -968,8 +968,8 @@ discard block |
||
968 | 968 | * @return float |
969 | 969 | * @throws \EE_Error |
970 | 970 | */ |
971 | - protected function _recalculate_pretax_total_for_line_item( $calculated_total_so_far, $my_children = null ) { |
|
972 | - if( $my_children === null ) { |
|
971 | + protected function _recalculate_pretax_total_for_line_item($calculated_total_so_far, $my_children = null) { |
|
972 | + if ($my_children === null) { |
|
973 | 973 | $my_children = $this->children(); |
974 | 974 | } |
975 | 975 | //we need to keep track of the running total for a single item, |
@@ -977,10 +977,10 @@ discard block |
||
977 | 977 | $unit_price_for_total = 0; |
978 | 978 | $quantity_for_total = 1; |
979 | 979 | //get the total of all its children |
980 | - foreach ( $my_children as $child_line_item ) { |
|
981 | - if ( $child_line_item instanceof EE_Line_Item && |
|
980 | + foreach ($my_children as $child_line_item) { |
|
981 | + if ($child_line_item instanceof EE_Line_Item && |
|
982 | 982 | ! $child_line_item->is_cancellation()) { |
983 | - if ( $child_line_item->is_percent() ) { |
|
983 | + if ($child_line_item->is_percent()) { |
|
984 | 984 | //it should be the unit-price-so-far multiplied by teh percent multiplied by the quantity |
985 | 985 | //not total multiplied by percent, because that ignores rounding along-the-way |
986 | 986 | $percent_unit_price = round( |
@@ -988,17 +988,17 @@ discard block |
||
988 | 988 | EE_Registry::instance()->CFG->currency->dec_plc |
989 | 989 | ); |
990 | 990 | $percent_total = $percent_unit_price * $quantity_for_total; |
991 | - $child_line_item->set_total( $percent_total ); |
|
991 | + $child_line_item->set_total($percent_total); |
|
992 | 992 | //so far all percent line items should have a quantity of 1 |
993 | 993 | //(ie, no double percent discounts. Although that might be requested someday) |
994 | - $child_line_item->set_quantity( 1 ); |
|
994 | + $child_line_item->set_quantity(1); |
|
995 | 995 | $child_line_item->maybe_save(); |
996 | 996 | $calculated_total_so_far += $percent_total; |
997 | 997 | $unit_price_for_total += $percent_unit_price; |
998 | 998 | } else { |
999 | 999 | //verify flat sub-line-item quantities match their parent |
1000 | - if( $child_line_item->is_sub_line_item() ) { |
|
1001 | - $child_line_item->set_quantity( $this->quantity() ); |
|
1000 | + if ($child_line_item->is_sub_line_item()) { |
|
1001 | + $child_line_item->set_quantity($this->quantity()); |
|
1002 | 1002 | } |
1003 | 1003 | $quantity_for_total = $child_line_item->quantity(); |
1004 | 1004 | $calculated_total_so_far += $child_line_item->recalculate_pre_tax_total(); |
@@ -1022,10 +1022,10 @@ discard block |
||
1022 | 1022 | //calculate the pretax total |
1023 | 1023 | $taxable_total = $this->taxable_total(); |
1024 | 1024 | $tax_total = 0; |
1025 | - foreach ( $taxes as $tax ) { |
|
1025 | + foreach ($taxes as $tax) { |
|
1026 | 1026 | $total_on_this_tax = $taxable_total * $tax->percent() / 100; |
1027 | 1027 | //remember the total on this line item |
1028 | - $tax->set_total( $total_on_this_tax ); |
|
1028 | + $tax->set_total($total_on_this_tax); |
|
1029 | 1029 | $tax_total += $tax->total(); |
1030 | 1030 | } |
1031 | 1031 | $this->_recalculate_tax_sub_total(); |
@@ -1039,21 +1039,21 @@ discard block |
||
1039 | 1039 | * @return void |
1040 | 1040 | */ |
1041 | 1041 | private function _recalculate_tax_sub_total() { |
1042 | - if ( $this->is_tax_sub_total() ) { |
|
1042 | + if ($this->is_tax_sub_total()) { |
|
1043 | 1043 | $total = 0; |
1044 | 1044 | $total_percent = 0; |
1045 | 1045 | //simply loop through all its children (which should be taxes) and sum their total |
1046 | - foreach ( $this->children() as $child_tax ) { |
|
1047 | - if ( $child_tax instanceof EE_Line_Item ) { |
|
1046 | + foreach ($this->children() as $child_tax) { |
|
1047 | + if ($child_tax instanceof EE_Line_Item) { |
|
1048 | 1048 | $total += $child_tax->total(); |
1049 | 1049 | $total_percent += $child_tax->percent(); |
1050 | 1050 | } |
1051 | 1051 | } |
1052 | - $this->set_total( $total ); |
|
1053 | - $this->set_percent( $total_percent ); |
|
1054 | - } elseif ( $this->is_total() ) { |
|
1055 | - foreach ( $this->children() as $maybe_tax_subtotal ) { |
|
1056 | - if ( $maybe_tax_subtotal instanceof EE_Line_Item ) { |
|
1052 | + $this->set_total($total); |
|
1053 | + $this->set_percent($total_percent); |
|
1054 | + } elseif ($this->is_total()) { |
|
1055 | + foreach ($this->children() as $maybe_tax_subtotal) { |
|
1056 | + if ($maybe_tax_subtotal instanceof EE_Line_Item) { |
|
1057 | 1057 | $maybe_tax_subtotal->_recalculate_tax_sub_total(); |
1058 | 1058 | } |
1059 | 1059 | } |
@@ -1069,8 +1069,8 @@ discard block |
||
1069 | 1069 | public function get_total_tax() { |
1070 | 1070 | $this->_recalculate_tax_sub_total(); |
1071 | 1071 | $total = 0; |
1072 | - foreach ( $this->tax_descendants() as $tax_line_item ) { |
|
1073 | - if ( $tax_line_item instanceof EE_Line_Item ) { |
|
1072 | + foreach ($this->tax_descendants() as $tax_line_item) { |
|
1073 | + if ($tax_line_item instanceof EE_Line_Item) { |
|
1074 | 1074 | $total += $tax_line_item->total(); |
1075 | 1075 | } |
1076 | 1076 | } |
@@ -1084,15 +1084,15 @@ discard block |
||
1084 | 1084 | */ |
1085 | 1085 | public function get_items_total() { |
1086 | 1086 | //by default, let's make sure we're consistent with the existing line item |
1087 | - if( $this->is_total() ) { |
|
1088 | - $pretax_subtotal_li = EEH_Line_Item::get_pre_tax_subtotal( $this ); |
|
1089 | - if( $pretax_subtotal_li instanceof EE_Line_Item ) { |
|
1087 | + if ($this->is_total()) { |
|
1088 | + $pretax_subtotal_li = EEH_Line_Item::get_pre_tax_subtotal($this); |
|
1089 | + if ($pretax_subtotal_li instanceof EE_Line_Item) { |
|
1090 | 1090 | return $pretax_subtotal_li->total(); |
1091 | 1091 | } |
1092 | 1092 | } |
1093 | 1093 | $total = 0; |
1094 | - foreach ( $this->get_items() as $item ) { |
|
1095 | - if ( $item instanceof EE_Line_Item ) { |
|
1094 | + foreach ($this->get_items() as $item) { |
|
1095 | + if ($item instanceof EE_Line_Item) { |
|
1096 | 1096 | $total += $item->total(); |
1097 | 1097 | } |
1098 | 1098 | } |
@@ -1107,7 +1107,7 @@ discard block |
||
1107 | 1107 | * @return EE_Line_Item[] |
1108 | 1108 | */ |
1109 | 1109 | public function tax_descendants() { |
1110 | - return EEH_Line_Item::get_tax_descendants( $this ); |
|
1110 | + return EEH_Line_Item::get_tax_descendants($this); |
|
1111 | 1111 | } |
1112 | 1112 | |
1113 | 1113 | |
@@ -1117,7 +1117,7 @@ discard block |
||
1117 | 1117 | * @return EE_Line_Item[] |
1118 | 1118 | */ |
1119 | 1119 | public function get_items() { |
1120 | - return EEH_Line_Item::get_line_item_descendants( $this ); |
|
1120 | + return EEH_Line_Item::get_line_item_descendants($this); |
|
1121 | 1121 | } |
1122 | 1122 | |
1123 | 1123 | |
@@ -1133,22 +1133,22 @@ discard block |
||
1133 | 1133 | */ |
1134 | 1134 | public function taxable_total() { |
1135 | 1135 | $total = 0; |
1136 | - if ( $this->children() ) { |
|
1137 | - foreach ( $this->children() as $child_line_item ) { |
|
1138 | - if ( $child_line_item->type() === EEM_Line_Item::type_line_item && $child_line_item->is_taxable()) { |
|
1136 | + if ($this->children()) { |
|
1137 | + foreach ($this->children() as $child_line_item) { |
|
1138 | + if ($child_line_item->type() === EEM_Line_Item::type_line_item && $child_line_item->is_taxable()) { |
|
1139 | 1139 | //if it's a percent item, only take into account the percent |
1140 | 1140 | //that's taxable too (the taxable total so far) |
1141 | - if( $child_line_item->is_percent() ) { |
|
1142 | - $total = $total + ( $total * $child_line_item->percent() / 100 ); |
|
1143 | - }else{ |
|
1141 | + if ($child_line_item->is_percent()) { |
|
1142 | + $total = $total + ($total * $child_line_item->percent() / 100); |
|
1143 | + } else { |
|
1144 | 1144 | $total += $child_line_item->total(); |
1145 | 1145 | } |
1146 | - }elseif( $child_line_item->type() === EEM_Line_Item::type_sub_total ){ |
|
1146 | + }elseif ($child_line_item->type() === EEM_Line_Item::type_sub_total) { |
|
1147 | 1147 | $total += $child_line_item->taxable_total(); |
1148 | 1148 | } |
1149 | 1149 | } |
1150 | 1150 | } |
1151 | - return max( $total, 0 ); |
|
1151 | + return max($total, 0); |
|
1152 | 1152 | } |
1153 | 1153 | |
1154 | 1154 | |
@@ -1158,7 +1158,7 @@ discard block |
||
1158 | 1158 | * @return EE_Transaction |
1159 | 1159 | */ |
1160 | 1160 | public function transaction() { |
1161 | - return $this->get_first_related( 'Transaction' ); |
|
1161 | + return $this->get_first_related('Transaction'); |
|
1162 | 1162 | } |
1163 | 1163 | |
1164 | 1164 | |
@@ -1171,18 +1171,18 @@ discard block |
||
1171 | 1171 | * @param int $txn_id if none is provided, assumes $this->TXN_ID() |
1172 | 1172 | * @return int count of items saved |
1173 | 1173 | */ |
1174 | - public function save_this_and_descendants_to_txn( $txn_id = NULL ) { |
|
1174 | + public function save_this_and_descendants_to_txn($txn_id = NULL) { |
|
1175 | 1175 | $count = 0; |
1176 | - if ( ! $txn_id ) { |
|
1176 | + if ( ! $txn_id) { |
|
1177 | 1177 | $txn_id = $this->TXN_ID(); |
1178 | 1178 | } |
1179 | - $this->set_TXN_ID( $txn_id ); |
|
1179 | + $this->set_TXN_ID($txn_id); |
|
1180 | 1180 | $children = $this->children(); |
1181 | 1181 | $count += $this->save() ? 1 : 0; |
1182 | - foreach ( $children as $child_line_item ) { |
|
1183 | - if ( $child_line_item instanceof EE_Line_Item ) { |
|
1184 | - $child_line_item->set_parent_ID( $this->ID() ); |
|
1185 | - $count += $child_line_item->save_this_and_descendants_to_txn( $txn_id ); |
|
1182 | + foreach ($children as $child_line_item) { |
|
1183 | + if ($child_line_item instanceof EE_Line_Item) { |
|
1184 | + $child_line_item->set_parent_ID($this->ID()); |
|
1185 | + $count += $child_line_item->save_this_and_descendants_to_txn($txn_id); |
|
1186 | 1186 | } |
1187 | 1187 | } |
1188 | 1188 | return $count; |
@@ -1199,9 +1199,9 @@ discard block |
||
1199 | 1199 | $count = 0; |
1200 | 1200 | $children = $this->children(); |
1201 | 1201 | $count += $this->save() ? 1 : 0; |
1202 | - foreach ( $children as $child_line_item ) { |
|
1203 | - if ( $child_line_item instanceof EE_Line_Item ) { |
|
1204 | - $child_line_item->set_parent_ID( $this->ID() ); |
|
1202 | + foreach ($children as $child_line_item) { |
|
1203 | + if ($child_line_item instanceof EE_Line_Item) { |
|
1204 | + $child_line_item->set_parent_ID($this->ID()); |
|
1205 | 1205 | $count += $child_line_item->save_this_and_descendants(); |
1206 | 1206 | } |
1207 | 1207 | } |
@@ -1215,8 +1215,8 @@ discard block |
||
1215 | 1215 | * @return EE_Line_Item[] |
1216 | 1216 | */ |
1217 | 1217 | public function get_cancellations() { |
1218 | - EE_Registry::instance()->load_helper( 'Line_Item' ); |
|
1219 | - return EEH_Line_Item::get_descendants_of_type( $this, EEM_Line_Item::type_cancellation ); |
|
1218 | + EE_Registry::instance()->load_helper('Line_Item'); |
|
1219 | + return EEH_Line_Item::get_descendants_of_type($this, EEM_Line_Item::type_cancellation); |
|
1220 | 1220 | } |
1221 | 1221 | |
1222 | 1222 | |
@@ -1227,7 +1227,7 @@ discard block |
||
1227 | 1227 | * @return int count of items saved |
1228 | 1228 | */ |
1229 | 1229 | public function maybe_save() { |
1230 | - if ( $this->ID() ) { |
|
1230 | + if ($this->ID()) { |
|
1231 | 1231 | return $this->save(); |
1232 | 1232 | } |
1233 | 1233 | return false; |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | * Sets quantity |
173 | 173 | * @param int $quantity |
174 | 174 | */ |
175 | - public function set_quantity( $quantity ) { |
|
175 | + public function set_quantity( $quantity ) { |
|
176 | 176 | $this->set( 'LIN_quantity', max( $quantity, 0 ) ); |
177 | 177 | } |
178 | 178 | |
@@ -212,25 +212,25 @@ discard block |
||
212 | 212 | * Gets item_type |
213 | 213 | * @return string |
214 | 214 | */ |
215 | - public function OBJ_type_i18n() { |
|
216 | - $obj_type = $this->OBJ_type(); |
|
217 | - switch ($obj_type) { |
|
218 | - case 'Event': |
|
219 | - $obj_type = __('Event', 'event_espresso'); |
|
220 | - break; |
|
221 | - case 'Price': |
|
222 | - $obj_type = __('Price', 'event_espresso'); |
|
223 | - break; |
|
224 | - case 'Promotion': |
|
225 | - $obj_type = __('Promotion', 'event_espresso'); |
|
226 | - break; |
|
227 | - case 'Ticket': |
|
228 | - $obj_type = __('Ticket', 'event_espresso'); |
|
229 | - break; |
|
230 | - case 'Transaction': |
|
231 | - $obj_type = __('Transaction', 'event_espresso'); |
|
232 | - break; |
|
233 | - } |
|
215 | + public function OBJ_type_i18n() { |
|
216 | + $obj_type = $this->OBJ_type(); |
|
217 | + switch ($obj_type) { |
|
218 | + case 'Event': |
|
219 | + $obj_type = __('Event', 'event_espresso'); |
|
220 | + break; |
|
221 | + case 'Price': |
|
222 | + $obj_type = __('Price', 'event_espresso'); |
|
223 | + break; |
|
224 | + case 'Promotion': |
|
225 | + $obj_type = __('Promotion', 'event_espresso'); |
|
226 | + break; |
|
227 | + case 'Ticket': |
|
228 | + $obj_type = __('Ticket', 'event_espresso'); |
|
229 | + break; |
|
230 | + case 'Transaction': |
|
231 | + $obj_type = __('Transaction', 'event_espresso'); |
|
232 | + break; |
|
233 | + } |
|
234 | 234 | return apply_filters('FHEE__EE_Line_Item__OBJ_type_i18n', $obj_type, $this); |
235 | 235 | } |
236 | 236 | |
@@ -1244,48 +1244,48 @@ discard block |
||
1244 | 1244 | |
1245 | 1245 | |
1246 | 1246 | |
1247 | - /** |
|
1248 | - * @param bool $raw |
|
1249 | - * @return int |
|
1250 | - * @throws \EE_Error |
|
1251 | - */ |
|
1252 | - public function timestamp($raw = false) |
|
1253 | - { |
|
1254 | - return $raw ? $this->get_raw('LIN_timestamp') : $this->get('LIN_timestamp'); |
|
1255 | - } |
|
1247 | + /** |
|
1248 | + * @param bool $raw |
|
1249 | + * @return int |
|
1250 | + * @throws \EE_Error |
|
1251 | + */ |
|
1252 | + public function timestamp($raw = false) |
|
1253 | + { |
|
1254 | + return $raw ? $this->get_raw('LIN_timestamp') : $this->get('LIN_timestamp'); |
|
1255 | + } |
|
1256 | 1256 | |
1257 | 1257 | |
1258 | 1258 | |
1259 | 1259 | |
1260 | - /************************* DEPRECATED *************************/ |
|
1260 | + /************************* DEPRECATED *************************/ |
|
1261 | 1261 | |
1262 | 1262 | |
1263 | 1263 | |
1264 | - /** |
|
1265 | - * @deprecated 4.6.0 |
|
1266 | - * @param string $type one of the constants on EEM_Line_Item |
|
1267 | - * @return EE_Line_Item[] |
|
1268 | - */ |
|
1269 | - protected function _get_descendants_of_type($type) |
|
1270 | - { |
|
1271 | - EE_Error::doing_it_wrong('EE_Line_Item::_get_descendants_of_type()', |
|
1272 | - __('Method replaced with EEH_Line_Item::get_descendants_of_type()', 'event_espresso'), '4.6.0'); |
|
1273 | - return EEH_Line_Item::get_descendants_of_type($this, $type); |
|
1274 | - } |
|
1264 | + /** |
|
1265 | + * @deprecated 4.6.0 |
|
1266 | + * @param string $type one of the constants on EEM_Line_Item |
|
1267 | + * @return EE_Line_Item[] |
|
1268 | + */ |
|
1269 | + protected function _get_descendants_of_type($type) |
|
1270 | + { |
|
1271 | + EE_Error::doing_it_wrong('EE_Line_Item::_get_descendants_of_type()', |
|
1272 | + __('Method replaced with EEH_Line_Item::get_descendants_of_type()', 'event_espresso'), '4.6.0'); |
|
1273 | + return EEH_Line_Item::get_descendants_of_type($this, $type); |
|
1274 | + } |
|
1275 | 1275 | |
1276 | 1276 | |
1277 | 1277 | |
1278 | - /** |
|
1279 | - * @deprecated 4.6.0 |
|
1280 | - * @param string $type like one of the EEM_Line_Item::type_* |
|
1281 | - * @return EE_Line_Item |
|
1282 | - */ |
|
1283 | - public function get_nearest_descendant_of_type($type) |
|
1284 | - { |
|
1285 | - EE_Error::doing_it_wrong('EE_Line_Item::get_nearest_descendant_of_type()', |
|
1286 | - __('Method replaced with EEH_Line_Item::get_nearest_descendant_of_type()', 'event_espresso'), '4.6.0'); |
|
1287 | - return EEH_Line_Item::get_nearest_descendant_of_type($this, $type); |
|
1288 | - } |
|
1278 | + /** |
|
1279 | + * @deprecated 4.6.0 |
|
1280 | + * @param string $type like one of the EEM_Line_Item::type_* |
|
1281 | + * @return EE_Line_Item |
|
1282 | + */ |
|
1283 | + public function get_nearest_descendant_of_type($type) |
|
1284 | + { |
|
1285 | + EE_Error::doing_it_wrong('EE_Line_Item::get_nearest_descendant_of_type()', |
|
1286 | + __('Method replaced with EEH_Line_Item::get_nearest_descendant_of_type()', 'event_espresso'), '4.6.0'); |
|
1287 | + return EEH_Line_Item::get_nearest_descendant_of_type($this, $type); |
|
1288 | + } |
|
1289 | 1289 | |
1290 | 1290 | |
1291 | 1291 |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | use EventEspresso\core\exceptions\UnexpectedEntityException; |
3 | 3 | |
4 | 4 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
5 | - exit('No direct script access allowed'); |
|
5 | + exit('No direct script access allowed'); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | |
@@ -21,952 +21,952 @@ discard block |
||
21 | 21 | class EED_Ticket_Sales_Monitor extends EED_Module |
22 | 22 | { |
23 | 23 | |
24 | - const debug = false; // true false |
|
25 | - |
|
26 | - /** |
|
27 | - * an array of raw ticket data from EED_Ticket_Selector |
|
28 | - * |
|
29 | - * @var array $ticket_selections |
|
30 | - */ |
|
31 | - protected $ticket_selections = array(); |
|
32 | - |
|
33 | - /** |
|
34 | - * the raw ticket data from EED_Ticket_Selector is organized in rows |
|
35 | - * according to how they are displayed in the actual Ticket_Selector |
|
36 | - * this tracks the current row being processed |
|
37 | - * |
|
38 | - * @var int $current_row |
|
39 | - */ |
|
40 | - protected $current_row = 0; |
|
41 | - |
|
42 | - /** |
|
43 | - * an array for tracking names of tickets that have sold out |
|
44 | - * |
|
45 | - * @var array $sold_out_tickets |
|
46 | - */ |
|
47 | - protected $sold_out_tickets = array(); |
|
48 | - |
|
49 | - /** |
|
50 | - * an array for tracking names of tickets that have had their quantities reduced |
|
51 | - * |
|
52 | - * @var array $decremented_tickets |
|
53 | - */ |
|
54 | - protected $decremented_tickets = array(); |
|
55 | - |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
60 | - * |
|
61 | - * @access public |
|
62 | - * @return void |
|
63 | - */ |
|
64 | - public static function set_hooks() |
|
65 | - { |
|
66 | - // release tickets for expired carts |
|
67 | - add_action('EED_Ticket_Selector__process_ticket_selections__before', |
|
68 | - array('EED_Ticket_Sales_Monitor', 'release_tickets_for_expired_carts'), |
|
69 | - 1 |
|
70 | - ); |
|
71 | - // check ticket reserves AFTER MER does it's check (hence priority 20) |
|
72 | - add_filter('FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty', |
|
73 | - array('EED_Ticket_Sales_Monitor', 'validate_ticket_sale'), |
|
74 | - 20, 3 |
|
75 | - ); |
|
76 | - // add notices for sold out tickets |
|
77 | - add_action('AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart', |
|
78 | - array('EED_Ticket_Sales_Monitor', 'post_notices'), |
|
79 | - 10 |
|
80 | - ); |
|
81 | - // handle ticket quantities adjusted in cart |
|
82 | - //add_action( |
|
83 | - // 'FHEE__EED_Multi_Event_Registration__adjust_line_item_quantity__line_item_quantity_updated', |
|
84 | - // array( 'EED_Ticket_Sales_Monitor', 'ticket_quantity_updated' ), |
|
85 | - // 10, 2 |
|
86 | - //); |
|
87 | - // handle tickets deleted from cart |
|
88 | - add_action( |
|
89 | - 'FHEE__EED_Multi_Event_Registration__delete_ticket__ticket_removed_from_cart', |
|
90 | - array('EED_Ticket_Sales_Monitor', 'ticket_removed_from_cart'), |
|
91 | - 10, 2 |
|
92 | - ); |
|
93 | - // handle emptied carts |
|
94 | - add_action( |
|
95 | - 'AHEE__EE_Session__reset_cart__before_reset', |
|
96 | - array('EED_Ticket_Sales_Monitor', 'session_cart_reset'), |
|
97 | - 10, 1 |
|
98 | - ); |
|
99 | - add_action( |
|
100 | - 'AHEE__EED_Multi_Event_Registration__empty_event_cart__before_delete_cart', |
|
101 | - array('EED_Ticket_Sales_Monitor', 'session_cart_reset'), |
|
102 | - 10, 1 |
|
103 | - ); |
|
104 | - // handle cancelled registrations |
|
105 | - add_action( |
|
106 | - 'AHEE__EE_Session__reset_checkout__before_reset', |
|
107 | - array('EED_Ticket_Sales_Monitor', 'session_checkout_reset'), |
|
108 | - 10, 1 |
|
109 | - ); |
|
110 | - // cron tasks |
|
111 | - add_action( |
|
112 | - 'AHEE__EE_Cron_Tasks__finalize_abandoned_transactions__abandoned_transaction', |
|
113 | - array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'), |
|
114 | - 10, 1 |
|
115 | - ); |
|
116 | - add_action( |
|
117 | - 'AHEE__EE_Cron_Tasks__process_expired_transactions__incomplete_transaction', |
|
118 | - array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'), |
|
119 | - 10, 1 |
|
120 | - ); |
|
121 | - add_action( |
|
122 | - 'AHEE__EE_Cron_Tasks__process_expired_transactions__failed_transaction', |
|
123 | - array('EED_Ticket_Sales_Monitor', 'process_failed_transactions'), |
|
124 | - 10, 1 |
|
125 | - ); |
|
126 | - } |
|
127 | - |
|
128 | - |
|
129 | - |
|
130 | - /** |
|
131 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
132 | - * |
|
133 | - * @access public |
|
134 | - * @return void |
|
135 | - */ |
|
136 | - public static function set_hooks_admin() |
|
137 | - { |
|
138 | - EED_Ticket_Sales_Monitor::set_hooks(); |
|
139 | - } |
|
140 | - |
|
141 | - |
|
142 | - |
|
143 | - /** |
|
144 | - * @return EED_Ticket_Sales_Monitor|EED_Module |
|
145 | - */ |
|
146 | - public static function instance() |
|
147 | - { |
|
148 | - return parent::get_instance(__CLASS__); |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * run |
|
155 | - * |
|
156 | - * @access public |
|
157 | - * @param WP_Query $WP_Query |
|
158 | - * @return void |
|
159 | - */ |
|
160 | - public function run($WP_Query) |
|
161 | - { |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - |
|
166 | - /********************************** PRE_TICKET_SALES **********************************/ |
|
167 | - |
|
168 | - |
|
169 | - |
|
170 | - /** |
|
171 | - * Retrieves grand totals from the line items that have no TXN ID |
|
172 | - * and timestamps less than the current time minus the session lifespan. |
|
173 | - * These are carts that have been abandoned before the "registrant" even attempted to checkout. |
|
174 | - * We're going to release the tickets for these line items before attempting to add more to the cart. |
|
175 | - * |
|
176 | - * @return void |
|
177 | - * @throws \EE_Error |
|
178 | - */ |
|
179 | - public static function release_tickets_for_expired_carts() |
|
180 | - { |
|
181 | - $expired_ticket_IDs = array(); |
|
182 | - $valid_ticket_line_items = array(); |
|
183 | - $total_line_items = EEM_Line_Item::instance()->get_total_line_items_with_no_transaction(); |
|
184 | - if(empty($total_line_items)){ |
|
185 | - return; |
|
186 | - } |
|
187 | - $expired = current_time('timestamp') - EE_Registry::instance()->SSN->lifespan(); |
|
188 | - foreach ($total_line_items as $total_line_item) { |
|
189 | - /** @var EE_Line_Item $total_line_item */ |
|
190 | - $ticket_line_items = EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total($total_line_item); |
|
191 | - foreach ($ticket_line_items as $ticket_line_item) { |
|
192 | - if(! $ticket_line_item instanceof EE_Line_Item) { |
|
193 | - continue; |
|
194 | - } |
|
195 | - if ($total_line_item->timestamp(true) <= $expired ) { |
|
196 | - $expired_ticket_IDs[$ticket_line_item->OBJ_ID()] = $ticket_line_item->OBJ_ID(); |
|
197 | - } else { |
|
198 | - $valid_ticket_line_items[$ticket_line_item->OBJ_ID()] = $ticket_line_item; |
|
199 | - } |
|
200 | - } |
|
201 | - } |
|
202 | - if (! empty($expired_ticket_IDs)) { |
|
203 | - EED_Ticket_Sales_Monitor::release_reservations_for_tickets( |
|
204 | - \EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs), |
|
205 | - $valid_ticket_line_items |
|
206 | - ); |
|
207 | - // let's get rid of expired line items so that they can't interfere with tracking |
|
208 | - add_action( |
|
209 | - 'shutdown', |
|
210 | - array('EED_Ticket_Sales_Monitor', 'clear_expired_line_items_with_no_transaction'), |
|
211 | - 999 |
|
212 | - ); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - |
|
217 | - |
|
218 | - /********************************** VALIDATE_TICKET_SALE **********************************/ |
|
219 | - |
|
220 | - |
|
221 | - |
|
222 | - /** |
|
223 | - * validate_ticket_sales |
|
224 | - * callback for 'FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data' |
|
225 | - * |
|
226 | - * @access public |
|
227 | - * @param int $qty |
|
228 | - * @param \EE_Ticket $ticket |
|
229 | - * @return bool |
|
230 | - * @throws UnexpectedEntityException |
|
231 | - * @throws EE_Error |
|
232 | - */ |
|
233 | - public static function validate_ticket_sale($qty = 1, EE_Ticket $ticket) |
|
234 | - { |
|
235 | - $qty = absint($qty); |
|
236 | - if ($qty > 0) { |
|
237 | - $qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty); |
|
238 | - } |
|
239 | - if (self::debug) { |
|
240 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '()'; |
|
241 | - echo '<br /><br /><b> RETURNED QTY: ' . $qty . '</b>'; |
|
242 | - } |
|
243 | - return $qty; |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * _validate_ticket_sale |
|
250 | - * checks whether an individual ticket is available for purchase based on datetime, and ticket details |
|
251 | - * |
|
252 | - * @access protected |
|
253 | - * @param \EE_Ticket $ticket |
|
254 | - * @param int $qty |
|
255 | - * @return int |
|
256 | - * @throws UnexpectedEntityException |
|
257 | - * @throws EE_Error |
|
258 | - */ |
|
259 | - protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1) |
|
260 | - { |
|
261 | - if (self::debug) { |
|
262 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
263 | - } |
|
264 | - if ( ! $ticket instanceof EE_Ticket) { |
|
265 | - return 0; |
|
266 | - } |
|
267 | - if (self::debug) { |
|
268 | - echo '<br /><b> . ticket->ID: ' . $ticket->ID() . '</b>'; |
|
269 | - echo '<br /> . original ticket->reserved: ' . $ticket->reserved(); |
|
270 | - } |
|
271 | - $ticket->refresh_from_db(); |
|
272 | - // first let's determine the ticket availability based on sales |
|
273 | - $available = $ticket->qty('saleable'); |
|
274 | - if (self::debug) { |
|
275 | - echo '<br /> . . . ticket->qty: ' . $ticket->qty(); |
|
276 | - echo '<br /> . . . ticket->sold: ' . $ticket->sold(); |
|
277 | - echo '<br /> . . . ticket->reserved: ' . $ticket->reserved(); |
|
278 | - echo '<br /> . . . ticket->qty(saleable): ' . $ticket->qty('saleable'); |
|
279 | - echo '<br /> . . . available: ' . $available; |
|
280 | - } |
|
281 | - if ($available < 1) { |
|
282 | - $this->_ticket_sold_out($ticket); |
|
283 | - return 0; |
|
284 | - } |
|
285 | - if (self::debug) { |
|
286 | - echo '<br /> . . . qty: ' . $qty; |
|
287 | - } |
|
288 | - if ($available < $qty) { |
|
289 | - $qty = $available; |
|
290 | - if (self::debug) { |
|
291 | - echo '<br /> . . . QTY ADJUSTED: ' . $qty; |
|
292 | - } |
|
293 | - $this->_ticket_quantity_decremented($ticket); |
|
294 | - } |
|
295 | - $this->_reserve_ticket($ticket, $qty); |
|
296 | - return $qty; |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - |
|
301 | - /** |
|
302 | - * _reserve_ticket |
|
303 | - * increments ticket reserved based on quantity passed |
|
304 | - * |
|
305 | - * @access protected |
|
306 | - * @param \EE_Ticket $ticket |
|
307 | - * @param int $quantity |
|
308 | - * @return bool |
|
309 | - * @throws EE_Error |
|
310 | - */ |
|
311 | - protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1) |
|
312 | - { |
|
313 | - if (self::debug) { |
|
314 | - echo '<br /><br /> . . . INCREASE RESERVED: ' . $quantity; |
|
315 | - } |
|
316 | - $ticket->increase_reserved($quantity); |
|
317 | - return $ticket->save(); |
|
318 | - } |
|
319 | - |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * _release_reserved_ticket |
|
324 | - * |
|
325 | - * @access protected |
|
326 | - * @param EE_Ticket $ticket |
|
327 | - * @param int $quantity |
|
328 | - * @return bool |
|
329 | - * @throws EE_Error |
|
330 | - */ |
|
331 | - protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1) |
|
332 | - { |
|
333 | - if (self::debug) { |
|
334 | - echo '<br /> . . . ticket->ID: ' . $ticket->ID(); |
|
335 | - echo '<br /> . . . ticket->reserved: ' . $ticket->reserved(); |
|
336 | - } |
|
337 | - $ticket->decrease_reserved($quantity); |
|
338 | - if (self::debug) { |
|
339 | - echo '<br /> . . . ticket->reserved: ' . $ticket->reserved(); |
|
340 | - } |
|
341 | - return $ticket->save() ? 1 : 0; |
|
342 | - } |
|
343 | - |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * _ticket_sold_out |
|
348 | - * removes quantities within the ticket selector based on zero ticket availability |
|
349 | - * |
|
350 | - * @access protected |
|
351 | - * @param \EE_Ticket $ticket |
|
352 | - * @return void |
|
353 | - * @throws UnexpectedEntityException |
|
354 | - * @throws EE_Error |
|
355 | - */ |
|
356 | - protected function _ticket_sold_out(EE_Ticket $ticket) |
|
357 | - { |
|
358 | - if (self::debug) { |
|
359 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
360 | - echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket); |
|
361 | - } |
|
362 | - $this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket); |
|
363 | - } |
|
364 | - |
|
365 | - |
|
366 | - |
|
367 | - /** |
|
368 | - * _ticket_quantity_decremented |
|
369 | - * adjusts quantities within the ticket selector based on decreased ticket availability |
|
370 | - * |
|
371 | - * @access protected |
|
372 | - * @param \EE_Ticket $ticket |
|
373 | - * @return void |
|
374 | - * @throws UnexpectedEntityException |
|
375 | - * @throws EE_Error |
|
376 | - */ |
|
377 | - protected function _ticket_quantity_decremented(EE_Ticket $ticket) |
|
378 | - { |
|
379 | - if (self::debug) { |
|
380 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
381 | - echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket); |
|
382 | - } |
|
383 | - $this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket); |
|
384 | - } |
|
385 | - |
|
386 | - |
|
387 | - |
|
388 | - /** |
|
389 | - * _get_ticket_and_event_name |
|
390 | - * builds string out of ticket and event name |
|
391 | - * |
|
392 | - * @access protected |
|
393 | - * @param \EE_Ticket $ticket |
|
394 | - * @return string |
|
395 | - * @throws UnexpectedEntityException |
|
396 | - * @throws EE_Error |
|
397 | - */ |
|
398 | - protected function _get_ticket_and_event_name(EE_Ticket $ticket) |
|
399 | - { |
|
400 | - $event = $ticket->get_related_event(); |
|
401 | - if ($event instanceof EE_Event) { |
|
402 | - $ticket_name = sprintf( |
|
403 | - _x('%1$s for %2$s', 'ticket name for event name', 'event_espresso'), |
|
404 | - $ticket->name(), |
|
405 | - $event->name() |
|
406 | - ); |
|
407 | - } else { |
|
408 | - $ticket_name = $ticket->name(); |
|
409 | - } |
|
410 | - return $ticket_name; |
|
411 | - } |
|
412 | - |
|
413 | - |
|
414 | - |
|
415 | - /********************************** EVENT CART **********************************/ |
|
416 | - |
|
417 | - |
|
418 | - |
|
419 | - /** |
|
420 | - * ticket_quantity_updated |
|
421 | - * releases or reserves ticket(s) based on quantity passed |
|
422 | - * |
|
423 | - * @access public |
|
424 | - * @param EE_Line_Item $line_item |
|
425 | - * @param int $quantity |
|
426 | - * @return void |
|
427 | - * @throws EE_Error |
|
428 | - */ |
|
429 | - public static function ticket_quantity_updated(EE_Line_Item $line_item, $quantity = 1) |
|
430 | - { |
|
431 | - $ticket = EEM_Ticket::instance()->get_one_by_ID(absint($line_item->OBJ_ID())); |
|
432 | - if ($ticket instanceof EE_Ticket) { |
|
433 | - if ($quantity > 0) { |
|
434 | - EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity); |
|
435 | - } else { |
|
436 | - EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity); |
|
437 | - } |
|
438 | - } |
|
439 | - } |
|
440 | - |
|
441 | - |
|
442 | - |
|
443 | - /** |
|
444 | - * ticket_removed_from_cart |
|
445 | - * releases reserved ticket(s) based on quantity passed |
|
446 | - * |
|
447 | - * @access public |
|
448 | - * @param EE_Ticket $ticket |
|
449 | - * @param int $quantity |
|
450 | - * @return void |
|
451 | - * @throws EE_Error |
|
452 | - */ |
|
453 | - public static function ticket_removed_from_cart(EE_Ticket $ticket, $quantity = 1) |
|
454 | - { |
|
455 | - EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity); |
|
456 | - } |
|
457 | - |
|
458 | - |
|
459 | - |
|
460 | - /********************************** POST_NOTICES **********************************/ |
|
461 | - |
|
462 | - |
|
463 | - |
|
464 | - /** |
|
465 | - * post_notices |
|
466 | - * |
|
467 | - * @access public |
|
468 | - * @return void |
|
469 | - * @throws EE_Error |
|
470 | - */ |
|
471 | - public static function post_notices() |
|
472 | - { |
|
473 | - EED_Ticket_Sales_Monitor::instance()->_post_notices(); |
|
474 | - } |
|
475 | - |
|
476 | - |
|
477 | - |
|
478 | - /** |
|
479 | - * _post_notices |
|
480 | - * |
|
481 | - * @access protected |
|
482 | - * @return void |
|
483 | - * @throws EE_Error |
|
484 | - */ |
|
485 | - protected function _post_notices() |
|
486 | - { |
|
487 | - if (self::debug) { |
|
488 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
489 | - } |
|
490 | - $refresh_msg = ''; |
|
491 | - $none_added_msg = ''; |
|
492 | - if (defined('DOING_AJAX') && DOING_AJAX) { |
|
493 | - $refresh_msg = __('Please refresh the page to view updated ticket quantities.', |
|
494 | - 'event_espresso'); |
|
495 | - $none_added_msg = __('No tickets were added for the event.', 'event_espresso'); |
|
496 | - } |
|
497 | - if ( ! empty($this->sold_out_tickets)) { |
|
498 | - EE_Error::add_attention( |
|
499 | - sprintf( |
|
500 | - apply_filters( |
|
501 | - 'FHEE__EED_Ticket_Sales_Monitor___post_notices__sold_out_tickets_notice', |
|
502 | - __('We\'re sorry...%1$sThe following items have sold out since you first viewed this page, and can no longer be registered for:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s', |
|
503 | - 'event_espresso') |
|
504 | - ), |
|
505 | - '<br />', |
|
506 | - implode('<br />', $this->sold_out_tickets), |
|
507 | - $none_added_msg, |
|
508 | - $refresh_msg |
|
509 | - ) |
|
510 | - ); |
|
511 | - // alter code flow in the Ticket Selector for better UX |
|
512 | - add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', '__return_true'); |
|
513 | - add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__success', '__return_false'); |
|
514 | - $this->sold_out_tickets = array(); |
|
515 | - // and reset the cart |
|
516 | - EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN); |
|
517 | - } |
|
518 | - if ( ! empty($this->decremented_tickets)) { |
|
519 | - EE_Error::add_attention( |
|
520 | - sprintf( |
|
521 | - apply_filters( |
|
522 | - 'FHEE__EED_Ticket_Sales_Monitor___ticket_quantity_decremented__notice', |
|
523 | - __('We\'re sorry...%1$sDue to sales that have occurred since you first viewed the last page, the following items have had their quantities adjusted to match the current available amount:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s', |
|
524 | - 'event_espresso') |
|
525 | - ), |
|
526 | - '<br />', |
|
527 | - implode('<br />', $this->decremented_tickets), |
|
528 | - $none_added_msg, |
|
529 | - $refresh_msg |
|
530 | - ) |
|
531 | - ); |
|
532 | - $this->decremented_tickets = array(); |
|
533 | - } |
|
534 | - } |
|
535 | - |
|
536 | - |
|
537 | - |
|
538 | - /********************************** RELEASE_ALL_RESERVED_TICKETS_FOR_TRANSACTION **********************************/ |
|
539 | - |
|
540 | - |
|
541 | - |
|
542 | - /** |
|
543 | - * _release_all_reserved_tickets_for_transaction |
|
544 | - * releases reserved tickets for all registrations of an EE_Transaction |
|
545 | - * by default, will NOT release tickets for finalized transactions |
|
546 | - * |
|
547 | - * @access protected |
|
548 | - * @param EE_Transaction $transaction |
|
549 | - * @return int |
|
550 | - * @throws EE_Error |
|
551 | - */ |
|
552 | - protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction) |
|
553 | - { |
|
554 | - if (self::debug) { |
|
555 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
556 | - echo '<br /> . transaction->ID: ' . $transaction->ID(); |
|
557 | - } |
|
558 | - // check if 'finalize_registration' step has been completed... |
|
559 | - $finalized = $transaction->reg_step_completed('finalize_registration'); |
|
560 | - if (self::debug) { |
|
561 | - // DEBUG LOG |
|
562 | - EEH_Debug_Tools::log( |
|
563 | - __CLASS__, __FUNCTION__, __LINE__, |
|
564 | - array('finalized' => $finalized), |
|
565 | - false, 'EE_Transaction: ' . $transaction->ID() |
|
566 | - ); |
|
567 | - } |
|
568 | - // how many tickets were released |
|
569 | - $count = 0; |
|
570 | - if (self::debug) { |
|
571 | - echo '<br /> . . . finalized: ' . $finalized; |
|
572 | - } |
|
573 | - $release_tickets_with_TXN_status = array( |
|
574 | - EEM_Transaction::failed_status_code, |
|
575 | - EEM_Transaction::abandoned_status_code, |
|
576 | - EEM_Transaction::incomplete_status_code, |
|
577 | - ); |
|
578 | - // if the session is getting cleared BEFORE the TXN has been finalized |
|
579 | - if ( ! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) { |
|
580 | - // let's cancel any reserved tickets |
|
581 | - $registrations = $transaction->registrations(); |
|
582 | - if ( ! empty($registrations)) { |
|
583 | - foreach ($registrations as $registration) { |
|
584 | - if ($registration instanceof EE_Registration) { |
|
585 | - $count += $this->_release_reserved_ticket_for_registration($registration, $transaction); |
|
586 | - } |
|
587 | - } |
|
588 | - } |
|
589 | - } |
|
590 | - return $count; |
|
591 | - } |
|
592 | - |
|
593 | - |
|
594 | - |
|
595 | - /** |
|
596 | - * _release_reserved_ticket_for_registration |
|
597 | - * releases reserved tickets for an EE_Registration |
|
598 | - * by default, will NOT release tickets for APPROVED registrations |
|
599 | - * |
|
600 | - * @access protected |
|
601 | - * @param EE_Registration $registration |
|
602 | - * @param EE_Transaction $transaction |
|
603 | - * @return int |
|
604 | - * @throws EE_Error |
|
605 | - */ |
|
606 | - protected function _release_reserved_ticket_for_registration( |
|
607 | - EE_Registration $registration, |
|
608 | - EE_Transaction $transaction |
|
609 | - ) { |
|
610 | - $STS_ID = $transaction->status_ID(); |
|
611 | - if (self::debug) { |
|
612 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
613 | - echo '<br /> . . registration->ID: ' . $registration->ID(); |
|
614 | - echo '<br /> . . registration->status_ID: ' . $registration->status_ID(); |
|
615 | - echo '<br /> . . transaction->status_ID(): ' . $STS_ID; |
|
616 | - } |
|
617 | - if ( |
|
618 | - // release Tickets for Failed Transactions and Abandoned Transactions |
|
619 | - $STS_ID === EEM_Transaction::failed_status_code |
|
620 | - || $STS_ID === EEM_Transaction::abandoned_status_code |
|
621 | - || ( |
|
622 | - // also release Tickets for Incomplete Transactions, but ONLY if the Registrations are NOT Approved |
|
623 | - $STS_ID === EEM_Transaction::incomplete_status_code |
|
624 | - && $registration->status_ID() !== EEM_Registration::status_id_approved |
|
625 | - ) |
|
626 | - ) { |
|
627 | - $ticket = $registration->ticket(); |
|
628 | - if ($ticket instanceof EE_Ticket) { |
|
629 | - return $this->_release_reserved_ticket($ticket); |
|
630 | - } |
|
631 | - } |
|
632 | - return 0; |
|
633 | - } |
|
634 | - |
|
635 | - |
|
636 | - |
|
637 | - /********************************** SESSION_CART_RESET **********************************/ |
|
638 | - |
|
639 | - |
|
640 | - |
|
641 | - /** |
|
642 | - * session_cart_reset |
|
643 | - * callback hooked into 'AHEE__EE_Session__reset_cart__before_reset' |
|
644 | - * |
|
645 | - * @access public |
|
646 | - * @param EE_Session $session |
|
647 | - * @return void |
|
648 | - * @throws EE_Error |
|
649 | - */ |
|
650 | - public static function session_cart_reset(EE_Session $session) |
|
651 | - { |
|
652 | - if (self::debug) { |
|
653 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
654 | - } |
|
655 | - $cart = $session->cart(); |
|
656 | - if ($cart instanceof EE_Cart) { |
|
657 | - if (self::debug) { |
|
658 | - echo '<br /><br /> cart instance of EE_Cart: '; |
|
659 | - } |
|
660 | - EED_Ticket_Sales_Monitor::instance()->_session_cart_reset($cart); |
|
661 | - } else { |
|
662 | - if (self::debug) { |
|
663 | - echo '<br /><br /> invalid EE_Cart: '; |
|
664 | - var_dump($cart); |
|
665 | - } |
|
666 | - } |
|
667 | - } |
|
668 | - |
|
669 | - |
|
670 | - |
|
671 | - /** |
|
672 | - * _session_cart_reset |
|
673 | - * releases reserved tickets in the EE_Cart |
|
674 | - * |
|
675 | - * @access protected |
|
676 | - * @param EE_Cart $cart |
|
677 | - * @return void |
|
678 | - * @throws EE_Error |
|
679 | - */ |
|
680 | - protected function _session_cart_reset(EE_Cart $cart) |
|
681 | - { |
|
682 | - if (self::debug) { |
|
683 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
684 | - } |
|
685 | - EE_Registry::instance()->load_helper('Line_Item'); |
|
686 | - $ticket_line_items = $cart->get_tickets(); |
|
687 | - if (empty($ticket_line_items)) { |
|
688 | - return; |
|
689 | - } |
|
690 | - foreach ($ticket_line_items as $ticket_line_item) { |
|
691 | - if (self::debug) { |
|
692 | - echo '<br /> . ticket_line_item->ID(): ' . $ticket_line_item->ID(); |
|
693 | - } |
|
694 | - if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') { |
|
695 | - if (self::debug) { |
|
696 | - echo '<br /> . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID(); |
|
697 | - } |
|
698 | - $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID()); |
|
699 | - if ($ticket instanceof EE_Ticket) { |
|
700 | - if (self::debug) { |
|
701 | - echo '<br /> . . ticket->ID(): ' . $ticket->ID(); |
|
702 | - echo '<br /> . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity(); |
|
703 | - } |
|
704 | - $this->_release_reserved_ticket($ticket, $ticket_line_item->quantity()); |
|
705 | - } |
|
706 | - } |
|
707 | - } |
|
708 | - if (self::debug) { |
|
709 | - echo '<br /><br /> RESET COMPLETED '; |
|
710 | - } |
|
711 | - } |
|
712 | - |
|
713 | - |
|
714 | - |
|
715 | - /********************************** SESSION_CHECKOUT_RESET **********************************/ |
|
716 | - |
|
717 | - |
|
718 | - |
|
719 | - /** |
|
720 | - * session_checkout_reset |
|
721 | - * callback hooked into 'AHEE__EE_Session__reset_checkout__before_reset' |
|
722 | - * |
|
723 | - * @access public |
|
724 | - * @param EE_Session $session |
|
725 | - * @return void |
|
726 | - * @throws EE_Error |
|
727 | - */ |
|
728 | - public static function session_checkout_reset(EE_Session $session) |
|
729 | - { |
|
730 | - $checkout = $session->checkout(); |
|
731 | - if ($checkout instanceof EE_Checkout) { |
|
732 | - EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout); |
|
733 | - } |
|
734 | - } |
|
735 | - |
|
736 | - |
|
737 | - |
|
738 | - /** |
|
739 | - * _session_checkout_reset |
|
740 | - * releases reserved tickets for the EE_Checkout->transaction |
|
741 | - * |
|
742 | - * @access protected |
|
743 | - * @param EE_Checkout $checkout |
|
744 | - * @return void |
|
745 | - * @throws EE_Error |
|
746 | - */ |
|
747 | - protected function _session_checkout_reset(EE_Checkout $checkout) |
|
748 | - { |
|
749 | - if (self::debug) { |
|
750 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
751 | - } |
|
752 | - // we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit |
|
753 | - if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) { |
|
754 | - return; |
|
755 | - } |
|
756 | - $this->_release_all_reserved_tickets_for_transaction($checkout->transaction); |
|
757 | - } |
|
758 | - |
|
759 | - |
|
760 | - |
|
761 | - /********************************** SESSION_EXPIRED_RESET **********************************/ |
|
762 | - |
|
763 | - |
|
764 | - |
|
765 | - /** |
|
766 | - * session_expired_reset |
|
767 | - * |
|
768 | - * @access public |
|
769 | - * @param EE_Session $session |
|
770 | - * @return void |
|
771 | - */ |
|
772 | - public static function session_expired_reset(EE_Session $session) |
|
773 | - { |
|
774 | - } |
|
775 | - |
|
776 | - |
|
777 | - |
|
778 | - /********************************** PROCESS_ABANDONED_TRANSACTIONS **********************************/ |
|
779 | - |
|
780 | - |
|
781 | - |
|
782 | - /** |
|
783 | - * process_abandoned_transactions |
|
784 | - * releases reserved tickets for all registrations of an ABANDONED EE_Transaction |
|
785 | - * by default, will NOT release tickets for free transactions, or any that have received a payment |
|
786 | - * |
|
787 | - * @access public |
|
788 | - * @param EE_Transaction $transaction |
|
789 | - * @return void |
|
790 | - * @throws EE_Error |
|
791 | - */ |
|
792 | - public static function process_abandoned_transactions(EE_Transaction $transaction) |
|
793 | - { |
|
794 | - // is this TXN free or has any money been paid towards this TXN? If so, then leave it alone |
|
795 | - if ($transaction->is_free() || $transaction->paid() > 0) { |
|
796 | - if (self::debug) { |
|
797 | - // DEBUG LOG |
|
798 | - EEH_Debug_Tools::log( |
|
799 | - __CLASS__, __FUNCTION__, __LINE__, |
|
800 | - array($transaction), |
|
801 | - false, 'EE_Transaction: ' . $transaction->ID() |
|
802 | - ); |
|
803 | - } |
|
804 | - return; |
|
805 | - } |
|
806 | - // have their been any successful payments made ? |
|
807 | - $payments = $transaction->payments(); |
|
808 | - foreach ($payments as $payment) { |
|
809 | - if ($payment instanceof EE_Payment && $payment->status() === EEM_Payment::status_id_approved) { |
|
810 | - if (self::debug) { |
|
811 | - // DEBUG LOG |
|
812 | - EEH_Debug_Tools::log( |
|
813 | - __CLASS__, __FUNCTION__, __LINE__, |
|
814 | - array($payment), |
|
815 | - false, 'EE_Transaction: ' . $transaction->ID() |
|
816 | - ); |
|
817 | - } |
|
818 | - return; |
|
819 | - } |
|
820 | - } |
|
821 | - // since you haven't even attempted to pay for your ticket... |
|
822 | - EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction); |
|
823 | - } |
|
824 | - |
|
825 | - |
|
826 | - |
|
827 | - /********************************** PROCESS_FAILED_TRANSACTIONS **********************************/ |
|
828 | - |
|
829 | - |
|
830 | - |
|
831 | - /** |
|
832 | - * process_abandoned_transactions |
|
833 | - * releases reserved tickets for absolutely ALL registrations of a FAILED EE_Transaction |
|
834 | - * |
|
835 | - * @access public |
|
836 | - * @param EE_Transaction $transaction |
|
837 | - * @return void |
|
838 | - * @throws EE_Error |
|
839 | - */ |
|
840 | - public static function process_failed_transactions(EE_Transaction $transaction) |
|
841 | - { |
|
842 | - // since you haven't even attempted to pay for your ticket... |
|
843 | - EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction); |
|
844 | - } |
|
845 | - |
|
846 | - |
|
847 | - |
|
848 | - /********************************** RESET RESERVATION COUNTS *********************************/ |
|
849 | - |
|
850 | - |
|
851 | - |
|
852 | - /** |
|
853 | - * Resets all ticket and datetime reserved counts to zero |
|
854 | - * Tickets that are currently associated with a Transaction that is in progress |
|
855 | - * |
|
856 | - * @throws \EE_Error |
|
857 | - * @throws \DomainException |
|
858 | - */ |
|
859 | - public static function reset_reservation_counts() |
|
860 | - { |
|
861 | - /** @var EE_Line_Item[] $valid_reserved_tickets */ |
|
862 | - $valid_reserved_tickets = array(); |
|
863 | - $transactions_in_progress = EEM_Transaction::instance()->get_transactions_in_progress(); |
|
864 | - foreach ($transactions_in_progress as $transaction_in_progress) { |
|
865 | - // if this TXN has been fully completed, then skip it |
|
866 | - if ($transaction_in_progress->reg_step_completed('finalize_registration')) { |
|
867 | - continue; |
|
868 | - } |
|
869 | - /** @var EE_Transaction $transaction_in_progress */ |
|
870 | - $total_line_item = $transaction_in_progress->total_line_item(); |
|
871 | - // $transaction_in_progress->line |
|
872 | - if (! $total_line_item instanceof EE_Line_Item) { |
|
873 | - throw new DomainException( |
|
874 | - esc_html__('Transaction does not have a valid Total Line Item associated with it.', 'event_espresso') |
|
875 | - ); |
|
876 | - } |
|
877 | - $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total( |
|
878 | - $total_line_item |
|
879 | - ); |
|
880 | - } |
|
881 | - $total_line_items = EEM_Line_Item::instance()->get_total_line_items_for_active_carts(); |
|
882 | - foreach ($total_line_items as $total_line_item) { |
|
883 | - $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total( |
|
884 | - $total_line_item |
|
885 | - ); |
|
886 | - } |
|
887 | - return EED_Ticket_Sales_Monitor::release_reservations_for_tickets( |
|
888 | - EEM_Ticket::instance()->get_tickets_with_reservations(), |
|
889 | - $valid_reserved_tickets |
|
890 | - ); |
|
891 | - } |
|
892 | - |
|
893 | - |
|
894 | - |
|
895 | - /** |
|
896 | - * @param EE_Line_Item $total_line_item |
|
897 | - * @return EE_Line_Item[] |
|
898 | - */ |
|
899 | - private static function get_ticket_line_items_for_grand_total(EE_Line_Item $total_line_item) |
|
900 | - { |
|
901 | - /** @var EE_Line_Item[] $valid_reserved_tickets */ |
|
902 | - $valid_reserved_tickets = array(); |
|
903 | - $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item); |
|
904 | - foreach ($ticket_line_items as $ticket_line_item) { |
|
905 | - if ($ticket_line_item instanceof EE_Line_Item) { |
|
906 | - $valid_reserved_tickets[] = $ticket_line_item; |
|
907 | - } |
|
908 | - } |
|
909 | - return $valid_reserved_tickets; |
|
910 | - } |
|
911 | - |
|
912 | - |
|
913 | - |
|
914 | - /** |
|
915 | - * @param EE_Ticket[] $tickets_with_reservations |
|
916 | - * @param EE_Line_Item[] $valid_reserved_ticket_line_items |
|
917 | - * @return int |
|
918 | - * @throws \EE_Error |
|
919 | - */ |
|
920 | - private static function release_reservations_for_tickets( |
|
921 | - array $tickets_with_reservations, |
|
922 | - $valid_reserved_ticket_line_items = array() |
|
923 | - ) { |
|
924 | - $total_tickets_released = 0; |
|
925 | - foreach ($tickets_with_reservations as $ticket_with_reservations) { |
|
926 | - if (! $ticket_with_reservations instanceof EE_Ticket) { |
|
927 | - continue; |
|
928 | - } |
|
929 | - $reserved_qty = $ticket_with_reservations->reserved(); |
|
930 | - foreach ($valid_reserved_ticket_line_items as $valid_reserved_ticket_line_item) { |
|
931 | - if ( |
|
932 | - $valid_reserved_ticket_line_item instanceof EE_Line_Item |
|
933 | - && $valid_reserved_ticket_line_item->OBJ_ID() === $ticket_with_reservations->ID() |
|
934 | - ) { |
|
935 | - $reserved_qty -= $valid_reserved_ticket_line_item->quantity(); |
|
936 | - } |
|
937 | - } |
|
938 | - if ($reserved_qty > 0) { |
|
939 | - $ticket_with_reservations->decrease_reserved($reserved_qty); |
|
940 | - $ticket_with_reservations->save(); |
|
941 | - $total_tickets_released += $reserved_qty; |
|
942 | - } |
|
943 | - } |
|
944 | - return $total_tickets_released; |
|
945 | - } |
|
946 | - |
|
947 | - |
|
948 | - |
|
949 | - /********************************** SHUTDOWN **********************************/ |
|
950 | - |
|
951 | - |
|
952 | - |
|
953 | - /** |
|
954 | - * @return false|int |
|
955 | - * @throws \EE_Error |
|
956 | - */ |
|
957 | - public static function clear_expired_line_items_with_no_transaction() |
|
958 | - { |
|
959 | - /** @type WPDB $wpdb */ |
|
960 | - global $wpdb; |
|
961 | - return $wpdb->query( |
|
962 | - $wpdb->prepare( |
|
963 | - 'DELETE FROM ' . EEM_Line_Item::instance()->table() . ' |
|
24 | + const debug = false; // true false |
|
25 | + |
|
26 | + /** |
|
27 | + * an array of raw ticket data from EED_Ticket_Selector |
|
28 | + * |
|
29 | + * @var array $ticket_selections |
|
30 | + */ |
|
31 | + protected $ticket_selections = array(); |
|
32 | + |
|
33 | + /** |
|
34 | + * the raw ticket data from EED_Ticket_Selector is organized in rows |
|
35 | + * according to how they are displayed in the actual Ticket_Selector |
|
36 | + * this tracks the current row being processed |
|
37 | + * |
|
38 | + * @var int $current_row |
|
39 | + */ |
|
40 | + protected $current_row = 0; |
|
41 | + |
|
42 | + /** |
|
43 | + * an array for tracking names of tickets that have sold out |
|
44 | + * |
|
45 | + * @var array $sold_out_tickets |
|
46 | + */ |
|
47 | + protected $sold_out_tickets = array(); |
|
48 | + |
|
49 | + /** |
|
50 | + * an array for tracking names of tickets that have had their quantities reduced |
|
51 | + * |
|
52 | + * @var array $decremented_tickets |
|
53 | + */ |
|
54 | + protected $decremented_tickets = array(); |
|
55 | + |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
60 | + * |
|
61 | + * @access public |
|
62 | + * @return void |
|
63 | + */ |
|
64 | + public static function set_hooks() |
|
65 | + { |
|
66 | + // release tickets for expired carts |
|
67 | + add_action('EED_Ticket_Selector__process_ticket_selections__before', |
|
68 | + array('EED_Ticket_Sales_Monitor', 'release_tickets_for_expired_carts'), |
|
69 | + 1 |
|
70 | + ); |
|
71 | + // check ticket reserves AFTER MER does it's check (hence priority 20) |
|
72 | + add_filter('FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty', |
|
73 | + array('EED_Ticket_Sales_Monitor', 'validate_ticket_sale'), |
|
74 | + 20, 3 |
|
75 | + ); |
|
76 | + // add notices for sold out tickets |
|
77 | + add_action('AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart', |
|
78 | + array('EED_Ticket_Sales_Monitor', 'post_notices'), |
|
79 | + 10 |
|
80 | + ); |
|
81 | + // handle ticket quantities adjusted in cart |
|
82 | + //add_action( |
|
83 | + // 'FHEE__EED_Multi_Event_Registration__adjust_line_item_quantity__line_item_quantity_updated', |
|
84 | + // array( 'EED_Ticket_Sales_Monitor', 'ticket_quantity_updated' ), |
|
85 | + // 10, 2 |
|
86 | + //); |
|
87 | + // handle tickets deleted from cart |
|
88 | + add_action( |
|
89 | + 'FHEE__EED_Multi_Event_Registration__delete_ticket__ticket_removed_from_cart', |
|
90 | + array('EED_Ticket_Sales_Monitor', 'ticket_removed_from_cart'), |
|
91 | + 10, 2 |
|
92 | + ); |
|
93 | + // handle emptied carts |
|
94 | + add_action( |
|
95 | + 'AHEE__EE_Session__reset_cart__before_reset', |
|
96 | + array('EED_Ticket_Sales_Monitor', 'session_cart_reset'), |
|
97 | + 10, 1 |
|
98 | + ); |
|
99 | + add_action( |
|
100 | + 'AHEE__EED_Multi_Event_Registration__empty_event_cart__before_delete_cart', |
|
101 | + array('EED_Ticket_Sales_Monitor', 'session_cart_reset'), |
|
102 | + 10, 1 |
|
103 | + ); |
|
104 | + // handle cancelled registrations |
|
105 | + add_action( |
|
106 | + 'AHEE__EE_Session__reset_checkout__before_reset', |
|
107 | + array('EED_Ticket_Sales_Monitor', 'session_checkout_reset'), |
|
108 | + 10, 1 |
|
109 | + ); |
|
110 | + // cron tasks |
|
111 | + add_action( |
|
112 | + 'AHEE__EE_Cron_Tasks__finalize_abandoned_transactions__abandoned_transaction', |
|
113 | + array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'), |
|
114 | + 10, 1 |
|
115 | + ); |
|
116 | + add_action( |
|
117 | + 'AHEE__EE_Cron_Tasks__process_expired_transactions__incomplete_transaction', |
|
118 | + array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'), |
|
119 | + 10, 1 |
|
120 | + ); |
|
121 | + add_action( |
|
122 | + 'AHEE__EE_Cron_Tasks__process_expired_transactions__failed_transaction', |
|
123 | + array('EED_Ticket_Sales_Monitor', 'process_failed_transactions'), |
|
124 | + 10, 1 |
|
125 | + ); |
|
126 | + } |
|
127 | + |
|
128 | + |
|
129 | + |
|
130 | + /** |
|
131 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
132 | + * |
|
133 | + * @access public |
|
134 | + * @return void |
|
135 | + */ |
|
136 | + public static function set_hooks_admin() |
|
137 | + { |
|
138 | + EED_Ticket_Sales_Monitor::set_hooks(); |
|
139 | + } |
|
140 | + |
|
141 | + |
|
142 | + |
|
143 | + /** |
|
144 | + * @return EED_Ticket_Sales_Monitor|EED_Module |
|
145 | + */ |
|
146 | + public static function instance() |
|
147 | + { |
|
148 | + return parent::get_instance(__CLASS__); |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * run |
|
155 | + * |
|
156 | + * @access public |
|
157 | + * @param WP_Query $WP_Query |
|
158 | + * @return void |
|
159 | + */ |
|
160 | + public function run($WP_Query) |
|
161 | + { |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + |
|
166 | + /********************************** PRE_TICKET_SALES **********************************/ |
|
167 | + |
|
168 | + |
|
169 | + |
|
170 | + /** |
|
171 | + * Retrieves grand totals from the line items that have no TXN ID |
|
172 | + * and timestamps less than the current time minus the session lifespan. |
|
173 | + * These are carts that have been abandoned before the "registrant" even attempted to checkout. |
|
174 | + * We're going to release the tickets for these line items before attempting to add more to the cart. |
|
175 | + * |
|
176 | + * @return void |
|
177 | + * @throws \EE_Error |
|
178 | + */ |
|
179 | + public static function release_tickets_for_expired_carts() |
|
180 | + { |
|
181 | + $expired_ticket_IDs = array(); |
|
182 | + $valid_ticket_line_items = array(); |
|
183 | + $total_line_items = EEM_Line_Item::instance()->get_total_line_items_with_no_transaction(); |
|
184 | + if(empty($total_line_items)){ |
|
185 | + return; |
|
186 | + } |
|
187 | + $expired = current_time('timestamp') - EE_Registry::instance()->SSN->lifespan(); |
|
188 | + foreach ($total_line_items as $total_line_item) { |
|
189 | + /** @var EE_Line_Item $total_line_item */ |
|
190 | + $ticket_line_items = EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total($total_line_item); |
|
191 | + foreach ($ticket_line_items as $ticket_line_item) { |
|
192 | + if(! $ticket_line_item instanceof EE_Line_Item) { |
|
193 | + continue; |
|
194 | + } |
|
195 | + if ($total_line_item->timestamp(true) <= $expired ) { |
|
196 | + $expired_ticket_IDs[$ticket_line_item->OBJ_ID()] = $ticket_line_item->OBJ_ID(); |
|
197 | + } else { |
|
198 | + $valid_ticket_line_items[$ticket_line_item->OBJ_ID()] = $ticket_line_item; |
|
199 | + } |
|
200 | + } |
|
201 | + } |
|
202 | + if (! empty($expired_ticket_IDs)) { |
|
203 | + EED_Ticket_Sales_Monitor::release_reservations_for_tickets( |
|
204 | + \EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs), |
|
205 | + $valid_ticket_line_items |
|
206 | + ); |
|
207 | + // let's get rid of expired line items so that they can't interfere with tracking |
|
208 | + add_action( |
|
209 | + 'shutdown', |
|
210 | + array('EED_Ticket_Sales_Monitor', 'clear_expired_line_items_with_no_transaction'), |
|
211 | + 999 |
|
212 | + ); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + |
|
217 | + |
|
218 | + /********************************** VALIDATE_TICKET_SALE **********************************/ |
|
219 | + |
|
220 | + |
|
221 | + |
|
222 | + /** |
|
223 | + * validate_ticket_sales |
|
224 | + * callback for 'FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data' |
|
225 | + * |
|
226 | + * @access public |
|
227 | + * @param int $qty |
|
228 | + * @param \EE_Ticket $ticket |
|
229 | + * @return bool |
|
230 | + * @throws UnexpectedEntityException |
|
231 | + * @throws EE_Error |
|
232 | + */ |
|
233 | + public static function validate_ticket_sale($qty = 1, EE_Ticket $ticket) |
|
234 | + { |
|
235 | + $qty = absint($qty); |
|
236 | + if ($qty > 0) { |
|
237 | + $qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty); |
|
238 | + } |
|
239 | + if (self::debug) { |
|
240 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '()'; |
|
241 | + echo '<br /><br /><b> RETURNED QTY: ' . $qty . '</b>'; |
|
242 | + } |
|
243 | + return $qty; |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * _validate_ticket_sale |
|
250 | + * checks whether an individual ticket is available for purchase based on datetime, and ticket details |
|
251 | + * |
|
252 | + * @access protected |
|
253 | + * @param \EE_Ticket $ticket |
|
254 | + * @param int $qty |
|
255 | + * @return int |
|
256 | + * @throws UnexpectedEntityException |
|
257 | + * @throws EE_Error |
|
258 | + */ |
|
259 | + protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1) |
|
260 | + { |
|
261 | + if (self::debug) { |
|
262 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
263 | + } |
|
264 | + if ( ! $ticket instanceof EE_Ticket) { |
|
265 | + return 0; |
|
266 | + } |
|
267 | + if (self::debug) { |
|
268 | + echo '<br /><b> . ticket->ID: ' . $ticket->ID() . '</b>'; |
|
269 | + echo '<br /> . original ticket->reserved: ' . $ticket->reserved(); |
|
270 | + } |
|
271 | + $ticket->refresh_from_db(); |
|
272 | + // first let's determine the ticket availability based on sales |
|
273 | + $available = $ticket->qty('saleable'); |
|
274 | + if (self::debug) { |
|
275 | + echo '<br /> . . . ticket->qty: ' . $ticket->qty(); |
|
276 | + echo '<br /> . . . ticket->sold: ' . $ticket->sold(); |
|
277 | + echo '<br /> . . . ticket->reserved: ' . $ticket->reserved(); |
|
278 | + echo '<br /> . . . ticket->qty(saleable): ' . $ticket->qty('saleable'); |
|
279 | + echo '<br /> . . . available: ' . $available; |
|
280 | + } |
|
281 | + if ($available < 1) { |
|
282 | + $this->_ticket_sold_out($ticket); |
|
283 | + return 0; |
|
284 | + } |
|
285 | + if (self::debug) { |
|
286 | + echo '<br /> . . . qty: ' . $qty; |
|
287 | + } |
|
288 | + if ($available < $qty) { |
|
289 | + $qty = $available; |
|
290 | + if (self::debug) { |
|
291 | + echo '<br /> . . . QTY ADJUSTED: ' . $qty; |
|
292 | + } |
|
293 | + $this->_ticket_quantity_decremented($ticket); |
|
294 | + } |
|
295 | + $this->_reserve_ticket($ticket, $qty); |
|
296 | + return $qty; |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + |
|
301 | + /** |
|
302 | + * _reserve_ticket |
|
303 | + * increments ticket reserved based on quantity passed |
|
304 | + * |
|
305 | + * @access protected |
|
306 | + * @param \EE_Ticket $ticket |
|
307 | + * @param int $quantity |
|
308 | + * @return bool |
|
309 | + * @throws EE_Error |
|
310 | + */ |
|
311 | + protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1) |
|
312 | + { |
|
313 | + if (self::debug) { |
|
314 | + echo '<br /><br /> . . . INCREASE RESERVED: ' . $quantity; |
|
315 | + } |
|
316 | + $ticket->increase_reserved($quantity); |
|
317 | + return $ticket->save(); |
|
318 | + } |
|
319 | + |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * _release_reserved_ticket |
|
324 | + * |
|
325 | + * @access protected |
|
326 | + * @param EE_Ticket $ticket |
|
327 | + * @param int $quantity |
|
328 | + * @return bool |
|
329 | + * @throws EE_Error |
|
330 | + */ |
|
331 | + protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1) |
|
332 | + { |
|
333 | + if (self::debug) { |
|
334 | + echo '<br /> . . . ticket->ID: ' . $ticket->ID(); |
|
335 | + echo '<br /> . . . ticket->reserved: ' . $ticket->reserved(); |
|
336 | + } |
|
337 | + $ticket->decrease_reserved($quantity); |
|
338 | + if (self::debug) { |
|
339 | + echo '<br /> . . . ticket->reserved: ' . $ticket->reserved(); |
|
340 | + } |
|
341 | + return $ticket->save() ? 1 : 0; |
|
342 | + } |
|
343 | + |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * _ticket_sold_out |
|
348 | + * removes quantities within the ticket selector based on zero ticket availability |
|
349 | + * |
|
350 | + * @access protected |
|
351 | + * @param \EE_Ticket $ticket |
|
352 | + * @return void |
|
353 | + * @throws UnexpectedEntityException |
|
354 | + * @throws EE_Error |
|
355 | + */ |
|
356 | + protected function _ticket_sold_out(EE_Ticket $ticket) |
|
357 | + { |
|
358 | + if (self::debug) { |
|
359 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
360 | + echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket); |
|
361 | + } |
|
362 | + $this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket); |
|
363 | + } |
|
364 | + |
|
365 | + |
|
366 | + |
|
367 | + /** |
|
368 | + * _ticket_quantity_decremented |
|
369 | + * adjusts quantities within the ticket selector based on decreased ticket availability |
|
370 | + * |
|
371 | + * @access protected |
|
372 | + * @param \EE_Ticket $ticket |
|
373 | + * @return void |
|
374 | + * @throws UnexpectedEntityException |
|
375 | + * @throws EE_Error |
|
376 | + */ |
|
377 | + protected function _ticket_quantity_decremented(EE_Ticket $ticket) |
|
378 | + { |
|
379 | + if (self::debug) { |
|
380 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
381 | + echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket); |
|
382 | + } |
|
383 | + $this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket); |
|
384 | + } |
|
385 | + |
|
386 | + |
|
387 | + |
|
388 | + /** |
|
389 | + * _get_ticket_and_event_name |
|
390 | + * builds string out of ticket and event name |
|
391 | + * |
|
392 | + * @access protected |
|
393 | + * @param \EE_Ticket $ticket |
|
394 | + * @return string |
|
395 | + * @throws UnexpectedEntityException |
|
396 | + * @throws EE_Error |
|
397 | + */ |
|
398 | + protected function _get_ticket_and_event_name(EE_Ticket $ticket) |
|
399 | + { |
|
400 | + $event = $ticket->get_related_event(); |
|
401 | + if ($event instanceof EE_Event) { |
|
402 | + $ticket_name = sprintf( |
|
403 | + _x('%1$s for %2$s', 'ticket name for event name', 'event_espresso'), |
|
404 | + $ticket->name(), |
|
405 | + $event->name() |
|
406 | + ); |
|
407 | + } else { |
|
408 | + $ticket_name = $ticket->name(); |
|
409 | + } |
|
410 | + return $ticket_name; |
|
411 | + } |
|
412 | + |
|
413 | + |
|
414 | + |
|
415 | + /********************************** EVENT CART **********************************/ |
|
416 | + |
|
417 | + |
|
418 | + |
|
419 | + /** |
|
420 | + * ticket_quantity_updated |
|
421 | + * releases or reserves ticket(s) based on quantity passed |
|
422 | + * |
|
423 | + * @access public |
|
424 | + * @param EE_Line_Item $line_item |
|
425 | + * @param int $quantity |
|
426 | + * @return void |
|
427 | + * @throws EE_Error |
|
428 | + */ |
|
429 | + public static function ticket_quantity_updated(EE_Line_Item $line_item, $quantity = 1) |
|
430 | + { |
|
431 | + $ticket = EEM_Ticket::instance()->get_one_by_ID(absint($line_item->OBJ_ID())); |
|
432 | + if ($ticket instanceof EE_Ticket) { |
|
433 | + if ($quantity > 0) { |
|
434 | + EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity); |
|
435 | + } else { |
|
436 | + EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity); |
|
437 | + } |
|
438 | + } |
|
439 | + } |
|
440 | + |
|
441 | + |
|
442 | + |
|
443 | + /** |
|
444 | + * ticket_removed_from_cart |
|
445 | + * releases reserved ticket(s) based on quantity passed |
|
446 | + * |
|
447 | + * @access public |
|
448 | + * @param EE_Ticket $ticket |
|
449 | + * @param int $quantity |
|
450 | + * @return void |
|
451 | + * @throws EE_Error |
|
452 | + */ |
|
453 | + public static function ticket_removed_from_cart(EE_Ticket $ticket, $quantity = 1) |
|
454 | + { |
|
455 | + EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity); |
|
456 | + } |
|
457 | + |
|
458 | + |
|
459 | + |
|
460 | + /********************************** POST_NOTICES **********************************/ |
|
461 | + |
|
462 | + |
|
463 | + |
|
464 | + /** |
|
465 | + * post_notices |
|
466 | + * |
|
467 | + * @access public |
|
468 | + * @return void |
|
469 | + * @throws EE_Error |
|
470 | + */ |
|
471 | + public static function post_notices() |
|
472 | + { |
|
473 | + EED_Ticket_Sales_Monitor::instance()->_post_notices(); |
|
474 | + } |
|
475 | + |
|
476 | + |
|
477 | + |
|
478 | + /** |
|
479 | + * _post_notices |
|
480 | + * |
|
481 | + * @access protected |
|
482 | + * @return void |
|
483 | + * @throws EE_Error |
|
484 | + */ |
|
485 | + protected function _post_notices() |
|
486 | + { |
|
487 | + if (self::debug) { |
|
488 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
489 | + } |
|
490 | + $refresh_msg = ''; |
|
491 | + $none_added_msg = ''; |
|
492 | + if (defined('DOING_AJAX') && DOING_AJAX) { |
|
493 | + $refresh_msg = __('Please refresh the page to view updated ticket quantities.', |
|
494 | + 'event_espresso'); |
|
495 | + $none_added_msg = __('No tickets were added for the event.', 'event_espresso'); |
|
496 | + } |
|
497 | + if ( ! empty($this->sold_out_tickets)) { |
|
498 | + EE_Error::add_attention( |
|
499 | + sprintf( |
|
500 | + apply_filters( |
|
501 | + 'FHEE__EED_Ticket_Sales_Monitor___post_notices__sold_out_tickets_notice', |
|
502 | + __('We\'re sorry...%1$sThe following items have sold out since you first viewed this page, and can no longer be registered for:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s', |
|
503 | + 'event_espresso') |
|
504 | + ), |
|
505 | + '<br />', |
|
506 | + implode('<br />', $this->sold_out_tickets), |
|
507 | + $none_added_msg, |
|
508 | + $refresh_msg |
|
509 | + ) |
|
510 | + ); |
|
511 | + // alter code flow in the Ticket Selector for better UX |
|
512 | + add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', '__return_true'); |
|
513 | + add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__success', '__return_false'); |
|
514 | + $this->sold_out_tickets = array(); |
|
515 | + // and reset the cart |
|
516 | + EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN); |
|
517 | + } |
|
518 | + if ( ! empty($this->decremented_tickets)) { |
|
519 | + EE_Error::add_attention( |
|
520 | + sprintf( |
|
521 | + apply_filters( |
|
522 | + 'FHEE__EED_Ticket_Sales_Monitor___ticket_quantity_decremented__notice', |
|
523 | + __('We\'re sorry...%1$sDue to sales that have occurred since you first viewed the last page, the following items have had their quantities adjusted to match the current available amount:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s', |
|
524 | + 'event_espresso') |
|
525 | + ), |
|
526 | + '<br />', |
|
527 | + implode('<br />', $this->decremented_tickets), |
|
528 | + $none_added_msg, |
|
529 | + $refresh_msg |
|
530 | + ) |
|
531 | + ); |
|
532 | + $this->decremented_tickets = array(); |
|
533 | + } |
|
534 | + } |
|
535 | + |
|
536 | + |
|
537 | + |
|
538 | + /********************************** RELEASE_ALL_RESERVED_TICKETS_FOR_TRANSACTION **********************************/ |
|
539 | + |
|
540 | + |
|
541 | + |
|
542 | + /** |
|
543 | + * _release_all_reserved_tickets_for_transaction |
|
544 | + * releases reserved tickets for all registrations of an EE_Transaction |
|
545 | + * by default, will NOT release tickets for finalized transactions |
|
546 | + * |
|
547 | + * @access protected |
|
548 | + * @param EE_Transaction $transaction |
|
549 | + * @return int |
|
550 | + * @throws EE_Error |
|
551 | + */ |
|
552 | + protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction) |
|
553 | + { |
|
554 | + if (self::debug) { |
|
555 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
556 | + echo '<br /> . transaction->ID: ' . $transaction->ID(); |
|
557 | + } |
|
558 | + // check if 'finalize_registration' step has been completed... |
|
559 | + $finalized = $transaction->reg_step_completed('finalize_registration'); |
|
560 | + if (self::debug) { |
|
561 | + // DEBUG LOG |
|
562 | + EEH_Debug_Tools::log( |
|
563 | + __CLASS__, __FUNCTION__, __LINE__, |
|
564 | + array('finalized' => $finalized), |
|
565 | + false, 'EE_Transaction: ' . $transaction->ID() |
|
566 | + ); |
|
567 | + } |
|
568 | + // how many tickets were released |
|
569 | + $count = 0; |
|
570 | + if (self::debug) { |
|
571 | + echo '<br /> . . . finalized: ' . $finalized; |
|
572 | + } |
|
573 | + $release_tickets_with_TXN_status = array( |
|
574 | + EEM_Transaction::failed_status_code, |
|
575 | + EEM_Transaction::abandoned_status_code, |
|
576 | + EEM_Transaction::incomplete_status_code, |
|
577 | + ); |
|
578 | + // if the session is getting cleared BEFORE the TXN has been finalized |
|
579 | + if ( ! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) { |
|
580 | + // let's cancel any reserved tickets |
|
581 | + $registrations = $transaction->registrations(); |
|
582 | + if ( ! empty($registrations)) { |
|
583 | + foreach ($registrations as $registration) { |
|
584 | + if ($registration instanceof EE_Registration) { |
|
585 | + $count += $this->_release_reserved_ticket_for_registration($registration, $transaction); |
|
586 | + } |
|
587 | + } |
|
588 | + } |
|
589 | + } |
|
590 | + return $count; |
|
591 | + } |
|
592 | + |
|
593 | + |
|
594 | + |
|
595 | + /** |
|
596 | + * _release_reserved_ticket_for_registration |
|
597 | + * releases reserved tickets for an EE_Registration |
|
598 | + * by default, will NOT release tickets for APPROVED registrations |
|
599 | + * |
|
600 | + * @access protected |
|
601 | + * @param EE_Registration $registration |
|
602 | + * @param EE_Transaction $transaction |
|
603 | + * @return int |
|
604 | + * @throws EE_Error |
|
605 | + */ |
|
606 | + protected function _release_reserved_ticket_for_registration( |
|
607 | + EE_Registration $registration, |
|
608 | + EE_Transaction $transaction |
|
609 | + ) { |
|
610 | + $STS_ID = $transaction->status_ID(); |
|
611 | + if (self::debug) { |
|
612 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
613 | + echo '<br /> . . registration->ID: ' . $registration->ID(); |
|
614 | + echo '<br /> . . registration->status_ID: ' . $registration->status_ID(); |
|
615 | + echo '<br /> . . transaction->status_ID(): ' . $STS_ID; |
|
616 | + } |
|
617 | + if ( |
|
618 | + // release Tickets for Failed Transactions and Abandoned Transactions |
|
619 | + $STS_ID === EEM_Transaction::failed_status_code |
|
620 | + || $STS_ID === EEM_Transaction::abandoned_status_code |
|
621 | + || ( |
|
622 | + // also release Tickets for Incomplete Transactions, but ONLY if the Registrations are NOT Approved |
|
623 | + $STS_ID === EEM_Transaction::incomplete_status_code |
|
624 | + && $registration->status_ID() !== EEM_Registration::status_id_approved |
|
625 | + ) |
|
626 | + ) { |
|
627 | + $ticket = $registration->ticket(); |
|
628 | + if ($ticket instanceof EE_Ticket) { |
|
629 | + return $this->_release_reserved_ticket($ticket); |
|
630 | + } |
|
631 | + } |
|
632 | + return 0; |
|
633 | + } |
|
634 | + |
|
635 | + |
|
636 | + |
|
637 | + /********************************** SESSION_CART_RESET **********************************/ |
|
638 | + |
|
639 | + |
|
640 | + |
|
641 | + /** |
|
642 | + * session_cart_reset |
|
643 | + * callback hooked into 'AHEE__EE_Session__reset_cart__before_reset' |
|
644 | + * |
|
645 | + * @access public |
|
646 | + * @param EE_Session $session |
|
647 | + * @return void |
|
648 | + * @throws EE_Error |
|
649 | + */ |
|
650 | + public static function session_cart_reset(EE_Session $session) |
|
651 | + { |
|
652 | + if (self::debug) { |
|
653 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
654 | + } |
|
655 | + $cart = $session->cart(); |
|
656 | + if ($cart instanceof EE_Cart) { |
|
657 | + if (self::debug) { |
|
658 | + echo '<br /><br /> cart instance of EE_Cart: '; |
|
659 | + } |
|
660 | + EED_Ticket_Sales_Monitor::instance()->_session_cart_reset($cart); |
|
661 | + } else { |
|
662 | + if (self::debug) { |
|
663 | + echo '<br /><br /> invalid EE_Cart: '; |
|
664 | + var_dump($cart); |
|
665 | + } |
|
666 | + } |
|
667 | + } |
|
668 | + |
|
669 | + |
|
670 | + |
|
671 | + /** |
|
672 | + * _session_cart_reset |
|
673 | + * releases reserved tickets in the EE_Cart |
|
674 | + * |
|
675 | + * @access protected |
|
676 | + * @param EE_Cart $cart |
|
677 | + * @return void |
|
678 | + * @throws EE_Error |
|
679 | + */ |
|
680 | + protected function _session_cart_reset(EE_Cart $cart) |
|
681 | + { |
|
682 | + if (self::debug) { |
|
683 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
684 | + } |
|
685 | + EE_Registry::instance()->load_helper('Line_Item'); |
|
686 | + $ticket_line_items = $cart->get_tickets(); |
|
687 | + if (empty($ticket_line_items)) { |
|
688 | + return; |
|
689 | + } |
|
690 | + foreach ($ticket_line_items as $ticket_line_item) { |
|
691 | + if (self::debug) { |
|
692 | + echo '<br /> . ticket_line_item->ID(): ' . $ticket_line_item->ID(); |
|
693 | + } |
|
694 | + if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') { |
|
695 | + if (self::debug) { |
|
696 | + echo '<br /> . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID(); |
|
697 | + } |
|
698 | + $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID()); |
|
699 | + if ($ticket instanceof EE_Ticket) { |
|
700 | + if (self::debug) { |
|
701 | + echo '<br /> . . ticket->ID(): ' . $ticket->ID(); |
|
702 | + echo '<br /> . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity(); |
|
703 | + } |
|
704 | + $this->_release_reserved_ticket($ticket, $ticket_line_item->quantity()); |
|
705 | + } |
|
706 | + } |
|
707 | + } |
|
708 | + if (self::debug) { |
|
709 | + echo '<br /><br /> RESET COMPLETED '; |
|
710 | + } |
|
711 | + } |
|
712 | + |
|
713 | + |
|
714 | + |
|
715 | + /********************************** SESSION_CHECKOUT_RESET **********************************/ |
|
716 | + |
|
717 | + |
|
718 | + |
|
719 | + /** |
|
720 | + * session_checkout_reset |
|
721 | + * callback hooked into 'AHEE__EE_Session__reset_checkout__before_reset' |
|
722 | + * |
|
723 | + * @access public |
|
724 | + * @param EE_Session $session |
|
725 | + * @return void |
|
726 | + * @throws EE_Error |
|
727 | + */ |
|
728 | + public static function session_checkout_reset(EE_Session $session) |
|
729 | + { |
|
730 | + $checkout = $session->checkout(); |
|
731 | + if ($checkout instanceof EE_Checkout) { |
|
732 | + EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout); |
|
733 | + } |
|
734 | + } |
|
735 | + |
|
736 | + |
|
737 | + |
|
738 | + /** |
|
739 | + * _session_checkout_reset |
|
740 | + * releases reserved tickets for the EE_Checkout->transaction |
|
741 | + * |
|
742 | + * @access protected |
|
743 | + * @param EE_Checkout $checkout |
|
744 | + * @return void |
|
745 | + * @throws EE_Error |
|
746 | + */ |
|
747 | + protected function _session_checkout_reset(EE_Checkout $checkout) |
|
748 | + { |
|
749 | + if (self::debug) { |
|
750 | + echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
751 | + } |
|
752 | + // we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit |
|
753 | + if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) { |
|
754 | + return; |
|
755 | + } |
|
756 | + $this->_release_all_reserved_tickets_for_transaction($checkout->transaction); |
|
757 | + } |
|
758 | + |
|
759 | + |
|
760 | + |
|
761 | + /********************************** SESSION_EXPIRED_RESET **********************************/ |
|
762 | + |
|
763 | + |
|
764 | + |
|
765 | + /** |
|
766 | + * session_expired_reset |
|
767 | + * |
|
768 | + * @access public |
|
769 | + * @param EE_Session $session |
|
770 | + * @return void |
|
771 | + */ |
|
772 | + public static function session_expired_reset(EE_Session $session) |
|
773 | + { |
|
774 | + } |
|
775 | + |
|
776 | + |
|
777 | + |
|
778 | + /********************************** PROCESS_ABANDONED_TRANSACTIONS **********************************/ |
|
779 | + |
|
780 | + |
|
781 | + |
|
782 | + /** |
|
783 | + * process_abandoned_transactions |
|
784 | + * releases reserved tickets for all registrations of an ABANDONED EE_Transaction |
|
785 | + * by default, will NOT release tickets for free transactions, or any that have received a payment |
|
786 | + * |
|
787 | + * @access public |
|
788 | + * @param EE_Transaction $transaction |
|
789 | + * @return void |
|
790 | + * @throws EE_Error |
|
791 | + */ |
|
792 | + public static function process_abandoned_transactions(EE_Transaction $transaction) |
|
793 | + { |
|
794 | + // is this TXN free or has any money been paid towards this TXN? If so, then leave it alone |
|
795 | + if ($transaction->is_free() || $transaction->paid() > 0) { |
|
796 | + if (self::debug) { |
|
797 | + // DEBUG LOG |
|
798 | + EEH_Debug_Tools::log( |
|
799 | + __CLASS__, __FUNCTION__, __LINE__, |
|
800 | + array($transaction), |
|
801 | + false, 'EE_Transaction: ' . $transaction->ID() |
|
802 | + ); |
|
803 | + } |
|
804 | + return; |
|
805 | + } |
|
806 | + // have their been any successful payments made ? |
|
807 | + $payments = $transaction->payments(); |
|
808 | + foreach ($payments as $payment) { |
|
809 | + if ($payment instanceof EE_Payment && $payment->status() === EEM_Payment::status_id_approved) { |
|
810 | + if (self::debug) { |
|
811 | + // DEBUG LOG |
|
812 | + EEH_Debug_Tools::log( |
|
813 | + __CLASS__, __FUNCTION__, __LINE__, |
|
814 | + array($payment), |
|
815 | + false, 'EE_Transaction: ' . $transaction->ID() |
|
816 | + ); |
|
817 | + } |
|
818 | + return; |
|
819 | + } |
|
820 | + } |
|
821 | + // since you haven't even attempted to pay for your ticket... |
|
822 | + EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction); |
|
823 | + } |
|
824 | + |
|
825 | + |
|
826 | + |
|
827 | + /********************************** PROCESS_FAILED_TRANSACTIONS **********************************/ |
|
828 | + |
|
829 | + |
|
830 | + |
|
831 | + /** |
|
832 | + * process_abandoned_transactions |
|
833 | + * releases reserved tickets for absolutely ALL registrations of a FAILED EE_Transaction |
|
834 | + * |
|
835 | + * @access public |
|
836 | + * @param EE_Transaction $transaction |
|
837 | + * @return void |
|
838 | + * @throws EE_Error |
|
839 | + */ |
|
840 | + public static function process_failed_transactions(EE_Transaction $transaction) |
|
841 | + { |
|
842 | + // since you haven't even attempted to pay for your ticket... |
|
843 | + EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction); |
|
844 | + } |
|
845 | + |
|
846 | + |
|
847 | + |
|
848 | + /********************************** RESET RESERVATION COUNTS *********************************/ |
|
849 | + |
|
850 | + |
|
851 | + |
|
852 | + /** |
|
853 | + * Resets all ticket and datetime reserved counts to zero |
|
854 | + * Tickets that are currently associated with a Transaction that is in progress |
|
855 | + * |
|
856 | + * @throws \EE_Error |
|
857 | + * @throws \DomainException |
|
858 | + */ |
|
859 | + public static function reset_reservation_counts() |
|
860 | + { |
|
861 | + /** @var EE_Line_Item[] $valid_reserved_tickets */ |
|
862 | + $valid_reserved_tickets = array(); |
|
863 | + $transactions_in_progress = EEM_Transaction::instance()->get_transactions_in_progress(); |
|
864 | + foreach ($transactions_in_progress as $transaction_in_progress) { |
|
865 | + // if this TXN has been fully completed, then skip it |
|
866 | + if ($transaction_in_progress->reg_step_completed('finalize_registration')) { |
|
867 | + continue; |
|
868 | + } |
|
869 | + /** @var EE_Transaction $transaction_in_progress */ |
|
870 | + $total_line_item = $transaction_in_progress->total_line_item(); |
|
871 | + // $transaction_in_progress->line |
|
872 | + if (! $total_line_item instanceof EE_Line_Item) { |
|
873 | + throw new DomainException( |
|
874 | + esc_html__('Transaction does not have a valid Total Line Item associated with it.', 'event_espresso') |
|
875 | + ); |
|
876 | + } |
|
877 | + $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total( |
|
878 | + $total_line_item |
|
879 | + ); |
|
880 | + } |
|
881 | + $total_line_items = EEM_Line_Item::instance()->get_total_line_items_for_active_carts(); |
|
882 | + foreach ($total_line_items as $total_line_item) { |
|
883 | + $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total( |
|
884 | + $total_line_item |
|
885 | + ); |
|
886 | + } |
|
887 | + return EED_Ticket_Sales_Monitor::release_reservations_for_tickets( |
|
888 | + EEM_Ticket::instance()->get_tickets_with_reservations(), |
|
889 | + $valid_reserved_tickets |
|
890 | + ); |
|
891 | + } |
|
892 | + |
|
893 | + |
|
894 | + |
|
895 | + /** |
|
896 | + * @param EE_Line_Item $total_line_item |
|
897 | + * @return EE_Line_Item[] |
|
898 | + */ |
|
899 | + private static function get_ticket_line_items_for_grand_total(EE_Line_Item $total_line_item) |
|
900 | + { |
|
901 | + /** @var EE_Line_Item[] $valid_reserved_tickets */ |
|
902 | + $valid_reserved_tickets = array(); |
|
903 | + $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item); |
|
904 | + foreach ($ticket_line_items as $ticket_line_item) { |
|
905 | + if ($ticket_line_item instanceof EE_Line_Item) { |
|
906 | + $valid_reserved_tickets[] = $ticket_line_item; |
|
907 | + } |
|
908 | + } |
|
909 | + return $valid_reserved_tickets; |
|
910 | + } |
|
911 | + |
|
912 | + |
|
913 | + |
|
914 | + /** |
|
915 | + * @param EE_Ticket[] $tickets_with_reservations |
|
916 | + * @param EE_Line_Item[] $valid_reserved_ticket_line_items |
|
917 | + * @return int |
|
918 | + * @throws \EE_Error |
|
919 | + */ |
|
920 | + private static function release_reservations_for_tickets( |
|
921 | + array $tickets_with_reservations, |
|
922 | + $valid_reserved_ticket_line_items = array() |
|
923 | + ) { |
|
924 | + $total_tickets_released = 0; |
|
925 | + foreach ($tickets_with_reservations as $ticket_with_reservations) { |
|
926 | + if (! $ticket_with_reservations instanceof EE_Ticket) { |
|
927 | + continue; |
|
928 | + } |
|
929 | + $reserved_qty = $ticket_with_reservations->reserved(); |
|
930 | + foreach ($valid_reserved_ticket_line_items as $valid_reserved_ticket_line_item) { |
|
931 | + if ( |
|
932 | + $valid_reserved_ticket_line_item instanceof EE_Line_Item |
|
933 | + && $valid_reserved_ticket_line_item->OBJ_ID() === $ticket_with_reservations->ID() |
|
934 | + ) { |
|
935 | + $reserved_qty -= $valid_reserved_ticket_line_item->quantity(); |
|
936 | + } |
|
937 | + } |
|
938 | + if ($reserved_qty > 0) { |
|
939 | + $ticket_with_reservations->decrease_reserved($reserved_qty); |
|
940 | + $ticket_with_reservations->save(); |
|
941 | + $total_tickets_released += $reserved_qty; |
|
942 | + } |
|
943 | + } |
|
944 | + return $total_tickets_released; |
|
945 | + } |
|
946 | + |
|
947 | + |
|
948 | + |
|
949 | + /********************************** SHUTDOWN **********************************/ |
|
950 | + |
|
951 | + |
|
952 | + |
|
953 | + /** |
|
954 | + * @return false|int |
|
955 | + * @throws \EE_Error |
|
956 | + */ |
|
957 | + public static function clear_expired_line_items_with_no_transaction() |
|
958 | + { |
|
959 | + /** @type WPDB $wpdb */ |
|
960 | + global $wpdb; |
|
961 | + return $wpdb->query( |
|
962 | + $wpdb->prepare( |
|
963 | + 'DELETE FROM ' . EEM_Line_Item::instance()->table() . ' |
|
964 | 964 | WHERE TXN_ID = 0 AND LIN_timestamp <= %s', |
965 | - // use GMT time because that's what LIN_timestamps are in |
|
966 | - date('Y-m-d H:i:s', time() - EE_Registry::instance()->SSN->lifespan()) |
|
967 | - ) |
|
968 | - ); |
|
969 | - } |
|
965 | + // use GMT time because that's what LIN_timestamps are in |
|
966 | + date('Y-m-d H:i:s', time() - EE_Registry::instance()->SSN->lifespan()) |
|
967 | + ) |
|
968 | + ); |
|
969 | + } |
|
970 | 970 | |
971 | 971 | } |
972 | 972 | // End of file EED_Ticket_Sales_Monitor.module.php |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | class EED_Ticket_Sales_Monitor extends EED_Module |
22 | 22 | { |
23 | 23 | |
24 | - const debug = false; // true false |
|
24 | + const debug = false; // true false |
|
25 | 25 | |
26 | 26 | /** |
27 | 27 | * an array of raw ticket data from EED_Ticket_Selector |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | $expired_ticket_IDs = array(); |
182 | 182 | $valid_ticket_line_items = array(); |
183 | 183 | $total_line_items = EEM_Line_Item::instance()->get_total_line_items_with_no_transaction(); |
184 | - if(empty($total_line_items)){ |
|
184 | + if (empty($total_line_items)) { |
|
185 | 185 | return; |
186 | 186 | } |
187 | 187 | $expired = current_time('timestamp') - EE_Registry::instance()->SSN->lifespan(); |
@@ -189,17 +189,17 @@ discard block |
||
189 | 189 | /** @var EE_Line_Item $total_line_item */ |
190 | 190 | $ticket_line_items = EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total($total_line_item); |
191 | 191 | foreach ($ticket_line_items as $ticket_line_item) { |
192 | - if(! $ticket_line_item instanceof EE_Line_Item) { |
|
192 | + if ( ! $ticket_line_item instanceof EE_Line_Item) { |
|
193 | 193 | continue; |
194 | 194 | } |
195 | - if ($total_line_item->timestamp(true) <= $expired ) { |
|
195 | + if ($total_line_item->timestamp(true) <= $expired) { |
|
196 | 196 | $expired_ticket_IDs[$ticket_line_item->OBJ_ID()] = $ticket_line_item->OBJ_ID(); |
197 | 197 | } else { |
198 | 198 | $valid_ticket_line_items[$ticket_line_item->OBJ_ID()] = $ticket_line_item; |
199 | 199 | } |
200 | 200 | } |
201 | 201 | } |
202 | - if (! empty($expired_ticket_IDs)) { |
|
202 | + if ( ! empty($expired_ticket_IDs)) { |
|
203 | 203 | EED_Ticket_Sales_Monitor::release_reservations_for_tickets( |
204 | 204 | \EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs), |
205 | 205 | $valid_ticket_line_items |
@@ -237,8 +237,8 @@ discard block |
||
237 | 237 | $qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty); |
238 | 238 | } |
239 | 239 | if (self::debug) { |
240 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '()'; |
|
241 | - echo '<br /><br /><b> RETURNED QTY: ' . $qty . '</b>'; |
|
240 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'()'; |
|
241 | + echo '<br /><br /><b> RETURNED QTY: '.$qty.'</b>'; |
|
242 | 242 | } |
243 | 243 | return $qty; |
244 | 244 | } |
@@ -259,36 +259,36 @@ discard block |
||
259 | 259 | protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1) |
260 | 260 | { |
261 | 261 | if (self::debug) { |
262 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
262 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() '; |
|
263 | 263 | } |
264 | 264 | if ( ! $ticket instanceof EE_Ticket) { |
265 | 265 | return 0; |
266 | 266 | } |
267 | 267 | if (self::debug) { |
268 | - echo '<br /><b> . ticket->ID: ' . $ticket->ID() . '</b>'; |
|
269 | - echo '<br /> . original ticket->reserved: ' . $ticket->reserved(); |
|
268 | + echo '<br /><b> . ticket->ID: '.$ticket->ID().'</b>'; |
|
269 | + echo '<br /> . original ticket->reserved: '.$ticket->reserved(); |
|
270 | 270 | } |
271 | 271 | $ticket->refresh_from_db(); |
272 | 272 | // first let's determine the ticket availability based on sales |
273 | 273 | $available = $ticket->qty('saleable'); |
274 | 274 | if (self::debug) { |
275 | - echo '<br /> . . . ticket->qty: ' . $ticket->qty(); |
|
276 | - echo '<br /> . . . ticket->sold: ' . $ticket->sold(); |
|
277 | - echo '<br /> . . . ticket->reserved: ' . $ticket->reserved(); |
|
278 | - echo '<br /> . . . ticket->qty(saleable): ' . $ticket->qty('saleable'); |
|
279 | - echo '<br /> . . . available: ' . $available; |
|
275 | + echo '<br /> . . . ticket->qty: '.$ticket->qty(); |
|
276 | + echo '<br /> . . . ticket->sold: '.$ticket->sold(); |
|
277 | + echo '<br /> . . . ticket->reserved: '.$ticket->reserved(); |
|
278 | + echo '<br /> . . . ticket->qty(saleable): '.$ticket->qty('saleable'); |
|
279 | + echo '<br /> . . . available: '.$available; |
|
280 | 280 | } |
281 | 281 | if ($available < 1) { |
282 | 282 | $this->_ticket_sold_out($ticket); |
283 | 283 | return 0; |
284 | 284 | } |
285 | 285 | if (self::debug) { |
286 | - echo '<br /> . . . qty: ' . $qty; |
|
286 | + echo '<br /> . . . qty: '.$qty; |
|
287 | 287 | } |
288 | 288 | if ($available < $qty) { |
289 | 289 | $qty = $available; |
290 | 290 | if (self::debug) { |
291 | - echo '<br /> . . . QTY ADJUSTED: ' . $qty; |
|
291 | + echo '<br /> . . . QTY ADJUSTED: '.$qty; |
|
292 | 292 | } |
293 | 293 | $this->_ticket_quantity_decremented($ticket); |
294 | 294 | } |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1) |
312 | 312 | { |
313 | 313 | if (self::debug) { |
314 | - echo '<br /><br /> . . . INCREASE RESERVED: ' . $quantity; |
|
314 | + echo '<br /><br /> . . . INCREASE RESERVED: '.$quantity; |
|
315 | 315 | } |
316 | 316 | $ticket->increase_reserved($quantity); |
317 | 317 | return $ticket->save(); |
@@ -331,12 +331,12 @@ discard block |
||
331 | 331 | protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1) |
332 | 332 | { |
333 | 333 | if (self::debug) { |
334 | - echo '<br /> . . . ticket->ID: ' . $ticket->ID(); |
|
335 | - echo '<br /> . . . ticket->reserved: ' . $ticket->reserved(); |
|
334 | + echo '<br /> . . . ticket->ID: '.$ticket->ID(); |
|
335 | + echo '<br /> . . . ticket->reserved: '.$ticket->reserved(); |
|
336 | 336 | } |
337 | 337 | $ticket->decrease_reserved($quantity); |
338 | 338 | if (self::debug) { |
339 | - echo '<br /> . . . ticket->reserved: ' . $ticket->reserved(); |
|
339 | + echo '<br /> . . . ticket->reserved: '.$ticket->reserved(); |
|
340 | 340 | } |
341 | 341 | return $ticket->save() ? 1 : 0; |
342 | 342 | } |
@@ -356,8 +356,8 @@ discard block |
||
356 | 356 | protected function _ticket_sold_out(EE_Ticket $ticket) |
357 | 357 | { |
358 | 358 | if (self::debug) { |
359 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
360 | - echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket); |
|
359 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() '; |
|
360 | + echo '<br /> . . ticket->name: '.$this->_get_ticket_and_event_name($ticket); |
|
361 | 361 | } |
362 | 362 | $this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket); |
363 | 363 | } |
@@ -377,8 +377,8 @@ discard block |
||
377 | 377 | protected function _ticket_quantity_decremented(EE_Ticket $ticket) |
378 | 378 | { |
379 | 379 | if (self::debug) { |
380 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
381 | - echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket); |
|
380 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() '; |
|
381 | + echo '<br /> . . ticket->name: '.$this->_get_ticket_and_event_name($ticket); |
|
382 | 382 | } |
383 | 383 | $this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket); |
384 | 384 | } |
@@ -485,7 +485,7 @@ discard block |
||
485 | 485 | protected function _post_notices() |
486 | 486 | { |
487 | 487 | if (self::debug) { |
488 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
488 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() '; |
|
489 | 489 | } |
490 | 490 | $refresh_msg = ''; |
491 | 491 | $none_added_msg = ''; |
@@ -552,8 +552,8 @@ discard block |
||
552 | 552 | protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction) |
553 | 553 | { |
554 | 554 | if (self::debug) { |
555 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
556 | - echo '<br /> . transaction->ID: ' . $transaction->ID(); |
|
555 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() '; |
|
556 | + echo '<br /> . transaction->ID: '.$transaction->ID(); |
|
557 | 557 | } |
558 | 558 | // check if 'finalize_registration' step has been completed... |
559 | 559 | $finalized = $transaction->reg_step_completed('finalize_registration'); |
@@ -562,13 +562,13 @@ discard block |
||
562 | 562 | EEH_Debug_Tools::log( |
563 | 563 | __CLASS__, __FUNCTION__, __LINE__, |
564 | 564 | array('finalized' => $finalized), |
565 | - false, 'EE_Transaction: ' . $transaction->ID() |
|
565 | + false, 'EE_Transaction: '.$transaction->ID() |
|
566 | 566 | ); |
567 | 567 | } |
568 | 568 | // how many tickets were released |
569 | 569 | $count = 0; |
570 | 570 | if (self::debug) { |
571 | - echo '<br /> . . . finalized: ' . $finalized; |
|
571 | + echo '<br /> . . . finalized: '.$finalized; |
|
572 | 572 | } |
573 | 573 | $release_tickets_with_TXN_status = array( |
574 | 574 | EEM_Transaction::failed_status_code, |
@@ -609,10 +609,10 @@ discard block |
||
609 | 609 | ) { |
610 | 610 | $STS_ID = $transaction->status_ID(); |
611 | 611 | if (self::debug) { |
612 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
613 | - echo '<br /> . . registration->ID: ' . $registration->ID(); |
|
614 | - echo '<br /> . . registration->status_ID: ' . $registration->status_ID(); |
|
615 | - echo '<br /> . . transaction->status_ID(): ' . $STS_ID; |
|
612 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() '; |
|
613 | + echo '<br /> . . registration->ID: '.$registration->ID(); |
|
614 | + echo '<br /> . . registration->status_ID: '.$registration->status_ID(); |
|
615 | + echo '<br /> . . transaction->status_ID(): '.$STS_ID; |
|
616 | 616 | } |
617 | 617 | if ( |
618 | 618 | // release Tickets for Failed Transactions and Abandoned Transactions |
@@ -650,7 +650,7 @@ discard block |
||
650 | 650 | public static function session_cart_reset(EE_Session $session) |
651 | 651 | { |
652 | 652 | if (self::debug) { |
653 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
653 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() '; |
|
654 | 654 | } |
655 | 655 | $cart = $session->cart(); |
656 | 656 | if ($cart instanceof EE_Cart) { |
@@ -680,7 +680,7 @@ discard block |
||
680 | 680 | protected function _session_cart_reset(EE_Cart $cart) |
681 | 681 | { |
682 | 682 | if (self::debug) { |
683 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
683 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() '; |
|
684 | 684 | } |
685 | 685 | EE_Registry::instance()->load_helper('Line_Item'); |
686 | 686 | $ticket_line_items = $cart->get_tickets(); |
@@ -689,17 +689,17 @@ discard block |
||
689 | 689 | } |
690 | 690 | foreach ($ticket_line_items as $ticket_line_item) { |
691 | 691 | if (self::debug) { |
692 | - echo '<br /> . ticket_line_item->ID(): ' . $ticket_line_item->ID(); |
|
692 | + echo '<br /> . ticket_line_item->ID(): '.$ticket_line_item->ID(); |
|
693 | 693 | } |
694 | 694 | if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') { |
695 | 695 | if (self::debug) { |
696 | - echo '<br /> . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID(); |
|
696 | + echo '<br /> . . ticket_line_item->OBJ_ID(): '.$ticket_line_item->OBJ_ID(); |
|
697 | 697 | } |
698 | 698 | $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID()); |
699 | 699 | if ($ticket instanceof EE_Ticket) { |
700 | 700 | if (self::debug) { |
701 | - echo '<br /> . . ticket->ID(): ' . $ticket->ID(); |
|
702 | - echo '<br /> . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity(); |
|
701 | + echo '<br /> . . ticket->ID(): '.$ticket->ID(); |
|
702 | + echo '<br /> . . ticket_line_item->quantity(): '.$ticket_line_item->quantity(); |
|
703 | 703 | } |
704 | 704 | $this->_release_reserved_ticket($ticket, $ticket_line_item->quantity()); |
705 | 705 | } |
@@ -747,7 +747,7 @@ discard block |
||
747 | 747 | protected function _session_checkout_reset(EE_Checkout $checkout) |
748 | 748 | { |
749 | 749 | if (self::debug) { |
750 | - echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() '; |
|
750 | + echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() '; |
|
751 | 751 | } |
752 | 752 | // we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit |
753 | 753 | if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) { |
@@ -798,7 +798,7 @@ discard block |
||
798 | 798 | EEH_Debug_Tools::log( |
799 | 799 | __CLASS__, __FUNCTION__, __LINE__, |
800 | 800 | array($transaction), |
801 | - false, 'EE_Transaction: ' . $transaction->ID() |
|
801 | + false, 'EE_Transaction: '.$transaction->ID() |
|
802 | 802 | ); |
803 | 803 | } |
804 | 804 | return; |
@@ -812,7 +812,7 @@ discard block |
||
812 | 812 | EEH_Debug_Tools::log( |
813 | 813 | __CLASS__, __FUNCTION__, __LINE__, |
814 | 814 | array($payment), |
815 | - false, 'EE_Transaction: ' . $transaction->ID() |
|
815 | + false, 'EE_Transaction: '.$transaction->ID() |
|
816 | 816 | ); |
817 | 817 | } |
818 | 818 | return; |
@@ -869,7 +869,7 @@ discard block |
||
869 | 869 | /** @var EE_Transaction $transaction_in_progress */ |
870 | 870 | $total_line_item = $transaction_in_progress->total_line_item(); |
871 | 871 | // $transaction_in_progress->line |
872 | - if (! $total_line_item instanceof EE_Line_Item) { |
|
872 | + if ( ! $total_line_item instanceof EE_Line_Item) { |
|
873 | 873 | throw new DomainException( |
874 | 874 | esc_html__('Transaction does not have a valid Total Line Item associated with it.', 'event_espresso') |
875 | 875 | ); |
@@ -923,7 +923,7 @@ discard block |
||
923 | 923 | ) { |
924 | 924 | $total_tickets_released = 0; |
925 | 925 | foreach ($tickets_with_reservations as $ticket_with_reservations) { |
926 | - if (! $ticket_with_reservations instanceof EE_Ticket) { |
|
926 | + if ( ! $ticket_with_reservations instanceof EE_Ticket) { |
|
927 | 927 | continue; |
928 | 928 | } |
929 | 929 | $reserved_qty = $ticket_with_reservations->reserved(); |
@@ -960,7 +960,7 @@ discard block |
||
960 | 960 | global $wpdb; |
961 | 961 | return $wpdb->query( |
962 | 962 | $wpdb->prepare( |
963 | - 'DELETE FROM ' . EEM_Line_Item::instance()->table() . ' |
|
963 | + 'DELETE FROM '.EEM_Line_Item::instance()->table().' |
|
964 | 964 | WHERE TXN_ID = 0 AND LIN_timestamp <= %s', |
965 | 965 | // use GMT time because that's what LIN_timestamps are in |
966 | 966 | date('Y-m-d H:i:s', time() - EE_Registry::instance()->SSN->lifespan()) |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | * the interaction of EE_Transaction and EE_Registration model objects |
8 | 8 | * Provides methods for manipulating and processing changes to an EE_Transaction |
9 | 9 | * and it's related EE_Registrations with regards to the checkout/registration process |
10 | - |
|
11 | -* |
|
10 | + * |
|
12 | 11 | *@package Event Espresso |
13 | 12 | * @subpackage core |
14 | 13 | * @author Brent Christensen |
@@ -177,24 +176,24 @@ discard block |
||
177 | 176 | |
178 | 177 | |
179 | 178 | |
180 | - /** |
|
181 | - * update_transaction_and_registrations_after_checkout_or_payment |
|
182 | - * cycles thru related registrations and calls update_registration_after_checkout_or_payment() on each |
|
183 | - * |
|
184 | - * @param EE_Transaction $transaction |
|
185 | - * @param \EE_Payment | NULL $payment |
|
186 | - * @param array $registration_query_params array of query WHERE params to use |
|
187 | - * when retrieving cached registrations from a transaction |
|
188 | - * @param bool $trigger_notifications whether or not to call |
|
189 | - * \EE_Registration_Processor::trigger_registration_update_notifications() |
|
190 | - * @return array |
|
191 | - * @throws \EE_Error |
|
192 | - */ |
|
179 | + /** |
|
180 | + * update_transaction_and_registrations_after_checkout_or_payment |
|
181 | + * cycles thru related registrations and calls update_registration_after_checkout_or_payment() on each |
|
182 | + * |
|
183 | + * @param EE_Transaction $transaction |
|
184 | + * @param \EE_Payment | NULL $payment |
|
185 | + * @param array $registration_query_params array of query WHERE params to use |
|
186 | + * when retrieving cached registrations from a transaction |
|
187 | + * @param bool $trigger_notifications whether or not to call |
|
188 | + * \EE_Registration_Processor::trigger_registration_update_notifications() |
|
189 | + * @return array |
|
190 | + * @throws \EE_Error |
|
191 | + */ |
|
193 | 192 | public function update_transaction_and_registrations_after_checkout_or_payment( |
194 | 193 | EE_Transaction $transaction, |
195 | 194 | $payment = null, |
196 | 195 | $registration_query_params = array(), |
197 | - $trigger_notifications = true |
|
196 | + $trigger_notifications = true |
|
198 | 197 | ) { |
199 | 198 | // make sure some query params are set for retrieving registrations |
200 | 199 | $this->_set_registration_query_params( $registration_query_params ); |
@@ -224,15 +223,15 @@ discard block |
||
224 | 223 | $update_params |
225 | 224 | ); |
226 | 225 | if ($trigger_notifications) { |
227 | - // send messages |
|
228 | - /** @type EE_Registration_Processor $registration_processor */ |
|
229 | - $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
230 | - $registration_processor->trigger_registration_update_notifications( |
|
231 | - $transaction->primary_registration(), |
|
232 | - $update_params |
|
233 | - ); |
|
234 | - } |
|
235 | - do_action( |
|
226 | + // send messages |
|
227 | + /** @type EE_Registration_Processor $registration_processor */ |
|
228 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
229 | + $registration_processor->trigger_registration_update_notifications( |
|
230 | + $transaction->primary_registration(), |
|
231 | + $update_params |
|
232 | + ); |
|
233 | + } |
|
234 | + do_action( |
|
236 | 235 | 'AHEE__EE_Transaction_Processor__update_transaction_and_registrations_after_checkout_or_payment', |
237 | 236 | $transaction, |
238 | 237 | $update_params |
@@ -242,17 +241,17 @@ discard block |
||
242 | 241 | |
243 | 242 | |
244 | 243 | |
245 | - /** |
|
246 | - * update_transaction_after_registration_reopened |
|
247 | - * readjusts TXN and Line Item totals after a registration is changed from |
|
248 | - * cancelled or declined to another reg status such as pending payment or approved |
|
249 | - * |
|
250 | - * @param \EE_Registration $registration |
|
251 | - * @param array $closed_reg_statuses |
|
252 | - * @param bool $update_txn |
|
253 | - * @return bool |
|
254 | - * @throws \EE_Error |
|
255 | - */ |
|
244 | + /** |
|
245 | + * update_transaction_after_registration_reopened |
|
246 | + * readjusts TXN and Line Item totals after a registration is changed from |
|
247 | + * cancelled or declined to another reg status such as pending payment or approved |
|
248 | + * |
|
249 | + * @param \EE_Registration $registration |
|
250 | + * @param array $closed_reg_statuses |
|
251 | + * @param bool $update_txn |
|
252 | + * @return bool |
|
253 | + * @throws \EE_Error |
|
254 | + */ |
|
256 | 255 | public function update_transaction_after_reinstating_canceled_registration( |
257 | 256 | EE_Registration $registration, |
258 | 257 | $closed_reg_statuses = array(), |
@@ -314,15 +313,15 @@ discard block |
||
314 | 313 | try { |
315 | 314 | $transaction = $this->get_transaction_for_registration( $registration ); |
316 | 315 | if ( |
317 | - apply_filters( |
|
318 | - 'FHEE__EE_Transaction_Processor__update_transaction_after_canceled_or_declined_registration__cancel_ticket_line_item', |
|
319 | - true, |
|
320 | - $registration, |
|
321 | - $transaction |
|
322 | - ) |
|
323 | - ){ |
|
324 | - $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration( $transaction, $registration ); |
|
325 | - EEH_Line_Item::cancel_ticket_line_item( $ticket_line_item ); |
|
316 | + apply_filters( |
|
317 | + 'FHEE__EE_Transaction_Processor__update_transaction_after_canceled_or_declined_registration__cancel_ticket_line_item', |
|
318 | + true, |
|
319 | + $registration, |
|
320 | + $transaction |
|
321 | + ) |
|
322 | + ){ |
|
323 | + $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration( $transaction, $registration ); |
|
324 | + EEH_Line_Item::cancel_ticket_line_item( $ticket_line_item ); |
|
326 | 325 | } |
327 | 326 | } catch ( EE_Error $e ) { |
328 | 327 | EE_Error::add_error( |
@@ -532,7 +531,7 @@ discard block |
||
532 | 531 | foreach ( $available_payment_methods as $available_payment_method ) { |
533 | 532 | if ( |
534 | 533 | $available_payment_method instanceof EE_Payment_Method |
535 | - && $available_payment_method->open_by_default() |
|
534 | + && $available_payment_method->open_by_default() |
|
536 | 535 | ) { |
537 | 536 | $PMD_ID = $available_payment_method->ID(); |
538 | 537 | break; |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
2 | -EE_Registry::instance()->load_class( 'Processor_Base' ); |
|
2 | +EE_Registry::instance()->load_class('Processor_Base'); |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * Class EE_Transaction_Processor |
@@ -50,10 +50,10 @@ discard block |
||
50 | 50 | * @param array $registration_query_params |
51 | 51 | *@return EE_Transaction_Processor instance |
52 | 52 | */ |
53 | - public static function instance( $registration_query_params = array() ) { |
|
53 | + public static function instance($registration_query_params = array()) { |
|
54 | 54 | // check if class object is instantiated |
55 | - if ( ! self::$_instance instanceof EE_Transaction_Processor ) { |
|
56 | - self::$_instance = new self( $registration_query_params ); |
|
55 | + if ( ! self::$_instance instanceof EE_Transaction_Processor) { |
|
56 | + self::$_instance = new self($registration_query_params); |
|
57 | 57 | } |
58 | 58 | return self::$_instance; |
59 | 59 | } |
@@ -63,9 +63,9 @@ discard block |
||
63 | 63 | /** |
64 | 64 | * @param array $registration_query_params |
65 | 65 | */ |
66 | - private function __construct( $registration_query_params = array() ) { |
|
66 | + private function __construct($registration_query_params = array()) { |
|
67 | 67 | // make sure some query params are set for retrieving registrations |
68 | - $this->_set_registration_query_params( $registration_query_params ); |
|
68 | + $this->_set_registration_query_params($registration_query_params); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | |
@@ -74,8 +74,8 @@ discard block |
||
74 | 74 | * @access private |
75 | 75 | * @param array $registration_query_params |
76 | 76 | */ |
77 | - private function _set_registration_query_params( $registration_query_params ) { |
|
78 | - $this->_registration_query_params = ! empty( $registration_query_params ) ? $registration_query_params : array( 'order_by' => array( 'REG_count' => 'ASC' )); |
|
77 | + private function _set_registration_query_params($registration_query_params) { |
|
78 | + $this->_registration_query_params = ! empty($registration_query_params) ? $registration_query_params : array('order_by' => array('REG_count' => 'ASC')); |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | |
@@ -104,10 +104,10 @@ discard block |
||
104 | 104 | ); |
105 | 105 | // send messages |
106 | 106 | /** @type EE_Registration_Processor $registration_processor */ |
107 | - $registration_processor = EE_Registry::instance()->load_class( 'Registration_Processor' ); |
|
107 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
108 | 108 | $registration_processor->trigger_registration_update_notifications( |
109 | 109 | $transaction->primary_registration(), |
110 | - array( 'manually_updated' => true ) |
|
110 | + array('manually_updated' => true) |
|
111 | 111 | ); |
112 | 112 | do_action( |
113 | 113 | 'AHEE__EE_Transaction_Processor__manually_update_registration_statuses', |
@@ -197,13 +197,13 @@ discard block |
||
197 | 197 | $trigger_notifications = true |
198 | 198 | ) { |
199 | 199 | // make sure some query params are set for retrieving registrations |
200 | - $this->_set_registration_query_params( $registration_query_params ); |
|
200 | + $this->_set_registration_query_params($registration_query_params); |
|
201 | 201 | // get final reg step status |
202 | 202 | $finalized = $transaction->final_reg_step_completed(); |
203 | 203 | // if the 'finalize_registration' step has been initiated (has a timestamp) |
204 | 204 | // but has not yet been fully completed (TRUE) |
205 | - if ( is_int( $finalized ) && $finalized !== false && $finalized !== true ) { |
|
206 | - $transaction->set_reg_step_completed( 'finalize_registration' ); |
|
205 | + if (is_int($finalized) && $finalized !== false && $finalized !== true) { |
|
206 | + $transaction->set_reg_step_completed('finalize_registration'); |
|
207 | 207 | $finalized = true; |
208 | 208 | } |
209 | 209 | $transaction->save(); |
@@ -259,22 +259,22 @@ discard block |
||
259 | 259 | $update_txn = true |
260 | 260 | ) { |
261 | 261 | // these reg statuses should not be considered in any calculations involving monies owing |
262 | - $closed_reg_statuses = ! empty( $closed_reg_statuses ) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
263 | - if ( in_array( $registration->status_ID(), $closed_reg_statuses, true ) ) { |
|
262 | + $closed_reg_statuses = ! empty($closed_reg_statuses) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
263 | + if (in_array($registration->status_ID(), $closed_reg_statuses, true)) { |
|
264 | 264 | return false; |
265 | 265 | } |
266 | 266 | try { |
267 | - $transaction = $this->get_transaction_for_registration( $registration ); |
|
267 | + $transaction = $this->get_transaction_for_registration($registration); |
|
268 | 268 | $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration( |
269 | 269 | $transaction, |
270 | 270 | $registration |
271 | 271 | ); |
272 | 272 | // un-cancel the ticket |
273 | - $success = EEH_Line_Item::reinstate_canceled_ticket_line_item( $ticket_line_item ); |
|
274 | - } catch ( EE_Error $e ) { |
|
273 | + $success = EEH_Line_Item::reinstate_canceled_ticket_line_item($ticket_line_item); |
|
274 | + } catch (EE_Error $e) { |
|
275 | 275 | EE_Error::add_error( |
276 | 276 | sprintf( |
277 | - __( 'The Ticket Line Item for Registration %1$d could not be reinstated because :%2$s%3$s', 'event_espresso' ), |
|
277 | + __('The Ticket Line Item for Registration %1$d could not be reinstated because :%2$s%3$s', 'event_espresso'), |
|
278 | 278 | $registration->ID(), |
279 | 279 | '<br />', |
280 | 280 | $e->getMessage() |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | ); |
284 | 284 | return false; |
285 | 285 | } |
286 | - if ( $update_txn ) { |
|
286 | + if ($update_txn) { |
|
287 | 287 | return $transaction->save() ? $success : false; |
288 | 288 | } |
289 | 289 | return $success; |
@@ -307,12 +307,12 @@ discard block |
||
307 | 307 | $update_txn = true |
308 | 308 | ) { |
309 | 309 | // these reg statuses should not be considered in any calculations involving monies owing |
310 | - $closed_reg_statuses = ! empty( $closed_reg_statuses ) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
311 | - if ( ! in_array( $registration->status_ID(), $closed_reg_statuses, true ) ) { |
|
310 | + $closed_reg_statuses = ! empty($closed_reg_statuses) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
311 | + if ( ! in_array($registration->status_ID(), $closed_reg_statuses, true)) { |
|
312 | 312 | return false; |
313 | 313 | } |
314 | 314 | try { |
315 | - $transaction = $this->get_transaction_for_registration( $registration ); |
|
315 | + $transaction = $this->get_transaction_for_registration($registration); |
|
316 | 316 | if ( |
317 | 317 | apply_filters( |
318 | 318 | 'FHEE__EE_Transaction_Processor__update_transaction_after_canceled_or_declined_registration__cancel_ticket_line_item', |
@@ -320,14 +320,14 @@ discard block |
||
320 | 320 | $registration, |
321 | 321 | $transaction |
322 | 322 | ) |
323 | - ){ |
|
324 | - $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration( $transaction, $registration ); |
|
325 | - EEH_Line_Item::cancel_ticket_line_item( $ticket_line_item ); |
|
323 | + ) { |
|
324 | + $ticket_line_item = $this->get_ticket_line_item_for_transaction_registration($transaction, $registration); |
|
325 | + EEH_Line_Item::cancel_ticket_line_item($ticket_line_item); |
|
326 | 326 | } |
327 | - } catch ( EE_Error $e ) { |
|
327 | + } catch (EE_Error $e) { |
|
328 | 328 | EE_Error::add_error( |
329 | 329 | sprintf( |
330 | - __( 'The Ticket Line Item for Registration %1$d could not be cancelled because :%2$s%3$s', 'event_espresso' ), |
|
330 | + __('The Ticket Line Item for Registration %1$d could not be cancelled because :%2$s%3$s', 'event_espresso'), |
|
331 | 331 | $registration->ID(), |
332 | 332 | '<br />', |
333 | 333 | $e->getMessage() |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | ); |
337 | 337 | return false; |
338 | 338 | } |
339 | - if ( $update_txn ) { |
|
339 | + if ($update_txn) { |
|
340 | 340 | return $transaction->save() ? true : false; |
341 | 341 | } |
342 | 342 | return true; |
@@ -352,12 +352,12 @@ discard block |
||
352 | 352 | * @return EE_Transaction |
353 | 353 | * @throws EE_Error |
354 | 354 | */ |
355 | - public function get_transaction_for_registration( EE_Registration $registration ) { |
|
355 | + public function get_transaction_for_registration(EE_Registration $registration) { |
|
356 | 356 | $transaction = $registration->transaction(); |
357 | - if ( ! $transaction instanceof EE_Transaction ) { |
|
357 | + if ( ! $transaction instanceof EE_Transaction) { |
|
358 | 358 | throw new EE_Error( |
359 | 359 | sprintf( |
360 | - __( 'The Transaction for Registration %1$d was not found or is invalid.', 'event_espresso' ), |
|
360 | + __('The Transaction for Registration %1$d was not found or is invalid.', 'event_espresso'), |
|
361 | 361 | $registration->ID() |
362 | 362 | ) |
363 | 363 | ); |
@@ -380,16 +380,16 @@ discard block |
||
380 | 380 | EE_Transaction $transaction, |
381 | 381 | EE_Registration $registration |
382 | 382 | ) { |
383 | - EE_Registry::instance()->load_helper( 'Line_Item' ); |
|
383 | + EE_Registry::instance()->load_helper('Line_Item'); |
|
384 | 384 | $ticket_line_item = EEM_Line_Item::instance()->get_ticket_line_item_for_transaction( |
385 | 385 | $transaction->ID(), |
386 | 386 | $registration->ticket_ID() |
387 | 387 | ); |
388 | - if ( ! $ticket_line_item instanceof EE_Line_Item ) { |
|
388 | + if ( ! $ticket_line_item instanceof EE_Line_Item) { |
|
389 | 389 | throw new EE_Error( |
390 | 390 | sprintf( |
391 | - __( 'The Line Item for Transaction %1$d and Ticket %2$d was not found or is invalid.', |
|
392 | - 'event_espresso' ), |
|
391 | + __('The Line Item for Transaction %1$d and Ticket %2$d was not found or is invalid.', |
|
392 | + 'event_espresso'), |
|
393 | 393 | $transaction->ID(), |
394 | 394 | $registration->ticket_ID() |
395 | 395 | ) |
@@ -422,22 +422,22 @@ discard block |
||
422 | 422 | $update_txn = true |
423 | 423 | ) { |
424 | 424 | // make sure some query params are set for retrieving registrations |
425 | - $this->_set_registration_query_params( $registration_query_params ); |
|
425 | + $this->_set_registration_query_params($registration_query_params); |
|
426 | 426 | // these reg statuses should not be considered in any calculations involving monies owing |
427 | - $closed_reg_statuses = ! empty( $closed_reg_statuses ) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
427 | + $closed_reg_statuses = ! empty($closed_reg_statuses) ? $closed_reg_statuses : EEM_Registration::closed_reg_statuses(); |
|
428 | 428 | // loop through cached registrations |
429 | - foreach ( $transaction->registrations( $this->_registration_query_params ) as $registration ) { |
|
429 | + foreach ($transaction->registrations($this->_registration_query_params) as $registration) { |
|
430 | 430 | if ( |
431 | 431 | $registration instanceof EE_Registration |
432 | - && ! in_array( $registration->status_ID(), $closed_reg_statuses ) |
|
432 | + && ! in_array($registration->status_ID(), $closed_reg_statuses) |
|
433 | 433 | ) { |
434 | 434 | return false; |
435 | 435 | } |
436 | 436 | } |
437 | - if ( in_array( $new_TXN_status, EEM_Transaction::txn_status_array() ) ) { |
|
438 | - $transaction->set_status( $new_TXN_status ); |
|
437 | + if (in_array($new_TXN_status, EEM_Transaction::txn_status_array())) { |
|
438 | + $transaction->set_status($new_TXN_status); |
|
439 | 439 | } |
440 | - if ( $update_txn ) { |
|
440 | + if ($update_txn) { |
|
441 | 441 | return $transaction->save() ? true : false; |
442 | 442 | } |
443 | 443 | return true; |
@@ -466,22 +466,22 @@ discard block |
||
466 | 466 | ) { |
467 | 467 | $response = false; |
468 | 468 | /** @type EE_Registration_Processor $registration_processor */ |
469 | - $registration_processor = EE_Registry::instance()->load_class( 'Registration_Processor' ); |
|
469 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
470 | 470 | // check that method exists |
471 | - if ( ! method_exists( $registration_processor, $method_name )) { |
|
472 | - throw new EE_Error( __( 'Method does not exist.', 'event_espresso' )); |
|
471 | + if ( ! method_exists($registration_processor, $method_name)) { |
|
472 | + throw new EE_Error(__('Method does not exist.', 'event_espresso')); |
|
473 | 473 | } |
474 | 474 | // make sure some query params are set for retrieving registrations |
475 | - $this->_set_registration_query_params( $registration_query_params ); |
|
475 | + $this->_set_registration_query_params($registration_query_params); |
|
476 | 476 | // loop through cached registrations |
477 | - foreach ( $transaction->registrations( $this->_registration_query_params ) as $registration ) { |
|
478 | - if ( $registration instanceof EE_Registration ) { |
|
479 | - if ( $additional_param ) { |
|
480 | - $response = $registration_processor->{$method_name}( $registration, $additional_param ) |
|
477 | + foreach ($transaction->registrations($this->_registration_query_params) as $registration) { |
|
478 | + if ($registration instanceof EE_Registration) { |
|
479 | + if ($additional_param) { |
|
480 | + $response = $registration_processor->{$method_name}($registration, $additional_param) |
|
481 | 481 | ? true |
482 | 482 | : $response; |
483 | 483 | } else { |
484 | - $response = $registration_processor->{$method_name}( $registration ) |
|
484 | + $response = $registration_processor->{$method_name}($registration) |
|
485 | 485 | ? true |
486 | 486 | : $response; |
487 | 487 | } |
@@ -508,28 +508,28 @@ discard block |
||
508 | 508 | public function set_transaction_payment_method_based_on_registration_statuses( |
509 | 509 | EE_Registration $edited_registration |
510 | 510 | ) { |
511 | - if ( $edited_registration instanceof EE_Registration ) { |
|
511 | + if ($edited_registration instanceof EE_Registration) { |
|
512 | 512 | $transaction = $edited_registration->transaction(); |
513 | - if ( $transaction instanceof EE_Transaction ) { |
|
513 | + if ($transaction instanceof EE_Transaction) { |
|
514 | 514 | $all_not_approved = true; |
515 | - foreach ( $transaction->registrations() as $registration ) { |
|
516 | - if ( $registration instanceof EE_Registration ) { |
|
515 | + foreach ($transaction->registrations() as $registration) { |
|
516 | + if ($registration instanceof EE_Registration) { |
|
517 | 517 | // if any REG != "Not Approved" then toggle to false |
518 | 518 | $all_not_approved = $registration->is_not_approved() ? $all_not_approved : false; |
519 | 519 | } |
520 | 520 | } |
521 | 521 | // if ALL Registrations are "Not Approved" |
522 | - if ( $all_not_approved ) { |
|
523 | - $transaction->set_payment_method_ID( null ); |
|
522 | + if ($all_not_approved) { |
|
523 | + $transaction->set_payment_method_ID(null); |
|
524 | 524 | $transaction->save(); |
525 | 525 | } else { |
526 | 526 | $available_payment_methods = EEM_Payment_Method::instance()->get_all_for_transaction( |
527 | 527 | $transaction, |
528 | 528 | EEM_Payment_Method::scope_cart |
529 | 529 | ); |
530 | - if ( ! empty( $available_payment_methods ) ) { |
|
530 | + if ( ! empty($available_payment_methods)) { |
|
531 | 531 | $PMD_ID = 0; |
532 | - foreach ( $available_payment_methods as $available_payment_method ) { |
|
532 | + foreach ($available_payment_methods as $available_payment_method) { |
|
533 | 533 | if ( |
534 | 534 | $available_payment_method instanceof EE_Payment_Method |
535 | 535 | && $available_payment_method->open_by_default() |
@@ -538,22 +538,22 @@ discard block |
||
538 | 538 | break; |
539 | 539 | } |
540 | 540 | } |
541 | - if ( ! $PMD_ID ) { |
|
542 | - $first_payment_method = reset( $available_payment_methods ); |
|
543 | - if ( $first_payment_method instanceof EE_Payment_Method ) { |
|
541 | + if ( ! $PMD_ID) { |
|
542 | + $first_payment_method = reset($available_payment_methods); |
|
543 | + if ($first_payment_method instanceof EE_Payment_Method) { |
|
544 | 544 | $PMD_ID = $first_payment_method->ID(); |
545 | 545 | } else { |
546 | 546 | EE_Error::add_error( |
547 | - __( 'A valid Payment Method could not be determined. Please ensure that at least one Payment Method is activated.', 'event_espresso' ), |
|
547 | + __('A valid Payment Method could not be determined. Please ensure that at least one Payment Method is activated.', 'event_espresso'), |
|
548 | 548 | __FILE__, __LINE__, __FUNCTION__ |
549 | 549 | ); |
550 | 550 | } |
551 | 551 | } |
552 | - $transaction->set_payment_method_ID( $PMD_ID ); |
|
552 | + $transaction->set_payment_method_ID($PMD_ID); |
|
553 | 553 | $transaction->save(); |
554 | 554 | } else { |
555 | 555 | EE_Error::add_error( |
556 | - __( 'Please activate at least one Payment Method in order for things to operate correctly.', 'event_espresso' ), |
|
556 | + __('Please activate at least one Payment Method in order for things to operate correctly.', 'event_espresso'), |
|
557 | 557 | __FILE__, __LINE__, __FUNCTION__ |
558 | 558 | ); |
559 | 559 | } |
@@ -590,7 +590,7 @@ discard block |
||
590 | 590 | * @deprecated 4.9.12 |
591 | 591 | * @param string $old_txn_status |
592 | 592 | */ |
593 | - public function set_old_txn_status( $old_txn_status ) { |
|
593 | + public function set_old_txn_status($old_txn_status) { |
|
594 | 594 | EE_Error::doing_it_wrong( |
595 | 595 | __METHOD__, |
596 | 596 | esc_html__( |
@@ -600,7 +600,7 @@ discard block |
||
600 | 600 | '4.9.12' |
601 | 601 | ); |
602 | 602 | // only set the first time |
603 | - if ( $this->_old_txn_status === null ) { |
|
603 | + if ($this->_old_txn_status === null) { |
|
604 | 604 | $this->_old_txn_status = $old_txn_status; |
605 | 605 | } |
606 | 606 | } |
@@ -629,7 +629,7 @@ discard block |
||
629 | 629 | * @deprecated 4.9.12 |
630 | 630 | * @param string $new_txn_status |
631 | 631 | */ |
632 | - public function set_new_txn_status( $new_txn_status ) { |
|
632 | + public function set_new_txn_status($new_txn_status) { |
|
633 | 633 | EE_Error::doing_it_wrong( |
634 | 634 | __METHOD__, |
635 | 635 | esc_html__( |
@@ -673,7 +673,7 @@ discard block |
||
673 | 673 | * @param EE_Transaction $transaction |
674 | 674 | * @return boolean |
675 | 675 | */ |
676 | - public function all_reg_steps_completed( EE_Transaction $transaction ) { |
|
676 | + public function all_reg_steps_completed(EE_Transaction $transaction) { |
|
677 | 677 | EE_Error::doing_it_wrong( |
678 | 678 | __METHOD__, |
679 | 679 | esc_html__( |
@@ -700,7 +700,7 @@ discard block |
||
700 | 700 | * @param string $exception |
701 | 701 | * @return boolean |
702 | 702 | */ |
703 | - public function all_reg_steps_completed_except( EE_Transaction $transaction, $exception = '' ) { |
|
703 | + public function all_reg_steps_completed_except(EE_Transaction $transaction, $exception = '') { |
|
704 | 704 | EE_Error::doing_it_wrong( |
705 | 705 | __METHOD__, |
706 | 706 | esc_html__( |
@@ -710,7 +710,7 @@ discard block |
||
710 | 710 | '4.9.12', |
711 | 711 | '5.0.0' |
712 | 712 | ); |
713 | - return $transaction->all_reg_steps_completed_except( $exception ); |
|
713 | + return $transaction->all_reg_steps_completed_except($exception); |
|
714 | 714 | } |
715 | 715 | |
716 | 716 | |
@@ -726,7 +726,7 @@ discard block |
||
726 | 726 | * @param EE_Transaction $transaction |
727 | 727 | * @return boolean |
728 | 728 | */ |
729 | - public function all_reg_steps_completed_except_final_step( EE_Transaction $transaction ) { |
|
729 | + public function all_reg_steps_completed_except_final_step(EE_Transaction $transaction) { |
|
730 | 730 | EE_Error::doing_it_wrong( |
731 | 731 | __METHOD__, |
732 | 732 | esc_html__( |
@@ -753,7 +753,7 @@ discard block |
||
753 | 753 | * @param string $reg_step_slug |
754 | 754 | * @return boolean | int |
755 | 755 | */ |
756 | - public function reg_step_completed( EE_Transaction $transaction, $reg_step_slug ) { |
|
756 | + public function reg_step_completed(EE_Transaction $transaction, $reg_step_slug) { |
|
757 | 757 | EE_Error::doing_it_wrong( |
758 | 758 | __METHOD__, |
759 | 759 | esc_html__( |
@@ -763,7 +763,7 @@ discard block |
||
763 | 763 | '4.9.12', |
764 | 764 | '5.0.0' |
765 | 765 | ); |
766 | - return $transaction->reg_step_completed( $reg_step_slug ); |
|
766 | + return $transaction->reg_step_completed($reg_step_slug); |
|
767 | 767 | } |
768 | 768 | |
769 | 769 | |
@@ -779,7 +779,7 @@ discard block |
||
779 | 779 | * @param EE_Transaction $transaction |
780 | 780 | * @return boolean | int |
781 | 781 | */ |
782 | - public function final_reg_step_completed( EE_Transaction $transaction ) { |
|
782 | + public function final_reg_step_completed(EE_Transaction $transaction) { |
|
783 | 783 | EE_Error::doing_it_wrong( |
784 | 784 | __METHOD__, |
785 | 785 | esc_html__( |
@@ -805,7 +805,7 @@ discard block |
||
805 | 805 | * @return boolean |
806 | 806 | * @throws \EE_Error |
807 | 807 | */ |
808 | - public function set_reg_step_initiated( EE_Transaction $transaction, $reg_step_slug ) { |
|
808 | + public function set_reg_step_initiated(EE_Transaction $transaction, $reg_step_slug) { |
|
809 | 809 | EE_Error::doing_it_wrong( |
810 | 810 | __METHOD__, |
811 | 811 | esc_html__( |
@@ -815,7 +815,7 @@ discard block |
||
815 | 815 | '4.9.12', |
816 | 816 | '5.0.0' |
817 | 817 | ); |
818 | - return $transaction->set_reg_step_initiated( $reg_step_slug ); |
|
818 | + return $transaction->set_reg_step_initiated($reg_step_slug); |
|
819 | 819 | } |
820 | 820 | |
821 | 821 | |
@@ -831,7 +831,7 @@ discard block |
||
831 | 831 | * @return boolean |
832 | 832 | * @throws \EE_Error |
833 | 833 | */ |
834 | - public function set_reg_step_completed( EE_Transaction $transaction, $reg_step_slug ) { |
|
834 | + public function set_reg_step_completed(EE_Transaction $transaction, $reg_step_slug) { |
|
835 | 835 | EE_Error::doing_it_wrong( |
836 | 836 | __METHOD__, |
837 | 837 | esc_html__( |
@@ -841,7 +841,7 @@ discard block |
||
841 | 841 | '4.9.12', |
842 | 842 | '5.0.0' |
843 | 843 | ); |
844 | - return $transaction->set_reg_step_completed( $reg_step_slug ); |
|
844 | + return $transaction->set_reg_step_completed($reg_step_slug); |
|
845 | 845 | } |
846 | 846 | |
847 | 847 | |
@@ -857,7 +857,7 @@ discard block |
||
857 | 857 | * @return boolean |
858 | 858 | * @throws \EE_Error |
859 | 859 | */ |
860 | - public function set_reg_step_not_completed( EE_Transaction $transaction, $reg_step_slug ) { |
|
860 | + public function set_reg_step_not_completed(EE_Transaction $transaction, $reg_step_slug) { |
|
861 | 861 | EE_Error::doing_it_wrong( |
862 | 862 | __METHOD__, |
863 | 863 | esc_html__( |
@@ -867,7 +867,7 @@ discard block |
||
867 | 867 | '4.9.12', |
868 | 868 | '5.0.0' |
869 | 869 | ); |
870 | - return $transaction->set_reg_step_not_completed( $reg_step_slug ); |
|
870 | + return $transaction->set_reg_step_not_completed($reg_step_slug); |
|
871 | 871 | } |
872 | 872 | |
873 | 873 | |
@@ -884,7 +884,7 @@ discard block |
||
884 | 884 | * @param string $reg_step_slug |
885 | 885 | * @return void |
886 | 886 | */ |
887 | - public function remove_reg_step( EE_Transaction $transaction, $reg_step_slug ) { |
|
887 | + public function remove_reg_step(EE_Transaction $transaction, $reg_step_slug) { |
|
888 | 888 | EE_Error::doing_it_wrong( |
889 | 889 | __METHOD__, |
890 | 890 | esc_html__( |
@@ -894,7 +894,7 @@ discard block |
||
894 | 894 | '4.9.12', |
895 | 895 | '5.0.0' |
896 | 896 | ); |
897 | - $transaction->remove_reg_step( $reg_step_slug ); |
|
897 | + $transaction->remove_reg_step($reg_step_slug); |
|
898 | 898 | } |
899 | 899 | |
900 | 900 | |
@@ -910,7 +910,7 @@ discard block |
||
910 | 910 | * @return boolean |
911 | 911 | * @throws \EE_Error |
912 | 912 | */ |
913 | - public function toggle_failed_transaction_status( EE_Transaction $transaction ) { |
|
913 | + public function toggle_failed_transaction_status(EE_Transaction $transaction) { |
|
914 | 914 | EE_Error::doing_it_wrong( |
915 | 915 | __METHOD__, |
916 | 916 | esc_html__( |
@@ -934,7 +934,7 @@ discard block |
||
934 | 934 | * @param EE_Transaction $transaction |
935 | 935 | * @return boolean |
936 | 936 | */ |
937 | - public function toggle_abandoned_transaction_status( EE_Transaction $transaction ) { |
|
937 | + public function toggle_abandoned_transaction_status(EE_Transaction $transaction) { |
|
938 | 938 | EE_Error::doing_it_wrong( |
939 | 939 | __METHOD__, |
940 | 940 | esc_html__( |
@@ -32,7 +32,6 @@ |
||
32 | 32 | * Removes the checkin and checkout endpoints from the index for requests |
33 | 33 | * to api versions lowers than 4.8.33 |
34 | 34 | * |
35 | - * @param array $routes_on_this_version |
|
36 | 35 | * @param EventEspresso\core\libraries\rest_api\controllers\Base $controller |
37 | 36 | * @return array like $routes_on_this_version |
38 | 37 | */ |
@@ -12,39 +12,39 @@ |
||
12 | 12 | class Changes_In_4_8_34 extends Changes_In_Base |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * Adds hooks so requests to 4.8.29 don't have the checkin endpoints |
|
17 | - */ |
|
18 | - public function set_hooks() |
|
19 | - { |
|
20 | - //set a hook to remove the checkout/checkout endpoints if the request |
|
21 | - //is for lower than 4.8.33 |
|
22 | - add_filter( |
|
23 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
24 | - array($this, 'remove_response_headers'), |
|
25 | - 10, |
|
26 | - 3 |
|
27 | - ); |
|
28 | - } |
|
29 | - |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * Removes the checkin and checkout endpoints from the index for requests |
|
34 | - * to api versions lowers than 4.8.33 |
|
35 | - * |
|
36 | - * @param array $routes_on_this_version |
|
37 | - * @param EventEspresso\core\libraries\rest_api\controllers\Base $controller |
|
38 | - * @return array like $routes_on_this_version |
|
39 | - */ |
|
40 | - public function remove_response_headers($response_headers, $controller, $requested_version) |
|
41 | - { |
|
42 | - if ($controller instanceof Base |
|
43 | - && $this->applies_to_version($requested_version) |
|
44 | - ) { |
|
45 | - return array(); |
|
46 | - } |
|
47 | - return $response_headers; |
|
48 | - } |
|
15 | + /** |
|
16 | + * Adds hooks so requests to 4.8.29 don't have the checkin endpoints |
|
17 | + */ |
|
18 | + public function set_hooks() |
|
19 | + { |
|
20 | + //set a hook to remove the checkout/checkout endpoints if the request |
|
21 | + //is for lower than 4.8.33 |
|
22 | + add_filter( |
|
23 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
24 | + array($this, 'remove_response_headers'), |
|
25 | + 10, |
|
26 | + 3 |
|
27 | + ); |
|
28 | + } |
|
29 | + |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * Removes the checkin and checkout endpoints from the index for requests |
|
34 | + * to api versions lowers than 4.8.33 |
|
35 | + * |
|
36 | + * @param array $routes_on_this_version |
|
37 | + * @param EventEspresso\core\libraries\rest_api\controllers\Base $controller |
|
38 | + * @return array like $routes_on_this_version |
|
39 | + */ |
|
40 | + public function remove_response_headers($response_headers, $controller, $requested_version) |
|
41 | + { |
|
42 | + if ($controller instanceof Base |
|
43 | + && $this->applies_to_version($requested_version) |
|
44 | + ) { |
|
45 | + return array(); |
|
46 | + } |
|
47 | + return $response_headers; |
|
48 | + } |
|
49 | 49 | } |
50 | 50 |
@@ -314,7 +314,7 @@ |
||
314 | 314 | * |
315 | 315 | * @param string $route |
316 | 316 | * @param string $regex |
317 | - * @param array $match_keys EXCLUDING matching the entire regex |
|
317 | + * @param string[] $match_keys EXCLUDING matching the entire regex |
|
318 | 318 | * @return array where $match_keys are the keys (the first value of $match_keys |
319 | 319 | * becomes the first key of the return value, etc. Eg passing in $match_keys of |
320 | 320 | * array( 'model', 'id' ), will, if the regex is successful, will return |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | use EventEspresso\core\libraries\rest_api\Rest_Exception; |
5 | 5 | |
6 | 6 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
7 | - exit('No direct script access allowed'); |
|
7 | + exit('No direct script access allowed'); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | |
@@ -20,309 +20,309 @@ discard block |
||
20 | 20 | class Base |
21 | 21 | { |
22 | 22 | |
23 | - const header_prefix_for_ee = 'X-EE-'; |
|
24 | - |
|
25 | - const header_prefix_for_wp = 'X-WP-'; |
|
26 | - |
|
27 | - /** |
|
28 | - * Contains debug info we'll send back in the response headers |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $_debug_info = array(); |
|
33 | - |
|
34 | - /** |
|
35 | - * Indicates whether or not the API is in debug mode |
|
36 | - * |
|
37 | - * @var boolean |
|
38 | - */ |
|
39 | - protected $_debug_mode = false; |
|
40 | - |
|
41 | - /** |
|
42 | - * Indicates the version that was requested |
|
43 | - * |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $_requested_version; |
|
47 | - |
|
48 | - /** |
|
49 | - * flat array of headers to send in the response |
|
50 | - * |
|
51 | - * @var array |
|
52 | - */ |
|
53 | - protected $_response_headers = array(); |
|
54 | - |
|
55 | - |
|
56 | - |
|
57 | - public function __construct() |
|
58 | - { |
|
59 | - $this->_debug_mode = defined('EE_REST_API_DEBUG_MODE') ? EE_REST_API_DEBUG_MODE : false; |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Sets the version the user requested |
|
66 | - * |
|
67 | - * @param string $version eg '4.8' |
|
68 | - */ |
|
69 | - public function set_requested_version($version) |
|
70 | - { |
|
71 | - $this->_requested_version = $version; |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * Sets some debug info that we'll send back in headers |
|
78 | - * |
|
79 | - * @param string $key |
|
80 | - * @param string|array $info |
|
81 | - */ |
|
82 | - protected function _set_debug_info($key, $info) |
|
83 | - { |
|
84 | - $this->_debug_info[$key] = $info; |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Sets headers for the response |
|
91 | - * |
|
92 | - * @param string $header_key , excluding the "X-EE-" part |
|
93 | - * @param array|string $value if an array, multiple headers will be added, one |
|
94 | - * for each key in the array |
|
95 | - * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
96 | - * the standard WP one |
|
97 | - */ |
|
98 | - protected function _set_response_header($header_key, $value, $use_ee_prefix = true) |
|
99 | - { |
|
100 | - if (is_array($value)) { |
|
101 | - foreach ($value as $value_key => $value_value) { |
|
102 | - $this->_set_response_header($header_key . '[' . $value_key . ']', $value_value); |
|
103 | - } |
|
104 | - } else { |
|
105 | - $prefix = $use_ee_prefix ? Base::header_prefix_for_ee : Base::header_prefix_for_wp; |
|
106 | - $this->_response_headers[$prefix . $header_key] = $value; |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * Returns a flat array of headers to be added to the response |
|
114 | - * |
|
115 | - * @return array |
|
116 | - */ |
|
117 | - protected function _get_response_headers() |
|
118 | - { |
|
119 | - return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
120 | - $this->_response_headers, |
|
121 | - $this, |
|
122 | - $this->_requested_version |
|
123 | - ); |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Adds error notices from EE_Error onto the provided \WP_Error |
|
130 | - * |
|
131 | - * @param \WP_Error $wp_error_response |
|
132 | - * @return \WP_Error |
|
133 | - */ |
|
134 | - protected function _add_ee_errors_to_response(\WP_Error $wp_error_response) |
|
135 | - { |
|
136 | - $notices_during_checkin = \EE_Error::get_raw_notices(); |
|
137 | - if (! empty($notices_during_checkin['errors'])) { |
|
138 | - foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
139 | - $wp_error_response->add( |
|
140 | - sanitize_key($error_code), |
|
141 | - strip_tags($error_message)); |
|
142 | - } |
|
143 | - } |
|
144 | - return $wp_error_response; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * Sends a response, but also makes sure to attach headers that |
|
151 | - * are handy for debugging. |
|
152 | - * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
153 | - * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
154 | - * the response, others? |
|
155 | - * |
|
156 | - * @param array|\WP_Error|\Exception $response |
|
157 | - * @return \WP_REST_Response |
|
158 | - */ |
|
159 | - public function send_response($response) |
|
160 | - { |
|
161 | - if ($response instanceof Rest_Exception) { |
|
162 | - $response = new \WP_Error($response->get_string_code(), $response->getMessage(), $response->get_data()); |
|
163 | - } |
|
164 | - if ($response instanceof \Exception) { |
|
165 | - $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
166 | - $response = new \WP_Error($code, $response->getMessage()); |
|
167 | - } |
|
168 | - if ($response instanceof \WP_Error) { |
|
169 | - $response = $this->_add_ee_errors_to_response($response); |
|
170 | - $rest_response = $this->_create_rest_response_from_wp_error($response); |
|
171 | - } else { |
|
172 | - $rest_response = new \WP_REST_Response($response, 200); |
|
173 | - } |
|
174 | - $headers = array(); |
|
175 | - if ($this->_debug_mode && is_array($this->_debug_info)) { |
|
176 | - foreach ($this->_debug_info as $debug_key => $debug_info) { |
|
177 | - if (is_array($debug_info)) { |
|
178 | - $debug_info = wp_json_encode($debug_info); |
|
179 | - } |
|
180 | - $headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info; |
|
181 | - } |
|
182 | - } |
|
183 | - $headers = array_merge( |
|
184 | - $headers, |
|
185 | - $this->_get_response_headers(), |
|
186 | - $this->_get_headers_from_ee_notices() |
|
187 | - ); |
|
188 | - $rest_response->set_headers($headers); |
|
189 | - return $rest_response; |
|
190 | - } |
|
191 | - |
|
192 | - |
|
193 | - |
|
194 | - /** |
|
195 | - * Converts the \WP_Error into `WP_REST_Response. |
|
196 | - * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
197 | - * (which is protected) |
|
198 | - * |
|
199 | - * @param \WP_Error $wp_error |
|
200 | - * @return \WP_REST_Response |
|
201 | - */ |
|
202 | - protected function _create_rest_response_from_wp_error(\WP_Error $wp_error) |
|
203 | - { |
|
204 | - $error_data = $wp_error->get_error_data(); |
|
205 | - if (is_array($error_data) && isset($error_data['status'])) { |
|
206 | - $status = $error_data['status']; |
|
207 | - } else { |
|
208 | - $status = 500; |
|
209 | - } |
|
210 | - $errors = array(); |
|
211 | - foreach ((array)$wp_error->errors as $code => $messages) { |
|
212 | - foreach ((array)$messages as $message) { |
|
213 | - $errors[] = array( |
|
214 | - 'code' => $code, |
|
215 | - 'message' => $message, |
|
216 | - 'data' => $wp_error->get_error_data($code), |
|
217 | - ); |
|
218 | - } |
|
219 | - } |
|
220 | - $data = isset($errors[0]) ? $errors[0] : array(); |
|
221 | - if (count($errors) > 1) { |
|
222 | - // Remove the primary error. |
|
223 | - array_shift($errors); |
|
224 | - $data['additional_errors'] = $errors; |
|
225 | - } |
|
226 | - return new \WP_REST_Response($data, $status); |
|
227 | - } |
|
228 | - |
|
229 | - |
|
230 | - |
|
231 | - /** |
|
232 | - * Array of headers derived from EE success, attention, and error messages |
|
233 | - * |
|
234 | - * @return array |
|
235 | - */ |
|
236 | - protected function _get_headers_from_ee_notices() |
|
237 | - { |
|
238 | - $headers = array(); |
|
239 | - $notices = \EE_Error::get_raw_notices(); |
|
240 | - foreach ($notices as $notice_type => $sub_notices) { |
|
241 | - if (! is_array($sub_notices)) { |
|
242 | - continue; |
|
243 | - } |
|
244 | - foreach ($sub_notices as $notice_code => $sub_notice) { |
|
245 | - $headers['X-EE4-Notices-' |
|
246 | - . \EEH_Inflector::humanize($notice_type) |
|
247 | - . '[' |
|
248 | - . $notice_code |
|
249 | - . ']'] = strip_tags($sub_notice); |
|
250 | - } |
|
251 | - } |
|
252 | - return apply_filters( |
|
253 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
254 | - $headers, |
|
255 | - $this->_requested_version, |
|
256 | - $notices |
|
257 | - ); |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * Finds which version of the API was requested given the route, and returns it. |
|
264 | - * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
265 | - * "4.8.29" |
|
266 | - * |
|
267 | - * @param string $route |
|
268 | - * @return string |
|
269 | - */ |
|
270 | - public function get_requested_version($route = null) |
|
271 | - { |
|
272 | - $matches = $this->parse_route( |
|
273 | - $route, |
|
274 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
275 | - array('version') |
|
276 | - ); |
|
277 | - if (isset($matches['version'])) { |
|
278 | - return $matches['version']; |
|
279 | - } else { |
|
280 | - return \EED_Core_Rest_Api::latest_rest_api_version(); |
|
281 | - } |
|
282 | - } |
|
283 | - |
|
284 | - |
|
285 | - |
|
286 | - /** |
|
287 | - * Applies the regex to the route, then creates an array using the values of |
|
288 | - * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
289 | - * For example, if you call |
|
290 | - * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
291 | - * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
292 | - * |
|
293 | - * @param string $route |
|
294 | - * @param string $regex |
|
295 | - * @param array $match_keys EXCLUDING matching the entire regex |
|
296 | - * @return array where $match_keys are the keys (the first value of $match_keys |
|
297 | - * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
298 | - * array( 'model', 'id' ), will, if the regex is successful, will return |
|
299 | - * array( 'model' => 'foo', 'id' => 'bar' ) |
|
300 | - * @throws \EE_Error if it couldn't be parsed |
|
301 | - */ |
|
302 | - public function parse_route($route, $regex, $match_keys) |
|
303 | - { |
|
304 | - $indexed_matches = array(); |
|
305 | - $success = preg_match($regex, $route, $matches); |
|
306 | - if ( |
|
307 | - is_array($matches) |
|
308 | - ) { |
|
309 | - //skip the overall regex match. Who cares |
|
310 | - for ($i = 1; $i <= count($match_keys); $i++) { |
|
311 | - if (! isset($matches[$i])) { |
|
312 | - $success = false; |
|
313 | - } else { |
|
314 | - $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
|
315 | - } |
|
316 | - } |
|
317 | - } |
|
318 | - if (! $success) { |
|
319 | - throw new \EE_Error( |
|
320 | - __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
321 | - 'endpoint_parsing_error' |
|
322 | - ); |
|
323 | - } |
|
324 | - return $indexed_matches; |
|
325 | - } |
|
23 | + const header_prefix_for_ee = 'X-EE-'; |
|
24 | + |
|
25 | + const header_prefix_for_wp = 'X-WP-'; |
|
26 | + |
|
27 | + /** |
|
28 | + * Contains debug info we'll send back in the response headers |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $_debug_info = array(); |
|
33 | + |
|
34 | + /** |
|
35 | + * Indicates whether or not the API is in debug mode |
|
36 | + * |
|
37 | + * @var boolean |
|
38 | + */ |
|
39 | + protected $_debug_mode = false; |
|
40 | + |
|
41 | + /** |
|
42 | + * Indicates the version that was requested |
|
43 | + * |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $_requested_version; |
|
47 | + |
|
48 | + /** |
|
49 | + * flat array of headers to send in the response |
|
50 | + * |
|
51 | + * @var array |
|
52 | + */ |
|
53 | + protected $_response_headers = array(); |
|
54 | + |
|
55 | + |
|
56 | + |
|
57 | + public function __construct() |
|
58 | + { |
|
59 | + $this->_debug_mode = defined('EE_REST_API_DEBUG_MODE') ? EE_REST_API_DEBUG_MODE : false; |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Sets the version the user requested |
|
66 | + * |
|
67 | + * @param string $version eg '4.8' |
|
68 | + */ |
|
69 | + public function set_requested_version($version) |
|
70 | + { |
|
71 | + $this->_requested_version = $version; |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * Sets some debug info that we'll send back in headers |
|
78 | + * |
|
79 | + * @param string $key |
|
80 | + * @param string|array $info |
|
81 | + */ |
|
82 | + protected function _set_debug_info($key, $info) |
|
83 | + { |
|
84 | + $this->_debug_info[$key] = $info; |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Sets headers for the response |
|
91 | + * |
|
92 | + * @param string $header_key , excluding the "X-EE-" part |
|
93 | + * @param array|string $value if an array, multiple headers will be added, one |
|
94 | + * for each key in the array |
|
95 | + * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
96 | + * the standard WP one |
|
97 | + */ |
|
98 | + protected function _set_response_header($header_key, $value, $use_ee_prefix = true) |
|
99 | + { |
|
100 | + if (is_array($value)) { |
|
101 | + foreach ($value as $value_key => $value_value) { |
|
102 | + $this->_set_response_header($header_key . '[' . $value_key . ']', $value_value); |
|
103 | + } |
|
104 | + } else { |
|
105 | + $prefix = $use_ee_prefix ? Base::header_prefix_for_ee : Base::header_prefix_for_wp; |
|
106 | + $this->_response_headers[$prefix . $header_key] = $value; |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * Returns a flat array of headers to be added to the response |
|
114 | + * |
|
115 | + * @return array |
|
116 | + */ |
|
117 | + protected function _get_response_headers() |
|
118 | + { |
|
119 | + return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
120 | + $this->_response_headers, |
|
121 | + $this, |
|
122 | + $this->_requested_version |
|
123 | + ); |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Adds error notices from EE_Error onto the provided \WP_Error |
|
130 | + * |
|
131 | + * @param \WP_Error $wp_error_response |
|
132 | + * @return \WP_Error |
|
133 | + */ |
|
134 | + protected function _add_ee_errors_to_response(\WP_Error $wp_error_response) |
|
135 | + { |
|
136 | + $notices_during_checkin = \EE_Error::get_raw_notices(); |
|
137 | + if (! empty($notices_during_checkin['errors'])) { |
|
138 | + foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
139 | + $wp_error_response->add( |
|
140 | + sanitize_key($error_code), |
|
141 | + strip_tags($error_message)); |
|
142 | + } |
|
143 | + } |
|
144 | + return $wp_error_response; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * Sends a response, but also makes sure to attach headers that |
|
151 | + * are handy for debugging. |
|
152 | + * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
153 | + * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
154 | + * the response, others? |
|
155 | + * |
|
156 | + * @param array|\WP_Error|\Exception $response |
|
157 | + * @return \WP_REST_Response |
|
158 | + */ |
|
159 | + public function send_response($response) |
|
160 | + { |
|
161 | + if ($response instanceof Rest_Exception) { |
|
162 | + $response = new \WP_Error($response->get_string_code(), $response->getMessage(), $response->get_data()); |
|
163 | + } |
|
164 | + if ($response instanceof \Exception) { |
|
165 | + $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
166 | + $response = new \WP_Error($code, $response->getMessage()); |
|
167 | + } |
|
168 | + if ($response instanceof \WP_Error) { |
|
169 | + $response = $this->_add_ee_errors_to_response($response); |
|
170 | + $rest_response = $this->_create_rest_response_from_wp_error($response); |
|
171 | + } else { |
|
172 | + $rest_response = new \WP_REST_Response($response, 200); |
|
173 | + } |
|
174 | + $headers = array(); |
|
175 | + if ($this->_debug_mode && is_array($this->_debug_info)) { |
|
176 | + foreach ($this->_debug_info as $debug_key => $debug_info) { |
|
177 | + if (is_array($debug_info)) { |
|
178 | + $debug_info = wp_json_encode($debug_info); |
|
179 | + } |
|
180 | + $headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info; |
|
181 | + } |
|
182 | + } |
|
183 | + $headers = array_merge( |
|
184 | + $headers, |
|
185 | + $this->_get_response_headers(), |
|
186 | + $this->_get_headers_from_ee_notices() |
|
187 | + ); |
|
188 | + $rest_response->set_headers($headers); |
|
189 | + return $rest_response; |
|
190 | + } |
|
191 | + |
|
192 | + |
|
193 | + |
|
194 | + /** |
|
195 | + * Converts the \WP_Error into `WP_REST_Response. |
|
196 | + * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
197 | + * (which is protected) |
|
198 | + * |
|
199 | + * @param \WP_Error $wp_error |
|
200 | + * @return \WP_REST_Response |
|
201 | + */ |
|
202 | + protected function _create_rest_response_from_wp_error(\WP_Error $wp_error) |
|
203 | + { |
|
204 | + $error_data = $wp_error->get_error_data(); |
|
205 | + if (is_array($error_data) && isset($error_data['status'])) { |
|
206 | + $status = $error_data['status']; |
|
207 | + } else { |
|
208 | + $status = 500; |
|
209 | + } |
|
210 | + $errors = array(); |
|
211 | + foreach ((array)$wp_error->errors as $code => $messages) { |
|
212 | + foreach ((array)$messages as $message) { |
|
213 | + $errors[] = array( |
|
214 | + 'code' => $code, |
|
215 | + 'message' => $message, |
|
216 | + 'data' => $wp_error->get_error_data($code), |
|
217 | + ); |
|
218 | + } |
|
219 | + } |
|
220 | + $data = isset($errors[0]) ? $errors[0] : array(); |
|
221 | + if (count($errors) > 1) { |
|
222 | + // Remove the primary error. |
|
223 | + array_shift($errors); |
|
224 | + $data['additional_errors'] = $errors; |
|
225 | + } |
|
226 | + return new \WP_REST_Response($data, $status); |
|
227 | + } |
|
228 | + |
|
229 | + |
|
230 | + |
|
231 | + /** |
|
232 | + * Array of headers derived from EE success, attention, and error messages |
|
233 | + * |
|
234 | + * @return array |
|
235 | + */ |
|
236 | + protected function _get_headers_from_ee_notices() |
|
237 | + { |
|
238 | + $headers = array(); |
|
239 | + $notices = \EE_Error::get_raw_notices(); |
|
240 | + foreach ($notices as $notice_type => $sub_notices) { |
|
241 | + if (! is_array($sub_notices)) { |
|
242 | + continue; |
|
243 | + } |
|
244 | + foreach ($sub_notices as $notice_code => $sub_notice) { |
|
245 | + $headers['X-EE4-Notices-' |
|
246 | + . \EEH_Inflector::humanize($notice_type) |
|
247 | + . '[' |
|
248 | + . $notice_code |
|
249 | + . ']'] = strip_tags($sub_notice); |
|
250 | + } |
|
251 | + } |
|
252 | + return apply_filters( |
|
253 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
254 | + $headers, |
|
255 | + $this->_requested_version, |
|
256 | + $notices |
|
257 | + ); |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * Finds which version of the API was requested given the route, and returns it. |
|
264 | + * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
265 | + * "4.8.29" |
|
266 | + * |
|
267 | + * @param string $route |
|
268 | + * @return string |
|
269 | + */ |
|
270 | + public function get_requested_version($route = null) |
|
271 | + { |
|
272 | + $matches = $this->parse_route( |
|
273 | + $route, |
|
274 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
275 | + array('version') |
|
276 | + ); |
|
277 | + if (isset($matches['version'])) { |
|
278 | + return $matches['version']; |
|
279 | + } else { |
|
280 | + return \EED_Core_Rest_Api::latest_rest_api_version(); |
|
281 | + } |
|
282 | + } |
|
283 | + |
|
284 | + |
|
285 | + |
|
286 | + /** |
|
287 | + * Applies the regex to the route, then creates an array using the values of |
|
288 | + * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
289 | + * For example, if you call |
|
290 | + * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
291 | + * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
292 | + * |
|
293 | + * @param string $route |
|
294 | + * @param string $regex |
|
295 | + * @param array $match_keys EXCLUDING matching the entire regex |
|
296 | + * @return array where $match_keys are the keys (the first value of $match_keys |
|
297 | + * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
298 | + * array( 'model', 'id' ), will, if the regex is successful, will return |
|
299 | + * array( 'model' => 'foo', 'id' => 'bar' ) |
|
300 | + * @throws \EE_Error if it couldn't be parsed |
|
301 | + */ |
|
302 | + public function parse_route($route, $regex, $match_keys) |
|
303 | + { |
|
304 | + $indexed_matches = array(); |
|
305 | + $success = preg_match($regex, $route, $matches); |
|
306 | + if ( |
|
307 | + is_array($matches) |
|
308 | + ) { |
|
309 | + //skip the overall regex match. Who cares |
|
310 | + for ($i = 1; $i <= count($match_keys); $i++) { |
|
311 | + if (! isset($matches[$i])) { |
|
312 | + $success = false; |
|
313 | + } else { |
|
314 | + $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
|
315 | + } |
|
316 | + } |
|
317 | + } |
|
318 | + if (! $success) { |
|
319 | + throw new \EE_Error( |
|
320 | + __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
321 | + 'endpoint_parsing_error' |
|
322 | + ); |
|
323 | + } |
|
324 | + return $indexed_matches; |
|
325 | + } |
|
326 | 326 | } |
327 | 327 | |
328 | 328 | // End of file Base.php |
329 | 329 | \ No newline at end of file |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | |
4 | 4 | use EventEspresso\core\libraries\rest_api\Rest_Exception; |
5 | 5 | |
6 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
6 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
7 | 7 | exit('No direct script access allowed'); |
8 | 8 | } |
9 | 9 | |
@@ -99,11 +99,11 @@ discard block |
||
99 | 99 | { |
100 | 100 | if (is_array($value)) { |
101 | 101 | foreach ($value as $value_key => $value_value) { |
102 | - $this->_set_response_header($header_key . '[' . $value_key . ']', $value_value); |
|
102 | + $this->_set_response_header($header_key.'['.$value_key.']', $value_value); |
|
103 | 103 | } |
104 | 104 | } else { |
105 | 105 | $prefix = $use_ee_prefix ? Base::header_prefix_for_ee : Base::header_prefix_for_wp; |
106 | - $this->_response_headers[$prefix . $header_key] = $value; |
|
106 | + $this->_response_headers[$prefix.$header_key] = $value; |
|
107 | 107 | } |
108 | 108 | } |
109 | 109 | |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | protected function _add_ee_errors_to_response(\WP_Error $wp_error_response) |
135 | 135 | { |
136 | 136 | $notices_during_checkin = \EE_Error::get_raw_notices(); |
137 | - if (! empty($notices_during_checkin['errors'])) { |
|
137 | + if ( ! empty($notices_during_checkin['errors'])) { |
|
138 | 138 | foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
139 | 139 | $wp_error_response->add( |
140 | 140 | sanitize_key($error_code), |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | if (is_array($debug_info)) { |
178 | 178 | $debug_info = wp_json_encode($debug_info); |
179 | 179 | } |
180 | - $headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info; |
|
180 | + $headers['X-EE4-Debug-'.ucwords($debug_key)] = $debug_info; |
|
181 | 181 | } |
182 | 182 | } |
183 | 183 | $headers = array_merge( |
@@ -208,8 +208,8 @@ discard block |
||
208 | 208 | $status = 500; |
209 | 209 | } |
210 | 210 | $errors = array(); |
211 | - foreach ((array)$wp_error->errors as $code => $messages) { |
|
212 | - foreach ((array)$messages as $message) { |
|
211 | + foreach ((array) $wp_error->errors as $code => $messages) { |
|
212 | + foreach ((array) $messages as $message) { |
|
213 | 213 | $errors[] = array( |
214 | 214 | 'code' => $code, |
215 | 215 | 'message' => $message, |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | $headers = array(); |
239 | 239 | $notices = \EE_Error::get_raw_notices(); |
240 | 240 | foreach ($notices as $notice_type => $sub_notices) { |
241 | - if (! is_array($sub_notices)) { |
|
241 | + if ( ! is_array($sub_notices)) { |
|
242 | 242 | continue; |
243 | 243 | } |
244 | 244 | foreach ($sub_notices as $notice_code => $sub_notice) { |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | { |
272 | 272 | $matches = $this->parse_route( |
273 | 273 | $route, |
274 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
274 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'~', |
|
275 | 275 | array('version') |
276 | 276 | ); |
277 | 277 | if (isset($matches['version'])) { |
@@ -308,14 +308,14 @@ discard block |
||
308 | 308 | ) { |
309 | 309 | //skip the overall regex match. Who cares |
310 | 310 | for ($i = 1; $i <= count($match_keys); $i++) { |
311 | - if (! isset($matches[$i])) { |
|
311 | + if ( ! isset($matches[$i])) { |
|
312 | 312 | $success = false; |
313 | 313 | } else { |
314 | 314 | $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
315 | 315 | } |
316 | 316 | } |
317 | 317 | } |
318 | - if (! $success) { |
|
318 | + if ( ! $success) { |
|
319 | 319 | throw new \EE_Error( |
320 | 320 | __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
321 | 321 | 'endpoint_parsing_error' |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | namespace EventEspresso\core\libraries\rest_api; |
3 | 3 | |
4 | 4 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
5 | - exit('No direct script access allowed'); |
|
5 | + exit('No direct script access allowed'); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | |
@@ -24,462 +24,462 @@ discard block |
||
24 | 24 | class Model_Version_Info |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * Constant used in the $_model_changes array to indicate that a model |
|
29 | - * was completely new in this version |
|
30 | - */ |
|
31 | - const model_added = 'model_added_in_this_version'; |
|
32 | - |
|
33 | - /** |
|
34 | - * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
35 | - * next-level keys are model names (eg "Event") that underwent some change in that version |
|
36 | - * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version), |
|
37 | - * or it's an array where the values are model field names, |
|
38 | - * or API resource properties (ie, non-model fields that appear in REST API results) |
|
39 | - * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
40 | - * |
|
41 | - * @var array |
|
42 | - */ |
|
43 | - protected $_model_changes = array(); |
|
44 | - |
|
45 | - /** |
|
46 | - * top-level keys are version numbers, |
|
47 | - * next-level keys are model CLASSNAMES (even parent classnames), |
|
48 | - * and next-level keys are extra resource properties to attach to those models' resources, |
|
49 | - * and next-level key-value pairs, where the keys are: |
|
50 | - * 'raw', 'type', 'nullable', 'table_alias', 'table_column', 'always_available' |
|
51 | - * |
|
52 | - * @var array |
|
53 | - */ |
|
54 | - protected $_resource_changes = array(); |
|
55 | - |
|
56 | - /** |
|
57 | - * @var string indicating what version of the API was requested |
|
58 | - * (eg although core might be at version 4.8.11, they may have sent a request |
|
59 | - * for 4.6) |
|
60 | - */ |
|
61 | - protected $_requested_version = null; |
|
62 | - |
|
63 | - /** |
|
64 | - * Keys are model names, values are their classnames. |
|
65 | - * We cache this so we only need to calculate this once per request |
|
66 | - * |
|
67 | - * @var array |
|
68 | - */ |
|
69 | - protected $_cached_models_for_requested_version = null; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - protected $_cached_model_changes_between_requested_version_and_current = null; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var array |
|
78 | - */ |
|
79 | - protected $_cached_resource_changes_between_requested_version_and_current = null; |
|
80 | - |
|
81 | - /** |
|
82 | - * 2d array where top-level keys are model names, 2nd-level keys are field names |
|
83 | - * and values are the actual field objects |
|
84 | - * |
|
85 | - * @var array |
|
86 | - */ |
|
87 | - protected $_cached_fields_on_models = array(); |
|
88 | - |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Model_Version_Info constructor. |
|
93 | - * |
|
94 | - * @param string $requested_version |
|
95 | - */ |
|
96 | - public function __construct($requested_version) |
|
97 | - { |
|
98 | - $this->_requested_version = $requested_version; |
|
99 | - $this->_model_changes = array( |
|
100 | - '4.8.29' => array( |
|
101 | - //first version where the REST API is in EE core, so no need |
|
102 | - //to specify how its different from the previous |
|
103 | - ), |
|
104 | - ); |
|
105 | - //setup data for "extra" fields added onto resources which don't actually exist on models |
|
106 | - $this->_resource_changes = apply_filters( |
|
107 | - 'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models', |
|
108 | - array() |
|
109 | - ); |
|
110 | - $defaults = array( |
|
111 | - 'raw' => false, |
|
112 | - 'type' => 'N/A', |
|
113 | - 'nullable' => true, |
|
114 | - 'table_alias' => 'N/A', |
|
115 | - 'table_column' => 'N/A', |
|
116 | - 'always_available' => true, |
|
117 | - ); |
|
118 | - foreach ($this->_resource_changes as $version => $model_classnames) { |
|
119 | - foreach ($model_classnames as $model_classname => $extra_fields) { |
|
120 | - foreach ($extra_fields as $fieldname => $field_data) { |
|
121 | - $this->_resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
|
122 | - foreach ($defaults as $attribute => $default_value) { |
|
123 | - if (! isset($this->_resource_changes[$model_classname][$fieldname][$attribute])) { |
|
124 | - $this->_resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
128 | - } |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * Returns a slice of Model_Version_Info::model_changes()'s array |
|
136 | - * indicating exactly what changes happened between the current core version, |
|
137 | - * and the version requested |
|
138 | - * |
|
139 | - * @return array |
|
140 | - */ |
|
141 | - public function model_changes_between_requested_version_and_current() |
|
142 | - { |
|
143 | - if ($this->_cached_model_changes_between_requested_version_and_current === null) { |
|
144 | - $model_changes = array(); |
|
145 | - foreach ($this->model_changes() as $version => $models_changed_in_version) { |
|
146 | - if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
147 | - $model_changes[$version] = $models_changed_in_version; |
|
148 | - } |
|
149 | - } |
|
150 | - $this->_cached_model_changes_between_requested_version_and_current = $model_changes; |
|
151 | - } |
|
152 | - return $this->_cached_model_changes_between_requested_version_and_current; |
|
153 | - } |
|
154 | - |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * Returns a slice of Model_Version_Info::model_changes()'s array |
|
159 | - * indicating exactly what changes happened between the current core version, |
|
160 | - * and the version requested |
|
161 | - * |
|
162 | - * @return array |
|
163 | - */ |
|
164 | - public function resource_changes_between_requested_version_and_current() |
|
165 | - { |
|
166 | - if ($this->_cached_resource_changes_between_requested_version_and_current === null) { |
|
167 | - $resource_changes = array(); |
|
168 | - foreach ($this->resource_changes() as $version => $model_classnames) { |
|
169 | - if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
170 | - $resource_changes[$version] = $model_classnames; |
|
171 | - } |
|
172 | - } |
|
173 | - $this->_cached_resource_changes_between_requested_version_and_current = $resource_changes; |
|
174 | - } |
|
175 | - return $this->_cached_resource_changes_between_requested_version_and_current; |
|
176 | - } |
|
177 | - |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7' |
|
182 | - * |
|
183 | - * @return string like '4.6' |
|
184 | - */ |
|
185 | - public function requested_version() |
|
186 | - { |
|
187 | - return $this->_requested_version; |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - |
|
192 | - /** |
|
193 | - * Returns an array describing how the models have changed in each version of core |
|
194 | - * that supports the API (starting at 4.6) |
|
195 | - * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
196 | - * next-level keys are model names (eg "Event") that underwent some change in that version |
|
197 | - * and the value is either NULL (indicating the model is completely NEW in this version), |
|
198 | - * or it's an array where fields are value names. |
|
199 | - * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
200 | - * |
|
201 | - * @return array |
|
202 | - */ |
|
203 | - public function model_changes() |
|
204 | - { |
|
205 | - return $this->_model_changes; |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * Takes into account the requested version, and the current version, and |
|
212 | - * what changed between the two, and tries to return. |
|
213 | - * Analogous to EE_Registry::instance()->non_abstract_db_models |
|
214 | - * |
|
215 | - * @return array keys are model names, values are their classname |
|
216 | - */ |
|
217 | - public function models_for_requested_version() |
|
218 | - { |
|
219 | - if ($this->_cached_models_for_requested_version === null) { |
|
220 | - $all_models_in_current_version = \EE_Registry::instance()->non_abstract_db_models; |
|
221 | - foreach ($this->model_changes_between_requested_version_and_current() as $version => $models_changed) { |
|
222 | - foreach ($models_changed as $model_name => $new_indicator_or_fields_added) { |
|
223 | - if ($new_indicator_or_fields_added === Model_Version_Info::model_added) { |
|
224 | - unset($all_models_in_current_version[$model_name]); |
|
225 | - } |
|
226 | - } |
|
227 | - } |
|
228 | - $this->_cached_models_for_requested_version = apply_filters( |
|
229 | - 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
230 | - $all_models_in_current_version, |
|
231 | - $this |
|
232 | - ); |
|
233 | - } |
|
234 | - return $this->_cached_models_for_requested_version; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - |
|
239 | - /** |
|
240 | - * Determines if this is a valid model name in the requested version. |
|
241 | - * Similar to EE_Registry::instance()->is_model_name(), but takes the requested |
|
242 | - * version's models into account |
|
243 | - * |
|
244 | - * @param string $model_name eg 'Event' |
|
245 | - * @return boolean |
|
246 | - */ |
|
247 | - public function is_model_name_in_this_version($model_name) |
|
248 | - { |
|
249 | - $model_names = $this->models_for_requested_version(); |
|
250 | - if (isset($model_names[$model_name])) { |
|
251 | - return true; |
|
252 | - } else { |
|
253 | - return false; |
|
254 | - } |
|
255 | - } |
|
256 | - |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Wrapper for EE_Registry::instance()->load_model(), but takes the requested |
|
261 | - * version's models into account |
|
262 | - * |
|
263 | - * @param string $model_name |
|
264 | - * @return \EEM_Base |
|
265 | - * @throws \EE_Error |
|
266 | - */ |
|
267 | - public function load_model($model_name) |
|
268 | - { |
|
269 | - if ($this->is_model_name_in_this_version($model_name)) { |
|
270 | - return \EE_Registry::instance()->load_model($model_name); |
|
271 | - } else { |
|
272 | - throw new \EE_Error( |
|
273 | - sprintf( |
|
274 | - __( |
|
275 | - 'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso', |
|
276 | - 'event_espresso' |
|
277 | - ), |
|
278 | - $model_name, |
|
279 | - $this->requested_version() |
|
280 | - ) |
|
281 | - ); |
|
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - |
|
287 | - /** |
|
288 | - * Gets all the fields that should exist on this model right now |
|
289 | - * |
|
290 | - * @param \EEM_Base $model |
|
291 | - * @return array|\EE_Model_Field_Base[] |
|
292 | - */ |
|
293 | - public function fields_on_model_in_this_version($model) |
|
294 | - { |
|
295 | - if (! isset($this->_cached_fields_on_models[$model->get_this_model_name()])) { |
|
296 | - //get all model changes between the requested version and current core version |
|
297 | - $changes = $this->model_changes_between_requested_version_and_current(); |
|
298 | - //fetch all fields currently on this model |
|
299 | - $current_fields = $model->field_settings(); |
|
300 | - //remove all fields that have been added since |
|
301 | - foreach ($changes as $version => $changes_in_version) { |
|
302 | - if ( |
|
303 | - isset($changes_in_version[$model->get_this_model_name()]) |
|
304 | - && $changes_in_version[$model->get_this_model_name()] !== Model_Version_Info::model_added |
|
305 | - ) { |
|
306 | - $current_fields = array_diff_key( |
|
307 | - $current_fields, |
|
308 | - array_flip($changes_in_version[$model->get_this_model_name()]) |
|
309 | - ); |
|
310 | - } |
|
311 | - } |
|
312 | - $this->_cached_fields_on_models = $current_fields; |
|
313 | - } |
|
314 | - return $this->_cached_fields_on_models; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Determines if $object is of one of the classes of $classes. Similar to |
|
321 | - * in_array(), except this checks if $object is a subclass of the classnames provided |
|
322 | - * in $classnames |
|
323 | - * |
|
324 | - * @param object $object |
|
325 | - * @param array $classnames |
|
326 | - * @return boolean |
|
327 | - */ |
|
328 | - public function is_subclass_of_one($object, $classnames) |
|
329 | - { |
|
330 | - foreach ($classnames as $classname) { |
|
331 | - if (is_a($object, $classname)) { |
|
332 | - return true; |
|
333 | - } |
|
334 | - } |
|
335 | - return false; |
|
336 | - } |
|
337 | - |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * Returns the list of model field classes that that the API basically ignores |
|
342 | - * |
|
343 | - * @return array |
|
344 | - */ |
|
345 | - public function fields_ignored() |
|
346 | - { |
|
347 | - return apply_filters( |
|
348 | - 'FHEE__Controller_Model_Read_fields_ignored', |
|
349 | - array('EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field') |
|
350 | - ); |
|
351 | - } |
|
352 | - |
|
353 | - |
|
354 | - |
|
355 | - /** |
|
356 | - * If this field one that should be ignored by the API? |
|
357 | - * |
|
358 | - * @param EE_Model_Field_Base |
|
359 | - * @return boolean |
|
360 | - */ |
|
361 | - public function field_is_ignored($field_obj) |
|
362 | - { |
|
363 | - return $this->is_subclass_of_one($field_obj, $this->fields_ignored()); |
|
364 | - } |
|
365 | - |
|
366 | - |
|
367 | - |
|
368 | - /** |
|
369 | - * Returns the list of model field classes that have a "raw" and non-raw formats. |
|
370 | - * Normally the "raw" versions are only accessible to those who can edit them. |
|
371 | - * |
|
372 | - * @return array an array of EE_Model_Field_Base child classnames |
|
373 | - */ |
|
374 | - public function fields_that_have_rendered_format() |
|
375 | - { |
|
376 | - return apply_filters( |
|
377 | - 'FHEE__Controller_Model_Read__fields_raw', |
|
378 | - array('EE_Post_Content_Field', 'EE_Full_HTML_Field') |
|
379 | - ); |
|
380 | - } |
|
381 | - |
|
382 | - |
|
383 | - |
|
384 | - /** |
|
385 | - * If this field one that has a raw format |
|
386 | - * |
|
387 | - * @param EE_Model_Field_Base |
|
388 | - * @return boolean |
|
389 | - */ |
|
390 | - public function field_has_rendered_format($field_obj) |
|
391 | - { |
|
392 | - return $this->is_subclass_of_one($field_obj, $this->fields_that_have_rendered_format()); |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - |
|
397 | - /** |
|
398 | - * Returns the list of model field classes that have a "_pretty" and non-pretty versions. |
|
399 | - * The pretty version of the field is NOT queryable or editable, but requires no extra permissions |
|
400 | - * to view |
|
401 | - * |
|
402 | - * @return array an array of EE_Model_Field_Base child classnames |
|
403 | - */ |
|
404 | - public function fields_that_have_pretty_format() |
|
405 | - { |
|
406 | - return apply_filters( |
|
407 | - 'FHEE__Controller_Model_Read__fields_pretty', |
|
408 | - array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field') |
|
409 | - ); |
|
410 | - } |
|
411 | - |
|
412 | - |
|
413 | - |
|
414 | - /** |
|
415 | - * If this field one that has a pretty equivalent |
|
416 | - * |
|
417 | - * @param EE_Model_Field_Base |
|
418 | - * @return boolean |
|
419 | - */ |
|
420 | - public function field_has_pretty_format($field_obj) |
|
421 | - { |
|
422 | - return $this->is_subclass_of_one($field_obj, $this->fields_that_have_pretty_format()); |
|
423 | - } |
|
424 | - |
|
425 | - |
|
426 | - |
|
427 | - /** |
|
428 | - * Returns an array describing what extra API resource properties have been added through the versions |
|
429 | - * |
|
430 | - * @return array @see $this->_extra_resource_properties_for_models |
|
431 | - */ |
|
432 | - public function resource_changes() |
|
433 | - { |
|
434 | - return $this->_resource_changes; |
|
435 | - } |
|
436 | - |
|
437 | - |
|
438 | - |
|
439 | - /** |
|
440 | - * Returns an array where keys are extra resource properties in this version of the API, |
|
441 | - * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes |
|
442 | - * |
|
443 | - * @param \EEM_Base $model |
|
444 | - * @return array |
|
445 | - */ |
|
446 | - public function extra_resource_properties_for_model($model) |
|
447 | - { |
|
448 | - $extra_properties = array(); |
|
449 | - foreach ($this->resource_changes_between_requested_version_and_current() as $version => $model_classnames) { |
|
450 | - foreach ($model_classnames as $model_classname => $properties_added_in_this_version) { |
|
451 | - if (is_subclass_of($model, $model_classname)) { |
|
452 | - $extra_properties = array_merge($extra_properties, $properties_added_in_this_version); |
|
453 | - } |
|
454 | - } |
|
455 | - } |
|
456 | - return $extra_properties; |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - |
|
461 | - /** |
|
462 | - * Gets all the related models for the specified model. It's good to use this |
|
463 | - * in case this model didn't exist for this version or something |
|
464 | - * |
|
465 | - * @param \EEM_Base $model |
|
466 | - * @return \EE_Model_Relation_Base[] |
|
467 | - */ |
|
468 | - public function relation_settings(\EEM_Base $model) |
|
469 | - { |
|
470 | - $relations = array(); |
|
471 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
472 | - if ($this->is_model_name_in_this_version($relation_name)) { |
|
473 | - $relations[$relation_name] = $relation_obj; |
|
474 | - } |
|
475 | - } |
|
476 | - //filter the results, but use the old filter name |
|
477 | - return apply_filters( |
|
478 | - 'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include', |
|
479 | - $relations, |
|
480 | - $model |
|
481 | - ); |
|
482 | - } |
|
27 | + /** |
|
28 | + * Constant used in the $_model_changes array to indicate that a model |
|
29 | + * was completely new in this version |
|
30 | + */ |
|
31 | + const model_added = 'model_added_in_this_version'; |
|
32 | + |
|
33 | + /** |
|
34 | + * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
35 | + * next-level keys are model names (eg "Event") that underwent some change in that version |
|
36 | + * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version), |
|
37 | + * or it's an array where the values are model field names, |
|
38 | + * or API resource properties (ie, non-model fields that appear in REST API results) |
|
39 | + * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
40 | + * |
|
41 | + * @var array |
|
42 | + */ |
|
43 | + protected $_model_changes = array(); |
|
44 | + |
|
45 | + /** |
|
46 | + * top-level keys are version numbers, |
|
47 | + * next-level keys are model CLASSNAMES (even parent classnames), |
|
48 | + * and next-level keys are extra resource properties to attach to those models' resources, |
|
49 | + * and next-level key-value pairs, where the keys are: |
|
50 | + * 'raw', 'type', 'nullable', 'table_alias', 'table_column', 'always_available' |
|
51 | + * |
|
52 | + * @var array |
|
53 | + */ |
|
54 | + protected $_resource_changes = array(); |
|
55 | + |
|
56 | + /** |
|
57 | + * @var string indicating what version of the API was requested |
|
58 | + * (eg although core might be at version 4.8.11, they may have sent a request |
|
59 | + * for 4.6) |
|
60 | + */ |
|
61 | + protected $_requested_version = null; |
|
62 | + |
|
63 | + /** |
|
64 | + * Keys are model names, values are their classnames. |
|
65 | + * We cache this so we only need to calculate this once per request |
|
66 | + * |
|
67 | + * @var array |
|
68 | + */ |
|
69 | + protected $_cached_models_for_requested_version = null; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + protected $_cached_model_changes_between_requested_version_and_current = null; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var array |
|
78 | + */ |
|
79 | + protected $_cached_resource_changes_between_requested_version_and_current = null; |
|
80 | + |
|
81 | + /** |
|
82 | + * 2d array where top-level keys are model names, 2nd-level keys are field names |
|
83 | + * and values are the actual field objects |
|
84 | + * |
|
85 | + * @var array |
|
86 | + */ |
|
87 | + protected $_cached_fields_on_models = array(); |
|
88 | + |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Model_Version_Info constructor. |
|
93 | + * |
|
94 | + * @param string $requested_version |
|
95 | + */ |
|
96 | + public function __construct($requested_version) |
|
97 | + { |
|
98 | + $this->_requested_version = $requested_version; |
|
99 | + $this->_model_changes = array( |
|
100 | + '4.8.29' => array( |
|
101 | + //first version where the REST API is in EE core, so no need |
|
102 | + //to specify how its different from the previous |
|
103 | + ), |
|
104 | + ); |
|
105 | + //setup data for "extra" fields added onto resources which don't actually exist on models |
|
106 | + $this->_resource_changes = apply_filters( |
|
107 | + 'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models', |
|
108 | + array() |
|
109 | + ); |
|
110 | + $defaults = array( |
|
111 | + 'raw' => false, |
|
112 | + 'type' => 'N/A', |
|
113 | + 'nullable' => true, |
|
114 | + 'table_alias' => 'N/A', |
|
115 | + 'table_column' => 'N/A', |
|
116 | + 'always_available' => true, |
|
117 | + ); |
|
118 | + foreach ($this->_resource_changes as $version => $model_classnames) { |
|
119 | + foreach ($model_classnames as $model_classname => $extra_fields) { |
|
120 | + foreach ($extra_fields as $fieldname => $field_data) { |
|
121 | + $this->_resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
|
122 | + foreach ($defaults as $attribute => $default_value) { |
|
123 | + if (! isset($this->_resource_changes[$model_classname][$fieldname][$attribute])) { |
|
124 | + $this->_resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | + } |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * Returns a slice of Model_Version_Info::model_changes()'s array |
|
136 | + * indicating exactly what changes happened between the current core version, |
|
137 | + * and the version requested |
|
138 | + * |
|
139 | + * @return array |
|
140 | + */ |
|
141 | + public function model_changes_between_requested_version_and_current() |
|
142 | + { |
|
143 | + if ($this->_cached_model_changes_between_requested_version_and_current === null) { |
|
144 | + $model_changes = array(); |
|
145 | + foreach ($this->model_changes() as $version => $models_changed_in_version) { |
|
146 | + if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
147 | + $model_changes[$version] = $models_changed_in_version; |
|
148 | + } |
|
149 | + } |
|
150 | + $this->_cached_model_changes_between_requested_version_and_current = $model_changes; |
|
151 | + } |
|
152 | + return $this->_cached_model_changes_between_requested_version_and_current; |
|
153 | + } |
|
154 | + |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * Returns a slice of Model_Version_Info::model_changes()'s array |
|
159 | + * indicating exactly what changes happened between the current core version, |
|
160 | + * and the version requested |
|
161 | + * |
|
162 | + * @return array |
|
163 | + */ |
|
164 | + public function resource_changes_between_requested_version_and_current() |
|
165 | + { |
|
166 | + if ($this->_cached_resource_changes_between_requested_version_and_current === null) { |
|
167 | + $resource_changes = array(); |
|
168 | + foreach ($this->resource_changes() as $version => $model_classnames) { |
|
169 | + if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
170 | + $resource_changes[$version] = $model_classnames; |
|
171 | + } |
|
172 | + } |
|
173 | + $this->_cached_resource_changes_between_requested_version_and_current = $resource_changes; |
|
174 | + } |
|
175 | + return $this->_cached_resource_changes_between_requested_version_and_current; |
|
176 | + } |
|
177 | + |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7' |
|
182 | + * |
|
183 | + * @return string like '4.6' |
|
184 | + */ |
|
185 | + public function requested_version() |
|
186 | + { |
|
187 | + return $this->_requested_version; |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + |
|
192 | + /** |
|
193 | + * Returns an array describing how the models have changed in each version of core |
|
194 | + * that supports the API (starting at 4.6) |
|
195 | + * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
196 | + * next-level keys are model names (eg "Event") that underwent some change in that version |
|
197 | + * and the value is either NULL (indicating the model is completely NEW in this version), |
|
198 | + * or it's an array where fields are value names. |
|
199 | + * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
200 | + * |
|
201 | + * @return array |
|
202 | + */ |
|
203 | + public function model_changes() |
|
204 | + { |
|
205 | + return $this->_model_changes; |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * Takes into account the requested version, and the current version, and |
|
212 | + * what changed between the two, and tries to return. |
|
213 | + * Analogous to EE_Registry::instance()->non_abstract_db_models |
|
214 | + * |
|
215 | + * @return array keys are model names, values are their classname |
|
216 | + */ |
|
217 | + public function models_for_requested_version() |
|
218 | + { |
|
219 | + if ($this->_cached_models_for_requested_version === null) { |
|
220 | + $all_models_in_current_version = \EE_Registry::instance()->non_abstract_db_models; |
|
221 | + foreach ($this->model_changes_between_requested_version_and_current() as $version => $models_changed) { |
|
222 | + foreach ($models_changed as $model_name => $new_indicator_or_fields_added) { |
|
223 | + if ($new_indicator_or_fields_added === Model_Version_Info::model_added) { |
|
224 | + unset($all_models_in_current_version[$model_name]); |
|
225 | + } |
|
226 | + } |
|
227 | + } |
|
228 | + $this->_cached_models_for_requested_version = apply_filters( |
|
229 | + 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
230 | + $all_models_in_current_version, |
|
231 | + $this |
|
232 | + ); |
|
233 | + } |
|
234 | + return $this->_cached_models_for_requested_version; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + |
|
239 | + /** |
|
240 | + * Determines if this is a valid model name in the requested version. |
|
241 | + * Similar to EE_Registry::instance()->is_model_name(), but takes the requested |
|
242 | + * version's models into account |
|
243 | + * |
|
244 | + * @param string $model_name eg 'Event' |
|
245 | + * @return boolean |
|
246 | + */ |
|
247 | + public function is_model_name_in_this_version($model_name) |
|
248 | + { |
|
249 | + $model_names = $this->models_for_requested_version(); |
|
250 | + if (isset($model_names[$model_name])) { |
|
251 | + return true; |
|
252 | + } else { |
|
253 | + return false; |
|
254 | + } |
|
255 | + } |
|
256 | + |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Wrapper for EE_Registry::instance()->load_model(), but takes the requested |
|
261 | + * version's models into account |
|
262 | + * |
|
263 | + * @param string $model_name |
|
264 | + * @return \EEM_Base |
|
265 | + * @throws \EE_Error |
|
266 | + */ |
|
267 | + public function load_model($model_name) |
|
268 | + { |
|
269 | + if ($this->is_model_name_in_this_version($model_name)) { |
|
270 | + return \EE_Registry::instance()->load_model($model_name); |
|
271 | + } else { |
|
272 | + throw new \EE_Error( |
|
273 | + sprintf( |
|
274 | + __( |
|
275 | + 'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso', |
|
276 | + 'event_espresso' |
|
277 | + ), |
|
278 | + $model_name, |
|
279 | + $this->requested_version() |
|
280 | + ) |
|
281 | + ); |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + |
|
287 | + /** |
|
288 | + * Gets all the fields that should exist on this model right now |
|
289 | + * |
|
290 | + * @param \EEM_Base $model |
|
291 | + * @return array|\EE_Model_Field_Base[] |
|
292 | + */ |
|
293 | + public function fields_on_model_in_this_version($model) |
|
294 | + { |
|
295 | + if (! isset($this->_cached_fields_on_models[$model->get_this_model_name()])) { |
|
296 | + //get all model changes between the requested version and current core version |
|
297 | + $changes = $this->model_changes_between_requested_version_and_current(); |
|
298 | + //fetch all fields currently on this model |
|
299 | + $current_fields = $model->field_settings(); |
|
300 | + //remove all fields that have been added since |
|
301 | + foreach ($changes as $version => $changes_in_version) { |
|
302 | + if ( |
|
303 | + isset($changes_in_version[$model->get_this_model_name()]) |
|
304 | + && $changes_in_version[$model->get_this_model_name()] !== Model_Version_Info::model_added |
|
305 | + ) { |
|
306 | + $current_fields = array_diff_key( |
|
307 | + $current_fields, |
|
308 | + array_flip($changes_in_version[$model->get_this_model_name()]) |
|
309 | + ); |
|
310 | + } |
|
311 | + } |
|
312 | + $this->_cached_fields_on_models = $current_fields; |
|
313 | + } |
|
314 | + return $this->_cached_fields_on_models; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Determines if $object is of one of the classes of $classes. Similar to |
|
321 | + * in_array(), except this checks if $object is a subclass of the classnames provided |
|
322 | + * in $classnames |
|
323 | + * |
|
324 | + * @param object $object |
|
325 | + * @param array $classnames |
|
326 | + * @return boolean |
|
327 | + */ |
|
328 | + public function is_subclass_of_one($object, $classnames) |
|
329 | + { |
|
330 | + foreach ($classnames as $classname) { |
|
331 | + if (is_a($object, $classname)) { |
|
332 | + return true; |
|
333 | + } |
|
334 | + } |
|
335 | + return false; |
|
336 | + } |
|
337 | + |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * Returns the list of model field classes that that the API basically ignores |
|
342 | + * |
|
343 | + * @return array |
|
344 | + */ |
|
345 | + public function fields_ignored() |
|
346 | + { |
|
347 | + return apply_filters( |
|
348 | + 'FHEE__Controller_Model_Read_fields_ignored', |
|
349 | + array('EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field') |
|
350 | + ); |
|
351 | + } |
|
352 | + |
|
353 | + |
|
354 | + |
|
355 | + /** |
|
356 | + * If this field one that should be ignored by the API? |
|
357 | + * |
|
358 | + * @param EE_Model_Field_Base |
|
359 | + * @return boolean |
|
360 | + */ |
|
361 | + public function field_is_ignored($field_obj) |
|
362 | + { |
|
363 | + return $this->is_subclass_of_one($field_obj, $this->fields_ignored()); |
|
364 | + } |
|
365 | + |
|
366 | + |
|
367 | + |
|
368 | + /** |
|
369 | + * Returns the list of model field classes that have a "raw" and non-raw formats. |
|
370 | + * Normally the "raw" versions are only accessible to those who can edit them. |
|
371 | + * |
|
372 | + * @return array an array of EE_Model_Field_Base child classnames |
|
373 | + */ |
|
374 | + public function fields_that_have_rendered_format() |
|
375 | + { |
|
376 | + return apply_filters( |
|
377 | + 'FHEE__Controller_Model_Read__fields_raw', |
|
378 | + array('EE_Post_Content_Field', 'EE_Full_HTML_Field') |
|
379 | + ); |
|
380 | + } |
|
381 | + |
|
382 | + |
|
383 | + |
|
384 | + /** |
|
385 | + * If this field one that has a raw format |
|
386 | + * |
|
387 | + * @param EE_Model_Field_Base |
|
388 | + * @return boolean |
|
389 | + */ |
|
390 | + public function field_has_rendered_format($field_obj) |
|
391 | + { |
|
392 | + return $this->is_subclass_of_one($field_obj, $this->fields_that_have_rendered_format()); |
|
393 | + } |
|
394 | + |
|
395 | + |
|
396 | + |
|
397 | + /** |
|
398 | + * Returns the list of model field classes that have a "_pretty" and non-pretty versions. |
|
399 | + * The pretty version of the field is NOT queryable or editable, but requires no extra permissions |
|
400 | + * to view |
|
401 | + * |
|
402 | + * @return array an array of EE_Model_Field_Base child classnames |
|
403 | + */ |
|
404 | + public function fields_that_have_pretty_format() |
|
405 | + { |
|
406 | + return apply_filters( |
|
407 | + 'FHEE__Controller_Model_Read__fields_pretty', |
|
408 | + array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field') |
|
409 | + ); |
|
410 | + } |
|
411 | + |
|
412 | + |
|
413 | + |
|
414 | + /** |
|
415 | + * If this field one that has a pretty equivalent |
|
416 | + * |
|
417 | + * @param EE_Model_Field_Base |
|
418 | + * @return boolean |
|
419 | + */ |
|
420 | + public function field_has_pretty_format($field_obj) |
|
421 | + { |
|
422 | + return $this->is_subclass_of_one($field_obj, $this->fields_that_have_pretty_format()); |
|
423 | + } |
|
424 | + |
|
425 | + |
|
426 | + |
|
427 | + /** |
|
428 | + * Returns an array describing what extra API resource properties have been added through the versions |
|
429 | + * |
|
430 | + * @return array @see $this->_extra_resource_properties_for_models |
|
431 | + */ |
|
432 | + public function resource_changes() |
|
433 | + { |
|
434 | + return $this->_resource_changes; |
|
435 | + } |
|
436 | + |
|
437 | + |
|
438 | + |
|
439 | + /** |
|
440 | + * Returns an array where keys are extra resource properties in this version of the API, |
|
441 | + * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes |
|
442 | + * |
|
443 | + * @param \EEM_Base $model |
|
444 | + * @return array |
|
445 | + */ |
|
446 | + public function extra_resource_properties_for_model($model) |
|
447 | + { |
|
448 | + $extra_properties = array(); |
|
449 | + foreach ($this->resource_changes_between_requested_version_and_current() as $version => $model_classnames) { |
|
450 | + foreach ($model_classnames as $model_classname => $properties_added_in_this_version) { |
|
451 | + if (is_subclass_of($model, $model_classname)) { |
|
452 | + $extra_properties = array_merge($extra_properties, $properties_added_in_this_version); |
|
453 | + } |
|
454 | + } |
|
455 | + } |
|
456 | + return $extra_properties; |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + |
|
461 | + /** |
|
462 | + * Gets all the related models for the specified model. It's good to use this |
|
463 | + * in case this model didn't exist for this version or something |
|
464 | + * |
|
465 | + * @param \EEM_Base $model |
|
466 | + * @return \EE_Model_Relation_Base[] |
|
467 | + */ |
|
468 | + public function relation_settings(\EEM_Base $model) |
|
469 | + { |
|
470 | + $relations = array(); |
|
471 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
472 | + if ($this->is_model_name_in_this_version($relation_name)) { |
|
473 | + $relations[$relation_name] = $relation_obj; |
|
474 | + } |
|
475 | + } |
|
476 | + //filter the results, but use the old filter name |
|
477 | + return apply_filters( |
|
478 | + 'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include', |
|
479 | + $relations, |
|
480 | + $model |
|
481 | + ); |
|
482 | + } |
|
483 | 483 | |
484 | 484 | } |
485 | 485 |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace EventEspresso\core\libraries\rest_api; |
3 | 3 | |
4 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
4 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
5 | 5 | exit('No direct script access allowed'); |
6 | 6 | } |
7 | 7 | |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | foreach ($extra_fields as $fieldname => $field_data) { |
121 | 121 | $this->_resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
122 | 122 | foreach ($defaults as $attribute => $default_value) { |
123 | - if (! isset($this->_resource_changes[$model_classname][$fieldname][$attribute])) { |
|
123 | + if ( ! isset($this->_resource_changes[$model_classname][$fieldname][$attribute])) { |
|
124 | 124 | $this->_resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
125 | 125 | } |
126 | 126 | } |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | */ |
293 | 293 | public function fields_on_model_in_this_version($model) |
294 | 294 | { |
295 | - if (! isset($this->_cached_fields_on_models[$model->get_this_model_name()])) { |
|
295 | + if ( ! isset($this->_cached_fields_on_models[$model->get_this_model_name()])) { |
|
296 | 296 | //get all model changes between the requested version and current core version |
297 | 297 | $changes = $this->model_changes_between_requested_version_and_current(); |
298 | 298 | //fetch all fields currently on this model |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | namespace EventEspresso\core\libraries\rest_api; |
3 | 3 | |
4 | 4 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
5 | - exit('No direct script access allowed'); |
|
5 | + exit('No direct script access allowed'); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | |
@@ -27,485 +27,485 @@ discard block |
||
27 | 27 | class Model_Data_Translator |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
32 | - * fields that COULD contain -1; so we use null |
|
33 | - */ |
|
34 | - const ee_inf_in_rest = null; |
|
35 | - |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * Prepares a possible array of input values from JSON for use by the models |
|
40 | - * |
|
41 | - * @param \EE_Model_Field_Base $field_obj |
|
42 | - * @param mixed $original_value_maybe_array |
|
43 | - * @param string $requested_version |
|
44 | - * @param string $timezone_string treat values as being in this timezone |
|
45 | - * @return mixed |
|
46 | - * @throws \DomainException |
|
47 | - */ |
|
48 | - public static function prepare_field_values_from_json( |
|
49 | - $field_obj, |
|
50 | - $original_value_maybe_array, |
|
51 | - $requested_version, |
|
52 | - $timezone_string = 'UTC' |
|
53 | - ) { |
|
54 | - if (is_array($original_value_maybe_array)) { |
|
55 | - $new_value_maybe_array = array(); |
|
56 | - foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
57 | - $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_from_json( |
|
58 | - $field_obj, |
|
59 | - $array_item, |
|
60 | - $requested_version, |
|
61 | - $timezone_string |
|
62 | - ); |
|
63 | - } |
|
64 | - } else { |
|
65 | - $new_value_maybe_array = Model_Data_Translator::prepare_field_value_from_json( |
|
66 | - $field_obj, |
|
67 | - $original_value_maybe_array, |
|
68 | - $requested_version, |
|
69 | - $timezone_string |
|
70 | - ); |
|
71 | - } |
|
72 | - return $new_value_maybe_array; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Prepares an array of field values FOR use in JSON/REST API |
|
79 | - * |
|
80 | - * @param \EE_Model_Field_Base $field_obj |
|
81 | - * @param mixed $original_value_maybe_array |
|
82 | - * @param string $request_version (eg 4.8.36) |
|
83 | - * @return array |
|
84 | - */ |
|
85 | - public static function prepare_field_values_for_json($field_obj, $original_value_maybe_array, $request_version) |
|
86 | - { |
|
87 | - if (is_array($original_value_maybe_array)) { |
|
88 | - $new_value_maybe_array = array(); |
|
89 | - foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
90 | - $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
91 | - $field_obj, |
|
92 | - $array_item, |
|
93 | - $request_version |
|
94 | - ); |
|
95 | - } |
|
96 | - } else { |
|
97 | - $new_value_maybe_array = Model_Data_Translator::prepare_field_value_for_json( |
|
98 | - $field_obj, |
|
99 | - $original_value_maybe_array, |
|
100 | - $request_version |
|
101 | - ); |
|
102 | - } |
|
103 | - return $new_value_maybe_array; |
|
104 | - } |
|
105 | - |
|
106 | - |
|
107 | - |
|
108 | - /** |
|
109 | - * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
110 | - * "$query_params". |
|
111 | - * |
|
112 | - * @param \EE_Model_Field_Base $field_obj |
|
113 | - * @param mixed $original_value |
|
114 | - * @param string $requested_version |
|
115 | - * @param string $timezone_string treat values as being in this timezone |
|
116 | - * @return mixed |
|
117 | - * @throws \DomainException |
|
118 | - */ |
|
119 | - public static function prepare_field_value_from_json( |
|
120 | - $field_obj, |
|
121 | - $original_value, |
|
122 | - $requested_version, |
|
123 | - $timezone_string = 'UTC' // UTC |
|
124 | - ) |
|
125 | - { |
|
126 | - $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
127 | - $new_value = null; |
|
128 | - if ($field_obj instanceof \EE_Infinite_Integer_Field |
|
129 | - && in_array($original_value, array(null, ''), true) |
|
130 | - ) { |
|
131 | - $new_value = EE_INF; |
|
132 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
133 | - list($offset_sign, $offset_secs) = Model_Data_Translator::parse_timezone_offset( |
|
134 | - $field_obj->get_timezone_offset( |
|
135 | - new \DateTimeZone($timezone_string) |
|
136 | - ) |
|
137 | - ); |
|
138 | - $offset_string = |
|
139 | - str_pad( |
|
140 | - floor($offset_secs / HOUR_IN_SECONDS), |
|
141 | - 2, |
|
142 | - '0', |
|
143 | - STR_PAD_LEFT |
|
144 | - ) |
|
145 | - . ':' |
|
146 | - . str_pad( |
|
147 | - ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
148 | - 2, |
|
149 | - '0', |
|
150 | - STR_PAD_LEFT |
|
151 | - ); |
|
152 | - $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
153 | - } else { |
|
154 | - $new_value = $original_value; |
|
155 | - } |
|
156 | - return $new_value; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * determines what's going on with them timezone strings |
|
163 | - * |
|
164 | - * @param int $timezone_offset |
|
165 | - * @return array |
|
166 | - */ |
|
167 | - private static function parse_timezone_offset($timezone_offset) |
|
168 | - { |
|
169 | - $first_char = substr((string)$timezone_offset, 0, 1); |
|
170 | - if ($first_char === '+' || $first_char === '-') { |
|
171 | - $offset_sign = $first_char; |
|
172 | - $offset_secs = substr((string)$timezone_offset, 1); |
|
173 | - } else { |
|
174 | - $offset_sign = '+'; |
|
175 | - $offset_secs = $timezone_offset; |
|
176 | - } |
|
177 | - return array($offset_sign, $offset_secs); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - |
|
182 | - /** |
|
183 | - * Prepares a field's value for display in the API |
|
184 | - * |
|
185 | - * @param \EE_Model_Field_Base $field_obj |
|
186 | - * @param mixed $original_value |
|
187 | - * @param string $requested_version |
|
188 | - * @return mixed |
|
189 | - */ |
|
190 | - public static function prepare_field_value_for_json($field_obj, $original_value, $requested_version) |
|
191 | - { |
|
192 | - if ($original_value === EE_INF) { |
|
193 | - $new_value = Model_Data_Translator::ee_inf_in_rest; |
|
194 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
195 | - if ($original_value instanceof \DateTime) { |
|
196 | - $new_value = $original_value->format('Y-m-d H:i:s'); |
|
197 | - } elseif (is_int($original_value)) { |
|
198 | - $new_value = date('Y-m-d H:i:s', $original_value); |
|
199 | - } else { |
|
200 | - $new_value = $original_value; |
|
201 | - } |
|
202 | - $new_value = mysql_to_rfc3339($new_value); |
|
203 | - } else { |
|
204 | - $new_value = $original_value; |
|
205 | - } |
|
206 | - return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
207 | - $new_value, |
|
208 | - $field_obj, |
|
209 | - $original_value, |
|
210 | - $requested_version |
|
211 | - ); |
|
212 | - } |
|
213 | - |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * Prepares condition-query-parameters (like what's in where and having) from |
|
218 | - * the format expected in the API to use in the models |
|
219 | - * |
|
220 | - * @param array $inputted_query_params_of_this_type |
|
221 | - * @param \EEM_Base $model |
|
222 | - * @param string $requested_version |
|
223 | - * @return array |
|
224 | - * @throws \DomainException |
|
225 | - * @throws \EE_Error |
|
226 | - */ |
|
227 | - public static function prepare_conditions_query_params_for_models( |
|
228 | - $inputted_query_params_of_this_type, |
|
229 | - \EEM_Base $model, |
|
230 | - $requested_version |
|
231 | - ) { |
|
232 | - $query_param_for_models = array(); |
|
233 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
234 | - $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key); |
|
235 | - $field = Model_Data_Translator::deduce_field_from_query_param( |
|
236 | - $query_param_sans_stars, |
|
237 | - $model |
|
238 | - ); |
|
239 | - //double-check is it a *_gmt field? |
|
240 | - if (! $field instanceof \EE_Model_Field_Base |
|
241 | - && Model_Data_Translator::is_gmt_date_field_name($query_param_sans_stars) |
|
242 | - ) { |
|
243 | - //yep, take off '_gmt', and find the field |
|
244 | - $query_param_key = Model_Data_Translator::remove_gmt_from_field_name($query_param_sans_stars); |
|
245 | - $field = Model_Data_Translator::deduce_field_from_query_param( |
|
246 | - $query_param_key, |
|
247 | - $model |
|
248 | - ); |
|
249 | - $timezone = 'UTC'; |
|
250 | - } else { |
|
251 | - //so it's not a GMT field. Set the timezone on the model to the default |
|
252 | - $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
253 | - } |
|
254 | - if ($field instanceof \EE_Model_Field_Base) { |
|
255 | - //did they specify an operator? |
|
256 | - if (is_array($query_param_value)) { |
|
257 | - $op = $query_param_value[0]; |
|
258 | - $translated_value = array($op); |
|
259 | - if (isset($query_param_value[1])) { |
|
260 | - $value = $query_param_value[1]; |
|
261 | - $translated_value[1] = Model_Data_Translator::prepare_field_values_from_json($field, $value, |
|
262 | - $requested_version, $timezone); |
|
263 | - } |
|
264 | - } else { |
|
265 | - $translated_value = Model_Data_Translator::prepare_field_value_from_json($field, $query_param_value, |
|
266 | - $requested_version, $timezone); |
|
267 | - } |
|
268 | - $query_param_for_models[$query_param_key] = $translated_value; |
|
269 | - } else { |
|
270 | - //so it's not for a field, assume it's a logic query param key |
|
271 | - $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_models($query_param_value, |
|
272 | - $model, $requested_version); |
|
273 | - } |
|
274 | - } |
|
275 | - return $query_param_for_models; |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
282 | - * gmt date field name |
|
283 | - * |
|
284 | - * @param string $field_name |
|
285 | - * @return boolean |
|
286 | - */ |
|
287 | - public static function is_gmt_date_field_name($field_name) |
|
288 | - { |
|
289 | - return substr( |
|
290 | - Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name), |
|
291 | - -4, |
|
292 | - 4 |
|
293 | - ) === '_gmt'; |
|
294 | - } |
|
295 | - |
|
296 | - |
|
297 | - |
|
298 | - /** |
|
299 | - * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
300 | - * |
|
301 | - * @param string $field_name |
|
302 | - * @return string |
|
303 | - */ |
|
304 | - public static function remove_gmt_from_field_name($field_name) |
|
305 | - { |
|
306 | - if (! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
307 | - return $field_name; |
|
308 | - } |
|
309 | - $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name); |
|
310 | - $query_param_sans_gmt_and_sans_stars = substr( |
|
311 | - $query_param_sans_stars, |
|
312 | - 0, |
|
313 | - strrpos( |
|
314 | - $field_name, |
|
315 | - '_gmt' |
|
316 | - ) |
|
317 | - ); |
|
318 | - return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
319 | - } |
|
320 | - |
|
321 | - |
|
322 | - |
|
323 | - /** |
|
324 | - * Takes a field name from the REST API and prepares it for the model querying |
|
325 | - * |
|
326 | - * @param string $field_name |
|
327 | - * @return string |
|
328 | - */ |
|
329 | - public static function prepare_field_name_from_json($field_name) |
|
330 | - { |
|
331 | - if (Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
332 | - return Model_Data_Translator::remove_gmt_from_field_name($field_name); |
|
333 | - } |
|
334 | - return $field_name; |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - |
|
339 | - /** |
|
340 | - * Takes array of field names from REST API and prepares for models |
|
341 | - * |
|
342 | - * @param array $field_names |
|
343 | - * @return array of field names (possibly include model prefixes) |
|
344 | - */ |
|
345 | - public static function prepare_field_names_from_json(array $field_names) |
|
346 | - { |
|
347 | - $new_array = array(); |
|
348 | - foreach ($field_names as $key => $field_name) { |
|
349 | - $new_array[$key] = Model_Data_Translator::prepare_field_name_from_json($field_name); |
|
350 | - } |
|
351 | - return $new_array; |
|
352 | - } |
|
353 | - |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * Takes array where array keys are field names (possibly with model path prefixes) |
|
358 | - * from the REST API and prepares them for model querying |
|
359 | - * |
|
360 | - * @param array $field_names_as_keys |
|
361 | - * @return array |
|
362 | - */ |
|
363 | - public static function prepare_field_names_in_array_keys_from_json(array $field_names_as_keys) |
|
364 | - { |
|
365 | - $new_array = array(); |
|
366 | - foreach ($field_names_as_keys as $field_name => $value) { |
|
367 | - $new_array[Model_Data_Translator::prepare_field_name_from_json($field_name)] = $value; |
|
368 | - } |
|
369 | - return $new_array; |
|
370 | - } |
|
371 | - |
|
372 | - |
|
373 | - |
|
374 | - /** |
|
375 | - * Prepares an array of model query params for use in the REST API |
|
376 | - * |
|
377 | - * @param array $model_query_params |
|
378 | - * @param \EEM_Base $model |
|
379 | - * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
380 | - * REST API |
|
381 | - * @return array which can be passed into the EE4 REST API when querying a model resource |
|
382 | - * @throws \EE_Error |
|
383 | - */ |
|
384 | - public static function prepare_query_params_for_rest_api( |
|
385 | - array $model_query_params, |
|
386 | - \EEM_Base $model, |
|
387 | - $requested_version = null |
|
388 | - ) { |
|
389 | - if ($requested_version === null) { |
|
390 | - $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
391 | - } |
|
392 | - $rest_query_params = $model_query_params; |
|
393 | - if (isset($model_query_params[0])) { |
|
394 | - $rest_query_params['where'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
395 | - $model_query_params[0], |
|
396 | - $model, |
|
397 | - $requested_version |
|
398 | - ); |
|
399 | - unset($rest_query_params[0]); |
|
400 | - } |
|
401 | - if (isset($model_query_params['having'])) { |
|
402 | - $rest_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
403 | - $model_query_params['having'], |
|
404 | - $model, |
|
405 | - $requested_version |
|
406 | - ); |
|
407 | - } |
|
408 | - return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
409 | - $rest_query_params, $model_query_params, $model, $requested_version); |
|
410 | - } |
|
411 | - |
|
412 | - |
|
413 | - |
|
414 | - /** |
|
415 | - * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
416 | - * |
|
417 | - * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
418 | - * passed into EEM_Base::get_all() |
|
419 | - * @param \EEM_Base $model |
|
420 | - * @param string $requested_version eg "4.8.36" |
|
421 | - * @return array ready for use in the rest api query params |
|
422 | - * @throws \EE_Error |
|
423 | - */ |
|
424 | - public static function prepare_conditions_query_params_for_rest_api( |
|
425 | - $inputted_query_params_of_this_type, |
|
426 | - \EEM_Base $model, |
|
427 | - $requested_version |
|
428 | - ) { |
|
429 | - $query_param_for_models = array(); |
|
430 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
431 | - $field = Model_Data_Translator::deduce_field_from_query_param( |
|
432 | - Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key), |
|
433 | - $model |
|
434 | - ); |
|
435 | - if ($field instanceof \EE_Model_Field_Base) { |
|
436 | - //did they specify an operator? |
|
437 | - if (is_array($query_param_value)) { |
|
438 | - $op = $query_param_value[0]; |
|
439 | - $translated_value = array($op); |
|
440 | - if (isset($query_param_value[1])) { |
|
441 | - $value = $query_param_value[1]; |
|
442 | - $translated_value[1] = Model_Data_Translator::prepare_field_values_for_json($field, $value, |
|
443 | - $requested_version); |
|
444 | - } |
|
445 | - } else { |
|
446 | - $translated_value = Model_Data_Translator::prepare_field_value_for_json($field, $query_param_value, |
|
447 | - $requested_version); |
|
448 | - } |
|
449 | - $query_param_for_models[$query_param_key] = $translated_value; |
|
450 | - } else { |
|
451 | - //so it's not for a field, assume it's a logic query param key |
|
452 | - $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api($query_param_value, |
|
453 | - $model, $requested_version); |
|
454 | - } |
|
455 | - } |
|
456 | - return $query_param_for_models; |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - |
|
461 | - /** |
|
462 | - * @param $condition_query_param_key |
|
463 | - * @return string |
|
464 | - */ |
|
465 | - public static function remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
466 | - { |
|
467 | - $pos_of_star = strpos($condition_query_param_key, '*'); |
|
468 | - if ($pos_of_star === false) { |
|
469 | - return $condition_query_param_key; |
|
470 | - } else { |
|
471 | - $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
472 | - return $condition_query_param_sans_star; |
|
473 | - } |
|
474 | - } |
|
475 | - |
|
476 | - |
|
477 | - |
|
478 | - /** |
|
479 | - * Takes the input parameter and finds the model field that it indicates. |
|
480 | - * |
|
481 | - * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
482 | - * @param \EEM_Base $model |
|
483 | - * @return \EE_Model_Field_Base |
|
484 | - * @throws \EE_Error |
|
485 | - */ |
|
486 | - public static function deduce_field_from_query_param($query_param_name, \EEM_Base $model) |
|
487 | - { |
|
488 | - //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
489 | - //which will help us find the database table and column |
|
490 | - $query_param_parts = explode('.', $query_param_name); |
|
491 | - if (empty($query_param_parts)) { |
|
492 | - throw new \EE_Error(sprintf(__('_extract_column_name is empty when trying to extract column and table name from %s', |
|
493 | - 'event_espresso'), $query_param_name)); |
|
494 | - } |
|
495 | - $number_of_parts = count($query_param_parts); |
|
496 | - $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
497 | - if ($number_of_parts === 1) { |
|
498 | - $field_name = $last_query_param_part; |
|
499 | - } else {// $number_of_parts >= 2 |
|
500 | - //the last part is the column name, and there are only 2parts. therefore... |
|
501 | - $field_name = $last_query_param_part; |
|
502 | - $model = \EE_Registry::instance()->load_model($query_param_parts[$number_of_parts - 2]); |
|
503 | - } |
|
504 | - try { |
|
505 | - return $model->field_settings_for($field_name); |
|
506 | - } catch (\EE_Error $e) { |
|
507 | - return null; |
|
508 | - } |
|
509 | - } |
|
30 | + /** |
|
31 | + * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
32 | + * fields that COULD contain -1; so we use null |
|
33 | + */ |
|
34 | + const ee_inf_in_rest = null; |
|
35 | + |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * Prepares a possible array of input values from JSON for use by the models |
|
40 | + * |
|
41 | + * @param \EE_Model_Field_Base $field_obj |
|
42 | + * @param mixed $original_value_maybe_array |
|
43 | + * @param string $requested_version |
|
44 | + * @param string $timezone_string treat values as being in this timezone |
|
45 | + * @return mixed |
|
46 | + * @throws \DomainException |
|
47 | + */ |
|
48 | + public static function prepare_field_values_from_json( |
|
49 | + $field_obj, |
|
50 | + $original_value_maybe_array, |
|
51 | + $requested_version, |
|
52 | + $timezone_string = 'UTC' |
|
53 | + ) { |
|
54 | + if (is_array($original_value_maybe_array)) { |
|
55 | + $new_value_maybe_array = array(); |
|
56 | + foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
57 | + $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_from_json( |
|
58 | + $field_obj, |
|
59 | + $array_item, |
|
60 | + $requested_version, |
|
61 | + $timezone_string |
|
62 | + ); |
|
63 | + } |
|
64 | + } else { |
|
65 | + $new_value_maybe_array = Model_Data_Translator::prepare_field_value_from_json( |
|
66 | + $field_obj, |
|
67 | + $original_value_maybe_array, |
|
68 | + $requested_version, |
|
69 | + $timezone_string |
|
70 | + ); |
|
71 | + } |
|
72 | + return $new_value_maybe_array; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Prepares an array of field values FOR use in JSON/REST API |
|
79 | + * |
|
80 | + * @param \EE_Model_Field_Base $field_obj |
|
81 | + * @param mixed $original_value_maybe_array |
|
82 | + * @param string $request_version (eg 4.8.36) |
|
83 | + * @return array |
|
84 | + */ |
|
85 | + public static function prepare_field_values_for_json($field_obj, $original_value_maybe_array, $request_version) |
|
86 | + { |
|
87 | + if (is_array($original_value_maybe_array)) { |
|
88 | + $new_value_maybe_array = array(); |
|
89 | + foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
90 | + $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
91 | + $field_obj, |
|
92 | + $array_item, |
|
93 | + $request_version |
|
94 | + ); |
|
95 | + } |
|
96 | + } else { |
|
97 | + $new_value_maybe_array = Model_Data_Translator::prepare_field_value_for_json( |
|
98 | + $field_obj, |
|
99 | + $original_value_maybe_array, |
|
100 | + $request_version |
|
101 | + ); |
|
102 | + } |
|
103 | + return $new_value_maybe_array; |
|
104 | + } |
|
105 | + |
|
106 | + |
|
107 | + |
|
108 | + /** |
|
109 | + * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
110 | + * "$query_params". |
|
111 | + * |
|
112 | + * @param \EE_Model_Field_Base $field_obj |
|
113 | + * @param mixed $original_value |
|
114 | + * @param string $requested_version |
|
115 | + * @param string $timezone_string treat values as being in this timezone |
|
116 | + * @return mixed |
|
117 | + * @throws \DomainException |
|
118 | + */ |
|
119 | + public static function prepare_field_value_from_json( |
|
120 | + $field_obj, |
|
121 | + $original_value, |
|
122 | + $requested_version, |
|
123 | + $timezone_string = 'UTC' // UTC |
|
124 | + ) |
|
125 | + { |
|
126 | + $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
127 | + $new_value = null; |
|
128 | + if ($field_obj instanceof \EE_Infinite_Integer_Field |
|
129 | + && in_array($original_value, array(null, ''), true) |
|
130 | + ) { |
|
131 | + $new_value = EE_INF; |
|
132 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
133 | + list($offset_sign, $offset_secs) = Model_Data_Translator::parse_timezone_offset( |
|
134 | + $field_obj->get_timezone_offset( |
|
135 | + new \DateTimeZone($timezone_string) |
|
136 | + ) |
|
137 | + ); |
|
138 | + $offset_string = |
|
139 | + str_pad( |
|
140 | + floor($offset_secs / HOUR_IN_SECONDS), |
|
141 | + 2, |
|
142 | + '0', |
|
143 | + STR_PAD_LEFT |
|
144 | + ) |
|
145 | + . ':' |
|
146 | + . str_pad( |
|
147 | + ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
148 | + 2, |
|
149 | + '0', |
|
150 | + STR_PAD_LEFT |
|
151 | + ); |
|
152 | + $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
153 | + } else { |
|
154 | + $new_value = $original_value; |
|
155 | + } |
|
156 | + return $new_value; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * determines what's going on with them timezone strings |
|
163 | + * |
|
164 | + * @param int $timezone_offset |
|
165 | + * @return array |
|
166 | + */ |
|
167 | + private static function parse_timezone_offset($timezone_offset) |
|
168 | + { |
|
169 | + $first_char = substr((string)$timezone_offset, 0, 1); |
|
170 | + if ($first_char === '+' || $first_char === '-') { |
|
171 | + $offset_sign = $first_char; |
|
172 | + $offset_secs = substr((string)$timezone_offset, 1); |
|
173 | + } else { |
|
174 | + $offset_sign = '+'; |
|
175 | + $offset_secs = $timezone_offset; |
|
176 | + } |
|
177 | + return array($offset_sign, $offset_secs); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + |
|
182 | + /** |
|
183 | + * Prepares a field's value for display in the API |
|
184 | + * |
|
185 | + * @param \EE_Model_Field_Base $field_obj |
|
186 | + * @param mixed $original_value |
|
187 | + * @param string $requested_version |
|
188 | + * @return mixed |
|
189 | + */ |
|
190 | + public static function prepare_field_value_for_json($field_obj, $original_value, $requested_version) |
|
191 | + { |
|
192 | + if ($original_value === EE_INF) { |
|
193 | + $new_value = Model_Data_Translator::ee_inf_in_rest; |
|
194 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
195 | + if ($original_value instanceof \DateTime) { |
|
196 | + $new_value = $original_value->format('Y-m-d H:i:s'); |
|
197 | + } elseif (is_int($original_value)) { |
|
198 | + $new_value = date('Y-m-d H:i:s', $original_value); |
|
199 | + } else { |
|
200 | + $new_value = $original_value; |
|
201 | + } |
|
202 | + $new_value = mysql_to_rfc3339($new_value); |
|
203 | + } else { |
|
204 | + $new_value = $original_value; |
|
205 | + } |
|
206 | + return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
207 | + $new_value, |
|
208 | + $field_obj, |
|
209 | + $original_value, |
|
210 | + $requested_version |
|
211 | + ); |
|
212 | + } |
|
213 | + |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * Prepares condition-query-parameters (like what's in where and having) from |
|
218 | + * the format expected in the API to use in the models |
|
219 | + * |
|
220 | + * @param array $inputted_query_params_of_this_type |
|
221 | + * @param \EEM_Base $model |
|
222 | + * @param string $requested_version |
|
223 | + * @return array |
|
224 | + * @throws \DomainException |
|
225 | + * @throws \EE_Error |
|
226 | + */ |
|
227 | + public static function prepare_conditions_query_params_for_models( |
|
228 | + $inputted_query_params_of_this_type, |
|
229 | + \EEM_Base $model, |
|
230 | + $requested_version |
|
231 | + ) { |
|
232 | + $query_param_for_models = array(); |
|
233 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
234 | + $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key); |
|
235 | + $field = Model_Data_Translator::deduce_field_from_query_param( |
|
236 | + $query_param_sans_stars, |
|
237 | + $model |
|
238 | + ); |
|
239 | + //double-check is it a *_gmt field? |
|
240 | + if (! $field instanceof \EE_Model_Field_Base |
|
241 | + && Model_Data_Translator::is_gmt_date_field_name($query_param_sans_stars) |
|
242 | + ) { |
|
243 | + //yep, take off '_gmt', and find the field |
|
244 | + $query_param_key = Model_Data_Translator::remove_gmt_from_field_name($query_param_sans_stars); |
|
245 | + $field = Model_Data_Translator::deduce_field_from_query_param( |
|
246 | + $query_param_key, |
|
247 | + $model |
|
248 | + ); |
|
249 | + $timezone = 'UTC'; |
|
250 | + } else { |
|
251 | + //so it's not a GMT field. Set the timezone on the model to the default |
|
252 | + $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
253 | + } |
|
254 | + if ($field instanceof \EE_Model_Field_Base) { |
|
255 | + //did they specify an operator? |
|
256 | + if (is_array($query_param_value)) { |
|
257 | + $op = $query_param_value[0]; |
|
258 | + $translated_value = array($op); |
|
259 | + if (isset($query_param_value[1])) { |
|
260 | + $value = $query_param_value[1]; |
|
261 | + $translated_value[1] = Model_Data_Translator::prepare_field_values_from_json($field, $value, |
|
262 | + $requested_version, $timezone); |
|
263 | + } |
|
264 | + } else { |
|
265 | + $translated_value = Model_Data_Translator::prepare_field_value_from_json($field, $query_param_value, |
|
266 | + $requested_version, $timezone); |
|
267 | + } |
|
268 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
269 | + } else { |
|
270 | + //so it's not for a field, assume it's a logic query param key |
|
271 | + $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_models($query_param_value, |
|
272 | + $model, $requested_version); |
|
273 | + } |
|
274 | + } |
|
275 | + return $query_param_for_models; |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
282 | + * gmt date field name |
|
283 | + * |
|
284 | + * @param string $field_name |
|
285 | + * @return boolean |
|
286 | + */ |
|
287 | + public static function is_gmt_date_field_name($field_name) |
|
288 | + { |
|
289 | + return substr( |
|
290 | + Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name), |
|
291 | + -4, |
|
292 | + 4 |
|
293 | + ) === '_gmt'; |
|
294 | + } |
|
295 | + |
|
296 | + |
|
297 | + |
|
298 | + /** |
|
299 | + * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
300 | + * |
|
301 | + * @param string $field_name |
|
302 | + * @return string |
|
303 | + */ |
|
304 | + public static function remove_gmt_from_field_name($field_name) |
|
305 | + { |
|
306 | + if (! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
307 | + return $field_name; |
|
308 | + } |
|
309 | + $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name); |
|
310 | + $query_param_sans_gmt_and_sans_stars = substr( |
|
311 | + $query_param_sans_stars, |
|
312 | + 0, |
|
313 | + strrpos( |
|
314 | + $field_name, |
|
315 | + '_gmt' |
|
316 | + ) |
|
317 | + ); |
|
318 | + return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
319 | + } |
|
320 | + |
|
321 | + |
|
322 | + |
|
323 | + /** |
|
324 | + * Takes a field name from the REST API and prepares it for the model querying |
|
325 | + * |
|
326 | + * @param string $field_name |
|
327 | + * @return string |
|
328 | + */ |
|
329 | + public static function prepare_field_name_from_json($field_name) |
|
330 | + { |
|
331 | + if (Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
332 | + return Model_Data_Translator::remove_gmt_from_field_name($field_name); |
|
333 | + } |
|
334 | + return $field_name; |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + |
|
339 | + /** |
|
340 | + * Takes array of field names from REST API and prepares for models |
|
341 | + * |
|
342 | + * @param array $field_names |
|
343 | + * @return array of field names (possibly include model prefixes) |
|
344 | + */ |
|
345 | + public static function prepare_field_names_from_json(array $field_names) |
|
346 | + { |
|
347 | + $new_array = array(); |
|
348 | + foreach ($field_names as $key => $field_name) { |
|
349 | + $new_array[$key] = Model_Data_Translator::prepare_field_name_from_json($field_name); |
|
350 | + } |
|
351 | + return $new_array; |
|
352 | + } |
|
353 | + |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * Takes array where array keys are field names (possibly with model path prefixes) |
|
358 | + * from the REST API and prepares them for model querying |
|
359 | + * |
|
360 | + * @param array $field_names_as_keys |
|
361 | + * @return array |
|
362 | + */ |
|
363 | + public static function prepare_field_names_in_array_keys_from_json(array $field_names_as_keys) |
|
364 | + { |
|
365 | + $new_array = array(); |
|
366 | + foreach ($field_names_as_keys as $field_name => $value) { |
|
367 | + $new_array[Model_Data_Translator::prepare_field_name_from_json($field_name)] = $value; |
|
368 | + } |
|
369 | + return $new_array; |
|
370 | + } |
|
371 | + |
|
372 | + |
|
373 | + |
|
374 | + /** |
|
375 | + * Prepares an array of model query params for use in the REST API |
|
376 | + * |
|
377 | + * @param array $model_query_params |
|
378 | + * @param \EEM_Base $model |
|
379 | + * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
380 | + * REST API |
|
381 | + * @return array which can be passed into the EE4 REST API when querying a model resource |
|
382 | + * @throws \EE_Error |
|
383 | + */ |
|
384 | + public static function prepare_query_params_for_rest_api( |
|
385 | + array $model_query_params, |
|
386 | + \EEM_Base $model, |
|
387 | + $requested_version = null |
|
388 | + ) { |
|
389 | + if ($requested_version === null) { |
|
390 | + $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
391 | + } |
|
392 | + $rest_query_params = $model_query_params; |
|
393 | + if (isset($model_query_params[0])) { |
|
394 | + $rest_query_params['where'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
395 | + $model_query_params[0], |
|
396 | + $model, |
|
397 | + $requested_version |
|
398 | + ); |
|
399 | + unset($rest_query_params[0]); |
|
400 | + } |
|
401 | + if (isset($model_query_params['having'])) { |
|
402 | + $rest_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
403 | + $model_query_params['having'], |
|
404 | + $model, |
|
405 | + $requested_version |
|
406 | + ); |
|
407 | + } |
|
408 | + return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
409 | + $rest_query_params, $model_query_params, $model, $requested_version); |
|
410 | + } |
|
411 | + |
|
412 | + |
|
413 | + |
|
414 | + /** |
|
415 | + * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
416 | + * |
|
417 | + * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
418 | + * passed into EEM_Base::get_all() |
|
419 | + * @param \EEM_Base $model |
|
420 | + * @param string $requested_version eg "4.8.36" |
|
421 | + * @return array ready for use in the rest api query params |
|
422 | + * @throws \EE_Error |
|
423 | + */ |
|
424 | + public static function prepare_conditions_query_params_for_rest_api( |
|
425 | + $inputted_query_params_of_this_type, |
|
426 | + \EEM_Base $model, |
|
427 | + $requested_version |
|
428 | + ) { |
|
429 | + $query_param_for_models = array(); |
|
430 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
431 | + $field = Model_Data_Translator::deduce_field_from_query_param( |
|
432 | + Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key), |
|
433 | + $model |
|
434 | + ); |
|
435 | + if ($field instanceof \EE_Model_Field_Base) { |
|
436 | + //did they specify an operator? |
|
437 | + if (is_array($query_param_value)) { |
|
438 | + $op = $query_param_value[0]; |
|
439 | + $translated_value = array($op); |
|
440 | + if (isset($query_param_value[1])) { |
|
441 | + $value = $query_param_value[1]; |
|
442 | + $translated_value[1] = Model_Data_Translator::prepare_field_values_for_json($field, $value, |
|
443 | + $requested_version); |
|
444 | + } |
|
445 | + } else { |
|
446 | + $translated_value = Model_Data_Translator::prepare_field_value_for_json($field, $query_param_value, |
|
447 | + $requested_version); |
|
448 | + } |
|
449 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
450 | + } else { |
|
451 | + //so it's not for a field, assume it's a logic query param key |
|
452 | + $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api($query_param_value, |
|
453 | + $model, $requested_version); |
|
454 | + } |
|
455 | + } |
|
456 | + return $query_param_for_models; |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + |
|
461 | + /** |
|
462 | + * @param $condition_query_param_key |
|
463 | + * @return string |
|
464 | + */ |
|
465 | + public static function remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
466 | + { |
|
467 | + $pos_of_star = strpos($condition_query_param_key, '*'); |
|
468 | + if ($pos_of_star === false) { |
|
469 | + return $condition_query_param_key; |
|
470 | + } else { |
|
471 | + $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
472 | + return $condition_query_param_sans_star; |
|
473 | + } |
|
474 | + } |
|
475 | + |
|
476 | + |
|
477 | + |
|
478 | + /** |
|
479 | + * Takes the input parameter and finds the model field that it indicates. |
|
480 | + * |
|
481 | + * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
482 | + * @param \EEM_Base $model |
|
483 | + * @return \EE_Model_Field_Base |
|
484 | + * @throws \EE_Error |
|
485 | + */ |
|
486 | + public static function deduce_field_from_query_param($query_param_name, \EEM_Base $model) |
|
487 | + { |
|
488 | + //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
489 | + //which will help us find the database table and column |
|
490 | + $query_param_parts = explode('.', $query_param_name); |
|
491 | + if (empty($query_param_parts)) { |
|
492 | + throw new \EE_Error(sprintf(__('_extract_column_name is empty when trying to extract column and table name from %s', |
|
493 | + 'event_espresso'), $query_param_name)); |
|
494 | + } |
|
495 | + $number_of_parts = count($query_param_parts); |
|
496 | + $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
497 | + if ($number_of_parts === 1) { |
|
498 | + $field_name = $last_query_param_part; |
|
499 | + } else {// $number_of_parts >= 2 |
|
500 | + //the last part is the column name, and there are only 2parts. therefore... |
|
501 | + $field_name = $last_query_param_part; |
|
502 | + $model = \EE_Registry::instance()->load_model($query_param_parts[$number_of_parts - 2]); |
|
503 | + } |
|
504 | + try { |
|
505 | + return $model->field_settings_for($field_name); |
|
506 | + } catch (\EE_Error $e) { |
|
507 | + return null; |
|
508 | + } |
|
509 | + } |
|
510 | 510 | |
511 | 511 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace EventEspresso\core\libraries\rest_api; |
3 | 3 | |
4 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
4 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
5 | 5 | exit('No direct script access allowed'); |
6 | 6 | } |
7 | 7 | |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | '0', |
150 | 150 | STR_PAD_LEFT |
151 | 151 | ); |
152 | - $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
152 | + $new_value = rest_parse_date($original_value.$offset_sign.$offset_string); |
|
153 | 153 | } else { |
154 | 154 | $new_value = $original_value; |
155 | 155 | } |
@@ -166,10 +166,10 @@ discard block |
||
166 | 166 | */ |
167 | 167 | private static function parse_timezone_offset($timezone_offset) |
168 | 168 | { |
169 | - $first_char = substr((string)$timezone_offset, 0, 1); |
|
169 | + $first_char = substr((string) $timezone_offset, 0, 1); |
|
170 | 170 | if ($first_char === '+' || $first_char === '-') { |
171 | 171 | $offset_sign = $first_char; |
172 | - $offset_secs = substr((string)$timezone_offset, 1); |
|
172 | + $offset_secs = substr((string) $timezone_offset, 1); |
|
173 | 173 | } else { |
174 | 174 | $offset_sign = '+'; |
175 | 175 | $offset_secs = $timezone_offset; |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | $model |
238 | 238 | ); |
239 | 239 | //double-check is it a *_gmt field? |
240 | - if (! $field instanceof \EE_Model_Field_Base |
|
240 | + if ( ! $field instanceof \EE_Model_Field_Base |
|
241 | 241 | && Model_Data_Translator::is_gmt_date_field_name($query_param_sans_stars) |
242 | 242 | ) { |
243 | 243 | //yep, take off '_gmt', and find the field |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | */ |
304 | 304 | public static function remove_gmt_from_field_name($field_name) |
305 | 305 | { |
306 | - if (! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
306 | + if ( ! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
307 | 307 | return $field_name; |
308 | 308 | } |
309 | 309 | $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name); |