From f848160158c3b68fb28fd9f50fb1c061fa9aaad2 Mon Sep 17 00:00:00 2001 From: Marcus Quinn Date: Sun, 16 Mar 2025 19:05:32 +0000 Subject: [PATCH] Fix theme tab loading with improved error handling and proper function scoping --- admin/js/wp-allstars-admin.js | 15 ++++++++++++--- admin/settings.php | 26 ++++++++++++++++++++++---- wp-allstars-plugin.php | 16 ++++++++-------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/admin/js/wp-allstars-admin.js b/admin/js/wp-allstars-admin.js index f5ff53d..992dedb 100644 --- a/admin/js/wp-allstars-admin.js +++ b/admin/js/wp-allstars-admin.js @@ -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 = $('
'); $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('

' + response.data + '

'); } }, 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('

Failed to load themes. Please try again. Error: ' + error + '

'); - console.error('AJAX Error:', xhr.responseText); + console.error('AJAX Error Response Text:', xhr.responseText); } }); diff --git a/admin/settings.php b/admin/settings.php index dcbcef6..22c5dca 100644 --- a/admin/settings.php +++ b/admin/settings.php @@ -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("

Error: Theme loading function not available.

"); + } } }); '); diff --git a/wp-allstars-plugin.php b/wp-allstars-plugin.php index ba4b0f1..8c8789b 100644 --- a/wp-allstars-plugin.php +++ b/wp-allstars-plugin.php @@ -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() {