GravityView  2.10.1
The best, easiest way to display Gravity Forms entries on your website.
class.field.type.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Modify option field type by extending this class
5  */
6 abstract class GravityView_FieldType {
7 
8  /**
9  * Field form html `name`
10  *
11  * @var string
12  */
13  protected $name;
14 
15  /**
16  * Field settings
17  *
18  * @var array
19  */
20  protected $field;
21 
22  /**
23  * Field current value
24  *
25  * @var mixed
26  */
27  protected $value;
28 
29  function __construct( $name = '', $field = array(), $curr_value = NULL ) {
30 
31  $this->name = $name;
32 
33  $defaults = self::get_field_defaults();
34 
35  // Backward compatibility
36  if( !empty( $field['choices'] ) ) {
37  $field['options'] = $field['choices'];
38  unset( $field['choices'] );
39  }
40 
41  $this->field = wp_parse_args( $field, $defaults );
42 
43  $this->value = is_null( $curr_value ) ? $this->field['value'] : $curr_value;
44 
45  }
46 
47  /**
48  * Returns the default details for a field option
49  *
50  * - default // default option value, in case nothing is defined (@deprecated)
51  * - desc // option description
52  * - value // the option default value
53  * - label // the option label
54  * - left_label // In case of checkboxes, left label will appear on the left of the checkbox
55  * - id // the field id
56  * - type // the option type ( text, checkbox, select, ... )
57  * - options // when type is select, define the select options ('choices' is @deprecated)
58  * - merge_tags // if the option supports merge tags feature
59  * - class // (new) define extra classes for the field
60  * - tooltip //
61  *
62  * @return array
63  */
64  public static function get_field_defaults() {
65  return array(
66  'desc' => '',
67  'value' => null,
68  'label' => '',
69  'left_label' => null,
70  'id' => null,
71  'type' => 'text',
72  'options' => null,
73  'merge_tags' => true,
74  'class' => '',
75  'tooltip' => null,
76  'requires' => null
77  );
78  }
79 
80 
81  function get_tooltip() {
82  if( ! function_exists('gform_tooltip') ) {
83  return null;
84  }
85 
86  $article = wp_parse_args( \GV\Utils::get( $this->field, 'article', array() ), array(
87  'id' => '',
88  'type' => 'modal',
89  'url' => '#',
90  ) );
91 
92  return !empty( $this->field['tooltip'] ) ? ' '. $this->tooltip( $this->field['tooltip'], false, true, $article ) : null;
93  }
94 
95  /**
96  * Displays the tooltip
97  *
98  * @since 2.8.1
99  *
100  * @global $__gf_tooltips
101  *
102  * @param string $name The name of the tooltip to be displayed
103  * @param string $css_class Optional. The CSS class to apply toi the element. Defaults to empty string.
104  * @param bool $return Optional. If the tooltip should be returned instead of output. Defaults to false (output)
105  * @param array $article Optional. Details about support doc article connected to the tooltip. {
106  * @type string $id Unique ID of article for Beacon API
107  * @type string $url URL of support doc article
108  * @type string $type Type of Beacon element to open. {@see https://developer.helpscout.com/beacon-2/web/javascript-api/#beaconarticle}
109  * }
110  *
111  * @return string
112  */
113  function tooltip( $name, $css_class = '', $return = false, $article = array() ) {
114  global $__gf_tooltips; //declared as global to improve WPML performance
115 
116  $css_class = empty( $css_class ) ? 'tooltip' : $css_class;
117  /**
118  * Filters the tooltips available
119  *
120  * @param array $__gf_tooltips Array containing the available tooltips
121  */
122  $__gf_tooltips = apply_filters( 'gform_tooltips', $__gf_tooltips );
123 
124  //AC: the $name parameter is a key when it has only one word. Maybe try to improve this later.
125  $parameter_is_key = count( explode( ' ', $name ) ) == 1;
126 
127  $tooltip_text = $parameter_is_key ? rgar( $__gf_tooltips, $name ) : $name;
128  $tooltip_class = isset( $__gf_tooltips[ $name ] ) ? "tooltip_{$name}" : '';
129  $tooltip_class = esc_attr( $tooltip_class );
130 
131  /**
132  * Below this line has been modified by GravityView.
133  */
134 
135  if ( empty( $tooltip_text ) && empty( $article['id'] ) ) {
136  return '';
137  }
138 
139  $url = '#';
140  $atts = 'onclick="return false;" onkeypress="return false;"';
141  $anchor_text = '<i class=\'fa fa-question-circle\'></i>';
142  $css_class = gravityview_sanitize_html_class( 'gf_tooltip ' . $css_class . ' ' . $tooltip_class );
143 
144  $tooltip = sprintf( '<a href="%s" %s class="%s" title="%s" role="button">%s</a>',
145  esc_url( $url ),
146  $atts,
147  $css_class,
148  esc_attr( $tooltip_text ),
149  $anchor_text
150  );
151 
152  /**
153  * Modify the tooltip HTML before outputting
154  * @internal
155  * @see GravityView_Support_Port::maybe_add_article_to_tooltip()
156  */
157  $tooltip = apply_filters( 'gravityview/tooltips/tooltip', $tooltip, $article, $url, $atts, $css_class, $tooltip_text, $anchor_text );
158 
159  if ( ! $return ) {
160  echo $tooltip;
161  }
162 
163  return $tooltip;
164  }
165 
166  /**
167  * Build input id based on the name
168  * @return string
169  */
170  function get_field_id() {
171  if( isset( $this->field['id'] ) ) {
172  return esc_attr( $this->field['id'] );
173  }
174  return esc_attr( sanitize_html_class( $this->name ) );
175  }
176 
177  /**
178  * Retrieve field label
179  * @return string
180  */
181  function get_field_label() {
182  return esc_html( trim( $this->field['label'] ) );
183  }
184 
185  /**
186  * Retrieve field left label
187  *
188  * @since 1.7
189  *
190  * @return string
191  */
192  function get_field_left_label() {
193  return ! empty( $this->field['left_label'] ) ? esc_html( trim( $this->field['left_label'] ) ) : NULL;
194  }
195 
196  /**
197  * Retrieve field label class
198  * @return string
199  */
200  function get_label_class() {
201  return 'gv-label-'. sanitize_html_class( $this->field['type'] );
202  }
203 
204 
205  /**
206  * Retrieve field description
207  * @return string
208  */
209  function get_field_desc() {
210  return !empty( $this->field['desc'] ) ? '<span class="howto">'. $this->field['desc'] .'</span>' : '';
211  }
212 
213 
214  /**
215  * Verify if field should have merge tags
216  * @return boolean
217  */
218  function show_merge_tags() {
219  // Show the merge tags if the field is a list view
220  $is_list = preg_match( '/_list-/ism', $this->name );
221  // Or is a single entry view
222  $is_single = preg_match( '/single_/ism', $this->name );
223 
224  return ( $is_single || $is_list );
225  }
226 
227 
228 
229  /**
230  * important! Override this class
231  * outputs the field option html
232  */
233  function render_option() {
234  // to replace on each field
235  }
236 
237  /**
238  * important! Override this class if needed
239  * outputs the field setting html
240  */
241  function render_setting( $override_input = NULL ) {
242 
243  if( !empty( $this->field['full_width'] ) ) { ?>
244  <th scope="row" colspan="2">
245  <div>
246  <label for="<?php echo $this->get_field_id(); ?>">
247  <?php echo $this->get_field_label() . $this->get_tooltip(); ?>
248  </label>
249  </div>
250  <?php $this->render_input( $override_input ); ?>
251  </th>
252  <?php } else { ?>
253  <th scope="row">
254  <label for="<?php echo $this->get_field_id(); ?>">
255  <?php echo $this->get_field_label() . $this->get_tooltip(); ?>
256  </label>
257  </th>
258  <td>
259  <?php $this->render_input( $override_input ); ?>
260  </td>
261  <?php }
262 
263  }
264 
265  /**
266  * important! Override this class
267  * outputs the input html part
268  */
269  function render_input( $override_input ) {
270  echo '';
271  }
272 
273 }
$url
Definition: post_image.php:25
static get_field_defaults()
Returns the default details for a field option.
render_option()
important! Override this class outputs the field option html
get_label_class()
Retrieve field label class.
show_merge_tags()
Verify if field should have merge tags.
get_field_id()
Build input id based on the name.
get_field_desc()
Retrieve field description.
get( $key, $default=null)
Retrieve a setting.
__construct( $name='', $field=array(), $curr_value=NULL)
render_setting( $override_input=NULL)
important! Override this class if needed outputs the field setting html
tooltip( $name, $css_class='', $return=false, $article=array())
Displays the tooltip.
Modify option field type by extending this class.
render_input( $override_input)
important! Override this class outputs the input html part
get_field_left_label()
Retrieve field left label.
get_field_label()
Retrieve field label.