$group[0], 'icon' => sanitize_key( (string) ( $group[1] ?? '' ) ), 'tone' => AIEO_Engine_Blocks::is_tone( $tone ) ? $tone : '' ]; } continue; } $cells = array_map( 'trim', explode( '|', $line ) ); if ( '' === ( $cells[0] ?? '' ) ) { continue; } $rest = array_filter( array_slice( $cells, 1 ), 'strlen' ); if ( ! $rest ) { $rows[] = [ 'group' => $cells[0] ]; continue; } $rows[] = [ 'cells' => array_pad( array_slice( $cells, 0, $n ), $n, '' ) ]; } if ( ! $rows ) { return; } $highlight = array_values( array_filter( array_map( 'intval', preg_split( '/[^0-9]+/', (string) ( $attributes['highlight'] ?? '' ), -1, PREG_SPLIT_NO_EMPTY ) ?: [] ), static fn ( int $c ): bool => $c > 0 && $c < $n ) ); $lit = static fn ( int $c ): bool => in_array( $c, $highlight, true ); $caption = trim( (string) ( $attributes['caption'] ?? '' ) ); $label = trim( (string) ( $attributes['label'] ?? '' ) ); $note = trim( (string) ( $attributes['note'] ?? '' ) ); $phone = 'scroll' === (string) ( $attributes['phone'] ?? '' ) ? 'scroll' : 'stack'; $mobile = 'mobile' === rooster_device_class(); $id = wp_unique_id( 'rooster-compare-' ); $group_class = static fn ( array $row ): string => 'rooster-compare-table__group' . ( '' !== (string) ( $row['tone'] ?? '' ) ? ' ' . AIEO_Engine_Blocks::tone_classes( (string) $row['tone'], true ) : '' ); $group_name = static function ( array $row ): string { $icon = '' !== ( $row['icon'] ?? '' ) && function_exists( 'rooster_icon' ) ? rooster_icon( (string) $row['icon'], [ 'width' => 16, 'height' => 16, 'class' => 'rooster-compare-table__group-icon' ] ) : ''; return '' . $icon . esc_html( (string) $row['group'] ) . ''; }; $words = [ 'yes' => __( 'Yes', 'ai-eshop-optimizer' ), 'no' => __( 'No', 'ai-eshop-optimizer' ), 'soon' => __( 'Soon', 'ai-eshop-optimizer' ), 'partial' => __( 'Partial', 'ai-eshop-optimizer' ), ]; $given = preg_split( '/\s*(?:\||\r\n|\r|\n)\s*/', trim( (string) ( $attributes['marks'] ?? '' ) ) ) ?: []; foreach ( array_keys( $words ) as $i => $kind ) { if ( '' !== trim( (string) ( $given[ $i ] ?? '' ) ) ) { $words[ $kind ] = trim( (string) $given[ $i ] ); } } $parse = static function ( string $cell ): array { $cell = trim( $cell ); if ( str_starts_with( $cell, 'text:' ) ) { return [ 'text', trim( substr( $cell, 5 ) ) ]; } if ( '' === $cell || in_array( $cell, [ '-', '–', '—' ], true ) ) { return [ 'no', '' ]; } if ( preg_match( '/^(yes|no|soon|partial)(?:\s*\((.*)\))?$/iu', $cell, $m ) ) { return [ strtolower( $m[1] ), trim( (string) ( $m[2] ?? '' ) ) ]; } return [ 'text', $cell ]; }; $icons = (array) apply_filters( 'rooster/blocks/mark_icons', AIEO_Engine_Blocks::MARK_ICONS ); $value = static function ( string $kind, string $v, bool $visible ) use ( $words, $icons ): string { if ( 'text' === $kind ) { return AIEO_Engine_Blocks::inline( $v ); } $note = '' !== $v ? '' . AIEO_Engine_Blocks::inline( $v ) . '' : ''; if ( 'soon' === $kind ) { return '' . esc_html( $words['soon'] ) . '' . $note; } if ( $visible && '' === $note ) { $slug = (string) ( $icons[ $kind ] ?? AIEO_Engine_Blocks::MARK_ICONS['yes'] ); return rooster_icon( $slug, [ 'width' => 20, 'height' => 20, 'class' => 'rooster-mark rooster-mark--' . $kind ] ) . esc_html( $words[ $kind ] ) . $note; } return AIEO_Engine_Blocks::mark( $kind, $words[ $kind ] ) . $note; }; if ( $mobile && 'stack' === $phone ) { $wrapper = AIEO_Engine_Blocks::wrapper( $library, [ 'class' => 'rooster-compare-table rooster-compare-table--stack', 'data-rooster-ui' => 'rooster-one/table' ] ); $inner = '' !== $caption ? '

' . esc_html( $caption ) . '

' : ''; $table_name = '' !== $label ? $label : $caption; for ( $c = 1; $c < $n; $c++ ) { $name = '' !== $table_name ? sprintf( __( '%1$s — %2$s', 'ai-eshop-optimizer' ), $columns[ $c ], $table_name ) : ''; $inner .= '
' : ' aria-labelledby="' . esc_attr( $id . '-' . $c ) . '">' ) . '

' . esc_html( $columns[ $c ] ) . '

' . '
'; } if ( '' !== $note ) { $inner .= '

' . AIEO_Engine_Blocks::inline( $note ) . '

'; } AIEO_Engine_Blocks::out( sprintf( '
%s
', $wrapper, $inner ) ); return; } $table = ( '' !== $caption ? '' . esc_html( $caption ) . '' : '' ) . ''; foreach ( $columns as $c => $column ) { $table .= '' . esc_html( $column ) . ''; } $table .= ''; foreach ( $rows as $row ) { if ( isset( $row['group'] ) ) { $table .= '' . $group_name( $row ) . ''; continue; } $table .= '' . esc_html( $row['cells'][0] ) . ''; for ( $c = 1; $c < $n; $c++ ) { [ $k, $v ] = $parse( $row['cells'][ $c ] ); $table .= '' . $value( $k, $v, false ) . ''; } $table .= ''; } $table .= ''; if ( $mobile ) { $wrapper = AIEO_Engine_Blocks::wrapper( $library, [ 'class' => 'rooster-compare-table rooster-compare-table--scroll', 'role' => 'region', 'aria-label' => '' !== $label ? $label : ( '' !== $caption ? $caption : __( 'Comparison', 'ai-eshop-optimizer' ) ), 'tabindex' => '0', 'data-rooster-ui' => 'rooster-one/table', ] ); AIEO_Engine_Blocks::out( sprintf( '
%s
%s
', $wrapper, $table, '' !== $note ? '

' . AIEO_Engine_Blocks::inline( $note ) . '

' : '' ) ); return; } if ( '' !== $note ) { $wrapper = AIEO_Engine_Blocks::wrapper( $library, [ 'class' => 'rooster-compare-table rooster-compare-table--table', 'data-rooster-ui' => 'rooster-one/table' ] ); AIEO_Engine_Blocks::out( sprintf( '
%s
%s
', $wrapper, $table, AIEO_Engine_Blocks::inline( $note ) ) ); return; } $wrapper = AIEO_Engine_Blocks::wrapper( $library, [ 'class' => 'rooster-compare-table rooster-compare-table--table', 'data-rooster-ui' => 'rooster-one/table' ] ); AIEO_Engine_Blocks::out( sprintf( '%s
', $wrapper, $table ) );