| 1 |
<?php |
| 2 |
/** |
| 3 |
* Robots.txt Admin Page Scripts |
| 4 |
* |
| 5 |
* @package Search Atlas SEO |
| 6 |
* @copyright Copyright (C) 2021-2025, Search Atlas Group - support@searchatlas.com |
| 7 |
* @since 2.5.6 |
| 8 |
*/ |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if (!defined('WPINC')) { |
| 12 |
die; |
| 13 |
} |
| 14 |
?> |
| 15 |
<script> |
| 16 |
jQuery(document).ready(function($) { |
| 17 |
let allowSave = false; |
| 18 |
|
| 19 |
// Check for restore success message after page reload |
| 20 |
const restoreMessage = sessionStorage.getItem('metasync_robots_restore_message'); |
| 21 |
if (restoreMessage) { |
| 22 |
// Remove the message from sessionStorage |
| 23 |
sessionStorage.removeItem('metasync_robots_restore_message'); |
| 24 |
|
| 25 |
// Display the success message |
| 26 |
const $notice = $('<div class="notice notice-success is-dismissible" style="margin: 20px 0;"><p>' + restoreMessage + '</p></div>'); |
| 27 |
$('.metasync-robots-txt-page').prepend($notice); |
| 28 |
|
| 29 |
// Scroll to top to show the message |
| 30 |
$('html, body').animate({ scrollTop: 0 }, 300); |
| 31 |
|
| 32 |
// Auto-dismiss after 5 seconds |
| 33 |
setTimeout(function() { |
| 34 |
$notice.fadeOut(300, function() { |
| 35 |
$(this).remove(); |
| 36 |
}); |
| 37 |
}, 5000); |
| 38 |
} |
| 39 |
|
| 40 |
// Modal functions |
| 41 |
function showModal(type, title, message, showConfirm) { |
| 42 |
const $modal = $('#robots-validation-modal'); |
| 43 |
const $header = $modal.find('.metasync-modal-header'); |
| 44 |
const $confirmBtn = $('#modal-confirm-btn'); |
| 45 |
|
| 46 |
// Set header style |
| 47 |
$header.removeClass('error warning success').addClass(type); |
| 48 |
|
| 49 |
// Set icon |
| 50 |
let icon = ''; |
| 51 |
if (type === 'error') { |
| 52 |
icon = '<div class="metasync-modal-icon error"><span class="dashicons dashicons-dismiss"></span></div>'; |
| 53 |
} else if (type === 'warning') { |
| 54 |
icon = '<div class="metasync-modal-icon warning"><span class="dashicons dashicons-warning"></span></div>'; |
| 55 |
} else { |
| 56 |
icon = '<div class="metasync-modal-icon success"><span class="dashicons dashicons-yes-alt"></span></div>'; |
| 57 |
} |
| 58 |
|
| 59 |
// Set content |
| 60 |
$('#modal-title').html(title); |
| 61 |
$('#modal-body').html(icon + '<div class="metasync-modal-message">' + message + '</div>'); |
| 62 |
|
| 63 |
// Show/hide confirm button |
| 64 |
if (showConfirm) { |
| 65 |
$confirmBtn.show(); |
| 66 |
} else { |
| 67 |
$confirmBtn.hide(); |
| 68 |
} |
| 69 |
|
| 70 |
// Show modal |
| 71 |
$modal.fadeIn(200); |
| 72 |
} |
| 73 |
|
| 74 |
function closeModal() { |
| 75 |
const $modal = $('#robots-validation-modal'); |
| 76 |
const $modalContent = $modal.find('.metasync-modal-content'); |
| 77 |
const $confirmBtn = $('#modal-confirm-btn'); |
| 78 |
const $cancelBtn = $('.metasync-modal-cancel'); |
| 79 |
|
| 80 |
// Remove preview class when closing |
| 81 |
$modalContent.removeClass('preview'); |
| 82 |
|
| 83 |
// Reset confirm button to original state |
| 84 |
$confirmBtn.removeClass('button-restore') |
| 85 |
.addClass('button-primary') |
| 86 |
.text('<?php esc_html_e('Save Anyway', 'metasync'); ?>') |
| 87 |
.removeData('backup-id') |
| 88 |
.removeData('nonce'); |
| 89 |
|
| 90 |
// Reset cancel button |
| 91 |
$cancelBtn.text('<?php esc_html_e('Cancel', 'metasync'); ?>'); |
| 92 |
|
| 93 |
$modal.fadeOut(200); |
| 94 |
} |
| 95 |
|
| 96 |
// Close modal on overlay or close button click |
| 97 |
$('.metasync-modal-overlay, .metasync-modal-close, .metasync-modal-cancel').on('click', function() { |
| 98 |
closeModal(); |
| 99 |
}); |
| 100 |
|
| 101 |
// Prevent closing when clicking modal content |
| 102 |
$('.metasync-modal-content').on('click', function(e) { |
| 103 |
e.stopPropagation(); |
| 104 |
}); |
| 105 |
|
| 106 |
// Form submission with validation |
| 107 |
$('#robots-txt-form').on('submit', function(e) { |
| 108 |
if (allowSave) { |
| 109 |
allowSave = false; |
| 110 |
return true; // Allow form submission |
| 111 |
} |
| 112 |
|
| 113 |
e.preventDefault(); |
| 114 |
|
| 115 |
const content = $('#robots-txt-editor').val(); |
| 116 |
|
| 117 |
// Validate via AJAX |
| 118 |
$.ajax({ |
| 119 |
url: ajaxurl, |
| 120 |
type: 'POST', |
| 121 |
data: { |
| 122 |
action: 'metasync_validate_robots', |
| 123 |
nonce: '<?php echo wp_create_nonce('metasync_nonce'); ?>', |
| 124 |
content: content |
| 125 |
}, |
| 126 |
success: function(response) { |
| 127 |
if (response.success && response.data) { |
| 128 |
// Check for errors |
| 129 |
if (response.data.errors && response.data.errors.length > 0) { |
| 130 |
let errorHtml = '<div><strong><?php esc_html_e('The following errors must be fixed before saving:', 'metasync'); ?></strong><ul style="margin: 16px 0 0 20px; text-align: left;">'; |
| 131 |
response.data.errors.forEach(function(error) { |
| 132 |
errorHtml += '<li>' + error + '</li>'; |
| 133 |
}); |
| 134 |
errorHtml += '</ul></div>'; |
| 135 |
|
| 136 |
showModal('error', '<?php esc_html_e('Syntax Errors Found', 'metasync'); ?>', errorHtml, false); |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
// Check for warnings |
| 141 |
if (response.data.warnings && response.data.warnings.length > 0) { |
| 142 |
let warningHtml = '<div><strong><?php esc_html_e('The following warnings were detected:', 'metasync'); ?></strong><ul style="margin: 16px 0 0 20px; text-align: left;">'; |
| 143 |
response.data.warnings.forEach(function(warning) { |
| 144 |
warningHtml += '<li>' + warning + '</li>'; |
| 145 |
}); |
| 146 |
warningHtml += '</ul><p style="margin-top: 16px;"><strong><?php esc_html_e('Do you want to save anyway?', 'metasync'); ?></strong></p></div>'; |
| 147 |
|
| 148 |
showModal('warning', '<?php esc_html_e('Validation Warnings', 'metasync'); ?>', warningHtml, true); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// No errors or warnings, submit form |
| 153 |
allowSave = true; |
| 154 |
$('#robots-txt-form').submit(); |
| 155 |
} |
| 156 |
} |
| 157 |
}); |
| 158 |
}); |
| 159 |
|
| 160 |
// Confirm button handler (for warnings and restore from preview) |
| 161 |
$('#modal-confirm-btn').on('click', function() { |
| 162 |
const $btn = $(this); |
| 163 |
|
| 164 |
// Check if this is a restore action from preview |
| 165 |
if ($btn.hasClass('button-restore')) { |
| 166 |
const backupId = $btn.data('backup-id'); |
| 167 |
const nonce = $btn.data('nonce'); |
| 168 |
|
| 169 |
if (!backupId) { |
| 170 |
alert('<?php esc_html_e('Invalid backup ID', 'metasync'); ?>'); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
if (!confirm('<?php esc_html_e('Are you sure you want to restore this backup? Current content will be backed up automatically.', 'metasync'); ?>')) { |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
// Show loading state |
| 179 |
const originalText = $btn.text(); |
| 180 |
$btn.prop('disabled', true).html('<span class="dashicons dashicons-update dashicons-spin"></span> <?php esc_html_e('Restoring...', 'metasync'); ?>'); |
| 181 |
|
| 182 |
// Close the modal |
| 183 |
closeModal(); |
| 184 |
|
| 185 |
// Perform restore via AJAX |
| 186 |
$.ajax({ |
| 187 |
url: ajaxurl, |
| 188 |
type: 'POST', |
| 189 |
data: { |
| 190 |
action: 'metasync_restore_robots_backup', |
| 191 |
backup_id: backupId, |
| 192 |
nonce: nonce |
| 193 |
}, |
| 194 |
success: function(response) { |
| 195 |
if (response.success) { |
| 196 |
// Store success message in sessionStorage |
| 197 |
if (response.data && response.data.message) { |
| 198 |
sessionStorage.setItem('metasync_robots_restore_message', response.data.message); |
| 199 |
} |
| 200 |
|
| 201 |
// Reload the page |
| 202 |
window.location.reload(); |
| 203 |
} else { |
| 204 |
// Show error message |
| 205 |
const errorMessage = response.data && response.data.message ? response.data.message : '<?php esc_html_e('Failed to restore backup.', 'metasync'); ?>'; |
| 206 |
alert(errorMessage); |
| 207 |
|
| 208 |
// Restore button |
| 209 |
$btn.prop('disabled', false).text(originalText); |
| 210 |
} |
| 211 |
}, |
| 212 |
error: function() { |
| 213 |
alert('<?php esc_html_e('An error occurred while restoring the backup.', 'metasync'); ?>'); |
| 214 |
|
| 215 |
// Restore button |
| 216 |
$btn.prop('disabled', false).text(originalText); |
| 217 |
} |
| 218 |
}); |
| 219 |
|
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
// Original behavior for validation warnings |
| 224 |
closeModal(); |
| 225 |
allowSave = true; |
| 226 |
$('#robots-txt-form').submit(); |
| 227 |
}); |
| 228 |
|
| 229 |
// Reset to default |
| 230 |
$('#reset-to-default').on('click', function() { |
| 231 |
if (confirm('<?php esc_html_e('Are you sure you want to reset to default content? This will replace your current content.', 'metasync'); ?>')) { |
| 232 |
$.ajax({ |
| 233 |
url: ajaxurl, |
| 234 |
type: 'POST', |
| 235 |
data: { |
| 236 |
action: 'metasync_get_default_robots', |
| 237 |
nonce: '<?php echo wp_create_nonce('metasync_nonce'); ?>' |
| 238 |
}, |
| 239 |
success: function(response) { |
| 240 |
if (response.success) { |
| 241 |
$('#robots-txt-editor').val(response.data.content); |
| 242 |
} |
| 243 |
} |
| 244 |
}); |
| 245 |
} |
| 246 |
}); |
| 247 |
|
| 248 |
// Validate content button |
| 249 |
$('#validate-content').on('click', function() { |
| 250 |
const content = $('#robots-txt-editor').val(); |
| 251 |
|
| 252 |
$.ajax({ |
| 253 |
url: ajaxurl, |
| 254 |
type: 'POST', |
| 255 |
data: { |
| 256 |
action: 'metasync_validate_robots', |
| 257 |
nonce: '<?php echo wp_create_nonce('metasync_nonce'); ?>', |
| 258 |
content: content |
| 259 |
}, |
| 260 |
success: function(response) { |
| 261 |
const $results = $('#validation-results'); |
| 262 |
$results.show(); |
| 263 |
|
| 264 |
if (response.success && response.data && response.data.valid) { |
| 265 |
$results.removeClass('error warning').addClass('success'); |
| 266 |
$results.html('<strong><?php esc_html_e('Validation passed!', 'metasync'); ?></strong> <?php esc_html_e('Your robots.txt syntax is correct.', 'metasync'); ?>'); |
| 267 |
|
| 268 |
if (response.data.warnings && response.data.warnings.length > 0) { |
| 269 |
$results.removeClass('success').addClass('warning'); |
| 270 |
let html = '<strong><?php esc_html_e('Validation passed with warnings:', 'metasync'); ?></strong><ul style="margin: 8px 0 0 20px;">'; |
| 271 |
|
| 272 |
response.data.warnings.forEach(function(warning) { |
| 273 |
html += '<li>' + warning + '</li>'; |
| 274 |
}); |
| 275 |
|
| 276 |
html += '</ul>'; |
| 277 |
$results.html(html); |
| 278 |
} |
| 279 |
} else { |
| 280 |
$results.removeClass('success warning').addClass('error'); |
| 281 |
let html = '<strong><?php esc_html_e('Validation failed:', 'metasync'); ?></strong><ul style="margin: 8px 0 0 20px;">'; |
| 282 |
|
| 283 |
if (response.data.errors) { |
| 284 |
response.data.errors.forEach(function(error) { |
| 285 |
html += '<li>' + error + '</li>'; |
| 286 |
}); |
| 287 |
} |
| 288 |
|
| 289 |
html += '</ul>'; |
| 290 |
$results.html(html); |
| 291 |
} |
| 292 |
} |
| 293 |
}); |
| 294 |
}); |
| 295 |
|
| 296 |
// Auto-resize textarea |
| 297 |
$('#robots-txt-editor').on('input', function() { |
| 298 |
this.style.height = 'auto'; |
| 299 |
this.style.height = (this.scrollHeight) + 'px'; |
| 300 |
}); |
| 301 |
|
| 302 |
// Preview backup content |
| 303 |
$('.metasync-preview-backup').on('click', function(e) { |
| 304 |
e.preventDefault(); |
| 305 |
const backupId = $(this).data('backup-id'); |
| 306 |
|
| 307 |
// Show loading state |
| 308 |
const $button = $(this); |
| 309 |
const originalText = $button.html(); |
| 310 |
$button.prop('disabled', true).html('<span class="dashicons dashicons-update dashicons-spin"></span> <?php esc_html_e('Loading...', 'metasync'); ?>'); |
| 311 |
|
| 312 |
$.ajax({ |
| 313 |
url: ajaxurl, |
| 314 |
type: 'POST', |
| 315 |
data: { |
| 316 |
action: 'metasync_preview_robots_backup', |
| 317 |
nonce: '<?php echo wp_create_nonce('metasync_nonce'); ?>', |
| 318 |
backup_id: backupId |
| 319 |
}, |
| 320 |
success: function(response) { |
| 321 |
if (response.success && response.data) { |
| 322 |
// Format the date |
| 323 |
let dateInfo = ''; |
| 324 |
if (response.data.created_at) { |
| 325 |
dateInfo = '<p style="margin-bottom: 16px;"><strong><?php esc_html_e('Created:', 'metasync'); ?></strong> ' + response.data.created_at; |
| 326 |
if (response.data.created_by_name) { |
| 327 |
dateInfo += ' <?php esc_html_e('by', 'metasync'); ?> ' + response.data.created_by_name; |
| 328 |
} |
| 329 |
dateInfo += '</p>'; |
| 330 |
} |
| 331 |
|
| 332 |
// Create preview content with pre/code block for display |
| 333 |
const previewContent = dateInfo + |
| 334 |
'<pre class="metasync-preview-code"><code>' + |
| 335 |
$('<div>').text(response.data.content).html() + |
| 336 |
'</code></pre>'; |
| 337 |
|
| 338 |
// Show in modal |
| 339 |
const $modal = $('#robots-validation-modal'); |
| 340 |
const $modalContent = $modal.find('.metasync-modal-content'); |
| 341 |
const $header = $modal.find('.metasync-modal-header'); |
| 342 |
const $confirmBtn = $('#modal-confirm-btn'); |
| 343 |
const $cancelBtn = $('.metasync-modal-cancel'); |
| 344 |
|
| 345 |
// Add preview class for larger width |
| 346 |
$modalContent.addClass('preview'); |
| 347 |
|
| 348 |
// Set header style |
| 349 |
$header.removeClass('error warning success').addClass('preview'); |
| 350 |
|
| 351 |
// Set content |
| 352 |
$('#modal-title').html('<?php esc_html_e('Backup Preview', 'metasync'); ?>'); |
| 353 |
$('#modal-body').html(previewContent); |
| 354 |
|
| 355 |
// Configure restore button (on the left) |
| 356 |
$confirmBtn.html('<span class="dashicons dashicons-backup"></span> <?php esc_html_e('Restore', 'metasync'); ?>') |
| 357 |
.removeClass('button-primary') |
| 358 |
.addClass('button-restore') |
| 359 |
.data('backup-id', backupId) |
| 360 |
.data('nonce', '<?php echo wp_create_nonce('metasync_restore_robots_backup'); ?>') |
| 361 |
.show(); |
| 362 |
|
| 363 |
// Hide close button for preview |
| 364 |
$cancelBtn.hide(); |
| 365 |
|
| 366 |
// Show modal |
| 367 |
$modal.fadeIn(200); |
| 368 |
} else { |
| 369 |
alert('<?php esc_html_e('Failed to load backup content.', 'metasync'); ?>'); |
| 370 |
} |
| 371 |
|
| 372 |
// Restore button state |
| 373 |
$button.prop('disabled', false).html(originalText); |
| 374 |
}, |
| 375 |
error: function() { |
| 376 |
alert('<?php esc_html_e('An error occurred while loading the backup.', 'metasync'); ?>'); |
| 377 |
// Restore button state |
| 378 |
$button.prop('disabled', false).html(originalText); |
| 379 |
} |
| 380 |
}); |
| 381 |
}); |
| 382 |
|
| 383 |
// Restore backup with AJAX |
| 384 |
$(document).on('click', '.metasync-restore-backup', function(e) { |
| 385 |
e.preventDefault(); |
| 386 |
|
| 387 |
if (!confirm('<?php esc_html_e('Are you sure you want to restore this backup? Current content will be backed up automatically.', 'metasync'); ?>')) { |
| 388 |
return; |
| 389 |
} |
| 390 |
|
| 391 |
const $button = $(this); |
| 392 |
const backupId = $button.data('backup-id'); |
| 393 |
const nonce = $button.data('nonce'); |
| 394 |
|
| 395 |
// Show loading state |
| 396 |
const originalHtml = $button.html(); |
| 397 |
$button.prop('disabled', true).html('<span class="dashicons dashicons-update dashicons-spin"></span> <?php esc_html_e('Restoring...', 'metasync'); ?>'); |
| 398 |
|
| 399 |
$.ajax({ |
| 400 |
url: ajaxurl, |
| 401 |
type: 'POST', |
| 402 |
data: { |
| 403 |
action: 'metasync_restore_robots_backup', |
| 404 |
backup_id: backupId, |
| 405 |
nonce: nonce |
| 406 |
}, |
| 407 |
success: function(response) { |
| 408 |
if (response.success) { |
| 409 |
// Store success message in sessionStorage |
| 410 |
if (response.data && response.data.message) { |
| 411 |
sessionStorage.setItem('metasync_robots_restore_message', response.data.message); |
| 412 |
} |
| 413 |
|
| 414 |
// Reload the page to show updated content and backup list |
| 415 |
window.location.reload(); |
| 416 |
} else { |
| 417 |
// Show error message |
| 418 |
const errorMessage = response.data && response.data.message ? response.data.message : '<?php esc_html_e('Failed to restore backup.', 'metasync'); ?>'; |
| 419 |
alert(errorMessage); |
| 420 |
|
| 421 |
// Restore button state |
| 422 |
$button.prop('disabled', false).html(originalHtml); |
| 423 |
} |
| 424 |
}, |
| 425 |
error: function() { |
| 426 |
alert('<?php esc_html_e('An error occurred while restoring the backup.', 'metasync'); ?>'); |
| 427 |
|
| 428 |
// Restore button state |
| 429 |
$button.prop('disabled', false).html(originalHtml); |
| 430 |
} |
| 431 |
}); |
| 432 |
}); |
| 433 |
|
| 434 |
// Delete backup with AJAX |
| 435 |
$(document).on('click', '.metasync-delete-backup', function(e) { |
| 436 |
e.preventDefault(); |
| 437 |
|
| 438 |
if (!confirm('<?php esc_html_e('Are you sure you want to delete this backup?', 'metasync'); ?>')) { |
| 439 |
return; |
| 440 |
} |
| 441 |
|
| 442 |
const $button = $(this); |
| 443 |
const backupId = $button.data('backup-id'); |
| 444 |
const nonce = $button.data('nonce'); |
| 445 |
const $backupItem = $button.closest('.metasync-backup-item'); |
| 446 |
|
| 447 |
// Show loading state |
| 448 |
const originalHtml = $button.html(); |
| 449 |
$button.prop('disabled', true).html('<span class="dashicons dashicons-update dashicons-spin"></span>'); |
| 450 |
|
| 451 |
$.ajax({ |
| 452 |
url: ajaxurl, |
| 453 |
type: 'POST', |
| 454 |
data: { |
| 455 |
action: 'metasync_delete_robots_backup', |
| 456 |
backup_id: backupId, |
| 457 |
nonce: nonce |
| 458 |
}, |
| 459 |
success: function(response) { |
| 460 |
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) |
| 472 |
if (response.data && response.data.message) { |
| 473 |
// Create a temporary success notice |
| 474 |
const $notice = $('<div class="notice notice-success is-dismissible" style="margin: 20px 0;"><p>' + response.data.message + '</p></div>'); |
| 475 |
$('.metasync-robots-txt-page').prepend($notice); |
| 476 |
|
| 477 |
// Auto-dismiss after 3 seconds |
| 478 |
setTimeout(function() { |
| 479 |
$notice.fadeOut(300, function() { |
| 480 |
$(this).remove(); |
| 481 |
}); |
| 482 |
}, 3000); |
| 483 |
} |
| 484 |
} else { |
| 485 |
// Show error message |
| 486 |
const errorMessage = response.data && response.data.message ? response.data.message : '<?php esc_html_e('Failed to delete backup.', 'metasync'); ?>'; |
| 487 |
alert(errorMessage); |
| 488 |
|
| 489 |
// Restore button state |
| 490 |
$button.prop('disabled', false).html(originalHtml); |
| 491 |
} |
| 492 |
}, |
| 493 |
error: function() { |
| 494 |
alert('<?php esc_html_e('An error occurred while deleting the backup.', 'metasync'); ?>'); |
| 495 |
|
| 496 |
// Restore button state |
| 497 |
$button.prop('disabled', false).html(originalHtml); |
| 498 |
} |
| 499 |
}); |
| 500 |
}); |
| 501 |
}); |
| 502 |
</script> |
| 503 |
|