GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-plugin-hooks-gravity-forms-chainedselects.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Add Gravity Forms Chained Selects compatibility.
4  *
5  * @file class-gravityview-plugin-hooks-gravity-forms-chainedselects.php
6  * @package GravityView
7  * @license GPL2+
8  * @author GravityView <[email protected]>
9  * @link https://gravityview.co
10  * @copyright Copyright 2022, Katz Web Services, Inc.
11  */
12 
14 
15  /**
16  * @var string $constant_name The name of the constant that, if defined, means the plugin is active.
17  */
18  protected $constant_name = 'GF_CHAINEDSELECTS_VERSION';
19 
20  const INPUT_TYPE = 'chainedselect';
21 
22  /**
23  * @since 1.20
24  */
25  protected function add_hooks() {
26  parent::add_hooks();
27 
28  add_filter( 'gravityview/extension/search/input_type', array( $this, 'set_input_type' ), 10, 3 );
29 
30  add_filter( 'gravityview/search/input_types', array( $this, 'add_input_type' ) );
31 
32  add_filter( 'gravityview/search/searchable_fields', array( $this, 'modify_searchable_fields' ), 10, 2 );
33 
34  add_filter( 'gravityview/search/searchable_fields/allowlist', array( $this, 'modify_searchable_fields_allowlist' ), 10, 3 );
35 
36  add_filter( 'gravityview/search/input_labels', array( $this, 'add_input_label' ) );
37 
38  add_action( 'gravityview_search_widget_field_before', array( $this, 'print_scripts' ), 10, 2 );
39 
40  add_action( 'gravityview_search_widget_field_before', array( $this, 'print_styles' ), 10, 2 );
41  }
42 
43  /**
44  * Allow all inputs of a Chained Select field to be searched, even though only the parent is added to the widget.
45  *
46  * @param array $searchable_fields Array of GravityView-formatted fields or only the field ID? Example: [ '1.2', 'created_by' ]
47  * @param \GV\View $view Object of View being searched.
48  * @param bool $with_full_field Does $searchable_fields contain the full field array or just field ID? Default: false (just field ID)
49  *
50  * @return array If chainedselect search type,
51  */
52  function modify_searchable_fields_allowlist( $searchable_fields, $view, $with_full_field ) {
53 
54  /**
55  * The first time through, it's just field IDs. We want the full details that include input type.
56  * @see GravityView_Widget_Search::filter_entries()
57  */
58  if ( ! $with_full_field ) {
59  return $searchable_fields;
60  }
61 
62  foreach( $searchable_fields as $searchable_field ) {
63 
64  if ( self::INPUT_TYPE !== \GV\Utils::get( $searchable_field, 'input' ) ) {
65  continue;
66  }
67 
68  $field = GFAPI::get_field( $searchable_field['form_id'], $searchable_field['field'] );
69 
70  foreach( $field->get_entry_inputs() as $input ) {
71  $searchable_fields[] = array(
72  'field' => $input['id'],
73  );
74  }
75  }
76 
77  return $searchable_fields;
78  }
79 
80  /**
81  * Outputs inline style for vertical display
82  *
83  * @param GravityView_Widget_Search $this GravityView Widget instance
84  * @param array{key:string,label:string,value:string,type:string,choices:array} $search_field
85  *
86  * @return void
87  */
88  public function print_styles( $search_widget, $search_field ) {
89 
90  if( self::INPUT_TYPE !== \GV\Utils::get( $search_field, 'type' ) ) {
91  return;
92  }
93 
94  static $did_print_styles;
95 
96  /**
97  * Prevent Chained Select Search Bar input fields from outputting styles.
98  * @since TODO
99  * @param bool $should_print_styles True: Output styles; False: don't.
100  * @param GravityView_Widget_Search $this GravityView Widget instance.
101  * @param array{key:string,label:string,value:string,type:string,choices:array} $search_field
102  */
103  $should_print_styles = apply_filters( 'gravityview/search/chained_selects/print_styles', true, $search_widget, $search_field );
104 
105  if ( ! $should_print_styles || $did_print_styles ) {
106  return;
107  }
108 
109  ?>
110  <style>
111  .gfield_chainedselect span {
112  display: inline-block;
113  padding: 0 4px 0 0;
114  }
115 
116  .gfield_chainedselect.vertical span {
117  display: block;
118  padding: 0 0 4px;
119  }
120 
121  .gfield_chainedselect.vertical select {
122  min-width: 100px;
123  max-width: 100%;
124  }
125  </style>
126  <?php
127 
128  $did_print_styles = true;
129  }
130 
131  /**
132  * Enqueues and prints the required scripts for
133  *
134  * @param GravityView_Widget_Search $this GravityView Widget instance
135  * @param array $search_field
136  *
137  * @return void
138  */
139  public function print_scripts( $search_widget, $search_field ) {
140 
141  if( self::INPUT_TYPE !== \GV\Utils::get( $search_field, 'type' ) ) {
142  return;
143  }
144 
145  if ( ! function_exists( 'gf_chained_selects' ) ) {
146  gravityview()->log->error( 'The Gravity Forms Chained Select Add-On is not active.' );
147 
148  return;
149  }
150 
151  if ( ! class_exists( 'GFFormDisplay' ) ) {
152  return;
153  }
154 
155  // Adds the gform hooks required by Chained Selects. See gforms_hooks.js.
156  if ( empty( GFFormDisplay::$hooks_js_printed ) ) {
157  echo GFCommon::get_hooks_javascript_code();
158  }
159 
160  if ( ! wp_script_is( 'gform_chained_selects' ) ) {
161  wp_enqueue_script( 'gform_chained_selects', gf_chained_selects()->get_base_url() . '/js/frontend.js', array(
162  'jquery',
163  'gform_gravityforms'
164  ), gf_chained_selects()->get_version() );
165  }
166 
167  // Print the required JS var that includes the ajaxURL.
168  gf_chained_selects()->localize_scripts();
169 
170  wp_print_scripts( 'gform_chained_selects' );
171  }
172 
173  /**
174  * @param GF_Field $gf_field
175  *
176  * @return array
177  */
178  static public function get_field_values( $gf_field ) {
179 
180  $field_values = array();
181 
182  foreach ( $gf_field->get_entry_inputs() as $input ) {
183 
184  // Inputs are converted from . to _
185  $input_url_arg = 'input_' . str_replace( '.', '_', $input['id'] );
186 
187  $field_values[ $input['id'] ] = \GV\Utils::_REQUEST( $input_url_arg );
188  }
189 
190  return $field_values;
191  }
192 
193  function add_input_label( $input_labels = array() ) {
194 
195  $input_labels[ self::INPUT_TYPE ] = esc_html__( 'Chained Select', 'gk-gravityview' );
196 
197  return $input_labels;
198  }
199 
200  /**
201  * Don't show inputs of the Chained Select field, only the parent.
202  *
203  * @see gravityview_get_form_fields() Used to fetch the fields
204  * @see GravityView_Widget_Search::get_search_input_types See this method to modify the type of input types allowed for a field
205  * @param array $fields Array of searchable fields, as fetched by gravityview_get_form_fields()
206  * @param int $form_id
207  *
208  * @return array
209  */
210  function modify_searchable_fields( $fields, $form_id ) {
211 
212  foreach( $fields as $key => $field ) {
213  if( 'chainedselect' === $field['type'] && ! empty( $field['parent'] ) ) {
214  unset( $fields[ $key ] );
215  }
216  }
217 
218  return $fields;
219  }
220 
221  /**
222  * Turns Chained Select inputs into the same choices as select/radio inputs.
223  *
224  * @param array $input_types Associative array: key is field `name`, value is array of GravityView input types (note: use `input_text` for `text`).
225  *
226  * @return array
227  */
228  public function add_input_type( $input_type ) {
229 
230  $input_type[ self::INPUT_TYPE ] = array( self::INPUT_TYPE, 'input_text' );
231 
232  return $input_type;
233  }
234 
235  /**
236  * Turns Chained Select inputs into the same choices as select/radio inputs.
237  *
238  * @param array $input_types Associative array: key is field `name`, value is array of GravityView input types (note: use `input_text` for `text`).
239  *
240  * @return string
241  */
242  public function set_input_type( $input_type, $field_type, $field_id ) {
243 
244  if ( ! in_array( $field_type, array( 'chainedselect' ) ) ) {
245  return $input_type;
246  }
247 
248  return self::INPUT_TYPE;
249  }
250 
251 }
252 
static _REQUEST( $name, $default=null)
Grab a value from the _REQUEST superglobal or default.
print_styles( $search_widget, $search_field)
Outputs inline style for vertical display.
modify_searchable_fields_allowlist( $searchable_fields, $view, $with_full_field)
Allow all inputs of a Chained Select field to be searched, even though only the parent is added to th...
set_input_type( $input_type, $field_type, $field_id)
Turns Chained Select inputs into the same choices as select/radio inputs.
add_input_type( $input_type)
Turns Chained Select inputs into the same choices as select/radio inputs.
if(empty( $created_by)) $form_id
gravityview()
The main GravityView wrapper function.
print_scripts( $search_widget, $search_field)
Enqueues and prints the required scripts for.
Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles...
modify_searchable_fields( $fields, $form_id)
Don&#39;t show inputs of the Chained Select field, only the parent.
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22