<?php
/* 
* Theme: PREMIUMPRESS CORE FRAMEWORK FILE
* Url: www.premiumpress.com
* Author: Mark Fail
*
* THIS FILE WILL BE UPDATED WITH EVERY UPDATE
* IF YOU WANT TO MODIFY THIS FILE, CREATE A CHILD THEME
*
* http://codex.wordpress.org/Child_Themes
*/
if (!defined('THEME_VERSION')) {	header('HTTP/1.0 403 Forbidden'); exit; }

global $CORE, $wp_query, $userdata; 

 
// LOAD HEADER 
if(!isset($GLOBALS['flag-elementor'])){
get_header(); 
}

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
 
$columns3       = 0;
$hasSidebar     = 0;
$searchLayout   = _ppt(array('design', 'search_layout'));
$searchFilters  = _ppt(array('design', 'search_filters')); 
$customperrow   = _ppt(array('searchcustom', 'perrow'));
 
$perrow = 4;
if(isset($_GET['map']) || isset($_GET['inline']) || isset($_GET['listview']) ){
  $perrow = 3;
}elseif(isset($_GET['3columns']) ){
  $perrow = 4;
}elseif(is_numeric($customperrow)){  
  $perrow = $customperrow;
} 

 
$cardLayout     = _ppt(array('searchcustom', 'cardlayout'));
if($cardLayout == ""){ 
	if(in_array(THEME_KEY, array("cp"))){ $cardLayout = "list";  }else{ $cardLayout = "grid"; }  
}   

if( isset($GLOBALS['flag-taxonomy']) && in_array($GLOBALS['flag-taxonomy-type'],array("store")) ){
  $hasSidebar= 1;
}

// TOGGLE BAR
$show_togglebar = 1;
if(_ppt(array('search', 'hidetogglebar')) == "1"){
$show_togglebar = 0;
}

// TOGGLE SEARCH
$search_toggle = 0;
if(isset($_GET['toggle']) || _ppt(array('design', 'search_toggle')) == "inline"  && !isset($_GET["inline"]) && !isset($_GET["3columns"])  && !isset($_GET["full"]) && !isset($_GET["map"]) && !isset($_GET["hidefilters"])  ){
  $search_toggle = 1;
  $show_togglebar = 0;
  $hasSidebar = 0;
  if(defined('WLT_DEMOMODE') && !in_array(THEME_KEY,array("dt")) ){
  $perrow = 5;
  }
}
// 3 COLUMNS
if(_ppt(array('design', 'searchcolumns')) == 3 || isset($_GET['3columns'])){
  $columns3 = 1;
  $hasSidebar = 1;
}

if( ( $searchLayout == "sidebar" || $cardLayout == "list" || _ppt(array('design', 'customsidebar')) == 1 ) && !isset($_GET['toggle']) ){
	$hasSidebar = 1;
}

if( ( isset($_GET['full']) || ( defined('WLT_DEMOMODE') && !isset($_GET["inline"]) && !isset($_GET["3columns"]) && !isset($_GET['toggle'])  ) && $searchLayout !="sidebar"  )){
    $hasSidebar = 0;
    $perrow = 4;
} 
 
if(isset($_GET['inline']) || isset($_GET['3columns']) ){
  $hasSidebar = 1; 
} 

if(isset($_GET['inline']) && !isset($_GET['3columns']) ){
  $columns3 = 0; 
}

if(in_array(THEME_KEY,array("cp")) || isset($_GET['map']) || isset($_GET['inline']) ){
  $cardLayout = "list";
  $hasSidebar = 1;
}


if(isset($_GET['listview'])  ){
  $hasSidebar = 1; 
  $cardLayout = "list";
}




  
$GLOBALS['search_settings'] = array(

  "show_sidebar"  => $hasSidebar,
  "show_3columns" => $columns3,

  "show_toggle_search" => $search_toggle,

  "show_togglebar" =>  $show_togglebar,
  
  "layout"        => $searchLayout,
  "show_filters"  => $searchFilters,

  "cardlayout"    => $cardLayout,
  "perrow"        => $perrow,

);  
 
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

if($search_toggle){

}elseif(isset($GLOBALS['flag-taxonomy']) && in_array($GLOBALS['flag-taxonomy-type'],array("store")) ){

 _ppt_template( 'search/search-taxonomy-top-store' ); 

}elseif(isset($GLOBALS['flag-taxonomy']) && in_array($GLOBALS['flag-taxonomy-type'],array("country")) ){

 _ppt_template( 'search/search-taxonomy-top-country' ); 

}elseif(isset($GLOBALS['flag-taxonomy']) && !in_array($GLOBALS['flag-taxonomy-type'],array("store","country",'dagender'))  ){ 

_ppt_template( 'search/search-taxonomy-top-others' ); 
 
 }
 
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

 
?> 

<?php if($search_toggle){ _ppt_template( 'search/search-toggle-search' );  }   ?>    
 
<section class="section-40 mt-1 <?php if($hasSidebar == "1" && $columns3){ ?> fullcontainer<?php } ?>">

  <div class="container main-container">
  
    <div class="row">
     
    <?php if( $hasSidebar == "1"){  ?>

      <div class="col-md-12"><?php _ppt_template( 'search/search-sponsored' ); ?></div> 

      <div class="col-md-12"><?php _ppt_template( 'search/search-filters' );  ?></div>  
       
      <div class="col-md-12 <?php if($columns3){ echo "col-lg-2"; }else{ echo "col-lg-3"; } ?>" >
      
        <?php if(!$CORE->isMobileDevice()){ _ppt_template( 'sidebar' ); } // stops duplicate filters ?>
        
      </div> 
      
    <?php } ?>         
      
      <div class="<?php if($hasSidebar == "1"){ ?>col-md-12 <?php if($columns3){ echo "col-lg-8"; }else{ echo "col-lg-9"; } ?> <?php }else{ ?>col<?php } ?>">
      
        <div class="row px-0">
        
        <?php if(_ppt(array('maps','enable')) == 1  && (_ppt(array('design', 'search_map')) == 1 || isset($_GET['map']) ) ){ ?> 
          
          <?php _ppt_template( 'search/search-map' ); ?>
          
          <?php } ?>
          
          <div class="col-12">
             
            <?php if(!isset($GLOBALS['flag-taxonomy']) ){ echo $CORE->ADVERTISING("display_banner", "search_top" ); } ?> 
           
            <?php if(!isset($GLOBALS['flag-hide-search-top']) ){ ?>
            
              <?php if($hasSidebar != "1"){ _ppt_template( 'search/search-sponsored' ); } ?>   
            
            <?php } ?>  
              
            <?php  if($hasSidebar != "1"){ _ppt_template( 'search/search-filters' );  }   ?> 

            <?php _ppt_template( 'search/search-togglebar' ); ?>          
             
			      <?php _ppt_template( 'search/search-results' ); ?>
            
            <?php _ppt_template( 'search/search-taxonomy-bot' );  ?>
            
            <?php echo $CORE->ADVERTISING("display_banner", "search_bottom" ); ?> 
             
          </div>
          
        </div> 
        
      </div>  

      <?php if($hasSidebar == "1" && $columns3){ ?>
        
        <div class="col-md-2 hide-mobile">
          
        <?php _ppt_template( 'search/search-sidebar-3' );  ?>

        </div>

        <?php } ?>
      
    </div> 
    
  </div>
  
</section>

<?php

 
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
 

if(isset($_GET['backdata']) && strlen($_GET['backdata']) > 1){
  
  ?> 

    <script>
    jQuery(document).ready(function(){ 

      setTimeout(function(){

          jQuery('#_filter_data').val("<?php echo strip_tags($_GET['backdata']); ?>");
          _filter_newsearch();

        }, 3000);

    });
    </script>


  <?php 
}

?>
<div class="ppt-js-trigger-search-update"></div>
<script>

var pagenum = 0;
jQuery(document).ready(function(){ 

  jQuery(".ppt-js-trigger-search-update").on('click', function(){  
 
    if(pagenum > 1){
      history.pushState({},"Search Results",'<?php echo home_url(); ?>/?s=&backdata='+jQuery('#_filter_data').val());
    }
    pagenum++;
      
      
  });

 });

</script>

<?php

 
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
 
 
?>
 
<textarea style="width:100%; height:100px; display:none" id="_filter_data"></textarea>

<!-- per row/ per page / card layout -->
<input type="hidden" name="cardlayout" class="customfilter" id="filter-cardlayout"  data-type="select" data-key="cardlayout" value="<?php echo $cardLayout; ?>" />
<input type="hidden" name="perrow"  class="customfilter" data-type="select" data-key="perrow" value="<?php echo $perrow;  ?>">
<input type="hidden" name="perpage"  class="customfilter" data-type="select" data-key="perpage" value="<?php echo $CORE->LAYOUT("default_perpage", ""); ?>">
<!-- end -->
 
<?php if(isset($_GET['uid']) && is_numeric($_GET['uid']) ){ ?>
<input type="hidden" class="customfilter"  name="userid" data-type="text" data-key="userid" value="<?php echo esc_attr($_GET['uid']); ?>" >
<?php } ?>

<?php if(isset($_GET['history']) ){ ?>
<input type="hidden" class="customfilter"  name="history" data-type="text" data-key="history" value="1" >
<?php } ?>

<?php if(isset($_GET['favs']) ){ ?>
<input type="hidden" class="customfilter" id="filter-custom-favs"  name="favorites" data-type="text" data-key="favorites" value="1" >
<?php }else{ ?>
<input type="hidden" id="filter-custom-favs"  name="favorites" data-type="text" data-key="favorites" value="1" >
<?php } ?>

<?php if(isset($GLOBALS['flag-taxonomy']) && isset($GLOBALS['flag-taxonomy-id'])  ){ ?>
<input type="hidden" name="taxonomy"  class="customfilter" data-type="text" data-key="taxonomy" value="<?php echo $GLOBALS['flag-taxonomy-type']."-".$GLOBALS['flag-taxonomy-id']; ?>" >
<?php } ?> 

<?php if(isset($_GET['tax-store']) && is_numeric($_GET['tax-store']) ){ ?>
<input type="hidden" name="taxonomy"  class="customfilter" data-type="text" data-key="taxonomy" value="store-<?php echo $_GET['tax-store']; ?>" >
<?php } ?>

<?php if(isset($_GET['tax-custom']) ){ ?>
<input type="hidden" name="taxonomy"  class="customfilter" data-type="text" data-key="taxonomy" value="<?php echo $_GET['tax-custom']; ?>" >
<?php } ?>

<?php if(isset($_GET['s']) && strlen($_GET['s']) > 1  ){ ?>
<input type="hidden" name="s"  class="customfilter" data-type="text" data-key="keyword" value="<?php echo esc_attr($_GET['s']); ?>">
<?php } ?>

<?php if( _ppt(array('searchcustom', 'with_images')) == 1){ ?>
<input type="hidden" name="images"  class="customfilter" data-type="text" data-key="images" value="1">
<?php } ?>

<?php if(isset($_GET['city']) && strlen($_GET['city']) > 2 ){ ?>
<input type="hidden" name="newcity"  class="customfilter" data-type="text" data-key="newcity" value="<?php echo esc_attr($_GET['city']); ?>" >
<?php } ?> 

<?php if(isset($_GET['user']) ){ ?>
<input type="hiddenxxx" name="usersearch"  class="customfilter" data-type="text" data-key="usersearch" value="100">
<?php } ?>

<?php if(isset($_GET['make']) ){ ?>
<input type="hidden" name="make"  class="customfilter" data-type="text" data-key="filter-make" value="<?php echo $_GET['make']; ?>">
<?php } ?>

<?php if(isset($_GET['zipcode']) && strlen($_GET['zipcode']) > 1 ){ ?>
<input type="hidden" value="<?php echo esc_attr($_GET['zipcode']); ?>"   >
<?php } ?>


<?php if($columns3){ ?>
<input type="hidden" name="sidebar"  class="customfilter" data-type="text" data-key="sidebar" value="3">
<?php }elseif($hasSidebar){ ?>
<input type="hidden" name="sidebar"  class="customfilter" data-type="text" data-key="sidebar" value="1">
<?php } ?>


<?php 

if(isset($_GET['s'])){
	foreach($_GET as $k => $v){
	
		if($v == ""){ continue; }
		
		if(substr($k,0,3) == "tax"){
		
		if($cardLayout == "list" && _ppt(array('lst','makemodels')) != "1" &&  str_replace("tax_","",str_replace("tax-","",$k)) == "listing"){ continue; }
		
		?>         
        <input type="hidden" name="catid[]"  class="customfilter" 
        data-key="taxonomy" 
        data-custom-text="<?php echo str_replace("tax_","",str_replace("tax-","",$k)); ?>" 
        data-type="custom" 
        custom-data-value="<?php echo substr($k,4,100)."-".$v; ?>" 
        data-value="<?php echo str_replace("tax-","",$k)."-".$v; ?>" 
        value="<?php echo str_replace("tax-","",$k)."-".$v; ?>">
        <?php
		
		}elseif(substr($k,0,6) == "price1" && is_numeric($v) ){
		
		?>         
         
        
        <input type="hidden" name="price1"  
        custom-data-value="<?php echo $v; ?>" 
        class="customfilter" 
        data-key="text" 
        data-value="<?php echo $v; ?>" 
        data-custom-text="<?php echo __("Min. Price","premiumpress"); ?>"  
        data-type="custom-text" value="<?php echo $v; ?>">
        
        <?php
		
		}elseif(substr($k,0,6) == "price2" && is_numeric($v) ){
		
		?>   
        
        <input type="hidden" name="price2"   
        custom-data-value="<?php echo $v; ?>" 
        class="customfilter" 
        data-key="text" 
        data-value="<?php echo $v; ?>" 
        data-custom-text="<?php echo __("Max. Price","premiumpress"); ?>"  
        data-type="custom-text" value="<?php echo $v; ?>">
        
        <?php
		
		
		}elseif(substr($k,0,6) == "price2"){
		
		
		}
	}
}

?>


<?php 
if(!isset($GLOBALS['flag-taxonomy']) && !isset($_GET['tax-dagender']) && THEME_KEY == "da" && $userdata->ID ){
 			 
	$seek2 = get_user_meta($userdata->ID,'da-seek2',true);
	if(is_numeric($seek2) && $seek2 > 0){
	
	$term = get_term_by( 'id', $seek2, 'dagender' ); 
	if(isset($term->term_id) && is_numeric($term->term_id)){
	$k = "dagender"; $v = $seek2;
?>
        <input type="hidden" name="catid[]"  class="customfilter" 
        data-key="taxonomy" 
        data-custom-text="<?php echo $CORE->GEO("translation_tax_value", array($term->term_id, $term->name)); ?>" 
        data-type="custom-text" 
        custom-data-value="<?php echo $k."-".$v; ?>" 
        data-value="<?php echo $k."-".$v; ?>" 
        value="<?php echo $k."-".$v; ?>">
<?php 
	}
	}				  	
}


if(isset($_GET['age1']) && is_numeric($_GET['age1']) ){ ?>
<input type="hidden" 
data-key="age1"
data-value="<?php echo $_GET['age1']; ?>"
data-type="custom" 
data-custom-text-value="<?php echo __("Age","premiumpress"); ?> <?php echo $_GET['age1']; ?>+" 

name="age"  class="customfilter" data-type="text"  value="<?php echo $_GET['age1']; ?>">
<input type="hidden" name="age2"  class="customfilter" data-type="text" data-key="age2" value="999999999">

<?php }elseif(isset($_GET['age']) && is_numeric($_GET['age']) ){ ?>
<input type="hidden" 
data-key="age1"
data-value="<?php echo $_GET['age']; ?>"
data-type="custom" 
data-custom-text-value="<?php echo __("Age","premiumpress"); ?> <?php echo $_GET['age']; ?>+" 

name="age"  class="customfilter" data-type="text"  value="<?php echo $_GET['age']; ?>">
<input type="hidden" name="age2"  class="customfilter" data-type="text" data-key="age2" value="999999999">

<?php } 



?>
 

<?php




if(is_tag()){ 
$tag_obj = $wp_query->get_queried_object();
?>
<input type="hidden" name="taxonomy"  class="customfilter" data-type="text" data-key="taxonomy" value="<?php echo $tag_obj->taxonomy."-".$tag_obj->term_id; ?>" >
<?php }elseif(strpos(home_url( $wp->request ), "/tag/") !== false){ 

  $ll = explode("/tag/", home_url( $wp->request )); 
  if(isset($ll[1])){ 
  $tagss = get_term_by( "slug", $ll[1], 'post_tag');
 
    if(isset($tagss->term_id)){
    ?>
      <input type="hidden" name="taxonomy"  class="customfilter" data-type="text" data-key="taxonomy" value="post_tag-<?php echo $tagss->term_id; ?>" >

    <?php
    } 
  }

}?> 

<?php if(isset($_GET['fo']) && strlen($_GET['fo']) > 1){ ?>
<script>
jQuery(document).ready(function(){
setTimeout(function(){  
jQuery(".filterbox-<?php echo $_GET['fo']; ?>").trigger('click');
}, 2000);
});
</script>
<?php } ?>


<?php if(isset($_GET['zipcode']) && strlen($_GET['zipcode']) > 5 ){ ?>

<script>
jQuery(document).ready(function(){
	setTimeout(function(){  
		jQuery(".filterbox-distance").trigger('click');
		
		setTimeout(function(){  
		jQuery(".filter-keyword .form-control.customfilter").val("<?php echo strip_tags($_GET['zipcode']); ?>");
		}, 3000); 
		
	}, 2000);

});
</script>
<?php } ?>

<?php if($hasSidebar == "1" || $show_togglebar == 0){ ?>
  <script> jQuery(document).ready(function(){  	setTimeout(function(){   jQuery("#currentFilters").removeClass('my-4'); }); }, 2000);   </script>
  <?php } ?> 

 
<?php
 if(!isset($GLOBALS['flag-elementor'])){ get_footer(); } ?>