Fix activation buttons for both free plugins grid and theme panel

This commit is contained in:
Marcus Quinn
2025-03-17 00:51:39 +00:00
parent d50dfd344c
commit 5f64feeaa8
3 changed files with 47 additions and 21 deletions

View File

@ -206,6 +206,11 @@ jQuery(document).ready(function($) {
// Initialize plugin action buttons // Initialize plugin action buttons
function initPluginActions() { function initPluginActions() {
// Remove any existing event handlers to prevent duplicates
$('.plugin-card .install-now').off('click');
$('.plugin-card .update-now').off('click');
$('.plugin-card .activate-now').off('click');
// Install plugin // Install plugin
$('.plugin-card .install-now').on('click', function(e) { $('.plugin-card .install-now').on('click', function(e) {
e.preventDefault(); e.preventDefault();
@ -219,10 +224,13 @@ jQuery(document).ready(function($) {
success: function(response) { success: function(response) {
$button.removeClass('updating-message').addClass('updated-message').text('Installed!'); $button.removeClass('updating-message').addClass('updated-message').text('Installed!');
setTimeout(function() { setTimeout(function() {
$button.removeClass('updated-message install-now') // Replace the button with an activate button
.addClass('activate-now') var $parent = $button.parent();
.text('Activate') $button.remove();
.attr('href', response.activateUrl); $parent.html('<a class="button activate-now" href="' + response.activateUrl + '" data-slug="' + slug + '" aria-label="Activate ' + slug + '">Activate</a>');
// Re-initialize the event handlers
initPluginActions();
}, 1000); }, 1000);
}, },
error: function(response) { error: function(response) {
@ -230,9 +238,7 @@ jQuery(document).ready(function($) {
alert(response.errorMessage); alert(response.errorMessage);
} }
}); });
}); });
// Update plugin // Update plugin
$('.plugin-card .update-now').on('click', function(e) { $('.plugin-card .update-now').on('click', function(e) {
@ -257,15 +263,14 @@ jQuery(document).ready(function($) {
alert(response.errorMessage); alert(response.errorMessage);
} }
}); });
}); });
// Activate plugin // Activate plugin
$('.plugin-card .activate-now').on('click', function(e) { $('.plugin-card .activate-now').on('click', function(e) {
e.preventDefault(); e.preventDefault();
var $button = $(this); var $button = $(this);
var url = $button.attr('href'); var url = $button.attr('href');
var slug = $button.data('slug');
$button.addClass('updating-message').text('Activating...'); $button.addClass('updating-message').text('Activating...');
@ -275,9 +280,10 @@ jQuery(document).ready(function($) {
success: function() { success: function() {
$button.removeClass('updating-message').addClass('updated-message').text('Activated!'); $button.removeClass('updating-message').addClass('updated-message').text('Activated!');
setTimeout(function() { setTimeout(function() {
$button.removeClass('activate-now updated-message') // Replace the button with an active button
.addClass('button-disabled') var $parent = $button.parent();
.text('Active'); $button.remove();
$parent.html('<button type="button" class="button button-disabled" disabled="disabled">Active</button>');
}, 1000); }, 1000);
}, },
error: function() { error: function() {
@ -285,13 +291,15 @@ jQuery(document).ready(function($) {
alert('Failed to activate plugin. Please try again or activate from the Plugins page.'); alert('Failed to activate plugin. Please try again or activate from the Plugins page.');
} }
}); });
}); });
} }
// Initialize theme handlers // Initialize theme handlers
function initThemeHandlers() { function initThemeHandlers() {
// Remove any existing event handlers to prevent duplicates
$('.theme-actions .install-now').off('click');
$('.theme-actions .activate-now').off('click');
// Install theme // Install theme
$('.theme-actions .install-now').on('click', function(e) { $('.theme-actions .install-now').on('click', function(e) {
e.preventDefault(); e.preventDefault();
@ -306,10 +314,13 @@ jQuery(document).ready(function($) {
success: function(response) { success: function(response) {
$button.removeClass('updating-message').addClass('updated-message').text('Installed!'); $button.removeClass('updating-message').addClass('updated-message').text('Installed!');
setTimeout(function() { setTimeout(function() {
$button.removeClass('updated-message install-now') // Replace the button with an activate button
.addClass('activate-now') var $parent = $button.closest('.theme-actions');
.text('Activate') $button.remove();
.attr('href', response.activateUrl); $parent.prepend('<button type="button" class="button button-primary activate-now" data-slug="' + slug + '" data-nonce="' + wpAllstars.nonce + '">Activate</button>');
// Re-initialize the event handlers
initThemeHandlers();
}, 1000); }, 1000);
}, },
error: function(response) { error: function(response) {
@ -340,9 +351,10 @@ jQuery(document).ready(function($) {
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() {
$button.removeClass('activate-now updated-message') // Replace the button with an active button
.addClass('button-disabled') var $parent = $button.closest('.theme-actions');
.text('Active'); $button.remove();
$parent.prepend('<button type="button" class="button button-disabled" disabled="disabled">Active</button>');
}, 1000); }, 1000);
} else { } else {
$button.removeClass('updating-message').text('Activate'); $button.removeClass('updating-message').text('Activate');

View File

@ -22,7 +22,14 @@ if (!defined('ABSPATH')) {
<?php if (current_user_can('install_themes')): ?> <?php if (current_user_can('install_themes')): ?>
<?php <?php
$installed_theme = wp_get_theme('kadence'); $installed_theme = wp_get_theme('kadence');
if ($installed_theme->exists()): ?> $current_theme = wp_get_theme();
$is_active = ($current_theme->get_stylesheet() === 'kadence');
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(wp_create_nonce('wp-allstars-nonce')); ?>">
<?php esc_html_e('Activate'); ?> <?php esc_html_e('Activate'); ?>
</button> </button>

View File

@ -1312,6 +1312,13 @@ function wp_allstars_activate_theme() {
return; return;
} }
// Get the theme object
$theme_obj = wp_get_theme($theme);
if (!$theme_obj->exists() || $theme_obj->errors()) {
wp_send_json_error('Theme does not exist');
return;
}
// Switch the theme // Switch the theme
switch_theme($theme); switch_theme($theme);