| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
function accua_form_Load($class) { |
| 5 |
if (substr($class,0,10) === 'AccuaForm_') { |
| 6 |
$class = substr($class, 10); |
| 7 |
$file = ACCUA_FORMS_DIR . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . str_replace("_", DIRECTORY_SEPARATOR, $class) . ".php"; |
| 8 |
if(is_file($file)) { |
| 9 |
include_once $file; |
| 10 |
} |
| 11 |
} |
| 12 |
} |
| 13 |
spl_autoload_register("accua_form_Load"); |
| 14 |
|
| 15 |
require_once('PFBC/Form.php'); |
| 16 |
require_once('AccuaForm.php'); |
| 17 |
|
| 18 |
define('ACCUA_FORM_API_PLUGIN_TEXTDOMAIN_PATH', dirname( plugin_basename( __FILE__ ) ) . '/languages/'); |
| 19 |
|
| 20 |
add_action('wp_enqueue_scripts', 'accua_form_enqueue_scripts_and_styles'); |
| 21 |
add_action('admin_enqueue_scripts', 'accua_form_enqueue_scripts_and_styles'); |
| 22 |
add_action('login_enqueue_scripts', 'accua_form_enqueue_scripts_and_styles'); |
| 23 |
function accua_form_enqueue_scripts_and_styles(){ |
| 24 |
static $first = TRUE; |
| 25 |
if ($first) { |
| 26 |
wp_enqueue_script('jquery'); |
| 27 |
|
| 28 |
// In admin context, use WordPress's wp-color-picker (has all dependencies available) |
| 29 |
if (is_admin()) { |
| 30 |
wp_enqueue_style('wp-color-picker'); |
| 31 |
wp_enqueue_script('wp-color-picker'); |
| 32 |
} |
| 33 |
// On frontend, we use HTML5 native color input (no JS dependencies needed) |
| 34 |
// This is WCAG 2.2 AA compliant, keyboard accessible, and works in all modern browsers |
| 35 |
|
| 36 |
wp_enqueue_style('accua-forms-api-base', plugins_url('assets/css/frontend.css', ACCUA_FORMS_FILE), array(), ACCUA_FORMS_CSS_VERSION); |
| 37 |
|
| 38 |
// Accessible file upload with drag & drop (WCAG 2.2 AA compliant) |
| 39 |
wp_enqueue_script( |
| 40 |
'accua-file-upload', |
| 41 |
plugins_url('assets/js/frontend/file-upload.js', ACCUA_FORMS_FILE), |
| 42 |
array(), // No dependencies - vanilla JS |
| 43 |
ACCUA_FORMS_JS_VERSION, |
| 44 |
true // Load in footer |
| 45 |
); |
| 46 |
|
| 47 |
// Localized strings for file upload |
| 48 |
wp_localize_script('accua-file-upload', 'accuaFileUploadL10n', array( |
| 49 |
'fileTooLarge' => __('File exceeds maximum allowed size', 'contact-forms'), |
| 50 |
'invalidType' => __('File type not allowed', 'contact-forms'), |
| 51 |
'fileAdded' => __('File added', 'contact-forms'), |
| 52 |
'filesAdded' => __('files added', 'contact-forms'), |
| 53 |
'fileRemoved' => __('File removed', 'contact-forms'), |
| 54 |
'errors' => __('Errors', 'contact-forms'), |
| 55 |
'removeFile' => __('Remove', 'contact-forms'), |
| 56 |
)); |
| 57 |
|
| 58 |
// libphonenumber-js for phone validation (min bundle, ~175KB) |
| 59 |
wp_enqueue_script( |
| 60 |
'libphonenumber', |
| 61 |
plugins_url('assets/js/frontend/libphonenumber-min.js', ACCUA_FORMS_FILE), |
| 62 |
array(), // No dependencies |
| 63 |
'1.12.5', // libphonenumber-js version |
| 64 |
true // Load in footer |
| 65 |
); |
| 66 |
|
| 67 |
// Phone validation for telephone fields (WCAG 2.2 AA compliant) |
| 68 |
// Uses libphonenumber-js for proper international phone validation |
| 69 |
wp_enqueue_script( |
| 70 |
'accua-phone-validation', |
| 71 |
plugins_url('assets/js/frontend/phone-validation.js', ACCUA_FORMS_FILE), |
| 72 |
array('jquery', 'libphonenumber'), |
| 73 |
ACCUA_FORMS_JS_VERSION, |
| 74 |
true // Load in footer |
| 75 |
); |
| 76 |
|
| 77 |
// Localized strings for phone validation |
| 78 |
wp_localize_script('accua-phone-validation', 'accuaPhoneL10n', array( |
| 79 |
'phoneError' => __('Please enter a valid phone number (e.g. +39 333 1234567)', 'contact-forms'), |
| 80 |
)); |
| 81 |
|
| 82 |
// Post select - AJAX searchable dropdown for post-select fields (WCAG 2.2 AA compliant) |
| 83 |
wp_enqueue_script( |
| 84 |
'accua-post-select', |
| 85 |
plugins_url('assets/js/frontend/post-select.js', ACCUA_FORMS_FILE), |
| 86 |
array(), // No dependencies - vanilla JS |
| 87 |
ACCUA_FORMS_JS_VERSION, |
| 88 |
true // Load in footer |
| 89 |
); |
| 90 |
|
| 91 |
// Localized strings for post select |
| 92 |
wp_localize_script('accua-post-select', 'accuaPostSelectI18n', array( |
| 93 |
'select' => __('Select...', 'contact-forms'), |
| 94 |
'search' => __('Search...', 'contact-forms'), |
| 95 |
'loading' => __('Loading...', 'contact-forms'), |
| 96 |
'noResults' => __('No results found', 'contact-forms'), |
| 97 |
'loadMore' => __('Loading more...', 'contact-forms'), |
| 98 |
)); |
| 99 |
|
| 100 |
/** |
| 101 |
* Action fired after Contact Forms enqueues its scripts and styles. |
| 102 |
* External plugins can use this to enqueue their own field-specific assets. |
| 103 |
*/ |
| 104 |
do_action( 'accua_forms_enqueue_scripts' ); |
| 105 |
|
| 106 |
$first = FALSE; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// Runs at init priority 5 so that: |
| 111 |
// 1. Translations are safe to use (after_setup_theme has fired before init). |
| 112 |
// 2. SMTP plugins (WP Mail SMTP etc.) have already registered their phpmailer_init hooks |
| 113 |
// at plugins_loaded priority ≤ 10, so emails sent here still go through SMTP. |
| 114 |
add_action('init', 'accua_form_init', 5); |
| 115 |
function accua_form_init(){ |
| 116 |
if (!defined('DOING_CRON')) { |
| 117 |
AccuaForm::isValid(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
add_action('wp_ajax_accua_form_submit', 'accua_form_ajax_submit_handler'); |
| 122 |
add_action('wp_ajax_nopriv_accua_form_submit', 'accua_form_ajax_submit_handler'); |
| 123 |
function accua_form_ajax_submit_handler(){ |
| 124 |
header('Content-Type: text/html; charset='.get_option('blog_charset')); |
| 125 |
header('Cache-Control: no-transform'); |
| 126 |
|
| 127 |
echo '<html><head></head><body><pre id="accua-form-ajax-response">'; |
| 128 |
|
| 129 |
$result = AccuaForm::ajaxSubmit(); |
| 130 |
|
| 131 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON encoded data for JavaScript consumption |
| 132 |
print _accua_forms_json_encode($result); |
| 133 |
|
| 134 |
echo '</pre><div id="accua-form-ajax-response-loaded"></div>'; |
| 135 |
|
| 136 |
// phpcs:disable PluginCheck.CodeAnalysis.Heredoc.NotAllowed -- Heredoc for inline JavaScript |
| 137 |
echo <<<EOJS |
| 138 |
<script type="text/javascript"> |
| 139 |
<!-- |
| 140 |
try { |
| 141 |
window.parent.postMessage(document.getElementById("accua-form-ajax-response").innerHTML, '*'); |
| 142 |
} catch (err) { |
| 143 |
} |
| 144 |
// --> |
| 145 |
</script> |
| 146 |
EOJS; |
| 147 |
// phpcs:enable PluginCheck.CodeAnalysis.Heredoc.NotAllowed |
| 148 |
|
| 149 |
echo '</body></html>'; |
| 150 |
die(''); |
| 151 |
} |
| 152 |
|