From ef8e548bcd674932a4a6d79a1cd20ff63288b4d6 Mon Sep 17 00:00:00 2001 From: Ren <18050944+renintw@users.noreply.github.com> Date: Thu, 18 Jul 2024 15:11:16 +0800 Subject: [PATCH] Fix My Courses returns all courses when 'All' is selected (#2671) * fix My Course returns all courses Because the get_course_ids_to_be_excluded function from sensei-lms directly returns an empty array when encountering "all" and unexpected options, it causes the filter to return all courses when "all" is selected, instead of the courses that a student has been enrolled in. Since there is no native hook to adjust this, it has been ported from Sensei. reference: https://github.com/Automattic/sensei/blob/trunk/includes/blocks/course-list/class-sensei-course-list-student-course-filter.php#L95-L97 --- .../pub/wporg-learn-2024/inc/block-config.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/wp-content/themes/pub/wporg-learn-2024/inc/block-config.php b/wp-content/themes/pub/wporg-learn-2024/inc/block-config.php index eb05af109..0e10eac30 100644 --- a/wp-content/themes/pub/wporg-learn-2024/inc/block-config.php +++ b/wp-content/themes/pub/wporg-learn-2024/inc/block-config.php @@ -6,6 +6,7 @@ namespace WordPressdotorg\Theme\Learn_2024\Block_Config; use function WPOrg_Learn\Post_Meta\{get_available_post_type_locales}; +use Sensei_Learner; add_filter( 'wporg_query_filter_options_language', __NAMESPACE__ . '\get_language_options' ); add_filter( 'wporg_query_filter_options_archive_language', __NAMESPACE__ . '\get_language_options_by_post_type' ); @@ -21,6 +22,7 @@ add_filter( 'query_vars', __NAMESPACE__ . '\add_student_course_filter_query_vars' ); add_filter( 'wporg_query_filter_options_student_course', __NAMESPACE__ . '\get_student_course_options' ); add_action( 'wporg_query_filter_in_form', __NAMESPACE__ . '\inject_other_filters' ); +add_filter( 'query_loop_block_query_vars', __NAMESPACE__ . '\modify_course_query' ); /** * Get the current URL. @@ -557,3 +559,38 @@ function inject_other_filters( $key ) { printf( '', esc_attr( $wp_query->query['s'] ) ); } } + +/** + * Modify the course query on the 'My Courses' page to display courses according to the filter status. + * Corresponds to https://github.com/Automattic/sensei/blob/trunk/includes/blocks/course-list/class-sensei-course-list-student-course-filter.php#L95 + * + * @param array $query The course query. + * @return array The modified course query. + */ +function modify_course_query( $query ) { + if ( get_the_ID() === Sensei()->settings->get_my_courses_page_id() ) { + $key = get_student_course_filter_query_var_name(); + $selected_option = isset( $_GET[ $key ] ) ? sanitize_text_field( wp_unslash( $_GET[ $key ] ) ) : ''; + + // The courses query with 'active' and 'completed' statuses have already been filtered in Sensei LMS, and can correctly display the course lists. + // See https://github.com/Automattic/sensei/blob/trunk/includes/blocks/course-list/class-sensei-course-list-student-course-filter.php#L114-L123. + if ( ! empty( $selected_option ) && ( 'active' === $selected_option || 'completed' === $selected_option ) ) { + return $query; + } + + $learner_manager = Sensei_Learner::instance(); + $user_id = get_current_user_id(); + $args = array( + 'posts_per_page' => -1, + 'fields' => 'ids', + ); + + // The courses query with 'all' or any other statuses. + $courses_query = $learner_manager->get_enrolled_courses_query( $user_id, $args ); + $course_ids = $courses_query->posts; + + $query['post__in'] = $course_ids; + } + + return $query; +}