@@ -102,6 +102,54 @@ public function get_option( $option, $default_value = '' ) {
102102 return $ default_value ;
103103 }
104104
105+ /**
106+ * Get field value from args or options.
107+ *
108+ * @param array $args Field arguments.
109+ * @return mixed Field value.
110+ */
111+ protected function get_field_value ( $ args ) {
112+ return $ args ['value ' ] ?? $ this ->get_option ( $ args ['id ' ], $ args ['default ' ] ?? '' );
113+ }
114+
115+ /**
116+ * Get sanitized field class string.
117+ *
118+ * @param array $args Field arguments.
119+ * @param string $default_class Default class to prepend.
120+ * @return string Sanitized class string.
121+ */
122+ protected function get_field_class ( $ args , $ default_class = '' ) {
123+ $ class = implode ( ' ' , array_map ( 'sanitize_html_class ' , explode ( ' ' , $ args ['field_class ' ] ?? '' ) ) );
124+ if ( $ default_class ) {
125+ $ class = $ default_class . ' ' . $ class ;
126+ }
127+ return trim ( $ class );
128+ }
129+
130+ /**
131+ * Get placeholder attribute string.
132+ *
133+ * @param array $args Field arguments.
134+ * @return string Placeholder attribute or empty string.
135+ */
136+ protected function get_placeholder_attribute ( $ args ) {
137+ return empty ( $ args ['placeholder ' ] ) ? '' : ' placeholder=" ' . esc_attr ( $ args ['placeholder ' ] ) . '" ' ;
138+ }
139+
140+ /**
141+ * Get boolean state attributes (disabled, readonly, required).
142+ *
143+ * @param array $args Field arguments.
144+ * @return string Concatenated boolean attributes.
145+ */
146+ protected function get_boolean_attributes ( $ args ) {
147+ $ disabled = ( ! empty ( $ args ['disabled ' ] ) || ! empty ( $ args ['pro ' ] ) ) ? ' disabled="disabled" ' : '' ;
148+ $ readonly = ( isset ( $ args ['readonly ' ] ) && true === $ args ['readonly ' ] ) ? ' readonly="readonly" ' : '' ;
149+ $ required = ( isset ( $ args ['required ' ] ) && true === $ args ['required ' ] ) ? ' required ' : '' ;
150+ return $ disabled . $ readonly . $ required ;
151+ }
152+
105153 /**
106154 * Get field ID and name attributes.
107155 *
@@ -281,24 +329,39 @@ public function callback_descriptive_text( $args ) {
281329 $ this ->callback_header ( $ args );
282330 }
283331
332+ /**
333+ * Build additional attributes string from field_attributes.
334+ *
335+ * @param array $args Field arguments.
336+ * @return string Additional attributes string.
337+ */
338+ protected function build_field_attributes ( $ args ) {
339+ $ attributes = '' ;
340+ foreach ( (array ) ( $ args ['field_attributes ' ] ?? array () ) as $ attribute => $ val ) {
341+ $ attr_name = sanitize_key ( $ attribute );
342+ if ( empty ( $ attr_name ) ) {
343+ continue ;
344+ }
345+ if ( true === $ val ) {
346+ $ attributes .= ' ' . $ attr_name ;
347+ } elseif ( false !== $ val ) {
348+ $ attributes .= sprintf ( ' %1$s="%2$s" ' , $ attr_name , esc_attr ( $ val ) );
349+ }
350+ }
351+ return $ attributes ;
352+ }
353+
284354 /**
285355 * Display text fields.
286356 *
287357 * @param array $args Array of arguments.
288358 */
289359 public function callback_text ( $ args ) {
290- $ value = $ args [ ' value ' ] ?? $ this ->get_option ( $ args[ ' id ' ], $ args [ ' default ' ] );
360+ $ value = $ this ->get_field_value ( $ args );
291361 $ size = sanitize_html_class ( $ args ['size ' ] ?? 'regular ' );
292- $ class = sanitize_html_class ( $ args ['field_class ' ] );
293- $ placeholder = empty ( $ args ['placeholder ' ] ) ? '' : ' placeholder=" ' . esc_attr ( $ args ['placeholder ' ] ) . '" ' ;
294- $ disabled = ( ! empty ( $ args ['disabled ' ] ) || $ args ['pro ' ] ) ? ' disabled="disabled" ' : '' ;
295- $ readonly = ( isset ( $ args ['readonly ' ] ) && true === $ args ['readonly ' ] ) ? ' readonly="readonly" ' : '' ;
296- $ required = ( isset ( $ args ['required ' ] ) && true === $ args ['required ' ] ) ? ' required ' : '' ;
297- $ attributes = $ disabled . $ readonly . $ required ;
298-
299- foreach ( (array ) $ args ['field_attributes ' ] as $ attribute => $ val ) {
300- $ attributes .= sprintf ( ' %1$s="%2$s" ' , $ attribute , esc_attr ( $ val ) );
301- }
362+ $ class = $ this ->get_field_class ( $ args );
363+ $ placeholder = $ this ->get_placeholder_attribute ( $ args );
364+ $ attributes = $ this ->get_boolean_attributes ( $ args ) . $ this ->build_field_attributes ( $ args );
302365
303366 $ field_attributes = $ this ->get_field_attributes ( $ args );
304367
@@ -342,7 +405,7 @@ public function callback_csv( $args ) {
342405 */
343406 public function callback_color ( $ args ) {
344407 // Add color-field class for wpColorPicker initialization.
345- $ args ['field_class ' ] = isset ( $ args ['field_class ' ] ) ? $ args ['field_class ' ] . ' color-field ' : 'color-field ' ;
408+ $ args ['field_class ' ] = ! empty ( $ args ['field_class ' ] ) ? $ args ['field_class ' ] . ' color-field ' : 'color-field ' ;
346409 $ this ->callback_text ( $ args );
347410 }
348411
@@ -371,14 +434,10 @@ public function callback_postids( $args ) {
371434 * @return void
372435 */
373436 public function callback_textarea ( $ args ) {
374-
375- $ value = $ args ['value ' ] ?? $ this ->get_option ( $ args ['id ' ], $ args ['default ' ] );
376- $ class = sanitize_html_class ( $ args ['field_class ' ] );
377- $ placeholder = empty ( $ args ['placeholder ' ] ) ? '' : ' placeholder=" ' . esc_attr ( $ args ['placeholder ' ] ) . '" ' ;
378- $ disabled = ( ! empty ( $ args ['disabled ' ] ) || $ args ['pro ' ] ) ? ' disabled="disabled" ' : '' ;
379- $ readonly = ( isset ( $ args ['readonly ' ] ) && true === $ args ['readonly ' ] ) ? ' readonly="readonly" ' : '' ;
380- $ required = ( isset ( $ args ['required ' ] ) && true === $ args ['required ' ] ) ? ' required ' : '' ;
381- $ attributes = $ disabled . $ readonly . $ required ;
437+ $ value = $ this ->get_field_value ( $ args );
438+ $ class = $ this ->get_field_class ( $ args , 'large-text ' );
439+ $ placeholder = $ this ->get_placeholder_attribute ( $ args );
440+ $ attributes = $ this ->get_boolean_attributes ( $ args );
382441
383442 $ field_attributes = $ this ->get_field_attributes ( $ args );
384443
@@ -387,7 +446,7 @@ public function callback_textarea( $args ) {
387446 $ field_attributes ['field_id ' ],
388447 $ field_attributes ['field_name ' ],
389448 esc_textarea ( stripslashes ( $ value ) ),
390- ' large-text ' . $ class ,
449+ $ class ,
391450 $ attributes ,
392451 $ placeholder
393452 );
@@ -417,18 +476,27 @@ public function callback_html( $args ) {
417476 $ this ->callback_textarea ( $ args );
418477 }
419478
479+ /**
480+ * Get disabled attribute for pro/premium fields.
481+ *
482+ * @param array $args Field arguments.
483+ * @return string Disabled attribute or empty string.
484+ */
485+ protected function get_disabled_attribute ( $ args ) {
486+ return ( ! empty ( $ args ['disabled ' ] ) || ! empty ( $ args ['pro ' ] ) ) ? ' disabled="disabled" ' : '' ;
487+ }
488+
420489 /**
421490 * Display checkboxes.
422491 *
423492 * @param array $args Array of arguments.
424493 * @return void
425494 */
426495 public function callback_checkbox ( $ args ) {
427-
428- $ value = $ args ['value ' ] ?? $ this ->get_option ( $ args ['id ' ], $ args ['default ' ] );
496+ $ value = $ this ->get_field_value ( $ args );
429497 $ checked = ! empty ( $ value ) ? checked ( 1 , $ value , false ) : '' ;
430498 $ default = isset ( $ args ['default ' ] ) ? (int ) $ args ['default ' ] : '' ;
431- $ disabled = ( ! empty ( $ args[ ' disabled ' ] ) || $ args [ ' pro ' ] ) ? ' disabled="disabled" ' : '' ;
499+ $ disabled = $ this -> get_disabled_attribute ( $ args ) ;
432500
433501 $ field_attributes = $ this ->get_field_attributes ( $ args );
434502
@@ -462,9 +530,9 @@ public function callback_checkbox( $args ) {
462530 public function callback_multicheck ( $ args ) {
463531 $ html = '' ;
464532
465- $ value = $ args [ ' value ' ] ?? $ this ->get_option ( $ args[ ' id ' ], $ args [ ' default ' ] );
533+ $ value = $ this ->get_field_value ( $ args );
466534 $ value_array = wp_parse_list ( $ value );
467- $ disabled = ( ! empty ( $ args[ ' disabled ' ] ) || $ args [ ' pro ' ] ) ? ' disabled="disabled" ' : '' ;
535+ $ disabled = $ this -> get_disabled_attribute ( $ args ) ;
468536
469537 $ field_attributes = $ this ->get_field_attributes ( $ args );
470538
@@ -516,8 +584,8 @@ public function callback_multicheck( $args ) {
516584 public function callback_radio ( $ args ) {
517585 $ html = '' ;
518586
519- $ value = $ args [ ' value ' ] ?? $ this ->get_option ( $ args[ ' id ' ], $ args [ ' default ' ] );
520- $ disabled = ( ! empty ( $ args[ ' disabled ' ] ) || $ args [ ' pro ' ] ) ? ' disabled="disabled" ' : '' ;
587+ $ value = $ this ->get_field_value ( $ args );
588+ $ disabled = $ this -> get_disabled_attribute ( $ args ) ;
521589
522590 $ field_attributes = $ this ->get_field_attributes ( $ args );
523591
@@ -556,8 +624,8 @@ public function callback_radio( $args ) {
556624 public function callback_radiodesc ( $ args ) {
557625 $ html = '' ;
558626
559- $ value = $ args [ ' value ' ] ?? $ this ->get_option ( $ args[ ' id ' ], $ args [ ' default ' ] );
560- $ disabled = ( ! empty ( $ args[ ' disabled ' ] ) || $ args [ ' pro ' ] ) ? ' disabled="disabled" ' : '' ;
627+ $ value = $ this ->get_field_value ( $ args );
628+ $ disabled = $ this -> get_disabled_attribute ( $ args ) ;
561629
562630 $ field_attributes = $ this ->get_field_attributes ( $ args );
563631
@@ -610,7 +678,7 @@ public function callback_thumbsizes( $args ) {
610678 );
611679 }
612680
613- $ value = $ args [ ' value ' ] ?? $ this ->get_option ( $ args[ ' id ' ], $ args [ ' default ' ] );
681+ $ value = $ this ->get_field_value ( $ args );
614682
615683 $ field_attributes = $ this ->get_field_attributes ( $ args );
616684
@@ -649,16 +717,13 @@ public function callback_thumbsizes( $args ) {
649717 * @return void
650718 */
651719 public function callback_number ( $ args ) {
652- $ value = $ args [ ' value ' ] ?? $ this ->get_option ( $ args[ ' id ' ], $ args [ ' default ' ] );
720+ $ value = $ this ->get_field_value ( $ args );
653721 $ max = isset ( $ args ['max ' ] ) ? intval ( $ args ['max ' ] ) : 999999 ;
654722 $ min = isset ( $ args ['min ' ] ) ? intval ( $ args ['min ' ] ) : 0 ;
655723 $ step = isset ( $ args ['step ' ] ) ? intval ( $ args ['step ' ] ) : 1 ;
656724 $ size = $ args ['size ' ] ?? 'regular ' ;
657- $ placeholder = empty ( $ args ['placeholder ' ] ) ? '' : ' placeholder=" ' . esc_attr ( $ args ['placeholder ' ] ) . '" ' ;
658- $ disabled = ( ! empty ( $ args ['disabled ' ] ) || $ args ['pro ' ] ) ? ' disabled="disabled" ' : '' ;
659- $ readonly = ( isset ( $ args ['readonly ' ] ) && true === $ args ['readonly ' ] ) ? ' readonly="readonly" ' : '' ;
660- $ required = ( isset ( $ args ['required ' ] ) && true === $ args ['required ' ] ) ? ' required ' : '' ;
661- $ attributes = $ disabled . $ readonly . $ required ;
725+ $ placeholder = $ this ->get_placeholder_attribute ( $ args );
726+ $ attributes = $ this ->get_boolean_attributes ( $ args );
662727
663728 $ field_attributes = $ this ->get_field_attributes ( $ args );
664729
@@ -689,15 +754,10 @@ public function callback_number( $args ) {
689754 * @return void
690755 */
691756 public function callback_select ( $ args ) {
692- $ value = $ args ['value ' ] ?? $ this ->get_option ( $ args ['id ' ], $ args ['default ' ] );
693- $ class = sanitize_html_class ( $ args ['field_class ' ] );
694- $ disabled = ( ! empty ( $ args ['disabled ' ] ) || $ args ['pro ' ] ) ? ' disabled="disabled" ' : '' ;
757+ $ value = $ this ->get_field_value ( $ args );
758+ $ class = $ this ->get_field_class ( $ args );
695759 $ required = ( isset ( $ args ['required ' ] ) && true === $ args ['required ' ] ) ? ' required ' : '' ;
696- $ attributes = $ disabled . $ required ;
697-
698- foreach ( (array ) $ args ['field_attributes ' ] as $ attribute => $ val ) {
699- $ attributes .= sprintf ( ' %1$s="%2$s" ' , $ attribute , esc_attr ( $ val ) );
700- }
760+ $ attributes = $ this ->get_disabled_attribute ( $ args ) . $ required . $ this ->build_field_attributes ( $ args );
701761
702762 if ( isset ( $ args ['chosen ' ] ) ) {
703763 $ class .= ' chosen ' ;
@@ -733,8 +793,8 @@ public function callback_select( $args ) {
733793 public function callback_posttypes ( $ args ) {
734794 $ html = '' ;
735795
736- $ options = $ args [ ' value ' ] ?? $ this ->get_option ( $ args[ ' id ' ], $ args [ ' default ' ] );
737- $ disabled = ( ! empty ( $ args[ ' disabled ' ] ) || $ args [ ' pro ' ] ) ? ' disabled="disabled" ' : '' ;
796+ $ options = $ this ->get_field_value ( $ args );
797+ $ disabled = $ this -> get_disabled_attribute ( $ args ) ;
738798
739799 // If post_types contains a query string then parse it with wp_parse_args.
740800 if ( is_string ( $ options ) && strpos ( $ options , '= ' ) ) {
@@ -791,7 +851,7 @@ public function callback_posttypes( $args ) {
791851 public function callback_taxonomies ( $ args ) {
792852 $ html = '' ;
793853
794- $ options = $ args [ ' value ' ] ?? $ this ->get_option ( $ args[ ' id ' ], $ args [ ' default ' ] );
854+ $ options = $ this ->get_field_value ( $ args );
795855
796856 // If taxonomies contains a query string then parse it with wp_parse_args.
797857 if ( is_string ( $ options ) && strpos ( $ options , '= ' ) ) {
@@ -842,8 +902,7 @@ public function callback_taxonomies( $args ) {
842902 * @param array $args Array of arguments.
843903 */
844904 public function callback_wysiwyg ( $ args ) {
845-
846- $ value = $ args ['value ' ] ?? $ this ->get_option ( $ args ['id ' ], $ args ['default ' ] );
905+ $ value = $ this ->get_field_value ( $ args );
847906 $ size = $ args ['size ' ] ?? '500px ' ;
848907
849908 $ field_attributes = $ this ->get_field_attributes ( $ args );
@@ -876,10 +935,9 @@ public function callback_wysiwyg( $args ) {
876935 * @param array $args Array of arguments.
877936 */
878937 public function callback_file ( $ args ) {
879-
880- $ value = $ args ['value ' ] ?? $ this ->get_option ( $ args ['id ' ], $ args ['default ' ] );
938+ $ value = $ this ->get_field_value ( $ args );
881939 $ size = sanitize_html_class ( $ args ['size ' ] ?? 'regular ' );
882- $ class = sanitize_html_class ( $ args[ ' field_class ' ] );
940+ $ class = $ this -> get_field_class ( $ args );
883941 $ label = $ args ['options ' ]['button_label ' ] ?? $ this ->translation_strings ['button_label ' ];
884942
885943 $ field_attributes = $ this ->get_field_attributes ( $ args );
@@ -904,10 +962,9 @@ public function callback_file( $args ) {
904962 * @param array $args Array of arguments.
905963 */
906964 public function callback_password ( $ args ) {
907-
908- $ value = $ args ['value ' ] ?? $ this ->get_option ( $ args ['id ' ], $ args ['default ' ] );
965+ $ value = $ this ->get_field_value ( $ args );
909966 $ size = sanitize_html_class ( $ args ['size ' ] ?? 'regular ' );
910- $ class = sanitize_html_class ( $ args[ ' field_class ' ] );
967+ $ class = $ this -> get_field_class ( $ args );
911968
912969 $ field_attributes = $ this ->get_field_attributes ( $ args );
913970
@@ -932,18 +989,11 @@ public function callback_password( $args ) {
932989 * @return void
933990 */
934991 public function callback_repeater ( $ args ) {
935- $ value = isset ( $ args ['value ' ] ) ? (array ) $ args ['value ' ] : $ this ->get_option ( $ args ['id ' ], array () );
936- $ value = ! empty ( $ value ) && is_array ( $ value ) ? $ value : array ();
937-
938- $ class = ! empty ( $ args ['field_class ' ] ) ? sanitize_html_class ( $ args ['field_class ' ] ) : '' ;
939- $ disabled = ( ! empty ( $ args ['disabled ' ] ) || ! empty ( $ args ['pro ' ] ) ) ? ' disabled="disabled" ' : '' ;
940- $ readonly = ( isset ( $ args ['readonly ' ] ) && true === $ args ['readonly ' ] ) ? ' readonly="readonly" ' : '' ;
941- $ attributes = $ disabled . $ readonly ;
992+ $ raw_value = $ this ->get_field_value ( $ args );
993+ $ value = ! empty ( $ raw_value ) && is_array ( $ raw_value ) ? $ raw_value : array ();
942994
943- // Process additional field attributes.
944- foreach ( (array ) $ args ['field_attributes ' ] as $ attribute => $ val ) {
945- $ attributes .= sprintf ( ' %1$s="%2$s" ' , sanitize_key ( $ attribute ), esc_attr ( $ val ) );
946- }
995+ $ class = $ this ->get_field_class ( $ args );
996+ $ attributes = $ this ->get_boolean_attributes ( $ args ) . $ this ->build_field_attributes ( $ args );
947997
948998 $ data_index = (string ) count ( $ value );
949999 $ live_update_field = ! empty ( $ args ['live_update_field ' ] ) ? $ args ['live_update_field ' ] : 'name ' ;
@@ -1093,7 +1143,7 @@ public function render_repeater_item( $args, $index, $item = null ) {
10931143 * @param array $args Array of arguments.
10941144 */
10951145 public function callback_sensitive ( $ args ) {
1096- $ encrypted_key = $ args [ ' value ' ] ?? $ this ->get_option ( $ args[ ' id ' ], $ args [ ' default ' ] );
1146+ $ encrypted_key = $ this ->get_field_value ( $ args );
10971147 $ decrypted_key = Settings_API::decrypt_api_key ( $ encrypted_key );
10981148
10991149 $ args ['value ' ] = $ decrypted_key ? str_repeat ( '* ' , strlen ( $ decrypted_key ) - 4 ) . substr ( $ decrypted_key , -4 ) : '' ;
0 commit comments