GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-renderer-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 \GV\View_Renderer class.
11  *
12  * Houses some preliminary \GV\View rendering functionality.
13  */
14 class View_Renderer extends Renderer {
15 
16  /**
17  * Renders a \GV\View instance.
18  *
19  * @since 2.0
20  *
21  * @api
22  *
23  * @param \GV\Request $request The request context we're currently in. Default: `gravityview()->request`
24  *
25  * @param View $view The View instance to render.
26  *
27  * @return string The rendered View.
28  */
29  public function render( View $view, Request $request = null ) {
30  if ( is_null( $request ) ) {
31  $request = &gravityview()->request;
32  }
33 
34  if ( ! $request->is_renderable() ) {
35  gravityview()->log->error( 'Renderer unable to render View in {request_class} context', array( 'request_class' => get_class( $request ) ) );
36  return null;
37  }
38 
39  /**
40  * @filter `gravityview_template_slug_{$template_id}` Modify the template slug about to be loaded in directory views.
41  * @since 1.6
42  * @deprecated
43  * @see The `gravityview_get_template_id` filter
44  * @param string $slug Default: 'table'
45  * @param string $view The current view context: directory.
46  */
47  $template_slug = apply_filters( 'gravityview_template_slug_' . $view->settings->get( 'template' ), 'table', 'directory' );
48 
49  /**
50  * Figure out whether to get the entries or not.
51  *
52  * Some contexts don't need initial entries, like the DataTables directory type.
53  *
54  * @filter `gravityview_get_view_entries_{$template_slug}` Whether to get the entries or not.
55  * @param boolean $get_entries Get entries or not, default: true.
56  */
57  $get_entries = apply_filters( 'gravityview_get_view_entries_' . $template_slug, true );
58 
59  $hide_until_searched = $view->settings->get( 'hide_until_searched' );
60 
61  /**
62  * Hide View data until search is performed.
63  */
64  $get_entries = ( $hide_until_searched && ! $request->is_search() ) ? false : $get_entries;
65 
66  /**
67  * Fetch entries for this View.
68  */
69  if ( $get_entries ) {
70  $entries = $view->get_entries( $request );
71  } else {
72  $entries = new \GV\Entry_Collection();
73  }
74 
75  /**
76  * Load a legacy override template if exists.
77  */
78  $override = new \GV\Legacy_Override_Template( $view, null, null, $request );
79  foreach ( array( 'header', 'body', 'footer' ) as $part ) {
80 
81  $path = $override->get_template_part( $template_slug, $part );
82 
83  if ( $path && strpos( $path, '/deprecated' ) === false ) {
84  /**
85  * We have to bail and call the legacy renderer. Crap!
86  */
87  gravityview()->log->notice( 'Legacy templates detected in theme {path}', array( 'path' => $path ) );
88 
89  /**
90  * Show a warning at the top, if View is editable by the user.
91  */
92  add_action( 'gravityview_before', $this->legacy_template_warning( $view, $path ) );
93 
94  return $override->render( $template_slug );
95  }
96  }
97 
98  /**
99  * @filter `gravityview/template/view/class` Filter the template class that is about to be used to render the view.
100  * @since 2.0
101  * @param string $class The chosen class - Default: \GV\View_Table_Template.
102  * @param View $view The view about to be rendered.
103  * @param \GV\Request $request The associated request.
104  */
105  $class = apply_filters( 'gravityview/template/view/class', sprintf( '\GV\View_%s_Template', ucfirst( $template_slug ) ), $view, $request );
106  if ( ! $class || ! class_exists( $class ) ) {
107  gravityview()->log->notice( '{template_class} not found, falling back to legacy', array( 'template_class' => $class ) );
108  $class = '\GV\View_Legacy_Template';
109  }
110 
111  /** @var \GV\View_Table_Template|\GV\View_List_Template|\GV\View_Legacy_Template $template */
112  $template = new $class( $view, $entries, $request );
113 
114  /**
115  * @var [] $counter A counter incrementing each time a View is rendered.
116  * @since 2.15
117  * @usedby `gravityview/template/view/render` filter
118  */
119  static $counter = array();
120 
121  $counter[ $view->ID ] = isset( $counter[ $view->ID ] ) ? $counter[ $view->ID ] + 1 : 1;
122 
123  /**
124  * Updates the View anchor ID each time the View is rendered.
125  * @since 2.15
126  * @uses {@var $counter}
127  * @param Template_Context $context
128  */
129  add_action( 'gravityview/template/view/render', $add_anchor_id_filter = function ( $context ) use ( &$counter ) {
130  /** @see \GV\View::set_anchor_id() */
131  $context->view->set_anchor_id( $counter[ $context->view->ID ] );
132  } );
133 
134  $add_search_action_filter = function ( $action ) use ( $view ) {
135  return $action . '#' . $view->get_anchor_id();
136  };
137 
138  /**
139  * @filter `gravityview/widget/search/set_view_id_anchor` Allow appending the View ID anchor to the search URL.
140  * @since 2.15
141  *
142  * @param bool $set_view_id_anchor
143  */
144  if ( apply_filters( 'gravityview/widget/search/append_view_id_anchor', true ) ) {
145  /**
146  * Append the View anchor ID to the search form action.
147  * @since 2.15
148  *
149  * @param string $action The search form action URL.
150  *
151  * @uses {@var View $view}
152  */
153  add_filter( 'gravityview/widget/search/form/action', $add_search_action_filter );
154  }
155 
156  /**
157  * Remove multiple sorting before calling legacy filters.
158  * This allows us to fake it till we make it.
159  */
160  $parameters = $view->settings->as_atts();
161  if ( ! empty( $parameters['sort_field'] ) && is_array( $parameters['sort_field'] ) ) {
162  $has_multisort = true;
163  $parameters['sort_field'] = reset( $parameters['sort_field'] );
164  if ( ! empty( $parameters['sort_direction'] ) && is_array( $parameters['sort_direction'] ) ) {
165  $parameters['sort_direction'] = reset( $parameters['sort_direction'] );
166  }
167  }
168 
169  /** @todo Deprecate this! */
170  $parameters = \GravityView_frontend::get_view_entries_parameters( $parameters, $view->form->ID );
171 
172  global $post;
173 
174  /** Mock the legacy state for the widgets and whatnot */
175  \GV\Mocks\Legacy_Context::push( array_merge( array(
176  'view' => $view,
177  'entries' => $entries,
178  'request' => $request,
179  ), empty( $parameters ) ? array() : array(
180  'paging' => $parameters['paging'],
181  'sorting' => $parameters['sorting'],
182  ), empty( $post ) ? array() : array(
183  'post' => $post,
184  ) ) );
185 
186  add_action( 'gravityview/template/after', $view_id_output = function( $context ) {
187  printf( '<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID );
188  } );
189 
190  ob_start();
191  $template->render();
192 
193  remove_action( 'gravityview/template/after', $view_id_output );
194  remove_filter( 'gravityview/template/view/render', $add_anchor_id_filter );
195  remove_filter( 'gravityview/widget/search/form/action', $add_search_action_filter );
196 
198  return ob_get_clean();
199  }
200 }
If this file is called directly, abort.
if(! isset( $gravityview)||empty( $gravityview->template)) $template
The entry loop for the list output.
If this file is called directly, abort.
$class
$entries
global $post
Definition: delete-entry.php:7
static pop()
Restores last saved state and context.
Definition: _mocks.php:410
static push( $configuration)
Set the state depending on the provided configuration.
Definition: _mocks.php:400
If this file is called directly, abort.
If this file is called directly, abort.
gravityview()
The main GravityView wrapper function.
get_entries( $request=null)
Retrieve the entries for the current view and request.
get_anchor_id()
Returns the anchor ID to be used in the View container HTML id attribute.