public static function get($args = array()) { // Store this for use in the final output/response... $sEcho = isset($_POST['sEcho']) ? $_POST['sEcho'] : 0; unset($_POST['sEcho']); $context_orig = isset($_POST['context']) ? $_POST['context'] : ''; unset($_POST['context']); // If length is not set for number of listings to return, set it to our Theme Options default $_POST['limit'] = isset($_POST['iDisplayLength']) ? $_POST['iDisplayLength'] : pls_get_option('listings_default_list_length'); unset($_POST['iDisplayLength']); $_POST['offset'] = isset($_POST['iDisplayStart']) ? $_POST['iDisplayStart'] : 0; unset($_POST['iDisplayStart']); $saved_search_lookup = isset($_POST['saved_search_lookup']) ? $_POST['saved_search_lookup'] : null; unset($_POST['saved_search_lookup']); // Remove this so it doesn't accidentally end up as a superfluous argument of an API call... unset($_POST['action']); // Handle location edge-case... if (!empty($_POST['location']) && !empty($_POST['location']['address']) && empty($_POST['location']['address_match'])) { $_POST['location']['address_match'] = 'like'; } // Handle saved search... if (!is_null($saved_search_lookup)) { // Attempt to retrieve search filters associated with the given saved search lookup ID... // NOTE: If no filters exist for the passed ID, $filters = PLS_Plugin_API::get_saved_search_filters($saved_search_lookup); if (empty($filters) || !is_array($filters)) { PLS_Plugin_API::save_search($saved_search_lookup, $_POST); } else { // For backwards compatibility, handle older fields that are no longer stored as saved search filters... $old_field_map = array('sEcho' => false, 'context' => false, 'iDisplayLength' => 'limit', 'iDisplayStart' => 'offset', 'saved_search_lookup' => false, 'action' => false); foreach ($old_field_map as $old => $new) { if (isset($filters[$old])) { if ($new !== false) { $filters[$new] = $filters[$old]; } unset($filters[$old]); } } // Swap all existing POST filters for the ones associated with the retrieved saved search... $_POST = $filters; } } // Define the default argument array $defaults = array('loading_img' => admin_url('images/wpspin_light.gif'), 'image_width' => 100, 'crop_description' => 0, 'sort_type' => pls_get_option('listings_default_sort_type'), 'listings_per_page' => pls_get_option('listings_default_list_length'), 'context' => $context_orig, 'context_var' => NULL, 'append_to_map' => true, 'search_query' => $_POST, 'property_ids' => isset($_POST['property_ids']) ? $_POST['property_ids'] : '', 'allow_id_empty' => false); // Resolve function args with default ones (which include any existing POST fields)... $merged_args = wp_parse_args($args, $defaults); $cache = new PLS_Cache('list'); if ($cached_response = $cache->get($merged_args)) { // This field must match the one passed in with this request... $cached_response['sEcho'] = $sEcho; echo json_encode($cached_response); die; } // Extract the arguments after they merged with the defaults extract($merged_args, EXTR_SKIP); // Start off with a placeholder in case the plugin is not active or there is no API key... $api_response = PLS_Listing_Helper::$default_listing; // If plugin is active, grab listings intelligently... if (!pls_has_plugin_error()) { // Get the listings list markup and JS if (!empty($property_ids) || $allow_id_empty) { // Sometimes property_ids are passed in as a flat screen from the JS post object if (is_string($property_ids)) { $property_ids = explode(',', $property_ids); } $api_response = PLS_Plugin_API::get_listing_details(array('property_ids' => $property_ids, 'limit' => $_POST['limit'], 'offset' => $_POST['offset'])); } elseif (isset($search_query['neighborhood_polygons']) && !empty($search_query['neighborhood_polygons'])) { $api_response = PLS_Plugin_API::get_polygon_listings($search_query); } else { $api_response = PLS_Plugin_API::get_listings($search_query); } } $response = array(); // Build response for datatables.js $listings = array(); $listings_cache = new PLS_Cache('Listing Thumbnail'); foreach ($api_response['listings'] as $key => $listing) { // Check for cached listing thumbnail... $cache_id = array('context' => $context, 'listing_id' => $listing['id']); if (!($item_html = $listings_cache->get($cache_id))) { // Handle case of zero listing images... if (empty($listing['images'])) { $listing['images'][0]['url'] = ''; } ob_start(); ?> <div class="listing-item grid_8 alpha" itemscope itemtype="http://schema.org/Offer" data-listing="<?php echo $listing['id']; ?> "> <div class="listing-thumbnail grid_3 alpha"> <?php $property_images = is_array($listing['images']) ? $listing['images'] : array(); usort($property_images, array(__CLASS__, 'order_images')); ?> <a href="<?php echo @$listing['cur_data']['url']; ?> " itemprop="url"> <?php echo PLS_Image::load($property_images[0]['url'], array('resize' => array('w' => 210, 'h' => 140), 'fancybox' => true, 'as_html' => true, 'html' => array('alt' => $listing['location']['full_address'], 'itemprop' => 'image', 'placeholder' => PLS_IMG_URL . "/null/listing-300x180.jpg"))); ?> </a> </div> <div class="listing-item-details grid_5 omega"> <header> <p class="listing-item-address h4" itemprop="name"> <a href="<?php echo PLS_Plugin_API::get_property_url($listing['id']); ?> " rel="bookmark" title="<?php echo $listing['location']['address']; ?> " itemprop="url"> <?php echo $listing['location']['address'] . ', ' . $listing['location']['locality'] . ' ' . $listing['location']['region'] . ' ' . $listing['location']['postal']; ?> </a> </p> </header> <div class="basic-details"> <ul> <?php if (!empty($listing['cur_data']['beds'])) { ?> <li class="basic-details-beds p1"><span>Beds:</span> <?php echo @$listing['cur_data']['beds']; ?> </li> <?php } ?> <?php if (!empty($listing['cur_data']['baths'])) { ?> <li class="basic-details-baths p1"><span>Baths:</span> <?php echo @$listing['cur_data']['baths']; ?> </li> <?php } ?> <?php if (!empty($listing['cur_data']['half_baths'])) { ?> <li class="basic-details-half-baths p1"><span>Half Baths:</span> <?php echo @$listing['cur_data']['half_baths']; ?> </li> <?php } ?> <?php if (!empty($listing['cur_data']['price'])) { ?> <li class="basic-details-price p1" itemprop="price"><span>Price:</span> <?php echo PLS_Format::number($listing['cur_data']['price'], array('abbreviate' => false, 'add_currency_sign' => true)); ?> </li> <?php } ?> <?php if (!empty($listing['cur_data']['sqft'])) { ?> <li class="basic-details-sqft p1"><span>Sqft:</span> <?php echo PLS_Format::number($listing['cur_data']['sqft'], array('abbreviate' => false, 'add_currency_sign' => false)); ?> </li> <?php } ?> <?php if (!empty($listing['rets']['mls_id'])) { ?> <li class="basic-details-mls p1"><span>MLS ID:</span> <?php echo @$listing['rets']['mls_id']; ?> </li> <?php } ?> </ul> </div> <p class="listing-description p4" itemprop="description"> <?php echo substr($listing['cur_data']['desc'], 0, 300); ?> </p> </div> <div class="actions"> <a class="more-link" href="<?php echo PLS_Plugin_API::get_property_url($listing['id']); ?> " itemprop="url">View Property Details</a> <?php echo PLS_Plugin_API::placester_favorite_link_toggle(array('property_id' => $listing['id'])); ?> </div> <?php PLS_Listing_Helper::get_compliance(array('context' => 'inline_search', 'agent_name' => @$listing['rets']['aname'], 'office_name' => @$listing['rets']['oname'])); ?> </div> <?php $item_html = ob_get_clean(); $item_html = apply_filters(pls_get_merged_strings(array("pls_listings_list_ajax_item_html", $context), '_', 'pre', false), htmlspecialchars_decode($item_html), $listing, $context_var); $listings_cache->save($item_html); } $listings[$key][] = $item_html; $listings[$key][] = $listing; } // Required for datatables.js to function properly... $response['sFirst'] = 'Previous'; $response['sPrevious'] = 'Next'; $response['sEcho'] = $sEcho; $response['aaData'] = $listings; $api_total = isset($api_response['total']) ? $api_response['total'] : 0; $response['iTotalRecords'] = $api_total; $response['iTotalDisplayRecords'] = $api_total; $cache->save($response); ob_start("ob_gzhandler"); echo json_encode($response); // Wordpress echos out a "0" randomly -- die prevents this... die; }