PluginProbe
Tutor LMS – eLearning and online course solution / 3.4.2
Tutor LMS – eLearning and online course solution v3.4.2
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
← All changes | models/OrderModel.php +154 -611 trunk3.4.2 View file →
@@ -9,24 +9,14 @@
9 9 */
10 10
11 11 namespace Tutor\Models;
12 12
13 -use DateTime;
14 13 use Exception;
15 -use Tutor\Cache\TutorCache;
16 -use Tutor\Components\Badge;
17 -use Tutor\Components\Button;
18 -use Tutor\Components\Constants\Variant;
19 -use TUTOR\Earnings;
20 -use Tutor\Ecommerce\BillingController;
21 -use Tutor\Ecommerce\CheckoutController;
22 14 use Tutor\Ecommerce\Ecommerce;
15 +use Tutor\Helpers\QueryHelper;
16 +use Tutor\Helpers\DateTimeHelper;
23 17 use Tutor\Ecommerce\OrderActivitiesController;
24 18 use Tutor\Ecommerce\Tax;
25 -use Tutor\Helpers\DateTimeHelper;
26 -use Tutor\Helpers\QueryHelper;
27 -use TUTOR\User;
28 -use TutorPro\Ecommerce\Invoice;
29 19
30 20 /**
31 21 * OrderModel Class
32 22 *
@@ -44,9 +34,8 @@
44 34 const ORDER_INCOMPLETE = 'incomplete';
45 35 const ORDER_COMPLETED = 'completed';
46 36 const ORDER_CANCELLED = 'cancelled';
47 37 const ORDER_TRASH = 'trash';
48 - const ORDER_PENDING = 'pending';
49 38
50 39 /**
51 40 * Payment status
52 41 *
@@ -58,21 +47,11 @@
58 47 const PAYMENT_FAILED = 'failed';
59 48 const PAYMENT_UNPAID = 'unpaid';
60 49 const PAYMENT_REFUNDED = 'refunded';
61 50 const PAYMENT_PARTIALLY_REFUNDED = 'partially-refunded';
62 - const PAYMENT_PENDING = 'pending';
51 + const PAYMENT_MANUAL = 'manual';
63 52
64 53 /**
65 - * Payment methods
66 - *
67 - * @since 3.5.0
68 - *
69 - * @var string
70 - */
71 - const PAYMENT_METHOD_MANUAL = 'manual';
72 - const PAYMENT_METHOD_FREE = 'free';
73 -
74 - /**
75 54 * Order Meta keys for history & refunds
76 55 *
77 56 * @since 3.0.0
78 57 *
@@ -77,27 +56,13 @@
77 56 * @since 3.0.0
78 57 *
79 58 * @var string
80 59 */
81 - const META_KEY_HISTORY = 'history';
82 - const META_KEY_REFUND = 'refund';
83 - const META_KEY_ORDER_ID = 'tutor_order_id_';
84 - const META_KEY_BILLING_ADDRESS = 'billing_address';
60 + const META_KEY_HISTORY = 'history';
61 + const META_KEY_REFUND = 'refund';
62 + const META_KEY_ORDER_ID = 'tutor_order_id_';
85 63
86 64 /**
87 - * Order meta for subscription order.
88 - *
89 - * @since 3.4.0
90 - *
91 - * @var string
92 - */
93 - const META_ENROLLMENT_FEE = 'plan_enrollment_fee';
94 - const META_TRIAL_FEE = 'plan_trial_fee';
95 - const META_PLAN_INFO = 'plan_info';
96 - const META_IS_PLAN_TRIAL_ORDER = 'is_plan_trial_order';
97 - const META_IS_RESUBSCRIPTION_ORDER = 'is_resubscription_order';
98 -
99 - /**
100 65 * Tax type constants
101 66 *
102 67 * @since 3.0.0
103 68 *
@@ -196,25 +161,11 @@
196 161 return $this->table_name;
197 162 }
198 163
199 164 /**
200 - * Get a order record.
201 - *
202 - * @since 3.6.0
203 - *
204 - * @param array $where where clause.
205 - *
206 - * @return mixed
207 - */
208 - public function get_row( $where = array() ) {
209 - return QueryHelper::get_row( $this->table_name, $where, 'id' );
210 - }
211 -
212 - /**
213 165 * Get recalculated order tax data.
214 166 *
215 167 * @since 3.4.0
216 - * @since 3.8.0 tax re-calculation based on pre_tax_price column value.
217 168 *
218 169 * @param int|object $order the order id or object.
219 170 *
220 171 * @return array
@@ -219,76 +170,29 @@
219 170 *
220 171 * @return array
221 172 */
222 173 public function get_recalculated_order_tax_data( $order ) {
223 - $tax_type = Tax::get_tax_type();
224 - $tax_rate = Tax::get_user_tax_rate( $order->user_id );
225 - $order = self::get_order( $order );
226 - $order_data = array(
227 - 'tax_type' => null,
228 - 'tax_rate' => null,
229 - 'tax_amount' => null,
230 - );
174 + $order = self::get_order( $order );
175 + $total_price = $order->total_price;
176 + $tax_rate = Tax::get_user_tax_rate( $order->user_id );
177 + $order_data = array();
178 + if ( $tax_rate ) {
179 + $order_data['tax_type'] = Tax::get_tax_type();
180 + $order_data['tax_rate'] = $tax_rate;
181 + $order_data['tax_amount'] = Tax::calculate_tax( $total_price, $tax_rate );
231 182
232 - if ( ! Tax::should_calculate_tax()
233 - || ! $tax_rate
234 - || ! $order
235 - || ! $order->pre_tax_price ) {
236 - return $order_data;
183 + if ( ! Tax::is_tax_included_in_price() ) {
184 + $total_price += $order_data['tax_amount'];
185 + $order_data['total_price'] = $total_price;
186 + $order_data['net_payment'] = $total_price;
187 + }
237 188 }
238 189
239 - $order_data['tax_type'] = $tax_type;
240 - $order_data['tax_rate'] = $tax_rate;
241 -
242 - if ( ! Tax::is_tax_included_in_price() ) {
243 - // For exclusive tax type.
244 - $tax_amount = Tax::calculate_tax( $order->pre_tax_price, $tax_rate );
245 - $total_price = $order->pre_tax_price + $tax_amount;
246 -
247 - $order_data['tax_amount'] = $tax_amount;
248 - $order_data['total_price'] = $total_price;
249 - $order_data['net_payment'] = $total_price;
250 - } else {
251 - // For inclusive tax type.
252 - $tax_amount = Tax::calculate_tax( $order->total_price, $tax_rate );
253 -
254 - $order_data['tax_amount'] = $tax_amount;
255 - $order_data['pre_tax_price'] = $order->total_price - $tax_amount;
256 - }
257 -
258 190 return $order_data;
259 191 }
260 192
261 - /**
262 - * Get order item display price.
263 - *
264 - * @since 3.5.0
265 - *
266 - * @param object $item order item object.
267 - *
268 - * @return string
269 - */
270 - public function get_order_item_display_price( $item ) {
271 - $display_price = is_numeric( $item->sale_price )
272 - ? $item->sale_price
273 - : ( is_numeric( $item->discount_price ) ? $item->discount_price : $item->regular_price );
274 - return $display_price;
275 - }
276 193
277 194 /**
278 - * Check order item has sale price or discount price
279 - *
280 - * @since 3.5.0
281 - *
282 - * @param object $item order item object.
283 - *
284 - * @return boolean
285 - */
286 - public function has_order_item_sale_price( $item ) {
287 - return is_numeric( $item->sale_price ) || is_numeric( $item->discount_price );
288 - }
289 -
290 - /**
291 195 * Get all order statuses
292 196 *
293 197 * @since 3.0.0
294 198 *
@@ -299,28 +203,12 @@
299 203 self::ORDER_INCOMPLETE => __( 'Incomplete', 'tutor' ),
300 204 self::ORDER_COMPLETED => __( 'Completed', 'tutor' ),
301 205 self::ORDER_CANCELLED => __( 'Cancelled', 'tutor' ),
302 206 self::ORDER_TRASH => __( 'Trash', 'tutor' ),
303 - self::ORDER_PENDING => __( 'Pending', 'tutor' ),
304 207 );
305 208 }
306 209
307 210 /**
308 - * Get all order types
309 - *
310 - * @since 3.7.0
311 - *
312 - * @return array
313 - */
314 - public static function get_order_type_list() {
315 - return array(
316 - self::TYPE_SINGLE_ORDER => __( 'Single Order', 'tutor' ),
317 - self::TYPE_SUBSCRIPTION => __( 'Subscription', 'tutor' ),
318 - self::TYPE_RENEWAL => __( 'Renewal', 'tutor' ),
319 - );
320 - }
321 -
322 - /**
323 211 * Get all payment statuses
324 212 *
325 213 * @since 3.0.0
326 214 *
@@ -332,9 +220,8 @@
332 220 self::PAYMENT_UNPAID => __( 'Unpaid', 'tutor' ),
333 221 self::PAYMENT_FAILED => __( 'Failed', 'tutor' ),
334 222 self::PAYMENT_REFUNDED => __( 'Refunded', 'tutor' ),
335 223 self::PAYMENT_PARTIALLY_REFUNDED => __( 'Partially Refunded', 'tutor' ),
336 - self::PAYMENT_PENDING => __( 'Pending', 'tutor' ),
337 224 );
338 225 }
339 226
340 227 /**
@@ -445,30 +332,21 @@
445 332 $items = array( $items );
446 333 }
447 334
448 335 // Set order id on each item.
449 - foreach ( $items as $item ) {
450 - $item['order_id'] = $order_id;
451 - $meta_data = $item['meta_data'] ?? null;
452 - try {
453 - unset( $item['meta_data'] );
454 - $insert = QueryHelper::insert(
455 - $this->order_item_table,
456 - $item,
457 - );
458 - if ( $insert ) {
459 - if ( ! empty( $meta_data ) ) {
460 - foreach ( $meta_data as $meta ) {
461 - ( new OrderItemMetaModel() )->add_meta( $insert, $meta['meta_key'], maybe_serialize( $meta['meta_value'] ) );
462 - }
463 - }
464 - }
465 - } catch ( \Throwable $th ) {
466 - return false;
467 - }
336 + foreach ( $items as $key => $item ) {
337 + $items[ $key ]['order_id'] = $order_id;
468 338 }
469 339
470 - return true;
340 + try {
341 + $insert = QueryHelper::insert_multiple_rows(
342 + $this->order_item_table,
343 + $items
344 + );
345 + return $insert ? true : false;
346 + } catch ( \Throwable $th ) {
347 + throw new Exception( $th->getMessage() );
348 + }
471 349 }
472 350
473 351 /**
474 352 * Retrieve order details by order ID.
@@ -500,15 +378,18 @@
500 378 return false;
501 379 }
502 380
503 381 $user_info = get_userdata( $order_data->user_id );
382 + if ( ! is_a( $user_info, 'WP_User' ) ) {
383 + return false;
384 + }
504 385
505 386 $student = new \stdClass();
506 - $student->id = (int) $user_info->ID ?? 0;
507 - $student->name = $user_info->data->display_name ?? '';
508 - $student->email = $user_info->data->user_email ?? '';
387 + $student->id = (int) $user_info->ID;
388 + $student->name = $user_info->data->display_name;
389 + $student->email = $user_info->data->user_email;
509 390 $student->phone = get_user_meta( $order_data->user_id, 'phone_number', true );
510 - $student->billing_address = $this->get_order_billing_address( $order_id, $order_data->user_id );
391 + $student->billing_address = $this->get_tutor_customer_data( $order_data->user_id );
511 392 $student->image = get_avatar_url( $order_data->user_id );
512 393
513 394 $order_data->student = $student;
514 395 $order_data->items = $this->get_order_items_by_id( $order_id );
@@ -531,13 +412,8 @@
531 412 $order_activities_model = new OrderActivitiesModel();
532 413 $order_data->activities = $order_activities_model->get_order_activities( $order_id );
533 414 $order_data->refunds = $this->get_order_refunds( $order_id );
534 415
535 - $enrollment_ids = $this->get_enrollment_ids( $order_id );
536 - if ( tutor_utils()->count( $enrollment_ids ) ) {
537 - $order_data->enrollment = EnrollmentModel::get_enrolment_by_enrol_id( $enrollment_ids[0] );
538 - }
539 -
540 416 unset( $student->billing_address->id );
541 417 unset( $student->billing_address->user_id );
542 418
543 419 return apply_filters( 'tutor_order_details', $order_data );
@@ -638,25 +514,22 @@
638 514 * items are found.
639 515 *
640 516 * @since 3.0.0
641 517 *
518 + * @global wpdb $wpdb WordPress database abstraction object.
519 + *
642 520 * @param int $order_id The ID of the order to retrieve items for.
643 521 *
644 522 * @return array The order items, each containing details and course titles, or an empty array if no items are found.
645 523 */
646 524 public function get_order_items_by_id( $order_id ) {
647 - $cache_key = 'tutor_get_order_items_by_id_' . $order_id;
648 - $cached = TutorCache::get( $cache_key );
525 + global $wpdb;
649 526
650 - if ( $cached ) {
651 - return $cached;
652 - }
653 -
654 - $primary_table = 'tutor_order_items AS oi';
527 + $primary_table = "{$wpdb->prefix}tutor_order_items AS oi";
655 528 $joining_tables = array(
656 529 array(
657 530 'type' => 'LEFT',
658 - 'table' => 'posts AS p',
531 + 'table' => "{$wpdb->prefix}posts AS p",
659 532 'on' => 'p.ID = oi.item_id',
660 533 ),
661 534 );
662 535
@@ -661,76 +534,76 @@
661 534 );
662 535
663 536 $where = array( 'order_id' => $order_id );
664 537
665 - $select_columns = array( 'oi.id AS primary_id', 'oi.item_id AS id', 'oi.regular_price', 'oi.sale_price', 'oi.discount_price', 'oi.coupon_code', 'p.post_title AS title', 'p.post_type AS type' );
538 + $select_columns = array( 'oi.item_id AS id', 'oi.regular_price', 'oi.sale_price', 'oi.discount_price', 'oi.coupon_code', 'p.post_title AS title', 'p.post_type AS type' );
666 539
667 540 $courses_data = QueryHelper::get_joined_data( $primary_table, $joining_tables, $select_columns, $where, array(), 'id', 0, 0 );
668 541 $courses = $courses_data['results'];
669 542
670 - $bundle_model = tutor()->has_pro ? new \TutorPro\CourseBundle\Models\BundleModel() : null;
543 + if ( tutor()->has_pro ) {
544 + $bundle_model = new \TutorPro\CourseBundle\Models\BundleModel();
545 + }
671 546
672 547 if ( ! empty( $courses_data['total_count'] ) ) {
673 548 foreach ( $courses as &$course ) {
674 - if ( is_object( $bundle_model ) && tutor()->bundle_post_type === $course->type ) {
549 + if ( tutor()->has_pro && 'course-bundle' === $course->type ) {
675 550 $course->total_courses = count( $bundle_model->get_bundle_course_ids( $course->id ) );
676 551 }
677 552
678 553 $course->id = (int) $course->id;
679 554 $course->regular_price = (float) $course->regular_price;
555 + $course->sale_price = (float) $course->sale_price;
680 556 $course->image = get_the_post_thumbnail_url( $course->id );
681 -
682 - // Add meta items.
683 - $order_item_meta = new OrderItemMetaModel();
684 - $course->item_meta_list = apply_filters( 'tutor_order_item_meta', $order_item_meta->get_meta( $course->primary_id, null, false ) );
685 557 }
686 558 }
687 559
688 560 unset( $course );
689 561
690 - $result = ! empty( $courses ) ? $courses : array();
691 - TutorCache::set( $cache_key, $result );
692 -
693 - return $result;
562 + return ! empty( $courses ) ? $courses : array();
694 563 }
695 564
696 565 /**
697 - * Get order billing address with fallback customer billing address record support.
698 - * It'll return order billing address if found, otherwise it'll return customer billing address record.
566 + * Retrieve tutor customer data by user ID.
699 567 *
700 - * @since 3.5.0
568 + * This function fetches customer data from the 'tutor_customers' table based on
569 + * the given user ID. It utilizes a helper function from the QueryHelper class
570 + * to perform the database query.
701 571 *
702 - * @param int $order_id order id.
703 - * @param int $user_id order id.
572 + * The function returns the customer data as an object.
704 573 *
705 - * @return object
574 + * @global wpdb $wpdb WordPress database abstraction object.
575 + *
576 + * @param int $user_id The ID of the user to retrieve customer data for.
577 + *
578 + * @since 3.0.0
579 + *
580 + * @return object|null The customer data retrieved from the database.
706 581 */
707 - public static function get_order_billing_address( $order_id, $user_id ) {
708 - $billing_address = OrderMetaModel::get_meta_value( $order_id, self::META_KEY_BILLING_ADDRESS, true );
582 + public function get_tutor_customer_data( $user_id ) {
583 + global $wpdb;
709 584
710 - /**
711 - * Fallback data from customer billing record.
712 - */
713 - if ( false === $billing_address ) {
714 - $billing_address = ( new BillingController( false ) )->get_billing_info( $user_id );
715 - } else {
716 - $billing_address = json_decode( $billing_address );
585 + // Retrieve customer data for the given user ID from the 'tutor_customers' table.
586 + $customer_data = QueryHelper::get_row( "{$wpdb->prefix}tutor_customers", array( 'user_id' => $user_id ), 'id' );
587 +
588 + if ( empty( $customer_data ) ) {
589 + return null;
717 590 }
718 591
719 - $data = (object) array(
720 - 'first_name' => $billing_address->billing_first_name ?? '',
721 - 'last_name' => $billing_address->billing_last_name ?? '',
722 - 'full_name' => trim( ( $billing_address->billing_first_name ?? '' ) . ' ' . ( $billing_address->billing_last_name ?? '' ) ),
723 - 'email' => $billing_address->billing_email ?? '',
724 - 'phone' => $billing_address->billing_phone ?? '',
725 - 'address' => $billing_address->billing_address ?? '',
726 - 'country' => $billing_address->billing_country ?? '',
727 - 'state' => $billing_address->billing_state ?? '',
728 - 'city' => $billing_address->billing_city ?? '',
729 - 'zip_code' => $billing_address->billing_zip_code ?? '',
592 + $return_data = (object) array(
593 + 'id' => $customer_data->id,
594 + 'user_id' => $customer_data->user_id,
595 + 'name' => $customer_data->billing_first_name . ' ' . $customer_data->billing_last_name,
596 + 'email' => $customer_data->billing_email,
597 + 'phone' => $customer_data->billing_phone,
598 + 'address' => $customer_data->billing_address,
599 + 'city' => $customer_data->billing_city,
600 + 'state' => $customer_data->billing_state,
601 + 'country' => $customer_data->billing_country,
602 + 'zip_code' => $customer_data->billing_zip_code,
730 603 );
731 604
732 - return $data;
605 + return $return_data;
733 606 }
734 607
735 608 /**
736 609 * Retrieve order refunds by order ID.
@@ -810,16 +683,14 @@
810 683 * @since 3.0.0
811 684 *
812 685 * @param int|array $order_id Integer or array of ids sql escaped.
813 686 * @param array $data Data to update, escape data.
814 - * @param array $order_items order items (optional).
815 687 *
816 688 * @return bool
817 689 */
818 - public function update_order( $order_id, array $data, array $order_items = array() ) {
690 + public function update_order( $order_id, array $data ) {
819 691 $order_id = is_array( $order_id ) ? $order_id : array( $order_id );
820 692 $order_id = QueryHelper::prepare_in_clause( $order_id );
821 -
822 693 try {
823 694 QueryHelper::update_where_in(
824 695 $this->table_name,
825 696 $data,
@@ -824,12 +695,8 @@
824 695 $this->table_name,
825 696 $data,
826 697 $order_id
827 698 );
828 -
829 - if ( ! empty( $order_items ) ) {
830 - $this->update_order_items( $order_id, $order_items );
831 - }
832 699 return true;
833 700 } catch ( \Throwable $th ) {
834 701 error_log( $th->getMessage() . ' in ' . $th->getFile() . ' at line ' . $th->getLine() );
835 702 return false;
@@ -853,9 +720,9 @@
853 720 $wpdb->prepare(
854 721 "SELECT * FROM {$wpdb->postmeta}
855 722 WHERE meta_key=%s
856 723 AND meta_value LIKE %d",
857 - EnrollmentModel::ENROLLMENT_ORDER_ID_META,
724 + '_tutor_enrolled_by_order_id',
858 725 $order_id
859 726 )
860 727 );
861 728
@@ -905,9 +772,9 @@
905 772 QueryHelper::delete(
906 773 $wpdb->prefix . 'tutor_earnings',
907 774 array(
908 775 'order_id' => $order_id,
909 - 'process_by' => Earnings::PROCESS_BY_TUTOR,
776 + 'process_by' => 'Tutor',
910 777 )
911 778 );
912 779
913 780 // Now delete order.
@@ -1007,30 +874,23 @@
1007 874 /**
1008 875 * Get order of a user
1009 876 *
1010 877 * @since 3.0.0
1011 - * @since 4.0.0 params $order_status, $order and $args added.
1012 878 *
1013 879 * @param string $time_period $time_period Sorting time period,
1014 880 * supported time periods are: today, monthly & yearly.
1015 881 * @param string $start_date $start_date For date range sorting.
1016 882 * @param string $end_date $end_date For date range sorting.
1017 - * @param string $order_status $order_status Order status.
1018 883 * @param int $user_id User id for fetching order list.
1019 884 * @param int $limit Limit to fetch record.
1020 885 * @param int $offset Offset to fetch record.
1021 - * @param string $order Order to fetch record.
1022 - * @param array $args Optional additional WHERE conditions.
1023 886 *
1024 887 * @throws \Exception Throw exception if database error occur.
1025 888 *
1026 889 * @return array
1027 890 */
1028 - public function get_user_orders( $time_period = null, $start_date = null, $end_date = null, $order_status = '', int $user_id = 0, $limit = 10, int $offset = 0, $order = 'DESC', $args = array() ) {
1029 - $user_id = tutor_utils()->get_user_id( $user_id );
1030 - $order = QueryHelper::get_valid_sort_order( $order );
1031 - $order_status = esc_sql( $order_status );
1032 - $order_type_clause = '';
891 + public function get_user_orders( $time_period = null, $start_date = null, $end_date = null, int $user_id = 0, $limit = 10, int $offset = 0 ) {
892 + $user_id = $user_id ? $user_id : get_current_user_id();
1033 893
1034 894 $response = array(
1035 895 'results' => array(),
1036 896 'total_count' => 0,
@@ -1037,29 +897,25 @@
1037 897 );
1038 898
1039 899 global $wpdb;
1040 900
1041 - $time_period_clause = '';
1042 - $date_range_clause = '';
1043 - $order_status_clause = ( empty( $order_status ) || 'all' === $order_status ) ? '' : "AND o.order_status = '{$order_status}'";
901 + $time_period_clause = '';
902 + $date_range_clause = '';
1044 903
1045 904 if ( $start_date && $end_date ) {
1046 905 $date_range_clause = $wpdb->prepare( 'AND DATE(created_at_gmt) BETWEEN %s AND %s', $start_date, $end_date );
1047 - } elseif ( $time_period ) {
1048 - if ( 'today' === $time_period ) {
1049 - $time_period_clause = 'AND DATE(o.created_at_gmt) = CURDATE()';
1050 - } elseif ( 'monthly' === $time_period ) {
1051 - $time_period_clause = 'AND MONTH(o.created_at_gmt) = MONTH(CURDATE()) ';
1052 - } else {
1053 - $time_period_clause = 'AND YEAR(o.created_at_gmt) = YEAR(CURDATE()) ';
906 + } else {
907 + if ( $time_period ) {
908 + if ( 'today' === $time_period ) {
909 + $time_period_clause = 'AND DATE(o.created_at_gmt) = CURDATE()';
910 + } elseif ( 'monthly' === $time_period ) {
911 + $time_period_clause = 'AND MONTH(o.created_at_gmt) = MONTH(CURDATE()) ';
912 + } else {
913 + $time_period_clause = 'AND YEAR(o.created_at_gmt) = YEAR(CURDATE()) ';
914 + }
1054 915 }
1055 916 }
1056 917
1057 - if ( ! empty( $args['order_type'] ) ) {
1058 - $order_type_clause = ' AND ' . QueryHelper::prepare_where_clause( array( 'o.order_type' => esc_sql( $args['order_type'] ) ) );
1059 - }
1060 -
1061 - //phpcs:disable
1062 918 $query = $wpdb->prepare(
1063 919 "SELECT
1064 920 SQL_CALC_FOUND_ROWS
1065 921 o.*
@@ -1064,13 +920,11 @@
1064 920 SQL_CALC_FOUND_ROWS
1065 921 o.*
1066 922 FROM $this->table_name AS o
1067 923 WHERE o.user_id = %d
1068 - {$order_type_clause}
1069 924 {$time_period_clause}
1070 925 {$date_range_clause}
1071 - {$order_status_clause}
1072 - ORDER BY o.id {$order}
926 + ORDER BY o.id DESC
1073 927 LIMIT %d OFFSET %d
1074 928 ",
1075 929 $user_id,
1076 930 $limit,
@@ -1077,9 +931,8 @@
1077 931 $offset
1078 932 );
1079 933
1080 934 $results = $wpdb->get_results( $query );
1081 - //phpcs:enable
1082 935
1083 936 if ( $wpdb->last_error ) {
1084 937 throw new \Exception( $wpdb->last_error );
1085 938 } else {
@@ -1097,9 +950,8 @@
1097 950 *
1098 951 * If period or date range not pass then it will return all time enrollment list
1099 952 *
1100 953 * @since 3.0.0
1101 - * @since 4.0.0 Minor query updates for the new graph.
1102 954 *
1103 955 * @param int $user_id User id, if user not have admin access
1104 956 * then only this user's refund amount will fetched.
1105 957 * @param string $period Time period.
@@ -1120,10 +972,10 @@
1120 972 $user_clause = '';
1121 973 $date_range_clause = '';
1122 974 $period_clause = '';
1123 975 $course_clause = '';
1124 - $group_clause = ' GROUP BY DATE(o.created_at_gmt) ';
1125 - $select_query = " DATE_FORMAT(o.created_at_gmt, '%%b') AS label_name, DATE(o.created_at_gmt) AS date_format ";
976 + $group_clause = ' GROUP BY DATE(date_format) ';
977 + $discount_clause = 'o.coupon_amount as total';
1126 978
1127 979 if ( $start_date && $end_date ) {
1128 980 $date_range_clause = $wpdb->prepare(
1129 981 'AND o.created_at_gmt BETWEEN %s AND %s',
@@ -1129,23 +981,14 @@
1129 981 'AND o.created_at_gmt BETWEEN %s AND %s',
1130 982 $start_date,
1131 983 $end_date
1132 984 );
1133 -
1134 - $diff_days = ( new DateTime( $start_date ) )->diff( new DateTime( $end_date ) )->days;
1135 -
1136 - if ( $diff_days > 31 ) {
1137 - $group_clause = ' GROUP BY MONTH(o.created_at_gmt) ';
1138 - $select_query = " DATE_FORMAT(o.created_at_gmt, '%%b') AS label_name ";
1139 -
1140 - }
1141 985 } else {
1142 986 $period_clause = QueryHelper::get_period_clause( 'o.created_at_gmt', $period );
1143 987 }
1144 988
1145 - if ( in_array( $period, array( 'last90days', 'last365days', 'yearly' ), true ) ) {
1146 - $group_clause = ' GROUP BY MONTH(o.created_at_gmt) ';
1147 - $select_query = " DATE_FORMAT(o.created_at_gmt, '%%b') AS label_name ";
989 + if ( 'today' !== $period ) {
990 + $group_clause = ' GROUP BY MONTH(date_format) ';
1148 991 }
1149 992
1150 993 if ( $course_id ) {
1151 994 $course_clause = $wpdb->prepare( 'AND i.item_id = %d', $course_id );
@@ -1158,9 +1001,8 @@
1158 1001 if ( $user_id ) {
1159 1002 $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1160 1003 }
1161 1004
1162 - //phpcs:disable
1163 1005 $discounts = $wpdb->get_results(
1164 1006 $wpdb->prepare(
1165 1007 "SELECT
1166 1008 i.item_id AS course_id,
@@ -1174,9 +1016,9 @@
1174 1016 ),
1175 1017 0
1176 1018 )
1177 1019 ) AS total,
1178 - {$select_query}
1020 + o.created_at_gmt AS date_format
1179 1021 FROM
1180 1022 {$this->table_name} o
1181 1023 JOIN
1182 1024 {$item_table} i ON o.id = i.order_id
@@ -1195,15 +1037,13 @@
1195 1037 tutor()->course_post_type,
1196 1038 $course_id
1197 1039 )
1198 1040 );
1199 - //phpcs:enable
1200 1041 } else {
1201 1042 if ( $user_id ) {
1202 1043 $user_clause = $wpdb->prepare( "AND %d = (SELECT user_id FROM {$wpdb->tutor_earnings} WHERE order_status = 'completed' LIMIT 1) ", $user_id );
1203 1044 }
1204 1045
1205 - //phpcs:disable
1206 1046 $discounts = $wpdb->get_results(
1207 1047 $wpdb->prepare(
1208 1048 "SELECT
1209 1049 SUM(
@@ -1216,9 +1056,9 @@
1216 1056 ),
1217 1057 0
1218 1058 )
1219 1059 ) AS total,
1220 - {$select_query}
1060 + o.created_at_gmt AS date_format
1221 1061 FROM {$this->table_name} AS o
1222 1062 WHERE 1 = %d
1223 1063 AND o.order_status = 'completed'
1224 1064 {$user_clause}
@@ -1230,9 +1070,8 @@
1230 1070 ",
1231 1071 1
1232 1072 )
1233 1073 );
1234 - //phpcs:enable
1235 1074 }
1236 1075
1237 1076 $total_discount = 0;
1238 1077 $discount_items = array();
@@ -1249,15 +1088,15 @@
1249 1088
1250 1089 // Split each discount.
1251 1090 list( $admin_discount, $instructor_discount ) = array_values( tutor_split_amounts( $discount->total ) );
1252 1091
1253 - $discount->total = User::is_admin() && is_admin() ? $admin_discount : $instructor_discount;
1092 + $discount->total = is_admin() ? $admin_discount : $instructor_discount;
1254 1093 }
1255 1094
1256 1095 list( $admin_total, $instructor_total ) = array_values( tutor_split_amounts( $total_discount ) );
1257 1096
1258 1097 $response['discounts'] = $discount_items;
1259 - $response['total_discounts'] = User::is_admin() && is_admin() ? $admin_total : $instructor_total;
1098 + $response['total_discounts'] = is_admin() ? $admin_total : $instructor_total;
1260 1099 }
1261 1100
1262 1101 return $response;
1263 1102 }
@@ -1269,9 +1108,8 @@
1269 1108 *
1270 1109 * If period or date range not pass then it will return all time enrollment list
1271 1110 *
1272 1111 * @since 3.0.0
1273 - * @since 4.0.0 Minor query updates for the new graph.
1274 1112 *
1275 1113 * @param int $user_id User id, if user not have admin access
1276 1114 * then only this user's refund amount will fetched.
1277 1115 * @param string $period Time period.
@@ -1291,31 +1129,26 @@
1291 1129
1292 1130 $user_clause = '';
1293 1131 $date_range_clause = '';
1294 1132 $period_clause = '';
1295 - $group_clause = ' GROUP BY DATE(order_meta.created_at_gmt) ';
1296 - $select_query = " DATE_FORMAT(order_meta.created_at_gmt, '%%b') AS label_name, order_meta.created_at_gmt AS date_format ";
1133 + $course_clause = '';
1134 + $commission_clause = '';
1135 + $group_clause = ' GROUP BY DATE(o.created_at_gmt) ';
1297 1136
1298 1137 if ( $start_date && $end_date ) {
1299 1138 $date_range_clause = $wpdb->prepare(
1300 - 'AND DATE(order_meta.created_at_gmt) BETWEEN %s AND %s',
1139 + 'AND o.created_at_gmt BETWEEN %s AND %s',
1301 1140 $start_date,
1302 1141 $end_date
1303 1142 );
1143 + $group_clause = ' GROUP BY DATE(o.created_at_gmt) ';
1304 1144
1305 - $diff_days = ( new DateTime( $start_date ) )->diff( new DateTime( $end_date ) )->days;
1306 -
1307 - if ( $diff_days > 31 ) {
1308 - $group_clause = ' GROUP BY MONTH(order_meta.created_at_gmt) ';
1309 - $select_query = " DATE_FORMAT(order_meta.created_at_gmt, '%%b') AS label_name ";
1310 - }
1311 1145 } else {
1312 - $period_clause = QueryHelper::get_period_clause( 'order_meta.created_at_gmt', $period );
1146 + $period_clause = QueryHelper::get_period_clause( 'o.created_at_gmt', $period );
1313 1147 }
1314 1148
1315 - if ( in_array( $period, array( 'last90days', 'last365days', 'yearly' ), true ) ) {
1316 - $group_clause = ' GROUP BY MONTH(order_meta.created_at_gmt) ';
1317 - $select_query = " DATE_FORMAT(order_meta.created_at_gmt, '%%b') AS label_name ";
1149 + if ( 'today' !== $period ) {
1150 + $group_clause = ' GROUP BY MONTH(o.created_at_gmt) ';
1318 1151 }
1319 1152
1320 1153 if ( $course_id ) {
1321 1154 if ( $user_id ) {
@@ -1320,24 +1153,18 @@
1320 1153 if ( $course_id ) {
1321 1154 if ( $user_id ) {
1322 1155 $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1323 1156 }
1324 - } elseif ( $user_id ) {
1325 - $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1157 + } else {
1158 + if ( $user_id ) {
1159 + $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1160 + }
1326 1161 }
1327 1162
1328 1163 // Refund query logic remains the same.
1329 - $item_table = $wpdb->prefix . 'tutor_order_items';
1330 - $order_meta_table = $wpdb->prefix . 'tutor_ordermeta';
1331 - $order_meta_in_clause = QueryHelper::prepare_in_clause(
1332 - array(
1333 - OrderActivitiesModel::META_KEY_REFUND,
1334 - OrderActivitiesModel::META_KEY_PARTIALLY_REFUND,
1335 - )
1336 - );
1164 + $item_table = $wpdb->prefix . 'tutor_order_items';
1337 1165
1338 1166 if ( $course_id ) {
1339 - //phpcs:disable
1340 1167 $refunds = $wpdb->get_results(
1341 1168 $wpdb->prepare(
1342 1169 "SELECT
1343 1170 i.item_id AS course_id,
@@ -1351,10 +1178,9 @@
1351 1178 ELSE i.regular_price
1352 1179 END / o.total_price
1353 1180 )
1354 1181 ), 2
1355 - ) AS total,
1356 - {$select_query}
1182 + ) AS total
1357 1183 FROM
1358 1184 {$this->table_name} o
1359 1185 JOIN
1360 1186 {$item_table} i ON o.id = i.order_id
@@ -1361,11 +1187,8 @@
1361 1187 JOIN
1362 1188 {$wpdb->posts} c
1363 1189 ON c.ID = i.item_id
1364 1190 AND c.post_type = %s
1365 - JOIN {$order_meta_table} AS order_meta
1366 - ON order_meta.order_id = o.id
1367 - AND order_meta.meta_key IN ({$order_meta_in_clause})
1368 1191 WHERE
1369 1192 o.refund_amount > 0
1370 1193 AND i.item_id = %d
1371 1194 {$user_clause}
@@ -1370,43 +1193,40 @@
1370 1193 AND i.item_id = %d
1371 1194 {$user_clause}
1372 1195 {$period_clause}
1373 1196 {$date_range_clause}
1374 - {$group_clause}
1375 - ORDER BY order_meta.created_at_gmt ASC",
1197 + {$group_clause},
1198 + i.item_id
1199 + ",
1376 1200 tutor()->course_post_type,
1377 1201 $course_id
1378 1202 )
1379 1203 );
1380 - //phpcs:enable
1381 1204 } else {
1382 1205 $earning_table = $wpdb->tutor_earnings;
1383 1206 if ( $user_id ) {
1384 - $user_clause = "AND {$user_id} = (SELECT user_id FROM {$earning_table} WHERE user_id = {$user_id} LIMIT 1)";
1207 + $user_clause = "AND {$user_id} = (SELECT user_id FROM {$earning_table} LIMIT 1)";
1385 1208 }
1386 1209
1387 - //phpcs:disable
1388 1210 $refunds = $wpdb->get_results(
1389 1211 $wpdb->prepare(
1390 1212 "SELECT
1391 1213 COALESCE(SUM(o.refund_amount), 0) AS total,
1392 - {$select_query}
1214 + created_at_gmt AS date_format
1393 1215 FROM {$this->table_name} AS o
1394 - LEFT JOIN {$order_meta_table} AS order_meta
1395 - ON order_meta.order_id = o.id
1396 - AND order_meta.meta_key IN ({$order_meta_in_clause})
1216 + -- LEFT JOIN {$item_table} AS i ON i.order_id = o.id
1217 + -- LEFT JOIN {$wpdb->posts} AS c ON c.id = i.item_id
1397 1218 WHERE 1 = %d
1398 1219 AND o.refund_amount > %d
1399 1220 {$user_clause}
1400 1221 {$period_clause}
1401 1222 {$date_range_clause}
1402 - {$group_clause}
1403 - ORDER BY order_meta.created_at_gmt ASC",
1223 + {$group_clause},
1224 + o.id",
1404 1225 1,
1405 1226 0
1406 1227 )
1407 1228 );
1408 - //phpcs:enable
1409 1229 }
1410 1230
1411 1231 $total_refund = 0;
1412 1232
@@ -1414,9 +1234,9 @@
1414 1234 $total_refund += $refund->total;
1415 1235
1416 1236 // Update total amount from list.
1417 1237 $split_refund = (object) tutor_split_amounts( $refund->total );
1418 - $refund->total = User::is_admin() && is_admin() ? $split_refund->admin : $split_refund->instructor;
1238 + $refund->total = is_admin() ? $split_refund->admin : $split_refund->instructor;
1419 1239 }
1420 1240
1421 1241 $split_total_refund = (object) tutor_split_amounts( $total_refund );
1422 1242
@@ -1421,9 +1241,9 @@
1421 1241 $split_total_refund = (object) tutor_split_amounts( $total_refund );
1422 1242
1423 1243 $response = array(
1424 1244 'refunds' => $refunds,
1425 - 'total_refunds' => User::is_admin() && is_admin() ? $split_total_refund->admin : $split_total_refund->instructor,
1245 + 'total_refunds' => is_admin() ? $split_total_refund->admin : $split_total_refund->instructor,
1426 1246 );
1427 1247
1428 1248 return $response;
1429 1249 }
@@ -1847,10 +1667,10 @@
1847 1667 */
1848 1668 public static function should_show_pay_btn( object $order ) {
1849 1669 $order_items = ( new self() )->get_order_items_by_id( $order->id );
1850 1670 $is_enrolled_any_course = false;
1851 - $is_incomplete_payment = self::PAYMENT_UNPAID === $order->payment_status && self::ORDER_INCOMPLETE === $order->order_status;
1852 - $is_manual_payment = $order->payment_method ? self::is_manual_payment( $order->payment_method ) : false;
1671 + $is_incomplete_payment = ! empty( $order->payment_method ) && self::ORDER_INCOMPLETE === $order->order_status;
1672 + $is_manual_payment = $order->payment_method ? self::is_manual_payment( $order->payment_method ) : true;
1853 1673
1854 1674 if ( $is_incomplete_payment && ! $is_manual_payment && $order_items ) {
1855 1675 if ( self::TYPE_SINGLE_ORDER === $order->order_type ) {
1856 1676 foreach ( $order_items as $item ) {
@@ -1855,9 +1675,9 @@
1855 1675 if ( self::TYPE_SINGLE_ORDER === $order->order_type ) {
1856 1676 foreach ( $order_items as $item ) {
1857 1677 $course_id = $item->id;
1858 1678 if ( $course_id ) {
1859 - $is_enrolled = EnrollmentModel::is_enrolled( $course_id );
1679 + $is_enrolled = tutor_utils()->is_enrolled( $course_id );
1860 1680 if ( $is_enrolled ) {
1861 1681 $is_enrolled_any_course = true;
1862 1682 break;
1863 1683 }
@@ -1862,12 +1682,14 @@
1862 1682 break;
1863 1683 }
1864 1684 }
1865 1685 }
1866 - } elseif ( tutor_utils()->count( $order_items ) ) {
1686 + } else {
1687 + if ( tutor_utils()->count( $order_items ) ) {
1867 1688 $course_id = apply_filters( 'tutor_subscription_course_by_plan', $order_items[0]->id );
1868 - if ( EnrollmentModel::is_enrolled( $course_id ) ) {
1869 - $is_enrolled_any_course = true;
1689 + if ( tutor_utils()->is_enrolled( $course_id ) ) {
1690 + $is_enrolled_any_course = true;
1691 + }
1870 1692 }
1871 1693 }
1872 1694 }
1873 1695
@@ -1904,12 +1726,10 @@
1904 1726 * @return void
1905 1727 */
1906 1728 public static function render_pay_button( $order ) {
1907 1729
1908 - $self = new self();
1909 -
1910 1730 if ( is_numeric( $order ) ) {
1911 - $order = $self->get_order_by_id( $order );
1731 + $order = ( new self() )->get_order_by_id( $order );
1912 1732 }
1913 1733
1914 1734 $show_pay_button = self::should_show_pay_btn( $order );
1915 1735
@@ -1919,13 +1739,24 @@
1919 1739 <span class="tooltip-txt tooltip-left">
1920 1740 <?php esc_html_e( 'Payment Is Pending Due To Gateway Processing.', 'tutor' ); ?>
1921 1741 </span>
1922 1742 </div>
1743 + <?php elseif ( $show_pay_button ) :
1744 + ob_start();
1745 + ?>
1746 +
1747 + <form method="post">
1748 + <?php tutor_nonce_field(); ?>
1749 + <input type="hidden" name="tutor_action" value="tutor_pay_incomplete_order">
1750 + <input type="hidden" name="order_id" value="<?php echo esc_attr( $order->id ); ?>">
1751 +
1752 + <button type="submit" class="tutor-btn tutor-btn-sm tutor-btn-outline-primary">
1753 + <?php esc_html_e( 'Pay', 'tutor' ); ?>
1754 + </button>
1755 + </form>
1756 +
1923 1757 <?php
1924 - elseif ( $show_pay_button ) :
1925 - ob_start();
1926 - $self->pay_now_link( $order->id );
1927 - echo apply_filters( 'tutor_after_pay_button', ob_get_clean(), $order );//phpcs:ignore --sanitized output.
1758 + echo apply_filters( 'tutor_after_pay_button', ob_get_clean(), $order );
1928 1759 endif;
1929 1760 }
1930 1761
1931 1762 /**
@@ -1956,294 +1787,6 @@
1956 1787 return false;
1957 1788 }
1958 1789
1959 1790 return true;
1960 - }
1961 -
1962 - /**
1963 - * Retrieves statements for a specific user.
1964 - *
1965 - * @since 3.5.0
1966 - * @since 4.0.0 Added $order_by and $order option.
1967 - *
1968 - * @param string $post_type_in_clause Prepared SQL IN clause containing allowed course post types.
1969 - * @param string $course_query Optional SQL fragment to filter by course ID.
1970 - * @param string $date_query Optional SQL fragment to filter by statement date.
1971 - * @param int $user_id User (instructor) ID.
1972 - * @param int $offset Number of records to skip (pagination offset).
1973 - * @param int $limit Maximum number of records to return.
1974 - * @param string $order_by Column name to order results by.
1975 - * @param string $order Sort direction. Accepts 'ASC' or 'DESC'.
1976 - *
1977 - * @return array
1978 - */
1979 - public function get_statements( $post_type_in_clause, $course_query, $date_query, $user_id, $offset, $limit, $order_by, $order ): array {
1980 - global $wpdb;
1981 -
1982 - $order_clause = '';
1983 -
1984 - if ( sanitize_sql_orderby( $order ) ) {
1985 - $order_clause = "ORDER BY {$order_by} {$order}";
1986 - }
1987 -
1988 - //phpcs:disable
1989 - $statements = $wpdb->get_results(
1990 - $wpdb->prepare(
1991 - "SELECT
1992 - IF (
1993 - orders.total_price,
1994 - orders.total_price,
1995 - statements.course_price_total
1996 - ) AS order_total_price,
1997 - orders.tax_amount AS order_tax_amount,
1998 - orders.tax_type AS order_tax_type,
1999 - statements.*,
2000 - course.post_title AS course_title
2001 - FROM {$wpdb->prefix}tutor_earnings AS statements
2002 - LEFT JOIN {$wpdb->prefix}tutor_orders AS orders
2003 - ON statements.order_id = orders.id
2004 - INNER JOIN {$wpdb->posts} AS course ON course.ID = statements.course_id
2005 - AND course.post_type IN ({$post_type_in_clause})
2006 - WHERE statements.user_id = %d
2007 - {$course_query}
2008 - {$date_query}
2009 - {$order_clause}
2010 - LIMIT %d, %d",
2011 - $user_id,
2012 - $offset,
2013 - $limit
2014 - )
2015 - );
2016 -
2017 - $total_statements = $wpdb->get_var(
2018 - $wpdb->prepare(
2019 - "SELECT COUNT(*)
2020 - FROM {$wpdb->prefix}tutor_earnings AS statements
2021 - INNER JOIN {$wpdb->posts} AS course ON course.ID = statements.course_id
2022 - AND course.post_type IN ({$post_type_in_clause})
2023 - WHERE statements.user_id = %d
2024 - {$course_query}
2025 - {$date_query}",
2026 - $user_id
2027 - )
2028 - );
2029 - //phpcs:enable
2030 -
2031 - return array(
2032 - 'statements' => $statements,
2033 - 'total_statements' => $total_statements,
2034 - );
2035 - }
2036 -
2037 - /**
2038 - * Get order details for given course IDs.
2039 - *
2040 - * @since 3.8.1
2041 - *
2042 - * @param int[] $course_ids Array of course IDs to fetch order details.
2043 - *
2044 - * @return array Returns an array of order details with each element containing:
2045 - * - order data (all columns from tutor_orders table)
2046 - * - order_items data (al columns except id)
2047 - * Returns an empty array if no results found or on error.
2048 - */
2049 - public function get_order_details( array $course_ids ) {
2050 - global $wpdb;
2051 -
2052 - $result = array();
2053 -
2054 - $select_columns = array(
2055 - 'orders.*,
2056 - order_items.id AS order_items_id,
2057 - order_items.order_id,
2058 - order_items.item_id,
2059 - order_items.regular_price,
2060 - order_items.sale_price,
2061 - order_items.discount_price,
2062 - order_items.coupon_code AS item_coupon_code',
2063 - );
2064 - $primary_table = "{$wpdb->tutor_order_items} AS order_items";
2065 - $joining_tables = array(
2066 - array(
2067 - 'type' => 'LEFT',
2068 - 'table' => "{$wpdb->tutor_orders} AS orders",
2069 - 'on' => 'orders.id = order_items.order_id',
2070 - ),
2071 - );
2072 - $where = array( 'order_items.item_id' => array( 'IN', $course_ids ) );
2073 -
2074 - $result = QueryHelper::get_joined_data( $primary_table, $joining_tables, $select_columns, $where, array(), '', -1 );
2075 -
2076 - return $result['results'];
2077 - }
2078 -
2079 - /**
2080 - * Retrieve order meta for a specific order.
2081 - *
2082 - * @since 3.8.1
2083 - *
2084 - * @param int $order_id The ID of the order for which the metadata is to be retrieved.
2085 - *
2086 - * @return array An array of order meta. Returns an empty array if no meta is found.
2087 - */
2088 - public function get_order_meta_by_order_id( $order_id ) {
2089 -
2090 - return QueryHelper::get_all( 'tutor_ordermeta', array( 'order_id' => $order_id ), 'order_id' );
2091 - }
2092 -
2093 - /**
2094 - * Retrieve earnings for a specific order and course.
2095 - *
2096 - * @since 3.8.1
2097 - *
2098 - * @param int $order_id The ID of the order for which the earnings are being retrieved.
2099 - * @param int $course_id The ID of the course for which the earnings are being retrieved.
2100 - *
2101 - * @return array An array of earnings data. Returns an empty array if no data is found.
2102 - */
2103 - public function get_earnings_by_order_and_course( $order_id, $course_id ) {
2104 -
2105 - $where = array( 'order_id' => $order_id );
2106 -
2107 - if ( ! empty( $course_id ) ) {
2108 - $where['course_id'] = $course_id;
2109 - }
2110 -
2111 - return QueryHelper::get_all( 'tutor_earnings', $where, 'order_id' );
2112 - }
2113 -
2114 - /**
2115 - * Retrieve an existing order if it is incomplete, unpaid, and belongs to the given user.
2116 - *
2117 - * @since 3.9.2
2118 - *
2119 - * @param int $order_id The ID of the order.
2120 - * @param int $user_id The ID of the current user.
2121 - * @param bool $return_order_data Optional. Whether to return the order data instead of a boolean.
2122 - *
2123 - * @return bool|object Returns:
2124 - * - The order object if valid and $return_order_data is true.
2125 - * - True if valid and $return_order_data is false.
2126 - * - false if the order is invalid or not found.
2127 - */
2128 - public static function get_valid_incomplete_order( int $order_id, int $user_id, $return_order_data = false ) {
2129 -
2130 - if ( empty( $order_id ) || empty( $user_id ) ) {
2131 - return false;
2132 - }
2133 -
2134 - $order_data = ( new self() )->get_order_by_id( $order_id );
2135 -
2136 - if ( empty( $order_data ) ) {
2137 - return false;
2138 - }
2139 -
2140 - $is_valid = self::ORDER_INCOMPLETE === $order_data->order_status
2141 - && self::PAYMENT_UNPAID === $order_data->payment_status
2142 - && $user_id === $order_data->student->id
2143 - && self::should_show_pay_btn( $order_data );
2144 -
2145 - if ( $is_valid && $return_order_data ) {
2146 - return $order_data;
2147 - }
2148 -
2149 - return $is_valid;
2150 - }
2151 -
2152 - /**
2153 - * Update all order items for a given order.
2154 - *
2155 - * @since 3.9.2
2156 - *
2157 - * @param int $order_id The ID of the order whose items are being updated.
2158 - * @param array $order_items An array of order item data arrays or objects to update.
2159 - *
2160 - * @return bool True on success, false if any update fails.
2161 - */
2162 - public function update_order_items( int $order_id, array $order_items ) {
2163 -
2164 - foreach ( $order_items as $item ) {
2165 - try {
2166 - QueryHelper::update_where_in(
2167 - $this->order_item_table,
2168 - $item,
2169 - $order_id
2170 - );
2171 -
2172 - } catch ( \Throwable $th ) {
2173 - tutor_log( "Failed to update order item for order ID {$order_id}: " . $th->getMessage() );
2174 - return false;
2175 - }
2176 - }
2177 -
2178 - return true;
2179 - }
2180 -
2181 - /**
2182 - * Generate the payment link button HTML for a given order.
2183 - *
2184 - * @since 3.9.2
2185 - *
2186 - * @param int $order_id The unique ID of the order.
2187 - * @param bool $display Optional. Whether to echo the link (true) or return it (false). Default true.
2188 - *
2189 - * @return string|void
2190 - */
2191 - public static function pay_now_link( $order_id, $display = true ) {
2192 -
2193 - $checkout_url = add_query_arg( array( 'order_id' => $order_id ), CheckoutController::get_page_url() );
2194 -
2195 - $link =
2196 - sprintf(
2197 - '<a href="%s" class="tutor-btn tutor-btn-link tutor-text-brand tutor-p-none tutor-min-h-fit">
2198 - %s
2199 - </a>',
2200 - esc_url( $checkout_url ),
2201 - esc_html__( 'Pay', 'tutor' )
2202 - );
2203 -
2204 - if ( $display ) {
2205 - echo wp_kses(
2206 - $link,
2207 - array(
2208 - 'a' => array(
2209 - 'href' => true,
2210 - 'class' => true,
2211 - ),
2212 - )
2213 - );
2214 - } else {
2215 - return $link;
2216 - }
2217 - }
2218 -
2219 - /**
2220 - * Get order item titles for order history display.
2221 - *
2222 - * @since 4.0.0
2223 - *
2224 - * @param object $order Order object.
2225 - *
2226 - * @return array<int, string> List of item titles.
2227 - */
2228 - public static function get_order_history_titles( $order ) {
2229 - $titles = array();
2230 - $items = ( new OrderModel() )->get_order_items_by_id( $order->id );
2231 - foreach ( $items as $item ) {
2232 -
2233 - if ( self::TYPE_SINGLE_ORDER === $order->order_type ) {
2234 - $titles[] = get_the_title( $item->id );
2235 - continue;
2236 - }
2237 -
2238 - $object_id = apply_filters( 'tutor_subscription_course_by_plan', $item->id, $order );
2239 - $plan_info = apply_filters( 'tutor_get_plan_info', null, $item->id );
2240 - if ( $plan_info && isset( $plan_info->is_membership_plan ) && $plan_info->is_membership_plan ) {
2241 - $titles[] = $plan_info->plan_name;
2242 - } else {
2243 - $titles[] = get_the_title( $object_id );
2244 - }
2245 - }
2246 -
2247 - return $titles;
2248 1791 }
2249 1792 }