<?php
/**
 * Pager
 *
 * @package Multi
 */

?>

<?php
global $wp_query;
$async_loading = is_post_type_archive( 'project' ) ? get_theme_mod( 'multi_builder_load_projects', 'off' ) : get_theme_mod( 'multi_load_postlist', MULTIMAGAZINE_LOAD_POSTLIST );
if ( 'button' === $async_loading ) {
	$multimagazine_load_button = true;
	if ( get_query_var( 'paged' ) !== $wp_query->max_num_pages && $wp_query->max_num_pages > 1 ) :
		?>
		<p class="load-button"><a href="#" class="button load-more-posts"><?php esc_html_e( 'Load more posts', 'multimagazine' ); ?></a></p>
		<?php
	endif;
}
?>

<?php
if ( $wp_query->max_num_pages > 1 ) :

	$multimagazine_nav_classes = 'navigation pagination';
	if ( 'modern' === get_theme_mod( 'multi_pagination_style', MULTIMAGAZINE_PAGINATION_STYLE ) ) {
		$multimagazine_nav_classes .= ' pagination-modern';
	}
	if ( 'outside' === get_theme_mod( 'pagination_dot_border_position', MULTIMAGAZINE_PAGINATION_DOT_BORDER_POSITION ) ) {
		$multimagazine_nav_classes .= ' pagination-outside';
	}
	if ( isset( $multimagazine_load_button ) ) {
		$multimagazine_nav_classes .= ' hide';
	}
	$permalink_structure = get_option( 'permalink_structure' );
	?>
	<nav class="<?php echo esc_attr( $multimagazine_nav_classes ); ?>" aria-label="<?php esc_attr_e( 'Pagination', 'multimagazine' ); ?>" data-structure="<?php echo esc_attr( $permalink_structure ); ?>">
	<?php
	if ( $permalink_structure ) {
		$format = '/page/%#%/';
	} else {
		$format = '?paged=%#%';
	}
	if ( is_search() ) {
		$page_link = get_pagenum_link( 1 );
		$base      = substr( $page_link, 0, strpos( $page_link, '?' ) );

		$search_string = ! empty( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; // phpcs:ignore
		echo wp_kses_post(
			paginate_links(
				array(
					'base'      => $base . '%_%?s=' . $search_string,
					'format'    => $format,
					'current'   => max( 1, get_query_var( 'paged' ) ),
					'total'     => $wp_query->max_num_pages,
					'prev_text' => '<i class="pagination-icon m-icon m-icon-long-arrow-left" aria-hidden="true"></i>' . __( 'Previous', 'multimagazine' ),
					'next_text' => __( 'Next', 'multimagazine' ) . '<i class="pagination-icon m-icon m-icon-long-arrow-right"></i>',
				)
			)
		);
	} else {
		echo wp_kses_post(
			paginate_links(
				array(
					'base'      => get_pagenum_link( 1 ) . '%_%',
					'format'    => $format,
					'current'   => max( 1, get_query_var( 'paged' ) ),
					'total'     => $wp_query->max_num_pages,
					'prev_text' => '<i class="pagination-icon m-icon m-icon-long-arrow-left" aria-hidden="true"></i>' . __( 'Previous', 'multimagazine' ),
					'next_text' => __( 'Next', 'multimagazine' ) . '<i class="pagination-icon m-icon m-icon-long-arrow-right"></i>',
				)
			)
		);
	}
	?>
</nav>
<?php endif; ?>
