/** * Contact Forms - Settings Page JavaScript * * Handles the "Restore to Default" functionality for message sections * in the default settings page. * * @package ContactForms * @since 2.0.0-beta.6 */ (function ($) { 'use strict'; /** * Initialize restore default buttons */ function initRestoreButtons() { $('.accua-restore-default-btn').on('click', function (e) { e.preventDefault(); var $button = $(this); var messageType = $button.data('message-type'); // Show confirmation dialog if (!confirm(accuaFormsSettings.i18n.confirmRestore)) { return; } // Disable button and show loading state var originalText = $button.text(); $button .prop('disabled', true) .addClass('accua-restoring') .text(accuaFormsSettings.i18n.restoring); // Make AJAX request $.ajax({ url: accuaFormsSettings.ajaxUrl, type: 'POST', data: { action: 'accua_forms_restore_default_message', nonce: accuaFormsSettings.nonce, message_type: messageType }, success: function (response) { if (response.success) { // Update the form fields with restored values updateFields(messageType, response.data.values); // Show success state $button .removeClass('accua-restoring') .addClass('accua-restored') .text(accuaFormsSettings.i18n.restored); // Reset button after 2 seconds setTimeout(function () { $button .prop('disabled', false) .removeClass('accua-restored') .text(originalText); }, 2000); } else { showError($button, originalText, response.data ? response.data.message : accuaFormsSettings.i18n.error); } }, error: function () { showError($button, originalText, accuaFormsSettings.i18n.error); } }); }); } /** * Show error state on button * * @param {jQuery} $button The button element * @param {string} originalText Original button HTML * @param {string} errorMessage Error message to log */ function showError($button, originalText, errorMessage) { console.error('Restore default error:', errorMessage); $button .removeClass('accua-restoring') .addClass('accua-error') .text(accuaFormsSettings.i18n.error); // Reset button after 3 seconds setTimeout(function () { $button .prop('disabled', false) .removeClass('accua-error') .text(originalText); }, 3000); } /** * Update form fields with restored values * * @param {string} messageType The type of message being restored * @param {Object} values The restored values object */ function updateFields(messageType, values) { switch (messageType) { case 'success_message': updateTinyMCE('success_message', values.success_message); break; case 'error_message': updateTinyMCE('error_message', values.error_message); break; case 'admin_emails': // Update subject field $('input[name="admin_emails_subject"]').val(values.admin_emails_subject); // Update message editor updateTinyMCE('admin_emails_message', values.admin_emails_message); break; case 'confirmation_emails': // Update subject field $('input[name="confirmation_emails_subject"]').val(values.confirmation_emails_subject); // Update message editor updateTinyMCE('confirmation_emails_message', values.confirmation_emails_message); break; } } /** * Update TinyMCE editor content * * Handles both visual (TinyMCE) and text mode editors. * * @param {string} editorId The ID of the editor * @param {string} content The new content */ function updateTinyMCE(editorId, content) { // Try to get TinyMCE editor instance if (typeof tinymce !== 'undefined') { var editor = tinymce.get(editorId); if (editor && !editor.isHidden()) { // Visual mode - update TinyMCE editor.setContent(content); editor.fire('change'); return; } } // Fallback to textarea (text mode or no TinyMCE) var $textarea = $('#' + editorId); if ($textarea.length) { $textarea.val(content).trigger('change'); } } /** * Initialize Danger Zone - Bulk Anonymize (two-step: preview then confirm) */ function initDangerZoneAnonymize() { $('#accua_danger_anon_btn').on('click', function () { var value = parseInt($('#accua_danger_anon_value').val(), 10); var unit = $('#accua_danger_anon_unit').val(); if (!value || value < 1) { alert(accuaFormsSettings.i18n.error); return; } var $btn = $(this); var $result = $('#accua_danger_anon_result'); var originalText = $btn.text(); // Step 1: fetch preview $btn.prop('disabled', true).text(accuaFormsSettings.i18n.loading || 'Loading...'); $result.hide(); $.ajax({ url: accuaFormsSettings.ajaxUrl, type: 'POST', data: { action: 'accua_forms_bulk_anonymize_preview', nonce: accuaFormsSettings.dangerNonce, value: value, unit: unit }, success: function (response) { $btn.prop('disabled', false).text(originalText); if (!response.success) { $result.html('
' + (response.data ? response.data.message : accuaFormsSettings.i18n.error) + '
' + accuaFormsSettings.i18n.noSubmissionsFound + '
' + accuaFormsSettings.i18n.previewHeading.replace('%d', data.total) + '
'; html += '| ' + accuaFormsSettings.i18n.formColumn + ' | ' + accuaFormsSettings.i18n.submissionsColumn + ' |
|---|---|
| ' + $('').text(f.title).html() + ' (#' + f.id + ') | ' + f.count + ' |
| ' + accuaFormsSettings.i18n.totalLabel + ' | ' + data.total + ' |
'; html += '
'; html += '' + resp.data.message + '
' + (resp.data ? resp.data.message : accuaFormsSettings.i18n.error) + '
' + accuaFormsSettings.i18n.error + '
' + accuaFormsSettings.i18n.error + '
' + response.data.message + '
' + (response.data ? response.data.message : accuaFormsSettings.i18n.error) + '
' + accuaFormsSettings.i18n.error + '