From a313123e5ed8f13a618e71b01519adef6ceea57b Mon Sep 17 00:00:00 2001 From: Archinj Date: Wed, 4 Sep 2024 15:00:19 +0530 Subject: [PATCH 1/4] Code review changes --- .../class-ld-custom-auto-complete-handler.php | 12 +++++++----- .../public/js/ld_custom_auto_complete-frontend.js | 13 ++++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) 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 3aeae18..fb93589 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 @@ -85,9 +85,10 @@ public function ld_custom_autocomplete_lesson( $post_id, $course_id, $user_id ) */ public function ld_custom_autocomplete_timer() { if ( isset( $_POST['nonce'] ) && ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'ld_custom_mark_complete_nonce' ) ) { + $nonce_error_message = apply_filters( 'ld_custom_autocomplete_timer_nonce_error_message', __( 'Please provide proper nonce', 'ld_custom_auto_complete' ) ); wp_send_json_error( array( - 'message' => __( 'Please provide proper nonce', 'ld_custom_auto_complete' ), + 'message' => $nonce_error_message, 'type' => 'error', ), 403 @@ -99,21 +100,22 @@ public function ld_custom_autocomplete_timer() { $is_complete = learndash_process_mark_complete( $user_id, intval( $_POST['post_id'] ), false, intval( $_POST['course_id'] ) ); } if ( true === $is_complete ) { + $success_message = apply_filters( 'ld_custom_autocomplete_timer_success_message', __( 'Mark complete successfully done.', 'ld_custom_auto_complete' ) ); wp_send_json_success( array( - 'message' => __( 'Mark complete Sucessfully done.', 'ld_custom_auto_complete' ), + 'message' => $success_message, 'type' => 'success', ) ); - } else { + } + $error_message = apply_filters( 'ld_custom_autocomplete_timer_failure_message', __( 'Mark complete failed due to insufficient permissions.', 'ld_custom_auto_complete' ) ); wp_send_json_error( array( - 'message' => __( 'Mark complete Failed due to insufficient permissions.', 'ld_custom_auto_complete' ), + 'message' => $error_message, 'type' => 'error', ), 403 ); - } } /** diff --git a/ld_custom_auto_complete/public/js/ld_custom_auto_complete-frontend.js b/ld_custom_auto_complete/public/js/ld_custom_auto_complete-frontend.js index c7808ff..05fa506 100644 --- a/ld_custom_auto_complete/public/js/ld_custom_auto_complete-frontend.js +++ b/ld_custom_auto_complete/public/js/ld_custom_auto_complete-frontend.js @@ -1,14 +1,17 @@ +/** + * Handles the LearnDash timer countdown and completion process. + */ jQuery( function () { if (jQuery( '.learndash_timer' ).length) { jQuery( '.learndash_timer' ).each( - function (idx, item) { - const timer_el = jQuery( item ); + function (index, timer_element) { + const timer_el = jQuery( timer_element ); let timer_seconds = timer_el.data( 'timer-seconds' ); - const button_ref = timer_el.data( 'button' ); + const button_selector = timer_el.data( 'button' ); - if (typeof button_ref !== 'undefined' && jQuery( button_ref ).length) { - const timer_button_el = jQuery( button_ref ); + if (typeof button_selector !== 'undefined' && jQuery( button_selector ).length) { + const timer_button_el = jQuery( button_selector ); if (typeof timer_seconds !== 'undefined' && typeof timer_button_el !== 'undefined') { timer_seconds = parseInt( timer_seconds ); From 059dd8b5039a6df28d9505605ed5d1ed0c3e2872 Mon Sep 17 00:00:00 2001 From: Archinj Date: Tue, 10 Sep 2024 19:02:14 +0530 Subject: [PATCH 2/4] Code review changes --- ...class-ld-custom-auto-complete-settings.php | 78 +++++++++--------- ...ss-ld-custom-auto-complete-deactivator.php | 35 -------- .../class-ld-custom-auto-complete-handler.php | 7 +- .../class-ld-custom-auto-complete.php | 16 ---- .../languages/ld_custom_auto_complete.pot | 80 +++++++++++++++++++ .../js/ld_custom_auto_complete-frontend.js | 17 +++- 6 files changed, 138 insertions(+), 95 deletions(-) delete mode 100644 ld_custom_auto_complete/includes/class-ld-custom-auto-complete-deactivator.php diff --git a/ld_custom_auto_complete/admin/class-ld-custom-auto-complete-settings.php b/ld_custom_auto_complete/admin/class-ld-custom-auto-complete-settings.php index 5d827fb..786a67f 100644 --- a/ld_custom_auto_complete/admin/class-ld-custom-auto-complete-settings.php +++ b/ld_custom_auto_complete/admin/class-ld-custom-auto-complete-settings.php @@ -60,49 +60,49 @@ public static function get_instance() { */ public function ld_custom_autocomplete_add_setting( $setting_option_fields = array(), $settings_metabox_key = '' ) { - if ( 'learndash-topic-display-content-settings' === $settings_metabox_key || 'learndash-lesson-display-content-settings' === $settings_metabox_key ) { - $post_id = get_the_ID(); - $is_autocomplete_on = get_post_meta( $post_id, LD_CUSTOM_AUTO_COMPLETE_META, true ); + if ( 'learndash-topic-display-content-settings' !== $settings_metabox_key && 'learndash-lesson-display-content-settings' !== $settings_metabox_key ) { + return $setting_option_fields; + } + $post_id = get_the_ID(); + $is_autocomplete_on = get_post_meta( $post_id, LD_CUSTOM_AUTO_COMPLETE_META, true ); + $label = sprintf( + // translators: placeholder: Lesson. + esc_html_x( '%s Autocomplete', 'placeholder: Lesson', 'ld_custom_auto_complete' ), + learndash_get_custom_label( 'lesson' ) + ); + $help_text = sprintf( + // translators: placeholder: lesson. + esc_html_x( 'Turning this on will allow you to autocomplete %s.', 'placeholder: Lesson.', 'ld_custom_auto_complete' ), + learndash_get_custom_label_lower( 'lesson' ) + ); - if ( 'learndash-topic-display-content-settings' === $settings_metabox_key ) { - $label = sprintf( - // translators: placeholder: Topic. - esc_html_x( '%s Autocomplete', 'placeholder: Topic', 'learndash' ), - learndash_get_custom_label( 'topic' ) - ); - $help_text = sprintf( - // translators: placeholder: topic. - esc_html_x( 'Turning this on will allow you to autocomplete %s.', 'placeholder: Topic.', 'learndash' ), - learndash_get_custom_label_lower( 'topic' ) - ); - } else { - $label = sprintf( - // translators: placeholder: Lesson. - esc_html_x( '%s Autocomplete', 'placeholder: Lesson', 'learndash' ), - learndash_get_custom_label( 'lesson' ) - ); + if ( 'learndash-topic-display-content-settings' === $settings_metabox_key ) { + $label = sprintf( + // translators: placeholder: Topic. + esc_html_x( '%s Autocomplete', 'placeholder: Topic', 'ld_custom_auto_complete' ), + learndash_get_custom_label( 'topic' ) + ); $help_text = sprintf( - // translators: placeholder: lesson. - esc_html_x( 'Turning this on will allow you to autocomplete %s.', 'placeholder: Lesson.', 'learndash' ), - learndash_get_custom_label_lower( 'lesson' ) + // translators: placeholder: topic. + esc_html_x( 'Turning this on will allow you to autocomplete %s.', 'placeholder: Topic.', 'ld_custom_auto_complete' ), + learndash_get_custom_label_lower( 'topic' ) ); - } + } - if ( ! isset( $setting_option_fields[ LD_CUSTOM_AUTO_COMPLETE_META ] ) ) { - $setting_option_fields[ LD_CUSTOM_AUTO_COMPLETE_META ] = array( - 'name' => LD_CUSTOM_AUTO_COMPLETE_META, - 'label' => $label, - 'type' => 'checkbox-switch', - 'class' => '-medium', - 'value' => $is_autocomplete_on, - 'default' => '', - 'options' => array( - '' => '', - 'on' => '', - ), - 'help_text' => $help_text, - ); - } + if ( ! isset( $setting_option_fields[ LD_CUSTOM_AUTO_COMPLETE_META ] ) ) { + $setting_option_fields[ LD_CUSTOM_AUTO_COMPLETE_META ] = array( + 'name' => LD_CUSTOM_AUTO_COMPLETE_META, + 'label' => $label, + 'type' => 'checkbox-switch', + 'class' => '-medium', + 'value' => $is_autocomplete_on, + 'default' => '', + 'options' => array( + '' => '', + 'on' => '', + ), + 'help_text' => $help_text, + ); } return $setting_option_fields; } diff --git a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-deactivator.php b/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-deactivator.php deleted file mode 100644 index d4c5bde..0000000 --- a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete-deactivator.php +++ /dev/null @@ -1,35 +0,0 @@ - - */ -class Ld_Custom_Auto_Complete_Deactivator { - - /** - * Short Description. (use period) - * - * Long Description. - * - * @since 1.0.0 - */ - public static function deactivate() { - } -} 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 fb93589..c6d13f5 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 @@ -96,8 +96,11 @@ public function ld_custom_autocomplete_timer() { } $user_id = get_current_user_id(); $is_complete = false; - if ( isset( $_POST['post_id'] ) && isset( $_POST['course_id'] ) && isset( $_POST['timer'] ) && '0' === $_POST['timer'] ) { - $is_complete = learndash_process_mark_complete( $user_id, intval( $_POST['post_id'] ), false, intval( $_POST['course_id'] ) ); + $post_id = isset( $_POST['post_id'] ) ? filter_var( wp_unslash( $_POST['post_id'] ), FILTER_SANITIZE_NUMBER_INT ) : '0'; + $course_id = isset( $_POST['course_id'] ) ? filter_var( wp_unslash( $_POST['course_id'] ), FILTER_SANITIZE_NUMBER_INT ) : '0'; + $timer = isset( $_POST['timer'] ) ? filter_var( wp_unslash( $_POST['timer'] ), FILTER_SANITIZE_NUMBER_INT ) : ''; + if ( '0' !== $post_id && '0' !== $course_id && '0' === $timer ) { + $is_complete = learndash_process_mark_complete( $user_id, absint( $post_id ), false, absint( $course_id ) ); } if ( true === $is_complete ) { $success_message = apply_filters( 'ld_custom_autocomplete_timer_success_message', __( 'Mark complete successfully done.', 'ld_custom_auto_complete' ) ); diff --git a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php b/ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php index e4235a7..7afdc3f 100644 --- a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php +++ b/ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php @@ -19,7 +19,6 @@ */ use Ld_Custom_Auto_Complete\Includes\Ld_Custom_Auto_Complete_Loader; use Ld_Custom_Auto_Complete\Includes\Ld_Custom_Auto_Complete_Activator; -use Ld_Custom_Auto_Complete\Includes\Ld_Custom_Auto_Complete_Deactivator; use Ld_Custom_Auto_Complete\Includes\Ld_Custom_Auto_Complete_I18n; /** @@ -95,7 +94,6 @@ public function __construct() { $this->load_dependencies(); $this->handle_activation(); - $this->handle_deactivation(); $this->set_locale(); $this->define_public_hooks(); $this->add_learndash_setting(); @@ -126,12 +124,6 @@ private function load_dependencies() { */ require_once plugin_dir_path( __DIR__ ) . 'includes/class-ld-custom-auto-complete-activator.php'; - /** - * The class responsible for handling deactivation functionalities of the - * core plugin. - */ - require_once plugin_dir_path( __DIR__ ) . 'includes/class-ld-custom-auto-complete-deactivator.php'; - /** * The class responsible for orchestrating the actions and filters of the * core plugin. @@ -200,14 +192,6 @@ private function handle_activation() { $plugin_activator->activate(); } - /** - * Handle plugin deactivation - */ - private function handle_deactivation() { - $plugin_activator = new Ld_Custom_Auto_Complete_Deactivator(); - $plugin_activator->deactivate(); - } - /** * Run the loader to execute all of the hooks with WordPress. * diff --git a/ld_custom_auto_complete/languages/ld_custom_auto_complete.pot b/ld_custom_auto_complete/languages/ld_custom_auto_complete.pot index e69de29..2fb5eeb 100644 --- a/ld_custom_auto_complete/languages/ld_custom_auto_complete.pot +++ b/ld_custom_auto_complete/languages/ld_custom_auto_complete.pot @@ -0,0 +1,80 @@ +# Copyright (C) 2024 wisdmlabs +# This file is distributed under the GPL-2.0+. +msgid "" +msgstr "" +"Project-Id-Version: AutoCompleter 1.0.0\n" +"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/ld_custom_auto_complete\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"POT-Creation-Date: 2024-09-10T13:30:36+00:00\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"X-Generator: WP-CLI 2.10.0\n" +"X-Domain: ld_custom_auto_complete\n" + +#. Plugin Name of the plugin +#: D:/Services repos/LD-auto-complete/ld_custom_auto_complete/ld-custom-auto-complete.php +msgid "AutoCompleter" +msgstr "" + +#. Plugin URI of the plugin +#: D:/Services repos/LD-auto-complete/ld_custom_auto_complete/ld-custom-auto-complete.php +msgid "https://wisdmlabs.com" +msgstr "" + +#. Description of the plugin +#: D:/Services repos/LD-auto-complete/ld_custom_auto_complete/ld-custom-auto-complete.php +msgid "A custom LearnDash autocompleter that provides administrators with the ability to enable or disable autocomplete settings for specific lessons or topics" +msgstr "" + +#. Author of the plugin +#: D:/Services repos/LD-auto-complete/ld_custom_auto_complete/ld-custom-auto-complete.php +msgid "wisdmlabs" +msgstr "" + +#. Author URI of the plugin +#: D:/Services repos/LD-auto-complete/ld_custom_auto_complete/ld-custom-auto-complete.php +msgid "https://wisdmlabs.com/" +msgstr "" + +#. translators: placeholder: Lesson. +#: admin/class-ld-custom-auto-complete-settings.php:70 +msgctxt "placeholder: Lesson" +msgid "%s Autocomplete" +msgstr "" + +#. translators: placeholder: lesson. +#: admin/class-ld-custom-auto-complete-settings.php:75 +msgctxt "placeholder: Lesson." +msgid "Turning this on will allow you to autocomplete %s." +msgstr "" + +#. translators: placeholder: Topic. +#: admin/class-ld-custom-auto-complete-settings.php:82 +msgctxt "placeholder: Topic" +msgid "%s Autocomplete" +msgstr "" + +#. translators: placeholder: topic. +#: admin/class-ld-custom-auto-complete-settings.php:87 +msgctxt "placeholder: Topic." +msgid "Turning this on will allow you to autocomplete %s." +msgstr "" + +#: includes/class-ld-custom-auto-complete-activator.php:54 +msgid "AutoCompleter requires LearnDash to be installed and activated." +msgstr "" + +#: includes/class-ld-custom-auto-complete-handler.php:88 +msgid "Please provide proper nonce" +msgstr "" + +#: includes/class-ld-custom-auto-complete-handler.php:106 +msgid "Mark complete successfully done." +msgstr "" + +#: includes/class-ld-custom-auto-complete-handler.php:114 +msgid "Mark complete failed due to insufficient permissions." +msgstr "" diff --git a/ld_custom_auto_complete/public/js/ld_custom_auto_complete-frontend.js b/ld_custom_auto_complete/public/js/ld_custom_auto_complete-frontend.js index 05fa506..7f882c9 100644 --- a/ld_custom_auto_complete/public/js/ld_custom_auto_complete-frontend.js +++ b/ld_custom_auto_complete/public/js/ld_custom_auto_complete-frontend.js @@ -1,14 +1,21 @@ /** * Handles the LearnDash timer countdown and completion process. */ + jQuery( + /** + * This function retrieves the countdown timer from the page, stores the timer state in a cookie to ensure persistence across + * page reloads, and sends an AJAX request to mark the lesson as complete when the timer expires. It also manages to hide + * the complete button and updates cookies for timer tracking. + */ function () { + // Check if any LearnDash timers exist on the page. if (jQuery( '.learndash_timer' ).length) { jQuery( '.learndash_timer' ).each( function (index, timer_element) { - const timer_el = jQuery( timer_element ); - let timer_seconds = timer_el.data( 'timer-seconds' ); - const button_selector = timer_el.data( 'button' ); + const timer_el = jQuery( timer_element ); + let timer_seconds = timer_el.data( 'timer-seconds' ); + const button_selector = timer_el.data( 'button' ); if (typeof button_selector !== 'undefined' && jQuery( button_selector ).length) { const timer_button_el = jQuery( button_selector ); @@ -23,15 +30,18 @@ jQuery( const cookie_timer_seconds = jQuery.cookie( cookie_name ); + // If cookie exists, update the timer with the cookie value. if (typeof cookie_timer_seconds !== 'undefined') { timer_seconds = parseInt( cookie_timer_seconds ); } + // Check if the timer is still running. if (timer_seconds >= 1) { const learndash_timer_var = setInterval( function () { timer_seconds -= 1; + // When the timer reaches zero, fire the ajax request. if (timer_seconds <= 0) { clearInterval( learndash_timer_var ); timer_button_el.hide(); @@ -54,6 +64,7 @@ jQuery( jQuery.cookie( updated_cookie_name, 0, { path: '/' } ); + // Send an AJAX request to mark the lesson complete. jQuery.ajax( { url: ld_custom_auto_complete_localized_data.ajax_url, From 2538820674a13a8dd95460ee96c1e159f30c2efa Mon Sep 17 00:00:00 2001 From: Archinj Date: Wed, 11 Sep 2024 16:04:38 +0530 Subject: [PATCH 3/4] 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 ); } } } From 06023abd597de8a2436789b9a0a79e7ff2f08292 Mon Sep 17 00:00:00 2001 From: Archinj Date: Thu, 12 Sep 2024 10:34:32 +0530 Subject: [PATCH 4/4] Change function call into single call --- .../class-ld-custom-auto-complete-handler.php | 38 +++---------------- .../class-ld-custom-auto-complete.php | 4 +- 2 files changed, 7 insertions(+), 35 deletions(-) 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 a740529..6201aa0 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 @@ -50,21 +50,6 @@ public static function get_instance() { return self::$instance; } - /** - * Autocomplete a lesson. - * - * Checks if the lesson is set to autocomplete and if the user has access to it. - * If the lesson is set to autocomplete and the user does not have access, it marks the lesson as complete for the user. - * - * @param int $post_id The ID of the lesson. - * @param int $course_id The ID of the course. - * @param int $user_id The ID of the user. - * @return void - */ - public function ld_custom_autocomplete_lesson( $post_id, $course_id, $user_id ) { - $this->ld_custom_autocomplete_checker( $post_id, $course_id, $user_id, true ); - } - /** * Handles the timer completion ajax request. * @@ -112,30 +97,17 @@ public function ld_custom_autocomplete_timer() { ); } - /** - * Autocompletes a topic. - * - * @param int $post_id The ID of the topic. - * @param int $course_id The ID of the course. - * @param int $user_id The ID of the user. - * @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. + * @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. * @return void */ - public function ld_custom_autocomplete_checker( $post_id, $course_id, $user_id, $is_lesson = true ) { + public function ld_custom_autocomplete_checker( $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; @@ -147,7 +119,7 @@ public function ld_custom_autocomplete_checker( $post_id, $course_id, $user_id, return; } - if ( ! $is_lesson ) { + if ( 'sfwd-topic' === get_post_type( $post_id ) ) { $lesson_id = learndash_get_lesson_id( $post_id, $course_id ); $is_available_lesson = ld_lesson_access_from( $lesson_id, $user_id, $course_id ); diff --git a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php b/ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php index 7afdc3f..25ff256 100644 --- a/ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php +++ b/ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php @@ -253,8 +253,8 @@ private function add_learndash_setting() { */ private function autocomplete_handler() { $handler = Ld_Custom_Auto_Complete_Handler::get_instance(); - $this->loader->add_action( 'learndash-lesson-before', $handler, 'ld_custom_autocomplete_lesson', 1, 3 ); - $this->loader->add_action( 'learndash-topic-before', $handler, 'ld_custom_autocomplete_topic', 1, 3 ); + $this->loader->add_action( 'learndash-lesson-before', $handler, 'ld_custom_autocomplete_checker', 1, 3 ); + $this->loader->add_action( 'learndash-topic-before', $handler, 'ld_custom_autocomplete_checker', 1, 3 ); $this->loader->add_action( 'wp_ajax_mark_complete', $handler, 'ld_custom_autocomplete_timer' ); } }