Skip to content

Commit

Permalink
Merge pull request #2401 from QuizandSurveyMaster/CU-86796v599-show-w…
Browse files Browse the repository at this point in the history
…rong-login-message

added login failed message
  • Loading branch information
zubairraeen authored Nov 24, 2023
2 parents 349dc8e + 542e52e commit 3721671
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
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

0 comments on commit 3721671

Please sign in to comment.