| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FilterEverything\Filter; |
| 5 |
|
| 6 |
if ( ! defined('ABSPATH') ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
class ChipsWidget extends \WP_Widget |
| 10 |
{ |
| 11 |
public function __construct() { |
| 12 |
parent::__construct( |
| 13 |
'wpc_chips_widget', // Base ID |
| 14 |
esc_html__( 'Filter Everything — Chips', 'filter-everything'), |
| 15 |
array( 'description' => esc_html__( 'Displays selected terms', 'filter-everything' ), ) |
| 16 |
); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Front-end display of widget. |
| 21 |
* |
| 22 |
* @see WP_Widget::widget() |
| 23 |
* |
| 24 |
* @param array $args Widget arguments. |
| 25 |
* @param array $instance Saved values from database. |
| 26 |
*/ |
| 27 |
public function widget( $args, $instance ) { |
| 28 |
$title = isset( $instance['title'] ) ? $instance['title'] : ''; |
| 29 |
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
| 30 |
$set_id = isset( $instance['set_id'] ) ? preg_replace('/[^\d\,]?/', '', $instance['set_id'] ) : ''; |
| 31 |
$mobile = ( !empty( $instance['mobile'] ) ) ? $instance['mobile'] : ''; |
| 32 |
$setIds = $classes = []; |
| 33 |
|
| 34 |
if( isset( $_POST['action'] ) && $_POST['action'] === 'elementor_ajax' ){ |
| 35 |
echo '<strong>'.esc_html__( 'Filter Everything — Chips', 'filter-everything' ).'</strong>'; |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
if( isset( $_GET['action'] ) && $_GET['action'] === 'elementor' ){ |
| 40 |
echo '<strong>'.esc_html__( 'Filter Everything — Chips', 'filter-everything' ).'</strong>'; |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if( $mobile ){ |
| 45 |
$classes[] = 'wpc-show-on-mobile'; |
| 46 |
if( strpos($args['before_title'], 'widget-title') !== false ){ |
| 47 |
$args['before_title'] = str_replace('widget-title', 'widget-title wpc-show-on-mobile-widget-title', $args['before_title']); |
| 48 |
} elseif( strpos($args['before_title'], 'widgettitle') !== false ){ |
| 49 |
$args['before_title'] = str_replace('widgettitle', 'widgettitle wpc-show-on-mobile-widget-title', $args['before_title']); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if( $set_id ){ |
| 54 |
$setIds = explode( ",", $set_id ); |
| 55 |
} |
| 56 |
|
| 57 |
echo $args['before_widget']; |
| 58 |
if ( ! empty( $title ) ) { |
| 59 |
echo $args['before_title'] . $title . $args['after_title']; |
| 60 |
} |
| 61 |
|
| 62 |
flrt_show_selected_terms(true, $setIds, $classes); |
| 63 |
|
| 64 |
echo $args['after_widget']; |
| 65 |
} |
| 66 |
|
| 67 |
public function form( $instance ) { |
| 68 |
|
| 69 |
$title = isset( $instance['title'] ) ? $instance['title'] : ''; |
| 70 |
$set_id = isset( $instance['set_id'] ) ? $instance['set_id'] : ''; |
| 71 |
$mobile = isset( $instance['mobile'] ) ? (bool) $instance['mobile'] : true; |
| 72 |
|
| 73 |
?> |
| 74 |
<p> |
| 75 |
<label for="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"><?php esc_html_e( 'Title:' ); ?></label> |
| 76 |
<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 ); ?>" /> |
| 77 |
</p> |
| 78 |
<p> |
| 79 |
<label for="<?php echo esc_attr( $this->get_field_id( 'set_id' ) ); ?>"><?php esc_html_e( 'Show Chips only for Set with IDs:', 'filter-everything' ); ?></label> |
| 80 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'set_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'set_id' ) ); ?>" type="text" value="<?php echo esc_attr( $set_id ); ?>" placeholder="<?php esc_html_e( 'e.g. 2745, 324', 'filter-everything' ); ?>"/> |
| 81 |
</p> |
| 82 |
<p> |
| 83 |
<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'mobile' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'mobile' ) ); ?>"<?php checked( $mobile ); ?> /> |
| 84 |
<label for="<?php echo esc_attr( $this->get_field_id( 'mobile' ) ); ?>"><?php esc_html_e( 'Show on mobile', 'filter-everything' ); ?></label> |
| 85 |
</p> |
| 86 |
<?php |
| 87 |
} |
| 88 |
|
| 89 |
public function update( $new_instance, $old_instance ) { |
| 90 |
$instance = []; |
| 91 |
$instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 92 |
$instance['set_id'] = ( !empty( $new_instance['set_id'] ) ) ? $new_instance['set_id'] : ''; |
| 93 |
$instance['mobile'] = ( !empty( $new_instance['mobile'] ) ) ? 1 : 0; |
| 94 |
|
| 95 |
return $instance; |
| 96 |
} |
| 97 |
} |