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
|
14 |
|
public function render( Entry $entry, View $view, Request $request = null ) { |
29
|
14 |
|
if ( is_null( $request ) ) { |
30
|
8 |
|
$request = &gravityview()->request; |
31
|
|
|
} |
32
|
|
|
|
33
|
14 |
|
if ( ! in_array( get_class( $request ), array( 'GV\Frontend_Request', 'GV\Mock_Request', 'GV\REST\Request' ) ) ) { |
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
|
14 |
|
if ( post_password_required( $view->ID ) ) { |
|
|
|
|
42
|
1 |
|
return get_the_password_form( $view->ID ); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
46
|
|
|
/** Entry does not belong to this view. */ |
47
|
14 |
|
if ( $view->form && $view->form->ID != $entry['form_id'] ) { |
48
|
1 |
|
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 ) ); |
|
|
|
|
49
|
1 |
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @action `gravityview_render_entry_{View ID}` Before rendering a single entry for a specific View ID |
54
|
|
|
* @since 1.17 |
55
|
|
|
* |
56
|
|
|
* @since 2.0 |
57
|
|
|
* @param \GV\Entry $entry The entry about to be rendered |
58
|
|
|
* @param \GV\View $view The connected view |
59
|
|
|
* @param \GV\Request $request The associated request |
60
|
|
|
*/ |
61
|
14 |
|
do_action( 'gravityview_render_entry_' . $view->ID, $entry, $view, $request ); |
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @filter `gravityview_template_slug_{$template_id}` Modify the template slug about to be loaded in directory views. |
65
|
|
|
* @since 1.6 |
66
|
|
|
* @param deprecated |
67
|
|
|
* @see The `gravityview_get_template_id` filter |
68
|
|
|
* @param string $slug Default: 'table' |
69
|
|
|
* @param string $view The current view context: single |
70
|
|
|
*/ |
71
|
14 |
|
$template_slug = apply_filters( 'gravityview_template_slug_' . $view->settings->get( 'template' ), 'table', 'single' ); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Load a legacy override template if exists. |
75
|
|
|
*/ |
76
|
14 |
|
$override = new \GV\Legacy_Override_Template( $view, $entry, null, $request ); |
77
|
14 |
|
foreach ( array( 'single' ) as $part ) { |
78
|
14 |
|
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
|
14 |
|
return $override->render( $template_slug ); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @filter `gravityview/template/entry/class` Filter the template class that is about to be used to render the entry. |
95
|
|
|
* @since 2.0 |
96
|
|
|
* @param string $class The chosen class - Default: \GV\Entry_Table_Template. |
97
|
|
|
* @param \GV\Entry $entry The entry about to be rendered. |
98
|
|
|
* @param \GV\View $view The view connected to it. |
99
|
|
|
* @param \GV\Request $request The associated request. |
100
|
|
|
*/ |
101
|
14 |
|
$class = apply_filters( 'gravityview/template/entry/class', sprintf( '\GV\Entry_%s_Template', ucfirst( $template_slug ) ), $entry, $view, $request ); |
102
|
14 |
|
if ( ! $class || ! class_exists( $class ) ) { |
103
|
|
|
gravityview()->log->notice( '{template_class} not found, falling back to legacy', array( 'template_class' => $class ) ); |
104
|
|
|
$class = '\GV\Entry_Legacy_Template'; |
105
|
|
|
} |
106
|
14 |
|
$template = new $class( $entry, $view, $request ); |
107
|
|
|
|
108
|
14 |
|
add_action( 'gravityview/template/after', $view_id_output = function( $context ) { |
109
|
14 |
|
printf( '<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID ); |
110
|
14 |
|
} ); |
111
|
|
|
|
112
|
|
|
/** Mock the legacy state for the widgets and whatnot */ |
113
|
|
|
$entries = new Entry_Collection(); |
114
|
|
|
$entries->add( $entry ); |
115
|
|
|
\GV\Mocks\Legacy_Context::push( array( |
116
|
|
|
'view' => $view, |
117
|
|
|
'entries' => $entries, |
118
|
|
|
'entry' => $entry, |
119
|
|
|
'request' => $request, |
120
|
|
|
) ); |
121
|
|
|
|
122
|
|
|
ob_start(); |
123
|
|
|
$template->render(); |
124
|
|
|
|
125
|
|
|
remove_action( 'gravityview/template/after', $view_id_output ); |
126
|
|
|
|
127
|
|
|
return ob_get_clean(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.