12 if ( ! defined(
'WPINC' ) ) {
18 public $icon =
'dashicons-search';
34 $this->widget_id =
'search_bar';
35 $this->widget_description =
'';
36 $this->widget_subtitle = esc_html__(
'Search form for searching entries.',
'gk-gravityview' );
38 self::$instance = &$this;
40 self::$file = plugin_dir_path( __FILE__ );
42 $default_values = array(
'header' => 0,
'footer' => 0 );
45 'search_layout' => array(
48 'label' => esc_html__(
'Search Layout',
'gk-gravityview' ),
49 'value' =>
'horizontal',
51 'horizontal' => esc_html__(
'Horizontal',
'gk-gravityview' ),
52 'vertical' => esc_html__(
'Vertical',
'gk-gravityview' ),
55 'search_clear' => array(
57 'label' => __(
'Show Clear button',
'gk-gravityview' ),
58 'desc' => __(
'When a search is performed, display a button that removes all search values.',
'gk-gravityview'),
61 'search_fields' => array(
64 'class' =>
'gv-search-fields-value',
65 'value' =>
'[{"field":"search_all","input":"input_text"}]',
67 'search_mode' => array(
70 'label' => esc_html__(
'Search Mode',
'gk-gravityview' ),
71 'desc' => __(
'Should search results match all search fields, or any?',
'gk-gravityview'),
73 'class' =>
'hide-if-js',
75 'any' => esc_html__(
'Match Any Fields',
'gk-gravityview' ),
76 'all' => esc_html__(
'Match All Fields',
'gk-gravityview' ),
79 'sieve_choices' => array(
82 'label' => esc_html__(
'Pre-Filter Choices',
'gk-gravityview' ),
85 esc_html__(
'For fields with choices: Instead of showing all choices for each field, show only field choices that exist in submitted form entries.',
'gk-gravityview' ) .
86 '<p><strong>⚠️ ' . esc_html__(
'This setting affects security.',
'gk-gravityview' ) .
'</strong> ' . esc_html__(
'[link]Learn about the Pre-Filter Choices setting[/link] before enabling it.',
'gk-gravityview') .
'</p>',
89 '[/b]' =>
'</strong>',
90 '[link]' =>
'<a href="https://docs.gravitykit.com/article/701-s" target="_blank" rel="external noopener nofollower" title="' . esc_attr__(
'This link opens in a new window.',
'gk-gravityview' ) .
'">',
95 'class' =>
'hide-if-js',
97 '0' => esc_html__(
'Show all field choices',
'gk-gravityview' ),
98 '1' => esc_html__(
'Only show choices that exist in form entries',
'gk-gravityview' ),
105 add_filter(
'gravityview_fe_search_criteria', array( $this,
'filter_entries' ), 10, 3 );
108 add_filter(
'gravityview_template_paths', array( $this,
'add_template_path' ) );
111 add_action(
'admin_enqueue_scripts', array( $this,
'add_scripts_and_styles' ), 1100 );
112 add_action(
'wp_enqueue_scripts', array( $this,
'register_scripts') );
113 add_filter(
'gravityview_noconflict_scripts', array( $this,
'register_no_conflict' ) );
116 add_action(
'wp_ajax_gv_searchable_fields', array(
'GravityView_Widget_Search',
'get_searchable_fields' ) );
118 add_action(
'gravityview_search_widget_fields_after', array( $this,
'add_preview_inputs' ) );
120 add_filter(
'gravityview/api/reserved_query_args', array( $this,
'add_reserved_args' ) );
123 add_filter(
'gravityview_widget_search_filters', array( $this,
'maybe_sieve_filter_choices' ), 1000, 4 );
126 parent::__construct( esc_html__(
'Search Bar',
'gk-gravityview' ), null, $default_values,
$settings );
136 if ( empty( self::$instance ) ) {
139 return self::$instance;
151 $args[] =
'gv_search';
152 $args[] =
'gv_start';
158 $get = (array) $_GET;
161 foreach ( $get as $key =>
$value ) {
181 $method = apply_filters(
'gravityview/search/method', $this->search_method );
183 $method = strtolower( $method );
185 $this->search_method = in_array( $method, array(
'get',
'post' ) ) ? $method :
'get';
209 $input_types = array(
210 'text' => array(
'input_text' ),
211 'address' => array(
'input_text' ),
212 'number' => array(
'input_text' ),
213 'date' => array(
'date',
'date_range' ),
214 'boolean' => array(
'single_checkbox' ),
215 'select' => array(
'select',
'radio',
'link' ),
216 'multi' => array(
'select',
'multiselect',
'radio',
'checkbox',
'link' ),
219 'created_by' => array(
'select',
'radio',
'checkbox',
'multiselect',
'link',
'input_text' ),
220 'product' => array(
'select',
'radio',
'link',
'input_text' ),
228 $input_types = apply_filters(
'gravityview/search/input_types', $input_types );
245 $input_labels = array(
246 'input_text' => esc_html__(
'Text',
'gk-gravityview' ),
247 'date' => esc_html__(
'Date',
'gk-gravityview' ),
248 'select' => esc_html__(
'Select',
'gk-gravityview' ),
249 'multiselect' => esc_html__(
'Select (multiple values)',
'gk-gravityview' ),
250 'radio' => esc_html__(
'Radio',
'gk-gravityview' ),
251 'checkbox' => esc_html__(
'Checkbox',
'gk-gravityview' ),
252 'single_checkbox' => esc_html__(
'Checkbox',
'gk-gravityview' ),
253 'link' => esc_html__(
'Links',
'gk-gravityview' ),
254 'date_range' => esc_html__(
'Date range',
'gk-gravityview' ),
261 $input_labels = apply_filters(
'gravityview/search/input_labels', $input_labels );
263 return $input_labels;
267 $labels = self::get_search_input_labels();
269 return \GV\Utils::get(
$labels, $input_type,
false );
280 if ( !
gravityview()->request->is_admin( $hook,
'single' ) && (
'widgets.php' !== $pagenow ) ) {
284 $script_min = ( defined(
'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ?
'' :
'.min';
285 $script_source = empty( $script_min ) ?
'/source' :
'';
287 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 );
289 wp_localize_script(
'gravityview_searchwidget_admin',
'gvSearchVar', array(
290 'nonce' => wp_create_nonce(
'gravityview_ajaxsearchwidget' ),
291 'label_nofields' => esc_html__(
'No search fields configured yet.',
'gk-gravityview' ),
292 'label_addfield' => esc_html__(
'Add Search Field',
'gk-gravityview' ),
293 'label_label' => esc_html__(
'Label',
'gk-gravityview' ),
294 'label_searchfield' => esc_html__(
'Search Field',
'gk-gravityview' ),
295 'label_inputtype' => esc_html__(
'Input Type',
'gk-gravityview' ),
296 'label_ajaxerror' => esc_html__(
'There was an error loading searchable fields. Save the View or refresh the page to fix this issue.',
'gk-gravityview' ),
297 'input_labels' => json_encode( self::get_search_input_labels() ),
298 'input_types' => json_encode( self::get_input_types_by_field_type() ),
309 $allowed[] =
'gravityview_searchwidget_admin';
321 if ( ! isset( $_POST[
'nonce'] ) || ! wp_verify_nonce( $_POST[
'nonce'],
'gravityview_ajaxsearchwidget' ) ) {
328 if ( ! empty( $_POST[
'view_id'] ) ) {
332 } elseif ( ! empty( $_POST[
'formid'] ) ) {
334 $form = (int) $_POST[
'formid'];
336 } elseif ( ! empty( $_POST[
'template_id'] ) && class_exists(
'GravityView_Ajax' ) ) {
343 $response = self::render_searchable_fields(
$form );
362 $output =
'<select class="gv-search-fields">';
364 $custom_fields = array(
365 'search_all' => array(
366 'text' => esc_html__(
'Search Everything',
'gk-gravityview' ),
369 'entry_date' => array(
370 'text' => esc_html__(
'Entry Date',
'gk-gravityview' ),
374 'text' => esc_html__(
'Entry ID',
'gk-gravityview' ),
377 'created_by' => array(
378 'text' => esc_html__(
'Entry Creator',
'gk-gravityview' ),
379 'type' =>
'created_by',
381 'is_starred' => array(
382 'text' => esc_html__(
'Is Starred',
'gk-gravityview' ),
387 if (
gravityview()->plugin->supports( \
GV\Plugin::FEATURE_GFQUERY ) ) {
388 $custom_fields[
'is_approved'] = array(
389 'text' => esc_html__(
'Approval Status',
'gk-gravityview' ),
394 foreach( $custom_fields as $custom_field_key => $custom_field ) {
395 $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'] );
409 $fields = apply_filters(
'gravityview/search/searchable_fields', $fields,
$form_id );
411 if ( ! empty( $fields ) ) {
413 $blocklist_field_types = apply_filters(
'gravityview_blocklist_field_types', array(
'fileupload',
'post_image',
'post_id',
'section' ), null );
415 foreach ( $fields as $id =>
$field ) {
417 if ( in_array(
$field[
'type'], $blocklist_field_types ) ) {
421 $types = self::get_search_input_types( $id,
$field[
'type'] );
423 $output .=
'<option value="'. $id .
'" '. selected( $id, $current,
false ).
'data-inputtypes="'. esc_attr( $types ) .
'">'. esc_html(
$field[
'label'] ) .
'</option>';
446 if (
false !== strpos( (
string)
$field_id,
'.' ) && in_array( $field_type, array(
'checkbox' ) ) || in_array( $field_id, array(
'is_fulfilled' ) ) ) {
447 $input_type =
'boolean';
448 } elseif ( in_array( $field_type, array(
'checkbox',
'post_category',
'multiselect' ) ) ) {
449 $input_type =
'multi';
450 } elseif ( in_array( $field_type, array(
'select',
'radio' ) ) ) {
451 $input_type =
'select';
452 } elseif ( in_array( $field_type, array(
'date' ) ) || in_array( $field_id, array(
'payment_date' ) ) ) {
453 $input_type =
'date';
454 } elseif ( in_array( $field_type, array(
'number' ) ) || in_array( $field_id, array(
'payment_amount' ) ) ) {
455 $input_type =
'number';
456 } elseif ( in_array( $field_type, array(
'product' ) ) ) {
457 $input_type =
'product';
459 $input_type =
'text';
470 $input_type = apply_filters(
'gravityview/extension/search/input_type', $input_type, $field_type, $field_id );
486 if (
false === $wp_rewrite->using_permalinks() ) {
492 if ( ! empty( $widget_args[
'post_id'] ) ) {
493 $post_id = absint( $widget_args[
'post_id'] );
496 else if ( ! empty( $widget_args[
'view_id'] ) ) {
497 $post_id = absint( $widget_args[
'view_id'] );
504 $search_fields[] = array(
512 return $search_fields;
534 $searchable_fields = array();
537 return $searchable_fields;
543 $widgets = (array) get_option(
'widget_gravityview_search', array() );
545 foreach ( $widgets as $widget ) {
546 if ( ! empty( $widget[
'view_id'] ) && $widget[
'view_id'] == $view->ID ) {
547 if( $_fields = json_decode( $widget[
'search_fields'],
true ) ) {
548 foreach ( $_fields as
$field ) {
549 if ( empty( $field[
'form_id'] ) ) {
550 $field[
'form_id'] = $view->form ? $view->form->ID : 0;
552 $searchable_fields[] = $with_full_field ?
$field : $field[
'field'];
558 foreach ( $view->widgets->by_id( $this->get_widget_id() )->all() as $widget ) {
559 if( $_fields = json_decode( $widget->configuration->get(
'search_fields' ), true ) ) {
560 foreach ( $_fields as
$field ) {
561 if ( empty( $field[
'form_id'] ) ) {
562 $field[
'form_id'] = $view->form ? $view->form->ID : 0;
564 $searchable_fields[] = $with_full_field ?
$field : $field[
'field'];
573 $searchable_fields = apply_filters_deprecated(
'gravityview/search/searchable_fields/whitelist', array( $searchable_fields, $view, $with_full_field ),
'2.14',
'gravityview/search/searchable_fields/allowlist' );
583 $searchable_fields = apply_filters(
'gravityview/search/searchable_fields/allowlist', $searchable_fields, $view, $with_full_field );
585 return $searchable_fields;
601 if ( ! $force_search_criteria &&
gravityview()->plugin->supports( \
GV\Plugin::FEATURE_GFQUERY ) ) {
606 add_action(
'gravityview/view/query', array( $this,
'gf_query_filter' ), 10, 3 );
607 return $search_criteria;
610 if(
'post' === $this->search_method ) {
617 $view_id = $view ? $view->ID : null;
618 $form_id = $view ? $view->form->ID : null;
620 gravityview()->log->debug(
'Requested $_{method}: ', array(
'method' => $this->search_method,
'data' => $get ) );
622 if ( empty( $get ) || ! is_array( $get ) ) {
623 return $search_criteria;
626 $get = stripslashes_deep( $get );
628 if ( ! is_null( $get ) ) {
633 $search_criteria[
'field_filters'] =
\GV\Utils::get( $search_criteria,
'field_filters', array() );
643 $split_words = apply_filters(
'gravityview/search-all-split-words',
true );
650 $trim_search_value = apply_filters(
'gravityview/search-trim-input',
true );
653 if ( isset( $get[
'gv_search'] ) &&
'' !== $get[
'gv_search'] && in_array(
'search_all', $searchable_fields ) ) {
655 $search_all_value = $trim_search_value ? trim( $get[
'gv_search'] ) : $get[
'gv_search'];
657 if ( $split_words ) {
659 $words = explode(
' ', $search_all_value );
661 $words = array_filter( $words );
665 $search_all_value = preg_replace(
'/\s+/ism',
' ', $search_all_value );
667 $words = array( $search_all_value );
670 foreach ( $words as $word ) {
671 $search_criteria[
'field_filters'][] = array(
674 'operator' =>
'contains',
680 if ( in_array(
'entry_date', $searchable_fields ) ) {
684 if ( $curr_start = ! empty( $get[
'gv_start'] ) ? $get[
'gv_start'] :
'' ) {
685 if( $curr_start_date = date_create_from_format( $this->
get_datepicker_format(
true ), $curr_start ) ) {
686 $curr_start = $curr_start_date->format(
'Y-m-d' );
690 if ( $curr_end = ! empty( $get[
'gv_start'] ) ? ( ! empty( $get[
'gv_end'] ) ? $get[
'gv_end'] :
'' ) :
'' ) {
692 $curr_end = $curr_end_date->format(
'Y-m-d' );
700 if ( $start_date = $view->settings->get(
'start_date' ) ) {
701 if ( $start_timestamp = strtotime( $curr_start ) ) {
702 $curr_start = $start_timestamp < strtotime( $start_date ) ? $start_date : $curr_start;
705 if ( $end_date = $view->settings->get(
'end_date' ) ) {
706 if ( $end_timestamp = strtotime( $curr_end ) ) {
707 $curr_end = $end_timestamp > strtotime( $end_date ) ? $end_date : $curr_end;
720 $adjust_tz = apply_filters(
'gravityview_date_created_adjust_timezone',
false,
'search' );
725 if ( ! empty( $curr_start ) ) {
726 $curr_start = date(
'Y-m-d H:i:s', strtotime( $curr_start ) );
727 $search_criteria[
'start_date'] = $adjust_tz ? get_gmt_from_date( $curr_start ) : $curr_start;
730 if ( ! empty( $curr_end ) ) {
732 $curr_end = date(
'Y-m-d H:i:s', strtotime( $curr_end ) + DAY_IN_SECONDS );
733 $search_criteria[
'end_date'] = $adjust_tz ? get_gmt_from_date( $curr_end ) : $curr_end;
734 if ( strpos( $search_criteria[
'end_date'],
'00:00:00' ) ) {
735 $search_criteria[
'end_date'] = date(
'Y-m-d H:i:s', strtotime( $search_criteria[
'end_date'] ) - 1 );
741 if ( ! empty( $get[
'gv_id' ] ) && in_array(
'entry_id', $searchable_fields ) ) {
742 $search_criteria[
'field_filters'][] = array(
744 'value' => absint( $get[
'gv_id' ] ),
745 'operator' => $this->
get_operator( $get,
'gv_id', array(
'=' ),
'=' ),
750 if ( ! empty( $get[
'gv_by' ] ) && in_array(
'created_by', $searchable_fields ) ) {
751 $search_criteria[
'field_filters'][] = array(
752 'key' =>
'created_by',
753 'value' => $get[
'gv_by'],
754 'operator' => $this->
get_operator( $get,
'gv_by', array(
'=' ),
'=' ),
759 $mode = isset( $get[
'mode'] ) && in_array( $get[
'mode'], array(
'any',
'all' ) ) ? $get[
'mode'] :
'any';
762 foreach ( $get as $key =>
$value ) {
763 if ( 0 !== strpos( $key,
'filter_' ) && 0 !== strpos( $key,
'input_' ) ) {
767 if ( strpos( $key,
'|op' ) !==
false ) {
773 if ( $trim_search_value ) {
786 $ignore_empty_values = apply_filters(
'gravityview/search/ignore-empty-values',
true, $filter_key,
$view_id,
$form_id );
788 if ( is_array(
$value ) || $ignore_empty_values ) {
800 $value =
'-' . PHP_INT_MAX;
808 if ( ! isset( $filter[
'operator'] ) ) {
809 $filter[
'operator'] = $this->
get_operator( $get, $key, array(
'contains' ),
'contains' );
812 if ( isset( $filter[0][
'value'] ) ) {
813 $filter[0][
'value'] = $trim_search_value ? trim( $filter[0][
'value'] ) : $filter[0][
'value'];
815 $search_criteria[
'field_filters'] = array_merge( $search_criteria[
'field_filters'], $filter );
818 if ( ! empty( $filter[0][
'operator'] ) && in_array( $filter[0][
'operator'], array(
'>=',
'<=',
'>',
'<' ) ) ) {
821 } elseif( !empty( $filter ) ) {
822 $search_criteria[
'field_filters'][] = $filter;
831 $search_criteria[
'field_filters'][
'mode'] = apply_filters(
'gravityview/search/mode', $mode );
833 gravityview()->log->debug(
'Returned Search Criteria: ', array(
'data' => $search_criteria ) );
837 return $search_criteria;
854 $search_criteria = $this->
filter_entries( array(), null, array(
'id' => $view->ID ),
true );
859 remove_filter(
'gravityview_fe_search_criteria', array( $this,
'filter_entries' ), 10, 3 );
860 $search_criteria = apply_filters(
'gravityview_fe_search_criteria', $search_criteria, $view->form->ID, $view->settings->as_atts() );
861 add_filter(
'gravityview_fe_search_criteria', array( $this,
'filter_entries' ), 10, 3 );
863 $query_class = $view->get_query_class();
865 if ( empty( $search_criteria[
'field_filters'] ) ) {
869 $widgets = $view->widgets->by_id( $this->widget_id );
870 if ( $widgets->count() ) {
871 $widgets = $widgets->all();
872 $widget = $widgets[0];
874 $search_fields = json_decode( $widget->configuration->get(
'search_fields' ), true );
878 $created_by_text_mode =
true;
883 $extra_conditions = array();
886 foreach ( $search_criteria[
'field_filters'] as $key => &$filter ) {
887 if ( ! is_array( $filter ) ) {
888 if ( in_array( strtolower( $filter ), array(
'any',
'all' ) ) ) {
896 $_tmp_query =
new $query_class( $view->form->ID, array(
897 'field_filters' => array(
900 'key' =>
'is_approved',
901 'value' => (array) $filter[
'value'],
905 'key' =>
'is_approved',
911 $_tmp_query_parts = $_tmp_query->_introspect();
913 $extra_conditions[] = $_tmp_query_parts[
'where'];
920 if (
'created_by' === $filter[
'key'] && ! empty( $created_by_text_mode ) ) {
927 $filter[
'operator'] = empty( $filter[
'operator'] ) ?
'contains' : $filter[
'operator'];
930 if ( is_array( $filter[
'value'] ) ) {
931 $filter[
'operator'] =
'in';
935 if ( empty( $filter[
'form_id'] ) ) {
936 $filter[
'form_id'] = $view->form ? $view->form->ID : 0;
946 $filter[
'operator'] = apply_filters(
'gravityview_search_operator', $filter[
'operator'], $filter, $view );
948 if (
'is' !== $filter[
'operator'] &&
'' === $filter[
'value'] ) {
949 unset( $search_criteria[
'field_filters'][ $key ] );
953 if ( ! empty( $search_criteria[
'start_date'] ) || ! empty( $search_criteria[
'end_date'] ) ) {
954 $date_criteria = array();
956 if ( isset( $search_criteria[
'start_date'] ) ) {
957 $date_criteria[
'start_date'] = $search_criteria[
'start_date'];
960 if ( isset( $search_criteria[
'end_date'] ) ) {
961 $date_criteria[
'end_date'] = $search_criteria[
'end_date'];
964 $_tmp_query =
new $query_class( $view->form->ID, $date_criteria );
965 $_tmp_query_parts = $_tmp_query->_introspect();
966 $extra_conditions[] = $_tmp_query_parts[
'where'];
969 $search_conditions = array();
971 if ( $filters = array_filter( $search_criteria[
'field_filters'] ) ) {
972 foreach ( $filters as &$filter ) {
973 if ( ! is_array( $filter ) ) {
983 $_tmp_query =
new $query_class( $filter[
'form_id'], array(
'mode' =>
'any',
'field_filters' => array( $filter ) ) );
984 $_tmp_query_parts = $_tmp_query->_introspect();
985 $search_condition = $_tmp_query_parts[
'where'];
987 if ( empty( $filter[
'key'] ) && $search_condition->expressions ) {
988 $search_conditions[] = $search_condition;
990 $left = $search_condition->left;
993 if ( $left instanceof GF_Query_Call ) {
995 $reflectionProperty = new \ReflectionProperty( $left,
'_parameters' );
996 $reflectionProperty->setAccessible(
true );
998 $value = $reflectionProperty->getValue( $left );
1000 if ( ! empty(
$value[0] ) &&
$value[0] instanceof GF_Query_Column ) {
1005 }
catch ( ReflectionException $e ) {
1010 $alias = $query->_alias( $left->field_id, $left->source, $left->is_entry_column() ?
't' :
'm' );
1012 if ( $view->joins && $left->field_id == GF_Query_Column::META ) {
1013 foreach ( $view->joins as $_join ) {
1014 $on = $_join->join_on;
1015 $join = $_join->join;
1017 $search_conditions[] = GF_Query_Condition::_or(
1019 new GF_Query_Condition(
1020 new GF_Query_Column( GF_Query_Column::META, $join->ID, $query->_alias( GF_Query_Column::META, $join->ID,
'm' ) ),
1021 $search_condition->operator,
1022 $search_condition->right
1025 new GF_Query_Condition(
1026 new GF_Query_Column( GF_Query_Column::META, $on->ID, $query->_alias( GF_Query_Column::META, $on->ID,
'm' ) ),
1027 $search_condition->operator,
1028 $search_condition->right
1033 $search_conditions[] =
new GF_Query_Condition(
1034 new GF_Query_Column( $left->field_id, $left->source, $alias ),
1035 $search_condition->operator,
1036 $search_condition->right
1042 if ( $search_conditions ) {
1043 $search_conditions = array( call_user_func_array(
'\GF_Query_Condition::' . ( $mode ==
'all' ?
'_and' :
'_or' ), $search_conditions ) );
1050 $query_parts = $query->_introspect();
1055 $where = call_user_func_array(
'\GF_Query_Condition::_and', array_merge( array( $query_parts[
'where'] ), $search_conditions, $extra_conditions ) );
1056 $query->where( $where );
1075 $field_id = str_replace( array(
'filter_',
'input_' ),
'', $key );
1078 if ( preg_match(
'/^[0-9_]+$/ism',
$field_id ) ) {
1105 $filter_key = explode(
':', $filter_key );
1109 if ( count( $filter_key ) > 1 ) {
1113 if (
$forms = \
GV\View::get_joined_forms( $view->ID ) ) {
1134 foreach ( $searchable_fields as
$field ) {
1135 if (
$field_id == $field[
'field'] && $form->ID == $field[
'form_id'] ) {
1146 $searchable_fields = wp_list_pluck( $searchable_fields,
'field' );
1147 if ( ! in_array(
'search_all', $searchable_fields ) && ! in_array(
$field_id, $searchable_fields ) ) {
1154 $form = $view->form;
1160 if ( ! $form_field ) {
1168 'form_id' =>
$form->ID,
1171 switch ( $form_field->type ) {
1175 $filter[
'operator'] = $this->
get_operator( $get, $key, array(
'is' ),
'is' );
1178 case 'post_category':
1180 if ( ! is_array(
$value ) ) {
1187 foreach (
$value as $val ) {
1188 $cat = get_term( $val,
'category' );
1191 'value' => esc_attr( $cat->name ) .
':' . $val,
1192 'operator' => $this->get_operator( $get, $key, array(
'is' ),
'is' ),
1200 if ( ! is_array(
$value ) ) {
1207 foreach (
$value as $val ) {
1208 $filter[] = array(
'key' =>
$field_id,
'value' => $val );
1215 if (
false !== strpos(
$field_id,
'.' ) && ! empty( $form_field->inputs ) && ! empty( $form_field->choices ) ) {
1216 foreach ( $form_field->inputs as $k => $input ) {
1218 $filter[
'value'] = $form_field->choices[ $k ][
'value'];
1219 $filter[
'operator'] = $this->
get_operator( $get, $key, array(
'is' ),
'is' );
1223 } elseif ( is_array(
$value ) ) {
1228 foreach (
$value as $val ) {
1232 'operator' => $this->
get_operator( $get, $key, array(
'is' ),
'is' ),
1242 if (
false === strpos(
$field_id,
'.' ) ) {
1244 $words = explode(
' ',
$value );
1247 foreach ( $words as $word ) {
1248 if ( ! empty( $word ) && strlen( $word ) > 1 ) {
1250 $filter[
'value'] = $word;
1252 $filters[] = $filter;
1260 if (
'address' === $form_field->field->type ) {
1264 foreach ( $searchable_fields as $searchable_field ) {
1266 if( $form_field->ID !== $searchable_field[
'field'] ) {
1271 if( in_array( $searchable_field[
'input'], array(
'text',
'search' ),
true ) ) {
1278 $filter[
'operator'] = $this->
get_operator( $get, $key, array(
'is' ),
'is' );
1285 case 'payment_date':
1290 if ( is_array(
$value ) ) {
1295 foreach (
$value as $k => $date ) {
1296 if ( empty( $date ) ) {
1299 $operator =
'start' === $k ?
'>=' :
'<=';
1306 if( !GFFormsModel::is_valid_operator( $operator ) && $operator ===
'<=' ) {
1308 $date = date(
'Y-m-d', strtotime( self::get_formatted_date( $date,
'Y-m-d', $date_format ) .
' +1 day' ) );
1313 'value' => self::get_formatted_date( $date,
'Y-m-d', $date_format ),
1314 'operator' => $this->
get_operator( $get, $key, array( $operator ), $operator ),
1319 $filter[
'value'] = self::get_formatted_date( $date,
'Y-m-d', $date_format );
1320 $filter[
'operator'] = $this->
get_operator( $get, $key, array(
'is' ),
'is' );
1323 if (
'payment_date' === $key) {
1324 $filter[
'operator'] =
'contains';
1343 $datepicker = array(
1346 'dmy_dash' =>
'd-m-Y',
1347 'dmy_dot' =>
'd.m.Y',
1348 'ymd_slash' =>
'Y/m/d',
1349 'ymd_dash' =>
'Y-m-d',
1350 'ymd_dot' =>
'Y.m.d',
1353 if ( ! empty( $field->dateFormat ) && isset( $datepicker[ $field->dateFormat ] ) ){
1354 $format = $datepicker[ $field->dateFormat ];
1373 $date = date_create_from_format( $value_format,
$value );
1375 if ( empty( $date ) ) {
1376 gravityview()->log->debug(
'Date format not valid: {value}', array(
'value' =>
$value ) );
1379 return $date->format(
$format );
1390 $file_paths[102] = self::$file .
'templates/';
1408 foreach ( $search_fields as $k =>
$field ) {
1409 if ( in_array(
$field[
'input'], array(
'date',
'date_range',
'entry_date' ) ) ) {
1431 gravityview()->log->debug(
'$gravityview_view not instantiated yet.' );
1438 $search_fields = ! empty( $widget_args[
'search_fields'] ) ? json_decode( $widget_args[
'search_fields'],
true ) :
'';
1440 if ( empty( $search_fields ) || ! is_array( $search_fields ) ) {
1441 gravityview()->log->debug(
'No search fields configured for widget:', array(
'data' => $widget_args ) );
1446 foreach ( $search_fields as $k =>
$field ) {
1452 switch (
$field[
'field'] ) {
1455 $updated_field[
'key'] =
'search_all';
1456 $updated_field[
'input'] =
'search_all';
1461 $updated_field[
'key'] =
'entry_date';
1462 $updated_field[
'input'] =
'entry_date';
1463 $updated_field[
'value'] = array(
1470 $updated_field[
'key'] =
'entry_id';
1471 $updated_field[
'input'] =
'entry_id';
1476 $updated_field[
'key'] =
'created_by';
1477 $updated_field[
'name'] =
'gv_by';
1482 $updated_field[
'key'] =
'is_approved';
1483 $updated_field[
'value'] = $this->
rgget_or_rgpost(
'filter_is_approved' );
1484 $updated_field[
'choices'] = self::get_is_approved_choices();
1488 $search_fields[ $k ] = $updated_field;
1491 gravityview()->log->debug(
'Calculated Search Fields: ', array(
'data' => $search_fields ) );
1501 $gravityview_view->search_fields = apply_filters(
'gravityview_widget_search_filters', $search_fields, $this, $widget_args, $context );
1505 $gravityview_view->search_layout = ! empty( $widget_args[
'search_layout'] ) ? $widget_args[
'search_layout'] :
'horizontal';
1508 $gravityview_view->search_mode = ! empty( $widget_args[
'search_mode'] ) ? $widget_args[
'search_mode'] :
'any';
1510 $custom_class = ! empty( $widget_args[
'custom_class'] ) ? $widget_args[
'custom_class'] :
'';
1514 $gravityview_view->search_clear = ! empty( $widget_args[
'search_clear'] ) ? $widget_args[
'search_clear'] :
false;
1536 $search_class =
'gv-search-'.$gravityview_view->search_layout;
1538 if ( ! empty( $custom_class ) ) {
1539 $search_class .=
' '.$custom_class;
1546 $search_class = apply_filters(
'gravityview_search_class', $search_class );
1551 return gravityview_sanitize_html_class( $search_class );
1566 $url = add_query_arg( array(), get_permalink( $post_id ) );
1575 return apply_filters(
'gravityview/widget/search/form/action',
$url );
1590 $label = isset( $form_field[
'label'] ) ? $form_field[
'label'] :
'';
1592 switch(
$field[
'field'] ) {
1594 $label = __(
'Search Entries:',
'gk-gravityview' );
1597 $label = __(
'Filter by date:',
'gk-gravityview' );
1600 $label = __(
'Entry ID:',
'gk-gravityview' );
1604 if ( strpos(
$field[
'field'],
'.' ) > 0 && ! empty( $form_field[
'inputs'] ) ) {
1607 $items = wp_list_filter( $form_field[
'inputs'], array(
'id' =>
$field[
'field'] ) );
1610 $values = wp_list_pluck( $items,
'label' );
1613 foreach ( $values as
$value ) {
1628 $label = apply_filters(
'gravityview_search_field_label', esc_attr(
$label ), $form_field,
$field );
1651 $name =
'filter_' . str_replace(
'.',
'_',
$field[
'field'] );
1662 'key' => \
GV\Utils::get(
$field,
'field' ),
1664 'label' => self::get_field_label(
$field, $form_field ),
1665 'input' => \
GV\Utils::get(
$field,
'input' ),
1667 'type' => $form_field_type,
1671 if (
'post_category' === $form_field_type && ! empty( $form_field[
'displayAllCategories'] ) && empty( $form_field[
'choices'] ) ) {
1673 } elseif ( ! empty( $form_field[
'choices'] ) ) {
1674 $filter[
'choices'] = $form_field[
'choices'];
1677 if (
'date_range' ===
$field[
'input'] && empty(
$value ) ) {
1678 $filter[
'value'] = array(
'start' =>
'',
'end' =>
'' );
1681 if (
'created_by' ===
$field[
'field'] ) {
1682 $filter[
'choices'] = self::get_created_by_choices( ( isset( $context->view ) ? $context->view : null ) );
1683 $filter[
'type'] =
'created_by';
1693 return apply_filters(
'gravityview/search/filter_details', $filter,
$field, $context );
1712 $sieve_choices =
\GV\Utils::get( $widget_args,
'sieve_choices',
false );
1714 if ( ! $sieve_choices ) {
1715 return $search_fields;
1718 foreach ( $search_fields as &$filter ) {
1719 if ( empty( $filter[
'choices'] ) ) {
1733 if ( apply_filters(
'gravityview/search/sieve_choices', $sieve_choices,
$field, $context, $widget_args ) ) {
1738 return $search_fields;
1753 if ( empty( $filter[
'key'] ) || empty( $filter[
'choices'] ) ) {
1758 if (
'created_by' !== $filter[
'key'] && ! is_numeric( $filter[
'key'] ) ) {
1762 $form_id = $context->view->form->ID;
1766 $filter_choices = $cache->get();
1768 if ( $filter_choices ) {
1769 return $filter_choices;
1774 $entry_table_name = GFFormsModel::get_entry_table_name();
1775 $entry_meta_table_name = GFFormsModel::get_entry_meta_table_name();
1777 $key_like = $wpdb->esc_like( $filter[
'key'] ) .
'.%';
1779 switch ( \
GV\Utils::get( $filter,
'type' ) ) {
1780 case 'post_category':
1781 $choices = $wpdb->get_col( $wpdb->prepare(
1782 "SELECT DISTINCT SUBSTRING_INDEX( `meta_value`, ':', 1) FROM $entry_meta_table_name WHERE ( `meta_key` LIKE %s OR `meta_key` = %d) AND `form_id` = %d",
1783 $key_like, $filter[
'key'],
$form_id 1787 $choices = $wpdb->get_col( $wpdb->prepare(
1788 "SELECT DISTINCT `created_by` FROM $entry_table_name WHERE `form_id` = %d",
1793 $sql = $wpdb->prepare(
1794 "SELECT DISTINCT `meta_value` FROM $entry_meta_table_name WHERE ( `meta_key` LIKE %s OR `meta_key` = %s ) AND `form_id` = %d",
1795 $key_like, $filter[
'key'],
$form_id 1798 $choices = $wpdb->get_col( $sql );
1803 $choices = array_map(
'json_decode', $choices );
1804 $_choices_array = array();
1805 foreach ( $choices as $choice ) {
1806 if ( is_array( $choice ) ) {
1807 $_choices_array = array_merge( $_choices_array, $choice );
1809 $_choices_array [] = $choice;
1812 $choices = array_unique( $_choices_array );
1818 $filter_choices = array();
1819 foreach ( $filter[
'choices'] as $choice ) {
1820 if ( in_array( $choice[
'text'], $choices,
true ) || in_array( $choice[
'value'], $choices,
true ) ) {
1821 $filter_choices[] = $choice;
1825 $cache->set( $filter_choices,
'sieve_filter_choices', WEEK_IN_SECONDS );
1827 return $filter_choices;
1846 $users =
GVCommon::get_users(
'search_widget', array(
'fields' => array(
'ID',
'display_name' ) ) );
1849 foreach ( $users as
$user ) {
1857 $text = apply_filters(
'gravityview/search/created_by/text', $user->display_name, $user, $view );
1859 'value' => $user->ID,
1879 'value' => $status[
'value'],
1880 'text' => $status[
'label'],
1896 $url = strtok( add_query_arg( array() ),
'?' );
1917 if ( ! is_null(
$value ) ) {
1934 $js_dependencies[] =
'jquery-ui-datepicker';
1936 return $js_dependencies;
1957 $datepicker_settings = apply_filters(
'gravityview_datepicker_settings', array(
1958 'yearRange' =>
'-5:+5',
1959 'changeMonth' =>
true,
1960 'changeYear' =>
true,
1961 'closeText' => esc_attr_x(
'Close',
'Close calendar',
'gk-gravityview' ),
1962 'prevText' => esc_attr_x(
'Prev',
'Previous month in calendar',
'gk-gravityview' ),
1963 'nextText' => esc_attr_x(
'Next',
'Next month in calendar',
'gk-gravityview' ),
1964 'currentText' => esc_attr_x(
'Today',
'Today in calendar',
'gk-gravityview' ),
1965 'weekHeader' => esc_attr_x(
'Week',
'Week in calendar',
'gk-gravityview' ),
1966 'monthStatus' => __(
'Show a different month',
'gk-gravityview' ),
1967 'monthNames' => array_values( $wp_locale->month ),
1968 'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
1969 'dayNames' => array_values( $wp_locale->weekday ),
1970 'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ),
1971 'dayNamesMin' => array_values( $wp_locale->weekday_initial ),
1973 'firstDay' => get_option(
'start_of_week' ),
1975 'isRTL' => is_rtl(),
1978 $localizations[
'datepicker'] = $datepicker_settings;
1980 return $localizations;
1994 wp_register_script(
'gv-flexibility', plugins_url(
'assets/lib/flexibility/flexibility.js',
GRAVITYVIEW_FILE ), array(), \
GV\Plugin::$version,
true );
2005 if ( isset( $_SERVER[
'HTTP_USER_AGENT'] ) && preg_match(
'/MSIE [8-9]/', $_SERVER[
'HTTP_USER_AGENT'] ) ) {
2006 wp_enqueue_script(
'gv-flexibility' );
2021 wp_enqueue_script(
'jquery-ui-datepicker' );
2023 add_filter(
'gravityview_js_dependencies', array( $this,
'add_datepicker_js_dependency' ) );
2024 add_filter(
'gravityview_js_localization', array( $this,
'add_datepicker_localization' ), 10, 2 );
2026 $scheme = is_ssl() ?
'https://' :
'http://';
2027 wp_enqueue_style(
'jquery-ui-datepicker', $scheme.
'ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/smoothness/jquery-ui.css' );
2042 $datepicker_class = apply_filters(
'gravityview_search_datepicker_class',
"gv-datepicker datepicker " . $this->
get_datepicker_format() );
2058 $default_format =
'mdy';
2073 $format = apply_filters(
'gravityview/widgets/search/datepicker/format', $default_format );
2075 $gf_date_formats = array(
2078 'dmy_dash' =>
'd-m-Y',
2079 'dmy_dot' =>
'd.m.Y',
2082 'ymd_slash' =>
'Y/m/d',
2083 'ymd_dash' =>
'Y-m-d',
2084 'ymd_dot' =>
'Y.m.d',
2087 if ( ! $date_format ) {
2089 return isset( $gf_date_formats[
$format ] ) ?
$format : $default_format;
2093 return \GV\Utils::get( $gf_date_formats,
$format, $gf_date_formats[ $default_format ] );
2106 if ( ! is_preview() || ! current_user_can(
'publish_gravityviews') ) {
2111 foreach ( $wp->query_vars as $key =>
$value ) {
2112 printf(
'<input type="hidden" name="%s" value="%s" />', esc_attr( $key ), esc_attr(
$value ) );
2133 $allowed = apply_filters_deprecated(
'gravityview/search/operator_whitelist', array( $allowed, $key ),
'2.14',
'gravityview/search/operator_allowlist' );
2141 $allowed = apply_filters(
'gravityview/search/operator_allowlist', $allowed, $key );
2143 if ( ! in_array( $operator, $allowed,
true ) ) {
2144 $operator = $default;
2155 if ( !
gravityview()->plugin->supports( \
GV\Plugin::FEATURE_GFQUERY ) ) {
2164 $this->value = $filter[
'value'];
2165 $this->view = $view;
2168 public function sql( $query ) {
2171 $user_meta_fields = array(
2172 'nickname',
'first_name',
'last_name',
2180 $user_meta_fields = apply_filters(
'gravityview/widgets/search/created_by/user_meta_fields', $user_meta_fields, $this->view );
2182 $user_fields = array(
2183 'user_nicename',
'user_login',
'display_name',
'user_email',
2191 $user_fields = apply_filters(
'gravityview/widgets/search/created_by/user_fields', $user_fields, $this->view );
2193 $conditions = array();
2195 foreach ( $user_fields as $user_field ) {
2196 $conditions[] = $wpdb->prepare(
"`u`.`$user_field` LIKE %s",
'%' . $wpdb->esc_like( $this->value ) .
'%' );
2199 foreach ( $user_meta_fields as $meta_field ) {
2200 $conditions[] = $wpdb->prepare(
"(`um`.`meta_key` = %s AND `um`.`meta_value` LIKE %s)", $meta_field,
'%' . $wpdb->esc_like( $this->value ) .
'%' );
2203 $conditions =
'(' . implode(
' OR ', $conditions ) .
')';
2205 $alias = $query->_alias( null );
2207 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)))";
static _GET( $name, $default=null)
Grab a value from the _GET superglobal or default.
static getInstance( $passed_post=NULL)
static _REQUEST( $name, $default=null)
Grab a value from the _REQUEST superglobal or default.
$gravityview_view
array $entry array $form array $field_settings
if(gv_empty( $field['value'], false, false)) $format
gravityview_get_link( $href='', $anchor_text='', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
if(gravityview() ->plugin->is_GF_25()) $form
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(empty( $field_settings['content'])) $content
gv_map_deep( $value, $callback)
Maps a function to all non-iterable elements of an array or an object.
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' id and label, for a given Form ID.
static pre_get_form_fields( $template_id='')
Get the form fields for a preset (no form created yet)
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.
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".
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.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview()
The main GravityView wrapper function.
gv_empty( $value, $zero_is_empty=true, $allow_string_booleans=true)
Is the value empty?
static get_all()
Return array of status options.
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
gravityview_get_permalink_query_args( $id=0)
Get get_permalink() without the home_url() prepended to it.
static getInstance()
Get the one true instantiated self.