/**
 * This shortcode allows for you to specify the property type(s) using 
 * [listing post_type="property,rental" status="current,sold,leased" template="default"] option. You can also 
 * limit the number of entries that display. using  [listing limit="5"]
 */
function epl_shortcode_listing_callback($atts)
{
    $property_types = epl_get_active_post_types();
    if (!empty($property_types)) {
        $property_types = array_keys($property_types);
    }
    extract(shortcode_atts(array('post_type' => $property_types, 'status' => array('current', 'sold', 'leased'), 'limit' => '10', 'template' => false, 'location' => '', 'tools_top' => 'off', 'tools_bottom' => 'off', 'sortby' => '', 'sort_order' => 'DESC', 'query_object' => ''), $atts));
    if (is_string($post_type) && $post_type == 'rental') {
        $meta_key_price = 'property_rent';
    } else {
        $meta_key_price = 'property_price';
    }
    $sort_options = array('price' => $meta_key_price, 'date' => 'post_date');
    if (!is_array($post_type)) {
        $post_type = array_map('trim', explode(',', $post_type));
    }
    ob_start();
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array('post_type' => $post_type, 'posts_per_page' => $limit, 'paged' => $paged);
    if (!empty($location)) {
        if (!is_array($location)) {
            $location = explode(",", $location);
            $location = array_map('trim', $location);
            $args['tax_query'][] = array('taxonomy' => 'location', 'field' => 'slug', 'terms' => $location);
        }
    }
    if (!empty($status)) {
        if (!is_array($status)) {
            $status = explode(",", $status);
            $status = array_map('trim', $status);
            $args['meta_query'][] = array('key' => 'property_status', 'value' => $status, 'compare' => 'IN');
            add_filter('epl_sorting_options', 'epl_sorting_options_callback');
        }
    }
    if ($sortby != '') {
        if ($sortby == 'price') {
            $args['orderby'] = 'meta_value_num';
            $args['meta_key'] = $meta_key_price;
        } else {
            $args['orderby'] = 'post_date';
            $args['order'] = 'DESC';
        }
        $args['order'] = $sort_order;
    }
    if (isset($_GET['sortby'])) {
        $orderby = sanitize_text_field(trim($_GET['sortby']));
        if ($orderby == 'high') {
            $args['orderby'] = 'meta_value_num';
            $args['meta_key'] = $meta_key_price;
            $args['order'] = 'DESC';
        } elseif ($orderby == 'low') {
            $args['orderby'] = 'meta_value_num';
            $args['meta_key'] = $meta_key_price;
            $args['order'] = 'ASC';
        } elseif ($orderby == 'new') {
            $args['orderby'] = 'post_date';
            $args['order'] = 'DESC';
        } elseif ($orderby == 'old') {
            $args['orderby'] = 'post_date';
            $args['order'] = 'ASC';
        } elseif ($orderby == 'status_desc') {
            $args['orderby'] = 'meta_value';
            $args['meta_key'] = 'property_status';
            $args['order'] = 'DESC';
        } elseif ($orderby == 'status_asc') {
            $args['orderby'] = 'meta_value';
            $args['meta_key'] = 'property_status';
            $args['order'] = 'ASC';
        }
    }
    $query_open = new WP_Query($args);
    if (is_object($query_object)) {
        $query_open = $query_object;
    }
    if ($query_open->have_posts()) {
        ?>
		<div class="loop epl-shortcode">
			<div class="loop-content epl-shortcode-listing <?php 
        echo epl_template_class($template);
        ?>
">
				<?php 
        if ($tools_top == 'on') {
            do_action('epl_property_loop_start');
        }
        while ($query_open->have_posts()) {
            $query_open->the_post();
            $template = str_replace('_', '-', $template);
            epl_property_blog($template);
        }
        if ($tools_bottom == 'on') {
            do_action('epl_property_loop_end');
        }
        ?>
			</div>
			<div class="loop-footer">
					<?php 
        do_action('epl_pagination', array('query' => $query_open));
        ?>
			</div>
		</div>
		<?php 
    } else {
        echo '<h3>' . __('Nothing found, please check back later.', 'epl') . '</h3>';
    }
    wp_reset_postdata();
    return ob_get_clean();
}
function rec_epl_sd_single_staff_listings_callback()
{
    global $epl_settings, $epl_author;
    $quantity = isset($epl_settings['epl_sd_single_listing_count']) ? $epl_settings['epl_sd_single_listing_count'] : '8';
    $owned_listings = epl_sd_recent_listings($epl_author, $quantity);
    $display = 'image';
    $image = 'thumbnail';
    $d_title = FALSE;
    $d_icons = FALSE;
    $name = $epl_author->get_author_name();
    if ($name == 'Michael Coulson') {
    }
    // echo $owned_listings;
    $listing_style = isset($epl_settings['epl_sd_listing_template']) ? $epl_settings['epl_sd_listing_template'] : 'blog';
    // default is card
    if ($owned_listings) {
        echo '<div class="epl-sd-listings directory-section epl-clearfix">';
        echo "<div class='rec-internal-container-title rec-internal-white'><h4 class='loop-title'>For Sale with {$name}</h4></div>";
        global $post;
        foreach ($owned_listings as $post) {
            setup_postdata($post);
            epl_property_blog($listing_style);
        }
        wp_reset_postdata();
        echo '</div>';
    }
}