<?php
/**
 * Pagination layout.
 *
 * @package masterpiece
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! function_exists ( 'masterpiece_pagination' ) ) {

	function masterpiece_pagination( $args = array(), $class = 'pagination' ) {

        // To avoid pagination display when Jetpack infinite scroll is active.
        if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'infinite-scroll' ) ) return;
        
        if ($GLOBALS['wp_query']->max_num_pages <= 1) return;

		$args = wp_parse_args( $args, array(
			'mid_size'           => 2,
			'prev_next'          => true,
			'prev_text'          => esc_html__('&laquo;', 'masterpiece'),
			'next_text'          => esc_html__('&raquo;', 'masterpiece'),
			'screen_reader_text' => esc_html__('Posts navigation', 'masterpiece'),
			'type'               => 'array',
			'current'            => max( 1, get_query_var('paged') ),
		) );

        $links = paginate_links($args);

        ?>

        <nav aria-label="<?php echo $args['screen_reader_text']; ?>">

            <ul class="pagination">

                <?php

                    foreach ( $links as $key => $link ) { ?>

                        <li class="page-item <?php echo strpos( $link, 'current' ) ? 'active' : '' ?>">

                            <?php echo str_replace( 'page-numbers', 'page-link', $link ); ?>

                        </li>

                <?php } ?>

            </ul>

        </nav>

        <?php
    }
}

?>
