Skip to content

Commit 6089d49

Browse files
authored
Merge pull request #2703 from themeum/query-optimize
Query optimize
2 parents 8e8819a + d536aa4 commit 6089d49

4 files changed

Lines changed: 38 additions & 43 deletions

File tree

GDPR/Controllers/LegalConsent.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,6 @@ public function __construct( $register_hooks = true ) {
9797
private function register_hooks() {
9898
add_action( 'wp_ajax_tutor_gdpr_legal_consents', array( $this, 'handle_legal_consent_ajax' ) );
9999
add_filter( 'tutor_localize_data', array( $this, 'extend_localize_data' ) );
100-
add_filter(
101-
'safe_style_css',
102-
function ( $styles ) {
103-
$styles[] = 'display';
104-
return $styles;
105-
}
106-
);
107100
add_action( 'tutor_login_form_end', array( $this, 'show_consent_field_on_login_form' ) );
108101
}
109102

@@ -783,6 +776,14 @@ private static function render_constructed_label_text( object $consent ): void {
783776
$message = str_replace( '{' . $key . '}', $anchor, $message );
784777
}
785778

779+
add_filter(
780+
'safe_style_css',
781+
function ( $styles ) {
782+
$styles[] = 'display';
783+
return $styles;
784+
}
785+
);
786+
786787
echo wp_kses(
787788
$message,
788789
array(

GDPR/GDPR.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __construct() {
6363
* @return void
6464
*/
6565
private function register_hooks() {
66-
add_action( 'init', array( $this, 'init' ), 5 );
66+
add_action( 'admin_init', array( $this, 'admin_init' ), 5 );
6767
}
6868

6969
/**
@@ -73,7 +73,7 @@ private function register_hooks() {
7373
*
7474
* @return void
7575
*/
76-
public function init() {
76+
public function admin_init() {
7777
$this->maybe_install_db();
7878

7979
$this->legal_consent = new LegalConsent();

classes/Utils.php

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7145,11 +7145,9 @@ public function array_only( $array = array(), $keys = null ) {
71457145
* @param int $course_id course id.
71467146
* @param bool $is_approved is approved.
71477147
*
7148-
* @return bool|int
7148+
* @return bool
71497149
*/
71507150
public function is_instructor_of_this_course( $instructor_id = 0, $course_id = 0, $is_approved = true ) {
7151-
global $wpdb;
7152-
71537151
$instructor_id = $this->get_user_id( $instructor_id );
71547152
$course_id = $this->get_post_id( $course_id );
71557153

@@ -7161,46 +7159,42 @@ public function is_instructor_of_this_course( $instructor_id = 0, $course_id = 0
71617159
$instructor = TutorCache::get( $cache_key );
71627160

71637161
if ( false === $instructor ) {
7162+
$meta_query_args = array(
7163+
array(
7164+
'key' => '_tutor_instructor_course_id',
7165+
'value' => $course_id,
7166+
'compare' => '=',
7167+
'type' => 'NUMERIC',
7168+
),
7169+
);
71647170

71657171
if ( $is_approved ) {
7166-
$is_approved_instructor = (int) $wpdb->get_var(
7167-
$wpdb->prepare(
7168-
"SELECT COUNT(umeta_id)
7169-
FROM {$wpdb->usermeta}
7170-
WHERE user_id = %d
7171-
AND meta_key = %s
7172-
AND meta_value = %s",
7173-
$instructor_id,
7174-
'_tutor_instructor_status',
7175-
'approved',
7176-
)
7172+
$meta_query_args[] = array(
7173+
'key' => '_tutor_instructor_status',
7174+
'value' => 'approved',
7175+
'compare' => '=',
71777176
);
7178-
7179-
if ( ! $is_approved_instructor ) {
7180-
return false;
7181-
}
71827177
}
71837178

7184-
//phpcs:disable
7185-
$instructor = $wpdb->get_col(
7186-
$wpdb->prepare(
7187-
"SELECT umeta_id
7188-
FROM {$wpdb->usermeta}
7189-
WHERE user_id = %d
7190-
AND meta_key = '_tutor_instructor_course_id'
7191-
AND meta_value = %d
7192-
",
7193-
$instructor_id,
7194-
$course_id
7195-
)
7179+
$meta_query_args['relation'] = 'AND';
7180+
$meta_query = new \WP_Meta_Query( $meta_query_args );
7181+
$instructor_query = new \WP_User_Query(
7182+
array(
7183+
'include' => array( $instructor_id ),
7184+
'number' => 1,
7185+
'fields' => 'ID',
7186+
'count_total' => false,
7187+
'meta_query' => $meta_query->queries,
7188+
)
71967189
);
7197-
//phpcs:enable
7190+
7191+
$instructor = $instructor_query->get_results();
71987192

71997193
TutorCache::set( $cache_key, $instructor );
72007194
}
72017195

7202-
if ( is_array( $instructor ) && count( $instructor ) ) {
7203-
return $instructor;
7196+
if ( $this->count( $instructor ) ) {
7197+
return true;
72047198
}
72057199

72067200
return false;

templates/learning-area/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
$tutor_course_list_url = tutor_utils()->course_archive_page_url();
5050
$tutor_is_enrolled = EnrollmentModel::is_enrolled( $tutor_course_id );
5151
$tutor_is_public_course = Course_List::is_public( $tutor_course_id );
52-
$tutor_is_course_instructor = tutor_utils()->has_user_course_content_access( $current_user_id, $tutor_course_id );
52+
$tutor_is_course_instructor = tutor_utils()->is_instructor_of_this_course( $current_user_id, $tutor_course_id, true );
5353
$tutor_is_course_completed = tutor_utils()->is_completed_course( $tutor_course_id, $current_user_id );
5454
$tutor_can_complete_course = CourseModel::can_complete_course( $tutor_course_id, $current_user_id ) && ! $tutor_is_course_completed;
5555
$tutor_course_progress = tutor_utils()->get_course_completed_percent( $tutor_course_id, $current_user_id );

0 commit comments

Comments
 (0)