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