GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-template-legacy-override.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  * Load up the Gamajo Template Loader.
11  *
12  * @see https://github.com/GaryJones/Gamajo-Template-Loader
13  */
14 if ( ! class_exists( '\GV\Gamajo_Template_Loader' ) ) {
15  require gravityview()->plugin->dir( 'future/lib/class-gamajo-template-loader.php' );
16 }
17 
18 /**
19  * Loads legacy override templates from the theme.
20  *
21  * Makes sure they work by setting up context, etc.
22  */
23 class Legacy_Override_Template extends \GV\Gamajo_Template_Loader {
24  /**
25  * Prefix for filter names.
26  * @var string
27  */
28  protected $filter_prefix = 'gravityview';
29 
30  /**
31  * Directory name where custom templates for this plugin should be found in the theme.
32  * @var string
33  */
34  protected $theme_template_directory = 'gravityview';
35 
36  /**
37  * @var \GV\View The view we're working with.
38  */
39  private $view;
40 
41  /**
42  * @var \GV\Entry The entry we're working with.
43  */
44  private $entry;
45 
46  /**
47  * Catch deprecated template loads.
48  *
49  * @param \GV\View $view The View.
50  * @param \GV\Entry $entry The Entry.
51  * @param \GV\Field $field The Field.
52  * @param \GV\Request $request The request.
53  *
54  * @return void
55  */
56  public function __construct( \GV\View $view, \GV\Entry $entry = null, \GV\Field $field = null, \GV\Request $request = null ) {
57  add_filter( $this->filter_prefix . '_get_template_part', array( $this, 'add_id_specific_templates' ), 10, 3 );
58 
59  $this->view = $view;
60  $this->entry = $entry;
61 
62  $this->plugin_directory = gravityview()->plugin->dir();
63  $this->plugin_template_directory = 'templates/deprecated/';
64  }
65 
66  public function __destruct() {
67  remove_filter( $this->filter_prefix . '_get_template_part', array( $this, 'add_id_specific_templates' ) );
68  }
69 
70  /**
71  * @inheritdoc
72  * @see Gamajo_Template_Loader::locate_template()
73  * @return null|string NULL: Template not found; String: path to template
74  */
75  public function locate_template( $template_names, $load = false, $require_once = false ) {
76  return parent::locate_template( $template_names, false, false );
77  }
78 
79  /**
80  * Enable overrides of GravityView templates on a granular basis
81  *
82  * The loading order is:
83  *
84  * - view-[View ID]-table-footer.php
85  * - form-[Form ID]-table-footer.php
86  * - page-[ID of post or page where view is embedded]-table-footer.php
87  * - table-footer.php
88  *
89  * @see Gamajo_Template_Loader::get_template_file_names() Where the filter is
90  * @param array $templates Existing list of templates.
91  * @param string $slug Name of the template base, example: `table`, `list`, `datatables`, `map`
92  * @param string $name Name of the template part, example: `body`, `footer`, `head`, `single`
93  *
94  * @return array $templates Modified template array, merged with existing $templates values
95  */
96  public function add_id_specific_templates( $templates, $slug, $name ) {
97 
98  $additional = array();
99 
100  // form-19-table-body.php
101  $additional[] = sprintf( 'form-%d-%s-%s.php', $this->view->form ? $this->view->form->ID : 0, $slug, $name );
102 
103  // view-3-table-body.php
104  $additional[] = sprintf( 'view-%d-%s-%s.php', $this->view->ID, $slug, $name );
105 
106  global $post;
107  if ( $post ) {
108  // page-19-table-body.php
109  $additional[] = sprintf( 'page-%d-%s-%s.php', $post->ID, $slug, $name );
110  }
111 
112  // Combine with existing table-body.php and table.php
113  $templates = array_merge( $additional, $templates );
114 
115  return $templates;
116  }
117 
118  /**
119  * Setup legacy rendering.
120  *
121  * @param string $slug The slug.
122  *
123  * @return string The output.
124  */
125  public function render( $slug ) {
126  add_action( 'gravityview/template/after', $view_id_output = function( $context ) {
127  printf( '<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID );
128  } );
129 
130  ob_start();
131 
132  $request = new Mock_Request();
133  $request->returns['is_view'] = $this->view;
134 
135  /**
136  * You got one shot. One opportunity. To render all the widgets you have ever wanted.
137  *
138  * Since we're overriding the singleton we need to remove the widget actions since they can only
139  * be called once in a request (did_action/do_action mutex).
140  *
141  * Oh, and Mom's spaghetti.
142  */
143  global $wp_filter;
144  foreach ( array( 'gravityview_before', 'gravityview_after' ) as $hook ) {
145  foreach ( $wp_filter[ $hook ]->callbacks[10] as $function_key => $callback ) {
146  if ( strpos( $function_key, 'render_widget_hooks' ) ) {
147  unset( $wp_filter[ $hook ]->callbacks[10][ $function_key ] );
148  }
149  }
150  }
151 
152  /**
153  * Single entry view.
154  */
155  if ( $this->entry ) {
156 
157  $request->returns['is_entry'] = $this->entry;
158 
159  global $post;
160 
161  $entries = new Entry_Collection();
162  $entries->add( $this->entry );
163 
165  'view' => $this->view,
166  'entry' => $this->entry,
167  'entries' => $entries,
168  'request' => $request,
169  'fields' => $this->view->fields->by_visible( $this->view ),
170  'in_the_loop' => true,
171  ) );
172 
173  \GravityView_View::getInstance()->setTemplatePartSlug( $slug );
174  \GravityView_View::getInstance()->setTemplatePartName( 'single' );
175 
176  \GravityView_View::getInstance()->_include( $this->get_template_part( $slug, 'single' ) );
177 
178  Mocks\Legacy_Context::pop();
179 
180  /**
181  * Directory view.
182  */
183  } else {
184  $entries = $this->view->get_entries( $request );
185 
186  /**
187  * Remove multiple sorting before calling legacy filters.
188  * This allows us to fake it till we make it.
189  */
190  $parameters = $this->view->settings->as_atts();
191  if ( ! empty( $parameters['sort_field'] ) && is_array( $parameters['sort_field'] ) ) {
192  $has_multisort = true;
193  $parameters['sort_field'] = reset( $parameters['sort_field'] );
194  if ( ! empty( $parameters['sort_direction'] ) && is_array( $parameters['sort_direction'] ) ) {
195  $parameters['sort_direction'] = reset( $parameters['sort_direction'] );
196  }
197  }
198 
199  $parameters = \GravityView_frontend::get_view_entries_parameters( $parameters, $this->view->form->ID );
200 
201  global $post;
202 
203  add_action( 'gravityview_before', array( \GravityView_View::getInstance(), 'render_widget_hooks' ) );
204  add_action( 'gravityview_after', array( \GravityView_View::getInstance(), 'render_widget_hooks' ) );
205 
206  foreach ( array( 'header', 'body', 'footer' ) as $part ) {
207  \GV\Mocks\Legacy_Context::push( array_merge( array(
208  'view' => $this->view,
209  'entries' => $entries,
210  'request' => $request,
211  'fields' => $this->view->fields->by_visible( $this->view ),
212  'in_the_loop' => true,
213  ), empty( $parameters ) ? array() : array(
214  'paging' => $parameters['paging'],
215  'sorting' => $parameters['sorting'],
216  ), $post ? array(
217  'post' => $post,
218  ) : array() ) );
219 
220  \GravityView_View::getInstance()->setTemplatePartSlug( $slug );
221 
222  \GravityView_View::getInstance()->setTemplatePartName( $part );
223 
224  \GravityView_View::getInstance()->_include( $this->get_template_part( $slug, $part ) );
225 
226  Mocks\Legacy_Context::pop();
227  }
228  }
229 
230  remove_action( 'gravityview/template/after', $view_id_output );
231 
232  return ob_get_clean();
233  }
234 }
If this file is called directly, abort.
If this file is called directly, abort.
static getInstance( $passed_post=NULL)
render( $slug)
Setup legacy rendering.
$entries
$templates
global $post
Definition: delete-entry.php:7
If this file is called directly, abort.
__construct(\GV\View $view, \GV\Entry $entry=null, \GV\Field $field=null, \GV\Request $request=null)
Catch deprecated template loads.
static push( $configuration)
Set the state depending on the provided configuration.
Definition: _mocks.php:400
If this file is called directly, abort.
add_id_specific_templates( $templates, $slug, $name)
Enable overrides of GravityView templates on a granular basis.
If this file is called directly, abort.
gravityview()
The main GravityView wrapper function.
If this file is called directly, abort.
$entry
Definition: notes.php:27
locate_template( $template_names, $load=false, $require_once=false)
If this file is called directly, abort.