<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variables injected by mbs_get_template_part(); not global vars
/**
 * File Path: templates/archive-mbs-book.php
 *
 * Archive Template for mbs_book post type
 *
 */

if ( ! mbs_is_registered() ) {
	get_header();
	get_footer();
	return;
}

get_header();

$archive_title = mbs_get_setting( 'formatting', 'archive_title', __( 'Our Books', 'my-book-showroom' ) );
if ( ! empty( $archive_title ) ) :
	?>
	<header class="mbs-archive-header">
		<h1 class="mbs-archive-title"><?php echo esc_html( $archive_title ); ?></h1>
	</header>
	<?php
endif;

$query = new WP_Query( array(
	'post_type'      => MBS_POST_TYPE,
	'posts_per_page' => mbs_get_setting( 'formatting', 'items_per_page', 6 ),
	'paged'          => max( 1, get_query_var( 'paged' ) ),
	'no_found_rows'  => false,
) );

$layout_slug = mbs_get_setting( 'formatting', 'default_layout', 'grid' );
$layout = MBS_Layout_Registry::get_layout( $layout_slug );

echo $layout->render( $query ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted HTML from layout class; layout owns .mbs-wrapper and all internal escaping

if ( $query->max_num_pages > 1 ) {
	echo paginate_links( array( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- paginate_links() is core-generated trusted HTML
		'total'   => $query->max_num_pages,
		'current' => max( 1, get_query_var( 'paged' ) ),
	) );
}

get_footer();
