Fix theme activation button functionality with improved error handling and debugging
This commit is contained in:
@ -296,6 +296,7 @@ jQuery(document).ready(function($) {
|
||||
|
||||
// Initialize theme handlers
|
||||
function initThemeHandlers() {
|
||||
console.log('Initializing theme handlers');
|
||||
// Remove any existing event handlers to prevent duplicates
|
||||
$('.theme-actions .install-now').off('click');
|
||||
$('.theme-actions .activate-now').off('click');
|
||||
@ -307,23 +308,29 @@ jQuery(document).ready(function($) {
|
||||
var slug = $button.data('slug');
|
||||
var $themeCard = $button.closest('.theme-card');
|
||||
|
||||
console.log('Installing theme:', slug);
|
||||
$button.addClass('updating-message').text('Installing...');
|
||||
|
||||
wp.updates.installTheme({
|
||||
slug: slug,
|
||||
success: function(response) {
|
||||
console.log('Theme installed successfully:', response);
|
||||
$button.removeClass('updating-message').addClass('updated-message').text('Installed!');
|
||||
setTimeout(function() {
|
||||
// Replace the button with an activate button
|
||||
var $parent = $button.closest('.theme-actions');
|
||||
$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
|
||||
initThemeHandlers();
|
||||
}, 1000);
|
||||
},
|
||||
error: function(response) {
|
||||
console.error('Theme installation failed:', response);
|
||||
$button.removeClass('updating-message').text('Install Now');
|
||||
alert(response.errorMessage);
|
||||
}
|
||||
@ -335,7 +342,9 @@ jQuery(document).ready(function($) {
|
||||
e.preventDefault();
|
||||
var $button = $(this);
|
||||
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...');
|
||||
|
||||
// Use AJAX to activate the theme
|
||||
@ -345,9 +354,10 @@ jQuery(document).ready(function($) {
|
||||
data: {
|
||||
action: 'wp_allstars_activate_theme',
|
||||
theme: slug,
|
||||
_wpnonce: $button.data('nonce') || wpAllstars.nonce
|
||||
_wpnonce: nonce
|
||||
},
|
||||
success: function(response) {
|
||||
console.log('Theme activation response:', response);
|
||||
if (response.success) {
|
||||
$button.removeClass('updating-message').addClass('updated-message').text('Activated!');
|
||||
setTimeout(function() {
|
||||
@ -361,7 +371,8 @@ jQuery(document).ready(function($) {
|
||||
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');
|
||||
alert('Failed to activate theme. Please try again or activate from the Themes page.');
|
||||
}
|
||||
|
@ -24,15 +24,24 @@ if (!defined('ABSPATH')) {
|
||||
$installed_theme = wp_get_theme('kadence');
|
||||
$current_theme = wp_get_theme();
|
||||
$is_active = ($current_theme->get_stylesheet() === 'kadence');
|
||||
$nonce = wp_create_nonce('wp-allstars-nonce');
|
||||
|
||||
if ($is_active): ?>
|
||||
<button type="button" class="button button-disabled" disabled="disabled">
|
||||
<?php esc_html_e('Active'); ?>
|
||||
</button>
|
||||
<?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'); ?>
|
||||
</button>
|
||||
<script>
|
||||
console.log('Theme activation button initialized with:', {
|
||||
slug: 'kadence',
|
||||
nonce: '<?php echo esc_js($nonce); ?>'
|
||||
});
|
||||
</script>
|
||||
<?php else: ?>
|
||||
<button type="button" class="button button-primary install-now" data-slug="kadence">
|
||||
<?php esc_html_e('Install'); ?>
|
||||
|
@ -1295,41 +1295,68 @@ add_action('switch_theme', 'wp_allstars_clear_theme_cache');
|
||||
|
||||
// Add AJAX handler for theme activation
|
||||
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
|
||||
if (!check_ajax_referer('wp-allstars-nonce', '_wpnonce', false)) {
|
||||
error_log('Theme activation failed: Invalid nonce');
|
||||
wp_send_json_error('Invalid security token sent.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!current_user_can('switch_themes')) {
|
||||
error_log('Theme activation failed: Permission denied');
|
||||
wp_send_json_error('Permission denied');
|
||||
return;
|
||||
}
|
||||
|
||||
$theme = isset($_POST['theme']) ? sanitize_text_field($_POST['theme']) : '';
|
||||
if (empty($theme)) {
|
||||
error_log('Theme activation failed: No theme specified');
|
||||
wp_send_json_error('No theme specified');
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the theme object
|
||||
$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');
|
||||
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
|
||||
error_log('Switching to theme: ' . $theme);
|
||||
switch_theme($theme);
|
||||
|
||||
// Check if the switch was successful
|
||||
$active_theme = wp_get_theme();
|
||||
if ($active_theme->get_stylesheet() === $theme) {
|
||||
error_log('Theme activated successfully: ' . $theme);
|
||||
wp_send_json_success(array(
|
||||
'message' => 'Theme activated successfully',
|
||||
'customize_url' => admin_url('customize.php')
|
||||
));
|
||||
} else {
|
||||
error_log('Failed to activate theme: ' . $theme . ', active theme is: ' . $active_theme->get_stylesheet());
|
||||
wp_send_json_error('Failed to activate theme');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user