Fix theme tab loading with improved error handling and proper function scoping
This commit is contained in:
@ -201,19 +201,23 @@ jQuery(document).ready(function($) {
|
||||
|
||||
}
|
||||
|
||||
// Function to load themes
|
||||
loadTheme = function() {
|
||||
// Function to load themes - global function
|
||||
window.loadTheme = function() {
|
||||
console.log('Starting theme loading process');
|
||||
var $container = $('#wpa-theme-list');
|
||||
|
||||
// Clear existing content
|
||||
$container.empty();
|
||||
console.log('Container emptied');
|
||||
|
||||
// Show loading overlay
|
||||
$container.css('position', 'relative');
|
||||
var $loadingOverlay = $('<div class="wp-allstars-loading-overlay"><span class="spinner is-active"></span></div>');
|
||||
$container.append($loadingOverlay);
|
||||
console.log('Loading overlay added');
|
||||
|
||||
// AJAX request to get themes
|
||||
console.log('Sending AJAX request with nonce:', wpAllstars.nonce);
|
||||
$.ajax({
|
||||
url: ajaxurl,
|
||||
type: 'POST',
|
||||
@ -222,27 +226,32 @@ jQuery(document).ready(function($) {
|
||||
_wpnonce: wpAllstars.nonce
|
||||
},
|
||||
success: function(response) {
|
||||
console.log('AJAX success response:', response);
|
||||
// Remove loading overlay
|
||||
$loadingOverlay.remove();
|
||||
|
||||
if (response.success) {
|
||||
console.log('Response success, updating container HTML');
|
||||
// Replace all content with new HTML
|
||||
$container.html(response.data);
|
||||
|
||||
// Initialize theme action buttons
|
||||
console.log('Initializing theme handlers');
|
||||
initThemeHandlers();
|
||||
} else {
|
||||
console.error('Response indicates error:', response.data);
|
||||
// Show error message
|
||||
$container.html('<div class="notice notice-error"><p>' + response.data + '</p></div>');
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('AJAX error:', { xhr: xhr, status: status, error: error });
|
||||
// Remove loading overlay
|
||||
$loadingOverlay.remove();
|
||||
|
||||
// Show error message
|
||||
$container.html('<div class="notice notice-error"><p>Failed to load themes. Please try again. Error: ' + error + '</p></div>');
|
||||
console.error('AJAX Error:', xhr.responseText);
|
||||
console.error('AJAX Error Response Text:', xhr.responseText);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -449,8 +449,17 @@ function wp_allstars_set_cached_theme($data) {
|
||||
|
||||
// Add AJAX endpoint for theme
|
||||
function wp_allstars_ajax_get_themes() {
|
||||
error_log('WP ALLSTARS: Theme AJAX handler started');
|
||||
|
||||
// Check nonce with the correct action name
|
||||
if (!isset($_POST['_wpnonce'])) {
|
||||
error_log('WP ALLSTARS: No nonce provided');
|
||||
wp_send_json_error('No security token provided.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!check_ajax_referer('wp-allstars-nonce', '_wpnonce', false)) {
|
||||
error_log('WP ALLSTARS: Invalid nonce: ' . sanitize_text_field($_POST['_wpnonce']));
|
||||
wp_send_json_error('Invalid security token sent.');
|
||||
return;
|
||||
}
|
||||
@ -461,7 +470,6 @@ function wp_allstars_ajax_get_themes() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add debugging
|
||||
error_log('WP ALLSTARS: Starting theme fetch process');
|
||||
|
||||
try {
|
||||
@ -521,6 +529,8 @@ function wp_allstars_ajax_get_themes() {
|
||||
$author = isset($theme_data->author['display_name']) ? $theme_data->author['display_name'] : '';
|
||||
}
|
||||
|
||||
error_log('WP ALLSTARS: Theme data retrieved, generating HTML');
|
||||
|
||||
// Generate custom HTML for the theme
|
||||
ob_start();
|
||||
?>
|
||||
@ -562,8 +572,9 @@ function wp_allstars_ajax_get_themes() {
|
||||
return;
|
||||
}
|
||||
|
||||
error_log('WP ALLSTARS: Successfully generated theme display');
|
||||
error_log('WP ALLSTARS: Successfully generated theme display, HTML length: ' . strlen($html));
|
||||
wp_send_json_success($html);
|
||||
exit; // Ensure we exit after sending the JSON response
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log('WP ALLSTARS Theme loading exception: ' . $e->getMessage());
|
||||
@ -691,9 +702,16 @@ function wp_allstars_settings_page() {
|
||||
// Add inline script to trigger theme loading through the main JS function
|
||||
wp_add_inline_script('wp-allstars-admin', '
|
||||
jQuery(document).ready(function($) {
|
||||
console.log("Theme tab ready, checking if we need to load themes");
|
||||
// Use the main loadTheme function from wp-allstars-admin.js if available
|
||||
if ($("#wpa-theme-list").length && $("#wpa-theme-list").is(":empty") && typeof loadTheme === "function") {
|
||||
loadTheme();
|
||||
if ($("#wpa-theme-list").length && $("#wpa-theme-list").is(":empty")) {
|
||||
console.log("Theme list is empty, calling loadTheme()");
|
||||
if (typeof window.loadTheme === "function") {
|
||||
window.loadTheme();
|
||||
} else {
|
||||
console.error("loadTheme function not found");
|
||||
$("#wpa-theme-list").html("<div class=\"notice notice-error\"><p>Error: Theme loading function not available.</p></div>");
|
||||
}
|
||||
}
|
||||
});
|
||||
');
|
||||
|
@ -37,14 +37,14 @@ if ( is_admin() ) {
|
||||
require_once plugin_dir_path( __FILE__ ) . 'admin/settings.php';
|
||||
}
|
||||
|
||||
// Localize script for AJAX
|
||||
function wp_allstars_localize_script() {
|
||||
wp_localize_script( 'wp-allstars-admin', 'wpAllstars', [
|
||||
'ajaxurl' => admin_url( 'admin-ajax.php' ),
|
||||
'nonce' => wp_create_nonce( 'wp-allstars-nonce' )
|
||||
] );
|
||||
}
|
||||
add_action( 'admin_enqueue_scripts', 'wp_allstars_localize_script' );
|
||||
// This function is not needed as we're localizing in wp_allstars_admin_assets
|
||||
// function wp_allstars_localize_script() {
|
||||
// wp_localize_script( 'wp-allstars-admin', 'wpAllstars', [
|
||||
// 'ajaxurl' => admin_url( 'admin-ajax.php' ),
|
||||
// 'nonce' => wp_create_nonce( 'wp-allstars-nonce' )
|
||||
// ] );
|
||||
// }
|
||||
// add_action( 'admin_enqueue_scripts', 'wp_allstars_localize_script' );
|
||||
|
||||
// Admin assets
|
||||
function wp_allstars_admin_assets() {
|
||||
|
Reference in New Issue
Block a user