Skip to content

Commit

Permalink
Adding helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Archinj committed Sep 11, 2024
1 parent 059dd8b commit 2538820
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Ld_Custom_Auto_Complete_Activator {
* @since 1.0.0
*/
public function activate() {
add_action( 'init', array( $this, 'ld_custom_is_learndash_active' ) );
add_action( 'admin_init', array( $this, 'ld_custom_is_learndash_active' ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,7 @@ public static function get_instance() {
* @return void
*/
public function ld_custom_autocomplete_lesson( $post_id, $course_id, $user_id ) {
$is_autocomplete_on = get_post_meta( $post_id, LD_CUSTOM_AUTO_COMPLETE_META, true );

if ( 'on' !== $is_autocomplete_on ) {
return;
}
$is_available = ld_lesson_access_from( $post_id, $user_id, $course_id );
if ( ! empty( $is_available ) ) {
return;
}
learndash_process_mark_complete( $user_id, $post_id, false, $course_id );
$this->ld_custom_autocomplete_checker( $post_id, $course_id, $user_id, true );
}

/**
Expand Down Expand Up @@ -130,17 +121,41 @@ public function ld_custom_autocomplete_timer() {
* @return void
*/
public function ld_custom_autocomplete_topic( $post_id, $course_id, $user_id ) {
$this->ld_custom_autocomplete_checker( $post_id, $course_id, $user_id, false );
}

/**
* Autocomplete a lesson or topic.
*
* Checks if the lesson or topic is set to autocomplete and if the user has access to it.
*
* @param int $post_id The ID of the lesson or topic.
* @param int $course_id The ID of the course.
* @param int $user_id The ID of the user.
* @param bool $is_lesson Whether the current content is a lesson or topic. Defaults to true.
* @return void
*/
public function ld_custom_autocomplete_checker( $post_id, $course_id, $user_id, $is_lesson = true ) {
$is_autocomplete_on = get_post_meta( $post_id, LD_CUSTOM_AUTO_COMPLETE_META, true );
if ( 'on' !== $is_autocomplete_on ) {
return;
}
$lesson_id = learndash_get_lesson_id( $post_id, $course_id );
$is_available_topic = ld_lesson_access_from( $post_id, $user_id, $course_id );
$is_available_lesson = ld_lesson_access_from( $lesson_id, $user_id, $course_id );
if ( ! empty( $is_available_topic ) && ! empty( $is_available_lesson ) ) {

$is_available_content = ld_lesson_access_from( $post_id, $user_id, $course_id );

if ( ! empty( $is_available_content ) ) {
return;
}
learndash_process_mark_complete( $user_id, $post_id, false, $course_id );

if ( ! $is_lesson ) {
$lesson_id = learndash_get_lesson_id( $post_id, $course_id );
$is_available_lesson = ld_lesson_access_from( $lesson_id, $user_id, $course_id );

if ( ! empty( $is_available_content ) && ! empty( $is_available_lesson ) ) {
return;
}
}
learndash_process_mark_complete( $user_id, $post_id, false, $course_id );
}
}
}

0 comments on commit 2538820

Please sign in to comment.