GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-admin-view-item.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-admin-view-item.php
4  * @since 1.17.3
5  */
6 
7 /**
8  * A field or widget in GravityView view configuration
9  */
11 
12  /**
13  * @var string Name of the item in the field or widget picker
14  */
15  protected $title;
16 
17  /**
18  * @var string The field ID or the widget slug ( `2.3` or `custom_content`)
19  */
20  protected $id;
21 
22  /**
23  * @var string Description of the item
24  */
25  protected $subtitle;
26 
27  /**
28  * @var string The type of item ("field" or "widget")
29  */
30  protected $label_type;
31 
32  /**
33  * @var array Associative array of item details
34  */
35  protected $item;
36 
37  /**
38  * @var array Existing settings for the item
39  */
40  protected $settings;
41 
42  /**
43  * @var string For ID, if available
44  */
45  protected $form_id;
46 
47  /**
48  * @var array Form data, if available
49  */
50  protected $form;
51 
52  function __construct( $title = '', $item_id = '', $item = array(), $settings = array(), $form_id = null, $form = array() ) {
53 
54  // Backward compat
55  if ( ! empty( $item['type'] ) ) {
56  $item['input_type'] = $item['type'];
57  unset( $item['type'] );
58  }
59 
60  if ( $admin_label = \GV\Utils::get( $settings, 'admin_label' ) ) {
61  $title = $admin_label;
62  }
63 
64  // Prevent items from not having index set
65  $item = wp_parse_args( $item, array(
66  'label_text' => $title,
67  'field_id' => null,
68  'parent_label' => null,
69  'label_type' => null,
70  'input_type' => null,
71  'settings_html' => null,
72  'adminLabel' => null,
73  'adminOnly' => null,
74  'subtitle' => null,
75  'placeholder' => null,
76  'icon' => null,
77  ) );
78 
79  $this->title = $title;
80  $this->item = $item;
81  $this->id = $item_id;
82  $this->form_id = $form_id;
83  $this->form = $form;
84  $this->settings = $settings;
85  $this->label_type = $item['label_type'];
86  }
87 
88  /**
89  * When echoing this class, print the HTML output
90  * @return string HTML output of the class
91  */
92  public function __toString() {
93 
94  return $this->getOutput();
95  }
96 
97  /**
98  * Overridden by child classes
99  * @return array Array of content with arrays for each item. Those arrays have `value`, `label` and (optional) `class` keys
100  */
101  protected function additional_info() {
102  return array();
103  }
104 
105  /**
106  * Generate the output for a field based on the additional_info() output
107  *
108  * @see GravityView_Admin_View_Item::additional_info()
109  * @param boolean $html Display HTML output? If yes, output is wrapped in spans. If no, plaintext.
110  * @return string|null If empty, return null. Otherwise, return output HTML/text.
111  */
112  protected function get_item_info( $html = true ) {
113 
114  $output = NULL;
115  $field_info_items = $this->additional_info();
116 
117  /**
118  * @filter `gravityview_admin_label_item_info` Tap in to modify the field information displayed next to an item
119  *
120  * @param array $field_info_items Additional information to display in a field
121  * @param GravityView_Admin_View_Field $this Field shown in the admin
122  */
123  $field_info_items = apply_filters( 'gravityview_admin_label_item_info', $field_info_items, $this );
124 
125  if ( $html ) {
126 
127  foreach ( $field_info_items as $item ) {
128 
129  if( \GV\Utils::get( $item, 'hide_in_picker', false ) ) {
130  continue;
131  }
132 
133  $class = isset( $item['class'] ) ? sanitize_html_class( $item['class'] ) . ' description' : 'description';
134  // Add the title in case the value's long, in which case, it'll be truncated by CSS.
135  $output .= '<span class="' . $class . '">';
136  $output .= esc_html( $item['value'] );
137  $output .= '</span>';
138  }
139 
140  } else {
141 
142  $values = wp_list_pluck( $field_info_items, 'value' );
143 
144  $output = esc_html( implode( "\n", $values ) );
145 
146  }
147 
148  return empty( $output ) ? NULL : $output;
149  }
150 
151  /**
152  * Generate HTML for field or a widget modal
153  *
154  * @return string
155  */
156  function getOutput() {
157 
158  $settings_title = sprintf( __( 'Configure %s Settings', 'gk-gravityview' ), esc_html( rgar( $this->item, 'label', ucfirst( $this->label_type ?: '' ) ) ) );
159  $delete_title = sprintf( __( 'Remove %s', 'gk-gravityview' ), ucfirst( $this->label_type ?: '' ) );
160  $single_link_title = __( 'This field links to the Single Entry', 'gk-gravityview' );
161  $visibility_title = __( 'This field has modified visibility', 'gk-gravityview' );
162 
163  // $settings_html will just be hidden inputs if empty. Otherwise, it'll have an <ul>. Ugly hack, I know.
164  // TODO: Un-hack this
165  $hide_settings_link_class = ( empty( $this->item['settings_html'] ) || strpos( $this->item['settings_html'], '<!-- No Options -->' ) > 0 ) ? 'hide-if-js' : '';
166  $settings_link = sprintf( '<button class="gv-field-settings %2$s" title="%1$s" aria-label="%1$s"><span class="dashicons-admin-generic dashicons"></span></button>', esc_attr( $settings_title ), $hide_settings_link_class );
167 
168  // When a field label is empty, use the Field ID
169  $label = empty( $this->title ) ? sprintf( _x( 'Field #%s (No Label)', 'Label in field picker for empty label', 'gk-gravityview' ), $this->id ) : $this->title;
170 
171  // If there's a custom label, and show label is checked, use that as the field heading
172  if ( ! empty( $this->settings['custom_label'] ) && ! empty( $this->settings['show_label'] ) ) {
173  $label = $this->settings['custom_label'];
174  } else if ( ! empty( $this->item['customLabel'] ) ) {
175  $label = $this->item['customLabel'];
176  }
177  $label = esc_attr( $label );
178 
179  $field_icon = '';
180 
181  $form = ! empty( $this->form ) ? $this->form : false;
182  $form = ! empty( $this->form_id ) ? GVCommon::get_form( $this->form_id ) : false;
183 
184  $nonexistent_form_field = $form && $this->id && preg_match('/^\d+\.\d+$|^\d+$/', $this->id) && ! gravityview_get_field( $form, $this->id );
185 
186  if ( $this->item['icon'] && ! \GV\Utils::get( $this->item, 'parent' ) ) {
187 
188  $has_gf_icon = ( false !== strpos( $this->item['icon'], 'gform-icon' ) );
189  $has_dashicon = ( false !== strpos( $this->item['icon'], 'dashicons' ) );
190 
191  if ( 0 === strpos( $this->item['icon'], 'data:' ) ) {
192  // Inline icon SVG
193  $field_icon = '<i class="dashicons background-icon" style="background-image: url(\'' . esc_attr( $this->item['icon'] ) . '\');"></i>';
194  } elseif( $has_gf_icon && gravityview()->plugin->is_GF_25() ) {
195  // Gravity Forms icon font
196  $field_icon = '<i class="gform-icon ' . esc_attr( $this->item['icon'] ) . '"></i>';
197  } elseif( $has_dashicon ) {
198  // Dashicon; prefix with "dashicons"
199  $field_icon = '<i class="dashicons ' . esc_attr( $this->item['icon'] ) . '"></i>';
200  } else {
201  // Not dashicon icon
202  $field_icon = '<i class="' . esc_attr( $this->item['icon'] ) . '"></i>';
203  }
204 
205  $field_icon = $field_icon . ' ';
206  } elseif( \GV\Utils::get( $this->item, 'parent' ) ) {
207  $field_icon = '<i class="gv-icon gv-icon-level-down"></i>' . ' ';
208  }
209 
210  $output = '<button class="gv-add-field screen-reader-text">' . sprintf( esc_html__( 'Add "%s"', 'gk-gravityview' ), $label ) . '</button>';
211  $title = esc_attr( sprintf( __( 'Field: %s', 'gk-gravityview' ), $label ) );
212  if ( ! $nonexistent_form_field ) {
213  $title .= "\n" . $this->get_item_info( false );
214  } else {
215  $output = '';
216  $settings_link = '';
217  $label = '<span class="dashicons-warning dashicons"></span> ' . esc_html( sprintf( __( 'The field connected to "%s" was deleted from the form. The associated entry data no longer exists.', 'gk-gravityview' ), $label ) );
218  }
219 
220  $output .= '<h5 class="selectable gfield field-id-' . esc_attr( $this->id ) . '">';
221 
222  $output .= '<span class="gv-field-controls">' . $settings_link . $this->get_indicator_icons() . '<button class="gv-remove-field" aria-label="' . esc_attr( $delete_title ) . '" title="' . esc_attr( $delete_title ) . '"><span class="dashicons-dismiss dashicons"></span></button></span>';
223 
224  $output .= '<span class="gv-field-label" data-original-title="' . esc_attr( $label ) . '" title="' . $title . '">' . $field_icon . '<span class="gv-field-label-text-container">' . $label . '</span></span>';
225 
226  // Displays only in the field/widget picker
227  if ( ! $nonexistent_form_field && $field_info = $this->get_item_info() ) {
228  $output .= '<span class="gv-field-info">' . $field_info . '</span>';
229  }
230 
231  $output .= '</h5>';
232 
233  $container_class = ! empty( $this->item['parent'] ) ? ' gv-child-field' : '';
234 
235  $container_class .= $nonexistent_form_field ? ' gv-nonexistent-form-field' : '';
236 
237  $container_class .= empty( $this->settings['show_as_link'] ) ? '' : ' has-single-entry-link';
238 
239  $container_class .= empty( $this->settings['only_loggedin'] ) ? '' : ' has-custom-visibility';
240 
241  $data_form_id = $form ? ' data-formid="' . esc_attr( $this->form_id ) . '"' : '';
242 
243  $data_parent_label = ! empty( $this->item['parent'] ) ? ' data-parent-label="' . esc_attr( $this->item['parent']['label'] ) . '"' : '';
244 
245  $output = '<div data-fieldid="' . esc_attr( $this->id ) . '" ' . $data_form_id . $data_parent_label . ' data-inputtype="' . esc_attr( $this->item['input_type'] ) . '" class="gv-fields' . $container_class . '">' . $output . $this->item['settings_html'] . '</div>';
246 
247  return $output;
248  }
249 
250  /**
251  * Returns array of item icons used to represent field settings state
252  *
253  * Has `gravityview/admin/indicator_icons` filter for other components to modify displayed icons.
254  *
255  * @since 2.9.5
256  *
257  * @return string HTML output of icons
258  */
259  private function get_indicator_icons() {
260 
261  $icons = array(
262  'show_as_link' => array(
263  'visible' => ( ! empty( $this->settings['show_as_link'] ) ),
264  'title' => __( 'This field links to the Single Entry', 'gk-gravityview' ),
265  'css_class' => 'dashicons dashicons-media-default icon-link-to-single-entry',
266  ),
267  'only_loggedin' => array(
268  'visible' => ( \GV\Utils::get( $this->settings, 'only_loggedin' ) || isset( $this->settings['allow_edit_cap'] ) && 'read' !== $this->settings['allow_edit_cap'] ),
269  'title' => __( 'This field has modified visibility', 'gk-gravityview' ),
270  'css_class' => 'dashicons dashicons-lock icon-custom-visibility',
271  ),
272  );
273 
274  $output = '';
275 
276  /**
277  * @filter `gravityview/admin/indicator_icons` Modify the icon output to add additional indicator icons
278  * @internal This is currently internally used. Consider not relying on it until further notice :-)
279  * @param array $icons Array of icons to be shown, with `visible`, `title`, `css_class` keys.
280  * @param array $item_settings Settings for the current item (widget or field)
281  */
282  $icons = (array) apply_filters( 'gravityview/admin/indicator_icons', $icons, $this->settings );
283 
284  foreach ( $icons as $icon ) {
285 
286  if ( empty( $icon['css_class'] ) || empty( $icon['title'] ) ) {
287  continue;
288  }
289 
290  $css_class = trim( $icon['css_class'] );
291 
292  if ( empty( $icon['visible'] ) ) {
293  $css_class .= ' hide-if-js';
294  }
295 
296  $output .= '<span class="' . gravityview_sanitize_html_class( $css_class ) . '" title="' . esc_attr( $icon['title'] ) . '"></span>';
297  }
298 
299  return $output;
300  }
301 
302 }
__toString()
When echoing this class, print the HTML output.
get_item_info( $html=true)
Generate the output for a field based on the additional_info() output.
$class
gravityview_get_field( $form, $field_id)
Returns the field details array of a specific form given the field id.
getOutput()
Generate HTML for field or a widget modal.
A field or widget in GravityView view configuration.
additional_info()
Overridden by child classes.
__construct( $title='', $item_id='', $item=array(), $settings=array(), $form_id=null, $form=array())
static get_form( $form_id)
Returns the form object for a given Form ID.