From 2538820674a13a8dd95460ee96c1e159f30c2efa Mon Sep 17 00:00:00 2001 From: Archinj Date: Wed, 11 Sep 2024 16:04:38 +0530 Subject: [PATCH] Adding helper function --- ...lass-ld-custom-auto-complete-activator.php | 2 +- .../class-ld-custom-auto-complete-handler.php | 45 ++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-activator.php b/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-activator.php index e177825..5c93c1b 100644 --- a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-activator.php +++ b/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-activator.php @@ -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' ) ); } /** diff --git a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-handler.php b/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-handler.php index c6d13f5..a740529 100644 --- a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-handler.php +++ b/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-handler.php @@ -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 ); } /** @@ -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 ); } } }