PluginProbe
Contact Forms by Cimatti / 2.3.5
Contact Forms by Cimatti v2.3.5
2.3.6 2.3.5 2.3.0 2.2.32 2.2.4 2.2.0 2.1.2 2.1.1 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 All 62 releases
contact-forms / classes / Error / Standard.php

Standard.php in Contact Forms by Cimatti 2.3.5, at classes/Error/Standard.php

177 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.Security.EscapeOutput.HeredocOutputNotEscaped, PluginCheck.CodeAnalysis.Heredoc -- PFBC Error class, HTML/JS output is controlled
3 class AccuaForm_Error_Standard extends PFBCError {
4 protected $errorfound;
5 protected $errorsfound;
6
7 public function applyAjaxErrorResponse() {
8 $id = $this->form->getId();
9 //$errorfound = _accua_forms_json_encode($this->errorfound);
10 //$errorsfound = _accua_forms_json_encode($this->errorsfound);
11 echo <<<JS
12 var errorSize = response.errors.length;
13 var errorHTML = '\\x3Cdiv class="pfbc-error ui-state-error ui-corner-all"\\x3E\\x3Cul\\x3E';
14 for(e = 0; e < errorSize; ++e) {
15 errorHTML += '\\x3Cli\\x3E' + response.errors[e] + '\\x3C/li\\x3E';
16 }
17 errorHTML += '\\x3C/ul\\x3E\\x3C/div\\x3E';
18 jQuery("#$id").append(errorHTML);
19
20 /* Apply ARIA attributes and inline errors for accessibility */
21 if (response.elementErrors) {
22 jQuery.each(response.elementErrors, function(fieldName, fieldErrors) {
23 var field = jQuery('[name="' + fieldName + '"]', '#$id');
24 if (!field.length) {
25 /* Elements with no named input in the DOM (e.g. reCAPTCHA v2) expose their name via data-accua-name */
26 field = jQuery('[data-accua-name="' + fieldName + '"]', '#$id');
27 }
28 if (field.length) {
29 var parent = field.closest('.pfbc-element, .pfbc-fieldwrap');
30 var errorId = field.attr('id') + '-error';
31
32 /* Add ARIA attributes */
33 field.attr('aria-invalid', 'true');
34 field.attr('aria-describedby', errorId);
35
36 /* Add error class to parent */
37 parent.addClass('pfbc-element-has-error');
38
39 /* Remove existing inline error if any */
40 parent.find('.pfbc-inline-error').remove();
41
42 /* Add inline error message */
43 var inlineErrorHTML = '<div class="pfbc-inline-error" id="' + errorId + '" role="alert" aria-live="polite">';
44 for (var i = 0; i < fieldErrors.length; i++) {
45 inlineErrorHTML += '<div class="pfbc-error-message">' + fieldErrors[i] + '</div>';
46 }
47 inlineErrorHTML += '</div>';
48
49 /* For file inputs with help text, insert error after help text */
50 var helpText = field.siblings('.pfbc-help').last();
51 if (field.is('[type="file"]') && helpText.length) {
52 helpText.after(inlineErrorHTML);
53 } else {
54 field.after(inlineErrorHTML);
55 }
56 }
57 });
58 }
59 JS;
60
61 }
62
63 public function applyAjaxErrorResponseUsingShowErrorMessages() {
64 echo <<<JS
65 var errorSize = response.errors.length;
66 var errorHTML = '';
67 for(e = 0; e < errorSize; ++e) {
68 errorHTML += '\\x3Cli\\x3E' + response.errors[e] + '\\x3C/li\\x3E';
69 }
70
71 /* Update summary area with server-side errors */
72 if (typeof updateSummaryWithServerErrors === 'function') {
73 updateSummaryWithServerErrors(response.errors, response.elementErrors);
74 }
75
76 /* Apply ARIA attributes and inline errors for accessibility */
77 if (response.elementErrors) {
78 jQuery.each(response.elementErrors, function(fieldName, fieldErrors) {
79 var field = jQuery('[name="' + fieldName + '"]');
80 if (!field.length) {
81 /* Elements with no named input in the DOM (e.g. reCAPTCHA v2) expose their name via data-accua-name */
82 field = jQuery('[data-accua-name="' + fieldName + '"]');
83 }
84 if (field.length) {
85 var parent = field.closest('.pfbc-element, .pfbc-fieldwrap');
86 var errorId = field.attr('id') + '-error';
87
88 /* Add ARIA attributes */
89 field.attr('aria-invalid', 'true');
90 field.attr('aria-describedby', errorId);
91
92 /* Add error class to parent */
93 parent.addClass('pfbc-element-has-error');
94
95 /* Remove existing inline error if any */
96 parent.find('.pfbc-inline-error').remove();
97
98 /* Add inline error message */
99 var inlineErrorHTML = '<div class=\"pfbc-inline-error\" id=\"' + errorId + '\" role=\"alert\" aria-live=\"polite\">';
100 for (var i = 0; i < fieldErrors.length; i++) {
101 inlineErrorHTML += '<div class=\"pfbc-error-message\">' + fieldErrors[i] + '</div>';
102 }
103 inlineErrorHTML += '</div>';
104
105 /* For file inputs with help text, insert error after help text */
106 var helpText = field.siblings('.pfbc-help').last();
107 if (field.is('[type=\"file\"]') && helpText.length) {
108 helpText.after(inlineErrorHTML);
109 } else {
110 field.after(inlineErrorHTML);
111 }
112 }
113 });
114 }
115 JS;
116
117 }
118 public function __construct(?array $properties = null) {
119 $this->errorfound = __('The following error was found:', 'contact-forms');
120 $this->errorsfound = __('The following @errorsize errors were found:', 'contact-forms');
121 $this->configure($properties);
122 }
123
124 public function setErrorsFound($text = '') {
125 $this->errorsfound = $text;
126 }
127
128 public function setErrorFound($text = '') {
129 $this->errorfound = $text;
130 }
131
132 private function parse($errors) {
133 $list = array();
134 if(!empty($errors)) {
135 $keys = array_keys($errors);
136 $keySize = sizeof($keys);
137 for($k = 0; $k < $keySize; ++$k)
138 $list = array_merge($list, $errors[$keys[$k]]);
139 }
140 return $list;
141 }
142
143 public function render() {
144 $errors = $this->parse($this->form->getErrors());
145 if(!empty($errors)) {
146 $size = sizeof($errors);
147 if($size == 1)
148 $format = $this->errorfound;
149 else
150 $format = str_replace('@errorsize', $size, $this->errorsfound);
151
152 echo '<div class="pfbc-error ui-state-error ui-corner-all">', $format, '<ul><li>', implode("</li><li>", $errors), "</li></ul></div>";
153 }
154 }
155
156 public function getAjaxErrorResponse() {
157 $flatErrors = $this->parse($this->form->getErrors());
158 $elementErrors = $this->form->getErrors(); // Structured errors for ARIA attributes
159 return array(
160 'errors' => $flatErrors,
161 'elementErrors' => $elementErrors
162 );
163 }
164
165 public function renderAjaxErrorResponse() {
166 $flatErrors = $this->parse($this->form->getErrors());
167 $elementErrors = $this->form->getErrors(); // Keep structured errors for field-level ARIA
168 if(!empty($flatErrors)) {
169 header("Content-type: application/json");
170 print _accua_forms_json_encode(array(
171 "errors" => $flatErrors,
172 "elementErrors" => $elementErrors // Add field-specific errors for ARIA attributes
173 ));
174 }
175 }
176 }
177