PluginProbe
Tutor LMS – eLearning and online course solution / 3.7.2
Tutor LMS – eLearning and online course solution v3.7.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 +113 -422 trunk3.7.2 View file →
@@ -9,24 +9,15 @@
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;
14 +use Tutor\Ecommerce\Tax;
15 +use Tutor\Ecommerce\Ecommerce;
16 +use Tutor\Helpers\QueryHelper;
17 +use Tutor\Helpers\DateTimeHelper;
20 18 use Tutor\Ecommerce\BillingController;
21 -use Tutor\Ecommerce\CheckoutController;
22 -use Tutor\Ecommerce\Ecommerce;
23 19 use Tutor\Ecommerce\OrderActivitiesController;
24 -use Tutor\Ecommerce\Tax;
25 -use Tutor\Helpers\DateTimeHelper;
26 -use Tutor\Helpers\QueryHelper;
27 -use TUTOR\User;
28 -use TutorPro\Ecommerce\Invoice;
29 20
30 21 /**
31 22 * OrderModel Class
32 23 *
@@ -44,9 +35,8 @@
44 35 const ORDER_INCOMPLETE = 'incomplete';
45 36 const ORDER_COMPLETED = 'completed';
46 37 const ORDER_CANCELLED = 'cancelled';
47 38 const ORDER_TRASH = 'trash';
48 - const ORDER_PENDING = 'pending';
49 39
50 40 /**
51 41 * Payment status
52 42 *
@@ -58,9 +48,8 @@
58 48 const PAYMENT_FAILED = 'failed';
59 49 const PAYMENT_UNPAID = 'unpaid';
60 50 const PAYMENT_REFUNDED = 'refunded';
61 51 const PAYMENT_PARTIALLY_REFUNDED = 'partially-refunded';
62 - const PAYMENT_PENDING = 'pending';
63 52
64 53 /**
65 54 * Payment methods
66 55 *
@@ -212,9 +201,8 @@
212 201 /**
213 202 * Get recalculated order tax data.
214 203 *
215 204 * @since 3.4.0
216 - * @since 3.8.0 tax re-calculation based on pre_tax_price column value.
217 205 *
218 206 * @param int|object $order the order id or object.
219 207 *
220 208 * @return array
@@ -219,43 +207,24 @@
219 207 *
220 208 * @return array
221 209 */
222 210 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 - );
211 + $order = self::get_order( $order );
212 + $total_price = $order->total_price;
213 + $tax_rate = Tax::get_user_tax_rate( $order->user_id );
214 + $order_data = array();
215 + if ( $tax_rate ) {
216 + $order_data['tax_type'] = Tax::get_tax_type();
217 + $order_data['tax_rate'] = $tax_rate;
218 + $order_data['tax_amount'] = Tax::calculate_tax( $total_price, $tax_rate );
231 219
232 - if ( ! Tax::should_calculate_tax()
233 - || ! $tax_rate
234 - || ! $order
235 - || ! $order->pre_tax_price ) {
236 - return $order_data;
220 + if ( ! Tax::is_tax_included_in_price() ) {
221 + $total_price += $order_data['tax_amount'];
222 + $order_data['total_price'] = $total_price;
223 + $order_data['net_payment'] = $total_price;
224 + }
237 225 }
238 226
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 227 return $order_data;
259 228 }
260 229
261 230 /**
@@ -299,9 +268,8 @@
299 268 self::ORDER_INCOMPLETE => __( 'Incomplete', 'tutor' ),
300 269 self::ORDER_COMPLETED => __( 'Completed', 'tutor' ),
301 270 self::ORDER_CANCELLED => __( 'Cancelled', 'tutor' ),
302 271 self::ORDER_TRASH => __( 'Trash', 'tutor' ),
303 - self::ORDER_PENDING => __( 'Pending', 'tutor' ),
304 272 );
305 273 }
306 274
307 275 /**
@@ -332,9 +300,8 @@
332 300 self::PAYMENT_UNPAID => __( 'Unpaid', 'tutor' ),
333 301 self::PAYMENT_FAILED => __( 'Failed', 'tutor' ),
334 302 self::PAYMENT_REFUNDED => __( 'Refunded', 'tutor' ),
335 303 self::PAYMENT_PARTIALLY_REFUNDED => __( 'Partially Refunded', 'tutor' ),
336 - self::PAYMENT_PENDING => __( 'Pending', 'tutor' ),
337 304 );
338 305 }
339 306
340 307 /**
@@ -445,30 +412,21 @@
445 412 $items = array( $items );
446 413 }
447 414
448 415 // 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 - }
416 + foreach ( $items as $key => $item ) {
417 + $items[ $key ]['order_id'] = $order_id;
468 418 }
469 419
470 - return true;
420 + try {
421 + $insert = QueryHelper::insert_multiple_rows(
422 + $this->order_item_table,
423 + $items
424 + );
425 + return $insert ? true : false;
426 + } catch ( \Throwable $th ) {
427 + throw new Exception( $th->getMessage() );
428 + }
471 429 }
472 430
473 431 /**
474 432 * Retrieve order details by order ID.
@@ -500,13 +458,16 @@
500 458 return false;
501 459 }
502 460
503 461 $user_info = get_userdata( $order_data->user_id );
462 + if ( ! is_a( $user_info, 'WP_User' ) ) {
463 + return false;
464 + }
504 465
505 466 $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 ?? '';
467 + $student->id = (int) $user_info->ID;
468 + $student->name = $user_info->data->display_name;
469 + $student->email = $user_info->data->user_email;
509 470 $student->phone = get_user_meta( $order_data->user_id, 'phone_number', true );
510 471 $student->billing_address = $this->get_order_billing_address( $order_id, $order_data->user_id );
511 472 $student->image = get_avatar_url( $order_data->user_id );
512 473
@@ -531,13 +492,8 @@
531 492 $order_activities_model = new OrderActivitiesModel();
532 493 $order_data->activities = $order_activities_model->get_order_activities( $order_id );
533 494 $order_data->refunds = $this->get_order_refunds( $order_id );
534 495
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 496 unset( $student->billing_address->id );
541 497 unset( $student->billing_address->user_id );
542 498
543 499 return apply_filters( 'tutor_order_details', $order_data );
@@ -638,25 +594,22 @@
638 594 * items are found.
639 595 *
640 596 * @since 3.0.0
641 597 *
598 + * @global wpdb $wpdb WordPress database abstraction object.
599 + *
642 600 * @param int $order_id The ID of the order to retrieve items for.
643 601 *
644 602 * @return array The order items, each containing details and course titles, or an empty array if no items are found.
645 603 */
646 604 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 );
605 + global $wpdb;
649 606
650 - if ( $cached ) {
651 - return $cached;
652 - }
653 -
654 - $primary_table = 'tutor_order_items AS oi';
607 + $primary_table = "{$wpdb->prefix}tutor_order_items AS oi";
655 608 $joining_tables = array(
656 609 array(
657 610 'type' => 'LEFT',
658 - 'table' => 'posts AS p',
611 + 'table' => "{$wpdb->prefix}posts AS p",
659 612 'on' => 'p.ID = oi.item_id',
660 613 ),
661 614 );
662 615
@@ -661,18 +614,20 @@
661 614 );
662 615
663 616 $where = array( 'order_id' => $order_id );
664 617
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' );
618 + $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 619
667 620 $courses_data = QueryHelper::get_joined_data( $primary_table, $joining_tables, $select_columns, $where, array(), 'id', 0, 0 );
668 621 $courses = $courses_data['results'];
669 622
670 - $bundle_model = tutor()->has_pro ? new \TutorPro\CourseBundle\Models\BundleModel() : null;
623 + if ( tutor()->has_pro ) {
624 + $bundle_model = new \TutorPro\CourseBundle\Models\BundleModel();
625 + }
671 626
672 627 if ( ! empty( $courses_data['total_count'] ) ) {
673 628 foreach ( $courses as &$course ) {
674 - if ( is_object( $bundle_model ) && tutor()->bundle_post_type === $course->type ) {
629 + if ( tutor()->has_pro && 'course-bundle' === $course->type ) {
675 630 $course->total_courses = count( $bundle_model->get_bundle_course_ids( $course->id ) );
676 631 }
677 632
678 633 $course->id = (int) $course->id;
@@ -677,21 +632,14 @@
677 632
678 633 $course->id = (int) $course->id;
679 634 $course->regular_price = (float) $course->regular_price;
680 635 $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 636 }
686 637 }
687 638
688 639 unset( $course );
689 640
690 - $result = ! empty( $courses ) ? $courses : array();
691 - TutorCache::set( $cache_key, $result );
692 -
693 - return $result;
641 + return ! empty( $courses ) ? $courses : array();
694 642 }
695 643
696 644 /**
697 645 * Get order billing address with fallback customer billing address record support.
@@ -810,16 +758,14 @@
810 758 * @since 3.0.0
811 759 *
812 760 * @param int|array $order_id Integer or array of ids sql escaped.
813 761 * @param array $data Data to update, escape data.
814 - * @param array $order_items order items (optional).
815 762 *
816 763 * @return bool
817 764 */
818 - public function update_order( $order_id, array $data, array $order_items = array() ) {
765 + public function update_order( $order_id, array $data ) {
819 766 $order_id = is_array( $order_id ) ? $order_id : array( $order_id );
820 767 $order_id = QueryHelper::prepare_in_clause( $order_id );
821 -
822 768 try {
823 769 QueryHelper::update_where_in(
824 770 $this->table_name,
825 771 $data,
@@ -824,12 +770,8 @@
824 770 $this->table_name,
825 771 $data,
826 772 $order_id
827 773 );
828 -
829 - if ( ! empty( $order_items ) ) {
830 - $this->update_order_items( $order_id, $order_items );
831 - }
832 774 return true;
833 775 } catch ( \Throwable $th ) {
834 776 error_log( $th->getMessage() . ' in ' . $th->getFile() . ' at line ' . $th->getLine() );
835 777 return false;
@@ -853,9 +795,9 @@
853 795 $wpdb->prepare(
854 796 "SELECT * FROM {$wpdb->postmeta}
855 797 WHERE meta_key=%s
856 798 AND meta_value LIKE %d",
857 - EnrollmentModel::ENROLLMENT_ORDER_ID_META,
799 + '_tutor_enrolled_by_order_id',
858 800 $order_id
859 801 )
860 802 );
861 803
@@ -905,9 +847,9 @@
905 847 QueryHelper::delete(
906 848 $wpdb->prefix . 'tutor_earnings',
907 849 array(
908 850 'order_id' => $order_id,
909 - 'process_by' => Earnings::PROCESS_BY_TUTOR,
851 + 'process_by' => 'Tutor',
910 852 )
911 853 );
912 854
913 855 // Now delete order.
@@ -1007,30 +949,23 @@
1007 949 /**
1008 950 * Get order of a user
1009 951 *
1010 952 * @since 3.0.0
1011 - * @since 4.0.0 params $order_status, $order and $args added.
1012 953 *
1013 954 * @param string $time_period $time_period Sorting time period,
1014 955 * supported time periods are: today, monthly & yearly.
1015 956 * @param string $start_date $start_date For date range sorting.
1016 957 * @param string $end_date $end_date For date range sorting.
1017 - * @param string $order_status $order_status Order status.
1018 958 * @param int $user_id User id for fetching order list.
1019 959 * @param int $limit Limit to fetch record.
1020 960 * @param int $offset Offset to fetch record.
1021 - * @param string $order Order to fetch record.
1022 - * @param array $args Optional additional WHERE conditions.
1023 961 *
1024 962 * @throws \Exception Throw exception if database error occur.
1025 963 *
1026 964 * @return array
1027 965 */
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 = '';
966 + public function get_user_orders( $time_period = null, $start_date = null, $end_date = null, int $user_id = 0, $limit = 10, int $offset = 0 ) {
967 + $user_id = $user_id ? $user_id : get_current_user_id();
1033 968
1034 969 $response = array(
1035 970 'results' => array(),
1036 971 'total_count' => 0,
@@ -1037,11 +972,10 @@
1037 972 );
1038 973
1039 974 global $wpdb;
1040 975
1041 - $time_period_clause = '';
1042 - $date_range_clause = '';
1043 - $order_status_clause = ( empty( $order_status ) || 'all' === $order_status ) ? '' : "AND o.order_status = '{$order_status}'";
976 + $time_period_clause = '';
977 + $date_range_clause = '';
1044 978
1045 979 if ( $start_date && $end_date ) {
1046 980 $date_range_clause = $wpdb->prepare( 'AND DATE(created_at_gmt) BETWEEN %s AND %s', $start_date, $end_date );
1047 981 } elseif ( $time_period ) {
@@ -1053,12 +987,8 @@
1053 987 $time_period_clause = 'AND YEAR(o.created_at_gmt) = YEAR(CURDATE()) ';
1054 988 }
1055 989 }
1056 990
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 991 //phpcs:disable
1062 992 $query = $wpdb->prepare(
1063 993 "SELECT
1064 994 SQL_CALC_FOUND_ROWS
@@ -1064,13 +994,11 @@
1064 994 SQL_CALC_FOUND_ROWS
1065 995 o.*
1066 996 FROM $this->table_name AS o
1067 997 WHERE o.user_id = %d
1068 - {$order_type_clause}
1069 998 {$time_period_clause}
1070 999 {$date_range_clause}
1071 - {$order_status_clause}
1072 - ORDER BY o.id {$order}
1000 + ORDER BY o.id DESC
1073 1001 LIMIT %d OFFSET %d
1074 1002 ",
1075 1003 $user_id,
1076 1004 $limit,
@@ -1097,9 +1025,8 @@
1097 1025 *
1098 1026 * If period or date range not pass then it will return all time enrollment list
1099 1027 *
1100 1028 * @since 3.0.0
1101 - * @since 4.0.0 Minor query updates for the new graph.
1102 1029 *
1103 1030 * @param int $user_id User id, if user not have admin access
1104 1031 * then only this user's refund amount will fetched.
1105 1032 * @param string $period Time period.
@@ -1120,10 +1047,10 @@
1120 1047 $user_clause = '';
1121 1048 $date_range_clause = '';
1122 1049 $period_clause = '';
1123 1050 $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 ";
1051 + $group_clause = ' GROUP BY DATE(date_format) ';
1052 + $discount_clause = 'o.coupon_amount as total';
1126 1053
1127 1054 if ( $start_date && $end_date ) {
1128 1055 $date_range_clause = $wpdb->prepare(
1129 1056 'AND o.created_at_gmt BETWEEN %s AND %s',
@@ -1129,23 +1056,14 @@
1129 1056 'AND o.created_at_gmt BETWEEN %s AND %s',
1130 1057 $start_date,
1131 1058 $end_date
1132 1059 );
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 1060 } else {
1142 1061 $period_clause = QueryHelper::get_period_clause( 'o.created_at_gmt', $period );
1143 1062 }
1144 1063
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 ";
1064 + if ( 'today' !== $period ) {
1065 + $group_clause = ' GROUP BY MONTH(date_format) ';
1148 1066 }
1149 1067
1150 1068 if ( $course_id ) {
1151 1069 $course_clause = $wpdb->prepare( 'AND i.item_id = %d', $course_id );
@@ -1174,9 +1092,9 @@
1174 1092 ),
1175 1093 0
1176 1094 )
1177 1095 ) AS total,
1178 - {$select_query}
1096 + o.created_at_gmt AS date_format
1179 1097 FROM
1180 1098 {$this->table_name} o
1181 1099 JOIN
1182 1100 {$item_table} i ON o.id = i.order_id
@@ -1216,9 +1134,9 @@
1216 1134 ),
1217 1135 0
1218 1136 )
1219 1137 ) AS total,
1220 - {$select_query}
1138 + o.created_at_gmt AS date_format
1221 1139 FROM {$this->table_name} AS o
1222 1140 WHERE 1 = %d
1223 1141 AND o.order_status = 'completed'
1224 1142 {$user_clause}
@@ -1249,15 +1167,15 @@
1249 1167
1250 1168 // Split each discount.
1251 1169 list( $admin_discount, $instructor_discount ) = array_values( tutor_split_amounts( $discount->total ) );
1252 1170
1253 - $discount->total = User::is_admin() && is_admin() ? $admin_discount : $instructor_discount;
1171 + $discount->total = is_admin() ? $admin_discount : $instructor_discount;
1254 1172 }
1255 1173
1256 1174 list( $admin_total, $instructor_total ) = array_values( tutor_split_amounts( $total_discount ) );
1257 1175
1258 1176 $response['discounts'] = $discount_items;
1259 - $response['total_discounts'] = User::is_admin() && is_admin() ? $admin_total : $instructor_total;
1177 + $response['total_discounts'] = is_admin() ? $admin_total : $instructor_total;
1260 1178 }
1261 1179
1262 1180 return $response;
1263 1181 }
@@ -1269,9 +1187,8 @@
1269 1187 *
1270 1188 * If period or date range not pass then it will return all time enrollment list
1271 1189 *
1272 1190 * @since 3.0.0
1273 - * @since 4.0.0 Minor query updates for the new graph.
1274 1191 *
1275 1192 * @param int $user_id User id, if user not have admin access
1276 1193 * then only this user's refund amount will fetched.
1277 1194 * @param string $period Time period.
@@ -1291,31 +1208,26 @@
1291 1208
1292 1209 $user_clause = '';
1293 1210 $date_range_clause = '';
1294 1211 $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 ";
1212 + $course_clause = '';
1213 + $commission_clause = '';
1214 + $group_clause = ' GROUP BY DATE(o.created_at_gmt) ';
1297 1215
1298 1216 if ( $start_date && $end_date ) {
1299 1217 $date_range_clause = $wpdb->prepare(
1300 - 'AND DATE(order_meta.created_at_gmt) BETWEEN %s AND %s',
1218 + 'AND o.created_at_gmt BETWEEN %s AND %s',
1301 1219 $start_date,
1302 1220 $end_date
1303 1221 );
1222 + $group_clause = ' GROUP BY DATE(o.created_at_gmt) ';
1304 1223
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 1224 } else {
1312 - $period_clause = QueryHelper::get_period_clause( 'order_meta.created_at_gmt', $period );
1225 + $period_clause = QueryHelper::get_period_clause( 'o.created_at_gmt', $period );
1313 1226 }
1314 1227
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 ";
1228 + if ( 'today' !== $period ) {
1229 + $group_clause = ' GROUP BY MONTH(o.created_at_gmt) ';
1318 1230 }
1319 1231
1320 1232 if ( $course_id ) {
1321 1233 if ( $user_id ) {
@@ -1321,20 +1233,13 @@
1321 1233 if ( $user_id ) {
1322 1234 $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1323 1235 }
1324 1236 } elseif ( $user_id ) {
1325 - $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1237 + $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1326 1238 }
1327 1239
1328 1240 // 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 - );
1241 + $item_table = $wpdb->prefix . 'tutor_order_items';
1337 1242
1338 1243 if ( $course_id ) {
1339 1244 //phpcs:disable
1340 1245 $refunds = $wpdb->get_results(
@@ -1351,10 +1256,9 @@
1351 1256 ELSE i.regular_price
1352 1257 END / o.total_price
1353 1258 )
1354 1259 ), 2
1355 - ) AS total,
1356 - {$select_query}
1260 + ) AS total
1357 1261 FROM
1358 1262 {$this->table_name} o
1359 1263 JOIN
1360 1264 {$item_table} i ON o.id = i.order_id
@@ -1361,11 +1265,8 @@
1361 1265 JOIN
1362 1266 {$wpdb->posts} c
1363 1267 ON c.ID = i.item_id
1364 1268 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 1269 WHERE
1369 1270 o.refund_amount > 0
1370 1271 AND i.item_id = %d
1371 1272 {$user_clause}
@@ -1370,10 +1271,11 @@
1370 1271 AND i.item_id = %d
1371 1272 {$user_clause}
1372 1273 {$period_clause}
1373 1274 {$date_range_clause}
1374 - {$group_clause}
1375 - ORDER BY order_meta.created_at_gmt ASC",
1275 + {$group_clause},
1276 + i.item_id
1277 + ",
1376 1278 tutor()->course_post_type,
1377 1279 $course_id
1378 1280 )
1379 1281 );
@@ -1380,9 +1282,9 @@
1380 1282 //phpcs:enable
1381 1283 } else {
1382 1284 $earning_table = $wpdb->tutor_earnings;
1383 1285 if ( $user_id ) {
1384 - $user_clause = "AND {$user_id} = (SELECT user_id FROM {$earning_table} WHERE user_id = {$user_id} LIMIT 1)";
1286 + $user_clause = "AND {$user_id} = (SELECT user_id FROM {$earning_table} LIMIT 1)";
1385 1287 }
1386 1288
1387 1289 //phpcs:disable
1388 1290 $refunds = $wpdb->get_results(
@@ -1388,20 +1290,19 @@
1388 1290 $refunds = $wpdb->get_results(
1389 1291 $wpdb->prepare(
1390 1292 "SELECT
1391 1293 COALESCE(SUM(o.refund_amount), 0) AS total,
1392 - {$select_query}
1294 + created_at_gmt AS date_format
1393 1295 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})
1296 + -- LEFT JOIN {$item_table} AS i ON i.order_id = o.id
1297 + -- LEFT JOIN {$wpdb->posts} AS c ON c.id = i.item_id
1397 1298 WHERE 1 = %d
1398 1299 AND o.refund_amount > %d
1399 1300 {$user_clause}
1400 1301 {$period_clause}
1401 1302 {$date_range_clause}
1402 - {$group_clause}
1403 - ORDER BY order_meta.created_at_gmt ASC",
1303 + {$group_clause},
1304 + o.id",
1404 1305 1,
1405 1306 0
1406 1307 )
1407 1308 );
@@ -1414,9 +1315,9 @@
1414 1315 $total_refund += $refund->total;
1415 1316
1416 1317 // Update total amount from list.
1417 1318 $split_refund = (object) tutor_split_amounts( $refund->total );
1418 - $refund->total = User::is_admin() && is_admin() ? $split_refund->admin : $split_refund->instructor;
1319 + $refund->total = is_admin() ? $split_refund->admin : $split_refund->instructor;
1419 1320 }
1420 1321
1421 1322 $split_total_refund = (object) tutor_split_amounts( $total_refund );
1422 1323
@@ -1421,9 +1322,9 @@
1421 1322 $split_total_refund = (object) tutor_split_amounts( $total_refund );
1422 1323
1423 1324 $response = array(
1424 1325 'refunds' => $refunds,
1425 - 'total_refunds' => User::is_admin() && is_admin() ? $split_total_refund->admin : $split_total_refund->instructor,
1326 + 'total_refunds' => is_admin() ? $split_total_refund->admin : $split_total_refund->instructor,
1426 1327 );
1427 1328
1428 1329 return $response;
1429 1330 }
@@ -1847,10 +1748,10 @@
1847 1748 */
1848 1749 public static function should_show_pay_btn( object $order ) {
1849 1750 $order_items = ( new self() )->get_order_items_by_id( $order->id );
1850 1751 $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;
1752 + $is_incomplete_payment = ! empty( $order->payment_method ) && self::ORDER_INCOMPLETE === $order->order_status;
1753 + $is_manual_payment = $order->payment_method ? self::is_manual_payment( $order->payment_method ) : true;
1853 1754
1854 1755 if ( $is_incomplete_payment && ! $is_manual_payment && $order_items ) {
1855 1756 if ( self::TYPE_SINGLE_ORDER === $order->order_type ) {
1856 1757 foreach ( $order_items as $item ) {
@@ -1855,9 +1756,9 @@
1855 1756 if ( self::TYPE_SINGLE_ORDER === $order->order_type ) {
1856 1757 foreach ( $order_items as $item ) {
1857 1758 $course_id = $item->id;
1858 1759 if ( $course_id ) {
1859 - $is_enrolled = EnrollmentModel::is_enrolled( $course_id );
1760 + $is_enrolled = tutor_utils()->is_enrolled( $course_id );
1860 1761 if ( $is_enrolled ) {
1861 1762 $is_enrolled_any_course = true;
1862 1763 break;
1863 1764 }
@@ -1864,9 +1765,9 @@
1864 1765 }
1865 1766 }
1866 1767 } elseif ( tutor_utils()->count( $order_items ) ) {
1867 1768 $course_id = apply_filters( 'tutor_subscription_course_by_plan', $order_items[0]->id );
1868 - if ( EnrollmentModel::is_enrolled( $course_id ) ) {
1769 + if ( tutor_utils()->is_enrolled( $course_id ) ) {
1869 1770 $is_enrolled_any_course = true;
1870 1771 }
1871 1772 }
1872 1773 }
@@ -1904,12 +1805,10 @@
1904 1805 * @return void
1905 1806 */
1906 1807 public static function render_pay_button( $order ) {
1907 1808
1908 - $self = new self();
1909 -
1910 1809 if ( is_numeric( $order ) ) {
1911 - $order = $self->get_order_by_id( $order );
1810 + $order = ( new self() )->get_order_by_id( $order );
1912 1811 }
1913 1812
1914 1813 $show_pay_button = self::should_show_pay_btn( $order );
1915 1814
@@ -1922,9 +1821,21 @@
1922 1821 </div>
1923 1822 <?php
1924 1823 elseif ( $show_pay_button ) :
1925 1824 ob_start();
1926 - $self->pay_now_link( $order->id );
1825 + ?>
1826 +
1827 + <form method="post">
1828 + <?php tutor_nonce_field(); ?>
1829 + <input type="hidden" name="tutor_action" value="tutor_pay_incomplete_order">
1830 + <input type="hidden" name="order_id" value="<?php echo esc_attr( $order->id ); ?>">
1831 +
1832 + <button type="submit" class="tutor-btn tutor-btn-sm tutor-btn-outline-primary">
1833 + <?php esc_html_e( 'Pay', 'tutor' ); ?>
1834 + </button>
1835 + </form>
1836 +
1837 + <?php
1927 1838 echo apply_filters( 'tutor_after_pay_button', ob_get_clean(), $order );//phpcs:ignore --sanitized output.
1928 1839 endif;
1929 1840 }
1930 1841
@@ -1962,30 +1873,21 @@
1962 1873 /**
1963 1874 * Retrieves statements for a specific user.
1964 1875 *
1965 1876 * @since 3.5.0
1966 - * @since 4.0.0 Added $order_by and $order option.
1967 1877 *
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'.
1878 + * @param string $post_type_in_clause SQL clause to filter the course post types.
1879 + * @param string $course_query SQL query string to further filter the courses .
1880 + * @param string $date_query SQL query string to filter by date range.
1881 + * @param int $user_id The user ID for which the statements are being retrieved.
1882 + * @param int $offset The offset for pagination.
1883 + * @param int $limit The number of rows to return.
1976 1884 *
1977 1885 * @return array
1978 1886 */
1979 - public function get_statements( $post_type_in_clause, $course_query, $date_query, $user_id, $offset, $limit, $order_by, $order ): array {
1887 + public function get_statements( $post_type_in_clause, $course_query, $date_query, $user_id, $offset, $limit ): array {
1980 1888 global $wpdb;
1981 1889
1982 - $order_clause = '';
1983 -
1984 - if ( sanitize_sql_orderby( $order ) ) {
1985 - $order_clause = "ORDER BY {$order_by} {$order}";
1986 - }
1987 -
1988 1890 //phpcs:disable
1989 1891 $statements = $wpdb->get_results(
1990 1892 $wpdb->prepare(
1991 1893 "SELECT
@@ -2005,10 +1907,11 @@
2005 1907 AND course.post_type IN ({$post_type_in_clause})
2006 1908 WHERE statements.user_id = %d
2007 1909 {$course_query}
2008 1910 {$date_query}
2009 - {$order_clause}
2010 - LIMIT %d, %d",
1911 + ORDER BY statements.created_at DESC
1912 + LIMIT %d, %d
1913 + ",
2011 1914 $user_id,
2012 1915 $offset,
2013 1916 $limit
2014 1917 )
@@ -2021,9 +1924,10 @@
2021 1924 INNER JOIN {$wpdb->posts} AS course ON course.ID = statements.course_id
2022 1925 AND course.post_type IN ({$post_type_in_clause})
2023 1926 WHERE statements.user_id = %d
2024 1927 {$course_query}
2025 - {$date_query}",
1928 + {$date_query}
1929 + ",
2026 1930 $user_id
2027 1931 )
2028 1932 );
2029 1933 //phpcs:enable
@@ -2031,219 +1935,6 @@
2031 1935 return array(
2032 1936 'statements' => $statements,
2033 1937 'total_statements' => $total_statements,
2034 1938 );
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 1939 }
2249 1940 }