-
Notifications
You must be signed in to change notification settings - Fork 88
Query Optimization #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.0.0-dev
Are you sure you want to change the base?
Query Optimization #2799
Changes from all commits
f712f42
4a06b47
25bd13e
867794a
62901e8
5c251ca
fef2d00
506c81b
6afc3ed
0678d40
b2259f3
5ff43b7
757cbbb
96d7bd2
350d36a
2ec33df
35c9d4c
cc42349
798b8dd
89b0062
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1470,9 +1470,13 @@ public function get_course_first_lesson( $course_id = 0, $post_type = null ) { | |
| $course_id = $this->get_post_id( $course_id ); | ||
| $user_id = get_current_user_id(); | ||
|
|
||
| $lessons = $wpdb->get_results( | ||
| $wpdb->prepare( | ||
| "SELECT items.ID | ||
| $course_first_lesson_cache_key = 'tutor_course_first_lesson_' . $course_id; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if $post_type is provided? |
||
| $cache = TutorCache::get( $course_first_lesson_cache_key ); | ||
|
|
||
| if ( false === $cache ) { | ||
| $lessons = $wpdb->get_results( | ||
| $wpdb->prepare( | ||
| "SELECT items.ID | ||
| FROM {$wpdb->posts} topic | ||
| INNER JOIN {$wpdb->posts} items | ||
| ON topic.ID = items.post_parent | ||
|
|
@@ -1482,10 +1486,15 @@ public function get_course_first_lesson( $course_id = 0, $post_type = null ) { | |
| ORDER BY topic.menu_order ASC, | ||
| items.menu_order ASC; | ||
| ', | ||
| $course_id, | ||
| 'publish' | ||
| ) | ||
| ); | ||
| $course_id, | ||
| 'publish' | ||
| ) | ||
| ); | ||
|
|
||
| TutorCache::set( $course_first_lesson_cache_key, $lessons ); | ||
| } else { | ||
| $lessons = $cache; | ||
| } | ||
|
|
||
| $first_lesson = false; | ||
|
|
||
|
|
@@ -2108,6 +2117,12 @@ public function get_completed_courses_ids_by_user( $user_id = 0 ) { | |
|
|
||
| $user_id = $this->get_user_id( $user_id ); | ||
|
|
||
| $completed_courses_cache_key = 'tutor_completed_courses_ids_by_user_' . $user_id; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use like this pattern $cache_key = 'tutor_completed_courses_ids_by_user_' . $user_id;
$cached = TutorCache::get( $cache_key );
if ( false !== $cached ) {
return $cached;
} |
||
| $cache = TutorCache::get( $completed_courses_cache_key ); | ||
| if ( false !== $cache ) { | ||
| return $cache; | ||
| } | ||
|
|
||
| $course_ids = (array) $wpdb->get_col( | ||
| $wpdb->prepare( | ||
| "SELECT comment_post_ID AS course_id | ||
|
|
@@ -2127,6 +2142,8 @@ public function get_completed_courses_ids_by_user( $user_id = 0 ) { | |
| ) | ||
| ); | ||
|
|
||
| TutorCache::set( $completed_courses_cache_key, $course_ids ); | ||
|
|
||
| return $course_ids; | ||
| } | ||
|
|
||
|
|
@@ -2158,6 +2175,12 @@ public function get_enrolled_courses_ids_by_user( $user_id = 0, $with_bundle_enr | |
| ); | ||
| } | ||
|
|
||
| $enrolled_courses_cache_key = 'tutor_enrolled_courses_ids_by_user_' . $user_id . '_bundle_' . $with_bundle_enrolled_courses; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow the mentioned pattern above and use it immediate after $user_id |
||
| $cache = TutorCache::get( $enrolled_courses_cache_key ); | ||
| if ( false !== $cache ) { | ||
| return $cache; | ||
| } | ||
|
|
||
| $course_ids = $wpdb->get_col( | ||
| $wpdb->prepare( | ||
| "SELECT DISTINCT e.post_parent, e.post_date | ||
|
|
@@ -2174,6 +2197,8 @@ public function get_enrolled_courses_ids_by_user( $user_id = 0, $with_bundle_enr | |
| ) | ||
| ); | ||
|
|
||
| TutorCache::set( $enrolled_courses_cache_key, (array) $course_ids ); | ||
|
|
||
| return $course_ids; | ||
| } | ||
|
|
||
|
|
@@ -3253,17 +3278,25 @@ function ( $instructor ) use ( $main_instructor ) { | |
| public function get_total_students_by_instructor( $instructor_id, $args = array() ) { | ||
| global $wpdb; | ||
|
|
||
| $course_post_type = tutor()->course_post_type; | ||
| $enrollment_date_clause = ''; | ||
| $course_post_type = tutor()->course_post_type; | ||
| $enrollment_date_clause = ''; | ||
| $total_students_cache_key = __FUNCTION__ . "_{$instructor_id}"; | ||
|
|
||
| if ( ! empty( $args['from'] ) && ! empty( $args['to'] ) ) { | ||
| $from = Input::sanitize( $args['from'] ); | ||
| $to = Input::sanitize( $args['to'] ); | ||
|
|
||
| $total_students_cache_key .= "_{$from}_{$to}"; | ||
|
|
||
| $where['enrollment.post_date'] = array( 'BETWEEN', array( $from, $to ) ); | ||
| $enrollment_date_clause = ' AND ' . QueryHelper::prepare_where_clause( $where ); | ||
| } | ||
|
|
||
| $cached = TutorCache::get( $total_students_cache_key ); | ||
| if ( false !== $cached ) { | ||
| return $cached; | ||
| } | ||
|
|
||
| $count = $wpdb->get_var( | ||
| $wpdb->prepare( | ||
| "SELECT COUNT(DISTINCT(enrollment.post_author)) | ||
|
|
@@ -3284,6 +3317,8 @@ public function get_total_students_by_instructor( $instructor_id, $args = array( | |
| ) | ||
| ); | ||
|
|
||
| TutorCache::set( $total_students_cache_key, $count ); | ||
|
|
||
| return (int) $count; | ||
| } | ||
|
|
||
|
|
@@ -3484,9 +3519,17 @@ public function get_courses_by_student_instructor_id( int $student_id, int $inst | |
| */ | ||
| public function get_completed_assignment( int $course_id, int $student_id ): int { | ||
| global $wpdb; | ||
| $course_id = sanitize_text_field( $course_id ); | ||
| $student_id = sanitize_text_field( $student_id ); | ||
| $count = $wpdb->get_var( | ||
|
|
||
| $course_id = sanitize_text_field( $course_id ); | ||
| $student_id = sanitize_text_field( $student_id ); | ||
| $complete_assignment_cache_key = __FUNCTION__ . "_{$course_id}_{$student_id}"; | ||
|
|
||
| $cached = TutorCache::get( $complete_assignment_cache_key ); | ||
| if ( false !== $cached ) { | ||
| return $cached; | ||
| } | ||
|
|
||
| $count = $wpdb->get_var( | ||
| $wpdb->prepare( | ||
| "SELECT COUNT( DISTINCT ID ) FROM {$wpdb->posts} | ||
| INNER JOIN {$wpdb->comments} c ON c.comment_post_ID = ID AND c.user_id = %d AND c.comment_approved = %s | ||
|
|
@@ -3503,6 +3546,8 @@ public function get_completed_assignment( int $course_id, int $student_id ): int | |
| 'publish' | ||
| ) | ||
| ); | ||
|
|
||
| TutorCache::set( $complete_assignment_cache_key, (int) $count ); | ||
| return (int) $count; | ||
| } | ||
|
|
||
|
|
@@ -3518,9 +3563,16 @@ public function get_completed_assignment( int $course_id, int $student_id ): int | |
| */ | ||
| public function get_completed_quiz( int $course_id, int $student_id ): int { | ||
| global $wpdb; | ||
| $course_id = sanitize_text_field( $course_id ); | ||
| $student_id = sanitize_text_field( $student_id ); | ||
| $count = $wpdb->get_var( | ||
| $course_id = sanitize_text_field( $course_id ); | ||
| $student_id = sanitize_text_field( $student_id ); | ||
| $complete_quiz_cache_key = __FUNCTION__ . "_{$course_id}_{$student_id}"; | ||
|
|
||
| $cached = TutorCache::get( $complete_quiz_cache_key ); | ||
| if ( false !== $cached ) { | ||
| return $cached; | ||
| } | ||
|
|
||
| $count = $wpdb->get_var( | ||
| $wpdb->prepare( | ||
| "SELECT COUNT(DISTINCT quiz_id) AS total | ||
| FROM {$wpdb->prefix}tutor_quiz_attempts | ||
|
|
@@ -3533,6 +3585,8 @@ public function get_completed_quiz( int $course_id, int $student_id ): int { | |
| 'attempt_ended' | ||
| ) | ||
| ); | ||
|
|
||
| TutorCache::set( $complete_quiz_cache_key, (int) $count ); | ||
| return (int) $count; | ||
| } | ||
|
|
||
|
|
@@ -7265,6 +7319,12 @@ public function get_student_emails_by_course_id( $course_id = 0 ) { | |
| */ | ||
| public function get_single_comment_user_post_id( $post_id, $user_id ) { | ||
| global $wpdb; | ||
| $single_user_comment_cache_key = __FUNCTION__ . "_{$post_id}_{$user_id}"; | ||
|
|
||
| $cached = TutorCache::get( $single_user_comment_cache_key ); | ||
| if ( false !== $cached ) { | ||
| return $cached; | ||
| } | ||
| $table = $wpdb->prefix . 'comments'; | ||
| $query = $wpdb->get_row( | ||
| $wpdb->prepare( | ||
|
|
@@ -7278,6 +7338,7 @@ public function get_single_comment_user_post_id( $post_id, $user_id ) { | |
| $user_id | ||
| ) | ||
| ); | ||
| TutorCache::set( $single_user_comment_cache_key, $query ); | ||
| return $query ? $query : false; | ||
| } | ||
|
|
||
|
|
@@ -8465,17 +8526,26 @@ public function has_attempted_quiz( $user_id, $quiz_id, $row = false ) { | |
| // Sanitize data. | ||
| $user_id = sanitize_text_field( $user_id ); | ||
| $quiz_id = sanitize_text_field( $quiz_id ); | ||
| $cache_key = 'tutor_has_attempted_quiz_' . $user_id . '_' . $quiz_id; | ||
| $cache = TutorCache::get( $cache_key ); | ||
|
|
||
| if ( false !== $cache ) { | ||
| return $cache ? true : false; | ||
| } | ||
|
|
||
| $attempted = $wpdb->get_row( | ||
| $wpdb->prepare( | ||
| "SELECT quiz_id | ||
| FROM {$wpdb->tutor_quiz_attempts} | ||
| WHERE user_id = %d | ||
| AND quiz_id = %d | ||
| ", | ||
| AND quiz_id = %d", | ||
| $user_id, | ||
| $quiz_id | ||
| ) | ||
| ); | ||
|
|
||
| TutorCache::set( $cache_key, $attempted ); | ||
|
|
||
| return $attempted ? true : false; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -463,7 +463,7 @@ public function prepare_checkout_items( $item_ids, $order_type = OrderModel::TYP | |
|
|
||
| $should_calculate_tax = Tax::should_calculate_tax(); | ||
| $tax_included = Tax::is_tax_included_in_price(); | ||
| $tax_rate = Tax::get_user_tax_rate(); | ||
| $tax_rate = $should_calculate_tax ? Tax::get_user_tax_rate() : ''; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rate should not be empty string |
||
|
|
||
| // Keep calculated price for each item. | ||
| foreach ( $items as $item ) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why need this dependency?
We have
$tutor_is_course_completedglobal