Skip to content

Commit

Permalink
Filter course ids when they are fetched for a user by status
Browse files Browse the repository at this point in the history
  • Loading branch information
merkushin committed Apr 9, 2024
1 parent d2b8ce6 commit c0a5fc9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
37 changes: 23 additions & 14 deletions includes/class-sensei-learner.php
Expand Up @@ -393,18 +393,6 @@ private function get_enrolled_courses_query_by_progress_status( $user_id, $base_
$course_ids = [ -1 ];
}

/**
* Filters the course IDs for a learner's enrolled courses query by progress status.
*
* @hook sensei_learner_enrolled_courses_query_by_progress_status_course_ids
*
* @param {int[]} $course_ids Course IDs.
* @param {int} $user_id User ID.
* @param {string} $type Progress status type.
* @return {int[]} Course IDs.
*/
$course_ids = apply_filters( 'sensei_learner_enrolled_courses_query_by_progress_status_course_ids', $course_ids, $user_id, $type );

$query_args['post__in'] = $course_ids;

return new WP_Query( $query_args );
Expand Down Expand Up @@ -441,8 +429,17 @@ public function get_enrolled_courses_query_args( $user_id, $base_query_args = []
$query_args = array_merge( $default_args, $base_query_args );
$learner_term = self::get_learner_term( $user_id );

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$term_id = apply_filters( 'wpml_object_id', $learner_term->term_id, Sensei_PostTypes::LEARNER_TAXONOMY_NAME, true );
/**
* Filters the term ID used in the query to fetch a learner's enrolled courses.
*
* @hook sensei_learner_get_enrolled_courses_query_args_term_id
*
* @since $$next-version$$
*
* @param {int} $term_id The term ID.
* @return {int} The term ID.
*/
$term_id = apply_filters( 'sensei_learner_get_enrolled_courses_query_args_term_id', $learner_term->term_id );

$query_args['post_type'] = 'course';
$query_args['tax_query'][] = [
Expand Down Expand Up @@ -512,6 +509,18 @@ private function get_course_ids_by_progress_status( $user_id, $status ) {
$course_ids[] = intval( $status->comment_post_ID );
}

/**
* Filters the course IDs when getting them for a user by progress status.
*
* @hook sensei_learner_get_course_ids_by_progress_status_course_ids
*
* @param {int[]} $course_ids Course IDs.
* @param {int} $user_id User ID.
* @param {string} $status Progress status.
* @return {int[]} Course IDs.
*/
$course_ids = apply_filters( 'sensei_learner_get_course_ids_by_progress_status_course_ids', $course_ids, $user_id, $status );

return $course_ids;
}

Expand Down
19 changes: 18 additions & 1 deletion includes/wpml/class-course-progress.php
Expand Up @@ -8,6 +8,7 @@
namespace Sensei\WPML;

use Sensei_Course_Enrolment_Manager;
use Sensei_PostTypes;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand Down Expand Up @@ -38,7 +39,8 @@ public function init() {
add_filter( 'sensei_course_progress_find_course_id', array( $this, 'translate_course_id' ) );
add_filter( 'sensei_lesson_progress_count_course_id', array( $this, 'translate_course_id' ) );
add_filter( 'sensei_course_start_course_id', array( $this, 'translate_course_id' ) );
add_filter( 'sensei_learner_enrolled_courses_query_by_progress_status_course_ids', array( $this, 'translate_course_ids' ) );
add_filter( 'sensei_learner_get_course_ids_by_progress_status_course_ids', array( $this, 'translate_course_ids' ) );
add_filter( 'sensei_learner_get_enrolled_courses_query_args_term_id', array( $this, 'translate_term_id' ) );

add_action( 'sensei_manual_enrolment_learner_enrolled', array( $this, 'enrol_learner' ), 10, 2 );
add_action( 'sensei_manual_enrolment_learner_withdrawn', array( $this, 'withdraw_learner' ), 10, 2 );
Expand Down Expand Up @@ -136,4 +138,19 @@ public function translate_course_ids( array $course_ids ): array {

return $course_ids;
}

/**
* Translate term ID.
*
* @since $$next-version$$
*
* @internal
*
* @param int $term_id Term ID.
* @return int
*/
public function translate_term_id( $term_id ): int {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
return (int) apply_filters( 'wpml_object_id', $term_id, Sensei_PostTypes::LEARNER_TAXONOMY_NAME, true );
}
}

0 comments on commit c0a5fc9

Please sign in to comment.