Fix AJAX functionality for theme Install and Activate buttons

This commit is contained in:
Marcus Quinn
2025-03-25 02:31:50 +00:00
parent ff306ed32c
commit e19dfecd6c
2 changed files with 68 additions and 53 deletions

View File

@ -114,9 +114,9 @@ class WP_Allstars_Theme_Manager {
$loadingOverlay.remove(); $loadingOverlay.remove();
if (response.success) { if (response.success) {
$container.html(response.data); $container.html(response.data);
// Initialize theme action buttons // Initialize theme handlers - use the global function from admin.js
if (typeof initThemeHandlers === "function") { if (typeof window.initThemeHandlers === "function") {
initThemeHandlers(); window.initThemeHandlers();
} }
} else { } else {
$container.html("<div class=\"notice notice-error\"><p>" + response.data + "</p></div>"); $container.html("<div class=\"notice notice-error\"><p>" + response.data + "</p></div>");
@ -129,46 +129,6 @@ class WP_Allstars_Theme_Manager {
} }
}); });
} }
// Initialize theme handlers
window.initThemeHandlers = function() {
// Activate theme
$(".activate-now").click(function(e) {
e.preventDefault();
var $button = $(this);
var slug = $button.data("slug");
var name = $button.data("name");
var nonce = $button.data("nonce");
$button.text("Activating...").addClass("updating-message").attr("disabled", true);
$.ajax({
url: ajaxurl,
type: "POST",
data: {
action: "wp_allstars_activate_theme",
theme: slug,
_wpnonce: nonce
},
success: function(response) {
if (response.success) {
$button.removeClass("updating-message").addClass("updated-message").text("Activated");
setTimeout(function() {
window.location.reload();
}, 1000);
} else {
$button.removeClass("updating-message").text("Error");
alert("Error: " + response.data);
}
},
error: function(xhr, status, error) {
$button.removeClass("updating-message").text("Error");
alert("Error: " + error);
}
});
});
};
}); });
'; ';
} }

View File

@ -305,26 +305,81 @@ jQuery(document).ready(function($) {
$('.theme-actions .install-now').off('click'); $('.theme-actions .install-now').off('click');
$('.theme-actions .activate-now').off('click'); $('.theme-actions .activate-now').off('click');
// Install theme - use standard WordPress behavior // Install theme - use wp.updates.installTheme AJAX method
$('.theme-actions .install-now').on('click', function(e) { $('.theme-actions .install-now').on('click', function(e) {
// We're not preventing default here - let the standard WordPress installer handle it e.preventDefault();
// Just add the updating message
var $button = $(this); var $button = $(this);
var slug = $button.data('slug'); var slug = $button.data('slug');
$button.addClass('updating-message').text('Installing...');
console.log('Installing theme using standard WordPress URL:', $button.attr('href'));
// The rest will be handled by WordPress core
});
// Activate theme - use standard WordPress behavior $button.addClass('updating-message').text('Installing...');
$('.theme-actions .activate-now').on('click', function(e) {
// We're not preventing default here - let the standard WordPress activation handle it wp.updates.installTheme({
// Just add the updating message slug: slug,
var $button = $(this); success: function(response) {
var slug = $button.data('slug'); $button.removeClass('updating-message').addClass('updated-message').text('Installed!');
$button.addClass('updating-message').text('Activating...'); setTimeout(function() {
console.log('Activating theme using standard WordPress URL:', $button.attr('href')); // Replace the button with an activate button
// The rest will be handled by WordPress core var $parent = $button.parent();
}); $button.remove();
// Create activate URL with nonce
var activateUrl = ajaxurl + '?action=wp_allstars_activate_theme&theme=' + slug + '&_wpnonce=' + wpAllstars.nonce;
$parent.prepend('<a href="' + activateUrl + '" class="button button-primary activate-now" data-slug="' + slug + '" data-name="Kadence" data-nonce="' + wpAllstars.nonce + '">Activate</a>');
// Re-initialize the event handlers
initThemeHandlers();
}, 1000);
},
error: function(response) {
$button.removeClass('updating-message').text('Install');
alert(response.errorMessage || 'Error installing theme');
} }
}); });
});
// Activate theme - use AJAX
$('.theme-actions .activate-now').on('click', function(e) {
e.preventDefault();
var $button = $(this);
var slug = $button.data('slug');
var nonce = $button.data('nonce');
$button.addClass('updating-message').text('Activating...');
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'wp_allstars_activate_theme',
theme: slug,
_wpnonce: wpAllstars.nonce
},
success: function(response) {
if (response.success) {
$button.removeClass('updating-message').addClass('updated-message').text('Activated!');
setTimeout(function() {
// Replace the button with an active button
var $parent = $button.parent();
$button.remove();
$parent.prepend('<button type="button" class="button button-disabled" disabled="disabled">Active</button>');
// Optionally reload the page to show the activated theme
// window.location.reload();
}, 1000);
} else {
$button.removeClass('updating-message').text('Activate');
alert(response.data || 'Error activating theme');
}
},
error: function(xhr, status, error) {
$button.removeClass('updating-message').text('Activate');
alert('Failed to activate theme. Please try again or activate from the Themes page. Error: ' + error);
}
});
});
}
// Expose initThemeHandlers to global scope for use in other scripts
window.initThemeHandlers = initThemeHandlers;
});