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