<?php

if (!class_exists('Solarva_Page')) {

    class Solarva_Page
    {
        public function get_site_loader(){

            $site_loader = solarva()->get_theme_opt( 'site_loader', 'off' );
            if($site_loader == 'on') { ?>
                <div id="pxl-loadding" class="pxl-loader">
                    <div class="pxl-loader-spinner">
                        <div class="pxl-loader-bounce1"></div>
                        <div class="pxl-loader-bounce2"></div>
                        <div class="pxl-loader-bounce3"></div>
                    </div>
                </div>
            <?php }
        }

        public function get_link_pages() {
            wp_link_pages( array(
                'before'      => '<div class="page-links">',
                'after'       => '</div>',
                'link_before' => '<span>',
                'link_after'  => '</span>',
            ) ); 
        }

        public function get_page_title(){
            $titles = $this->get_title();
            $pt_mode = solarva()->get_opt('pt_mode');
            $pt_mode_product = solarva()->get_opt('pt_mode_product');
            $ptitle_scroll_opacity = solarva()->get_opt('ptitle_scroll_opacity');
            $ptitle_layout = (int)solarva()->get_opt('ptitle_layout');
            $ptitle_layout_product = (int)solarva()->get_opt('ptitle_layout_product');
            if ($pt_mode == 'bd' && $ptitle_layout > 0 && class_exists('Pxltheme_Core') && is_callable( 'Elementor\Plugin::instance' )) { ?>
                <div id="pxl-page-title-elementor" class="<?php if($ptitle_scroll_opacity == true) { echo 'pxl-scroll-opacity'; } ?>">
                    <?php echo Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $ptitle_layout);?>
                </div>
                <?php 
            } elseif ($pt_mode_product == 'bd' && $ptitle_layout_product > 0 && class_exists('Pxltheme_Core') && is_callable( 'Elementor\Plugin::instance' ) ) { ?>
                <?php if(class_exists( 'WooCommerce' ) && is_shop() || class_exists( 'WooCommerce' ) && is_singular('product')) : ?>
                    <div id="pxl-page-title-elementor" class="pxl-page-title-shop <?php if($ptitle_scroll_opacity == true) { echo 'pxl-scroll-opacity'; } ?>">
                        <?php echo Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $ptitle_layout_product);?>
                    </div>
                <?php endif; ?>
            <?php } elseif($pt_mode == 'df') {
                $ptitle_breadcrumb_on = solarva()->get_opt( 'ptitle_breadcrumb_on', '1' ); 
                ?>
                <div id="pxl-page-title-default" class="pxl--parallax">
                    <div class="container">
                        <div class="row">
                            <div class="col-12">
                                <h1 class="pxl-page-title"><?php echo solarva_html($titles['title']) ?></h1>
                            </div>
                            <div class="col-12">
                                <?php if($ptitle_breadcrumb_on == '1') : ?>
                                    <?php $this->get_breadcrumb(); ?>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } 
        } 

        public function get_title() {
            $title = '';
            // Default titles
            if ( ! is_archive() ) {
                // Posts page view
                if ( is_home() ) {
                    // Only available if posts page is set.
                    if ( ! is_front_page() && $page_for_posts = get_option( 'page_for_posts' ) ) {
                        $title = get_post_meta( $page_for_posts, 'custom_title', true );
                        if ( empty( $title ) ) {
                            $title = get_the_title( $page_for_posts );
                        }
                    }
                    if ( is_front_page() ) {
                        $title = esc_html__( 'Blog', 'solarva' );
                    }
                } // Single page view
                elseif ( is_page() ) {
                    $title = get_post_meta( get_the_ID(), 'custom_title', true );
                    if ( ! $title ) {
                        $title = get_the_title();
                    }
                } elseif ( is_404() ) {
                    $title = esc_html__( '404 Error', 'solarva' );
                } elseif ( is_search() ) {
                    $title = esc_html__( 'Search results', 'solarva' );
                } elseif ( is_singular('lp_course') ) {
                    $title = esc_html__( 'Course', 'solarva' );
                } else {
                    $title = get_post_meta( get_the_ID(), 'custom_title', true );
                    if ( ! $title ) {
                        $title = get_the_title();
                    }
                }
            } else {
                $title = get_the_archive_title();
                if( (class_exists( 'WooCommerce' ) && is_shop()) ) {
                    $title = get_post_meta( wc_get_page_id('shop'), 'custom_title', true );
                    if(!$title) {
                        $title = get_the_title( get_option( 'woocommerce_shop_page_id' ) );
                    }
                }
            }

            return array(
                'title' => $title,
            );
        }

        public function get_breadcrumb(){

            if ( ! class_exists( 'CASE_Breadcrumb' ) )
            {
                return;
            }

            $breadcrumb = new CASE_Breadcrumb();
            $entries = $breadcrumb->get_entries();

            if ( empty( $entries ) )
            {
                return;
            }

            ob_start();

            foreach ( $entries as $entry )
            {
                $entry = wp_parse_args( $entry, array(
                    'label' => '',
                    'url'   => ''
                ) );

                $entry_label = $entry['label'];

                if(!empty($_GET['blog_title'])) {
                    $blog_title = $_GET['blog_title'];
                    $custom_title = explode('_', $blog_title);
                    foreach ($custom_title as $index => $value) {
                        $arr_str_b[$index] = $value;
                    }
                    $str = implode(' ', $arr_str_b);
                    $entry_label = $str;
                }

                if ( empty( $entry_label ) )
                {
                    continue;
                }

                echo '<li>';

                if ( ! empty( $entry['url'] ) )
                {
                    printf(
                        '<a class="breadcrumb-hidden" href="%1$s">%2$s<i class="flaticon-breadcrumb "></i></a>',
                        esc_url( $entry['url'] ),
                        esc_attr( $entry_label )
                    );
                }
                else
                {
                    $sg_post_title = solarva()->get_theme_opt('sg_post_title', 'default');
                    $sg_post_title_text = solarva()->get_theme_opt('sg_post_title_text');
                    if(is_singular('post') && $sg_post_title == 'custom_text' && !empty($sg_post_title_text)) {
                        $entry_label = $sg_post_title_text;
                    }
                    $sg_product_ptitle = solarva()->get_theme_opt('sg_product_ptitle', 'default');
                    $sg_product_ptitle_text = solarva()->get_theme_opt('sg_product_ptitle_text');
                    if(is_singular('product') && $sg_product_ptitle == 'custom_text' && !empty($sg_product_ptitle_text)) {
                        $entry_label = $sg_product_ptitle_text;
                    }
                    printf( '<span class="breadcrumb-entry" >%s</span>', esc_html( $entry_label ) );
                }

                echo '</li>';
            }

            $output = ob_get_clean();

            if ( $output )
            {
                printf( '<ul class="pxl-breadcrumb">%s</ul>', wp_kses_post($output));
            }
        }

        public function get_pagination( $query = null, $ajax = false ){

            if($ajax){
                add_filter('paginate_links', 'solarva_ajax_paginate_links');
            }

            $classes = array();

            if ( empty( $query ) )
            {
                $query = $GLOBALS['wp_query'];
            }

            if ( empty( $query->max_num_pages ) || ! is_numeric( $query->max_num_pages ) || $query->max_num_pages < 2 )
            {
                return;
            }

            $paged = $query->get( 'paged', '' );

            if ( ! $paged && is_front_page() && ! is_home() )
            {
                $paged = $query->get( 'page', '' );
            }

            $paged = $paged ? intval( $paged ) : 1;

            $pagenum_link = html_entity_decode( get_pagenum_link() );
            $query_args   = array();
            $url_parts    = explode( '?', $pagenum_link );

            if ( isset( $url_parts[1] ) )
            {
                wp_parse_str( $url_parts[1], $query_args );
            }

            $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
            $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
            $paginate_links_args = array(
                'base'     => $pagenum_link,
                'total'    => $query->max_num_pages,
                'current'  => $paged,
                'mid_size' => 1,
                'add_args' => array_map( 'urlencode', $query_args ),
                'prev_text' => '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="19" viewBox="0 0 18 19" fill="none">
                                    <path d="M14.9941 9.80566H3.00586C2.79492 9.79395 2.61621 9.71777 2.46973 9.57715C2.32324 9.43652 2.25 9.26074 2.25 9.0498C2.25 8.83887 2.32324 8.66309 2.46973 8.52246C2.61621 8.38184 2.79492 8.30566 3.00586 8.29395H14.9941C15.2051 8.30566 15.3838 8.38184 15.5303 8.52246C15.6768 8.66309 15.75 8.83887 15.75 9.0498C15.75 9.26074 15.6768 9.43652 15.5303 9.57715C15.3838 9.71777 15.2051 9.79395 14.9941 9.80566ZM8.0332 13.0225C8.17383 13.1748 8.24414 13.3506 8.24414 13.5498C8.24414 13.749 8.16797 13.9219 8.01562 14.0684C7.86328 14.2148 7.69043 14.2881 7.49707 14.2881C7.30371 14.2881 7.13086 14.2178 6.97852 14.0771L2.47852 9.57715C2.32617 9.4248 2.25 9.24902 2.25 9.0498C2.25 8.85059 2.32617 8.6748 2.47852 8.52246L6.97852 4.02246C7.13086 3.88184 7.30371 3.81152 7.49707 3.81152C7.69043 3.81152 7.86328 3.88477 8.01562 4.03125C8.16797 4.17773 8.24414 4.35059 8.24414 4.5498C8.24414 4.74902 8.17383 4.9248 8.0332 5.07715L4.06055 9.0498L8.0332 13.0225Z" fill="#5C6972"/>
                                </svg>',
                'next_text' => '<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 19 19" fill="none">
                                    <path d="M3.50586 9.80566H15.4941C15.7051 9.79395 15.8838 9.71777 16.0303 9.57715C16.1768 9.43652 16.25 9.26074 16.25 9.0498C16.25 8.83887 16.1768 8.66309 16.0303 8.52246C15.8838 8.38184 15.7051 8.30566 15.4941 8.29395H3.50586C3.29492 8.30566 3.11621 8.38184 2.96973 8.52246C2.82324 8.66309 2.75 8.83887 2.75 9.0498C2.75 9.26074 2.82324 9.43652 2.96973 9.57715C3.11621 9.71777 3.29492 9.79395 3.50586 9.80566ZM10.4668 13.0225C10.3262 13.1748 10.2559 13.3506 10.2559 13.5498C10.2559 13.749 10.332 13.9219 10.4844 14.0684C10.6367 14.2148 10.8096 14.2881 11.0029 14.2881C11.1963 14.2881 11.3691 14.2178 11.5215 14.0771L16.0215 9.57715C16.1738 9.4248 16.25 9.24902 16.25 9.0498C16.25 8.85059 16.1738 8.6748 16.0215 8.52246L11.5215 4.02246C11.3691 3.88184 11.1963 3.81152 11.0029 3.81152C10.8096 3.81152 10.6367 3.88477 10.4844 4.03125C10.332 4.17773 10.2559 4.35059 10.2559 4.5498C10.2559 4.74902 10.3262 4.9248 10.4668 5.07715L14.4395 9.0498L10.4668 13.0225Z" fill="#5C6972"/>
                                </svg>',
                'before_page_number' => '<span>0',
                'after_page_number' => '</span>',
            );
            if($ajax){
                $paginate_links_args['format'] = '?page=%#%';
            }
            $links = paginate_links( $paginate_links_args );
            if ( $links ): ?>
                <nav class="pxl-pagination <?php echo esc_attr($ajax ? 'ajax' : ''); ?>">
                    <div class="pxl-pagination-links">
                        <?php pxl_print_html($links); ?>
                    </div>
                </nav>
            <?php endif;
        }
    }
}
