| 1 |
/** |
| 2 |
* Snippet validation before publish/update |
| 3 |
*/ |
| 4 |
(function($) { |
| 5 |
'use strict'; |
| 6 |
|
| 7 |
let validationInProgress = false; |
| 8 |
let validationPassed = false; |
| 9 |
|
| 10 |
/** |
| 11 |
* Validate snippet code via AJAX |
| 12 |
*/ |
| 13 |
function validateSnippetCode(callback) { |
| 14 |
const postId = $('#post_ID').val(); |
| 15 |
|
| 16 |
// Get snippet code from CodeMirror if available, otherwise from textarea |
| 17 |
let snippetCode = $('#post_content').val(); |
| 18 |
if (wp && wp.codeEditor && wp.codeEditor.instances && wp.codeEditor.instances.post_content) { |
| 19 |
const editor = wp.codeEditor.instances.post_content; |
| 20 |
if (editor && editor.codemirror) { |
| 21 |
snippetCode = editor.codemirror.getValue(); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
const snippetType = $('#wbcr_inp_snippet_type').val(); |
| 26 |
|
| 27 |
// Show validation indicator |
| 28 |
const $publishButton = $('#publish'); |
| 29 |
const originalText = $publishButton.val(); |
| 30 |
const originalClasses = $publishButton.attr('class'); |
| 31 |
$publishButton.val(wbcrInpValidation.validatingText).prop('disabled', true); |
| 32 |
|
| 33 |
$.ajax({ |
| 34 |
url: ajaxurl, |
| 35 |
type: 'POST', |
| 36 |
data: { |
| 37 |
action: 'wbcr_inp_ajax_validate_snippet', |
| 38 |
post_id: postId, |
| 39 |
snippet_code: snippetCode, |
| 40 |
snippet_type: snippetType, |
| 41 |
nonce: wbcrInpValidation.nonce |
| 42 |
}, |
| 43 |
success: function(response) { |
| 44 |
// Restore button state |
| 45 |
$publishButton.prop('disabled', false); |
| 46 |
$publishButton.val(originalText); |
| 47 |
if (originalClasses) { |
| 48 |
$publishButton.attr('class', originalClasses); |
| 49 |
} |
| 50 |
|
| 51 |
if (response.success) { |
| 52 |
callback(true); |
| 53 |
} else { |
| 54 |
showValidationError(response.data.message); |
| 55 |
callback(false); |
| 56 |
} |
| 57 |
}, |
| 58 |
error: function(xhr) { |
| 59 |
// Restore button state |
| 60 |
$publishButton.prop('disabled', false); |
| 61 |
$publishButton.val(originalText); |
| 62 |
if (originalClasses) { |
| 63 |
$publishButton.attr('class', originalClasses); |
| 64 |
} |
| 65 |
|
| 66 |
let errorMessage = wbcrInpValidation.errorText; |
| 67 |
if (xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) { |
| 68 |
errorMessage = xhr.responseJSON.data.message; |
| 69 |
} |
| 70 |
|
| 71 |
showValidationError(errorMessage); |
| 72 |
callback(false); |
| 73 |
} |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Show validation error message |
| 79 |
*/ |
| 80 |
function showValidationError(message) { |
| 81 |
// Remove any existing error notices |
| 82 |
$('.winp-validation-error').remove(); |
| 83 |
|
| 84 |
// Create error notice |
| 85 |
const $notice = $('<div>', { |
| 86 |
class: 'notice notice-error is-dismissible winp-validation-error', |
| 87 |
html: '<p><strong>' + wbcrInpValidation.errorTitle + '</strong><br>' + message + '</p>' |
| 88 |
}); |
| 89 |
|
| 90 |
// Add dismiss button functionality |
| 91 |
$notice.append('<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>'); |
| 92 |
|
| 93 |
// Insert after page title or at the top of the form |
| 94 |
if ($('#wpbody-content .wrap h1').length) { |
| 95 |
$notice.insertAfter('#wpbody-content .wrap h1'); |
| 96 |
} else { |
| 97 |
$('#post').prepend($notice); |
| 98 |
} |
| 99 |
|
| 100 |
// Scroll to the error |
| 101 |
$('html, body').animate({ |
| 102 |
scrollTop: $notice.offset().top - 100 |
| 103 |
}, 500); |
| 104 |
|
| 105 |
// Handle dismiss button |
| 106 |
$notice.on('click', '.notice-dismiss', function() { |
| 107 |
$notice.fadeOut(300, function() { |
| 108 |
$(this).remove(); |
| 109 |
}); |
| 110 |
}); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Intercept form submission |
| 115 |
*/ |
| 116 |
function interceptFormSubmission() { |
| 117 |
$('#post').on('submit', function(e) { |
| 118 |
const $form = $(this); |
| 119 |
const snippetType = $('#wbcr_inp_snippet_type').val(); |
| 120 |
const snippetScope = $('input[name="wbcr_inp_snippet_scope"]:checked').val(); |
| 121 |
|
| 122 |
// Only validate PHP and Universal snippets with evrywhere or auto scope |
| 123 |
const needsValidation = (snippetType === 'php' || snippetType === 'universal') && |
| 124 |
(snippetScope === 'evrywhere' || snippetScope === 'auto'); |
| 125 |
|
| 126 |
if (!needsValidation) { |
| 127 |
return true; // Allow form submission |
| 128 |
} |
| 129 |
|
| 130 |
// If validation already passed, allow submission |
| 131 |
if (validationPassed) { |
| 132 |
validationPassed = false; // Reset for next time |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
// If validation in progress, prevent submission |
| 137 |
if (validationInProgress) { |
| 138 |
e.preventDefault(); |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
// Prevent form submission and validate |
| 143 |
e.preventDefault(); |
| 144 |
validationInProgress = true; |
| 145 |
|
| 146 |
validateSnippetCode(function(isValid) { |
| 147 |
validationInProgress = false; |
| 148 |
|
| 149 |
if (isValid) { |
| 150 |
validationPassed = true; |
| 151 |
// Remove any error notices |
| 152 |
$('.winp-validation-error').remove(); |
| 153 |
// Submit the form |
| 154 |
$form.off('submit').submit(); |
| 155 |
} |
| 156 |
// If not valid, error is already shown, just stop here |
| 157 |
}); |
| 158 |
|
| 159 |
return false; |
| 160 |
}); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Initialize validation on document ready |
| 165 |
*/ |
| 166 |
$(document).ready(function() { |
| 167 |
// Check if we're on the right page |
| 168 |
const postType = $('#post_type').val(); |
| 169 |
|
| 170 |
// Only initialize on snippet edit pages |
| 171 |
if (typeof wbcrInpValidation !== 'undefined' && postType === wbcrInpValidation.postType) { |
| 172 |
interceptFormSubmission(); |
| 173 |
} else { |
| 174 |
console.warn('Snippet Validation: Not initializing - wrong page type or config not loaded'); |
| 175 |
} |
| 176 |
}); |
| 177 |
|
| 178 |
})(jQuery); |
| 179 |
|