-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-search-filter.php
174 lines (150 loc) · 5.73 KB
/
wp-search-filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/*
Plugin Name: WP Search Filter
Plugin URI: https://victorassis.com.br/
Description: Create filters for your content
Version: 0.0.1
Author: Victor Assis
Author URI: https://victorassis.com.br/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wp-search-filter
Domain Path: /languages
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Registra Widgets
*/
class FilterFieldWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, 'WPFS: Filter Field' );
}
function widget( $args, $instance ) {
echo '<div class="wpfs-filter-field">';
if ( ! empty( $instance['title'] ) )
echo "<h2>" . $instance['title'] . "</h2>";
$field = get_field_object($instance['field']);
switch ($field['type']) {
case 'select':
$options = $field['choices'];
break;
case 'post_object':
$posts = get_posts([
'post_type' => $field['post_type'],
'numberposts' => -1,
'orderby' => 'title'
]);
$options = [];
if ($posts)
foreach ($posts as $post)
$options[$post->ID] = $post->post_title;
break;
default:
$options = [];
break;
}
foreach ($options as $key => $value) {
echo "<label><input type='checkbox' name='{$field['name']}[]' value='$key' /> $value</label>";
}
echo '</div>';
}
function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'wp-search-filter' );
$field = ! empty( $instance['field'] ) ? $instance['field'] : esc_html__( 'Field name', 'wp-search-filter' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'wp-search-filter' ); ?></label>
<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 ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'field' ) ); ?>"><?php esc_attr_e( 'Field:', 'wp-search-filter' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'field' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'field' ) ); ?>" type="text" value="<?php echo esc_attr( $field ); ?>">
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
$instance['field'] = ( ! empty( $new_instance['field'] ) ) ? sanitize_text_field( $new_instance['field'] ) : '';
return $instance;
}
}
class ResetFilterWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, 'WPFS: Reset Filter' );
}
function widget( $args, $instance ) {
if (empty( $instance['btn_text'] ))
return;
echo '<div class="wpfs-reset-filter">';
echo "<a id='wpfs-reset-filter-button' class='button'>" . $instance['btn_text'] . "</a>";
echo '</div>';
}
function form( $instance ) {
$btn_text = ! empty( $instance['btn_text'] ) ? $instance['btn_text'] : esc_html__( 'Limpar filtros', 'wp-search-filter' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'btn_text' ) ); ?>"><?php esc_attr_e( 'Button Label:', 'wp-search-filter' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'btn_text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'btn_text' ) ); ?>" type="text" value="<?php echo esc_attr( $btn_text ); ?>">
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = array();
$instance['btn_text'] = ( ! empty( $new_instance['btn_text'] ) ) ? sanitize_text_field( $new_instance['btn_text'] ) : '';
return $instance;
}
}
function wpfilterfield_register_widgets() {
register_widget( 'FilterFieldWidget' );
register_widget( 'ResetFilterWidget' );
}
add_action( 'widgets_init', 'wpfilterfield_register_widgets' );
/**
* Filter results
*/
add_action('pre_get_posts', 'wpfilterfield_pre_get_posts', 10, 1);
function wpfilterfield_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ) return;
// bail early if not main query
// - allows custom code / plugins to continue working
if( !$query->is_main_query() ) return;
// bail early if isnt right post type
if ( !is_post_type_archive( ['cartao'] ) ) return;
// get meta query
$meta_query = $query->get('meta_query');
// loop over filters
if (!isset($_GET['f'])) return;
foreach( $_GET['f'] as $key => $values ) {
// continue if not found in url
if( empty($values) ) {
continue;
}
$value = explode(',',$values);
// append meta query
$meta_query[] = array(
'key' => $key,
'value' => $value,
'compare' => 'IN',
);
}
// update meta query
$query->set('meta_query', $meta_query);
}
/**
* Enqueue Scripts
*/
function wpfilterfield_enqueue_script() {
wp_enqueue_script( 'wpfilterfield_scripts', plugin_dir_url( __FILE__ ) . 'assets/js/wpfilterfield.js', ['jquery'] );
$data = [
'params' => $_GET
];
wp_localize_script( 'wpfilterfield_scripts', 'wp', $data );
wp_enqueue_style('wpfilterfield_styles', plugin_dir_url( __FILE__ ) . 'assets/css/wpfilterfield.css');
}
add_action('wp_enqueue_scripts', 'wpfilterfield_enqueue_script');