1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Search widget class |
6
|
|
|
* @since 1.6 |
7
|
|
|
*/ |
8
|
|
|
class GravityView_Search_WP_Widget extends WP_Widget { |
9
|
|
|
|
10
|
|
|
public function __construct() { |
11
|
|
|
|
12
|
|
|
$widget_ops = array( |
13
|
|
|
'classname' => 'widget_gravityview_search', |
14
|
|
|
'description' => __( 'A search form for a specific GravityView.', 'gravityview') |
|
|
|
|
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
$widget_display = array( |
18
|
|
|
'width' => 650 |
|
|
|
|
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
parent::__construct( 'gravityview_search', __( 'GravityView Search', 'gravityview' ), $widget_ops, $widget_display ); |
22
|
|
|
|
23
|
|
|
$this->load_required_files(); |
24
|
|
|
|
25
|
|
|
$gravityview_widget = GravityView_Widget_Search::getInstance(); |
26
|
|
|
|
27
|
|
|
// frontend - filter entries |
28
|
|
|
add_filter( 'gravityview_fe_search_criteria', array( $gravityview_widget, 'filter_entries' ), 10, 3 ); |
29
|
|
|
|
30
|
|
|
// frontend - add template path |
31
|
|
|
add_filter( 'gravityview_template_paths', array( $gravityview_widget, 'add_template_path' ) ); |
32
|
|
|
|
33
|
|
|
unset( $gravityview_widget ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function load_required_files() { |
37
|
|
|
if( !class_exists( 'GravityView_Widget_Search' ) ) { |
|
|
|
|
38
|
|
|
gravityview_register_gravityview_widgets(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private static function get_defaults() { |
43
|
|
|
return array( |
44
|
|
|
'title' => '', |
45
|
|
|
'view_id' => 0, |
46
|
|
|
'post_id' => '', |
47
|
|
|
'search_fields' => '', |
48
|
|
|
'search_clear' => 0, |
49
|
|
|
'search_mode' => 'any' |
|
|
|
|
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function widget( $args, $instance ) { |
54
|
|
|
|
55
|
|
|
// Don't show unless a View ID has been set. |
56
|
|
|
if( empty( $instance['view_id'] ) ) { |
57
|
|
|
|
58
|
|
|
gravityview()->log->debug( 'No View ID has been defined. Not showing the widget.', array( 'data' => $instance ) ); |
59
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ( ! class_exists( 'GravityView_View' ) ) { |
64
|
|
|
gravityview()->log->debug( 'GravityView_View does not exist. Not showing the widget.' ); |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** This filter is documented in wp-includes/default-widgets.php */ |
69
|
|
|
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); |
70
|
|
|
|
71
|
|
|
echo $args['before_widget']; |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ( $title ) { |
74
|
|
|
echo $args['before_title'] . $title . $args['after_title']; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// @todo Add to the widget configuration form |
78
|
|
|
$instance['search_layout'] = apply_filters( 'gravityview/widget/search/layout', 'vertical', $instance ); |
79
|
|
|
|
80
|
|
|
$instance['context'] = 'wp_widget'; |
81
|
|
|
|
82
|
|
|
// form |
83
|
|
|
$instance['form_id'] = GVCommon::get_meta_form_id( $instance['view_id'] ); |
84
|
|
|
$instance['form'] = GVCommon::get_form( $instance['form_id'] ); |
85
|
|
|
|
86
|
|
|
// We don't want to overwrite existing context, etc. |
87
|
|
|
$previous_view = GravityView_View::getInstance(); |
88
|
|
|
|
89
|
|
|
/** @hack */ |
90
|
|
|
new GravityView_View( $instance ); |
91
|
|
|
|
92
|
|
|
GravityView_Widget_Search::getInstance()->render_frontend( $instance ); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Restore previous View context |
96
|
|
|
* @hack |
97
|
|
|
*/ |
98
|
|
|
new GravityView_View( $previous_view ); |
99
|
|
|
|
100
|
|
|
echo $args['after_widget']; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
|
|
public function update( $new_instance, $old_instance ) { |
107
|
|
|
|
108
|
|
|
$instance = $old_instance; |
109
|
|
|
|
110
|
|
|
if( $this->is_preview() ) { |
111
|
|
|
//Oh! Sorry but still not fully compatible with customizer |
112
|
|
|
return $instance; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() ); |
116
|
|
|
|
117
|
|
|
$instance['title'] = strip_tags( $new_instance['title'] ); |
118
|
|
|
$instance['view_id'] = absint( $new_instance['view_id'] ); |
119
|
|
|
$instance['search_fields'] = $new_instance['search_fields']; |
120
|
|
|
$instance['post_id'] = $new_instance['post_id']; |
121
|
|
|
$instance['search_clear'] = $new_instance['search_clear']; |
122
|
|
|
$instance['search_mode'] = $new_instance['search_mode']; |
123
|
|
|
|
124
|
|
|
$is_valid_embed_id = GravityView_View_Data::is_valid_embed_id( $instance['post_id'], $instance['view_id'], true ); |
125
|
|
|
|
126
|
|
|
//check if post_id is a valid post with embedded View |
127
|
|
|
$instance['error_post_id'] = is_wp_error( $is_valid_embed_id ) ? $is_valid_embed_id->get_error_message() : NULL; |
|
|
|
|
128
|
|
|
|
129
|
|
|
// Share that the widget isn't brand new |
130
|
|
|
$instance['updated'] = 1; |
131
|
|
|
|
132
|
|
|
return $instance; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
|
|
public function form( $instance ) { |
139
|
|
|
|
140
|
|
|
// @todo Make compatible with Customizer |
141
|
|
|
if( $this->is_preview() ) { |
142
|
|
|
|
143
|
|
|
$warning = sprintf( esc_html__( 'This widget is not configurable from this screen. Please configure it on the %sWidgets page%s.', 'gravityview' ), '<a href="'.admin_url('widgets.php').'">', '</a>' ); |
|
|
|
|
144
|
|
|
|
145
|
|
|
echo wpautop( GravityView_Admin::get_floaty() . $warning ); |
|
|
|
|
146
|
|
|
|
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$instance = wp_parse_args( (array) $instance, self::get_defaults() ); |
151
|
|
|
|
152
|
|
|
$title = $instance['title']; |
153
|
|
|
$view_id = $instance['view_id']; |
154
|
|
|
$post_id = $instance['post_id']; |
155
|
|
|
$search_fields = $instance['search_fields']; |
156
|
|
|
$search_clear = $instance['search_clear']; |
157
|
|
|
$search_mode = $instance['search_mode']; |
158
|
|
|
|
159
|
|
|
$views = GVCommon::get_all_views(); |
160
|
|
|
|
161
|
|
|
// If there are no views set up yet, we get outta here. |
162
|
|
|
if( empty( $views ) ) { ?> |
163
|
|
|
<div id="select_gravityview_view"> |
164
|
|
|
<div class="wrap"><?php echo GravityView_Admin::no_views_text(); ?></div> |
|
|
|
|
165
|
|
|
</div> |
166
|
|
|
<?php return; |
167
|
|
|
} |
168
|
|
|
?> |
169
|
|
|
|
170
|
|
|
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'gravityview'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p> |
|
|
|
|
171
|
|
|
|
172
|
|
|
<?php |
173
|
|
|
/** |
174
|
|
|
* Display errors generated for invalid embed IDs |
175
|
|
|
* @see GravityView_View_Data::is_valid_embed_id |
176
|
|
|
*/ |
177
|
|
|
if( isset( $instance['updated'] ) && empty( $instance['view_id'] ) ) { |
178
|
|
|
?> |
179
|
|
|
<div class="error inline hide-on-view-change"> |
180
|
|
|
<p><?php esc_html_e('Please select a View to search.', 'gravityview'); ?></p> |
|
|
|
|
181
|
|
|
</div> |
182
|
|
|
<?php |
183
|
|
|
unset ( $error ); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
?> |
186
|
|
|
|
187
|
|
|
<p> |
188
|
|
|
<label for="gravityview_view_id"><?php _e( 'View:', 'gravityview' ); ?></label> |
189
|
|
|
<select id="gravityview_view_id" name="<?php echo $this->get_field_name('view_id'); ?>" class="widefat"> |
|
|
|
|
190
|
|
|
<option value=""><?php esc_html_e( '— Select a View —', 'gravityview' ); ?></option> |
191
|
|
|
<?php |
192
|
|
|
foreach( $views as $view_option ) { |
193
|
|
|
$title = empty( $view_option->post_title ) ? __('(no title)', 'gravityview') : $view_option->post_title; |
|
|
|
|
194
|
|
|
echo '<option value="'. $view_option->ID .'" ' . selected( esc_attr( $view_id ), $view_option->ID, false ) . '>'. esc_html( sprintf('%s #%d', $title, $view_option->ID ) ) .'</option>'; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
?> |
197
|
|
|
</select> |
198
|
|
|
|
199
|
|
|
</p> |
200
|
|
|
|
201
|
|
|
<?php |
202
|
|
|
/** |
203
|
|
|
* Display errors generated for invalid embed IDs |
204
|
|
|
* @see GravityView_View_Data::is_valid_embed_id |
205
|
|
|
*/ |
206
|
|
|
if( !empty( $instance['error_post_id'] ) ) { |
|
|
|
|
207
|
|
|
?> |
208
|
|
|
<div class="error inline"> |
209
|
|
|
<p><?php echo $instance['error_post_id']; ?></p> |
|
|
|
|
210
|
|
|
</div> |
211
|
|
|
<?php |
212
|
|
|
unset ( $error ); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
?> |
215
|
|
|
|
216
|
|
|
<p> |
217
|
|
|
<label for="<?php echo $this->get_field_id('post_id'); ?>"><?php esc_html_e( 'If Embedded, Page ID:', 'gravityview' ); ?></label> |
|
|
|
|
218
|
|
|
<input class="code" size="3" id="<?php echo $this->get_field_id('post_id'); ?>" name="<?php echo $this->get_field_name('post_id'); ?>" type="text" value="<?php echo esc_attr( $post_id ); ?>" /> |
|
|
|
|
219
|
|
|
<span class="howto"><?php |
220
|
|
|
esc_html_e('To have a search performed on an embedded View, enter the ID of the post or page where the View is embedded.', 'gravityview' ); |
|
|
|
|
221
|
|
|
echo ' '.gravityview_get_link('https://docs.gravityview.co/article/222-the-search-widget', __('Learn more…', 'gravityview' ), 'target=_blank' ); |
|
|
|
|
222
|
|
|
?></span> |
223
|
|
|
</p> |
224
|
|
|
|
225
|
|
|
<p> |
226
|
|
|
<label for="<?php echo $this->get_field_id('search_clear'); ?>"><?php esc_html_e( 'Show Clear button', 'gravityview' ); ?>:</label> |
|
|
|
|
227
|
|
|
<input name="<?php echo $this->get_field_name('search_clear'); ?>" type="hidden" value="0"> |
|
|
|
|
228
|
|
|
<input id="<?php echo $this->get_field_id('search_clear'); ?>" name="<?php echo $this->get_field_name('search_clear'); ?>" type="checkbox" class="checkbox" value="1" <?php checked( $search_clear, 1, true ); ?>> |
|
|
|
|
229
|
|
|
</p> |
230
|
|
|
|
231
|
|
|
<p> |
232
|
|
|
<label><?php esc_html_e( 'Search Mode', 'gravityview' ); ?>:</label> |
233
|
|
|
<label for="<?php echo $this->get_field_id('search_mode'); ?>_any"> |
|
|
|
|
234
|
|
|
<input id="<?php echo $this->get_field_id('search_mode'); ?>_any" name="<?php echo $this->get_field_name('search_mode'); ?>" type="radio" class="radio" value="any" <?php checked( $search_mode, 'any', true ); ?>> |
|
|
|
|
235
|
|
|
<?php esc_html_e( 'Match Any Fields', 'gravityview' ); ?> |
236
|
|
|
</label> |
237
|
|
|
<label for="<?php echo $this->get_field_id('search_mode'); ?>_all"> |
|
|
|
|
238
|
|
|
<input id="<?php echo $this->get_field_id('search_mode'); ?>_all" name="<?php echo $this->get_field_name('search_mode'); ?>" type="radio" class="radio" value="all" <?php checked( $search_mode, 'all', true ); ?>> |
|
|
|
|
239
|
|
|
<?php esc_html_e( 'Match All Fields', 'gravityview' ); ?> |
240
|
|
|
</label> |
241
|
|
|
<span class="howto"><?php esc_html_e('Should search results match all search fields, or any?', 'gravityview' ); ?></span |
|
|
|
|
242
|
|
|
</p> |
243
|
|
|
|
244
|
|
|
<hr /> |
245
|
|
|
|
246
|
|
|
<?php // @todo: move style to CSS ?> |
247
|
|
|
<div style="margin-bottom: 1em;"> |
248
|
|
|
<label class="screen-reader-text" for="<?php echo $this->get_field_id('search_fields'); ?>"><?php _e( 'Searchable fields:', 'gravityview' ); ?></label> |
|
|
|
|
249
|
|
|
<div class="gv-widget-search-fields" title="<?php esc_html_e('Search Fields', 'gravityview'); ?>"> |
|
|
|
|
250
|
|
|
<input id="<?php echo $this->get_field_id('search_fields'); ?>" name="<?php echo $this->get_field_name('search_fields'); ?>" type="hidden" value="<?php echo esc_attr( $search_fields ); ?>" class="gv-search-fields-value"> |
|
|
|
|
251
|
|
|
</div> |
252
|
|
|
|
253
|
|
|
</div> |
254
|
|
|
|
255
|
|
|
<script> |
256
|
|
|
// When the widget is saved or added, refresh the Merge Tags (here for backward compatibility) |
257
|
|
|
// WordPress 3.9 added widget-added and widget-updated actions |
258
|
|
|
jQuery('#<?php echo $this->get_field_id( 'view_id' ); ?>').trigger( 'change' ); |
|
|
|
|
259
|
|
|
</script> |
260
|
|
|
<?php |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
} |
264
|
|
|
|