| @@ -15,8 +15,54 @@ | ||
| 15 | 15 | <script> |
| 16 | 16 | jQuery(document).ready(function($) { |
| 17 | 17 | let allowSave = false; |
| 18 | 18 | |
| 19 | + // Nonce for fetching a paginated page of backups. | |
| 20 | + const backupsPageNonce = '<?php echo esc_js(wp_create_nonce('metasync_get_robots_backups')); ?>'; | |
| 21 | + | |
| 22 | + // Load a page of the Backup History panel and swap it in place. Keeps the | |
| 23 | + // list, its total count, and pagination consistent with backend storage | |
| 24 | + // after navigation or deletion. Clamps to a valid page if the requested | |
| 25 | + // one no longer exists (e.g. the last item on a page was deleted). | |
| 26 | + function loadBackupsPage(page) { | |
| 27 | + const $panel = $('#metasync-backups-panel'); | |
| 28 | + if (!$panel.length) { | |
| 29 | + return; | |
| 30 | + } | |
| 31 | + $panel.css('opacity', 0.5); | |
| 32 | + $.ajax({ | |
| 33 | + url: ajaxurl, | |
| 34 | + type: 'POST', | |
| 35 | + data: { | |
| 36 | + action: 'metasync_get_robots_backups', | |
| 37 | + nonce: backupsPageNonce, | |
| 38 | + page: page | |
| 39 | + }, | |
| 40 | + success: function(response) { | |
| 41 | + if (response.success && response.data && typeof response.data.html === 'string') { | |
| 42 | + $panel.replaceWith(response.data.html); | |
| 43 | + } else { | |
| 44 | + $panel.css('opacity', 1); | |
| 45 | + } | |
| 46 | + }, | |
| 47 | + error: function() { | |
| 48 | + $panel.css('opacity', 1); | |
| 49 | + } | |
| 50 | + }); | |
| 51 | + } | |
| 52 | + | |
| 53 | + // Pagination controls (Prev/Next). | |
| 54 | + $(document).on('click', '.metasync-backup-page', function() { | |
| 55 | + const $btn = $(this); | |
| 56 | + if ($btn.prop('disabled')) { | |
| 57 | + return; | |
| 58 | + } | |
| 59 | + const page = parseInt($btn.data('page'), 10); | |
| 60 | + if (!isNaN(page) && page >= 1) { | |
| 61 | + loadBackupsPage(page); | |
| 62 | + } | |
| 63 | + }); | |
| 64 | + | |
| 19 | 65 | // Check for restore success message after page reload |
| 20 | 66 | const restoreMessage = sessionStorage.getItem('metasync_robots_restore_message'); |
| 21 | 67 | if (restoreMessage) { |
| 22 | 68 | // Remove the message from sessionStorage |
| @@ -299,9 +345,9 @@ | ||
| 299 | 345 | this.style.height = (this.scrollHeight) + 'px'; |
| 300 | 346 | }); |
| 301 | 347 | |
| 302 | 348 | // Preview backup content |
| 303 | - $('.metasync-preview-backup').on('click', function(e) { | |
| 349 | + $(document).on('click', '.metasync-preview-backup', function(e) { | |
| 304 | 350 | e.preventDefault(); |
| 305 | 351 | const backupId = $(this).data('backup-id'); |
| 306 | 352 | |
| 307 | 353 | // Show loading state |
| @@ -441,10 +487,9 @@ | ||
| 441 | 487 | |
| 442 | 488 | const $button = $(this); |
| 443 | 489 | const backupId = $button.data('backup-id'); |
| 444 | 490 | const nonce = $button.data('nonce'); |
| 445 | - const $backupItem = $button.closest('.metasync-backup-item'); | |
| 446 | - | |
| 491 | + | |
| 447 | 492 | // Show loading state |
| 448 | 493 | const originalHtml = $button.html(); |
| 449 | 494 | $button.prop('disabled', true).html('<span class="dashicons dashicons-update dashicons-spin"></span>'); |
| 450 | 495 | |
| @@ -457,24 +502,25 @@ | ||
| 457 | 502 | nonce: nonce |
| 458 | 503 | }, |
| 459 | 504 | success: function(response) { |
| 460 | 505 | if (response.success) { |
| 461 | - // Fade out and remove the backup item | |
| 462 | - $backupItem.fadeOut(300, function() { | |
| 463 | - $(this).remove(); | |
| 464 | - | |
| 465 | - // Check if there are any backups left | |
| 466 | - if ($('.metasync-backup-item').length === 0) { | |
| 467 | - $('.metasync-backups-list').html('<p class="description" style="text-align: center; padding: 20px;"><?php esc_html_e('No backups available yet. Backups are created automatically when you save changes.', 'metasync'); ?></p>'); | |
| 468 | - } | |
| 469 | - }); | |
| 470 | - | |
| 471 | - // Show success message (you can add a notification system here) | |
| 506 | + // Refresh the panel so the count and pagination reflect the | |
| 507 | + // new backend state. Stay on the current page, but if the | |
| 508 | + // deleted entry was the last one on it, fall back a page. | |
| 509 | + const $panel = $('#metasync-backups-panel'); | |
| 510 | + let currentPage = parseInt($panel.data('page'), 10) || 1; | |
| 511 | + const remainingOnPage = $panel.find('.metasync-backup-item').length - 1; | |
| 512 | + if (remainingOnPage <= 0 && currentPage > 1) { | |
| 513 | + currentPage -= 1; | |
| 514 | + } | |
| 515 | + loadBackupsPage(currentPage); | |
| 516 | + | |
| 517 | + // Show success message | |
| 472 | 518 | if (response.data && response.data.message) { |
| 473 | 519 | // Create a temporary success notice |
| 474 | 520 | const $notice = $('<div class="notice notice-success is-dismissible" style="margin: 20px 0;"><p>' + response.data.message + '</p></div>'); |
| 475 | 521 | $('.metasync-robots-txt-page').prepend($notice); |
| 476 | - | |
| 522 | + | |
| 477 | 523 | // Auto-dismiss after 3 seconds |
| 478 | 524 | setTimeout(function() { |
| 479 | 525 | $notice.fadeOut(300, function() { |
| 480 | 526 | $(this).remove(); |