| 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 |
events: function events() { |
| 13 |
var self = this; |
| 14 |
$(document).on('click', self.installButtonSelector, function (e) { |
| 15 |
e.preventDefault(); |
| 16 |
var type = $(this).data('type') === 'external' ? 'external' : 'wporg'; |
| 17 |
var plugin_name = $(this).data('plugin-name'); |
| 18 |
var redirect_to = $(this).data('redirect-to'); |
| 19 |
if (type === 'external') { |
| 20 |
var url = $(this).data('plugin-url'); |
| 21 |
self.installExternalPlugin($(this), url, plugin_name, redirect_to); |
| 22 |
} 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. |
| 24 |
} |
| 25 |
}); |
| 26 |
}, |
| 27 |
installExternalPlugin: function installExternalPlugin(button, url, plugin_name, redirect_to) { |
| 28 |
var data = { |
| 29 |
action: 'merchant_install_external_plugin', |
| 30 |
url: url, |
| 31 |
plugin_name: plugin_name, |
| 32 |
nonce: merchantPluginInstallerConfig.nonce |
| 33 |
}; |
| 34 |
button.text(merchantPluginInstallerConfig.i18n.installingText); |
| 35 |
$.post(ajaxurl, data, function (response) { |
| 36 |
if (!response.success) { |
| 37 |
button.text(merchantPluginInstallerConfig.i18n.defaultText); |
| 38 |
alert(response.data.message); |
| 39 |
return; |
| 40 |
} |
| 41 |
button.text(merchantPluginInstallerConfig.i18n.activatingText); |
| 42 |
setTimeout(function () { |
| 43 |
window.location.href = redirect_to; |
| 44 |
}, 1000); |
| 45 |
}); |
| 46 |
} |
| 47 |
}; |
| 48 |
$(document).ready(function () { |
| 49 |
merchantPluginInstaller.init(); |
| 50 |
}); |
| 51 |
})(jQuery); |