| 1 |
<?php |
| 2 |
|
| 3 |
namespace FilterEverything\Filter; |
| 4 |
|
| 5 |
if ( ! defined('WPINC') ) { |
| 6 |
wp_die(); |
| 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__( 'Filters Widget', 'filter-everything' ), |
| 19 |
'show_instance_in_rest' => false, |
| 20 |
) |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Front-end display of widget. |
| 26 |
* |
| 27 |
* @see WP_Widget::widget() |
| 28 |
* |
| 29 |
* @param array $args Widget arguments. |
| 30 |
* @param array $instance Saved values from database. |
| 31 |
*/ |
| 32 |
public function widget( $args, $instance ) { |
| 33 |
extract( $args ); |
| 34 |
$title = isset( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : ''; |
| 35 |
$show_selected_class = ( !empty( $instance['chips'] ) ) ? ' wpc-show-on-desktop' : ''; |
| 36 |
$show_count = ( !empty( $instance['show_count'] ) ) ? $instance['show_count'] : ''; |
| 37 |
$set_id = isset( $instance['id'] ) ? preg_replace('/[^\d]?/', '', $instance['id'] ) : 0; |
| 38 |
|
| 39 |
// Display nothing if preview mode |
| 40 |
if( isset( $_GET['legacy-widget-preview'] ) || isset( $_GET['_locale'] ) ){ |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if( isset( $_POST['action'] ) && $_POST['action'] === 'elementor_ajax' ){ |
| 45 |
echo '<strong>'.esc_html__( 'Filters Widget', 'filter-everything' ).'</strong>'; |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @feature Add ability to choose what filter Set to display in widget settings |
| 51 |
*/ |
| 52 |
$debug_mode = flrt_is_debug_mode(); |
| 53 |
$container = Container::instance(); |
| 54 |
$wpManager = $container->getWpManager(); |
| 55 |
|
| 56 |
if( ! $wpManager->getQueryVar( 'allowed_filter_page' ) ){ |
| 57 |
if( $debug_mode ){ |
| 58 |
|
| 59 |
if( defined('FLRT_FILTERS_PRO') ){ |
| 60 |
echo '<p class="wpc-debug-message">'; |
| 61 |
echo sprintf( |
| 62 |
wp_kses( |
| 63 |
__( 'No one Filter Set is related with this page. You can configure it in the <a href="%s">Filter Set</a> location field.', 'filter-everything' ), |
| 64 |
array( 'a' => array('href' => true) ) |
| 65 |
), |
| 66 |
admin_url( 'edit.php?post_type=filter-set' ) |
| 67 |
); |
| 68 |
echo '</p>'; |
| 69 |
}else{ |
| 70 |
if( is_singular() ){ |
| 71 |
echo '<p class="wpc-debug-message">'; |
| 72 |
echo sprintf( |
| 73 |
wp_kses( |
| 74 |
__( 'The free version of the plugin does not support filtering on singular pages. But <a href="%s">PRO version</a> supports.', 'filter-everything' ), |
| 75 |
array( 'a' => array('href' => true) ) |
| 76 |
), |
| 77 |
esc_url(FLRT_PLUGIN_LINK .'/?get_pro=true') |
| 78 |
); |
| 79 |
echo '</p>'; |
| 80 |
}else{ |
| 81 |
echo '<p class="wpc-debug-message">'; |
| 82 |
echo sprintf( |
| 83 |
wp_kses( |
| 84 |
__( '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' ), |
| 85 |
array( 'a' => array('href' => true) ) |
| 86 |
), |
| 87 |
admin_url( 'edit.php?post_type=filter-set' ) |
| 88 |
); |
| 89 |
echo '</p>'; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
flrt_debug_title(); |
| 94 |
} |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
$templateManager = $container->getTemplateManager(); |
| 99 |
$em = $container->getEntityManager(); |
| 100 |
$fss = $container->getFilterSetService(); |
| 101 |
$urlManager = new UrlManager(); |
| 102 |
|
| 103 |
$has_not_empty_children = []; |
| 104 |
$theSet = flrt_the_set( $set_id ); |
| 105 |
|
| 106 |
if( ! $theSet ){ |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
$setId = $theSet['ID']; |
| 111 |
$posType = $theSet['filtered_post_type']; |
| 112 |
$set = $fss->getSet( $theSet['ID'] ); |
| 113 |
$chipsObj = new Chips(true, array($setId) ); |
| 114 |
$chips = $chipsObj->getChips(); |
| 115 |
$set_edit_url = ( isset( $theSet['ID'] )) ? admin_url('post.php?post='.$theSet['ID'].'&action=edit') : admin_url( 'edit.php?post_type=filter-set' ); |
| 116 |
|
| 117 |
$related_filters = $em->getSetsRelatedFilters( array( $theSet ) ); |
| 118 |
$found_posts = flrt_posts_found_quantity( $setId, true ); |
| 119 |
$actionUrl = $urlManager->getFormActionUrl(true); |
| 120 |
|
| 121 |
if( empty( $related_filters ) ){ |
| 122 |
if( $debug_mode ){ |
| 123 |
|
| 124 |
echo '<p class="wpc-debug-message">'; |
| 125 |
echo sprintf( |
| 126 |
wp_kses( |
| 127 |
__( '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' ), |
| 128 |
array( 'a' => array('href' => true) ) |
| 129 |
), |
| 130 |
$set_edit_url |
| 131 |
); |
| 132 |
echo '</p>'; |
| 133 |
|
| 134 |
flrt_debug_title(); |
| 135 |
} |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
echo $before_widget; |
| 140 |
echo '<div class="wpc-filters-widget-main-wrapper wpc-filter-set-'.esc_attr( $setId ).'" data-set="'.esc_attr( $setId ).'">'."\n"; |
| 141 |
// Open/Closed status class |
| 142 |
$widgetContentClass = flrt_filters_widget_content_class($setId); |
| 143 |
if( flrt_get_experimental_option('disable_buttons') !== 'on' ) { |
| 144 |
flrt_filters_button($setId, $widgetContentClass); |
| 145 |
} |
| 146 |
|
| 147 |
echo '<div class="wpc-filters-widget-content'.esc_attr($widgetContentClass).'">'; |
| 148 |
echo '<div class="wpc-filters-widget-controls-container"> |
| 149 |
<div class="wpc-filters-widget-controls-wrapper"> |
| 150 |
<div class="wpc-filters-widget-controls-item wpc-filters-widget-controls-one"> |
| 151 |
<a class="wpc-filters-close-button" href="'.esc_url($actionUrl).'">' . esc_html__('Cancel', 'filter-everything') . '</a> |
| 152 |
</div> |
| 153 |
<div class="wpc-filters-widget-controls-item wpc-filters-widget-controls-two"> |
| 154 |
<a class="wpc-filters-apply-button wpc-posts-loaded" href="'.esc_url($actionUrl).'">' . wp_kses( sprintf( __('Show %s', 'filter-everything'), '<span class="wpc-filters-found-posts-wrapper">(</span><span class="wpc-filters-found-posts">'.esc_html($found_posts).'</span>)</span>'), |
| 155 |
array( 'span' => array('class'=>true) ) |
| 156 |
). '</a> |
| 157 |
</div> |
| 158 |
</div> |
| 159 |
</div>'; |
| 160 |
|
| 161 |
if ( ! empty( $title ) ) { |
| 162 |
echo '<div class="wpc-filter-set-widget-title">'."\n"; |
| 163 |
echo $before_title . $title . $after_title; |
| 164 |
echo '</div>'."\n"; |
| 165 |
} |
| 166 |
|
| 167 |
echo flrt_spinner_html(); |
| 168 |
|
| 169 |
echo '<div class="wpc-filters-scroll-container">'; |
| 170 |
|
| 171 |
echo '<div class="wpc-filters-widget-wrapper">'."\r\n"; |
| 172 |
|
| 173 |
if( $show_count ){ |
| 174 |
flrt_posts_found( $setId ); |
| 175 |
} |
| 176 |
|
| 177 |
// Add selected terms for mobile widget |
| 178 |
echo '<div class="wpc-inner-widget-chips-wrapper'.esc_attr($show_selected_class).'">'; |
| 179 |
$templateManager->includeFrontView( 'chips', array( 'chips' => $chips, 'setid' => $setId ) ); |
| 180 |
echo '</div>'; |
| 181 |
|
| 182 |
foreach ( $related_filters as $filter ){ |
| 183 |
|
| 184 |
$entityObj = $em->getEntityByFilter( $filter, $posType ); |
| 185 |
$entityObj->setExcludedTerms( $filter['exclude'] ); |
| 186 |
|
| 187 |
$terms = $entityObj->getTerms(); |
| 188 |
|
| 189 |
if( $filter['hierarchy'] === 'yes' ){ |
| 190 |
$has_not_empty_children = flrt_get_parents_with_not_empty_children( $terms, 'count' ); |
| 191 |
} |
| 192 |
|
| 193 |
// Create a list with excluded empty terms |
| 194 |
if( ( $set['hide_empty']['value'] === 'yes' ) || ( isset( $set['hide_empty_filter'] ) && $set['hide_empty_filter']['value'] === 'yes' ) ){ |
| 195 |
$allWpQueriedPostIds = $em->getAllSetWpQueriedPostIds( $setId ); |
| 196 |
|
| 197 |
$checkTerms = $terms; |
| 198 |
|
| 199 |
foreach ($checkTerms as $index => $term) { |
| 200 |
if( $filter['hierarchy'] === 'yes' ){ |
| 201 |
|
| 202 |
if( empty( array_intersect( $allWpQueriedPostIds, $term->posts ) ) |
| 203 |
&& ! in_array( $term->term_id, $has_not_empty_children ) ){ |
| 204 |
unset($checkTerms[$index]); |
| 205 |
} |
| 206 |
|
| 207 |
}else{ |
| 208 |
if( empty( array_intersect( $allWpQueriedPostIds, $term->posts ) ) ){ |
| 209 |
unset($checkTerms[$index]); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
// Remove empty terms, if such option is enabled |
| 218 |
if( $set['hide_empty']['value'] === 'yes' && ($filter['entity'] !== 'post_meta_num') ) { |
| 219 |
$terms = $checkTerms; |
| 220 |
} |
| 221 |
|
| 222 |
// Hide Filter if there are no posts in its terms |
| 223 |
if( isset( $set['hide_empty_filter'] ) |
| 224 |
&& |
| 225 |
$set['hide_empty_filter']['value'] === 'yes' ){ |
| 226 |
|
| 227 |
if( $filter['entity'] === 'post_meta_num' ){ |
| 228 |
// Temporary not ideal solution |
| 229 |
if( $terms[0]->max === NULL && $terms[1]->min === NULL ){ |
| 230 |
// Huh, finally |
| 231 |
continue; |
| 232 |
} |
| 233 |
}else{ |
| 234 |
if( empty( $checkTerms ) ){ |
| 235 |
// Huh, finally |
| 236 |
continue; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
$terms = flrt_extract_objects_vars( $terms, array( |
| 242 |
'term_id', |
| 243 |
'slug', |
| 244 |
'name', |
| 245 |
'count', |
| 246 |
'cross_count', |
| 247 |
'max', |
| 248 |
'min', |
| 249 |
'absMax', |
| 250 |
'absMin', |
| 251 |
'parent') |
| 252 |
); |
| 253 |
|
| 254 |
// Hook terms before display to allow developers modify them. |
| 255 |
$terms = apply_filters( 'wpc_terms_before_display', $terms, $filter, $set, $urlManager ); |
| 256 |
|
| 257 |
$templateManager->includeFrontView( |
| 258 |
apply_filters( 'wpc_view_include_filename', $filter['view'], $filter ), |
| 259 |
array( |
| 260 |
'filter' => $filter, |
| 261 |
'terms' => $terms, |
| 262 |
'set' => $set, |
| 263 |
'url_manager' => $urlManager |
| 264 |
) |
| 265 |
); |
| 266 |
} |
| 267 |
|
| 268 |
echo '</div>'."\r\n"; |
| 269 |
|
| 270 |
echo '</div>' . "\r\n"; |
| 271 |
|
| 272 |
if( current_user_can( 'manage_options' ) ){ |
| 273 |
echo '<div class="wpc-edit-filter-set">'; |
| 274 |
echo sprintf( |
| 275 |
wp_kses( |
| 276 |
__( '<a href="%s">Edit</a> Filter Set', 'filter-everything' ), |
| 277 |
array( 'a' => array('href' => true) ) |
| 278 |
), |
| 279 |
$set_edit_url |
| 280 |
); |
| 281 |
echo '</div>'; |
| 282 |
} |
| 283 |
|
| 284 |
echo '</div>' . "\r\n"; |
| 285 |
|
| 286 |
// Show button, that opens bottom filters container |
| 287 |
$wpc_mobile_width = flrt_get_mobile_width(); |
| 288 |
echo '<style type="text/css"> |
| 289 |
@media screen and (max-width: '.$wpc_mobile_width.'px) { |
| 290 |
.wpc_show_bottom_widget .wpc-filters-widget-controls-container, |
| 291 |
.wpc_show_open_close_button .wpc-filters-open-button-container, |
| 292 |
.wpc_show_bottom_widget .wpc-filters-open-button-container{ |
| 293 |
display: block; |
| 294 |
} |
| 295 |
} |
| 296 |
</style>'."\r\n"; |
| 297 |
echo '</div>'."\n"; // <!-- wpc-filters-widget-main-wrapper --> |
| 298 |
echo $after_widget; |
| 299 |
} |
| 300 |
|
| 301 |
public function form( $instance ) { |
| 302 |
|
| 303 |
$title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : ''; |
| 304 |
$chips = isset( $instance['chips'] ) ? (bool) $instance['chips'] : false; |
| 305 |
$show_count = isset( $instance['show_count'] ) ? (bool) $instance['show_count'] : true; |
| 306 |
|
| 307 |
?> |
| 308 |
<p> |
| 309 |
<label for="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"><?php esc_html_e( 'Title:' ); ?></label> |
| 310 |
<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 ); ?>" /> |
| 311 |
</p> |
| 312 |
<p> |
| 313 |
<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 ); ?> /> |
| 314 |
<label for="<?php echo esc_attr( $this->get_field_id( 'chips' ) ); ?>"><?php esc_html_e( 'Show selected terms (Chips)', 'filter-everything' ); ?></label> |
| 315 |
</p> |
| 316 |
<p> |
| 317 |
<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 ); ?> /> |
| 318 |
<label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php esc_html_e( 'Show number of posts found', 'filter-everything' ); ?></label> |
| 319 |
</p> |
| 320 |
<?php |
| 321 |
} |
| 322 |
|
| 323 |
public function update( $new_instance, $old_instance ) { |
| 324 |
$instance = []; |
| 325 |
$instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 326 |
$instance['chips'] = ( !empty( $new_instance['chips'] ) ) ? 1 : 0; |
| 327 |
$instance['show_count'] = ( !empty( $new_instance['show_count'] ) ) ? 1 : 0; |
| 328 |
|
| 329 |
return $instance; |
| 330 |
} |
| 331 |
} |