$value ) { if ( false === $value || null === $value || '' === $value ) { continue; } if ( true === $value ) { $out .= ' ' . esc_attr( $name ); continue; } $out .= sprintf( ' %s="%s"', esc_attr( $name ), esc_attr( (string) $value ) ); } return $out; } /** * Common input attributes shared by single-input fields. * * @param array $field Field config. * @param array $ctx Render context. * @return array */ protected function base_attrs( array $field, array $ctx ) { return array( 'id' => $ctx['id'], 'name' => $ctx['name'], 'class' => 'pf-input', 'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '', 'required' => ! empty( $field['required'] ), 'aria-required' => ! empty( $field['required'] ) ? 'true' : false, 'aria-describedby' => $ctx['describedby'], ); } /** * Clean a list of choice options. * * @param mixed $options Raw options. * @return array */ protected function sanitize_options( $options ) { $clean = array(); if ( ! is_array( $options ) ) { return $clean; } foreach ( array_slice( $options, 0, 200 ) as $option ) { if ( ! is_array( $option ) ) { continue; } $label = isset( $option['label'] ) ? sanitize_text_field( (string) $option['label'] ) : ''; $value = isset( $option['value'] ) && '' !== (string) $option['value'] ? sanitize_text_field( (string) $option['value'] ) : $label; if ( '' === $label && '' === $value ) { continue; } $clean[] = array( 'label' => '' === $label ? $value : $label, 'value' => $value, ); } return $clean; } /** * Allowed option values. * * @param array $field Field config. * @return string[] */ protected function option_values( array $field ) { return array_map( static function ( $o ) { return (string) $o['value']; }, isset( $field['options'] ) ? $field['options'] : array() ); } /** * Label for a stored option value. * * @param array $field Field config. * @param string $value Stored value. * @return string */ protected function option_label( array $field, $value ) { foreach ( isset( $field['options'] ) ? $field['options'] : array() as $o ) { if ( (string) $o['value'] === (string) $value ) { return $o['label']; } } return (string) $value; } }