GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-admin-views.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Renders all the metaboxes on Add New / Edit View post type.
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.0.0
12  */
13 
14 /** If this file is called directly, abort. */
15 if ( ! defined( 'ABSPATH' ) ) {
16  die;
17 }
18 
20 
21 
22 
23  function __construct() {
24 
25  add_action( 'save_post', array( $this, 'save_postdata' ) );
26 
27  // set the blacklist field types across the entire plugin
28  add_filter( 'gravityview_blacklist_field_types', array( $this, 'default_field_blacklist' ), 10, 2 );
29 
30  // Tooltips
31  add_filter( 'gform_tooltips', array( $this, 'tooltips') );
32 
33  // adding styles and scripts
34  add_action( 'admin_enqueue_scripts', array( 'GravityView_Admin_Views', 'add_scripts_and_styles'), 999 );
35  add_filter( 'gform_noconflict_styles', array( $this, 'register_no_conflict') );
36  add_filter( 'gform_noconflict_scripts', array( $this, 'register_no_conflict') );
37  add_filter( 'gravityview_noconflict_styles', array( $this, 'register_no_conflict') );
38  add_filter( 'gravityview_noconflict_scripts', array( $this, 'register_no_conflict') );
39 
40  add_action( 'gravityview_render_directory_active_areas', array( $this, 'render_directory_active_areas'), 10, 4 );
41  add_action( 'gravityview_render_widgets_active_areas', array( $this, 'render_widgets_active_areas'), 10, 3 );
42  add_action( 'gravityview_render_available_fields', array( $this, 'render_available_fields'), 10, 2 );
43  add_action( 'gravityview_render_available_widgets', array( $this, 'render_available_widgets') );
44  add_action( 'gravityview_render_active_areas', array( $this, 'render_active_areas'), 10, 5 );
45 
46  // @todo check if this hook is needed..
47  //add_action( 'gravityview_render_field_options', array( $this, 'render_field_options'), 10, 9 );
48 
49  // Add Connected Form column
50  add_filter('manage_gravityview_posts_columns' , array( $this, 'add_post_type_columns' ) );
51 
52  add_filter( 'gform_toolbar_menu', array( 'GravityView_Admin_Views', 'gform_toolbar_menu' ), 10, 2 );
53 
54  add_action( 'manage_gravityview_posts_custom_column', array( $this, 'add_custom_column_content'), 10, 2 );
55 
56  add_action( 'restrict_manage_posts', array( $this, 'add_view_dropdown' ) );
57 
58  add_action( 'pre_get_posts', array( $this, 'filter_pre_get_posts_by_gravityview_form_id' ) );
59 
60  }
61 
62  /**
63  * @since 1.15
64  * @param WP_Query $query
65  */
66  public function filter_pre_get_posts_by_gravityview_form_id( &$query ) {
67  global $pagenow;
68 
69  if ( !is_admin() ) {
70  return;
71  }
72 
73  if( 'edit.php' !== $pagenow || ! rgget( 'gravityview_form_id' ) || ! isset( $query->query_vars[ 'post_type' ] ) ) {
74  return;
75  }
76 
77  if ( $query->query_vars[ 'post_type' ] == 'gravityview' ) {
78  $query->set( 'meta_query', array(
79  array(
80  'key' => '_gravityview_form_id',
81  'value' => rgget( 'gravityview_form_id' ),
82  )
83  ) );
84  }
85  }
86 
87  function add_view_dropdown() {
88  $current_screen = get_current_screen();
89 
90  if( 'gravityview' !== $current_screen->post_type ) {
91  return;
92  }
93 
95  $current_form = rgget( 'gravityview_form_id' );
96  // If there are no forms to select, show no forms.
97  if( !empty( $forms ) ) { ?>
98  <select name="gravityview_form_id" id="gravityview_form_id">
99  <option value="" <?php selected( '', $current_form, true ); ?>><?php esc_html_e( 'All forms', 'gravityview' ); ?></option>
100  <?php foreach( $forms as $form ) { ?>
101  <option value="<?php echo $form['id']; ?>" <?php selected( $form['id'], $current_form, true ); ?>><?php echo esc_html( $form['title'] ); ?></option>
102  <?php } ?>
103  </select>
104  <?php }
105  }
106 
107 
108  /**
109  * @deprecated since 1.2
110  * Start using GravityView_Render_Settings::render_setting_row
111  */
112  public static function render_setting_row( $key = '', $current_settings = array(), $override_input = null, $name = 'template_settings[%s]', $id = 'gravityview_se_%s' ) {
113  _deprecated_function( 'GravityView_Admin_Views::render_setting_row', '1.1.7', 'GravityView_Render_Settings::render_setting_row' );
114  GravityView_Render_Settings::render_setting_row( $key, $current_settings, $override_input, $name , $id );
115  }
116 
117  /**
118  * @deprecated since 1.2
119  * Start using GravityView_Render_Settings::render_field_option
120  */
121  public static function render_field_option( $name = '', $option, $curr_value = NULL ) {
122  _deprecated_function( 'GravityView_Admin_Views::render_field_option', '1.1.7', 'GravityView_Render_Settings::render_field_option' );
123  return GravityView_Render_Settings::render_field_option( $name, $option, $curr_value );
124  }
125 
126 
127  /**
128  * Add a GravityView menu to the Form Toolbar with connected views
129  * @param array $menu_items Menu items, as set in GFForms::top_toolbar()
130  * @param int $id ID of the current Gravity form
131  * @return array Modified array
132  */
133  static function gform_toolbar_menu( $menu_items = array(), $id = NULL ) {
134 
135  $connected_views = gravityview_get_connected_views( $id );
136 
137  if( empty( $connected_views ) ) {
138 
139  $menu_items['gravityview'] = array(
140  'label' => esc_attr__( 'Create a View', 'gravityview' ),
141  'icon' => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>',
142  'title' => esc_attr__( 'Create a View using this form as a data source', 'gravityview' ),
143  'url' => admin_url( 'post-new.php?post_type=gravityview&form_id=' . $id ),
144  'menu_class' => 'gv_connected_forms gf_form_toolbar_settings',
145  'priority' => 0,
146  'capabilities' => array( 'edit_gravityviews' ),
147  );
148 
149  return $menu_items;
150  }
151 
152  $sub_menu_items = array();
153  foreach ( (array)$connected_views as $view ) {
154 
155  if( ! GVCommon::has_cap( 'edit_gravityview', $view->ID ) ) {
156  continue;
157  }
158 
159  $label = empty( $view->post_title ) ? sprintf( __('No Title (View #%d)', 'gravityview' ), $view->ID ) : $view->post_title;
160 
161  $sub_menu_items[] = array(
162  'label' => esc_attr( $label ),
163  'url' => admin_url( 'post.php?action=edit&post='.$view->ID ),
164  );
165  }
166 
167  // If there were no items added, then let's create the parent menu
168  if( $sub_menu_items ) {
169 
170  $sub_menu_items[] = array(
171  'label' => esc_attr__( 'Create a View', 'gravityview' ),
172  'link_class' => 'gv-create-view',
173  'title' => esc_attr__( 'Create a View using this form as a data source', 'gravityview' ),
174  'url' => admin_url( 'post-new.php?post_type=gravityview&form_id=' . $id ),
175  'capabilities' => array( 'edit_gravityviews' ),
176  );
177 
178  // Make sure Gravity Forms uses the submenu; if there's only one item, it uses a link instead of a dropdown
179  $sub_menu_items[] = array(
180  'url' => '#',
181  'label' => '',
182  'menu_class' => 'hidden',
183  'capabilities' => '',
184  );
185 
186  $menu_items['gravityview'] = array(
187  'label' => __( 'Connected Views', 'gravityview' ),
188  'icon' => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>',
189  'title' => __( 'GravityView Views using this form as a data source', 'gravityview' ),
190  'url' => '#',
191  'onclick' => 'return false;',
192  'menu_class' => 'gv_connected_forms gf_form_toolbar_settings',
193  'sub_menu_items' => $sub_menu_items,
194  'priority' => 0,
195  'capabilities' => array( 'edit_gravityviews' ),
196  );
197  }
198 
199  return $menu_items;
200  }
201 
202  /**
203  * List the field types without presentation properties (on a View context)
204  *
205  * @param array $array Existing field types to add to a blacklist
206  * @param string|null $context Context for the blacklist. Default: NULL.
207  * @access public
208  * @return array Default blacklist fields merged with existing blacklist fields
209  */
210  function default_field_blacklist( $array = array(), $context = NULL ) {
211 
212  $add = array( 'captcha', 'page' );
213 
214  // Don't allowing editing the following values:
215  if( $context === 'edit' ) {
216  $add[] = 'post_id';
217  }
218 
219  $return = array_merge( $array, $add );
220 
221  return $return;
222  }
223 
224  /**
225  * Add tooltip text for use throughout the UI
226  * @param array $tooltips Array of Gravity Forms tooltips
227  * @return array Modified tooltips array
228  */
229  public function tooltips( $tooltips = array() ) {
230 
231  $gv_tooltips = array();
232 
233  // Generate tooltips for View settings
234  $default_args = GravityView_View_Data::get_default_args( true );
235 
236  foreach ( $default_args as $key => $arg ) {
237 
238  // If an arg has `tooltip` defined, but it's false, don't display a tooltip
239  if( isset( $arg['tooltip'] ) && empty( $arg['tooltip'] ) ) { continue; }
240 
241  // By default, use `tooltip` if defined.
242  $tooltip = empty( $arg['tooltip'] ) ? NULL : $arg['tooltip'];
243 
244  // Otherwise, use the description as a tooltip.
245  if( empty( $tooltip ) && !empty( $arg['desc'] ) ) {
246  $tooltip = $arg['desc'];
247  }
248 
249  // If there's no tooltip set, continue
250  if( empty( $tooltip ) ) {
251  continue;
252  }
253 
254  // Add the tooltip
255  $gv_tooltips[ 'gv_'.$key ] = array(
256  'title' => $arg['label'],
257  'value' => $tooltip,
258  );
259 
260  }
261 
262  $gv_tooltips['gv_css_merge_tags'] = array(
263  'title' => __('CSS Merge Tags', 'gravityview'),
264  'value' => sprintf( __( 'Developers: The CSS classes will be sanitized using the %ssanitize_title_with_dashes()%s function.', 'gravityview'), '<code>', '</code>' )
265  );
266 
267  /**
268  * @filter `gravityview_tooltips` The tooltips GravityView adds to the Gravity Forms tooltip array
269  * @param array $gv_tooltips Associative array with unique keys containing array of `title` and `value` keys, as expected by `gform_tooltips` filter
270  */
271  $gv_tooltips = apply_filters( 'gravityview_tooltips', $gv_tooltips );
272 
273  foreach ( $gv_tooltips as $key => $tooltip ) {
274 
275  $title = empty( $tooltip['title'] ) ? '' : '<h6>'.esc_html( $tooltip['title'] ) .'</h6>';
276 
277  $tooltips[ $key ] = $title . wpautop( esc_html( $tooltip['value'] ) );
278  }
279 
280  return $tooltips;
281  }
282 
283  /**
284  * Add the Data Source information
285  *
286  * @param null $column_name
287  * @param $post_id
288  *
289  * @return void
290  */
291  public function add_custom_column_content( $column_name = NULL, $post_id ) {
292 
293  $output = '';
294 
295  switch ( $column_name ) {
296  case 'gv_template':
297 
298  $template_id = gravityview_get_template_id( $post_id );
299 
300  // All Views should have a connected form. If it doesn't, that's not right.
301  if ( empty( $template_id ) ) {
302  do_action( 'gravityview_log_error', sprintf( __METHOD__ . ' View ID %s does not have a connected template.', $post_id ) );
303  break;
304  }
305 
307 
308  $template = isset( $templates[ $template_id ] ) ? $templates[ $template_id ] : false;
309 
310  // Generate backup if label doesn't exist: `example_name` => `Example Name`
311  $template_id_pretty = ucwords( implode( ' ', explode( '_', $template_id ) ) );
312 
313  $output = $template ? $template['label'] : $template_id_pretty;
314 
315  break;
316 
317  case 'gv_connected_form':
318 
319  $form_id = gravityview_get_form_id( $post_id );
320 
321  // All Views should have a connected form. If it doesn't, that's not right.
322  if ( empty( $form_id ) ) {
323  do_action( 'gravityview_log_error', sprintf( '[add_data_source_column_content] View ID %s does not have a connected GF form.', $post_id ) );
324  $output = __( 'Not connected.', 'gravityview' );
325  break;
326  }
327 
329 
330  if ( ! $form ) {
331  do_action( 'gravityview_log_error', sprintf( '[add_data_source_column_content] Connected form not found: Form #%d', $form_id ) );
332 
333  $output = __( 'The connected form can not be found; it may no longer exist.', 'gravityview' );
334  } else {
335  $output = self::get_connected_form_links( $form );
336  }
337 
338  break;
339  }
340 
341  echo $output;
342  }
343 
344 
345  /**
346  * Get HTML links relating to a connected form, like Edit, Entries, Settings, Preview
347  * @param array|int $form Gravity Forms forms array, or the form ID
348  * @param boolean $include_form_link Whether to include the bold name of the form in the output
349  * @return string HTML links
350  */
351  static public function get_connected_form_links( $form, $include_form_link = true ) {
352 
353  // Either the form is empty or the form ID is 0, not yet set.
354  if( empty( $form ) ) {
355  return '';
356  }
357 
358  // The $form is passed as the form ID
359  if( !is_array( $form ) ) {
361  }
362 
363  $form_id = $form['id'];
364  $links = array();
365 
366  if( GVCommon::has_cap( 'gravityforms_edit_forms' ) ) {
367  $form_url = admin_url( sprintf( 'admin.php?page=gf_edit_forms&amp;id=%d', $form_id ) );
368  $form_link = '<strong class="gv-form-title">'.gravityview_get_link( $form_url, $form['title'], 'class=row-title' ).'</strong>';
369  $links[] = '<span>'.gravityview_get_link( $form_url, __('Edit Form', 'gravityview') ).'</span>';
370  } else {
371  $form_link = '<strong class="gv-form-title">'. esc_html( $form['title'] ). '</strong>';
372  }
373 
374  if( GVCommon::has_cap( 'gravityforms_view_entries' ) ) {
375  $entries_url = admin_url( sprintf( 'admin.php?page=gf_entries&amp;id=%d', $form_id ) );
376  $links[] = '<span>'.gravityview_get_link( $entries_url, __('Entries', 'gravityview') ).'</span>';
377  }
378 
379  if( GVCommon::has_cap( array( 'gravityforms_edit_settings', 'gravityview_view_settings' ) ) ) {
380  $settings_url = admin_url( sprintf( 'admin.php?page=gf_edit_forms&amp;view=settings&amp;id=%d', $form_id ) );
381  $links[] = '<span>'.gravityview_get_link( $settings_url, __('Settings', 'gravityview'), 'title='.__('Edit settings for this form', 'gravityview') ).'</span>';
382  }
383 
384  if( GVCommon::has_cap( array("gravityforms_edit_forms", "gravityforms_create_form", "gravityforms_preview_forms") ) ) {
385  $preview_url = site_url( sprintf( '?gf_page=preview&amp;id=%d', $form_id ) );
386  $links[] = '<span>'.gravityview_get_link( $preview_url, __('Preview Form', 'gravityview'), 'title='.__('Preview this form', 'gravityview') ).'</span>';
387  }
388 
389  $output = '';
390 
391  if( !empty( $include_form_link ) ) {
392  $output .= $form_link;
393  }
394 
395  /**
396  * @filter `gravityview_connected_form_links` Modify the links shown in the Connected Form links
397  * @since 1.6
398  * @param array $links Links to show
399  * @param array $form Gravity Forms form array
400  */
401  $links = apply_filters( 'gravityview_connected_form_links', $links, $form );
402 
403  $output .= '<div class="row-actions">'. implode( ' | ', $links ) .'</div>';
404 
405  return $output;
406  }
407 
408  /**
409  * Add the Data Source column to the Views page
410  * @param array $columns Columns array
411  */
412  public function add_post_type_columns( $columns ) {
413 
414  // Get the date column and save it for later to add back in.
415  // This adds it after the Data Source column.
416  // This way, we don't need to do array_slice, array_merge, etc.
417  $date = $columns['date'];
418  unset( $columns['date'] );
419 
420  $data_source_required_caps = array(
421  'gravityforms_edit_forms',
422  'gravityforms_view_entries',
423  'gravityforms_edit_settings',
424  'gravityview_view_settings',
425  'gravityforms_create_form',
426  'gravityforms_preview_forms',
427  );
428 
429  if( GVCommon::has_cap( $data_source_required_caps ) ) {
430  $columns['gv_connected_form'] = __( 'Data Source', 'gravityview' );
431  }
432 
433  $columns['gv_template'] = _x( 'Template', 'Column title that shows what template is being used for Views', 'gravityview' );
434 
435  // Add the date back in.
436  $columns['date'] = $date;
437 
438  return $columns;
439  }
440 
441  /**
442  * Save View configuration
443  *
444  * @access public
445  * @param int $post_id Currently saved Post ID
446  * @return void
447  */
448  function save_postdata( $post_id ) {
449 
450  if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
451  return;
452  }
453 
454  // validate post_type
455  if ( ! isset( $_POST['post_type'] ) || 'gravityview' != $_POST['post_type'] ) {
456  return;
457  }
458 
459  // validate user can edit and save View
460  if ( ! GVCommon::has_cap( 'edit_gravityview', $post_id ) ) {
461  do_action( 'gravityview_log_error', __METHOD__ . ' - Current user does not have the capability to edit View #' . $post_id, wp_get_current_user() );
462  return;
463  }
464 
465  do_action( 'gravityview_log_debug', '[save_postdata] Saving View post type.', $_POST );
466 
467  $statii = array();
468 
469  // check if this is a start fresh View
470  if ( isset( $_POST['gravityview_select_form_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_form_nonce'], 'gravityview_select_form' ) ) {
471 
472  $form_id = !empty( $_POST['gravityview_form_id'] ) ? $_POST['gravityview_form_id'] : '';
473  // save form id
474  $statii['form_id'] = update_post_meta( $post_id, '_gravityview_form_id', $form_id );
475 
476  }
477 
478  if( false === GVCommon::has_cap( 'gravityforms_create_form' ) && empty( $statii['form_id'] ) ) {
479  do_action( 'gravityview_log_error', __METHOD__ . ' - Current user does not have the capability to create a new Form.', wp_get_current_user() );
480  return;
481  }
482 
483  // Was this a start fresh?
484  if ( ! empty( $_POST['gravityview_form_id_start_fresh'] ) ) {
485  $statii['start_fresh'] = add_post_meta( $post_id, '_gravityview_start_fresh', 1 );
486  } else {
487  $statii['start_fresh'] = delete_post_meta( $post_id, '_gravityview_start_fresh' );
488  }
489 
490  // Check if we have a template id
491  if ( isset( $_POST['gravityview_select_template_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_template_nonce'], 'gravityview_select_template' ) ) {
492 
493  $template_id = !empty( $_POST['gravityview_directory_template'] ) ? $_POST['gravityview_directory_template'] : '';
494 
495  // now save template id
496  $statii['directory_template'] = update_post_meta( $post_id, '_gravityview_directory_template', $template_id );
497  }
498 
499 
500  // save View Configuration metabox
501  if ( isset( $_POST['gravityview_view_configuration_nonce'] ) && wp_verify_nonce( $_POST['gravityview_view_configuration_nonce'], 'gravityview_view_configuration' ) ) {
502 
503  // template settings
504  if( empty( $_POST['template_settings'] ) ) {
505  $_POST['template_settings'] = array();
506  }
507  $statii['template_settings'] = update_post_meta( $post_id, '_gravityview_template_settings', $_POST['template_settings'] );
508 
509  $fields = array();
510 
511  // Directory&single Visible Fields
512  if( !empty( $preset_fields ) ) {
513 
514  $fields = $preset_fields;
515 
516  } elseif( !empty( $_POST['gv_fields'] ) ) {
518  }
519 
520  $statii['directory_fields'] = update_post_meta( $post_id, '_gravityview_directory_fields', $fields );
521 
522  // Directory Visible Widgets
523  if( empty( $_POST['widgets'] ) ) {
524  $_POST['widgets'] = array();
525  }
526  $statii['directory_widgets'] = gravityview_set_directory_widgets( $post_id, $_POST['widgets'] );
527 
528  } // end save view configuration
529 
530  /**
531  * @action `gravityview_view_saved` After a View has been saved in the admin
532  * @param int $post_id ID of the View that has been saved
533  * @param array $statii Array of statuses of the post meta saving processes. If saving worked, each key should be mapped to a value of the post ID (`directory_widgets` => `124`). If failed (or didn't change), the value will be false.
534  * @since 1.17.2
535  */
536  do_action('gravityview_view_saved', $post_id, $statii );
537 
538  do_action('gravityview_log_debug', '[save_postdata] Update Post Meta Statuses (also returns false if nothing changed)', array_map( 'intval', $statii ) );
539  }
540 
541  /**
542  * @deprecated 1.1.6
543  */
544  function render_label() {
545  _deprecated_function( 'GravityView_Admin_Views::render_label()', '1.1.6', 'Use the GravityView_Admin_View_Field class instead.' );
546  }
547 
548  /**
549  * Render html for displaying available fields based on a Form ID
550  * $blacklist_field_types - contains the field types which are not proper to be shown in a directory.
551  *
552  * @see GravityView_Ajax::get_available_fields_html() Triggers `gravityview_render_available_fields` action
553  * @access public
554  *
555  * @param int $form Gravity Forms Form ID (default: '')
556  * @param string $context (default: 'single')
557  *
558  * @return void
559  */
560  function render_available_fields( $form = 0, $context = 'single' ) {
561 
562  /**
563  * @filter `gravityview_blacklist_field_types` Modify the types of fields that shouldn't be shown in a View.
564  * @param[in,out] array $blacklist_field_types Array of field types to block for this context.
565  * @param[in] string $context View context ('single', 'directory', or 'edit')
566  */
567  $blacklist_field_types = apply_filters( 'gravityview_blacklist_field_types', array(), $context );
568 
569  $fields = $this->get_available_fields( $form, $context );
570 
571  $output = '';
572 
573  if( !empty( $fields ) ) {
574 
575  foreach( $fields as $id => $details ) {
576 
577  if( in_array( $details['type'], $blacklist_field_types ) ) {
578  continue;
579  }
580 
581  // Edit mode only allows editing the parent fields, not single inputs.
582  if( $context === 'edit' && !empty( $details['parent'] ) ) {
583  continue;
584  }
585 
586  $output .= new GravityView_Admin_View_Field( $details['label'], $id, $details );
587 
588  } // End foreach
589  }
590 
591  echo $output;
592 
593  // For the EDIT view we only want to allow the form fields.
594  if( $context === 'edit' ) {
595  return;
596  }
597 
598  $this->render_additional_fields( $form, $context );
599  }
600 
601  /**
602  * Render html for displaying additional fields based on a Form ID
603  *
604  * @access public
605  * @param int $form Gravity Forms Form ID (default: '')
606  * @param string $context (default: 'single')
607  * @return void
608  */
609  public function render_additional_fields( $form = 0, $context = 'single' ) {
610 
611  /**
612  * @filter `gravityview_additional_fields` non-standard Fields to show at the bottom of the field picker
613  * @param array $additional_fields Associative array of field arrays, with `label_text`, `desc`, `field_id`, `label_type`, `input_type`, `field_options`, and `settings_html` keys
614  */
615  $additional_fields = apply_filters( 'gravityview_additional_fields', array(
616  array(
617  'label_text' => __( '+ Add All Fields', 'gravityview' ),
618  'desc' => __('Add all the available fields at once.', 'gravityview'),
619  'field_id' => 'all-fields',
620  'label_type' => 'field',
621  'input_type' => NULL,
622  'field_options' => NULL,
623  'settings_html' => NULL,
624  )
625  ));
626 
627  if( !empty( $additional_fields )) {
628  foreach ( (array)$additional_fields as $item ) {
629 
630  // Prevent items from not having index set
631  $item = wp_parse_args( $item, array(
632  'label_text' => NULL,
633  'field_id' => NULL,
634  'label_type' => NULL,
635  'input_type' => NULL,
636  'field_options' => NULL,
637  'settings_html' => NULL,
638  ));
639 
640  // Backward compat.
641  if( !empty( $item['field_options'] ) ) {
642  // Use settings_html from now on.
643  $item['settings_html'] = $item['field_options'];
644  }
645 
646  // Render a label for each of them
647  echo new GravityView_Admin_View_Field( $item['label_text'], $item['field_id'], $item );
648 
649  }
650  }
651 
652  }
653 
654  /**
655  * Retrieve the default fields id, label and type
656  * @param string|array $form form_ID or form object
657  * @param string $zone Either 'single', 'directory', 'header', 'footer'
658  * @return array
659  */
660  function get_entry_default_fields($form, $zone) {
661 
662  $entry_default_fields = array();
663 
664  if( in_array( $zone, array( 'directory', 'single' ) ) ) {
665 
666  $entry_default_fields = array(
667  'id' => array(
668  'label' => __('Entry ID', 'gravityview'),
669  'type' => 'id',
670  'desc' => __('The unique ID of the entry.', 'gravityview'),
671  ),
672  'date_created' => array(
673  'label' => __('Entry Date', 'gravityview'),
674  'desc' => __('The date the entry was created.', 'gravityview'),
675  'type' => 'date_created',
676  ),
677  'source_url' => array(
678  'label' => __('Source URL', 'gravityview'),
679  'type' => 'source_url',
680  'desc' => __('The URL of the page where the form was submitted.', 'gravityview'),
681  ),
682  'ip' => array(
683  'label' => __('User IP', 'gravityview'),
684  'type' => 'ip',
685  'desc' => __('The IP Address of the user who created the entry.', 'gravityview'),
686  ),
687  'created_by' => array(
688  'label' => __('User', 'gravityview'),
689  'type' => 'created_by',
690  'desc' => __('Details of the logged-in user who created the entry (if any).', 'gravityview'),
691  ),
692 
693  /**
694  * @since 1.7.2
695  */
696  'other_entries' => array(
697  'label' => __('Other Entries', 'gravityview'),
698  'type' => 'other_entries',
699  'desc' => __('Display other entries created by the entry creator.', 'gravityview'),
700  ),
701  );
702 
703  if( 'single' !== $zone) {
704 
705  $entry_default_fields['entry_link'] = array(
706  'label' => __('Link to Entry', 'gravityview'),
707  'desc' => __('A dedicated link to the single entry with customizable text.', 'gravityview'),
708  'type' => 'entry_link',
709  );
710  }
711 
712  } // if not zone directory or single
713 
714  /**
715  * @since 1.2
716  */
717  $entry_default_fields['custom'] = array(
718  'label' => __('Custom Content', 'gravityview'),
719  'type' => 'custom',
720  'desc' => __('Insert custom text or HTML.', 'gravityview'),
721  );
722 
723  /**
724  * @filter `gravityview_entry_default_fields` Modify the default fields for each zone and context
725  * @param array $entry_default_fields Array of fields shown by default
726  * @param string|array $form form_ID or form object
727  * @param string $zone Either 'single', 'directory', 'header', 'footer'
728  */
729  return apply_filters( 'gravityview_entry_default_fields', $entry_default_fields, $form, $zone);
730  }
731 
732  /**
733  * Calculate the available fields
734  * @param string|array $form form_ID or form object
735  * @param string $zone Either 'single', 'directory', 'header', 'footer'
736  * @return array fields
737  */
738  function get_available_fields( $form = '', $zone = NULL ) {
739 
740  if( empty( $form ) ) {
741  do_action( 'gravityview_log_error', '[get_available_fields] $form is empty' );
742  return array();
743  }
744 
745  // get form fields
746  $fields = gravityview_get_form_fields( $form, true );
747 
748  // get meta fields ( only if form was already created )
749  if( !is_array( $form ) ) {
750  $meta_fields = gravityview_get_entry_meta( $form );
751  } else {
752  $meta_fields = array();
753  }
754 
755  // get default fields
756  $default_fields = $this->get_entry_default_fields( $form, $zone );
757 
758  //merge without loosing the keys
759  $fields = $fields + $meta_fields + $default_fields;
760 
761  return $fields;
762  }
763 
764 
765  /**
766  * Render html for displaying available widgets
767  * @return string html
768  */
770 
771  $widgets = $this->get_registered_widgets();
772 
773  if( !empty( $widgets ) ) {
774 
775  foreach( $widgets as $id => $details ) {
776 
777  echo new GravityView_Admin_View_Widget( $details['label'], $id, $details );
778 
779  }
780  }
781 
782  }
783 
784  /**
785  * Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object
786  * @since 1.13.1
787  * @return array
788  */
790  /**
791  * @filter `gravityview_register_directory_widgets` Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object
792  * @param array $registered_widgets Empty array
793  */
794  $registered_widgets = apply_filters( 'gravityview_register_directory_widgets', array() );
795 
796  return $registered_widgets;
797  }
798 
799  /**
800  * Generic function to render rows and columns of active areas for widgets & fields
801  * @param string $template_id The current slug of the selected View template
802  * @param string $type Either 'widget' or 'field'
803  * @param string $zone Either 'single', 'directory', 'header', 'footer'
804  * @param array $rows The layout structure: rows, columns and areas
805  * @param array $values Saved objects
806  * @return void
807  */
808  function render_active_areas( $template_id, $type, $zone, $rows, $values ) {
809  global $post;
810 
811  if( $type === 'widget' ) {
812  $button_label = __( 'Add Widget', 'gravityview' );
813  } else {
814  $button_label = __( 'Add Field', 'gravityview' );
815  }
816 
817  $available_items = array();
818 
819  // if saved values, get available fields to label everyone
820  if( !empty( $values ) && ( !empty( $post->ID ) || !empty( $_POST['template_id'] ) ) ) {
821 
822  if( !empty( $_POST['template_id'] ) ) {
823  $form = GravityView_Ajax::pre_get_form_fields( $_POST['template_id'] );
824  } else {
825  $form = gravityview_get_form_id( $post->ID );
826  }
827 
828  if( 'field' === $type ) {
829  $available_items = $this->get_available_fields( $form, $zone );
830  } else {
831  $available_items = $this->get_registered_widgets();
832  }
833 
834  }
835 
836  foreach( $rows as $row ) :
837  foreach( $row as $col => $areas ) :
838  $column = ($col == '2-2') ? '1-2' : $col; ?>
839 
840  <div class="gv-grid-col-<?php echo esc_attr( $column ); ?>">
841 
842  <?php foreach( $areas as $area ) : ?>
843 
844  <div class="gv-droppable-area">
845  <div class="active-drop active-drop-<?php echo esc_attr( $type ); ?>" data-areaid="<?php echo esc_attr( $zone .'_'. $area['areaid'] ); ?>">
846 
847  <?php // render saved fields
848 
849  if( !empty( $values[ $zone .'_'. $area['areaid'] ] ) ) {
850 
851  foreach( $values[ $zone .'_'. $area['areaid'] ] as $uniqid => $field ) {
852 
853  $input_type = NULL;
854  $original_item = isset( $available_items[ $field['id'] ] ) ? $available_items[ $field['id'] ] : false ;
855 
856  if( !$original_item ) {
857 
858  do_action('gravityview_log_error', 'An item was not available when rendering the output; maybe it was added by a plugin that is now de-activated.', array('available_items' => $available_items, 'field' => $field ));
859 
860  $original_item = $field;
861  } else {
862 
863  $input_type = isset( $original_item['type'] ) ? $original_item['type'] : NULL;
864 
865  }
866 
867  // Field options dialog box
868  $field_options = GravityView_Render_Settings::render_field_options( $type, $template_id, $field['id'], $original_item['label'], $zone .'_'. $area['areaid'], $input_type, $uniqid, $field, $zone, $original_item );
869 
870  $item = array(
871  'input_type' => $input_type,
872  'settings_html' => $field_options,
873  'label_type' => $type
874  );
875 
876  // Merge the values with the current item to pass things like widget descriptions and original field names
877  if( $original_item ) {
878  $item = wp_parse_args( $item, $original_item );
879  }
880 
881  switch( $type ) {
882  case 'widget':
883  echo new GravityView_Admin_View_Widget( $item['label'], $field['id'], $item, $field );
884  break;
885  default:
886  echo new GravityView_Admin_View_Field( $item['label'], $field['id'], $item, $field );
887  }
888 
889 
890  //endif;
891 
892  }
893 
894  } // End if zone is not empty ?>
895 
896  <span class="drop-message"><?php echo sprintf(esc_attr__('"+ %s" or drag existing %ss here.', 'gravityview'), $button_label, $type ); ?></span>
897  </div>
898  <div class="gv-droppable-area-action">
899  <a href="#" class="gv-add-field button-secondary" title="" data-objecttype="<?php echo esc_attr( $type ); ?>" data-areaid="<?php echo esc_attr( $zone .'_'. $area['areaid'] ); ?>" data-context="<?php echo esc_attr( $zone ); ?>"><?php echo '+ '.esc_html( $button_label ); ?></a>
900  <p class="gv-droppable-area-title"><strong><?php echo esc_html( $area['title'] ); ?></strong><?php if( !empty( $area['subtitle'] ) ) { ?><span class="gv-droppable-area-subtitle"> &ndash; <?php echo esc_html( $area['subtitle'] ); ?></span><?php } ?></p>
901  </div>
902  </div>
903 
904  <?php endforeach; ?>
905 
906  </div>
907  <?php endforeach;
908  endforeach;
909  }
910 
911  /**
912  * Render the widget active areas
913  * @param string $zone Either 'header' or 'footer'
914  * @param string $post_id Current Post ID (view)
915  * @return string html
916  */
917  function render_widgets_active_areas( $template_id = '', $zone, $post_id = '' ) {
918 
919  $default_widget_areas = GravityView_Plugin::get_default_widget_areas();
920 
921  $widgets = array();
922  if( !empty( $post_id ) ) {
923  $widgets = gravityview_get_directory_widgets( $post_id );
924  }
925 
926  ob_start();
927  ?>
928 
929  <div class="gv-grid gv-grid-pad gv-grid-border" id="directory-<?php echo $zone; ?>-widgets">
930  <?php $this->render_active_areas( $template_id, 'widget', $zone, $default_widget_areas, $widgets ); ?>
931  </div>
932 
933  <?php
934  $output = ob_get_clean();
935 
936  echo $output;
937 
938  return $output;
939  }
940 
941  /**
942  * Render the Template Active Areas and configured active fields for a given template id and post id
943  *
944  * @access public
945  * @param string $template_id (default: '') Template ID, like `default_list`, `default_table`, `preset_business_data`, etc. {@see GravityView_Template::__construct()}
946  * @param string $post_id (default: '')
947  * @param string $context (default: 'single')
948  * @return string HTML of the active areas
949  */
950  function render_directory_active_areas( $template_id = '', $context = 'single', $post_id = '', $echo = false ) {
951 
952  if( empty( $template_id ) ) {
953  do_action( 'gravityview_log_debug', '[render_directory_active_areas] $template_id is empty' );
954  return '';
955  }
956 
957  /**
958  * @filter `gravityview_template_active_areas`
959  * @see GravityView_Template::assign_active_areas()
960  * @param array $template_areas Empty array, to be filled in by the template class
961  * @param string $template_id Template ID, like `default_list`, `default_table`, `preset_business_data`, etc. {@see GravityView_Template::__construct()}
962  * @param string $context Current View context: `directory`, `single`, or `edit` (default: 'single')
963  */
964  $template_areas = apply_filters( 'gravityview_template_active_areas', array(), $template_id, $context );
965 
966  if( empty( $template_areas ) ) {
967 
968  do_action( 'gravityview_log_debug', '[render_directory_active_areas] No areas defined. Maybe template %s is disabled.', $template_id );
969  $output = '<div>';
970  $output .= '<h2 class="description" style="font-size: 16px; margin:0">'. sprintf( esc_html__( 'This View is configured using the %s View type, which is disabled.', 'gravityview' ), '<em>'.$template_id.'</em>' ) .'</h2>';
971  $output .= '<p class="description" style="font-size: 14px; margin:0 0 1em 0;padding:0">'.esc_html__('The data is not lost; re-activate the associated plugin and the configuration will re-appear.', 'gravityview').'</p>';
972  $output .= '</div>';
973  } else {
974 
975  $fields = '';
976  if ( ! empty( $post_id ) ) {
977  $fields = gravityview_get_directory_fields( $post_id );
978  }
979 
980  ob_start();
981  $this->render_active_areas( $template_id, 'field', $context, $template_areas, $fields );
982  $output = ob_get_clean();
983 
984  }
985 
986  if( $echo ) {
987  echo $output;
988  }
989 
990  return $output;
991  }
992 
993  /**
994  * Enqueue scripts and styles at Views editor
995  *
996  * @access public
997  * @param mixed $hook
998  * @return void
999  */
1000  static function add_scripts_and_styles( $hook ) {
1001  global $plugin_page, $pagenow;
1002 
1003  $is_widgets_page = ( $pagenow === 'widgets.php' );
1004 
1005  // Add the GV font (with the Astronaut)
1006  wp_enqueue_style( 'gravityview_global', plugins_url('assets/css/admin-global.css', GRAVITYVIEW_FILE), array(), GravityView_Plugin::version );
1007 
1008  wp_register_script( 'gravityview-jquery-cookie', plugins_url('assets/lib/jquery.cookie/jquery.cookie.min.js', GRAVITYVIEW_FILE), array( 'jquery' ), GravityView_Plugin::version, true );
1009 
1010  // Don't process any scripts below here if it's not a GravityView page.
1011  if( !gravityview_is_admin_page($hook) && !$is_widgets_page ) { return; }
1012 
1013  // Only enqueue the following on single pages
1014  if( gravityview_is_admin_page($hook, 'single') || $is_widgets_page ) {
1015 
1016  wp_enqueue_script( 'jquery-ui-datepicker' );
1017  wp_enqueue_style( 'gravityview_views_datepicker', plugins_url('assets/css/admin-datepicker.css', GRAVITYVIEW_FILE), GravityView_Plugin::version );
1018 
1019  $script_debug = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
1020 
1021  //enqueue scripts
1022  wp_enqueue_script( 'gravityview_views_scripts', plugins_url( 'assets/js/admin-views' . $script_debug . '.js', GRAVITYVIEW_FILE ), array( 'jquery-ui-tabs', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-ui-tooltip', 'jquery-ui-dialog', 'gravityview-jquery-cookie', 'jquery-ui-datepicker', 'underscore' ), GravityView_Plugin::version );
1023 
1024  wp_localize_script('gravityview_views_scripts', 'gvGlobals', array(
1025  'cookiepath' => COOKIEPATH,
1026  'passed_form_id' => (bool) rgget( 'form_id' ),
1027  'nonce' => wp_create_nonce( 'gravityview_ajaxviews' ),
1028  'label_viewname' => __( 'Enter View name here', 'gravityview' ),
1029  'label_close' => __( 'Close', 'gravityview' ),
1030  'label_cancel' => __( 'Cancel', 'gravityview' ),
1031  'label_continue' => __( 'Continue', 'gravityview' ),
1032  'label_ok' => __( 'Ok', 'gravityview' ),
1033  'label_publisherror' => __( 'Error while creating the View for you. Check the settings or contact GravityView support.', 'gravityview' ),
1034  'loading_text' => esc_html__( 'Loading&hellip;', 'gravityview' ),
1035  'loading_error' => esc_html__( 'There was an error loading dynamic content.', 'gravityview' ),
1036  'field_loaderror' => __( 'Error while adding the field. Please try again or contact GravityView support.', 'gravityview' ),
1037  'remove_all_fields' => __( 'Would you like to remove all fields in this zone? (You are seeing this message because you were holding down the ALT key)', 'gravityview' ),
1038  ));
1039 
1040  wp_enqueue_style( 'gravityview_views_styles', plugins_url( 'assets/css/admin-views.css', GRAVITYVIEW_FILE ), array('dashicons', 'wp-jquery-ui-dialog' ), GravityView_Plugin::version );
1041 
1042  self::enqueue_gravity_forms_scripts();
1043 
1044  } // End single page
1045  }
1046 
1047  static function enqueue_gravity_forms_scripts() {
1048  GFForms::register_scripts();
1049 
1050  $scripts = array(
1051  'sack',
1052  'gform_gravityforms',
1053  'gform_forms',
1054  'gform_form_admin',
1055  'jquery-ui-autocomplete'
1056  );
1057 
1058  if ( wp_is_mobile() ) {
1059  $scripts[] = 'jquery-touch-punch';
1060  }
1061 
1062  foreach ($scripts as $script) {
1063  wp_enqueue_script( $script );
1064  }
1065  }
1066 
1067  /**
1068  * Add GravityView scripts and styles to Gravity Forms and GravityView No-Conflict modes
1069  *
1070  * @param array $registered Existing scripts or styles that have been registered (array of the handles)
1071  *
1072  * @return array
1073  */
1074  function register_no_conflict( $registered ) {
1075 
1076  $allowed_dependencies = array();
1077 
1078  $filter = current_filter();
1079 
1080  if ( preg_match( '/script/ism', $filter ) ) {
1081 
1082  $allowed_dependencies = array(
1083  'jquery-ui-core',
1084  'jquery-ui-dialog',
1085  'jquery-ui-tabs',
1086  'jquery-ui-draggable',
1087  'jquery-ui-droppable',
1088  'jquery-ui-sortable',
1089  'jquery-ui-tooltip',
1090  'gravityview_views_scripts',
1091  'gravityview-support',
1092  'gravityview-jquery-cookie',
1093  'gravityview_views_datepicker',
1094  'sack',
1095  'gform_gravityforms',
1096  'gform_forms',
1097  'gform_form_admin',
1098  'jquery-ui-autocomplete'
1099  );
1100 
1101  } elseif ( preg_match( '/style/ism', $filter ) ) {
1102 
1103  $allowed_dependencies = array(
1104  'dashicons',
1105  'wp-jquery-ui-dialog',
1106  'gravityview_views_styles',
1107  'gravityview_global',
1108  'gravityview_views_datepicker'
1109  );
1110  }
1111 
1112  return array_merge( $registered, $allowed_dependencies );
1113  }
1114 
1115 
1116 }
1117 
register_no_conflict($registered)
Add GravityView scripts and styles to Gravity Forms and GravityView No-Conflict modes.
save_postdata($post_id)
Save View configuration.
render_additional_fields($form=0, $context= 'single')
Render html for displaying additional fields based on a Form ID.
$forms
Definition: data-source.php:20
tooltips($tooltips=array())
Add tooltip text for use throughout the UI.
filter_pre_get_posts_by_gravityview_form_id(&$query)
static render_field_option($name= '', $option, $curr_value=NULL)
render_directory_active_areas($template_id= '', $context= 'single', $post_id= '', $echo=false)
Render the Template Active Areas and configured active fields for a given template id and post id...
gravityview_get_template_id($post_id)
Get the template ID (list, table, datatables, map) for a View.
get_entry_default_fields($form, $zone)
Retrieve the default fields id, label and type.
default_field_blacklist($array=array(), $context=NULL)
List the field types without presentation properties (on a View context)
$current_settings
static has_cap($caps= '', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
const GRAVITYVIEW_FILE(!defined( 'ABSPATH'))
Plugin Name: GravityView Plugin URI: https://gravityview.co Description: The best, easiest way to display Gravity Forms entries on your website.
Definition: gravityview.php:26
$templates
If this file is called directly, abort.
static get_connected_form_links($form, $include_form_link=true)
Get HTML links relating to a connected form, like Edit, Entries, Settings, Preview.
static get_default_widget_areas()
helper function to define the default widget areas
static get_default_args($with_details=false, $group=NULL)
Retrieve the default args for shortcode and theme function.
Definition: class-data.php:543
get_available_fields($form= '', $zone=NULL)
Calculate the available fields.
new GravityView_Admin_Views
gravityview_get_connected_views($form_id)
Get the views for a particular form.
gravityview_is_admin_page($hook= '', $page=NULL)
Alias for GravityView_Admin::is_admin_page()
gravityview_get_directory_fields($post_id, $apply_filter=true)
Get the field configuration for the View.
gravityview_get_form($form_id)
Returns the form object for a given Form ID.
gravityview_get_directory_widgets($post_id)
Get the widgets, as configured for a View.
get_registered_widgets()
Get the list of registered widgets.
gravityview_get_registered_templates()
Get all available preset templates.
render_active_areas($template_id, $type, $zone, $rows, $values)
Generic function to render rows and columns of active areas for widgets & fields. ...
if(empty($created_by)) $form_id
gravityview_get_forms($active=true, $trash=false)
Alias of GFAPI::get_forms()
_gravityview_process_posted_fields()
Maybe convert jQuery-serialized fields into array, otherwise return $_POST[&#39;fields&#39;] array...
gravityview_get_form_fields($form= '', $add_default_properties=false, $include_parent_field=true)
Return array of fields&#39; id and label, for a given Form ID.
global $post
gravityview_set_directory_widgets($post_id, $widgets=array())
Set the widgets, as configured for a View.
static render_setting_row($key= '', $current_settings=array(), $override_input=null, $name= 'template_settings[%s]', $id= 'gravityview_se_%s')
render_available_widgets()
Render html for displaying available widgets.
add_post_type_columns($columns)
Add the Data Source column to the Views page.
static add_scripts_and_styles($hook)
Enqueue scripts and styles at Views editor.
render_available_fields($form=0, $context= 'single')
Render html for displaying available fields based on a Form ID $blacklist_field_types - contains the ...
static gform_toolbar_menu($menu_items=array(), $id=NULL)
Add a GravityView menu to the Form Toolbar with connected views.
add_custom_column_content($column_name=NULL, $post_id)
Add the Data Source information.
gravityview_get_form_id($view_id)
Get the connected form ID from a View ID.
gravityview_get_entry_meta($form_id, $only_default_column=true)
get extra fields from entry meta
static render_field_options($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)
$current_form
Definition: data-source.php:14
static pre_get_form_fields($template_id= '')
Get the the form fields for a preset (no form created yet)
Definition: class-ajax.php:303
$field
Definition: gquiz_grade.php:11
render_widgets_active_areas($template_id= '', $zone, $post_id= '')
Render the widget active areas.
$title