| 1 |
<?php |
| 2 |
|
| 3 |
namespace FilterEverything\Filter; |
| 4 |
|
| 5 |
if ( ! defined('ABSPATH') ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class FiltersWidget extends \WP_Widget |
| 10 |
{ |
| 11 |
public $show_instance_in_rest = false; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
parent::__construct( |
| 15 |
'wpc_filters_widget', // Base ID |
| 16 |
esc_html__( 'Filter Everything — Filters', 'filter-everything'), |
| 17 |
array( |
| 18 |
'description' => esc_html__( 'Displays filters', 'filter-everything' ), |
| 19 |
'show_instance_in_rest' => false, |
| 20 |
) |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Outputs the content for the current Filters widget instance. |
| 26 |
* @since 1.0.0 |
| 27 |
* @param array $args Display arguments including 'before_title', 'after_title', |
| 28 |
* 'before_widget', and 'after_widget'. |
| 29 |
* @param array $instance Settings for the current Filters widget instance. |
| 30 |
*/ |
| 31 |
public function widget( $args, $instance ) { |
| 32 |
|
| 33 |
if (current_user_can(flrt_plugin_user_caps())) { |
| 34 |
add_action('wp_footer', function () { |
| 35 |
?> |
| 36 |
<script> |
| 37 |
window.wpcFilterWidgetActive = true; |
| 38 |
</script> |
| 39 |
<?php |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
if ( is_array( $args ) ){ |
| 45 |
extract( $args ); |
| 46 |
} |
| 47 |
|
| 48 |
$title = isset( $instance['title'] ) ? $instance['title'] : ''; |
| 49 |
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
| 50 |
$show_selected_class = ( !empty( $instance['chips'] ) ) ? ' wpc-show-on-desktop' : ''; |
| 51 |
$show_count = ( !empty( $instance['show_count'] ) ) ? $instance['show_count'] : ''; |
| 52 |
$horizontal = ( !empty( $instance['horizontal'] ) ) ? $instance['horizontal'] : ''; |
| 53 |
$cols_count = ( isset( $instance['cols_count'] ) && $instance['cols_count'] > 0 ) ? $instance['cols_count'] : 3; |
| 54 |
|
| 55 |
$set_id = isset( $instance['id'] ) ? preg_replace('/[^\d]?/', '', $instance['id'] ) : 0; |
| 56 |
$popup_title = esc_html__('Filters', 'filter-everything'); |
| 57 |
|
| 58 |
if ( ! empty( $title ) ) { |
| 59 |
$popup_title = $title; |
| 60 |
} |
| 61 |
|
| 62 |
// Display nothing in Page Builders preview mode |
| 63 |
if( isset( $_GET['legacy-widget-preview'] ) || isset( $_GET['_locale'] ) ){ |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
if( isset( $_POST['action'] ) && $_POST['action'] === 'elementor_ajax' ){ |
| 68 |
echo '<strong>'.esc_html__( 'Filter Everything — Filters', 'filter-everything' ).'</strong>'; |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if( isset( $_GET['action'] ) && $_GET['action'] === 'elementor' ){ |
| 73 |
echo '<strong>'.esc_html__( 'Filter Everything — Filters', 'filter-everything' ).'</strong>'; |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Fires before current widget output |
| 79 |
*/ |
| 80 |
do_action( 'wpc_before_filters_widget', $args, $instance ); |
| 81 |
|
| 82 |
/** |
| 83 |
* @feature Add ability to choose what filter Set to display in widget settings |
| 84 |
*/ |
| 85 |
$debug_mode = flrt_is_debug_mode(); |
| 86 |
$container = Container::instance(); |
| 87 |
$wpManager = $container->getWpManager(); |
| 88 |
$fss = $container->getFilterSetService(); |
| 89 |
|
| 90 |
/** |
| 91 |
* Display debug messages in case if the current page has not Filter Set |
| 92 |
*/ |
| 93 |
if( empty( $wpManager->getQueryVar('wpc_page_related_set_ids') ) ){ |
| 94 |
$under_limit_filter_set = $fss->under_limit_filter_set($set_id); |
| 95 |
if( $debug_mode ){ |
| 96 |
$this->_debug_messages($under_limit_filter_set, $fss, $set_id); |
| 97 |
} |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
$templateManager = $container->getTemplateManager(); |
| 102 |
$em = $container->getEntityManager(); |
| 103 |
$urlManager = Container::instance()->getUrlManager(); |
| 104 |
|
| 105 |
$has_not_empty_children = []; |
| 106 |
$theSet = flrt_the_set( $set_id ); |
| 107 |
|
| 108 |
// Display or not Filter Set in dependency from the Apply button configuration |
| 109 |
if( isset( $theSet['show_on_the_page'] ) && ! $theSet['show_on_the_page'] ){ |
| 110 |
$theSet = flrt_the_set( $set_id ); |
| 111 |
} |
| 112 |
|
| 113 |
if( ! $theSet ){ |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
$setId = $theSet['ID']; |
| 118 |
$posType = $theSet['filtered_post_type']; |
| 119 |
$set = $fss->getSet( $theSet['ID'] ); |
| 120 |
|
| 121 |
$chipsObj = new Chips(true, array($setId) ); |
| 122 |
$chips = $chipsObj->getChips(); |
| 123 |
|
| 124 |
$set_edit_url = ( isset( $theSet['ID'] )) ? admin_url('post.php?post='.$theSet['ID'].'&action=edit') : admin_url( 'edit.php?post_type=filter-set' ); |
| 125 |
|
| 126 |
$related_filters = $em->getSetsRelatedFilters( array( $theSet ) ); |
| 127 |
$found_posts = flrt_posts_found_quantity( $setId, true ); |
| 128 |
$actionUrl = $urlManager->getFormActionOrFullPageUrl(true); |
| 129 |
$view_args = [ 'ask_to_select_parent' => false ]; |
| 130 |
|
| 131 |
// Apply button preparations |
| 132 |
$filters_counter = 0; |
| 133 |
$use_apply_button = ( isset( $set['use_apply_button']['value'] ) && $set['use_apply_button']['value'] === 'yes' ); |
| 134 |
// Instant (client-side) recount is a site-wide opt-in; without it Apply |
| 135 |
// button Sets keep the legacy per-click AJAX recount markup and behaviour. |
| 136 |
$instant_recount = $use_apply_button && flrt_instant_recount(); |
| 137 |
$view_args['use_apply_button'] = $instant_recount; |
| 138 |
$use_search_field = ( isset( $set['use_search_field']['value'] ) && $set['use_search_field']['value'] === 'yes' ); |
| 139 |
$apply_button_menu_order = isset( $set['apply_button_menu_order']['value'] ) ? (int) $set['apply_button_menu_order']['value'] : -1; |
| 140 |
$search_field_menu_order = isset( $set['search_field_menu_order']['value'] ) ? (int) $set['search_field_menu_order']['value'] : -1; |
| 141 |
|
| 142 |
$has_not_empty_children_flipped = []; |
| 143 |
$wrapper_class = ''; |
| 144 |
if ( flrt_is_filter_request() ) { |
| 145 |
$wrapper_class = ' wpc-filter-request'; |
| 146 |
} |
| 147 |
|
| 148 |
if (isset($set['horizontal_view_priority']['value']) && $set['horizontal_view_priority']['value'] === 'filter_set') { |
| 149 |
|
| 150 |
if (isset($set['horizontal_view']['value'])) { |
| 151 |
$horizontal = ( $set['horizontal_view']['value'] === 'yes') ? true : false; |
| 152 |
if ( $horizontal ) { |
| 153 |
$cols_count = ( isset( $set['horizontal_view_column']['value'] ) && $set['horizontal_view_column']['value'] > 0 ) ? $set['horizontal_view_column']['value'] : 3; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if ( $horizontal ) { |
| 159 |
$wrapper_class .= ' wpc-horizontal-layout'; |
| 160 |
$wrapper_class .= ' wpc-horizontal-cols-' . $cols_count; |
| 161 |
} |
| 162 |
|
| 163 |
if ( $use_apply_button ) { |
| 164 |
|
| 165 |
$base_permalink = ''; |
| 166 |
|
| 167 |
if ( defined('FLRT_FILTERS_PRO') && FLRT_FILTERS_PRO ) { |
| 168 |
$base_permalink = flrt_get_location_permalink( $set ); |
| 169 |
} |
| 170 |
|
| 171 |
$apply_url = $urlManager->getTermUrl( '', '', '', $base_permalink ); |
| 172 |
$reset_url = $urlManager->getResetUrl(); |
| 173 |
} |
| 174 |
|
| 175 |
do_action( 'wpc_before_display_filters_widget', $setId, $args, $instance ); |
| 176 |
|
| 177 |
if ( empty( $related_filters ) && ! $use_search_field ) { |
| 178 |
if ( $debug_mode ) { |
| 179 |
|
| 180 |
echo '<p class="wpc-debug-message">'; |
| 181 |
echo sprintf( |
| 182 |
wp_kses( |
| 183 |
__( 'There are no filters in the Filter Set yet. Please add them to the <a href="%s">Filter Set</a> related to this page.', 'filter-everything' ), |
| 184 |
array( 'a' => array('href' => true) ) |
| 185 |
), |
| 186 |
$set_edit_url |
| 187 |
); |
| 188 |
echo '</p>'; |
| 189 |
|
| 190 |
flrt_debug_title(); |
| 191 |
} |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
echo $before_widget; |
| 196 |
$filter_wrap_classes = apply_filters('wpc_filter_wrap_classes', array(), $setId); |
| 197 |
$class_attr = implode( ' ', array_map( 'esc_attr', $filter_wrap_classes ) ); |
| 198 |
|
| 199 |
echo '<div class="wpc-filters-main-wrap ' . $class_attr . ' wpc-filter-set-'.esc_attr( $setId ).$wrapper_class.'" data-wpc-hide-empty="'.esc_attr( $set['hide_empty']['value'] ).'" data-wpc-use-apply-button="'.esc_attr( $instant_recount ).'" data-set="'.esc_attr( $setId ).'">'."\n"; |
| 200 |
// Open/Closed status class |
| 201 |
$widgetContentClass = flrt_filters_widget_content_class($setId); |
| 202 |
$widgetContentClass .= ' wpc-show-counts-' . $set['show_count']['value']; |
| 203 |
|
| 204 |
if ( flrt_get_experimental_option('disable_buttons') !== 'on' ) { |
| 205 |
flrt_filters_button( $setId, $widgetContentClass ); |
| 206 |
} |
| 207 |
|
| 208 |
if ( $use_apply_button ) { |
| 209 |
$widgetContentClass .= ( $theSet['query_on_the_page'] ) ? ' wpc-query-on-the-page' : ' wpc-query-not-on-the-page'; |
| 210 |
} |
| 211 |
|
| 212 |
echo flrt_spinner_html(); |
| 213 |
|
| 214 |
echo '<div class="wpc-filters-widget-content'.esc_attr($widgetContentClass).'">'; |
| 215 |
echo '<div class="wpc-widget-close-container"> |
| 216 |
<a class="wpc-widget-close-icon"> |
| 217 |
<span class="wpc-icon-html-wrapper"> |
| 218 |
<span class="wpc-icon-line-1"></span><span class="wpc-icon-line-2"></span><span class="wpc-icon-line-3"></span> |
| 219 |
</span> |
| 220 |
</a>'; |
| 221 |
|
| 222 |
echo '<span class="wpc-widget-popup-title">'.$popup_title.'</span>'; |
| 223 |
echo '</div>'; |
| 224 |
echo '<div class="wpc-filters-widget-containers-wrapper">'."\r\n"; |
| 225 |
|
| 226 |
do_action( 'wpc_before_mobile_filters_widget', $setId, $args, $instance ); |
| 227 |
|
| 228 |
echo '<div class="wpc-filters-widget-top-container'.esc_attr($show_selected_class).'">'; |
| 229 |
echo '<div class="wpc-widget-top-inside">'; |
| 230 |
|
| 231 |
// Add selected terms for mobile widget |
| 232 |
echo '<div class="wpc-inner-widget-chips-wrapper">'; |
| 233 |
$templateManager->includeFrontView( 'chips', array( 'chips' => $chips, 'setid' => $setId, 'use_apply_button' => $instant_recount ) ); |
| 234 |
echo '</div>'; |
| 235 |
|
| 236 |
echo '</div>'; |
| 237 |
echo '</div>'; |
| 238 |
|
| 239 |
if ( ! empty( $title ) ) { |
| 240 |
echo '<div class="wpc-filter-set-widget-title">'."\n"; |
| 241 |
echo $before_title . $title . $after_title; |
| 242 |
echo '</div>'."\n"; |
| 243 |
} |
| 244 |
|
| 245 |
echo '<div class="wpc-filters-scroll-container">'; |
| 246 |
echo '<div class="wpc-filters-widget-wrapper">'."\r\n"; |
| 247 |
|
| 248 |
if( $show_count ){ |
| 249 |
flrt_posts_found( $setId ); |
| 250 |
} else { |
| 251 |
flrt_posts_found( $setId, false, true ); |
| 252 |
} |
| 253 |
|
| 254 |
$to = count( $related_filters ); |
| 255 |
if ( $use_apply_button ) { |
| 256 |
$to += 2; |
| 257 |
} |
| 258 |
if ( $use_search_field ) { |
| 259 |
$to += 2; |
| 260 |
} |
| 261 |
|
| 262 |
// Add Search field and Apply Button to the array if they enabled |
| 263 |
//@todo if Filter Set in Free version contains filter for PRO counters are incorrect (-1 from filters count) |
| 264 |
// and that's why Apply button or Search field on the last position(s) may not appear |
| 265 |
$filters_and_fields = []; |
| 266 |
$related_filters = array_reverse( $related_filters, true ); |
| 267 |
$search_field_displayed = false; |
| 268 |
$apply_button_displayed = false; |
| 269 |
|
| 270 |
for ( $i = 1; $i <= $to; $i++ ) { |
| 271 |
if ( $use_apply_button && $apply_button_menu_order === $i ) { |
| 272 |
$filters_and_fields[-1] = array(); |
| 273 |
} else if ( $use_search_field && $search_field_menu_order === $i ) { |
| 274 |
$filters_and_fields[-2] = array(); |
| 275 |
} else { |
| 276 |
// To avoid "empty" filter |
| 277 |
if ( ! empty( $related_filters ) ) { |
| 278 |
$key = array_key_last($related_filters); |
| 279 |
$value = array_pop($related_filters); |
| 280 |
$filters_and_fields[$key] = $value; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
//removes child terms if parent filter is empty |
| 287 |
foreach ($filters_and_fields as $filter_id => $filter) { |
| 288 |
if ($filter_id > 0) { |
| 289 |
$terms = flrt_get_filter_terms($filter, $posType, $em); |
| 290 |
if (flrtParentFilter($filter)) { |
| 291 |
$parent_filter_id = (int)$filter['parent_filter']; |
| 292 |
// The parent filter can be absent from the rendered set (deleted |
| 293 |
// or does not belong to the post type anymore) — do not create |
| 294 |
// a stub without entity/e_name for it |
| 295 |
if (!isset($filters_and_fields[$parent_filter_id])) { |
| 296 |
continue; |
| 297 |
} |
| 298 |
if (!isset($filters_and_fields[$parent_filter_id]['child_values']) || !is_array($filters_and_fields[$parent_filter_id]['child_values'])) { |
| 299 |
$filters_and_fields[$parent_filter_id]['child_values'] = []; |
| 300 |
} |
| 301 |
$filters_and_fields[$parent_filter_id]['has_child_filter'] = true; |
| 302 |
foreach ($terms as $single_term) { |
| 303 |
$filters_and_fields[$parent_filter_id]['child_values'][] = $single_term->slug; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
foreach ($filters_and_fields as $filter_id => $filter) { |
| 311 |
$filters_counter++; |
| 312 |
|
| 313 |
$is_rating = (isset($filter['view']) && $filter['view'] === 'rating' && $filter['e_name'] === 'product_visibility' ) ? true : false; |
| 314 |
if ( $filter_id > 0 ) { |
| 315 |
/** |
| 316 |
* Allows the developer to modify a filter before output. |
| 317 |
*/ |
| 318 |
$filter = apply_filters('wpc_filter_options_before_display', $filter, $set); |
| 319 |
|
| 320 |
$terms = flrt_get_filter_terms( $filter, $posType, $em ); |
| 321 |
|
| 322 |
// Collect terms for a parent filter, if exists |
| 323 |
if ( flrtParentFilter( $filter ) ) { |
| 324 |
// Here we have to calculate all related with the parent filter |
| 325 |
$parent_filter_id = (int)$filter['parent_filter']; |
| 326 |
if(!$instant_recount){ |
| 327 |
if ( isset( $filters_and_fields[$parent_filter_id] ) ) { |
| 328 |
$parent_filter = $filters_and_fields[$parent_filter_id]; |
| 329 |
|
| 330 |
$parent_filter_terms = flrt_get_filter_terms( $parent_filter, $posType, $em ); |
| 331 |
$wp_queried_term = flrt_get_wp_queried_term( $parent_filter_terms ); |
| 332 |
|
| 333 |
if ( $wp_queried_term ) { |
| 334 |
$parent_filter['values'][] = $wp_queried_term->slug; |
| 335 |
} |
| 336 |
|
| 337 |
if ( empty( $parent_filter['values'] ) ) { |
| 338 |
|
| 339 |
// Do not show this filter, until parent is selected |
| 340 |
if ( $filter['hide_until_parent'] === 'yes' && empty( $filter['values'] ) ) { |
| 341 |
continue; |
| 342 |
} |
| 343 |
|
| 344 |
if ( ! empty( $filter['values'] ) ) { |
| 345 |
$actual_filter_terms = []; |
| 346 |
$filter_values_flipped = array_flip( $filter['values'] ); |
| 347 |
foreach ( $terms as $single_term ) { |
| 348 |
if(!$is_rating) { |
| 349 |
if (isset($filter_values_flipped[$single_term->slug])) { |
| 350 |
$actual_filter_terms[] = $single_term; |
| 351 |
} |
| 352 |
}else{ |
| 353 |
$actual_filter_terms[] = $single_term; |
| 354 |
} |
| 355 |
} |
| 356 |
$terms = $actual_filter_terms; |
| 357 |
} else { |
| 358 |
if( isset( $parent_filter['label'] ) && $parent_filter['label'] ) { |
| 359 |
$view_args['ask_to_select_parent'] = sprintf( esc_html__('Select %s first', 'filter-everything'), $parent_filter['label'] ); |
| 360 |
} else { |
| 361 |
$view_args['ask_to_select_parent'] = esc_html__('First, select another filter', 'filter-everything'); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
// Here we have to setup signal that forces message "Select parent first" |
| 366 |
} else { |
| 367 |
if ( ! in_array( $filter['entity'], ['post_meta_num', 'tax_numeric'] ) ) { |
| 368 |
// Selected values are term slugs |
| 369 |
$actual_parent_filter_posts = []; |
| 370 |
$parent_filter_values_flipped = array_flip( $parent_filter['values'] ); |
| 371 |
|
| 372 |
foreach ( $parent_filter_terms as $parent_filter_term ) { |
| 373 |
if ( isset( $parent_filter_values_flipped[$parent_filter_term->slug] ) ) { |
| 374 |
$actual_parent_filter_posts = array_merge($actual_parent_filter_posts, $parent_filter_term->posts); |
| 375 |
} |
| 376 |
} |
| 377 |
$actual_filter_terms = []; |
| 378 |
// if ! empty( $filter['values'] ) |
| 379 |
foreach ($terms as $single_term) { |
| 380 |
if(!$is_rating){ |
| 381 |
$current_intersection = array_intersect($actual_parent_filter_posts, $single_term->posts); |
| 382 |
if (!empty($current_intersection)) { |
| 383 |
$actual_filter_terms[] = $single_term; |
| 384 |
} |
| 385 |
}else{ |
| 386 |
$actual_filter_terms[] = $single_term; |
| 387 |
} |
| 388 |
} |
| 389 |
$terms = $actual_filter_terms; |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if($instant_recount){ |
| 396 |
if ( isset( $filters_and_fields[$parent_filter_id] ) ) { |
| 397 |
$parent_filter = $filters_and_fields[$parent_filter_id]; |
| 398 |
|
| 399 |
$parent_filter_terms = flrt_get_filter_terms( $parent_filter, $posType, $em ); |
| 400 |
$wp_queried_term = flrt_get_wp_queried_term( $parent_filter_terms ); |
| 401 |
|
| 402 |
if( isset( $parent_filter['label'] ) && $parent_filter['label'] ) { |
| 403 |
$view_args['ask_to_select_parent'] = sprintf( esc_html__('Select %s first', 'filter-everything'), $parent_filter['label'] ); |
| 404 |
} else { |
| 405 |
$view_args['ask_to_select_parent'] = esc_html__('First, select another filter', 'filter-everything'); |
| 406 |
} |
| 407 |
|
| 408 |
if ( $wp_queried_term ) { |
| 409 |
$parent_filter['values'][] = $wp_queried_term->slug; |
| 410 |
} |
| 411 |
|
| 412 |
if ( empty( $parent_filter['values'] ) ) { |
| 413 |
|
| 414 |
if ( ! empty( $filter['values'] ) ) { |
| 415 |
$actual_filter_terms = []; |
| 416 |
$filter_values_flipped = array_flip( $filter['values'] ); |
| 417 |
foreach ( $terms as $single_term ) { |
| 418 |
if(!$is_rating) { |
| 419 |
if (isset($filter_values_flipped[$single_term->slug])) { |
| 420 |
$actual_filter_terms[] = $single_term; |
| 421 |
} |
| 422 |
}else{ |
| 423 |
$actual_filter_terms[] = $single_term; |
| 424 |
} |
| 425 |
} |
| 426 |
$terms = $actual_filter_terms; |
| 427 |
}else{ |
| 428 |
$view_args['empty_parent_filter'] = true; |
| 429 |
} |
| 430 |
// Here we have to setup signal that forces message "Select parent first" |
| 431 |
} else { |
| 432 |
if ( ! in_array( $filter['entity'], ['post_meta_num', 'tax_numeric'] ) ) { |
| 433 |
// Selected values are term slugs |
| 434 |
$actual_parent_filter_posts = []; |
| 435 |
$parent_filter_values_flipped = array_flip( $parent_filter['values'] ); |
| 436 |
|
| 437 |
foreach ( $parent_filter_terms as $parent_filter_term ) { |
| 438 |
if ( isset( $parent_filter_values_flipped[$parent_filter_term->slug] ) ) { |
| 439 |
$actual_parent_filter_posts = array_merge($actual_parent_filter_posts, $parent_filter_term->posts); |
| 440 |
} |
| 441 |
} |
| 442 |
$actual_filter_terms = []; |
| 443 |
foreach ($terms as $single_term) { |
| 444 |
$single_term->show_with_parent = false; |
| 445 |
if(!$is_rating){ |
| 446 |
$current_intersection = array_intersect($actual_parent_filter_posts, $single_term->posts); |
| 447 |
if (!empty($current_intersection)) { |
| 448 |
$single_term->show_with_parent = true; |
| 449 |
} |
| 450 |
$actual_filter_terms[] = $single_term; |
| 451 |
}else{ |
| 452 |
$actual_filter_terms[] = $single_term; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
$terms = $actual_filter_terms; |
| 457 |
} |
| 458 |
if ( in_array( $filter['entity'], ['post_meta_num', 'tax_numeric'] ) ) { |
| 459 |
$actual_filter_terms = []; |
| 460 |
foreach ($terms as $single_term) { |
| 461 |
$single_term->show_with_parent = false; |
| 462 |
$actual_filter_terms[] = $single_term; |
| 463 |
} |
| 464 |
$terms = $actual_filter_terms; |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
} else { |
| 470 |
$view_args['ask_to_select_parent'] = false; |
| 471 |
} |
| 472 |
|
| 473 |
if ( $filter['hierarchy'] === 'yes' ) { |
| 474 |
$hierarchy_key = 'cross_count'; |
| 475 |
if ($set['hide_empty']['value'] === 'initial') { |
| 476 |
$hierarchy_key = 'count'; |
| 477 |
} |
| 478 |
|
| 479 |
$has_not_empty_children = flrt_get_parents_with_not_empty_children($terms, $hierarchy_key); |
| 480 |
$has_not_empty_children_flipped = array_flip($has_not_empty_children); |
| 481 |
} |
| 482 |
|
| 483 |
// Create a list with excluded empty terms |
| 484 |
if (($set['hide_empty']['value'] === 'yes') || ($set['hide_empty']['value'] === 'initial') || (isset($set['hide_empty_filter']) && $set['hide_empty_filter']['value'] === 'yes')) { |
| 485 |
$allWpQueriedPostIds = $em->getAllSetWpQueriedPostIds($setId); |
| 486 |
$allWpQueriedPostIds_flipped = array_flip($allWpQueriedPostIds); |
| 487 |
$checkTerms = $terms; |
| 488 |
|
| 489 |
if(!$is_rating) { |
| 490 |
if ($set['hide_empty']['value'] === 'initial') { |
| 491 |
foreach ($checkTerms as $index => $term) { |
| 492 |
if ($filter['hierarchy'] === 'yes') { |
| 493 |
|
| 494 |
$intersection = false; |
| 495 |
foreach ($term->posts as $post_id) { |
| 496 |
if (isset($allWpQueriedPostIds_flipped[$post_id])) { |
| 497 |
$intersection = true; |
| 498 |
break; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
if (!$intersection && !isset($has_not_empty_children_flipped[$term->term_id])) { |
| 503 |
unset($checkTerms[$index]); |
| 504 |
} |
| 505 |
|
| 506 |
} else { |
| 507 |
|
| 508 |
$intersection = false; |
| 509 |
foreach ($term->posts as $post_id) { |
| 510 |
if (isset($allWpQueriedPostIds_flipped[$post_id])) { |
| 511 |
$intersection = true; |
| 512 |
break; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
if (!$intersection) { |
| 517 |
unset($checkTerms[$index]); |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
} else { |
| 522 |
$checkTerms = flrt_remove_empty_terms($terms, $filter, $has_not_empty_children_flipped, $instant_recount); |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
// Remove empty terms, if such option is enabled |
| 528 |
if ( |
| 529 |
($set['hide_empty']['value'] === 'yes' || $set['hide_empty']['value'] === 'initial') |
| 530 |
&& |
| 531 |
! in_array( $filter['entity'], ['post_meta_num', 'tax_numeric', 'post_date', 'post_meta_date'] ) |
| 532 |
) { |
| 533 |
$terms = $checkTerms; |
| 534 |
} |
| 535 |
/** |
| 536 |
* @todo we have to check this for Dates and consider if we need to hide |
| 537 |
* filter by dates at all |
| 538 |
*/ |
| 539 |
// Hide entire Filter if there are no posts in its terms |
| 540 |
if ( isset( $set['hide_empty_filter'] ) |
| 541 |
&& |
| 542 |
$set['hide_empty_filter']['value'] === 'yes') { |
| 543 |
|
| 544 |
if ( in_array( $filter['entity'], ['post_meta_num', 'tax_numeric'] ) ) { |
| 545 |
// Temporary not ideal solution |
| 546 |
// Sometimes it is $terms[0] sometimes $terms['max'] |
| 547 |
if(!$instant_recount) { |
| 548 |
if (isset($terms[0])) { |
| 549 |
if ((int)$terms[0]->max === 0 && (int)$terms[1]->min === 0) { |
| 550 |
// Huh, finally |
| 551 |
continue; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
if (isset($terms['min'])) { |
| 556 |
if ((int)$terms['max']->max === 0 && (int)$terms['min']->min === 0) { |
| 557 |
// Huh, finally |
| 558 |
continue; |
| 559 |
} |
| 560 |
} |
| 561 |
}else{ |
| 562 |
if (isset($terms[0])) { |
| 563 |
if ((int)$terms[0]->max === 0 && (int)$terms[1]->min === 0) { |
| 564 |
$terms = []; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
if (isset($terms['min'])) { |
| 569 |
if ((int)$terms['max']->max === 0 && (int)$terms['min']->min === 0) { |
| 570 |
$terms = []; |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
} else if ( in_array( $filter['entity'], ['post_date', 'post_meta_date'] ) ) { |
| 576 |
if ($found_posts < 1) { |
| 577 |
// Huh, finally |
| 578 |
continue; |
| 579 |
} |
| 580 |
} else { |
| 581 |
$checkTerms = flrt_remove_empty_terms($terms, $filter, $has_not_empty_children_flipped); |
| 582 |
if(!$instant_recount){ |
| 583 |
if (empty($checkTerms)) { |
| 584 |
// Huh, finally |
| 585 |
continue; |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
/** |
| 591 |
* Extract only needed values without extra fields |
| 592 |
*/ |
| 593 |
$terms = flrt_extract_objects_vars( $terms, array( |
| 594 |
'term_id', |
| 595 |
'slug', |
| 596 |
'name', |
| 597 |
'count', |
| 598 |
'cross_count', |
| 599 |
'range_list_input', |
| 600 |
'abs_values', |
| 601 |
'max', |
| 602 |
'min', |
| 603 |
'from', |
| 604 |
'to', |
| 605 |
'time_to', |
| 606 |
'time_from', |
| 607 |
'parent', |
| 608 |
'wp_queried', |
| 609 |
'show_with_parent' |
| 610 |
) |
| 611 |
); |
| 612 |
|
| 613 |
// Hook terms before display to allow developers modify them. |
| 614 |
$terms = apply_filters('wpc_terms_before_display', $terms, $filter, $set, $urlManager); |
| 615 |
$templateManager->includeFrontView( |
| 616 |
/** |
| 617 |
* Allows you to include your own filters template |
| 618 |
*/ |
| 619 |
apply_filters('wpc_view_include_filename', $filter['view'], $filter, $set), |
| 620 |
|
| 621 |
array( |
| 622 |
'filter' => $filter, |
| 623 |
'terms' => $terms, |
| 624 |
'set' => $set, |
| 625 |
'url_manager' => $urlManager, |
| 626 |
'view_args' => $view_args |
| 627 |
) |
| 628 |
); |
| 629 |
|
| 630 |
} else { |
| 631 |
|
| 632 |
// Show Search Field if enabled |
| 633 |
if ( $use_search_field && $search_field_menu_order === $filters_counter) { |
| 634 |
$templateManager->includeFrontView( 'search', array( 'set' => $set, 'url_manager' => $urlManager, 'wp_manager' => $wpManager ) ); |
| 635 |
$search_field_displayed = true; |
| 636 |
} |
| 637 |
|
| 638 |
// Show Apply button if configured |
| 639 |
if ( $use_apply_button && $apply_button_menu_order === $filters_counter) { |
| 640 |
$templateManager->includeFrontView('apply-button', array('set' => $set, 'apply_url' => $apply_url, 'apply_base_url' => $base_permalink, 'reset_url' => $reset_url, 'found_posts' => $found_posts, 'is_filter_request' => flrt_is_filter_request() )); |
| 641 |
$apply_button_displayed = true; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
} // end $related_filters foreach |
| 646 |
|
| 647 |
if ( $use_search_field && $search_field_displayed === false ) { |
| 648 |
$templateManager->includeFrontView( 'search', array( 'set' => $set, 'url_manager' => $urlManager, 'wp_manager' => $wpManager ) ); |
| 649 |
} |
| 650 |
|
| 651 |
if ( $use_apply_button && $apply_button_displayed === false ) { |
| 652 |
$templateManager->includeFrontView('apply-button', array('set' => $set, 'apply_url' => $apply_url, 'apply_base_url' => $base_permalink, 'reset_url' => $reset_url, 'found_posts' => $found_posts, 'is_filter_request' => flrt_is_filter_request() )); |
| 653 |
} |
| 654 |
|
| 655 |
echo '</div>'."\r\n"; |
| 656 |
echo '</div>' . "\r\n"; |
| 657 |
echo '<div class="wpc-filters-widget-controls-container"> |
| 658 |
<div class="wpc-filters-widget-controls-wrapper">'; |
| 659 |
|
| 660 |
$templateManager->includeFrontView( 'bottom-controls', array( 'action_url' => $actionUrl, 'found_posts' => $found_posts ) ); |
| 661 |
|
| 662 |
echo ' |
| 663 |
</div>'; |
| 664 |
|
| 665 |
if( $use_apply_button ){ |
| 666 |
$templateManager->includeFrontView( 'apply-button', array( 'set' => $set, 'apply_url' => $apply_url, 'apply_base_url' => $base_permalink, 'reset_url' => $reset_url, 'found_posts' => $found_posts, 'is_filter_request' => flrt_is_filter_request() ) ); |
| 667 |
} |
| 668 |
|
| 669 |
echo '</div>'; |
| 670 |
|
| 671 |
if( current_user_can( flrt_plugin_user_caps() ) ){ |
| 672 |
echo '<div class="wpc-edit-filter-set">'; |
| 673 |
echo sprintf( |
| 674 |
wp_kses( |
| 675 |
__( '<a href="%s">Edit</a> Filter Set', 'filter-everything' ), |
| 676 |
array( 'a' => array('href' => true) ) |
| 677 |
), |
| 678 |
$set_edit_url |
| 679 |
); |
| 680 |
echo '</div>'; |
| 681 |
} |
| 682 |
|
| 683 |
do_action( 'wpc_after_mobile_filters_widget', $setId, $args, $instance ); |
| 684 |
|
| 685 |
echo '</div>' . "\r\n"; |
| 686 |
echo '</div>' . "\r\n"; // end .wpc-filters-widget-content |
| 687 |
echo '</div>'."\n"; // <!-- wpc-filters-main-wrap --> |
| 688 |
echo $after_widget; |
| 689 |
|
| 690 |
do_action( 'wpc_after_filters_widget', $args, $instance ); |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* Outputs the settings form for the Filters widget. |
| 695 |
* @since 1.0.0 |
| 696 |
* @param array $instance Current settings. |
| 697 |
*/ |
| 698 |
public function form( $instance ) { |
| 699 |
|
| 700 |
$title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : ''; |
| 701 |
$show_count = isset( $instance['show_count'] ) ? (bool) $instance['show_count'] : true; |
| 702 |
$chips = isset( $instance['chips'] ) ? (bool) $instance['chips'] : false; |
| 703 |
$horizontal = isset( $instance['horizontal'] ) ? (bool) $instance['horizontal'] : false; |
| 704 |
$cols_count = isset( $instance['cols_count'] ) ? $instance['cols_count'] : 3; |
| 705 |
|
| 706 |
?> |
| 707 |
<p> |
| 708 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:' ); ?></label> |
| 709 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 710 |
</p> |
| 711 |
<p> |
| 712 |
<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>"<?php checked( $show_count ); ?> /> |
| 713 |
<label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php esc_html_e( 'Show the number of posts found', 'filter-everything' ); ?></label> |
| 714 |
</p> |
| 715 |
<p> |
| 716 |
<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'chips' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'chips' ) ); ?>"<?php checked( $chips ); ?> /> |
| 717 |
<label for="<?php echo esc_attr( $this->get_field_id( 'chips' ) ); ?>"><?php esc_html_e( 'Show selected terms (Chips)', 'filter-everything' ); ?></label> |
| 718 |
</p> |
| 719 |
<p> |
| 720 |
<input type="checkbox" class="checkbox wpc-horizontal-checkbox" id="<?php echo esc_attr( $this->get_field_id( 'horizontal' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'horizontal' ) ); ?>"<?php checked( $horizontal ); ?> disabled="disabled"/> |
| 721 |
<label for="<?php echo esc_attr( $this->get_field_id( 'horizontal' ) ); ?>" style="color:#b7b7b7;"><?php esc_html_e( 'Horizontal filters', 'filter-everything' ); ?>.</label> |
| 722 |
<span class="wpc-columns-wrapper" style="display: none"> |
| 723 |
<label for="<?php echo $this->get_field_id( 'cols_count' ); ?>"><?php _e( 'Columns', 'filter-everything' ); ?>:</label> |
| 724 |
<select id="<?php echo $this->get_field_id( 'cols_count' ); ?>" name="<?php echo $this->get_field_name( 'cols_count' ); ?>" style="display: none" disabled="disabled"> |
| 725 |
<?php for ( $i = 2; $i <= 5; $i++ ) : ?> |
| 726 |
<option value="<?php echo esc_attr( $i ); ?>" <?php selected( $cols_count, $i ); ?>> |
| 727 |
<?php echo esc_html( $i ); ?> |
| 728 |
</option> |
| 729 |
<?php endfor; ?> |
| 730 |
</select> |
| 731 |
</span> |
| 732 |
<br> |
| 733 |
<span>(<?php esc_html_e('This setting has moved to the Filter Set', 'filter-everything'); ?>)</span> |
| 734 |
</p> |
| 735 |
<?php |
| 736 |
} |
| 737 |
|
| 738 |
/** |
| 739 |
* Handles updating settings for the current Filters widget instance. |
| 740 |
* @since 1.0.0 |
| 741 |
* @param array $new_instance New settings for this instance as input by the user via |
| 742 |
* WP_Widget::form(). |
| 743 |
* @param array $old_instance Old settings for this instance. |
| 744 |
* @return array Updated settings to save. |
| 745 |
*/ |
| 746 |
public function update( $new_instance, $old_instance ) { |
| 747 |
$instance = []; |
| 748 |
$instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 749 |
$instance['chips'] = ( !empty( $new_instance['chips'] ) ) ? 1 : 0; |
| 750 |
$instance['show_count'] = ( !empty( $new_instance['show_count'] ) ) ? 1 : 0; |
| 751 |
$instance['horizontal'] = ( !empty( $new_instance['horizontal'] ) ) ? 1 : 0; |
| 752 |
$instance['cols_count'] = ( isset( $new_instance['cols_count'] ) && $new_instance['cols_count'] > 0 ) ? $new_instance['cols_count'] : 3; |
| 753 |
|
| 754 |
return $instance; |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* Outputs Filters widget debug messages |
| 759 |
* @since 1.2.2 |
| 760 |
*/ |
| 761 |
private function _debug_messages($under_limit_filter_set = false, $fss = null, $set_id = 0){ |
| 762 |
if ( defined('FLRT_FILTERS_PRO') ) { |
| 763 |
echo '<p class="wpc-debug-message">'; |
| 764 |
echo sprintf( |
| 765 |
wp_kses( |
| 766 |
__( 'No one Filter Set is related to this page. You can configure it in the <a href="%s">Filter Set</a> -> "Where to filter?" field.', 'filter-everything' ), |
| 767 |
array( 'a' => array('href' => true) ) |
| 768 |
), |
| 769 |
admin_url( 'edit.php?post_type=filter-set' ) |
| 770 |
); |
| 771 |
echo '</p>'; |
| 772 |
} else { |
| 773 |
if ( is_singular() ) { |
| 774 |
$limit = $fss ? $fss->getFreeLimitForPostType( get_post_type() ) : FilterSet::FREE_LIMIT_NEW; |
| 775 |
echo '<p class="wpc-debug-message">'; |
| 776 |
echo sprintf( |
| 777 |
wp_kses( |
| 778 |
/* translators: 1: maximum number of free filter sets per post type, 2: PRO upgrade URL */ |
| 779 |
__( 'The free version allows up to %1$d filter sets per post type. Upgrade to <a href="%2$s">PRO</a> to display more filter sets.', 'filter-everything' ), |
| 780 |
array( 'a' => array('href' => true) ) |
| 781 |
), |
| 782 |
$limit, |
| 783 |
esc_url(FLRT_PLUGIN_URL .'/pricing/?utm_source=free_plugin&utm_medium=internal&utm_campaign=free_plugin_upgrade&utm_content=msg_debug_three') |
| 784 |
); |
| 785 |
echo '</p>'; |
| 786 |
} else if ( $under_limit_filter_set ) { |
| 787 |
$post_type_field = $fss ? $fss->getPostTypeField($set_id) : []; |
| 788 |
$post_type = !empty($post_type_field['post_type']['value']) ? $post_type_field['post_type']['value'] : ''; |
| 789 |
$limit = $fss ? $fss->getFreeLimitForPostType($post_type) : FilterSet::FREE_LIMIT_NEW; |
| 790 |
echo '<p class="wpc-debug-message">'; |
| 791 |
echo sprintf( |
| 792 |
wp_kses( |
| 793 |
/* translators: 1: maximum number of free filter sets per post type, 2: PRO upgrade URL */ |
| 794 |
__( 'The free version allows up to %1$d filter sets per post type. Upgrade to <a href="%2$s">PRO</a> to display more filter sets.', 'filter-everything' ), |
| 795 |
array( 'a' => array('href' => true) ) |
| 796 |
), |
| 797 |
$limit, |
| 798 |
esc_url(FLRT_PLUGIN_URL .'/pricing/?utm_source=free_plugin&utm_medium=internal&utm_campaign=free_plugin_upgrade&utm_content=msg_debug_three') |
| 799 |
); |
| 800 |
echo '</p>'; |
| 801 |
} else { |
| 802 |
echo '<p class="wpc-debug-message">'; |
| 803 |
echo sprintf( |
| 804 |
wp_kses( |
| 805 |
__( 'No one Filter Set is configured for this post type pages. You can create a new <a href="%s">Filter Set</a> for them.', 'filter-everything' ), |
| 806 |
array( 'a' => array('href' => true) ) |
| 807 |
), |
| 808 |
admin_url( 'edit.php?post_type=filter-set' ) |
| 809 |
); |
| 810 |
echo '</p>'; |
| 811 |
} |
| 812 |
} |
| 813 |
} |
| 814 |
} |