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-ui.js

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

1,323 lines 51.4 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.wp = globalThis.wp || {};
4 globalThis.mainwpVars = globalThis.mainwpVars || {};
5
6 let executingUpdateCategories = false;
7 let queueUpdateCategories = 0;
8
9 function reload_init() {
10 read_current_url();
11 if (typeof wpOnload == 'function')
12 wpOnload();
13 managebackups_init();
14 managesites_init();
15 // Update form action URL, workaround for browser without History API
16 jQuery('#wpbody-content form').each(function () {
17 if (jQuery(this).attr('action') == '')
18 jQuery(this).attr('action', mainwp_current_url);
19 });
20 stick_element_init();
21 }
22
23 /** AJAX page load **/
24 let mainwp_current_url = '';
25 function read_current_url() {
26 mainwp_current_url = document.location.href.replace(/^.*?\/([^/]*?)\/?$/i, '$1');
27 return mainwp_current_url;
28 }
29 function load_url(href, obj, e) { // NOSONAR - complex.
30 let page = href.match(/page=/i) ? href.replace(/^.*\?page=([^&]+).*?$/i, '$1') : ''; // NOSONAR - safe with run time error.
31 if (page || href == 'index.php') {
32 if (!jQuery('body').hasClass('mainwp-ui-page')) {
33 return;
34 }
35
36 if (e !== undefined)
37 e.preventDefault();
38
39 jQuery('#wpbody-content').html('<div class="mainwp-loading"><img src="images/loading.gif" /> ' + MainWP.I18n.t('Please wait...') + '</div>');
40 if (jQuery(obj).hasClass('menu-top')) {
41 let top = jQuery(obj).closest('li.menu-top');
42 jQuery('#adminmenu .current').removeClass('current').addClass('wp-not-current-submenu');
43 jQuery('.wp-has-current-submenu').removeClass('wp-has-current-submenu').addClass('wp-not-current-submenu');
44 if (top.hasClass('wp-has-submenu')) {
45 top.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu');
46 jQuery(obj).removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu');
47 } else {
48 top.removeClass('wp-not-current-submenu').addClass('current');
49 jQuery(obj).removeClass('wp-not-current-submenu').addClass('current');
50 }
51 top.find('li.wp-first-item').addClass('current');
52 } else {
53 jQuery('#adminmenu .current').removeClass('current');
54 jQuery(obj).closest('li').addClass('current');
55 let top = jQuery(obj).closest('li.menu-top');
56 if (top.hasClass('wp-not-current-submenu')) {
57 jQuery('.wp-has-current-submenu').removeClass('wp-has-current-submenu').addClass('wp-not-current-submenu');
58 top.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu');
59 }
60 top.find('a.menu-top').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu');
61 }
62 if (page) {
63 jQuery.get(ajaxurl, {
64 action: 'mainwp_load_title',
65 page: page,
66 nonce: mainwp_ajax_nonce
67 }, function (data) {
68 document.title = data;
69 });
70 jQuery.get(ajaxurl, {
71 action: 'mainwp_load',
72 page: page,
73 nonce: mainwp_ajax_nonce
74 }, function (data) {
75 pagenow = page; // NOSONAR - wp variable.
76 data += '<div class="clear"></div>';
77 jQuery('#wpbody-content').html(data);
78 reload_init();
79 });
80 } else {
81 jQuery.get(ajaxurl, {
82 action: 'mainwp_load_dashboard_title',
83 nonce: mainwp_ajax_nonce
84 }, function (data) {
85 document.title = data;
86 });
87 jQuery.get(ajaxurl, {
88 action: 'mainwp_load_dashboard',
89 nonce: mainwp_ajax_nonce
90 }, function (data) {
91 pagenow = 'dashboard';
92 data += '<div class="clear"></div>';
93 jQuery('#wpbody-content').html(data);
94 reload_init();
95 postboxes.init(pagenow);
96 mainwp_ga_getstats();
97 });
98 }
99
100 }
101 }
102
103 globalThis.onpopstate = function (e) {
104 read_current_url();
105 if (e.state)
106 load_url(mainwp_current_url, e.state.anchor);
107 }
108
109
110
111
112 function scroll_element() {
113 let top = jQuery(this).scrollTop();
114 let start = 20;
115 jQuery('.stick-to-window').each(function () {
116 let init = jQuery(this).data('init-position');
117 if (top > init.top)
118 jQuery(this).stop().animate({ top: (top - init.top) + init.top + start }, 1000);
119 else
120 jQuery(this).stop().css({ top: init.top });
121 });
122 }
123 function stick_element_init() {
124 jQuery('.stick-to-window').each(function () {
125 let pos = jQuery(this).position();
126 jQuery(this).css({
127 position: 'absolute',
128 top: pos.top,
129 left: pos.left
130 });
131 jQuery(this).data('init-position', pos);
132 });
133 }
134 function stick_element_reset() {
135 jQuery('.stick-to-window').each(function () {
136 jQuery(this).css({
137 position: 'static',
138 top: 0,
139 left: 0
140 });
141 });
142 stick_element_init();
143 scroll_element();
144 }
145 jQuery(function () {
146 jQuery(globalThis).trigger('scroll', scroll_element).trigger('resize', stick_element_reset);
147 stick_element_init();
148 });
149
150 // eslint-disable-next-line complexity
151 globalThis.mainwp_confirm = function (msg, confirmed_callback, cancelled_callback, updateType, multiple, extra) { // updateType: 1 single update, 2 multi update
152 let confVal;
153 if (jQuery('#mainwp-disable-update-confirmations').length > 0) {
154 confVal = jQuery('#mainwp-disable-update-confirmations').val();
155 }
156 if (jQuery('#mainwp-disable-update-confirmations').length > 0 && updateType !== undefined && updateType !== false && (confVal == 2 || (confVal == 1 && updateType == 1)) && confirmed_callback && typeof confirmed_callback == 'function') {
157 confirmed_callback();
158 return;
159 }
160
161
162 jQuery('#mainwp-modal-confirm .content-massage').html(msg);
163
164 jQuery('#mainwp-confirm-form').hide();
165 jQuery('#mainwp-confirm-input').val('');
166
167 if (extra !== undefined && extra !== false) {
168 jQuery('#mainwp-confirm-form').show();
169 jQuery('#mainwp-confirm-form').find('label').html('Type ' + extra + ' to confirm');
170 }
171
172 let opts = {
173 onApprove: function () {
174 if (extra !== undefined && extra !== false) {
175 let extraValue = jQuery('#mainwp-confirm-input').val();
176 if (confirmed_callback && typeof confirmed_callback == 'function') {
177 if (extraValue === extra) {
178 confirmed_callback();
179 } else {
180 jQuery('#mainwp-confirm-input').val('').trigger('focus').transition('shake');
181 return false;
182 }
183 }
184 } else if (confirmed_callback && typeof confirmed_callback == 'function') {
185 confirmed_callback();
186 }
187 },
188 onDeny() {
189 if (cancelled_callback && typeof cancelled_callback == 'function')
190 cancelled_callback();
191 }
192 }
193
194 if (multiple) {
195 opts.allowMultiple = true;
196 }
197
198 jQuery('#mainwp-modal-confirm').modal(opts).modal('show');
199
200 // if it is update confirm and then display the update confirm notice text
201 if (updateType !== undefined && updateType !== false && (updateType == 1 || updateType == 2)) {
202 jQuery('#mainwp-modal-confirm .update-confirm-notice ').show();
203 }
204 }
205
206 /**
207 * Select sites
208 */
209 jQuery(document).on('change', '.mainwp_selected_sites_item input:checkbox', function () {
210 if (jQuery(this).is(':checked'))
211 jQuery(this).parent().addClass('selected_sites_item_checked');
212 else
213 jQuery(this).parent().removeClass('selected_sites_item_checked');
214 mainwp_site_select();
215 });
216
217 jQuery(document).on('change', '.mainwp_selected_groups_item input:checkbox', function () {
218 if (jQuery(this).is(':checked'))
219 jQuery(this).parent().addClass('selected_groups_item_checked');
220 else
221 jQuery(this).parent().removeClass('selected_groups_item_checked');
222 mainwp_group_select();
223 });
224
225 jQuery(document).on('change', '.mainwp_selected_clients_item input:checkbox', function () {
226 if (jQuery(this).is(':checked'))
227 jQuery(this).parent().addClass('selected_clients_item_checked');
228 else
229 jQuery(this).parent().removeClass('selected_clients_item_checked');
230 mainwp_client_select();
231 });
232
233
234 jQuery(function () {
235 // seems not used.
236 jQuery('.mainwp_selected_sites_item input:radio').on('change', function () {
237 if (jQuery(this).is(':checked')) {
238 jQuery(this).parent().addClass('selected_sites_item_checked');
239 jQuery(this).parent().parent().find('.mainwp_selected_sites_item input:radio:not(:checked)').parent().removeClass('selected_sites_item_checked');
240 } else
241 jQuery(this).parent().removeClass('selected_sites_item_checked');
242 mainwp_site_select();
243 });
244 // seems not used.
245 jQuery('.mainwp_selected_groups_item input:radio').on('change', function () {
246 if (jQuery(this).is(':checked')) {
247 jQuery(this).parent().addClass('selected_groups_item_checked');
248 jQuery(this).parent().parent().find('.mainwp_selected_groups_item input:radio:not(:checked)').parent().removeClass('selected_groups_item_checked');
249 } else
250 jQuery(this).parent().removeClass('selected_groups_item_checked');
251 });
252 });
253
254 let mainwp_update_selection_counter = function () {
255 let parent = jQuery('.mainwp_select_sites_wrapper');
256 let tab = parent.find('input[name="select_sites_tab"]').val();
257 let count = 0;
258
259 if (tab == 'site') {
260 count = parent.find('.mainwp-select-sites-list INPUT:checkbox:checked').length;
261 } else if (tab == 'staging') {
262 count = parent.find('#mainwp-select-staging-sites-list INPUT:checkbox:checked').length;
263 } else if (tab == 'client') {
264 count = parent.find('#mainwp-select-clients-list INPUT:checkbox:checked').length;
265 } else if (tab == 'group') {
266 count = parent.find('.mainwp-select-groups-list INPUT:checkbox:checked').length;
267 }
268
269 let counter = parent.find('.mainwp-sites-selection-counter');
270 counter.text(count);
271
272 let wrapper = parent.find('.mainwp-selection-counter-wrapper');
273 let selectedText = parent.find('.mainwp-selected-text');
274
275 if (wrapper.length > 0) {
276 if (count > 0) {
277 counter.removeClass('grey').addClass('green');
278 selectedText.removeClass('grey').addClass('green');
279 wrapper.css('cursor', 'pointer')
280 .attr('data-tooltip', 'Create a tag with selected sites')
281 .attr('data-position', 'bottom right')
282 .attr('data-inverted', '');
283
284 if (!wrapper.find('.tag.icon').length) {
285 counter.before('<i class="tag small green icon"></i> ');
286 }
287 } else {
288 counter.removeClass('green').addClass('grey');
289 selectedText.removeClass('green').addClass('grey');
290 wrapper.css('cursor', 'default')
291 .removeAttr('data-tooltip')
292 .removeAttr('data-position')
293 .removeAttr('data-inverted');
294
295 wrapper.find('.tag.icon').remove();
296 }
297 }
298 };
299
300 let mainwp_site_select = function () {
301 mainwp_newpost_updateCategories();
302 mainwp_update_selection_counter();
303 };
304 let mainwp_group_select = function () {
305 mainwp_newpost_updateCategories();
306 mainwp_update_selection_counter();
307 };
308 let mainwp_client_select = function () {
309 mainwp_newpost_updateCategories();
310 mainwp_update_selection_counter();
311 };
312
313 let mainwp_ss_select = function (me, val) {
314 let parent = jQuery(me).closest('.mainwp_select_sites_wrapper');
315 let tab = parent.find('input[name="select_sites_tab"]').val();
316 if (tab == 'site') {
317 parent.find('.mainwp-select-sites-list .item:not(.no-select) INPUT:enabled:checkbox').each(function () {
318 jQuery(this).attr('checked', val).trigger("change");
319 if (val) {
320 jQuery(this).closest('.item.checkbox').checkbox('set checked');
321 }
322 else {
323 jQuery(this).closest('.item.checkbox').checkbox('set unchecked');
324 }
325 });
326 } else if (tab == 'staging') {
327 parent.find('#mainwp-select-staging-sites-list .item:not(.no-select) INPUT:enabled:checkbox').each(function () {
328 jQuery(this).attr('checked', val).trigger("change");
329 if (val) {
330 jQuery(this).closest('.item.checkbox').checkbox('set checked');
331 }
332 else {
333 jQuery(this).closest('.item.checkbox').checkbox('set unchecked');
334 }
335 });
336 } else if (tab == 'client') {
337 parent.find('#mainwp-select-clients-list .item:not(.no-select) INPUT:enabled:checkbox').each(function () {
338 jQuery(this).attr('checked', val).trigger("change");
339 if (val) {
340 jQuery(this).closest('.item.checkbox').checkbox('set checked');
341 }
342 else {
343 jQuery(this).closest('.item.checkbox').checkbox('set unchecked');
344 }
345 });
346
347 } else { //group
348 parent.find('.mainwp-select-groups-list .item:not(.no-select) INPUT:enabled:checkbox').each(function () {
349 jQuery(this).attr('checked', val).trigger("change");
350 if (val) {
351 jQuery(this).closest('.item.checkbox').checkbox('set checked');
352 }
353 else {
354 jQuery(this).closest('.item.checkbox').checkbox('set unchecked');
355 }
356 });
357 }
358 mainwp_newpost_updateCategories();
359 mainwp_update_selection_counter();
360 return false;
361 };
362
363
364 let mainwp_ss_select_disconnected = function (me, val) {
365 let parent = jQuery(me).closest('.mainwp_select_sites_wrapper');
366 let tab = parent.find('input[name="select_sites_tab"]').val();
367 if (tab == 'site') {
368 parent.find('.mainwp-select-sites-list .warning.item:not(.no-select) INPUT:enabled:checkbox').each(function () {
369 jQuery(this).attr('checked', val).trigger("change");
370 if (val) {
371 jQuery(this).closest('.item.warning.checkbox').checkbox('set checked');
372 }
373 else {
374 jQuery(this).closest('.item.warning.checkbox').checkbox('set unchecked');
375 }
376 });
377 }
378 mainwp_newpost_updateCategories();
379 mainwp_update_selection_counter();
380 return false;
381 };
382
383 globalThis.mainwp_sites_selection_onvisible_callback = function (me) {
384 let selected_tab = jQuery(me).attr('select-by');
385 let select_by = 'site';
386 let parent = jQuery(me).closest('.mainwp_select_sites_wrapper');
387 if (selected_tab == 'staging') {
388 // uncheck live sites
389 parent.find('.mainwp-select-sites-list INPUT:checkbox').prop('checked', false);
390 parent.find('.mainwp-select-groups-list INPUT:checkbox').prop('checked', false);
391 parent.find('#mainwp-select-clients-list INPUT:checkbox').prop('checked', false);
392 } else if (selected_tab == 'site') {
393 // uncheck staging sites
394 parent.find('#mainwp-select-staging-sites-list INPUT:checkbox').prop('checked', false);
395 parent.find('.mainwp-select-groups-list INPUT:checkbox').prop('checked', false);
396 parent.find('#mainwp-select-clients-list INPUT:checkbox').prop('checked', false);
397 } else if (selected_tab == 'group') {
398 // uncheck sites
399 parent.find('.mainwp-select-sites-list INPUT:checkbox').prop('checked', false);
400 parent.find('#mainwp-select-staging-sites-list INPUT:checkbox').prop('checked', false);
401 parent.find('#mainwp-select-clients-list INPUT:checkbox').prop('checked', false);
402 select_by = 'group';
403 } else if (selected_tab == 'client') {
404 // uncheck sites
405 parent.find('.mainwp-select-sites-list INPUT:checkbox').prop('checked', false);
406 parent.find('.mainwp-select-groups-list INPUT:checkbox').prop('checked', false);
407 parent.find('#mainwp-select-staging-sites-list INPUT:checkbox').prop('checked', false);
408 select_by = 'client';
409 }
410
411 console.log('select by: ' + select_by);
412
413 parent.find('input[name="select_by"]').val(select_by);
414 parent.find('input[name="select_sites_tab"]').val(selected_tab);
415
416 mainwp_update_selection_counter();
417 }
418
419
420 let mainwp_newpost_updateCategories = function () {
421 if (executingUpdateCategories) {
422 queueUpdateCategories++;
423 return;
424 }
425 executingUpdateCategories = true;
426 let catsSelection = jQuery('#categorychecklist');
427 if (catsSelection.length > 0) {
428 let tab = jQuery('input[name="select_sites_tab"]').val();
429 let sites = [];
430 let groups = [];
431 let clients = [];
432 if (tab == 'site') {
433 sites = jQuery.map(jQuery('.mainwp-select-sites-list INPUT:checkbox:checked'), function (el) {
434 return jQuery(el).val();
435 });
436 } else if (tab == 'staging') {
437 sites = jQuery.map(jQuery('#mainwp-select-staging-sites-list INPUT:checkbox:checked'), function (el) {
438 return jQuery(el).val();
439 });
440 } else if (tab == 'client') {
441 clients = jQuery.map(jQuery('#mainwp-select-clients-list INPUT:checkbox:checked'), function (el) {
442 return jQuery(el).val();
443 });
444 } else { //group
445 groups = jQuery.map(jQuery('.mainwp-select-groups-list INPUT:checkbox:checked'), function (el) {
446 return jQuery(el).val();
447 });
448 }
449
450 let selected_categories = catsSelection.dropdown('get value');
451
452
453 let data = mainwp_secure_data({
454 action: 'mainwp_get_categories',
455 sites: encodeURIComponent(sites.join(',')),
456 groups: encodeURIComponent(groups.join(',')),
457 clients: encodeURIComponent(clients.join(',')),
458 selected_categories: selected_categories ? encodeURIComponent(selected_categories) : '',
459 post_id: jQuery('#post_ID').val(),
460 custom_post_type: jQuery('#mainwp_custom_post_type_edit_post').length ? jQuery('#mainwp_custom_post_type_edit_post').val() : '' // support CPT ext.
461 });
462
463 jQuery.post(ajaxurl, data, function (pSelectedCategories) {
464 return function (response) {
465 if (response?.content) {
466 catsSelection.dropdown('remove selected');
467 catsSelection.find('.menu').html(response.content);
468 catsSelection.dropdown('refresh');
469 let arrVal = pSelectedCategories.split(",");
470 catsSelection.dropdown('set selected', arrVal);
471 updateCategoriesPostFunc();
472 let popWrapper = jQuery('#categorychecklist .mainwp-html-popup');
473 if( popWrapper.length > 0 ) {
474 jQuery(popWrapper).each(
475 function(){
476 const popupContent = jQuery(this).attr('html-popup-content') ?? '';
477 mainwp_init_html_popup(this, mainwp_escape_html(popupContent));
478 }
479 );
480 }
481
482 }
483 };
484 }(selected_categories), 'json');
485 } else {
486 updateCategoriesPostFunc();
487 }
488 };
489 let updateCategoriesPostFunc = function () {
490 if (queueUpdateCategories > 0) {
491 queueUpdateCategories--;
492 executingUpdateCategories = false;
493 mainwp_newpost_updateCategories();
494 } else {
495 executingUpdateCategories = false;
496 }
497 };
498
499 jQuery(document).on('keydown', 'form[name="post"]', function (event) {
500 if (event.keyCode == 13 && event.srcElement.tagName.toLowerCase() == "input") {
501 event.preventDefault();
502 }
503 });
504 jQuery(document).on('keyup', '#mainwp-sites-menu-filter', function () {
505 let filter = jQuery(this).val().toLowerCase();
506 let parent = jQuery('#mainwp-sites-sidebar-menu');
507 let siteItems = parent.find('.mainwp-site-menu-item');
508
509 for (let ss of siteItems) {
510 let currentElement = jQuery(ss);
511 let value = currentElement.find('label').text().toLowerCase();
512 if (value.includes(filter)) {
513 currentElement.show();
514 } else {
515 currentElement.hide();
516 }
517 }
518 });
519
520 // Accordion initialization on pre-existing markup
521 jQuery(function () {
522 mainwp_sidebar_accordion_init();
523 });
524
525 globalThis.mainwp_sidebar_accordion_init = function () {
526 if (jQuery('.mainwp-sidebar-accordion').length > 0) {
527 console.log('sidebar accordion init');
528 jQuery('.mainwp-sidebar-accordion').accordion({
529 "onOpening": function () {
530 let parent = jQuery(this).closest('.mainwp-sidebar-accordion');
531 let ident = jQuery('.mainwp-sidebar-accordion').index(parent);
532 mainwp_accordion_on_collapse(ident, 1);
533 },
534 "onClosing": function () {
535 let parent = jQuery(this).closest('.mainwp-sidebar-accordion');
536 let ident = jQuery('.mainwp-sidebar-accordion').index(parent);
537 mainwp_accordion_on_collapse(ident, 0);
538 }
539 });
540 mainwp_accordion_init_collapse();
541 }
542 }
543
544
545 let mainwp_accordion_on_collapse = function (ident, val) {
546 if (typeof (Storage) !== 'undefined') {
547 if (typeof (pagenow) !== 'undefined') {
548 localStorage.setItem('mainwp-accordion[' + pagenow + '][' + ident + ']', val);
549 }
550 }
551 };
552 let mainwp_accordion_init_collapse = function () {
553 jQuery('.mainwp-sidebar-accordion .title').addClass('active');
554 jQuery('.mainwp-sidebar-accordion .content').addClass('active');
555
556 jQuery('.mainwp-sidebar-accordion').each(function () {
557 let ident = jQuery('.mainwp-sidebar-accordion').index(this);
558 if (typeof (pagenow) !== 'undefined') {
559 let val = localStorage.getItem('mainwp-accordion[' + pagenow + '][' + ident + ']');
560 if (val === '0') {
561 jQuery(this).find('.title').removeClass('active');
562 jQuery(this).find('.content').removeClass('active');
563 }
564 }
565 });
566 };
567
568 globalThis.mainwp_ui_state_save = function (ident, val) {
569 if (typeof (Storage) !== 'undefined') {
570 localStorage.setItem('mainwp-dashboard[' + ident + ']', val);
571 }
572 };
573
574 globalThis.mainwp_ui_state_load = function (ident) {
575 if (typeof (Storage) !== 'undefined') {
576 return localStorage.getItem('mainwp-dashboard[' + ident + ']');
577 }
578 return '1'; // show if Storage undefined.
579 };
580
581 globalThis.mainwp_ui_state_init = function (ident, callback) {
582 if (typeof (Storage) !== 'undefined') {
583 let state = mainwp_ui_state_load(ident);
584 if (typeof callback === 'function') {
585 callback(state);
586 }
587 }
588 };
589
590 globalThis.mainwp_sites_filter_select = function (objInput) {
591 let filter = jQuery(objInput).val().toLowerCase();
592 let parent = jQuery(objInput).closest('.mainwp_select_sites_wrapper');
593 let tab = jQuery('input[name="select_sites_tab"]').val();
594 let siteItems = [];
595
596 if (tab == 'group') {
597 siteItems = parent.find('.mainwp_selected_groups_item');
598 } else if (tab == 'site' || tab == 'staging') {
599 siteItems = parent.find('.mainwp_selected_sites_item');
600 }
601
602 for (let id of siteItems) {
603 let currentElement = jQuery(id);
604 let value = currentElement.find('label').text().toLowerCase();
605 if (value.includes(filter)) {
606 currentElement.removeClass('no-select').show();
607 } else {
608 currentElement.addClass('no-select').hide();
609 }
610 }
611 if (tab == 'site' || tab == 'staging') {
612 mainwp_newpost_updateCategories();
613 }
614 }
615
616 globalThis.mainwp_tags_filter_select = function (objInput) {
617 let filter = jQuery(objInput).val().toLowerCase();
618 let parent = jQuery(objInput).closest('.mainwp-search-options');
619 let tags = [];
620
621 tags = parent.find('a.item');
622
623 console.log(tags);
624
625 for (let id of tags) {
626 let currentElement = jQuery(id);
627 let value = currentElement.text().toLowerCase();
628 if (value.includes(filter)) {
629 currentElement.show();
630 } else {
631 currentElement.hide();
632 }
633 }
634 }
635
636 jQuery(document).on('keyup', '#mainwp-select-sites-filter', function () {
637 mainwp_sites_filter_select(this);
638 });
639
640 jQuery(document).on('keyup', '#mainwp-screenshots-sites-filter', function () {
641 mainwp_sites_filter_select(this);
642 });
643
644 jQuery(document).on('keyup', '#mainwp-select-tags-filter', function () {
645 mainwp_tags_filter_select(this);
646 });
647
648 jQuery(function () {
649 if (jQuery(document).find('.mainwp-select-sites-header .ui.menu .item').length) {
650 jQuery(document).find('.mainwp-select-sites-header .ui.menu .item').tab({ 'onVisible': function () { mainwp_sites_selection_onvisible_callback(this); } });
651 }
652 });
653
654 globalThis.mainwp_get_icon_start = function () {
655 jQuery('.cached-icon-expired').attr('queue', 1);
656 mainwpVars.bulkInstallTotal = jQuery('.cached-icon-expired[queue="1"]').length;
657 if (0 === mainwpVars.bulkInstallTotal) {
658 return;
659 }
660 mainwp_get_icon_start_next();
661 }
662
663 let mainwp_get_icon_start_next = function () {
664 let itemIconProcess = jQuery('.cached-icon-expired[queue="1"]:first');
665 if (itemIconProcess.length > 0) {
666 mainwp_get_icon_start_specific(itemIconProcess);
667 } else {
668 console.log('Finished update icons.');
669 }
670 };
671
672 let mainwp_get_icon_start_specific = function (itemIconProcess) {
673 itemIconProcess.attr('queue', '0');
674 let slug;
675 let type = itemIconProcess.attr('icon-type');
676 if (type == 'plugin' || type == 'theme') {
677 slug = jQuery(itemIconProcess).attr('item-slug');
678 } else {
679 return;
680 }
681
682 if ('' === slug || '.' === slug) {
683 mainwp_get_icon_start_next();
684 }
685
686 let data = mainwp_secure_data({
687 action: 'mainwp_refresh_icon',
688 slug: slug,
689 type: type
690 });
691 jQuery.post(ajaxurl, data, function (response) {
692 mainwp_get_icon_start_next();
693 });
694 }
695
696 jQuery(function () {
697 jQuery(document).on('click', '.cached-icon-customable', function () {
698 let iconObj = jQuery(this);
699 jQuery('#mainwp_delete_image_field').hide();
700 jQuery('#mainwp-upload-custom-icon-modal').modal('setting', 'closable', false).modal('show');
701 if (iconObj[0].hasAttribute('src') && iconObj.attr('src') != '') {
702 jQuery('#mainwp_delete_image_field').find('.ui.image').attr('src', iconObj.attr('src'));
703 jQuery('#mainwp_delete_image_field').show();
704 }
705 jQuery(document).on('click', '#update_custom_icon_btn', function () {
706 mainwp_upload_custom_icon(iconObj);
707 return false;
708 });
709 return false;
710 });
711
712
713 jQuery(document).on('click', '.mainwp-selecte-theme-button', function () {
714 jQuery('#mainwp-select-mainwp-themes-modal').modal({
715 allowMultiple: false,
716 closable: true,
717 onHide: function () {
718 mainwp_forceReload();
719 }
720 }).modal('show');
721
722 return false;
723 });
724
725 });
726
727
728 let mainwp_upload_custom_icon = function (iconObj) {
729
730 let type = iconObj.attr('icon-type');
731
732 if (type !== 'plugin' && type !== 'theme') {
733 return false;
734 }
735
736 let slug = jQuery(iconObj).attr('item-slug');
737
738 let deleteIcon = jQuery('#mainwp_delete_image_chk').is(':checked');
739
740 let msg = MainWP.I18n.t('Updating the icon. Please wait...');
741 mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + msg, '');
742 jQuery('#update_custom_icon_btn').attr('disabled', 'disabled');
743
744 //Add via ajax!!
745 let formdata = new FormData(jQuery('#uploadicon_form')[0]);
746 formdata.append("action", 'mainwp_upload_custom_icon');
747 formdata.append("type", type);
748 formdata.append("slug", slug);
749 formdata.append("delete", deleteIcon ? 1 : 0);
750 formdata.append("security", security_nonces['mainwp_upload_custom_icon']);
751 formdata.append("delnonce", jQuery(iconObj).attr('del-icon-nonce'));
752
753 jQuery.ajax({
754 type: 'POST',
755 url: ajaxurl,
756 data: formdata,
757 success: function (response) {
758 if (response?.result == 'success') {
759 let msg = '';
760
761 if (type === 'plugin') {
762 if (jQuery('#mainwp-show-plugins').length > 0) {
763 msg = MainWP.I18n.t('Loading...');
764 mainwp_fetch_plugins();
765 }
766 } else if (jQuery('#mainwp_show_themes').length > 0) {
767 msg = MainWP.I18n.t('Loading...');
768 mainwp_fetch_themes();
769 }
770 if (msg === '') {
771 mainwp_set_message_zone('#mainwp-message-zone-upload');
772 } else {
773 mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Loading...'), '');
774 }
775 setTimeout(function () {
776 mainwp_forceReload();
777 }, 3000);
778 } else if (response.error) {
779 feedback('mainwp-message-zone-upload', response.error, 'red');
780 } else {
781 feedback('mainwp-message-zone-upload', MainWP.I18n.t('Undefined error. Please try again.'), 'red');
782 }
783 },
784 error: function () {
785 },
786 contentType: false,
787 cache: false,
788 processData: false,
789 enctype: 'multipart/form-data',
790 dataType: 'json'
791 });
792
793 };
794
795 /**
796 * Initialize button state based on site selection checkboxes.
797 * Disables specified buttons until at least one site/tag(group)/client is selected.
798 *
799 * @param {Array|string} buttonIds - Array of button IDs or single button ID string
800 * @param {string} containerSelector - Optional container selector (default: '#mainwp-select-sites')
801 */
802 let mainwp_init_button_site_selection_dependency = function (buttonIds, containerSelector = '#mainwp-select-sites') {
803
804 // Convert single button ID to array
805 if (typeof buttonIds === 'string') {
806 buttonIds = [buttonIds];
807 }
808
809 // Function to check if any checkbox is selected
810 let checkSelectionState = function () {
811 let hasSelection = false;
812
813 // Check all three types of checkboxes
814 let selectors = [
815 'input[name="selected_sites[]"]:checked',
816 'input[name="selected_groups[]"]:checked',
817 'input[name="selected_clients[]"]:checked'
818 ];
819
820 jQuery.each(selectors, function (index, selector) {
821 if (jQuery(containerSelector + ' ' + selector).length > 0) {
822 hasSelection = true;
823 return false; // break the loop
824 }
825 });
826
827 // Enable or disable buttons based on selection state
828 jQuery.each(buttonIds, function (index, buttonId) {
829 let $button = jQuery('#' + buttonId);
830 let $wrapper = jQuery('#' + buttonId + '_wrapper');
831
832 if ($button.length > 0) {
833 if (hasSelection) {
834 // Enable button
835 $button.removeClass('disabled').css('pointer-events', '');
836 // Remove wrapper tooltip if exists
837 if ($wrapper.length > 0) {
838 $wrapper.removeAttr('data-tooltip');
839 }
840 } else {
841 // Disable button
842 $button.addClass('disabled').css('pointer-events', 'none');
843 // Add wrapper tooltip if exists
844 if ($wrapper.length > 0) {
845 $wrapper.attr('data-tooltip', 'Select at least one site, client or tag.').attr('data-inverted', '').attr('data-position', 'bottom center');
846 }
847 }
848 }
849 });
850 };
851
852 // Initialize buttons as disabled with tooltip wrapper
853 jQuery.each(buttonIds, function (index, buttonId) {
854 let $button = jQuery('#' + buttonId);
855 if ($button.length > 0) {
856 // Wrap button in a span for tooltip (if not already wrapped)
857 if ($button.parent().attr('id') !== buttonId + '_wrapper') {
858 $button.wrap('<span id="' + buttonId + '_wrapper"></span>');
859 }
860 let $wrapper = jQuery('#' + buttonId + '_wrapper');
861
862 // Disable button and add tooltip to wrapper
863 $button.addClass('disabled');
864 $wrapper.attr('data-tooltip', 'Select at least one site, client or tag.').attr('data-inverted', '').attr('data-position', 'bottom center');
865 }
866 });
867
868 // Use event delegation to handle checkbox changes (including dynamically loaded ones)
869 jQuery(document).on('change', containerSelector + ' input[name="selected_sites[]"], ' +
870 containerSelector + ' input[name="selected_groups[]"], ' +
871 containerSelector + ' input[name="selected_clients[]"]', function () {
872 checkSelectionState();
873 });
874
875 // Also check on page load in case checkboxes are pre-selected
876 jQuery(document).ready(function () {
877 checkSelectionState();
878 });
879 };
880
881 let mainwp_upload_custom_types_icon = function (iconObj, uploadAct, iconItemId, iconFileSlug, deleteIcon, callback_uploaded) {
882 let msg = MainWP.I18n.t('Updating the icon. Please wait...');
883 mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + msg, '');
884 jQuery('#update_custom_icon_btn').attr('disabled', 'disabled');
885
886 let upload_act = uploadAct !== undefined && uploadAct !== '' ? uploadAct : 'mainwp_upload_custom_types_icon';
887 let elemid = jQuery(iconObj).attr('data-element-id') === undefined ? '' : jQuery(iconObj).attr('data-element-id');
888 let delNonce = jQuery(iconObj).attr('del-icon-nonce') === undefined ? '' : jQuery(iconObj).attr('del-icon-nonce');
889
890 //Add via ajax!!
891 let formdata = new FormData(jQuery('#uploadicon_form')[0]);
892 formdata.append("action", upload_act);
893 formdata.append("iconFileSlug", iconFileSlug);
894 formdata.append("iconItemId", iconItemId);
895 formdata.append("delete", deleteIcon ? 1 : 0);
896 formdata.append("security", security_nonces[upload_act]);
897 formdata.append("elementid", elemid);
898 formdata.append("delnonce", delNonce);
899
900 jQuery.ajax({
901 type: 'POST',
902 url: ajaxurl,
903 data: formdata,
904 success: function (response) {
905 jQuery('#update_custom_icon_btn').removeAttr('disabled');
906 if (response?.result == 'success') {
907 mainwp_set_message_zone('#mainwp-message-zone-upload');
908 if (typeof callback_uploaded == 'function') {
909 callback_uploaded(response);
910 }
911 } else if (response.error) {
912 feedback('mainwp-message-zone-upload', response.error, 'red');
913 } else {
914 feedback('mainwp-message-zone-upload', MainWP.I18n.t('Undefined error. Please try again.'), 'red');
915 }
916 },
917 error: function () {
918 },
919 contentType: false,
920 cache: false,
921 processData: false,
922 enctype: 'multipart/form-data',
923 dataType: 'json'
924 });
925
926 };
927
928 let mainwp_guidedtours_onchange = function (me) {
929 let data = mainwp_secure_data({
930 action: 'mainwp_guided_tours_option_update',
931 enable: jQuery(me).is(":checked") ? 1 : 0,
932 });
933 jQuery.post(ajaxurl, data, function () {
934 setTimeout(() => {
935 mainwp_forceReload();
936 }, 1000);
937 });
938 }
939
940 let mainwp_help_modal_content_onclick = function (hide, isToolsPage) {
941
942 // to fix confict ids when current page is tool page.
943 let enab_tour = jQuery('#mainwp-guided-tours-check').checkbox('is checked') ? 1 : 0;
944 let enab_video = jQuery('#mainwp-guided-video-check').checkbox('is checked') ? 1 : 0;
945 let enab_chatbase = jQuery('#mainwp-guided-chatbase-check').checkbox('is checked') ? 1 : 0;
946
947 const me = jQuery('#mainwp-update-permissions-button');
948
949 let data = mainwp_secure_data({
950 action: 'mainwp_help_modal_content_update',
951 enable_tour: enab_tour,
952 enable_video: enab_video,
953 enable_chatbase: enab_chatbase,
954 });
955
956 jQuery(me).html('<i class="notched circle loading icon"></i> Saving...').addClass('disabled');
957
958 jQuery.post(ajaxurl, data, function () {
959 if (isToolsPage !== undefined && isToolsPage) {
960 mainwp_forceReload('admin.php?page=MainWPTools');
961 } else {
962 if (jQuery('#mainwp-guided-chatbase-option').is(':checked')) {
963 jQuery('#mainwp-start-chat-card').removeClass('mainwp-disabled-link');
964 } else {
965 jQuery('#mainwp-start-chat-card').addClass('mainwp-disabled-link');
966 jQuery('#mainwp-chatbase-chat-screen').fadeIn('100').find('iframe').attr('src', '');
967 }
968
969 if (jQuery('#mainwp-guided-tours-option').is(':checked')) {
970 jQuery('#mainwp-start-tour-card').removeClass('mainwp-disabled-link');
971 } else {
972 jQuery('#mainwp-start-tour-card').addClass('mainwp-disabled-link');
973 }
974
975 if (jQuery('#mainwp-guided-video-option').is(':checked')) {
976 jQuery('#mainwp-start-video-card').removeClass('mainwp-disabled-link');
977 } else {
978 jQuery('#mainwp-start-video-card').addClass('mainwp-disabled-link');
979 jQuery('#mainwp-chatbase-video-screen').fadeIn('100').find('iframe').attr('src', '');
980 }
981
982 jQuery(me).html('Update Permissions').removeClass('disabled');
983 }
984 });
985 }
986
987 let mainwp_help_modal_start_content_onclick = function (tour_id, video_id, show_chat) {
988 jQuery('#mainwp-chatbase-chat-screen').fadeOut('100');
989 jQuery('#mainwp-chatbase-video-screen').fadeOut('100');
990 jQuery('#mainwp-help-modal-options').fadeOut('50');
991
992 if (tour_id) {
993 globalThis.USETIFUL.tour.start(tour_id);
994 }
995
996 if (video_id) {
997 jQuery('#mainwp-chatbase-video-screen').fadeIn('100').find('iframe').attr('src', 'https://www.youtube.com/embed/' + video_id);
998 }
999
1000 if (show_chat) {
1001 jQuery('#mainwp-chatbase-chat-screen').fadeIn('100').find('iframe').attr('src', 'https://supportassistant.mainwp.com/chatbot-iframe/Tv5dqV-xiQxwgPeMQFCZ4');
1002 jQuery('#mainwp-start-tour-button').fadeIn('100');
1003 }
1004 }
1005
1006 jQuery(document).on('click', '#mainwp-select-light-theme-button', function (e) {
1007 e.preventDefault();
1008 const url = new URL(globalThis.location.href);
1009 url.searchParams.set('mainwp_quick_theme_change', 'default');
1010 url.searchParams.set('_wpnonce', mainwpParams.quickThemeChangeNonce);
1011 globalThis.location.href = url.toString();
1012 });
1013
1014 jQuery(document).on('click', '#mainwp-select-dark-theme-button', function (e) {
1015 e.preventDefault();
1016 const url = new URL(globalThis.location.href);
1017 url.searchParams.set('mainwp_quick_theme_change', 'default-dark');
1018 url.searchParams.set('_wpnonce', mainwpParams.quickThemeChangeNonce);
1019 globalThis.location.href = url.toString();
1020 });
1021
1022 /**
1023 * Widget layout UI handler object.
1024 *
1025 * Provides methods for managing the widget layout modal UI state and status messages.
1026 */
1027 if (!globalThis.mainwpUIHandleWidgetsLayout) {
1028 globalThis.mainwpUIHandleWidgetsLayout = (function () {
1029 const statusElemId = '#mainwp-common-edit-widgets-layout-status';
1030 let _instance = {
1031 loadingStatus: function () {
1032 jQuery(statusElemId).html('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Loading layouts. Please wait...')).show();
1033 },
1034 savingStatus: function () {
1035 jQuery(statusElemId).html('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Saving layout. Please wait...')).show();
1036 },
1037 deletingStatus: function () {
1038 jQuery(statusElemId).html('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Deleting layout. Please wait...')).show();
1039
1040 },
1041 showWorkingStatus: function (status, addClass) {
1042 if (addClass) {
1043 jQuery(statusElemId).removeClass('red green yellow');
1044 jQuery(statusElemId).addClass(addClass);
1045 }
1046 jQuery(statusElemId).html(status).show();
1047 },
1048 hideWorkingStatus: function () {
1049 jQuery(statusElemId).removeClass('red green').hide();
1050 },
1051 showLayout: function (showBtn) {
1052 jQuery('#mainwp-common-edit-widgets-layout-modal > div.header').html(MainWP.I18n.t('Save Layout'));
1053 jQuery('#mainwp-common-edit-widgets-layout-edit-fields').show();
1054 jQuery('#mainwp-common-edit-widgets-layout-save-button').show();
1055 jQuery('#mainwp-common-layout-widgets-select-fields').hide();
1056 jQuery('#mainwp-common-edit-widgets-select-layout-button').hide();
1057 jQuery('#mainwp-common-edit-widgets-layout-delete-button').hide();
1058
1059 if (jQuery('#mainwp-widgets-selected-layout').length > 0) {
1060 let name = jQuery('#mainwp-widgets-selected-layout').attr('layout-name');
1061 let lay_idx = jQuery('#mainwp-widgets-selected-layout').attr('layout-idx');
1062 jQuery('#mainwp-common-edit-widgets-layout-name').val(name);
1063 jQuery(showBtn).attr('selected-layout-id', lay_idx);
1064 }
1065 this.hideWorkingStatus();
1066 mainwp_common_show_edit_widgets_layout_modal();
1067 },
1068 loadSegment: function (loadCallback) {
1069 jQuery('#mainwp-common-edit-widgets-layout-edit-fields').hide();
1070 jQuery('#mainwp-common-edit-widgets-layout-save-button').hide();
1071 jQuery('#mainwp-common-edit-widgets-layout-modal > div.header').html(MainWP.I18n.t('Load Layout'));
1072 jQuery('#mainwp-common-layout-widgets-select-fields').show();
1073 jQuery('#mainwp-common-edit-widgets-select-layout-button').show();
1074 jQuery('#mainwp-common-edit-widgets-layout-delete-button').show();
1075 this.hideWorkingStatus();
1076 mainwp_common_show_edit_widgets_layout_modal(loadCallback);
1077 },
1078 showResults: function (result) {
1079 jQuery(statusElemId).hide();
1080 jQuery('#mainwp-common-edit-widgets-layout-lists-wrapper').html(result);
1081 jQuery('#mainwp-common-edit-widgets-layout-lists-wrapper .ui.dropdown').dropdown();
1082 jQuery('#mainwp-common-layout-widgets-select-fields').show();
1083 }
1084 }
1085 return _instance;
1086 })();
1087 }
1088
1089 /**
1090 * Show the widget layout modal.
1091 *
1092 * Opens the modal dialog for managing widget layouts with optional callback.
1093 *
1094 * @param {Function} loadCallback - Optional callback function to execute when modal shows.
1095 */
1096 let mainwp_common_show_edit_widgets_layout_modal = function (loadCallback) {
1097 jQuery('#mainwp-common-edit-widgets-layout-modal').modal({
1098 allowMultiple: false,
1099 onShow: function () {
1100 if (typeof loadCallback == 'function') {
1101 loadCallback();
1102 }
1103 }
1104 }).modal('show');
1105 };
1106
1107 /**
1108 * Save widget layout configuration.
1109 *
1110 * Collects grid stack item positions and sizes from GridStack engine, then saves the layout via AJAX.
1111 *
1112 * @param {string} itemClass - CSS selector for grid items (e.g., '.grid-stack-item').
1113 * @param {Object} data - Data object containing layout metadata (name, seg_id, settings_slug).
1114 * @param {Function} callBack - Callback function to execute after save completes.
1115 */
1116 let mainwp_common_ui_widgets_save_layout = function (itemClass, data, callBack) {
1117
1118 let orders = [];
1119 let wgIds = [];
1120
1121 const gridElement = document.querySelector('.grid-stack');
1122
1123 if (!gridElement) {
1124 return;
1125 }
1126
1127 const grid = gridElement.gridstack;
1128
1129 if (!grid?.engine?.nodes) {
1130 return;
1131 }
1132
1133 grid.engine.nodes.forEach(function (node) {
1134 if (!node?.el?.id) {
1135 return;
1136 }
1137
1138 let obj = {
1139 "x": node.x ?? 0,
1140 "y": node.y ?? 0,
1141 "w": node.w ?? 4,
1142 "h": node.h ?? 4
1143 };
1144 orders.push(obj);
1145 wgIds.push(node.el.id);
1146 });
1147
1148 if (wgIds.length === 0) {
1149 return;
1150 }
1151
1152 data.action = 'mainwp_ui_save_widgets_layout';
1153 data.order = orders;
1154 data.wgids = wgIds;
1155
1156 jQuery.post(ajaxurl, mainwp_secure_data(data), function (res) {
1157 if (typeof callBack === 'function') {
1158 callBack(res);
1159 }
1160 }, 'json');
1161 }
1162
1163 /**
1164 * Initialize widget layout management handlers.
1165 *
1166 * Sets up event handlers for saving, loading, and deleting widget layout configurations.
1167 * Manages button states based on user input and selections.
1168 */
1169 function mainwp_init_widget_layout_handlers() {
1170 let mainwp_load_manage_widgets_layout = function () {
1171 jQuery('#mainwp-common-layout-widgets-select-fields').hide();
1172 let data = mainwp_secure_data({
1173 action: 'mainwp_ui_load_widgets_layout',
1174 settings_slug: jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('settings-slug')
1175 });
1176 mainwpUIHandleWidgetsLayout.showWorkingStatus('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Loading layouts. Please wait...'), '');
1177 jQuery.post(ajaxurl, data, function (response) {
1178 if (response.error != undefined) {
1179 mainwpUIHandleWidgetsLayout.showWorkingStatus(response.error, 'red');
1180 } else if (response.result) {
1181 mainwpUIHandleWidgetsLayout.showResults(response.result);
1182 jQuery('#mainwp-common-edit-widgets-select-layout-button').addClass('disabled');
1183 jQuery('#mainwp-common-edit-widgets-layout-delete-button').addClass('disabled');
1184 } else {
1185 mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('No saved layouts found.'), 'yellow');
1186 }
1187 }, 'json');
1188 };
1189
1190 jQuery('#mainwp-manage-widgets-load-saved-layout-button').on('click', function () {
1191 jQuery('#mainwp-common-edit-widgets-layout-save-button').addClass('disabled');
1192 jQuery('#mainwp-common-edit-widgets-layout-name').val('');
1193 mainwpUIHandleWidgetsLayout.showLayout(this);
1194 });
1195
1196 jQuery('#mainwp-manage-widgets-ui-choose-layout').on('click', function () {
1197 mainwpUIHandleWidgetsLayout.loadSegment(mainwp_load_manage_widgets_layout);
1198 });
1199
1200 jQuery(document).on('input', '#mainwp-common-edit-widgets-layout-name', function () {
1201 if (jQuery(this).val().trim().length > 0) {
1202 jQuery('#mainwp-common-edit-widgets-layout-save-button').removeClass('disabled');
1203 } else {
1204 jQuery('#mainwp-common-edit-widgets-layout-save-button').addClass('disabled');
1205 }
1206 });
1207
1208 jQuery(document).on('change', '#mainwp-common-layout-widgets-select-fields .ui.dropdown', function () {
1209 let selected_value = jQuery(this).dropdown('get value');
1210 if (selected_value && selected_value !== '') {
1211 jQuery('#mainwp-common-edit-widgets-select-layout-button').removeClass('disabled');
1212 jQuery('#mainwp-common-edit-widgets-layout-delete-button').removeClass('disabled');
1213 } else {
1214 jQuery('#mainwp-common-edit-widgets-select-layout-button').addClass('disabled');
1215 jQuery('#mainwp-common-edit-widgets-layout-delete-button').addClass('disabled');
1216 }
1217 });
1218
1219 jQuery('#mainwp-common-edit-widgets-layout-save-button').on('click', function (e) {
1220 if (jQuery(this).hasClass('disabled')) {
1221 return false;
1222 }
1223
1224 mainwpUIHandleWidgetsLayout.hideWorkingStatus();
1225
1226 let seg_name = jQuery('#mainwp-common-edit-widgets-layout-name').val().trim();
1227
1228 if ('' == seg_name) {
1229 mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('Please enter a layout name.'), 'yellow');
1230 return false;
1231 }
1232
1233 let data = {
1234 name: seg_name,
1235 seg_id: jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('selected-layout-id'),
1236 settings_slug: jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('settings-slug')
1237 };
1238
1239 mainwpUIHandleWidgetsLayout.showWorkingStatus('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Saving layout. Please wait...'), '');
1240
1241 mainwp_common_ui_widgets_save_layout('.grid-stack-item', data, function (response) {
1242 if (response.error != undefined) {
1243 mainwpUIHandleWidgetsLayout.showWorkingStatus(response.error, 'red');
1244 } else if (response.result == 'SUCCESS') {
1245 mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('Layout saved successfully.'), 'green');
1246 setTimeout(function () {
1247 jQuery('#mainwp-common-edit-widgets-layout-status').fadeOut(300);
1248 jQuery('#mainwp-common-edit-widgets-layout-modal').modal('hide');
1249 mainwp_forceReload();
1250 }, 2000);
1251 } else {
1252 mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('An error occurred while saving the layout.'), 'red');
1253 }
1254 });
1255 e.preventDefault();
1256 e.stopPropagation();
1257 return true;
1258 });
1259
1260 jQuery('#mainwp-common-edit-widgets-select-layout-button').on('click', function () {
1261 if (jQuery(this).hasClass('disabled')) {
1262 return false;
1263 }
1264
1265 mainwpUIHandleWidgetsLayout.hideWorkingStatus();
1266 let seg_id = jQuery('#mainwp-common-layout-widgets-select-fields .ui.dropdown').dropdown('get value');
1267 let screen_slug = jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('settings-slug');
1268 let loc_url = removeUrlParams(location.href, ['screen_slug', 'updated', '_opennonce']);
1269 if ('' == loc_url) {
1270 loc_url = location.href;
1271 }
1272 globalThis.location.href = loc_url + '&select_layout=1&screen_slug=' + encodeURIComponent(screen_slug) + '&updated=' + encodeURIComponent(seg_id) + '&_opennonce=' + mainwpWidgetLayout.openNonce;
1273 });
1274
1275 jQuery('#mainwp-common-edit-widgets-layout-delete-button').on('click', function (e) {
1276
1277 e.preventDefault();
1278 e.stopPropagation();
1279
1280 const $button = jQuery(this);
1281
1282 if ($button.hasClass('disabled')) {
1283 return;
1284 }
1285
1286 mainwpUIHandleWidgetsLayout.hideWorkingStatus();
1287
1288 let seg_id = jQuery('#mainwp-common-layout-widgets-select-fields .ui.dropdown').dropdown('get value');
1289 if (!seg_id) {
1290 return;
1291 }
1292
1293 if ('yes' === $button.attr('running')) {
1294 return;
1295 }
1296
1297 $button.attr('running', 'yes');
1298
1299 let data = mainwp_secure_data({
1300 action: 'mainwp_ui_delete_widgets_layout',
1301 seg_id: seg_id,
1302 settings_slug: jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('settings-slug')
1303 });
1304 mainwpUIHandleWidgetsLayout.showWorkingStatus('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Deleting layout. Please wait...'), '');
1305 jQuery.post(ajaxurl, data, (response) => {
1306
1307 $button.removeAttr('running');
1308
1309 if (response.error != undefined) {
1310 mainwpUIHandleWidgetsLayout.showWorkingStatus(response.error, 'red');
1311 } else if (response.result == 'SUCCESS') {
1312 mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('Layout deleted successfully.'), 'green');
1313 setTimeout(function () {
1314 jQuery('#mainwp-common-edit-widgets-layout-status').fadeOut(300);
1315 jQuery('#mainwp-common-edit-widgets-layout-modal').modal('hide');
1316 }, 2000);
1317 } else {
1318 mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('An error occurred while deleting the layout.'), 'red');
1319 }
1320 }, 'json');
1321 });
1322 }
1323