<?php
/**
 * Tables render.
 *
 * @var $table_class
 * @var $table_headings
 * @var $table_items
 *
 * @package cyberpress/templates
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$table_class   = 'cyberpress-table' . ( isset( $table_class ) ? ( ' ' . $table_class ) : '' );
$allowed_items = array();

foreach ( $table_items as $items ) {
    foreach ( $items as $name => $data ) {
        $output = $data;

        if ( is_array( $data ) ) {
            if ( isset( $data['output'] ) ) {
                $output = $data['output'];
            } else {
                $output = '';
            }
        }

        if ( ! isset( $allowed_items[ $name ] ) && $output ) {
            $allowed_items[ $name ] = 1;
        }
    }
}

?>

<table class="<?php echo esc_attr( $table_class ); ?>">
    <?php if ( isset( $table_headings ) ) : ?>
        <thead>
            <tr>
                <?php
                foreach ( $table_headings as $name => $data ) :
                    if ( ! isset( $allowed_items[ $name ] ) ) {
                        continue;
                    }

                    $label = $data;
                    $class = '';

                    if ( is_array( $data ) ) {
                        if ( isset( $data['label'] ) ) {
                            $label = $data['label'];
                        } else {
                            $label = '';
                        }

                        if ( isset( $data['class'] ) ) {
                            $class = $data['class'];
                        }
                    }

                    // phpcs:ignore ?>
                    <th<?php echo ( $class ? ( ' class="' . esc_attr( $class ) . '"' ) : '' ); ?>><?php echo esc_html( $label ); ?></th>
                <?php endforeach; ?>
            </tr>
        </thead>
    <?php endif; ?>
    <tbody>
        <?php foreach ( $table_items as $items ) : ?>
            <tr>
                <?php
                foreach ( $items as $name => $data ) :
                    if ( ! isset( $allowed_items[ $name ] ) ) {
                        continue;
                    }

                    $output = $data;
                    $class  = '';

                    if ( is_array( $data ) ) {
                        if ( isset( $data['output'] ) ) {
                            $output = $data['output'];
                        } else {
                            $output = '';
                        }

                        if ( isset( $data['class'] ) ) {
                            $class = $data['class'];
                        }
                    }

                    // phpcs:ignore ?>
                    <td<?php echo ( $class ? ( ' class="' . esc_attr( $class ) . '"' ) : '' ); ?>>
                        <?php
                        // All output should be escaped.
                        // phpcs:ignore
                        echo $output;
                        ?>
                    </td>
                <?php endforeach; ?>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
