| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Search & Filter |
| 4 |
Plugin URI: https://free.searchandfilter.com/ |
| 5 |
Description: Search and Filtering system for Pages, Posts, Categories, Tags and Taxonomies |
| 6 |
Author: Code Amp |
| 7 |
Author URI: https://codeamp.com |
| 8 |
Version: 1.2.18 |
| 9 |
Text Domain: searchandfilter |
| 10 |
License: GPLv2 |
| 11 |
*/ |
| 12 |
|
| 13 |
/* |
| 14 |
* Set up Plugin Globals |
| 15 |
*/ |
| 16 |
if ( ! defined( 'SEARCHANDFILTER_VERSION_NUM' ) ) { |
| 17 |
define( 'SEARCHANDFILTER_VERSION_NUM', '1.2.17' ); |
| 18 |
} |
| 19 |
|
| 20 |
if ( ! defined( 'SEARCHANDFILTER_THEME_DIR' ) ) { |
| 21 |
define( 'SEARCHANDFILTER_THEME_DIR', ABSPATH . 'wp-content/themes/' . get_template() ); |
| 22 |
} |
| 23 |
|
| 24 |
if ( ! defined( 'SEARCHANDFILTER_PLUGIN_NAME' ) ) { |
| 25 |
define( 'SEARCHANDFILTER_PLUGIN_NAME', trim( dirname( plugin_basename( __FILE__ ) ), '/' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
if ( ! defined( 'SEARCHANDFILTER_PLUGIN_DIR' ) ) { |
| 29 |
define( 'SEARCHANDFILTER_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . SEARCHANDFILTER_PLUGIN_NAME ); |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! defined( 'SEARCHANDFILTER_PLUGIN_URL' ) ) { |
| 33 |
define( 'SEARCHANDFILTER_PLUGIN_URL', WP_PLUGIN_URL . '/' . SEARCHANDFILTER_PLUGIN_NAME ); |
| 34 |
} |
| 35 |
|
| 36 |
if ( ! defined( 'SEARCHANDFILTER_BASENAME' ) ) { |
| 37 |
define( 'SEARCHANDFILTER_BASENAME', plugin_basename( __FILE__ ) ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( ! defined( 'SEARCHANDFILTER_VERSION_KEY' ) ) { |
| 41 |
define( 'SEARCHANDFILTER_VERSION_KEY', 'searchandfilter_version' ); |
| 42 |
} |
| 43 |
|
| 44 |
// Form prefix for plugin. |
| 45 |
if ( ! defined( 'SF_FPRE' ) ) { |
| 46 |
define( 'SF_FPRE', 'of' ); |
| 47 |
} |
| 48 |
|
| 49 |
add_option( SEARCHANDFILTER_VERSION_KEY, SEARCHANDFILTER_VERSION_NUM ); |
| 50 |
|
| 51 |
/* |
| 52 |
* Set up Plugin Globals |
| 53 |
*/ |
| 54 |
if ( ! class_exists( 'SearchAndFilter' ) ) { |
| 55 |
class SearchAndFilter { |
| 56 |
|
| 57 |
private $has_form_posted = false; |
| 58 |
private $hasqmark = false; |
| 59 |
private $hassearchquery = false; |
| 60 |
private $urlparams = '/'; |
| 61 |
private $searchterm = ''; |
| 62 |
private $tagid = 0; |
| 63 |
private $catid = 0; |
| 64 |
private $defaults = array(); |
| 65 |
private $frmreserved = array(); |
| 66 |
private $frmqreserved = array(); |
| 67 |
private $taxonomylist = array(); |
| 68 |
|
| 69 |
public function __construct() { |
| 70 |
// Set up reserved fields. |
| 71 |
$this->frmreserved = array( SF_FPRE . 'category', SF_FPRE . 'search', SF_FPRE . 'post_tag', SF_FPRE . 'submitted', SF_FPRE . 'post_date', SF_FPRE . 'post_types' ); |
| 72 |
$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 |
| 73 |
|
| 74 |
// Add query vars. |
| 75 |
add_filter( 'query_vars', array( $this, 'add_queryvars' ) ); |
| 76 |
|
| 77 |
// Filter post type & date if it is set. |
| 78 |
add_filter( 'pre_get_posts', array( $this, 'filter_query_post_types' ) ); |
| 79 |
add_filter( 'pre_get_posts', array( $this, 'filter_query_post_date' ) ); |
| 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( 'template_redirect', 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 |
wp_enqueue_style( 'searchandfilter', SEARCHANDFILTER_PLUGIN_URL . '/style.css', false, 1.0, 'all' ); |
| 96 |
} |
| 97 |
public function of_enqueue_admin_ss( $hook ) { |
| 98 |
if ( 'toplevel_page_searchandfilter-settings' == $hook ) { |
| 99 |
wp_enqueue_style( 'of_syntax_style', SEARCHANDFILTER_PLUGIN_URL . '/admin/github.css', false, 1.0, 'all' ); |
| 100 |
wp_enqueue_style( 'of_style', SEARCHANDFILTER_PLUGIN_URL . '/admin/style.css', false, 1.0, 'all' ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public function shortcode( $atts, $content = null ) { |
| 105 |
// Extract the attributes into variables. |
| 106 |
extract( |
| 107 |
shortcode_atts( |
| 108 |
array( |
| 109 |
|
| 110 |
'fields' => null, |
| 111 |
'taxonomies' => null, // Will be deprecated - use `fields` instead. |
| 112 |
'submit_label' => null, |
| 113 |
'submitlabel' => null, // Will be deprecated - use `submit_label` instead. |
| 114 |
'search_placeholder' => 'Search …', |
| 115 |
'types' => '', |
| 116 |
'type' => '', // Will be deprecated - use `types` instead. |
| 117 |
'headings' => '', |
| 118 |
'all_items_labels' => '', |
| 119 |
'class' => '', |
| 120 |
'post_types' => '', |
| 121 |
'hierarchical' => '', |
| 122 |
'hide_empty' => '', |
| 123 |
'order_by' => '', |
| 124 |
'show_count' => '', |
| 125 |
'order_dir' => '', |
| 126 |
'operators' => '', |
| 127 |
'add_search_param' => '0', |
| 128 |
'empty_search_url' => '', |
| 129 |
|
| 130 |
), |
| 131 |
$atts |
| 132 |
) |
| 133 |
); |
| 134 |
|
| 135 |
// Init `fields`. |
| 136 |
if ( $fields != null ) { |
| 137 |
$fields = explode( ',', $fields ); |
| 138 |
} else { |
| 139 |
$fields = explode( ',', $taxonomies ); |
| 140 |
} |
| 141 |
|
| 142 |
$this->taxonomylist = $fields; |
| 143 |
$nofields = count( $fields ); |
| 144 |
|
| 145 |
$add_search_param = (int) $add_search_param; |
| 146 |
|
| 147 |
// Init `submitlabel`. |
| 148 |
if ( $submitlabel !== null ) { |
| 149 |
// Then the old "submitlabel" has been supplied. |
| 150 |
if ( $submit_label === null ) { |
| 151 |
// Then the new label has not been supplied so do nothing/ |
| 152 |
$submit_label = $submitlabel; |
| 153 |
} |
| 154 |
} elseif ( $submitlabel === null ) { |
| 155 |
if ( $submit_label === null ) { |
| 156 |
$submit_label = 'Submit'; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
// Init `post_types`. |
| 161 |
if ( $post_types !== '' ) { |
| 162 |
$post_types = explode( ',', $post_types ); |
| 163 |
} else { |
| 164 |
if ( in_array( 'post_types', $fields, true ) ) { |
| 165 |
$post_types = array( 'all' ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// Init `hierarchical`. |
| 170 |
if ( $hierarchical != '' ) { |
| 171 |
$hierarchical = explode( ',', $hierarchical ); |
| 172 |
} else { |
| 173 |
$hierarchical = array( '' ); |
| 174 |
} |
| 175 |
|
| 176 |
// Init `hide_empty`. |
| 177 |
if ( $hide_empty != '' ) { |
| 178 |
$hide_empty = explode( ',', $hide_empty ); |
| 179 |
} else { |
| 180 |
$hide_empty = array( '' ); |
| 181 |
} |
| 182 |
|
| 183 |
// Init `show_count`. |
| 184 |
if ( $show_count !== '' ) { |
| 185 |
$show_count = explode( ',', $show_count ); |
| 186 |
} else { |
| 187 |
$show_count = array(); |
| 188 |
} |
| 189 |
|
| 190 |
// Init `order_by`. |
| 191 |
if ( $order_by !== '' ) { |
| 192 |
$order_by = explode( ',', $order_by ); |
| 193 |
} else { |
| 194 |
$order_by = array( '' ); |
| 195 |
} |
| 196 |
|
| 197 |
// Init `order_dir`. |
| 198 |
if ( $order_dir !== '' ) { |
| 199 |
$order_dir = explode( ',', $order_dir ); |
| 200 |
} else { |
| 201 |
$order_dir = array( '' ); |
| 202 |
} |
| 203 |
|
| 204 |
// Init `operators`. |
| 205 |
if ( $operators !== '' ) { |
| 206 |
$operators = explode( ',', $operators ); |
| 207 |
} else { |
| 208 |
$operators = array( '' ); |
| 209 |
} |
| 210 |
|
| 211 |
// Init `labels`. |
| 212 |
$labels = explode( ',', $headings ); |
| 213 |
|
| 214 |
if ( ! is_array( $labels ) ) { |
| 215 |
$labels = array(); |
| 216 |
} |
| 217 |
|
| 218 |
// Init `all_items_labels`. |
| 219 |
$all_items_labels = explode( ',', $all_items_labels ); |
| 220 |
|
| 221 |
if ( ! is_array( $all_items_labels ) ) { |
| 222 |
$all_items_labels = array(); |
| 223 |
} |
| 224 |
|
| 225 |
// Init `types`. |
| 226 |
if ( $types != null ) { |
| 227 |
$types = explode( ',', $types ); |
| 228 |
} else { |
| 229 |
$types = explode( ',', $type ); |
| 230 |
} |
| 231 |
|
| 232 |
if ( ! is_array( $types ) ) { |
| 233 |
$types = array(); |
| 234 |
} |
| 235 |
|
| 236 |
// Loop through Fields and set up default vars. |
| 237 |
for ( $i = 0; $i < $nofields; $i++ ) { |
| 238 |
// Set up types. |
| 239 |
if ( isset( $types[ $i ] ) ) { |
| 240 |
// Check for post date field. |
| 241 |
if ( $fields[ $i ] == 'post_date' ) { |
| 242 |
if ( ( $types[ $i ] != 'date' ) && ( $types[ $i ] != 'daterange' ) ) { |
| 243 |
// If not expected value use default. |
| 244 |
$types[ $i ] = 'date'; |
| 245 |
} |
| 246 |
} else { |
| 247 |
// Everything else can use a standard form input - checkbox/radio/dropdown/list/multiselect. |
| 248 |
if ( ( $types[ $i ] !== 'select' ) && ( $types[ $i ] !== 'checkbox' ) && ( $types[ $i ] !== 'radio' ) && ( $types[ $i ] !== 'list' ) && ( $types[ $i ] !== 'multiselect' ) ) { |
| 249 |
$types[ $i ] = 'select'; // Use default. |
| 250 |
} |
| 251 |
} |
| 252 |
} else { |
| 253 |
// Omitted, so set default. |
| 254 |
if ( $fields[ $i ] === 'post_date' ) { |
| 255 |
$types[ $i ] = 'date'; |
| 256 |
} else { |
| 257 |
$types[ $i ] = 'select'; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
// Setup labels. |
| 262 |
if ( ! isset( $labels[ $i ] ) ) { |
| 263 |
$labels[ $i ] = ''; |
| 264 |
} |
| 265 |
|
| 266 |
// Setup all_items_labels. |
| 267 |
if ( ! isset( $all_items_labels[ $i ] ) ) { |
| 268 |
$all_items_labels[ $i ] = ''; |
| 269 |
} |
| 270 |
|
| 271 |
if ( isset( $order_by[ $i ] ) ) { |
| 272 |
if ( ( $order_by[ $i ] !== 'id' ) && ( $order_by[ $i ] !== 'name' ) && ( $order_by[ $i ] !== 'slug' ) && ( $order_by[ $i ] !== 'count' ) && ( $order_by[ $i ] !== 'term_group' ) ) { |
| 273 |
$order_by[ $i ] = 'name'; // Use default - possible typo or use of unknown value. |
| 274 |
} |
| 275 |
} else { |
| 276 |
$order_by[ $i ] = 'name'; // Use default. |
| 277 |
} |
| 278 |
|
| 279 |
if ( isset( $order_dir[ $i ] ) ) { |
| 280 |
if ( ( $order_dir[ $i ] !== 'asc' ) && ( $order_dir[ $i ] !== 'desc' ) ) { |
| 281 |
// Then order_dir is not a wanted value, set to default. |
| 282 |
$order_dir[ $i ] = 'asc'; |
| 283 |
} |
| 284 |
} else { |
| 285 |
$order_dir[ $i ] = 'asc'; // Use default value. |
| 286 |
} |
| 287 |
|
| 288 |
if ( isset( $operators[ $i ] ) ) { |
| 289 |
$operators[ $i ] = strtolower( $operators[ $i ] ); |
| 290 |
|
| 291 |
if ( ( $operators[ $i ] !== 'and' ) && ( $operators[ $i ] !== 'or' ) ) { |
| 292 |
$operators[ $i ] = 'and'; // Else use default - possible typo or use of unknown value. |
| 293 |
} |
| 294 |
} else { |
| 295 |
$operators[ $i ] = 'and'; // Use default value. |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
// Set all form defaults / dropdowns etc. |
| 300 |
$this->set_defaults(); |
| 301 |
|
| 302 |
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 ); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
public function add_queryvars( $qvars ) { |
| 307 |
$qvars[] = 'post_types'; |
| 308 |
$qvars[] = 'post_date'; |
| 309 |
return $qvars; |
| 310 |
} |
| 311 |
|
| 312 |
public function filter_query_post_types( $query ) { |
| 313 |
global $wp_query; |
| 314 |
|
| 315 |
if ( ( $query->is_main_query() ) && ( ! is_admin() ) ) { |
| 316 |
if ( isset( $wp_query->query['post_types'] ) ) { |
| 317 |
$search_all = false; |
| 318 |
|
| 319 |
$post_types = explode( ',', esc_attr( $wp_query->query['post_types'] ) ); |
| 320 |
if ( isset( $post_types[0] ) ) { |
| 321 |
if ( count( $post_types ) == 1 ) { |
| 322 |
if ( $post_types[0] == 'all' ) { |
| 323 |
$search_all = true; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
if ( $search_all ) { |
| 328 |
$post_types = get_post_types( '', 'names' ); |
| 329 |
$query->set( 'post_type', $post_types ); // Set the post types that we want WP to search. |
| 330 |
} else { |
| 331 |
$query->set( 'post_type', $post_types ); |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return $query; |
| 337 |
} |
| 338 |
|
| 339 |
public function limit_date_range_query( $where ) { |
| 340 |
global $wp_query; |
| 341 |
|
| 342 |
// Get post dates into array. |
| 343 |
$post_date = explode( '+', esc_attr( str_replace( ' ', '+', $wp_query->query['post_date'] ) ) ); |
| 344 |
|
| 345 |
if ( count( $post_date ) > 1 && $post_date[0] !== $post_date[1] ) { |
| 346 |
$date_query = array(); |
| 347 |
$from_date = DateTime::createFromFormat( 'Y-m-d', $post_date[0] ); |
| 348 |
$to_date = DateTime::createFromFormat( 'Y-m-d', $post_date[1] ); |
| 349 |
|
| 350 |
if ( ! empty( $post_date[0] ) ) { |
| 351 |
$date_query['after'] = $from_date->format( 'Y-m-d 00:00:00' ); |
| 352 |
} |
| 353 |
|
| 354 |
if ( ! empty( $post_date[1] ) ) { |
| 355 |
$date_query['before'] = $to_date->format( 'Y-m-d 23:59:59' ); |
| 356 |
} |
| 357 |
|
| 358 |
$where .= " AND post_date >='" . $date_query['after'] . "' AND post_date <='" . $date_query['before'] . "'"; |
| 359 |
} |
| 360 |
|
| 361 |
return $where; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Remove the filter after it runs so that it doesn't affect any other |
| 366 |
* queries that might be performed on the same page (eg. Recent Posts |
| 367 |
* widget). |
| 368 |
*/ |
| 369 |
public function remove_limit_date_range_query() { |
| 370 |
remove_filter( 'posts_where', 'limit_date_range_query' ); |
| 371 |
} |
| 372 |
|
| 373 |
public function filter_query_post_date( $query ) { |
| 374 |
global $wp_query; |
| 375 |
if ( ( $query->is_main_query() ) && ( ! is_admin() ) ) { |
| 376 |
if ( isset( $wp_query->query['post_date'] ) ) { |
| 377 |
// Get post dates into array |
| 378 |
$post_date = explode( '+', esc_attr( str_replace( ' ', '+', $wp_query->query['post_date'] ) ) ); |
| 379 |
|
| 380 |
if ( ! empty( $post_date ) ) { |
| 381 |
// If there is more than 1 post date and the dates are not the same. |
| 382 |
if ( count( $post_date ) > 1 && $post_date[0] !== $post_date[1] ) { |
| 383 |
if ( ( ! empty( $post_date[0] ) ) && ( ! empty( $post_date[1] ) ) ) { |
| 384 |
// Attach hook to filter WHERE clause. |
| 385 |
add_filter( 'posts_where', array( $this, 'limit_date_range_query' ) ); |
| 386 |
// Remove the filter after it is executed. |
| 387 |
add_action( 'posts_selection', array( $this, 'remove_limit_date_range_query' ) ); |
| 388 |
} |
| 389 |
} else { |
| 390 |
// Else we are dealing with one date or both dates are the same (so need to find posts for a single day). |
| 391 |
if ( ! empty( $post_date[0] ) ) { |
| 392 |
$post_time = DateTime::createFromFormat( 'Y-m-d', $post_date[0] ); |
| 393 |
$query->set( 'year', $post_time->format( 'Y' ) ); |
| 394 |
$query->set( 'monthnum', $post_time->format( 'm' ) ); |
| 395 |
$query->set( 'day', $post_time->format( 'd' ) ); |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
return $query; |
| 402 |
} |
| 403 |
|
| 404 |
/* |
| 405 |
* check to set defaults - to be called after the shortcodes have been init so we can grab the wanted list of fields |
| 406 |
*/ |
| 407 |
public function set_defaults() { |
| 408 |
global $wp_query; |
| 409 |
|
| 410 |
$categories = array(); |
| 411 |
|
| 412 |
if ( isset( $wp_query->query['category_name'] ) ) { |
| 413 |
$category_params = ( preg_split( '/[,\+ ]/', esc_attr( $wp_query->query['category_name'] ) ) ); // explode with 2 delims |
| 414 |
|
| 415 |
foreach ( $category_params as $category_param ) { |
| 416 |
$category = get_category_by_slug( $category_param ); |
| 417 |
if ( isset( $category->cat_ID ) ) { |
| 418 |
$categories[] = $category->cat_ID; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
$this->defaults[ SF_FPRE . 'category' ] = $categories; |
| 424 |
|
| 425 |
// Grab search term for prefilling search input. |
| 426 |
if ( isset( $wp_query->query['s'] ) ) { |
| 427 |
$this->searchterm = trim( get_search_query() ); |
| 428 |
} |
| 429 |
|
| 430 |
// Check to see if tag is set. |
| 431 |
$tags = array(); |
| 432 |
|
| 433 |
if ( isset( $wp_query->query['tag'] ) ) { |
| 434 |
$tag_params = ( preg_split( '/[,\+ ]/', esc_attr( $wp_query->query['tag'] ) ) ); // Explode with 2 delims. |
| 435 |
|
| 436 |
foreach ( $tag_params as $tag_param ) { |
| 437 |
$tag = get_term_by( 'slug', $tag_param, 'post_tag' ); |
| 438 |
if ( isset( $tag->term_id ) ) { |
| 439 |
$tags[] = $tag->term_id; |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
$this->defaults[ SF_FPRE . 'post_tag' ] = $tags; |
| 445 |
|
| 446 |
if ( isset( $wp_query->query ) && is_array( $wp_query->query ) ) { |
| 447 |
// Loop through all the query vars. |
| 448 |
foreach ( $wp_query->query as $key => $val ) { |
| 449 |
// Make sure the get is not a reserved get as they have already been handled above. |
| 450 |
if ( ! in_array( SF_FPRE . $key, $this->frmqreserved, true ) ) { |
| 451 |
// Now check it is a desired key. |
| 452 |
if ( in_array( $key, $this->taxonomylist, true ) ) { |
| 453 |
$taxslug = $val; |
| 454 |
$tax_params = ( preg_split( '/[,\+ ]/', esc_attr( $taxslug ) ) ); |
| 455 |
|
| 456 |
$taxs = array(); |
| 457 |
foreach ( $tax_params as $tax_param ) { |
| 458 |
$tax = get_term_by( 'slug', $tax_param, $key ); |
| 459 |
|
| 460 |
if ( isset( $tax->term_id ) ) { |
| 461 |
$taxs[] = $tax->term_id; |
| 462 |
} |
| 463 |
} |
| 464 |
$this->defaults[ SF_FPRE . $key ] = $taxs; |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
$post_date = array( '', '' ); |
| 470 |
if ( isset( $wp_query->query['post_date'] ) ) { |
| 471 |
$post_date = explode( '+', esc_attr( str_replace( ' ', '+', $wp_query->query['post_date'] ) ) ); |
| 472 |
if ( count( $post_date ) === 1 ) { |
| 473 |
$post_date[1] = ''; |
| 474 |
} |
| 475 |
} |
| 476 |
$this->defaults[ SF_FPRE . 'post_date' ] = $post_date; |
| 477 |
|
| 478 |
$post_types = array(); |
| 479 |
if ( isset( $wp_query->query['post_types'] ) ) { |
| 480 |
$post_types = explode( ',', esc_attr( $wp_query->query['post_types'] ) ); |
| 481 |
} |
| 482 |
$this->defaults[ SF_FPRE . 'post_types' ] = $post_types; |
| 483 |
|
| 484 |
} |
| 485 |
|
| 486 |
/* |
| 487 |
* check to see if form has been submitted and handle vars |
| 488 |
*/ |
| 489 |
public function check_posts() { |
| 490 |
if ( ! isset( $_POST['_searchandfilter_nonce'] ) || ! wp_verify_nonce( $_POST['_searchandfilter_nonce'], 'searchandfilter_form' ) ) { |
| 491 |
return; // Exit early if nonce verification fails |
| 492 |
} |
| 493 |
|
| 494 |
if ( isset( $_POST[ SF_FPRE . 'submitted' ] ) ) { |
| 495 |
if ( $_POST[ SF_FPRE . 'submitted' ] === '1' ) { |
| 496 |
// Verify nonce for CSRF protection |
| 497 |
|
| 498 |
// Set var to confirm the form was posted. |
| 499 |
$this->has_form_posted = true; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
$taxcount = 0; |
| 504 |
|
| 505 |
/* Categories */ |
| 506 |
if ( ( isset( $_POST[ SF_FPRE . 'category' ] ) ) && ( $this->has_form_posted ) ) { |
| 507 |
|
| 508 |
$the_post_cat = wp_unslash( $_POST[ SF_FPRE . 'category' ] ); |
| 509 |
|
| 510 |
// Make the cat an array consistency. |
| 511 |
if ( ! is_array( $the_post_cat ) ) { |
| 512 |
$post_cat[] = $the_post_cat; |
| 513 |
} else { |
| 514 |
$post_cat = $the_post_cat; |
| 515 |
} |
| 516 |
$catarr = array(); |
| 517 |
|
| 518 |
foreach ( $post_cat as $cat ) { |
| 519 |
$cat = sanitize_text_field( $cat ); |
| 520 |
$catobj = get_category( $cat ); |
| 521 |
|
| 522 |
if ( isset( $catobj->slug ) ) { |
| 523 |
$catarr[] = $catobj->slug; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
if ( count( $catarr ) > 0 ) { |
| 528 |
$operator = '+'; // Default behaviour. |
| 529 |
|
| 530 |
// check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects |
| 531 |
if ( isset( $_POST[ SF_FPRE . 'category_operator' ] ) ) { |
| 532 |
$post_cat_operator = wp_unslash( $_POST[ SF_FPRE . 'category_operator' ] ); |
| 533 |
if ( strtolower( $post_cat_operator ) === 'and' ) { |
| 534 |
$operator = '+'; |
| 535 |
} elseif ( strtolower( $post_cat_operator ) === 'or' ) { |
| 536 |
$operator = ','; |
| 537 |
} else { |
| 538 |
$operator = '+'; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
$categories = implode( $operator, $catarr ); |
| 543 |
|
| 544 |
if ( get_option( 'permalink_structure' ) && ( $taxcount == 0 ) ) { |
| 545 |
$category_base = ( get_option( 'category_base' ) === '' ) ? 'category' : get_option( 'category_base' ); |
| 546 |
$category_path = $category_base . '/' . $categories . '/'; |
| 547 |
$this->urlparams .= $category_path; |
| 548 |
} else { |
| 549 |
if ( ! $this->hasqmark ) { |
| 550 |
$this->urlparams .= '?'; |
| 551 |
$this->hasqmark = true; |
| 552 |
} else { |
| 553 |
$this->urlparams .= '&'; |
| 554 |
} |
| 555 |
|
| 556 |
$this->urlparams .= 'category_name=' . $categories; |
| 557 |
} |
| 558 |
|
| 559 |
$taxcount++; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
/* Tags */ |
| 564 |
if ( ( isset( $_POST[ SF_FPRE . 'post_tag' ] ) ) && ( $this->has_form_posted ) ) { |
| 565 |
$the_post_tag = wp_unslash( $_POST[ SF_FPRE . 'post_tag' ] ); |
| 566 |
|
| 567 |
// Make the tag an array consistency. |
| 568 |
if ( ! is_array( $the_post_tag ) ) { |
| 569 |
$post_tag[] = $the_post_tag; |
| 570 |
} else { |
| 571 |
$post_tag = $the_post_tag; |
| 572 |
} |
| 573 |
|
| 574 |
$tagarr = array(); |
| 575 |
|
| 576 |
foreach ( $post_tag as $tag ) { |
| 577 |
$tag = sanitize_text_field( $tag ); |
| 578 |
$tagobj = get_tag( $tag ); |
| 579 |
|
| 580 |
if ( isset( $tagobj->slug ) ) { |
| 581 |
$tagarr[] = $tagobj->slug; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
if ( count( $tagarr ) > 0 ) { |
| 586 |
$operator = '+'; // Default behaviour. |
| 587 |
|
| 588 |
// Check to see if an operator has been specified - only applies with fields that use multiple selects such as checkboxes or multi selects |
| 589 |
if ( isset( $_POST[ SF_FPRE . 'post_tag_operator' ] ) ) { |
| 590 |
$post_tag_operator = wp_unslash( $_POST[ SF_FPRE . 'post_tag_operator' ] ); |
| 591 |
if ( strtolower( $post_tag_operator ) === 'and' ) { |
| 592 |
$operator = '+'; |
| 593 |
} elseif ( strtolower( $post_tag_operator ) === 'or' ) { |
| 594 |
$operator = ','; |
| 595 |
} else { |
| 596 |
$operator = '+'; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
$tags = implode( $operator, $tagarr ); |
| 601 |
|
| 602 |
if ( get_option( 'permalink_structure' ) && ( $taxcount === 0 ) ) { |
| 603 |
$tag_path = 'tag/' . $tags . '/'; |
| 604 |
$this->urlparams .= $tag_path; |
| 605 |
} else { |
| 606 |
if ( ! $this->hasqmark ) { |
| 607 |
$this->urlparams .= '?'; |
| 608 |
$this->hasqmark = true; |
| 609 |
} else { |
| 610 |
$this->urlparams .= '&'; |
| 611 |
} |
| 612 |
$this->urlparams .= 'tag=' . $tags; |
| 613 |
} |
| 614 |
|
| 615 |
$taxcount++; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
// Now we have dealt with the all the special case fields - search, tags, categories, post_types, post_date. |
| 620 |
|
| 621 |
// Loop through the posted data - double check that it is the search form that has been posted, otherwise we |
| 622 |
// could be looping through the posts submitted from an entirely unrelated form. |
| 623 |
if ( $this->has_form_posted ) { |
| 624 |
foreach ( $_POST as $key => $val ) { |
| 625 |
|
| 626 |
if ( ! in_array( $key, $this->frmreserved, true ) ) { |
| 627 |
// if the key is not in the reserved array (ie, on a custom taxonomy - not tags, categories, search term, post type & post date) |
| 628 |
|
| 629 |
// Strip off all prefixes for custom fields - we just want to do a redirect - no processing. |
| 630 |
if ( strpos( $key, SF_FPRE ) === 0 ) { |
| 631 |
$key = substr( $key, strlen( SF_FPRE ) ); |
| 632 |
} |
| 633 |
$prepped_val = wp_unslash( $val ); |
| 634 |
$val_arr = array(); |
| 635 |
if ( ! is_array( $prepped_val ) ) { |
| 636 |
$val_arr[] = $prepped_val; |
| 637 |
} else { |
| 638 |
$val_arr = $prepped_val; |
| 639 |
} |
| 640 |
|
| 641 |
$taxarr = array(); |
| 642 |
|
| 643 |
foreach ( $val_arr as $tax ) { |
| 644 |
$tax = sanitize_text_field( $tax ); |
| 645 |
$taxobj = get_term_by( 'id', $tax, $key ); |
| 646 |
|
| 647 |
if ( isset( $taxobj->slug ) ) { |
| 648 |
$taxarr[] = $taxobj->slug; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
if ( count( $taxarr ) > 0 ) { |
| 653 |
$operator = '+'; // Default behaviour. |
| 654 |
|
| 655 |
// Check to see if an operator has been specified - only applies with fields that use multiple |
| 656 |
// selects such as checkboxes or multi selects. |
| 657 |
if ( isset( $_POST[ SF_FPRE . $key . '_operator' ] ) ) { |
| 658 |
if ( strtolower( $_POST[ SF_FPRE . $key . '_operator' ] ) === 'and' ) { |
| 659 |
$operator = '+'; |
| 660 |
} elseif ( strtolower( $_POST[ SF_FPRE . $key . '_operator' ] ) === 'or' ) { |
| 661 |
$operator = ','; |
| 662 |
} else { |
| 663 |
$operator = '+'; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
$taxs = implode( $operator, $taxarr ); |
| 668 |
|
| 669 |
// *Due to some new wierd rewrite in WordPress, the first taxonomy which get rewritten |
| 670 |
// to /taxonomyname/taxonomyvalue only uses the first value of an array - so do it manually. |
| 671 |
if ( get_option( 'permalink_structure' ) && ( $taxcount === 0 ) ) { |
| 672 |
$key_taxonomy = get_taxonomy( $key ); |
| 673 |
|
| 674 |
$tax_path = $key . '/' . $taxs . '/'; |
| 675 |
if ( ( isset( $key_taxonomy->rewrite ) ) && ( isset( $key_taxonomy->rewrite['slug'] ) ) ) { |
| 676 |
$tax_path = $key_taxonomy->rewrite['slug'] . '/' . $taxs . '/'; |
| 677 |
} |
| 678 |
|
| 679 |
$this->urlparams .= $tax_path; |
| 680 |
} else { |
| 681 |
if ( ! $this->hasqmark ) { |
| 682 |
$this->urlparams .= '?'; |
| 683 |
$this->hasqmark = true; |
| 684 |
} else { |
| 685 |
$this->urlparams .= '&'; |
| 686 |
} |
| 687 |
$this->urlparams .= $key . '=' . $taxs; |
| 688 |
} |
| 689 |
$taxcount++; |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
/* Search input */ |
| 696 |
if ( ( isset( $_POST[ SF_FPRE . 'search' ] ) ) && ( $this->has_form_posted ) ) { |
| 697 |
$this->searchterm = trim( sanitize_text_field( wp_unslash( $_POST[ SF_FPRE . 'search' ] ) ) ); |
| 698 |
if ( $this->searchterm !== '' ) { |
| 699 |
if ( ! $this->hasqmark ) { |
| 700 |
$this->urlparams .= '?'; |
| 701 |
$this->hasqmark = true; |
| 702 |
} else { |
| 703 |
$this->urlparams .= '&'; |
| 704 |
} |
| 705 |
|
| 706 |
$this->urlparams .= 's=' . rawurlencode( $this->searchterm ); |
| 707 |
$this->hassearchquery = true; |
| 708 |
} |
| 709 |
} |
| 710 |
if ( ! $this->hassearchquery ) { |
| 711 |
if ( ( isset( $_POST[ SF_FPRE . 'add_search_param' ] ) ) && ( $this->has_form_posted ) ) { |
| 712 |
// This is only set when a search box is displayed - it tells S&F to append a blank search to the URL to indicate |
| 713 |
// a search has been submitted with no terms, however, still load the search template. |
| 714 |
if ( ! $this->hasqmark ) { |
| 715 |
$this->urlparams .= '?'; |
| 716 |
$this->hasqmark = true; |
| 717 |
} else { |
| 718 |
$this->urlparams .= '&'; |
| 719 |
} |
| 720 |
$this->urlparams .= 's='; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
/* Post types */ |
| 725 |
if ( ( isset( $_POST[ SF_FPRE . 'post_types' ] ) ) && ( $this->has_form_posted ) ) { |
| 726 |
$the_post_types = wp_unslash( $_POST[ SF_FPRE . 'post_types' ] ); |
| 727 |
|
| 728 |
// Make the post an array consistency. |
| 729 |
if ( ! is_array( $the_post_types ) ) { |
| 730 |
$post_types_arr[] = $the_post_types; |
| 731 |
} else { |
| 732 |
$post_types_arr = $the_post_types; |
| 733 |
} |
| 734 |
|
| 735 |
$num_post_types = count( $post_types_arr ); |
| 736 |
|
| 737 |
for ( $i = 0; $i < $num_post_types; $i++ ) { |
| 738 |
if ( $post_types_arr[ $i ] === '0' ) { |
| 739 |
$post_types_arr[ $i ] = 'all'; |
| 740 |
} else { |
| 741 |
$post_types_arr[ $i ] = sanitize_text_field( $post_types_arr[ $i ] ); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
if ( count( $post_types_arr ) > 0 ) { |
| 746 |
$operator = ','; // Default behaviour. |
| 747 |
$post_types = implode( $operator, $post_types_arr ); |
| 748 |
|
| 749 |
if ( ! $this->hasqmark ) { |
| 750 |
$this->urlparams .= '?'; |
| 751 |
$this->hasqmark = true; |
| 752 |
} else { |
| 753 |
$this->urlparams .= '&'; |
| 754 |
} |
| 755 |
$this->urlparams .= 'post_types=' . $post_types; |
| 756 |
|
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
/* Post date */ |
| 761 |
if ( ( isset( $_POST[ SF_FPRE . 'post_date' ] ) ) && ( $this->has_form_posted ) ) { |
| 762 |
$the_post_date = wp_unslash( $_POST[ SF_FPRE . 'post_date' ] ); |
| 763 |
|
| 764 |
// Make the post an array consistency. |
| 765 |
if ( ! is_array( $the_post_date ) ) { |
| 766 |
$post_date_arr[] = $the_post_date; |
| 767 |
} else { |
| 768 |
$post_date_arr = $the_post_date; |
| 769 |
} |
| 770 |
|
| 771 |
$num_post_date = count( $post_date_arr ); |
| 772 |
|
| 773 |
for ( $i = 0; $i < $num_post_date; $i++ ) { |
| 774 |
if ( $post_date_arr[ $i ] == '0' ) { |
| 775 |
$post_date_arr[ $i ] = 'all'; |
| 776 |
} else { |
| 777 |
$post_date_arr[ $i ] = sanitize_text_field( $post_date_arr[ $i ] ); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
if ( count( $post_date_arr ) > 0 ) { |
| 782 |
$post_date_count = count( $post_date_arr ); |
| 783 |
|
| 784 |
if ( $post_date_count == 2 ) { |
| 785 |
// See if there are 2 elements in arr (second date range selector). |
| 786 |
if ( ( $post_date_arr[0] !== '' ) && ( $post_date_arr[1] === '' ) ) { |
| 787 |
$post_date = $post_date_arr[0]; |
| 788 |
} elseif ( $post_date_arr[1] === '' ) { |
| 789 |
// If second date range is blank then remove the array element - this removes the addition of a '+' by implode below and only use first element. |
| 790 |
unset( $post_date_arr[1] ); |
| 791 |
} elseif ( $post_date_arr[0] === '' ) { |
| 792 |
$post_date = '+' . $post_date_arr[1]; |
| 793 |
} else { |
| 794 |
$post_date = implode( '+', array_filter( $post_date_arr ) ); |
| 795 |
} |
| 796 |
} else { |
| 797 |
$post_date = $post_date_arr[0]; |
| 798 |
} |
| 799 |
|
| 800 |
if ( isset( $post_date ) ) { |
| 801 |
if ( $post_date != '' ) { |
| 802 |
if ( ! $this->hasqmark ) { |
| 803 |
$this->urlparams .= '?'; |
| 804 |
$this->hasqmark = true; |
| 805 |
} else { |
| 806 |
$this->urlparams .= '&'; |
| 807 |
} |
| 808 |
$this->urlparams .= 'post_date=' . $post_date; |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
if ( $this->has_form_posted ) { |
| 815 |
// If the search has been posted, redirect to the newly formed url with all the right params. |
| 816 |
if ( $this->urlparams === '/' ) { |
| 817 |
// Check to see if url params are set, if not ("/") then add "?s=" to force load search results, |
| 818 |
// without this it would redirect to the homepage, which may be a custom page with no blog items/results. |
| 819 |
$this->urlparams .= '?s='; |
| 820 |
} |
| 821 |
|
| 822 |
if ( $this->urlparams == '/?s=' ) { |
| 823 |
// If a blank search was submitted - need to check for this string here in case `add_search_param` |
| 824 |
// has already added a "?s=" to the url. |
| 825 |
if ( isset( $_POST[ SF_FPRE . 'empty_search_url' ] ) ) { |
| 826 |
// Redirect to the provided empty search url. |
| 827 |
wp_redirect( esc_url( sanitize_text_field( wp_unslash( $_POST[ SF_FPRE . 'empty_search_url' ] ) ) ) ); |
| 828 |
exit; |
| 829 |
} |
| 830 |
} |
| 831 |
wp_safe_redirect( home_url() . $this->urlparams ); |
| 832 |
exit; |
| 833 |
} |
| 834 |
// phpcs:enable WordPress.Security.NonceVerification.Missing |
| 835 |
} |
| 836 |
|
| 837 |
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 ) { |
| 838 |
$returnvar = ''; |
| 839 |
|
| 840 |
$addclass = ''; |
| 841 |
if ( $class != '' ) { |
| 842 |
$addclass = ' ' . $class; |
| 843 |
} |
| 844 |
|
| 845 |
$returnvar .= ' |
| 846 |
<form action="" method="post" class="searchandfilter' . esc_attr( $addclass ) . '"> |
| 847 |
<div>'; |
| 848 |
|
| 849 |
if ( ! in_array( 'post_types', $fields, true ) ) { |
| 850 |
// Then the user has not added it to the fields list so the user does not want a post types |
| 851 |
// drop down... so add (if any) the post types to a hidden attribute. |
| 852 |
if ( ( $post_types !== '' ) && ( is_array( $post_types ) ) ) { |
| 853 |
foreach ( $post_types as $post_type ) { |
| 854 |
$returnvar .= '<input type="hidden" name="' . esc_attr( SF_FPRE ) . 'post_types[]" value="' . esc_attr( $post_type ) . '" />'; |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
$returnvar .= '<ul>'; |
| 859 |
$i = 0; |
| 860 |
|
| 861 |
foreach ( $fields as $field ) { |
| 862 |
// Special cases - post_types & post_date, all others assumed regular wp taxonomy. |
| 863 |
if ( $field === 'search' ) { |
| 864 |
$returnvar .= '<li>'; |
| 865 |
if ( $labels[ $i ] !== '' ) { |
| 866 |
$returnvar .= '<h4>' . wp_kses_post( $labels[ $i ] ) . '</h4>'; |
| 867 |
} |
| 868 |
$clean_searchterm = esc_attr( $this->searchterm ); |
| 869 |
$returnvar .= '<input type="text" name="' . esc_attr( SF_FPRE ) . 'search" placeholder="' . esc_attr( $search_placeholder ) . '" value="' . esc_attr( $clean_searchterm ) . '">'; |
| 870 |
$returnvar .= '</li>'; |
| 871 |
} elseif ( $field === 'post_types' ) { |
| 872 |
// Build field array. |
| 873 |
$returnvar .= $this->build_post_type_element( $types, $labels, $post_types, $field, $all_items_labels, $i ); |
| 874 |
} elseif ( $field === 'post_date' ) { |
| 875 |
$returnvar .= $this->build_post_date_element( $labels, $i, $types, $field ); |
| 876 |
} else { |
| 877 |
$returnvar .= $this->build_taxonomy_element( $types, $labels, $field, $hierarchical, $hide_empty, $show_count, $order_by, $order_dir, $operators, $all_items_labels, $i ); |
| 878 |
} |
| 879 |
$i++; |
| 880 |
|
| 881 |
} |
| 882 |
|
| 883 |
$returnvar .= '<li>'; |
| 884 |
|
| 885 |
if ( $add_search_param === 1 ) { |
| 886 |
$returnvar .= '<input type="hidden" name="' . esc_attr( SF_FPRE ) . 'add_search_param" value="1" />'; |
| 887 |
} |
| 888 |
|
| 889 |
if ( $empty_search_url !== '' ) { |
| 890 |
$returnvar .= '<input type="hidden" name="' . esc_attr( SF_FPRE ) . 'empty_search_url" value="' . esc_url( html_entity_decode( $empty_search_url ) ) . '" />'; |
| 891 |
} |
| 892 |
|
| 893 |
// Add nonce field for CSRF protection |
| 894 |
$returnvar .= wp_nonce_field( 'searchandfilter_form', '_searchandfilter_nonce', true, false ); |
| 895 |
|
| 896 |
$returnvar .= '<input type="hidden" name="' . esc_attr( SF_FPRE ) . 'submitted" value="1"><input type="submit" value="' . esc_attr( $submitlabel ) . '"></li>'; |
| 897 |
$returnvar .= '</ul>'; |
| 898 |
$returnvar .= '</div></form>'; |
| 899 |
|
| 900 |
return $returnvar; |
| 901 |
} |
| 902 |
|
| 903 |
public function build_post_date_element( $labels, $i, $types, $field ) { |
| 904 |
$returnvar = ''; |
| 905 |
|
| 906 |
$taxonomychildren = array(); |
| 907 |
|
| 908 |
$taxonomychildren = (object) $taxonomychildren; |
| 909 |
|
| 910 |
$returnvar .= '<li>'; |
| 911 |
|
| 912 |
if ( $labels[ $i ] !== '' ) { |
| 913 |
$returnvar .= '<h4>' . wp_kses_post( $labels[ $i ] ) . '</h4>'; |
| 914 |
} |
| 915 |
|
| 916 |
if ( $types[ $i ] === 'date' ) { |
| 917 |
$returnvar .= $this->generate_date( $taxonomychildren, $field, $this->tagid ); |
| 918 |
} |
| 919 |
if ( $types[ $i ] === 'daterange' ) { |
| 920 |
$returnvar .= $this->generate_date( $taxonomychildren, $field, 0 ); |
| 921 |
$returnvar .= '</li><li>'; |
| 922 |
$returnvar .= $this->generate_date( $taxonomychildren, $field, 1 ); |
| 923 |
} |
| 924 |
$returnvar .= '</li>'; |
| 925 |
|
| 926 |
return $returnvar; |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
public function build_post_type_element( $types, $labels, $post_types, $field, $all_items_labels, $i ) { |
| 931 |
$returnvar = ''; |
| 932 |
$taxonomychildren = array(); |
| 933 |
$post_type_count = count( $post_types ); |
| 934 |
|
| 935 |
// Then check the post types array. |
| 936 |
if ( is_array( $post_types ) ) { |
| 937 |
if ( ( $post_type_count === 1 ) && ( $post_types[0] === 'all' ) ) { |
| 938 |
$args = array( 'public' => true ); |
| 939 |
$output = 'object'; |
| 940 |
$operator = 'and'; |
| 941 |
|
| 942 |
$post_types_objs = get_post_types( $args, $output, $operator ); |
| 943 |
|
| 944 |
$post_types = array(); |
| 945 |
|
| 946 |
foreach ( $post_types_objs as $post_type ) { |
| 947 |
if ( $post_type->name !== 'attachment' ) { |
| 948 |
$tempobject = array(); |
| 949 |
$tempobject['term_id'] = $post_type->name; |
| 950 |
$tempobject['cat_name'] = $post_type->labels->name; |
| 951 |
|
| 952 |
$taxonomychildren[] = (object) $tempobject; |
| 953 |
|
| 954 |
$post_types[] = $post_type->name; |
| 955 |
|
| 956 |
} |
| 957 |
} |
| 958 |
$post_type_count = count( $post_types_objs ); |
| 959 |
|
| 960 |
} else { |
| 961 |
foreach ( $post_types as $post_type ) { |
| 962 |
$post_type_data = get_post_type_object( $post_type ); |
| 963 |
|
| 964 |
if ( $post_type_data ) { |
| 965 |
$tempobject = array(); |
| 966 |
$tempobject['term_id'] = $post_type; |
| 967 |
$tempobject['cat_name'] = $post_type_data->labels->name; |
| 968 |
|
| 969 |
$taxonomychildren[] = (object) $tempobject; |
| 970 |
} |
| 971 |
} |
| 972 |
} |
| 973 |
} |
| 974 |
$taxonomychildren = (object) $taxonomychildren; |
| 975 |
|
| 976 |
$returnvar .= '<li>'; |
| 977 |
|
| 978 |
$post_type_labels = array(); |
| 979 |
$post_type_labels['name'] = 'Post Types'; |
| 980 |
$post_type_labels['singular_name'] = 'Post Type'; |
| 981 |
$post_type_labels['search_items'] = 'Search Post Types'; |
| 982 |
|
| 983 |
if ( $all_items_labels[ $i ] !== '' ) { |
| 984 |
$post_type_labels['all_items'] = $all_items_labels[ $i ]; |
| 985 |
} else { |
| 986 |
$post_type_labels['all_items'] = 'All Post Types'; |
| 987 |
} |
| 988 |
|
| 989 |
$post_type_labels = (object) $post_type_labels; |
| 990 |
|
| 991 |
if ( $labels[ $i ] != '' ) { |
| 992 |
$returnvar .= '<h4>' . wp_kses_post( $labels[ $i ] ) . '</h4>'; |
| 993 |
} |
| 994 |
|
| 995 |
if ( $post_type_count > 0 ) { |
| 996 |
$defaultval = implode( ',', $post_types ); |
| 997 |
} else { |
| 998 |
$defaultval = 'all'; |
| 999 |
} |
| 1000 |
|
| 1001 |
if ( $types[ $i ] === 'select' ) { |
| 1002 |
$returnvar .= $this->generate_select( $taxonomychildren, $field, $this->tagid, $post_type_labels, $defaultval ); |
| 1003 |
} elseif ( $types[ $i ] === 'checkbox' ) { |
| 1004 |
$returnvar .= $this->generate_checkbox( $taxonomychildren, $field, $this->tagid ); |
| 1005 |
} elseif ( $types[ $i ] === 'radio' ) { |
| 1006 |
$returnvar .= $this->generate_radio( $taxonomychildren, $field, $this->tagid, $post_type_labels, $defaultval ); |
| 1007 |
} |
| 1008 |
$returnvar .= '</li>'; |
| 1009 |
|
| 1010 |
return $returnvar; |
| 1011 |
} |
| 1012 |
|
| 1013 |
// Gets all the data for the taxonomy then display as form element. |
| 1014 |
public function build_taxonomy_element( $types, $labels, $taxonomy, $hierarchical, $hide_empty, $show_count, $order_by, $order_dir, $operators, $all_items_labels, $i ) { |
| 1015 |
$returnvar = ''; |
| 1016 |
|
| 1017 |
$taxonomydata = get_taxonomy( $taxonomy ); |
| 1018 |
|
| 1019 |
if ( $taxonomydata ) { |
| 1020 |
$returnvar .= '<li>'; |
| 1021 |
|
| 1022 |
if ( $labels[ $i ] !== '' ) { |
| 1023 |
$returnvar .= '<h4>' . wp_kses_post( $labels[ $i ] ) . '</h4>'; |
| 1024 |
} |
| 1025 |
|
| 1026 |
$args = array( |
| 1027 |
'sf_name' => SF_FPRE . $taxonomy, |
| 1028 |
'taxonomy' => $taxonomy, |
| 1029 |
'hierarchical' => false, |
| 1030 |
'child_of' => 0, |
| 1031 |
'echo' => false, |
| 1032 |
'hide_if_empty' => false, |
| 1033 |
'hide_empty' => true, |
| 1034 |
'order' => $order_dir[ $i ], |
| 1035 |
'orderby' => $order_by[ $i ], |
| 1036 |
'show_option_none' => '', |
| 1037 |
'show_count' => '0', |
| 1038 |
'show_option_all' => '', |
| 1039 |
'show_option_all_sf' => '', |
| 1040 |
); |
| 1041 |
|
| 1042 |
if ( isset( $hierarchical[ $i ] ) ) { |
| 1043 |
if ( $hierarchical[ $i ] == 1 ) { |
| 1044 |
$args['hierarchical'] = true; |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
if ( isset( $hide_empty[ $i ] ) ) { |
| 1049 |
if ( $hide_empty[ $i ] == 0 ) { |
| 1050 |
$args['hide_empty'] = false; |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
if ( isset( $show_count[ $i ] ) ) { |
| 1055 |
if ( (int) $show_count[ $i ] === 1 ) { |
| 1056 |
$args['show_count'] = true; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
if ( $all_items_labels[ $i ] !== '' ) { |
| 1061 |
$args['show_option_all_sf'] = $all_items_labels[ $i ]; |
| 1062 |
} |
| 1063 |
|
| 1064 |
if ( $types[ $i ] === 'select' ) { |
| 1065 |
$returnvar .= $this->generate_wp_dropdown( $args, $taxonomy, $this->tagid, $taxonomydata->labels ); |
| 1066 |
} elseif ( $types[ $i ] === 'checkbox' ) { |
| 1067 |
$args['title_li'] = ''; |
| 1068 |
$args['defaults'] = ''; |
| 1069 |
if ( isset( $this->defaults[ $args['sf_name'] ] ) ) { |
| 1070 |
$args['defaults'] = $this->defaults[ $args['sf_name'] ]; |
| 1071 |
} |
| 1072 |
$returnvar .= $this->generate_wp_checkbox( $args, $taxonomy, $this->tagid, $taxonomydata->labels ); |
| 1073 |
} elseif ( $types[ $i ] === 'radio' ) { |
| 1074 |
$args['title_li'] = ''; |
| 1075 |
$args['defaults'] = ''; |
| 1076 |
|
| 1077 |
if ( isset( $this->defaults[ $args['sf_name'] ] ) ) { |
| 1078 |
$args['defaults'] = $this->defaults[ $args['sf_name'] ]; |
| 1079 |
} |
| 1080 |
$returnvar .= $this->generate_wp_radio( $args, $taxonomy, $this->tagid, $taxonomydata->labels ); |
| 1081 |
|
| 1082 |
} elseif ( $types[ $i ] === 'multiselect' ) { |
| 1083 |
$args['title_li'] = ''; |
| 1084 |
$args['defaults'] = ''; |
| 1085 |
|
| 1086 |
if ( isset( $this->defaults[ $args['sf_name'] ] ) ) { |
| 1087 |
$args['defaults'] = $this->defaults[ $args['sf_name'] ]; |
| 1088 |
} |
| 1089 |
|
| 1090 |
$returnvar .= $this->generate_wp_multiselect( $args, $taxonomy, $this->tagid, $taxonomydata->labels ); |
| 1091 |
} |
| 1092 |
|
| 1093 |
// Check to see if operator is set for this field. |
| 1094 |
if ( isset( $operators[ $i ] ) ) { |
| 1095 |
$operators[ $i ] = strtolower( $operators[ $i ] ); |
| 1096 |
if ( ( $operators[ $i ] === 'and' ) || ( $operators[ $i ] === 'or' ) ) { |
| 1097 |
$returnvar .= '<input type="hidden" name="' . esc_attr( SF_FPRE . $taxonomy ) . '_operator" value="' . esc_attr( $operators[ $i ] ) . '" />'; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
$returnvar .= '</li>'; |
| 1101 |
} |
| 1102 |
return $returnvar; |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
/* |
| 1107 |
* Display various forms |
| 1108 |
*/ |
| 1109 |
|
| 1110 |
// Use wp array walker to enable hierarchical display. |
| 1111 |
public function generate_wp_dropdown( $args, $name, $currentid = 0, $labels = null, $defaultval = '0' ) { |
| 1112 |
$args['name'] = $args['sf_name']; |
| 1113 |
|
| 1114 |
$returnvar = ''; |
| 1115 |
|
| 1116 |
if ( $args['show_option_all_sf'] == '' ) { |
| 1117 |
$args['show_option_all'] = $labels->all_items !== '' ? $labels->all_items : 'All ' . $labels->name; |
| 1118 |
} else { |
| 1119 |
$args['show_option_all'] = wp_kses_post( $args['show_option_all_sf'] ); |
| 1120 |
} |
| 1121 |
|
| 1122 |
if ( isset( $this->defaults[ SF_FPRE . $name ] ) ) { |
| 1123 |
$defaults = $this->defaults[ SF_FPRE . $name ]; |
| 1124 |
if ( is_array( $defaults ) ) { |
| 1125 |
if ( count( $defaults ) === 1 ) { |
| 1126 |
$args['selected'] = $defaults[0]; |
| 1127 |
} |
| 1128 |
} else { |
| 1129 |
$args['selected'] = $defaultval; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
|
| 1133 |
$returnvar .= wp_dropdown_categories( $args ); |
| 1134 |
|
| 1135 |
return $returnvar; |
| 1136 |
} |
| 1137 |
|
| 1138 |
// use wp array walker to enable hierarchical display |
| 1139 |
public function generate_wp_multiselect( $args, $name, $currentid = 0, $labels = null, $defaultval = '0' ) { |
| 1140 |
$returnvar = '<select multiple="multiple" name="' . esc_attr( $args['sf_name'] ) . '[]" class="postform">'; |
| 1141 |
$returnvar .= searchandfilter_walk_taxonomy( 'multiselect', $args ); |
| 1142 |
$returnvar .= '</select>'; |
| 1143 |
|
| 1144 |
return $returnvar; |
| 1145 |
} |
| 1146 |
|
| 1147 |
// use wp array walker to enable hierarchical display |
| 1148 |
public function generate_wp_checkbox( $args, $name, $currentid = 0, $labels = null, $defaultval = '0' ) { |
| 1149 |
$returnvar = '<ul>'; |
| 1150 |
$returnvar .= searchandfilter_walk_taxonomy( 'checkbox', $args ); |
| 1151 |
$returnvar .= '</ul>'; |
| 1152 |
|
| 1153 |
return $returnvar; |
| 1154 |
} |
| 1155 |
|
| 1156 |
// use wp array walker to enable hierarchical display |
| 1157 |
public function generate_wp_radio( $args, $name, $currentid = 0, $labels = null, $defaultval = '0' ) { |
| 1158 |
if ( $args['show_option_all_sf'] === '' ) { |
| 1159 |
$show_option_all = $labels->all_items !== '' ? $labels->all_items : 'All ' . $labels->name; |
| 1160 |
} else { |
| 1161 |
$show_option_all = $args['show_option_all_sf']; |
| 1162 |
} |
| 1163 |
|
| 1164 |
$checked = ( $defaultval === '0' ) ? " checked='checked'" : ''; |
| 1165 |
$returnvar = '<ul>'; |
| 1166 |
$returnvar .= '<li>' . "<label><input type='radio' name='" . esc_attr( $args['sf_name'] ) . "[]' value='0'$checked /> " . wp_kses_post( $show_option_all ) . '</label></li>'; |
| 1167 |
$returnvar .= searchandfilter_walk_taxonomy( 'radio', $args ); |
| 1168 |
$returnvar .= '</ul>'; |
| 1169 |
|
| 1170 |
return $returnvar; |
| 1171 |
} |
| 1172 |
|
| 1173 |
// Generate generic form inputs for use elsewhere, such as post types and non taxonomy fields. |
| 1174 |
public function generate_select( $dropdata, $name, $currentid = 0, $labels = null, $defaultval = '0' ) { |
| 1175 |
$returnvar = ''; |
| 1176 |
$returnvar .= '<select class="postform" name="' . esc_attr( SF_FPRE . $name ) . '">'; |
| 1177 |
if ( isset( $labels ) ) { |
| 1178 |
if ( $labels->all_items !== '' ) { |
| 1179 |
// Check to see if all items has been registered in field then use this label. |
| 1180 |
$returnvar .= '<option class="level-0" value="' . esc_attr( $defaultval ) . '">' . esc_html( $labels->all_items ) . '</option>'; |
| 1181 |
} else { |
| 1182 |
// Check to see if all items has been registered in field then use this label with prefix of "All". |
| 1183 |
$returnvar .= '<option class="level-0" value="' . esc_attr( $defaultval ) . '">All ' . esc_html( $labels->name ) . '</option>'; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
foreach ( $dropdata as $dropdown ) { |
| 1188 |
$selected = ''; |
| 1189 |
|
| 1190 |
if ( isset( $this->defaults[ SF_FPRE . $name ] ) ) { |
| 1191 |
$defaults = $this->defaults[ SF_FPRE . $name ]; |
| 1192 |
|
| 1193 |
$noselected = count( $defaults ); |
| 1194 |
|
| 1195 |
if ( ( $noselected === 1 ) && ( is_array( $defaults ) ) ) { |
| 1196 |
foreach ( $defaults as $defaultid ) { |
| 1197 |
// TODO - check to see if we can use strict comparison here. |
| 1198 |
if ( $defaultid == $dropdown->term_id ) { |
| 1199 |
$selected = ' selected="selected"'; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
} |
| 1203 |
} |
| 1204 |
$returnvar .= '<option class="level-0" value="' . esc_attr( $dropdown->term_id ) . '"' . $selected . '>' . esc_html( $dropdown->cat_name ) . '</option>'; |
| 1205 |
} |
| 1206 |
$returnvar .= '</select>'; |
| 1207 |
return $returnvar; |
| 1208 |
} |
| 1209 |
|
| 1210 |
public function generate_checkbox( $dropdata, $name, $currentid = 0, $labels = null, $defaultval = '' ) { |
| 1211 |
$returnvar = '<ul>'; |
| 1212 |
|
| 1213 |
foreach ( $dropdata as $dropdown ) { |
| 1214 |
$checked = ''; |
| 1215 |
|
| 1216 |
// Check a default has been set. |
| 1217 |
if ( isset( $this->defaults[ SF_FPRE . $name ] ) ) { |
| 1218 |
$defaults = $this->defaults[ SF_FPRE . $name ]; |
| 1219 |
$noselected = count( $defaults ); |
| 1220 |
if ( ( $noselected > 0 ) && ( is_array( $defaults ) ) ) { |
| 1221 |
foreach ( $defaults as $defaultid ) { |
| 1222 |
// TODO - test changing this to strict equals. |
| 1223 |
if ( $defaultid == $dropdown->term_id ) { |
| 1224 |
$checked = ' checked="checked"'; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
} |
| 1229 |
$returnvar .= '<li class="cat-item"><label><input class="postform cat-item" type="checkbox" name="' . esc_attr( SF_FPRE . $name ) . '[]" value="' . esc_attr( $dropdown->term_id ) . '"' . $checked . '> ' . esc_html( $dropdown->cat_name ) . '</label></li>'; |
| 1230 |
} |
| 1231 |
$returnvar .= '</ul>'; |
| 1232 |
return $returnvar; |
| 1233 |
} |
| 1234 |
|
| 1235 |
public function generate_radio( $dropdata, $name, $currentid = 0, $labels = null, $defaultval = '0' ) { |
| 1236 |
$returnvar = '<ul>'; |
| 1237 |
|
| 1238 |
if ( isset( $labels ) ) { |
| 1239 |
$checked = ''; |
| 1240 |
if ( isset( $this->defaults[ SF_FPRE . $name ] ) ) { |
| 1241 |
$defaults = $this->defaults[ SF_FPRE . $name ]; |
| 1242 |
$noselected = count( $defaults ); |
| 1243 |
|
| 1244 |
if ( $noselected === 0 ) { |
| 1245 |
$checked = ' checked="checked"'; |
| 1246 |
} elseif ( $noselected === 1 ) { |
| 1247 |
// TODO - test changing this to strict equals. |
| 1248 |
if ( $this->defaults[ SF_FPRE . $name ][0] == $defaultval ) { |
| 1249 |
$checked = ' checked="checked"'; |
| 1250 |
} |
| 1251 |
} |
| 1252 |
} else { |
| 1253 |
$checked = ' checked="checked"'; |
| 1254 |
} |
| 1255 |
|
| 1256 |
if ( isset( $this->defaults[ SF_FPRE . $name ] ) ) { |
| 1257 |
$defaults = $this->defaults[ SF_FPRE . $name ]; |
| 1258 |
if ( count( $defaults ) > 1 ) {// then we are dealing with multiple defaults - this means mutliple radios are selected, this is only possible with "ALL" so set as default. |
| 1259 |
$checked = ' checked="checked"'; |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
$all_items_name = ''; |
| 1264 |
// Check to see if all items has been registered in field then use this label. |
| 1265 |
if ( $labels->all_items !== '' ) { |
| 1266 |
$all_items_name = $labels->all_items; |
| 1267 |
} else { // check to see if all items has been registered in field then use this label with prefix of "All" |
| 1268 |
$all_items_name = 'All ' . $labels->name; |
| 1269 |
} |
| 1270 |
$returnvar .= '<li class="cat-item"><label><input class="postform" type="radio" name="' . esc_attr( SF_FPRE . $name ) . '[]" value="' . esc_attr( $defaultval ) . '"' . $checked . '> ' . esc_html( $all_items_name ) . '</label></li>'; |
| 1271 |
} |
| 1272 |
|
| 1273 |
foreach ( $dropdata as $dropdown ) { |
| 1274 |
$checked = ''; |
| 1275 |
|
| 1276 |
// Check a default has been set. |
| 1277 |
if ( isset( $this->defaults[ SF_FPRE . $name ] ) ) { |
| 1278 |
$defaults = $this->defaults[ SF_FPRE . $name ]; |
| 1279 |
$noselected = count( $defaults ); |
| 1280 |
|
| 1281 |
if ( ( $noselected === 1 ) && ( is_array( $defaults ) ) ) { |
| 1282 |
foreach ( $defaults as $defaultid ) { |
| 1283 |
// TODO - test changing this to strict equals. |
| 1284 |
if ( $defaultid == $dropdown->term_id ) { |
| 1285 |
$checked = ' checked="checked"'; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
} |
| 1290 |
$returnvar .= '<li class="cat-item"><label><input class="postform" type="radio" name="' . esc_attr( SF_FPRE . $name ) . '[]" value="' . esc_attr( $dropdown->term_id ) . '"' . $checked . '> ' . esc_html( $dropdown->cat_name ) . '</label></li>'; |
| 1291 |
|
| 1292 |
} |
| 1293 |
$returnvar .= '</ul>'; |
| 1294 |
return $returnvar; |
| 1295 |
} |
| 1296 |
|
| 1297 |
public function generate_date( $dropdata, $name, $currentid = 0, $labels = null, $defaultval = '0' ) { |
| 1298 |
$returnvar = ''; |
| 1299 |
$current_date = ''; |
| 1300 |
|
| 1301 |
// Check a default has been set - upto two possible vars for array. |
| 1302 |
if ( isset( $this->defaults[ SF_FPRE . $name ] ) ) { |
| 1303 |
$defaults = $this->defaults[ SF_FPRE . $name ]; |
| 1304 |
|
| 1305 |
$noselected = count( $defaults ); |
| 1306 |
|
| 1307 |
if ( ( $noselected > 0 ) && ( is_array( $defaults ) ) ) { |
| 1308 |
$current_date = $defaults[ $currentid ]; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
$returnvar .= '<input class="postform" type="date" name="' . esc_attr( SF_FPRE . $name ) . '[]" value="' . esc_attr( $current_date ) . '" />'; |
| 1313 |
|
| 1314 |
return $returnvar; |
| 1315 |
} |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
function searchandfilter_walk_taxonomy( $type = 'checkbox', $args = array() ) { |
| 1320 |
$args['walker'] = new SF_Taxonomy_Walker( $type, $args['sf_name'] ); |
| 1321 |
$output = wp_list_categories( $args ); |
| 1322 |
if ( $output ) { |
| 1323 |
return $output; |
| 1324 |
} |
| 1325 |
} |
| 1326 |
|
| 1327 |
if ( class_exists( 'SearchAndFilter' ) ) { |
| 1328 |
global $SearchAndFilter; |
| 1329 |
$SearchAndFilter = new SearchAndFilter(); |
| 1330 |
} |
| 1331 |
|
| 1332 |
// Classes. |
| 1333 |
require_once SEARCHANDFILTER_PLUGIN_DIR . '/of-list-table.php'; |
| 1334 |
require_once SEARCHANDFILTER_PLUGIN_DIR . '/of-taxonomy-walker.php'; |
| 1335 |
|
| 1336 |
// Admin screens & plugin mods. |
| 1337 |
require_once SEARCHANDFILTER_PLUGIN_DIR . '/of-admin.php'; |
| 1338 |
|