<?php
 

if (!function_exists('blog_query_filter')):
	function blog_query_filter($query) {
		
		$archive_order = apply_filters('archive_default_ordering',  'new');
		if($archive_order!='new' && !isset($_GET['sort_by'])){
			$_GET['sort_by'] = $archive_order;
		}
		
		if(isset($_GET['sort_by']) && trim($_GET['sort_by'])!='' && $query->is_main_query() && ($query->is_archive() || $query->is_home() || $query->is_search() || $query->is_author())){
			$swi_query = sanitize_text_field(trim($_GET['sort_by']));
			
			switch($swi_query){
				case 'new':
					$query->set('orderby', 'date');
					$query->set('order', 'DESC');
					break;
					
				case 'old':
					$query->set('orderby', 'date');
					$query->set('order', 'ASC');
					break;
					
				case 'views':
					$query->set('orderby', 'date');
					$query->set('order', 'ASC');
					break;
					
				case 'title_a_z':
					$query->set('orderby', 'title');
					$query->set('order', 'ASC');
					break;
				
				case 'title_z_a':
					$query->set('orderby', 'title');
					$query->set('order', 'DESC');
					break;	
					
				case 'most_viewed':
					do_action('most_viewed_query_blog', $query);
					break;
					 					
			}
		}
	}
endif;
if(!is_admin()){
   add_filter('pre_get_posts', 'blog_query_filter');
}

if (!function_exists('archive_style')) :
    function archive_style()
    {
        global $archive_style;

        if (isset($archive_style) && $archive_style != '') {
            return $archive_style;
        }

        $archive_style = '';

        if (!defined('PREFIX')) {
            define('PREFIX', TEXT_DOMAIN);
        }

        if(is_archive()){
            if(is_tax() || is_category()){
                $archive_style = get_term_meta(get_queried_object()->term_id, PREFIX . '_archive_loop_style', true);
            }elseif(is_author()){
				$archive_style = get_redux_option('_author_loop_style', 'alyssa');
			}
        }elseif(is_search()){
			$archive_style = get_redux_option('_search_loop_style', 'alyssa');
		}

        if($archive_style == '') {
            $archive_style = apply_filters('default_archive_loop_style',  'default' );
        }

        if(isset($_GET['archive_style']) && trim($_GET['archive_style']) !== ''){
            $archive_style = trim($_GET['archive_style']);
        }

        return $archive_style;
    }
endif;

if (!function_exists('get_nopaging_url')):
	function get_nopaging_url() {
		global $wp;
		$current_url = home_url( $wp->request );
		$position = strpos( $current_url , '/page' );
		$nopaging_url = ( $position ) ? substr( $current_url, 0, $position ) : $current_url;
		return add_query_arg( $wp->query_string, '', trailingslashit( $nopaging_url ));
	}
endif;


if ( !function_exists('blog_filter' ) ):
	function blog_filter($archive_style){
		
		global $wp_query;
		
		$archive_order = apply_filters('archive_default_ordering' , 'new');		
		$query_order = $archive_order;		
			
		if(isset($_GET['sort_by']) && $_GET['sort_by']!=''){
			$query_order = $_GET['sort_by'];
		}
		
		$all_sort = apply_filters('all_sort_query', array(
			'new'		=> esc_html__('Newest', TEXT_DOMAIN),
			'old'		=> esc_html__('Oldest', TEXT_DOMAIN), 
			'views'		=> esc_html__('Popular', TEXT_DOMAIN),					
			/*
			'title_a_z'	=> esc_html__('Alphabetical (A-Z)', TEXT_DOMAIN),
			'title_z_a'	=> esc_html__('Alphabetical (Z-A)', TEXT_DOMAIN), 
			*/			
		), 'default-archive-page');
		
		?>
		
		<nav class="overflow-auto d-flex mb-4"  >  
						<?php 
						$curr_URL = add_query_arg( array('paged' => '1'), get_nopaging_url());
						foreach($all_sort as $key => $value){
						?>
							<a class="btn btn-light text-nowrap mr-2 " href="<?php echo esc_url(add_query_arg(array('sort_by' => $key), $curr_URL));?>" title="<?php echo esc_attr($value)?>"> <?php echo esc_html($value)?> </a>
						<?php	
						}
						?>
					  
		</nav>
	<?php	
	}
endif;
add_action('filer_archive_posts', 'blog_filter', 10, 1);
