'; const STAR_SVG = ''; private static $seq = 0; private static function id( $prefix = 'aieo-c' ) { self::$seq++; return $prefix . '-' . self::$seq; } private static function args( $a, $extra = array() ) { return array_merge( array( 'id' => '', 'name' => '', 'label' => '', 'help' => '', 'error' => '', 'value' => '', 'required' => false, 'disabled' => false, 'class' => '', ), $extra, (array) $a ); } private static function label_for( $a ) { $desc = array(); $html = ''; if ( '' !== (string) $a['label'] ) { $html .= ''; } if ( '' !== (string) $a['help'] ) { $desc[] = $a['id'] . '-help'; } if ( '' !== (string) $a['error'] ) { $desc[] = $a['id'] . '-err'; } return array( $html, implode( ' ', $desc ) ); } private static function notes( $a ) { $out = ''; if ( '' !== (string) $a['help'] ) { $out .= '

' . esc_html( $a['help'] ) . '

'; } if ( '' !== (string) $a['error'] ) { $out .= '

' . esc_html( $a['error'] ) . '

'; } return $out; } private static function common_attrs( $a, $desc ) { return ' id="' . esc_attr( $a['id'] ) . '"' . ( '' !== (string) $a['name'] ? ' name="' . esc_attr( $a['name'] ) . '"' : '' ) . ( $a['required'] ? ' required' : '' ) . ( $a['disabled'] ? ' disabled' : '' ) . ( '' !== (string) $a['error'] ? ' aria-invalid="true"' : '' ) . ( '' !== $desc ? ' aria-describedby="' . esc_attr( $desc ) . '"' : '' ); } public static function btn( $a ) { $a = array_merge( array( 'label' => '', 'variant' => 'secondary', 'size' => '', 'href' => '', 'type' => 'button', 'hook' => '', 'disabled' => false, 'attrs' => '', ), (array) $a ); $variant = class_exists( 'AIEO_Variants' ) ? AIEO_Variants::action( $a['variant'] ) : 'secondary'; $cls = 'aieo-c-btn aieo-c-btn--' . $variant; if ( in_array( $a['size'], array( 'sm', 'lg', 'block' ), true ) ) { $cls .= ' aieo-c-btn--' . $a['size']; } if ( '' !== (string) $a['hook'] ) { $cls .= ' ' . $a['hook']; } if ( '' !== (string) $a['href'] ) { return '' . esc_html( $a['label'] ) . ''; } return ''; } public static function badge( $label, $variant = 'secondary', $small = false ) { $variant = class_exists( 'AIEO_Variants' ) ? AIEO_Variants::badge( $variant ) : 'secondary'; return '' . esc_html( $label ) . ''; } public static function input( $a ) { $a = self::args( $a, array( 'type' => 'text', 'placeholder' => '', 'narrow' => false ) ); if ( '' === $a['id'] ) { $a['id'] = self::id( 'aieo-c-in' ); } list( $lab, $desc ) = self::label_for( $a ); return '
' . $lab . '' . self::notes( $a ) . '
'; } public static function textarea( $a ) { $a = self::args( $a, array( 'rows' => 4, 'placeholder' => '' ) ); if ( '' === $a['id'] ) { $a['id'] = self::id( 'aieo-c-ta' ); } list( $lab, $desc ) = self::label_for( $a ); return '
' . $lab . '' . self::notes( $a ) . '
'; } public static function select( $a ) { $a = self::args( $a, array( 'options' => array(), 'placeholder' => '', 'narrow' => false ) ); if ( '' === $a['id'] ) { $a['id'] = self::id( 'aieo-c-sel' ); } list( $lab, $desc ) = self::label_for( $a ); $opts = ''; if ( '' !== (string) $a['placeholder'] ) { $opts .= ''; } foreach ( (array) $a['options'] as $code => $label ) { $opts .= ''; } return '
' . $lab . '' . self::notes( $a ) . '
'; } public static function choice( $a ) { $a = self::args( $a, array( 'options' => array(), 'many' => false, 'inline' => false ) ); if ( '' === $a['id'] ) { $a['id'] = self::id( 'aieo-c-ch' ); } $type = $a['many'] ? 'checkbox' : 'radio'; $name = (string) $a['name']; if ( $a['many'] && '' !== $name && '[]' !== substr( $name, -2 ) ) { $name .= '[]'; } $chosen = is_array( $a['value'] ) ? array_map( 'strval', $a['value'] ) : array( (string) $a['value'] ); $items = ''; $i = 0; foreach ( (array) $a['options'] as $code => $opt ) { $label = is_array( $opt ) ? ( isset( $opt['label'] ) ? $opt['label'] : '' ) : $opt; $sub = is_array( $opt ) && isset( $opt['sub'] ) ? $opt['sub'] : ''; $oid = $a['id'] . '-' . $i; $i++; $items .= ''; } return self::group( $a, '
' . $items . '
' ); } private static function group( $a, $inner ) { $legend = ''; if ( '' !== (string) $a['label'] ) { $legend = '' . esc_html( $a['label'] ) . ( $a['required'] ? ' ' : '' ) . ''; } return '
' . $legend . $inner . self::notes( $a ) . '
'; } public static function scale( $a ) { $a = self::args( $a, array( 'min' => 1, 'max' => 5, 'labels' => array(), 'low' => '', 'high' => '', 'widget' => 'segmented', ) ); if ( '' === $a['id'] ) { $a['id'] = self::id( 'aieo-c-sc' ); } $min = (int) $a['min']; $max = (int) $a['max']; if ( $max <= $min ) { $max = $min + 1; } $ends = ( '' !== (string) $a['low'] || '' !== (string) $a['high'] ) ? '
' . esc_html( $a['low'] ) . '' . esc_html( $a['high'] ) . '
' : ''; if ( 'slider' === $a['widget'] ) { $val = (string) $a['value']; $start = '' !== $val ? (int) $val : (int) round( ( $min + $max ) / 2 ); return self::group( $a, '
' . '
' . '' . '' . esc_html( self::EMPTY_MARKER ) . '' . '
' . $ends . '' . '
' ); } $opts = ''; for ( $v = $min; $v <= $max; $v++ ) { $oid = $a['id'] . '-' . $v; $point = isset( $a['labels'][ $v ] ) ? $a['labels'][ $v ] : (string) $v; $opts .= ''; } return self::group( $a, '
' . '
' . $opts . '
' . $ends . '
' ); } public static function rating( $a ) { $a = self::args( $a, array( 'max' => 5, 'point_label' => '' ) ); if ( '' === $a['id'] ) { $a['id'] = self::id( 'aieo-c-rt' ); } $max = max( 2, (int) $a['max'] ); $tpl = '' !== (string) $a['point_label'] ? $a['point_label'] : __( '%1$s out of %2$s', 'ai-eshop-optimizer' ); $opts = ''; for ( $v = 1; $v <= $max; $v++ ) { $oid = $a['id'] . '-' . $v; $opts .= ''; } return self::group( $a, '
' . $opts . '
' ); } public static function rank( $a ) { $a = self::args( $a, array( 'options' => array() ) ); if ( '' === $a['id'] ) { $a['id'] = self::id( 'aieo-c-rk' ); } $hidden_id = $a['id'] . '-value'; $options = (array) $a['options']; if ( '' !== (string) $a['value'] ) { $order = array_filter( array_map( 'trim', explode( ',', (string) $a['value'] ) ) ); $sorted = array(); foreach ( $order as $code ) { if ( array_key_exists( $code, $options ) ) { $sorted[ $code ] = $options[ $code ]; } } foreach ( $options as $code => $label ) { if ( ! isset( $sorted[ $code ] ) ) { $sorted[ $code ] = $label; } } $options = $sorted; } $items = ''; $pos = 1; foreach ( $options as $code => $label ) { $items .= '
  • ' . '' . '' . '' . esc_html( $label ) . '' . '' . '' . '' . '
  • '; $pos++; } $inner = '
      ' . $items . '
    ' . '
    ' . ''; return self::group( $a, $inner ); } public static function steps( $steps, $current = 1 ) { $steps = array_values( (array) $steps ); $total = count( $steps ); if ( ! $total ) { return ''; } $current = max( 1, min( $total, (int) $current ) ); $out = '
      '; foreach ( $steps as $i => $label ) { $n = $i + 1; $state = ''; if ( $n < $current ) { $state = ' is-done'; } elseif ( $n === $current ) { $state = ' is-current'; } $out .= '
    1. ' . '' . ( $n < $current ? self::CHECK_SVG . '' . esc_html__( 'Done:', 'ai-eshop-optimizer' ) . '' : $n ) . '' . '' . esc_html( $label ) . '' . ( $n < $total ? '' : '' ) . '
    2. '; } return $out . '
    '; } public static function progress( $done, $total, $label = '' ) { $total = max( 1, (int) $total ); $done = max( 0, min( $total, (int) $done ) ); $pct = (int) round( ( $done / $total ) * 100 ); return '' . $pct . '%'; } public static function question( $q ) { $q = array_merge( array( 'answers' => 'choice', 'cardinality' => 'one', 'ordered' => 'no', 'scored' => 'none', 'length' => 'short', 'widget' => '', ), (array) $q ); if ( 'text' === $q['answers'] ) { return 'long' === $q['length'] ? self::textarea( $q ) : self::input( $q ); } if ( 'number' === $q['answers'] ) { $q['type'] = 'number'; return self::input( $q ); } if ( 'yes' === $q['ordered'] ) { return self::rank( $q ); } if ( 'scale' === $q['scored'] && 'one' === $q['cardinality'] ) { if ( 'rating' === $q['widget'] ) { return self::rating( $q ); } $q['widget'] = ( 'slider' === $q['widget'] ) ? 'slider' : 'segmented'; return self::scale( $q ); } if ( 'select' === $q['widget'] ) { return self::select( $q ); } $q['many'] = ( 'many' === $q['cardinality'] ); return self::choice( $q ); } }