-
Notifications
You must be signed in to change notification settings - Fork 1
/
simply-rets-post-pages.php
947 lines (810 loc) · 35.6 KB
/
simply-rets-post-pages.php
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
<?php
/*
*
* simply-rets-post-pages.php - Copyright (C) 2014-2015 SimplyRETS
* This file provides the logic for the simply-rets custom post type pages.
*
*/
/* Code starts here */
add_action('init', array('SimplyRetsCustomPostPages', 'srInitRewriteRules'));
add_action('init', array('SimplyRetsCustomPostPages', 'srRegisterPostType'));
add_filter('comments_template', array('SimplyRetsCustomPostPages', 'srClearComments'));
add_filter('single_template', array('SimplyRetsCustomPostPages', 'srLoadPostTemplate'));
add_filter('the_content', array('SimplyRetsCustomPostPages', 'srPostDefaultContent'));
add_filter('the_posts', array('SimplyRetsCustomPostPages', 'srCreateDynamicPost'));
add_action('add_meta_boxes', array('SimplyRetsCustomPostPages', 'postFilterMetaBox'));
add_action('add_meta_boxes', array('SimplyRetsCustomPostPages', 'postTemplateMetaBox'));
add_action('save_post', array('SimplyRetsCustomPostPages', 'postFilterMetaBoxSave'));
add_action('save_post', array('SimplyRetsCustomPostPages', 'postTemplateMetaBoxSave'));
add_action('admin_init', array('SimplyRetsCustomPostPages', 'postFilterMetaBoxCss'));
add_action('admin_enqueue_scripts', array('SimplyRetsCustomPostPages', 'postFilterMetaBoxJs'));
// ^TODO: load css/js only on sr-listings post type pages when admin
// and move these into a constructor
add_action('sr_update_adv_search_meta_action', array('SimplyRetsApiHelper', 'srUpdateAdvSearchOptions'));
add_filter("rewrite_rules_array", array("SimplyRetsCustomPostPages", "srAddRewriteRules"));
class SimplyRetsCustomPostPages {
public static function srActivate() {
SimplyRetsCustomPostPages::srRegisterPostType();
SimplyRetsApiHelper::srUpdateAdvSearchOptions();
wp_schedule_event(
get_option('sr_adv_search_meta_timestamp')
, 'daily'
, 'sr_update_adv_search_options_action'
);
add_option( 'sr_api_name', 'simplyrets' );
add_option( 'sr_api_key', 'simplyrets' );
add_option( 'sr_listing_gallery', 'fancy' );
add_option( 'sr_show_leadcapture', true );
add_option( 'sr_search_map_position', 'map_above');
add_option( 'sr_permalink_structure', 'pretty' );
add_option('sr_show_admin_message', 1);
add_option('sr_demo_page_created', false);
add_option('sr_office_on_thumbnails', false);
add_option('sr_agent_on_thumbnails', false);
add_option('sr_thumbnail_idx_image', '');
add_option('sr_agent_office_above_the_fold', false);
add_option('sr_show_mls_trademark_symbol', false);
flush_rewrite_rules();
}
public static function srDeactivate() {
delete_option('sr_show_admin_message');
delete_option('sr_demo_page_created');
flush_rewrite_rules();
}
public static function srInitRewriteRules() {
$rules = get_option('rewrite_rules');
add_rewrite_tag('%listings%', '([^&]+)');
// 'pretty_extra' permalinks
add_rewrite_rule(
'listings/([^&]+)/([^&]+)/([^&]+)/([^&]+)/([^&]+)/?$',
'index.php?sr-listings=sr-single&sr_city=$matches[1]&sr_state=$matches[2]&sr_zip=$matches[3]&listing_id=$matches[5]&listing_title=$matches[4]',
'top'
);
// 'pretty' permalinks
add_rewrite_rule(
'listings/(.*)/(.*)?$',
'index.php?sr-listings=sr-single&listing_id=$matches[1]&listing_title=$matches[2]',
'top'
);
if(!isset($rules['%listings%'])) {
flush_rewrite_rules();
}
return;
}
public static function srAddRewriteRules($incoming) {
$rules = array(
'listings/([^&]+)/([^&]+)/([^&]+)/([^&]+)/([^&]+)/?$'
=> 'index.php?sr-listings=sr-single&sr_city=$matches[1]&sr_state=$matches[2]&sr_zip=$matches[3]&listing_title=$matches[4]&listing_id=$matches[5]',
'listings/(.*)/(.*)?$'
=> 'index.php?sr-listings=sr-single&listing_id=$matches[1]&listing_title=$matches[2]'
);
return $incoming + $rules;
}
public static function onActivationNotice () {
return (
'<div id="setting-error-settings_updated" class="updated settings-error notice">'.
'<p>' .
' <span>'.
' <form id="admin-msg" method="post" action="options-general.php?page=simplyrets-admin.php">' .
' <input type="hidden" name="sr_create_demo_page" value="1" />' .
' <strong>SimplyRETS: </strong>' .
' <button class="sr-admin-msg-btn" type="submit">Click here</button>' .
' to set up a demo page!' .
' </form>' .
' </span>' .
' <span style="float:right">' .
' <form id="admin-dismiss" method="post" action="options-general.php?page=simplyrets-admin.php">' .
' <input type="hidden" name="sr_dismiss_admin_msg" value="1" />' .
' <button class="sr-admin-msg-btn" type="submit">Dismiss</button>' .
' </form>' .
' </span>' .
'</p>' .
'</div>'
);
}
public static function srPluginSettingsLink( $links ) {
$settings_link =
'<a href="' . admin_url( 'options-general.php?page=simplyrets-admin.php' ) . '">'
. __( 'Settings', 'SimplyRETS' )
. '</a>';
array_unshift( $links, $settings_link );
return $links;
}
// Create our Custom Post Type
public static function srRegisterPostType() {
$labels = array(
'name' => __( 'SimplyRETS' ),
'singular_name' => __( 'SimplyRETS Page' ),
'add_new_item' => __( 'New SimplyRETS Page' ),
'edit_item' => __( 'Edit SimplyRETS Page' ),
'new_item' => __( 'New SimplyRETS Page' ),
'view_item' => __( 'View SimplyRETS Page' ),
'all_items' => __( 'All SimplyRETS Pages' ),
'search_items' => __( 'Search SimplyRETS Pages' ),
);
$args = array(
'public' => true,
'has_archive' => false,
'labels' => $labels,
'description' => 'SimplyRETS property listings pages',
'query_var' => true,
'menu_positions' => '15',
'capability_type' => 'page',
'hierarchical' => true,
'taxonomies' => array(),
'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
'rewrite' => true
);
register_post_type( 'sr-listings', $args );
}
public static function srQueryVarsInit( $vars ) {
global $wp_query;
$vars[] = "listing_id";
$vars[] = "listing_title";
$vars[] = "listing_price";
$vars[] = "limit";
$vars[] = "offset";
$vars[] = "advanced";
$vars[] = "status";
// sr prefixes are for the search form
$vars[] = "sr_minprice";
$vars[] = "sr_maxprice";
$vars[] = "sr_minbeds";
$vars[] = "sr_maxbeds";
$vars[] = "sr_minbaths";
$vars[] = "sr_maxbaths";
$vars[] = "sr_q";
$vars[] = "sr_keywords";
$vars[] = "sr_type";
$vars[] = "sr_ptype";
$vars[] = "sr_agent";
$vars[] = "sr_brokers";
$vars[] = "sr_sort";
$vars[] = "water";
// post type
$vars[] = "sr-listings";
// advanced search form parameters
$vars[] = "sr_lotsize";
$vars[] = "sr_area";
$vars[] = "sr_cities";
$vars[] = "sr_neighborhoods";
$vars[] = "sr_amenities";
$vars[] = "sr_features";
$vars[] = "sr_counties";
// multi-mls
$vars[] = "vendor";
$vars[] = "sr_vendor";
// settings
$vars[] = "sr_map_position";
return $vars;
}
public static function postFilterMetaBox() {
add_meta_box(
'sr-meta-box-filter'
, __( 'Filter Results on This Page', 'sr-textdomain')
, array('SimplyRetsCustomPostPages', 'postFilterMetaBoxMarkup')
, 'sr-listings'
, 'normal'
, 'high'
);
}
public static function postTemplateMetaBox() {
add_meta_box(
'sr-template-meta-box'
, __('Page Template', 'sr-textdomain')
, array( 'SimplyRetsCustomPostPages', 'postTemplateMetaBoxMarkup' )
, 'sr-listings'
, 'side'
, 'core'
);
}
public static function postFilterMetaBoxJs() {
wp_register_script( 'simply-rets-admin-js'
, plugins_url( 'assets/js/simply-rets-admin.js', __FILE__ )
, array( 'jquery' )
);
wp_enqueue_script( 'simply-rets-admin-js' );
}
public static function postFilterMetaBoxCss() {
wp_register_style( 'simply-rets-admin-css', plugins_url( 'assets/css/simply-rets-admin.css', __FILE__ ) );
wp_enqueue_style( 'simply-rets-admin-css' );
}
public static function postFilterMetaBoxMarkup( $post ) {
wp_nonce_field( basename(__FILE__), 'sr_meta_box_nonce' );
$min_price_filter = "";
$max_price_filter = "";
$min_bed_filter = "";
$max_bed_filter = "";
$min_bath_filter = "";
$max_bath_filter = "";
$agent_id_filter = "";
$listing_type_filter = "";
$limit_filter = "";
$sr_filters = get_post_meta( $post->ID, 'sr_filters', true);
// TODO: Once all the query parameters are finalized, we can generate
// most of the markup below.
?>
<div class="current-filters">
<span class="filter-add">
<?php _e( 'Add new Filter' ); ?>
</span>
<select name="sr-filter-select" id="sr-filter-select">
<option> -- Select a Filter -- </option>
<option val="minprice-option"> Minimum Price </option>
<option val="maxprice-option"> Maximum Price </option>
<option val="minbeds-option"> Minimum Beds </option>
<option val="maxbeds-option"> Maximum Beds </option>
<option val="minbaths-option"> Minimum Bathrooms </option>
<option val="maxbaths-option"> Maximum Bathrooms </option>
<option val="agentid-option"> Listing Agent </option>
<option val="type-option"> Listing Type </option>
<option val="limit-option"> Amount of listings </option>
</select>
<hr>
</div>
<div class="sr-meta-inner">
<!-- Min Price Filter -->
<div class="sr-filter-input" id="sr-min-price-span">
<label for="sr-min-price-input">
Minimum Price:
</label>
<input id="minprice" type="number" name="sr_filters[minprice]"
value="<?php print_r( $min_price_filter ); ?>"/>
<span class="sr-remove-filter">Remove Filter</span>
</div>
<!-- Max Price Filter -->
<div class="sr-filter-input" id="sr-max-price-span">
<label for="sr-max-price-input">
Maximum Price:
</label>
<input id="maxprice" type="number" name="sr_filters[maxprice]"
value="<?php print_r( $max_price_filter ); ?>"/>
<span class="sr-remove-filter">Remove Filter</span>
</div>
<!-- Min Bed Filter -->
<div class="sr-filter-input" id="sr-min-bed-span">
<label for="sr-min-bed-input">
Minimum Bedrooms:
</label>
<input id="minbeds" type="number" name="sr_filters[minbeds]"
value="<?php print_r( $min_bed_filter ); ?>"/>
<span class="sr-remove-filter">Remove Filter</span>
</div>
<!-- Max Bed Filter -->
<div class="sr-filter-input" id="sr-max-bed-span">
<label for="sr-max-bed-input">
Maximum Bedrooms:
</label>
<input id="maxbeds" type="number" name="sr_filters[maxbeds]"
value="<?php print_r( $max_bed_filter ); ?>"/>
<span class="sr-remove-filter">Remove Filter</span>
</div>
<!-- Min Baths Filter -->
<div class="sr-filter-input" id="sr-min-bath-span">
<label for="sr-min-bath-input">
Minimum Bathrooms:
</label>
<input id="minbaths" type="number" name="sr_filters[minbaths]"
value="<?php print_r( $min_bath_filter ); ?>"/>
<span class="sr-remove-filter">Remove Filter</span>
</div>
<!-- Max Baths Filter -->
<div class="sr-filter-input" id="sr-max-bath-span">
<label for="sr-max-bath-input">
Maximum Bathrooms:
</label>
<input id="maxbaths" type="number" name="sr_filters[maxbaths]"
value="<?php print_r( $max_bath_filisting_typelter ); ?>"/>
<span class="sr-remove-filter">Remove Filter</span>
</div>
<!-- Agent ID Filter -->
<div class="sr-filter-input" id="sr-listing-agent-span">
<label for="sr-listing-agent-input">
Listing Agent ID:
</label>
<input id="agent" type="number" name="sr_filters[agent]"
value="<?php print_r( $agent_id_filter ); ?>"/>
<span class="sr-remove-filter">Remove Filter</span>
</div>
<!-- Listing Type Filter -->
<div class="sr-filter-input" id="sr-listing-type-span">
<label for="sr-listing-type-input">
Property Type:
</label>
<input id="type" type="text" name="sr_filters[type]"
value="<?php print_r( $listing_type_filter ); ?>"/>
<span class="sr-remove-filter">Remove Filter</span>
</div>
<!-- Response Limit Filter -->
<div class="sr-filter-input" id="sr-limit-span">
<label for="sr-limit-input">
Amount of listings to show:
</label>
<input id="limit" type="text" name="sr_filters[limit]"
value="<?php print_r( $limit_filter ); ?>"/>
<span class="sr-remove-filter">Remove Filter</span>
</div>
</div>
<?php
// echo '<br>Current filters: <br>'; print_r( $sr_filters );
// echo '<br>';
// ^TODO: Remove degbug
// on page load, if there are any filters already saved, load them,
// show the input field, and remove the option from the dropdown
if( !is_array($sr_filters) ) {
$sr_filters = array();
}
foreach( $sr_filters as $key=>$val ) {
if ( $val != '' ) {
?>
<script>
var filterArea = jQuery('.current-filters');
var key = jQuery(<?php print_r( $key ); ?>);
var val = <?php echo json_encode( $val ); ?>;
var parent = key.parent();
key.val(val); // set value to $key
filterArea.append(parent); //append div to filters area
parent.show(); //display: block the div since it has a value
</script>
<?php
}
}
}
public static function postFilterMetaBoxSave( $post_id ) {
if( isset($_POST['sr_meta_box_nonce']) ) {
$current_nonce = $_POST['sr_meta_box_nonce'];
} else {
$current_nonce = NULL;
}
$is_autosaving = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$valid_nonce = ( isset( $current_nonce ) && wp_verify_nonce( $current_nonce, basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_autosaving || $is_revision || !$valid_nonce ) {
return;
}
if( isset($_POST['sr_filters']) ) {
$sr_filters = $_POST['sr_filters'];
} else {
$sr_filters = NULL;
}
update_post_meta( $post_id, 'sr_filters', $sr_filters );
}
public static function postTemplateMetaBoxMarkup( $post ) {
wp_nonce_field( basename(__FILE__), 'sr_template_meta_nonce' );
$current_template = get_post_meta( $post->ID, 'sr_page_template', true);
$template_options = get_page_templates();
$box_label = '<label class="sr-filter-meta-box" for="sr_page_template">Page Template</label><br />';
$box_select = '<select name="sr_page_template" id="sr-page-template-select">';
$box_default_option = '<option value="">Default Template</option>';
$box_option = '';
echo $box_label;
foreach ( $template_options as $name=>$file ) {
if ( $current_template == $file ) {
$box_option .= '<option value="' . $file . '" selected="selected">' . $name . '</option>';
} else {
$box_option .= '<option value="' . $file . '">' . $name . '</option>';
}
}
echo $box_select;
echo $box_default_option;
echo $box_option;
echo '</select>';
}
public static function postTemplateMetaBoxSave( $post_id ) {
if( isset($_POST['sr_template_meta_nonce']) ) {
$current_nonce = $_POST['sr_template_meta_nonce'];
} else {
$current_nonce = NULL;
}
$is_autosaving = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$valid_nonce = ( isset( $current_nonce ) && wp_verify_nonce( $current_nonce, basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_autosaving || $is_revision || !$valid_nonce ) {
return;
}
if( isset($_POST['sr_page_template']) ) {
$sr_page_template = $_POST['sr_page_template'];
} else {
$sr_page_template = NULL;
}
update_post_meta( $post_id, 'sr_page_template', $sr_page_template );
}
// TODO: not sure if this is entirely necessary...at one time it was
public static function srClearComments() {
global $post;
if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
return;
}
if ( $post->post_type == 'sr-listings') {
return dirname(__FILE__) . '/simply-rets-comments-template.php';
}
return;
}
public static function srLoadPostTemplate($single) {
$query_object = get_queried_object();
$sr_post_type = 'sr-listings';
// If this isn't a SimplyRETS page, return default template
if ($query_object->post_type !== $sr_post_type) {
return $single;
}
// The user can use a custom template if the file name is:
// single-sr-listings.php
$default_templates = array();
$default_templates[] = "single-{$query_object->post_type}.php";
$default_templates[] = "page.php";
// If the user is using a "SimplyRETS Page", they may select a
// specific template from the current theme.
$page_template = get_post_meta($query_object->ID, 'sr_page_template', true);
if (!empty($page_template)) {
$default_templates = $page_template;
}
// Resolve path to the template
$new_template = locate_template($default_templates, false);
return $new_template;
}
/**
* When loading a sr-listings page, this function will parse a GET
* (search) parameter, and return the original values, formatting
* query string, and an array of the values.
*/
public static function parseGetParameter($name, $key, $params) {
$param = isset($_GET[$name]) ? $_GET[$name] : "";
$param_arr = is_array($param) ? $param : explode(";", $param);
$param_str = "";
if(is_array($param_arr) && !empty($param_arr)) {
foreach((array)$param_arr as $idx => $val) {
if (!empty($val)) {
$final = trim($val);
$param_str .= "&{$key}={$final}";
}
}
}
return array("param" => $param, "query" => $param_str);
}
public static function srPostDefaultContent( $content ) {
require_once( plugin_dir_path(__FILE__) . 'simply-rets-api-helper.php' );
$post_type = get_post_type();
$page_name = get_query_var('sr-listings');
$sr_post_type = 'sr-listings';
if (get_query_var('listing_id') != NULL AND get_query_var('listing_title') != NULL) {
$listing_id = get_query_var('listing_id');
$vendor = get_query_var('sr_vendor', '');
$add_rooms = get_option('sr_additional_rooms') ? 'rooms' : '';
$params = http_build_query(
array(
"vendor" => $vendor,
"include" => $add_rooms
)
);
$resource = "/{$listing_id}?{$params}";
$content .= SimplyRetsApiHelper::retrieveListingDetails( $resource );
return $content;
}
if ( $page_name == 'sr-search' ) {
$minbeds = get_query_var( 'sr_minbeds', '' );
$maxbeds = get_query_var( 'sr_maxbeds', '' );
$minbaths = get_query_var( 'sr_minbaths', '' );
$maxbaths = get_query_var( 'sr_maxbaths', '' );
$minprice = get_query_var( 'sr_minprice', '' );
$maxprice = get_query_var( 'sr_maxprice', '' );
$brokers = get_query_var( 'sr_brokers', '' );
$water = get_query_var( 'water', '' );
/** Pagination */
$limit = get_query_var( 'limit', '' );
$offset = get_query_var( 'offset', '' );
/** Advanced Search */
$advanced = get_query_var( 'advanced', '' );
$status = get_query_var( 'status', '' );
$lotsize = get_query_var( 'sr_lotsize', '' );
$area = get_query_var( 'sr_area', '' );
$sort = get_query_var( 'sr_sort', '' );
/** multi mls */
$vendor = get_query_var('sr_vendor', '');
$map_position = get_query_var('sr_map_position',
get_option('sr_search_map_position'));
/**
* Format the 'type' parameter.
* Note that the 'type' might come in as an Array or a
* String. For strings, we split on ";" to support
* multiple property types only if the string is not
* empty. Arrays are concated into multiple type=
* parameters.
*/
$p_types = isset($_GET['sr_ptype']) ? $_GET['sr_ptype'] : '';
$ptypes_string = '';
if(!is_array($p_types) && !empty($p_types)) {
if(strpos($p_types, ";") !== FALSE) {
$p_types = explode(';', $p_types);
} else {
$ptypes_string = "&type=$p_types";
}
}
if(is_array($p_types) && !empty($p_types)) {
foreach((array)$p_types as $key => $ptype) {
$final = trim($ptype);
$ptypes_string .= "&type=$final";
}
}
/**
* Format the 'status' parameter.
* Note that the 'status' might come in as an Array or a
* String. For strings, we split on ";" to support
* multiple statuses only if the string is not empty.
* Arrays are concated into multiple status= parameters.
*
* NOTE: it is important to not send an empty status
* parameter, for example "status=" to the API, as it will
* interpret it as Active, whereas _no_ status parameter
* is Active and Pending.
*/
$statuses = isset($_GET['sr_status']) ? $_GET['sr_status'] : $status;
$statuses_string = '';
$statuses_attribute = '';
if(!is_array($statuses) && !empty($statuses)) {
if(strpos($statuses, ";") !== FALSE) {
$statuses = explode(';', $statuses);
} else {
$statuses_string = "&status=$statuses";
}
$statuses_attribute = $statuses;
}
if(is_array($statuses) && !empty($statuses)) {
foreach((array)$statuses as $key => $stat) {
$final = trim($stat);
$statuses_string .= "&status=$final";
}
$statuses_attribute = implode(";", $statuses);
}
/**
* The loops below check if the short-code has multiple
* values for any query parameter. Eg, multiple cities.
* Since they support multiple, we do the following for
* each:
*
*
* - Split string on ';' delimeter (which returns a single
item array if there is none)
*
* - Make each array item into a query (eg, &status=Closed)
*
* - Concat them together (eg,&status=Active&status=Closed)
*/
$features = isset($_GET['sr_features']) ? $_GET['sr_features'] : '';
$features_string = "";
if(!empty($features)) {
foreach((array)$features as $key => $feature) {
$features_string .= "&features=$feature";
}
}
$agents = isset($_GET['sr_agent']) ? $_GET['sr_agent'] : '';
$agents_string = "";
if(!empty($agents)) {
foreach((array)$agents as $key => $agent) {
$agents_string .= "&agent=$agent";
}
}
$amenities = isset($_GET['sr_amenities']) ? $_GET['sr_amenities'] : '';
$amenities_string = "";
if(!empty($amenities)) {
foreach((array)$amenities as $key => $amenity) {
$amenities_string .= "&amenities=$amenity";
}
}
/** Parse multiple postalCodes from short-code parameter */
$postalCodesData = SimplyRetsCustomPostPages::parseGetParameter(
"sr_postalCodes",
"postalCodes",
$_GET
);
$postalCodes = $postalCodesData["param"];
$postalCodes_string = $postalCodesData["query"];
/** Parse multiple cities from short-code parameter */
$citiesData = SimplyRetsCustomPostPages::parseGetParameter(
"sr_cities",
"cities",
$_GET
);
$cities = $citiesData["param"];
$cities_string = $citiesData["query"];
/** Parse multiple counties from short-code parameter */
$countiesData = SimplyRetsCustomPostPages::parseGetParameter(
"sr_counties",
"counties",
$_GET
);
$counties = $countiesData["param"];
$counties_string = $countiesData["query"];
/** Parse multiple neighborhoods from short-code parameter */
$neighborhoodsData = SimplyRetsCustomPostPages::parseGetParameter(
"sr_neighborhoods",
"neighborhoods",
$_GET
);
$neighborhoods = $neighborhoodsData["param"];
$neighborhoods_string = $neighborhoodsData["query"];
/**
* 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.
*
* Note: We're only using params that weren't transformed
* above.
*/
$listing_params = array(
"brokers" => $brokers,
"minbeds" => $minbeds,
"maxbeds" => $maxbeds,
"minbaths" => $minbaths,
"maxbaths" => $maxbaths,
"minprice" => $minprice,
"maxprice" => $maxprice,
"water" => $water,
/** Pagination */
"limit" => $limit,
"offset" => $offset,
/** Advanced Search */
"lotsize" => $lotsize,
"area" => $area,
"sort" => $sort,
/** Multi MLS */
"vendor" => $vendor,
/**
Settings that need to be propogated through to
pagination. It's a bit awkward because these aren't
SimplyRETS query parameters, but it's the easiest
way to get them back on the other side right now.
*/
"map_position" => $map_position
);
$settings = array(
"map_position" => $map_position
);
/**
* 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.
*/
// Combine sr_q and _keywords into 1 string.
$sr_q = get_query_var('sr_q', '');
$sr_kw = get_query_var('sr_keywords', '');
$kw_string = implode("; ", array($sr_q) + array($sr_kw));
$next_atts = $listing_params + array(
"q" => $kw_string,
"agent" => get_query_var('sr_agent', ''),
"status" => $statuses_attribute,
"advanced" => $advanced == "true" ? "true" : "false",
"postalCodes" => $postalCodes,
"cities" => $cities,
"counties" => $counties,
"neighborhoods" => $neighborhoods
);
// Create a string of attributes to put on the
// [sr_search_form] short-code.
$filters_string = '';
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
. $postalCodes_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;
}
if( $post_type == $sr_post_type ) {
$query_object = get_queried_object();
$listing_params = get_post_meta( $query_object->ID, 'sr_filters', true );
if ( empty($listing_params) ) {
return $content;
}
// debug for viewing the search filters saved from the custom post page
// foreach ( $listing_params as $key=>$value ) {
// echo 'param: ' . $key . ' value: ' . $value . $br;
// }
// the simply rets api helper takes care of retrieving, parsing, and generating
// the markup for the listings to be shown on this page based off of the sr_filters
// saved for this post
$listings_content = SimplyRetsApiHelper::retrieveRetsListings( $listing_params );
$content = $content . $listings_content;
return $content;
}
return $content;
}
public static function srCreateDynamicPost( $posts ) {
// if we catch a singlelisting query, create a new post on the fly
global $wp_query;
$wpq = $wp_query->query;
if( (!empty($wpq['sr-listings']) AND $wpq['sr-listings'] == 'sr-search') AND
array_key_exists("listing_id", $wp_query->query_vars) AND
array_key_exists("listing_title", $wp_query->query_vars) OR
(!empty($wpq['sr-listings']) AND $wpq['sr-listings'] == "sr-single")
) {
$post_id = urldecode(get_query_var( 'listing_id', '' ));
$post_addr = urldecode(get_query_var( 'listing_title', '' ));
$post_price = urldecode(get_query_var( 'listing_price', '' ));
$listing_USD = $post_price == '' ? '' : '$' . number_format( $post_price );
$title_normalize = "background-color:transparent;padding:0px;";
$post_title = "{$post_addr} <span style='{$title_normalize}'><small><i> {$listing_USD}</i></small></span>";
$post = (object)array(
"ID" => $post_id,
"comment_count" => 0,
"comment_status" => "closed",
"ping_status" => "closed",
"post_author" => 1,
"post_name" => $post_id,
"post_date" => date("c"),
"post_date_gmt" => gmdate("c"),
"post_parent" => 0,
"post_status" => "publish",
"post_title" => $post_title,
"post_type" => "sr-listings"
);
return $posts + array($post);
}
// if we catch a search results query, create a new post on the fly
if(!empty($wpq['sr-listings']) AND $wpq['sr-listings'] == "sr-search") {
$post_id = get_query_var( 'sr_minprice', '9998' );
$post = (object)array(
"ID" => $post_id,
"comment_count" => 0,
"comment_status" => "closed",
"ping_status" => "closed",
"post_author" => 1,
"post_name" => "Search Listings",
"post_date" => date("c"),
"post_date_gmt" => gmdate("c"),
"post_parent" => 0,
"post_status" => "publish",
"post_title" => "Search Results",
"post_type" => "sr-listings"
);
return $posts + array($post);
}
return $posts;
}
}
?>