|null $schema Public or active schema. * @param bool $showTitle Whether the title is shown. * @param bool $showDescription Whether the description is shown. * @return string Markup, or '' without a schema. */ public static function html( ?array $schema, bool $showTitle = true, bool $showDescription = true ): string { if ( ! is_array( $schema ) ) { return ''; } $meta = is_array( $schema['metadata'] ?? null ) ? $schema['metadata'] : array(); $out = ''; $title = trim( (string) ( $meta['title'] ?? '' ) ); if ( $showTitle && '' !== $title ) { $out .= '

' . esc_html( $title ) . '

'; } $description = trim( wp_strip_all_tags( (string) ( $meta['description'] ?? '' ) ) ); if ( $showDescription && '' !== $description ) { $out .= '

' . esc_html( $description ) . '

'; } $fields = is_array( $schema['fields'] ?? null ) ? $schema['fields'] : array(); $leaves = array(); self::collect( self::firstPage( $fields ), $leaves ); if ( 'conversational' === strtolower( (string) ( $meta['type'] ?? '' ) ) ) { $leaves = array_slice( $leaves, 0, 1 ); } foreach ( array_slice( $leaves, 0, self::MAX_FIELDS ) as $field ) { $out .= self::field( $field ); } if ( 'conversational' !== strtolower( (string) ( $meta['type'] ?? '' ) ) ) { $out .= '
'; } if ( '' === $out ) { return ''; } return ''; } /** * One field: its label and a box the shape of its control. * * @param array $field Field. * @return string */ private static function field( array $field ): string { $type = (string) ( $field['type'] ?? '' ); $label = self::label( $field ); $width = (int) ( $field['appearance']['width'] ?? $field['width'] ?? 100 ); $style = ( $width > 0 && $width < 100 ) ? ' style="' . esc_attr( 'flex-basis:calc(' . $width . '% - var(--subtleforms-field-gap, 20px))' ) . '"' : ''; $html = '
'; if ( '' !== $label ) { $html .= '' . esc_html( $label ) . ''; } if ( in_array( $type, self::CHOICES, true ) ) { $count = is_array( $field['options'] ?? null ) ? count( $field['options'] ) : 3; $html .= str_repeat( '', max( 1, min( 5, $count ) ) ); } elseif ( in_array( $type, self::TALL, true ) ) { $html .= ''; } else { $html .= ''; } return $html . '
'; } /** * The text of a field's label. * * @param array $field Field. * @return string */ private static function label( array $field ): string { $config = is_array( $field['config'] ?? null ) ? $field['config'] : array(); $label = $field['label'] ?? $config['label'] ?? ''; return is_string( $label ) ? trim( wp_strip_all_tags( $label ) ) : ''; } /** * Fields on the first page: the first step's fields, the fields before the * first page break, or all of them. * * @param array $fields Top-level fields. * @return array */ private static function firstPage( array $fields ): array { foreach ( $fields as $field ) { if ( is_array( $field ) && 'step' === ( $field['type'] ?? '' ) ) { return self::children( $field ); } } $page = array(); foreach ( $fields as $field ) { if ( is_array( $field ) && 'form_step' === ( $field['type'] ?? '' ) ) { return $page; } $page[] = $field; } return $page; } /** * Flatten containers and columns into the fields inside them. * * @param array $fields Fields. * @param array> $leaves Collected fields. */ private static function collect( array $fields, array &$leaves ): void { foreach ( $fields as $field ) { if ( ! is_array( $field ) || count( $leaves ) >= self::MAX_FIELDS ) { continue; } $type = (string) ( $field['type'] ?? '' ); if ( in_array( $type, self::HIDDEN, true ) ) { continue; } // Conditional logic that shows the field only when a rule matches. if ( ! empty( $field['logic']['groups'] ) && 'show' === ( $field['logic']['action'] ?? '' ) ) { continue; } if ( ! empty( $field['columns'] ) && is_array( $field['columns'] ) ) { foreach ( $field['columns'] as $column ) { self::collect( is_array( $column ) ? ( array_is_list( $column ) ? $column : self::children( $column ) ) : array(), $leaves ); } continue; } $children = self::children( $field ); if ( $children ) { self::collect( $children, $leaves ); continue; } $leaves[] = $field; } } /** * Child fields of a container, a step or a column. * * @param array $node Node. * @return array */ private static function children( array $node ): array { foreach ( array( 'fields', 'children' ) as $key ) { if ( ! empty( $node[ $key ] ) && is_array( $node[ $key ] ) ) { return array_values( $node[ $key ] ); } } return array(); } }