<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package nextpage
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 *
 * @return array
 */

if ( ! class_exists( 'Nextpage_Functions' ) ) {
	class Nextpage_Functions {
		private static $instance;

		public function __construct() {
			add_filter( 'body_class', array( $this, 'body_classes' ) );
			add_action( 'wp_head', array( $this, 'pingback_header' ) );
		}

		/**
		 * get instance
		 * @since 1.0.0
		 * */
		public static function getInstance() {
			if ( null == self::$instance ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * nextpage_body_classes
		 * @since 1.0.0
		 * */
		public function body_classes( $classes ) {
			// Adds a class of hfeed to non-singular pages.
			if ( ! is_singular() ) {
				$classes[] = 'hfeed';
			}

			// Adds a class of no-sidebar when there is no sidebar present.
			if ( ! is_active_sidebar( 'sidebar-1' ) ) {
				$classes[] = 'no-sidebar';
			}

			return $classes;
		}

		/**
		 * pingback_header
		 * @since 1.0.0
		 * */
		public function pingback_header() {
			if ( is_singular() && pings_open() ) {
				printf( '<link rel="pingback" href="%s">', esc_url( get_bloginfo( 'pingback_url' ) ) );
			}
		}

		/*
         * Pages Links
         *
         * @since 1.0.0
          */
		public static function link_pages() {
			$defaults = array(
				'before'         => '<div class="wp-link-pages"><span>' . esc_html__( 'Pages:', 'nextpage' ) . '</span>',
				'after'          => '</div>',
				'link_before'    => '',
				'link_after'     => '',
				'next_or_number' => 'number',
				'separator'      => ' ',
				'pagelink'       => '%',
				'echo'           => 1
			);
			wp_link_pages( $defaults );
		}

		/*
		 * Pagination
		 *
		 * @since 1.0.0
		  */

		function post_pagination( \WP_Query $wp_query = null, $echo = true, $params = [] ) {
			if ( null === $wp_query ) {
				global $wp_query;
			}

			$add_args = [];

			$pages = paginate_links( array_merge( [
					'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
					'format'       => '?paged=%#%',
					'current'      => max( 1, get_query_var( 'paged' ) ),
					'total'        => $wp_query->max_num_pages,
					'type'         => 'array',
					'show_all'     => false,
					'end_size'     => 3,
					'mid_size'     => 1,
					'prev_next'    => true,
					'prev_text'    => '<i class="la la-angle-left"></i>',
					'next_text'    => '<i class="la la-angle-right"></i>',
					'add_args'     => $add_args,
					'add_fragment' => ''
				], $params )
			);

			if ( is_array( $pages ) ) {
				$pagination = '<div class="nextpage-pagination"><ul class="pagination pagination-2">';

				foreach ( $pages as $page ) {
					$pagination .= '<li class="page-item ' . ( strpos( $page, 'current' ) !== false ? 'active' : '' ) . '"> ' . str_replace( 'page-numbers', 'page-link', $page ) . '</li>';
				}

				$pagination .= '</ul></div>';

				if ( $echo ) {
					echo wp_kses_post( $pagination );
				} else {
					return $pagination;
				}
			}

			return null;
		}

	}//end class

}
