@@ -43,9 +43,10 @@ public function text_field( $atts ) {
4343 $ atts ,
4444 'text '
4545 );
46-
47- $ atts ['name ' ] = sanitize_text_field ( esc_attr ( esc_html__ ($ atts ['name ' ]) ) );
4846
47+ // sanitize name attribute before using it.
48+ $ atts ['name ' ] = $ this ->sanitize_and_escape ( $ atts ['name ' ] );
49+
4950 // translators: %s: input field name to be entered by the user
5051 $ name = sanitize_text_field ( sprintf ( esc_attr__ ( 'Enter %s ' , 'pff-paystack ' ), $ atts ['name ' ] ) );
5152 $ required = $ atts ['required ' ] === 'required ' ? 'required ' : '' ;
@@ -71,6 +72,7 @@ public function text_field( $atts ) {
7172 * @return string
7273 */
7374 public function textarea_field ( $ atts ) {
75+
7476 $ atts = shortcode_atts (
7577 array (
7678 'name ' => esc_html__ ( 'Title ' , 'pff-paystack ' ),
@@ -79,8 +81,8 @@ public function textarea_field( $atts ) {
7981 $ atts ,
8082 'textarea '
8183 );
82-
83- $ atts ['name ' ] = sanitize_text_field ( esc_attr ( esc_html__ ( $ atts ['name ' ]) ) );
84+ // sanitize name attribute before using it
85+ $ atts ['name ' ] = $ this -> sanitize_and_escape ( $ atts ['name ' ] );
8486
8587 // translators: %s: textarea field to be entered by the user
8688 $ name = sanitize_text_field ( sprintf ( esc_attr__ ( 'Enter %s ' , 'pff-paystack ' ), $ atts ['name ' ] ) );
@@ -160,10 +162,10 @@ public function input_field( $atts ) {
160162 $ atts ,
161163 'input '
162164 );
163-
164- $ atts ['name ' ] = sanitize_text_field ( esc_attr ( esc_html__ ($ atts ['name ' ]) ) );
165-
165+
166+ $ atts ['name ' ] = $ this ->sanitize_and_escape ( $ atts ['name ' ] );
166167 $ name = sanitize_text_field ( $ atts ['name ' ] );
168+
167169 $ required = $ atts ['required ' ] === 'required ' ? 'required ' : '' ;
168170 $ fileInputId = uniqid ( 'file-input- ' );
169171 $ textInputId = uniqid ( 'text-input- ' );
@@ -201,9 +203,10 @@ public function datepicker_field( $atts ) {
201203 $ atts ,
202204 'datepicker '
203205 );
204-
205- $ atts ['name ' ] = sanitize_text_field ( esc_attr ( esc_html__ ($ atts ['name ' ]) ) );
206206
207+ // sanitize name attribute before using it
208+ $ atts ['name ' ] = $ this ->sanitize_and_escape ( $ atts ['name ' ] );
209+
207210 // translators: %s: datepicker field to be selected by the user
208211 $ name = sanitize_text_field ( sprintf ( esc_attr__ ( 'Enter %s ' , 'pff-paystack ' ), $ atts ['name ' ] ) );
209212 $ required = $ atts ['required ' ] === 'required ' ? 'required ' : '' ;
@@ -238,7 +241,6 @@ public function select_field( $atts ) {
238241 $ atts ,
239242 'select '
240243 );
241- $ atts ['name ' ] = sanitize_text_field ( esc_attr ( esc_html__ ($ atts ['name ' ]) ) );
242244
243245 $ name = sanitize_text_field ( $ atts ['name ' ] );
244246 $ options = array_map ( 'sanitize_text_field ' , explode ( ', ' , $ atts ['options ' ] ) );
@@ -280,9 +282,7 @@ public function radio_field( $atts ) {
280282 $ atts ,
281283 'radio '
282284 );
283-
284- $ atts ['name ' ] = sanitize_text_field ( esc_attr ( esc_html__ ($ atts ['name ' ]) ) );
285-
285+
286286 $ name = sanitize_text_field ( $ atts ['name ' ] );
287287 $ options = array_map ( 'sanitize_text_field ' , explode ( ', ' , $ atts ['options ' ] ) );
288288 $ required = $ atts ['required ' ] === 'required ' ? 'required ' : '' ;
@@ -311,4 +311,24 @@ public function radio_field( $atts ) {
311311
312312 return $ code ;
313313 }
314- }
314+
315+ /**
316+ * Sanitize and escape a string for safe HTML output.
317+ *
318+ * @param string $value The input string to sanitize and escape.
319+ * @return string The sanitized and escaped string.
320+ */
321+ private function sanitize_and_escape ( $ value ) {
322+ // Remove all HTML tags, including malformed ones
323+ $ value = wp_kses ( $ value , array () );
324+
325+ // Replace backticks with single quotes
326+ $ value = str_replace ( '` ' , '` ' , $ value );
327+
328+ // Sanitize the string for safe database storage
329+ $ value = sanitize_text_field ( $ value );
330+
331+ // Escape the string for safe HTML output
332+ return esc_html ( $ value );
333+ }
334+ }
0 commit comments