forked from SimplyRETS/simplyretswp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimply-rets-utils.php
More file actions
313 lines (248 loc) · 8.78 KB
/
simply-rets-utils.php
File metadata and controls
313 lines (248 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/*
*
* simply-rets-utils.php - Copyright (C) 2014-2015 SimplyRETS
* This file provides general utilities for the SimplyRETS plugin.
*
*/
/* Code starts here */
class SrUtils
{
public static function isSingleVendor()
{
$vendors = get_option('sr_adv_search_meta_vendors', array());
if (count($vendors) > 1) {
return false;
}
return true;
}
public static function srShowListingMeta()
{
if (get_option('sr_show_listingmeta')) {
$show_listing_meta = false;
} else {
$show_listing_meta = true;
}
return $show_listing_meta;
}
/**
* The naming for the database option is backwards.
* If it's 'checked', we _don't_ show data.
*/
public static function showAgentContact()
{
if (get_option('sr_show_agent_contact')) {
$show = false;
} else {
$show = true;
}
return $show;
}
/**
* Builds a link to a listings' details page. Used in search results.
*/
public static function buildDetailsLink($listing, $params = array())
{
$permalink_struct = get_option('permalink_structure', '');
$custom_permalink_struct = get_option('sr_permalink_structure', '');
// Are pretty permalinks enabled?
$prettify = true;
$prettify = $custom_permalink_struct === "pretty" ? true : $prettify;
$prettify = $custom_permalink_struct === "pretty_extra" ? true : $prettify;
$prettify = $custom_permalink_struct === "query_string" ? false : $prettify;
$prettify = $permalink_struct === "" ? false : $prettify;
// Build a query string
$_query = http_build_query($params);
$query = !empty($_query) ? $_query : "";
// Base of the URL we're building
$url = get_home_url();
// Listing details
$listing_id = $listing->mlsId;
$listing_address = str_replace(array('#','/'), array('', '-'), $listing->address->full);
if ($prettify && $custom_permalink_struct === "pretty_extra") {
$listing_city = $listing->address->city;
$listing_state = $listing->address->state;
$listing_zip = $listing->address->postalCode;
$url .= "/listings/$listing_city/$listing_state/$listing_zip/$listing_address/$listing_id";
if (!empty($query)) {
$url .= "?" . $query;
}
} elseif ($prettify && $custom_permalink_struct === "pretty") {
$url .= "/listings/$listing_id/$listing_address";
if (!empty($query)) {
$url .= "?" . $query;
}
} else {
$url .= "?sr-listings=sr-single"
. "&listing_id=$listing_id"
. "&listing_title=$listing_address";
if (!empty($query)) {
$url .= "&" . $query;
}
}
$url = str_replace(' ', '+', $url);
$url = str_replace('#', '%23', $url);
return $url;
}
public static function buildPaginationLinks($pagination)
{
$pag = array(
'prev' => '',
'next' => ''
);
$siteUrl = get_home_url() . '/?sr-listings=sr-search&';
if ($pagination['prev'] !== null && !empty($pagination['prev'] )) {
$previous = $pagination['prev'];
$prev = str_replace( 'https://api.simplyrets.com/properties?', $siteUrl, $previous );
$prev_link = "<a href='{$prev}'>Prev</a>";
$pag['prev'] = $prev_link;
}
if ($pagination['next'] !== null && !empty($pagination['next'] )) {
$nextLink = $pagination['next'];
$next = str_replace( 'https://api.simplyrets.com/properties?', $siteUrl, $nextLink );
$next_link = "<a href='{$next}'>Next</a>";
$pag['next'] = $next_link;
}
return $pag;
}
/**
* Use this instead of builting parse_str
* proper_parse_str will sanely handle duplicate
* keys in the query. id ?foo=1&foo2
*
* @param $str - a query string
* @result $arr - the query string in array form
*/
public static function proper_parse_str($str)
{
$arr = array();
# split on outer delimiter
$pairs = explode('&', $str);
foreach ($pairs as $i) {
list($name,$value) = explode('=', $i, 2);
if (isset($arr[$name])) {
if (is_array($arr[$name])) {
$arr[$name][] = $value;
} else {
$arr[$name] = array($arr[$name], $value);
}
} else {
$arr[$name] = $value;
}
}
return $arr;
}
public static function ordinalSuffix($number)
{
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number%100) <= 13)) {
return $number. 'th';
} else {
return $number. $ends[$number % 10];
}
}
public static function mkListingSummaryCompliance($listing_office)
{
$office_on_thumbnails = get_option('sr_office_on_thumbnails', false);
$idx_img_on_thumbnails = get_option('sr_thumbnail_idx_image', false);
$listing_office_markup = "";
$listing_idx_img_markup = "";
if (!empty($listing_office) && !empty($office_on_thumbnails)) {
$listing_office_markup = "Listing broker: {$listing_office}";
}
if ($idx_img_on_thumbnails !== false && !empty($idx_img_on_thumbnails)) {
$listing_idx_img_markup = "<img src=\"{$idx_img_on_thumbnails}\"/>";
}
// Add a line break if both fields are enabled
if (!empty($listing_office_markup) && !empty($listing_idx_img_markup)) {
return "{$listing_office_markup}<br/>{$listing_idx_img_markup}";
} else {
return "{$listing_office_markup} {$listing_idx_img_markup}";
}
}
/**
* Generate disclaimer text shown with short-code listings. If
* the user has provided a custom disclaimer in their settings
* page use that, otherwise use the SimplyRETS default.
*/
public static function mkDisclaimerText($lastUpdate)
{
$custom_disclaimer = get_option('sr_custom_disclaimer', false);
if ($custom_disclaimer) {
// Splice lastUpdate date into custom disclaimer
$built_disclaimer = str_replace('{lastUpdate}', $lastUpdate, $custom_disclaimer);
return html_entity_decode($built_disclaimer);
} else {
return "This information is believed to be accurate, but without any warranty.";
}
}
}
class SrListing
{
/**
* Return a 'display-ready' status for a listing. Checks the
* sr_show_mls_status_text option and returns either the
* statusText or status for the listing.
*/
public static function listingStatus($listing)
{
$useStatusText = get_option('sr_show_mls_status_text', false);
return $useStatusText ? $listing->mls->statusText : $listing->mls->status;
}
}
class SrMessages
{
public static function noResultsMsg($response)
{
$response = (array)$response;
if ($response['message']) {
return (
'<br><p><strong>'
. $response['message']
. '</br></p></strong>'
);
}
$noResultsMsg = "<br><p><strong>There are 0 listings that match this search. "
. "Please try to broaden your search criteria or feel free to try again later.</p></strong>";
return $noResultsMsg;
}
}
class SrViews
{
public static function listDateResults($date)
{
$markup = <<<HTML
<li>
<span>Listed on $date</span>
</li>
HTML;
return $markup;
}
}
/**
* Top level 'pollyfill' for 'http_parse_headers'
*
* Taken from PHP implementation:
* http://php.net/manual/it/function.http-parse-headers.php
*/
if (!function_exists('http_parse_headers')) {
function http_parse_headers($raw_headers)
{
$headers = array(); // $headers = [];
foreach (explode("\n", $raw_headers) as $i => $h) {
$h = explode(':', $h, 2);
if (isset($h[1])) {
if (!isset($headers[$h[0]])) {
$headers[$h[0]] = trim($h[1]);
} elseif (is_array($headers[$h[0]])) {
$tmp = array_merge($headers[$h[0]], array(trim($h[1])));
$headers[$h[0]] = $tmp;
} else {
$tmp = array_merge(array($headers[$h[0]]), array(trim($h[1])));
$headers[$h[0]] = $tmp;
}
}
}
return $headers;
}
}