PluginProbe
Tutor LMS – eLearning and online course solution / 3.8.2
Tutor LMS – eLearning and online course solution v3.8.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 +87 -285 trunk3.8.2 View file →
@@ -9,24 +9,16 @@
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 14 use TUTOR\Earnings;
15 +use Tutor\Ecommerce\Tax;
16 +use Tutor\Ecommerce\Ecommerce;
17 +use Tutor\Helpers\QueryHelper;
18 +use Tutor\Helpers\DateTimeHelper;
20 19 use Tutor\Ecommerce\BillingController;
21 -use Tutor\Ecommerce\CheckoutController;
22 -use Tutor\Ecommerce\Ecommerce;
23 20 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 21
30 22 /**
31 23 * OrderModel Class
32 24 *
@@ -44,9 +36,8 @@
44 36 const ORDER_INCOMPLETE = 'incomplete';
45 37 const ORDER_COMPLETED = 'completed';
46 38 const ORDER_CANCELLED = 'cancelled';
47 39 const ORDER_TRASH = 'trash';
48 - const ORDER_PENDING = 'pending';
49 40
50 41 /**
51 42 * Payment status
52 43 *
@@ -58,9 +49,8 @@
58 49 const PAYMENT_FAILED = 'failed';
59 50 const PAYMENT_UNPAID = 'unpaid';
60 51 const PAYMENT_REFUNDED = 'refunded';
61 52 const PAYMENT_PARTIALLY_REFUNDED = 'partially-refunded';
62 - const PAYMENT_PENDING = 'pending';
63 53
64 54 /**
65 55 * Payment methods
66 56 *
@@ -299,9 +289,8 @@
299 289 self::ORDER_INCOMPLETE => __( 'Incomplete', 'tutor' ),
300 290 self::ORDER_COMPLETED => __( 'Completed', 'tutor' ),
301 291 self::ORDER_CANCELLED => __( 'Cancelled', 'tutor' ),
302 292 self::ORDER_TRASH => __( 'Trash', 'tutor' ),
303 - self::ORDER_PENDING => __( 'Pending', 'tutor' ),
304 293 );
305 294 }
306 295
307 296 /**
@@ -332,9 +321,8 @@
332 321 self::PAYMENT_UNPAID => __( 'Unpaid', 'tutor' ),
333 322 self::PAYMENT_FAILED => __( 'Failed', 'tutor' ),
334 323 self::PAYMENT_REFUNDED => __( 'Refunded', 'tutor' ),
335 324 self::PAYMENT_PARTIALLY_REFUNDED => __( 'Partially Refunded', 'tutor' ),
336 - self::PAYMENT_PENDING => __( 'Pending', 'tutor' ),
337 325 );
338 326 }
339 327
340 328 /**
@@ -500,13 +488,16 @@
500 488 return false;
501 489 }
502 490
503 491 $user_info = get_userdata( $order_data->user_id );
492 + if ( ! is_a( $user_info, 'WP_User' ) ) {
493 + return false;
494 + }
504 495
505 496 $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 ?? '';
497 + $student->id = (int) $user_info->ID;
498 + $student->name = $user_info->data->display_name;
499 + $student->email = $user_info->data->user_email;
509 500 $student->phone = get_user_meta( $order_data->user_id, 'phone_number', true );
510 501 $student->billing_address = $this->get_order_billing_address( $order_id, $order_data->user_id );
511 502 $student->image = get_avatar_url( $order_data->user_id );
512 503
@@ -531,13 +522,8 @@
531 522 $order_activities_model = new OrderActivitiesModel();
532 523 $order_data->activities = $order_activities_model->get_order_activities( $order_id );
533 524 $order_data->refunds = $this->get_order_refunds( $order_id );
534 525
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 526 unset( $student->billing_address->id );
541 527 unset( $student->billing_address->user_id );
542 528
543 529 return apply_filters( 'tutor_order_details', $order_data );
@@ -638,25 +624,22 @@
638 624 * items are found.
639 625 *
640 626 * @since 3.0.0
641 627 *
628 + * @global wpdb $wpdb WordPress database abstraction object.
629 + *
642 630 * @param int $order_id The ID of the order to retrieve items for.
643 631 *
644 632 * @return array The order items, each containing details and course titles, or an empty array if no items are found.
645 633 */
646 634 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 );
635 + global $wpdb;
649 636
650 - if ( $cached ) {
651 - return $cached;
652 - }
653 -
654 - $primary_table = 'tutor_order_items AS oi';
637 + $primary_table = "{$wpdb->prefix}tutor_order_items AS oi";
655 638 $joining_tables = array(
656 639 array(
657 640 'type' => 'LEFT',
658 - 'table' => 'posts AS p',
641 + 'table' => "{$wpdb->prefix}posts AS p",
659 642 'on' => 'p.ID = oi.item_id',
660 643 ),
661 644 );
662 645
@@ -666,13 +649,15 @@
666 649
667 650 $courses_data = QueryHelper::get_joined_data( $primary_table, $joining_tables, $select_columns, $where, array(), 'id', 0, 0 );
668 651 $courses = $courses_data['results'];
669 652
670 - $bundle_model = tutor()->has_pro ? new \TutorPro\CourseBundle\Models\BundleModel() : null;
653 + if ( tutor()->has_pro ) {
654 + $bundle_model = new \TutorPro\CourseBundle\Models\BundleModel();
655 + }
671 656
672 657 if ( ! empty( $courses_data['total_count'] ) ) {
673 658 foreach ( $courses as &$course ) {
674 - if ( is_object( $bundle_model ) && tutor()->bundle_post_type === $course->type ) {
659 + if ( tutor()->has_pro && 'course-bundle' === $course->type ) {
675 660 $course->total_courses = count( $bundle_model->get_bundle_course_ids( $course->id ) );
676 661 }
677 662
678 663 $course->id = (int) $course->id;
@@ -686,12 +671,9 @@
686 671 }
687 672
688 673 unset( $course );
689 674
690 - $result = ! empty( $courses ) ? $courses : array();
691 - TutorCache::set( $cache_key, $result );
692 -
693 - return $result;
675 + return ! empty( $courses ) ? $courses : array();
694 676 }
695 677
696 678 /**
697 679 * Get order billing address with fallback customer billing address record support.
@@ -810,16 +792,14 @@
810 792 * @since 3.0.0
811 793 *
812 794 * @param int|array $order_id Integer or array of ids sql escaped.
813 795 * @param array $data Data to update, escape data.
814 - * @param array $order_items order items (optional).
815 796 *
816 797 * @return bool
817 798 */
818 - public function update_order( $order_id, array $data, array $order_items = array() ) {
799 + public function update_order( $order_id, array $data ) {
819 800 $order_id = is_array( $order_id ) ? $order_id : array( $order_id );
820 801 $order_id = QueryHelper::prepare_in_clause( $order_id );
821 -
822 802 try {
823 803 QueryHelper::update_where_in(
824 804 $this->table_name,
825 805 $data,
@@ -824,12 +804,8 @@
824 804 $this->table_name,
825 805 $data,
826 806 $order_id
827 807 );
828 -
829 - if ( ! empty( $order_items ) ) {
830 - $this->update_order_items( $order_id, $order_items );
831 - }
832 808 return true;
833 809 } catch ( \Throwable $th ) {
834 810 error_log( $th->getMessage() . ' in ' . $th->getFile() . ' at line ' . $th->getLine() );
835 811 return false;
@@ -853,9 +829,9 @@
853 829 $wpdb->prepare(
854 830 "SELECT * FROM {$wpdb->postmeta}
855 831 WHERE meta_key=%s
856 832 AND meta_value LIKE %d",
857 - EnrollmentModel::ENROLLMENT_ORDER_ID_META,
833 + '_tutor_enrolled_by_order_id',
858 834 $order_id
859 835 )
860 836 );
861 837
@@ -1007,30 +983,23 @@
1007 983 /**
1008 984 * Get order of a user
1009 985 *
1010 986 * @since 3.0.0
1011 - * @since 4.0.0 params $order_status, $order and $args added.
1012 987 *
1013 988 * @param string $time_period $time_period Sorting time period,
1014 989 * supported time periods are: today, monthly & yearly.
1015 990 * @param string $start_date $start_date For date range sorting.
1016 991 * @param string $end_date $end_date For date range sorting.
1017 - * @param string $order_status $order_status Order status.
1018 992 * @param int $user_id User id for fetching order list.
1019 993 * @param int $limit Limit to fetch record.
1020 994 * @param int $offset Offset to fetch record.
1021 - * @param string $order Order to fetch record.
1022 - * @param array $args Optional additional WHERE conditions.
1023 995 *
1024 996 * @throws \Exception Throw exception if database error occur.
1025 997 *
1026 998 * @return array
1027 999 */
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 = '';
1000 + public function get_user_orders( $time_period = null, $start_date = null, $end_date = null, int $user_id = 0, $limit = 10, int $offset = 0 ) {
1001 + $user_id = $user_id ? $user_id : get_current_user_id();
1033 1002
1034 1003 $response = array(
1035 1004 'results' => array(),
1036 1005 'total_count' => 0,
@@ -1037,11 +1006,10 @@
1037 1006 );
1038 1007
1039 1008 global $wpdb;
1040 1009
1041 - $time_period_clause = '';
1042 - $date_range_clause = '';
1043 - $order_status_clause = ( empty( $order_status ) || 'all' === $order_status ) ? '' : "AND o.order_status = '{$order_status}'";
1010 + $time_period_clause = '';
1011 + $date_range_clause = '';
1044 1012
1045 1013 if ( $start_date && $end_date ) {
1046 1014 $date_range_clause = $wpdb->prepare( 'AND DATE(created_at_gmt) BETWEEN %s AND %s', $start_date, $end_date );
1047 1015 } elseif ( $time_period ) {
@@ -1053,12 +1021,8 @@
1053 1021 $time_period_clause = 'AND YEAR(o.created_at_gmt) = YEAR(CURDATE()) ';
1054 1022 }
1055 1023 }
1056 1024
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 1025 //phpcs:disable
1062 1026 $query = $wpdb->prepare(
1063 1027 "SELECT
1064 1028 SQL_CALC_FOUND_ROWS
@@ -1064,13 +1028,11 @@
1064 1028 SQL_CALC_FOUND_ROWS
1065 1029 o.*
1066 1030 FROM $this->table_name AS o
1067 1031 WHERE o.user_id = %d
1068 - {$order_type_clause}
1069 1032 {$time_period_clause}
1070 1033 {$date_range_clause}
1071 - {$order_status_clause}
1072 - ORDER BY o.id {$order}
1034 + ORDER BY o.id DESC
1073 1035 LIMIT %d OFFSET %d
1074 1036 ",
1075 1037 $user_id,
1076 1038 $limit,
@@ -1097,9 +1059,8 @@
1097 1059 *
1098 1060 * If period or date range not pass then it will return all time enrollment list
1099 1061 *
1100 1062 * @since 3.0.0
1101 - * @since 4.0.0 Minor query updates for the new graph.
1102 1063 *
1103 1064 * @param int $user_id User id, if user not have admin access
1104 1065 * then only this user's refund amount will fetched.
1105 1066 * @param string $period Time period.
@@ -1120,10 +1081,10 @@
1120 1081 $user_clause = '';
1121 1082 $date_range_clause = '';
1122 1083 $period_clause = '';
1123 1084 $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 ";
1085 + $group_clause = ' GROUP BY DATE(date_format) ';
1086 + $discount_clause = 'o.coupon_amount as total';
1126 1087
1127 1088 if ( $start_date && $end_date ) {
1128 1089 $date_range_clause = $wpdb->prepare(
1129 1090 'AND o.created_at_gmt BETWEEN %s AND %s',
@@ -1129,23 +1090,14 @@
1129 1090 'AND o.created_at_gmt BETWEEN %s AND %s',
1130 1091 $start_date,
1131 1092 $end_date
1132 1093 );
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 1094 } else {
1142 1095 $period_clause = QueryHelper::get_period_clause( 'o.created_at_gmt', $period );
1143 1096 }
1144 1097
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 ";
1098 + if ( 'today' !== $period ) {
1099 + $group_clause = ' GROUP BY MONTH(date_format) ';
1148 1100 }
1149 1101
1150 1102 if ( $course_id ) {
1151 1103 $course_clause = $wpdb->prepare( 'AND i.item_id = %d', $course_id );
@@ -1174,9 +1126,9 @@
1174 1126 ),
1175 1127 0
1176 1128 )
1177 1129 ) AS total,
1178 - {$select_query}
1130 + o.created_at_gmt AS date_format
1179 1131 FROM
1180 1132 {$this->table_name} o
1181 1133 JOIN
1182 1134 {$item_table} i ON o.id = i.order_id
@@ -1216,9 +1168,9 @@
1216 1168 ),
1217 1169 0
1218 1170 )
1219 1171 ) AS total,
1220 - {$select_query}
1172 + o.created_at_gmt AS date_format
1221 1173 FROM {$this->table_name} AS o
1222 1174 WHERE 1 = %d
1223 1175 AND o.order_status = 'completed'
1224 1176 {$user_clause}
@@ -1249,15 +1201,15 @@
1249 1201
1250 1202 // Split each discount.
1251 1203 list( $admin_discount, $instructor_discount ) = array_values( tutor_split_amounts( $discount->total ) );
1252 1204
1253 - $discount->total = User::is_admin() && is_admin() ? $admin_discount : $instructor_discount;
1205 + $discount->total = is_admin() ? $admin_discount : $instructor_discount;
1254 1206 }
1255 1207
1256 1208 list( $admin_total, $instructor_total ) = array_values( tutor_split_amounts( $total_discount ) );
1257 1209
1258 1210 $response['discounts'] = $discount_items;
1259 - $response['total_discounts'] = User::is_admin() && is_admin() ? $admin_total : $instructor_total;
1211 + $response['total_discounts'] = is_admin() ? $admin_total : $instructor_total;
1260 1212 }
1261 1213
1262 1214 return $response;
1263 1215 }
@@ -1269,9 +1221,8 @@
1269 1221 *
1270 1222 * If period or date range not pass then it will return all time enrollment list
1271 1223 *
1272 1224 * @since 3.0.0
1273 - * @since 4.0.0 Minor query updates for the new graph.
1274 1225 *
1275 1226 * @param int $user_id User id, if user not have admin access
1276 1227 * then only this user's refund amount will fetched.
1277 1228 * @param string $period Time period.
@@ -1291,31 +1242,26 @@
1291 1242
1292 1243 $user_clause = '';
1293 1244 $date_range_clause = '';
1294 1245 $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 ";
1246 + $course_clause = '';
1247 + $commission_clause = '';
1248 + $group_clause = ' GROUP BY DATE(o.created_at_gmt) ';
1297 1249
1298 1250 if ( $start_date && $end_date ) {
1299 1251 $date_range_clause = $wpdb->prepare(
1300 - 'AND DATE(order_meta.created_at_gmt) BETWEEN %s AND %s',
1252 + 'AND o.created_at_gmt BETWEEN %s AND %s',
1301 1253 $start_date,
1302 1254 $end_date
1303 1255 );
1256 + $group_clause = ' GROUP BY DATE(o.created_at_gmt) ';
1304 1257
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 1258 } else {
1312 - $period_clause = QueryHelper::get_period_clause( 'order_meta.created_at_gmt', $period );
1259 + $period_clause = QueryHelper::get_period_clause( 'o.created_at_gmt', $period );
1313 1260 }
1314 1261
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 ";
1262 + if ( 'today' !== $period ) {
1263 + $group_clause = ' GROUP BY MONTH(o.created_at_gmt) ';
1318 1264 }
1319 1265
1320 1266 if ( $course_id ) {
1321 1267 if ( $user_id ) {
@@ -1321,20 +1267,13 @@
1321 1267 if ( $user_id ) {
1322 1268 $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1323 1269 }
1324 1270 } elseif ( $user_id ) {
1325 - $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1271 + $user_clause = $wpdb->prepare( 'AND c.post_author = %d', $user_id );
1326 1272 }
1327 1273
1328 1274 // 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 - );
1275 + $item_table = $wpdb->prefix . 'tutor_order_items';
1337 1276
1338 1277 if ( $course_id ) {
1339 1278 //phpcs:disable
1340 1279 $refunds = $wpdb->get_results(
@@ -1351,10 +1290,9 @@
1351 1290 ELSE i.regular_price
1352 1291 END / o.total_price
1353 1292 )
1354 1293 ), 2
1355 - ) AS total,
1356 - {$select_query}
1294 + ) AS total
1357 1295 FROM
1358 1296 {$this->table_name} o
1359 1297 JOIN
1360 1298 {$item_table} i ON o.id = i.order_id
@@ -1361,11 +1299,8 @@
1361 1299 JOIN
1362 1300 {$wpdb->posts} c
1363 1301 ON c.ID = i.item_id
1364 1302 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 1303 WHERE
1369 1304 o.refund_amount > 0
1370 1305 AND i.item_id = %d
1371 1306 {$user_clause}
@@ -1370,10 +1305,11 @@
1370 1305 AND i.item_id = %d
1371 1306 {$user_clause}
1372 1307 {$period_clause}
1373 1308 {$date_range_clause}
1374 - {$group_clause}
1375 - ORDER BY order_meta.created_at_gmt ASC",
1309 + {$group_clause},
1310 + i.item_id
1311 + ",
1376 1312 tutor()->course_post_type,
1377 1313 $course_id
1378 1314 )
1379 1315 );
@@ -1380,9 +1316,9 @@
1380 1316 //phpcs:enable
1381 1317 } else {
1382 1318 $earning_table = $wpdb->tutor_earnings;
1383 1319 if ( $user_id ) {
1384 - $user_clause = "AND {$user_id} = (SELECT user_id FROM {$earning_table} WHERE user_id = {$user_id} LIMIT 1)";
1320 + $user_clause = "AND {$user_id} = (SELECT user_id FROM {$earning_table} LIMIT 1)";
1385 1321 }
1386 1322
1387 1323 //phpcs:disable
1388 1324 $refunds = $wpdb->get_results(
@@ -1388,20 +1324,19 @@
1388 1324 $refunds = $wpdb->get_results(
1389 1325 $wpdb->prepare(
1390 1326 "SELECT
1391 1327 COALESCE(SUM(o.refund_amount), 0) AS total,
1392 - {$select_query}
1328 + created_at_gmt AS date_format
1393 1329 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})
1330 + -- LEFT JOIN {$item_table} AS i ON i.order_id = o.id
1331 + -- LEFT JOIN {$wpdb->posts} AS c ON c.id = i.item_id
1397 1332 WHERE 1 = %d
1398 1333 AND o.refund_amount > %d
1399 1334 {$user_clause}
1400 1335 {$period_clause}
1401 1336 {$date_range_clause}
1402 - {$group_clause}
1403 - ORDER BY order_meta.created_at_gmt ASC",
1337 + {$group_clause},
1338 + o.id",
1404 1339 1,
1405 1340 0
1406 1341 )
1407 1342 );
@@ -1414,9 +1349,9 @@
1414 1349 $total_refund += $refund->total;
1415 1350
1416 1351 // Update total amount from list.
1417 1352 $split_refund = (object) tutor_split_amounts( $refund->total );
1418 - $refund->total = User::is_admin() && is_admin() ? $split_refund->admin : $split_refund->instructor;
1353 + $refund->total = is_admin() ? $split_refund->admin : $split_refund->instructor;
1419 1354 }
1420 1355
1421 1356 $split_total_refund = (object) tutor_split_amounts( $total_refund );
1422 1357
@@ -1421,9 +1356,9 @@
1421 1356 $split_total_refund = (object) tutor_split_amounts( $total_refund );
1422 1357
1423 1358 $response = array(
1424 1359 'refunds' => $refunds,
1425 - 'total_refunds' => User::is_admin() && is_admin() ? $split_total_refund->admin : $split_total_refund->instructor,
1360 + 'total_refunds' => is_admin() ? $split_total_refund->admin : $split_total_refund->instructor,
1426 1361 );
1427 1362
1428 1363 return $response;
1429 1364 }
@@ -1847,10 +1782,10 @@
1847 1782 */
1848 1783 public static function should_show_pay_btn( object $order ) {
1849 1784 $order_items = ( new self() )->get_order_items_by_id( $order->id );
1850 1785 $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;
1786 + $is_incomplete_payment = ! empty( $order->payment_method ) && self::ORDER_INCOMPLETE === $order->order_status;
1787 + $is_manual_payment = $order->payment_method ? self::is_manual_payment( $order->payment_method ) : true;
1853 1788
1854 1789 if ( $is_incomplete_payment && ! $is_manual_payment && $order_items ) {
1855 1790 if ( self::TYPE_SINGLE_ORDER === $order->order_type ) {
1856 1791 foreach ( $order_items as $item ) {
@@ -1855,9 +1790,9 @@
1855 1790 if ( self::TYPE_SINGLE_ORDER === $order->order_type ) {
1856 1791 foreach ( $order_items as $item ) {
1857 1792 $course_id = $item->id;
1858 1793 if ( $course_id ) {
1859 - $is_enrolled = EnrollmentModel::is_enrolled( $course_id );
1794 + $is_enrolled = tutor_utils()->is_enrolled( $course_id );
1860 1795 if ( $is_enrolled ) {
1861 1796 $is_enrolled_any_course = true;
1862 1797 break;
1863 1798 }
@@ -1864,9 +1799,9 @@
1864 1799 }
1865 1800 }
1866 1801 } elseif ( tutor_utils()->count( $order_items ) ) {
1867 1802 $course_id = apply_filters( 'tutor_subscription_course_by_plan', $order_items[0]->id );
1868 - if ( EnrollmentModel::is_enrolled( $course_id ) ) {
1803 + if ( tutor_utils()->is_enrolled( $course_id ) ) {
1869 1804 $is_enrolled_any_course = true;
1870 1805 }
1871 1806 }
1872 1807 }
@@ -1904,12 +1839,10 @@
1904 1839 * @return void
1905 1840 */
1906 1841 public static function render_pay_button( $order ) {
1907 1842
1908 - $self = new self();
1909 -
1910 1843 if ( is_numeric( $order ) ) {
1911 - $order = $self->get_order_by_id( $order );
1844 + $order = ( new self() )->get_order_by_id( $order );
1912 1845 }
1913 1846
1914 1847 $show_pay_button = self::should_show_pay_btn( $order );
1915 1848
@@ -1922,9 +1855,21 @@
1922 1855 </div>
1923 1856 <?php
1924 1857 elseif ( $show_pay_button ) :
1925 1858 ob_start();
1926 - $self->pay_now_link( $order->id );
1859 + ?>
1860 +
1861 + <form method="post">
1862 + <?php tutor_nonce_field(); ?>
1863 + <input type="hidden" name="tutor_action" value="tutor_pay_incomplete_order">
1864 + <input type="hidden" name="order_id" value="<?php echo esc_attr( $order->id ); ?>">
1865 +
1866 + <button type="submit" class="tutor-btn tutor-btn-sm tutor-btn-outline-primary">
1867 + <?php esc_html_e( 'Pay', 'tutor' ); ?>
1868 + </button>
1869 + </form>
1870 +
1871 + <?php
1927 1872 echo apply_filters( 'tutor_after_pay_button', ob_get_clean(), $order );//phpcs:ignore --sanitized output.
1928 1873 endif;
1929 1874 }
1930 1875
@@ -1962,30 +1907,21 @@
1962 1907 /**
1963 1908 * Retrieves statements for a specific user.
1964 1909 *
1965 1910 * @since 3.5.0
1966 - * @since 4.0.0 Added $order_by and $order option.
1967 1911 *
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'.
1912 + * @param string $post_type_in_clause SQL clause to filter the course post types.
1913 + * @param string $course_query SQL query string to further filter the courses .
1914 + * @param string $date_query SQL query string to filter by date range.
1915 + * @param int $user_id The user ID for which the statements are being retrieved.
1916 + * @param int $offset The offset for pagination.
1917 + * @param int $limit The number of rows to return.
1976 1918 *
1977 1919 * @return array
1978 1920 */
1979 - public function get_statements( $post_type_in_clause, $course_query, $date_query, $user_id, $offset, $limit, $order_by, $order ): array {
1921 + public function get_statements( $post_type_in_clause, $course_query, $date_query, $user_id, $offset, $limit ): array {
1980 1922 global $wpdb;
1981 1923
1982 - $order_clause = '';
1983 -
1984 - if ( sanitize_sql_orderby( $order ) ) {
1985 - $order_clause = "ORDER BY {$order_by} {$order}";
1986 - }
1987 -
1988 1924 //phpcs:disable
1989 1925 $statements = $wpdb->get_results(
1990 1926 $wpdb->prepare(
1991 1927 "SELECT
@@ -2005,10 +1941,11 @@
2005 1941 AND course.post_type IN ({$post_type_in_clause})
2006 1942 WHERE statements.user_id = %d
2007 1943 {$course_query}
2008 1944 {$date_query}
2009 - {$order_clause}
2010 - LIMIT %d, %d",
1945 + ORDER BY statements.created_at DESC
1946 + LIMIT %d, %d
1947 + ",
2011 1948 $user_id,
2012 1949 $offset,
2013 1950 $limit
2014 1951 )
@@ -2021,9 +1958,10 @@
2021 1958 INNER JOIN {$wpdb->posts} AS course ON course.ID = statements.course_id
2022 1959 AND course.post_type IN ({$post_type_in_clause})
2023 1960 WHERE statements.user_id = %d
2024 1961 {$course_query}
2025 - {$date_query}",
1962 + {$date_query}
1963 + ",
2026 1964 $user_id
2027 1965 )
2028 1966 );
2029 1967 //phpcs:enable
@@ -2108,142 +2046,6 @@
2108 2046 $where['course_id'] = $course_id;
2109 2047 }
2110 2048
2111 2049 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 2050 }
2249 2051 }