# contact-forms/trunk/assets/js/admin/settings-page.js

Contact Forms by Cimatti, version trunk. 325 lines.

- Page: https://pluginprobe.com/plugins/contact-forms/trunk/code/assets/js/admin/settings-page.js
- Raw: https://pluginprobe.com/plugins/contact-forms/trunk/raw/assets/js/admin/settings-page.js
- Modified: 2026-09-02T14:00:10+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/contact-forms/trunk/code/assets/js/admin/settings-page.js#L10-L20`.

```javascript
/**
 * 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('<div class="notice notice-error inline"><p>' + (response.data ? response.data.message : accuaFormsSettings.i18n.error) + '</p></div>').show();
            return;
          }

          var data = response.data;
          if (data.total === 0) {
            $result.html('<div class="notice notice-info inline"><p>' + accuaFormsSettings.i18n.noSubmissionsFound + '</p></div>').show();
            return;
          }

          // Build preview table
          var html = '<div class="accua-anon-preview">';
          html += '<p><strong>' + accuaFormsSettings.i18n.previewHeading.replace('%d', data.total) + '</strong></p>';
          html += '<table class="widefat striped" style="margin: 8px 0;">';
          html += '<thead><tr><th>' + accuaFormsSettings.i18n.formColumn + '</th><th style="text-align:right;">' + accuaFormsSettings.i18n.submissionsColumn + '</th></tr></thead>';
          html += '<tbody>';
          for (var i = 0; i < data.forms.length; i++) {
            var f = data.forms[i];
            html += '<tr><td>' + $('<span>').text(f.title).html() + ' <small style="color:#888;">(#' + f.id + ')</small></td><td style="text-align:right;">' + f.count + '</td></tr>';
          }
          html += '</tbody>';
          html += '<tfoot><tr><th><strong>' + accuaFormsSettings.i18n.totalLabel + '</strong></th><th style="text-align:right;"><strong>' + data.total + '</strong></th></tr></tfoot>';
          html += '</table>';
          html += '<p><button type="button" id="accua_danger_anon_confirm" class="button" style="color:#d63638;border-color:#d63638;">' + accuaFormsSettings.i18n.confirmAnonymize + '</button> ';
          html += '<button type="button" id="accua_danger_anon_cancel" class="button">' + accuaFormsSettings.i18n.cancel + '</button></p>';
          html += '</div>';

          $result.html(html).show();

          // Cancel
          $('#accua_danger_anon_cancel').on('click', function () {
            $result.hide().empty();
          });

          // Step 2: execute
          $('#accua_danger_anon_confirm').on('click', function () {
            var $confirmBtn = $(this);
            $confirmBtn.prop('disabled', true).text(accuaFormsSettings.i18n.anonymizing);
            $('#accua_danger_anon_cancel').prop('disabled', true);

            $.ajax({
              url: accuaFormsSettings.ajaxUrl,
              type: 'POST',
              data: {
                action: 'accua_forms_bulk_anonymize',
                nonce: accuaFormsSettings.dangerNonce,
                value: value,
                unit: unit
              },
              success: function (resp) {
                if (resp.success) {
                  $result.html('<div class="notice notice-success inline"><p>' + resp.data.message + '</p></div>').show();
                } else {
                  $result.html('<div class="notice notice-error inline"><p>' + (resp.data ? resp.data.message : accuaFormsSettings.i18n.error) + '</p></div>').show();
                }
              },
              error: function () {
                $result.html('<div class="notice notice-error inline"><p>' + accuaFormsSettings.i18n.error + '</p></div>').show();
              }
            });
          });
        },
        error: function () {
          $btn.prop('disabled', false).text(originalText);
          $result.html('<div class="notice notice-error inline"><p>' + accuaFormsSettings.i18n.error + '</p></div>').show();
        }
      });
    });
  }

  /**
   * Initialize Danger Zone - Delete All Data
   */
  function initDangerZoneDelete() {
    $('#accua_danger_delete_btn').on('click', function () {
      var typed = prompt(accuaFormsSettings.i18n.confirmDeletePrompt);

      if (typed === null) {
        return; // Cancelled
      }

      if (typed !== accuaFormsSettings.siteDomain) {
        alert('Domain name does not match. Operation cancelled.');
        return;
      }

      var $btn = $(this);
      var $result = $('#accua_danger_delete_result');
      var originalText = $btn.text();

      $btn.prop('disabled', true).text(accuaFormsSettings.i18n.deleting);
      $result.hide();

      $.ajax({
        url: accuaFormsSettings.ajaxUrl,
        type: 'POST',
        data: {
          action: 'accua_forms_delete_all_data',
          nonce: accuaFormsSettings.dangerNonce,
          confirm_domain: typed
        },
        success: function (response) {
          $btn.prop('disabled', false).text(originalText);
          if (response.success) {
            $result.html('<div class="notice notice-success inline"><p>' + response.data.message + '</p></div>').show();
          } else {
            $result.html('<div class="notice notice-error inline"><p>' + (response.data ? response.data.message : accuaFormsSettings.i18n.error) + '</p></div>').show();
          }
        },
        error: function () {
          $btn.prop('disabled', false).text(originalText);
          $result.html('<div class="notice notice-error inline"><p>' + accuaFormsSettings.i18n.error + '</p></div>').show();
        }
      });
    });
  }

  // Initialize when DOM is ready
  $(document).ready(function () {
    $('#accua_settings_tabs').cfTabs();
    initRestoreButtons();
    initDangerZoneAnonymize();
    initDangerZoneDelete();

    // Initialize color pickers on the Styling tab.
    // No change callback here: triggering a jQuery change on the input
    // re-enters Iris's own input listener before Iris has written the new
    // colour back to the field, so Iris reads the stale value and either
    // reverts the pick or, when the field was empty, clears the swatch.
    if ($.fn.wpColorPicker) {
      $('.accua-color-picker').wpColorPicker();
    }
  });

})(jQuery);

```
