GravityView  2.10.1
The best, easiest way to display Gravity Forms entries on your website.
class-gv-view.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * The default GravityView View class.
11  *
12  * Houses all base View functionality.
13  *
14  * Can be accessed as an array for old compatibility's sake
15  * in line with the elements inside the \GravityView_View_Data::$views array.
16  */
17 class View implements \ArrayAccess {
18 
19  /**
20  * @var \WP_Post The backing post instance.
21  */
22  private $post;
23 
24  /**
25  * @var \GV\View_Settings The settings.
26  *
27  * @api
28  * @since 2.0
29  */
30  public $settings;
31 
32  /**
33  * @var \GV\Widget_Collection The widets attached here.
34  *
35  * @api
36  * @since 2.0
37  */
38  public $widgets;
39 
40  /**
41  * @var \GV\GF_Form|\GV\Form The backing form for this view.
42  *
43  * Contains the form that is sourced for entries in this view.
44  *
45  * @api
46  * @since 2.0
47  */
48  public $form;
49 
50  /**
51  * @var \GV\Field_Collection The fields for this view.
52  *
53  * Contains all the fields that are attached to this view.
54  *
55  * @api
56  * @since 2.0
57  */
58  public $fields;
59 
60  /**
61  * @var array
62  *
63  * Internal static cache for gets, and whatnot.
64  * This is not persistent, resets across requests.
65 
66  * @internal
67  */
68  private static $cache = array();
69 
70  /**
71  * @var \GV\Join[] The joins for all sources in this view.
72  *
73  * @api
74  * @since 2.0.1
75  */
76  public $joins = array();
77 
78  /**
79  * @var \GV\Field[][] The unions for all sources in this view.
80  * An array of fields grouped by form_id keyed by
81  * main field_id:
82  *
83  * array(
84  * $form_id => array(
85  * $field_id => $field,
86  * $field_id => $field,
87  * )
88  * )
89  *
90  * @api
91  * @since 2.2.2
92  */
93  public $unions = array();
94 
95  /**
96  * The constructor.
97  */
98  public function __construct() {
99  $this->settings = new View_Settings();
100  $this->fields = new Field_Collection();
101  $this->widgets = new Widget_Collection();
102  }
103 
104  /**
105  * Register the gravityview WordPress Custom Post Type.
106  *
107  * @internal
108  * @return void
109  */
110  public static function register_post_type() {
111 
112  /** Register only once */
113  if ( post_type_exists( 'gravityview' ) ) {
114  return;
115  }
116 
117  /**
118  * @filter `gravityview_is_hierarchical` Make GravityView Views hierarchical by returning TRUE
119  * This will allow for Views to be nested with Parents and also allows for menu order to be set in the Page Attributes metabox
120  * @since 1.13
121  * @param boolean $is_hierarchical Default: false
122  */
123  $is_hierarchical = (bool)apply_filters( 'gravityview_is_hierarchical', false );
124 
125  $supports = array( 'title', 'revisions' );
126 
127  if ( $is_hierarchical ) {
128  $supports[] = 'page-attributes';
129  }
130 
131  /**
132  * @filter `gravityview_post_type_supports` Modify post type support values for `gravityview` post type
133  * @see add_post_type_support()
134  * @since 1.15.2
135  * @param array $supports Array of features associated with a functional area of the edit screen. Default: 'title', 'revisions'. If $is_hierarchical, also 'page-attributes'
136  * @param[in] boolean $is_hierarchical Do Views support parent/child relationships? See `gravityview_is_hierarchical` filter.
137  */
138  $supports = apply_filters( 'gravityview_post_type_support', $supports, $is_hierarchical );
139 
140  /** Register Custom Post Type - gravityview */
141  $labels = array(
142  'name' => _x( 'Views', 'Post Type General Name', 'gravityview' ),
143  'singular_name' => _x( 'View', 'Post Type Singular Name', 'gravityview' ),
144  'menu_name' => _x( 'Views', 'Menu name', 'gravityview' ),
145  'parent_item_colon' => __( 'Parent View:', 'gravityview' ),
146  'all_items' => __( 'All Views', 'gravityview' ),
147  'view_item' => _x( 'View', 'View Item', 'gravityview' ),
148  'add_new_item' => __( 'Add New View', 'gravityview' ),
149  'add_new' => __( 'New View', 'gravityview' ),
150  'edit_item' => __( 'Edit View', 'gravityview' ),
151  'update_item' => __( 'Update View', 'gravityview' ),
152  'search_items' => __( 'Search Views', 'gravityview' ),
153  'not_found' => \GravityView_Admin::no_views_text(),
154  'not_found_in_trash' => __( 'No Views found in Trash', 'gravityview' ),
155  'filter_items_list' => __( 'Filter Views list', 'gravityview' ),
156  'items_list_navigation' => __( 'Views list navigation', 'gravityview' ),
157  'items_list' => __( 'Views list', 'gravityview' ),
158  'view_items' => __( 'See Views', 'gravityview' ),
159  'attributes' => __( 'View Attributes', 'gravityview' ),
160  );
161  $args = array(
162  'label' => __( 'view', 'gravityview' ),
163  'description' => __( 'Create views based on a Gravity Forms form', 'gravityview' ),
164  'labels' => $labels,
165  'supports' => $supports,
166  'hierarchical' => $is_hierarchical,
167  /**
168  * @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode?
169  * @see https://codex.wordpress.org/Function_Reference/register_post_type#public
170  * @since 1.15.2
171  * @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded via shortcode. Default: `true`
172  * @param int $view_id The ID of the View currently being requested. `0` for general setting
173  */
174  'public' => apply_filters( 'gravityview_direct_access', gravityview()->plugin->is_compatible(), 0 ),
175  'show_ui' => gravityview()->plugin->is_compatible(),
176  'show_in_menu' => gravityview()->plugin->is_compatible(),
177  'show_in_nav_menus' => true,
178  'show_in_admin_bar' => true,
179  'menu_position' => 17,
180  'menu_icon' => '',
181  'can_export' => true,
182  /**
183  * @filter `gravityview_has_archive` Enable Custom Post Type archive?
184  * @since 1.7.3
185  * @param boolean False: don't have frontend archive; True: yes, have archive. Default: false
186  */
187  'has_archive' => apply_filters( 'gravityview_has_archive', false ),
188  'exclude_from_search' => true,
189  'rewrite' => array(
190  /**
191  * @filter `gravityview_slug` Modify the url part for a View.
192  * @see https://docs.gravityview.co/article/62-changing-the-view-slug
193  * @param string $slug The slug shown in the URL
194  */
195  'slug' => apply_filters( 'gravityview_slug', 'view' ),
196 
197  /**
198  * @filter `gravityview/post_type/with_front` Should the permalink structure
199  * be prepended with the front base.
200  * (example: if your permalink structure is /blog/, then your links will be: false->/view/, true->/blog/view/).
201  * Defaults to true.
202  * @see https://codex.wordpress.org/Function_Reference/register_post_type
203  * @since 2.0
204  * @param bool $with_front
205  */
206  'with_front' => apply_filters( 'gravityview/post_type/with_front', true ),
207  ),
208  'capability_type' => 'gravityview',
209  'map_meta_cap' => true,
210  );
211 
212  register_post_type( 'gravityview', $args );
213  }
214 
215  /**
216  * Add extra rewrite endpoints.
217  *
218  * @return void
219  */
220  public static function add_rewrite_endpoint() {
221  /**
222  * CSV.
223  */
224  global $wp_rewrite;
225 
226  $slug = apply_filters( 'gravityview_slug', 'view' );
227  $slug = ( '/' !== $wp_rewrite->front ) ? sprintf( '%s/%s', trim( $wp_rewrite->front, '/' ), $slug ) : $slug;
228  $csv_rule = array( sprintf( '%s/([^/]+)/csv/?', $slug ), 'index.php?gravityview=$matches[1]&csv=1', 'top' );
229  $tsv_rule = array( sprintf( '%s/([^/]+)/tsv/?', $slug ), 'index.php?gravityview=$matches[1]&tsv=1', 'top' );
230 
231  add_filter( 'query_vars', function( $query_vars ) {
232  $query_vars[] = 'csv';
233  $query_vars[] = 'tsv';
234  return $query_vars;
235  } );
236 
237  if ( ! isset( $wp_rewrite->extra_rules_top[ $csv_rule[0] ] ) ) {
238  call_user_func_array( 'add_rewrite_rule', $csv_rule );
239  call_user_func_array( 'add_rewrite_rule', $tsv_rule );
240  }
241  }
242 
243  /**
244  * A renderer filter for the View post type content.
245  *
246  * @param string $content Should be empty, as we don't store anything there.
247  *
248  * @return string $content The view content as output by the renderers.
249  */
250  public static function content( $content ) {
251  $request = gravityview()->request;
252 
253  // Plugins may run through the content in the header. WP SEO does this for its OpenGraph functionality.
254  if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
255  if ( ! did_action( 'loop_start' ) ) {
256  gravityview()->log->debug( 'Not processing yet: loop_start hasn\'t run yet. Current action: {action}', array( 'action' => current_filter() ) );
257  return $content;
258  }
259 
260  // We don't want this filter to run infinite loop on any post content fields
261  remove_filter( 'the_content', array( __CLASS__, __METHOD__ ) );
262  }
263 
264  /**
265  * This is not a View. Bail.
266  *
267  * Shortcodes and oEmbeds and whatnot will be handled
268  * elsewhere.
269  */
270  if ( ! $view = $request->is_view() ) {
271  return $content;
272  }
273 
274  /**
275  * Check permissions.
276  */
277  while ( $error = $view->can_render( null, $request ) ) {
278  if ( ! is_wp_error( $error ) )
279  break;
280 
281  switch ( str_replace( 'gravityview/', '', $error->get_error_code() ) ) {
282  case 'post_password_required':
283  return get_the_password_form( $view->ID );
284  case 'no_form_attached':
285 
286  gravityview()->log->error( 'View #{view_id} cannot render: {error_code} {error_message}', array( 'error_code' => $error->get_error_code(), 'error_message' => $error->get_error_message() ) );
287 
288  /**
289  * This View has no data source. There's nothing to show really.
290  * ...apart from a nice message if the user can do anything about it.
291  */
292  if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) {
293 
294  $title = sprintf( __( 'This View is not configured properly. Start by <a href="%s">selecting a form</a>.', 'gravityview' ), esc_url( get_edit_post_link( $view->ID, false ) ) );
295 
296  $message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
297 
298  $image = sprintf( '<img alt="%s" src="%s" style="margin-top: 10px;" />', esc_attr__( 'Data Source', 'gravityview' ), esc_url( plugins_url( 'assets/images/screenshots/data-source.png', GRAVITYVIEW_FILE ) ) );
299 
300  return \GVCommon::generate_notice( '<h3>' . $title . '</h3>' . wpautop( $message . $image ), 'notice' );
301  }
302  break;
303  case 'in_trash':
304 
305  if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) {
306  $notice = sprintf( __( 'This View is in the Trash. You can <a href="%s">restore the View here</a>.', 'gravityview' ), esc_url( get_edit_post_link( $view->ID, false ) ) );
307 
308  return \GVCommon::generate_notice( '<h3>' . $notice . '</h3>', 'notice', array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID );
309  }
310 
311  return ''; // Do not show
312  break;
313  case 'no_direct_access':
314  case 'embed_only':
315  case 'not_public':
316  default:
317  gravityview()->log->notice( 'View #{view_id} cannot render: {error_code} {error_message}', array( 'error_code' => $error->get_error_code(), 'error_message' => $error->get_error_message() ) );
318  return __( 'You are not allowed to view this content.', 'gravityview' );
319  }
320 
321  return $content;
322  }
323 
324  $is_admin_and_can_view = $view->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap('gravityview_moderate_entries', $view->ID );
325 
326  /**
327  * Editing a single entry.
328  */
329  if ( $entry = $request->is_edit_entry( $view->form ? $view->form->ID : 0 ) ) {
330  if ( $entry['status'] != 'active' ) {
331  gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) );
332  return __( 'You are not allowed to view this content.', 'gravityview' );
333  }
334 
335  if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) {
336  gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) );
337  return __( 'You are not allowed to view this content.', 'gravityview' );
338  }
339 
340  if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) {
342  gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) );
343  return __( 'You are not allowed to view this content.', 'gravityview' );
344  }
345  }
346 
347  $renderer = new Edit_Entry_Renderer();
348  return $renderer->render( $entry, $view, $request );
349 
350  /**
351  * Viewing a single entry.
352  */
353  } else if ( $entry = $request->is_entry( $view->form ? $view->form->ID : 0 ) ) {
354 
355  $entryset = $entry->is_multi() ? $entry->entries : array( $entry );
356 
357  $custom_slug = apply_filters( 'gravityview_custom_entry_slug', false );
358  $ids = explode( ',', get_query_var( \GV\Entry::get_endpoint_name() ) );
359 
360  $show_only_approved = $view->settings->get( 'show_only_approved' );
361 
362  foreach ( $entryset as $e ) {
363 
364  if ( 'active' !== $e['status'] ) {
365  gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $e->ID ) );
366  return __( 'You are not allowed to view this content.', 'gravityview' );
367  }
368 
369  if ( $custom_slug && ! in_array( $e->slug, $ids ) ) {
370  gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $e->ID ) );
371  return __( 'You are not allowed to view this content.', 'gravityview' );
372  }
373 
374  if ( $show_only_approved && ! $is_admin_and_can_view ) {
376  gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $e->ID ) );
377  return __( 'You are not allowed to view this content.', 'gravityview' );
378  }
379  }
380 
381  $error = \GVCommon::check_entry_display( $e->as_entry(), $view );
382 
383  if ( is_wp_error( $error ) ) {
384  gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing: {message}', array( 'entry_id' => $e->ID, 'message' => $error->get_error_message() ) );
385  return __( 'You are not allowed to view this content.', 'gravityview' );
386  }
387  }
388 
389  $renderer = new Entry_Renderer();
390  return $renderer->render( $entry, $view, $request );
391  }
392 
393  /**
394  * Plain old View.
395  */
396  $renderer = new View_Renderer();
397  return $renderer->render( $view, $request );
398  }
399 
400  /**
401  * Checks whether this view can be accessed or not.
402  *
403  * @param string[] $context The context we're asking for access from.
404  * Can any and as many of one of:
405  * edit An edit context.
406  * single A single context.
407  * cpt The custom post type single page accessed.
408  * shortcode Embedded as a shortcode.
409  * oembed Embedded as an oEmbed.
410  * rest A REST call.
411  * @param \GV\Request $request The request
412  *
413  * @return bool|\WP_Error An error if this View shouldn't be rendered here.
414  */
415  public function can_render( $context = null, $request = null ) {
416  if ( ! $request ) {
417  $request = gravityview()->request;
418  }
419 
420  if ( ! is_array( $context ) ) {
421  $context = array();
422  }
423 
424  /**
425  * @filter `gravityview/view/can_render` Whether the view can be rendered or not.
426  * @param bool|\WP_Error $result The result. Default: null.
427  * @param \GV\View $view The view.
428  * @param string[] $context See \GV\View::can_render
429  * @param \GV\Request $request The request.
430  */
431  if ( ! is_null( $result = apply_filters( 'gravityview/view/can_render', null, $this, $context, $request ) ) ) {
432  return $result;
433  }
434 
435  if ( in_array( 'rest', $context ) ) {
436  // REST
437  if ( gravityview()->plugin->settings->get( 'rest_api' ) === '1' && $this->settings->get( 'rest_disable' ) === '1' ) {
438  return new \WP_Error( 'gravityview/rest_disabled' );
439  } elseif ( gravityview()->plugin->settings->get( 'rest_api' ) !== '1' && $this->settings->get( 'rest_enable' ) !== '1' ) {
440  return new \WP_Error( 'gravityview/rest_disabled' );
441  }
442  }
443 
444  if ( in_array( 'csv', $context ) ) {
445  if ( $this->settings->get( 'csv_enable' ) !== '1' ) {
446  return new \WP_Error( 'gravityview/csv_disabled', 'The CSV endpoint is not enabled for this View' );
447  }
448  }
449 
450  /**
451  * This View is password protected. Nothing to do here.
452  */
453  if ( post_password_required( $this->ID ) ) {
454  gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $this->ID ) );
455  return new \WP_Error( 'gravityview/post_password_required' );
456  }
457 
458  if ( ! $this->form ) {
459  gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $this->ID ) );
460  return new \WP_Error( 'gravityview/no_form_attached' );
461  }
462 
463  if ( ! in_array( 'shortcode', $context ) ) {
464  /**
465  * Is this View directly accessible via a post URL?
466  *
467  * @see https://codex.wordpress.org/Function_Reference/register_post_type#public
468  */
469 
470  /**
471  * @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode?
472  * @deprecated
473  * @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded. Default: `true`
474  * @param int $view_id The ID of the View currently being requested. `0` for general setting
475  */
476  $direct_access = apply_filters( 'gravityview_direct_access', true, $this->ID );
477 
478  /**
479  * @filter `gravityview/request/output/direct` Should this View be directly accessbile?
480  * @since 2.0
481  * @param[in,out] boolean Accessible or not. Default: accessbile.
482  * @param \GV\View $view The View we're trying to directly render here.
483  * @param \GV\Request $request The current request.
484  */
485  if ( ! apply_filters( 'gravityview/view/output/direct', $direct_access, $this, $request ) ) {
486  return new \WP_Error( 'gravityview/no_direct_access' );
487  }
488 
489  /**
490  * Is this View an embed-only View? If so, don't allow rendering here,
491  * as this is a direct request.
492  */
493  if ( $this->settings->get( 'embed_only' ) && ! \GVCommon::has_cap( 'read_private_gravityviews' ) ) {
494  return new \WP_Error( 'gravityview/embed_only' );
495  }
496  }
497 
498  /** Private, pending, draft, etc. */
499  $public_states = get_post_stati( array( 'public' => true ) );
500  if ( ! in_array( $this->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $this->ID ) ) {
501  gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $this->ID ) );
502  return new \WP_Error( 'gravityview/not_public' );
503  }
504 
505  return true;
506  }
507 
508  /**
509  * Get joins associated with a view
510  *
511  * @param \WP_Post $post GravityView CPT to get joins for
512  *
513  * @api
514  * @since 2.0.11
515  *
516  * @return \GV\Join[] Array of \GV\Join instances
517  */
518  public static function get_joins( $post ) {
519  $joins = array();
520 
521  if ( ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
522  gravityview()->log->error( 'Cannot get joined forms; joins feature not supported.' );
523  return $joins;
524  }
525 
526  if ( ! $post || 'gravityview' !== get_post_type( $post ) ) {
527  gravityview()->log->error( 'Only "gravityview" post types can be \GV\View instances.' );
528  return $joins;
529  }
530 
531  $joins_meta = get_post_meta( $post->ID, '_gravityview_form_joins', true );
532 
533  if ( empty( $joins_meta ) ) {
534  return $joins;
535  }
536 
537  foreach ( $joins_meta as $meta ) {
538  if ( ! is_array( $meta ) || count( $meta ) != 4 ) {
539  continue;
540  }
541 
542  list( $join, $join_column, $join_on, $join_on_column ) = $meta;
543 
544  $join = GF_Form::by_id( $join );
545  $join_on = GF_Form::by_id( $join_on );
546 
547  $join_column = is_numeric( $join_column ) ? GF_Field::by_id( $join, $join_column ) : Internal_Field::by_id( $join_column );
548  $join_on_column = is_numeric( $join_on_column ) ? GF_Field::by_id( $join_on, $join_on_column ) : Internal_Field::by_id( $join_on_column );
549 
550  $joins [] = new Join( $join, $join_column, $join_on, $join_on_column );
551  }
552 
553  return $joins;
554  }
555 
556  /**
557  * Get joined forms associated with a view
558  * In no particular order.
559  *
560  * @since 2.0.11
561  *
562  * @api
563  * @since 2.0
564  * @param int $post_id ID of the View
565  *
566  * @return \GV\GF_Form[] Array of \GV\GF_Form instances
567  */
568  public static function get_joined_forms( $post_id ) {
569  $forms = array();
570 
571  if ( ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
572  gravityview()->log->error( 'Cannot get joined forms; joins feature not supported.' );
573  return $forms;
574  }
575 
576  if ( ! $post_id || ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) {
577  return $forms;
578  }
579 
580  if ( empty( $post_id ) ) {
581  gravityview()->log->error( 'Cannot get joined forms; $post_id was empty' );
582  return $forms;
583  }
584 
585  $joins_meta = get_post_meta( $post_id, '_gravityview_form_joins', true );
586 
587  if ( empty( $joins_meta ) ) {
588  return $forms;
589  }
590 
591  foreach ( $joins_meta as $meta ) {
592  if ( ! is_array( $meta ) || count( $meta ) != 4 ) {
593  continue;
594  }
595 
596  list( $join, $join_column, $join_on, $join_on_column ) = $meta;
597 
598  if ( $form = GF_Form::by_id( $join_on ) ) {
599  $forms[ $join_on ] = $form;
600  }
601 
602  if ( $form = GF_Form::by_id( $join ) ) {
603  $forms[ $join ] = $form;
604  }
605  }
606 
607  return $forms;
608  }
609 
610  /**
611  * Get unions associated with a view
612  *
613  * @param \WP_Post $post GravityView CPT to get unions for
614  *
615  * @api
616  * @since 2.2.2
617  *
618  * @return \GV\Field[][] Array of unions (see self::$unions)
619  */
620  public static function get_unions( $post ) {
621  $unions = array();
622 
623  if ( ! $post || 'gravityview' !== get_post_type( $post ) ) {
624  gravityview()->log->error( 'Only "gravityview" post types can be \GV\View instances.' );
625  return $unions;
626  }
627 
628  $fields = get_post_meta( $post->ID, '_gravityview_directory_fields', true );
629 
630  if ( empty( $fields ) ) {
631  return $unions;
632  }
633 
634  foreach ( $fields as $location => $_fields ) {
635  if ( strpos( $location, 'directory_' ) !== 0 ) {
636  continue;
637  }
638 
639  foreach ( $_fields as $field ) {
640  if ( ! empty( $field['unions'] ) ) {
641  foreach ( $field['unions'] as $form_id => $field_id ) {
642  if ( ! isset( $unions[ $form_id ] ) ) {
643  $unions[ $form_id ] = array();
644  }
645 
646  $unions[ $form_id ][ $field['id'] ] =
647  is_numeric( $field_id ) ? \GV\GF_Field::by_id( \GV\GF_Form::by_id( $form_id ), $field_id ) : \GV\Internal_Field::by_id( $field_id );
648  }
649  }
650  }
651 
652  break;
653  }
654 
655  if ( $unions ) {
656  if ( ! gravityview()->plugin->supports( Plugin::FEATURE_UNIONS ) ) {
657  gravityview()->log->error( 'Cannot get unions; unions feature not supported.' );
658  }
659  }
660 
661  // @todo We'll probably need to backfill null unions
662 
663  return $unions;
664  }
665 
666  /**
667  * Construct a \GV\View instance from a \WP_Post.
668  *
669  * @param \WP_Post $post The \WP_Post instance to wrap.
670  *
671  * @api
672  * @since 2.0
673  * @return \GV\View|null An instance around this \WP_Post if valid, null otherwise.
674  */
675  public static function from_post( $post ) {
676 
677  if ( ! $post || 'gravityview' !== get_post_type( $post ) ) {
678  gravityview()->log->error( 'Only gravityview post types can be \GV\View instances.' );
679  return null;
680  }
681 
682  if ( $view = Utils::get( self::$cache, "View::from_post:{$post->ID}" ) ) {
683  /**
684  * @filter `gravityview/view/get` Override View.
685  * @param \GV\View $view The View instance pointer.
686  * @since 2.1
687  */
688  do_action_ref_array( 'gravityview/view/get', array( &$view ) );
689 
690  return $view;
691  }
692 
693  $view = new self();
694  $view->post = $post;
695 
696  /** Get connected form. */
697  $view->form = GF_Form::by_id( $view->_gravityview_form_id );
698  if ( ! $view->form ) {
699  gravityview()->log->error( 'View #{view_id} tried attaching non-existent Form #{form_id} to it.', array(
700  'view_id' => $view->ID,
701  'form_id' => $view->_gravityview_form_id ? : 0,
702  ) );
703  }
704 
705  $view->joins = $view::get_joins( $post );
706 
707  $view->unions = $view::get_unions( $post );
708 
709  /**
710  * @filter `gravityview/configuration/fields` Filter the View fields' configuration array.
711  * @since 1.6.5
712  *
713  * @deprecated Use `gravityview/view/configuration/fields` or `gravityview/view/fields` filters.
714  *
715  * @param $fields array Multi-array of fields with first level being the field zones.
716  * @param $view_id int The View the fields are being pulled for.
717  */
718  $configuration = apply_filters( 'gravityview/configuration/fields', (array)$view->_gravityview_directory_fields, $view->ID );
719 
720  /**
721  * @filter `gravityview/view/configuration/fields` Filter the View fields' configuration array.
722  * @since 2.0
723  *
724  * @param array $fields Multi-array of fields with first level being the field zones.
725  * @param \GV\View $view The View the fields are being pulled for.
726  */
727  $configuration = apply_filters( 'gravityview/view/configuration/fields', $configuration, $view );
728 
729  /**
730  * @filter `gravityview/view/fields` Filter the Field Collection for this View.
731  * @since 2.0
732  *
733  * @param \GV\Field_Collection $fields A collection of fields.
734  * @param \GV\View $view The View the fields are being pulled for.
735  */
736  $view->fields = apply_filters( 'gravityview/view/fields', Field_Collection::from_configuration( $configuration ), $view );
737 
738  /**
739  * @filter `gravityview/view/configuration/widgets` Filter the View widgets' configuration array.
740  * @since 2.0
741  *
742  * @param array $fields Multi-array of widgets with first level being the field zones.
743  * @param \GV\View $view The View the widgets are being pulled for.
744  */
745  $configuration = apply_filters( 'gravityview/view/configuration/widgets', (array)$view->_gravityview_directory_widgets, $view );
746 
747  /**
748  * @filter `gravityview/view/widgets` Filter the Widget Collection for this View.
749  * @since 2.0
750  *
751  * @param \GV\Widget_Collection $widgets A collection of widgets.
752  * @param \GV\View $view The View the widgets are being pulled for.
753  */
754  $view->widgets = apply_filters( 'gravityview/view/widgets', Widget_Collection::from_configuration( $configuration ), $view );
755 
756  /** View configuration. */
757  $view->settings->update( gravityview_get_template_settings( $view->ID ) );
758 
759  /** Add the template name into the settings. */
760  $view->settings->update( array( 'template' => gravityview_get_template_id( $view->ID ) ) );
761 
762  /** View basics. */
763  $view->settings->update( array(
764  'id' => $view->ID,
765  ) );
766 
767  self::$cache[ "View::from_post:{$post->ID}" ] = &$view;
768 
769  /**
770  * @filter `gravityview/view/get` Override View.
771  * @param \GV\View $view The View instance pointer.
772  * @since 2.1
773  */
774  do_action_ref_array( 'gravityview/view/get', array( &$view ) );
775 
776  return $view;
777  }
778 
779  /**
780  * Flush the view cache.
781  *
782  * @param int $view_id The View to reset cache for. Optional. Default: resets everything.
783  *
784  * @internal
785  */
786  public static function _flush_cache( $view_id = null ) {
787  if ( $view_id ) {
788  unset( self::$cache[ "View::from_post:$view_id" ] );
789  return;
790  }
791  self::$cache = array();
792  }
793 
794  /**
795  * Construct a \GV\View instance from a post ID.
796  *
797  * @param int|string $post_id The post ID.
798  *
799  * @api
800  * @since 2.0
801  * @return \GV\View|null An instance around this \WP_Post or null if not found.
802  */
803  public static function by_id( $post_id ) {
804  if ( ! $post_id || ! $post = get_post( $post_id ) ) {
805  return null;
806  }
807  return self::from_post( $post );
808  }
809 
810  /**
811  * Determines if a view exists to begin with.
812  *
813  * @param int|\WP_Post|null $view The WordPress post ID, a \WP_Post object or null for global $post;
814  *
815  * @api
816  * @since 2.0
817  * @return bool Whether the post exists or not.
818  */
819  public static function exists( $view ) {
820  return get_post_type( $view ) == 'gravityview';
821  }
822 
823  /**
824  * ArrayAccess compatibility layer with GravityView_View_Data::$views
825  *
826  * @internal
827  * @deprecated
828  * @since 2.0
829  * @return bool Whether the offset exists or not, limited to GravityView_View_Data::$views element keys.
830  */
831  public function offsetExists( $offset ) {
832  $data_keys = array( 'id', 'view_id', 'form_id', 'template_id', 'atts', 'fields', 'widgets', 'form' );
833  return in_array( $offset, $data_keys );
834  }
835 
836  /**
837  * ArrayAccess compatibility layer with GravityView_View_Data::$views
838  *
839  * Maps the old keys to the new data;
840  *
841  * @internal
842  * @deprecated
843  * @since 2.0
844  *
845  * @return mixed The value of the requested view data key limited to GravityView_View_Data::$views element keys. If offset not found, return null.
846  */
847  public function offsetGet( $offset ) {
848 
849  gravityview()->log->notice( 'This is a \GV\View object should not be accessed as an array.' );
850 
851  if ( ! isset( $this[ $offset ] ) ) {
852  return null;
853  }
854 
855  switch ( $offset ) {
856  case 'id':
857  case 'view_id':
858  return $this->ID;
859  case 'form':
860  return $this->form;
861  case 'form_id':
862  return $this->form ? $this->form->ID : null;
863  case 'atts':
864  return $this->settings->as_atts();
865  case 'template_id':
866  return $this->settings->get( 'template' );
867  case 'widgets':
868  return $this->widgets->as_configuration();
869  }
870 
871  return null;
872  }
873 
874  /**
875  * ArrayAccess compatibility layer with GravityView_View_Data::$views
876  *
877  * @internal
878  * @deprecated
879  * @since 2.0
880  *
881  * @return void
882  */
883  public function offsetSet( $offset, $value ) {
884  gravityview()->log->error( 'The old view data is no longer mutable. This is a \GV\View object should not be accessed as an array.' );
885  }
886 
887  /**
888  * ArrayAccess compatibility layer with GravityView_View_Data::$views
889  *
890  * @internal
891  * @deprecated
892  * @since 2.0
893  * @return void
894  */
895  public function offsetUnset( $offset ) {
896  gravityview()->log->error( 'The old view data is no longer mutable. This is a \GV\View object should not be accessed as an array.' );
897  }
898 
899  /**
900  * Be compatible with the old data object.
901  *
902  * Some external code expects an array (doing things like foreach on this, or array_keys)
903  * so let's return an array in the old format for such cases. Do not use unless using
904  * for back-compatibility.
905  *
906  * @internal
907  * @deprecated
908  * @since 2.0
909  * @return array
910  */
911  public function as_data() {
912  return array(
913  'id' => $this->ID,
914  'view_id' => $this->ID,
915  'form_id' => $this->form ? $this->form->ID : null,
916  'form' => $this->form ? gravityview_get_form( $this->form->ID ) : null,
917  'atts' => $this->settings->as_atts(),
918  'fields' => $this->fields->by_visible( $this )->as_configuration(),
919  'template_id' => $this->settings->get( 'template' ),
920  'widgets' => $this->widgets->as_configuration(),
921  );
922  }
923 
924  /**
925  * Retrieve the entries for the current view and request.
926  *
927  * @param \GV\Request The request. Unused for now.
928  *
929  * @return \GV\Entry_Collection The entries.
930  */
931  public function get_entries( $request = null ) {
932  $entries = new \GV\Entry_Collection();
933  if ( $this->form ) {
934  $parameters = $this->settings->as_atts();
935 
936  /**
937  * Remove multiple sorting before calling legacy filters.
938  * This allows us to fake it till we make it.
939  */
940  if ( ! empty( $parameters['sort_field'] ) && is_array( $parameters['sort_field'] ) ) {
941  $has_multisort = true;
942  $parameters['sort_field'] = reset( $parameters['sort_field'] );
943  if ( ! empty( $parameters['sort_direction'] ) && is_array( $parameters['sort_direction'] ) ) {
944  $parameters['sort_direction'] = reset( $parameters['sort_direction'] );
945  }
946  }
947 
948  /**
949  * @todo: Stop using _frontend and use something like $request->get_search_criteria() instead
950  */
951  $parameters = \GravityView_frontend::get_view_entries_parameters( $parameters, $this->form->ID );
952 
953  $parameters['context_view_id'] = $this->ID;
954  $parameters = \GVCommon::calculate_get_entries_criteria( $parameters, $this->form->ID );
955 
956  if ( ! is_array( $parameters ) ) {
957  $parameters = array();
958  }
959 
960  if ( ! is_array( $parameters['search_criteria'] ) ) {
961  $parameters['search_criteria'] = array();
962  }
963 
964  if ( ( ! isset( $parameters['search_criteria']['field_filters'] ) ) || ( ! is_array( $parameters['search_criteria']['field_filters'] ) ) ) {
965  $parameters['search_criteria']['field_filters'] = array();
966  }
967 
968  if ( $request instanceof REST\Request ) {
969  $atts = $this->settings->as_atts();
970  $paging_parameters = wp_parse_args( $request->get_paging(), array(
971  'paging' => array( 'page_size' => $atts['page_size'] ),
972  ) );
973  $parameters['paging'] = $paging_parameters['paging'];
974  }
975 
976  $page = Utils::get( $parameters['paging'], 'current_page' ) ?
977  : ( ( ( $parameters['paging']['offset'] - $this->settings->get( 'offset' ) ) / \GV\Utils::get( $parameters, 'paging/page_size', 25 ) ) + 1 );
978 
979  /**
980  * Cleanup duplicate field_filter parameters to simplify the query.
981  */
982  $unique_field_filters = array();
983  foreach ( Utils::get( $parameters, 'search_criteria/field_filters', array() ) as $key => $filter ) {
984  if ( 'mode' === $key ) {
985  $unique_field_filters['mode'] = $filter;
986  } else if ( ! in_array( $filter, $unique_field_filters ) ) {
987  $unique_field_filters[] = $filter;
988  }
989  }
990  $parameters['search_criteria']['field_filters'] = $unique_field_filters;
991 
992  if ( ! empty( $parameters['search_criteria']['field_filters'] ) ) {
993  gravityview()->log->notice( 'search_criteria/field_filters is not empty, third-party code may be using legacy search_criteria filters.' );
994  }
995 
996  if ( gravityview()->plugin->supports( Plugin::FEATURE_GFQUERY ) ) {
997 
998  $query_class = $this->get_query_class();
999 
1000  /** @type \GF_Query $query */
1001  $query = new $query_class( $this->form->ID, $parameters['search_criteria'], Utils::get( $parameters, 'sorting' ) );
1002 
1003  /**
1004  * Apply multisort.
1005  */
1006  if ( ! empty( $has_multisort ) ) {
1007  $atts = $this->settings->as_atts();
1008 
1009  $view_setting_sort_field_ids = \GV\Utils::get( $atts, 'sort_field', array() );
1010  $view_setting_sort_directions = \GV\Utils::get( $atts, 'sort_direction', array() );
1011 
1012  $has_sort_query_param = ! empty( $_GET['sort'] ) && is_array( $_GET['sort'] );
1013 
1014  if( $has_sort_query_param ) {
1015  $has_sort_query_param = array_filter( array_values( $_GET['sort'] ) );
1016  }
1017 
1018  if ( $this->settings->get( 'sort_columns' ) && $has_sort_query_param ) {
1019  $sort_field_ids = array_keys( $_GET['sort'] );
1020  $sort_directions = array_values( $_GET['sort'] );
1021  } else {
1022  $sort_field_ids = $view_setting_sort_field_ids;
1023  $sort_directions = $view_setting_sort_directions;
1024  }
1025 
1026  $skip_first = false;
1027 
1028  foreach ( (array) $sort_field_ids as $key => $sort_field_id ) {
1029 
1030  if ( ! $skip_first && ! $has_sort_query_param ) {
1031  $skip_first = true; // Skip the first one, it's already in the query
1032  continue;
1033  }
1034 
1035  $sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type( $sort_field_id, $this->form->ID );
1036  $sort_direction = strtoupper( \GV\Utils::get( $sort_directions, $key, 'ASC' ) );
1037 
1038  if ( ! empty( $sort_field_id ) ) {
1039  $order = new \GF_Query_Column( $sort_field_id, $this->form->ID );
1040  if ( 'id' !== $sort_field_id && \GVCommon::is_field_numeric( $this->form->ID, $sort_field_id ) ) {
1041  $order = \GF_Query_Call::CAST( $order, defined( 'GF_Query::TYPE_DECIMAL' ) ? \GF_Query::TYPE_DECIMAL : \GF_Query::TYPE_SIGNED );
1042  }
1043 
1044  $query->order( $order, $sort_direction );
1045  }
1046  }
1047  }
1048 
1049  /**
1050  * Merge time subfield sorts.
1051  */
1052  add_filter( 'gform_gf_query_sql', $gf_query_timesort_sql_callback = function( $sql ) use ( &$query ) {
1053  $q = $query->_introspect();
1054  $orders = array();
1055 
1056  $merged_time = false;
1057 
1058  foreach ( $q['order'] as $oid => $order ) {
1059  if ( $order[0] instanceof \GF_Query_Column ) {
1060  $column = $order[0];
1061  } else if ( $order[0] instanceof \GF_Query_Call ) {
1062  if ( count( $order[0]->columns ) != 1 || ! $order[0]->columns[0] instanceof \GF_Query_Column ) {
1063  $orders[ $oid ] = $order;
1064  continue; // Need something that resembles a single sort
1065  }
1066  $column = $order[0]->columns[0];
1067  }
1068 
1069  if ( ( ! $field = \GFAPI::get_field( $column->source, $column->field_id ) ) || $field->type !== 'time' ) {
1070  $orders[ $oid ] = $order;
1071  continue; // Not a time field
1072  }
1073 
1074  if ( ! class_exists( '\GV\Mocks\GF_Query_Call_TIMESORT' ) ) {
1075  require_once gravityview()->plugin->dir( 'future/_mocks.timesort.php' );
1076  }
1077 
1078  $orders[ $oid ] = array(
1079  new \GV\Mocks\GF_Query_Call_TIMESORT( 'timesort', array( $column, $sql ) ),
1080  $order[1] // Mock it!
1081  );
1082 
1083  $merged_time = true;
1084  }
1085 
1086  if ( $merged_time ) {
1087  /**
1088  * ORDER again.
1089  */
1090  if ( ! empty( $orders ) && $_orders = $query->_order_generate( $orders ) ) {
1091  $sql['order'] = 'ORDER BY ' . implode( ', ', $_orders );
1092  }
1093  }
1094 
1095  return $sql;
1096  } );
1097 
1098  $query->limit( $parameters['paging']['page_size'] )
1099  ->offset( ( ( $page - 1 ) * $parameters['paging']['page_size'] ) + $this->settings->get( 'offset' ) );
1100 
1101  /**
1102  * Any joins?
1103  */
1104  if ( gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) && count( $this->joins ) ) {
1105 
1106  $is_admin_and_can_view = $this->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap( 'gravityview_moderate_entries', $this->ID );
1107 
1108  foreach ( $this->joins as $join ) {
1109  $query = $join->as_query_join( $query );
1110 
1111  if ( $this->settings->get( 'multiple_forms_disable_null_joins' ) ) {
1112 
1113  // Disable NULL outputs
1114  $condition = new \GF_Query_Condition(
1115  new \GF_Query_Column( $join->join_on_column->ID, $join->join_on->ID ),
1116  \GF_Query_Condition::NEQ,
1117  new \GF_Query_Literal( '' )
1118  );
1119 
1120  $query_parameters = $query->_introspect();
1121 
1122  $query->where( \GF_Query_Condition::_and( $query_parameters['where'], $condition ) );
1123  }
1124 
1125  /**
1126  * This is a temporary stub filter, until GF_Query supports NULL conditions.
1127  * Do not use! This filter will be removed.
1128  */
1129  if ( defined( 'GF_Query_Condition::NULL' ) ) {
1130  $is_null_condition_native = true;
1131  } else {
1132  $is_null_condition_class = apply_filters( 'gravityview/query/is_null_condition', null );
1133  $is_null_condition_native = false;
1134  }
1135 
1136  // Filter to active entries only
1137  $condition = new \GF_Query_Condition(
1138  new \GF_Query_Column( 'status', $join->join_on->ID ),
1139  \GF_Query_Condition::EQ,
1140  new \GF_Query_Literal( 'active' )
1141  );
1142 
1143  if ( $is_null_condition_native ) {
1144  $condition = \GF_Query_Condition::_or( $condition, new \GF_Query_Condition(
1145  new \GF_Query_Column( 'status', $join->join_on->ID ),
1146  \GF_Query_Condition::IS,
1147  \GF_Query_Condition::NULL
1148  ) );
1149  } else if ( ! is_null( $is_null_condition_class ) ) {
1150  $condition = \GF_Query_Condition::_or( $condition, new $is_null_condition_class(
1151  new \GF_Query_Column( 'status', $join->join_on->ID )
1152  ) );
1153  }
1154 
1155  $q = $query->_introspect();
1156  $query->where( \GF_Query_Condition::_and( $q['where'], $condition ) );
1157 
1158  if ( $this->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) {
1159 
1160  // Show only approved joined entries
1161  $condition = new \GF_Query_Condition(
1162  new \GF_Query_Column( \GravityView_Entry_Approval::meta_key, $join->join_on->ID ),
1163  \GF_Query_Condition::EQ,
1164  new \GF_Query_Literal( \GravityView_Entry_Approval_Status::APPROVED )
1165  );
1166 
1167  if ( $is_null_condition_native ) {
1168  $condition = \GF_Query_Condition::_or( $condition, new \GF_Query_Condition(
1169  new \GF_Query_Column( \GravityView_Entry_Approval::meta_key, $join->join_on->ID ),
1170  \GF_Query_Condition::IS,
1171  \GF_Query_Condition::NULL
1172  ) );
1173  } else if ( ! is_null( $is_null_condition_class ) ) {
1174  $condition = \GF_Query_Condition::_or( $condition, new $is_null_condition_class(
1175  new \GF_Query_Column( \GravityView_Entry_Approval::meta_key, $join->join_on->ID )
1176  ) );
1177  }
1178 
1179  $query_parameters = $query->_introspect();
1180 
1181  $query->where( \GF_Query_Condition::_and( $query_parameters['where'], $condition ) );
1182  }
1183  }
1184 
1185  /**
1186  * Unions?
1187  */
1188  } else if ( gravityview()->plugin->supports( Plugin::FEATURE_UNIONS ) && count( $this->unions ) ) {
1189  $query_parameters = $query->_introspect();
1190 
1191  $unions_sql = array();
1192 
1193  /**
1194  * @param \GF_Query_Condition $condition
1195  * @param array $fields
1196  * @param $recurse
1197  *
1198  * @return \GF_Query_Condition
1199  */
1200  $where_union_substitute = function( $condition, $fields, $recurse ) {
1201  if ( $condition->expressions ) {
1202  $conditions = array();
1203 
1204  foreach ( $condition->expressions as $_condition ) {
1205  $conditions[] = $recurse( $_condition, $fields, $recurse );
1206  }
1207 
1208  return call_user_func_array(
1209  array( '\GF_Query_Condition', $condition->operator == 'AND' ? '_and' : '_or' ),
1210  $conditions
1211  );
1212  }
1213 
1214  if ( ! ( $condition->left && $condition->left instanceof \GF_Query_Column ) || ( ! $condition->left->is_entry_column() && ! $condition->left->is_meta_column() ) ) {
1215  return new \GF_Query_Condition(
1216  new \GF_Query_Column( $fields[ $condition->left->field_id ]->ID ),
1217  $condition->operator,
1218  $condition->right
1219  );
1220  }
1221 
1222  return $condition;
1223  };
1224 
1225  foreach ( $this->unions as $form_id => $fields ) {
1226 
1227  // Build a new query for every unioned form
1228  $query_class = $this->get_query_class();
1229 
1230  /** @type \GF_Query|\GF_Patched_Query $q */
1231  $q = new $query_class( $form_id );
1232 
1233  // Copy the WHERE clauses but substitute the field_ids to the respective ones
1234  $q->where( $where_union_substitute( $query_parameters['where'], $fields, $where_union_substitute ) );
1235 
1236  // Copy the ORDER clause and substitute the field_ids to the respective ones
1237  foreach ( $query_parameters['order'] as $order ) {
1238  list( $column, $_order ) = $order;
1239 
1240  if ( $column && $column instanceof \GF_Query_Column ) {
1241  if ( ! $column->is_entry_column() && ! $column->is_meta_column() ) {
1242  $column = new \GF_Query_Column( $fields[ $column->field_id ]->ID );
1243  }
1244 
1245  $q->order( $column, $_order );
1246  }
1247  }
1248 
1249  add_filter( 'gform_gf_query_sql', $gf_query_sql_callback = function( $sql ) use ( &$unions_sql ) {
1250  // Remove SQL_CALC_FOUND_ROWS as it's not needed in UNION clauses
1251  $select = 'UNION ALL ' . str_replace( 'SQL_CALC_FOUND_ROWS ', '', $sql['select'] );
1252 
1253  // Record the SQL
1254  $unions_sql[] = array(
1255  // Remove columns, we'll rebuild them
1256  'select' => preg_replace( '#DISTINCT (.*)#', 'DISTINCT ', $select ),
1257  'from' => $sql['from'],
1258  'join' => $sql['join'],
1259  'where' => $sql['where'],
1260  // Remove order and limit
1261  );
1262 
1263  // Return empty query, no need to call the database
1264  return array();
1265  } );
1266 
1267  do_action_ref_array( 'gravityview/view/query', array( &$q, $this, $request ) );
1268 
1269  $q->get(); // Launch
1270 
1271  remove_filter( 'gform_gf_query_sql', $gf_query_sql_callback );
1272  }
1273 
1274  add_filter( 'gform_gf_query_sql', $gf_query_sql_callback = function( $sql ) use ( $unions_sql ) {
1275  // Remove SQL_CALC_FOUND_ROWS as it's not needed in UNION clauses
1276  $sql['select'] = str_replace( 'SQL_CALC_FOUND_ROWS ', '', $sql['select'] );
1277 
1278  // Remove columns, we'll rebuild them
1279  preg_match( '#DISTINCT (`[motc]\d+`.`.*?`)#', $sql['select'], $select_match );
1280  $sql['select'] = preg_replace( '#DISTINCT (.*)#', 'DISTINCT ', $sql['select'] );
1281 
1282  $unions = array();
1283 
1284  // Transform selected columns to shared alias names
1285  $column_to_alias = function( $column ) {
1286  $column = str_replace( '`', '', $column );
1287  return '`' . str_replace( '.', '_', $column ) . '`';
1288  };
1289 
1290  // Add all the order columns into the selects, so we can order by the whole union group
1291  preg_match_all( '#(`[motc]\d+`.`.*?`)#', $sql['order'], $order_matches );
1292 
1293  $columns = array(
1294  sprintf( '%s AS %s', $select_match[1], $column_to_alias( $select_match[1] ) )
1295  );
1296 
1297  foreach ( array_slice( $order_matches, 1 ) as $match ) {
1298  $columns[] = sprintf( '%s AS %s', $match[0], $column_to_alias( $match[0] ) );
1299 
1300  // Rewrite the order columns to the shared aliases
1301  $sql['order'] = str_replace( $match[0], $column_to_alias( $match[0] ), $sql['order'] );
1302  }
1303 
1304  $columns = array_unique( $columns );
1305 
1306  // Add the columns to every UNION
1307  foreach ( $unions_sql as $union_sql ) {
1308  $union_sql['select'] .= implode( ', ', $columns );
1309  $unions []= implode( ' ', $union_sql );
1310  }
1311 
1312  // Add the columns to the main SELECT, but only grab the entry id column
1313  $sql['select'] = 'SELECT SQL_CALC_FOUND_ROWS t1_id FROM (' . $sql['select'] . implode( ', ', $columns );
1314  $sql['order'] = implode( ' ', $unions ) . ') AS u ' . $sql['order'];
1315 
1316  return $sql;
1317  } );
1318  }
1319 
1320  /**
1321  * @action `gravityview/view/query` Override the \GF_Query before the get() call.
1322  * @param \GF_Query $query The current query object reference
1323  * @param \GV\View $this The current view object
1324  * @param \GV\Request $request The request object
1325  */
1326  do_action_ref_array( 'gravityview/view/query', array( &$query, $this, $request ) );
1327 
1328  gravityview()->log->debug( 'GF_Query parameters: ', array( 'data' => Utils::gf_query_debug( $query ) ) );
1329 
1330  /**
1331  * Map from Gravity Forms entries arrays to an Entry_Collection.
1332  */
1333  if ( count( $this->joins ) ) {
1334  foreach ( $query->get() as $entry ) {
1335  $entries->add(
1336  Multi_Entry::from_entries( array_map( '\GV\GF_Entry::from_entry', $entry ) )
1337  );
1338  }
1339  } else {
1340  array_map( array( $entries, 'add' ), array_map( '\GV\GF_Entry::from_entry', $query->get() ) );
1341  }
1342 
1343  if ( isset( $gf_query_sql_callback ) ) {
1344  remove_action( 'gform_gf_query_sql', $gf_query_sql_callback );
1345  }
1346 
1347  if ( isset( $gf_query_timesort_sql_callback ) ) {
1348  remove_action( 'gform_gf_query_sql', $gf_query_timesort_sql_callback );
1349  }
1350 
1351  /**
1352  * Add total count callback.
1353  */
1354  $entries->add_count_callback( function() use ( $query ) {
1355  return $query->total_found;
1356  } );
1357  } else {
1358  $entries = $this->form->entries
1359  ->filter( \GV\GF_Entry_Filter::from_search_criteria( $parameters['search_criteria'] ) )
1360  ->offset( $this->settings->get( 'offset' ) )
1361  ->limit( $parameters['paging']['page_size'] )
1362  ->page( $page );
1363 
1364  if ( ! empty( $parameters['sorting'] ) && is_array( $parameters['sorting'] && ! isset( $parameters['sorting']['key'] ) ) ) {
1365  // Pluck off multisort arrays
1366  $parameters['sorting'] = $parameters['sorting'][0];
1367  }
1368 
1369  if ( ! empty( $parameters['sorting'] ) && ! empty( $parameters['sorting']['key'] ) ) {
1370  $field = new \GV\Field();
1371  $field->ID = $parameters['sorting']['key'];
1372  $direction = strtolower( $parameters['sorting']['direction'] ) == 'asc' ? \GV\Entry_Sort::ASC : \GV\Entry_Sort::DESC;
1373  $entries = $entries->sort( new \GV\Entry_Sort( $field, $direction ) );
1374  }
1375  }
1376  }
1377 
1378  /**
1379  * @filter `gravityview/view/entries` Modify the entry fetching filters, sorts, offsets, limits.
1380  * @param \GV\Entry_Collection $entries The entries for this view.
1381  * @param \GV\View $view The view.
1382  * @param \GV\Request $request The request.
1383  */
1384  return apply_filters( 'gravityview/view/entries', $entries, $this, $request );
1385  }
1386 
1387  /**
1388  * Last chance to configure the output.
1389  *
1390  * Used for CSV output, for example.
1391  *
1392  * @return void
1393  */
1394  public static function template_redirect() {
1395 
1396  $is_csv = get_query_var( 'csv' );
1397  $is_tsv = get_query_var( 'tsv' );
1398 
1399  /**
1400  * CSV output.
1401  */
1402  if ( ! $is_csv && ! $is_tsv ) {
1403  return;
1404  }
1405 
1406  $view = gravityview()->request->is_view();
1407 
1408  if ( ! $view ) {
1409  return;
1410  }
1411 
1412  $error_csv = $view->can_render( array( 'csv' ) );
1413 
1414  if ( is_wp_error( $error_csv ) ) {
1415  gravityview()->log->error( 'Not rendering CSV or TSV: ' . $error_csv->get_error_message() );
1416  return;
1417  }
1418 
1419  $file_type = $is_csv ? 'csv' : 'tsv';
1420 
1421  /**
1422  * @filter `gravityview/output/{csv|tsv}/filename` Modify the name of the generated CSV or TSV file. Name will be sanitized using sanitize_file_name() before output.
1423  * @see sanitize_file_name()
1424  * @since 2.1
1425  * @param string $filename File name used when downloading a CSV or TSV. Default is "{View title}.csv" or "{View title}.tsv"
1426  * @param \GV\View $view Current View being rendered
1427  */
1428  $filename = apply_filters( 'gravityview/output/' . $file_type . '/filename', get_the_title( $view->post ), $view );
1429 
1430  if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
1431  header( sprintf( 'Content-Disposition: attachment;filename="%s.' . $file_type . '"', sanitize_file_name( $filename ) ) );
1432  header( 'Content-Transfer-Encoding: binary' );
1433  header( 'Content-Type: text/' . $file_type );
1434  }
1435 
1436  ob_start();
1437  $csv_or_tsv = fopen( 'php://output', 'w' );
1438 
1439  /**
1440  * Add da' BOM if GF uses it
1441  * @see GFExport::start_export()
1442  */
1443  if ( apply_filters( 'gform_include_bom_export_entries', true, $view->form ? $view->form->form : null ) ) {
1444  fputs( $csv_or_tsv, "\xef\xbb\xbf" );
1445  }
1446 
1447  if ( $view->settings->get( 'csv_nolimit' ) ) {
1448  $view->settings->update( array( 'page_size' => -1 ) );
1449  }
1450 
1451  $entries = $view->get_entries();
1452 
1453  $headers_done = false;
1454  $allowed = $headers = array();
1455 
1456  foreach ( $view->fields->by_position( "directory_*" )->by_visible( $view )->all() as $id => $field ) {
1457  $allowed[] = $field;
1458  }
1459 
1460  $renderer = new Field_Renderer();
1461 
1462  foreach ( $entries->all() as $entry ) {
1463 
1464  $return = array();
1465 
1466  /**
1467  * @filter `gravityview/csv/entry/fields` Whitelist more entry fields by ID that are output in CSV requests.
1468  * @param[in,out] array $allowed The allowed ones, default by_visible, by_position( "context_*" ), i.e. as set in the View.
1469  * @param \GV\View $view The view.
1470  * @param \GV\Entry $entry WordPress representation of the item.
1471  */
1472  $allowed_field_ids = apply_filters( 'gravityview/csv/entry/fields', wp_list_pluck( $allowed, 'ID' ), $view, $entry );
1473 
1474  $allowed = array_filter( $allowed, function( $field ) use ( $allowed_field_ids ) {
1475  return in_array( $field->ID, $allowed_field_ids, true );
1476  } );
1477 
1478  foreach ( array_diff( $allowed_field_ids, wp_list_pluck( $allowed, 'ID' ) ) as $field_id ) {
1479  $allowed[] = is_numeric( $field_id ) ? \GV\GF_Field::by_id( $view->form, $field_id ) : \GV\Internal_Field::by_id( $field_id );
1480  }
1481 
1482  foreach ( $allowed as $field ) {
1483  $source = is_numeric( $field->ID ) ? $view->form : new \GV\Internal_Source();
1484 
1485  $return[] = $renderer->render( $field, $view, $source, $entry, gravityview()->request, '\GV\Field_CSV_Template' );
1486 
1487  if ( ! $headers_done ) {
1488  $label = $field->get_label( $view, $source, $entry );
1489  $headers[] = $label ? $label : $field->ID;
1490  }
1491  }
1492 
1493  // If not "tsv" then use comma
1494  $delimiter = ( 'tsv' === $file_type ) ? "\t" : ',';
1495 
1496  if ( ! $headers_done ) {
1497  $headers_done = fputcsv( $csv_or_tsv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), array_values( $headers ) ), $delimiter );
1498  }
1499 
1500  fputcsv( $csv_or_tsv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), $return ), $delimiter );
1501  }
1502 
1503  fflush( $csv_or_tsv );
1504 
1505  echo rtrim( ob_get_clean() );
1506 
1507  if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
1508  exit;
1509  }
1510  }
1511 
1512  /**
1513  * Return the query class for this View.
1514  *
1515  * @return string The class name.
1516  */
1517  public function get_query_class() {
1518  /**
1519  * @filter `gravityview/query/class`
1520  * @param[in,out] string The query class. Default: GF_Query.
1521  * @param \GV\View $this The View.
1522  */
1523  $query_class = apply_filters( 'gravityview/query/class', '\GF_Query', $this );
1524  return $query_class;
1525  }
1526 
1527  /**
1528  * Restrict View access to specific capabilities.
1529  *
1530  * Hooked into `map_meta_cap` WordPress filter.
1531  *
1532  * @since develop
1533  *
1534  * @param $caps array The output capabilities.
1535  * @param $cap string The cap that is being checked.
1536  * @param $user_id int The User ID.
1537  * @param $args array Additional arguments to the capability.
1538  *
1539  * @return array The resulting capabilities.
1540  */
1541  public static function restrict( $caps, $cap, $user_id, $args ) {
1542  /**
1543  * @filter `gravityview/security/require_unfiltered_html` Bypass restrictions on Views that require `unfiltered_html`.
1544  * @param[in,out] boolean
1545  *
1546  * @since develop
1547  * @param string $cap The capability requested.
1548  * @param int $user_id The user ID.
1549  * @param array $args Any additional args to map_meta_cap
1550  */
1551  if ( ! apply_filters( 'gravityview/security/require_unfiltered_html', true, $cap, $user_id ) ) {
1552  return $caps;
1553  }
1554 
1555  switch ( $cap ):
1556  case 'edit_gravityview':
1557  case 'edit_gravityviews':
1558  case 'edit_others_gravityviews':
1559  case 'edit_private_gravityviews':
1560  case 'edit_published_gravityviews':
1561  if ( ! user_can( $user_id, 'unfiltered_html' ) ) {
1562  if ( ! user_can( $user_id, 'gravityview_full_access' ) ) {
1563  return array( 'do_not_allow' );
1564  }
1565  }
1566 
1567  return $caps;
1568  case 'edit_post':
1569  if ( 'gravityview' === get_post_type( array_pop( $args ) ) ) {
1570  return self::restrict( $caps, 'edit_gravityview', $user_id, $args );
1571  }
1572  endswitch;
1573 
1574  return $caps;
1575  }
1576 
1577  public function __get( $key ) {
1578  if ( $this->post ) {
1579  $raw_post = $this->post->filter( 'raw' );
1580  return $raw_post->{$key};
1581  }
1582  return isset( $this->{$key} ) ? $this->{$key} : null;
1583  }
1584 }
If this file is called directly, abort.
$image
Definition: post_image.php:98
$labels
static get_joins( $post)
Get joins associated with a view.
If this file is called directly, abort.
__construct()
The constructor.
static content( $content)
A renderer filter for the View post type content.
$forms
Definition: data-source.php:19
__get( $key)
static calculate_get_entries_criteria( $passed_criteria=array(), $form_ids=array())
Calculates the Search Criteria used on the self::get_entries / self::get_entry methods.
static _override_sorting_id_by_field_type( $sort_field_id, $form_id)
Override sorting per field.
static restrict( $caps, $cap, $user_id, $args)
Restrict View access to specific capabilities.
as_data()
Be compatible with the old data object.
offsetUnset( $offset)
ArrayAccess compatibility layer with GravityView_View_Data::$views.
offsetSet( $offset, $value)
ArrayAccess compatibility layer with GravityView_View_Data::$views.
can_render( $context=null, $request=null)
Checks whether this view can be accessed or not.
gravityview_get_form( $form_id)
Returns the form object for a given Form ID.
gravityview_get_template_settings( $post_id)
Get all the settings for a View.
If this file is called directly, abort.
$entries
offsetGet( $offset)
ArrayAccess compatibility layer with GravityView_View_Data::$views.
gravityview()
Definition: _stubs.php:26
global $post
Definition: delete-entry.php:7
get( $key, $default=null)
Retrieve a setting.
if(gravityview() ->plugin->is_GF_25()) $form
static by_id( $form, $field_id)
Get a by and Field ID.
const DESC
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
static _flush_cache( $view_id=null)
Flush the view cache.
static no_views_text()
Get text for no views found.
Definition: class-admin.php:65
static check_entry_display( $entry, $view=null)
Checks if a certain entry is valid according to the View search filters (specially the Adv Filters) ...
const APPROVED
If this file is called directly, abort.
const ASC
static exists( $view)
Determines if a view exists to begin with.
static is_field_numeric( $form=null, $field='')
Checks if the field type is a &#39;numeric&#39; field type (e.g.
static by_id( $post_id)
Construct a instance from a post ID.
If this file is called directly, abort.
static add_rewrite_endpoint()
Add extra rewrite endpoints.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:31
$field_id
Definition: time.php:17
offsetExists( $offset)
ArrayAccess compatibility layer with GravityView_View_Data::$views.
static template_redirect()
Last chance to configure the output.
static by_id( $field_id)
Get a from an internal Gravity Forms field ID.
if(empty( $created_by)) $form_id
If this file is called directly, abort.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
If this file is called directly, abort.
If this file is called directly, abort.
gravityview_get_template_id( $post_id)
Get the template ID (list, table, datatables, map) for a View.
get_entries( $request=null)
Retrieve the entries for the current view and request.
static get_joined_forms( $post_id)
Get joined forms associated with a view In no particular order.
static is_approved( $status)
If this file is called directly, abort.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
$entry
Definition: notes.php:27
static register_post_type()
Register the gravityview WordPress Custom Post Type.
static get_unions( $post)
Get unions associated with a view.
static from_post( $post)
Construct a instance from a .
const meta_key
If this file is called directly, abort.
$field
Definition: gquiz_grade.php:11
$title
get_query_class()
Return the query class for this View.
If this file is called directly, abort.