diff --git a/admin/settings.php b/admin/settings.php index 535a808..436943a 100644 --- a/admin/settings.php +++ b/admin/settings.php @@ -510,6 +510,9 @@ function wpa_superstar_settings_page() { ensureMinLoadingTime(function() { $('#wpa-theme-list').html(response.data); $('.wpa-loading-overlay').fadeOut(); + + // Initialize theme installation handlers + initThemeHandlers(); }); } else { console.error('Server returned error:', response); @@ -525,6 +528,75 @@ function wpa_superstar_settings_page() { }); } + function initThemeHandlers() { + // Handle theme installation + $('.install-theme').on('click', function(e) { + e.preventDefault(); + var $button = $(this); + var slug = $button.data('slug'); + + $button.addClass('updating-message').text('Installing...'); + + wp.updates.installTheme({ + slug: slug, + success: function(response) { + $button + .removeClass('updating-message install-theme') + .addClass('button-primary activate-theme') + .text('Activate'); + + // Refresh the theme display + loadTheme(); + }, + error: function(error) { + $button.removeClass('updating-message'); + console.error('Theme installation failed:', error); + if (error.errorMessage) { + alert(error.errorMessage); + } + } + }); + }); + + // Handle theme activation + $('.activate-theme').on('click', function(e) { + e.preventDefault(); + var $button = $(this); + var slug = $button.data('slug'); + + $button.addClass('updating-message').text('Activating...'); + + $.ajax({ + url: ajaxurl, + type: 'POST', + data: { + action: 'switch_theme', + stylesheet: slug, + _wpnonce: '' + }, + success: function(response) { + if (response.success) { + $button + .removeClass('updating-message') + .text('Activated'); + + // Optionally redirect to customize page + window.location.href = ''; + } else { + $button.removeClass('updating-message'); + console.error('Theme activation failed:', response); + alert('Theme activation failed. Please try again.'); + } + }, + error: function(xhr, status, error) { + $button.removeClass('updating-message'); + console.error('Theme activation failed:', error); + alert('Theme activation failed. Please try again.'); + } + }); + }); + } + // Function to ensure minimum loading time function ensureMinLoadingTime(callback) { var currentTime = Date.now();