1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LSX Search Shortcode Class. |
4
|
|
|
* |
5
|
|
|
* @package lsx-search |
6
|
|
|
*/ |
7
|
|
|
class LSX_Search_Shortcode { |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Construct method. |
11
|
|
|
*/ |
12
|
|
|
public function __construct() { |
|
|
|
|
13
|
|
|
add_shortcode( 'lsx_search_form', array( $this, 'search_form' ) ); |
14
|
|
|
} |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Outputs the appropriate search form |
18
|
|
|
*/ |
19
|
|
|
public function search_form( $atts = array() ) { |
|
|
|
|
20
|
|
|
$classes = 'search-form lsx-search-form form-inline'; |
21
|
|
|
|
22
|
|
|
if ( isset( $atts['class'] ) ) { |
|
|
|
|
23
|
|
|
$classes .= $atts['class']; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$placeholder = __( 'Where do you want to go?', 'lsx-search' ); |
27
|
|
|
|
28
|
|
|
if ( isset( $atts['placeholder'] ) ) { |
|
|
|
|
29
|
|
|
$placeholder = $atts['placeholder']; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$action = '/'; |
33
|
|
|
|
34
|
|
|
if ( isset( $atts['action'] ) ) { |
|
|
|
|
35
|
|
|
$action = $atts['action']; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$method = 'get'; |
39
|
|
|
|
40
|
|
|
if ( isset( $atts['method'] ) ) { |
|
|
|
|
41
|
|
|
$method = $atts['method']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$button_label = __( 'Search', 'lsx-search' ); |
45
|
|
|
|
46
|
|
|
if ( isset( $atts['button_label'] ) ) { |
|
|
|
|
47
|
|
|
$button_label = $atts['button_label']; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$button_class = 'btn cta-btn '; |
51
|
|
|
|
52
|
|
|
if ( isset( $atts['button_class'] ) ) { |
|
|
|
|
53
|
|
|
$button_class .= $atts['button_class']; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$engine = false; |
57
|
|
|
|
58
|
|
|
if ( isset( $atts['engine'] ) ) { |
|
|
|
|
59
|
|
|
$engine = $atts['engine']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$engine_select = false; |
63
|
|
|
|
64
|
|
|
if ( isset( $atts['engine_select'] ) ) { |
|
|
|
|
65
|
|
|
$engine_select = true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$display_search_field = true; |
69
|
|
|
|
70
|
|
|
if ( isset( $atts['search_field'] ) ) { |
|
|
|
|
71
|
|
|
$display_search_field = (boolean) $atts['search_field']; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$facets = false; |
75
|
|
|
|
76
|
|
|
if ( isset( $atts['facets'] ) ) { |
|
|
|
|
77
|
|
|
$facets = $atts['facets']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$combo_box = false; |
81
|
|
|
|
82
|
|
|
if ( isset( $atts['combo_box'] ) ) { |
|
|
|
|
83
|
|
|
$combo_box = true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$return = ''; |
87
|
|
|
|
88
|
|
|
ob_start(); ?> |
89
|
|
|
|
90
|
|
|
<?php do_action( 'lsx_search_form_before' ); ?> |
91
|
|
|
|
92
|
|
|
<nav class="navbar navbar-light bg-light"> |
93
|
|
|
|
94
|
|
|
<form class="<?php echo esc_attr( $classes ); ?>" action="<?php echo esc_attr( $action ); ?>" method="<?php echo esc_attr( $method ); ?>"> |
95
|
|
|
|
96
|
|
|
<?php do_action( 'lsx_search_form_top' ); ?> |
97
|
|
|
|
98
|
|
|
<div class="input-group navbar-nav"> |
99
|
|
|
<?php if ( true === $display_search_field ) : ?> |
|
|
|
|
100
|
|
|
<div class="field"> |
101
|
|
|
<input class="search-field form-control" name="s" type="search" placeholder="<?php echo esc_attr( $placeholder ); ?>" autocomplete="off"> |
102
|
|
|
</div> |
103
|
|
|
<?php endif; ?> |
104
|
|
|
|
105
|
|
|
<?php if ( false !== $engine_select && false !== $engine && 'default' !== $engine ) : |
|
|
|
|
106
|
|
|
$engines = explode( '|',$engine ); ?> |
|
|
|
|
107
|
|
|
<div class="field engine-select"> |
108
|
|
|
<div class="dropdown nav-item"> |
109
|
|
|
<?php |
110
|
|
|
$plural = 's'; |
111
|
|
|
if ( 'accommodation' === $engine[0] ) { |
|
|
|
|
112
|
|
|
$plural = ''; |
113
|
|
|
} |
114
|
|
|
?> |
115
|
|
|
<button id="engine" data-selection="<?php echo esc_attr( $engines[0] ); ?>" class="btn border-btn btn-dropdown dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?php echo esc_html( ucwords( str_replace( '_', ' ',$engines[0] ) ) . $plural ); ?> <span class="caret"></span></button> |
116
|
|
|
<ul class="dropdown-menu"> |
117
|
|
|
<?php |
118
|
|
|
foreach ( $engines as $engine ) { |
|
|
|
|
119
|
|
|
$plural = 's'; |
120
|
|
|
if ( 'accommodation' === $engine ) { |
|
|
|
|
121
|
|
|
$plural = ''; |
122
|
|
|
} |
123
|
|
|
echo '<li><a data-value="' . esc_attr( $engine ) . '" href="#">' . esc_html( ucfirst( str_replace( '_', ' ',$engine ) ) . $plural ) . '</a></li>'; |
124
|
|
|
} |
125
|
|
|
?> |
126
|
|
|
</ul> |
127
|
|
|
</div> |
128
|
|
|
</div> |
129
|
|
|
<?php endif; ?> |
130
|
|
|
|
131
|
|
|
<?php if ( false !== $facets ) { |
|
|
|
|
132
|
|
|
$facets = explode( '|',$facets ); |
133
|
|
|
|
134
|
|
|
if ( ! is_array( $facets ) ) { |
|
|
|
|
135
|
|
|
$facets = array( $facets ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$field_class = 'field'; |
139
|
|
|
|
140
|
|
|
if ( false !== $combo_box ) { |
|
|
|
|
141
|
|
|
$this->combo_box( $facets ); |
142
|
|
|
$field_class .= ' combination-toggle hidden'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
foreach ( $facets as $facet ) { |
|
|
|
|
146
|
|
|
?> |
147
|
|
|
<div class="<?php echo wp_kses_post( $field_class ); ?>"> |
148
|
|
|
<?php |
149
|
|
|
$facet = FWP()->helper->get_facet_by_name( $facet ); |
|
|
|
|
150
|
|
|
if ( isset( $facet['source'] ) ) { |
|
|
|
|
151
|
|
|
$values = $this->get_form_facet( $facet['source'] ); |
152
|
|
|
} else { |
153
|
|
|
$values = array(); |
154
|
|
|
} |
155
|
|
|
$facet_display_type = apply_filters( 'lsx_search_form_field_type', 'select', $facet ); |
156
|
|
|
$this->display_form_field( $facet_display_type,$facet,$values,$combo_box ); |
157
|
|
|
?> |
158
|
|
|
</div> |
159
|
|
|
<?php |
160
|
|
|
} |
161
|
|
|
} ?> |
|
|
|
|
162
|
|
|
|
163
|
|
|
<div class="field submit-button"> |
164
|
|
|
<button class="<?php echo esc_attr( $button_class ); ?>" type="submit"><?php echo wp_kses_post( $button_label ); ?></button> |
165
|
|
|
</div> |
166
|
|
|
|
167
|
|
|
<?php if ( false === $engine_select && false !== $engine && 'default' !== $engine ) : ?> |
|
|
|
|
168
|
|
|
<input name="engine" type="hidden" value="<?php echo esc_attr( $engine ); ?>"> |
169
|
|
|
<?php endif; ?> |
170
|
|
|
</div> |
171
|
|
|
|
172
|
|
|
<?php do_action( 'lsx_search_form_bottom' ); ?> |
173
|
|
|
|
174
|
|
|
</form> |
175
|
|
|
|
176
|
|
|
</nav> |
177
|
|
|
|
178
|
|
|
<?php do_action( 'lsx_search_form_after' ); ?> |
179
|
|
|
<?php |
180
|
|
|
$return = ob_get_clean(); |
181
|
|
|
|
182
|
|
|
$return = preg_replace( '/[\n]+/', ' ', $return ); |
183
|
|
|
$return = preg_replace( '/[\t]+/', ' ', $return ); |
184
|
|
|
|
185
|
|
|
return $return; |
186
|
|
|
} |
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Outputs the combination selector |
190
|
|
|
*/ |
191
|
|
|
public function combo_box( $facets ) { |
|
|
|
|
192
|
|
|
?> |
193
|
|
|
<div class="field combination-dropdown"> |
194
|
|
|
<div class="dropdown"> |
195
|
|
|
<button data-selection="0" class="btn border-btn btn-dropdown dropdown-toggle btn-combination" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> |
196
|
|
|
<?php esc_attr_e( 'Select', 'lsx-search' ); ?> |
197
|
|
|
<span class="caret"></span> |
198
|
|
|
</button> |
199
|
|
|
<ul class="dropdown-menu"> |
200
|
|
|
|
201
|
|
|
<li style="display: none;"><a class="default" data-value="0" href="#"><?php esc_attr_e( 'Select ', 'lsx-search' ); ?></a></li> |
202
|
|
|
|
203
|
|
|
<?php foreach ( $facets as $facet ) { |
|
|
|
|
204
|
|
|
$facet = FWP()->helper->get_facet_by_name( $facet ); |
|
|
|
|
205
|
|
|
?> |
206
|
|
|
<li><a data-value="fwp_<?php echo wp_kses_post( $facet['name'] ); ?>" href="#"><?php echo wp_kses_post( $facet['label'] ); ?></a></li> |
207
|
|
|
<?php } ?> |
208
|
|
|
</ul> |
209
|
|
|
</div> |
210
|
|
|
</div> |
211
|
|
|
<?php |
212
|
|
|
} |
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Grabs the Values for the Facet in Question. |
216
|
|
|
*/ |
217
|
|
|
protected function get_form_facet( $facet_source = false ) { |
|
|
|
|
218
|
|
|
global $wpdb; |
219
|
|
|
|
220
|
|
|
$values = array(); |
221
|
|
|
$select = 'f.facet_value, f.facet_display_value'; |
222
|
|
|
$from = "{$wpdb->prefix}facetwp_index f"; |
|
|
|
|
223
|
|
|
$where = "f.facet_source = '{$facet_source}'"; |
|
|
|
|
224
|
|
|
|
225
|
|
|
//Check if the current facet is showing destinations. |
226
|
|
|
if ( stripos( $facet_source, 'destination_to' ) ) { |
|
|
|
|
227
|
|
|
$from .= " INNER JOIN {$wpdb->posts} p ON f.facet_value = p.ID"; |
|
|
|
|
228
|
|
|
$where .= " AND p.post_parent = '0'"; |
229
|
|
|
|
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$response = $wpdb->prepare( "SELECT {$select} FROM {$from} WHERE {$where}" );// WPCS: unprepared SQL OK. |
|
|
|
|
233
|
|
|
|
234
|
|
|
if ( ! empty( $response ) ) { |
|
|
|
|
235
|
|
|
foreach ( $response as $re ) { |
|
|
|
|
236
|
|
|
$display_value = $re->facet_display_value; |
237
|
|
|
if ( function_exists( 'pll_translate_string' ) ) { |
|
|
|
|
238
|
|
|
$current_lang = pll_current_language(); |
|
|
|
|
239
|
|
|
$display_value = pll_translate_string( $display_value, $current_lang ); |
240
|
|
|
} |
241
|
|
|
$display_value = apply_filters( 'lsx_search_facetwp_display_value', $display_value, $re->facet_value ); |
|
|
|
|
242
|
|
|
$values[ $re->facet_value ] = $display_value; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
asort( $values ); |
247
|
|
|
return $values; |
248
|
|
|
} |
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Change FaceWP pagination HTML to be equal main pagination (WP-PageNavi) |
252
|
|
|
*/ |
253
|
|
|
public function display_form_field( $type = 'select', $facet = array(), $values = array(), $combo = false ) { |
|
|
|
|
254
|
|
|
if ( empty( $facet ) ) { |
|
|
|
|
255
|
|
|
return; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$source = 'fwp_' . $facet['name']; |
259
|
|
|
|
260
|
|
|
switch ( $type ) { |
|
|
|
|
261
|
|
|
|
262
|
|
|
case 'select': ?> |
|
|
|
|
263
|
|
|
<div class="dropdown nav-item <?php if ( true === $combo ) { echo 'combination-dropdown'; } ?>"> |
|
|
|
|
264
|
|
|
<button data-selection="0" class="btn border-btn btn-dropdown dropdown-toggle" type="button" id="<?php echo wp_kses_post( $source ); ?>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> |
265
|
|
|
<?php echo esc_attr( apply_filters( 'lsx_search_facet_label', __( 'Select', 'lsx-search' ) . ' ' . wp_kses_post( $facet['label'] ) ) ); ?> |
266
|
|
|
<span class="caret"></span> |
267
|
|
|
</button> |
268
|
|
|
<ul class="dropdown-menu" aria-labelledby="<?php echo wp_kses_post( $source ); ?>"> |
269
|
|
|
<?php if ( ! empty( $values ) ) { ?> |
|
|
|
|
270
|
|
|
|
271
|
|
|
<li style="display: none;"> |
272
|
|
|
<a class="default" data-value="0" href="#"> |
273
|
|
|
<?php |
274
|
|
|
$facet_label = __( 'Select ', 'lsx-search' ) . ' ' . wp_kses_post( $facet['label'] ); |
275
|
|
|
$facet_label = apply_filters( 'lsx_search_facet_label', $facet_label ); |
276
|
|
|
echo esc_attr( $facet_label ); |
277
|
|
|
?> |
278
|
|
|
</a> |
279
|
|
|
</li> |
280
|
|
|
|
281
|
|
|
<?php foreach ( $values as $key => $value ) { ?> |
|
|
|
|
282
|
|
|
<li><a data-value="<?php echo wp_kses_post( $key ); ?>" href="#"><?php echo wp_kses_post( $value ); ?></a></li> |
283
|
|
|
<?php } ?> |
284
|
|
|
<?php } else { ?> |
285
|
|
|
<li><a data-value="0" href="#"><?php esc_attr_e( 'Please re-index your facets.', 'lsx-search' ); ?></a></li> |
286
|
|
|
<?php } ?> |
287
|
|
|
</ul> |
288
|
|
|
</div> |
289
|
|
|
<?php |
290
|
|
|
break; |
291
|
|
|
|
292
|
|
|
case 'datepicker': ?> |
|
|
|
|
293
|
|
|
<div class="datepicker nav-item"> |
294
|
|
|
<input autocomplete="off" class="datepicker-value" placeholder="<?php echo wp_kses_post( apply_filters( 'lsx_search_facet_label' , $facet['label'] ) ); ?>" name="<?php echo wp_kses_post( $source ); ?>" id="<?php echo wp_kses_post( $source ); ?>" type="text" value="" /> |
|
|
|
|
295
|
|
|
</div> |
296
|
|
|
<?php |
297
|
|
|
break; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
?> |
301
|
|
|
|
302
|
|
|
<?php } |
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.