GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-search-wp-widget.php
Go to the documentation of this file.
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.', 'gk-gravityview')
15  );
16 
17  $widget_display = array(
18  'width' => 650
19  );
20 
21  parent::__construct( 'gravityview_search', __( 'GravityView Search', 'gk-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' ) ) {
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  if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
56  return false;
57  }
58 
59  // Don't show unless a View ID has been set.
60  if( empty( $instance['view_id'] ) ) {
61 
62  gravityview()->log->debug( 'No View ID has been defined. Not showing the widget.', array( 'data' => $instance ) );
63 
64  return;
65  }
66 
67  if ( ! class_exists( 'GravityView_View' ) ) {
68  gravityview()->log->debug( 'GravityView_View does not exist. Not showing the widget.' );
69  return;
70  }
71 
72  /** This filter is documented in wp-includes/default-widgets.php */
73  $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
74 
75  echo $args['before_widget'];
76 
77  if ( $title ) {
78  echo $args['before_title'] . $title . $args['after_title'];
79  }
80 
81  // @todo Add to the widget configuration form
82  $instance['search_layout'] = apply_filters( 'gravityview/widget/search/layout', 'vertical', $instance );
83 
84  $instance['context'] = 'wp_widget';
85 
86  // form
87  $instance['form_id'] = GVCommon::get_meta_form_id( $instance['view_id'] );
88  $instance['form'] = GVCommon::get_form( $instance['form_id'] );
89 
90  // We don't want to overwrite existing context, etc.
91  $previous_view = GravityView_View::getInstance();
92 
93  /** @hack */
94  new GravityView_View( $instance );
95 
96  GravityView_Widget_Search::getInstance()->render_frontend( $instance );
97 
98  /**
99  * Restore previous View context
100  * @hack
101  */
102  new GravityView_View( $previous_view );
103 
104  echo $args['after_widget'];
105  }
106 
107  /**
108  * @inheritDoc
109  */
110  public function update( $new_instance, $old_instance ) {
111 
112  $instance = $old_instance;
113 
114  if( $this->is_preview() ) {
115  //Oh! Sorry but still not fully compatible with customizer
116  return $instance;
117  }
118 
119  $new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() );
120 
121  $instance['title'] = strip_tags( $new_instance['title'] );
122  $instance['view_id'] = absint( $new_instance['view_id'] );
123  $instance['search_fields'] = $new_instance['search_fields'];
124  $instance['post_id'] = $new_instance['post_id'];
125  $instance['search_clear'] = $new_instance['search_clear'];
126  $instance['search_mode'] = $new_instance['search_mode'];
127 
128  $is_valid_embed_id = GravityView_View_Data::is_valid_embed_id( $instance['post_id'], $instance['view_id'], true );
129 
130  //check if post_id is a valid post with embedded View
131  $instance['error_post_id'] = is_wp_error( $is_valid_embed_id ) ? $is_valid_embed_id->get_error_message() : NULL;
132 
133  // Share that the widget isn't brand new
134  $instance['updated'] = 1;
135 
136  return $instance;
137  }
138 
139  /**
140  * @inheritDoc
141  */
142  public function form( $instance ) {
143 
144  // @todo Make compatible with Customizer
145  if( $this->is_preview() ) {
146 
147  $warning = sprintf( esc_html__( 'This widget is not configurable from this screen. Please configure it on the %sWidgets page%s.', 'gk-gravityview' ), '<a href="'.admin_url('widgets.php').'">', '</a>' );
148 
149  echo wpautop( GravityView_Admin::get_floaty() . $warning );
150 
151  return;
152  }
153 
154  $instance = wp_parse_args( (array) $instance, self::get_defaults() );
155 
156  $title = $instance['title'];
157  $view_id = $instance['view_id'];
158  $post_id = $instance['post_id'];
159  $search_fields = $instance['search_fields'];
160  $search_clear = $instance['search_clear'];
161  $search_mode = $instance['search_mode'];
162 
163  $views = GVCommon::get_all_views();
164 
165  // If there are no views set up yet, we get outta here.
166  if( empty( $views ) ) { ?>
167  <div id="select_gravityview_view">
168  <div class="wrap"><?php echo GravityView_Admin::no_views_text(); ?></div>
169  </div>
170  <?php return;
171  }
172  ?>
173 
174  <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'gk-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>
175 
176  <?php
177  /**
178  * Display errors generated for invalid embed IDs
179  * @see GravityView_View_Data::is_valid_embed_id
180  */
181  if( isset( $instance['updated'] ) && empty( $instance['view_id'] ) ) {
182  ?>
183  <div class="error inline hide-on-view-change">
184  <p><?php esc_html_e('Please select a View to search.', 'gk-gravityview'); ?></p>
185  </div>
186  <?php
187  unset ( $error );
188  }
189  ?>
190 
191  <p>
192  <label for="gravityview_view_id"><?php _e( 'View:', 'gk-gravityview' ); ?></label>
193  <select id="gravityview_view_id" name="<?php echo $this->get_field_name('view_id'); ?>" class="widefat">
194  <option value=""><?php esc_html_e( '&mdash; Select a View &mdash;', 'gk-gravityview' ); ?></option>
195  <?php
196  foreach( $views as $view_option ) {
197  $title = empty( $view_option->post_title ) ? __('(no title)', 'gk-gravityview') : $view_option->post_title;
198  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>';
199  }
200  ?>
201  </select>
202 
203  </p>
204 
205  <?php
206  /**
207  * Display errors generated for invalid embed IDs
208  * @see GravityView_View_Data::is_valid_embed_id
209  */
210  if( !empty( $instance['error_post_id'] ) ) {
211  ?>
212  <div class="error inline">
213  <p><?php echo $instance['error_post_id']; ?></p>
214  </div>
215  <?php
216  unset ( $error );
217  }
218  ?>
219 
220  <p>
221  <label for="<?php echo $this->get_field_id('post_id'); ?>"><?php esc_html_e( 'If Embedded, Page ID:', 'gk-gravityview' ); ?></label>
222  <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 ); ?>" />
223  <span class="howto gv-howto"><?php
224  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.', 'gk-gravityview' );
225  echo ' '.gravityview_get_link('https://docs.gravityview.co/article/222-the-search-widget', __('Learn more&hellip;', 'gk-gravityview' ), 'target=_blank' );
226  ?></span>
227  </p>
228 
229  <p>
230  <label for="<?php echo $this->get_field_id('search_clear'); ?>"><?php esc_html_e( 'Show Clear button', 'gk-gravityview' ); ?>:</label>
231  <input name="<?php echo $this->get_field_name('search_clear'); ?>" type="hidden" value="0">
232  <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 ); ?>>
233  </p>
234 
235  <p>
236  <label><?php esc_html_e( 'Search Mode', 'gk-gravityview' ); ?>:</label>
237  <label for="<?php echo $this->get_field_id('search_mode'); ?>_any">
238  <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 ); ?>>
239  <?php esc_html_e( 'Match Any Fields', 'gk-gravityview' ); ?>
240  </label>
241  <label for="<?php echo $this->get_field_id('search_mode'); ?>_all">
242  <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 ); ?>>
243  <?php esc_html_e( 'Match All Fields', 'gk-gravityview' ); ?>
244  </label>
245  <span class="howto gv-howto"><?php esc_html_e('Should search results match all search fields, or any?', 'gk-gravityview' ); ?></span
246  </p>
247 
248  <hr />
249 
250  <?php // @todo: move style to CSS ?>
251  <div style="margin-bottom: 1em;">
252  <label class="screen-reader-text" for="<?php echo $this->get_field_id('search_fields'); ?>"><?php _e( 'Searchable fields:', 'gk-gravityview' ); ?></label>
253  <div class="gv-widget-search-fields" title="<?php esc_html_e('Search Fields', 'gk-gravityview'); ?>">
254  <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">
255  </div>
256 
257  </div>
258 
259  <script>
260  // When the widget is saved or added, refresh the Merge Tags (here for backward compatibility)
261  // WordPress 3.9 added widget-added and widget-updated actions
262  jQuery('#<?php echo $this->get_field_id( 'view_id' ); ?>').trigger( 'change' );
263  </script>
264  <?php
265  }
266 
267 }
static getInstance( $passed_post=NULL)
gravityview_register_gravityview_widgets()
Register the default widgets.
static get_meta_form_id( $view_id)
Get the Gravity Forms form ID connected to a View.
static is_valid_embed_id( $post_id='', $view_id='', $empty_is_valid=false)
Checks if the passed post id has the passed View id embedded.
Definition: class-data.php:286
static no_views_text()
Get text for no views found.
Definition: class-admin.php:55
If this file is called directly, abort.
static get_floaty()
Get an image of our intrepid explorer friend.
gravityview()
The main GravityView wrapper function.
static get_all_views( $args=array())
Get all existing Views.
static get_form( $form_id)
Returns the form object for a given Form ID.
$title