GravityView  2.10.1
The best, easiest way to display Gravity Forms entries on your website.
class-gv-renderer-entry.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\Entry_Renderer class.
11  *
12  * Houses some preliminary \GV\Entry rendering functionality.
13  */
14 class Entry_Renderer extends Renderer {
15 
16  /**
17  * Renders a single \GV\Entry instance.
18  *
19  * @param \GV\Entry $entry The Entry instance to render.
20  * @param \GV\View $view The View connected to the entry.
21  * @param \GV\Request $request The request context we're currently in. Default: `gravityview()->request`
22  *
23  * @api
24  * @since 2.0
25  *
26  * @return string The rendered Entry.
27  */
28  public function render( Entry $entry, View $view, Request $request = null ) {
29  if ( is_null( $request ) ) {
30  $request = &gravityview()->request;
31  }
32 
33  if ( ! $request->is_renderable() ) {
34  gravityview()->log->error( 'Renderer unable to render Entry in {request_class} context', array( 'request_class' => get_class( $request ) ) );
35  return null;
36  }
37 
38  /**
39  * This View is password protected. Output the form.
40  */
41  if ( post_password_required( $view->ID ) ) {
42  return get_the_password_form( $view->ID );
43  }
44 
45 
46  /**
47  * @action `gravityview_render_entry_{View ID}` Before rendering a single entry for a specific View ID
48  * @since 1.17
49  *
50  * @since 2.0
51  * @param \GV\Entry $entry The entry about to be rendered
52  * @param \GV\View $view The connected view
53  * @param \GV\Request $request The associated request
54  */
55  do_action( 'gravityview_render_entry_' . $view->ID, $entry, $view, $request );
56 
57  /** Entry does not belong to this view. */
58  if ( $view->joins ) {
59  $form_ids = array();
60  foreach ( $view->joins as $join ) {
61  $form_ids[] = $join->join->ID;
62  $form_ids[] = $join->join_on->ID;
63  }
64  foreach ( $entry->entries as $e ) {
65  if ( ! in_array( $e['form_id'], $form_ids ) ) {
66  gravityview()->log->error( 'The requested entry does not belong to this View. Entry #{entry_id}, #View {view_id}', array( 'entry_id' => $e->ID, 'view_id' => $view->ID ) );
67  return null;
68  }
69  }
70  } else if ( $view->form && $view->form->ID != $entry['form_id'] ) {
71  gravityview()->log->error( 'The requested entry does not belong to this View. Entry #{entry_id}, #View {view_id}', array( 'entry_id' => $entry->ID, 'view_id' => $view->ID ) );
72  return null;
73  }
74 
75  /**
76  * @filter `gravityview_template_slug_{$template_id}` Modify the template slug about to be loaded in directory views.
77  * @since 1.6
78  * @param deprecated
79  * @see The `gravityview_get_template_id` filter
80  * @param string $slug Default: 'table'
81  * @param string $view The current view context: single
82  */
83  $template_slug = apply_filters( 'gravityview_template_slug_' . $view->settings->get( 'template' ), 'table', 'single' );
84 
85  /**
86  * Load a legacy override template if exists.
87  */
88  $override = new \GV\Legacy_Override_Template( $view, $entry, null, $request );
89  foreach ( array( 'single' ) as $part ) {
90  if ( ( $path = $override->get_template_part( $template_slug, $part ) ) && strpos( $path, '/deprecated' ) === false ) {
91  /**
92  * We have to bail and call the legacy renderer. Crap!
93  */
94  gravityview()->log->notice( 'Legacy templates detected in theme {path}', array( 'path' => $path ) );
95 
96  /**
97  * Show a warning at the top, if View is editable by the user.
98  */
99  add_action( 'gravityview_before', $this->legacy_template_warning( $view, $path ) );
100 
101  return $override->render( $template_slug );
102  }
103  }
104 
105  /**
106  * @filter `gravityview/template/entry/class` Filter the template class that is about to be used to render the entry.
107  * @since 2.0
108  * @param string $class The chosen class - Default: \GV\Entry_Table_Template.
109  * @param \GV\Entry $entry The entry about to be rendered.
110  * @param \GV\View $view The view connected to it.
111  * @param \GV\Request $request The associated request.
112  */
113  $class = apply_filters( 'gravityview/template/entry/class', sprintf( '\GV\Entry_%s_Template', ucfirst( $template_slug ) ), $entry, $view, $request );
114  if ( ! $class || ! class_exists( $class ) ) {
115  gravityview()->log->notice( '{template_class} not found, falling back to legacy', array( 'template_class' => $class ) );
116  $class = '\GV\Entry_Legacy_Template';
117  }
118  $template = new $class( $entry, $view, $request );
119 
120  add_action( 'gravityview/template/after', $view_id_output = function( $context ) {
121  printf( '<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID );
122  } );
123 
124  /** Mock the legacy state for the widgets and whatnot */
125  $entries = new Entry_Collection();
126  $entries->add( $entry );
128  'view' => $view,
129  'entries' => $entries,
130  'entry' => $entry,
131  'request' => $request,
132  ) );
133 
134  ob_start();
135  $template->render();
136 
137  remove_action( 'gravityview/template/after', $view_id_output );
138 
139  return ob_get_clean();
140  }
141 }
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
gravityview()
Definition: _stubs.php:26
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.
If this file is called directly, abort.
If this file is called directly, abort.
$entry
Definition: notes.php:27
render(Entry $entry, View $view, Request $request=null)
Renders a single instance.