| 1 |
|
| 2 |
/** |
| 3 |
* MainWP_Page.page |
| 4 |
*/ |
| 5 |
|
| 6 |
let countSent = 0; |
| 7 |
let countReceived = 0; |
| 8 |
|
| 9 |
let updatePagesBulkActionsState = function () { |
| 10 |
let checkedCount = jQuery('#mainwp-pages-table input[name="page[]"]:checked').length; |
| 11 |
let dropdown = jQuery('#mainwp-manage-pages #mainwp-bulk-actions'); |
| 12 |
let applyButton = jQuery('#mainwp-do-pages-bulk-actions'); |
| 13 |
|
| 14 |
if (checkedCount > 0) { |
| 15 |
dropdown.removeClass('disabled'); |
| 16 |
dropdown.parent('.ui.dropdown').removeClass('disabled'); |
| 17 |
applyButton.removeClass('disabled'); |
| 18 |
} else { |
| 19 |
dropdown.addClass('disabled'); |
| 20 |
dropdown.parent('.ui.dropdown').addClass('disabled'); |
| 21 |
dropdown.dropdown('set selected', 'none'); |
| 22 |
applyButton.addClass('disabled'); |
| 23 |
} |
| 24 |
}; |
| 25 |
|
| 26 |
jQuery(function () { |
| 27 |
|
| 28 |
// to fix issue not loaded calendar js library |
| 29 |
if (jQuery('.ui.calendar').length > 0) { |
| 30 |
if (mainwpParams.use_wp_datepicker == 1) { |
| 31 |
jQuery('#mainwp-manage-pages .ui.calendar input[type=text],#mainwp-manage-posts .ui.calendar input[type=text]').datepicker({ dateFormat: "yy-mm-dd" }); |
| 32 |
} else { |
| 33 |
mainwp_init_ui_calendar('#mainwp-manage-pages .ui.calendar, #mainwp-manage-posts .ui.calendar'); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
updatePagesBulkActionsState(); |
| 38 |
|
| 39 |
jQuery(document).on('change', '#mainwp-pages-table .check-column INPUT:checkbox', function () { |
| 40 |
updatePagesBulkActionsState(); |
| 41 |
}); |
| 42 |
|
| 43 |
jQuery(document).on('click', '#mainwp_show_pages', function () { |
| 44 |
mainwp_fetch_pages(); |
| 45 |
}); |
| 46 |
jQuery(document).on('click', '.page_submitpublish', function () { |
| 47 |
mainwppage_postAction(jQuery(this), 'publish'); |
| 48 |
return false; |
| 49 |
}); |
| 50 |
jQuery(document).on('click', '.page_submitdelete', function () { |
| 51 |
mainwppage_postAction(jQuery(this), 'trash'); |
| 52 |
return false; |
| 53 |
}); |
| 54 |
jQuery(document).on('click', '.page_submitdelete_perm', function () { |
| 55 |
mainwppage_postAction(jQuery(this), 'delete'); |
| 56 |
return false; |
| 57 |
}); |
| 58 |
jQuery(document).on('click', '.page_submitrestore', function () { |
| 59 |
mainwppage_postAction(jQuery(this), 'restore'); |
| 60 |
return false; |
| 61 |
}); |
| 62 |
jQuery(document).on('click', '#mainwp-do-pages-bulk-actions', function () { |
| 63 |
let action = jQuery('#mainwp-bulk-actions').val(); |
| 64 |
if (action != 'trash' && action != 'restore' && action != 'delete') { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
let tmp = jQuery("input[name='page[]']:checked"); |
| 69 |
countSent = tmp.length; |
| 70 |
|
| 71 |
if (countSent == 0) |
| 72 |
return; |
| 73 |
|
| 74 |
let _callback = function () { |
| 75 |
jQuery('#mainwp-do-pages-bulk-actions').attr('disabled', 'true'); |
| 76 |
tmp.each( |
| 77 |
function (index, elem) { |
| 78 |
mainwppage_postAction(elem, action); |
| 79 |
} |
| 80 |
); |
| 81 |
}; |
| 82 |
|
| 83 |
if (action == 'delete') { |
| 84 |
let msg = __('You are about to delete %1 page(s). Are you sure you want to proceed?', countSent); |
| 85 |
mainwp_confirm(msg, _callback); |
| 86 |
return; |
| 87 |
} |
| 88 |
_callback(); |
| 89 |
}); |
| 90 |
}); |
| 91 |
|
| 92 |
|
| 93 |
let mainwppage_postAction = function (elem, what) { |
| 94 |
let rowElement = jQuery(elem).closest('tr'); |
| 95 |
let pageId = rowElement.find('.pageId').val(); |
| 96 |
let websiteId = rowElement.find('.websiteId').val(); |
| 97 |
|
| 98 |
if (!rowElement.find('.allowedBulkActions').val().includes('|' + what + '|')) { |
| 99 |
jQuery(elem).prop("checked", false); |
| 100 |
countReceived++; |
| 101 |
|
| 102 |
if (countReceived == countSent) { |
| 103 |
countReceived = 0; |
| 104 |
countSent = 0; |
| 105 |
setTimeout(function () { |
| 106 |
jQuery('#mainwp-do-pages-bulk-actions').prop("disabled", false); |
| 107 |
}, 50); |
| 108 |
} |
| 109 |
|
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
let data = mainwp_secure_data({ |
| 114 |
action: 'mainwp_page_' + what, |
| 115 |
postId: pageId, |
| 116 |
websiteId: websiteId |
| 117 |
}); |
| 118 |
|
| 119 |
rowElement.html('<td colspan="99"><i class="notched circle loading icon"></i> Please wait...</td>'); |
| 120 |
jQuery.post(ajaxurl, data, function (response) { |
| 121 |
if (response.error) { |
| 122 |
rowElement.html('<td colspan="99"><i class="times red icon"></i>' + response.error + '</td>'); |
| 123 |
} else if (response.result) { |
| 124 |
rowElement.html('<td colspan="99"><i class="green check icon"></i> ' + response.result + '</td>'); |
| 125 |
if (jQuery(rowElement).hasClass('child')) { |
| 126 |
jQuery(rowElement).prev().hide(); |
| 127 |
} |
| 128 |
} |
| 129 |
countReceived++; |
| 130 |
|
| 131 |
if (countReceived == countSent) { |
| 132 |
countReceived = 0; |
| 133 |
countSent = 0; |
| 134 |
jQuery('#mainwp-do-pages-bulk-actions').prop("disabled", false); |
| 135 |
} |
| 136 |
}, 'json'); |
| 137 |
|
| 138 |
return false; |
| 139 |
}; |
| 140 |
|
| 141 |
let mainwp_fetch_pages = function () { |
| 142 |
|
| 143 |
let params = mainwp_fetch_pages_prepare(); |
| 144 |
|
| 145 |
if (typeof params !== 'object') { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
let _status = params['_status']; |
| 150 |
let selected_sites = params['selected_sites']; |
| 151 |
let selected_clients = params['selected_clients']; |
| 152 |
let selected_groups = params['selected_groups']; |
| 153 |
|
| 154 |
let data = mainwp_secure_data({ |
| 155 |
action: 'mainwp_pages_search', |
| 156 |
keyword: jQuery('#mainwp_page_search_by_keyword').val(), |
| 157 |
dtsstart: jQuery('#mainwp_page_search_by_dtsstart').val(), |
| 158 |
dtsstop: jQuery('#mainwp_page_search_by_dtsstop').val(), |
| 159 |
status: _status, |
| 160 |
'groups[]': selected_groups, |
| 161 |
'sites[]': selected_sites, |
| 162 |
'clients[]': selected_clients, |
| 163 |
maximum: jQuery("#mainwp_maximumPages").val(), |
| 164 |
search_on: jQuery("#mainwp_page_search_on").val(), |
| 165 |
}); |
| 166 |
|
| 167 |
jQuery('#mainwp-loading-pages-row').show(); |
| 168 |
jQuery.post(ajaxurl, data, function (response) { |
| 169 |
response = response.trim(); |
| 170 |
jQuery('#mainwp-loading-pages-row').hide(); |
| 171 |
jQuery('#mainwp_pages_main').show(); |
| 172 |
jQuery('#mainwp_pages_wrap_table').html(response); |
| 173 |
// re-initialize datatable |
| 174 |
jQuery("#mainwp-pages-table").DataTable().destroy(); |
| 175 |
jQuery('#mainwp-pages-table').DataTable({ |
| 176 |
"responsive": true, |
| 177 |
"colReorder": { columns: ":not(.check-column):not(:last-child)" }, |
| 178 |
"stateSave": true, |
| 179 |
"pagingType": "full_numbers", |
| 180 |
"scrollX": true, |
| 181 |
"lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]], |
| 182 |
"order": [], |
| 183 |
"columnDefs": [{ |
| 184 |
"targets": 'no-sort', |
| 185 |
"orderable": false |
| 186 |
}], |
| 187 |
"drawCallback": function () { |
| 188 |
setTimeout(() => { |
| 189 |
jQuery('#mainwp-pages-table .ui.dropdown').dropdown(); |
| 190 |
jQuery('#mainwp-pages-table .ui.checkbox').checkbox(); |
| 191 |
mainwp_table_check_columns_init(); // ajax: to fix checkbox all. |
| 192 |
mainwp_datatable_fix_menu_overflow('#mainwp-pages-table'); |
| 193 |
updatePagesBulkActionsState(); |
| 194 |
}, 1000); |
| 195 |
}, |
| 196 |
select: { |
| 197 |
items: 'row', |
| 198 |
style: 'multi+shift', |
| 199 |
selector: 'tr>td:not(.not-selectable)' |
| 200 |
} |
| 201 |
}).on('select', function (e, dt, type, indexes) { |
| 202 |
if ('row' == type) { |
| 203 |
dt.rows(indexes) |
| 204 |
.nodes() |
| 205 |
.to$().find('td.check-column .ui.checkbox').checkbox('set checked'); |
| 206 |
updatePagesBulkActionsState(); |
| 207 |
} |
| 208 |
}).on('deselect', function (e, dt, type, indexes) { |
| 209 |
if ('row' == type) { |
| 210 |
dt.rows(indexes) |
| 211 |
.nodes() |
| 212 |
.to$().find('td.check-column .ui.checkbox').checkbox('set unchecked'); |
| 213 |
updatePagesBulkActionsState(); |
| 214 |
} |
| 215 |
}).on('columns-reordered', function () { |
| 216 |
setTimeout(() => { |
| 217 |
jQuery('#mainwp-pages-table .ui.dropdown').dropdown(); |
| 218 |
jQuery('#mainwp-pages-table .ui.checkbox').checkbox(); |
| 219 |
mainwp_datatable_fix_menu_overflow('#mainwp-pages-table'); |
| 220 |
mainwp_table_check_columns_init(); // ajax: to fix checkbox all. |
| 221 |
updatePagesBulkActionsState(); |
| 222 |
}, 1000); |
| 223 |
}); |
| 224 |
}); |
| 225 |
}; |
| 226 |
|
| 227 |
let mainwp_fetch_pages_prepare = function () { // NOSONAR - complexity 19/15. |
| 228 |
|
| 229 |
let errors = []; |
| 230 |
let selected_sites = []; |
| 231 |
let selected_groups = []; |
| 232 |
let selected_clients = []; |
| 233 |
|
| 234 |
if (jQuery('input[name="select_by"]').val() == 'site') { |
| 235 |
jQuery("input[name='selected_sites[]']:checked").each(function () { |
| 236 |
selected_sites.push(jQuery(this).val()); |
| 237 |
}); |
| 238 |
if (selected_sites.length == 0) { |
| 239 |
errors.push('<div class="ui yellow message">' + __('Please select websites or groups or clients.') + '</div>'); |
| 240 |
} |
| 241 |
} else if (jQuery('input[name="select_by"]').val() == 'client') { |
| 242 |
jQuery("input[name='selected_clients[]']:checked").each(function () { |
| 243 |
selected_clients.push(jQuery(this).val()); |
| 244 |
}); |
| 245 |
if (selected_clients.length == 0) { |
| 246 |
errors.push('<div class="ui yellow message">' + __('Please select websites or groups or clients.') + '</div>'); |
| 247 |
} |
| 248 |
} else { |
| 249 |
jQuery("input[name='selected_groups[]']:checked").each(function () { |
| 250 |
selected_groups.push(jQuery(this).val()); |
| 251 |
}); |
| 252 |
if (selected_groups.length == 0) { |
| 253 |
errors.push('<div class="ui yellow message">' + __('Please select websites or groups or clients.') + '</div>'); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
let _status = ''; |
| 258 |
let statuses = jQuery("#mainwp_page_search_type").dropdown("get value"); |
| 259 |
if (statuses == null) |
| 260 |
errors.push('Please select a page status.'); |
| 261 |
else { |
| 262 |
_status = statuses.join(','); |
| 263 |
} |
| 264 |
|
| 265 |
if (errors.length > 0) { |
| 266 |
jQuery('#mainwp_pages_error').html(errors.join('<br />')); |
| 267 |
jQuery('#mainwp_pages_error').show(); |
| 268 |
return; |
| 269 |
} else { |
| 270 |
jQuery('#mainwp_pages_error').html(""); |
| 271 |
jQuery('#mainwp_pages_error').hide(); |
| 272 |
} |
| 273 |
|
| 274 |
return { |
| 275 |
'selected_sites': selected_sites, |
| 276 |
'selected_groups': selected_groups, |
| 277 |
'selected_clients': selected_clients, |
| 278 |
'_status': _status, |
| 279 |
} |
| 280 |
} |
| 281 |
/** |
| 282 |
* MainWP_Post.page |
| 283 |
*/ |
| 284 |
|
| 285 |
let updatePostsBulkActionsState = function () { |
| 286 |
let checkedCount = jQuery('#mainwp-posts-table input[name="post[]"]:checked').length; |
| 287 |
let dropdown = jQuery('#mainwp-manage-posts #mainwp-bulk-actions'); |
| 288 |
let applyButton = jQuery('#mainwp-do-posts-bulk-actions'); |
| 289 |
|
| 290 |
if (checkedCount > 0) { |
| 291 |
dropdown.removeClass('disabled'); |
| 292 |
dropdown.parent('.ui.dropdown').removeClass('disabled'); |
| 293 |
applyButton.removeClass('disabled'); |
| 294 |
} else { |
| 295 |
dropdown.addClass('disabled'); |
| 296 |
dropdown.parent('.ui.dropdown').addClass('disabled'); |
| 297 |
dropdown.dropdown('set selected', 'none'); |
| 298 |
applyButton.addClass('disabled'); |
| 299 |
} |
| 300 |
}; |
| 301 |
|
| 302 |
let mainwp_copyTextToClipboard = function (value) { |
| 303 |
|
| 304 |
if (!value) { |
| 305 |
return Promise.reject(new Error('Empty clipboard value')); |
| 306 |
} |
| 307 |
|
| 308 |
if (navigator.clipboard && globalThis.isSecureContext) { |
| 309 |
return navigator.clipboard.writeText(value); |
| 310 |
} |
| 311 |
|
| 312 |
return new Promise(function (resolve, reject) { |
| 313 |
|
| 314 |
let tempField = jQuery('<textarea readonly class="mainwp-copy-temp-field" aria-hidden="true"></textarea>'); |
| 315 |
tempField.css({ |
| 316 |
position: 'absolute', |
| 317 |
left: '-9999px', |
| 318 |
top: '0' |
| 319 |
}); |
| 320 |
jQuery('body').append(tempField); |
| 321 |
tempField.val(value).trigger('focus').trigger('select'); |
| 322 |
|
| 323 |
try { |
| 324 |
if (document.execCommand('copy')) { |
| 325 |
tempField.remove(); |
| 326 |
resolve(); |
| 327 |
return; |
| 328 |
} |
| 329 |
} catch (err) { |
| 330 |
tempField.remove(); |
| 331 |
reject(err); |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
tempField.remove(); |
| 336 |
reject(new Error('Copy command failed')); |
| 337 |
}); |
| 338 |
}; |
| 339 |
|
| 340 |
jQuery(function () { |
| 341 |
|
| 342 |
updatePostsBulkActionsState(); |
| 343 |
|
| 344 |
jQuery(document).on('change', '#mainwp-posts-table .check-column INPUT:checkbox', function () { |
| 345 |
updatePostsBulkActionsState(); |
| 346 |
}); |
| 347 |
|
| 348 |
jQuery(document).on('click', '#mainwp_show_posts', function () { |
| 349 |
mainwp_fetch_posts(); |
| 350 |
}); |
| 351 |
jQuery(document).on('click', '.post_submitdelete', function () { |
| 352 |
mainwppost_postAction(jQuery(this), 'trash'); |
| 353 |
return false; |
| 354 |
}); |
| 355 |
jQuery(document).on('click', '.post_submitpublish', function () { |
| 356 |
mainwppost_postAction(jQuery(this), 'publish'); |
| 357 |
return false; |
| 358 |
}); |
| 359 |
jQuery(document).on('click', '.post_submitunpublish', function () { |
| 360 |
mainwppost_postAction(jQuery(this), 'unpublish'); |
| 361 |
return false; |
| 362 |
}); |
| 363 |
jQuery(document).on('click', '.post_submitapprove', function () { |
| 364 |
mainwppost_postAction(jQuery(this), 'approve'); |
| 365 |
return false; |
| 366 |
}); |
| 367 |
jQuery(document).on('click', '.post_submitdelete_perm', function () { |
| 368 |
mainwppost_postAction(jQuery(this), 'delete'); |
| 369 |
return false; |
| 370 |
}); |
| 371 |
jQuery(document).on('click', '.post_submitrestore', function () { |
| 372 |
mainwppost_postAction(jQuery(this), 'restore'); |
| 373 |
return false; |
| 374 |
}); |
| 375 |
|
| 376 |
jQuery(document).on('click', '.post_getedit', function () { |
| 377 |
mainwppost_postAction(jQuery(this), 'get_edit', 'post'); |
| 378 |
return false; |
| 379 |
}); |
| 380 |
|
| 381 |
jQuery(document).on('click', '.page_getedit', function () { |
| 382 |
mainwppost_postAction(jQuery(this), 'get_edit', 'page'); |
| 383 |
return false; |
| 384 |
}); |
| 385 |
|
| 386 |
jQuery(document).on('click', '.mainwp-copy-row-value', function () { |
| 387 |
let button = jQuery(this); |
| 388 |
let rowElement = button.closest('tr'); |
| 389 |
let target = button.data('copy-target'); |
| 390 |
let value = ''; |
| 391 |
|
| 392 |
if (target) { |
| 393 |
value = rowElement.find(target).val() || ''; |
| 394 |
} |
| 395 |
|
| 396 |
mainwp_copyTextToClipboard(value).catch(function () { |
| 397 |
alert(__('Unable to copy to clipboard.')); |
| 398 |
}); |
| 399 |
|
| 400 |
return false; |
| 401 |
}); |
| 402 |
|
| 403 |
jQuery(document).on('click', '#mainwp-do-posts-bulk-actions', function () { |
| 404 |
let action = jQuery('#mainwp-bulk-actions').val(); |
| 405 |
if (action != 'publish' && action != 'unpublish' && action != 'trash' && action != 'restore' && action != 'delete') { |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
let tmp = jQuery("input[name='post[]']:checked"); |
| 410 |
countSent = tmp.length; |
| 411 |
|
| 412 |
if (countSent == 0) |
| 413 |
return; |
| 414 |
|
| 415 |
let _callback = function () { |
| 416 |
jQuery('#mainwp-do-posts-bulk-actions').attr('disabled', 'true'); |
| 417 |
tmp.each( |
| 418 |
function (index, elem) { |
| 419 |
mainwppost_postAction(elem, action); |
| 420 |
} |
| 421 |
); |
| 422 |
} |
| 423 |
if (action == 'delete') { |
| 424 |
let msg = __('You are about to delete %1 post(s). Are you sure you want to proceed?', countSent); |
| 425 |
mainwp_confirm(msg, _callback); |
| 426 |
return; |
| 427 |
} |
| 428 |
_callback(); |
| 429 |
}); |
| 430 |
}); |
| 431 |
|
| 432 |
let mainwppost_postAction = function (elem, what, postType) { |
| 433 |
let rowElement = jQuery(elem).closest('tr'); |
| 434 |
let postId = rowElement.find('.postId').val(); |
| 435 |
let websiteId = rowElement.find('.websiteId').val(); |
| 436 |
if (!rowElement.find('.allowedBulkActions').val().includes('|' + what + '|')) { |
| 437 |
jQuery(elem).prop("checked", false); |
| 438 |
countReceived++; |
| 439 |
|
| 440 |
if (countReceived == countSent) { |
| 441 |
countReceived = 0; |
| 442 |
countSent = 0; |
| 443 |
setTimeout(function () { |
| 444 |
jQuery('#mainwp-do-posts-bulk-actions').prop("disabled", false); |
| 445 |
}, 50); |
| 446 |
} |
| 447 |
|
| 448 |
return; |
| 449 |
} |
| 450 |
|
| 451 |
if (what == 'get_edit' && postType === 'page') { |
| 452 |
postId = rowElement.find('.pageId').val(); |
| 453 |
} |
| 454 |
|
| 455 |
let data = { |
| 456 |
action: 'mainwp_post_' + what, |
| 457 |
postId: postId, |
| 458 |
websiteId: websiteId |
| 459 |
}; |
| 460 |
if (postType !== undefined) { |
| 461 |
data['postType'] = postType; |
| 462 |
} |
| 463 |
data = mainwp_secure_data(data); |
| 464 |
|
| 465 |
rowElement.html('<td colspan="99"><i class="notched circle loading icon"></i> Please wait...</td>'); |
| 466 |
jQuery.post(ajaxurl, data, function (response) { // NOSONAR - complexity 11/10. |
| 467 |
if (response.error) { |
| 468 |
rowElement.html('<td colspan="99"><i class="times red icon"></i>' + response.error + '</td>'); |
| 469 |
} else if (response.result) { |
| 470 |
rowElement.html('<td colspan="99"><i class="green check icon"></i> ' + response.result + '</td>'); |
| 471 |
if (jQuery(rowElement).hasClass('child')) { |
| 472 |
jQuery(rowElement).prev().hide(); |
| 473 |
} |
| 474 |
} else { |
| 475 |
rowElement.hide(); |
| 476 |
if (what == 'get_edit' && response.id) { |
| 477 |
let reloadUrl = ''; |
| 478 |
if (response.redirect_to) { |
| 479 |
reloadUrl = response.redirect_to; |
| 480 |
} else if (postType == 'post') { |
| 481 |
reloadUrl = 'admin.php?page=PostBulkEdit&post_id=' + response.id; |
| 482 |
} else if (postType == 'page') { |
| 483 |
reloadUrl = 'admin.php?page=PageBulkEdit&post_id=' + response.id; |
| 484 |
} |
| 485 |
if (reloadUrl !== '') { |
| 486 |
mainwp_forceReload(reloadUrl); |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
countReceived++; |
| 491 |
if (countReceived == countSent) { |
| 492 |
countReceived = 0; |
| 493 |
countSent = 0; |
| 494 |
jQuery('#mainwp-do-posts-bulk-actions').prop("disabled", false); |
| 495 |
} |
| 496 |
}, 'json'); |
| 497 |
|
| 498 |
return false; |
| 499 |
}; |
| 500 |
|
| 501 |
let mainwp_show_post = function (siteId, postId, userId) { |
| 502 |
let siteElement = jQuery('input[name="selected_sites[]"][siteid="' + siteId + '"]'); |
| 503 |
siteElement.prop('checked', true); |
| 504 |
siteElement.trigger("change"); |
| 505 |
mainwp_fetch_posts(postId, userId); |
| 506 |
}; |
| 507 |
/* eslint-disable complexity */ |
| 508 |
let mainwp_fetch_posts = function (postId, userId, start_sites) { |
| 509 |
|
| 510 |
let params = mainwp_fetch_posts_prepare(postId, userId, start_sites); |
| 511 |
|
| 512 |
if (typeof params !== 'object') { |
| 513 |
return; |
| 514 |
} |
| 515 |
|
| 516 |
start_sites = params['start_sites']; |
| 517 |
|
| 518 |
let errors = params['errors']; |
| 519 |
let selected_sites = params['selected_sites']; |
| 520 |
let selected_clients = params['selected_clients']; |
| 521 |
let selected_groups = params['selected_groups']; |
| 522 |
let bulk_search = params['bulk_search']; |
| 523 |
|
| 524 |
|
| 525 |
let _status = ''; |
| 526 |
let statuses = jQuery("#mainwp_post_search_type").dropdown("get value"); |
| 527 |
if (statuses == null) |
| 528 |
errors.push('<div class="ui yellow message">' + __('Please select at least one post status.') + '</div>'); |
| 529 |
else { |
| 530 |
_status = statuses.join(','); |
| 531 |
} |
| 532 |
|
| 533 |
if (errors.length > 0) { |
| 534 |
mainwp_set_message_zone('#mainwp-message-zone', errors.join('<br />'), 'red'); |
| 535 |
return; |
| 536 |
} else { |
| 537 |
mainwp_set_message_zone('#mainwp-message-zone'); |
| 538 |
} |
| 539 |
|
| 540 |
if (bulk_search && selected_sites.length == 0) { |
| 541 |
mainwp_fetch_posts_done(); |
| 542 |
return; |
| 543 |
} |
| 544 |
|
| 545 |
let data = mainwp_secure_data({ |
| 546 |
action: 'mainwp_posts_search', |
| 547 |
keyword: jQuery('#mainwp_post_search_by_keyword').val(), |
| 548 |
dtsstart: jQuery('#mainwp_post_search_by_dtsstart').val(), |
| 549 |
dtsstop: jQuery('#mainwp_post_search_by_dtsstop').val(), |
| 550 |
status: _status, |
| 551 |
'groups[]': selected_groups, |
| 552 |
'sites[]': selected_sites, |
| 553 |
'clients[]': selected_clients, |
| 554 |
postId: (postId == undefined ? '' : postId), |
| 555 |
userId: (userId == undefined ? '' : userId), |
| 556 |
post_type: jQuery("#mainwp_get_custom_post_types_select").val(), |
| 557 |
maximum: jQuery("#mainwp_maximumPosts").val(), |
| 558 |
search_on: jQuery("#mainwp_post_search_on").val() |
| 559 |
}); |
| 560 |
|
| 561 |
if (bulk_search && start_sites > 0) { |
| 562 |
data.table_content = 1; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
jQuery('#mainwp-loading-posts-row').show(); |
| 567 |
|
| 568 |
jQuery.post(ajaxurl, data, function (response) { |
| 569 |
response = response.trim(); |
| 570 |
if (bulk_search && start_sites > 0) { |
| 571 |
jQuery('#mainwp-posts-list').append(response); |
| 572 |
} else { |
| 573 |
jQuery('#mainwp-posts-table-wrapper').html(response); |
| 574 |
} |
| 575 |
|
| 576 |
if (bulk_search) { |
| 577 |
start_sites = start_sites + num_sites; |
| 578 |
mainwp_fetch_posts(postId, userId, start_sites); |
| 579 |
} else { |
| 580 |
mainwp_fetch_posts_done(); |
| 581 |
} |
| 582 |
}); |
| 583 |
|
| 584 |
}; |
| 585 |
|
| 586 |
let mainwp_fetch_posts_prepare = function (postId, userId, start_sites) { // NOSONAR - complexity. |
| 587 |
|
| 588 |
let errors = []; |
| 589 |
let selected_sites = []; |
| 590 |
let selected_groups = []; |
| 591 |
let selected_clients = []; |
| 592 |
|
| 593 |
let i = 0; |
| 594 |
let num_sites = jQuery('#search-bulk-sites').attr('number-sites'); |
| 595 |
num_sites = Number.parseInt(num_sites); |
| 596 |
|
| 597 |
let select_sites_error = '<div class="ui yellow message">' + __('Please select at least one website or group or client.') + '</div>'; |
| 598 |
|
| 599 |
let bulk_search = num_sites > 0; |
| 600 |
|
| 601 |
if (jQuery('input[name="select_by"]').val() == 'site' && start_sites == undefined) { |
| 602 |
start_sites = 0; |
| 603 |
} |
| 604 |
|
| 605 |
if (jQuery('input[name="select_by"]').val() == 'site') { |
| 606 |
jQuery("input[name='selected_sites[]']:checked").each(function () { |
| 607 |
if (bulk_search) { |
| 608 |
if (i >= start_sites && i < start_sites + num_sites) { |
| 609 |
selected_sites.push(jQuery(this).val()); |
| 610 |
} |
| 611 |
i++; |
| 612 |
} else { |
| 613 |
selected_sites.push(jQuery(this).val()); |
| 614 |
} |
| 615 |
}); |
| 616 |
if (selected_sites.length == 0 && (!bulk_search || (bulk_search && start_sites == 0))) { |
| 617 |
errors.push(select_sites_error); |
| 618 |
} |
| 619 |
} else if (jQuery('input[name="select_by"]').val() == 'client') { |
| 620 |
jQuery("input[name='selected_clients[]']:checked").each(function () { |
| 621 |
selected_clients.push(jQuery(this).val()); |
| 622 |
}); |
| 623 |
if (selected_clients.length == 0) { |
| 624 |
errors.push(select_sites_error); |
| 625 |
} |
| 626 |
} else if (jQuery('input[name="select_by"]').val() == 'group') { |
| 627 |
jQuery("input[name='selected_groups[]']:checked").each(function () { |
| 628 |
selected_groups.push(jQuery(this).val()); |
| 629 |
}); |
| 630 |
|
| 631 |
if (selected_groups.length == 0) { |
| 632 |
errors.push(select_sites_error); |
| 633 |
} else if (selected_groups.length > 0 && bulk_search && start_sites == undefined) { |
| 634 |
start_sites = 0; |
| 635 |
// get sites of groups. |
| 636 |
let data = mainwp_secure_data({ |
| 637 |
action: 'mainwp_get_sites_of_groups', |
| 638 |
'groups[]': selected_groups |
| 639 |
}); |
| 640 |
jQuery('#mainwp-loading-posts-row').show(); |
| 641 |
jQuery.post(ajaxurl, data, function (response) { |
| 642 |
let site_ids = response; |
| 643 |
if (site_ids) { |
| 644 |
jQuery("input[name='selected_sites[]'][bulk-search=true]").attr('bulk-search', false); |
| 645 |
jQuery.each(site_ids, function (index, value) { |
| 646 |
jQuery("input[name='selected_sites[]'][value=" + value + "]").attr('bulk-search', true); |
| 647 |
}); |
| 648 |
} |
| 649 |
mainwp_fetch_posts(postId, userId, start_sites); |
| 650 |
}, 'json'); |
| 651 |
return; |
| 652 |
} |
| 653 |
|
| 654 |
if (selected_groups.length > 0 && bulk_search) { |
| 655 |
jQuery("input[name='selected_sites[]'][bulk-search=true]").each(function () { |
| 656 |
if (i >= start_sites && i < start_sites + num_sites) { |
| 657 |
selected_sites.push(jQuery(this).val()); |
| 658 |
} |
| 659 |
i++; |
| 660 |
}); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
return { |
| 665 |
'errors': errors, |
| 666 |
'selected_sites': selected_sites, |
| 667 |
'selected_clients': selected_clients, |
| 668 |
'selected_groups': selected_groups, |
| 669 |
'start_sites': start_sites, |
| 670 |
'bulk_search': bulk_search, |
| 671 |
}; |
| 672 |
} |
| 673 |
|
| 674 |
/* eslint-enable complexity */ |
| 675 |
|
| 676 |
let mainwp_fetch_posts_done = function () { |
| 677 |
jQuery('#mainwp-loading-posts-row').hide(); |
| 678 |
jQuery('#mainwp_posts_main').show(); |
| 679 |
let responsive = true; |
| 680 |
if (jQuery(window).width() > 1140) { |
| 681 |
responsive = false; |
| 682 |
} |
| 683 |
// re-initialize datatable. |
| 684 |
jQuery("#mainwp-posts-table").DataTable().destroy(); |
| 685 |
jQuery('#mainwp-posts-table').DataTable({ |
| 686 |
"responsive": responsive, |
| 687 |
"colReorder": { columns: ":not(.check-column):not(:last-child)" }, |
| 688 |
"stateSave": true, |
| 689 |
"pagingType": "full_numbers", |
| 690 |
"order": [], |
| 691 |
"scrollX": true, |
| 692 |
"lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]], |
| 693 |
"columnDefs": [{ |
| 694 |
"targets": 'no-sort', |
| 695 |
"orderable": false |
| 696 |
}], |
| 697 |
"drawCallback": function () { |
| 698 |
setTimeout(() => { |
| 699 |
jQuery('#mainwp-posts-table .ui.dropdown').dropdown(); |
| 700 |
jQuery('#mainwp-posts-table .ui.checkbox').checkbox(); |
| 701 |
mainwp_datatable_fix_menu_overflow('#mainwp-posts-table'); |
| 702 |
mainwp_table_check_columns_init(); // ajax: to fix checkbox all. |
| 703 |
updatePostsBulkActionsState(); |
| 704 |
}, 1000); |
| 705 |
}, |
| 706 |
select: { |
| 707 |
items: 'row', |
| 708 |
style: 'multi+shift', |
| 709 |
selector: 'tr>td:not(.not-selectable)' |
| 710 |
} |
| 711 |
}).on('select', function (e, dt, type, indexes) { |
| 712 |
if ('row' == type) { |
| 713 |
dt.rows(indexes) |
| 714 |
.nodes() |
| 715 |
.to$().find('td.check-column .ui.checkbox').checkbox('set checked'); |
| 716 |
updatePostsBulkActionsState(); |
| 717 |
} |
| 718 |
}).on('deselect', function (e, dt, type, indexes) { |
| 719 |
if ('row' == type) { |
| 720 |
dt.rows(indexes) |
| 721 |
.nodes() |
| 722 |
.to$().find('td.check-column .ui.checkbox').checkbox('set unchecked'); |
| 723 |
updatePostsBulkActionsState(); |
| 724 |
} |
| 725 |
}).on('columns-reordered', function () { |
| 726 |
setTimeout(() => { |
| 727 |
jQuery('#mainwp-posts-table .ui.dropdown').dropdown(); |
| 728 |
jQuery('#mainwp-posts-table .ui.checkbox').checkbox(); |
| 729 |
mainwp_datatable_fix_menu_overflow('#mainwp-posts-table'); |
| 730 |
mainwp_table_check_columns_init(); // ajax: to fix checkbox all. |
| 731 |
updatePostsBulkActionsState(); |
| 732 |
}, 1000); |
| 733 |
}); |
| 734 |
} |
| 735 |
|