Skip to content

Commit

Permalink
Added button in admin database notification to check if issue has bee…
Browse files Browse the repository at this point in the history
…n fixed.
  • Loading branch information
randhirexpresstech committed May 21, 2024
1 parent 861394b commit fb1174c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 25 deletions.
27 changes: 27 additions & 0 deletions js/qsm-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4075,4 +4075,31 @@ var import_button;
quiz_action:'retrieve'
} );
} );

// On click retrieve link
$( document ).on( 'click', '.notice.qmn-database-user-incorrect-permission .check-db-fix-btn', function( e ) {
e.preventDefault();
let dbFixBtn = $( this );
let formData = {
action: 'qsm_check_fix_db',
qmnnonce: $( this ).attr( 'qmnnonce' ),
};
dbFixBtn.text("processing...");
dbFixBtn.removeClass( 'check-db-fix-btn' );
dbFixBtn.removeClass( 'button-primary' );
$.ajax({
type: 'POST',
url: ajaxurl,
data: formData,
success: function (response) {
if ( undefined !== response.data ) {
dbFixBtn.text( response.data.message );
}
},
error: function ( jqXHR, textStatus, errorThrown ) {
console.log( "error", errorThrown );
}
});
} );

}(jQuery));
26 changes: 2 additions & 24 deletions mlw_quizmaster2.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function qsm_is_admin( $check_permission = 'manage_options' ) {
* @since 9.0.2
* @return array alter qmn table query list
*/
private function get_failed_alter_table_queries() {
public function get_failed_alter_table_queries() {
$failed_queries = get_option( 'qmn_failed_alter_table_queries', array() );
return is_array( $failed_queries ) ? $failed_queries: array();
}
Expand Down Expand Up @@ -336,28 +336,6 @@ private function add_hooks() {
add_action( 'admin_init', array( $this, 'qsm_overide_old_setting_options' ) );
add_action( 'admin_notices', array( $this, 'qsm_admin_notices' ) );
add_filter( 'manage_edit-qsm_category_columns', array( $this, 'modify_qsm_category_columns' ) );
add_action( 'admin_init', array( $this, 'has_alter_table_issue_solved' ), 999 );
}

/**
* Check if alter table issue has been solved by trying failed alter table query
*
* @since 9.0.2
*
* @return void
*/
public function has_alter_table_issue_solved() {
// Get failed alter table query list.
$failed_queries = $this->get_failed_alter_table_queries();
if ( ! empty( $failed_queries ) ) {
foreach ( $failed_queries as $failed_query ) {
$failed_query = $this->wpdb_alter_table_query( $failed_query );
// exit loop if query failed to execute
if ( false === $failed_query ) {
break;
}
}
}
}

/**
Expand Down Expand Up @@ -856,7 +834,7 @@ public function qsm_admin_notices() {
if ( ! empty( $failed_queries ) && 0 < count( $failed_queries ) ) {
?>
<div class="notice notice-warning is-dismissible qmn-database-user-incorrect-permission">
<p><?php esc_html_e( "It seems your database user doesn't have permission to ALTER TABLE. Please ensure the necessary permissions are in place or contact your hosting provider.", "quiz-master-next" ); ?></p>
<p><?php esc_html_e( "It seems your database user doesn't have permission to ALTER TABLE. Please ensure the necessary permissions are in place or contact your hosting provider.", "quiz-master-next" ); ?> <a href="#" qmnnonce="<?php echo wp_create_nonce( 'qmn_check_db' ); ?>" class="button button-primary check-db-fix-btn" ><?php esc_html_e( "Check If Already Fixed", "quiz-master-next" ); ?></a> </p>
</div>
<?php
}
Expand Down
69 changes: 69 additions & 0 deletions php/classes/class-qmn-quiz-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,75 @@ public function add_hooks() {

// Failed submission resubmit or trash
add_action( 'wp_ajax_qsm_action_failed_submission_table', array( $this, 'process_action_failed_submission_table' ) );

// Run failed ALTER TABLE query via ajax on notification button click
add_action( 'wp_ajax_qsm_check_fix_db', array( $this, 'has_alter_table_issue_solved' ) );
}

/**
* Check if alter table issue has been solved by trying failed alter table query
*
* @since 9.0.2
*
* @return void
*/
public function has_alter_table_issue_solved() {
if ( empty( $_POST['qmnnonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['qmnnonce'] ), 'qmn_check_db' ) || ! function_exists( 'is_admin' ) || ! is_admin() ) {
wp_send_json_error(
array(
'status' => 'error',
'message' => __( 'Unauthorized!', 'quiz-master-next' ),
)
);
} else {
global $mlwQuizMasterNext, $wpdb;
$wperror = '';
$query = array();
// Failed Query has been saved in wrong order sometimes due to order of execution. So run twice
for ( $i = 0; $i < 2 ; $i++ ) {
// Get failed alter table query list.
$failed_queries = $mlwQuizMasterNext->get_failed_alter_table_queries();
if ( ! empty( $failed_queries ) ) {
if ( 0 === $i ) {
$failed_queries = array_reverse( $failed_queries );
}
foreach ( $failed_queries as $failed_query ) {
$res = $mlwQuizMasterNext->wpdb_alter_table_query( $failed_query );

// exit loop if query failed to execute
if ( false === $res ) {
$wperror = $wpdb->last_error;
if ( false !== stripos( $wperror, 'Duplicate' ) ) {
// Remove failed query from list.
$failed_queries = array_diff( $failed_queries, array( $failed_query ) );
// Update failed queries list.
update_option( 'qmn_failed_alter_table_queries', $failed_queries );
$query['update'] = $failed_queries;
}
}
}
}
}

$failed_queries = $mlwQuizMasterNext->get_failed_alter_table_queries();
if ( ! empty( $failed_queries ) ) {
$query['latest'] = $failed_queries;
wp_send_json_error(
array(
'status' => 'error',
'query' => $query,
'message' => $wperror,
)
);
} else {
wp_send_json_success(
array(
'status' => 'success',
'message' => __( 'Fixed!', 'quiz-master-next' ),
)
);
}
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ This is usually a theme conflict. You can [checkout out our common conflict solu
= 9.0.2 ( Beta ) =
* Bug: Fixed display shortcode php buffer due to invalid quiz id
* Feature: Added Failed submission list in QSM menu
* Feature: Added retrieve or delete failed submission
* Feature: Added resubmit or delete failed submission
* Enhancement: Added admin notification if database user does not have proper permission.
* Feature: Added button in admin database notification to check if issue has been fixed.

= 9.0.1 (April 25, 2024) =
* Bug: Fixed date format in %ANSWER_X% variable
Expand Down

0 comments on commit fb1174c

Please sign in to comment.