<?php
// register the custom post type
add_action( 'init', 'wpestate_create_agent_type',1);

if( !function_exists('wpestate_create_agent_type') ):

function wpestate_create_agent_type() {
    $rewrites =  get_option('wp_estate_url_rewrites');
    register_post_type( 'estate_agent',
            array(
                    'labels' => array(
                            'name'          => esc_html__( 'Agents','wpestatetheme-core'),
                            'singular_name' => esc_html__( 'Agent','wpestatetheme-core'),
                            'add_new'       => esc_html__('Add New Agent','wpestatetheme-core'),
            'add_new_item'          =>  esc_html__('Add Agent','wpestatetheme-core'),
            'edit'                  =>  esc_html__('Edit' ,'wpestatetheme-core'),
            'edit_item'             =>  esc_html__('Edit Agent','wpestatetheme-core'),
            'new_item'              =>  esc_html__('New Agent','wpestatetheme-core'),
            'view'                  =>  esc_html__('View','wpestatetheme-core'),
            'view_item'             =>  esc_html__('View Agent','wpestatetheme-core'),
            'search_items'          =>  esc_html__('Search Agent','wpestatetheme-core'),
            'not_found'             =>  esc_html__('No Agents found','wpestatetheme-core'),
            'not_found_in_trash'    =>  esc_html__('No Agents found','wpestatetheme-core'),
            'parent'                =>  esc_html__('Parent Agent','wpestatetheme-core')
                    ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => $rewrites[6]),
            'supports' => array('title', 'editor', 'thumbnail','comments'),
            'can_export' => true,
            'register_meta_box_cb' => 'wpestate_add_agents_metaboxes',
             'menu_icon'=> get_template_directory_uri().'/img/agents.png'    
            )
    );
    // add custom taxonomy
   
    // add custom taxonomy
    register_taxonomy('property_category_agent', array('estate_agent'), array(
        'labels' => array(
            'name'              => esc_html__('Agent Categories','wpestatetheme-core'),
            'add_new_item'      => esc_html__('Add New Agent Category','wpestatetheme-core'),
            'new_item_name'     => esc_html__('New Agent Category','wpestatetheme-core')
        ),
        'hierarchical'  => true,
        'query_var'     => true,
        'rewrite'       => array( 'slug' => $rewrites[7] )
        )
    );


    
    register_taxonomy('property_action_category_agent', 'estate_agent', array(
        'labels' => array(
            'name'              => esc_html__('Agent Action Categories','wpestatetheme-core'),
            'add_new_item'      => esc_html__('Add New Agent Action','wpestatetheme-core'),
            'new_item_name'     => esc_html__('New Agent Action','wpestatetheme-core')
        ),
        'hierarchical'  => true,
        'query_var'     => true,
        'rewrite'       => array( 'slug' => $rewrites[8] )
       )      
    );



    // add custom taxonomy
    register_taxonomy('property_city_agent','estate_agent', array(
        'labels' => array(
            'name'              => esc_html__('Agent City','wpestatetheme-core'),
            'add_new_item'      => esc_html__('Add New Agent City','wpestatetheme-core'),
            'new_item_name'     => esc_html__('New Agent City','wpestatetheme-core')
        ),
        'hierarchical'  => true,
        'query_var'     => true,
        'rewrite'       => array( 'slug' => $rewrites[9],'with_front' => false)
        )
    );




    // add custom taxonomy
    register_taxonomy('property_area_agent', 'estate_agent', array(
        'labels' => array(
            'name'              => esc_html__('Agent Neighborhood','wpestatetheme-core'),
            'add_new_item'      => esc_html__('Add New Agent Neighborhood','wpestatetheme-core'),
            'new_item_name'     => esc_html__('New Agent Neighborhood','wpestatetheme-core')
        ),
        'hierarchical'  => true,
        'query_var'     => true,
        'rewrite'       => array( 'slug' => $rewrites[10] )

        )
    );

    // add custom taxonomy
    register_taxonomy('property_county_state_agent', array('estate_agent'), array(
        'labels' => array(
            'name'              => esc_html__('Agent County / State','wpestatetheme-core'),
            'add_new_item'      => esc_html__('Add New Agent County / State','wpestatetheme-core'),
            'new_item_name'     => esc_html__('New Agent County / State','wpestatetheme-core')
        ),
        'hierarchical'  => true,
        'query_var'     => true,
        'rewrite'       => array( 'slug' =>  $rewrites[11] )

        )
    );
}
endif; // end   wpestate_create_agent_type  


////////////////////////////////////////////////////////////////////////////////////////////////
// Add agent metaboxes
////////////////////////////////////////////////////////////////////////////////////////////////
if( !function_exists('wpestate_add_agents_metaboxes') ):
function wpestate_add_agents_metaboxes() {	
  add_meta_box(  'estate_agent-sectionid', esc_html__( 'Agent Settings', 'wpestatetheme-core' ), 'estate_agent', 'estate_agent' ,'normal','default');
}
endif; // end   wpestate_add_agents_metaboxes  



////////////////////////////////////////////////////////////////////////////////////////////////
// Agent details
////////////////////////////////////////////////////////////////////////////////////////////////
if( !function_exists('estate_agent') ):
function estate_agent( $post ) {
    wp_nonce_field( plugin_basename( __FILE__ ), 'estate_agent_noncename' );
    global $post;

    print'
    <p class="meta-options">
    <label for="agent_position">'.esc_html__('Position:','wpestatetheme-core').' </label><br />
    <input type="text" id="agent_position" size="58" name="agent_position" value="'.  esc_html(get_post_meta($post->ID, 'agent_position', true)).'">
    </p>

    <p class="meta-options">
    <label for="agent_email">'.esc_html__('Email:','wpestatetheme-core').' </label><br />
    <input type="text" id="agent_email" size="58" name="agent_email" value="'.  esc_html(get_post_meta($post->ID, 'agent_email', true)).'">
    </p>

    <p class="meta-options">
    <label for="agent_phone">'.esc_html__('Phone: ','wpestatetheme-core').'</label><br />
    <input type="text" id="agent_phone" size="58" name="agent_phone" value="'.  esc_html(get_post_meta($post->ID, 'agent_phone', true)).'">
    </p>

    <p class="meta-options">
    <label for="agent_mobile">'.esc_html__('Mobile:','wpestatetheme-core').' </label><br />
    <input type="text" id="agent_mobile" size="58" name="agent_mobile" value="'.  esc_html(get_post_meta($post->ID, 'agent_mobile', true)).'">
    </p>

    <p class="meta-options">
    <label for="agent_skype">'.esc_html__('Skype: ','wpestatetheme-core').'</label><br />
    <input type="text" id="agent_skype" size="58" name="agent_skype" value="'.  esc_html(get_post_meta($post->ID, 'agent_skype', true)).'">
    </p>
    
                
    <p class="meta-options">
    <label for="agent_facebook">'.esc_html__('Facebook: ','wpestatetheme-core').'</label><br />
    <input type="text" id="agent_facebook" size="58" name="agent_facebook" value="'.  esc_html(get_post_meta($post->ID, 'agent_facebook', true)).'">
    </p>
    
    <p class="meta-options">
    <label for="agent_twitter">'.esc_html__('Twitter: ','wpestatetheme-core').'</label><br />
    <input type="text" id="agent_twitter" size="58" name="agent_twitter" value="'.  esc_html(get_post_meta($post->ID, 'agent_twitter', true)).'">
    </p>
    
    <p class="meta-options">
    <label for="agent_linkedin">'.esc_html__('Linkedin: ','wpestatetheme-core').'</label><br />
    <input type="text" id="agent_linkedin" size="58" name="agent_linkedin" value="'.  esc_html(get_post_meta($post->ID, 'agent_linkedin', true)).'">
    </p>
    
    <p class="meta-options">
    <label for="agent_pinterest">'.esc_html__('Pinterest: ','wpestatetheme-core').'</label><br />
    <input type="text" id="agent_pinterest" size="58" name="agent_pinterest" value="'.  esc_html(get_post_meta($post->ID, 'agent_pinterest', true)).'">
    </p>
    
    <p class="meta-options">
    <label for="agent_instagram">'.esc_html__('Instagram: ','wpestatetheme-core').'</label><br />
    <input type="text" id="agent_instagram" size="58" name="agent_instagram" value="'.  esc_html(get_post_meta($post->ID, 'agent_instagram', true)).'">
    </p>



    <p class="meta-options">
        <label for="agent_website">'.esc_html__('Website (without http): ','wpestatetheme-core').'</label><br />
        <input type="text" id="agent_website" size="58" name="agent_website" value="'.  esc_html(get_post_meta($post->ID, 'agent_website', true)).'">
    </p>
    ';            
}
endif; // end   estate_agent  




add_action('save_post', 'wpsx_5688_update_post', 1, 2);

if( !function_exists('wpsx_5688_update_post') ):
function wpsx_5688_update_post($post_id,$post){
    
    if(!is_object($post) || !isset($post->post_type)) {
        return;
    }
    
     if($post->post_type!='estate_agent'){
        return;    
     }
     
     if( !isset($_POST['agent_email']) ){
         return;
     }
     if('yes' ==  esc_html ( get_option('wp_estate_user_agent','') )){  
            $allowed_html   =   array();
            $user_id    = get_post_meta($post_id, 'user_meda_id', true);
            $email      = wp_kses($_POST['agent_email'],$allowed_html);
            $phone      = wp_kses($_POST['agent_phone'],$allowed_html);
            $skype      = wp_kses($_POST['agent_skype'],$allowed_html);
            $position   = wp_kses($_POST['agent_position'],$allowed_html);
            $mobile     = wp_kses($_POST['agent_mobile'],$allowed_html);
            $desc       = wp_kses($_POST['content'],$allowed_html);
            $image_id   = get_post_thumbnail_id($post_id);
            $full_img   = wp_get_attachment_image_src($image_id, 'property_listings');           
            $facebook   = wp_kses($_POST['agent_facebook'],$allowed_html);
            $twitter    = wp_kses($_POST['agent_twitter'],$allowed_html);
            $linkedin   = wp_kses($_POST['agent_linkedin'],$allowed_html);
            $pinterest  = wp_kses($_POST['agent_pinterest'],$allowed_html);
            $instagram  = wp_kses($_POST['agent_instagram'],$allowed_html);
            $agent_website  = wp_kses($_POST['agent_website'],$allowed_html);
            
            update_user_meta( $user_id, 'aim', '/'.$full_img[0].'/') ;
            update_user_meta( $user_id, 'phone' , $phone) ;
            update_user_meta( $user_id, 'mobile' , $mobile) ;
            update_user_meta( $user_id, 'description' , $desc) ;
            update_user_meta( $user_id, 'skype' , $skype) ;
            update_user_meta( $user_id, 'title', $position) ;
            update_user_meta( $user_id, 'custom_picture', $full_img[0]) ;
            update_user_meta( $user_id, 'facebook', $facebook) ;
            update_user_meta( $user_id, 'twitter', $twitter) ;
            update_user_meta( $user_id, 'linkedin', $linkedin) ;
            update_user_meta( $user_id, 'pinterest', $pinterest) ;
            update_user_meta( $user_id, 'instagram', $pinterest) ;
            update_user_meta( $user_id, 'website', $agent_website) ;
             
            update_user_meta( $user_id, 'small_custom_picture', $image_id) ;
           
            $new_user_id    =   email_exists( $email ) ;
            if ( $new_user_id){
             //   esc_html_e('The email was not saved because it is used by another user.</br>','wpestatetheme-core');
            } else{
                $args = array(
                     'ID'         => $user_id,
                     'user_email' => $email
                ); 
                wp_update_user( $args );
            } 
    }//end if
}
endif;




add_filter( 'manage_edit-estate_agent_columns', 'wpestate_my_columns_agent' );

if( !function_exists('wpestate_my_columns_agent') ):
function wpestate_my_columns_agent( $columns ) {
    $slice=array_slice($columns,2,2);
    unset( $columns['comments'] );
    unset( $slice['comments'] );
    $splice=array_splice($columns, 2);   
    $columns['estate_agent_thumb']    = esc_html__('Image','wpestatetheme-core');
    $columns['estate_agent_city']     = esc_html__('City','wpestatetheme-core');
    $columns['estate_agent_action']   = esc_html__('Action','wpestatetheme-core');
    $columns['estate_agent_category'] = esc_html__( 'Category','wpestatetheme-core');
    $columns['estate_agent_email']    = esc_html__('Email','wpestatetheme-core');
    $columns['estate_agent_phone']    = esc_html__('Phone','wpestatetheme-core');

    return  array_merge($columns,array_reverse($slice));
}
endif; // end   wpestate_my_columns  


$restrict_manage_posts = function($post_type, $taxonomy) {
    return function() use($post_type, $taxonomy) {
        global $typenow;

        if($typenow == $post_type) {
            $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
            $info_taxonomy = get_taxonomy($taxonomy);

            wp_dropdown_categories(array(
                'show_option_all'   => esc_html__("Show All",'wpestatetheme-core').' '.$info_taxonomy->label,
                'taxonomy'          => $taxonomy,
                'name'              => $taxonomy,
                'orderby'           => 'name',
                'selected'          => $selected,
                'show_count'        => TRUE,
                'hide_empty'        => TRUE,
                'hierarchical'      => true
            ));

        }

    };

};

$parse_query = function($post_type, $taxonomy) {

    return function($query) use($post_type, $taxonomy) {
        global $pagenow;

        $q_vars = &$query->query_vars;

        if( $pagenow == 'edit.php'
            && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type
            && isset($q_vars[$taxonomy])
            && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0
        ) {
            $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
            $q_vars[$taxonomy] = $term->slug;
        }

    };

};

add_action('restrict_manage_posts', $restrict_manage_posts('estate_agent', 'property_category_agent') );
add_filter('parse_query', $parse_query('estate_agent', 'property_category_agent') );


add_action('restrict_manage_posts', $restrict_manage_posts('estate_agent', 'property_action_category_agent') );
add_filter('parse_query', $parse_query('estate_agent', 'property_action_category_agent') );


add_action('restrict_manage_posts', $restrict_manage_posts('estate_agent', 'property_city_agent') );
add_filter('parse_query', $parse_query('estate_agent', 'property_city_agent') );


?>