wp-optimize
/
vendor
/
team-updraft
/
common-libs
/
src
/
updraft-plugin-deinstall-dialog
/
assets
/
js
/
bulk-deinstall.js
bulk-deinstall.js in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance trunk, at vendor/team-updraft/common-libs/src/updraft-plugin-deinstall-dialog/assets/js/bulk-deinstall.js
| 1 | jQuery(function($) { |
| 2 | |
| 3 | // Force the use of the latest bulk deactivation dialog if there are multiple classes with different versions (e.g. Updraft_Deinstall_Dialog_v1, Updraft_Deinstall_Dialog_v2) |
| 4 | if (!$('body.updraft-deinstall-dialog-v1').length) return; |
| 5 | |
| 6 | var data = window.updraft_bulk_deinstall_data_v1; |
| 7 | |
| 8 | $(document).on('change', '#updraft-bulk-deinstall-dialog input[name="updraft_remove_data[]"]', function() { |
| 9 | var any_checked = $('#updraft-bulk-deinstall-dialog input[name="updraft_remove_data[]"]:checked').length > 0; |
| 10 | $('#updraft-bulk-deinstall-dialog-deactivate-button').text( |
| 11 | any_checked ? data.deactivate_and_remove_label : data.deactivate_label |
| 12 | ); |
| 13 | }); |
| 14 | |
| 15 | $('#doaction, #doaction2').on('click', function (e) { |
| 16 | e.preventDefault(); |
| 17 | e.stopPropagation(); |
| 18 | if (true === $('#updraft-bulk-deinstall-dialog').dialog('isOpen') || true === $('#updraft-bulk-deinstall-dialog').parents('.ui-dialog').is(':visible') || $('#updraft-bulk-deinstall-dialog').length) return; |
| 19 | var $form = $(this).closest('form'); |
| 20 | var bulk_deactivation_cancelled, button_clicked = false, bulk_deactivation_deferred; |
| 21 | |
| 22 | var plugins_deactivated = $('input[name="checked[]"]:checked', $form) |
| 23 | .filter(function() { |
| 24 | return $(this).closest('tr.active').length > 0; |
| 25 | }) |
| 26 | .map(function () { |
| 27 | return $(this).val(); |
| 28 | }) |
| 29 | .get(); |
| 30 | |
| 31 | var selected_action = ''; |
| 32 | if ($(e.target).is('#doaction2')) { |
| 33 | selected_action = $('select[name="action2"]', $form).val(); |
| 34 | } else { |
| 35 | selected_action = $('select[name="action"]', $form).val(); |
| 36 | } |
| 37 | |
| 38 | var target_plugins = plugins_deactivated.filter(function (slug) { |
| 39 | return window.updraft_bulk_deinstall_plugins_data.some(function (p) { |
| 40 | return p.slug === slug; |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | if (selected_action === 'deactivate-selected' && target_plugins.length > 0) { |
| 45 | bulk_deactivation_deferred = $.Deferred(); |
| 46 | |
| 47 | var plugins = target_plugins |
| 48 | .map(function(slug) { |
| 49 | return window.updraft_bulk_deinstall_plugins_data.find(function(p) { |
| 50 | return p.slug === slug; |
| 51 | }); |
| 52 | }) |
| 53 | .filter(Boolean); |
| 54 | |
| 55 | var other_count = plugins_deactivated.length - target_plugins.length; |
| 56 | var total_count = other_count + plugins.length; |
| 57 | var other_message = "object" === typeof wp.i18n ? wp.i18n.sprintf(data.other_message, other_count) : data.other_message.replace('%d', other_count); |
| 58 | |
| 59 | var names = plugins.map(function(p) { return p.name; }); |
| 60 | var name_list = names.length <= 2 ? names.join(' and ') : names.slice(0, -1).join(', ') + ', and ' + names[names.length - 1]; |
| 61 | var cancel_label = data.cancel_label; |
| 62 | if (other_count > 0) { |
| 63 | cancel_label = "object" === typeof wp.i18n ? wp.i18n.sprintf(data.deactivate_non_teamupdraft_label, other_count) : data.deactivate_non_teamupdraft_label.replace('%d', other_count); |
| 64 | } |
| 65 | |
| 66 | data.deactivate_and_remove_label = data.deactivate_label = "object" === typeof wp.i18n ? wp.i18n.sprintf(data.deactivate_label, total_count) : data.deactivate_label.replace('%d', total_count); |
| 67 | |
| 68 | data.intro = "object" === typeof wp.i18n ? wp.i18n.sprintf(data.intro, target_plugins.length) : data.intro.replace('%d', target_plugins.length); |
| 69 | |
| 70 | var dialog_html = wp.template('updraft-bulk-deinstall-dialog-'+data.dialog_version)({ |
| 71 | plugins: plugins, |
| 72 | intro: data.intro, |
| 73 | other_count: other_count, |
| 74 | other_message: other_message, |
| 75 | label_remove: data.remove_label |
| 76 | }); |
| 77 | |
| 78 | $('<div id="updraft-bulk-deinstall-dialog" />').html(dialog_html).dialog({ |
| 79 | title: data.dialog_title, |
| 80 | modal: true, |
| 81 | closeOnEscape: false, |
| 82 | draggable: false, |
| 83 | width: 500, |
| 84 | height: 300, |
| 85 | min_height: 400, |
| 86 | max_height: 650, |
| 87 | classes: { |
| 88 | "ui-dialog": "updraft-bulk-dialog-"+data.dialog_version |
| 89 | }, |
| 90 | dialogClass: "updraft-bulk-dialog-"+data.dialog_version, |
| 91 | buttons: [ |
| 92 | { |
| 93 | text: data.deactivate_label, |
| 94 | id: 'updraft-bulk-deinstall-dialog-deactivate-button', |
| 95 | click: function() { |
| 96 | var $btn = $('#updraft-bulk-deinstall-dialog-deactivate-button'); |
| 97 | if ($btn.hasClass('udp-loading')) return; |
| 98 | $btn.addClass('udp-loading').html('<span class="udp-spinner" aria-hidden="true"></span>' + $btn.text()); |
| 99 | button_clicked = true; |
| 100 | var $dialog = $('#updraft-bulk-deinstall-dialog'); |
| 101 | $dialog.dialog('option', 'beforeClose', function() { return false; }); |
| 102 | var checked_slugs = $('#updraft-deinstall-dialog-bulk-deactivate-form input[name="updraft_remove_data[]"]:checked') |
| 103 | .map(function() { return $(this).val(); }) |
| 104 | .get(); |
| 105 | |
| 106 | var plugin_handlers = []; |
| 107 | var checked_handlers = []; |
| 108 | |
| 109 | target_plugins.forEach(function(slug) { |
| 110 | var plugin = window.updraft_bulk_deinstall_plugins_data.find(function(p) { |
| 111 | return p.slug === slug; |
| 112 | }); |
| 113 | if (!plugin) return; |
| 114 | plugin_handlers.push(plugin.handler); |
| 115 | if (checked_slugs.indexOf(slug) !== -1) { |
| 116 | checked_handlers.push(plugin.handler); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | if (checked_handlers.length > 0) { |
| 121 | var form_data = [ |
| 122 | { name: '_nonce', value: data.nonce }, |
| 123 | { name: 'action', value: 'bulk_deinstall_confirm_'+data.dialog_version }, |
| 124 | ]; |
| 125 | checked_handlers.forEach(function(handler) { |
| 126 | form_data.push({ name: 'plugins[]', value: handler }); |
| 127 | form_data.push({ name: handler + '_deinstall_option', value: 'yes' }); |
| 128 | }); |
| 129 | |
| 130 | $.post(ajaxurl, form_data).always(function() { |
| 131 | $dialog.dialog('option', 'beforeClose', null); |
| 132 | $dialog.dialog('close'); |
| 133 | }); |
| 134 | } else { |
| 135 | setTimeout(function() { |
| 136 | $dialog.dialog('option', 'beforeClose', null); |
| 137 | $dialog.dialog('close'); |
| 138 | }, 400); |
| 139 | } |
| 140 | } |
| 141 | }, |
| 142 | { |
| 143 | text: cancel_label, |
| 144 | id: 'updraft-bulk-deinstall-dialog-cancel-button', |
| 145 | click: function() { |
| 146 | button_clicked = true; |
| 147 | bulk_deactivation_cancelled = true; |
| 148 | $(this).dialog('close'); |
| 149 | } |
| 150 | } |
| 151 | ], |
| 152 | open: function() { |
| 153 | $('.ui-widget-overlay').on('mousedown', function() { |
| 154 | $('#updraft-bulk-deinstall-dialog').dialog('close'); |
| 155 | }); |
| 156 | }, |
| 157 | close: function() { |
| 158 | if (bulk_deactivation_deferred) bulk_deactivation_deferred.resolve(); |
| 159 | } |
| 160 | }).parent().find('.ui-dialog-titlebar-close').hide(); |
| 161 | |
| 162 | jQuery.when(bulk_deactivation_deferred).done(function() { |
| 163 | bulk_deactivation_deferred = undefined; |
| 164 | if (bulk_deactivation_cancelled) { |
| 165 | target_plugins.forEach(function(slug) { |
| 166 | $('input[value="' + slug + '"]', $form).prop('checked', false); |
| 167 | }); |
| 168 | } |
| 169 | if ($('input[name="checked[]"]:checked', $form).length && button_clicked && (other_count || target_plugins)) { |
| 170 | $form.trigger('submit'); |
| 171 | // $form[0].submit(); |
| 172 | } |
| 173 | $('#updraft-bulk-deinstall-dialog').dialog('close').remove(); |
| 174 | }); |
| 175 | } |
| 176 | }); |
| 177 | }); |
| 178 |