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