1111namespace Tutor \Models ;
1212
1313use Exception ;
14+ use Tutor \Cache \TutorCache ;
1415use Tutor \Components \Badge ;
1516use Tutor \Components \Button ;
1617use Tutor \Components \Constants \Variant ;
@@ -632,20 +633,23 @@ public function mark_as_paid( $order_id, $note = '', $trigger_hooks = true ) {
632633 *
633634 * @since 3.0.0
634635 *
635- * @global wpdb $wpdb WordPress database abstraction object.
636- *
637636 * @param int $order_id The ID of the order to retrieve items for.
638637 *
639638 * @return array The order items, each containing details and course titles, or an empty array if no items are found.
640639 */
641640 public function get_order_items_by_id ( $ order_id ) {
642- global $ wpdb ;
641+ $ cache_key = 'tutor_get_order_items_by_id_ ' . $ order_id ;
642+ $ cached = TutorCache::get ( $ cache_key );
643643
644- $ primary_table = "{$ wpdb ->prefix }tutor_order_items AS oi " ;
644+ if ( $ cached ) {
645+ return $ cached ;
646+ }
647+
648+ $ primary_table = 'tutor_order_items AS oi ' ;
645649 $ joining_tables = array (
646650 array (
647651 'type ' => 'LEFT ' ,
648- 'table ' => "{ $ wpdb -> prefix } posts AS p " ,
652+ 'table ' => ' posts AS p ' ,
649653 'on ' => 'p.ID = oi.item_id ' ,
650654 ),
651655 );
@@ -657,13 +661,11 @@ public function get_order_items_by_id( $order_id ) {
657661 $ courses_data = QueryHelper::get_joined_data ( $ primary_table , $ joining_tables , $ select_columns , $ where , array (), 'id ' , 0 , 0 );
658662 $ courses = $ courses_data ['results ' ];
659663
660- if ( tutor ()->has_pro ) {
661- $ bundle_model = new \TutorPro \CourseBundle \Models \BundleModel ();
662- }
664+ $ bundle_model = tutor ()->has_pro ? new \TutorPro \CourseBundle \Models \BundleModel () : null ;
663665
664666 if ( ! empty ( $ courses_data ['total_count ' ] ) ) {
665667 foreach ( $ courses as &$ course ) {
666- if ( tutor ()-> has_pro && ' course-bundle ' === $ course ->type ) {
668+ if ( is_object ( $ bundle_model ) && tutor ()-> bundle_post_type === $ course ->type ) {
667669 $ course ->total_courses = count ( $ bundle_model ->get_bundle_course_ids ( $ course ->id ) );
668670 }
669671
@@ -679,7 +681,10 @@ public function get_order_items_by_id( $order_id ) {
679681
680682 unset( $ course );
681683
682- return ! empty ( $ courses ) ? $ courses : array ();
684+ $ result = ! empty ( $ courses ) ? $ courses : array ();
685+ TutorCache::set ( $ cache_key , $ result );
686+
687+ return $ result ;
683688 }
684689
685690 /**
0 commit comments