GravityView  2.9.2
The best, easiest way to display Gravity Forms entries on your website.
class-search-widget.php
Go to the documentation of this file.
1 <?php
2 /**
3  * The GravityView New Search widget
4  *
5  * @package GravityView-DataTables-Ext
6  * @license GPL2+
7  * @author GravityView <hello@gravityview.co>
8  * @link http://gravityview.co
9  * @copyright Copyright 2014, Katz Web Services, Inc.
10  */
11 
12 if ( ! defined( 'WPINC' ) ) {
13  die;
14 }
15 
17 
18  public static $file;
19  public static $instance;
20 
21  private $search_filters = array();
22 
23  /**
24  * whether search method is GET or POST ( default: GET )
25  * @since 1.16.4
26  * @var string $search_method
27  */
28  private $search_method = 'get';
29 
30  public function __construct() {
31 
32  $this->widget_id = 'search_bar';
33  $this->widget_description = esc_html__( 'Search form for searching entries.', 'gravityview' );
34 
35  self::$instance = &$this;
36 
37  self::$file = plugin_dir_path( __FILE__ );
38 
39  $default_values = array( 'header' => 0, 'footer' => 0 );
40 
41  $settings = array(
42  'search_layout' => array(
43  'type' => 'radio',
44  'full_width' => true,
45  'label' => esc_html__( 'Search Layout', 'gravityview' ),
46  'value' => 'horizontal',
47  'options' => array(
48  'horizontal' => esc_html__( 'Horizontal', 'gravityview' ),
49  'vertical' => esc_html__( 'Vertical', 'gravityview' ),
50  ),
51  ),
52  'search_clear' => array(
53  'type' => 'checkbox',
54  'label' => __( 'Show Clear button', 'gravityview' ),
55  'value' => false,
56  ),
57  'search_fields' => array(
58  'type' => 'hidden',
59  'label' => '',
60  'class' => 'gv-search-fields-value',
61  'value' => '[{"field":"search_all","input":"input_text"}]', // Default: Search Everything text box
62  ),
63  'search_mode' => array(
64  'type' => 'radio',
65  'full_width' => true,
66  'label' => esc_html__( 'Search Mode', 'gravityview' ),
67  'desc' => __('Should search results match all search fields, or any?', 'gravityview'),
68  'value' => 'any',
69  'class' => 'hide-if-js',
70  'options' => array(
71  'any' => esc_html__( 'Match Any Fields', 'gravityview' ),
72  'all' => esc_html__( 'Match All Fields', 'gravityview' ),
73  ),
74  ),
75  );
76 
77  if ( ! $this->is_registered() ) {
78  // frontend - filter entries
79  add_filter( 'gravityview_fe_search_criteria', array( $this, 'filter_entries' ), 10, 3 );
80 
81  // frontend - add template path
82  add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) );
83 
84  // admin - add scripts - run at 1100 to make sure GravityView_Admin_Views::add_scripts_and_styles() runs first at 999
85  add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts_and_styles' ), 1100 );
86  add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts') );
87  add_filter( 'gravityview_noconflict_scripts', array( $this, 'register_no_conflict' ) );
88 
89  // ajax - get the searchable fields
90  add_action( 'wp_ajax_gv_searchable_fields', array( 'GravityView_Widget_Search', 'get_searchable_fields' ) );
91 
92  add_action( 'gravityview_search_widget_fields_after', array( $this, 'add_preview_inputs' ) );
93  }
94 
95  parent::__construct( esc_html__( 'Search Bar', 'gravityview' ), null, $default_values, $settings );
96 
97  // calculate the search method (POST / GET)
98  $this->set_search_method();
99  }
100 
101  /**
102  * @return GravityView_Widget_Search
103  */
104  public static function getInstance() {
105  if ( empty( self::$instance ) ) {
106  self::$instance = new GravityView_Widget_Search;
107  }
108  return self::$instance;
109  }
110 
111  /**
112  * Sets the search method to GET (default) or POST
113  * @since 1.16.4
114  */
115  private function set_search_method() {
116  /**
117  * @filter `gravityview/search/method` Modify the search form method (GET / POST)
118  * @since 1.16.4
119  * @param string $search_method Assign an input type according to the form field type. Defaults: `boolean`, `multi`, `select`, `date`, `text`
120  * @param string $field_type Gravity Forms field type (also the `name` parameter of GravityView_Field classes)
121  */
122  $method = apply_filters( 'gravityview/search/method', $this->search_method );
123 
124  $method = strtolower( $method );
125 
126  $this->search_method = in_array( $method, array( 'get', 'post' ) ) ? $method : 'get';
127  }
128 
129  /**
130  * Returns the search method
131  * @since 1.16.4
132  * @return string
133  */
134  public function get_search_method() {
135  return $this->search_method;
136  }
137 
138  /**
139  * Get the input types available for different field types
140  *
141  * @since 1.17.5
142  *
143  * @return array [field type name] => (array|string) search bar input types
144  */
145  public static function get_input_types_by_field_type() {
146  /**
147  * Input Type groups
148  * @see admin-search-widget.js (getSelectInput)
149  */
150  $input_types = array(
151  'text' => array( 'input_text' ),
152  'address' => array( 'input_text' ),
153  'number' => array( 'input_text' ),
154  'date' => array( 'date', 'date_range' ),
155  'boolean' => array( 'single_checkbox' ),
156  'select' => array( 'select', 'radio', 'link' ),
157  'multi' => array( 'select', 'multiselect', 'radio', 'checkbox', 'link' ),
158 
159  // hybrids
160  'created_by' => array( 'select', 'radio', 'checkbox', 'multiselect', 'link', 'input_text' ),
161  'product' => array( 'select', 'radio', 'link', 'input_text' ),
162  );
163 
164  /**
165  * @filter `gravityview/search/input_types` Change the types of search fields available to a field type
166  * @see GravityView_Widget_Search::get_search_input_labels() for the available input types
167  * @param array $input_types Associative array: key is field `name`, value is array of GravityView input types (note: use `input_text` for `text`)
168  */
169  $input_types = apply_filters( 'gravityview/search/input_types', $input_types );
170 
171  return $input_types;
172  }
173 
174  /**
175  * Get labels for different types of search bar inputs
176  *
177  * @since 1.17.5
178  *
179  * @return array [input type] => input type label
180  */
181  public static function get_search_input_labels() {
182  /**
183  * Input Type labels l10n
184  * @see admin-search-widget.js (getSelectInput)
185  */
186  $input_labels = array(
187  'input_text' => esc_html__( 'Text', 'gravityview' ),
188  'date' => esc_html__( 'Date', 'gravityview' ),
189  'select' => esc_html__( 'Select', 'gravityview' ),
190  'multiselect' => esc_html__( 'Select (multiple values)', 'gravityview' ),
191  'radio' => esc_html__( 'Radio', 'gravityview' ),
192  'checkbox' => esc_html__( 'Checkbox', 'gravityview' ),
193  'single_checkbox' => esc_html__( 'Checkbox', 'gravityview' ),
194  'link' => esc_html__( 'Links', 'gravityview' ),
195  'date_range' => esc_html__( 'Date range', 'gravityview' ),
196  );
197 
198  /**
199  * @filter `gravityview/search/input_types` Change the label of search field input types
200  * @param array $input_types Associative array: key is input type name, value is label
201  */
202  $input_labels = apply_filters( 'gravityview/search/input_labels', $input_labels );
203 
204  return $input_labels;
205  }
206 
207  public static function get_search_input_label( $input_type ) {
208  $labels = self::get_search_input_labels();
209 
210  return \GV\Utils::get( $labels, $input_type, false );
211  }
212 
213  /**
214  * Add script to Views edit screen (admin)
215  * @param mixed $hook
216  */
217  public function add_scripts_and_styles( $hook ) {
218  global $pagenow;
219 
220  // Don't process any scripts below here if it's not a GravityView page or the widgets screen
221  if ( ! gravityview()->request->is_admin( $hook, 'single' ) && ( 'widgets.php' !== $pagenow ) ) {
222  return;
223  }
224 
225  $script_min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
226  $script_source = empty( $script_min ) ? '/source' : '';
227 
228  wp_enqueue_script( 'gravityview_searchwidget_admin', plugins_url( 'assets/js'.$script_source.'/admin-search-widget'.$script_min.'.js', __FILE__ ), array( 'jquery', 'gravityview_views_scripts' ), \GV\Plugin::$version );
229 
230  wp_localize_script( 'gravityview_searchwidget_admin', 'gvSearchVar', array(
231  'nonce' => wp_create_nonce( 'gravityview_ajaxsearchwidget' ),
232  'label_nofields' => esc_html__( 'No search fields configured yet.', 'gravityview' ),
233  'label_addfield' => esc_html__( 'Add Search Field', 'gravityview' ),
234  'label_label' => esc_html__( 'Label', 'gravityview' ),
235  'label_searchfield' => esc_html__( 'Search Field', 'gravityview' ),
236  'label_inputtype' => esc_html__( 'Input Type', 'gravityview' ),
237  'label_ajaxerror' => esc_html__( 'There was an error loading searchable fields. Save the View or refresh the page to fix this issue.', 'gravityview' ),
238  'input_labels' => json_encode( self::get_search_input_labels() ),
239  'input_types' => json_encode( self::get_input_types_by_field_type() ),
240  ) );
241 
242  }
243 
244  /**
245  * Add admin script to the no-conflict scripts whitelist
246  * @param array $allowed Scripts allowed in no-conflict mode
247  * @return array Scripts allowed in no-conflict mode, plus the search widget script
248  */
249  public function register_no_conflict( $allowed ) {
250  $allowed[] = 'gravityview_searchwidget_admin';
251  return $allowed;
252  }
253 
254  /**
255  * Ajax
256  * Returns the form fields ( only the searchable ones )
257  *
258  * @return void
259  */
260  public static function get_searchable_fields() {
261 
262  if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'gravityview_ajaxsearchwidget' ) ) {
263  exit( '0' );
264  }
265 
266  $form = '';
267 
268  // Fetch the form for the current View
269  if ( ! empty( $_POST['view_id'] ) ) {
270 
271  $form = gravityview_get_form_id( $_POST['view_id'] );
272 
273  } elseif ( ! empty( $_POST['formid'] ) ) {
274 
275  $form = (int) $_POST['formid'];
276 
277  } elseif ( ! empty( $_POST['template_id'] ) && class_exists( 'GravityView_Ajax' ) ) {
278 
279  $form = GravityView_Ajax::pre_get_form_fields( $_POST['template_id'] );
280 
281  }
282 
283  // fetch form id assigned to the view
284  $response = self::render_searchable_fields( $form );
285 
286  exit( $response );
287  }
288 
289  /**
290  * Generates html for the available Search Fields dropdown
291  * @param int $form_id
292  * @param string $current (for future use)
293  * @return string
294  */
295  public static function render_searchable_fields( $form_id = null, $current = '' ) {
296 
297  if ( is_null( $form_id ) ) {
298  return '';
299  }
300 
301  // start building output
302 
303  $output = '<select class="gv-search-fields">';
304 
305  $custom_fields = array(
306  'search_all' => array(
307  'text' => esc_html__( 'Search Everything', 'gravityview' ),
308  'type' => 'text',
309  ),
310  'entry_date' => array(
311  'text' => esc_html__( 'Entry Date', 'gravityview' ),
312  'type' => 'date',
313  ),
314  'entry_id' => array(
315  'text' => esc_html__( 'Entry ID', 'gravityview' ),
316  'type' => 'text',
317  ),
318  'created_by' => array(
319  'text' => esc_html__( 'Entry Creator', 'gravityview' ),
320  'type' => 'created_by',
321  ),
322  'is_starred' => array(
323  'text' => esc_html__( 'Is Starred', 'gravityview' ),
324  'type' => 'boolean',
325  ),
326  );
327 
328  if ( gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ) {
329  $custom_fields['is_approved'] = array(
330  'text' => esc_html__( 'Approval Status', 'gravityview' ),
331  'type' => 'multi',
332  );
333  }
334 
335  foreach( $custom_fields as $custom_field_key => $custom_field ) {
336  $output .= sprintf( '<option value="%s" %s data-inputtypes="%s" data-placeholder="%s">%s</option>', $custom_field_key, selected( $custom_field_key, $current, false ), $custom_field['type'], self::get_field_label( array('field' => $custom_field_key ) ), $custom_field['text'] );
337  }
338 
339  // Get fields with sub-inputs and no parent
340  $fields = gravityview_get_form_fields( $form_id, true, true );
341 
342  /**
343  * @filter `gravityview/search/searchable_fields` Modify the fields that are displayed as searchable in the Search Bar dropdown\n
344  * @since 1.17
345  * @see gravityview_get_form_fields() Used to fetch the fields
346  * @see GravityView_Widget_Search::get_search_input_types See this method to modify the type of input types allowed for a field
347  * @param array $fields Array of searchable fields, as fetched by gravityview_get_form_fields()
348  * @param int $form_id
349  */
350  $fields = apply_filters( 'gravityview/search/searchable_fields', $fields, $form_id );
351 
352  if ( ! empty( $fields ) ) {
353 
354  $blacklist_field_types = apply_filters( 'gravityview_blacklist_field_types', array( 'fileupload', 'post_image', 'post_id', 'section' ), null );
355 
356  foreach ( $fields as $id => $field ) {
357 
358  if ( in_array( $field['type'], $blacklist_field_types ) ) {
359  continue;
360  }
361 
362  $types = self::get_search_input_types( $id, $field['type'] );
363 
364  $output .= '<option value="'. $id .'" '. selected( $id, $current, false ).'data-inputtypes="'. esc_attr( $types ) .'">'. esc_html( $field['label'] ) .'</option>';
365  }
366  }
367 
368  $output .= '</select>';
369 
370  return $output;
371 
372  }
373 
374  /**
375  * Assign an input type according to the form field type
376  *
377  * @see admin-search-widget.js
378  *
379  * @param string|int|float $field_id Gravity Forms field ID
380  * @param string $field_type Gravity Forms field type (also the `name` parameter of GravityView_Field classes)
381  *
382  * @return string GV field search input type ('multi', 'boolean', 'select', 'date', 'text')
383  */
384  public static function get_search_input_types( $field_id = '', $field_type = null ) {
385 
386  // @todo - This needs to be improved - many fields have . including products and addresses
387  if ( false !== strpos( (string) $field_id, '.' ) && in_array( $field_type, array( 'checkbox' ) ) || in_array( $field_id, array( 'is_fulfilled' ) ) ) {
388  $input_type = 'boolean'; // on/off checkbox
389  } elseif ( in_array( $field_type, array( 'checkbox', 'post_category', 'multiselect' ) ) ) {
390  $input_type = 'multi'; //multiselect
391  } elseif ( in_array( $field_type, array( 'select', 'radio' ) ) ) {
392  $input_type = 'select';
393  } elseif ( in_array( $field_type, array( 'date' ) ) || in_array( $field_id, array( 'payment_date' ) ) ) {
394  $input_type = 'date';
395  } elseif ( in_array( $field_type, array( 'number' ) ) || in_array( $field_id, array( 'payment_amount' ) ) ) {
396  $input_type = 'number';
397  } elseif ( in_array( $field_type, array( 'product' ) ) ) {
398  $input_type = 'product';
399  } else {
400  $input_type = 'text';
401  }
402 
403  /**
404  * @filter `gravityview/extension/search/input_type` Modify the search form input type based on field type
405  * @since 1.2
406  * @since 1.19.2 Added $field_id parameter
407  * @param string $input_type Assign an input type according to the form field type. Defaults: `boolean`, `multi`, `select`, `date`, `text`
408  * @param string $field_type Gravity Forms field type (also the `name` parameter of GravityView_Field classes)
409  * @param string|int|float $field_id ID of the field being processed
410  */
411  $input_type = apply_filters( 'gravityview/extension/search/input_type', $input_type, $field_type, $field_id );
412 
413  return $input_type;
414  }
415 
416  /**
417  * Display hidden fields to add support for sites using Default permalink structure
418  *
419  * @since 1.8
420  * @return array Search fields, modified if not using permalinks
421  */
422  public function add_no_permalink_fields( $search_fields, $object, $widget_args = array() ) {
423  /** @global WP_Rewrite $wp_rewrite */
424  global $wp_rewrite;
425 
426  // Support default permalink structure
427  if ( false === $wp_rewrite->using_permalinks() ) {
428 
429  // By default, use current post.
430  $post_id = 0;
431 
432  // We're in the WordPress Widget context, and an overriding post ID has been set.
433  if ( ! empty( $widget_args['post_id'] ) ) {
434  $post_id = absint( $widget_args['post_id'] );
435  }
436  // We're in the WordPress Widget context, and the base View ID should be used
437  else if ( ! empty( $widget_args['view_id'] ) ) {
438  $post_id = absint( $widget_args['view_id'] );
439  }
440 
441  $args = gravityview_get_permalink_query_args( $post_id );
442 
443  // Add hidden fields to the search form
444  foreach ( $args as $key => $value ) {
445  $search_fields[] = array(
446  'name' => $key,
447  'input' => 'hidden',
448  'value' => $value,
449  );
450  }
451  }
452 
453  return $search_fields;
454  }
455 
456  /**
457  * Get the fields that are searchable for a View
458  *
459  * @since 2.0
460  * @since 2.0.9 Added $with_full_field parameter
461  *
462  * @param \GV\View|null $view
463  * @param bool $with_full_field Return full field array, or just field ID? Default: false (just field ID)
464  *
465  * TODO: Move to \GV\View, perhaps? And return a Field_Collection
466  * TODO: Use in gravityview()->request->is_search() to calculate whether a valid search
467  *
468  * @return array If no View, returns empty array. Otherwise, returns array of fields configured in widgets and Search Bar for a View
469  */
470  private function get_view_searchable_fields( $view, $with_full_field = false ) {
471 
472  /**
473  * Find all search widgets on the view and get the searchable fields settings.
474  */
475  $searchable_fields = array();
476 
477  if ( ! $view ) {
478  return $searchable_fields;
479  }
480 
481  /**
482  * Include the sidebar Widgets.
483  */
484  $widgets = (array) get_option( 'widget_gravityview_search', array() );
485 
486  foreach ( $widgets as $widget ) {
487  if ( ! empty( $widget['view_id'] ) && $widget['view_id'] == $view->ID ) {
488  if( $_fields = json_decode( $widget['search_fields'], true ) ) {
489  foreach ( $_fields as $field ) {
490  if ( empty( $field['form_id'] ) ) {
491  $field['form_id'] = $view->form ? $view->form->ID : 0;
492  }
493  $searchable_fields[] = $with_full_field ? $field : $field['field'];
494  }
495  }
496  }
497  }
498 
499  foreach ( $view->widgets->by_id( $this->get_widget_id() )->all() as $widget ) {
500  if( $_fields = json_decode( $widget->configuration->get( 'search_fields' ), true ) ) {
501  foreach ( $_fields as $field ) {
502  if ( empty( $field['form_id'] ) ) {
503  $field['form_id'] = $view->form ? $view->form->ID : 0;
504  }
505  $searchable_fields[] = $with_full_field ? $field : $field['field'];
506  }
507  }
508  }
509 
510  /**
511  * @filter `gravityview/search/searchable_fields/whitelist` Modifies the fields able to be searched using the Search Bar
512  * @since 2.5.1
513  *
514  * @param array $searchable_fields Array of GravityView-formatted fields or only the field ID? Example: [ '1.2', 'created_by' ]
515  * @param \GV\View $view Object of View being searched.
516  * @param bool $with_full_field Does $searchable_fields contain the full field array or just field ID? Default: false (just field ID)
517  */
518  return apply_filters( 'gravityview/search/searchable_fields/whitelist', $searchable_fields, $view, $with_full_field );
519  }
520 
521  /** --- Frontend --- */
522 
523  /**
524  * Calculate the search criteria to filter entries
525  * @param array $search_criteria The search criteria
526  * @param int $form_id The form ID
527  * @param array $args Some args
528  *
529  * @param bool $force_search_criteria Whether to suppress GF_Query filter, internally used in self::gf_query_filter
530  *
531  * @return array
532  */
533  public function filter_entries( $search_criteria, $form_id = null, $args = array(), $force_search_criteria = false ) {
534  if ( ! $force_search_criteria && gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ) {
535  /**
536  * If GF_Query is available, we can construct custom conditions with nested
537  * booleans on the query, giving up the old ways of flat search_criteria field_filters.
538  */
539  add_action( 'gravityview/view/query', array( $this, 'gf_query_filter' ), 10, 3 );
540  return $search_criteria; // Return the original criteria, GF_Query modification kicks in later
541  }
542 
543  if( 'post' === $this->search_method ) {
544  $get = $_POST;
545  } else {
546  $get = $_GET;
547  }
548 
549  $view = \GV\View::by_id( \GV\Utils::get( $args, 'id' ) );
550 
551  gravityview()->log->debug( 'Requested $_{method}: ', array( 'method' => $this->search_method, 'data' => $get ) );
552 
553  if ( empty( $get ) || ! is_array( $get ) ) {
554  return $search_criteria;
555  }
556 
557  $get = stripslashes_deep( $get );
558 
559  $get = gv_map_deep( $get, 'rawurldecode' );
560 
561  // Make sure array key is set up
562  $search_criteria['field_filters'] = \GV\Utils::get( $search_criteria, 'field_filters', array() );
563 
564  $searchable_fields = $this->get_view_searchable_fields( $view );
565  $searchable_field_objects = $this->get_view_searchable_fields( $view, true );
566 
567  // add free search
568  if ( isset( $get['gv_search'] ) && '' !== $get['gv_search'] && in_array( 'search_all', $searchable_fields ) ) {
569 
570  $search_all_value = trim( $get['gv_search'] );
571 
572  /**
573  * @filter `gravityview/search-all-split-words` Search for each word separately or the whole phrase?
574  * @since 1.20.2
575  * @param bool $split_words True: split a phrase into words; False: search whole word only [Default: true]
576  */
577  $split_words = apply_filters( 'gravityview/search-all-split-words', true );
578 
579  if ( $split_words ) {
580 
581  // Search for a piece
582  $words = explode( ' ', $search_all_value );
583 
584  $words = array_filter( $words );
585 
586  } else {
587 
588  // Replace multiple spaces with one space
589  $search_all_value = preg_replace( '/\s+/ism', ' ', $search_all_value );
590 
591  $words = array( $search_all_value );
592  }
593 
594  foreach ( $words as $word ) {
595  $search_criteria['field_filters'][] = array(
596  'key' => null, // The field ID to search
597  'value' => $word, // The value to search
598  'operator' => 'contains', // What to search in. Options: `is` or `contains`
599  );
600  }
601  }
602 
603  // start date & end date
604  if ( in_array( 'entry_date', $searchable_fields ) ) {
605  /**
606  * Get and normalize the dates according to the input format.
607  */
608  if ( $curr_start = ! empty( $get['gv_start'] ) ? $get['gv_start'] : '' ) {
609  if( $curr_start_date = date_create_from_format( $this->get_datepicker_format( true ), $curr_start ) ) {
610  $curr_start = $curr_start_date->format( 'Y-m-d' );
611  }
612  }
613 
614  if ( $curr_end = ! empty( $get['gv_start'] ) ? ( ! empty( $get['gv_end'] ) ? $get['gv_end'] : '' ) : '' ) {
615  if( $curr_end_date = date_create_from_format( $this->get_datepicker_format( true ), $curr_end ) ) {
616  $curr_end = $curr_end_date->format( 'Y-m-d' );
617  }
618  }
619 
620  if ( $view ) {
621  /**
622  * Override start and end dates if View is limited to some already.
623  */
624  if ( $start_date = $view->settings->get( 'start_date' ) ) {
625  if ( $start_timestamp = strtotime( $curr_start ) ) {
626  $curr_start = $start_timestamp < strtotime( $start_date ) ? $start_date : $curr_start;
627  }
628  }
629  if ( $end_date = $view->settings->get( 'end_date' ) ) {
630  if ( $end_timestamp = strtotime( $curr_end ) ) {
631  $curr_end = $end_timestamp > strtotime( $end_date ) ? $end_date : $curr_end;
632  }
633  }
634  }
635 
636  /**
637  * @filter `gravityview_date_created_adjust_timezone` Whether to adjust the timezone for entries. \n
638  * date_created is stored in UTC format. Convert search date into UTC (also used on templates/fields/date_created.php)
639  * @since 1.12
640  * @param[out,in] boolean $adjust_tz Use timezone-adjusted datetime? If true, adjusts date based on blog's timezone setting. If false, uses UTC setting. Default: true
641  * @param[in] string $context Where the filter is being called from. `search` in this case.
642  */
643  $adjust_tz = apply_filters( 'gravityview_date_created_adjust_timezone', true, 'search' );
644 
645  /**
646  * Don't set $search_criteria['start_date'] if start_date is empty as it may lead to bad query results (GFAPI::get_entries)
647  */
648  if ( ! empty( $curr_start ) ) {
649  $curr_start = date( 'Y-m-d H:i:s', strtotime( $curr_start ) );
650  $search_criteria['start_date'] = $adjust_tz ? get_gmt_from_date( $curr_start ) : $curr_start;
651  }
652 
653  if ( ! empty( $curr_end ) ) {
654  // Fast-forward 24 hour on the end time
655  $curr_end = date( 'Y-m-d H:i:s', strtotime( $curr_end ) + DAY_IN_SECONDS );
656  $search_criteria['end_date'] = $adjust_tz ? get_gmt_from_date( $curr_end ) : $curr_end;
657  if ( strpos( $search_criteria['end_date'], '00:00:00' ) ) { // See https://github.com/gravityview/GravityView/issues/1056
658  $search_criteria['end_date'] = date( 'Y-m-d H:i:s', strtotime( $search_criteria['end_date'] ) - 1 );
659  }
660  }
661  }
662 
663  // search for a specific entry ID
664  if ( ! empty( $get[ 'gv_id' ] ) && in_array( 'entry_id', $searchable_fields ) ) {
665  $search_criteria['field_filters'][] = array(
666  'key' => 'id',
667  'value' => absint( $get[ 'gv_id' ] ),
668  'operator' => $this->get_operator( $get, 'gv_id', array( '=' ), '=' ),
669  );
670  }
671 
672  // search for a specific Created_by ID
673  if ( ! empty( $get[ 'gv_by' ] ) && in_array( 'created_by', $searchable_fields ) ) {
674  $search_criteria['field_filters'][] = array(
675  'key' => 'created_by',
676  'value' => $get['gv_by'],
677  'operator' => $this->get_operator( $get, 'gv_by', array( '=' ), '=' ),
678  );
679  }
680 
681  // Get search mode passed in URL
682  $mode = isset( $get['mode'] ) && in_array( $get['mode'], array( 'any', 'all' ) ) ? $get['mode'] : 'any';
683 
684  // get the other search filters
685  foreach ( $get as $key => $value ) {
686 
687  if ( 0 !== strpos( $key, 'filter_' ) || gv_empty( $value, false, false ) || ( is_array( $value ) && count( $value ) === 1 && gv_empty( $value[0], false, false ) ) ) {
688  continue; // Not a filter, or empty
689  }
690 
691  if ( strpos( $key, '|op' ) !== false ) {
692  continue; // This is an operator
693  }
694 
695  $filter_key = $this->convert_request_key_to_filter_key( $key );
696 
697  if ( ! $filter = $this->prepare_field_filter( $filter_key, $value, $view, $searchable_field_objects, $get ) ) {
698  continue;
699  }
700 
701  if ( ! isset( $filter['operator'] ) ) {
702  $filter['operator'] = $this->get_operator( $get, $key, array( 'contains' ), 'contains' );
703  }
704 
705  if ( isset( $filter[0]['value'] ) ) {
706  $search_criteria['field_filters'] = array_merge( $search_criteria['field_filters'], $filter );
707 
708  // if date range type, set search mode to ALL
709  if ( ! empty( $filter[0]['operator'] ) && in_array( $filter[0]['operator'], array( '>=', '<=', '>', '<' ) ) ) {
710  $mode = 'all';
711  }
712  } elseif( !empty( $filter ) ) {
713  $search_criteria['field_filters'][] = $filter;
714  }
715  }
716 
717  /**
718  * @filter `gravityview/search/mode` Set the Search Mode (`all` or `any`)
719  * @since 1.5.1
720  * @param[out,in] string $mode Search mode (`any` vs `all`)
721  */
722  $search_criteria['field_filters']['mode'] = apply_filters( 'gravityview/search/mode', $mode );
723 
724  gravityview()->log->debug( 'Returned Search Criteria: ', array( 'data' => $search_criteria ) );
725 
726  unset( $get );
727 
728  return $search_criteria;
729  }
730 
731  /**
732  * Filters the \GF_Query with advanced logic.
733  *
734  * Dropin for the legacy flat filters when \GF_Query is available.
735  *
736  * @param \GF_Query $query The current query object reference
737  * @param \GV\View $this The current view object
738  * @param \GV\Request $request The request object
739  */
740  public function gf_query_filter( &$query, $view, $request ) {
741  /**
742  * This is a shortcut to get all the needed search criteria.
743  * We feed these into an new GF_Query and tack them onto the current object.
744  */
745  $search_criteria = $this->filter_entries( array(), null, array( 'id' => $view->ID ), true /** force search_criteria */ );
746 
747  /**
748  * Call any userland filters that they might have.
749  */
750  remove_filter( 'gravityview_fe_search_criteria', array( $this, 'filter_entries' ), 10, 3 );
751  $search_criteria = apply_filters( 'gravityview_fe_search_criteria', $search_criteria, $view->form->ID, $view->settings->as_atts() );
752  add_filter( 'gravityview_fe_search_criteria', array( $this, 'filter_entries' ), 10, 3 );
753 
754  $query_class = $view->get_query_class();
755 
756  if ( empty( $search_criteria['field_filters'] ) ) {
757  return;
758  }
759 
760  $widgets = $view->widgets->by_id( $this->widget_id );
761  if ( $widgets->count() ) {
762  $widgets = $widgets->all();
763  $widget = $widgets[0];
764 
765  $search_fields = json_decode( $widget->configuration->get( 'search_fields' ), true );
766 
767  foreach ( (array) $search_fields as $search_field ) {
768  if ( 'created_by' === $search_field['field'] && 'input_text' === $search_field['input'] ) {
769  $created_by_text_mode = true;
770  }
771  }
772  }
773 
774  $extra_conditions = array();
775  $mode = 'any';
776 
777  foreach ( $search_criteria['field_filters'] as &$filter ) {
778  if ( ! is_array( $filter ) ) {
779  if ( in_array( strtolower( $filter ), array( 'any', 'all' ) ) ) {
780  $mode = $filter;
781  }
782  continue;
783  }
784 
785  // Construct a manual query for unapproved statuses
786  if ( 'is_approved' === $filter['key'] && in_array( \GravityView_Entry_Approval_Status::UNAPPROVED, (array) $filter['value'] ) ) {
787  $_tmp_query = new $query_class( $view->form->ID, array(
788  'field_filters' => array(
789  array(
790  'operator' => 'in',
791  'key' => 'is_approved',
792  'value' => (array) $filter['value'],
793  ),
794  array(
795  'operator' => 'is',
796  'key' => 'is_approved',
797  'value' => '',
798  ),
799  'mode' => 'any'
800  ),
801  ) );
802  $_tmp_query_parts = $_tmp_query->_introspect();
803 
804  $extra_conditions[] = $_tmp_query_parts['where'];
805 
806  $filter = false;
807  continue;
808  }
809 
810  // Construct manual query for text mode creator search
811  if ( 'created_by' === $filter['key'] && ! empty( $created_by_text_mode ) ) {
812  $extra_conditions[] = new GravityView_Widget_Search_Author_GF_Query_Condition( $filter, $view );
813  $filter = false;
814  continue;
815  }
816 
817  // By default, we want searches to be wildcard for each field.
818  $filter['operator'] = empty( $filter['operator'] ) ? 'contains' : $filter['operator'];
819 
820  // For multichoice, let's have an in (OR) search.
821  if ( is_array( $filter['value'] ) ) {
822  $filter['operator'] = 'in'; // @todo what about in contains (OR LIKE chains)?
823  }
824 
825  // Default form with joins functionality
826  if ( empty( $filter['form_id'] ) ) {
827  $filter['form_id'] = $view->form ? $view->form->ID : 0;
828  }
829 
830  /**
831  * @filter `gravityview_search_operator` Modify the search operator for the field (contains, is, isnot, etc)
832  * @param string $operator Existing search operator
833  * @param array $filter array with `key`, `value`, `operator`, `type` keys
834  * @since develop
835  * @param \GV\View $view The View we're operating on.
836  */
837  $filter['operator'] = apply_filters( 'gravityview_search_operator', $filter['operator'], $filter, $view );
838  }
839 
840  if ( ! empty( $search_criteria['start_date'] ) || ! empty( $search_criteria['end_date'] ) ) {
841  $date_criteria = array();
842 
843  if ( isset( $search_criteria['start_date'] ) ) {
844  $date_criteria['start_date'] = $search_criteria['start_date'];
845  }
846 
847  if ( isset( $search_criteria['end_date'] ) ) {
848  $date_criteria['end_date'] = $search_criteria['end_date'];
849  }
850 
851  $_tmp_query = new $query_class( $view->form->ID, $date_criteria );
852  $_tmp_query_parts = $_tmp_query->_introspect();
853  $extra_conditions[] = $_tmp_query_parts['where'];
854  }
855 
856  $search_conditions = array();
857 
858  if ( $filters = array_filter( $search_criteria['field_filters'] ) ) {
859  foreach ( $filters as &$filter ) {
860  if ( ! is_array( $filter ) ) {
861  continue;
862  }
863 
864  /**
865  * Parse the filter criteria to generate the needed
866  * WHERE condition. This is a trick to not write our own generation
867  * code by reusing what's inside GF_Query already as they
868  * take care of many small things like forcing numeric, etc.
869  */
870  $_tmp_query = new $query_class( $filter['form_id'], array( 'mode' => 'any', 'field_filters' => array( $filter ) ) );
871  $_tmp_query_parts = $_tmp_query->_introspect();
872  $search_condition = $_tmp_query_parts['where'];
873 
874  if ( empty( $filter['key'] ) && $search_condition->expressions ) {
875  $search_conditions[] = $search_condition;
876  } else {
877  $left = $search_condition->left;
878  $alias = $query->_alias( $left->field_id, $left->source, $left->is_entry_column() ? 't' : 'm' );
879 
880  if ( $view->joins && $left->field_id == GF_Query_Column::META ) {
881  foreach ( $view->joins as $_join ) {
882  $on = $_join->join_on;
883  $join = $_join->join;
884 
885  $search_conditions[] = GF_Query_Condition::_or(
886  // Join
887  new GF_Query_Condition(
888  new GF_Query_Column( GF_Query_Column::META, $join->ID, $query->_alias( GF_Query_Column::META, $join->ID, 'm' ) ),
889  $search_condition->operator,
890  $search_condition->right
891  ),
892  // On
893  new GF_Query_Condition(
894  new GF_Query_Column( GF_Query_Column::META, $on->ID, $query->_alias( GF_Query_Column::META, $on->ID, 'm' ) ),
895  $search_condition->operator,
896  $search_condition->right
897  )
898  );
899  }
900  } else {
901  $search_conditions[] = new GF_Query_Condition(
902  new GF_Query_Column( $left->field_id, $left->source, $alias ),
903  $search_condition->operator,
904  $search_condition->right
905  );
906  }
907  }
908  }
909 
910  if ( $search_conditions ) {
911  $search_conditions = array( call_user_func_array( '\GF_Query_Condition::' . ( $mode == 'all' ? '_and' : '_or' ), $search_conditions ) );
912  }
913  }
914 
915  /**
916  * Grab the current clauses. We'll be combining them shortly.
917  */
918  $query_parts = $query->_introspect();
919 
920  /**
921  * Combine the parts as a new WHERE clause.
922  */
923  $where = call_user_func_array( '\GF_Query_Condition::_and', array_merge( array( $query_parts['where'] ), $search_conditions, $extra_conditions ) );
924  $query->where( $where );
925  }
926 
927  /**
928  * Convert $_GET/$_POST key to the field/meta ID
929  *
930  * Examples:
931  * - `filter_is_starred` => `is_starred`
932  * - `filter_1_2` => `1.2`
933  * - `filter_5` => `5`
934  *
935  * @since 2.0
936  *
937  * @param string $key $_GET/_$_POST search key
938  *
939  * @return string
940  */
941  private function convert_request_key_to_filter_key( $key ) {
942 
943  $field_id = str_replace( 'filter_', '', $key );
944 
945  // calculates field_id, removing 'filter_' and for '_' for advanced fields ( like name or checkbox )
946  if ( preg_match('/^[0-9_]+$/ism', $field_id ) ) {
947  $field_id = str_replace( '_', '.', $field_id );
948  }
949 
950  return $field_id;
951  }
952 
953  /**
954  * Prepare the field filters to GFAPI
955  *
956  * The type post_category, multiselect and checkbox support multi-select search - each value needs to be separated in an independent filter so we could apply the ANY search mode.
957  *
958  * Format searched values
959  *
960  * @param string $filter_key ID of the field, or entry meta key
961  * @param string $value $_GET/$_POST search value
962  * @param \GV\View $view The view we're looking at
963  * @param array[] $searchable_fields The searchable fields as configured by the widget.
964  * @param string[] $get The $_GET/$_POST array.
965  *
966  * @since develop Added 5th $get parameter for operator overrides.
967  * @todo Set function as private.
968  *
969  * @return array|false 1 or 2 deph levels, false if not allowed
970  */
971  public function prepare_field_filter( $filter_key, $value, $view, $searchable_fields, $get = array() ) {
972  $key = $filter_key;
973  $filter_key = explode( ':', $filter_key ); // field_id, form_id
974 
975  $form = null;
976 
977  if ( count( $filter_key ) > 1 ) {
978  // form is specified
979  list( $field_id, $form_id ) = $filter_key;
980 
981  if ( $forms = \GV\View::get_joined_forms( $view->ID ) ) {
982  if ( ! $form = \GV\GF_Form::by_id( $form_id ) ) {
983  return false;
984  }
985  }
986 
987  // form is allowed
988  $found = false;
989  foreach ( $forms as $form ) {
990  if ( $form->ID == $form_id ) {
991  $found = true;
992  break;
993  }
994  }
995 
996  if ( ! $found ) {
997  return false;
998  }
999 
1000  // form is in searchable fields
1001  $found = false;
1002  foreach ( $searchable_fields as $field ) {
1003  if ( $field_id == $field['field'] && $form->ID == $field['form_id'] ) {
1004  $found = true;
1005  break;
1006  }
1007  }
1008 
1009  if ( ! $found ) {
1010  return false;
1011  }
1012  } else {
1013  $field_id = reset( $filter_key );
1014  $searchable_fields = wp_list_pluck( $searchable_fields, 'field' );
1015  if ( ! in_array( 'search_all', $searchable_fields ) && ! in_array( $field_id, $searchable_fields ) ) {
1016  return false;
1017  }
1018  }
1019 
1020  if ( ! $form ) {
1021  // fallback
1022  $form = $view->form;
1023  }
1024 
1025  // get form field array
1026  $form_field = is_numeric( $field_id ) ? \GV\GF_Field::by_id( $form, $field_id ) : \GV\Internal_Field::by_id( $field_id );
1027 
1028  if ( ! $form_field ) {
1029  return false;
1030  }
1031 
1032  // default filter array
1033  $filter = array(
1034  'key' => $field_id,
1035  'value' => $value,
1036  'form_id' => $form->ID,
1037  );
1038 
1039  switch ( $form_field->type ) {
1040 
1041  case 'select':
1042  case 'radio':
1043  $filter['operator'] = $this->get_operator( $get, $key, array( 'is' ), 'is' );
1044  break;
1045 
1046  case 'post_category':
1047 
1048  if ( ! is_array( $value ) ) {
1049  $value = array( $value );
1050  }
1051 
1052  // Reset filter variable
1053  $filter = array();
1054 
1055  foreach ( $value as $val ) {
1056  $cat = get_term( $val, 'category' );
1057  $filter[] = array(
1058  'key' => $field_id,
1059  'value' => esc_attr( $cat->name ) . ':' . $val,
1060  'operator' => $this->get_operator( $get, $key, array( 'is' ), 'is' ),
1061  );
1062  }
1063 
1064  break;
1065 
1066  case 'multiselect':
1067 
1068  if ( ! is_array( $value ) ) {
1069  break;
1070  }
1071 
1072  // Reset filter variable
1073  $filter = array();
1074 
1075  foreach ( $value as $val ) {
1076  $filter[] = array( 'key' => $field_id, 'value' => $val );
1077  }
1078 
1079  break;
1080 
1081  case 'checkbox':
1082  // convert checkbox on/off into the correct search filter
1083  if ( false !== strpos( $field_id, '.' ) && ! empty( $form_field->inputs ) && ! empty( $form_field->choices ) ) {
1084  foreach ( $form_field->inputs as $k => $input ) {
1085  if ( $input['id'] == $field_id ) {
1086  $filter['value'] = $form_field->choices[ $k ]['value'];
1087  $filter['operator'] = $this->get_operator( $get, $key, array( 'is' ), 'is' );
1088  break;
1089  }
1090  }
1091  } elseif ( is_array( $value ) ) {
1092 
1093  // Reset filter variable
1094  $filter = array();
1095 
1096  foreach ( $value as $val ) {
1097  $filter[] = array(
1098  'key' => $field_id,
1099  'value' => $val,
1100  'operator' => $this->get_operator( $get, $key, array( 'is' ), 'is' ),
1101  );
1102  }
1103  }
1104 
1105  break;
1106 
1107  case 'name':
1108  case 'address':
1109 
1110  if ( false === strpos( $field_id, '.' ) ) {
1111 
1112  $words = explode( ' ', $value );
1113 
1114  $filters = array();
1115  foreach ( $words as $word ) {
1116  if ( ! empty( $word ) && strlen( $word ) > 1 ) {
1117  // Keep the same key for each filter
1118  $filter['value'] = $word;
1119  // Add a search for the value
1120  $filters[] = $filter;
1121  }
1122  }
1123 
1124  $filter = $filters;
1125  }
1126 
1127  // State/Province should be exact matches
1128  if ( 'address' === $form_field->field->type ) {
1129 
1130  $searchable_fields = $this->get_view_searchable_fields( $view, true );
1131 
1132  foreach ( $searchable_fields as $searchable_field ) {
1133 
1134  if( $form_field->ID !== $searchable_field['field'] ) {
1135  continue;
1136  }
1137 
1138  // Only exact-match dropdowns, not text search
1139  if( in_array( $searchable_field['input'], array( 'text', 'search' ), true ) ) {
1140  continue;
1141  }
1142 
1143  $input_id = gravityview_get_input_id_from_id( $form_field->ID );
1144 
1145  if ( 4 === $input_id ) {
1146  $filter['operator'] = $this->get_operator( $get, $key, array( 'is' ), 'is' );
1147  };
1148  }
1149  }
1150 
1151  break;
1152 
1153  case 'date':
1154 
1155  $date_format = $this->get_datepicker_format( true );
1156 
1157  if ( is_array( $value ) ) {
1158 
1159  // Reset filter variable
1160  $filter = array();
1161 
1162  foreach ( $value as $k => $date ) {
1163  if ( empty( $date ) ) {
1164  continue;
1165  }
1166  $operator = 'start' === $k ? '>=' : '<=';
1167 
1168  /**
1169  * @hack
1170  * @since 1.16.3
1171  * Safeguard until GF implements '<=' operator
1172  */
1173  if( !GFFormsModel::is_valid_operator( $operator ) && $operator === '<=' ) {
1174  $operator = '<';
1175  $date = date( 'Y-m-d', strtotime( self::get_formatted_date( $date, 'Y-m-d', $date_format ) . ' +1 day' ) );
1176  }
1177 
1178  $filter[] = array(
1179  'key' => $field_id,
1180  'value' => self::get_formatted_date( $date, 'Y-m-d', $date_format ),
1181  'operator' => $this->get_operator( $get, $key, array( $operator ), $operator ),
1182  );
1183  }
1184  } else {
1185  $date = $value;
1186  $filter['value'] = self::get_formatted_date( $date, 'Y-m-d', $date_format );
1187  $filter['operator'] = $this->get_operator( $get, $key, array( 'is' ), 'is' );
1188  }
1189 
1190  break;
1191 
1192 
1193  } // switch field type
1194 
1195  return $filter;
1196  }
1197 
1198  /**
1199  * Get the Field Format form GravityForms
1200  *
1201  * @param GF_Field_Date $field The field object
1202  * @since 1.10
1203  *
1204  * @return string Format of the date in the database
1205  */
1206  public static function get_date_field_format( GF_Field_Date $field ) {
1207  $format = 'm/d/Y';
1208  $datepicker = array(
1209  'mdy' => 'm/d/Y',
1210  'dmy' => 'd/m/Y',
1211  'dmy_dash' => 'd-m-Y',
1212  'dmy_dot' => 'd.m.Y',
1213  'ymd_slash' => 'Y/m/d',
1214  'ymd_dash' => 'Y-m-d',
1215  'ymd_dot' => 'Y.m.d',
1216  );
1217 
1218  if ( ! empty( $field->dateFormat ) && isset( $datepicker[ $field->dateFormat ] ) ){
1219  $format = $datepicker[ $field->dateFormat ];
1220  }
1221 
1222  return $format;
1223  }
1224 
1225  /**
1226  * Format a date value
1227  *
1228  * @param string $value Date value input
1229  * @param string $format Wanted formatted date
1230  *
1231  * @since 2.1.2
1232  * @param string $value_format The value format. Default: Y-m-d
1233  *
1234  * @return string
1235  */
1236  public static function get_formatted_date( $value = '', $format = 'Y-m-d', $value_format = 'Y-m-d' ) {
1237 
1238  $date = date_create_from_format( $value_format, $value );
1239 
1240  if ( empty( $date ) ) {
1241  gravityview()->log->debug( 'Date format not valid: {value}', array( 'value' => $value ) );
1242  return '';
1243  }
1244  return $date->format( $format );
1245  }
1246 
1247 
1248  /**
1249  * Include this extension templates path
1250  * @param array $file_paths List of template paths ordered
1251  */
1252  public function add_template_path( $file_paths ) {
1253 
1254  // Index 100 is the default GravityView template path.
1255  $file_paths[102] = self::$file . 'templates/';
1256 
1257  return $file_paths;
1258  }
1259 
1260  /**
1261  * Check whether the configured search fields have a date field
1262  *
1263  * @since 1.17.5
1264  *
1265  * @param array $search_fields
1266  *
1267  * @return bool True: has a `date` or `date_range` field
1268  */
1269  private function has_date_field( $search_fields ) {
1270 
1271  $has_date = false;
1272 
1273  foreach ( $search_fields as $k => $field ) {
1274  if ( in_array( $field['input'], array( 'date', 'date_range', 'entry_date' ) ) ) {
1275  $has_date = true;
1276  break;
1277  }
1278  }
1279 
1280  return $has_date;
1281  }
1282 
1283  /**
1284  * Renders the Search Widget
1285  * @param array $widget_args
1286  * @param string $content
1287  * @param string|\GV\Template_Context $context
1288  *
1289  * @return void
1290  */
1291  public function render_frontend( $widget_args, $content = '', $context = '' ) {
1292 
1294 
1295  if ( empty( $gravityview_view ) ) {
1296  gravityview()->log->debug( '$gravityview_view not instantiated yet.' );
1297  return;
1298  }
1299 
1300  $view = \GV\View::by_id( $gravityview_view->view_id );
1301 
1302  // get configured search fields
1303  $search_fields = ! empty( $widget_args['search_fields'] ) ? json_decode( $widget_args['search_fields'], true ) : '';
1304 
1305  if ( empty( $search_fields ) || ! is_array( $search_fields ) ) {
1306  gravityview()->log->debug( 'No search fields configured for widget:', array( 'data' => $widget_args ) );
1307  return;
1308  }
1309 
1310  // prepare fields
1311  foreach ( $search_fields as $k => $field ) {
1312 
1313  $updated_field = $field;
1314 
1315  $updated_field = $this->get_search_filter_details( $updated_field, $context );
1316 
1317  switch ( $field['field'] ) {
1318 
1319  case 'search_all':
1320  $updated_field['key'] = 'search_all';
1321  $updated_field['input'] = 'search_all';
1322  $updated_field['value'] = $this->rgget_or_rgpost( 'gv_search' );
1323  break;
1324 
1325  case 'entry_date':
1326  $updated_field['key'] = 'entry_date';
1327  $updated_field['input'] = 'entry_date';
1328  $updated_field['value'] = array(
1329  'start' => $this->rgget_or_rgpost( 'gv_start' ),
1330  'end' => $this->rgget_or_rgpost( 'gv_end' ),
1331  );
1332  break;
1333 
1334  case 'entry_id':
1335  $updated_field['key'] = 'entry_id';
1336  $updated_field['input'] = 'entry_id';
1337  $updated_field['value'] = $this->rgget_or_rgpost( 'gv_id' );
1338  break;
1339 
1340  case 'created_by':
1341  $updated_field['key'] = 'created_by';
1342  $updated_field['name'] = 'gv_by';
1343  $updated_field['value'] = $this->rgget_or_rgpost( 'gv_by' );
1344  $updated_field['choices'] = self::get_created_by_choices( $view );
1345  break;
1346 
1347  case 'is_approved':
1348  $updated_field['key'] = 'is_approved';
1349  $updated_field['value'] = $this->rgget_or_rgpost( 'filter_is_approved' );
1350  $updated_field['choices'] = self::get_is_approved_choices();
1351  break;
1352  }
1353 
1354  $search_fields[ $k ] = $updated_field;
1355  }
1356 
1357  gravityview()->log->debug( 'Calculated Search Fields: ', array( 'data' => $search_fields ) );
1358 
1359  /**
1360  * @filter `gravityview_widget_search_filters` Modify what fields are shown. The order of the fields in the $search_filters array controls the order as displayed in the search bar widget.
1361  * @param array $search_fields Array of search filters with `key`, `label`, `value`, `type`, `choices` keys
1362  * @param GravityView_Widget_Search $this Current widget object
1363  * @param array $widget_args Args passed to this method. {@since 1.8}
1364  * @param \GV\Template_Context $context {@since 2.0}
1365  * @type array
1366  */
1367  $gravityview_view->search_fields = apply_filters( 'gravityview_widget_search_filters', $search_fields, $this, $widget_args, $context );
1368 
1369  $gravityview_view->permalink_fields = $this->add_no_permalink_fields( array(), $this, $widget_args );
1370 
1371  $gravityview_view->search_layout = ! empty( $widget_args['search_layout'] ) ? $widget_args['search_layout'] : 'horizontal';
1372 
1373  /** @since 1.14 */
1374  $gravityview_view->search_mode = ! empty( $widget_args['search_mode'] ) ? $widget_args['search_mode'] : 'any';
1375 
1376  $custom_class = ! empty( $widget_args['custom_class'] ) ? $widget_args['custom_class'] : '';
1377 
1378  $gravityview_view->search_class = self::get_search_class( $custom_class );
1379 
1380  $gravityview_view->search_clear = ! empty( $widget_args['search_clear'] ) ? $widget_args['search_clear'] : false;
1381 
1382  if ( $this->has_date_field( $search_fields ) ) {
1383  // enqueue datepicker stuff only if needed!
1384  $this->enqueue_datepicker();
1385  }
1386 
1387  $this->maybe_enqueue_flexibility();
1388 
1389  $gravityview_view->render( 'widget', 'search', false );
1390  }
1391 
1392  /**
1393  * Get the search class for a search form
1394  *
1395  * @since 1.5.4
1396  *
1397  * @return string Sanitized CSS class for the search form
1398  */
1399  public static function get_search_class( $custom_class = '' ) {
1401 
1402  $search_class = 'gv-search-'.$gravityview_view->search_layout;
1403 
1404  if ( ! empty( $custom_class ) ) {
1405  $search_class .= ' '.$custom_class;
1406  }
1407 
1408  /**
1409  * @filter `gravityview_search_class` Modify the CSS class for the search form
1410  * @param string $search_class The CSS class for the search form
1411  */
1412  $search_class = apply_filters( 'gravityview_search_class', $search_class );
1413 
1414  // Is there an active search being performed? Used by fe-views.js
1415  $search_class .= GravityView_frontend::getInstance()->isSearch() ? ' gv-is-search' : '';
1416 
1417  return gravityview_sanitize_html_class( $search_class );
1418  }
1419 
1420 
1421  /**
1422  * Calculate the search form action
1423  * @since 1.6
1424  *
1425  * @return string
1426  */
1427  public static function get_search_form_action() {
1429 
1430  $post_id = $gravityview_view->getPostId() ? $gravityview_view->getPostId() : $gravityview_view->getViewId();
1431 
1432  $url = add_query_arg( array(), get_permalink( $post_id ) );
1433 
1434  /**
1435  * @filter `gravityview/widget/search/form/action` Override the search URL.
1436  * @param[in,out] string $action Where the form submits to.
1437  *
1438  * Further parameters will be added once adhoc context is added.
1439  * Use gravityview()->request until then.
1440  */
1441  return apply_filters( 'gravityview/widget/search/form/action', $url );
1442  }
1443 
1444  /**
1445  * Get the label for a search form field
1446  * @param array $field Field setting as sent by the GV configuration - has `field`, `input` (input type), and `label` keys
1447  * @param array $form_field Form field data, as fetched by `gravityview_get_field()`
1448  * @return string Label for the search form
1449  */
1450  private static function get_field_label( $field, $form_field = array() ) {
1451 
1452  $label = \GV\Utils::_GET( 'label', \GV\Utils::get( $field, 'label' ) );
1453 
1454  if ( ! $label ) {
1455 
1456  $label = isset( $form_field['label'] ) ? $form_field['label'] : '';
1457 
1458  switch( $field['field'] ) {
1459  case 'search_all':
1460  $label = __( 'Search Entries:', 'gravityview' );
1461  break;
1462  case 'entry_date':
1463  $label = __( 'Filter by date:', 'gravityview' );
1464  break;
1465  case 'entry_id':
1466  $label = __( 'Entry ID:', 'gravityview' );
1467  break;
1468  default:
1469  // If this is a field input, not a field
1470  if ( strpos( $field['field'], '.' ) > 0 && ! empty( $form_field['inputs'] ) ) {
1471 
1472  // Get the label for the field in question, which returns an array
1473  $items = wp_list_filter( $form_field['inputs'], array( 'id' => $field['field'] ) );
1474 
1475  // Get the item with the `label` key
1476  $values = wp_list_pluck( $items, 'label' );
1477 
1478  // There will only one item in the array, but this is easier
1479  foreach ( $values as $value ) {
1480  $label = $value;
1481  break;
1482  }
1483  }
1484  }
1485  }
1486 
1487  /**
1488  * @filter `gravityview_search_field_label` Modify the label for a search field. Supports returning HTML
1489  * @since 1.17.3 Added $field parameter
1490  * @param[in,out] string $label Existing label text, sanitized.
1491  * @param[in] array $form_field Gravity Forms field array, as returned by `GFFormsModel::get_field()`
1492  * @param[in] array $field Field setting as sent by the GV configuration - has `field`, `input` (input type), and `label` keys
1493  */
1494  $label = apply_filters( 'gravityview_search_field_label', esc_attr( $label ), $form_field, $field );
1495 
1496  return $label;
1497  }
1498 
1499  /**
1500  * Prepare search fields to frontend render with other details (label, field type, searched values)
1501  *
1502  * @param array $field
1503  * @param \GV\Context $context
1504  *
1505  * @return array
1506  */
1507  private function get_search_filter_details( $field, $context ) {
1508 
1510 
1511  $form = $gravityview_view->getForm();
1512 
1513  // for advanced field ids (eg, first name / last name )
1514  $name = 'filter_' . str_replace( '.', '_', $field['field'] );
1515 
1516  // get searched value from $_GET/$_POST (string or array)
1517  $value = $this->rgget_or_rgpost( $name );
1518 
1519  // get form field details
1520  $form_field = gravityview_get_field( $form, $field['field'] );
1521 
1522  $form_field_type = \GV\Utils::get( $form_field, 'type' );
1523 
1524  $filter = array(
1525  'key' => \GV\Utils::get( $field, 'field' ),
1526  'name' => $name,
1527  'label' => self::get_field_label( $field, $form_field ),
1528  'input' => \GV\Utils::get( $field, 'input' ),
1529  'value' => $value,
1530  'type' => $form_field_type,
1531  );
1532 
1533  // collect choices
1534  if ( 'post_category' === $form_field_type && ! empty( $form_field['displayAllCategories'] ) && empty( $form_field['choices'] ) ) {
1535  $filter['choices'] = gravityview_get_terms_choices();
1536  } elseif ( ! empty( $form_field['choices'] ) ) {
1537  $filter['choices'] = $form_field['choices'];
1538  }
1539 
1540  if ( 'date_range' === $field['input'] && empty( $value ) ) {
1541  $filter['value'] = array( 'start' => '', 'end' => '' );
1542  }
1543 
1544  if ( ! empty( $filter['choices'] ) ) {
1545  /**
1546  * @filter `gravityview/search/sieve_choices` Only output used choices for this field.
1547  * @param[in,out] bool Yes or no.
1548  * @param array $field The field configuration.
1549  * @param \GV\Context The context.
1550  */
1551  if ( apply_filters( 'gravityview/search/sieve_choices', false, $field, $context ) ) {
1552  $filter['choices'] = $this->sieve_filter_choices( $filter, $context );
1553  }
1554  }
1555 
1556  /**
1557  * @filter `gravityview/search/filter_details` Filter the output filter details for the Search widget.
1558  * @param[in,out] array $filter The filter details
1559  * @param array $field The search field configuration
1560  * @param \GV\Context The context
1561  * @since develop
1562  */
1563  $filter = apply_filters( 'gravityview/search/filter_details', $filter, $field, $context );
1564 
1565  return $filter;
1566 
1567  }
1568 
1569  /**
1570  * Sieve filter choices to only ones that are used.
1571  *
1572  * @param array $filter The filter configuration.
1573  * @param \GV\Context $context The context
1574  *
1575  * @since develop
1576  * @internal
1577  *
1578  * @return array The filter choices.
1579  */
1580  private function sieve_filter_choices( $filter, $context ) {
1581  if ( empty( $filter['key'] ) || empty( $filter['choices'] ) ) {
1582  return $filter; // @todo Populate plugins might give us empty choices
1583  }
1584 
1585  if ( ! is_numeric( $filter['key'] ) ) {
1586  return $filter;
1587  }
1588 
1589  $form_id = $context->view->form->ID; // @todo Support multiple forms (joins)
1590 
1591  global $wpdb;
1592 
1593  $table = GFFormsModel::get_entry_meta_table_name();
1594 
1595  $key_like = $wpdb->esc_like( $filter['key'] ) . '.%';
1596 
1597  switch ( \GV\Utils::get( $filter, 'type' ) ):
1598  case 'post_category':
1599  $choices = $wpdb->get_col( $wpdb->prepare(
1600  "SELECT DISTINCT SUBSTRING_INDEX(meta_value, ':', 1) FROM $table WHERE (meta_key LIKE %s OR meta_key = %d) AND form_id = %d",
1601  $key_like, $filter['key'], $form_id
1602  ) );
1603  break;
1604  default:
1605  $choices = $wpdb->get_col( $wpdb->prepare(
1606  "SELECT DISTINCT meta_value FROM $table WHERE (meta_key LIKE %s OR meta_key = %d) AND form_id = %d",
1607  $key_like, $filter['key'], $form_id
1608  ) );
1609 
1610  if ( ( $field = gravityview_get_field( $form_id, $filter['key'] ) ) && 'json' === $field->storageType ) {
1611  $choices = array_map( 'json_decode', $choices );
1612  $_choices_array = array();
1613  foreach ( $choices as $choice ) {
1614  if ( is_array( $choice ) ) {
1615  $_choices_array = array_merge( $_choices_array, $choice );
1616  } else {
1617  $_choices_array []= $choice;
1618  }
1619  }
1620  $choices = array_unique( $_choices_array );
1621  }
1622 
1623  break;
1624  endswitch;
1625 
1626  $filter_choices = array();
1627  foreach ( $filter['choices'] as $choice ) {
1628  if ( in_array( $choice['text'], $choices, true ) || in_array( $choice['value'], $choices, true ) ) {
1629  $filter_choices[] = $choice;
1630  }
1631  }
1632 
1633  return $filter_choices;
1634  }
1635 
1636  /**
1637  * Calculate the search choices for the users
1638  *
1639  * @param \GV\View $view The view
1640  * @since develop
1641  *
1642  * @since 1.8
1643  *
1644  * @return array Array of user choices (value = ID, text = display name)
1645  */
1646  private static function get_created_by_choices( $view ) {
1647 
1648  /**
1649  * filter gravityview/get_users/search_widget
1650  * @see \GVCommon::get_users
1651  */
1652  $users = GVCommon::get_users( 'search_widget', array( 'fields' => array( 'ID', 'display_name' ) ) );
1653 
1654  $choices = array();
1655  foreach ( $users as $user ) {
1656  /**
1657  * @filter `gravityview/search/created_by/text` Filter the display text in created by search choices
1658  * @since develop
1659  * @param string[in,out] The text. Default: $user->display_name
1660  * @param \WP_User $user The user.
1661  * @param \GV\View $view The view.
1662  */
1663  $text = apply_filters( 'gravityview/search/created_by/text', $user->display_name, $user, $view );
1664  $choices[] = array(
1665  'value' => $user->ID,
1666  'text' => $text,
1667  );
1668  }
1669 
1670  return $choices;
1671  }
1672 
1673  /**
1674  * Calculate the search checkbox choices for approval status
1675  *
1676  * @since develop
1677  *
1678  * @return array Array of approval status choices (value = status, text = display name)
1679  */
1680  private static function get_is_approved_choices() {
1681 
1682  $choices = array();
1683  foreach ( GravityView_Entry_Approval_Status::get_all() as $status ) {
1684  $choices[] = array(
1685  'value' => $status['value'],
1686  'text' => $status['label'],
1687  );
1688  }
1689 
1690  return $choices;
1691  }
1692 
1693  /**
1694  * Output the Clear Search Results button
1695  * @since 1.5.4
1696  */
1697  public static function the_clear_search_button() {
1699 
1700  if ( $gravityview_view->search_clear ) {
1701 
1702  $url = strtok( add_query_arg( array() ), '?' );
1703 
1704  echo gravityview_get_link( $url, esc_html__( 'Clear', 'gravityview' ), 'class=button gv-search-clear' );
1705 
1706  }
1707  }
1708 
1709  /**
1710  * Based on the search method, fetch the value for a specific key
1711  *
1712  * @since 1.16.4
1713  *
1714  * @param string $name Name of the request key to fetch the value for
1715  *
1716  * @return mixed|string Value of request at $name key. Empty string if empty.
1717  */
1718  private function rgget_or_rgpost( $name ) {
1720 
1721  $value = stripslashes_deep( $value );
1722 
1723  $value = gv_map_deep( $value, 'rawurldecode' );
1724 
1725  $value = gv_map_deep( $value, '_wp_specialchars' );
1726 
1727  return $value;
1728  }
1729 
1730 
1731  /**
1732  * Require the datepicker script for the frontend GV script
1733  * @param array $js_dependencies Array of existing required scripts for the fe-views.js script
1734  * @return array Array required scripts, with `jquery-ui-datepicker` added
1735  */
1736  public function add_datepicker_js_dependency( $js_dependencies ) {
1737 
1738  $js_dependencies[] = 'jquery-ui-datepicker';
1739 
1740  return $js_dependencies;
1741  }
1742 
1743  /**
1744  * Modify the array passed to wp_localize_script()
1745  *
1746  * @param array $js_localization The data padded to the Javascript file
1747  * @param array $view_data View data array with View settings
1748  *
1749  * @return array
1750  */
1751  public function add_datepicker_localization( $localizations = array(), $view_data = array() ) {
1752  global $wp_locale;
1753 
1754  /**
1755  * @filter `gravityview_datepicker_settings` Modify the datepicker settings
1756  * @see http://api.jqueryui.com/datepicker/ Learn what settings are available
1757  * @see http://www.renegadetechconsulting.com/tutorials/jquery-datepicker-and-wordpress-i18n Thanks for the helpful information on $wp_locale
1758  * @param array $js_localization The data padded to the Javascript file
1759  * @param array $view_data View data array with View settings
1760  */
1761  $datepicker_settings = apply_filters( 'gravityview_datepicker_settings', array(
1762  'yearRange' => '-5:+5',
1763  'changeMonth' => true,
1764  'changeYear' => true,
1765  'closeText' => esc_attr_x( 'Close', 'Close calendar', 'gravityview' ),
1766  'prevText' => esc_attr_x( 'Prev', 'Previous month in calendar', 'gravityview' ),
1767  'nextText' => esc_attr_x( 'Next', 'Next month in calendar', 'gravityview' ),
1768  'currentText' => esc_attr_x( 'Today', 'Today in calendar', 'gravityview' ),
1769  'weekHeader' => esc_attr_x( 'Week', 'Week in calendar', 'gravityview' ),
1770  'monthStatus' => __( 'Show a different month', 'gravityview' ),
1771  'monthNames' => array_values( $wp_locale->month ),
1772  'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
1773  'dayNames' => array_values( $wp_locale->weekday ),
1774  'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ),
1775  'dayNamesMin' => array_values( $wp_locale->weekday_initial ),
1776  // get the start of week from WP general setting
1777  'firstDay' => get_option( 'start_of_week' ),
1778  // is Right to left language? default is false
1779  'isRTL' => is_rtl(),
1780  ), $view_data );
1781 
1782  $localizations['datepicker'] = $datepicker_settings;
1783 
1784  return $localizations;
1785 
1786  }
1787 
1788  /**
1789  * Register search widget scripts, including Flexibility
1790  *
1791  * @see https://github.com/10up/flexibility
1792  *
1793  * @since 1.17
1794  *
1795  * @return void
1796  */
1797  public function register_scripts() {
1798  wp_register_script( 'gv-flexibility', plugins_url( 'assets/lib/flexibility/flexibility.js', GRAVITYVIEW_FILE ), array(), \GV\Plugin::$version, true );
1799  }
1800 
1801  /**
1802  * If the current visitor is running IE 8 or 9, enqueue Flexibility
1803  *
1804  * @since 1.17
1805  *
1806  * @return void
1807  */
1808  private function maybe_enqueue_flexibility() {
1809  if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && preg_match( '/MSIE [8-9]/', $_SERVER['HTTP_USER_AGENT'] ) ) {
1810  wp_enqueue_script( 'gv-flexibility' );
1811  }
1812  }
1813 
1814  /**
1815  * Enqueue the datepicker script
1816  *
1817  * It sets the $gravityview->datepicker_class parameter
1818  *
1819  * @todo Use own datepicker javascript instead of GF datepicker.js - that way, we can localize the settings and not require the changeMonth and changeYear pickers.
1820  * @return void
1821  */
1822  public function enqueue_datepicker() {
1824 
1825  wp_enqueue_script( 'jquery-ui-datepicker' );
1826 
1827  add_filter( 'gravityview_js_dependencies', array( $this, 'add_datepicker_js_dependency' ) );
1828  add_filter( 'gravityview_js_localization', array( $this, 'add_datepicker_localization' ), 10, 2 );
1829 
1830  $scheme = is_ssl() ? 'https://' : 'http://';
1831  wp_enqueue_style( 'jquery-ui-datepicker', $scheme.'ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/smoothness/jquery-ui.css' );
1832 
1833  /**
1834  * @filter `gravityview_search_datepicker_class`
1835  * Modify the CSS class for the datepicker, used by the CSS class is used by Gravity Forms' javascript to determine the format for the date picker. The `gv-datepicker` class is required by the GravityView datepicker javascript.
1836  * @param string $css_class CSS class to use. Default: `gv-datepicker datepicker mdy` \n
1837  * Options are:
1838  * - `mdy` (mm/dd/yyyy)
1839  * - `dmy` (dd/mm/yyyy)
1840  * - `dmy_dash` (dd-mm-yyyy)
1841  * - `dmy_dot` (dd.mm.yyyy)
1842  * - `ymd_slash` (yyyy/mm/dd)
1843  * - `ymd_dash` (yyyy-mm-dd)
1844  * - `ymd_dot` (yyyy.mm.dd)
1845  */
1846  $datepicker_class = apply_filters( 'gravityview_search_datepicker_class', "gv-datepicker datepicker " . $this->get_datepicker_format() );
1847 
1848  $gravityview_view->datepicker_class = $datepicker_class;
1849  }
1850 
1851  /**
1852  * Retrieve the datepicker format.
1853  *
1854  * @param bool $date_format Whether to return the PHP date format or the datpicker class name. Default: false.
1855  *
1856  * @see https://docs.gravityview.co/article/115-changing-the-format-of-the-search-widgets-date-picker
1857  *
1858  * @return string The datepicker format placeholder, or the PHP date format.
1859  */
1860  private function get_datepicker_format( $date_format = false ) {
1861 
1862  $default_format = 'mdy';
1863 
1864  /**
1865  * @filter `gravityview/widgets/search/datepicker/format`
1866  * @since 2.1.1
1867  * @param string $format Default: mdy
1868  * Options are:
1869  * - `mdy` (mm/dd/yyyy)
1870  * - `dmy` (dd/mm/yyyy)
1871  * - `dmy_dash` (dd-mm-yyyy)
1872  * - `dmy_dot` (dd.mm.yyyy)
1873  * - `ymd_slash` (yyyy/mm/dd)
1874  * - `ymd_dash` (yyyy-mm-dd)
1875  * - `ymd_dot` (yyyy.mm.dd)
1876  */
1877  $format = apply_filters( 'gravityview/widgets/search/datepicker/format', $default_format );
1878 
1879  $gf_date_formats = array(
1880  'mdy' => 'm/d/Y',
1881 
1882  'dmy_dash' => 'd-m-Y',
1883  'dmy_dot' => 'd.m.Y',
1884  'dmy' => 'd/m/Y',
1885 
1886  'ymd_slash' => 'Y/m/d',
1887  'ymd_dash' => 'Y-m-d',
1888  'ymd_dot' => 'Y.m.d',
1889  );
1890 
1891  if ( ! $date_format ) {
1892  // If the format key isn't valid, return default format key
1893  return isset( $gf_date_formats[ $format ] ) ? $format : $default_format;
1894  }
1895 
1896  // If the format key isn't valid, return default format value
1897  return \GV\Utils::get( $gf_date_formats, $format, $gf_date_formats[ $default_format ] );
1898  }
1899 
1900  /**
1901  * If previewing a View or page with embedded Views, make the search work properly by adding hidden fields with query vars
1902  *
1903  * @since 2.2.1
1904  *
1905  * @return void
1906  */
1907  public function add_preview_inputs() {
1908  global $wp;
1909 
1910  if ( ! is_preview() || ! current_user_can( 'publish_gravityviews') ) {
1911  return;
1912  }
1913 
1914  // Outputs `preview` and `post_id` variables
1915  foreach ( $wp->query_vars as $key => $value ) {
1916  printf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $key ), esc_attr( $value ) );
1917  }
1918 
1919  }
1920 
1921  /**
1922  * Get an operator URL override.
1923  *
1924  * @param array $get Where to look for the operator.
1925  * @param string $key The filter key to look for.
1926  * @param array $allowed The allowed operators (whitelist).
1927  * @param string $default The default operator.
1928  *
1929  * @return string The operator.
1930  */
1931  private function get_operator( $get, $key, $allowed, $default ) {
1932  $operator = \GV\Utils::get( $get, "$key|op", $default );
1933 
1934  /**
1935  * @filter `gravityview/search/operator_whitelist` An array of allowed operators for a field.
1936  * @param[in,out] string[] A whitelist of allowed operators.
1937  * @param string The filter name.
1938  */
1939  $allowed = apply_filters( 'gravityview/search/operator_whitelist', $allowed, $key );
1940 
1941  if ( ! in_array( $operator, $allowed, true ) ) {
1942  $operator = $default;
1943  }
1944 
1945  return $operator;
1946  }
1947 
1948 
1949 } // end class
1950 
1952 
1953 if ( ! gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ) {
1954  return;
1955 }
1956 
1957 /**
1958  * A GF_Query condition that allows user data searches.
1959  */
1960 class GravityView_Widget_Search_Author_GF_Query_Condition extends \GF_Query_Condition {
1961  public function __construct( $filter, $view ) {
1962  $this->value = $filter['value'];
1963  $this->view = $view;
1964  }
1965 
1966  public function sql( $query ) {
1967  global $wpdb;
1968 
1969  $user_meta_fields = array(
1970  'nickname', 'first_name', 'last_name',
1971  );
1972 
1973  /**
1974  * @filter `gravityview/widgets/search/created_by/user_meta_fields` Filter the user meta fields to search.
1975  * @param[in,out] array The user meta fields.
1976  * @param \GV\View $view The view.
1977  */
1978  $user_meta_fields = apply_filters( 'gravityview/widgets/search/created_by/user_meta_fields', $user_meta_fields, $this->view );
1979 
1980  $user_fields = array(
1981  'user_nicename', 'user_login', 'display_name', 'user_email',
1982  );
1983 
1984  /**
1985  * @filter `gravityview/widgets/search/created_by/user_fields` Filter the user fields to search.
1986  * @param[in,out] array The user fields.
1987  * @param \GV\View $view The view.
1988  */
1989  $user_fields = apply_filters( 'gravityview/widgets/search/created_by/user_fields', $user_fields, $this->view );
1990 
1991  $conditions = array();
1992 
1993  foreach ( $user_fields as $user_field ) {
1994  $conditions[] = $wpdb->prepare( "`u`.`$user_field` LIKE %s", '%' . $wpdb->esc_like( $this->value ) . '%' );
1995  }
1996 
1997  foreach ( $user_meta_fields as $meta_field ) {
1998  $conditions[] = $wpdb->prepare( "(`um`.`meta_key` = %s AND `um`.`meta_value` LIKE %s)", $meta_field, '%' . $wpdb->esc_like( $this->value ) . '%' );
1999  }
2000 
2001  $conditions = '(' . implode( ' OR ', $conditions ) . ')';
2002 
2003  $alias = $query->_alias( null );
2004 
2005  return "(EXISTS (SELECT 1 FROM $wpdb->users u LEFT JOIN $wpdb->usermeta um ON u.ID = um.user_id WHERE (u.ID = `$alias`.`created_by` AND $conditions)))";
2006  }
2007 }
new GravityView_Widget_Search
rgget_or_rgpost( $name)
Based on the search method, fetch the value for a specific key.
$url
Definition: post_image.php:25
sieve_filter_choices( $filter, $context)
Sieve filter choices to only ones that are used.
static get_field_label( $field, $form_field=array())
Get the label for a search form field.
all()
Get all the settings.
$labels
static get_searchable_fields()
Ajax Returns the form fields ( only the searchable ones )
static _GET( $name, $default=null)
Grab a value from the _GET superglobal or default.
$forms
Definition: data-source.php:19
add_datepicker_localization( $localizations=array(), $view_data=array())
Modify the array passed to wp_localize_script()
static getInstance( $passed_post=NULL)
set_search_method()
Sets the search method to GET (default) or POST.
get_search_method()
Returns the search method.
static get_search_input_labels()
Get labels for different types of search bar inputs.
if(empty( $value)) $user
static _REQUEST( $name, $default=null)
Grab a value from the _REQUEST superglobal or default.
if(gv_empty( $field['value'], false, false)) $format
register_no_conflict( $allowed)
Add admin script to the no-conflict scripts whitelist.
static get_created_by_choices( $view)
Calculate the search choices for the users.
static get_formatted_date( $value='', $format='Y-m-d', $value_format='Y-m-d')
Format a date value.
add_preview_inputs()
If previewing a View or page with embedded Views, make the search work properly by adding hidden fiel...
get_search_filter_details( $field, $context)
Prepare search fields to frontend render with other details (label, field type, searched values) ...
enqueue_datepicker()
Enqueue the datepicker script.
gravityview_get_link( $href='', $anchor_text='', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
static get_search_form_action()
Calculate the search form action.
gravityview()
Definition: _stubs.php:26
get( $key, $default=null)
Retrieve a setting.
gravityview_get_input_id_from_id( $field_id='')
Very commonly needed: get the # of the input based on a full field ID.
static by_id( $form, $field_id)
Get a by and Field ID.
If this file is called directly, abort.
render_frontend( $widget_args, $content='', $context='')
Renders the Search Widget.
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
static get_search_input_label( $input_type)
static get_date_field_format(GF_Field_Date $field)
Get the Field Format form GravityForms.
gv_map_deep( $value, $callback)
Maps a function to all non-iterable elements of an array or an object.
has_date_field( $search_fields)
Check whether the configured search fields have a date field.
gravityview_get_field( $form, $field_id)
Returns the field details array of a specific form given the field id.
gravityview_get_form_fields( $form='', $add_default_properties=false, $include_parent_field=true)
Return array of fields&#39; id and label, for a given Form ID.
static pre_get_form_fields( $template_id='')
Get the the form fields for a preset (no form created yet)
Definition: class-ajax.php:317
$search_method
whether search method is GET or POST ( default: GET )
register_scripts()
Register search widget scripts, including Flexibility.
add_scripts_and_styles( $hook)
Add script to Views edit screen (admin)
static by_id( $post_id)
Construct a instance from a post ID.
static get_users( $context='change_entry_creator', $args=array())
Get WordPress users with reasonable limits set.
is_registered()
Whether this Widget&#39;s been registered already or not.
gravityview_get_form_id( $view_id)
Get the connected form ID from a View ID.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:31
$field_id
Definition: time.php:17
static get_search_class( $custom_class='')
Get the search class for a search form.
gf_query_filter(&$query, $view, $request)
Filters the with advanced logic.
maybe_enqueue_flexibility()
If the current visitor is running IE 8 or 9, enqueue Flexibility.
add_datepicker_js_dependency( $js_dependencies)
Require the datepicker script for the frontend GV script.
static get_search_input_types( $field_id='', $field_type=null)
Assign an input type according to the form field type.
static by_id( $field_id)
Get a from an internal Gravity Forms field ID.
if(empty( $created_by)) $form_id
gravityview_get_terms_choices( $args=array())
Get categories formatted in a way used by GravityView and Gravity Forms input choices.
const UNAPPROVED
get_datepicker_format( $date_format=false)
Retrieve the datepicker format.
prepare_field_filter( $filter_key, $value, $view, $searchable_fields, $get=array())
Prepare the field filters to GFAPI.
convert_request_key_to_filter_key( $key)
Convert $_GET/$_POST key to the field/meta ID.
filter_entries( $search_criteria, $form_id=null, $args=array(), $force_search_criteria=false)
— Frontend —
get_operator( $get, $key, $allowed, $default)
Get an operator URL override.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
A GF_Query condition that allows user data searches.
static render_searchable_fields( $form_id=null, $current='')
Generates html for the available Search Fields dropdown.
gv_empty( $value, $zero_is_empty=true, $allow_string_booleans=true)
Is the value empty?
static get_all()
Return array of status options.
static get_is_approved_choices()
Calculate the search checkbox choices for approval status.
gravityview_get_permalink_query_args( $id=0)
Get get_permalink() without the home_url() prepended to it.
add_no_permalink_fields( $search_fields, $object, $widget_args=array())
Display hidden fields to add support for sites using Default permalink structure. ...
add_template_path( $file_paths)
Include this extension templates path.
get_view_searchable_fields( $view, $with_full_field=false)
Get the fields that are searchable for a View.
$field
Definition: gquiz_grade.php:11
static the_clear_search_button()
Output the Clear Search Results button.
static get_input_types_by_field_type()
Get the input types available for different field types.
static getInstance()
Get the one true instantiated self.