PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.1
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
mainwp / assets / js / mainwp-plugins-themes.js

mainwp-plugins-themes.js in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.1, at assets/js/mainwp-plugins-themes.js

1,174 lines 46.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* eslint-disable complexity */
2
3 globalThis.mainwpVars = globalThis.mainwpVars || {};
4
5 //Ignore plugin
6 jQuery(function () {
7 jQuery(document).on('click', 'input[name="plugins"]', function () {
8 if (jQuery(this).is(':checked')) {
9 jQuery('input[name="plugins"]').attr('checked', 'checked');
10 jQuery('input[name="plugin[]"]').attr('checked', 'checked');
11 } else {
12 jQuery('input[name="plugins"]').prop("checked", false);
13 jQuery('input[name="plugin[]"]').prop("checked", false);
14 }
15 });
16 jQuery(document).on('click', 'input[name="themes"]', function () {
17 if (jQuery(this).is(':checked')) {
18 jQuery('input[name="themes"]').attr('checked', 'checked');
19 jQuery('input[name="theme[]"]').attr('checked', 'checked');
20 } else {
21 jQuery('input[name="themes"]').prop("checked", false);
22 jQuery('input[name="theme[]"]').prop("checked", false);
23 }
24 });
25
26 jQuery(document).on('click', '#mainwp-bulk-trust-plugins-action-apply', function () {
27
28 let action = jQuery("#mainwp-bulk-actions").dropdown("get value");
29
30 if (action == 'none')
31 return;
32
33 let slugs = jQuery.map(jQuery("input[name='plugin[]']:checked"), function (el) {
34 return jQuery(el).val();
35 });
36
37 if (slugs.length == 0)
38 return;
39
40 jQuery('#mainwp-bulk-trust-plugins-action-apply').attr('disabled', 'true');
41
42 let data = mainwp_secure_data({
43 action: 'mainwp_trust_plugin',
44 slugs: slugs,
45 do: action
46 });
47
48 jQuery.post(ajaxurl, data, function () {
49 jQuery('#mainwp-bulk-trust-plugins-action-apply').prop("disabled", false);
50 mainwp_fetch_all_active_plugins();
51 }, 'json');
52 });
53 jQuery(document).on('click', '#mainwp-bulk-trust-themes-action-apply', function () {
54 let action = jQuery("#mainwp-bulk-actions").dropdown("get value");
55 if (action == 'none')
56 return;
57
58 let slugs = jQuery.map(jQuery("input[name='theme[]']:checked"), function (el) {
59 return jQuery(el).val();
60 });
61 if (slugs.length == 0)
62 return;
63
64 jQuery('#mainwp-bulk-trust-themes-action-apply').attr('disabled', 'true');
65
66 let data = mainwp_secure_data({
67 action: 'mainwp_trust_theme',
68 slugs: slugs,
69 do: action
70 });
71
72 jQuery.post(ajaxurl, data, function () {
73 jQuery('#mainwp-bulk-trust-themes-action-apply').prop("disabled", false);
74 mainwp_fetch_all_themes();
75 }, 'json');
76 });
77 });
78
79
80 // Manage Plugins -- Fetch plugins
81 globalThis.mainwp_fetch_plugins = function (notFetchContent) {
82 let errors = [];
83 let selected_sites = [];
84 let selected_groups = [];
85 let selected_clients = [];
86
87 if (jQuery('input[name="select_by"]').val() == 'site') {
88 jQuery("input[name='selected_sites[]']:checked").each(function () {
89 selected_sites.push(jQuery(this).val());
90 });
91 if (selected_sites.length == 0) {
92 errors.push(MainWP.I18n.t('Please select at least one website or group or client.'));
93 }
94 } else if (jQuery('input[name="select_by"]').val() == 'client') {
95 jQuery("input[name='selected_clients[]']:checked").each(function () {
96 selected_clients.push(jQuery(this).val());
97 });
98 if (selected_clients.length == 0) {
99 errors.push(MainWP.I18n.t('Please select at least one website or group or client.'));
100 }
101 } else {
102 jQuery("input[name='selected_groups[]']:checked").each(function () {
103 selected_groups.push(jQuery(this).val());
104 });
105 if (selected_groups.length == 0) {
106 errors.push(MainWP.I18n.t('Please select at least one website or group or client.'));
107 }
108 }
109
110
111 let _status = jQuery("#mainwp_plugins_search_by_status").dropdown("get value");
112
113 if (_status == null) {
114 errors.push(MainWP.I18n.t('Please select at least one plugin status.'));
115 }
116
117 if (errors.length > 0) {
118 mainwp_set_message_zone('#mainwp-message-zone', errors.join('<br />'), 'yellow');
119 return;
120 } else {
121 mainwp_set_message_zone('#mainwp-message-zone');
122 }
123 let data = mainwp_secure_data({
124 action: 'mainwp_plugins_search',
125 keyword: jQuery('#mainwp_plugin_search_by_keyword').val(),
126 status: _status,
127 not_criteria: jQuery('#display_sites_not_meeting_criteria').is(':checked'),
128 'groups[]': selected_groups,
129 'sites[]': selected_sites,
130 'clients[]': selected_clients
131 });
132
133 if (notFetchContent) {
134 data.not_fetchdata = 1;
135 }
136
137 if (!notFetchContent) {
138 jQuery('#mainwp-loading-plugins-row').show(); //Silience.
139 }
140
141 jQuery.post(ajaxurl, data, function (response) {
142 if (!notFetchContent) {
143 jQuery('#mainwp-loading-plugins-row').hide();
144 jQuery('#mainwp-plugins-main-content').show();
145
146 if (response?.result) {
147 jQuery('#mainwp-plugins-content').html(response.result);
148 jQuery('#mainwp-plugins-bulk-actions-wapper').html(response.bulk_actions);
149 jQuery('#mainwp-plugins-bulk-actions-wapper .ui.dropdown').dropdown();
150 }
151 }
152 }, 'json');
153 };
154
155
156 /**
157 * MainWP_Plugins.page
158 */
159
160 let pluginCountSent;
161 let pluginCountReceived;
162 let pluginResetAllowed = true;
163 let pluginCurrentProccesingQueueIndex = 0;
164 let pluginCountCurrentProcces = 0;
165 let pluginMaxProcces = mainwpVars.maxThreads;
166
167 jQuery(function () {
168 jQuery(document).on('click', '#mainwp-show-plugins', function () {
169 mainwp_fetch_plugins();
170 });
171
172 jQuery(document).on('click', '#mainwp-install-to-selected-sites', function () {
173 let checkedVals = jQuery('.mainwp-manage-plugin-item-website .mainwp-selected-plugin-site:checked').map(function () {
174 let rowElement = jQuery(this).closest('.mainwp-manage-plugin-item-website');
175 let val = rowElement.attr("site-id");
176 return val;
177 }).get();
178
179 let selectedIds = [];
180 if (Array.isArray(checkedVals)) {
181 jQuery.grep(checkedVals, function (val) {
182 if (jQuery.inArray(val, selectedIds) == -1) {
183 selectedIds.push(val);
184 }
185 });
186 }
187 if (selectedIds.length == 0) {
188 feedback('mainwp-message-zone', MainWP.I18n.t('Please select at least one website.'), 'yellow');
189 } else {
190 jQuery('#mainwp-message-zone').fadeOut(5000);
191 let ids = selectedIds.join("-");
192 let kwd = jQuery('#mainwp_plugin_search_by_keyword').val();
193 if ('' != kwd) {
194 kwd = '&s=' + encodeURIComponent(kwd);
195 }
196 location.href = 'admin.php?page=PluginsInstall&selected_sites=' + ids + kwd;
197 }
198 });
199
200 jQuery('#mainwp-plugins-content .checkbox').on('click', function () {
201 if (jQuery('.mainwp-manage-plugin-item-website .checkbox.checked').length > 0) {
202 jQuery('#mainwp-install-to-selected-sites').show();
203 } else {
204 jQuery('#mainwp-install-to-selected-sites').hide();
205 }
206 });
207
208 jQuery('#mainwp_show_all_active_plugins').on('click', function () {
209 mainwp_fetch_all_active_plugins();
210 return false;
211 });
212
213
214 jQuery(document).on('click', '#mainwp-do-plugins-bulk-actions', function () {
215 let action = jQuery("#mainwp-bulk-actions").dropdown("get value");
216 if (action == '') {
217 return false;
218 }
219
220 jQuery(this).attr('disabled', 'true');
221 jQuery('#mainwp_bulk_action_loading').show();
222 pluginResetAllowed = false;
223 pluginCountSent = 0;
224 pluginCountReceived = 0;
225 let selectedSites = [];
226 let selectedSitePlugins = [];
227 pluginCurrentProccesingQueueIndex = 0;
228 pluginCountCurrentProcces = 0;
229 //Find all checked boxes
230 jQuery('.mainwp-selected-plugin-site:checked').each(function () {
231 let rowElement = jQuery(this).closest('.mainwp-manage-plugin-item-website');
232 let websiteId = jQuery(rowElement).attr('site-id');
233 let pluginSlug = jQuery(rowElement).attr('plugin-slug');
234 selectedSitePlugins.push({ 'siteid': websiteId, 'plugin': pluginSlug });
235 if (!selectedSites.includes(websiteId)) {
236 selectedSites.push(websiteId);
237 }
238 });
239
240 jQuery(selectedSitePlugins).each(function (idx, val) {
241 let itselector = '.mainwp-manage-plugin-item-website[site-id="' + val.siteid + '"][plugin-slug="' + val.plugin + '"]';
242 if (jQuery(itselector).length > 0) {
243 jQuery(itselector).html('<span><i class="clock outline icon"></i> Please wait…</span>');
244 }
245 });
246
247 mainwp_manage_plugins_bulk_actions_perform(action, selectedSites, selectedSitePlugins);
248 pluginResetAllowed = true;
249 });
250
251 jQuery(document).on('click', '.mainwp-edit-plugin-note', function () {
252 let rowEl = jQuery(jQuery(this).parents('tr')[0]);
253 let slug = rowEl.attr('plugin-slug');
254 let name = rowEl.attr('plugin-name');
255 let note = rowEl.find('.esc-content-note').html();
256 jQuery('#mainwp-notes-title').html(decodeURIComponent(name));
257 jQuery('#mainwp-notes-html').html(note == '' ? MainWP.I18n.t('No saved notes. Click the Edit button to edit plugin notes.') : note);
258 jQuery('#mainwp-notes-note').val(note);
259 jQuery('#mainwp-notes-slug').val(slug);
260 mainwp_notes_show();
261 });
262
263 globalThis.mainwp_notes_plugin_save = function () {
264 let slug = jQuery('#mainwp-notes-slug').val();
265 let newnote = jQuery('#mainwp-notes-note').val();
266 newnote = newnote.replaceAll(/\r\n|\r|\n/g, '<br>');
267 let data = mainwp_secure_data({
268 action: 'mainwp_trusted_plugin_notes_save',
269 slug: slug,
270 note: newnote
271 });
272
273 jQuery('#mainwp-notes-status').html('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Saving note. Please wait...')).show();
274
275 jQuery.post(ajaxurl, data, function (pSlug) {
276 return function (response) {
277 let rowEl = jQuery('tr[plugin-slug="' + pSlug + '"]');
278 if (response.result == 'SUCCESS') {
279 jQuery('#mainwp-notes-status').html(MainWP.I18n.t('Note saved successfully.')).addClass('green');
280 rowEl.find('.esc-content-note').html(response?.esc_note_content??'');
281 jQuery('#mainwp-notes-html').html(response?.esc_note_content??'');
282
283 if (newnote == '') {
284 rowEl.find('.mainwp-edit-plugin-note').html('<i class="sticky note outline icon"></i>');
285 } else {
286 rowEl.find('.mainwp-edit-plugin-note').html('<i class="sticky green note icon"></i>');
287 }
288
289 } else if (response.error === undefined) {
290 jQuery('#mainwp-notes-status').html('<i class="times red icon"></i> ' + MainWP.I18n.t('Undefined error occurred while saving your note') + '.');
291 } else {
292 jQuery('#mainwp-notes-status').html('<i class="times red icon"></i> ' + MainWP.I18n.t('Undefined error occurred while saving your note') + ': ' + response.error);
293 }
294 }
295 }(slug), 'json');
296 setTimeout(function () {
297 jQuery('#mainwp-notes-status').fadeOut(300);
298 }, 3000);
299
300 jQuery('#mainwp-notes-html').show();
301 jQuery('#mainwp-notes-editor').hide();
302 jQuery('#mainwp-notes-save').hide();
303 jQuery('#mainwp-notes-edit').show();
304
305 return false;
306 }
307
308 jQuery(document).on('click', '.mainwp-edit-theme-note', function () {
309 let rowEl = jQuery(jQuery(this).parents('tr')[0]);
310 let slug = rowEl.attr('theme-slug');
311 let name = rowEl.attr('theme-name');
312 let note = rowEl.find('.esc-content-note').html();
313 jQuery('#mainwp-notes-modal').removeClass('edit-mode');
314 jQuery('#mainwp-notes-title').html(decodeURIComponent(name));
315 jQuery('#mainwp-notes-html').html(note == '' ? 'No saved notes. Click the Edit button to edit theme notes.' : note);
316 jQuery('#mainwp-notes-note').val(note);
317 jQuery('#mainwp-notes-slug').val(slug);
318 mainwp_notes_show();
319 });
320
321 globalThis.mainwp_notes_theme_save = function () {
322 let slug = jQuery('#mainwp-notes-slug').val();
323 let newnote = jQuery('#mainwp-notes-note').val();
324 newnote = newnote.replaceAll(/\r\n|\r|\n/g, '<br>');
325
326 let data = mainwp_secure_data({
327 action: 'mainwp_trusted_theme_notes_save',
328 slug: slug,
329 note: newnote
330 });
331
332 jQuery('#mainwp-notes-status').html('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Saving note. Please wait...')).show();
333
334 jQuery.post(ajaxurl, data, function (pSlug) {
335 return function (response) {
336 let rowEl = jQuery('tr[theme-slug="' + pSlug + '"]');
337 if (response.result == 'SUCCESS') {
338 jQuery('#mainwp-notes-status').html(MainWP.I18n.t('Note saved successfully.')).addClass('green');
339 rowEl.find('.esc-content-note').html(response?.esc_note_content??'');
340 jQuery('#mainwp-notes-html').html(response?.esc_note_content??'');
341 if (newnote == '') {
342 rowEl.find('.mainwp-edit-theme-note').html('<i class="sticky note outline icon"></i>');
343 } else {
344 rowEl.find('.mainwp-edit-theme-note').html('<i class="sticky green note icon"></i>');
345 }
346 } else if (response.error === undefined) {
347 jQuery('#mainwp-notes-status').html('<i class="times red icon"></i> ' + MainWP.I18n.t('Undefined error occurred while saving your note!'));
348 } else {
349 jQuery('#mainwp-notes-status').html('<i class="times red icon"></i> ' + MainWP.I18n.t('Undefined error occurred while saving your note!') + ': ' + response.error);
350 }
351 }
352 }(slug), 'json');
353 setTimeout(function () {
354 jQuery('#mainwp-notes-status').fadeOut(300);
355 }, 3000);
356
357 jQuery('#mainwp-notes-html').show();
358 jQuery('#mainwp-notes-editor').hide();
359 jQuery('#mainwp-notes-save').hide();
360 jQuery('#mainwp-notes-edit').show();
361
362 return false;
363 }
364 });
365
366
367
368 globalThis.mainwp_show_hide_install_to_selected_sites = function (what) {
369 if ('plugin' == what) {
370 jQuery('#mainwp-plugins-content .checkbox').on('click', function () {
371 if (jQuery('.mainwp-manage-plugin-item-website .checkbox.checked').length > 0) {
372 jQuery('#mainwp-install-to-selected-sites').show();
373 } else {
374 jQuery('#mainwp-install-to-selected-sites').hide();
375 }
376 });
377 } else {
378 jQuery('#mainwp-themes-content .checkbox').on('click', function () {
379 if (jQuery('.mainwp-manage-theme-item-website .checkbox.checked').length > 0) {
380 jQuery('#mainwp-install-themes-to-selected-sites').show();
381 } else {
382 jQuery('#mainwp-install-themes-to-selected-sites').hide();
383 }
384 });
385 }
386 }
387
388 let mainwp_manage_plugins_bulk_actions_next = function (action, selectedSites, selectedSitePlugins) {
389 if (pluginCurrentProccesingQueueIndex < selectedSites.length) {
390 mainwp_manage_plugins_bulk_actions_perform(action, selectedSites, selectedSitePlugins);
391 }
392 mainwp_manage_plugins_bulk_actions_check_done();
393 };
394
395 let mainwp_manage_plugins_bulk_actions_check_done = function () {
396 if (pluginCountReceived == pluginCountSent) {
397 pluginCountReceived = 0;
398 pluginCountSent = 0;
399 jQuery('#mainwp_bulk_action_loading').hide();
400 }
401 };
402
403 // Bulk processing plugins.
404 let mainwp_manage_plugins_bulk_actions_perform = function (action, selectedSites, selectedSitePlugins) {
405
406 let websiteId = 0;
407
408 if (selectedSites[pluginCurrentProccesingQueueIndex]) {
409 websiteId = selectedSites[pluginCurrentProccesingQueueIndex];
410 }
411
412 pluginCurrentProccesingQueueIndex++;
413
414 if (!websiteId) {
415 mainwp_manage_plugins_bulk_actions_next(action, selectedSites, selectedSitePlugins);
416 return;
417 }
418
419 let selectedPlugins = [];
420
421 jQuery(selectedSitePlugins).each(function (idx, val) {
422 if (val.siteid == websiteId) {
423 let itselector = '.mainwp-manage-plugin-item-website[site-id="' + val.siteid + '"][plugin-slug="' + val.plugin + '"]';
424 if (jQuery(itselector).length > 0) {
425 selectedPlugins.push(jQuery(itselector)[0]);
426 }
427 }
428 });
429
430 if (selectedPlugins.length == 0) {
431 mainwp_manage_plugins_bulk_actions_next(action, selectedSites, selectedSitePlugins);
432 return;
433 }
434
435 if ((action == 'activate') || (action == 'delete') || (action == 'deactivate') || (action == 'ignore_updates')) {
436 let pluginsToSend = [];
437 let namesToSend = [];
438 for (let ss of selectedPlugins) {
439 pluginsToSend.push(jQuery(ss).attr('plugin-slug'));
440 namesToSend.push(jQuery(ss).attr('plugin-name'));
441 jQuery(ss).html('<span><i class="ui active inline loader tiny"></i> Please wait…<span>');
442 }
443
444 let data = mainwp_secure_data({
445 action: 'mainwp_plugin_' + action,
446 plugins: pluginsToSend,
447 websiteId: websiteId
448 });
449
450 if (action == 'ignore_updates') {
451 data['names'] = namesToSend;
452 }
453 pluginCountCurrentProcces++;
454 pluginCountSent++;
455
456 jQuery.post(ajaxurl, data, function (response) {
457 pluginCountCurrentProcces--;
458 pluginCountReceived++;
459 for (let ss of selectedPlugins) {
460 let pslug = jQuery(ss).attr('plugin-slug');
461 let itselector = '.mainwp-manage-plugin-item-website[site-id="' + websiteId + '"][plugin-slug="' + pslug + '"]';
462 if (jQuery(itselector).length > 0) {
463 if (response?.error) {
464 jQuery(itselector).html('<span data-tooltip="' + response.error + '" data-inverted="" data-position="left center"><i class="times red icon"></i></span>');
465 } else if (response?.result) {
466 jQuery(itselector).html('<span><i class="green check icon"></i></span>');
467 } else {
468 jQuery(itselector).html('<span data-tooltip="Invalid response from the server, please try again." data-inverted="" data-position="left center"><i class="times red icon"></i></span>');
469 }
470 }
471 }
472 if (response?.result) {
473 for (let ss of selectedPlugins) {
474 let pslug = jQuery(ss).attr('plugin-slug');
475 let itselector = '.mainwp-manage-plugin-item-website[site-id="' + websiteId + '"][plugin-slug="' + pslug + '"]';
476 setTimeout(function () {
477 jQuery(itselector).fadeOut(1000);
478 }, 1000);
479 }
480 }
481 if (pluginCountCurrentProcces < pluginMaxProcces) {
482 mainwp_manage_plugins_bulk_actions_next(action, selectedSites, selectedSitePlugins);
483 } else {
484 mainwp_manage_plugins_bulk_actions_check_done();
485 }
486 }, 'json');
487 }
488 }
489
490 // Fetch plugins for the Auto Update feature
491 let mainwp_fetch_all_active_plugins = function () {
492 let data = mainwp_secure_data({
493 action: 'mainwp_plugins_search_all_active',
494 keyword: jQuery("#mainwp_au_plugin_keyword").val(),
495 status: jQuery("#mainwp_au_plugin_trust_status").val(),
496 plugin_status: jQuery("#mainwp_au_plugin_status").val()
497 });
498
499 jQuery('#mainwp-auto-updates-plugins-content').find('.dimmer').addClass('active');
500
501 jQuery.post(ajaxurl, data, function (response) {
502 response = response.trim();
503 jQuery('#mainwp-auto-updates-plugins-content').find('.dimmer').removeClass('active');
504 jQuery('#mainwp-auto-updates-plugins-table-wrapper').html(response);
505 if (jQuery('#mainwp-auto-updates-plugins-table-wrapper').find('.mainwp-empty-page-placeholder').length > 0) {
506 jQuery('#mainwp-plugin-auto-updates .mainwp-actions-bar').hide();
507 } else {
508 jQuery('#mainwp-plugin-auto-updates .mainwp-actions-bar').show();
509 }
510 });
511 };
512
513 // Fetch themes for the Auto Update feature
514 let mainwp_fetch_all_themes = function () {
515 let data = mainwp_secure_data({
516 action: 'mainwp_themes_search_all',
517 keyword: jQuery("#mainwp_au_theme_keyword").val(),
518 status: jQuery("#mainwp_au_theme_trust_status").val(),
519 theme_status: jQuery("#mainwp_au_theme_status").val()
520 });
521
522 jQuery('#mainwp-auto-updates-themes-content').find('.dimmer').addClass('active');
523
524 jQuery.post(ajaxurl, data, function (response) {
525 jQuery('#mainwp-auto-updates-themes-content').find('.dimmer').removeClass('active');
526 jQuery('#mainwp-auto-updates-themes-table-wrapper').html(response);
527 if (jQuery('#mainwp-auto-updates-themes-table-wrapper').find('.mainwp-empty-page-placeholder').length > 0) {
528 jQuery('#mainwp-theme-auto-updates .mainwp-actions-bar').hide();
529 } else {
530 jQuery('#mainwp-theme-auto-updates .mainwp-actions-bar').show();
531 }
532 });
533 };
534
535 /**
536 * MainWP_Themes.page
537 */
538 jQuery(function () {
539 jQuery(document).on('click', '#mainwp_show_themes', function () {
540 mainwp_fetch_themes();
541 });
542
543 jQuery(document).on('click', '#mainwp-install-themes-to-selected-sites', function () {
544 let checkedVals = jQuery('.mainwp-manage-theme-item-website .mainwp-selected-theme-site:checked').map(function () {
545 let rowElement = jQuery(this).closest('.mainwp-manage-theme-item-website');
546 let val = rowElement.attr('site-id');
547 return val;
548 }).get();
549
550 let selectedIds = [];
551 if (Array.isArray(checkedVals)) {
552 jQuery.grep(checkedVals, function (val) {
553 if (jQuery.inArray(val, selectedIds) == -1) {
554 selectedIds.push(val);
555 }
556 });
557 }
558
559 if (selectedIds.length == 0) {
560 feedback('mainwp-message-zone', MainWP.I18n.t('Please select at least one website.'), 'yellow');
561 } else {
562 jQuery('#mainwp-message-zone').fadeOut(5000);
563 let ids = selectedIds.join("-");
564 let kwd = jQuery('#mainwp_theme_search_by_keyword').val();
565 if ('' != kwd) {
566 kwd = '&s=' + encodeURIComponent(kwd);
567 }
568 location.href = 'admin.php?page=ThemesInstall&selected_sites=' + ids + kwd;
569 }
570 });
571
572 jQuery('#mainwp-themes-content .checkbox').on('click', function () {
573 if (jQuery('.mainwp-manage-theme-item-website .checkbox.checked').length > 0) {
574 jQuery('#mainwp-install-themes-to-selected-sites').show();
575 } else {
576 jQuery('#mainwp-install-themes-to-selected-sites').hide();
577 }
578 });
579
580
581 jQuery(document).on('click', '#mainwp_show_all_active_themes', function () {
582 mainwp_fetch_all_themes();
583 return false;
584 });
585
586 let themeCountSent;
587 let themeCountReceived;
588 let themeResetAllowed = true;
589
590 jQuery(document).on('click', '#mainwp-do-themes-bulk-actions', function () {
591 let action = jQuery("#mainwp-bulk-actions").dropdown("get value");
592 if (action == '' || action == 'none')
593 return;
594
595 jQuery('#mainwp-do-themes-bulk-actions').attr('disabled', 'true');
596 jQuery('#mainwp_bulk_action_loading').show();
597 themeResetAllowed = false;
598 themeCountSent = 0;
599 themeCountReceived = 0;
600 let selectedSites = [];
601 let selectedSiteThemes = [];
602
603 //Find all checked boxes
604 jQuery('.mainwp-selected-theme-site:checked').each(function () {
605 let rowElement = jQuery(this).closest('.mainwp-manage-theme-item-website');
606 let websiteId = jQuery(rowElement).attr('site-id');
607 let theme = jQuery(rowElement).attr('theme-slug');
608 if (action == 'activate' && jQuery(rowElement).attr('is-actived') == 1) {
609 jQuery(rowElement).find('.mainwp-selected-theme-site').attr('checked', false);
610 return;
611 }
612 selectedSiteThemes.push({ 'siteid': websiteId, 'theme': theme });
613 if (!selectedSites.includes(websiteId)) {
614 selectedSites.push(websiteId);
615 }
616 });
617
618
619 jQuery(selectedSites).each(function (idx, val) { // NOSONAR - complex.
620 let websiteId = val;
621 let selectedThemes = [];
622
623 jQuery(selectedSiteThemes).each(function (idx, val) {
624 if (val.siteid == websiteId) {
625 if (jQuery('.mainwp-manage-theme-item-website[site-id="' + val.siteid + '"][theme-slug="' + val.theme + '"]').length > 0) {
626 selectedThemes.push(jQuery('.mainwp-manage-theme-item-website[site-id="' + val.siteid + '"][theme-slug="' + val.theme + '"]')[0]);
627 }
628 }
629 });
630
631 if (selectedThemes.length == 0)
632 return;
633
634 if (action == 'activate' || action == 'ignore_updates') {
635 let themeToActivate = jQuery(selectedThemes[0]).attr('theme-slug');
636 let themesToSend = [];
637 let namesToSend = [];
638
639 let data = mainwp_secure_data({
640 action: 'mainwp_theme_' + action,
641 websiteId: websiteId
642 });
643
644 if (action == 'ignore_updates') {
645 for (let ss of selectedThemes) {
646 themesToSend.push(jQuery(ss).attr('theme-slug'));
647 namesToSend.push(jQuery(ss).attr('theme-name'));
648 }
649 data['themes'] = themesToSend;
650 data['names'] = namesToSend;
651 } else {
652 data['theme'] = themeToActivate;
653 }
654
655 themeCountSent++;
656 jQuery.post(ajaxurl, data, function () {
657 themeCountReceived++;
658 if (themeResetAllowed && themeCountReceived == themeCountSent) {
659 themeCountReceived = 0;
660 themeCountSent = 0;
661 jQuery('#mainwp_bulk_action_loading').hide();
662 jQuery('#mainwp_themes_loading_info').show();
663 mainwp_fetch_themes();
664 }
665 });
666 } else if (action == 'delete') {
667 let themesToDelete = [];
668 for (let ss of selectedThemes) {
669 if (jQuery(ss).attr('not-delete') == 1 || jQuery(ss).attr('is-actived') == 1) {
670 jQuery(ss).find('.mainwp-selected-theme-site').attr('checked', false);
671 continue;
672 }
673 themesToDelete.push(jQuery(ss).attr('theme-slug'));
674 }
675 if (themesToDelete.length == 0) {
676 return;
677 }
678 let data = mainwp_secure_data({
679 action: 'mainwp_theme_delete',
680 themes: themesToDelete,
681 websiteId: websiteId
682 });
683
684 themeCountSent++;
685 jQuery.post(ajaxurl, data, function (response) {
686 if (response.error != undefined && response.error == new Object(response.error)) { // check if .error is object.
687 let entries = Object.entries(response.error);
688 for (let entry of entries) {
689 let warnings = MainWP.I18n.t(entry[0], encodeURIComponent(entry[1])); // entry[0]:id message, entry[1] string value.
690 jQuery('#mainwp-message-zone').after('<div class="ui info message yellow"><i class="ui close icon"></i><span>' + warnings + '</span></div>');
691 }
692 }
693 themeCountReceived++;
694 if (themeResetAllowed && themeCountReceived == themeCountSent) {
695 themeCountReceived = 0;
696 themeCountSent = 0;
697 jQuery('#mainwp_bulk_action_loading').hide();
698 jQuery('#mainwp_themes_loading_info').show();
699 mainwp_fetch_themes();
700 }
701 });
702 }
703 });
704
705 themeResetAllowed = true;
706 if (themeCountReceived == themeCountSent) {
707 themeCountReceived = 0;
708 themeCountSent = 0;
709 jQuery('#mainwp_bulk_action_loading').hide();
710 jQuery('#mainwp_themes_loading_info').show();
711 mainwp_fetch_themes();
712 }
713 });
714
715 });
716
717
718 // Manage Themes -- Fetch themes from child sites
719 globalThis.mainwp_fetch_themes = function (notFetchContent) {
720 let errors = [];
721 let selected_sites = [];
722 let selected_groups = [];
723 let selected_clients = [];
724
725 if (jQuery('input[name="select_by"]').val() == 'site') {
726 jQuery("input[name='selected_sites[]']:checked").each(function () {
727 selected_sites.push(jQuery(this).val());
728 });
729 if (selected_sites.length == 0) {
730 errors.push(MainWP.I18n.t('Please select at least one website or group or client.'));
731 }
732 } else if (jQuery('input[name="select_by"]').val() == 'client') {
733 jQuery("input[name='selected_clients[]']:checked").each(function () {
734 selected_clients.push(jQuery(this).val());
735 });
736 if (selected_clients.length == 0) {
737 errors.push(MainWP.I18n.t('Please select at least one website or group or client.'));
738 }
739 } else {
740 jQuery("input[name='selected_groups[]']:checked").each(function () {
741 selected_groups.push(jQuery(this).val());
742 });
743 if (selected_groups.length == 0) {
744 errors.push(MainWP.I18n.t('Please select at least one website or group or client.'));
745 }
746 }
747
748 let _status = jQuery("#mainwp_themes_search_by_status").dropdown("get value");
749 if (_status == null) {
750 errors.push(MainWP.I18n.t('Please select at least one theme status.'));
751 }
752
753 if (errors.length > 0) {
754 mainwp_set_message_zone('#mainwp-message-zone', errors.join('<br />'), 'yellow');
755 return;
756 } else {
757 mainwp_set_message_zone('#mainwp-message-zone');
758 }
759
760 let data = mainwp_secure_data({
761 action: 'mainwp_themes_search',
762 keyword: jQuery('#mainwp_theme_search_by_keyword').val(),
763 status: _status,
764 not_criteria: jQuery('#display_sites_not_meeting_criteria').is(':checked'),
765 'groups[]': selected_groups,
766 'sites[]': selected_sites,
767 'clients[]': selected_clients
768 });
769
770 if (notFetchContent) {
771 data.not_fetchdata = 1;
772 }
773
774 if (!notFetchContent) {
775 jQuery('#mainwp-loading-themes-row').show();
776 }
777
778 jQuery.post(ajaxurl, data, function (response) {
779 if (!notFetchContent) {
780 jQuery('#mainwp-loading-themes-row').hide();
781 jQuery('#mainwp-themes-main-content').show();
782 if (response?.result) {
783 jQuery('#mainwp-themes-content').html(response.result);
784 jQuery('#mainwp-themes-bulk-actions-wapper').html(response.bulk_actions);
785 jQuery('#mainwp-themes-bulk-actions-wapper .ui.dropdown').dropdown();
786 mainwp_show_hide_install_to_selected_sites( 'theme' );
787 }
788 }
789 }, 'json');
790 };
791
792 /**
793 * Plugins manages.
794 */
795 jQuery(function () {
796 jQuery(document).on('click', '.mainwp-manage-plugin-deactivate', function () {
797 manage_plugin_Action(jQuery(this), 'deactivate');
798 return false;
799 });
800 jQuery(document).on('click', '.mainwp-manage-plugin-activate', function () {
801 manage_plugin_Action(jQuery(this), 'activate');
802 return false;
803 });
804 jQuery(document).on('click', '.mainwp-manage-plugin-delete', function () {
805 let name = jQuery(this).closest('.mainwp-manage-plugin-item-website').attr('plugin-name');
806 let confirmMsg = MainWP.I18n.t('You are about to delete the %1?', name);
807 mainwp_confirm(confirmMsg,() => {
808 manage_plugin_Action(jQuery(this), 'delete');
809 });
810 return false;
811 });
812 });
813
814
815 let manage_plugin_Action = function (elem, what) {
816 let rowElement = jQuery(elem).closest('.mainwp-manage-plugin-item-website');
817 let plugin = rowElement.attr('plugin-slug');
818 let websiteId = rowElement.attr('site-id');
819
820 let data = mainwp_secure_data({
821 action: 'mainwp_widget_plugin_' + what, // same with the widgets.
822 plugin: plugin,
823 websiteId: websiteId
824 });
825 let start_row = '<div class="one wide center aligned middle aligned column"></div><div class="thirteen wide left aligned middle aligned column">';
826 let end_row = '</div>';
827 jQuery(rowElement).html(start_row + '<i class="notched circle loading icon"></i>' + MainWP.I18n.t('Please wait...') + end_row);
828 jQuery.post(ajaxurl, data, function (response) {
829 if (response?.error) {
830 jQuery(rowElement).html(start_row + '<span data-tooltip="' + response.error + '" data-inverted="" data-position="left center"><i class="times red icon"></i></span>' + end_row);
831 } else if (response?.result) {
832 if (what == 'delete') {
833 jQuery(rowElement).html(start_row + '<i class="green check icon"></i> ' + response.result + '</div>');
834 } else {
835 jQuery(rowElement).html(start_row + '<i class="green check icon"></i> ' + response.result + end_row);
836 }
837 setTimeout(function () {
838 jQuery(rowElement).fadeOut(1000);
839 }, 1000);
840 setTimeout(function () {
841 mainwp_fetch_plugins(true);
842 }, 3000);
843 } else {
844 jQuery(rowElement).html(start_row + '<span data-tooltip="Undefined error occurred. Please try again." data-inverted="" data-position="left center"><i class="times red icon"></i></span>' + end_row);
845 }
846 }, 'json');
847
848 return false;
849 };
850
851
852 let manage_plugins_upgrade = function (slug, websiteid) {
853 let msg = MainWP.I18n.t('Are you sure you want to update the plugin on the selected site?');
854 mainwp_confirm(msg, function () {
855 return manage_plugins_upgrade_int(slug, websiteid);
856 }, false, 1);
857 };
858
859 let manage_plugins_upgrade_continueAfterBackup = function (slug, websiteId, websiteHolder) {
860 let siteHolder = websiteHolder;
861 return function () {
862 let data = mainwp_secure_data({
863 action: 'mainwp_upgradeplugintheme',
864 websiteId: websiteId,
865 type: 'plugin',
866 slug: slug
867 });
868 jQuery.ajax({
869 type: "POST",
870 url: ajaxurl,
871 data: data,
872 success: function (response) {
873 if (response.error) {
874 let extErr = getErrorMessageInfo(response.error, 'ui')
875 siteHolder.find('.column.update-column').html(extErr);
876 } else {
877 let res = response.result;
878 let res_error = response.result_error;
879 if (res[slug]) {
880 siteHolder.attr('updated', 1);
881 siteHolder.find('.column.update-column').html('<span data-inverted="" data-position="left center" data-tooltip="' + MainWP.I18n.t('Update successful', 'mainwp') + '"><i class="green check icon"></i></span>');
882 } else if (res_error[slug]) {
883 let _error = res_error[slug];
884 let _icon = '<i class="red times icon"></i>';
885 let roll_error = mainwp_updates_get_rollback_msg(_error);
886 if (roll_error) {
887 _error = roll_error;
888 _icon = mainwpParams.roll_ui_icon;
889 }
890 siteHolder.find('.column.update-column').html('<span data-inverted="" data-position="left center" data-tooltip="' + _error + '">' + _icon + '</span>');
891 } else {
892 siteHolder.find('.column.update-column').html('<i class="red times icon"></i>');
893 }
894 setTimeout(function () {
895 mainwp_fetch_plugins();
896 }, 3000);
897 }
898 },
899 tryCount: 0,
900 retryLimit: 3,
901 endError: function () {
902 siteHolder.find('.column.update-column').html('<i class="red times icon"></i>');
903 },
904 error: function (xhr) {
905 this.tryCount++;
906 if (this.tryCount >= this.retryLimit) {
907 this.endError();
908 return;
909 }
910
911 setTimeout((pRqst, pXhr) => {
912 if (pXhr.status == 404) {
913 //handle error
914 jQuery.ajax(pRqst);
915 } else if (pXhr.status == 500) {
916 //handle error
917 } else {
918 //handle error
919 }
920 }, 500, this, xhr);
921 },
922 dataType: 'json'
923 });
924 }();
925 }
926
927
928 let manage_plugins_upgrade_int = function (slug, websiteId) {
929 let websiteHolder = jQuery('.mainwp-manage-plugin-item-website[plugin-slug="' + slug + '"][site-id="' + websiteId + '"]');
930 websiteHolder.find('.column.update-column').html('<span data-tooltip="Updating..." data-inverted="" data-position="left center"><i class="notched circle loading icon"></i></span>');
931
932 let _callbackAfterBackup = function () {
933 return manage_plugins_upgrade_continueAfterBackup(slug, websiteId, websiteHolder);
934 };
935
936 if (mainwpParams['disable_checkBackupBeforeUpgrade']) {
937 _callbackAfterBackup();
938 return false;
939 }
940
941 let sitesToUpdate = [websiteId];
942 let siteNames = [];
943 siteNames[websiteId] = jQuery(websiteHolder).attr('site-name');
944
945 return mainwp_manages_checkBackups(sitesToUpdate, siteNames, _callbackAfterBackup);
946 };
947
948
949 /**
950 * Themes manage.
951 */
952 jQuery(function () {
953 jQuery(document).on('click', '.mainwp-manages-theme-activate', function () {
954 manages_themeAction(jQuery(this), 'activate');
955 return false;
956 });
957 jQuery(document).on('click', '.mainwp-manages-theme-delete', function () {
958 let name = jQuery(this).closest('.mainwp-manage-theme-item-website').attr('theme-name');
959 let confirmMsg = MainWP.I18n.t('You are about to delete the %1?', name);
960 mainwp_confirm(confirmMsg, () => {
961 manages_themeAction(jQuery(this), 'delete');
962 });
963 return false;
964 });
965 });
966
967 let manages_themeAction = function (elem, what) {
968 let rowElement = jQuery(elem).closest('.mainwp-manage-theme-item-website');
969 let theme = rowElement.attr('theme-slug');
970 let websiteId = rowElement.attr('site-id');
971
972 let data = mainwp_secure_data({
973 action: 'mainwp_widget_theme_' + what, // same with theme widget.
974 theme: theme,
975 websiteId: websiteId
976 });
977
978 let start_row = '<div class="one wide center aligned middle aligned column"></div><div class="thirteen wide left aligned middle aligned column">';
979 let end_row = '</div>';
980
981 jQuery(rowElement).html(start_row + '<i class="notched circle loading icon"></i>' + MainWP.I18n.t('Please wait...') + end_row);
982 jQuery.post(ajaxurl, data, function (response) {
983 if (response?.error) {
984 jQuery(rowElement).html(start_row + '<span data-tooltip="' + response.error + '" data-inverted="" data-position="left center"><i class="times red icon"></i></span>' + end_row);
985 } else if (response?.result) {
986 if (what == 'delete') {
987 jQuery(rowElement).html(start_row + '<i class="green check icon"></i> ' + response.result + end_row);
988 setTimeout(function () {
989 jQuery(rowElement).fadeOut(1000);
990 }, 1000);
991
992 } else {
993 jQuery(rowElement).html(start_row + '<i class="green check icon"></i> ' + response.result + end_row);
994 }
995 setTimeout(function () {
996 mainwp_fetch_themes(true);
997 }, 100);
998 } else {
999 jQuery(rowElement).html(start_row + '<span data-tooltip="Undefined error occurred. Please try again." data-inverted="" data-position="left center"><i class="times red icon"></i></span>' + end_row);
1000 }
1001 }, 'json');
1002
1003 return false;
1004 };
1005
1006
1007 let manage_themes_upgrade_theme = function (slug, websiteid) {
1008 let msg = MainWP.I18n.t('Are you sure you want to update the theme on the selected site?');
1009 mainwp_confirm(msg, function () {
1010 return manage_themes_upgrade_int(slug, websiteid);
1011 }, false, 1);
1012 };
1013
1014 let manage_themes_upgrade_continueAfterBackup = function (slug, websiteId, websiteHolder) {
1015 let siteHolder = websiteHolder;
1016 let pSlug = slug;
1017 return function () {
1018 let data = mainwp_secure_data({
1019 action: 'mainwp_upgradeplugintheme',
1020 websiteId: websiteId,
1021 type: 'theme',
1022 slug: slug
1023 });
1024 jQuery.ajax({
1025 type: "POST",
1026 url: ajaxurl,
1027 data: data,
1028 success: function (response) {
1029 if (response.error) {
1030 let extErr = getErrorMessageInfo(response.error, 'ui')
1031 siteHolder.find('.column.update-column').html(extErr);
1032 } else {
1033 let res = response.result;
1034 let res_error = response.result_error;
1035 if (res[pSlug]) {
1036 siteHolder.attr('updated', 1);
1037 siteHolder.find('.column.update-column').html('<span data-inverted="" data-position="left center" data-tooltip="' + MainWP.I18n.t('Update successful', 'mainwp') + '"><i class="green check icon"></i></span>');
1038 } else if (res_error ? res_error[slug] : false) {
1039 let _error = res_error[slug];
1040 let _icon = '<i class="red times icon"></i>';
1041 let roll_error = mainwp_updates_get_rollback_msg(_error);
1042 if (roll_error) {
1043 _error = roll_error;
1044 _icon = mainwpParams.roll_ui_icon;
1045 }
1046 siteHolder.find('.column.update-column').html('<span data-inverted="" data-position="left center" data-tooltip="' + _error + '">' + _icon + '</span>');
1047 } else {
1048 siteHolder.find('.column.update-column').html('<i class="red times icon"></i>');
1049 }
1050 setTimeout(function () {
1051 mainwp_fetch_themes();
1052 }, 3000);
1053 }
1054 },
1055 tryCount: 0,
1056 retryLimit: 3,
1057 endError: function () {
1058 siteHolder.find('.column.update-column').html('<i class="red times icon"></i>');
1059 },
1060 error: function (xhr) {
1061 this.tryCount++;
1062 if (this.tryCount >= this.retryLimit) {
1063 this.endError();
1064 return;
1065 }
1066 setTimeout((pRqst, pXhr) => {
1067 if (pXhr.status == 404) {
1068 //handle error
1069 jQuery.ajax(pRqst);
1070 } else if (pXhr.status == 500) {
1071 //handle error
1072 } else {
1073 //handle error
1074 }
1075 }, 500, this, xhr);
1076 },
1077 dataType: 'json'
1078 });
1079 }();
1080 }
1081
1082 let manage_themes_upgrade_int = function (slug, websiteId) {
1083 let websiteHolder = jQuery('.mainwp-manage-theme-item-website[theme-slug="' + slug + '"][site-id="' + websiteId + '"]');
1084 websiteHolder.find('.column.update-column').html('<span data-tooltip="Updating..." data-inverted="" data-position="left center"><i class="notched circle loading icon"></i></span');
1085
1086 let _callbackAfterBackup = manage_themes_upgrade_continueAfterBackup(slug, websiteId, websiteHolder);
1087
1088 if (mainwpParams['disable_checkBackupBeforeUpgrade']) {
1089 _callbackAfterBackup();
1090 return false;
1091 }
1092
1093 let sitesToUpdate = [websiteId];
1094 let siteNames = [];
1095 siteNames[websiteId] = jQuery(websiteHolder).attr('site-name');
1096
1097 return mainwp_manages_checkBackups(sitesToUpdate, siteNames, _callbackAfterBackup);
1098 };
1099
1100
1101 /**
1102 * Check Backups.
1103 */
1104 let mainwp_manages_checkBackups = function (sitesToUpdate, siteNames, continueAfterBackup) {
1105 let managesitesShowBusyFunction = function () {
1106 let output = MainWP.I18n.t('Checking if a backup is required for the selected updates...');
1107 mainwpPopup('#managesites-backup-box').getContentEl().html(output);
1108 jQuery('#managesites-backup-all').hide();
1109 jQuery('#managesites-backup-ignore').hide();
1110 mainwpPopup('#managesites-backup-box').init({
1111 title: MainWP.I18n.t("Checking backup settings..."), callback: function () {
1112 mainwpVars.bulkManageSitesTaskRunning = false;
1113 mainwp_forceReload();
1114 }
1115 });
1116
1117 };
1118
1119 let managesitesShowBusyTimeout = setTimeout(managesitesShowBusyFunction, 300);
1120
1121 //Step 2: Check if backups are ok.
1122 let data = mainwp_secure_data({
1123 action: 'mainwp_checkbackups',
1124 sites: sitesToUpdate
1125 });
1126
1127 jQuery.ajax({
1128 type: "POST",
1129 url: ajaxurl,
1130 data: data,
1131 success: function (pSiteNames) {
1132 return function (response) {
1133 clearTimeout(managesitesShowBusyTimeout);
1134
1135 mainwpPopup('#managesites-backup-box').close();
1136 let siteFeedback;
1137
1138 if (response?.result?.sites) {
1139 siteFeedback = [];
1140 for (let currSiteId in response['result']['sites']) {
1141 if (!response['result']['sites'][currSiteId]) {
1142 siteFeedback.push(currSiteId);
1143 }
1144 }
1145 if (siteFeedback.length == 0)
1146 siteFeedback = undefined;
1147 }
1148
1149 if (siteFeedback != undefined) {
1150 mainwp_managesites_prepare_backup_popup(response, pSiteNames, siteFeedback);
1151 mainwpPopup('#managesites-backup-box').init({
1152 title: MainWP.I18n.t("Full backup required!"), callback: function () {
1153 continueAfterBackup = undefined;
1154 mainwp_forceReload();
1155 }
1156 });
1157
1158 return false;
1159 }
1160 if (continueAfterBackup != undefined) {
1161 continueAfterBackup();
1162 }
1163
1164 }
1165 }(siteNames),
1166 error: function () {
1167 mainwpPopup('#managesites-backup-box').close(true);
1168 },
1169 dataType: 'json'
1170 });
1171
1172 return false;
1173 };
1174