<?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;
		}

		/*
		 * kses allowed html
		 * @since 1.0.0
		 * */
		public function kses_allowed_html( $allowed_tags = 'all' ) {
			$allowed_html = array(
				'div'    => array( 'class' => array(), 'id' => array() ),
				'header' => array( 'class' => array(), 'id' => array() ),
				'h1'     => array( 'class' => array(), 'id' => array() ),
				'h2'     => array( 'class' => array(), 'id' => array() ),
				'h3'     => array( 'class' => array(), 'id' => array() ),
				'h4'     => array( 'class' => array(), 'id' => array() ),
				'h5'     => array( 'class' => array(), 'id' => array() ),
				'h6'     => array( 'class' => array(), 'id' => array() ),
				'p'      => array( 'class' => array(), 'id' => array() ),
				'span'   => array( 'class' => array(), 'id' => array() ),
				'i'      => array( 'class' => array(), 'id' => array() ),
				'mark'   => array( 'class' => array(), 'id' => array() ),
				'strong' => array( 'class' => array(), 'id' => array() ),
				'br'     => array( 'class' => array(), 'id' => array() ),
				'b'      => array( 'class' => array(), 'id' => array() ),
				'em'     => array( 'class' => array(), 'id' => array() ),
				'del'    => array( 'class' => array(), 'id' => array() ),
				'ins'    => array( 'class' => array(), 'id' => array() ),
				'u'      => array( 'class' => array(), 'id' => array() ),
				's'      => array( 'class' => array(), 'id' => array() ),
				'nav'    => array( 'class' => array(), 'id' => array() ),
				'ul'     => array( 'class' => array(), 'id' => array() ),
				'li'     => array( 'class' => array(), 'id' => array() ),
				'form'   => array( 'class' => array(), 'id' => array() ),
				'select' => array( 'class' => array(), 'id' => array() ),
				'option' => array( 'class' => array(), 'id' => array() ),
				'img'    => array( 'class' => array(), 'id' => array() ),
				'a'      => array( 'class' => array(), 'id' => array(), 'href' => array() ),
			);

			if ( 'all' == $allowed_tags ) {
				return $allowed_html;
			} else {
				if ( is_array( $allowed_tags ) && ! empty( $allowed_tags ) ) {
					$specific_tags = array();
					foreach ( $allowed_tags as $allowed_tag ) {
						if ( array_key_exists( $allowed_tag, $allowed_html ) ) {
							$specific_tags[ $allowed_tag ] = $allowed_html[ $allowed_tag ];
						}
					}

					return $specific_tags;
				}
			}

		} //end kses_allowed_html


		/*
		 * meta query
		 * @since 1.0.0
		 * */
		public function meta_query( $prefix, $id ) {
			global $post;
			$meta = '';
			if ( ! empty( $post->ID ) ) {
				$meta = get_post_meta( $post->ID, $prefix, true );
				$meta = ( isset( $meta[ $id ] ) && ! empty( $meta[ $id ] ) ) ? $meta[ $id ] : '';
			}

			return $meta;

		}


		/*
		 *header
		 *
		 * @since 1.0.0
		 * */
		public function header() {

			$nextpage_header_global = nextpage_get_option( 'nav_menu', 'header-one' ); //for globally

			$nextpage_header_style = get_post_meta( get_queried_object_id(), 'nextpage_page_meta', true ); // only for page

			if ( is_page() && isset( $nextpage_header_style ) && ! empty( $nextpage_header_style ) ) {
				if ( isset( $nextpage_header_style['header_style'] ) && 'header-builder' == $nextpage_header_style['header_style'] ) {
					get_template_part( 'template-parts/header/' . $nextpage_header_style['header_style'] );
				} else {
					get_template_part( 'template-parts/header/' . $nextpage_header_style['nextpage_nav_menu'] );
				}
			} else {
				if ( 'header-builder' == nextpage_get_option( 'header_style' ) ) {
					get_template_part( 'template-parts/header/header-builder' );
				} else {
					get_template_part( 'template-parts/header/' . $nextpage_header_global );
				}

			}

		}


		/*
		 *header
		 *
		 * @since 1.0.0
		 * */
		public function footer() {

			$nextpage_footer_global = nextpage_get_option( 'footer_style', 'footer-1' ); //for globally

			$nextpage_footer_style = get_post_meta( get_queried_object_id(), 'nextpage_page_meta', true ); // only for page

			if ( is_page() && isset( $nextpage_footer_style ) && ! empty( $nextpage_footer_style ) ) {
				if ( isset( $nextpage_footer_style['footer_type'] ) && 'footer-builder' == $nextpage_footer_style['footer_type'] ) {
					get_template_part( 'template-parts/footer/footer-builder' );
				} else {
					get_template_part( 'template-parts/footer/' . $nextpage_footer_style['nextpage_footer_style'] );
				}
			} else {
				if ( 'footer-builder' == nextpage_get_option( 'footer_type', 'premade-footer' ) ) {
					get_template_part( 'template-parts/footer/footer-builder' );
				} else {
					get_template_part( 'template-parts/footer/' . $nextpage_footer_global );
				}
			}

		}

		/*
		*  header builder
		*/
		function get_header_builder_library() {

			$pageslist = get_posts( array(
				'post_type'      => 'header-builder',
				'posts_per_page' => - 1
			) );

			$pagearray = array();
			if ( ! empty( $pageslist ) ) {
				foreach ( $pageslist as $page ) {
					$pagearray[ $page->ID ] = $page->post_title;
				}
			}

			return $pagearray;
		}


		/*
		 *  header builder
		 */
		function get_footer_builder_library() {

			$pageslist = get_posts( array(
				'post_type'      => 'footer-builder',
				'posts_per_page' => - 1
			) );

			$pagearray = array();
			if ( ! empty( $pageslist ) ) {
				foreach ( $pageslist as $page ) {
					$pagearray[ $page->ID ] = $page->post_title;
				}
			}

			return $pagearray;
		}

		/*
		* page layout
		*/
		public function page_layout() {

			global $post;
			$sidebar_status                          = is_active_sidebar( 'sidebar-1' ) ? true : false;
			$return_var['content_column_class']      = $sidebar_status ? 'col-lg-9' : 'col-lg-12';
			$return_var['sidebar_column_class']      = '';
			$return_var['nextpage_sidebar_activity'] = '';
			if ( is_page() ) {
				$page_meta_layout = get_post_meta( $post->ID, 'nextpage_page_meta', true );
			}
			if ( is_page() && ! empty( $page_meta_layout['nextpage_layout'] ) ) {
				$return_var['sidebar_column_class'] = ( 'left-sidebar' == $page_meta_layout['nextpage_layout'] ) ? 'order-first' : '';
				//if left-sidebar select change the order of column
				$return_var['content_column_class']      = ( 'full-width' == $page_meta_layout['nextpage_layout'] ) ? 'col-lg-12' : $return_var['content_column_class'];
				$return_var['nextpage_sidebar_activity'] = $page_meta_layout['nextpage_layout'];
			} //endif

			if ( is_home() && ! empty( nextpage_get_option( 'nextpage_blog_layout' ) ) ) {

				$return_var['sidebar_column_class'] = ( 'left-sidebar' == nextpage_get_option( 'nextpage_blog_layout' ) ) ? 'order-first' : '';
				//if left-sidebar select change the order of column
				$return_var['content_column_class']      = ( 'full-width' == nextpage_get_option( 'nextpage_blog_layout' ) ) ? 'col-lg-12' : $return_var['content_column_class'];
				$return_var['nextpage_sidebar_activity'] = nextpage_get_option( 'nextpage_blog_layout' );
			} //endif


			if ( is_single() && ! empty( nextpage_get_option( 'nextpage_single_page_layout' ) ) ) {

				$return_var['sidebar_column_class'] = ( 'left-sidebar' == nextpage_get_option( 'nextpage_single_page_layout' ) ) ? 'order-first' : '';
				//if left-sidebar select change the order of column
				$return_var['content_column_class']      = ( 'full-width' == nextpage_get_option( 'nextpage_single_page_layout' ) ) ? 'col-lg-12' : $return_var['content_column_class'];
				$return_var['nextpage_sidebar_activity'] = nextpage_get_option( 'nextpage_single_page_layout' );
			} //endif

			if ( is_archive() && ! empty( nextpage_get_option( 'nextpage_archive_layout' ) ) ) {

				$return_var['sidebar_column_class'] = ( 'left-sidebar' == nextpage_get_option( 'nextpage_archive_layout' ) ) ? 'order-first' : '';
				//if left-sidebar select change the order of column
				$return_var['content_column_class']      = ( 'full-width' == nextpage_get_option( 'nextpage_archive_layout' ) ) ? 'col-lg-12' : $return_var['content_column_class'];
				$return_var['nextpage_sidebar_activity'] = nextpage_get_option( 'nextpage_archive_layout' );

			} //

			if ( is_search() && ! empty( nextpage_get_option( 'nextpage_search_layout' ) ) ) {

				$return_var['sidebar_column_class'] = ( 'left-sidebar' == nextpage_get_option( 'nextpage_search_layout' ) ) ? 'order-first' : '';
				//if left-sidebar select change the order of column
				$return_var['content_column_class']      = ( 'full-width' == nextpage_get_option( 'nextpage_search_layout' ) ) ? 'col-lg-12' : $return_var['content_column_class'];
				$return_var['nextpage_sidebar_activity'] = nextpage_get_option( 'nextpage_search_layout' );
			} //


			return $return_var;
		}


	}//end class

}
