<?php
if (! defined('ABSPATH')) {
    exit;
} $current_page = isset($args['current_page']) ? absint($args['current_page']) : 0;
$max_pages = isset($args['max_pages']) ? absint($args['max_pages']) : 0;
$current_url = isset($args['current_url']) ? esc_url_raw($args['current_url']) : '';

if ($max_pages <= 1) {
    return;
} $delta = 2;
$range_start = max(1, $current_page - $delta);
$range_end = min($max_pages, $current_page + $delta);
$pages = array();

if ($range_start > 1) {
    $pages[] = 1;

    if ($range_start > 2) {
        $pages[] = '...';
    }
} for ($i = $range_start; $i <= $range_end; $i++) {
    $pages[] = $i;
}

if ($range_end < $max_pages) {
    if ($range_end < $max_pages - 1) {
        $pages[] = '...';
    } $pages[] = $max_pages;
} ?>
<div class="blog-pagination">
	<div class="pagination">
		<?php if ($current_page > 1) : $prev_url = add_query_arg('paged', $current_page - 1, $current_url); ?>
			<a href="<?php echo esc_url($prev_url); ?>" class="nav-button" data-paged="<?php echo esc_attr($current_page - 1); ?>">
				<svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M7.41 10.59L2.83 6L7.41 1.41L6 0L0 6L6 12L7.41 10.59Z" fill="#4F4769"/></svg>
			</a>
		<?php else : ?>
			<span class="nav-button disabled">
				<svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M7.41 10.59L2.83 6L7.41 1.41L6 0L0 6L6 12L7.41 10.59Z" fill="#D9D9D9"/></svg>
			</span>
		<?php endif; ?>

		<?php foreach ($pages as $page) {
		    if ($page === '...') { ?>
				<span class="page-ellipsis">&hellip;</span>
			<?php } else {
			    $page_url = add_query_arg('paged', $page, $current_url); ?>
				<a href="<?php echo esc_url($page_url); ?>" class="page-button <?php echo $page === $current_page ? 'active' : ''; ?>" data-paged="<?php echo esc_attr($page); ?>">
					<?php echo esc_html($page); ?>
				</a>
			<?php }
			} ?>

		<?php if ($current_page < $max_pages) : $next_url = add_query_arg('paged', $current_page + 1, $current_url); ?>
			<a href="<?php echo esc_url($next_url); ?>" class="nav-button" data-paged="<?php echo esc_attr($current_page + 1); ?>">
				<svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M4.59 1.41L9.17 6L4.59 10.59L6 12L12 6L6 0L4.59 1.41Z" fill="#4F4769"/></svg>
			</a>
		<?php else : ?>
			<span class="nav-button disabled">
				<svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M4.59 1.41L9.17 6L4.59 10.59L6 12L12 6L6 0L4.59 1.41Z" fill="#D9D9D9"/></svg>
			</span>
		<?php endif; ?>
	</div>
</div>
