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

added login failed message #2401

Merged
merged 2 commits into from
Nov 24, 2023
Merged
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
29 changes: 29 additions & 0 deletions js/qsm-quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div class="qsm-result-page-warning qsm-login-form-warning">' + response.data.message + '</div>');
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();
Expand Down
33 changes: 32 additions & 1 deletion php/classes/class-qmn-quiz-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

/**
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -2744,7 +2772,10 @@ function qmn_require_login_check( $display, $qmn_quiz_options, $qmn_array_for_va
$mlw_message = str_replace( "\n", '<br>', $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;
}
Expand Down