id}` (callers rendering fields from more than one section should pass a section-qualified id to avoid duplicate HTML ids). * @return void */ public static function render( Field $field, mixed $value, string $name_prefix, ?string $id = null ): void { $name = $name_prefix . '[' . $field->id . ']'; $id ??= 'aiou-field-' . $field->id; /** * Allows custom field types to be rendered. * * A callback should echo its markup and return true to mark the * field as handled, short-circuiting the built-in renderers. * * @since 2.0.0 * * @param bool $handled Whether the field has been rendered. * @param Field $field Field definition. * @param mixed $value Current value. * @param string $name Input name attribute. * @param string $id Input id attribute. */ $handled = apply_filters( 'aiou_settings_render_field', false, $field, $value, $name, $id ); if ( $handled ) { return; } switch ( $field->type ) { case 'checkbox': self::render_checkbox( $field, $value, $name, $id ); break; case 'text': self::render_text( $field, $value, $name, $id ); break; case 'password': self::render_password( $field, $value, $name, $id ); break; case 'number': self::render_number( $field, $value, $name, $id ); break; case 'textarea': self::render_textarea( $field, $value, $name, $id ); break; case 'code': self::render_code( $field, $value, $name, $id ); break; case 'html': self::render_html( $field, $value, $name, $id ); break; case 'color': self::render_color( $field, $value, $name, $id ); break; case 'button_link': self::render_button_link( $field, $id ); break; case 'select': self::render_select( $field, $value, $name, $id ); break; case 'radio': self::render_radio( $field, $value, $name, $id ); break; case 'media': self::render_media( $field, $value, $name, $id ); break; case 'page': PagePicker::render( $field, $value, $name, $id ); break; case 'post_types': case 'checklist': self::render_post_types( $field, $value, $name, $id ); break; case 'email': if ( ( $field->args['email'] ?? null ) instanceof Email ) { EmailRenderer::render( $field->args['email'], $value, $name, $id ); } break; default: /** * Fires for unrecognized field types so extensions can render them. * * @since 2.0.0 */ do_action( 'aiou_settings_render_field_' . $field->type, $field, $value, $name, $id ); } } /** * Default sanitization for a value by field type. * * @param string $type Field type. * @param mixed $value Raw value. * @param array $args Field args (e.g. 'choices'). * @return mixed */ public static function default_sanitize( string $type, mixed $value, array $args = array() ): mixed { $args['choices'] = self::choices( $args ); switch ( $type ) { case 'checkbox': return empty( $value ) ? 0 : 1; case 'number': return is_numeric( $value ) ? $value + 0 : 0; case 'select': case 'radio': // array_keys() casts purely-numeric string keys (e.g. '0', '7', '30') // to int, which would make the strict in_array() below reject every // submitted value -- $value from $_POST is always a string. Casting // every choice key to string first keeps the comparison correct // regardless of whether a caller's choices use numeric-looking keys. $choices = array_map( 'strval', array_keys( $args['choices'] ) ); $value = (string) $value; return in_array( $value, $choices, true ) ? $value : ( $choices[0] ?? '' ); case 'media': $id = absint( $value ); return ( $id && wp_attachment_is_image( $id ) ) ? $id : 0; case 'page': return PagePicker::sanitize( $value, (string) ( $args['post_type'] ?? 'page' ) ); case 'post_types': $choices = array_keys( $args['choices'] ); $value = is_array( $value ) ? $value : array(); return array_values( array_intersect( $choices, array_map( 'sanitize_key', $value ) ) ); case 'checklist': // Matched against the choices as-is (ids such as dashboard // widget ids are not always valid sanitize_key() output). $choices = array_map( 'strval', array_keys( $args['choices'] ) ); $value = is_array( $value ) ? array_map( 'strval', $value ) : array(); return array_values( array_intersect( $choices, $value ) ); case 'textarea': return sanitize_textarea_field( (string) $value ); case 'code': // Code is stored as typed; a field that outputs it must check // `unfiltered_html` in its own sanitizer (see Custom Code). return current_user_can( 'unfiltered_html' ) ? (string) $value : sanitize_textarea_field( (string) $value ); case 'html': return wp_kses_post( (string) $value ); case 'color': return (string) sanitize_hex_color( (string) $value ); case 'button_link': return ''; case 'text': case 'password': default: return sanitize_text_field( (string) $value ); } } /** * Accessible toggle switch (a real checkbox, restyled). * * @param Field $field Field. * @param mixed $value Value. * @param string $name Name attr. * @param string $id Id attr. * @return void */ private static function render_checkbox( Field $field, mixed $value, string $name, string $id ): void { ?> args['placeholder'] ) ? 'placeholder="' . esc_attr( $field->args['placeholder'] ) . '"' : ''; ?> /> args['min'] ) ? 'min="' . esc_attr( (string) $field->args['min'] ) . '"' : ''; ?> args['max'] ) ? 'max="' . esc_attr( (string) $field->args['max'] ) . '"' : ''; ?> args['step'] ) ? 'step="' . esc_attr( (string) $field->args['step'] ) . '"' : ''; ?> /> args['rows'] ) ? (int) $field->args['rows'] : 4; ?> 'text/css', 'javascript' => 'text/javascript', 'html' => 'text/html', ); $language = (string) ( $field->args['language'] ?? 'css' ); $language = isset( $types[ $language ] ) ? $language : 'css'; $rows = isset( $field->args['rows'] ) ? (int) $field->args['rows'] : 12; $settings = function_exists( 'wp_enqueue_code_editor' ) ? wp_enqueue_code_editor( array( 'type' => $types[ $language ] ) ) : false; ?>
` tags (no wpautop), since the markup -- e.g. an email header or * footer -- is used as-is. Optional `args['variables']` (tag => * description) renders click-to-insert variable chips underneath. * * @param Field $field Field. * @param mixed $value Value. * @param string $name Name attr. * @param string $id Id attr. * @return void */ private static function render_html( Field $field, mixed $value, string $name, string $id ): void { if ( function_exists( 'wp_enqueue_editor' ) ) { wp_enqueue_editor(); } $variables = (array) ( $field->args['variables'] ?? array() ); ?>

$description ) : ?>

args['new_tab'] ); ?> > args['button_label'] ?? $field->label ) ); ?> args ); ?> args ); $current = (string) $value; if ( ! isset( $choices[ $current ] ) ) { $current = (string) array_key_first( $choices ); } ?>
$choice ) : ?>

args ); $selected = is_array( $value ) ? $value : array(); ?>
type ? ' class="aiou-checklist"' : ''; ?>> label ); ?>

args['empty'] ?? __( 'No eligible post types were found.', 'all-in-one-utilities' ) ) ); ?>

$label ) : ?>
$args Field args. * @return array> A label, or (radio) array( label, hint ). */ public static function choices( array $args ): array { $choices = $args['choices'] ?? array(); return (array) ( $choices instanceof \Closure ? $choices() : $choices ); } /** * Shared inline description output. * * @param Field $field Field. * @return void */ private static function render_description( Field $field ): void { if ( ! empty( $field->args['description'] ) ) { ?>

args['description'] ); ?>