| 1 |
"use strict"; |
| 2 |
|
| 3 |
(function ($) { |
| 4 |
'use strict'; |
| 5 |
|
| 6 |
var merchantPluginInstaller = window.merchantPluginInstaller || {}; |
| 7 |
merchantPluginInstaller = { |
| 8 |
installButtonSelector: '.merchant-install-plugin', |
| 9 |
init: function init() { |
| 10 |
this.events(); |
| 11 |
}, |
| 12 |
// Binds install button clicks across any dynamically rendered content. |
| 13 |
events: function events() { |
| 14 |
var self = this; |
| 15 |
$(document).on('click', self.installButtonSelector, function (e) { |
| 16 |
e.preventDefault(); |
| 17 |
|
| 18 |
// External plugins come from a URL; wporg ones use a slug. |
| 19 |
var type = $(this).data('type') === 'external' ? 'external' : 'wporg'; |
| 20 |
var plugin_name = $(this).data('plugin-name'); |
| 21 |
var redirect_to = $(this).data('redirect-to'); |
| 22 |
if (type === 'external') { |
| 23 |
var url = $(this).data('plugin-url'); |
| 24 |
self.installExternalPlugin($(this), url, plugin_name, redirect_to); |
| 25 |
} else { |
| 26 |
var slug = $(this).data('plugin-slug'); |
| 27 |
self.installPlugin($(this), slug, plugin_name, redirect_to); |
| 28 |
} |
| 29 |
}); |
| 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. |
| 41 |
installExternalPlugin: function installExternalPlugin(button, url, plugin_name, redirect_to) { |
| 42 |
this._doInstall(button, { |
| 43 |
action: 'merchant_install_external_plugin', |
| 44 |
url: url, |
| 45 |
plugin_name: plugin_name, |
| 46 |
nonce: merchantPluginInstallerConfig.nonce |
| 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); |
| 52 |
button.text(merchantPluginInstallerConfig.i18n.installingText); |
| 53 |
$.post(ajaxurl, data, function (response) { |
| 54 |
if (!response.success) { |
| 55 |
button.prop('disabled', false); |
| 56 |
button.text(merchantPluginInstallerConfig.i18n.defaultText); |
| 57 |
alert(response.data.message); |
| 58 |
return; |
| 59 |
} |
| 60 |
button.text(merchantPluginInstallerConfig.i18n.activatingText); |
| 61 |
|
| 62 |
// Brief delay so the user sees the "Activating" state before the page changes. |
| 63 |
setTimeout(function () { |
| 64 |
if (redirect_to) { |
| 65 |
window.location.href = redirect_to; |
| 66 |
} else { |
| 67 |
window.location.reload(); |
| 68 |
} |
| 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); |
| 75 |
}); |
| 76 |
} |
| 77 |
}; |
| 78 |
$(document).ready(function () { |
| 79 |
merchantPluginInstaller.init(); |
| 80 |
}); |
| 81 |
})(jQuery); |