diff --git a/js/qsm-quiz.js b/js/qsm-quiz.js index e5bfae2fc..47e8102db 100644 --- a/js/qsm-quiz.js +++ b/js/qsm-quiz.js @@ -1617,6 +1617,35 @@ jQuery(function () { }, 2000); }); + jQuery(document).on('submit', 'form[name="qsm-login-form"]', function (e) { + e.preventDefault(); + + let form = jQuery(this); + let username = form.find('input[name="log"]').val(); + let password = form.find('input[name="pwd"]').val(); + form.find('input[type="submit"]').attr('disabled', true); + jQuery(".qsm-login-form-warning").remove(); + + // Make a request to the WordPress REST API to log in + jQuery.ajax({ + url: qmn_ajax_object.ajaxurl, + method: 'POST', + data: { + action: 'qsm_ajax_login', + username: username, + password: password, + }, + success: function (response) { + if ( response.success ) { + form.get(0).submit(); + } else { + form.append('
' + response.data.message + '
'); + form.find('input[type="submit"]').attr('disabled', false); + } + } + }); + }); + //inline result status function function qsm_show_inline_result(quizID, question_id, value, $this, answer_type, $i_this, index = null) { jQuery('.qsm-spinner-loader').remove(); diff --git a/php/classes/class-qmn-quiz-manager.php b/php/classes/class-qmn-quiz-manager.php index 2355075cb..d30481f3f 100644 --- a/php/classes/class-qmn-quiz-manager.php +++ b/php/classes/class-qmn-quiz-manager.php @@ -86,6 +86,33 @@ public function add_hooks() { add_action( 'wp_ajax_nopriv_qsm_remove_file_fd_question', array( $this, 'qsm_remove_file_fd_question' ) ); add_action( 'init', array( $this, 'qsm_process_background_email' ) ); + add_action('wp_ajax_nopriv_qsm_ajax_login', array( $this, 'qsm_ajax_login' ) ); + + } + + /** + * @version 8.2.0 + * ajax login function + */ + public function qsm_ajax_login() { + $username = ! empty( $_POST['username'] ) ? sanitize_user( wp_unslash( $_POST['username'] ) ) : ''; + $password = ! empty( $_POST['password'] ) ? sanitize_text_field( wp_unslash( $_POST['password'] ) ) : ''; + + $user = get_user_by('login', $username); + + if ( ! $user ) { + wp_send_json_error( array( 'message' => __( 'User not found! Please try again.', 'quiz-master-next' ) ) ); + } + + $user_id = $user->ID; + + // Check the password + if ( ! wp_check_password( $password, $user->user_pass, $user_id ) ) { + wp_send_json_error( array( 'message' => __( 'Incorrect username or password! Please try again.', 'quiz-master-next' ) ) ); + }else { + wp_send_json_success(); + } + } /** @@ -898,6 +925,7 @@ public function display_quiz( $options, $quiz_data, $question_amount, $shortcode 'qsm_quiz', 'qmn_ajax_object', array( + 'site_url' => site_url(), 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'multicheckbox_limit_reach' => $mlwQuizMasterNext->pluginHelper->qsm_language_support( $options->quiz_limit_choice, "quiz_quiz_limit_choice-{$options->quiz_id}" ), 'out_of_text' => __( ' out of ', 'quiz-master-next' ), @@ -2744,7 +2772,10 @@ function qmn_require_login_check( $display, $qmn_quiz_options, $qmn_array_for_va $mlw_message = str_replace( "\n", '
', $mlw_message ); // $display .= do_shortcode($mlw_message); $display .= do_shortcode( $mlw_message ); - $display .= wp_login_form( array( 'echo' => false ) ); + $display .= wp_login_form( array( + 'echo' => false, + 'form_id' => 'qsm-login-form', + ) ); } return $display; }