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
|
|
|
* @param \GV\View $view The View instance to render. |
20
|
|
|
* @param \GV\Request $request The request context we're currently in. Default: `gravityview()->request` |
21
|
|
|
* |
22
|
|
|
* @api |
23
|
|
|
* @since 2.0 |
24
|
|
|
* |
25
|
|
|
* @return string The rendered View. |
26
|
|
|
*/ |
27
|
24 |
|
public function render( View $view, Request $request = null ) { |
28
|
24 |
|
if ( is_null( $request ) ) { |
29
|
14 |
|
$request = &gravityview()->request; |
30
|
|
|
} |
31
|
|
|
|
32
|
24 |
|
if ( ! $request->is_renderable() ) { |
33
|
|
|
gravityview()->log->error( 'Renderer unable to render View in {request_class} context', array( 'request_class' => get_class( $request ) ) ); |
34
|
|
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @filter `gravityview_template_slug_{$template_id}` Modify the template slug about to be loaded in directory views. |
39
|
|
|
* @since 1.6 |
40
|
|
|
* @param deprecated |
41
|
|
|
* @see The `gravityview_get_template_id` filter |
42
|
|
|
* @param string $slug Default: 'table' |
43
|
|
|
* @param string $view The current view context: directory. |
44
|
|
|
*/ |
45
|
24 |
|
$template_slug = apply_filters( 'gravityview_template_slug_' . $view->settings->get( 'template' ), 'table', 'directory' ); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Figure out whether to get the entries or not. |
49
|
|
|
* |
50
|
|
|
* Some contexts don't need initial entries, like the DataTables directory type. |
51
|
|
|
* |
52
|
|
|
* @filter `gravityview_get_view_entries_{$template_slug}` Whether to get the entries or not. |
53
|
|
|
* @param boolean $get_entries Get entries or not, default: true. |
54
|
|
|
*/ |
55
|
24 |
|
$get_entries = apply_filters( 'gravityview_get_view_entries_' . $template_slug, true ); |
56
|
|
|
|
57
|
24 |
|
$hide_until_searched = $view->settings->get( 'hide_until_searched' ); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Hide View data until search is performed. |
61
|
|
|
*/ |
62
|
24 |
|
$get_entries = ( $hide_until_searched && ! $request->is_search() ) ? false : $get_entries; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Fetch entries for this View. |
66
|
|
|
*/ |
67
|
24 |
|
if ( $get_entries ) { |
68
|
23 |
|
$entries = $view->get_entries( $request ); |
69
|
|
|
} else { |
70
|
4 |
|
$entries = new \GV\Entry_Collection(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Load a legacy override template if exists. |
75
|
|
|
*/ |
76
|
24 |
|
$override = new \GV\Legacy_Override_Template( $view, null, null, $request ); |
77
|
24 |
|
foreach ( array( 'header', 'body', 'footer' ) as $part ) { |
78
|
24 |
|
if ( ( $path = $override->get_template_part( $template_slug, $part ) ) && strpos( $path, '/deprecated' ) === false ) { |
79
|
|
|
/** |
80
|
|
|
* We have to bail and call the legacy renderer. Crap! |
81
|
|
|
*/ |
82
|
|
|
gravityview()->log->notice( 'Legacy templates detected in theme {path}', array( 'path' => $path ) ); |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Show a warning at the top, if View is editable by the user. |
86
|
|
|
*/ |
87
|
|
|
add_action( 'gravityview_before', $this->legacy_template_warning( $view, $path ) ); |
88
|
|
|
|
89
|
|
|
return $override->render( $template_slug ); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @filter `gravityview/template/view/class` Filter the template class that is about to be used to render the view. |
95
|
|
|
* @since 2.0 |
96
|
|
|
* @param string $class The chosen class - Default: \GV\View_Table_Template. |
97
|
|
|
* @param \GV\View $view The view about to be rendered. |
98
|
|
|
* @param \GV\Request $request The associated request. |
99
|
|
|
*/ |
100
|
24 |
|
$class = apply_filters( 'gravityview/template/view/class', sprintf( '\GV\View_%s_Template', ucfirst( $template_slug ) ), $view, $request ); |
101
|
24 |
|
if ( ! $class || ! class_exists( $class ) ) { |
102
|
|
|
gravityview()->log->notice( '{template_class} not found, falling back to legacy', array( 'template_class' => $class ) ); |
103
|
|
|
$class = '\GV\View_Legacy_Template'; |
104
|
|
|
} |
105
|
24 |
|
$template = new $class( $view, $entries, $request ); |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Remove multiple sorting before calling legacy filters. |
109
|
|
|
* This allows us to fake it till we make it. |
110
|
|
|
*/ |
111
|
24 |
|
$parameters = $view->settings->as_atts(); |
|
|
|
|
112
|
24 |
|
if ( ! empty( $parameters['sort_field'] ) && is_array( $parameters['sort_field'] ) ) { |
113
|
|
|
$has_multisort = true; |
114
|
|
|
$parameters['sort_field'] = reset( $parameters['sort_field'] ); |
115
|
|
|
if ( ! empty( $parameters['sort_direction'] ) && is_array( $parameters['sort_direction'] ) ) { |
116
|
|
|
$parameters['sort_direction'] = reset( $parameters['sort_direction'] ); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** @todo Deprecate this! */ |
121
|
24 |
|
$parameters = \GravityView_frontend::get_view_entries_parameters( $parameters, $view->form->ID ); |
122
|
|
|
|
123
|
24 |
|
global $post; |
124
|
|
|
|
125
|
|
|
/** Mock the legacy state for the widgets and whatnot */ |
126
|
24 |
|
\GV\Mocks\Legacy_Context::push( array_merge( array( |
127
|
24 |
|
'view' => $view, |
128
|
24 |
|
'entries' => $entries, |
129
|
24 |
|
'request' => $request, |
130
|
|
|
), empty( $parameters ) ? array() : array( |
131
|
24 |
|
'paging' => $parameters['paging'], |
132
|
24 |
|
'sorting' => $parameters['sorting'], |
133
|
13 |
|
), empty( $post ) ? array() : array( |
134
|
24 |
|
'post' => $post, |
135
|
|
|
) ) ); |
136
|
|
|
|
137
|
|
|
add_action( 'gravityview/template/after', $view_id_output = function( $context ) { |
138
|
24 |
|
printf( '<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID ); |
139
|
24 |
|
} ); |
140
|
|
|
|
141
|
24 |
|
ob_start(); |
142
|
24 |
|
$template->render(); |
143
|
|
|
|
144
|
24 |
|
remove_action( 'gravityview/template/after', $view_id_output ); |
145
|
|
|
|
146
|
24 |
|
\GV\Mocks\Legacy_Context::pop(); |
147
|
24 |
|
return ob_get_clean(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This method has been deprecated.