/** * Shop Toolkit Pro Plugin Recommendations Admin JavaScript */ (function($) { 'use strict'; $(document).ready(function() { // Initialize plugin recommendations PluginRecommendations.init(); }); var PluginRecommendations = { init: function() { this.bindEvents(); this.initFilters(); // Check if recommended plugins notice should be hidden if (window.location.href.indexOf('wp-admin') !== -1) { this.checkRecommendedPluginsStatus(); } }, bindEvents: function() { // Install plugin $(document).on('click', '.install-plugin', this.installPlugin); // Activate plugin $(document).on('click', '.activate-plugin', this.activatePlugin); // Update plugin $(document).on('click', '.update-plugin', this.updatePlugin); // Filter tabs $(document).on('click', '.filter-tab', this.filterPlugins); // Dismiss install notice - Handle .install-dismiss buttons $(document).on('click', '.install-dismiss', this.dismissInstallNotice); // Bulk install required plugins $(document).on('click', '#install-required-plugins', this.bulkInstallRequired); // Bulk install recommended plugins $(document).on('click', '#install-recommended-plugins', this.bulkInstallRecommended); // Update all plugins $(document).on('click', '#update-all-plugins', this.updateAllPlugins); }, initFilters: function() { // Set initial counts this.updateFilterCounts(); }, installPlugin: function(e) { e.preventDefault(); var $button = $(this); var slug = $button.data('slug'); var isLocal = $button.data('is-local') === 1; var originalText = $button.text(); // Disable button and show loading $button.addClass('loading').prop('disabled', true); $.ajax({ url: blogBuildProPlugins.ajaxurl, type: 'POST', data: { action: 'beshop_install_plugin', slug: slug, is_local: isLocal ? '1' : '0', nonce: blogBuildProPlugins.nonce }, success: function(response) { if (response.success) { PluginRecommendations.showMessage($button, response.data.message || blogBuildProPlugins.strings.installed, 'success'); // Update plugin status setTimeout(function() { PluginRecommendations.updatePluginStatus(slug, 'inactive'); // Auto-activate the plugin $button.removeClass('loading').trigger('click'); }, 1000); } else { PluginRecommendations.showMessage($button, response.data.message || blogBuildProPlugins.strings.error, 'error'); $button.removeClass('loading').prop('disabled', false); } }, error: function() { PluginRecommendations.showMessage($button, blogBuildProPlugins.strings.error, 'error'); $button.removeClass('loading').prop('disabled', false); } }); }, activatePlugin: function(e) { e.preventDefault(); var $button = $(this); var file = $button.data('file'); var slug = $button.data('slug'); var originalText = $button.text(); // Disable button and show loading $button.addClass('loading').prop('disabled', true); $.ajax({ url: blogBuildProPlugins.ajaxurl, type: 'POST', data: { action: 'beshop_activate_plugin', file: file, nonce: blogBuildProPlugins.nonce }, success: function(response) { if (response.success) { PluginRecommendations.showMessage($button, blogBuildProPlugins.strings.activated, 'success'); // Update plugin status setTimeout(function() { PluginRecommendations.updatePluginStatus(slug, 'active'); // Check if all recommended plugins are now active PluginRecommendations.checkRecommendedPluginsStatus(); }, 1000); } else { PluginRecommendations.showMessage($button, response.data || blogBuildProPlugins.strings.error, 'error'); $button.removeClass('loading').prop('disabled', false); } }, error: function() { PluginRecommendations.showMessage($button, blogBuildProPlugins.strings.error, 'error'); $button.removeClass('loading').prop('disabled', false); } }); }, updatePlugin: function(e) { e.preventDefault(); var $button = $(this); var slug = $button.data('slug'); var file = $button.data('file'); var isLocal = $button.data('is-local') === 1; var originalText = $button.text(); // Disable button and show loading $button.addClass('loading').prop('disabled', true); $.ajax({ url: blogBuildProPlugins.ajaxurl, type: 'POST', data: { action: 'beshop_update_plugin', slug: slug, file: file, is_local: isLocal ? '1' : '0', nonce: blogBuildProPlugins.nonce }, success: function(response) { if (response.success) { PluginRecommendations.showMessage($button, response.data.message || blogBuildProPlugins.strings.updated, 'success'); // Update plugin status to active (since it was reactivated) setTimeout(function() { PluginRecommendations.updatePluginStatus(slug, 'active'); // Check if all recommended plugins are now active PluginRecommendations.checkRecommendedPluginsStatus(); }, 1000); } else { PluginRecommendations.showMessage($button, response.data.message || blogBuildProPlugins.strings.error, 'error'); $button.removeClass('loading').prop('disabled', false); } }, error: function() { PluginRecommendations.showMessage($button, blogBuildProPlugins.strings.error, 'error'); $button.removeClass('loading').prop('disabled', false); } }); }, filterPlugins: function(e) { e.preventDefault(); var $tab = $(this); var filter = $tab.data('filter'); // Update active tab $('.filter-tab').removeClass('active'); $tab.addClass('active'); // Filter plugin cards $('.plugin-card').each(function() { var $card = $(this); var status = $card.data('status'); var featured = $card.data('featured'); var required = $card.data('required'); var show = false; switch (filter) { case 'all': show = true; break; case 'featured': show = featured == 1; break; case 'required': show = required == 1; break; case 'active': show = status === 'active'; break; case 'inactive': show = status === 'inactive'; break; case 'not-installed': show = status === 'not-installed'; break; } if (show) { $card.removeClass('hidden').fadeIn(300); } else { $card.addClass('hidden').fadeOut(300); } }); // Update URL without reloading if (history.pushState) { var newUrl = window.location.href.split('?')[0] + '?page=beshop-plugins'; if (filter !== 'all') { newUrl += '&filter=' + filter; } history.pushState(null, null, newUrl); } }, updateFilterCounts: function() { var counts = { all: 0, featured: 0, required: 0, active: 0, inactive: 0, 'not-installed': 0 }; $('.plugin-card').each(function() { var $card = $(this); var status = $card.data('status'); var featured = $card.data('featured'); var required = $card.data('required'); counts.all++; counts[status]++; if (featured == 1) { counts.featured++; } if (required == 1) { counts.required++; } }); // Update tab text with counts $('.filter-tab').each(function() { var $tab = $(this); var filter = $tab.data('filter'); var count = counts[filter]; var baseText = $tab.text().split(' (')[0]; if (count > 0) { $tab.text(baseText + ' (' + count + ')'); } else { $tab.text(baseText); } }); }, showMessage: function($button, message, type) { var $card = $button.closest('.plugin-card'); var $message = $('
') .text(message) .addClass(type === 'error' ? 'error' : '') .appendTo($card); // Show message setTimeout(function() { $message.addClass('show'); }, 100); // Hide message after 3 seconds setTimeout(function() { $message.removeClass('show'); setTimeout(function() { $message.remove(); }, 300); }, 3000); }, // NEW: dismissInstallNotice function to handle .install-dismiss button clicks dismissInstallNotice: function(e) { e.preventDefault(); var $button = $(this); var $notice = $button.closest('.notice'); // Determine notice type based on CSS class var noticeType = 'recommended'; // default if ($notice.hasClass('beshop-pro-recommended-notice')) { noticeType = 'recommended'; } else if ($notice.hasClass('beshop-pro-update-notice')) { noticeType = 'update'; } // Send AJAX request to hide notice permanently var requestData = { action: 'beshop_dismiss_plugin_notice', nonce: blogBuildProPlugins.nonce }; // Add notice_type only if it's not an update notice if (noticeType !== 'update') { requestData.notice_type = noticeType; } else { // For update notices, use the specific action requestData.action = 'beshop_dismiss_update_notice'; } $.post(blogBuildProPlugins.ajaxurl, requestData); // Hide the notice with animation $notice.fadeOut(300, function() { $(this).remove(); }); }, bulkInstallRequired: function(e) { e.preventDefault(); var $button = $(this); var originalText = $button.text(); // Disable button and show loading $button.addClass('loading').prop('disabled', true); // Add progress indicator var $progress = $('' + response.data.message + '
' + response.data.message + '
An error occurred while installing plugins. Please try again.
' + response.data.message + '
' + response.data.message + '
An error occurred while installing plugins. Please try again.