Fix theme activation button functionality with improved error handling and debugging

This commit is contained in:
Marcus Quinn
2025-03-17 00:57:30 +00:00
parent 5f64feeaa8
commit e0d2904fbc
3 changed files with 52 additions and 5 deletions

View File

@ -296,6 +296,7 @@ jQuery(document).ready(function($) {
// Initialize theme handlers // Initialize theme handlers
function initThemeHandlers() { function initThemeHandlers() {
console.log('Initializing theme handlers');
// Remove any existing event handlers to prevent duplicates // Remove any existing event handlers to prevent duplicates
$('.theme-actions .install-now').off('click'); $('.theme-actions .install-now').off('click');
$('.theme-actions .activate-now').off('click'); $('.theme-actions .activate-now').off('click');
@ -307,23 +308,29 @@ jQuery(document).ready(function($) {
var slug = $button.data('slug'); var slug = $button.data('slug');
var $themeCard = $button.closest('.theme-card'); var $themeCard = $button.closest('.theme-card');
console.log('Installing theme:', slug);
$button.addClass('updating-message').text('Installing...'); $button.addClass('updating-message').text('Installing...');
wp.updates.installTheme({ wp.updates.installTheme({
slug: slug, slug: slug,
success: function(response) { success: function(response) {
console.log('Theme installed successfully:', response);
$button.removeClass('updating-message').addClass('updated-message').text('Installed!'); $button.removeClass('updating-message').addClass('updated-message').text('Installed!');
setTimeout(function() { setTimeout(function() {
// Replace the button with an activate button // Replace the button with an activate button
var $parent = $button.closest('.theme-actions'); var $parent = $button.closest('.theme-actions');
$button.remove(); $button.remove();
$parent.prepend('<button type="button" class="button button-primary activate-now" data-slug="' + slug + '" data-nonce="' + wpAllstars.nonce + '">Activate</button>'); var activateButton = $('<button type="button" class="button button-primary activate-now">Activate</button>');
activateButton.attr('data-slug', slug);
activateButton.attr('data-nonce', wpAllstars.nonce);
$parent.prepend(activateButton);
// Re-initialize the event handlers // Re-initialize the event handlers
initThemeHandlers(); initThemeHandlers();
}, 1000); }, 1000);
}, },
error: function(response) { error: function(response) {
console.error('Theme installation failed:', response);
$button.removeClass('updating-message').text('Install Now'); $button.removeClass('updating-message').text('Install Now');
alert(response.errorMessage); alert(response.errorMessage);
} }
@ -335,7 +342,9 @@ jQuery(document).ready(function($) {
e.preventDefault(); e.preventDefault();
var $button = $(this); var $button = $(this);
var slug = $button.data('slug'); var slug = $button.data('slug');
var nonce = $button.data('nonce') || wpAllstars.nonce;
console.log('Activating theme:', slug, 'with nonce:', nonce);
$button.addClass('updating-message').text('Activating...'); $button.addClass('updating-message').text('Activating...');
// Use AJAX to activate the theme // Use AJAX to activate the theme
@ -345,9 +354,10 @@ jQuery(document).ready(function($) {
data: { data: {
action: 'wp_allstars_activate_theme', action: 'wp_allstars_activate_theme',
theme: slug, theme: slug,
_wpnonce: $button.data('nonce') || wpAllstars.nonce _wpnonce: nonce
}, },
success: function(response) { success: function(response) {
console.log('Theme activation response:', response);
if (response.success) { if (response.success) {
$button.removeClass('updating-message').addClass('updated-message').text('Activated!'); $button.removeClass('updating-message').addClass('updated-message').text('Activated!');
setTimeout(function() { setTimeout(function() {
@ -361,7 +371,8 @@ jQuery(document).ready(function($) {
alert(response.data || 'Failed to activate theme'); alert(response.data || 'Failed to activate theme');
} }
}, },
error: function() { error: function(jqXHR, textStatus, errorThrown) {
console.error('Theme activation AJAX error:', textStatus, errorThrown, jqXHR.responseText);
$button.removeClass('updating-message').text('Activate'); $button.removeClass('updating-message').text('Activate');
alert('Failed to activate theme. Please try again or activate from the Themes page.'); alert('Failed to activate theme. Please try again or activate from the Themes page.');
} }

View File

@ -24,15 +24,24 @@ if (!defined('ABSPATH')) {
$installed_theme = wp_get_theme('kadence'); $installed_theme = wp_get_theme('kadence');
$current_theme = wp_get_theme(); $current_theme = wp_get_theme();
$is_active = ($current_theme->get_stylesheet() === 'kadence'); $is_active = ($current_theme->get_stylesheet() === 'kadence');
$nonce = wp_create_nonce('wp-allstars-nonce');
if ($is_active): ?> if ($is_active): ?>
<button type="button" class="button button-disabled" disabled="disabled"> <button type="button" class="button button-disabled" disabled="disabled">
<?php esc_html_e('Active'); ?> <?php esc_html_e('Active'); ?>
</button> </button>
<?php elseif ($installed_theme->exists()): ?> <?php elseif ($installed_theme->exists()): ?>
<button type="button" class="button button-primary activate-now" data-slug="kadence" data-nonce="<?php echo esc_attr(wp_create_nonce('wp-allstars-nonce')); ?>"> <button type="button" class="button button-primary activate-now"
data-slug="kadence"
data-nonce="<?php echo esc_attr($nonce); ?>">
<?php esc_html_e('Activate'); ?> <?php esc_html_e('Activate'); ?>
</button> </button>
<script>
console.log('Theme activation button initialized with:', {
slug: 'kadence',
nonce: '<?php echo esc_js($nonce); ?>'
});
</script>
<?php else: ?> <?php else: ?>
<button type="button" class="button button-primary install-now" data-slug="kadence"> <button type="button" class="button button-primary install-now" data-slug="kadence">
<?php esc_html_e('Install'); ?> <?php esc_html_e('Install'); ?>

View File

@ -1295,41 +1295,68 @@ add_action('switch_theme', 'wp_allstars_clear_theme_cache');
// Add AJAX handler for theme activation // Add AJAX handler for theme activation
function wp_allstars_activate_theme() { function wp_allstars_activate_theme() {
// Debug information
error_log('Theme activation AJAX request received: ' . print_r($_POST, true));
// Check nonce with the correct action name // Check nonce with the correct action name
if (!check_ajax_referer('wp-allstars-nonce', '_wpnonce', false)) { if (!check_ajax_referer('wp-allstars-nonce', '_wpnonce', false)) {
error_log('Theme activation failed: Invalid nonce');
wp_send_json_error('Invalid security token sent.'); wp_send_json_error('Invalid security token sent.');
return; return;
} }
if (!current_user_can('switch_themes')) { if (!current_user_can('switch_themes')) {
error_log('Theme activation failed: Permission denied');
wp_send_json_error('Permission denied'); wp_send_json_error('Permission denied');
return; return;
} }
$theme = isset($_POST['theme']) ? sanitize_text_field($_POST['theme']) : ''; $theme = isset($_POST['theme']) ? sanitize_text_field($_POST['theme']) : '';
if (empty($theme)) { if (empty($theme)) {
error_log('Theme activation failed: No theme specified');
wp_send_json_error('No theme specified'); wp_send_json_error('No theme specified');
return; return;
} }
// Get the theme object // Get the theme object
$theme_obj = wp_get_theme($theme); $theme_obj = wp_get_theme($theme);
if (!$theme_obj->exists() || $theme_obj->errors()) { if (!$theme_obj->exists()) {
error_log('Theme activation failed: Theme does not exist - ' . $theme);
wp_send_json_error('Theme does not exist'); wp_send_json_error('Theme does not exist');
return; return;
} }
if ($theme_obj->errors()) {
error_log('Theme activation failed: Theme has errors - ' . print_r($theme_obj->errors(), true));
wp_send_json_error('Theme has errors');
return;
}
// Check if theme is already active
$current_theme = wp_get_theme();
if ($current_theme->get_stylesheet() === $theme) {
error_log('Theme is already active: ' . $theme);
wp_send_json_success(array(
'message' => 'Theme is already active',
'customize_url' => admin_url('customize.php')
));
return;
}
// Switch the theme // Switch the theme
error_log('Switching to theme: ' . $theme);
switch_theme($theme); switch_theme($theme);
// Check if the switch was successful // Check if the switch was successful
$active_theme = wp_get_theme(); $active_theme = wp_get_theme();
if ($active_theme->get_stylesheet() === $theme) { if ($active_theme->get_stylesheet() === $theme) {
error_log('Theme activated successfully: ' . $theme);
wp_send_json_success(array( wp_send_json_success(array(
'message' => 'Theme activated successfully', 'message' => 'Theme activated successfully',
'customize_url' => admin_url('customize.php') 'customize_url' => admin_url('customize.php')
)); ));
} else { } else {
error_log('Failed to activate theme: ' . $theme . ', active theme is: ' . $active_theme->get_stylesheet());
wp_send_json_error('Failed to activate theme'); wp_send_json_error('Failed to activate theme');
} }
} }