Search & Filter - Version 1.2.5

Version Description

  • Fixed a PHP error when setting defaults for taxonomies - many users did not see this but resulted in unexpected behaviour
  • Fixed an error with post date sometimes being undefined for blank searches
  • Added argument empty_search_url - when a users submits the search form without any search preferences selected they will be redirected to this URL
  • Updated argument add_search_param - setting to 1 will force add a "?s=" to all urls generate by the plugin - this may help with the loading of search templates in some themes
Download this release

Release Info

Developer DesignsAndCode
Plugin Icon 128x128 Search & Filter
Version 1.2.5
Comparing to
See all releases

Code changes from version 1.2.4 to 1.2.5

Files changed (3) hide show
  1. of-taxonomy-walker.php +318 -318
  2. readme.txt +34 -9
  3. search-filter.php +1723 -1689
of-taxonomy-walker.php CHANGED
@@ -1,319 +1,319 @@
1
- <?php
2
-
3
- class Taxonomy_Walker extends Walker_Category {
4
-
5
-
6
- private $type = '';
7
- private $defaults = array();
8
- private $multidepth = 0; //manually calculate depth on multiselects
9
- private $multilastid = 0; //manually calculate depth on multiselects
10
- private $multilastdepthchange = 0; //manually calculate depth on multiselects
11
-
12
- function __construct($type = 'checkbox', $defaults = array()) {
13
- // fetch the list of term ids for the given post
14
- //$this->term_ids = wp_get_post_terms( $post_id, $taxonomy, 'fields=ids' );
15
- //var_dump($this->term_ids);
16
-
17
- $this->type = $type;
18
- $this->defaults = $defaults;
19
- }
20
-
21
- function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
22
- /*$display = false;
23
-
24
- $id = $element->term_id;
25
-
26
- $display = true;
27
- if ( isset( $children_elements[ $id ] ) ) {
28
- // the current term has children
29
- foreach ( $children_elements[ $id ] as $child ) {
30
- if ( in_array( $child->term_id, $this->term_ids ) ) {
31
- // one of the term's children is in the list
32
- $display = true;
33
- // can stop searching now
34
- break;
35
- }
36
- }
37
- }
38
-
39
- if ( $display )*/
40
- parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
41
- }
42
-
43
-
44
- function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 )
45
- {
46
-
47
- if($this->type=="list")
48
- {
49
- extract($args);
50
-
51
- $cat_name = esc_attr( $category->name );
52
- $cat_name = apply_filters( 'list_cats', $cat_name, $category );
53
- $link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
54
- if ( $use_desc_for_title == 0 || empty($category->description) )
55
- $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
56
- else
57
- $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
58
- $link .= '>';
59
- $link .= $cat_name . '</a>';
60
-
61
- if ( !empty($feed_image) || !empty($feed) ) {
62
- $link .= ' ';
63
-
64
- if ( empty($feed_image) )
65
- $link .= '(';
66
-
67
- $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
68
-
69
- if ( empty($feed) ) {
70
- $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
71
- } else {
72
- $title = ' title="' . $feed . '"';
73
- $alt = ' alt="' . $feed . '"';
74
- $name = $feed;
75
- $link .= $title;
76
- }
77
-
78
- $link .= '>';
79
-
80
- if ( empty($feed_image) )
81
- $link .= $name;
82
- else
83
- $link .= "<img src='$feed_image'$alt$title" . ' />';
84
-
85
- $link .= '</a>';
86
-
87
- if ( empty($feed_image) )
88
- $link .= ')';
89
- }
90
-
91
- if ( !empty($show_count) )
92
- $link .= ' (' . intval($category->count) . ')';
93
-
94
- if ( 'list' == $args['style'] ) {
95
- $output .= "\t<li";
96
- $class = 'cat-item cat-item-' . $category->term_id;
97
- if ( !empty($current_category) ) {
98
- $_current_category = get_term( $current_category, $category->taxonomy );
99
- if ( $category->term_id == $current_category )
100
- $class .= ' current-cat';
101
- elseif ( $category->term_id == $_current_category->parent )
102
- $class .= ' current-cat-parent';
103
- }
104
- $output .= ' class="' . $class . '"';
105
- $output .= ">$link\n";
106
- } else {
107
- $output .= "\t$link<br />\n";
108
- }
109
- }
110
- else if(($this->type=="checkbox")||($this->type=="radio"))
111
- {
112
- extract($args);
113
-
114
- $cat_name = esc_attr( $category->name );
115
- $cat_id = esc_attr( $category->term_id );
116
- $cat_name = apply_filters( 'list_cats', $cat_name, $category );
117
-
118
- //check a default has been set
119
- $checked = "";
120
- if($defaults)
121
- {
122
- $noselected = count($this->defaults);
123
-
124
- if(($noselected>0)&&(is_array($defaults)))
125
- {
126
- foreach($defaults as $defaultid)
127
- {
128
- if($defaultid==$cat_id)
129
- {
130
- $checked = ' checked="checked"';
131
- }
132
- }
133
- }
134
- }
135
-
136
- $link = "<label><input type='".$this->type."' name='".$name."[]' value='".$cat_id."'".$checked." /> ".$cat_name;
137
-
138
-
139
- if ( !empty($show_count) )
140
- $link .= ' (' . intval($category->count) . ')';
141
-
142
-
143
- $link .= "</label>";
144
-
145
- if ( 'list' == $args['style'] ) {
146
- $output .= "\t<li";
147
- $class = 'cat-item cat-item-' . $category->term_id;
148
- if ( !empty($current_category) ) {
149
- $_current_category = get_term( $current_category, $category->taxonomy );
150
- if ( $category->term_id == $current_category )
151
- $class .= ' current-cat';
152
- elseif ( $category->term_id == $_current_category->parent )
153
- $class .= ' current-cat-parent';
154
- }
155
- $output .= ' class="' . $class . '"';
156
- $output .= ">$link\n";
157
- } else {
158
- $output .= "\t$link<br />\n";
159
- }
160
- }
161
- else if($this->type=="multiselect")
162
- {
163
- extract($args);
164
-
165
- $cat_name = esc_attr( $category->name );
166
- $cat_id = esc_attr( $category->term_id );
167
- $cat_name = apply_filters( 'list_cats', $cat_name, $category );
168
-
169
- //check a default has been set
170
- $checked = "";
171
- if($defaults)
172
- {
173
- $noselected = count($this->defaults);
174
-
175
- if(($noselected>0)&&(is_array($defaults)))
176
- {
177
- foreach($defaults as $defaultid)
178
- {
179
- if($defaultid==$cat_id)
180
- {
181
- $checked = ' selected="selected"';
182
- }
183
- }
184
- }
185
- }
186
-
187
-
188
- /* Custom depth calculations! :/ */
189
- if($category->parent == 0)
190
- {//then this has no parent so reset depth
191
- $this->multidepth = 0;
192
- }
193
- else if($category->parent == $this->multilastid)
194
- {
195
- $this->multidepth++;
196
- $this->multilastdepthchange = $this->multilastid;
197
- }
198
- else if($category->parent == $this->multilastdepthchange)
199
- {//then this is also a child with the same parent so don't change depth
200
-
201
- }
202
- else
203
- {//then this has a different parent so must be lower depth
204
- if($this->multidepth>0)
205
- {
206
- $this->multidepth--;
207
- }
208
- }
209
-
210
- $pad = str_repeat('&nbsp;', $this->multidepth * 3);
211
- $link = "<option class=\"level-".$this->multidepth."\" value='".$cat_id."'$checked />".$pad.$cat_name;
212
-
213
- if ( !empty($show_count) )
214
- $link .= '&nbsp;&nbsp;(' . intval($category->count) . ')';
215
-
216
-
217
- $link .= "</option>";
218
- $output .= "\t$link\n";
219
-
220
-
221
- $this->multilastid = $cat_id;
222
-
223
-
224
- /*
225
- $pad = str_repeat('&nbsp;', $depth * 3);
226
-
227
- $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
228
- $cat_name = apply_filters('list_cats', $category->name, $category);
229
- if ( $category->term_id == $args['selected'] )
230
- $output .= ' selected="selected"';
231
- $output .= '>';
232
- $output .= $pad.$cat_name;
233
- if ( $args['show_count'] )
234
- $output .= '&nbsp;&nbsp;('. $category->count .')';
235
- $output .= "</option>\n";*/
236
- }
237
-
238
-
239
- }
240
-
241
- function end_el( &$output, $page, $depth = 0, $args = array() )
242
- {
243
- if($this->type=="list")
244
- {
245
- if ( 'list' != $args['style'] )
246
- return;
247
-
248
- $output .= "</li>\n";
249
- }
250
- else if(($this->type=="checkbox")||($this->type=="radio"))
251
- {
252
- if ( 'list' != $args['style'] )
253
- return;
254
-
255
- $output .= "</li>\n";
256
- }
257
- else if($this->type=="multiselect")
258
- {
259
- if ( 'list' != $args['style'] )
260
- return;
261
-
262
- $output .= "</option>\n";
263
- }
264
- }
265
-
266
- function start_lvl( &$output, $depth = 0, $args = array() )
267
- {
268
-
269
- if($this->type=="list")
270
- {
271
- if ( 'list' != $args['style'] )
272
- return;
273
-
274
- $indent = str_repeat("\t", $depth);
275
- $output .= "$indent<ul class='children'>\n";
276
- }
277
- else if(($this->type=="checkbox")||($this->type=="radio"))
278
- {
279
- if ( 'list' != $args['style'] )
280
- return;
281
-
282
- $indent = str_repeat("\t", $depth);
283
- $output .= "$indent<ul class='children'>\n";
284
- }
285
- else if($this->type=="multiselect")
286
- {
287
- /*if ( 'list' != $args['style'] )
288
- return;
289
-
290
- $indent = str_repeat("\t", $depth);
291
- $output .= "$indent<ul class='children'>\n";*/
292
- }
293
- }
294
-
295
- function end_lvl( &$output, $depth = 0, $args = array() ) {
296
- if($this->type=="list")
297
- {
298
- if ( 'list' != $args['style'] )
299
- return;
300
-
301
- $indent = str_repeat("\t", $depth);
302
- $output .= "$indent</ul>\n";
303
- }
304
- else if(($this->type=="checkbox")||($this->type=="radio"))
305
- {
306
- if ( 'list' != $args['style'] )
307
- return;
308
-
309
- $indent = str_repeat("\t", $depth);
310
- $output .= "$indent</ul>\n";
311
- }
312
- else if($this->type=="multiselect")
313
- {
314
-
315
- }
316
- }
317
- }
318
-
319
  ?>
1
+ <?php
2
+
3
+ class Taxonomy_Walker extends Walker_Category {
4
+
5
+
6
+ private $type = '';
7
+ private $defaults = array();
8
+ private $multidepth = 0; //manually calculate depth on multiselects
9
+ private $multilastid = 0; //manually calculate depth on multiselects
10
+ private $multilastdepthchange = 0; //manually calculate depth on multiselects
11
+
12
+ function __construct($type = 'checkbox', $defaults = array()) {
13
+ // fetch the list of term ids for the given post
14
+ //$this->term_ids = wp_get_post_terms( $post_id, $taxonomy, 'fields=ids' );
15
+ //var_dump($this->term_ids);
16
+
17
+ $this->type = $type;
18
+ $this->defaults = $defaults;
19
+ }
20
+
21
+ function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
22
+ /*$display = false;
23
+
24
+ $id = $element->term_id;
25
+
26
+ $display = true;
27
+ if ( isset( $children_elements[ $id ] ) ) {
28
+ // the current term has children
29
+ foreach ( $children_elements[ $id ] as $child ) {
30
+ if ( in_array( $child->term_id, $this->term_ids ) ) {
31
+ // one of the term's children is in the list
32
+ $display = true;
33
+ // can stop searching now
34
+ break;
35
+ }
36
+ }
37
+ }
38
+
39
+ if ( $display )*/
40
+ parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
41
+ }
42
+
43
+
44
+ function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 )
45
+ {
46
+
47
+ if($this->type=="list")
48
+ {
49
+ extract($args);
50
+
51
+ $cat_name = esc_attr( $category->name );
52
+ $cat_name = apply_filters( 'list_cats', $cat_name, $category );
53
+ $link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
54
+ if ( $use_desc_for_title == 0 || empty($category->description) )
55
+ $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
56
+ else
57
+ $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
58
+ $link .= '>';
59
+ $link .= $cat_name . '</a>';
60
+
61
+ if ( !empty($feed_image) || !empty($feed) ) {
62
+ $link .= ' ';
63
+
64
+ if ( empty($feed_image) )
65
+ $link .= '(';
66
+
67
+ $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
68
+
69
+ if ( empty($feed) ) {
70
+ $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
71
+ } else {
72
+ $title = ' title="' . $feed . '"';
73
+ $alt = ' alt="' . $feed . '"';
74
+ $name = $feed;
75
+ $link .= $title;
76
+ }
77
+
78
+ $link .= '>';
79
+
80
+ if ( empty($feed_image) )
81
+ $link .= $name;
82
+ else
83
+ $link .= "<img src='$feed_image'$alt$title" . ' />';
84
+
85
+ $link .= '</a>';
86
+
87
+ if ( empty($feed_image) )
88
+ $link .= ')';
89
+ }
90
+
91
+ if ( !empty($show_count) )
92
+ $link .= ' (' . intval($category->count) . ')';
93
+
94
+ if ( 'list' == $args['style'] ) {
95
+ $output .= "\t<li";
96
+ $class = 'cat-item cat-item-' . $category->term_id;
97
+ if ( !empty($current_category) ) {
98
+ $_current_category = get_term( $current_category, $category->taxonomy );
99
+ if ( $category->term_id == $current_category )
100
+ $class .= ' current-cat';
101
+ elseif ( $category->term_id == $_current_category->parent )
102
+ $class .= ' current-cat-parent';
103
+ }
104
+ $output .= ' class="' . $class . '"';
105
+ $output .= ">$link\n";
106
+ } else {
107
+ $output .= "\t$link<br />\n";
108
+ }
109
+ }
110
+ else if(($this->type=="checkbox")||($this->type=="radio"))
111
+ {
112
+ extract($args);
113
+
114
+ $cat_name = esc_attr( $category->name );
115
+ $cat_id = esc_attr( $category->term_id );
116
+ $cat_name = apply_filters( 'list_cats', $cat_name, $category );
117
+
118
+ //check a default has been set
119
+ $checked = "";
120
+ if($defaults)
121
+ {
122
+ $noselected = count($this->defaults);
123
+
124
+ if(($noselected>0)&&(is_array($defaults)))
125
+ {
126
+ foreach($defaults as $defaultid)
127
+ {
128
+ if($defaultid==$cat_id)
129
+ {
130
+ $checked = ' checked="checked"';
131
+ }
132
+ }
133
+ }
134
+ }
135
+
136
+ $link = "<label><input type='".$this->type."' name='".$name."[]' value='".$cat_id."'".$checked." /> ".$cat_name;
137
+
138
+
139
+ if ( !empty($show_count) )
140
+ $link .= ' (' . intval($category->count) . ')';
141
+
142
+
143
+ $link .= "</label>";
144
+
145
+ if ( 'list' == $args['style'] ) {
146
+ $output .= "\t<li";
147
+ $class = 'cat-item cat-item-' . $category->term_id;
148
+ if ( !empty($current_category) ) {
149
+ $_current_category = get_term( $current_category, $category->taxonomy );
150
+ if ( $category->term_id == $current_category )
151
+ $class .= ' current-cat';
152
+ elseif ( $category->term_id == $_current_category->parent )
153
+ $class .= ' current-cat-parent';
154
+ }
155
+ $output .= ' class="' . $class . '"';
156
+ $output .= ">$link\n";
157
+ } else {
158
+ $output .= "\t$link<br />\n";
159
+ }
160
+ }
161
+ else if($this->type=="multiselect")
162
+ {
163
+ extract($args);
164
+
165
+ $cat_name = esc_attr( $category->name );
166
+ $cat_id = esc_attr( $category->term_id );
167
+ $cat_name = apply_filters( 'list_cats', $cat_name, $category );
168
+
169
+ //check a default has been set
170
+ $checked = "";
171
+ if($defaults)
172
+ {
173
+ $noselected = count($this->defaults);
174
+
175
+ if(($noselected>0)&&(is_array($defaults)))
176
+ {
177
+ foreach($defaults as $defaultid)
178
+ {
179
+ if($defaultid==$cat_id)
180
+ {
181
+ $checked = ' selected="selected"';
182
+ }
183
+ }
184
+ }
185
+ }
186
+
187
+
188
+ /* Custom depth calculations! :/ */
189
+ if($category->parent == 0)
190
+ {//then this has no parent so reset depth
191
+ $this->multidepth = 0;
192
+ }
193
+ else if($category->parent == $this->multilastid)
194
+ {
195
+ $this->multidepth++;
196
+ $this->multilastdepthchange = $this->multilastid;
197
+ }
198
+ else if($category->parent == $this->multilastdepthchange)
199
+ {//then this is also a child with the same parent so don't change depth
200
+
201
+ }
202
+ else
203
+ {//then this has a different parent so must be lower depth
204
+ if($this->multidepth>0)
205
+ {
206
+ $this->multidepth--;
207
+ }
208
+ }
209
+
210
+ $pad = str_repeat('&nbsp;', $this->multidepth * 3);
211
+ $link = "<option class=\"level-".$this->multidepth."\" value='".$cat_id."'$checked />".$pad.$cat_name;
212
+
213
+ if ( !empty($show_count) )
214
+ $link .= '&nbsp;&nbsp;(' . intval($category->count) . ')';
215
+
216
+
217
+ $link .= "</option>";
218
+ $output .= "\t$link\n";
219
+
220
+
221
+ $this->multilastid = $cat_id;
222
+
223
+
224
+ /*
225
+ $pad = str_repeat('&nbsp;', $depth * 3);
226
+
227
+ $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
228
+ $cat_name = apply_filters('list_cats', $category->name, $category);
229
+ if ( $category->term_id == $args['selected'] )
230
+ $output .= ' selected="selected"';
231
+ $output .= '>';
232
+ $output .= $pad.$cat_name;
233
+ if ( $args['show_count'] )
234
+ $output .= '&nbsp;&nbsp;('. $category->count .')';
235
+ $output .= "</option>\n";*/
236
+ }
237
+
238
+
239
+ }
240
+
241
+ function end_el( &$output, $page, $depth = 0, $args = array() )
242
+ {
243
+ if($this->type=="list")
244
+ {
245
+ if ( 'list' != $args['style'] )
246
+ return;
247
+
248
+ $output .= "</li>\n";
249
+ }
250
+ else if(($this->type=="checkbox")||($this->type=="radio"))
251
+ {
252
+ if ( 'list' != $args['style'] )
253
+ return;
254
+
255
+ $output .= "</li>\n";
256
+ }
257
+ else if($this->type=="multiselect")
258
+ {
259
+ if ( 'list' != $args['style'] )
260
+ return;
261
+
262
+ $output .= "</option>\n";
263
+ }
264
+ }
265
+
266
+ function start_lvl( &$output, $depth = 0, $args = array() )
267
+ {
268
+
269
+ if($this->type=="list")
270
+ {
271
+ if ( 'list' != $args['style'] )
272
+ return;
273
+
274
+ $indent = str_repeat("\t", $depth);
275
+ $output .= "$indent<ul class='children'>\n";
276
+ }
277
+ else if(($this->type=="checkbox")||($this->type=="radio"))
278
+ {
279
+ if ( 'list' != $args['style'] )
280
+ return;
281
+
282
+ $indent = str_repeat("\t", $depth);
283
+ $output .= "$indent<ul class='children'>\n";
284
+ }
285
+ else if($this->type=="multiselect")
286
+ {
287
+ /*if ( 'list' != $args['style'] )
288
+ return;
289
+
290
+ $indent = str_repeat("\t", $depth);
291
+ $output .= "$indent<ul class='children'>\n";*/
292
+ }
293
+ }
294
+
295
+ function end_lvl( &$output, $depth = 0, $args = array() ) {
296
+ if($this->type=="list")
297
+ {
298
+ if ( 'list' != $args['style'] )
299
+ return;
300
+
301
+ $indent = str_repeat("\t", $depth);
302
+ $output .= "$indent</ul>\n";
303
+ }
304
+ else if(($this->type=="checkbox")||($this->type=="radio"))
305
+ {
306
+ if ( 'list' != $args['style'] )
307
+ return;
308
+
309
+ $indent = str_repeat("\t", $depth);
310
+ $output .= "$indent</ul>\n";
311
+ }
312
+ else if($this->type=="multiselect")
313
+ {
314
+
315
+ }
316
+ }
317
+ }
318
+
319
  ?>
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: DesignsAndCode
3
  Donate link:
4
  Tags: category, filter, taxonomy, search, wordpress, post type, post date
5
  Requires at least: 3.5
6
- Tested up to: 3.8
7
- Stable tag: 1.2.4
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -34,6 +34,12 @@ The documentation has been updated to include examples almost all configurable o
34
 
35
  == Changelog ==
36
 
 
 
 
 
 
 
37
  = 1.2.4 =
38
  * Fixed a bug created in 1.2.3 when doing an empty search
39
 
@@ -107,13 +113,32 @@ The documentation has been updated to include examples almost all configurable o
107
 
108
  == Description ==
109
 
110
- Search & Filter is a simple search and filtering plugin for WordPress. It is essentially an advancement of the WordPress search box, adding taxonomy, post type and post date filters to really refine your searches.
111
-
112
- You can search by Category, Tag, Custom Taxonomy, Post Type, Post Date or any combination of these easily - you can even remove the search box and simply use it as a filtering system for your posts and pages. Taxonomies and Post Types can be displayed as dropdown selects, checkboxes, radio buttons or multi selects.
113
-
114
- = Links =
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
- * [Search & Filter Documentation](http://docs.designsandcode.com/search-filter/)
117
- * [Search & Filter Discussion](http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/)
118
 
119
 
3
  Donate link:
4
  Tags: category, filter, taxonomy, search, wordpress, post type, post date
5
  Requires at least: 3.5
6
+ Tested up to: 4
7
+ Stable tag: 1.2.5
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
34
 
35
  == Changelog ==
36
 
37
+ = 1.2.5 =
38
+ * Fixed a PHP error when setting defaults for taxonomies - many users did not see this but resulted in unexpected behaviour
39
+ * Fixed an error with post date sometimes being undefined for blank searches
40
+ * Added argument `empty_search_url` - when a users submits the search form without any search preferences selected they will be redirected to this URL
41
+ * Updated argument `add_search_param` - setting to `1` will force add a "?s=" to all urls generate by the plugin - this may help with the loading of search templates in some themes
42
+
43
  = 1.2.4 =
44
  * Fixed a bug created in 1.2.3 when doing an empty search
45
 
113
 
114
  == Description ==
115
 
116
+ Search & Filter is a simple search and filtering plugin for WordPress - it is an advancement of the WordPress search box.
117
+
118
+ You can search by Category, Tag, Custom Taxonomy, Post Type, Post Date or any combination of these easily to really refine your searches - remove the search box and use it as a filtering system for your posts and pages. Fields can be displayed as dropdowns, checkboxes, radio buttons or multi selects.
119
+
120
+ **Links:** [Search & Filter Documentation](http://docs.designsandcode.com/search-filter/) | [Search & Filter Discussion](http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/)
121
+
122
+ = New: Search & Filter Pro =
123
+
124
+
125
+ * View live demo >> [demo 1](http://demo.designsandcode.com/sfpro-movie-reviews/) | [demo 2](http://demo.designsandcode.com/sfpro-woo-mystile/product-search/) | [video](http://www.designsandcode.com/wordpress-plugins/search-filter-pro/)
126
+ * Search **Custom Fields**, **Post Meta**, **Authors**, Post Types, Post Dates, Taxonomies, Tags, Categories
127
+ * Use **AJAX** to display results - no more page reloading!
128
+ * Search **Post Meta/Custom Fields** with checkboxes, radio buttons, dropdowns, multiselects or comboboxes
129
+ * jQuery range slider, date pickers and **auto-complete comboboxes** for selects and multiselects
130
+ * Order Results Field - users can order results by meta value, Post ID, author, title, name, date, date modified, parent ID, random, comment count and menu order
131
+ * Drag & Drop editor
132
+ * Use custom templates
133
+ * Create as many fields and different search forms as you like
134
+ * Use for blogs, reviews sites, news sites, property sites and more.
135
+ * Use for your online shop - tested and compatible with **WooCommerce**, **WP eCommerce**, **Easy Digital Downloads**
136
+ * Place anywhere in your themes and posts using shortcodes and widgets
137
+ * Works with **WPML**
138
+ * Works with **Advanced Custom Fields**
139
+ * Extremely easy to use admin UI, fully integrated with WP 3.8+
140
+ * **Dedicated Support**
141
+ * [More info >>](http://www.designsandcode.com/wordpress-plugins/search-filter-pro/)
142
 
 
 
143
 
144
 
search-filter.php CHANGED
@@ -1,1690 +1,1724 @@
1
- <?php
2
- /*
3
- Plugin Name: Search & Filter
4
- Plugin URI: http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/
5
- Description: Search and Filtering system for Pages, Posts, Categories, Tags and Taxonomies
6
- Author: Designs & Code
7
- Author URI: http://www.designsandcode.com/
8
- Version: 1.2.4
9
- Text Domain: searchandfilter
10
- License: GPLv2
11
- */
12
-
13
- // TO DO - i18n http://codex.wordpress.org/I18n_for_WordPress_Developers
14
-
15
- /*
16
- * Set up Plugin Globals
17
- */
18
- if (!defined('SEARCHANDFILTER_VERSION_NUM'))
19
- define('SEARCHANDFILTER_VERSION_NUM', '1.2.4');
20
-
21
- if (!defined('SEARCHANDFILTER_THEME_DIR'))
22
- define('SEARCHANDFILTER_THEME_DIR', ABSPATH . 'wp-content/themes/' . get_template());
23
-
24
- if (!defined('SEARCHANDFILTER_PLUGIN_NAME'))
25
- define('SEARCHANDFILTER_PLUGIN_NAME', trim(dirname(plugin_basename(__FILE__)), '/'));
26
-
27
- if (!defined('SEARCHANDFILTER_PLUGIN_DIR'))
28
- define('SEARCHANDFILTER_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . SEARCHANDFILTER_PLUGIN_NAME);
29
-
30
- if (!defined('SEARCHANDFILTER_PLUGIN_URL'))
31
- define('SEARCHANDFILTER_PLUGIN_URL', WP_PLUGIN_URL . '/' . SEARCHANDFILTER_PLUGIN_NAME);
32
-
33
- if (!defined('SEARCHANDFILTER_BASENAME'))
34
- define('SEARCHANDFILTER_BASENAME', plugin_basename(__FILE__));
35
-
36
- if (!defined('SEARCHANDFILTER_VERSION_KEY'))
37
- define('SEARCHANDFILTER_VERSION_KEY', 'searchandfilter_version');
38
-
39
-
40
-
41
- //form prefix for plugin
42
- if (!defined('SF_FPRE'))
43
- define('SF_FPRE', 'of');
44
-
45
- add_option(SEARCHANDFILTER_VERSION_KEY, SEARCHANDFILTER_VERSION_NUM);
46
-
47
- /*
48
- * Set up Plugin Globals
49
- */
50
- if ( ! class_exists( 'SearchAndFilter' ) )
51
- {
52
- class SearchAndFilter
53
- {
54
- private $has_form_posted = false;
55
- private $hasqmark = false;
56
- private $hassearchquery = false;
57
- private $urlparams = "/";
58
- private $searchterm = "";
59
- private $tagid = 0;
60
- private $catid = 0;
61
- private $defaults = array();
62
- private $frmreserved = array();
63
- private $taxonomylist = array();
64
- private $add_search_param = 1;
65
-
66
- public function __construct()
67
- {
68
-
69
- // Set up reserved fields
70
- $this->frmreserved = array(SF_FPRE."category", SF_FPRE."search", SF_FPRE."post_tag", SF_FPRE."submitted", SF_FPRE."post_date", SF_FPRE."post_types");
71
- $this->frmqreserved = array(SF_FPRE."category_name", SF_FPRE."s", SF_FPRE."tag", SF_FPRE."submitted", SF_FPRE."post_date", SF_FPRE."post_types"); //same as reserved
72
-
73
- //add query vars
74
- add_filter('query_vars', array($this,'add_queryvars') );
75
-
76
- //filter post type & date if it is set
77
- add_filter('pre_get_posts', array($this,'filter_query_post_types'));
78
- add_filter('pre_get_posts', array($this,'filter_query_post_date'));
79
-
80
- //add_filter('pre_get_posts',array($this, 'fix_blank_search')); //temporaril disabled
81
-
82
- // Add shortcode support for widgets
83
- add_shortcode('searchandfilter', array($this, 'shortcode'));
84
- add_filter('widget_text', 'do_shortcode');
85
-
86
- // Check the header to see if the form has been submitted
87
- add_action( 'get_header', array( $this, 'check_posts' ) );
88
-
89
- // Add styles
90
- add_action( 'wp_enqueue_scripts', array($this, 'of_enqueue_styles') );
91
- add_action( 'admin_enqueue_scripts', array($this, 'of_enqueue_admin_ss') );
92
-
93
- }
94
-
95
- public function of_enqueue_styles()
96
- {
97
- wp_enqueue_style( 'searchandfilter', SEARCHANDFILTER_PLUGIN_URL . '/style.css', false, 1.0, 'all' );
98
- }
99
- public function of_enqueue_admin_ss($hook)
100
- {
101
- if( 'toplevel_page_searchandfilter-settings' == $hook )
102
- {
103
- wp_enqueue_style( 'of_syntax_style', SEARCHANDFILTER_PLUGIN_URL.'/admin/github.css', false, 1.0, 'all' ); //more highlight styles http://softwaremaniacs.org/media/soft/highlight/test.html
104
- wp_enqueue_style( 'of_style', SEARCHANDFILTER_PLUGIN_URL.'/admin/style.css', false, 1.0, 'all' );
105
- wp_enqueue_script( 'of_syntax_script', SEARCHANDFILTER_PLUGIN_URL.'/admin/syntax.highlight.min.js' );
106
- }
107
- }
108
-
109
- public function shortcode($atts, $content = null)
110
- {
111
- // extract the attributes into variables
112
- extract(shortcode_atts(array(
113
-
114
- 'fields' => null,
115
- 'taxonomies' => null, //will be deprecated - use `fields` instead
116
- 'submit_label' => null,
117
- 'submitlabel' => null, //will be deprecated - use `submit_label` instead
118
- 'search_placeholder' => "Search &hellip;",
119
- 'types' => "",
120
- 'type' => "", //will be deprecated - use `types` instead
121
- 'headings' => "",
122
- 'all_items_labels' => "",
123
- 'class' => "",
124
- 'post_types' => "",
125
- 'hierarchical' => "",
126
- 'hide_empty' => "",
127
- 'order_by' => "",
128
- 'show_count' => "",
129
- 'order_dir' => "",
130
- 'operators' => "",
131
- 'add_search_param' => ""
132
-
133
- ), $atts));
134
-
135
- //init `fields`
136
- if($fields!=null)
137
- {
138
- $fields = explode(",",$fields);
139
- }
140
- else
141
- {
142
- $fields = explode(",",$taxonomies);
143
- }
144
-
145
- $this->taxonomylist = $fields;
146
- $nofields = count($fields);
147
-
148
- if($add_search_param==0)
149
- {
150
- $this->add_search_param = $add_search_param;
151
- }
152
-
153
- //init `submitlabel`
154
- if($submitlabel!=null)
155
- {//then the old "submitlabel" has been supplied
156
-
157
- if($submit_label==null)
158
- {
159
- //then the new label has not been supplied so do nothing
160
- $submit_label = $submitlabel;
161
- }
162
- else
163
- {
164
- //then the new label has been supplied so take the new label value
165
- //$submit_label = $submit_label;
166
- }
167
- }
168
- else if($submitlabel==null)
169
- {
170
- if($submit_label==null)
171
- {//default value
172
- $submit_label = "Submit";
173
- }
174
- }
175
-
176
- //init `post_types`
177
- if($post_types!="")
178
- {
179
- $post_types = explode(",",$post_types);
180
- }
181
- else
182
- {
183
- if(in_array("post_types", $fields))
184
- {
185
- $post_types = array("all");
186
- }
187
-
188
- }
189
-
190
- //init `hierarchical`
191
- if($hierarchical!="")
192
- {
193
- $hierarchical = explode(",",$hierarchical);
194
- }
195
- else
196
- {
197
- $hierarchical = array("");
198
- }
199
-
200
- //init `hide_empty`
201
- if($hide_empty!="")
202
- {
203
- $hide_empty = explode(",",$hide_empty);
204
- }
205
- else
206
- {
207
- $hide_empty = array("");
208
- }
209
-
210
- //init `show_count`
211
- if($show_count!="")
212
- {
213
- $show_count = explode(",",$show_count);
214
- }
215
- else
216
- {
217
- $show_count = array();
218
- }
219
-
220
- //init `order_by`
221
- if($order_by!="")
222
- {
223
- $order_by = explode(",",$order_by);
224
- }
225
- else
226
- {
227
- $order_by = array("");
228
- }
229
-
230
- //init `order_dir`
231
- if($order_dir!="")
232
- {
233
- $order_dir = explode(",",$order_dir);
234
- }
235
- else
236
- {
237
- $order_dir = array("");
238
- }
239
-
240
- //init `operators`
241
- if($operators!="")
242
- {
243
- $operators = explode(",",$operators);
244
- }
245
- else
246
- {
247
- $operators = array("");
248
- }
249
-
250
-
251
- //init `labels`
252
- $labels = explode(",",$headings);
253
-
254
- if(!is_array($labels))
255
- {
256
- $labels = array();
257
- }
258
-
259
- //init `all_items_labels`
260
- $all_items_labels = explode(",",$all_items_labels);
261
-
262
- if(!is_array($all_items_labels))
263
- {
264
- $all_items_labels = array();
265
- }
266
-
267
- //init `types`
268
- if($types!=null)
269
- {
270
- $types = explode(",",$types);
271
- }
272
- else
273
- {
274
- $types = explode(",",$type);
275
- }
276
-
277
- if(!is_array($types))
278
- {
279
- $types = array();
280
- }
281
-
282
-
283
- //Loop through Fields and set up default vars
284
- for($i=0; $i<$nofields; $i++)
285
- {//loop through all fields
286
-
287
- //set up types
288
- if(isset($types[$i]))
289
- {
290
- if($fields[$i] == 'post_date')
291
- {//check for post date field
292
-
293
- if(($types[$i]!="date")&&($types[$i]!="daterange"))
294
- {//if not expected value
295
-
296
- $types[$i] = "date"; //use default
297
- }
298
- }
299
- else
300
- {//everything else can use a standard form input - checkbox/radio/dropdown/list/multiselect
301
-
302
- if(($types[$i]!="select")&&($types[$i]!="checkbox")&&($types[$i]!="radio")&&($types[$i]!="list")&&($types[$i]!="multiselect"))
303
- {//no accepted type matched - non compatible type defined by user
304
-
305
- $types[$i] = "select"; //use default
306
- }
307
- }
308
- }
309
- else
310
- {//omitted, so set default
311
-
312
- if($fields[$i] == 'post_date')
313
- {
314
- $types[$i] = "date";
315
- }
316
- else
317
- {
318
- $types[$i] = "select";
319
- }
320
- }
321
-
322
- //setup labels
323
- if(!isset($labels[$i]))
324
- {
325
- $labels[$i] = "";
326
- }
327
-
328
- //setup all_items_labels
329
- if(!isset($all_items_labels[$i]))
330
- {
331
- $all_items_labels[$i] = "";
332
- }
333
-
334
-
335
- if(isset($order_by[$i]))
336
- {
337
- if(($order_by[$i]!="id")&&($order_by[$i]!="name")&&($order_by[$i]!="slug")&&($order_by[$i]!="count")&&($order_by[$i]!="term_group"))
338
- {
339
- $order_by[$i] = "name"; //use default - possible typo or use of unknown value
340
- }
341
- }
342
- else
343
- {
344
- $order_by[$i] = "name"; //use default
345
- }
346
-
347
- if(isset($order_dir[$i]))
348
- {
349
- if(($order_dir[$i]!="asc")&&($order_dir[$i]!="desc"))
350
- {//then order_dir is not a wanted value
351
-
352
- $order_dir[$i] = "asc"; //set to default
353
- }
354
- }
355
- else
356
- {
357
- $order_dir[$i] = "asc"; //use default
358
- }
359
-
360
- if(isset($operators[$i]))
361
- {
362
- if(($operators[$i]!="and")&&($operators[$i]!="or"))
363
- {
364
- $operators[$i] = "and"; //else use default - possible typo or use of unknown value
365
- }
366
- }
367
- else
368
- {
369
- $operators[$i] = "and"; //use default
370
- }
371
-
372
- }
373
-
374
- //set all form defaults / dropdowns etc
375
- $this->set_defaults();
376
-
377
- return $this->get_search_filter_form($submit_label, $search_placeholder, $fields, $types, $labels, $hierarchical, $hide_empty, $show_count, $post_types, $order_by, $order_dir, $operators, $all_items_labels, $class);
378
- }
379
-
380
-
381
- function add_queryvars( $qvars )
382
- {
383
- $qvars[] = 'post_types';
384
- $qvars[] = 'post_date';
385
- return $qvars;
386
- }
387
-
388
- function filter_query_post_types($query)
389
- {
390
- global $wp_query;
391
-
392
- if(($query->is_main_query())&&(!is_admin()))
393
- {
394
- if(isset($wp_query->query['post_types']))
395
- {
396
- $search_all = false;
397
-
398
- $post_types = explode(",",esc_attr($wp_query->query['post_types']));
399
- if(isset($post_types[0]))
400
- {
401
- if(count($post_types)==1)
402
- {
403
- if($post_types[0]=="all")
404
- {
405
- $search_all = true;
406
- }
407
- }
408
- }
409
- if($search_all)
410
- {
411
- $post_types = get_post_types( '', 'names' );
412
- $query->set('post_type', $post_types); //here we set the post types that we want WP to search
413
- }
414
- else
415
- {
416
- $query->set('post_type', $post_types); //here we set the post types that we want WP to search
417
- }
418
- }
419
- }
420
-
421
- return $query;
422
- }
423
-
424
-
425
- function limit_date_range_query( $where )
426
- {
427
- global $wp_query;
428
-
429
- //get post dates into array
430
- $post_date = explode("+", esc_attr(urlencode($wp_query->query['post_date'])));
431
-
432
- if (count($post_date) > 1 && $post_date[0] != $post_date[1])
433
- {
434
- $date_query = array();
435
-
436
- if (!empty($post_date[0]))
437
- {
438
- $date_query['after'] = date('Y-m-d 00:00:00', strtotime($post_date[0]));
439
- }
440
-
441
- if (!empty($post_date[1]))
442
- {
443
- $date_query['before'] = date('Y-m-d 23:59:59', strtotime($post_date[1]));
444
- }
445
-
446
- }
447
-
448
- // Append fragment to WHERE clause to select posts newer than the past week.
449
- $where .= " AND post_date >='" . $date_query['after'] . "' AND post_date <='" . $date_query['before'] . "'";
450
-
451
- return $where;
452
- }
453
-
454
- /**
455
- * Remove the filter limiting posts to the past week.
456
- *
457
- * Remove the filter after it runs so that it doesn't affect any other
458
- * queries that might be performed on the same page (eg. Recent Posts
459
- * widget).
460
- */
461
- function remove_limit_date_range_query()
462
- {
463
- remove_filter( 'posts_where', 'limit_date_range_query' );
464
- }
465
-
466
- function fix_blank_search($query)
467
- {//needs to be re-implemented
468
-
469
- if((isset($_GET['s'])) && (empty($_GET['s'])) && ($query->is_main_query()))
470
- {
471
- $query->is_search = true;
472
- $query->is_home = false;
473
- }
474
-
475
- }
476
-
477
- function filter_query_post_date($query)
478
- {
479
- global $wp_query;
480
-
481
- if(($query->is_main_query())&&(!is_admin()))
482
- {
483
- if(isset($wp_query->query['post_date']))
484
- {
485
- //get post dates into array
486
- $post_date = explode("+", esc_attr(urlencode($wp_query->query['post_date'])));
487
-
488
- if(!empty($post_date))
489
- {
490
- //if there is more than 1 post date and the dates are not the same
491
- if (count($post_date) > 1 && $post_date[0] != $post_date[1])
492
- {
493
- if((!empty($post_date[0]))&&(!empty($post_date[1])))
494
- {
495
- // Attach hook to filter WHERE clause.
496
- add_filter('posts_where', array($this,'limit_date_range_query'));
497
-
498
- // Remove the filter after it is executed.
499
- add_action('posts_selection', array($this,'remove_limit_date_range_query'));
500
- }
501
- }
502
- else
503
- { //else we are dealing with one date or both dates are the same (so need to find posts for a single day)
504
-
505
- if (!empty($post_date[0]))
506
- {
507
- $post_time = strtotime($post_date[0]);
508
- $query->set('year', date('Y', $post_time));
509
- $query->set('monthnum', date('m', $post_time));
510
- $query->set('day', date('d', $post_time));
511
- }
512
- }
513
- }
514
- }
515
- }
516
-
517
- return $query;
518
- }
519
-
520
- /*
521
- * check to set defaults - to be called after the shortcodes have been init so we can grab the wanted list of fields
522
- */
523
- public function set_defaults()
524
- {
525
- global $wp_query;
526
-
527
- $categories = array();
528
-
529
- if(isset($wp_query->query['category_name']))
530
- {
531
- $category_params = (preg_split("/[,\+ ]/", esc_attr($wp_query->query['category_name']))); //explode with 2 delims
532
-
533
- //$category_params = explode("+",esc_attr($wp_query->query['category_name']));
534
-
535
- foreach($category_params as $category_param)
536
- {
537
- $category = get_category_by_slug( $category_param );
538
- if(isset($category->cat_ID))
539
- {
540
- $categories[] = $category->cat_ID;
541
- }
542
- }
543
- }
544
-
545
- $this->defaults[SF_FPRE.'category'] = $categories;
546
-
547
-
548
- //grab search term for prefilling search input
549
- if(isset($wp_query->query['s']))
550
- {//!"�$%^&*()
551
- $this->searchterm = trim(get_search_query());
552
- }
553
-
554
- //check to see if tag is set
555
-
556
- $tags = array();
557
-
558
- if(isset($wp_query->query['tag']))
559
- {
560
- $tag_params = (preg_split("/[,\+ ]/", esc_attr($wp_query->query['tag']))); //explode with 2 delims
561
- //$tag_params = explode("+",esc_attr($wp_query->query['tag']));
562
-
563
- foreach($tag_params as $tag_param)
564
- {
565
- $tag = get_term_by("slug",$tag_param, "post_tag");
566
- if(isset($tag->term_id))
567
- {
568
- $tags[] = $tag->term_id;
569
- }
570
- }
571
- }
572
-
573
- $this->defaults[SF_FPRE.'post_tag'] = $tags;
574
-
575
- $taxs = array();
576
- //loop through all the query vars
577
- foreach($wp_query->query as $key=>$val)
578
- {
579
- if(!in_array(SF_FPRE.$key, $this->frmqreserved))
580
- {//make sure the get is not a reserved get as they have already been handled above
581
-
582
- //now check it is a desired key
583
- if(in_array($key, $this->taxonomylist))
584
- {
585
- $taxslug = ($val);
586
- //$tax_params = explode("+",esc_attr($taxslug));
587
-
588
- $tax_params = (preg_split("/[,\+ ]/", esc_attr($taxslug))); //explode with 2 delims
589
-
590
- foreach($tax_params as $tax_param)
591
- {
592
- $tax = get_term_by("slug",$tax_param, $key);
593
-
594
- if(isset($tax->term_id))
595
- {
596
- $taxs[] = $tax->term_id;
597
- }
598
- }
599
-
600
- $this->defaults[SF_FPRE.$key] = $taxs;
601
- }
602
- }
603
- }
604
-
605
- $post_date = array("","");
606
- if(isset($wp_query->query['post_date']))
607
- {
608
- $post_date = explode("+", esc_attr(urlencode($wp_query->query['post_date'])));
609
- if(count($post_date)==1)
610
- {
611
- $post_date[1] = "";
612
- }
613
- }
614
- $this->defaults[SF_FPRE.'post_date'] = $post_date;
615
-
616
-
617
- $post_types = array();
618
- if(isset($wp_query->query['post_types']))
619
- {
620
- $post_types = explode(",",esc_attr($wp_query->query['post_types']));
621
- }
622
- $this->defaults[SF_FPRE.'post_types'] = $post_types;
623
-
624
- }
625
-
626
- /*
627
- * check to see if form has been submitted and handle vars
628
- */
629
-
630
- public function check_posts()
631
- {
632
- if(isset($_POST[SF_FPRE.'submitted']))
633
- {
634
- if($_POST[SF_FPRE.'submitted']==="1")
635
- {
636
- //set var to confirm the form was posted
637
- $this->has_form_posted = true;
638
- }
639
- }
640
-
641
- /* CATEGORIES */
642
- if((isset($_POST[SF_FPRE.'category']))&&($this->has_form_posted))
643
- {
644
- $the_post_cat = ($_POST[SF_FPRE.'category']);
645
-
646
- //make the post an array for easy looping
647
- if(!is_array($_POST[SF_FPRE.'category']))
648
- {
649
- $post_cat[] = $the_post_cat;
650
- }
651
- else
652
- {
653
- $post_cat = $the_post_cat;
654
- }
655
- $catarr = array();
656
-
657
- foreach ($post_cat as $cat)
658
- {
659
- $cat = esc_attr($cat);
660
- $catobj = get_category($cat);
661
-
662
- if(isset($catobj->slug))
663
- {
664
- $catarr[] = $catobj->slug;
665
- //$catarr[] = $catobj->term_id;
666
- }
667
- }
668
-
669
- if(count($catarr)>0)
670
- {
671
- $operator = "+"; //default behaviour
672
-
673
- //check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects
674
- if(isset($_POST[SF_FPRE.'category_operator']))
675
- {
676
- if($_POST[SF_FPRE.'category_operator']=="and")
677
- {
678
- $operator = "+";
679
- }
680
- else if($_POST[SF_FPRE.'category_operator']=="or")
681
- {
682
- $operator = ",";
683
- }
684
- else
685
- {
686
- $operator = "+";
687
- }
688
- }
689
-
690
- $categories = implode($operator,$catarr);
691
-
692
- if(get_option('permalink_structure'))
693
- {
694
- //$catrel = trim(str_replace(home_url(), "", get_category_link()), "/").$categories."/"; //get full category link, remvoe the home url to get relative, trim traling slashed, the append slash at the end
695
- $category_base = (get_option( 'category_base' )=="") ? "category" : get_option( 'category_base' );
696
- $category_path = $category_base."/".$categories."/";
697
- $this->urlparams .= $category_path;
698
- }
699
- else
700
- {
701
- if(!$this->hasqmark)
702
- {
703
- $this->urlparams .= "?";
704
- $this->hasqmark = true;
705
- }
706
- else
707
- {
708
- $this->urlparams .= "&";
709
- }
710
- $this->urlparams .= "category_name=".$categories;
711
- }
712
- }
713
- }
714
-
715
- /* SEARCH BOX */
716
- if((isset($_POST[SF_FPRE.'search']))&&($this->has_form_posted))
717
- {
718
- $this->searchterm = trim(stripslashes($_POST[SF_FPRE.'search']));
719
-
720
- if($this->searchterm!="")
721
- {
722
- if(!$this->hasqmark)
723
- {
724
- $this->urlparams .= "?";
725
- $this->hasqmark = true;
726
- }
727
- else
728
- {
729
- $this->urlparams .= "&";
730
- }
731
- $this->urlparams .= "s=".urlencode($this->searchterm);
732
- $this->hassearchquery = true;
733
- }
734
- }
735
- if(!$this->hassearchquery)
736
- {
737
- if((isset($_POST[SF_FPRE.'add_search_param']))&&($this->has_form_posted))
738
- {//this is only set when a search box is displayed - it tells S&F to append a blank search to the URL to indicate a search has been submitted with no terms, however, still load the search template
739
-
740
- if(!$this->hasqmark)
741
- {
742
- $this->urlparams .= "?";
743
- $this->hasqmark = true;
744
- }
745
- else
746
- {
747
- $this->urlparams .= "&";
748
- }
749
- $this->urlparams .= "s=";
750
- }
751
- }
752
-
753
- /* TAGS */
754
- if((isset($_POST[SF_FPRE.'post_tag']))&&($this->has_form_posted))
755
- {
756
- $the_post_tag = ($_POST[SF_FPRE.'post_tag']);
757
-
758
- //make the post an array for easy looping
759
- if(!is_array($_POST[SF_FPRE.'post_tag']))
760
- {
761
- $post_tag[] = $the_post_tag;
762
- }
763
- else
764
- {
765
- $post_tag = $the_post_tag;
766
- }
767
-
768
- $tagarr = array();
769
-
770
- foreach ($post_tag as $tag)
771
- {
772
- $tag = esc_attr($tag);
773
- $tagobj = get_tag($tag);
774
-
775
- if(isset($tagobj->slug))
776
- {
777
- $tagarr[] = $tagobj->slug;
778
- }
779
- }
780
-
781
- if(count($tagarr)>0)
782
- {
783
- $operator = "+"; //default behaviour
784
-
785
- //check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects
786
- if(isset($_POST[SF_FPRE.'post_tag_operator']))
787
- {
788
- if($_POST[SF_FPRE.'post_tag_operator']=="and")
789
- {
790
- $operator = "+";
791
- }
792
- else if($_POST[SF_FPRE.'post_tag_operator']=="or")
793
- {
794
- $operator = ",";
795
- }
796
- else
797
- {
798
- $operator = "+";
799
- }
800
- }
801
-
802
- $tags = implode($operator,$tagarr);
803
-
804
- if(!$this->hasqmark)
805
- {
806
- $this->urlparams .= "?";
807
- $this->hasqmark = true;
808
- }
809
- else
810
- {
811
- $this->urlparams .= "&";
812
- }
813
- $this->urlparams .= "tag=".$tags;
814
-
815
- }
816
- }
817
-
818
-
819
- /* POST TYPES */
820
- if((isset($_POST[SF_FPRE.'post_types']))&&($this->has_form_posted))
821
- {
822
- $the_post_types = ($_POST[SF_FPRE.'post_types']);
823
-
824
- //make the post an array for easy looping
825
- if(!is_array($the_post_types))
826
- {
827
- $post_types_arr[] = $the_post_types;
828
- }
829
- else
830
- {
831
- $post_types_arr = $the_post_types;
832
- }
833
-
834
- $num_post_types = count($post_types_arr);
835
-
836
- for($i=0; $i<$num_post_types; $i++)
837
- {
838
- if($post_types_arr[$i]=="0")
839
- {
840
- $post_types_arr[$i] = "all";
841
- }
842
- }
843
-
844
- if(count($post_types_arr)>0)
845
- {
846
- $operator = ","; //default behaviour
847
-
848
- //check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects
849
- /*if(isset($_POST[SF_FPRE.'post_types_operator']))
850
- {
851
- if($_POST[SF_FPRE.'post_types_operator']=="and")
852
- {
853
- $operator = "+";
854
- }
855
- else if($_POST[SF_FPRE.'post_types_operator']=="or")
856
- {
857
- $operator = ",";
858
- }
859
- else
860
- {
861
- $operator = "+";
862
- }
863
- }*/
864
-
865
- $post_types = implode($operator,$post_types_arr);
866
-
867
- if(!$this->hasqmark)
868
- {
869
- $this->urlparams .= "?";
870
- $this->hasqmark = true;
871
- }
872
- else
873
- {
874
- $this->urlparams .= "&";
875
- }
876
- $this->urlparams .= "post_types=".$post_types;
877
-
878
- }
879
- }
880
-
881
-
882
- /* POST DATE */
883
- if((isset($_POST[SF_FPRE.'post_date']))&&($this->has_form_posted))
884
- {
885
- $the_post_date = ($_POST[SF_FPRE.'post_date']);
886
-
887
- //make the post an array for easy looping
888
- if(!is_array($the_post_date))
889
- {
890
- $post_date_arr[] = $the_post_date;
891
- }
892
- else
893
- {
894
- $post_date_arr = $the_post_date;
895
- }
896
-
897
- $num_post_date = count($post_date_arr);
898
-
899
- for($i=0; $i<$num_post_date; $i++)
900
- {
901
- if($post_date_arr[$i]=="0")
902
- {
903
- $post_date_arr[$i] = "all";
904
- }
905
- }
906
-
907
- if(count($post_date_arr)>0)
908
- {
909
- $post_date_count = count($post_date_arr);
910
-
911
- if($post_date_count==2)
912
- {//see if there are 2 elements in arr (second date range selector)
913
-
914
- if(($post_date_arr[0]!="")&&($post_date_arr[1]==""))
915
- {
916
- $post_date = $post_date_arr[0];
917
- }
918
- else if($post_date_arr[1]=="")
919
- {//if second date range is blank then remove the array element - this remove the addition of a '+' by implode below and only use first element
920
- unset($post_date_arr[1]);
921
- }
922
- else if($post_date_arr[0]=="")
923
- {
924
- $post_date = "+".$post_date_arr[1];
925
- }
926
- else
927
- {
928
- $post_date = implode("+",array_filter($post_date_arr));
929
- }
930
- }
931
- else
932
- {
933
- $post_date = $post_date_arr[0];
934
- }
935
-
936
- if($post_date!="")
937
- {
938
- if(!$this->hasqmark)
939
- {
940
- $this->urlparams .= "?";
941
- $this->hasqmark = true;
942
- }
943
- else
944
- {
945
- $this->urlparams .= "&";
946
- }
947
- $this->urlparams .= "post_date=".$post_date;
948
- }
949
- }
950
- }
951
-
952
-
953
- //now we have dealt with the all the special case fields - search, tags, categories, post_types, post_date
954
-
955
- //loop through the posts - double check that it is the search form that has been posted, otherwise we could be looping through the posts submitted from an entirely unrelated form
956
- if($this->has_form_posted)
957
- {
958
- foreach($_POST as $key=>$val)
959
- {
960
- if(!in_array($key, $this->frmreserved))
961
- {//if the key is not in the reserved array (ie, on a custom taxonomy - not tags, categories, search term, post type & post date)
962
-
963
- // strip off all prefixes for custom fields - we just want to do a redirect - no processing
964
- if (strpos($key, SF_FPRE) === 0)
965
- {
966
- $key = substr($key, strlen(SF_FPRE));
967
- }
968
-
969
- $the_post_tax = $val;
970
-
971
- //make the post an array for easy looping
972
- if(!is_array($val))
973
- {
974
- $post_tax[] = $the_post_tax;
975
- }
976
- else
977
- {
978
- $post_tax = $the_post_tax;
979
- }
980
- $taxarr = array();
981
-
982
- foreach ($post_tax as $tax)
983
- {
984
- $tax = esc_attr($tax);
985
- $taxobj = get_term_by('id',$tax,$key);
986
-
987
- if(isset($taxobj->slug))
988
- {
989
- $taxarr[] = $taxobj->slug;
990
- }
991
- }
992
-
993
-
994
- if(count($taxarr)>0)
995
- {
996
- $operator = "+"; //default behaviour
997
-
998
- //check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects
999
- if(isset($_POST[SF_FPRE.$key.'_operator']))
1000
- {
1001
- if($_POST[SF_FPRE.$key.'_operator']=="and")
1002
- {
1003
- $operator = "+";
1004
- }
1005
- else if($_POST[SF_FPRE.$key.'_operator']=="or")
1006
- {
1007
- $operator = ",";
1008
- }
1009
- else
1010
- {
1011
- $operator = "+";
1012
- }
1013
- }
1014
-
1015
- $tags = implode($operator,$taxarr);
1016
-
1017
- if(!$this->hasqmark)
1018
- {
1019
- $this->urlparams .= "?";
1020
- $this->hasqmark = true;
1021
- }
1022
- else
1023
- {
1024
- $this->urlparams .= "&";
1025
- }
1026
- $this->urlparams .= $key."=".$tags;
1027
-
1028
- }
1029
- }
1030
- }
1031
- }
1032
-
1033
-
1034
- if($this->has_form_posted)
1035
- {//if the search has been posted, redirect to the newly formed url with all the right params
1036
-
1037
- if($this->urlparams=="/")
1038
- {//check to see if url params are set, if not ("/") then add "?s=" to force load search results, without this it would redirect to the homepage, which may be a custom page with no blog items/results
1039
- //echo "HERE";exit;
1040
- $this->urlparams .= "?s=";
1041
- }
1042
-
1043
- wp_redirect( (home_url().$this->urlparams) );
1044
- }
1045
- }
1046
-
1047
- public function get_search_filter_form($submitlabel, $search_placeholder, $fields, $types, $labels, $hierarchical, $hide_empty, $show_count, $post_types, $order_by, $order_dir, $operators, $all_items_labels, $class)
1048
- {
1049
- $returnvar = '';
1050
-
1051
- $addclass = "";
1052
- if($class!="")
1053
- {
1054
- $addclass = ' '.$class;
1055
- }
1056
-
1057
- $returnvar .= '
1058
- <form action="" method="post" class="searchandfilter'.$addclass.'">
1059
- <div>';
1060
-
1061
- if(!in_array("post_types", $fields))
1062
- {//then the user has not added it to the fields list so the user does not want a post types drop down... so add (if any) the post types to a hidden attribute
1063
-
1064
- if(($post_types!="")&&(is_array($post_types)))
1065
- {
1066
- foreach($post_types as $post_type)
1067
- {
1068
- $returnvar .= "<input type=\"hidden\" name=\"".SF_FPRE."post_types[]\" value=\"".$post_type."\" />";
1069
- }
1070
- }
1071
- }
1072
- $returnvar .= '
1073
- <ul>';
1074
-
1075
- $i = 0;
1076
-
1077
- foreach($fields as $field)
1078
- {
1079
- //special cases - post_types & post_date.. all others assumed regular wp taxonomy
1080
-
1081
- if($field == "search")
1082
- {
1083
- $returnvar .= '<li>';
1084
- if($labels[$i]!="")
1085
- {
1086
- $returnvar .= "<h4>".$labels[$i]."</h4>";
1087
- }
1088
- $clean_searchterm = (esc_attr($this->searchterm));
1089
- $returnvar .= '<input type="text" name="'.SF_FPRE.'search" placeholder="'.$search_placeholder.'" value="'.$clean_searchterm.'">';
1090
- $returnvar .= '</li>';
1091
- }
1092
- else if($field == "post_types") //a post can only every have 1 type, so checkboxes & multiselects will always be "OR"
1093
- {//build field array
1094
-
1095
- //check to see if operator is set for this field
1096
- /*if(isset($operators[$i]))
1097
- {
1098
- $operators[$i] = strtolower($operators[$i]);
1099
-
1100
- if(($operators[$i]=="and")||($operators[$i]=="or"))
1101
- {
1102
- $returnvar .= '<input type="hidden" name="'.SF_FPRE.$field.'_operator" value="'.$operators[$i].'" />';
1103
- }
1104
- }*/
1105
-
1106
-
1107
- $returnvar .= $this->build_post_type_element($types, $labels, $post_types, $field, $all_items_labels, $i);
1108
-
1109
- }
1110
- else if($field == 'post_date')
1111
- {
1112
- $returnvar .= $this->build_post_date_element($labels, $i, $types, $field);
1113
- }
1114
- else
1115
- {
1116
- $returnvar .= $this->build_taxonomy_element($types, $labels, $field, $hierarchical, $hide_empty, $show_count, $order_by, $order_dir, $operators, $all_items_labels, $i);
1117
- }
1118
- $i++;
1119
-
1120
- }
1121
-
1122
- $returnvar .='<li>';
1123
-
1124
- if($this->add_search_param==1)
1125
- {
1126
- $returnvar .= "<input type=\"hidden\" name=\"".SF_FPRE."add_search_param\" value=\"1\" />";
1127
- }
1128
-
1129
- $returnvar .=
1130
- '<input type="hidden" name="'.SF_FPRE.'submitted" value="1">
1131
- <input type="submit" value="'.$submitlabel.'">
1132
- </li>';
1133
-
1134
- $returnvar .= "</ul>";
1135
- $returnvar .= '</div>
1136
- </form>';
1137
-
1138
- return $returnvar;
1139
- }
1140
-
1141
- ///////////////////////////////////////////////////////////
1142
- function build_post_date_element($labels, $i, $types, $field)
1143
- {
1144
- $returnvar = "";
1145
-
1146
- $taxonomychildren = array();
1147
-
1148
- $taxonomychildren = (object)$taxonomychildren;
1149
-
1150
- $returnvar .= "<li>";
1151
-
1152
- if($labels[$i]!="")
1153
- {
1154
- $returnvar .= "<h4>".$labels[$i]."</h4>";
1155
- }
1156
-
1157
- $defaultval = "";
1158
-
1159
- if($types[$i]=="date")
1160
- {
1161
- $returnvar .= $this->generate_date($taxonomychildren, $field, $this->tagid);
1162
- }
1163
- if($types[$i]=="daterange")
1164
- {
1165
- $returnvar .= $this->generate_date($taxonomychildren, $field, 0);
1166
- $returnvar .= "</li><li>";
1167
- $returnvar .= $this->generate_date($taxonomychildren, $field, 1);
1168
- }
1169
- $returnvar .= "</li>";
1170
-
1171
- return $returnvar;
1172
- }
1173
-
1174
-
1175
- function build_post_type_element($types, $labels, $post_types, $field, $all_items_labels, $i)
1176
- {
1177
- $returnvar = "";
1178
- $taxonomychildren = array();
1179
- $post_type_count = count($post_types);
1180
-
1181
- //then check the post types array
1182
- if(is_array($post_types))
1183
- {
1184
- if(($post_type_count==1)&&($post_types[0]=="all"))
1185
- {
1186
- $args = array('public' => true);
1187
- $output = 'object'; // names or objects, note names is the default
1188
- $operator = 'and'; // 'and' or 'or'
1189
-
1190
- $post_types_objs = get_post_types( $args, $output, $operator );
1191
-
1192
- $post_types = array();
1193
-
1194
- foreach ( $post_types_objs as $post_type )
1195
- {
1196
- if($post_type->name!="attachment")
1197
- {
1198
- $tempobject = array();
1199
- $tempobject['term_id'] = $post_type->name;
1200
- $tempobject['cat_name'] = $post_type->labels->name;
1201
-
1202
- $taxonomychildren[] = (object)$tempobject;
1203
-
1204
- $post_types[] = $post_type->name;
1205
-
1206
- }
1207
- }
1208
- $post_type_count = count($post_types_objs);
1209
-
1210
- }
1211
- else
1212
- {
1213
- foreach($post_types as $post_type)
1214
- {
1215
- //var_dump(get_post_type_object( $post_type ));
1216
- $post_type_data = get_post_type_object( $post_type );
1217
-
1218
- if($post_type_data)
1219
- {
1220
- $tempobject = array();
1221
- $tempobject['term_id'] = $post_type;
1222
- $tempobject['cat_name'] = $post_type_data->labels->name;
1223
-
1224
- $taxonomychildren[] = (object)$tempobject;
1225
- }
1226
- }
1227
- }
1228
- }
1229
- $taxonomychildren = (object)$taxonomychildren;
1230
-
1231
- $returnvar .= "<li>";
1232
-
1233
- $post_type_labels = array();
1234
- $post_type_labels['name'] = "Post Types";
1235
- $post_type_labels['singular_name'] = "Post Type";
1236
- $post_type_labels['search_items'] = "Search Post Types";
1237
-
1238
- if($all_items_labels[$i]!="")
1239
- {
1240
- $post_type_labels['all_items'] = $all_items_labels[$i];
1241
- }
1242
- else
1243
- {
1244
- $post_type_labels['all_items'] = "All Post Types";
1245
- }
1246
-
1247
- $post_type_labels = (object)$post_type_labels;
1248
-
1249
- if($labels[$i]!="")
1250
- {
1251
- $returnvar .= "<h4>".$labels[$i]."</h4>";
1252
- }
1253
-
1254
- if($post_type_count>0)
1255
- {
1256
- $defaultval = implode(",",$post_types);
1257
- }
1258
- else
1259
- {
1260
- $defaultval = "all";
1261
- }
1262
-
1263
- if($types[$i]=="select")
1264
- {
1265
- $returnvar .= $this->generate_select($taxonomychildren, $field, $this->tagid, $post_type_labels, $defaultval);
1266
- }
1267
- else if($types[$i]=="checkbox")
1268
- {
1269
-
1270
- $returnvar .= $this->generate_checkbox($taxonomychildren, $field, $this->tagid);
1271
- }
1272
- else if($types[$i]=="radio")
1273
- {
1274
- $returnvar .= $this->generate_radio($taxonomychildren, $field, $this->tagid, $post_type_labels, $defaultval);
1275
- }
1276
- $returnvar .= "</li>";
1277
-
1278
- return $returnvar;
1279
- }
1280
-
1281
- //gets all the data for the taxonomy then display as form element
1282
- function build_taxonomy_element($types, $labels, $taxonomy, $hierarchical, $hide_empty, $show_count, $order_by, $order_dir, $operators, $all_items_labels, $i)
1283
- {
1284
- $returnvar = "";
1285
-
1286
- $taxonomydata = get_taxonomy($taxonomy);
1287
-
1288
- if($taxonomydata)
1289
- {
1290
- $returnvar .= "<li>";
1291
-
1292
- if($labels[$i]!="")
1293
- {
1294
- $returnvar .= "<h4>".$labels[$i]."</h4>";
1295
- }
1296
-
1297
- $args = array(
1298
- 'name' => SF_FPRE . $taxonomy,
1299
- 'taxonomy' => $taxonomy,
1300
- 'hierarchical' => false,
1301
- 'child_of' => 0,
1302
- 'echo' => false,
1303
- 'hide_if_empty' => false,
1304
- 'hide_empty' => true,
1305
- 'order' => $order_dir[$i],
1306
- 'orderby' => $order_by[$i],
1307
- 'show_option_none' => '',
1308
- 'show_count' => '0',
1309
- 'show_option_all' => '',
1310
- 'show_option_all_sf' => ''
1311
- );
1312
-
1313
- if(isset($hierarchical[$i]))
1314
- {
1315
- if($hierarchical[$i]==1)
1316
- {
1317
- $args['hierarchical'] = true;
1318
- }
1319
- }
1320
-
1321
- if(isset($hide_empty[$i]))
1322
- {
1323
- if($hide_empty[$i]==0)
1324
- {
1325
- $args['hide_empty'] = false;
1326
- }
1327
- }
1328
-
1329
- if(isset($show_count[$i]))
1330
- {
1331
- if($show_count[$i]==1)
1332
- {
1333
- $args['show_count'] = true;
1334
- }
1335
- }
1336
-
1337
- if($all_items_labels[$i]!="")
1338
- {
1339
- $args['show_option_all_sf'] = $all_items_labels[$i];
1340
- }
1341
-
1342
-
1343
-
1344
- $taxonomychildren = get_categories($args);
1345
-
1346
- if($types[$i]=="select")
1347
- {
1348
- $returnvar .= $this->generate_wp_dropdown($args, $taxonomy, $this->tagid, $taxonomydata->labels);
1349
- }
1350
- else if($types[$i]=="checkbox")
1351
- {
1352
- $args['title_li'] = '';
1353
- $args['defaults'] = $this->defaults[$args['name']];
1354
- //$args['show_option_all'] = 0;
1355
-
1356
- $returnvar .= $this->generate_wp_checkbox($args, $taxonomy, $this->tagid, $taxonomydata->labels);
1357
- }
1358
- else if($types[$i]=="radio")
1359
- {
1360
- $args['title_li'] = '';
1361
- $args['defaults'] = $this->defaults[$args['name']];
1362
-
1363
- $returnvar .= $this->generate_wp_radio($args, $taxonomy, $this->tagid, $taxonomydata->labels);
1364
- }
1365
- else if($types[$i]=="multiselect")
1366
- {
1367
- $args['title_li'] = '';
1368
- $args['defaults'] = $this->defaults[$args['name']];
1369
-
1370
- $returnvar .= $this->generate_wp_multiselect($args, $taxonomy, $this->tagid, $taxonomydata->labels);
1371
- }
1372
-
1373
- //check to see if operator is set for this field
1374
- if(isset($operators[$i]))
1375
- {
1376
- $operators[$i] = strtolower($operators[$i]);
1377
-
1378
- if(($operators[$i]=="and")||($operators[$i]=="or"))
1379
- {
1380
- $returnvar .= '<input type="hidden" name="'.SF_FPRE.$taxonomy.'_operator" value="'.$operators[$i].'" />';
1381
- }
1382
- }
1383
-
1384
- $returnvar .= "</li>";
1385
- }
1386
-
1387
- return $returnvar;
1388
- }
1389
-
1390
-
1391
- /*
1392
- * Display various forms
1393
- */
1394
-
1395
- //use wp array walker to enable hierarchical display
1396
- public function generate_wp_dropdown($args, $name, $currentid = 0, $labels = null, $defaultval = "0")
1397
- {
1398
- $returnvar = '';
1399
-
1400
- if($args['show_option_all_sf']=="")
1401
- {
1402
- $args['show_option_all'] = $labels->all_items != "" ? $labels->all_items : 'All ' . $labels->name;
1403
- }
1404
- else
1405
- {
1406
- $args['show_option_all'] = $args['show_option_all_sf'];
1407
- }
1408
-
1409
- if(isset($this->defaults[SF_FPRE.$name]))
1410
- {
1411
- $defaults = $this->defaults[SF_FPRE . $name];
1412
- if (is_array($defaults)) {
1413
- if (count($defaults) == 1) {
1414
- $args['selected'] = $defaults[0];
1415
- }
1416
- }
1417
- else {
1418
- $args['selected'] = $defaultval;
1419
- }
1420
- }
1421
-
1422
- $returnvar .= wp_dropdown_categories($args);
1423
-
1424
- return $returnvar;
1425
- }
1426
-
1427
- //use wp array walker to enable hierarchical display
1428
- public function generate_wp_multiselect($args, $name, $currentid = 0, $labels = null, $defaultval = "0")
1429
- {
1430
- $returnvar = '<select multiple="multiple" name="'.$args['name'].'[]" class="postform">';
1431
- $returnvar .= walk_taxonomy('multiselect', $args);
1432
- $returnvar .= "</select>";
1433
-
1434
- return $returnvar;
1435
- }
1436
-
1437
- //use wp array walker to enable hierarchical display
1438
- public function generate_wp_checkbox($args, $name, $currentid = 0, $labels = null, $defaultval = "0")
1439
- {
1440
- $returnvar = '<ul>';
1441
- $returnvar .= walk_taxonomy('checkbox', $args);
1442
- $returnvar .= "</ul>";
1443
-
1444
- return $returnvar;
1445
- }
1446
-
1447
- //use wp array walker to enable hierarchical display
1448
- public function generate_wp_radio($args, $name, $currentid = 0, $labels = null, $defaultval = "0")
1449
- {
1450
-
1451
- if($args['show_option_all_sf']=="")
1452
- {
1453
- $show_option_all = $labels->all_items != "" ? $labels->all_items : 'All ' . $labels->name;
1454
- }
1455
- else
1456
- {
1457
- $show_option_all = $args['show_option_all_sf'];
1458
- }
1459
-
1460
- $checked = ($defaultval=="0") ? " checked='checked'" : "";
1461
- $returnvar = '<ul>';
1462
- $returnvar .= '<li>'."<label><input type='radio' name='".$args['name']."[]' value='0'$checked /> ".$show_option_all."</label>".'</li>';
1463
- $returnvar .= walk_taxonomy('radio', $args);
1464
- $returnvar .= "</ul>";
1465
-
1466
- return $returnvar;
1467
- }
1468
-
1469
- //generate generic form inputs for use elsewhere, such as post types and non taxonomy fields
1470
- public function generate_select($dropdata, $name, $currentid = 0, $labels = null, $defaultval = "0")
1471
- {
1472
- $returnvar = "";
1473
-
1474
- $returnvar .= '<select class="postform" name="'.SF_FPRE.$name.'">';
1475
- if(isset($labels))
1476
- {
1477
- if($labels->all_items!="")
1478
- {//check to see if all items has been registered in field then use this label
1479
- $returnvar .= '<option class="level-0" value="'.$defaultval.'">'.$labels->all_items.'</option>';
1480
- }
1481
- else
1482
- {//check to see if all items has been registered in field then use this label with prefix of "All"
1483
- $returnvar .= '<option class="level-0" value="'.$defaultval.'">All '.$labels->name.'</option>';
1484
- }
1485
- }
1486
-
1487
- foreach($dropdata as $dropdown)
1488
- {
1489
- $selected = "";
1490
-
1491
- if(isset($this->defaults[SF_FPRE.$name]))
1492
- {
1493
- $defaults = $this->defaults[SF_FPRE.$name];
1494
-
1495
- $noselected = count($defaults);
1496
-
1497
- if(($noselected==1)&&(is_array($defaults))) //there should never be more than 1 default in a select, if there are then don't set any, user is obviously searching multiple values, in the case of a select this must be "all"
1498
- {
1499
- foreach($defaults as $defaultid)
1500
- {
1501
- if($defaultid==$dropdown->term_id)
1502
- {
1503
- $selected = ' selected="selected"';
1504
- }
1505
- }
1506
- }
1507
- }
1508
- $returnvar .= '<option class="level-0" value="'.$dropdown->term_id.'"'.$selected.'>'.$dropdown->cat_name.'</option>';
1509
-
1510
- }
1511
- $returnvar .= "</select>";
1512
-
1513
- return $returnvar;
1514
- }
1515
-
1516
- public function generate_checkbox($dropdata, $name, $currentid = 0, $labels = null, $defaultval = '')
1517
- {
1518
- $returnvar = '<ul>';
1519
-
1520
- foreach($dropdata as $dropdown)
1521
- {
1522
- $checked = "";
1523
-
1524
- //check a default has been set
1525
- if(isset($this->defaults[SF_FPRE.$name]))
1526
- {
1527
- $defaults = $this->defaults[SF_FPRE.$name];
1528
-
1529
- $noselected = count($defaults);
1530
-
1531
- if(($noselected>0)&&(is_array($defaults)))
1532
- {
1533
- foreach($defaults as $defaultid)
1534
- {
1535
- if($defaultid==$dropdown->term_id)
1536
- {
1537
- $checked = ' checked="checked"';
1538
- }
1539
- }
1540
- }
1541
- }
1542
- $returnvar .= '<li class="cat-item"><label><input class="postform cat-item" type="checkbox" name="'.SF_FPRE.$name.'[]" value="'.$dropdown->term_id.'"'.$checked.'> '.$dropdown->cat_name.'</label></li>';
1543
-
1544
- }
1545
-
1546
- $returnvar .= '</ul>';
1547
-
1548
- return $returnvar;
1549
- }
1550
-
1551
-
1552
- public function generate_radio($dropdata, $name, $currentid = 0, $labels = null, $defaultval = "0")
1553
- {
1554
- $returnvar = '<ul>';
1555
-
1556
- if(isset($labels))
1557
- {
1558
- $checked = "";
1559
- if(isset($this->defaults[SF_FPRE.$name]))
1560
- {
1561
- $defaults = $this->defaults[SF_FPRE.$name];
1562
- $noselected = count($defaults);
1563
-
1564
- if($noselected==0)
1565
- {
1566
- $checked = ' checked="checked"';
1567
- }
1568
- else if($noselected==1)
1569
- {
1570
- if($this->defaults[SF_FPRE.$name][0]==$defaultval)
1571
- {
1572
- $checked = ' checked="checked"';
1573
- }
1574
- }
1575
- }
1576
- else
1577
- {
1578
- $checked = ' checked="checked"';
1579
- }
1580
-
1581
- if(isset($this->defaults[SF_FPRE.$name]))
1582
- {
1583
- $defaults = $this->defaults[SF_FPRE.$name];
1584
- if(count($defaults)>1)
1585
- {//then we are dealing with multiple defaults - this means mutliple radios are selected, this is only possible with "ALL" so set as default.
1586
- $checked = ' checked="checked"';
1587
- }
1588
- }
1589
-
1590
- $all_items_name = "";
1591
- if($labels->all_items!="")
1592
- {//check to see if all items has been registered in field then use this label
1593
- $all_items_name = $labels->all_items;
1594
- }
1595
- else
1596
- {//check to see if all items has been registered in field then use this label with prefix of "All"
1597
- $all_items_name = "All ".$labels->name;
1598
- }
1599
-
1600
- $returnvar .= '<li class="cat-item"><label><input class="postform" type="radio" name="'.SF_FPRE.$name.'[]" value="'.$defaultval.'"'.$checked.'> '.$all_items_name.'</label></li>';
1601
- }
1602
-
1603
- foreach($dropdata as $dropdown)
1604
- {
1605
- $checked = "";
1606
-
1607
- //check a default has been set
1608
- if(isset($this->defaults[SF_FPRE.$name]))
1609
- {
1610
- $defaults = $this->defaults[SF_FPRE.$name];
1611
-
1612
- $noselected = count($defaults);
1613
-
1614
- if(($noselected==1)&&(is_array($defaults)))
1615
- {
1616
- foreach($defaults as $defaultid)
1617
- {
1618
- if($defaultid==$dropdown->term_id)
1619
- {
1620
- $checked = ' checked="checked"';
1621
- }
1622
- }
1623
- }
1624
- }
1625
- $returnvar .= '<li class="cat-item"><label><input class="postform" type="radio" name="'.SF_FPRE.$name.'[]" value="'.$dropdown->term_id.'"'.$checked.'> '.$dropdown->cat_name.'</label></li>';
1626
-
1627
- }
1628
-
1629
- $returnvar .= '</ul>';
1630
-
1631
- return $returnvar;
1632
- }
1633
-
1634
- public function generate_date($dropdata, $name, $currentid = 0, $labels = null, $defaultval = "0")
1635
- {
1636
- $returnvar = '';
1637
- $current_date = '';
1638
- //check a default has been set - upto two possible vars for array
1639
-
1640
- if(isset($this->defaults[SF_FPRE.$name]))
1641
- {
1642
- $defaults = $this->defaults[SF_FPRE.$name];
1643
-
1644
- $noselected = count($defaults);
1645
-
1646
- if(($noselected>0)&&(is_array($defaults)))
1647
- {
1648
- $current_date = $defaults[$currentid];
1649
- }
1650
- }
1651
-
1652
- $returnvar .= '<input class="postform" type="date" name="'.SF_FPRE.$name.'[]" value="' . $current_date . '" />';
1653
-
1654
- return $returnvar;
1655
- }
1656
- }
1657
- }
1658
-
1659
-
1660
- function walk_taxonomy( $type = "checkbox", $args = array() ) {
1661
-
1662
- $args['walker'] = new Taxonomy_Walker($type, $args['name']);
1663
-
1664
- $output = wp_list_categories($args);
1665
- if ( $output )
1666
- return $output;
1667
- }
1668
-
1669
-
1670
-
1671
-
1672
- if ( class_exists( 'SearchAndFilter' ) )
1673
- {
1674
- global $SearchAndFilter;
1675
- $SearchAndFilter = new SearchAndFilter();
1676
- }
1677
-
1678
- /*
1679
- * Includes
1680
- */
1681
-
1682
- // classes
1683
- require_once(SEARCHANDFILTER_PLUGIN_DIR."/of-list-table.php");
1684
- require_once(SEARCHANDFILTER_PLUGIN_DIR."/of-taxonomy-walker.php");
1685
-
1686
- // admin screens & plugin mods
1687
- require_once(SEARCHANDFILTER_PLUGIN_DIR."/of-admin.php");
1688
-
1689
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1690
  ?>
1
+ <?php
2
+ /*
3
+ Plugin Name: Search & Filter
4
+ Plugin URI: http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/
5
+ Description: Search and Filtering system for Pages, Posts, Categories, Tags and Taxonomies
6
+ Author: Designs & Code
7
+ Author URI: http://www.designsandcode.com/
8
+ Version: 1.2.5
9
+ Text Domain: searchandfilter
10
+ License: GPLv2
11
+ */
12
+
13
+ // TO DO - i18n http://codex.wordpress.org/I18n_for_WordPress_Developers
14
+
15
+ /*
16
+ * Set up Plugin Globals
17
+ */
18
+ if (!defined('SEARCHANDFILTER_VERSION_NUM'))
19
+ define('SEARCHANDFILTER_VERSION_NUM', '1.2.5');
20
+
21
+ if (!defined('SEARCHANDFILTER_THEME_DIR'))
22
+ define('SEARCHANDFILTER_THEME_DIR', ABSPATH . 'wp-content/themes/' . get_template());
23
+
24
+ if (!defined('SEARCHANDFILTER_PLUGIN_NAME'))
25
+ define('SEARCHANDFILTER_PLUGIN_NAME', trim(dirname(plugin_basename(__FILE__)), '/'));
26
+
27
+ if (!defined('SEARCHANDFILTER_PLUGIN_DIR'))
28
+ define('SEARCHANDFILTER_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . SEARCHANDFILTER_PLUGIN_NAME);
29
+
30
+ if (!defined('SEARCHANDFILTER_PLUGIN_URL'))
31
+ define('SEARCHANDFILTER_PLUGIN_URL', WP_PLUGIN_URL . '/' . SEARCHANDFILTER_PLUGIN_NAME);
32
+
33
+ if (!defined('SEARCHANDFILTER_BASENAME'))
34
+ define('SEARCHANDFILTER_BASENAME', plugin_basename(__FILE__));
35
+
36
+ if (!defined('SEARCHANDFILTER_VERSION_KEY'))
37
+ define('SEARCHANDFILTER_VERSION_KEY', 'searchandfilter_version');
38
+
39
+
40
+
41
+ //form prefix for plugin
42
+ if (!defined('SF_FPRE'))
43
+ define('SF_FPRE', 'of');
44
+
45
+ add_option(SEARCHANDFILTER_VERSION_KEY, SEARCHANDFILTER_VERSION_NUM);
46
+
47
+ /*
48
+ * Set up Plugin Globals
49
+ */
50
+ if ( ! class_exists( 'SearchAndFilter' ) )
51
+ {
52
+ class SearchAndFilter
53
+ {
54
+ private $has_form_posted = false;
55
+ private $hasqmark = false;
56
+ private $hassearchquery = false;
57
+ private $urlparams = "/";
58
+ private $searchterm = "";
59
+ private $tagid = 0;
60
+ private $catid = 0;
61
+ private $defaults = array();
62
+ private $frmreserved = array();
63
+ private $taxonomylist = array();
64
+
65
+ public function __construct()
66
+ {
67
+
68
+ // Set up reserved fields
69
+ $this->frmreserved = array(SF_FPRE."category", SF_FPRE."search", SF_FPRE."post_tag", SF_FPRE."submitted", SF_FPRE."post_date", SF_FPRE."post_types");
70
+ $this->frmqreserved = array(SF_FPRE."category_name", SF_FPRE."s", SF_FPRE."tag", SF_FPRE."submitted", SF_FPRE."post_date", SF_FPRE."post_types"); //same as reserved
71
+
72
+ //add query vars
73
+ add_filter('query_vars', array($this,'add_queryvars') );
74
+
75
+ //filter post type & date if it is set
76
+ add_filter('pre_get_posts', array($this,'filter_query_post_types'));
77
+ add_filter('pre_get_posts', array($this,'filter_query_post_date'));
78
+
79
+ //add_filter('pre_get_posts',array($this, 'fix_blank_search')); //temporaril disabled
80
+
81
+ // Add shortcode support for widgets
82
+ add_shortcode('searchandfilter', array($this, 'shortcode'));
83
+ add_filter('widget_text', 'do_shortcode');
84
+
85
+ // Check the header to see if the form has been submitted
86
+ add_action( 'get_header', array( $this, 'check_posts' ) );
87
+
88
+ // Add styles
89
+ add_action( 'wp_enqueue_scripts', array($this, 'of_enqueue_styles') );
90
+ add_action( 'admin_enqueue_scripts', array($this, 'of_enqueue_admin_ss') );
91
+
92
+ }
93
+
94
+ public function of_enqueue_styles()
95
+ {
96
+ wp_enqueue_style( 'searchandfilter', SEARCHANDFILTER_PLUGIN_URL . '/style.css', false, 1.0, 'all' );
97
+ }
98
+ public function of_enqueue_admin_ss($hook)
99
+ {
100
+ if( 'toplevel_page_searchandfilter-settings' == $hook )
101
+ {
102
+ wp_enqueue_style( 'of_syntax_style', SEARCHANDFILTER_PLUGIN_URL.'/admin/github.css', false, 1.0, 'all' ); //more highlight styles http://softwaremaniacs.org/media/soft/highlight/test.html
103
+ wp_enqueue_style( 'of_style', SEARCHANDFILTER_PLUGIN_URL.'/admin/style.css', false, 1.0, 'all' );
104
+ wp_enqueue_script( 'of_syntax_script', SEARCHANDFILTER_PLUGIN_URL.'/admin/syntax.highlight.min.js' );
105
+ }
106
+ }
107
+
108
+ public function shortcode($atts, $content = null)
109
+ {
110
+ // extract the attributes into variables
111
+ extract(shortcode_atts(array(
112
+
113
+ 'fields' => null,
114
+ 'taxonomies' => null, //will be deprecated - use `fields` instead
115
+ 'submit_label' => null,
116
+ 'submitlabel' => null, //will be deprecated - use `submit_label` instead
117
+ 'search_placeholder' => "Search &hellip;",
118
+ 'types' => "",
119
+ 'type' => "", //will be deprecated - use `types` instead
120
+ 'headings' => "",
121
+ 'all_items_labels' => "",
122
+ 'class' => "",
123
+ 'post_types' => "",
124
+ 'hierarchical' => "",
125
+ 'hide_empty' => "",
126
+ 'order_by' => "",
127
+ 'show_count' => "",
128
+ 'order_dir' => "",
129
+ 'operators' => "",
130
+ 'add_search_param' => "0",
131
+ 'empty_search_url' => ""
132
+
133
+ ), $atts));
134
+
135
+ //init `fields`
136
+ if($fields!=null)
137
+ {
138
+ $fields = explode(",",$fields);
139
+ }
140
+ else
141
+ {
142
+ $fields = explode(",",$taxonomies);
143
+ }
144
+
145
+ $this->taxonomylist = $fields;
146
+ $nofields = count($fields);
147
+
148
+ $add_search_param = (int)$add_search_param;
149
+
150
+
151
+ //init `submitlabel`
152
+ if($submitlabel!=null)
153
+ {//then the old "submitlabel" has been supplied
154
+
155
+ if($submit_label==null)
156
+ {
157
+ //then the new label has not been supplied so do nothing
158
+ $submit_label = $submitlabel;
159
+ }
160
+ else
161
+ {
162
+ //then the new label has been supplied so take the new label value
163
+ //$submit_label = $submit_label;
164
+ }
165
+ }
166
+ else if($submitlabel==null)
167
+ {
168
+ if($submit_label==null)
169
+ {//default value
170
+ $submit_label = "Submit";
171
+ }
172
+ }
173
+
174
+ //init `post_types`
175
+ if($post_types!="")
176
+ {
177
+ $post_types = explode(",",$post_types);
178
+ }
179
+ else
180
+ {
181
+ if(in_array("post_types", $fields))
182
+ {
183
+ $post_types = array("all");
184
+ }
185
+
186
+ }
187
+
188
+ //init `hierarchical`
189
+ if($hierarchical!="")
190
+ {
191
+ $hierarchical = explode(",",$hierarchical);
192
+ }
193
+ else
194
+ {
195
+ $hierarchical = array("");
196
+ }
197
+
198
+ //init `hide_empty`
199
+ if($hide_empty!="")
200
+ {
201
+ $hide_empty = explode(",",$hide_empty);
202
+ }
203
+ else
204
+ {
205
+ $hide_empty = array("");
206
+ }
207
+
208
+ //init `show_count`
209
+ if($show_count!="")
210
+ {
211
+ $show_count = explode(",",$show_count);
212
+ }
213
+ else
214
+ {
215
+ $show_count = array();
216
+ }
217
+
218
+ //init `order_by`
219
+ if($order_by!="")
220
+ {
221
+ $order_by = explode(",",$order_by);
222
+ }
223
+ else
224
+ {
225
+ $order_by = array("");
226
+ }
227
+
228
+ //init `order_dir`
229
+ if($order_dir!="")
230
+ {
231
+ $order_dir = explode(",",$order_dir);
232
+ }
233
+ else
234
+ {
235
+ $order_dir = array("");
236
+ }
237
+
238
+ //init `operators`
239
+ if($operators!="")
240
+ {
241
+ $operators = explode(",",$operators);
242
+ }
243
+ else
244
+ {
245
+ $operators = array("");
246
+ }
247
+
248
+
249
+ //init `labels`
250
+ $labels = explode(",",$headings);
251
+
252
+ if(!is_array($labels))
253
+ {
254
+ $labels = array();
255
+ }
256
+
257
+ //init `all_items_labels`
258
+ $all_items_labels = explode(",",$all_items_labels);
259
+
260
+ if(!is_array($all_items_labels))
261
+ {
262
+ $all_items_labels = array();
263
+ }
264
+
265
+ //init `types`
266
+ if($types!=null)
267
+ {
268
+ $types = explode(",",$types);
269
+ }
270
+ else
271
+ {
272
+ $types = explode(",",$type);
273
+ }
274
+
275
+ if(!is_array($types))
276
+ {
277
+ $types = array();
278
+ }
279
+
280
+ //init empty_search_url
281
+
282
+
283
+ //Loop through Fields and set up default vars
284
+ for($i=0; $i<$nofields; $i++)
285
+ {//loop through all fields
286
+
287
+ //set up types
288
+ if(isset($types[$i]))
289
+ {
290
+ if($fields[$i] == 'post_date')
291
+ {//check for post date field
292
+
293
+ if(($types[$i]!="date")&&($types[$i]!="daterange"))
294
+ {//if not expected value
295
+
296
+ $types[$i] = "date"; //use default
297
+ }
298
+ }
299
+ else
300
+ {//everything else can use a standard form input - checkbox/radio/dropdown/list/multiselect
301
+
302
+ if(($types[$i]!="select")&&($types[$i]!="checkbox")&&($types[$i]!="radio")&&($types[$i]!="list")&&($types[$i]!="multiselect"))
303
+ {//no accepted type matched - non compatible type defined by user
304
+
305
+ $types[$i] = "select"; //use default
306
+ }
307
+ }
308
+ }
309
+ else
310
+ {//omitted, so set default
311
+
312
+ if($fields[$i] == 'post_date')
313
+ {
314
+ $types[$i] = "date";
315
+ }
316
+ else
317
+ {
318
+ $types[$i] = "select";
319
+ }
320
+ }
321
+
322
+ //setup labels
323
+ if(!isset($labels[$i]))
324
+ {
325
+ $labels[$i] = "";
326
+ }
327
+
328
+ //setup all_items_labels
329
+ if(!isset($all_items_labels[$i]))
330
+ {
331
+ $all_items_labels[$i] = "";
332
+ }
333
+
334
+
335
+ if(isset($order_by[$i]))
336
+ {
337
+ if(($order_by[$i]!="id")&&($order_by[$i]!="name")&&($order_by[$i]!="slug")&&($order_by[$i]!="count")&&($order_by[$i]!="term_group"))
338
+ {
339
+ $order_by[$i] = "name"; //use default - possible typo or use of unknown value
340
+ }
341
+ }
342
+ else
343
+ {
344
+ $order_by[$i] = "name"; //use default
345
+ }
346
+
347
+ if(isset($order_dir[$i]))
348
+ {
349
+ if(($order_dir[$i]!="asc")&&($order_dir[$i]!="desc"))
350
+ {//then order_dir is not a wanted value
351
+
352
+ $order_dir[$i] = "asc"; //set to default
353
+ }
354
+ }
355
+ else
356
+ {
357
+ $order_dir[$i] = "asc"; //use default
358
+ }
359
+
360
+ if(isset($operators[$i]))
361
+ {
362
+ if(($operators[$i]!="and")&&($operators[$i]!="or"))
363
+ {
364
+ $operators[$i] = "and"; //else use default - possible typo or use of unknown value
365
+ }
366
+ }
367
+ else
368
+ {
369
+ $operators[$i] = "and"; //use default
370
+ }
371
+
372
+ }
373
+
374
+ //set all form defaults / dropdowns etc
375
+ $this->set_defaults();
376
+
377
+ return $this->get_search_filter_form($submit_label, $search_placeholder, $fields, $types, $labels, $hierarchical, $hide_empty, $show_count, $post_types, $order_by, $order_dir, $operators, $all_items_labels, $empty_search_url, $add_search_param, $class);
378
+ }
379
+
380
+
381
+ function add_queryvars( $qvars )
382
+ {
383
+ $qvars[] = 'post_types';
384
+ $qvars[] = 'post_date';
385
+ return $qvars;
386
+ }
387
+
388
+ function filter_query_post_types($query)
389
+ {
390
+ global $wp_query;
391
+
392
+ if(($query->is_main_query())&&(!is_admin()))
393
+ {
394
+ if(isset($wp_query->query['post_types']))
395
+ {
396
+ $search_all = false;
397
+
398
+ $post_types = explode(",",esc_attr($wp_query->query['post_types']));
399
+ if(isset($post_types[0]))
400
+ {
401
+ if(count($post_types)==1)
402
+ {
403
+ if($post_types[0]=="all")
404
+ {
405
+ $search_all = true;
406
+ }
407
+ }
408
+ }
409
+ if($search_all)
410
+ {
411
+ $post_types = get_post_types( '', 'names' );
412
+ $query->set('post_type', $post_types); //here we set the post types that we want WP to search
413
+ }
414
+ else
415
+ {
416
+ $query->set('post_type', $post_types); //here we set the post types that we want WP to search
417
+ }
418
+ }
419
+ }
420
+
421
+ return $query;
422
+ }
423
+
424
+
425
+ function limit_date_range_query( $where )
426
+ {
427
+ global $wp_query;
428
+
429
+ //get post dates into array
430
+ $post_date = explode("+", esc_attr(urlencode($wp_query->query['post_date'])));
431
+
432
+ if (count($post_date) > 1 && $post_date[0] != $post_date[1])
433
+ {
434
+ $date_query = array();
435
+
436
+ if (!empty($post_date[0]))
437
+ {
438
+ $date_query['after'] = date('Y-m-d 00:00:00', strtotime($post_date[0]));
439
+ }
440
+
441
+ if (!empty($post_date[1]))
442
+ {
443
+ $date_query['before'] = date('Y-m-d 23:59:59', strtotime($post_date[1]));
444
+ }
445
+
446
+ }
447
+
448
+ // Append fragment to WHERE clause to select posts newer than the past week.
449
+ $where .= " AND post_date >='" . $date_query['after'] . "' AND post_date <='" . $date_query['before'] . "'";
450
+
451
+ return $where;
452
+ }
453
+
454
+ /**
455
+ * Remove the filter limiting posts to the past week.
456
+ *
457
+ * Remove the filter after it runs so that it doesn't affect any other
458
+ * queries that might be performed on the same page (eg. Recent Posts
459
+ * widget).
460
+ */
461
+ function remove_limit_date_range_query()
462
+ {
463
+ remove_filter( 'posts_where', 'limit_date_range_query' );
464
+ }
465
+
466
+ function fix_blank_search($query)
467
+ {//needs to be re-implemented
468
+
469
+ if((isset($_GET['s'])) && (empty($_GET['s'])) && ($query->is_main_query()))
470
+ {
471
+ $query->is_search = true;
472
+ $query->is_home = false;
473
+ }
474
+
475
+ }
476
+
477
+ function filter_query_post_date($query)
478
+ {
479
+ global $wp_query;
480
+
481
+ if(($query->is_main_query())&&(!is_admin()))
482
+ {
483
+ if(isset($wp_query->query['post_date']))
484
+ {
485
+ //get post dates into array
486
+ $post_date = explode("+", esc_attr(urlencode($wp_query->query['post_date'])));
487
+
488
+ if(!empty($post_date))
489
+ {
490
+ //if there is more than 1 post date and the dates are not the same
491
+ if (count($post_date) > 1 && $post_date[0] != $post_date[1])
492
+ {
493
+ if((!empty($post_date[0]))&&(!empty($post_date[1])))
494
+ {
495
+ // Attach hook to filter WHERE clause.
496
+ add_filter('posts_where', array($this,'limit_date_range_query'));
497
+
498
+ // Remove the filter after it is executed.
499
+ add_action('posts_selection', array($this,'remove_limit_date_range_query'));
500
+ }
501
+ }
502
+ else
503
+ { //else we are dealing with one date or both dates are the same (so need to find posts for a single day)
504
+
505
+ if (!empty($post_date[0]))
506
+ {
507
+ $post_time = strtotime($post_date[0]);
508
+ $query->set('year', date('Y', $post_time));
509
+ $query->set('monthnum', date('m', $post_time));
510
+ $query->set('day', date('d', $post_time));
511
+ }
512
+ }
513
+ }
514
+ }
515
+ }
516
+
517
+ return $query;
518
+ }
519
+
520
+ /*
521
+ * check to set defaults - to be called after the shortcodes have been init so we can grab the wanted list of fields
522
+ */
523
+ public function set_defaults()
524
+ {
525
+ global $wp_query;
526
+
527
+ $categories = array();
528
+
529
+ if(isset($wp_query->query['category_name']))
530
+ {
531
+ $category_params = (preg_split("/[,\+ ]/", esc_attr($wp_query->query['category_name']))); //explode with 2 delims
532
+
533
+ //$category_params = explode("+",esc_attr($wp_query->query['category_name']));
534
+
535
+ foreach($category_params as $category_param)
536
+ {
537
+ $category = get_category_by_slug( $category_param );
538
+ if(isset($category->cat_ID))
539
+ {
540
+ $categories[] = $category->cat_ID;
541
+ }
542
+ }
543
+ }
544
+
545
+ $this->defaults[SF_FPRE.'category'] = $categories;
546
+
547
+
548
+ //grab search term for prefilling search input
549
+ if(isset($wp_query->query['s']))
550
+ {//!"�$%^&*()
551
+ $this->searchterm = trim(get_search_query());
552
+ }
553
+
554
+ //check to see if tag is set
555
+
556
+ $tags = array();
557
+
558
+ if(isset($wp_query->query['tag']))
559
+ {
560
+ $tag_params = (preg_split("/[,\+ ]/", esc_attr($wp_query->query['tag']))); //explode with 2 delims
561
+ //$tag_params = explode("+",esc_attr($wp_query->query['tag']));
562
+
563
+ foreach($tag_params as $tag_param)
564
+ {
565
+ $tag = get_term_by("slug",$tag_param, "post_tag");
566
+ if(isset($tag->term_id))
567
+ {
568
+ $tags[] = $tag->term_id;
569
+ }
570
+ }
571
+ }
572
+
573
+ $this->defaults[SF_FPRE.'post_tag'] = $tags;
574
+
575
+ $taxs = array();
576
+ //loop through all the query vars
577
+ foreach($wp_query->query as $key=>$val)
578
+ {
579
+ if(!in_array(SF_FPRE.$key, $this->frmqreserved))
580
+ {//make sure the get is not a reserved get as they have already been handled above
581
+
582
+ //now check it is a desired key
583
+ if(in_array($key, $this->taxonomylist))
584
+ {
585
+ $taxslug = ($val);
586
+ //$tax_params = explode("+",esc_attr($taxslug));
587
+
588
+ $tax_params = (preg_split("/[,\+ ]/", esc_attr($taxslug))); //explode with 2 delims
589
+
590
+ foreach($tax_params as $tax_param)
591
+ {
592
+ $tax = get_term_by("slug",$tax_param, $key);
593
+
594
+ if(isset($tax->term_id))
595
+ {
596
+ $taxs[] = $tax->term_id;
597
+ }
598
+ }
599
+
600
+ $this->defaults[SF_FPRE.$key] = $taxs;
601
+ }
602
+ }
603
+ }
604
+
605
+ $post_date = array("","");
606
+ if(isset($wp_query->query['post_date']))
607
+ {
608
+ $post_date = explode("+", esc_attr(urlencode($wp_query->query['post_date'])));
609
+ if(count($post_date)==1)
610
+ {
611
+ $post_date[1] = "";
612
+ }
613
+ }
614
+ $this->defaults[SF_FPRE.'post_date'] = $post_date;
615
+
616
+
617
+ $post_types = array();
618
+ if(isset($wp_query->query['post_types']))
619
+ {
620
+ $post_types = explode(",",esc_attr($wp_query->query['post_types']));
621
+ }
622
+ $this->defaults[SF_FPRE.'post_types'] = $post_types;
623
+
624
+ }
625
+
626
+ /*
627
+ * check to see if form has been submitted and handle vars
628
+ */
629
+
630
+ public function check_posts()
631
+ {
632
+ if(isset($_POST[SF_FPRE.'submitted']))
633
+ {
634
+ if($_POST[SF_FPRE.'submitted']==="1")
635
+ {
636
+ //set var to confirm the form was posted
637
+ $this->has_form_posted = true;
638
+ }
639
+ }
640
+
641
+ /* CATEGORIES */
642
+ if((isset($_POST[SF_FPRE.'category']))&&($this->has_form_posted))
643
+ {
644
+ $the_post_cat = ($_POST[SF_FPRE.'category']);
645
+
646
+ //make the post an array for easy looping
647
+ if(!is_array($_POST[SF_FPRE.'category']))
648
+ {
649
+ $post_cat[] = $the_post_cat;
650
+ }
651
+ else
652
+ {
653
+ $post_cat = $the_post_cat;
654
+ }
655
+ $catarr = array();
656
+
657
+ foreach ($post_cat as $cat)
658
+ {
659
+ $cat = esc_attr($cat);
660
+ $catobj = get_category($cat);
661
+
662
+ if(isset($catobj->slug))
663
+ {
664
+ $catarr[] = $catobj->slug;
665
+ //$catarr[] = $catobj->term_id;
666
+ }
667
+ }
668
+
669
+ if(count($catarr)>0)
670
+ {
671
+ $operator = "+"; //default behaviour
672
+
673
+ //check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects
674
+ if(isset($_POST[SF_FPRE.'category_operator']))
675
+ {
676
+ if($_POST[SF_FPRE.'category_operator']=="and")
677
+ {
678
+ $operator = "+";
679
+ }
680
+ else if($_POST[SF_FPRE.'category_operator']=="or")
681
+ {
682
+ $operator = ",";
683
+ }
684
+ else
685
+ {
686
+ $operator = "+";
687
+ }
688
+ }
689
+
690
+ $categories = implode($operator,$catarr);
691
+
692
+ if(get_option('permalink_structure'))
693
+ {
694
+ //$catrel = trim(str_replace(home_url(), "", get_category_link()), "/").$categories."/"; //get full category link, remvoe the home url to get relative, trim traling slashed, the append slash at the end
695
+ $category_base = (get_option( 'category_base' )=="") ? "category" : get_option( 'category_base' );
696
+ $category_path = $category_base."/".$categories."/";
697
+ $this->urlparams .= $category_path;
698
+ }
699
+ else
700
+ {
701
+ if(!$this->hasqmark)
702
+ {
703
+ $this->urlparams .= "?";
704
+ $this->hasqmark = true;
705
+ }
706
+ else
707
+ {
708
+ $this->urlparams .= "&";
709
+ }
710
+ $this->urlparams .= "category_name=".$categories;
711
+ }
712
+ }
713
+ }
714
+
715
+ /* SEARCH BOX */
716
+ if((isset($_POST[SF_FPRE.'search']))&&($this->has_form_posted))
717
+ {
718
+ $this->searchterm = trim(stripslashes($_POST[SF_FPRE.'search']));
719
+
720
+ if($this->searchterm!="")
721
+ {
722
+ if(!$this->hasqmark)
723
+ {
724
+ $this->urlparams .= "?";
725
+ $this->hasqmark = true;
726
+ }
727
+ else
728
+ {
729
+ $this->urlparams .= "&";
730
+ }
731
+ $this->urlparams .= "s=".urlencode($this->searchterm);
732
+ $this->hassearchquery = true;
733
+ }
734
+ }
735
+ if(!$this->hassearchquery)
736
+ {
737
+
738
+ if((isset($_POST[SF_FPRE.'add_search_param']))&&($this->has_form_posted))
739
+ {//this is only set when a search box is displayed - it tells S&F to append a blank search to the URL to indicate a search has been submitted with no terms, however, still load the search template
740
+
741
+
742
+
743
+ if(!$this->hasqmark)
744
+ {
745
+ $this->urlparams .= "?";
746
+ $this->hasqmark = true;
747
+ }
748
+ else
749
+ {
750
+ $this->urlparams .= "&";
751
+ }
752
+ $this->urlparams .= "s=";
753
+ }
754
+ }
755
+
756
+ /* TAGS */
757
+ if((isset($_POST[SF_FPRE.'post_tag']))&&($this->has_form_posted))
758
+ {
759
+ $the_post_tag = ($_POST[SF_FPRE.'post_tag']);
760
+
761
+ //make the post an array for easy looping
762
+ if(!is_array($_POST[SF_FPRE.'post_tag']))
763
+ {
764
+ $post_tag[] = $the_post_tag;
765
+ }
766
+ else
767
+ {
768
+ $post_tag = $the_post_tag;
769
+ }
770
+
771
+ $tagarr = array();
772
+
773
+ foreach ($post_tag as $tag)
774
+ {
775
+ $tag = esc_attr($tag);
776
+ $tagobj = get_tag($tag);
777
+
778
+ if(isset($tagobj->slug))
779
+ {
780
+ $tagarr[] = $tagobj->slug;
781
+ }
782
+ }
783
+
784
+ if(count($tagarr)>0)
785
+ {
786
+ $operator = "+"; //default behaviour
787
+
788
+ //check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects
789
+ if(isset($_POST[SF_FPRE.'post_tag_operator']))
790
+ {
791
+ if($_POST[SF_FPRE.'post_tag_operator']=="and")
792
+ {
793
+ $operator = "+";
794
+ }
795
+ else if($_POST[SF_FPRE.'post_tag_operator']=="or")
796
+ {
797
+ $operator = ",";
798
+ }
799
+ else
800
+ {
801
+ $operator = "+";
802
+ }
803
+ }
804
+
805
+ $tags = implode($operator,$tagarr);
806
+
807
+ if(!$this->hasqmark)
808
+ {
809
+ $this->urlparams .= "?";
810
+ $this->hasqmark = true;
811
+ }
812
+ else
813
+ {
814
+ $this->urlparams .= "&";
815
+ }
816
+ $this->urlparams .= "tag=".$tags;
817
+
818
+ }
819
+ }
820
+
821
+
822
+ /* POST TYPES */
823
+ if((isset($_POST[SF_FPRE.'post_types']))&&($this->has_form_posted))
824
+ {
825
+ $the_post_types = ($_POST[SF_FPRE.'post_types']);
826
+
827
+ //make the post an array for easy looping
828
+ if(!is_array($the_post_types))
829
+ {
830
+ $post_types_arr[] = $the_post_types;
831
+ }
832
+ else
833
+ {
834
+ $post_types_arr = $the_post_types;
835
+ }
836
+
837
+ $num_post_types = count($post_types_arr);
838
+
839
+ for($i=0; $i<$num_post_types; $i++)
840
+ {
841
+ if($post_types_arr[$i]=="0")
842
+ {
843
+ $post_types_arr[$i] = "all";
844
+ }
845
+ }
846
+
847
+ if(count($post_types_arr)>0)
848
+ {
849
+ $operator = ","; //default behaviour
850
+
851
+ //check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects
852
+ /*if(isset($_POST[SF_FPRE.'post_types_operator']))
853
+ {
854
+ if($_POST[SF_FPRE.'post_types_operator']=="and")
855
+ {
856
+ $operator = "+";
857
+ }
858
+ else if($_POST[SF_FPRE.'post_types_operator']=="or")
859
+ {
860
+ $operator = ",";
861
+ }
862
+ else
863
+ {
864
+ $operator = "+";
865
+ }
866
+ }*/
867
+
868
+ $post_types = implode($operator,$post_types_arr);
869
+
870
+ if(!$this->hasqmark)
871
+ {
872
+ $this->urlparams .= "?";
873
+ $this->hasqmark = true;
874
+ }
875
+ else
876
+ {
877
+ $this->urlparams .= "&";
878
+ }
879
+ $this->urlparams .= "post_types=".$post_types;
880
+
881
+ }
882
+ }
883
+
884
+
885
+ /* POST DATE */
886
+ if((isset($_POST[SF_FPRE.'post_date']))&&($this->has_form_posted))
887
+ {
888
+ $the_post_date = ($_POST[SF_FPRE.'post_date']);
889
+
890
+ //make the post an array for easy looping
891
+ if(!is_array($the_post_date))
892
+ {
893
+ $post_date_arr[] = $the_post_date;
894
+ }
895
+ else
896
+ {
897
+ $post_date_arr = $the_post_date;
898
+ }
899
+
900
+ $num_post_date = count($post_date_arr);
901
+
902
+ for($i=0; $i<$num_post_date; $i++)
903
+ {
904
+ if($post_date_arr[$i]=="0")
905
+ {
906
+ $post_date_arr[$i] = "all";
907
+ }
908
+ }
909
+
910
+ if(count($post_date_arr)>0)
911
+ {
912
+ $post_date_count = count($post_date_arr);
913
+
914
+ if($post_date_count==2)
915
+ {//see if there are 2 elements in arr (second date range selector)
916
+
917
+ if(($post_date_arr[0]!="")&&($post_date_arr[1]==""))
918
+ {
919
+ $post_date = $post_date_arr[0];
920
+ }
921
+ else if($post_date_arr[1]=="")
922
+ {//if second date range is blank then remove the array element - this remove the addition of a '+' by implode below and only use first element
923
+ unset($post_date_arr[1]);
924
+ }
925
+ else if($post_date_arr[0]=="")
926
+ {
927
+ $post_date = "+".$post_date_arr[1];
928
+ }
929
+ else
930
+ {
931
+ $post_date = implode("+",array_filter($post_date_arr));
932
+ }
933
+ }
934
+ else
935
+ {
936
+ $post_date = $post_date_arr[0];
937
+ }
938
+
939
+ if(isset($post_date))
940
+ {
941
+ if($post_date!="")
942
+ {
943
+ if(!$this->hasqmark)
944
+ {
945
+ $this->urlparams .= "?";
946
+ $this->hasqmark = true;
947
+ }
948
+ else
949
+ {
950
+ $this->urlparams .= "&";
951
+ }
952
+ $this->urlparams .= "post_date=".$post_date;
953
+ }
954
+ }
955
+ }
956
+ }
957
+
958
+
959
+ //now we have dealt with the all the special case fields - search, tags, categories, post_types, post_date
960
+
961
+ //loop through the posts - double check that it is the search form that has been posted, otherwise we could be looping through the posts submitted from an entirely unrelated form
962
+ if($this->has_form_posted)
963
+ {
964
+ foreach($_POST as $key=>$val)
965
+ {
966
+ if(!in_array($key, $this->frmreserved))
967
+ {//if the key is not in the reserved array (ie, on a custom taxonomy - not tags, categories, search term, post type & post date)
968
+
969
+ // strip off all prefixes for custom fields - we just want to do a redirect - no processing
970
+ if (strpos($key, SF_FPRE) === 0)
971
+ {
972
+ $key = substr($key, strlen(SF_FPRE));
973
+ }
974
+
975
+ $the_post_tax = $val;
976
+
977
+ //make the post an array for easy looping
978
+ if(!is_array($val))
979
+ {
980
+ $post_tax[] = $the_post_tax;
981
+ }
982
+ else
983
+ {
984
+ $post_tax = $the_post_tax;
985
+ }
986
+ $taxarr = array();
987
+
988
+ foreach ($post_tax as $tax)
989
+ {
990
+ $tax = esc_attr($tax);
991
+ $taxobj = get_term_by('id',$tax,$key);
992
+
993
+ if(isset($taxobj->slug))
994
+ {
995
+ $taxarr[] = $taxobj->slug;
996
+ }
997
+ }
998
+
999
+
1000
+ if(count($taxarr)>0)
1001
+ {
1002
+ $operator = "+"; //default behaviour
1003
+
1004
+ //check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects
1005
+ if(isset($_POST[SF_FPRE.$key.'_operator']))
1006
+ {
1007
+ if($_POST[SF_FPRE.$key.'_operator']=="and")
1008
+ {
1009
+ $operator = "+";
1010
+ }
1011
+ else if($_POST[SF_FPRE.$key.'_operator']=="or")
1012
+ {
1013
+ $operator = ",";
1014
+ }
1015
+ else
1016
+ {
1017
+ $operator = "+";
1018
+ }
1019
+ }
1020
+
1021
+ $tags = implode($operator,$taxarr);
1022
+
1023
+ if(!$this->hasqmark)
1024
+ {
1025
+ $this->urlparams .= "?";
1026
+ $this->hasqmark = true;
1027
+ }
1028
+ else
1029
+ {
1030
+ $this->urlparams .= "&";
1031
+ }
1032
+ $this->urlparams .= $key."=".$tags;
1033
+
1034
+ }
1035
+ }
1036
+ }
1037
+ }
1038
+
1039
+
1040
+ if($this->has_form_posted)
1041
+ {//if the search has been posted, redirect to the newly formed url with all the right params
1042
+
1043
+ if($this->urlparams=="/")
1044
+ {//check to see if url params are set, if not ("/") then add "?s=" to force load search results, without this it would redirect to the homepage, which may be a custom page with no blog items/results
1045
+ $this->urlparams .= "?s=";
1046
+ }
1047
+
1048
+ if($this->urlparams=="/?s=")
1049
+ {//if a blank search was submitted - need to check for this string here in case `add_search_param` has already added a "?s=" to the url
1050
+
1051
+ if(isset($_POST[SF_FPRE.'empty_search_url']))
1052
+ {//then redirect to the provided empty search url
1053
+
1054
+ wp_redirect(esc_url($_POST[SF_FPRE.'empty_search_url']));
1055
+ exit;
1056
+ }
1057
+ }
1058
+
1059
+ wp_redirect((home_url().$this->urlparams));
1060
+ }
1061
+ }
1062
+
1063
+ public function get_search_filter_form($submitlabel, $search_placeholder, $fields, $types, $labels, $hierarchical, $hide_empty, $show_count, $post_types, $order_by, $order_dir, $operators, $all_items_labels, $empty_search_url, $add_search_param, $class)
1064
+ {
1065
+ $returnvar = '';
1066
+
1067
+ $addclass = "";
1068
+ if($class!="")
1069
+ {
1070
+ $addclass = ' '.$class;
1071
+ }
1072
+
1073
+ $returnvar .= '
1074
+ <form action="" method="post" class="searchandfilter'.$addclass.'">
1075
+ <div>';
1076
+
1077
+ if(!in_array("post_types", $fields))
1078
+ {//then the user has not added it to the fields list so the user does not want a post types drop down... so add (if any) the post types to a hidden attribute
1079
+
1080
+ if(($post_types!="")&&(is_array($post_types)))
1081
+ {
1082
+ foreach($post_types as $post_type)
1083
+ {
1084
+ $returnvar .= "<input type=\"hidden\" name=\"".SF_FPRE."post_types[]\" value=\"".$post_type."\" />";
1085
+ }
1086
+ }
1087
+ }
1088
+ $returnvar .= '
1089
+ <ul>';
1090
+
1091
+ $i = 0;
1092
+
1093
+ foreach($fields as $field)
1094
+ {
1095
+ //special cases - post_types & post_date.. all others assumed regular wp taxonomy
1096
+
1097
+ if($field == "search")
1098
+ {
1099
+ $returnvar .= '<li>';
1100
+ if($labels[$i]!="")
1101
+ {
1102
+ $returnvar .= "<h4>".$labels[$i]."</h4>";
1103
+ }
1104
+ $clean_searchterm = (esc_attr($this->searchterm));
1105
+ $returnvar .= '<input type="text" name="'.SF_FPRE.'search" placeholder="'.$search_placeholder.'" value="'.$clean_searchterm.'">';
1106
+ $returnvar .= '</li>';
1107
+ }
1108
+ else if($field == "post_types") //a post can only every have 1 type, so checkboxes & multiselects will always be "OR"
1109
+ {//build field array
1110
+
1111
+ //check to see if operator is set for this field
1112
+ /*if(isset($operators[$i]))
1113
+ {
1114
+ $operators[$i] = strtolower($operators[$i]);
1115
+
1116
+ if(($operators[$i]=="and")||($operators[$i]=="or"))
1117
+ {
1118
+ $returnvar .= '<input type="hidden" name="'.SF_FPRE.$field.'_operator" value="'.$operators[$i].'" />';
1119
+ }
1120
+ }*/
1121
+
1122
+
1123
+ $returnvar .= $this->build_post_type_element($types, $labels, $post_types, $field, $all_items_labels, $i);
1124
+
1125
+ }
1126
+ else if($field == 'post_date')
1127
+ {
1128
+ $returnvar .= $this->build_post_date_element($labels, $i, $types, $field);
1129
+ }
1130
+ else
1131
+ {
1132
+ $returnvar .= $this->build_taxonomy_element($types, $labels, $field, $hierarchical, $hide_empty, $show_count, $order_by, $order_dir, $operators, $all_items_labels, $i);
1133
+ }
1134
+ $i++;
1135
+
1136
+ }
1137
+
1138
+ $returnvar .='<li>';
1139
+
1140
+ if($add_search_param==1)
1141
+ {
1142
+ $returnvar .= "<input type=\"hidden\" name=\"".SF_FPRE."add_search_param\" value=\"1\" />";
1143
+ }
1144
+
1145
+ if($empty_search_url!="")
1146
+ {
1147
+ $returnvar .= "<input type=\"hidden\" name=\"".SF_FPRE."empty_search_url\" value=\"".esc_url($empty_search_url)."\" />";
1148
+ }
1149
+
1150
+
1151
+ $returnvar .=
1152
+ '<input type="hidden" name="'.SF_FPRE.'submitted" value="1">
1153
+ <input type="submit" value="'.$submitlabel.'">
1154
+ </li>';
1155
+
1156
+ $returnvar .= "</ul>";
1157
+ $returnvar .= '</div>
1158
+ </form>';
1159
+
1160
+ return $returnvar;
1161
+ }
1162
+
1163
+ ///////////////////////////////////////////////////////////
1164
+ function build_post_date_element($labels, $i, $types, $field)
1165
+ {
1166
+ $returnvar = "";
1167
+
1168
+ $taxonomychildren = array();
1169
+
1170
+ $taxonomychildren = (object)$taxonomychildren;
1171
+
1172
+ $returnvar .= "<li>";
1173
+
1174
+ if($labels[$i]!="")
1175
+ {
1176
+ $returnvar .= "<h4>".$labels[$i]."</h4>";
1177
+ }
1178
+
1179
+ $defaultval = "";
1180
+
1181
+ if($types[$i]=="date")
1182
+ {
1183
+ $returnvar .= $this->generate_date($taxonomychildren, $field, $this->tagid);
1184
+ }
1185
+ if($types[$i]=="daterange")
1186
+ {
1187
+ $returnvar .= $this->generate_date($taxonomychildren, $field, 0);
1188
+ $returnvar .= "</li><li>";
1189
+ $returnvar .= $this->generate_date($taxonomychildren, $field, 1);
1190
+ }
1191
+ $returnvar .= "</li>";
1192
+
1193
+ return $returnvar;
1194
+ }
1195
+
1196
+
1197
+ function build_post_type_element($types, $labels, $post_types, $field, $all_items_labels, $i)
1198
+ {
1199
+ $returnvar = "";
1200
+ $taxonomychildren = array();
1201
+ $post_type_count = count($post_types);
1202
+
1203
+ //then check the post types array
1204
+ if(is_array($post_types))
1205
+ {
1206
+ if(($post_type_count==1)&&($post_types[0]=="all"))
1207
+ {
1208
+ $args = array('public' => true);
1209
+ $output = 'object'; // names or objects, note names is the default
1210
+ $operator = 'and'; // 'and' or 'or'
1211
+
1212
+ $post_types_objs = get_post_types( $args, $output, $operator );
1213
+
1214
+ $post_types = array();
1215
+
1216
+ foreach ( $post_types_objs as $post_type )
1217
+ {
1218
+ if($post_type->name!="attachment")
1219
+ {
1220
+ $tempobject = array();
1221
+ $tempobject['term_id'] = $post_type->name;
1222
+ $tempobject['cat_name'] = $post_type->labels->name;
1223
+
1224
+ $taxonomychildren[] = (object)$tempobject;
1225
+
1226
+ $post_types[] = $post_type->name;
1227
+
1228
+ }
1229
+ }
1230
+ $post_type_count = count($post_types_objs);
1231
+
1232
+ }
1233
+ else
1234
+ {
1235
+ foreach($post_types as $post_type)
1236
+ {
1237
+ //var_dump(get_post_type_object( $post_type ));
1238
+ $post_type_data = get_post_type_object( $post_type );
1239
+
1240
+ if($post_type_data)
1241
+ {
1242
+ $tempobject = array();
1243
+ $tempobject['term_id'] = $post_type;
1244
+ $tempobject['cat_name'] = $post_type_data->labels->name;
1245
+
1246
+ $taxonomychildren[] = (object)$tempobject;
1247
+ }
1248
+ }
1249
+ }
1250
+ }
1251
+ $taxonomychildren = (object)$taxonomychildren;
1252
+
1253
+ $returnvar .= "<li>";
1254
+
1255
+ $post_type_labels = array();
1256
+ $post_type_labels['name'] = "Post Types";
1257
+ $post_type_labels['singular_name'] = "Post Type";
1258
+ $post_type_labels['search_items'] = "Search Post Types";
1259
+
1260
+ if($all_items_labels[$i]!="")
1261
+ {
1262
+ $post_type_labels['all_items'] = $all_items_labels[$i];
1263
+ }
1264
+ else
1265
+ {
1266
+ $post_type_labels['all_items'] = "All Post Types";
1267
+ }
1268
+
1269
+ $post_type_labels = (object)$post_type_labels;
1270
+
1271
+ if($labels[$i]!="")
1272
+ {
1273
+ $returnvar .= "<h4>".$labels[$i]."</h4>";
1274
+ }
1275
+
1276
+ if($post_type_count>0)
1277
+ {
1278
+ $defaultval = implode(",",$post_types);
1279
+ }
1280
+ else
1281
+ {
1282
+ $defaultval = "all";
1283
+ }
1284
+
1285
+ if($types[$i]=="select")
1286
+ {
1287
+ $returnvar .= $this->generate_select($taxonomychildren, $field, $this->tagid, $post_type_labels, $defaultval);
1288
+ }
1289
+ else if($types[$i]=="checkbox")
1290
+ {
1291
+
1292
+ $returnvar .= $this->generate_checkbox($taxonomychildren, $field, $this->tagid);
1293
+ }
1294
+ else if($types[$i]=="radio")
1295
+ {
1296
+ $returnvar .= $this->generate_radio($taxonomychildren, $field, $this->tagid, $post_type_labels, $defaultval);
1297
+ }
1298
+ $returnvar .= "</li>";
1299
+
1300
+ return $returnvar;
1301
+ }
1302
+
1303
+ //gets all the data for the taxonomy then display as form element
1304
+ function build_taxonomy_element($types, $labels, $taxonomy, $hierarchical, $hide_empty, $show_count, $order_by, $order_dir, $operators, $all_items_labels, $i)
1305
+ {
1306
+ $returnvar = "";
1307
+
1308
+ $taxonomydata = get_taxonomy($taxonomy);
1309
+
1310
+ if($taxonomydata)
1311
+ {
1312
+ $returnvar .= "<li>";
1313
+
1314
+ if($labels[$i]!="")
1315
+ {
1316
+ $returnvar .= "<h4>".$labels[$i]."</h4>";
1317
+ }
1318
+
1319
+ $args = array(
1320
+ 'name' => SF_FPRE . $taxonomy,
1321
+ 'taxonomy' => $taxonomy,
1322
+ 'hierarchical' => false,
1323
+ 'child_of' => 0,
1324
+ 'echo' => false,
1325
+ 'hide_if_empty' => false,
1326
+ 'hide_empty' => true,
1327
+ 'order' => $order_dir[$i],
1328
+ 'orderby' => $order_by[$i],
1329
+ 'show_option_none' => '',
1330
+ 'show_count' => '0',
1331
+ 'show_option_all' => '',
1332
+ 'show_option_all_sf' => ''
1333
+ );
1334
+
1335
+ if(isset($hierarchical[$i]))
1336
+ {
1337
+ if($hierarchical[$i]==1)
1338
+ {
1339
+ $args['hierarchical'] = true;
1340
+ }
1341
+ }
1342
+
1343
+ if(isset($hide_empty[$i]))
1344
+ {
1345
+ if($hide_empty[$i]==0)
1346
+ {
1347
+ $args['hide_empty'] = false;
1348
+ }
1349
+ }
1350
+
1351
+ if(isset($show_count[$i]))
1352
+ {
1353
+ if($show_count[$i]==1)
1354
+ {
1355
+ $args['show_count'] = true;
1356
+ }
1357
+ }
1358
+
1359
+ if($all_items_labels[$i]!="")
1360
+ {
1361
+ $args['show_option_all_sf'] = $all_items_labels[$i];
1362
+ }
1363
+
1364
+
1365
+
1366
+ $taxonomychildren = get_categories($args);
1367
+
1368
+ if($types[$i]=="select")
1369
+ {
1370
+ $returnvar .= $this->generate_wp_dropdown($args, $taxonomy, $this->tagid, $taxonomydata->labels);
1371
+ }
1372
+ else if($types[$i]=="checkbox")
1373
+ {
1374
+ $args['title_li'] = '';
1375
+ $args['defaults'] = "";
1376
+ if(isset($this->defaults[$args['name']]))
1377
+ {
1378
+ $args['defaults'] = $this->defaults[$args['name']];
1379
+ }
1380
+ //$args['show_option_all'] = 0;
1381
+
1382
+ $returnvar .= $this->generate_wp_checkbox($args, $taxonomy, $this->tagid, $taxonomydata->labels);
1383
+ }
1384
+ else if($types[$i]=="radio")
1385
+ {
1386
+ $args['title_li'] = '';
1387
+ $args['defaults'] = "";
1388
+ if(isset($this->defaults[$args['name']]))
1389
+ {
1390
+ $args['defaults'] = $this->defaults[$args['name']];
1391
+ }
1392
+
1393
+ $returnvar .= $this->generate_wp_radio($args, $taxonomy, $this->tagid, $taxonomydata->labels);
1394
+ }
1395
+ else if($types[$i]=="multiselect")
1396
+ {
1397
+ $args['title_li'] = '';
1398
+ $args['defaults'] = "";
1399
+ if(isset($this->defaults[$args['name']]))
1400
+ {
1401
+ $args['defaults'] = $this->defaults[$args['name']];
1402
+ }
1403
+
1404
+ $returnvar .= $this->generate_wp_multiselect($args, $taxonomy, $this->tagid, $taxonomydata->labels);
1405
+ }
1406
+
1407
+ //check to see if operator is set for this field
1408
+ if(isset($operators[$i]))
1409
+ {
1410
+ $operators[$i] = strtolower($operators[$i]);
1411
+
1412
+ if(($operators[$i]=="and")||($operators[$i]=="or"))
1413
+ {
1414
+ $returnvar .= '<input type="hidden" name="'.SF_FPRE.$taxonomy.'_operator" value="'.$operators[$i].'" />';
1415
+ }
1416
+ }
1417
+
1418
+ $returnvar .= "</li>";
1419
+ }
1420
+
1421
+ return $returnvar;
1422
+ }
1423
+
1424
+
1425
+ /*
1426
+ * Display various forms
1427
+ */
1428
+
1429
+ //use wp array walker to enable hierarchical display
1430
+ public function generate_wp_dropdown($args, $name, $currentid = 0, $labels = null, $defaultval = "0")
1431
+ {
1432
+ $returnvar = '';
1433
+
1434
+ if($args['show_option_all_sf']=="")
1435
+ {
1436
+ $args['show_option_all'] = $labels->all_items != "" ? $labels->all_items : 'All ' . $labels->name;
1437
+ }
1438
+ else
1439
+ {
1440
+ $args['show_option_all'] = $args['show_option_all_sf'];
1441
+ }
1442
+
1443
+ if(isset($this->defaults[SF_FPRE.$name]))
1444
+ {
1445
+ $defaults = $this->defaults[SF_FPRE . $name];
1446
+ if (is_array($defaults)) {
1447
+ if (count($defaults) == 1) {
1448
+ $args['selected'] = $defaults[0];
1449
+ }
1450
+ }
1451
+ else {
1452
+ $args['selected'] = $defaultval;
1453
+ }
1454
+ }
1455
+
1456
+ $returnvar .= wp_dropdown_categories($args);
1457
+
1458
+ return $returnvar;
1459
+ }
1460
+
1461
+ //use wp array walker to enable hierarchical display
1462
+ public function generate_wp_multiselect($args, $name, $currentid = 0, $labels = null, $defaultval = "0")
1463
+ {
1464
+ $returnvar = '<select multiple="multiple" name="'.$args['name'].'[]" class="postform">';
1465
+ $returnvar .= walk_taxonomy('multiselect', $args);
1466
+ $returnvar .= "</select>";
1467
+
1468
+ return $returnvar;
1469
+ }
1470
+
1471
+ //use wp array walker to enable hierarchical display
1472
+ public function generate_wp_checkbox($args, $name, $currentid = 0, $labels = null, $defaultval = "0")
1473
+ {
1474
+ $returnvar = '<ul>';
1475
+ $returnvar .= walk_taxonomy('checkbox', $args);
1476
+ $returnvar .= "</ul>";
1477
+
1478
+ return $returnvar;
1479
+ }
1480
+
1481
+ //use wp array walker to enable hierarchical display
1482
+ public function generate_wp_radio($args, $name, $currentid = 0, $labels = null, $defaultval = "0")
1483
+ {
1484
+
1485
+ if($args['show_option_all_sf']=="")
1486
+ {
1487
+ $show_option_all = $labels->all_items != "" ? $labels->all_items : 'All ' . $labels->name;
1488
+ }
1489
+ else
1490
+ {
1491
+ $show_option_all = $args['show_option_all_sf'];
1492
+ }
1493
+
1494
+ $checked = ($defaultval=="0") ? " checked='checked'" : "";
1495
+ $returnvar = '<ul>';
1496
+ $returnvar .= '<li>'."<label><input type='radio' name='".$args['name']."[]' value='0'$checked /> ".$show_option_all."</label>".'</li>';
1497
+ $returnvar .= walk_taxonomy('radio', $args);
1498
+ $returnvar .= "</ul>";
1499
+
1500
+ return $returnvar;
1501
+ }
1502
+
1503
+ //generate generic form inputs for use elsewhere, such as post types and non taxonomy fields
1504
+ public function generate_select($dropdata, $name, $currentid = 0, $labels = null, $defaultval = "0")
1505
+ {
1506
+ $returnvar = "";
1507
+
1508
+ $returnvar .= '<select class="postform" name="'.SF_FPRE.$name.'">';
1509
+ if(isset($labels))
1510
+ {
1511
+ if($labels->all_items!="")
1512
+ {//check to see if all items has been registered in field then use this label
1513
+ $returnvar .= '<option class="level-0" value="'.$defaultval.'">'.$labels->all_items.'</option>';
1514
+ }
1515
+ else
1516
+ {//check to see if all items has been registered in field then use this label with prefix of "All"
1517
+ $returnvar .= '<option class="level-0" value="'.$defaultval.'">All '.$labels->name.'</option>';
1518
+ }
1519
+ }
1520
+
1521
+ foreach($dropdata as $dropdown)
1522
+ {
1523
+ $selected = "";
1524
+
1525
+ if(isset($this->defaults[SF_FPRE.$name]))
1526
+ {
1527
+ $defaults = $this->defaults[SF_FPRE.$name];
1528
+
1529
+ $noselected = count($defaults);
1530
+
1531
+ if(($noselected==1)&&(is_array($defaults))) //there should never be more than 1 default in a select, if there are then don't set any, user is obviously searching multiple values, in the case of a select this must be "all"
1532
+ {
1533
+ foreach($defaults as $defaultid)
1534
+ {
1535
+ if($defaultid==$dropdown->term_id)
1536
+ {
1537
+ $selected = ' selected="selected"';
1538
+ }
1539
+ }
1540
+ }
1541
+ }
1542
+ $returnvar .= '<option class="level-0" value="'.$dropdown->term_id.'"'.$selected.'>'.$dropdown->cat_name.'</option>';
1543
+
1544
+ }
1545
+ $returnvar .= "</select>";
1546
+
1547
+ return $returnvar;
1548
+ }
1549
+
1550
+ public function generate_checkbox($dropdata, $name, $currentid = 0, $labels = null, $defaultval = '')
1551
+ {
1552
+ $returnvar = '<ul>';
1553
+
1554
+ foreach($dropdata as $dropdown)
1555
+ {
1556
+ $checked = "";
1557
+
1558
+ //check a default has been set
1559
+ if(isset($this->defaults[SF_FPRE.$name]))
1560
+ {
1561
+ $defaults = $this->defaults[SF_FPRE.$name];
1562
+
1563
+ $noselected = count($defaults);
1564
+
1565
+ if(($noselected>0)&&(is_array($defaults)))
1566
+ {
1567
+ foreach($defaults as $defaultid)
1568
+ {
1569
+ if($defaultid==$dropdown->term_id)
1570
+ {
1571
+ $checked = ' checked="checked"';
1572
+ }
1573
+ }
1574
+ }
1575
+ }
1576
+ $returnvar .= '<li class="cat-item"><label><input class="postform cat-item" type="checkbox" name="'.SF_FPRE.$name.'[]" value="'.$dropdown->term_id.'"'.$checked.'> '.$dropdown->cat_name.'</label></li>';
1577
+
1578
+ }
1579
+
1580
+ $returnvar .= '</ul>';
1581
+
1582
+ return $returnvar;
1583
+ }
1584
+
1585
+
1586
+ public function generate_radio($dropdata, $name, $currentid = 0, $labels = null, $defaultval = "0")
1587
+ {
1588
+ $returnvar = '<ul>';
1589
+
1590
+ if(isset($labels))
1591
+ {
1592
+ $checked = "";
1593
+ if(isset($this->defaults[SF_FPRE.$name]))
1594
+ {
1595
+ $defaults = $this->defaults[SF_FPRE.$name];
1596
+ $noselected = count($defaults);
1597
+
1598
+ if($noselected==0)
1599
+ {
1600
+ $checked = ' checked="checked"';
1601
+ }
1602
+ else if($noselected==1)
1603
+ {
1604
+ if($this->defaults[SF_FPRE.$name][0]==$defaultval)
1605
+ {
1606
+ $checked = ' checked="checked"';
1607
+ }
1608
+ }
1609
+ }
1610
+ else
1611
+ {
1612
+ $checked = ' checked="checked"';
1613
+ }
1614
+
1615
+ if(isset($this->defaults[SF_FPRE.$name]))
1616
+ {
1617
+ $defaults = $this->defaults[SF_FPRE.$name];
1618
+ if(count($defaults)>1)
1619
+ {//then we are dealing with multiple defaults - this means mutliple radios are selected, this is only possible with "ALL" so set as default.
1620
+ $checked = ' checked="checked"';
1621
+ }
1622
+ }
1623
+
1624
+ $all_items_name = "";
1625
+ if($labels->all_items!="")
1626
+ {//check to see if all items has been registered in field then use this label
1627
+ $all_items_name = $labels->all_items;
1628
+ }
1629
+ else
1630
+ {//check to see if all items has been registered in field then use this label with prefix of "All"
1631
+ $all_items_name = "All ".$labels->name;
1632
+ }
1633
+
1634
+ $returnvar .= '<li class="cat-item"><label><input class="postform" type="radio" name="'.SF_FPRE.$name.'[]" value="'.$defaultval.'"'.$checked.'> '.$all_items_name.'</label></li>';
1635
+ }
1636
+
1637
+ foreach($dropdata as $dropdown)
1638
+ {
1639
+ $checked = "";
1640
+
1641
+ //check a default has been set
1642
+ if(isset($this->defaults[SF_FPRE.$name]))
1643
+ {
1644
+ $defaults = $this->defaults[SF_FPRE.$name];
1645
+
1646
+ $noselected = count($defaults);
1647
+
1648
+ if(($noselected==1)&&(is_array($defaults)))
1649
+ {
1650
+ foreach($defaults as $defaultid)
1651
+ {
1652
+ if($defaultid==$dropdown->term_id)
1653
+ {
1654
+ $checked = ' checked="checked"';
1655
+ }
1656
+ }
1657
+ }
1658
+ }
1659
+ $returnvar .= '<li class="cat-item"><label><input class="postform" type="radio" name="'.SF_FPRE.$name.'[]" value="'.$dropdown->term_id.'"'.$checked.'> '.$dropdown->cat_name.'</label></li>';
1660
+
1661
+ }
1662
+
1663
+ $returnvar .= '</ul>';
1664
+
1665
+ return $returnvar;
1666
+ }
1667
+
1668
+ public function generate_date($dropdata, $name, $currentid = 0, $labels = null, $defaultval = "0")
1669
+ {
1670
+ $returnvar = '';
1671
+ $current_date = '';
1672
+ //check a default has been set - upto two possible vars for array
1673
+
1674
+ if(isset($this->defaults[SF_FPRE.$name]))
1675
+ {
1676
+ $defaults = $this->defaults[SF_FPRE.$name];
1677
+
1678
+ $noselected = count($defaults);
1679
+
1680
+ if(($noselected>0)&&(is_array($defaults)))
1681
+ {
1682
+ $current_date = $defaults[$currentid];
1683
+ }
1684
+ }
1685
+
1686
+ $returnvar .= '<input class="postform" type="date" name="'.SF_FPRE.$name.'[]" value="' . $current_date . '" />';
1687
+
1688
+ return $returnvar;
1689
+ }
1690
+ }
1691
+ }
1692
+
1693
+
1694
+ function walk_taxonomy( $type = "checkbox", $args = array() ) {
1695
+
1696
+ $args['walker'] = new Taxonomy_Walker($type, $args['name']);
1697
+
1698
+ $output = wp_list_categories($args);
1699
+ if ( $output )
1700
+ return $output;
1701
+ }
1702
+
1703
+
1704
+
1705
+
1706
+ if ( class_exists( 'SearchAndFilter' ) )
1707
+ {
1708
+ global $SearchAndFilter;
1709
+ $SearchAndFilter = new SearchAndFilter();
1710
+ }
1711
+
1712
+ /*
1713
+ * Includes
1714
+ */
1715
+
1716
+ // classes
1717
+ require_once(SEARCHANDFILTER_PLUGIN_DIR."/of-list-table.php");
1718
+ require_once(SEARCHANDFILTER_PLUGIN_DIR."/of-taxonomy-walker.php");
1719
+
1720
+ // admin screens & plugin mods
1721
+ require_once(SEARCHANDFILTER_PLUGIN_DIR."/of-admin.php");
1722
+
1723
+
1724
  ?>