| @@ -8,12 +8,15 @@ | ||
| 8 | 8 | installButtonSelector: '.merchant-install-plugin', |
| 9 | 9 | init: function init() { |
| 10 | 10 | this.events(); |
| 11 | 11 | }, |
| 12 | + // Binds install button clicks across any dynamically rendered content. | |
| 12 | 13 | events: function events() { |
| 13 | 14 | var self = this; |
| 14 | 15 | $(document).on('click', self.installButtonSelector, function (e) { |
| 15 | 16 | e.preventDefault(); |
| 17 | + | |
| 18 | + // External plugins come from a URL; wporg ones use a slug. | |
| 16 | 19 | var type = $(this).data('type') === 'external' ? 'external' : 'wporg'; |
| 17 | 20 | var plugin_name = $(this).data('plugin-name'); |
| 18 | 21 | var redirect_to = $(this).data('redirect-to'); |
| 19 | 22 | if (type === 'external') { |
| @@ -19,30 +22,57 @@ | ||
| 19 | 22 | if (type === 'external') { |
| 20 | 23 | var url = $(this).data('plugin-url'); |
| 21 | 24 | self.installExternalPlugin($(this), url, plugin_name, redirect_to); |
| 22 | 25 | } else { |
| 23 | - // self.installPlugin(plugin_name); - TODO: We don't need it yet, but we should rely on the WP API to install plugins from wp.org. | |
| 26 | + var slug = $(this).data('plugin-slug'); | |
| 27 | + self.installPlugin($(this), slug, plugin_name, redirect_to); | |
| 24 | 28 | } |
| 25 | 29 | }); |
| 26 | 30 | }, |
| 31 | + // Handles wporg plugins — resolved server-side via the slug. | |
| 32 | + installPlugin: function installPlugin(button, slug, plugin_name, redirect_to) { | |
| 33 | + this._doInstall(button, { | |
| 34 | + action: 'merchant_install_plugin', | |
| 35 | + slug: slug, | |
| 36 | + plugin_name: plugin_name, | |
| 37 | + nonce: merchantPluginInstallerConfig.nonce | |
| 38 | + }, redirect_to); | |
| 39 | + }, | |
| 40 | + // Handles external plugins — the server downloads from the given URL. | |
| 27 | 41 | installExternalPlugin: function installExternalPlugin(button, url, plugin_name, redirect_to) { |
| 28 | - var data = { | |
| 42 | + this._doInstall(button, { | |
| 29 | 43 | action: 'merchant_install_external_plugin', |
| 30 | 44 | url: url, |
| 31 | 45 | plugin_name: plugin_name, |
| 32 | 46 | nonce: merchantPluginInstallerConfig.nonce |
| 33 | - }; | |
| 47 | + }, redirect_to); | |
| 48 | + }, | |
| 49 | + // Fires the AJAX request, manages button state, and handles the redirect. | |
| 50 | + _doInstall: function _doInstall(button, data, redirect_to) { | |
| 51 | + button.prop('disabled', true); | |
| 34 | 52 | button.text(merchantPluginInstallerConfig.i18n.installingText); |
| 35 | 53 | $.post(ajaxurl, data, function (response) { |
| 36 | 54 | if (!response.success) { |
| 55 | + button.prop('disabled', false); | |
| 37 | 56 | button.text(merchantPluginInstallerConfig.i18n.defaultText); |
| 38 | 57 | alert(response.data.message); |
| 39 | 58 | return; |
| 40 | 59 | } |
| 41 | 60 | button.text(merchantPluginInstallerConfig.i18n.activatingText); |
| 61 | + | |
| 62 | + // Brief delay so the user sees the "Activating" state before the page changes. | |
| 42 | 63 | setTimeout(function () { |
| 43 | - window.location.href = redirect_to; | |
| 64 | + if (redirect_to) { | |
| 65 | + window.location.href = redirect_to; | |
| 66 | + } else { | |
| 67 | + window.location.reload(); | |
| 68 | + } | |
| 44 | 69 | }, 1000); |
| 70 | + }).fail(function () { | |
| 71 | + // Network failure or a non-200 response — restore the button. | |
| 72 | + button.prop('disabled', false); | |
| 73 | + button.text(merchantPluginInstallerConfig.i18n.defaultText); | |
| 74 | + alert(merchantPluginInstallerConfig.i18n.networkErrorText); | |
| 45 | 75 | }); |
| 46 | 76 | } |
| 47 | 77 | }; |
| 48 | 78 | $(document).ready(function () { |