GravityView  2.9.2
The best, easiest way to display Gravity Forms entries on your website.
class.render.settings.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Renders field/widget options and view settings
4  *
5  * @package GravityView
6  * @license GPL2+
7  * @author GravityView <hello@gravityview.co>
8  * @link http://gravityview.co
9  * @copyright Copyright 2014, Katz Web Services, Inc.
10  *
11  * @since 1.2
12  */
13 
15 
16  /**
17  * Get the default options for a standard field.
18  *
19  * @param string $field_type Type of field options to render (`field` or `widget`)
20  * @param string $template_id Layout slug (`default_table`, `default_list`, `datatables_table`, etc.
21  * @param float $field_id GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
22  * @param string $context What context are we in? Example: `single` or `directory`
23  * @param string $input_type (textarea, list, select, etc.)
24  * @param int $form_id The form ID. @since develop
25  * @return array Array of field options with `label`, `value`, `type`, `default` keys
26  */
27  public static function get_default_field_options( $field_type, $template_id, $field_id, $context, $input_type, $form_id ) {
28 
29  $field_options = array();
30 
31  $is_table_layout = preg_match( '/table/ism', $template_id );
32 
33  if( 'field' === $field_type ) {
34 
35  // Default options - fields
36  $field_options = array(
37  'show_label' => array(
38  'type' => 'checkbox',
39  'label' => __( 'Show Label', 'gravityview' ),
40  'value' => ! empty ( $is_table_layout ),
41  ),
42  'custom_label' => array(
43  'type' => 'text',
44  'label' => __( 'Custom Label:', 'gravityview' ),
45  'value' => '',
46  'class' => 'widefat',
47  'merge_tags' => true,
48  ),
49  'custom_class' => array(
50  'type' => 'text',
51  'label' => __( 'Custom CSS Class:', 'gravityview' ),
52  'desc' => __( 'This class will be added to the field container', 'gravityview'),
53  'value' => '',
54  'merge_tags' => true,
55  'tooltip' => 'gv_css_merge_tags',
56  'class' => 'widefat code',
57  ),
58  'only_loggedin' => array(
59  'type' => 'checkbox',
60  'label' => __( 'Make visible only to logged-in users?', 'gravityview' ),
61  'value' => ''
62  ),
63  'only_loggedin_cap' => array(
64  'type' => 'select',
65  'label' => __( 'Make visible for:', 'gravityview' ),
66  'options' => self::get_cap_choices( $template_id, $field_id, $context, $input_type ),
67  'class' => 'widefat',
68  'value' => 'read',
69  ),
70  );
71 
72  // Match Table as well as DataTables
73  if( $is_table_layout && 'directory' === $context ) {
74  $field_options['width'] = array(
75  'type' => 'number',
76  'label' => __('Percent Width', 'gravityview'),
77  'desc' => __( 'Leave blank for column width to be based on the field content.', 'gravityview'),
78  'class' => 'code widefat',
79  'value' => '',
80  );
81  }
82 
83  }
84 
85  /**
86  * @filter `gravityview_template_{$field_type}_options` Filter the field options by field type. Filter names: `gravityview_template_field_options` and `gravityview_template_widget_options`
87  * @param[in,out] array Array of field options with `label`, `value`, `type`, `default` keys
88  * @param[in] string $template_id Table slug
89  * @param[in] float $field_id GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
90  * @param[in] string $context What context are we in? Example: `single` or `directory`
91  * @param[in] string $input_type (textarea, list, select, etc.)
92  * @param[in] int $form_id The form ID. @since develop
93  */
94  $field_options = apply_filters( "gravityview_template_{$field_type}_options", $field_options, $template_id, $field_id, $context, $input_type, $form_id );
95 
96  /**
97  * @filter `gravityview_template_{$input_type}_options` Filter the field options by input type (`$input_type` examples: `textarea`, `list`, `select`, etc.)
98  * @param[in,out] array Array of field options with `label`, `value`, `type`, `default` keys
99  * @param[in] string $template_id Table slug
100  * @param[in] float $field_id GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
101  * @param[in] string $context What context are we in? Example: `single` or `directory`
102  * @param[in] string $input_type (textarea, list, select, etc.)
103  * @param[in] int $form_id The form ID. @since develop
104  */
105  $field_options = apply_filters( "gravityview_template_{$input_type}_options", $field_options, $template_id, $field_id, $context, $input_type, $form_id );
106 
107  return $field_options;
108  }
109 
110  /**
111  * Get capabilities options for GravityView
112  *
113  * Parameters are only to pass to the filter.
114  *
115  * @param string $template_id Optional. View slug
116  * @param string $field_id Optional. GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
117  * @param string $context Optional. What context are we in? Example: `single` or `directory`
118  * @param string $input_type Optional. (textarea, list, select, etc.)
119  * @return array Associative array, with the key being the capability and the value being the label shown.
120  */
121  static public function get_cap_choices( $template_id = '', $field_id = '', $context = '', $input_type = '' ) {
122 
123  $select_cap_choices = array(
124  'read' => __( 'Any Logged-In User', 'gravityview' ),
125  'publish_posts' => __( 'Author Or Higher', 'gravityview' ),
126  'gravityforms_view_entries' => __( 'Can View Gravity Forms Entries', 'gravityview' ),
127  'delete_others_posts' => __( 'Editor Or Higher', 'gravityview' ),
128  'gravityforms_edit_entries' => __( 'Can Edit Gravity Forms Entries', 'gravityview' ),
129  'manage_options' => __( 'Administrator', 'gravityview' ),
130  );
131 
132  if( is_multisite() ) {
133  $select_cap_choices['manage_network'] = __('Multisite Super Admin', 'gravityview' );
134  }
135 
136  /**
137  * @filter `gravityview_field_visibility_caps` Modify the capabilities shown in the field dropdown
138  * @see https://docs.gravityview.co/article/96-how-to-modify-capabilities-shown-in-the-field-only-visible-to-dropdown
139  * @since 1.0.1
140  * @param array $select_cap_choices Associative rray of role slugs with labels ( `manage_options` => `Administrator` )
141  * @param string $template_id Optional. View slug
142  * @param string $field_id Optional. GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
143  * @param string $context Optional. What context are we in? Example: `single` or `directory`
144  * @param string $input_type Optional. (textarea, list, select, etc.)
145  */
146  $select_cap_choices = apply_filters('gravityview_field_visibility_caps', $select_cap_choices, $template_id, $field_id, $context, $input_type );
147 
148  return $select_cap_choices;
149  }
150 
151 
152  /**
153  * Render Field Options html (shown through a dialog box)
154  *
155  * @see GravityView_Ajax::get_field_options
156  * @see GravityView_Admin_Views::render_active_areas
157  *
158  * @param string $form_id
159  * @param string $field_type field / widget
160  * @param string $template_id
161  * @param string $field_id
162  * @param string $field_label
163  * @param string $area
164  * @param string $uniqid (default: '')
165  * @param string $current (default: '')
166  * @param string $context (default: 'single')
167  * @param array $item Field or widget array that's being rendered
168  *
169  * @return string HTML of dialog box
170  */
171  public static function render_field_options( $form_id, $field_type, $template_id, $field_id, $field_label, $area, $input_type = NULL, $uniqid = '', $current = '', $context = 'single', $item = array() ) {
172 
173  if( empty( $uniqid ) ) {
174  //generate a unique field id
175  $uniqid = uniqid('', false);
176  }
177 
178  // get field/widget options
179  $options = self::get_default_field_options( $field_type, $template_id, $field_id, $context, $input_type, $form_id );
180 
181  // two different post arrays, depending of the field type
182  $name_prefix = $field_type .'s' .'['. $area .']['. $uniqid .']';
183 
184  // build output
185  $output = '';
186  $output .= '<input type="hidden" class="field-key" name="'. $name_prefix .'[id]" value="'. esc_attr( $field_id ) .'">';
187  $output .= '<input type="hidden" class="field-label" name="'. $name_prefix .'[label]" value="'. esc_attr( $field_label ) .'">';
188  if ( $form_id ) {
189  $output .= '<input type="hidden" class="field-form-id" name="'. $name_prefix .'[form_id]" value="'. esc_attr( $form_id ) .'">';
190  }
191 
192  // If there are no options, return what we got.
193  if(empty($options)) {
194 
195  // This is here for checking if the output is empty in render_label()
196  $output .= '<!-- No Options -->';
197 
198  return $output;
199  }
200 
201  $output .= '<div class="gv-dialog-options" title="'. esc_attr( sprintf( __( 'Options: %s', 'gravityview' ) , strip_tags( html_entity_decode( $field_label ) ) ) ) .'">';
202 
203  /**
204  * @since 1.8
205  */
206  if( !empty( $item['subtitle'] ) ) {
207  $output .= '<div class="subtitle">' . $item['subtitle'] . '</div>';
208  }
209 
210  foreach( $options as $key => $option ) {
211 
212  $value = isset( $current[ $key ] ) ? $current[ $key ] : NULL;
213 
214  $field_output = self::render_field_option( $name_prefix . '['. $key .']' , $option, $value);
215 
216  // The setting is empty
217  if( empty( $field_output ) ) {
218  continue;
219  }
220 
221  switch( $option['type'] ) {
222  // Hide hidden fields
223  case 'hidden':
224  $output .= '<div class="gv-setting-container gv-setting-container-'. esc_attr( $key ) . ' screen-reader-text">'. $field_output . '</div>';
225  break;
226  default:
227  $output .= '<div class="gv-setting-container gv-setting-container-'. esc_attr( $key ) . '">'. $field_output .'</div>';
228  }
229  }
230 
231  // close options window
232  $output .= '</div>';
233 
234  return $output;
235 
236  }
237 
238 
239 
240  /**
241  * Handle rendering a field option form element
242  *
243  * @param string $name Input `name` attribute
244  * @param array $option Associative array of options. See the $defaults variable for available keys.
245  * @param mixed $curr_value Current value of option
246  * @return string HTML output of option
247  */
248  public static function render_field_option( $name = '', $option = array(), $curr_value = NULL ) {
249 
250  $output = '';
251 
252  /**
253  * @deprecated setting index 'default' was replaced by 'value'
254  * @see GravityView_FieldType::get_field_defaults
255  */
256  if( !empty( $option['default'] ) && empty( $option['value'] ) ) {
257  $option['value'] = $option['default'];
258  _deprecated_function( 'GravityView_FieldType::get_field_defaults', '1.1.7', '[value] instead of [default] when defining the setting '. $name .' details' );
259  }
260 
261  // prepare to render option field type
262  if( isset( $option['type'] ) ) {
263 
264  $type_class = self::load_type_class( $option );
265 
266  if( class_exists( $type_class ) ) {
267 
268  /** @type GravityView_FieldType $render_type */
269  $render_type = new $type_class( $name, $option, $curr_value );
270 
271  ob_start();
272 
273  $render_type->render_option();
274 
275  $output = ob_get_clean();
276 
277  /**
278  * @filter `gravityview/option/output/{option_type}` Modify the output for a GravityView setting.\n
279  * `$option_type` is the type of setting (`radio`, `text`, etc.)
280  * @param[in,out] string $output field class name
281  * @param[in] array $option option field data
282  */
283  $output = apply_filters( "gravityview/option/output/{$option['type']}" , $output, $option );
284  }
285 
286  } // isset option[type]
287 
288  return $output;
289  }
290 
291 
292 
293 
294 
295 
296  /**
297  * Output a table row for view settings
298  * @param string $key The key of the input
299  * @param array $current_settings Associative array of current settings to use as input values, if set. If not set, the defaults are used.
300  * @param string $override_input [description]
301  * @param string $name [description]
302  * @param string $id [description]
303  * @return void [description]
304  */
305  public static function render_setting_row( $key = '', $current_settings = array(), $override_input = null, $name = 'template_settings[%s]', $id = 'gravityview_se_%s' ) {
306 
308 
309  // If the key doesn't exist, there's something wrong.
310  if ( ! $setting = $settings->get( $key ) ) {
311  return;
312  }
313 
314  /**
315  * @deprecated setting index 'name' was replaced by 'label'
316  * @see GravityView_FieldType::get_field_defaults
317  */
318  if( isset( $setting['name'] ) && empty( $setting['label'] ) ) {
319  $setting['label'] = $setting['name'];
320  _deprecated_function( 'GravityView_FieldType::get_field_defaults', '1.1.7', '[label] instead of [name] when defining the setting '. $key .' details' );
321  }
322 
323  $name = esc_attr( sprintf( $name, $key ) );
324  $setting['id'] = esc_attr( sprintf( $id, $key ) );
325  $setting['tooltip'] = 'gv_' . $key;
326 
327  // Use default if current setting isn't set.
328  $curr_value = isset( $current_settings[ $key ] ) ? $current_settings[ $key ] : $setting['value'];
329 
330  // default setting type = text
331  $setting['type'] = empty( $setting['type'] ) ? 'text' : $setting['type'];
332 
333  // merge tags
334  if( !isset( $setting['merge_tags'] ) ) {
335  if( $setting['type'] === 'text' ) {
336  $setting['merge_tags'] = true;
337  } else {
338  $setting['merge_tags'] = false;
339  }
340  }
341 
342  $output = '';
343 
344  // render the setting
345  $type_class = self::load_type_class( $setting );
346  if( class_exists( $type_class ) ) {
347  /** @type GravityView_FieldType $render_type */
348  $render_type = new $type_class( $name, $setting, $curr_value );
349  ob_start();
350  $render_type->render_setting( $override_input );
351  $output = ob_get_clean();
352  }
353 
354  // Check if setting is specific for a template
355  if( !empty( $setting['show_in_template'] ) ) {
356  if( !is_array( $setting['show_in_template'] ) ) {
357  $setting['show_in_template'] = array( $setting['show_in_template'] );
358  }
359  $show_if = ' data-show-if="'. implode( ' ', $setting['show_in_template'] ).'"';
360  } else {
361  $show_if = '';
362  }
363 
364  if( ! empty( $setting['requires'] ) ) {
365  $show_if .= sprintf( ' data-requires="%s"', $setting['requires'] );
366  }
367 
368  if( ! empty( $setting['requires_not'] ) ) {
369  $show_if .= sprintf( ' data-requires-not="%s"', $setting['requires_not'] );
370  }
371 
372  // output
373  echo '<tr style="vertical-align: top;" '. $show_if .'>' . $output . '</tr>';
374 
375  }
376 
377 
378  /**
379  * Given a field type calculates the php class. If not found try to load it.
380  * @param array $field
381  * @return string type class name
382  */
383  public static function load_type_class( $field = NULL ) {
384 
385  if( empty( $field['type'] ) ) {
386  return NULL;
387  }
388 
389  /**
390  * @filter `gravityview/setting/class/{field_type}`
391  * @param string $class_suffix field class suffix; `GravityView_FieldType_{$class_suffix}`
392  * @param array $field field data
393  */
394  $type_class = apply_filters( "gravityview/setting/class/{$field['type']}", 'GravityView_FieldType_' . $field['type'], $field );
395 
396  if( class_exists( $type_class ) ) {
397  return $type_class;
398  }
399 
400  /**
401  * @filter `gravityview/setting/class_file/{field_type}`
402  * @param string $field_type_include_path field class file path
403  * @param array $field field data
404  */
405  $class_file = apply_filters( "gravityview/setting/class_file/{$field['type']}", GRAVITYVIEW_DIR . "includes/admin/field-types/type_{$field['type']}.php", $field );
406 
407  if( $class_file && file_exists( $class_file ) ) {
408  require_once( $class_file );
409  }
410 
411  return $type_class;
412  }
413 
414 
415 
416 
417 
418  /**
419  * @deprecated 1.2
420  * Render the HTML for a checkbox input to be used on the field & widgets options
421  * @param string $name , name attribute
422  * @param string $current current value
423  * @return string html tags
424  */
425  public static function render_checkbox_option( $name = '', $id = '', $current = '' ) {
426 
427  _deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_checkbox::render_input' );
428 
429  $output = '<input name="'. esc_attr( $name ) .'" type="hidden" value="0">';
430  $output .= '<input name="'. esc_attr( $name ) .'" id="'. esc_attr( $id ) .'" type="checkbox" value="1" '. checked( $current, '1', false ) .' >';
431 
432  return $output;
433  }
434 
435 
436  /**
437  * @deprecated 1.2
438  * Render the HTML for an input text to be used on the field & widgets options
439  * @param string $name Unique name of the field. Exampe: `fields[directory_list-title][5374ff6ab128b][custom_label]`
440  * @param string $current [current value]
441  * @param string $add_merge_tags Add merge tags to the input?
442  * @param array $args Field settings, including `class` key for CSS class
443  * @return string [html tags]
444  */
445  public static function render_text_option( $name = '', $id = '', $current = '', $add_merge_tags = NULL, $args = array() ) {
446 
447  _deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_text::render_input' );
448 
449  // Show the merge tags if the field is a list view
450  $is_list = ( preg_match( '/_list-/ism', $name ));
451 
452  // Or is a single entry view
453  $is_single = ( preg_match( '/single_/ism', $name ));
454  $show = ( $is_single || $is_list );
455 
456  $class = '';
457  // and $add_merge_tags is not false
458  if( $show && $add_merge_tags !== false || $add_merge_tags === 'force' ) {
459  $class = 'merge-tag-support mt-position-right mt-hide_all_fields ';
460  }
461 
462  $class .= !empty( $args['class'] ) ? $args['class'] : 'widefat';
463  $type = !empty( $args['type'] ) ? $args['type'] : 'text';
464 
465  return '<input name="'. esc_attr( $name ) .'" id="'. esc_attr( $id ) .'" type="'.esc_attr($type).'" value="'. esc_attr( $current ) .'" class="'.esc_attr( $class ).'">';
466  }
467 
468  /**
469  * @deprecated 1.2
470  * Render the HTML for an textarea input to be used on the field & widgets options
471  * @param string $name Unique name of the field. Exampe: `fields[directory_list-title][5374ff6ab128b][custom_label]`
472  * @param string $current [current value]
473  * @param string|boolean $add_merge_tags Add merge tags to the input?
474  * @param array $args Field settings, including `class` key for CSS class
475  * @return string [html tags]
476  */
477  public static function render_textarea_option( $name = '', $id = '', $current = '', $add_merge_tags = NULL, $args = array() ) {
478 
479  _deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_textarea::render_input' );
480 
481  // Show the merge tags if the field is a list view
482  $is_list = ( preg_match( '/_list-/ism', $name ));
483 
484  // Or is a single entry view
485  $is_single = ( preg_match( '/single_/ism', $name ));
486  $show = ( $is_single || $is_list );
487 
488  $class = '';
489  // and $add_merge_tags is not false
490  if( $show && $add_merge_tags !== false || $add_merge_tags === 'force' ) {
491  $class = 'merge-tag-support mt-position-right mt-hide_all_fields ';
492  }
493 
494  $class .= !empty( $args['class'] ) ? 'widefat '.$args['class'] : 'widefat';
495 
496  return '<textarea name="'. esc_attr( $name ) .'" id="'. esc_attr( $id ) .'" class="'.esc_attr( $class ).'">'. esc_textarea( $current ) .'</textarea>';
497  }
498 
499  /**
500  *
501  * Render the HTML for a select box to be used on the field & widgets options
502  * @deprecated 1.2
503  * @param string $name [name attribute]
504  * @param array $choices [select options]
505  * @param string $current [current value]
506  * @return string [html tags]
507  */
508  public static function render_select_option( $name = '', $id = '', $choices, $current = '' ) {
509 
510  _deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_select::render_input' );
511 
512  $output = '<select name="'. $name .'" id="'. $id .'">';
513  foreach( $choices as $value => $label ) {
514  $output .= '<option value="'. esc_attr( $value ) .'" '. selected( $value, $current, false ) .'>'. esc_html( $label ) .'</option>';
515  }
516  $output .= '</select>';
517 
518  return $output;
519  }
520 
521 
522 }
const GRAVITYVIEW_DIR
"GRAVITYVIEW_DIR" "./" The absolute path to the plugin directory, with trailing slash ...
Definition: gravityview.php:40
static get_default_field_options( $field_type, $template_id, $field_id, $context, $input_type, $form_id)
Get the default options for a standard field.
static render_select_option( $name='', $id='', $choices, $current='')
Render the HTML for a select box to be used on the field & widgets options.
static render_field_options( $form_id, $field_type, $template_id, $field_id, $field_label, $area, $input_type=NULL, $uniqid='', $current='', $context='single', $item=array())
Render Field Options html (shown through a dialog box)
static load_type_class( $field=NULL)
Given a field type calculates the php class.
static with_defaults( $detailed=false)
Retrieve an instance of the settings with default values.
static render_checkbox_option( $name='', $id='', $current='')
static render_setting_row( $key='', $current_settings=array(), $override_input=null, $name='template_settings[%s]', $id='gravityview_se_%s')
Output a table row for view settings.
$class
static render_textarea_option( $name='', $id='', $current='', $add_merge_tags=NULL, $args=array())
$current_settings
Definition: edit-entry.php:10
$field_id
Definition: time.php:17
if(empty( $field_settings['email_field'])) $settings
if(empty( $created_by)) $form_id
static render_text_option( $name='', $id='', $current='', $add_merge_tags=NULL, $args=array())
static get_cap_choices( $template_id='', $field_id='', $context='', $input_type='')
Get capabilities options for GravityView.
static render_field_option( $name='', $option=array(), $curr_value=NULL)
Handle rendering a field option form element.
$field
Definition: gquiz_grade.php:11