<?php
add_action('add_meta_boxes', 'estate_sidebar_meta');
add_action('save_post', 'estate_save_postdata', 1, 2);


if( !function_exists('estate_sidebar_meta') ):

function estate_sidebar_meta() {
    global $post;
    add_meta_box('wpestate-sidebar-post',       esc_html__('Sidebar Settings',  'wpestatetheme-core'), 'estate_sidebar_box', 'post');
    add_meta_box('wpestate-sidebar-page',       esc_html__('Sidebar Settings',  'wpestatetheme-core'), 'estate_sidebar_box', 'page');
    add_meta_box('wpestate-sidebar-property',   esc_html__('Sidebar Settings',  'wpestatetheme-core'), 'estate_sidebar_box', 'estate_property');
    add_meta_box('wpestate-sidebar-agent',      esc_html__('Sidebar Settings',  'wpestatetheme-core'), 'estate_sidebar_box', 'estate_agent');
    add_meta_box('wpestate-settings-post',      esc_html__('Post Settings',     'wpestatetheme-core'), 'estate_post_options_box', 'post', 'normal', 'default' );
    add_meta_box('wpestate-settings-page',      esc_html__('Page Settings',     'wpestatetheme-core'), 'estate_page_options_box', 'page', 'normal', 'default' );
    if( basename(get_page_template($post->ID))== 'property_list.php' ||  basename(get_page_template($post->ID))== 'property_list_half.php'){
        add_meta_box('wpestate-pro_list_adv',       esc_html__('Property List Advanced Options','wpestatetheme-core'), 'estate_prop_advanced_function', 'page', 'normal', 'low');
    }
    add_meta_box('wpestate-header',             esc_html__('Appearance Options','wpestatetheme-core'), 'estate_header_function', 'page', 'normal', 'low');
    add_meta_box('wpestate-header',             esc_html__('Appearance Options','wpestatetheme-core'), 'estate_header_function', 'post', 'normal', 'low');
    add_meta_box('wpestate-header',             esc_html__('Appearance Options','wpestatetheme-core'), 'estate_header_function', 'estate_agent', 'normal', 'low');
    add_meta_box('wpestate-header',             esc_html__('Appearance Options','wpestatetheme-core'), 'estate_header_function', 'estate_property', 'normal', 'low');
    
}
endif; // end   estate_sidebar_meta  




///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Header Option
///////////////////////////////////////////////////////////////////////////////////////////////////////////

if( !function_exists('estate_header_function') ):
function estate_header_function(){
    global $post;
    
    
    if( get_post_type ($post->ID) =='estate_property' ){
    
    $option = '';
    $title_values = array('yes', 'no');
    $post_title = get_post_meta($post->ID, 'post_show_title', true);
    foreach ($title_values as $value) {
        $option.='<option value="' . $value . '"';
        if ($value == $post_title) {
            $option.='selected="selected"';
        }
        $option.='>' . $value . '</option>';
    }

    print '<p class="meta-options">	
                <label for="post_show_title">'.esc_html__('Show Title:','wpestatetheme-core').' </label><br />
                <select id="post_show_title" name="post_show_title" style="width: 200px;">
                        ' . $option . '
                </select><br />
          </p>';

    
    
    
    $pages = get_pages(array(
            'meta_key' => '_wp_page_template',
            'meta_value' => 'page_property_design.php'
            ));
    
    
    $local_property_page_template_options   =   '<option value="">'.esc_html__('default','wpestatetheme-core').'</option>';
    $wp_estate_local_page_template          =  get_post_meta($post->ID, 'property_page_desing_local', true);
    
    foreach($pages as $page){
        $local_property_page_template_options.='<option value="'.$page->ID.'"'; 
        if($wp_estate_local_page_template==$page->ID){
            $local_property_page_template_options.=' selected="selected" ';
        }
        $local_property_page_template_options.=' >'.$page->post_title.'</option>';       
    }
    

    
    } 
    
    
    
    
    
    
    
    $header_array   =   array(
                            'global',
                            'none',
                            'image',
                            'theme slider',
                            'revolution slider',
                            'google map'
                            );
    
    $header_type    =   get_post_meta ( $post->ID, 'header_type', true);
    $header_select  =   '';
    
    foreach($header_array as $key=>$value){
       $header_select.='<option value="'.$key.'" ';
       if($key==$header_type){
           $header_select.=' selected="selected" ';
       }
       $header_select.='>'.$value.'</option>'; 
    }
    
      ////////// search form
    $cache_array                        =   array('global','no','yes');
    $use_float_search_form_local_select =   '';
    $search_float_type                  =   get_post_meta ( $post->ID, 'use_float_search_form_local_set', true);
    foreach($cache_array as $key=>$value){
       $use_float_search_form_local_select.='<option value="'.$key.'" ';
       if($key==$search_float_type){
           $use_float_search_form_local_select.=' selected="selected" ';
       }
       $use_float_search_form_local_select.='>'.$value.'</option>'; 
    }
   
    
    
    ////////// end logo header
    $cache_array                =   array('global','no','yes');
    $header_transparent         =   get_post_meta ( $post->ID, 'header_transparent', true);
    $header_transparent_select  =   '';
    
    foreach($cache_array as $key=>$value){
       $header_transparent_select.='<option value="'.$value.'" ';
       if($value==$header_transparent){
           $header_transparent_select.=' selected="selected" ';
       }
       $header_transparent_select.='>'.$value.'</option>'; 
    }
    
    $use_float_search_form_local = get_post_meta ( $post->ID, 'use_float_search_form_local', true);
    
    print'
    <p class="meta-options third-meta-options"><label for="header_type">'.esc_html__('Select header type','wpestatetheme-core').'  </label><br>
        <select name="header_type">
        '.$header_select.'
        </select>
    </p>';
  
  
    
    print'
    <p class="meta-options third-meta-options"><label for="header_transparent">'.esc_html__('Use transparent header','wpestatetheme-core').' </label><br>
        <select name="header_transparent">
        '.$header_transparent_select.'
        </select>
    </p>';
    
    
    
    print'
    <p class="meta-options third-meta-options"><label for="use_float_search_form_local_set">'.esc_html__('Use a float search form ?','wpestatetheme-core').'  </label><br>
        <select name="use_float_search_form_local_set">
        '.$use_float_search_form_local_select.'
        </select>
    </p>';
       
       
    print'
    <p class="meta-options third-meta-options"><label for="use_float_search_form_local">'.esc_html__('Distance betwen advanced search form and the top margin of the browser in px or % ( Ex 200px or 20%)  if "Float form" option is selected','wpestatetheme-core').'  </label><br>
         <input type="text" id="use_float_search_form_local" name="use_float_search_form_local" value="'.$use_float_search_form_local.'" size="40">
    </p>';
  

     
     
  
     
    estate_page_map_box($post);
    estate_page_slider_box($post);
    estate_prpg_design_option($post);
    }
    
endif;



if(!function_exists('estate_prpg_design_option')):
    function estate_prpg_design_option(){
        global $post;
        if(   'estate_property' == get_post_type() ){
            print '<p class="meta-options pblank">
            <h3 class="pblankh">'.esc_html__('Content options','wpestatetheme-core').'</h3>
            </p>';
            
            

            // slider type
            $slider_type                    =   array('global','vertical','horizontal','full width header','gallery');
            $local_pgpr_slider_type_symbol='';
            $local_pgpr_slider_type_status=  get_post_meta($post->ID, 'local_pgpr_slider_type', true);

            foreach($slider_type as $value){
                $local_pgpr_slider_type_symbol.='<option value="'.$value.'"';
                if ($local_pgpr_slider_type_status==$value){
                    $local_pgpr_slider_type_symbol.=' selected="selected" ';
                }
                $local_pgpr_slider_type_symbol.='>'.$value.'</option>';
            }
            
            //  content
            $content_type                       =   array('global','accordion','tabs');
            $local_pgpr_content_type_symbol     =   '';
            $local_pgpr_content_type_status     =  get_post_meta($post->ID, 'local_pgpr_content_type', true);

            foreach($content_type as $value){
                $local_pgpr_content_type_symbol.='<option value="'.$value.'"';
                if ($local_pgpr_content_type_status==$value){
                    $local_pgpr_content_type_symbol.=' selected="selected" ';
                }
                $local_pgpr_content_type_symbol.='>'.$value.'</option>';
            }

            print '

            <p class="meta-options third-meta-options"><label for="local_pgpr_slider_type">'.esc_html__('Slider Type ? ','wpestatetheme-core').' </label><br />
            <select id="local_pgpr_slider_type" name="local_pgpr_slider_type"  style="width: 200px;">
                '.$local_pgpr_slider_type_symbol.'
            </select> 

         
            
            <p class="meta-options third-meta-options"><label for="local_pgpr_content_type">'.esc_html__('Show Content as ','wpestatetheme-core').' </label><br />
            <select id="local_pgpr_content_type" name="local_pgpr_content_type"  style="width: 200px;">
                '.$local_pgpr_content_type_symbol.'
            </select> 

            </p>'; 
            }  
    }
endif;






///////////////////////////////////////////////////////////////////////////////////////////////////////////
///  Property Listing advanced options
///////////////////////////////////////////////////////////////////////////////////////////////////////////
if( !function_exists('estate_prop_advanced_function') ):
function estate_prop_advanced_function(){
    global $post;
    
    if( basename(get_page_template($post->ID))!= 'property_list.php' && basename(get_page_template($post->ID))!= 'property_list_half.php' ){
        esc_html_e('Only for "Properties List" page template ! ','wpestatetheme-core');
        return;
    }
    
    $args = array(
        'hide_empty'    => false 
    );  
    
    $actions_select     =   '';
    $categ_select       =   '';
    $taxonomy           =   'property_action_category';
    $tax_terms          =   get_terms($taxonomy,$args);

    $current_adv_filter_search_action = get_post_meta ( $post->ID, 'adv_filter_search_action', true);
    if($current_adv_filter_search_action==''){
        $current_adv_filter_search_action=array();
    }
    
    
    $all_selected='';
    if(!empty($current_adv_filter_search_action) && $current_adv_filter_search_action[0]=='all'){
      $all_selected=' selected="selected" ';  
    }
   
    $actions_select.='<option value="all" '.$all_selected.'>'.esc_html__('all','wpestatetheme-core').'</option>';
    if( !empty( $tax_terms ) ){                       
        foreach ($tax_terms as $tax_term) {
            $actions_select.='<option value="'.$tax_term->name.'" ';
            if( in_array  ( $tax_term->name,$current_adv_filter_search_action) ){
              $actions_select.=' selected="selected" ';  
            }
            $actions_select.=' />'.$tax_term->name.'</option>';      
        } 
    }
      
   
    
    //////////////////////////////////////////////////////////////////////////////////////////
    $taxonomy           =   'property_category';
    $tax_terms          =   get_terms($taxonomy,$args);

    $current_adv_filter_search_category = get_post_meta ( $post->ID, 'adv_filter_search_category', true);
    if($current_adv_filter_search_category==''){
        $current_adv_filter_search_category=array();
    }
    
    $all_selected='';
    if( !empty($current_adv_filter_search_category) && $current_adv_filter_search_category[0]=='all'){
      $all_selected=' selected="selected" ';  
    }
    
    $categ_select.='<option value="all" '.$all_selected.'>'.esc_html__('all','wpestatetheme-core').'</option>';
    if( !empty( $tax_terms ) ){                       
        foreach ($tax_terms as $tax_term) {
            $categ_select.='<option value="'.$tax_term->name.'" ';
            if( in_array  ( $tax_term->name, $current_adv_filter_search_category) ){
              $categ_select.=' selected="selected" ';  
            }
            $categ_select.=' />'.$tax_term->name.'</option>';      
        } 
    }
    
  
 //////////////////////////////////////////////////////////////////////////////////////////   
    
    $select_city='';
    $taxonomy = 'property_city';
    $tax_terms_city = get_terms($taxonomy,$args);
    $current_adv_filter_city = get_post_meta ( $post->ID, 'current_adv_filter_city', true);
    
    if($current_adv_filter_city==''){
        $current_adv_filter_city=array();
    }

    $all_selected='';
    if( !empty($current_adv_filter_city) && $current_adv_filter_city[0]=='all'){
      $all_selected=' selected="selected" ';  
    }
    
    $select_city.='<option value="all" '.$all_selected.' >'.esc_html__('all','wpestatetheme-core').'</option>';
    foreach ($tax_terms_city as $tax_term) {
        
        $select_city.= '<option value="' . $tax_term->name . '" ';
        if( in_array  ( $tax_term->name, $current_adv_filter_city) ){
              $select_city.=' selected="selected" ';  
        }
        $select_city.= '>' . $tax_term->name . '</option>';
    } 
  
     
 //////////////////////////////////////////////////////////////////////////////////////////   
    
    $select_area='';
    $taxonomy = 'property_area';
    $tax_terms_area = get_terms($taxonomy,$args);
    $current_adv_filter_area = get_post_meta ( $post->ID, 'current_adv_filter_area', true);
    if($current_adv_filter_area==''){
        $current_adv_filter_area=array();
    }
    
    $all_selected='';
    if(!empty($current_adv_filter_area) && $current_adv_filter_area[0]=='all'){
      $all_selected=' selected="selected" ';  
    }
    
    $select_area.='<option value="all" '.$all_selected.'>'.esc_html__('all','wpestatetheme-core').'</option>';
    foreach ($tax_terms_area as $tax_term) {
        $term_meta=  get_option( "taxonomy_$tax_term->term_id");
        $select_area.= '<option value="' . $tax_term->name . '" ';
        if( in_array  ( $tax_term->name, $current_adv_filter_area) ){
              $select_area.=' selected="selected" ';  
        }
        $select_area.= '>' . $tax_term->name . '</option>';
    } 

//////////////////////////////////   
    
    
    
    $show_filter_area_select='';
    $cache_array=array('yes','no');
    $show_filter_area  =   get_post_meta($post->ID, 'show_filter_area', true);
    
    foreach($cache_array as $value){
         $show_filter_area_select.='<option value="'.$value.'"';
         if ( $show_filter_area == $value ){
                 $show_filter_area_select.=' selected="selected" ';
         }
         $show_filter_area_select.='>'.$value.'</option>';
    }
    
    
    
    
    
    
    $show_featured_only_select='';
    $show_featured_only  =   get_post_meta($post->ID, 'show_featured_only', true);
    foreach($cache_array as $value){
       
         $show_featured_only_select.='<option value="'.$value.'" ';
         if ( $show_featured_only == $value ){
                 $show_featured_only_select.=' selected="selected" ';
         }
         $show_featured_only_select.='>'.$value.'</option>';
    }

    $listing_filter = get_post_meta($post->ID, 'listing_filter',true );

    $listing_filter_array=array(
                            "1"=>esc_html__('Price High to Low','wpestatetheme-core'),
                            "2"=>esc_html__('Price Low to High','wpestatetheme-core'),
                            "3"=>esc_html__('Newest first','wpestatetheme-core'),
                            "4"=>esc_html__('Oldest first','wpestatetheme-core'),
                            "5"=>esc_html__('Bedrooms High to Low','wpestatetheme-core'),
                            "6"=>esc_html__('Bedrooms Low to high','wpestatetheme-core'),
                            "7"=>esc_html__('Bathrooms High to Low','wpestatetheme-core'),
                            "8"=>esc_html__('Bathrooms Low to high','wpestatetheme-core'),
                            "0"=>esc_html__('Default','wpestatetheme-core')
                            );
    
    $property_list_second_content = get_post_meta($post->ID, 'property_list_second_content', true);
    print '
     *press CTRL for multiple selection
     <table>
     <tr>
    <td width="33%" valign="top" align="left">
        <p class="meta-options">
            <label   for="filter_search_action[]">Pick actions</label> </br>
            <select  name="adv_filter_search_action[]"  multiple="multiple" style="width:250px;" >
            '.$actions_select.'
             </select>
        </p>
    </td>
    
    <td width="33%" valign="top" align="left">
        <p class="meta-options">
           <label for="adv_filter_search_category[]">Pick category</label> </br>
           <select  name="adv_filter_search_category[]"  multiple="multiple" style="width:250px;" >
           '.$categ_select.'
           </select>
        </p>
    </td>
    
    </tr>
    

    <tr>
        <td width="33%" valign="top" align="left">
            <p class="meta-options">
                <label for="current_adv_filter_city[]">Pick City</label> </br>
                <select  name="current_adv_filter_city[]"  multiple="multiple" style="width:250px;" >
                '.$select_city.'
                </select>
            </p>
        </td>
        <td width="33%" valign="top" align="left">
            <p class="meta-options">
               <label for="current_adv_filter_area[]">Pick Area</label> </br>
                <select  name="current_adv_filter_area[]"  multiple="multiple" style="width:250px;" >
                '.$select_area.'
                </select>
            </p>
        </td>

    </tr>
    
    <tr>
        <td width="33%" valign="top" align="left">
            <p class="meta-options">
               <label for="listing_filter_div">Default sort ?</label><br />
               <select id="listing_filter_div" name="listing_filter"  style="width:250px;">';
               foreach($listing_filter_array as $key=>$value){
                  print '<option  value="'.$key.'" ';
                      if($key==$listing_filter){
                          print ' selected="selected" ';
                      }
                  print '>'.$value.'</option>'; 
               }        
               print '
               </select>
            </p>
        </td>
        <td width="33%" valign="top" align="left">
            <p class="meta-options">
               <label for="show_featured_only">Show featured only </label><br />
                <select id="show_featured_only"  name="show_featured_only" style="width:250px;" >
                ' .$show_featured_only_select . '
                </select>
            </p>
        </td>

    </tr>
    
    <tr>
        <td width="33%" valign="top" align="left">
            <p class="meta-options">
                <label for="show_filter_area">Show filter area</label><br />
              <select id="show_filter_area"  name="show_filter_area" style="width:250px;" >
              ' .$show_filter_area_select . '
              </select>
            </p>
        </td>
        <td width="33%" valign="top" align="left">
            <p class="meta-options">
              
             
            </p>
        </td>

    </tr>
    
    </table>
    
    <div>      
        <label for="property_list_second_content">Content that comes after property list.If you want to use shortcodes just create your page in the original content area and them copy paste it from text mode.</label><br />
       
        ';
         wp_editor( $property_list_second_content, 'property_list_second_content' ); 
        print'
    </div>
    
 
     
<style media="screen" type="text/css">

.adv_prop_container{
float:left;
width:22%;
margin-right:10px;
}

</style>
';
}

endif;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///  Listing options
///////////////////////////////////////////////////////////////////////////////////////////////////////////

if( !function_exists('estate_listing_options') ):
  function estate_listing_options(){

      
    global $post;
    if ( 'property_list.php'== basename( get_page_template() )){
   
        $listing_action  =   get_post_meta($post->ID, 'listing_action', true);
        $listing_categ   =   get_post_meta($post->ID, 'listing_categ', true);
        $listing_city    =   get_post_meta($post->ID, 'listing_city', true);
        $listing_area    =   get_post_meta($post->ID, 'listing_area', true);
      
        $args = array(
        'hide_empty'    => false  
        ); 

        $taxonomy = 'property_action_category';
        $tax_terms = get_terms($taxonomy,$args);

        $taxonomy_categ = 'property_category';
        $tax_terms_categ = get_terms($taxonomy_categ,$args);

        $actions_select     =   '';
        $categ_select       =   '';


        ///////////////////////// actions
        if( !empty( $tax_terms ) ){                       
            foreach ($tax_terms as $tax_term) {
              $actions_select.='<option value="'.$tax_term->name.'" ';
              if ($tax_term->name == $listing_action ){
                   $actions_select.=' selected="selected" ';
              }
              $actions_select.=' >'.$tax_term->name.'</option>';
            } 
        }


        /////////////////////////categ
        
        if( !empty( $tax_terms_categ ) ){                       
            foreach ($tax_terms_categ as $categ) {
              $categ_select.='<option value="'.$categ->name.'" ';   
               if ($categ->name == $listing_categ ){
                   $categ_select.=' selected="selected" ';
              }
              $categ_select.='>'.$categ->name.'</option>';
            }
        }

        
        ///////////////////////// city 
        $select_city='';
        $taxonomy = 'property_city';
        $tax_terms = get_terms($taxonomy,$args);
        foreach ($tax_terms as $tax_term) {
           $select_city.= '<option value="' . $tax_term->name . '" ';
           if ( $tax_term->name  == $listing_city ){
                   $select_city.=' selected="selected" ';
              }
           $select_city.='>' . $tax_term->name . '</option>';
        }

        if ($select_city==''){
              $select_city.= '<option value="">No Cities</option>';
        }

        
        
        /////////////////////////area 
        $select_area='';
        $taxonomy = 'property_area';
        $tax_terms = get_terms($taxonomy,$args);

        foreach ($tax_terms as $tax_term) {
            $term_meta=  get_option( "taxonomy_$tax_term->term_id");
            $select_area.= '<option value="' . $tax_term->name . '" data-parentcity="' . $term_meta['cityparent'] . '" ';
            
             if ( $tax_term->name  == $listing_area ){
                   $select_area.=' selected="selected" ';
              }
      
            $select_area.= '>' . $tax_term->name . '</option>';
             
         }
   
        print '
        <p class="meta-options">
            <label for="listing_action">'.esc_html__('Action category','wpestatetheme-core').'</label><br />
            <select  name="listing_action" >
                <option value="all">'.esc_html__('All Listings','wpestatetheme-core').'</option>
                '.$actions_select.'
           </select>
        </p>


        <p class="meta-options">
        <label for="listing_categ">'.esc_html__('Pick Category','wpestatetheme-core').'</label><br />
            <select name="listing_categ"  >
                <option value="all">'.esc_html__('All Types','wpestatetheme-core').'</option>
                '. $categ_select.'
            </select>
        </p>

        <p class="meta-options">
            <label for="listing_city">'.esc_html__('Pick City','wpestatetheme-core').'</label><br />
            <select  name="listing_city"  >
                <option value="all">'.esc_html__('All Cities','wpestatetheme-core').'</option>
                '. $select_city.'
             </select>
        </p>

        <p class="meta-options">
            <label for="listing_area">'.esc_html__('Pick Area','wpestatetheme-core').'</label><br />
            <select  name="listing_area">
                <option data-parentcity="*" value="all">'.esc_html__('All Areas','wpestatetheme-core').'</option>
                '.$select_area.'
            </select>
        </p>
         ';
      
      
        }else{
            print esc_html_e('These Options are available for "Property list" page template only!','wpestatetheme-core');
        }
   
  }
endif; // end   estate_listing_options





////////////////////////////////////////////////////////////////////////////////////////////////
// Manage Revolution Slider
////////////////////////////////////////////////////////////////////////////////////////////////

if( !function_exists('estate_page_slider_box') ):
function estate_page_slider_box($post) {
    global $post;
    $rev_slider           = get_post_meta($post->ID, 'rev_slider', true);
    print '  
    <p class="meta-options pblank">
        <h3 class="pblankh">'.esc_html__('Options for Revolution Slider (if Header Type "revolution slider" is selected)','wpestatetheme-core').'</h3>
    </p>
    <p class="meta-options">	
        <label for="page_custom_lat">'.esc_html__('Revolution Slider Name','wpestatetheme-core').'</label><br />
        <input type="text" id="rev_slider" name="rev_slider" size="40" value="'.$rev_slider.'">
    </p>
    ';
}
endif; // end   estate_page_slider_box  


////////////////////////////////////////////////////////////////////////////////////////////////
// Manage Google Maps
////////////////////////////////////////////////////////////////////////////////////////////////
if( !function_exists('estate_page_map_box') ): 
function estate_page_map_box($post) {
    global $post;
    $page_lat           = get_post_meta($post->ID, 'page_custom_lat', true);
    $page_long          = get_post_meta($post->ID, 'page_custom_long', true);
    $page_custom_image  = get_post_meta($post->ID, 'page_custom_image', true);
    $page_custom_zoom   = get_post_meta($post->ID, 'page_custom_zoom', true);
    $min_height         = intval( esc_html(get_post_meta($post->ID, 'min_height', true)) );
   
    $cache_array        = array('yes','no');
    $keep_min_symbol    = '';
    $keep_min_status    = esc_html ( get_post_meta($post->ID, 'keep_min', true) );

    foreach($cache_array as $value){
            $keep_min_symbol.='<option value="'.$value.'"';
            if ($keep_min_status==$value){
                    $keep_min_symbol.=' selected="selected" ';
            }
            $keep_min_symbol.='>'.$value.'</option>';
    }
    
    if ($page_custom_zoom==''){
        $page_custom_zoom=15;
    }

    
    print '
        <p class="meta-options pblank">
            <h3 class="pblankh">'.esc_html__('Options for Google Maps (if Header Type "google map" is selected)','wpestatetheme-core').'</h3>
        </p>';
    
    if( get_post_type($post->ID)!="estate_property" ){
        print '
       
        <p class="meta-options">
            '.esc_html__('  Leave these blank in order to get the general map settings.','wpestatetheme-core').'
        </p>
        
        <p class="meta-options third-meta-options">	
            <label for="page_custom_lat">'.esc_html__('Map - Center point  Latitudine: ','wpestatetheme-core').'</label><br />
            <input type="text" id="page_custom_lat" name="page_custom_lat"  value="'.$page_lat.'">
        </p>

        <p class="meta-options third-meta-options">	
            <label for="page_custom_long">'.esc_html__('Map - Center point  Longitudine: ','wpestatetheme-core').'</label><br />
            <input type="text" id="page_custom_long" name="page_custom_long"  value="'.$page_long.'">
        </p>



        <p class="meta-options third-meta-options">
            <label for="page_custom_zoom">'.esc_html__('Zoom Level for map (1-20)','wpestatetheme-core').'</label><br />
            <select name="page_custom_zoom" id="page_custom_zoom">';

            for ($i=1;$i<21;$i++){
                print '<option value="'.$i.'"';
                if($page_custom_zoom==$i){
                    print ' selected="selected" ';
                }
                print '>'.$i.'</option>';
            }
            print'
            </select>
        </p>';
    }
 
    print'
        <p class="meta-options third-meta-options">
            <label for="min_height">'.esc_html__('Height of the map','wpestatetheme-core').'</label><br />
            <input id="min_height" type="text"  name="min_height" value="'.$min_height.'" />
        </p>

       
    
        <p class="meta-options pblank">
            <h3 class="pblankh">'.esc_html__('Options for Static Image  (if Header Type "image" is selected)','wpestatetheme-core').'</h3>
        </p>
     
        <p class="meta-options ">
             <label for="page_custom_image">'.esc_html__('Header Image','wpestatetheme-core').'</label><br />
             <input id="page_custom_image" type="text" size="36" name="page_custom_image" value="'.$page_custom_image.'" />
             <input id="page_custom_image_button" type="button"   size="40" class="upload_button button" value="'.esc_html__('Upload Image','wpestatetheme-core').'" />
        </p>
        
        <p class="meta-options pblank">
        </p>';
}
endif; // end   estate_page_map_box 


////////////////////////////////////////////////////////////////////////////////////////////////
// Manage Custom Header of the page
////////////////////////////////////////////////////////////////////////////////////////////////
if( !function_exists('estate_page_map_box_agent') ):
function estate_page_map_box_agent($post) {
    global $post;
    $page_lat           = get_post_meta($post->ID, 'page_custom_lat', true);
    $page_long          = get_post_meta($post->ID, 'page_custom_long', true);
    $page_custom_image  = get_post_meta($post->ID, 'page_custom_image', true);
    $page_custom_zoom  = get_post_meta($post->ID, 'page_custom_zoom', true);
    
    if ($page_custom_zoom==''){
        $page_custom_zoom=15;
    }
    
    print '
   
    <p class="meta-options third-meta-options">
        <label for="page_custom_image">'.esc_html__('Replace Map with this image','wpestatetheme-core').'</label><br />
        <input id="page_custom_image" type="text" size="36" name="page_custom_image" value="'.$page_custom_image.'" />
	<input id="page_custom_image_button" type="button"   size="40" class="upload_button button" value="'.__('Upload Image','wpestatetheme-core').'" />
     </p>
     
     <p class="meta-options third-meta-options">
       <label for="page_custom_zoom">'.esc_html__('Zoom Level for map (1-20)','wpestatetheme-core').'</label><br />
       <select name="page_custom_zoom" id="page_custom_zoom">';
      
      for ($i=1;$i<21;$i++){
           print '<option value="'.$i.'"';
           if($page_custom_zoom==$i){
               print ' selected="selected" ';
           }
           print '>'.$i.'</option>';
       }
        
     print'
       </select>
     <p>
    ';
     
}
endif; // end   estate_page_map_box_agent  



////////////////////////////////////////////////////////////////////////////////////////////////
// Manage page options
////////////////////////////////////////////////////////////////////////////////////////////////
if( !function_exists('estate_page_options_box') ):
function estate_page_options_box($post) {
    global $post;

    $page_title = get_post_meta($post->ID, 'page_show_title', true);
    $selected_no = $selected_yes = '';

    if ($page_title == 'no') {
        $selected_no = 'selected="selected"';
    } else {
        $selected_yes = 'selected="selected"';
    }

    if ($page_title != '') {
        $page_title_select = '<option value="' . $page_title . '" selected="selected">' . $page_title . '</option>';
    }

    print '
    <p class="meta-options">	
    <label for="page_show_title">'.esc_html__('Show Title: ','wpestatetheme-core').'</label><br />
    <select id="page_show_title" name="page_show_title" style="width: 200px;">
            <option value="yes" ' . $selected_yes . '>yes</optionpage_show_title>
            <option value="no" ' . $selected_no . '>no</option>
    </select></p>';
}
endif; // end   estate_page_options_box  


////////////////////////////////////////////////////////////////////////////////////////////////
// Manage post options
////////////////////////////////////////////////////////////////////////////////////////////////
if( !function_exists('estate_post_options_box') ):
function estate_post_options_box($post) {
    wp_nonce_field(plugin_basename(__FILE__), 'estate_property_noncename');
    global $post;

    $option = '';
    $title_values = array('yes', 'no');
    $post_title = get_post_meta($post->ID, 'post_show_title', true);
    foreach ($title_values as $value) {
        $option.='<option value="' . $value . '"';
        if ($value == $post_title) {
            $option.='selected="selected"';
        }
        $option.='>' . $value . '</option>';
    }

    print '<p class="meta-options">	
                <label for="post_show_title">'.esc_html__('Show Title:','wpestatetheme-core').' </label><br />
                <select id="post_show_title" name="post_show_title" style="width: 200px;">
                        ' . $option . '
                </select><br />
          </p>';

    

    
}
endif; // end   estate_post_options_box  





////////////////////////////////////////////////////////////////////////////////////////////////
// Manage Sidebars per posts/page
////////////////////////////////////////////////////////////////////////////////////////////////
if( !function_exists('estate_sidebar_box') ):
function estate_sidebar_box($post) {

    // Use nonce for verification
    wp_nonce_field(plugin_basename(__FILE__), 'wpestate_sidebar_noncename');
    global $post;
    global $wp_registered_sidebars ;
    $sidebar_name   = get_post_meta($post->ID, 'sidebar_select', true);
    $sidebar_option = get_post_meta($post->ID, 'sidebar_option', true);
    
    $sidebar_values = array(   0=>'right', 
                               1=>'left', 
                               2=>'none');
    
    $option         = '';
    
    if( get_post_type ($post->ID) =='estate_property' ){
     //   $sidebar_values[]='global';
        array_unshift($sidebar_values, "global");
    }

    foreach ($sidebar_values as $key=>$value) {
        $option.='<option value="' . $value . '"';
        if ($value == $sidebar_option) {
            $option.=' selected="selected"';
        }
        $option.='>' . $value . '</option>';
    }

    print '   
    <p class="meta-options third-meta-options"><label for="sidebar_option">'.esc_html__('Where to show the sidebar: ','wpestatetheme-core').' </label><br />
        <select id="sidebar_option" name="sidebar_option" style="width: 200px;">
        ' . $option . '
        </select>
    </p>';
    
    print'
    <p class="meta-options  third-meta-options"><label for="sidebar_select">'.esc_html__('Select the sidebar: ','wpestatetheme-core').'</label><br />                  
        <select name="sidebar_select" id="sidebar_select" style="width: 200px;">';
        if( get_post_type ($post->ID) =='estate_property' ){
            print '<option value="">global</option>';
        }
        foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar) {
            print'<option value="' . ($sidebar['id'] ) . '"';
            if ($sidebar_name == $sidebar['id']) {
                print' selected="selected"';
            }
            print' >' . ucwords($sidebar['name']) . '</option>';
        }
        print '
        </select>
    </p>';
       
    
        
}
endif; // end   estate_sidebar_box  





////////////////////////////////////////////////////////////////////////////////////////////////
// Saving of custom data
////////////////////////////////////////////////////////////////////////////////////////////////
if( !function_exists('estate_save_postdata') ):
function estate_save_postdata($post_id) {
    global $post;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    
 
    ///////////////////////////////////// Check permissions   
    if(isset($_POST['post_type'])){       
            if ('page' == $_POST['post_type'] or 'post' == $_POST['post_type'] or 'estate_property' == $_POST['post_type']) {
                if (!current_user_can('edit_page', $post_id))
                    return;
            }
            else {
                if (!current_user_can('edit_post', $post_id))
                    return;
            }
     }

   
     
    $allowed_keys=array(
        'use_float_search_form_local_set',
        'use_float_search_form_local',
        'property_theme_slider',
        'property_has_subunits',
        'property_subunits_list',
        'property_subunits_list_manual',
        'image_to_attach',
        'sidebar_option',
        'sidebar_select',
        'post_show_title',
        'group_pictures',
        'embed_video_id',
        'embed_virtual_tour',
        'embed_video_type',      
        'page_show_title',
        'adv_filter_search_action',
        'adv_filter_search_category',
        'current_adv_filter_city',
        'current_adv_filter_area',
        'listing_filter',
        'show_featured_only',
        'show_filter_area',
        'header_type',
        'header_transparent',
        'page_custom_lat',
        'page_custom_long',
        'page_custom_zoom',
        'min_height',
        'max_height',
        'keep_min',
        'page_custom_image',
        'rev_slider',
        'local_pgpr_slider_type',
        'local_pgpr_content_type',
        'agent_position',
        'agent_email',
        'agent_phone',
        'agent_mobile',
        'agent_skype',
        'agent_facebook',
        'agent_twitter',
        'agent_linkedin',
        'agent_pinterest',
        'agent_instagram',
        'agent_website',
        'item_id',
        'item_price',
        'purchase_date',
        'buyer_id',
        'biling_period',
        'billing_freq',
        'pack_listings',
        'mem_list_unl',
        'pack_featured_listings',
        'pack_price',
        'pack_visible',
        'pack_stripe_id',
        'property_address',
        'property_zip',
        'property_state',
        'property_country',
        'property_status',
        'prop_featured',
        'property_price',
        'property_label',
        'property_label_before',
        'property_size',
        'property_lot_size',
        'property_rooms',
        'property_bedrooms',
        'property_bathrooms',
        'embed_video_type',
        'embed_video_id',
        'owner_notes',       
        'property_latitude',
        'property_longitude',
        'property_google_view',
        'google_camera_angle',
        'page_custom_zoom',
        'property_agent',
        'property_user',
        'use_floor_plans',
        'property_page_desing_local',
        'property_list_second_content'
    );
    
    $custom_fields = get_option( 'wp_estate_custom_fields', true);    
     if( !empty($custom_fields)){  
        $i=0;
        while($i< count($custom_fields) ){     
            $name =   $custom_fields[$i][0]; 
            $slug         =     wpestate_limit45(sanitize_title( $name )); 
            $slug         =     sanitize_key($slug); 
            $allowed_keys[]=     $slug;
            $i++;
       }
    }
    
    $feature_list       =   esc_html( get_option('wp_estate_feature_list') );
    $feature_list_array =   explode( ',',$feature_list);
    
    
    
    foreach($feature_list_array as $key => $value){
        $post_var_name=  str_replace(' ','_', trim($value) );
        $input_name =   wpestate_limit45(sanitize_title( $post_var_name ));
        $input_name =   sanitize_key($input_name);
        $allowed_keys[]=     $input_name;
    }
    
    
   
    foreach ($_POST as $key => $value) {
        if( !is_array ($value) ){
           
            if (in_array ($key, $allowed_keys)) {
                $postmeta = wp_filter_kses( $value ); 
                update_post_meta($post_id, sanitize_key($key), $postmeta );
            }
            
         
            
        }       
    }
    
      //
    global $allowedtags;
    $iframe = array( 'iframe' => array(
                            
                            'width'             => array(),
                            'height'            => array(),
                            'frameborder'       => array(),
                            'style'             => array(),
                            'allowFullScreen'   => array(),
                            'allowfullscreen'   => array(),
                            'src'               => array(),// add any other attributes you wish to allow
                          ) 
    );
    $allowed_html = array_merge( $allowedtags, $iframe );
    
    if ( isset($_POST['embed_virtual_tour'])){
        update_post_meta($post_id, 'embed_virtual_tour', (wp_kses (trim($_POST['embed_virtual_tour']),$allowed_html)) );
    }
    
    if(isset($_POST['property_list_second_content'])){
        update_post_meta($post_id, 'property_list_second_content', $_POST['property_list_second_content'] );
    }
    

    //////////////////////////////////////////////////////////////////
    //// change listing author id
    //////////////////////////////////////////////////////////////////
 
    if ( isset($_POST['property_user']) && isset($post->ID)){
           
        $current_id =   wpsestate_get_author($post_id);
        $new_user   =   intval($_POST['property_user']);
        

        if($current_id != $new_user && $new_user!=0 ){
            // change author
            $post = array(
                'ID'            => $post_id,
                'post_author'   => $new_user
            );

            wp_update_post($post ); 
       
        }

        
        
        wp_reset_postdata();
        wp_reset_query();
    }
    ///////////////////////////// end change author id
    
    //////////////////////////////////////////////////////////////////
    /// save floor plan
    //////////////////////////////////////////////////////////////////

    if(isset($_POST['plan_title'])){        
        update_post_meta($post->ID, 'plan_title',wpestate_sanitize_array ( $_POST['plan_title'] ) );
    }else{
        if(isset($post->ID)){
            update_post_meta($post->ID, 'plan_title','' );
        }
    }
     
    if(isset($_POST['plan_description'])){        
            update_post_meta($post->ID, 'plan_description',wpestate_sanitize_array ( $_POST['plan_description'] ) );
    }else{
        if(isset($post->ID)){
            update_post_meta($post->ID, 'plan_description','' );
        }
    }
     
    if(isset($_POST['plan_image_attach'])){        
            update_post_meta($post->ID, 'plan_image_attach',wpestate_sanitize_array ( $_POST['plan_image_attach'] ) );
    }else{
        if(isset($post->ID)){
            update_post_meta($post->ID, 'plan_image_attach','' );
        }
    }
    
    if(isset($_POST['plan_image'])){        
            update_post_meta($post->ID, 'plan_image',wpestate_sanitize_array ( $_POST['plan_image'] ) );
    }else{
        if(isset($post->ID)){
            update_post_meta($post->ID, 'plan_image','' );
        }
    }
    
    if(isset($_POST['plan_size'])){        
            update_post_meta($post->ID, 'plan_size',wpestate_sanitize_array ( $_POST['plan_size'] ) );
    }else{
        if(isset($post->ID)){
            update_post_meta($post->ID, 'plan_size','' );
        }
    }
   
    
      if(isset($_POST['plan_rooms'])){        
            update_post_meta($post->ID, 'plan_rooms',wpestate_sanitize_array ( $_POST['plan_rooms'] ) );
    }else{
        if(isset($post->ID)){
            update_post_meta($post->ID, 'plan_rooms','' );
        }
    }
    
      if(isset($_POST['plan_bath'])){        
            update_post_meta($post->ID, 'plan_bath',wpestate_sanitize_array ( $_POST['plan_bath'] ) );
    }else{
        if(isset($post->ID)){
            update_post_meta($post->ID, 'plan_bath','' );
        }
    }
    
      if(isset($_POST['plan_price'])){        
            update_post_meta($post->ID, 'plan_price',wpestate_sanitize_array ( $_POST['plan_price'] ) );
    }else{
        if(isset($post->ID)){
            update_post_meta($post->ID, 'plan_price','' );
        }
    }
    
    
    //////////////////////////////////////// end save floor plan

        
    if(isset($_POST['property_subunits_list']) && isset($post->ID) ){     
        $property_subunits_list = $_POST['property_subunits_list'];
        update_post_meta($post->ID, 'property_subunits_list', wpestate_sanitize_array( $property_subunits_list) );
        
        if(is_array($property_subunits_list)){
            foreach ($property_subunits_list as $key) {
                update_post_meta(intval($key), 'property_subunits_master',$post->ID );
            }
        }else{
            update_post_meta(intval($property_subunits_list), 'property_subunits_master',$post->ID );
        }
        
        
    }else{
        if( isset($post->ID) ){
            $already_childs = get_post_meta($post->ID, 'property_subunits_list', true) ;
            
            if(is_array($already_childs)){
                foreach ($already_childs as $key) {
                    delete_post_meta(intval($key), 'property_subunits_master','' );
                }
                update_post_meta($post->ID, 'property_subunits_list', '') ;
            }
        }
        
    }    
            
    
            
    
    if(isset($_POST['adv_filter_search_action'])){        
        update_post_meta($post->ID, 'adv_filter_search_action',wpestate_sanitize_array ( $_POST['adv_filter_search_action'] ) );
     }else{
         if(isset($post->ID)){
            update_post_meta($post->ID, 'adv_filter_search_action','' );
         }
     }
     
     if(isset($_POST['adv_filter_search_category'])){
        update_post_meta($post->ID, 'adv_filter_search_category', wpestate_sanitize_array ($_POST['adv_filter_search_category']) );
     }else{
         if(isset($post->ID)){
            update_post_meta($post->ID, 'adv_filter_search_category','' ); 
         } 
     }
     
     if(isset($_POST['current_adv_filter_city'])){
        update_post_meta($post->ID, 'current_adv_filter_city',wpestate_sanitize_array($_POST['current_adv_filter_city']) );
     }else{
         if(isset($post->ID)){
            update_post_meta($post->ID, 'current_adv_filter_city','' ); 
         } 
     }
     
     
     if(isset($_POST['current_adv_filter_area'])){
        update_post_meta($post->ID, 'current_adv_filter_area',wpestate_sanitize_array ($_POST['current_adv_filter_area']) );
     }else{
         if(isset($post->ID)){
            update_post_meta($post->ID, 'current_adv_filter_area','' ); 
         } 
     }
  
    $order=0;
    if( isset( $_POST['image_to_attach'] ) &&  isset($post->ID) ){
        $all_media = explode(',',$_POST['image_to_attach']);
       // print_r($all_media);
        
        if(is_array($all_media)){
            foreach($all_media as $value){
                $order++;
                $value=intval($value);
                if($value!=0){
                    wp_update_post( array(
                        'ID'            =>  $value,
                        'post_parent'   =>  $post->ID,
                        'menu_order'    =>  $order
                ));

                }

            }
        }
    }
     
    
    // theme slider
    if(isset($_POST['property_theme_slider']) ){
        $theme_slider   =   get_option( 'wp_estate_theme_slider', ''); 
        $is_slider      =   intval ( $_POST['property_theme_slider'] );
        
        if( isset($post->ID) ){
            if( $is_slider==1){
                if( is_array($theme_slider) && !in_array($post->ID, $theme_slider) ){
                    $theme_slider[]=$post->ID;
                }else{
                     $theme_slider[]=$post->ID; 
                }
            }else{
                if( is_array($theme_slider) ){
                    if(($key = array_search($post->ID, $theme_slider)) !== false) {
                        unset($theme_slider[$key]);
                    }
                }
            }
            update_option( 'wp_estate_theme_slider', $theme_slider);
        }
    }

    if( isset($post->ID) && isset($_POST['property_address'])){
        wpestate_update_hiddent_address_single($post->ID);
    }
    
   
    
}
endif; // end   estate_save_postdata  

if( !function_exists('wpestate_sanitize_array') ): 
    function wpestate_sanitize_array($original){
        $new_Array=array();
        $allowed_html=array();
        foreach($original as $key=>$value){
            $new_Array[sanitize_key($key)]=  wp_kses(esc_html($value),$allowed_html);
        }
        return $new_Array;
    }
endif;
?>