<?php

/**
 * Layout: Numeric.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

$pagination_settings = aiopod_get_option( 'aiopod_pagination_settings' );

$max_num_pages = ! empty( $attributes['max_num_pages'] ) ? (int) $attributes['max_num_pages'] : 1;
$paged         = ! empty( $attributes['paged'] ) ? (int) $attributes['paged'] : aiopod_get_page_number();
$page_range    = ! empty( $pagination_settings['page_range'] ) ? (int) $pagination_settings['page_range'] : 2;

// Construct the pagination arguments to enter into our paginate_links function
$arr_params = array();

parse_str( isset( $_SERVER['QUERY_STRING'] ) ? wp_unslash( $_SERVER['QUERY_STRING'] ) : '', $queries );
if ( ! empty( $queries ) ) {
    $arr_params = array_keys( $queries );
}
    
$base = aiopod_remove_query_arg( $arr_params, get_pagenum_link( 1 ) );	

if ( ! get_option( 'permalink_structure' ) ) {
    $prefix = strpos( $base, '?' ) ? '&' : '?';
    $format = $prefix . 'paged=%#%';
} else {
    $prefix = ( '/' == substr( $base, -1 ) ) ? '' : '/';
    $format = $prefix . 'page/%#%';
} 

$pagination_args = array(
    'base'         => $base . '%_%',
    'format'       => $format,
    'total'        => $max_num_pages,
    'current'      => $paged,
    'show_all'     => false,
    'end_size'     => 1,
    'mid_size'     => $page_range,
    'prev_next'    => true,
    'prev_text'    => '<span class="aiopod-screen-reader-text">' . esc_html__( 'Previous page', 'aio-podcast-studio' ) . '</span><span aria-hidden="true">&laquo;</span>',
    'next_text'    => '<span class="aiopod-screen-reader-text">' . esc_html__( 'Next page', 'aio-podcast-studio' ) . '</span><span aria-hidden="true">&raquo;</span>',
    'type'         => 'array',
    'add_args'     => false,
    'add_fragment' => ''
);

$pagination_args = apply_filters( 'aiopod_pagination_args', $pagination_args, $attributes );
$paginate_links  = paginate_links( $pagination_args );

if ( is_array( $paginate_links ) ) {
    echo '<div class="aiopod-pagination">';
    echo '<aiopod-pagination>';

    echo '<div class="aiopod-pagination__links">'; 		   	
    foreach ( $paginate_links as $key => $page_link ) {
        echo wp_kses_post( $page_link );
    }
    echo '</div>';	

    echo '<div class="aiopod-pagination__info aiopod-text-muted aiopod-text-small">';
    echo sprintf( esc_html__( 'Page %d of %d', 'aio-podcast-studio' ), (int) $paged, (int) $max_num_pages );
    echo '</div>';		
    
    echo '</aiopod-pagination>';
    echo '</div>';
}
