<?php 
/**
 * @package WordPress
 * @subpackage Campoal
 */

/**
* Display posts pagination
*
* @since Campoal 1.0.0
*/
if( !function_exists('conikal_pagination') ):
	function conikal_pagination($pages = '', $range = 2) {
		$showitems = ($range * 2)+1;

		global $paged;
		if(empty($paged)) $paged = 1;

		if($pages == '') {
			global $wp_query;
			$pages = $wp_query->max_num_pages;
			if(!$pages)
			{
				$pages = 1;
			}
		}

		if(1 != $pages) {
			echo '<div class="pagination-buttons">';

			if($paged > 2 && $paged > $range+1 && $showitems < $pages) {
				echo '<a href="' . esc_url(get_pagenum_link(1)) . '" class="ui basic icon button" data-bjax><i class="angle double left icon"></i></a>';
			}

			if($paged > 1 && $showitems < $pages) {
				echo '<a href="' . esc_url(get_pagenum_link($paged - 1)) . '" class="ui basic icon button" data-bjax><i class="angle left icon"></i></a>';
			}

			for ($i = 1; $i <= $pages; $i++) {
				if (1 != $pages &&( !($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems )) {
					if ($paged == $i) {
						echo '<a href="#" class="ui primary icon button">' . esc_html($i) . '</a>';
					} else {
						echo '<a href="' . esc_url(get_pagenum_link($i)) . '" class="ui basic icon button" data-bjax>' . esc_html($i) . '</a>';
					}
				}
			}

			if ($paged < $pages && $showitems < $pages) {
				echo '<a href="' . esc_url(get_pagenum_link($paged + 1)) . '" class="ui basic icon button" data-bjax><i class="angle right icon"></i></a>';
			}

			if ($paged < $pages - 1 &&  $paged + $range - 1 < $pages && $showitems < $pages) {
				echo '<a href="' . esc_url(get_pagenum_link($pages)) . '" class="ui basic icon button" data-bjax><i class="angle double right icon"></i></a>';
			}

			echo '</div>';
		}
	}
endif;


/**
* Display pagination buttons
*
* @since Campoal 2.0.0
*/
if (!function_exists('conikal_pagination_buttons')) :
	function conikal_pagination_buttons($text_count = '', $max_num_pages = 0,  $total = 0, $posts_per_page = 10) { ?>
		<div class="pagination">
            <div class="ui grid">
                <div class="sixteen wide mobile ten wide tablet twelve wide computer column pagination-column-left">
                    <?php conikal_pagination($max_num_pages) ?>
                </div>
                <div class="sixteen wide mobile six wide tablet four wide computer right aligned column pagination-column-right">
                    <?php conikal_pagination_count($text_count, $total, $posts_per_page); ?>
                </div>
            </div>
        </div>
	<?php }
endif;


/**
* Display pagination number
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_pagination_count')) :
	function conikal_pagination_count($text, $total = false, $per_page = false) {
		global $wp_query;

		$per_p_setting = $per_page ? $per_page : get_option('posts_per_page');
		$per_p = $per_p_setting != '' ? intval($per_p_setting) : 10;
		$total_p = $total ? $total : $wp_query->found_posts;
		$page_no = (get_query_var('paged')) ? get_query_var('paged') : 1;

		$from_p = ($page_no == 1) ? 1 : $per_p * ($page_no - 1) + 1;
		$to_p = ($total_p - ($page_no - 1) * $per_p > $per_p) ? $per_p * $page_no : $total_p;

		echo esc_html($from_p) . ' - ' . esc_html($to_p) . ' ' . esc_html__('of', 'campoal') . ' ' . esc_html($total_p) . ' ' . esc_html($text);
	}
endif;

?>