|
| 1 | +var cnkt_installer = cnkt_installer || {}; |
| 2 | + |
| 3 | +jQuery(document).ready(function ($) { |
| 4 | + ("use strict"); |
| 5 | + |
| 6 | + var is_loading = false; |
| 7 | + |
| 8 | + /** |
| 9 | + * Install the plugin. |
| 10 | + * |
| 11 | + * @param {Element} el Button element. |
| 12 | + * @param {string} plugin The plugin slug. |
| 13 | + */ |
| 14 | + cnkt_installer.install_plugin = function (el, plugin) { |
| 15 | + // Confirm activation. |
| 16 | + var r = confirm(cnkt_installer_localize.install_now); |
| 17 | + if (r) { |
| 18 | + is_loading = true; |
| 19 | + el.addClass("installing"); |
| 20 | + |
| 21 | + $.ajax({ |
| 22 | + type: "POST", |
| 23 | + url: cnkt_installer_localize.ajax_url, |
| 24 | + data: { |
| 25 | + action: "cnkt_plugin_installer", |
| 26 | + plugin: plugin, |
| 27 | + nonce: cnkt_installer_localize.admin_nonce, |
| 28 | + dataType: "json", |
| 29 | + }, |
| 30 | + success: function (data) { |
| 31 | + if (data) { |
| 32 | + if (data.status === "success") { |
| 33 | + el.attr("class", "activate button button-primary"); |
| 34 | + el.html(cnkt_installer_localize.activate_btn); |
| 35 | + } else { |
| 36 | + el.removeClass("installing"); |
| 37 | + } |
| 38 | + } else { |
| 39 | + el.removeClass("installing"); |
| 40 | + } |
| 41 | + is_loading = false; |
| 42 | + }, |
| 43 | + error: function (status) { |
| 44 | + console.log(status); |
| 45 | + el.removeClass("installing"); |
| 46 | + is_loading = false; |
| 47 | + }, |
| 48 | + }); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + /** |
| 53 | + * Activate the plugin |
| 54 | + * |
| 55 | + * @param {Element} el Button element. |
| 56 | + * @param {string} plugin The plugin slug. |
| 57 | + */ |
| 58 | + cnkt_installer.activate_plugin = function (el, plugin) { |
| 59 | + $.ajax({ |
| 60 | + type: "POST", |
| 61 | + url: cnkt_installer_localize.ajax_url, |
| 62 | + data: { |
| 63 | + action: "cnkt_plugin_activation", |
| 64 | + plugin: plugin, |
| 65 | + nonce: cnkt_installer_localize.admin_nonce, |
| 66 | + dataType: "json", |
| 67 | + }, |
| 68 | + success: function (data) { |
| 69 | + if (data) { |
| 70 | + if (data.status === "success") { |
| 71 | + el.attr("class", "installed button disabled"); |
| 72 | + el.html(cnkt_installer_localize.installed_btn); |
| 73 | + } |
| 74 | + } |
| 75 | + is_loading = false; |
| 76 | + }, |
| 77 | + error: function (xhr, status, error) { |
| 78 | + console.log(status); |
| 79 | + is_loading = false; |
| 80 | + }, |
| 81 | + }); |
| 82 | + }; |
| 83 | + |
| 84 | + /** |
| 85 | + * Install/Activate Button Click. |
| 86 | + * |
| 87 | + * @since 1.0 |
| 88 | + */ |
| 89 | + $(document).on("click", ".cnkt-plugin-installer a.button", function (e) { |
| 90 | + var el = $(this), |
| 91 | + plugin = el.data("slug"); |
| 92 | + |
| 93 | + e.preventDefault(); |
| 94 | + |
| 95 | + if (!el.hasClass("disabled")) { |
| 96 | + if (is_loading) return false; |
| 97 | + |
| 98 | + // Installation |
| 99 | + if (el.hasClass("install")) { |
| 100 | + cnkt_installer.install_plugin(el, plugin); |
| 101 | + } |
| 102 | + |
| 103 | + // Activation |
| 104 | + if (el.hasClass("activate")) { |
| 105 | + cnkt_installer.activate_plugin(el, plugin); |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | +}); |
0 commit comments