Skip to content

Commit

Permalink
Merge pull request #59 from SimplyRETS/feat-multi-q-support
Browse files Browse the repository at this point in the history
Support searching with multiple `q` parameters
  • Loading branch information
CodyReichert committed Oct 22, 2017
2 parents 4b3f788 + c7117d0 commit 9938f4f
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 51 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.3.6
* FEATURE: Add support for multiple values in the `q` parameter.

## 2.3.5
* FEATURE: Add admin compliance option to show trademark symbol next to "MLS" text.

Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributors: SimplyRETS
Tags: rets, idx, real estate listings, real estate, listings, rets listings, simply rets, realtor, rets feed, idx feed
Requires at least: 3.0.1
Tested up to: 4.8.1
Stable tag: 2.3.5
Stable tag: 2.3.6
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html

Expand Down Expand Up @@ -235,6 +235,9 @@ listing sidebar widget.

== Changelog ==

= 2.3.6 =
* FEATURE: Add support for multiple values in the `q` parameter.

= 2.3.5 =
* FEATURE: Add admin compliance option to show trademark symbol next to "MLS" text.

Expand Down
4 changes: 2 additions & 2 deletions simply-rets-api-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function srApiOptionsRequest( $url ) {
$php_version = phpversion();
$site_url = get_site_url();

$ua_string = "SimplyRETSWP/2.3.5 Wordpress/{$wp_version} PHP/{$php_version}";
$ua_string = "SimplyRETSWP/2.3.6 Wordpress/{$wp_version} PHP/{$php_version}";
$accept_header = "Accept: application/json; q=0.2, application/vnd.simplyrets-v0.1+json";

if( is_callable( 'curl_init' ) ) {
Expand Down Expand Up @@ -209,7 +209,7 @@ public static function srApiRequest( $url ) {
$wp_version = get_bloginfo('version');
$php_version = phpversion();

$ua_string = "SimplyRETSWP/2.3.5 Wordpress/{$wp_version} PHP/{$php_version}";
$ua_string = "SimplyRETSWP/2.3.6 Wordpress/{$wp_version} PHP/{$php_version}";
$accept_header = "Accept: application/json; q=0.2, application/vnd.simplyrets-v0.1+json";

if( is_callable( 'curl_init' ) ) {
Expand Down
120 changes: 73 additions & 47 deletions simply-rets-post-pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,6 @@ public static function srPostDefaultContent( $content ) {
$maxbaths = get_query_var( 'sr_maxbaths', '' );
$minprice = get_query_var( 'sr_minprice', '' );
$maxprice = get_query_var( 'sr_maxprice', '' );
$keywords = get_query_var( 'sr_keywords', '' )
. get_query_var( 'sr_q', '' );
$brokers = get_query_var( 'sr_brokers', '' );
$water = get_query_var( 'water', '' );
/** Pagination */
Expand Down Expand Up @@ -668,6 +666,39 @@ public static function srPostDefaultContent( $content ) {
}
}

/**
* If `sr_q` is set, the user clicked a pagination link
* (next/prev), and `sr_q` will possibly be an array of
* values. Those translate to multiple `q` params in the
* API request.
*/
$q_string = '';

$kws = isset($_GET['sr_q']) ? $_GET['sr_q'] : '';
if(!empty($kws)) {
foreach((array)$kws as $key => $kw) {
$q_string .= "&q=$kw";
}
}

/**
* If `sr_keywords` is set, the user submitted a search
* form. This will always be a string, but it may contain
* multiple searches separated by a ';'. If so, split and
* translate them to multiple `q` parameters in the API
* request.
*/
$sfq = isset($_GET['sr_keywords']) ? $_GET['sr_keywords'] : '';
if(!empty($sfq)) {
$splitkw = explode(';', $sfq);
if(!empty($splitkw)) {
foreach( $splitkw as $key => $value) {
$trimmedkw = trim($value);
$q_string .= "&q=$trimmedkw";
}
}
}

/**
* Make a new array with all query parameters.
*
Expand All @@ -676,7 +707,6 @@ public static function srPostDefaultContent( $content ) {
*/

$listing_params = array(
"q" => $keywords,
"brokers" => $brokers,
"minbeds" => $minbeds,
"maxbeds" => $maxbeds,
Expand Down Expand Up @@ -711,57 +741,53 @@ public static function srPostDefaultContent( $content ) {
"map_position" => $map_position
);

foreach( $listing_params as $param => $val ) {
if( !$val == '' ) {
$filters_string .= ' ' . $param . '=\'' . $val . '\'';
}
}

/**
* Make advanced search page with new query
* The next two blocks create a string of attributes that
* will be added to the (dynamically injected)
* sr_search_form short-code on the new page. This is so,
* if a user is viewing results and goes to the "next"
* page, the search form values stay in tact.
*/
if( !$advanced || !$advanced == "true" ) {
$qs = '?'
. http_build_query( array_filter( $listing_params ) )
. $features_string
. $cities_string
. $counties_string
. $neighborhoods_string
. $agents_string
. $ptypes_string
. $statuses_string
. $amenities_string;

$qs = str_replace(' ', '%20', $qs);
$listings_content = SimplyRetsApiHelper::retrieveRetsListings($qs, $settings);
$content .= do_shortcode( "[sr_search_form $filters_string]");
$content .= $listings_content;
return $content;

/**
* Make regular search page with new query
*/
} else {
// Combine sr_q and _keywords into 1 string.
$kw_string = implode(
"; ",
get_query_var('sr_q', array()) + array(get_query_var('sr_keywords', ''))
);

$qs = '?';
$qs .= http_build_query( array_filter( $listing_params ) );
$qs .= $features_string;
$qs .= $cities_string;
$qs .= $counties_string;
$qs .= $agents_string;
$qs .= $ptypes_string;
$qs .= $neighborhoods_string;
$qs .= $statuses_string;
$qs .= $amenities_string;

$qs = str_replace(' ', '%20', $qs);
$listings_content = SimplyRetsApiHelper::retrieveRetsListings( $qs );

$content .= do_shortcode( "[sr_search_form advanced='True' $filters_string]");
$content .= $listings_content;
return $content;
$filters_string = '';
$next_atts = $listing_params + array(
"q" => $kw_string,
"advanced" => $advanced == "true" ? "true" : "false"
);

foreach( $next_atts as $param => $att ) {
if( !$att == '' ) {
$filters_string .= ' ' . $param . '=\'' . $att . '\'';
}
}


// Final API query string
$qs = '?'
. http_build_query( array_filter( $listing_params ) )
. $features_string
. $cities_string
. $counties_string
. $neighborhoods_string
. $agents_string
. $ptypes_string
. $statuses_string
. $amenities_string
. $q_string;

$qs = str_replace(' ', '%20', $qs);

$listings_content = SimplyRetsApiHelper::retrieveRetsListings($qs, $settings);
$content .= do_shortcode( "[sr_search_form $filters_string]");
$content .= $listings_content;

return $content;
}

Expand Down
12 changes: 12 additions & 0 deletions simply-rets-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public function sr_residential_shortcode( $atts ) {
&& !isset($listing_params['agent'])
&& !isset($listing_params['type'])
&& !isset($listing_params['status'])
&& !isset($listing_params['q'])
)
{
$listings_content = SimplyRetsApiHelper::retrieveRetsListings( $listing_params, $atts );
Expand Down Expand Up @@ -274,6 +275,15 @@ public function sr_residential_shortcode( $atts ) {
$counties_string = str_replace(' ', '%20', $counties_string );
}

if( isset( $listing_params['q'] ) && !empty( $listing_params['q'] ) ) {
$keywords = explode( ';', $listing_params['q'] );
foreach( $keywords as $key => $keyword ) {
$kw = trim($keyword);
$q_string .= "q=$kw&";
}
$q_string = str_replace(' ', '%20', $q_string );
}

/**
* Multiple statuses
*/
Expand Down Expand Up @@ -301,6 +311,7 @@ public function sr_residential_shortcode( $atts ) {
&& $key !== 'agent'
&& $key !== 'type'
&& $key !== 'status'
&& $key !== 'q'
) {
$params_string .= $key . "=" . $value . "&";
}
Expand All @@ -318,6 +329,7 @@ public function sr_residential_shortcode( $atts ) {
$qs .= $agents_string;
$qs .= $ptypes_string;
$qs .= $statuses_string;
$qs .= $q_string;

$listings_content = SimplyRetsApiHelper::retrieveRetsListings( $qs, $atts );
return $listings_content;
Expand Down
2 changes: 1 addition & 1 deletion simply-rets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin URI: https://simplyrets.com
Description: Show your Real Estate listings on your Wordpress site. SimplyRETS provides a very simple set up and full control over your listings.
Author: SimplyRETS
Version: 2.3.5
Version: 2.3.6
License: GNU General Public License v3 or later
Copyright (c) SimplyRETS 2014 - 2015
Expand Down

0 comments on commit 9938f4f

Please sign in to comment.