Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code review changes #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @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 Expand Up @@ -63,6 +63,6 @@
*/
public function ld_custom_deactivate_custom_plugin() {
deactivate_plugins( plugin_basename( LD_CUSTOM_AUTO_COMPLETE_BASE ) );
unset( $_GET['activate'] );

Check warning on line 66 in ld_custom_auto_complete/includes/class-ld-custom-auto-complete-activator.php

View workflow job for this annotation

GitHub Actions / WPCS

Processing form data without nonce verification.
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +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 ) {
$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 );
}

/**
* Handles the timer completion ajax request.
*
Expand All @@ -85,57 +61,73 @@ 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
);
}
$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' ) );
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
);
}
}

/**
* Autocompletes a topic.
* Autocomplete a lesson or topic.
*
* @param int $post_id The ID of the 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.
* @return void
*/
public function ld_custom_autocomplete_topic( $post_id, $course_id, $user_id ) {
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;
}
$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 ( '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 );

if ( ! empty( $is_available_content ) && ! empty( $is_available_lesson ) ) {
return;
}
}
learndash_process_mark_complete( $user_id, $post_id, false, $course_id );
}
}
}
20 changes: 2 additions & 18 deletions ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
*/
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;

/**
* Public Class
*/
use Ld_Custom_Auto_Complete\Public\Ld_Custom_Auto_Complete_Frontend;

Check failure on line 27 in ld_custom_auto_complete/includes/class-ld-custom-auto-complete.php

View workflow job for this annotation

GitHub Actions / WPCS

PHP syntax error: syntax error, unexpected 'Public' (T_PUBLIC), expecting identifier (T_STRING) or '{'

/**
* Functionality Classes
Expand Down Expand Up @@ -95,7 +94,6 @@

$this->load_dependencies();
$this->handle_activation();
$this->handle_deactivation();
$this->set_locale();
$this->define_public_hooks();
$this->add_learndash_setting();
Expand Down Expand Up @@ -126,12 +124,6 @@
*/
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.
Expand Down Expand Up @@ -200,14 +192,6 @@
$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.
*
Expand Down Expand Up @@ -269,8 +253,8 @@
*/
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' );
}
}
80 changes: 80 additions & 0 deletions ld_custom_auto_complete/languages/ld_custom_auto_complete.pot
Original file line number Diff line number Diff line change
@@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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 ""
Loading
Loading