PluginProbe
Order Tracking – WordPress Status Tracking Plugin / 3.2.2
Order Tracking – WordPress Status Tracking Plugin v3.2.2
3.5.4 3.5.3 3.5.2 3.5.1 3.5.0 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.3.0 3.3.1 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.15 All 235 releases
order-tracking / includes / OrderManager.class.php

OrderManager.class.php in Order Tracking – WordPress Status Tracking Plugin 3.2.2, at includes/OrderManager.class.php

741 lines 21.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to handle all order database interactions for the Order Tracking plugin
4 */
5
6 if ( !defined( 'ABSPATH' ) )
7 exit;
8
9 if ( !class_exists( 'ewdotpOrderManager' ) ) {
10 class ewdotpOrderManager {
11
12 // The name of the orders table, set in the constructor
13 public $orders_table_name;
14
15 // The name of the orders statuses table, set in the constructor
16 public $order_statuses_table_name;
17
18 // The name of the meta table, set in the constructor
19 public $meta_table_name;
20
21 // Array containing the arguments for the query
22 public $args = array();
23
24 // Array containing retrieved order objects
25 public $orders = array();
26
27 public function __construct() {
28 global $wpdb;
29
30 $this->orders_table_name = $wpdb->prefix . "EWD_OTP_Orders";
31 $this->order_statuses_table_name = $wpdb->prefix . "EWD_OTP_Order_Statuses";
32 $this->meta_table_name = $wpdb->prefix . "EWD_OTP_Fields_Meta";
33
34 if ( get_transient( 'ewd-otp-update-tables' ) ) {
35
36 add_action( 'plugins_loaded', array( $this, 'create_tables' ) );
37 }
38 }
39
40 /**
41 * Creates the tables used to store orders and their meta information
42 * @since 3.0.0
43 */
44 public function create_tables() {
45
46 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
47
48 $sql = "CREATE TABLE $this->orders_table_name (
49 Order_ID mediumint(9) NOT NULL AUTO_INCREMENT,
50 Order_Name text DEFAULT '' NOT NULL,
51 Order_Number text DEFAULT '' NOT NULL,
52 Order_Status text DEFAULT '' NOT NULL,
53 Order_External_Status text DEFAULT '' NOT NULL,
54 Order_Location text DEFAULT '' NOT NULL,
55 Order_Notes_Public text DEFAULT '' NOT NULL,
56 Order_Notes_Private text DEFAULT '' NOT NULL,
57 Order_Customer_Notes text DEFAULT '' NOT NULL,
58 Order_Email text DEFAULT '' NOT NULL,
59 Sales_Rep_ID mediumint(9) DEFAULT 0 NOT NULL,
60 Customer_ID mediumint(9) DEFAULT 0 NOT NULL,
61 WooCommerce_ID mediumint(9) DEFAULT 0 NOT NULL,
62 Zendesk_ID mediumint(9) DEFAULT 0 NOT NULL,
63 Order_Status_Updated datetime DEFAULT '0000-00-00 00:00:00' NULL,
64 Order_Display text DEFAULT '' NOT NULL,
65 Order_Payment_Price text DEFAULT '' NOT NULL,
66 Order_Payment_Completed text DEFAULT '' NOT NULL,
67 Order_PayPal_Receipt_Number text DEFAULT '' NOT NULL,
68 Order_View_Count mediumint(9) DEFAULT 0 NOT NULL,
69 Order_Tracking_Link_Clicked text DEFAULT '' NOT NULL,
70 Order_Tracking_Link_Code text DEFAULT '' NOT NULL,
71 UNIQUE KEY id (Order_ID)
72 )
73 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
74
75 dbDelta($sql);
76
77
78 $sql = "CREATE TABLE $this->order_statuses_table_name (
79 Order_Status_ID mediumint(9) NOT NULL AUTO_INCREMENT,
80 Order_ID mediumint(9) DEFAULT 0 NOT NULL,
81 Order_Status text DEFAULT '' NOT NULL,
82 Order_Location text DEFAULT '' NOT NULL,
83 Order_Internal_Status text DEFAULT '' NOT NULL,
84 Order_Status_Created datetime DEFAULT '0000-00-00 00:00:00' NULL,
85 UNIQUE KEY id (Order_Status_ID)
86 )
87 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
88
89 dbDelta($sql);
90
91 $sql = "CREATE TABLE $this->meta_table_name (
92 Meta_ID mediumint(9) NOT NULL AUTO_INCREMENT,
93 Field_ID mediumint(9) DEFAULT '0',
94 Order_ID mediumint(9) DEFAULT '0',
95 Customer_ID mediumint(9) DEFAULT '0',
96 Sales_Rep_ID mediumint(9) DEFAULT '0',
97 Meta_Value text DEFAULT '' NOT NULL,
98 UNIQUE KEY id (Meta_ID)
99 )
100 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
101
102 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
103
104 dbDelta($sql);
105 }
106
107 /**
108 * Returns a single order given its order ID
109 * @since 3.0.0
110 */
111 public function get_order_from_id( $order_id ) {
112 global $wpdb;
113
114 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE Order_ID=%d", $order_id ) );
115
116 return $db_order;
117 }
118
119 /**
120 * Returns a single order given its Zendesk ID
121 * @since 3.0.0
122 */
123 public function get_order_from_zendesk_id( $zendesk_id ) {
124 global $wpdb;
125
126 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE Zendesk_ID=%d", $zendesk_id ) );
127
128 return ! empty( $db_order ) ? $db_order : null;
129 }
130
131 /**
132 * Returns a single order given its WooCommerce ID
133 * @since 3.0.0
134 */
135 public function get_order_from_woocommerce_id( $post_id ) {
136 global $wpdb;
137
138 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE WooCommerce_ID=%d", $post_id ) );
139
140 return ! empty( $db_order ) ? $db_order : null;
141 }
142
143 /**
144 * Returns a single order given its order number
145 * @since 3.0.0
146 */
147 public function get_order_from_tracking_number( $order_number ) {
148 global $wpdb;
149
150 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE Order_Number=%s", $order_number ) );
151
152 return ! empty( $db_order ) ? $db_order : null;
153 }
154
155 /**
156 * Returns orders matching the arguments supplied
157 * @since 3.0.0
158 */
159 public function get_matching_orders( $args ) {
160
161 $this->orders = array();
162
163 $defaults = array(
164 'orders_per_page' => 20,
165 'order' => 'ASC',
166 'paged' => 1,
167 'date_range' => '',
168 );
169
170 $this->args = wp_parse_args( $args, $defaults );
171
172 $this->prepare_args();
173
174 $this->run_query();
175
176 return $this->orders;
177 }
178
179 /**
180 * Return the counts for orders being displayed on the admin orders page
181 * @since 3.0.0
182 */
183 public function get_order_counts( $args ) {
184 global $wpdb;
185 global $ewd_otp_controller;
186
187 $this->args = $args;
188
189 $this->prepare_args();
190
191 $args = $this->args;
192
193 $query_string = "SELECT Order_Status, count( * ) AS num_orders
194 FROM $this->orders_table_name
195 WHERE 1=%d
196 ";
197
198 $query_args = array(1);
199
200 if ( ! empty( $args['number'] ) ) {
201
202 $query_string .= " AND Order_Number=%s";
203 $query_args[] = sanitize_text_field( $args['number'] );
204 }
205
206 if ( ! empty( $args['display'] ) ) {
207
208 $query_string .= " AND Order_Display=%s";
209 $query_args[] = 'Yes';
210 }
211
212 if ( ! empty( $args['after'] ) ) {
213
214 $query_string .= " AND Order_Status_Updated>=%s";
215 $query_args[] = $args['after'];
216 }
217
218 if ( ! empty( $args['before'] ) ) {
219
220 $query_string .= " AND Order_Status_Updated<=%s";
221 $query_args[] = $args['before'];
222 }
223
224 if ( ! empty( $args['date'] ) ) {
225
226 $query_string .= " AND DATE(Order_Status_Updated)=%s";
227 $query_args[] = $args['date'];
228 }
229
230 $query_string .= " GROUP BY Order_Status";
231
232 $count_results = $wpdb->get_results( $wpdb->prepare( $query_string, $query_args ) );
233
234 $statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) );
235
236 $counts = array();
237
238 foreach( $statuses as $status ) {
239
240 $counts[ sanitize_title( $status->status, '', 'ewd_otp' ) ] = 0;
241 }
242
243 foreach ( $count_results as $count ) {
244
245 $counts[ sanitize_title( $count->Order_Status, '', 'ewd_otp' ) ] = $count->num_orders;
246 }
247
248 $counts['total'] = array_sum( $counts );
249
250 return $counts;
251 }
252
253 /**
254 * Prepares the arguments before the query is run
255 * @since 3.0.0
256 */
257 public function prepare_args() {
258
259 $args = $this->args;
260
261 if ( is_string( $args['date_range'] ) ) {
262
263 if ( !empty( $args['start_date'] ) || !empty( $args['end_date'] ) ) {
264
265 if ( !empty( $args['start_date'] ) ) {
266 $args['after'] = sanitize_text_field( $args['start_date'] ) . ( ( isset( $args['start_time'] ) and $args['start_time'] ) ? $args['start_time'] : '' );
267 }
268
269 if ( !empty( $args['end_date'] ) ) {
270 $args['before'] = sanitize_text_field( $args['end_date'] ) . ( ( isset( $args['end_time'] ) and $args['end_time'] ) ? $args['end_time'] : ' 23:59' );
271 }
272 } elseif ( $args['date_range'] === 'today' ) {
273
274 $args['after'] = date( 'Y-m-d H:i:s', strtotime( 'today midnight' ) );
275 $args['before'] = date( 'Y-m-d H:i:s', strtotime( 'tomorrow midnight' ) );
276
277 } elseif ( $args['date_range'] === 'week' ) {
278
279 $args['after'] = date( 'Y-m-d H:i:s', strtotime( 'monday this week' ) );
280 $args['before'] = date( 'Y-m-d H:i:s', strtotime( 'sunday this week' ) );
281 } elseif ( $args['date_range'] === 'past' ) {
282
283 $args['before'] = date( 'Y-m-d H:i:s', strtotime( 'now' ) );
284 }
285 }
286
287 $this->args = $args;
288
289 return $this->args;
290 }
291
292 /**
293 * Create and run the SQL query based on the arguments received
294 * @since 3.0.0
295 */
296 public function run_query() {
297 global $wpdb;
298
299 $args = $this->args;
300
301 $query_string = "SELECT * FROM $this->orders_table_name WHERE 1=%d";
302
303 $query_args = array( 1 );
304
305 if ( ! empty( $args['id'] ) ) {
306
307 $query_string .= " AND Order_ID=%d";
308 $query_args[] = intval( $args['id'] );
309 }
310
311 if ( ! empty( $args['name'] ) ) {
312
313 $query_string .= " AND Order_Name=%s";
314 $query_args[] = $args['name'];
315 }
316
317 if ( ! empty( $args['number'] ) ) {
318
319 $query_string .= " AND Order_Number=%s";
320 $query_args[] = $args['number'];
321 }
322
323 if ( ! empty( $args['location'] ) ) {
324
325 $query_string .= " AND Order_Location=%s";
326 $query_args[] = $args['location'];
327 }
328
329 if ( ! empty( $args['status'] ) ) {
330
331 if( is_array( $args['status'] ) ) {
332 $stts_plchldr = implode( ', ', array_fill( 0, count( $args['status'] ), '%s' ) );
333 $query_string .= " AND Order_Status IN ( $stts_plchldr )";
334 $query_args = array_merge( $query_args, $args['status'] );
335 }
336 else {
337 $query_string .= " AND Order_Status=%s";
338 $query_args[] = $args['status'];
339 }
340 }
341
342 if ( ! empty( $args['email'] ) ) {
343
344 $query_string .= " AND Order_Email=%s";
345 $query_args[] = $args['email'];
346 }
347
348 if ( ! empty( $args['customer'] ) ) {
349
350 if( is_array( $args['customer'] ) ) {
351 $cstm_plchldr = implode( ', ', array_fill( 0, count( $args['customer'] ), '%d' ) );
352 $query_string .= " AND Customer_ID IN ( $cstm_plchldr )";
353 $query_args = array_merge( $query_args, $args['customer'] );
354 }
355 else {
356 $query_string .= " AND Customer_ID=%d";
357 $query_args[] = intval( $args['customer'] );
358 }
359 }
360
361 if ( ! empty( $args['sales_rep'] ) ) {
362
363 if( is_array( $args['sales_rep'] ) ) {
364 $sls_rp_plchldr = implode( ', ', array_fill( 0, count( $args['sales_rep'] ), '%d' ) );
365 $query_string .= " AND Sales_Rep_ID IN ( $sls_rp_plchldr )";
366 $query_args = array_merge( $query_args, $args['sales_rep'] );
367 }
368 else {
369 $query_string .= " AND Sales_Rep_ID=%d";
370 $query_args[] = intval( $args['sales_rep'] );
371 }
372 }
373
374 if ( ! empty( $args['display'] ) ) {
375
376 if ( strtolower( $args['display'] ) == 'no' ){
377
378 $query_string .= " AND Order_Display=%s";
379 $query_args[] = 'No';
380 }
381 else {
382
383 $query_string .= " AND Order_Display=%s";
384 $query_args[] = 'Yes';
385 }
386 }
387
388 if ( ! empty( $args['payment_completed'] ) ) {
389
390 $query_string .= " AND Order_Payment_Completed=%s";
391 $query_args[] = $args['payment_completed'];
392 }
393
394 if ( ! empty( $args['after'] ) ) {
395
396 $query_string .= " AND Order_Status_Updated>=%s";
397 $query_args[] = $args['after'];
398 }
399
400 if ( ! empty( $args['before'] ) ) {
401
402 $query_string .= " AND Order_Status_Updated<=%s";
403 $query_args[] = $args['before'];
404 }
405
406 if ( ! empty( $args['date'] ) ) {
407
408 $query_string .= " AND DATE(Order_Status_Updated)=%s";
409 $query_args[] = $args['date'];
410 }
411
412 if ( ! empty( $args['orderby'] ) ) {
413
414 $query_string .= ' ORDER BY ' . esc_sql( $args['orderby'] ) . ' ' . ( strtolower( $args['order'] ) == 'desc' ? 'DESC' : 'ASC' );
415 }
416
417 if ( $args['orders_per_page'] > 0 ) {
418
419 $query_string .= ' LIMIT ' . intval( ( $args['paged'] - 1 ) * $args['orders_per_page'] ) . ', ' . intval( $args['orders_per_page'] );
420 }
421
422 $db_orders = $wpdb->get_results( $wpdb->prepare( $query_string, $query_args ) );
423
424 foreach ( $db_orders as $db_order ) {
425
426 $order = new ewdotpOrder();
427
428 $order->load_order( $db_order );
429
430 $this->orders[] = $order;
431 }
432 }
433
434 /**
435 * Returns the value for a given field/order id pair
436 * @since 3.0.0
437 */
438 public function get_order_status_history( $order_id ) {
439 global $wpdb;
440
441 return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $this->order_statuses_table_name WHERE Order_ID=%d ORDER BY Order_Status_Created", $order_id ) );
442 }
443
444 /**
445 * Returns the value for a given field/order id pair
446 * @since 3.0.0
447 */
448 public function get_order_field( $field, $order_id ) {
449 global $wpdb;
450
451 $db_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->orders_table_name WHERE Order_ID=%d", $order_id ) );
452
453 $order = new ewdotpOrder();
454 $order->load_order( $db_order );
455
456 return ! empty( $order->$field ) ? $order->$field : '';
457 }
458
459 /**
460 * Returns the value for a given custom_field/order pair
461 * @since 3.0.0
462 */
463 public function get_field_value( $custom_field_id, $order_id ) {
464 global $wpdb;
465
466 return $wpdb->get_var( $wpdb->prepare( "SELECT Meta_Value FROM $this->meta_table_name WHERE Field_ID=%d AND Order_ID=%d ORDER BY Meta_ID DESC", $custom_field_id, $order_id ) );
467 }
468
469 /**
470 * Accepts an order object, inserts it into the database, and returns the ID of the newly inserted order
471 * @since 3.0.0
472 */
473 public function insert_order( $order ) {
474 global $wpdb;
475 global $ewd_otp_controller;
476
477 $query_args = array(
478 'Order_Name' => ! empty( $order->name ) ? $order->name : '',
479 'Order_Number' => ! empty( $order->number ) ? $order->number : '',
480 'Order_Status' => ! empty( $order->status ) ? $order->status : '',
481 'Order_External_Status' => ! empty( $order->external_status ) ? $order->external_status : '',
482 'Order_Location' => ! empty( $order->location ) ? $order->location : '',
483 'Order_Notes_Public' => ! empty( $order->notes_public ) ? $order->notes_public : '',
484 'Order_Notes_Private' => ! empty( $order->notes_private ) ? $order->notes_private : '',
485 'Order_Customer_Notes' => ! empty( $order->customer_notes ) ? $order->customer_notes : '',
486 'Order_Email' => ! empty( $order->email ) ? $order->email : '',
487 'Customer_ID' => ! empty( $order->customer ) ? $order->customer : 0,
488 'Sales_Rep_ID' => ! empty( $order->sales_rep ) ? $order->sales_rep : 0,
489 'WooCommerce_ID' => ! empty( $order->woocommerce_id ) ? $order->woocommerce_id : 0,
490 'Zendesk_ID' => ! empty( $order->zendesk_id ) ? $order->zendesk_id : 0,
491 'Order_Status_Updated' => ! empty( $order->status_updated ) ? $order->status_updated : date( 'Y-m-d H:i:s' ),
492 'Order_Display' => ! empty( $order->display ) ? 'Yes' : 'No',
493 'Order_Payment_Price' => ! empty( $order->payment_price ) ? $order->payment_price : '',
494 'Order_Payment_Completed' => ! empty( $order->payment_completed ) ? $order->payment_completed : '',
495 'Order_PayPal_Receipt_Number' => ! empty( $order->paypal_receipt_number ) ? $order->paypal_receipt_number : '',
496 'Order_View_Count' => ! empty( $order->views ) ? $order->views : 0,
497 'Order_Tracking_Link_Clicked' => ! empty( $order->tracking_link_clicked ) ? 'Yes' : 'No',
498 'Order_Tracking_Link_Code' => ! empty( $order->tracking_link_code ) ? $order->tracking_link_code : '',
499 );
500
501 $wpdb->insert(
502 $this->orders_table_name,
503 $query_args
504 );
505
506 $order_id = $wpdb->insert_id;
507
508 if ( ! $order_id ) { return $order_id; }
509
510 $custom_fields = $ewd_otp_controller->settings->get_order_custom_fields();
511
512 foreach ( $custom_fields as $custom_field ) {
513
514 if ( empty( $order->custom_fields[ $custom_field->id ] ) ) { continue; }
515
516 $query_args = array(
517 'Field_ID' => $custom_field->id,
518 'Order_ID' => $order_id,
519 'Meta_Value' => $order->custom_fields[ $custom_field->id ]
520 );
521
522 $wpdb->insert(
523 $this->meta_table_name,
524 $query_args
525 );
526 }
527
528 return $order_id;
529 }
530
531 /**
532 * Accepts an order object, updates it in the database, and returns the ID if successful or false otherwise
533 * @since 3.0.0
534 */
535 public function update_order( $order ) {
536 global $wpdb;
537 global $ewd_otp_controller;
538
539 if ( empty( $order->id ) ) { return false; }
540
541 $query_args = array(
542 'Order_Name' => ! empty( $order->name ) ? $order->name : '',
543 'Order_Number' => ! empty( $order->number ) ? $order->number : '',
544 'Order_Status' => ! empty( $order->status ) ? $order->status : '',
545 'Order_External_Status' => ! empty( $order->external_status ) ? $order->external_status : '',
546 'Order_Location' => ! empty( $order->location ) ? $order->location : '',
547 'Order_Notes_Public' => ! empty( $order->notes_public ) ? $order->notes_public : '',
548 'Order_Notes_Private' => ! empty( $order->notes_private ) ? $order->notes_private : '',
549 'Order_Customer_Notes' => ! empty( $order->customer_notes ) ? $order->customer_notes : '',
550 'Order_Email' => ! empty( $order->email ) ? $order->email : '',
551 'Customer_ID' => ! empty( $order->customer ) ? $order->customer : 0,
552 'Sales_Rep_ID' => ! empty( $order->sales_rep ) ? $order->sales_rep : 0,
553 'WooCommerce_ID' => ! empty( $order->woocommerce_id ) ? $order->woocommerce_id : 0,
554 'Zendesk_ID' => ! empty( $order->zendesk_id ) ? $order->zendesk_id : 0,
555 'Order_Status_Updated' => ! empty( $order->status_updated ) ? $order->status_updated : date( 'Y-m-d H:i:s' ),
556 'Order_Display' => ! empty( $order->display ) ? 'Yes' : 'No',
557 'Order_Payment_Price' => ! empty( $order->payment_price ) ? $order->payment_price : '',
558 'Order_Payment_Completed' => ! empty( $order->payment_completed ) ? 'Yes' : 'No',
559 'Order_PayPal_Receipt_Number' => ! empty( $order->paypal_receipt_number ) ? $order->paypal_receipt_number : '',
560 'Order_View_Count' => ! empty( $order->views ) ? $order->views : 0,
561 'Order_Tracking_Link_Clicked' => ! empty( $order->tracking_link_clicked ) ? 'Yes' : 'No',
562 'Order_Tracking_Link_Code' => ! empty( $order->tracking_link_code ) ? $order->tracking_link_code : '',
563 );
564
565 $wpdb->update(
566 $this->orders_table_name,
567 $query_args,
568 array( 'Order_ID' => $order->id )
569 );
570
571 $custom_fields = $ewd_otp_controller->settings->get_order_custom_fields();
572
573 foreach ( $custom_fields as $custom_field ) {
574
575 $wpdb->get_var( $wpdb->prepare( "SELECT Meta_Value from $this->meta_table_name WHERE Field_ID=%d AND Order_ID=%d ORDER BY Meta_ID DESC", $custom_field->id, $order->id ) );
576
577 $update = $wpdb->num_rows ? true : false;
578
579 if ( empty( $order->custom_fields[ $custom_field->id ] ) ) {
580
581 $where_args = array(
582 'Field_ID' => $custom_field->id,
583 'Order_ID' => $order->id,
584 );
585
586 $wpdb->delete(
587 $this->meta_table_name,
588 $where_args
589 );
590 }
591 elseif ( $update ) {
592
593 $query_args = array(
594 'Meta_Value' => $order->custom_fields[ $custom_field->id ]
595 );
596
597 $where_args = array(
598 'Field_ID' => $custom_field->id,
599 'Order_ID' => $order->id,
600 );
601
602 $wpdb->update(
603 $this->meta_table_name,
604 $query_args,
605 $where_args
606 );
607 }
608 else {
609
610 $query_args = array(
611 'Meta_Value' => $order->custom_fields[ $custom_field->id ],
612 'Field_ID' => $custom_field->id,
613 'Order_ID' => $order->id,
614 );
615
616 $wpdb->insert(
617 $this->meta_table_name,
618 $query_args
619 );
620 }
621 }
622
623 return $order->id;
624 }
625
626 /**
627 * Adds a new order status to the database
628 * @since 3.0.0
629 */
630 public function update_order_status( $order ) {
631 global $wpdb;
632
633 $query_args = array(
634 'Order_ID' => $order->id,
635 'Order_Status' => ! empty( $order->status ) ? $order->status : '',
636 'Order_Location' => ! empty( $order->location ) ? $order->location : '',
637 'Order_Internal_Status' => ! empty( $order->status ) ? $order->status : '',
638 'Order_Status_Created' => ! empty( $order->status_updated ) ? $order->status_updated : date( 'Y-m-d H:i:s' ),
639 );
640
641 $wpdb->insert(
642 $this->order_statuses_table_name,
643 $query_args
644 );
645 }
646
647 /**
648 * Removs an existing order status
649 * @since 3.0.1
650 */
651 public function delete_order_status( $order_status_id ) {
652 global $wpdb;
653
654 $wpdb->delete(
655 $this->order_statuses_table_name,
656 array( 'Order_Status_ID' => $order_status_id)
657 );
658 }
659
660 /**
661 * Accepts an order id, deletes the corresponding order
662 * @since 3.0.0
663 */
664 public function delete_order( $order_id ) {
665 global $wpdb;
666
667 $wpdb->delete(
668 $this->orders_table_name,
669 array( 'Order_ID' => $order_id )
670 );
671
672 $wpdb->delete(
673 $this->order_statuses_table_name,
674 array( 'Order_ID' => $order_id )
675 );
676
677 $wpdb->delete(
678 $this->meta_table_name,
679 array( 'Order_ID' => $order_id )
680 );
681 }
682
683 /**
684 * Accepts an order id, sets the payment_received field for the corresponding order to Yes
685 * @since 3.0.0
686 */
687 public function set_order_paid( $order_id ) {
688 global $wpdb;
689
690 $query_args = array(
691 'Order_Payment_Completed' => 'Yes'
692 );
693
694 $wpdb->update(
695 $this->orders_table_name,
696 $query_args,
697 array( 'Order_ID' => $order_id )
698 );
699 }
700
701 /**
702 * Accepts an order id, updates the status of the corresponding order
703 * @since 3.0.0
704 */
705 public function set_order_status( $order_id, $status ) {
706 global $wpdb;
707
708 $order = new ewdotpOrder();
709 $order->load_order_from_id( $order_id );
710
711 $order->set_status( $status );
712 }
713
714 /**
715 * Increment the views for a given order_id
716 * @since 3.0.4
717 */
718 public function increase_order_views( $order_id ) {
719 global $wpdb;
720
721 $wpdb->query( $wpdb->prepare( "UPDATE $this->orders_table_name SET Order_View_Count=Order_View_Count+1 WHERE Order_ID=%d", $order_id ) );
722 }
723
724 /**
725 * Accepts an email and order id, and verify that they match the saved data,
726 * @since 3.0.0
727 */
728 public function verify_order_email( $email, $order_id ) {
729 global $wpdb;
730
731 $order_email = $wpdb->get_var( $wpdb->prepare( "SELECT Order_Email FROM $this->orders_table_name WHERE Order_ID=%d", $order_id ) );
732
733 if ( $order_email == $email ) {
734
735 return true;
736 }
737
738 return false;
739 }
740 }
741 }