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 View List Template class . |
11
|
|
|
* |
12
|
|
|
* Renders a \GV\View and a \GV\Entry_Collection via a \GV\View_Renderer. |
13
|
|
|
*/ |
14
|
|
|
class View_List_Template extends View_Template { |
15
|
|
|
/** |
16
|
|
|
* @var string The template slug to be loaded (like "table", "list") |
17
|
|
|
*/ |
18
|
|
|
public static $slug = 'list'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Output the field in the list view. |
22
|
|
|
* |
23
|
|
|
* @param \GV\Field $field The field to output. |
24
|
|
|
* @param \GV\Entry $entry The entry. |
25
|
|
|
* @param array $extras Extra stuff, like wpautop, etc. |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
3 |
|
public function the_field( \GV\Field $field, \GV\Entry $entry, $extras = null ) { |
30
|
3 |
|
$form = $this->view->form; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Push legacy entry context. |
34
|
|
|
*/ |
35
|
3 |
|
\GV\Mocks\Legacy_Context::load( array( |
36
|
3 |
|
'entry' => $entry, |
37
|
|
|
) ); |
38
|
|
|
|
39
|
3 |
|
if ( $entry instanceof Multi_Entry ) { |
40
|
1 |
|
if ( ! $entry = Utils::get( $entry, $field->form_id ) ) { |
|
|
|
|
41
|
1 |
|
return; |
42
|
|
|
} |
43
|
1 |
|
$form = GF_Form::by_id( $field->form_id ); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
$context = Template_Context::from_template( $this, compact( 'field', 'entry' ) ); |
47
|
|
|
|
48
|
3 |
|
$renderer = new Field_Renderer(); |
49
|
3 |
|
$source = is_numeric( $field->ID ) ? $form : new Internal_Source(); |
50
|
|
|
|
51
|
3 |
|
$value = $renderer->render( $field, $this->view, $source, $entry, $this->request ); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @deprecated Here for back-compatibility. |
55
|
|
|
*/ |
56
|
3 |
|
$label = apply_filters( 'gravityview_render_after_label', $field->get_label( $this->view, $form ), $field->as_configuration() ); |
57
|
3 |
|
$label = apply_filters( 'gravityview/template/field_label', $label, $field->as_configuration(), $form->form ? $form->form : null, null ); |
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @filter `gravityview/template/field/label` Override the field label. |
61
|
|
|
* @since 2.0 |
62
|
|
|
* @param[in,out] string $label The label to override. |
63
|
|
|
* @param \GV\Template_Context $context The context. |
64
|
|
|
*/ |
65
|
3 |
|
$label = apply_filters( 'gravityview/template/field/label', $label, $context ); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @filter `gravityview/template/table/entry/hide_empty` |
69
|
|
|
* @param boolean Should the row be hidden if the value is empty? Default: don't hide. |
70
|
|
|
* @param \GV\Template_Context $context The context ;) Love it, cherish it. And don't you dare modify it! |
71
|
|
|
*/ |
72
|
3 |
|
$hide_empty = apply_filters( 'gravityview/render/hide-empty-zone', Utils::get( $extras, 'hide_empty', $this->view->settings->get( 'hide_empty', false ) ), $context ); |
73
|
|
|
|
74
|
3 |
|
if ( is_numeric( $field->ID ) ) { |
75
|
3 |
|
$extras['field'] = $field->as_configuration(); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
$extras['entry'] = $entry->as_entry(); |
79
|
3 |
|
$extras['hide_empty'] = $hide_empty; |
80
|
3 |
|
$extras['label'] = $label; |
81
|
3 |
|
$extras['value'] = $value; |
82
|
|
|
|
83
|
3 |
|
return \gravityview_field_output( $extras, $context ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return an array of variables ready to be extracted. |
88
|
|
|
* |
89
|
|
|
* @param string|array $zones The field zones to grab. |
90
|
|
|
* |
91
|
|
|
* @return array An array ready to be extract()ed in the form of |
92
|
|
|
* $zone => \GV\Field_Collection |
93
|
|
|
* has_$zone => int |
94
|
|
|
*/ |
95
|
3 |
|
public function extract_zone_vars( $zones ) { |
96
|
3 |
|
if ( ! is_array( $zones ) ) { |
97
|
|
|
$zones = array( $zones ); |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
$vars = array(); |
101
|
3 |
|
foreach ( $zones as $zone ) { |
102
|
3 |
|
$zone_var = str_replace( '-', '_', $zone ); |
103
|
3 |
|
$vars[ $zone_var ] = $this->view->fields->by_position( 'directory_list-' . $zone )->by_visible(); |
104
|
3 |
|
$vars[ "has_$zone_var" ] = $vars[ $zone_var ]->count(); |
105
|
|
|
} |
106
|
|
|
|
107
|
3 |
|
return $vars; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* `gravityview_entry_class` and `gravityview/template/list/entry/class` filters. |
112
|
|
|
* |
113
|
|
|
* Modify of the class of a row. |
114
|
|
|
* |
115
|
|
|
* @param string $class The class. |
116
|
|
|
* @param \GV\Entry $entry The entry. |
117
|
|
|
* @param \GV\Template_Context The context. |
118
|
|
|
* |
119
|
|
|
* @return string The classes. |
120
|
|
|
*/ |
121
|
3 |
|
public static function entry_class( $class, $entry, $context ) { |
122
|
|
|
/** |
123
|
|
|
* @filter `gravityview_entry_class` Modify the class applied to the entry row. |
124
|
|
|
* @param string $class Existing class. |
125
|
|
|
* @param array $entry Current entry being displayed |
126
|
|
|
* @param \GravityView_View $this Current GravityView_View object |
127
|
|
|
* @deprecated Use `gravityview/template/list/entry/class` |
128
|
|
|
* @return string The modified class. |
129
|
|
|
*/ |
130
|
3 |
|
$class = apply_filters( 'gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance() ); |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @filter `gravityview/template/list/entry/class` Modify the class aplied to the entry row. |
134
|
|
|
* @param string $class The existing class. |
135
|
|
|
* @param \GV\Template_Context The context. |
136
|
|
|
* @return string The modified class. |
137
|
|
|
*/ |
138
|
3 |
|
return apply_filters( 'gravityview/template/list/entry/class', $class, Template_Context::from_template( $context->template, compact( 'entry' ) ) ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* `gravityview_list_body_before` and `gravityview/template/list/body/before` actions. |
143
|
|
|
* |
144
|
|
|
* Output inside the `tbody` of the list. |
145
|
|
|
* |
146
|
|
|
* @param $context \GV\Template_Context The 2.0 context. |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
3 |
|
public static function body_before( $context ) { |
151
|
|
|
/** |
152
|
|
|
* @action `gravityview/template/list/body/before` Output inside the `tbody` of the list. |
153
|
|
|
* @since 2.0 |
154
|
|
|
* @param \GV\Template_Context $context The template context. |
155
|
|
|
*/ |
156
|
3 |
|
do_action( 'gravityview/template/list/body/before', $context ); |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @action `gravityview_list_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows. |
160
|
|
|
* @deprecated Use `gravityview/template/list/body/before` |
161
|
|
|
* @since 1.0.7 |
162
|
|
|
* @param \GravityView_View $gravityview_view Current GravityView_View object. |
163
|
|
|
*/ |
164
|
3 |
|
do_action( 'gravityview_list_body_before', \GravityView_View::getInstance() /** ugh! */ ); |
165
|
3 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* `gravityview_list_body_after` and `gravityview/template/list/body/after` actions. |
169
|
|
|
* |
170
|
|
|
* Output inside the `tbody` of the list. |
171
|
|
|
* |
172
|
|
|
* @param $context \GV\Template_Context The 2.0 context. |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
3 |
|
public static function body_after( $context ) { |
177
|
|
|
/** |
178
|
|
|
* @action `gravityview/template/list/body/after` Output inside the `tbody` of the list at the end. |
179
|
|
|
* @since 2.0 |
180
|
|
|
* @param \GV\Template_Context $context The template context. |
181
|
|
|
*/ |
182
|
3 |
|
do_action( 'gravityview/template/list/body/after', $context ); |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @action `gravityview_list_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows. |
186
|
|
|
* @deprecated Use `gravityview/template/list/body/after` |
187
|
|
|
* @since 1.0.7 |
188
|
|
|
* @param \GravityView_View $gravityview_view Current GravityView_View object. |
189
|
|
|
*/ |
190
|
3 |
|
do_action( 'gravityview_list_body_after', \GravityView_View::getInstance() /** ugh! */ ); |
191
|
3 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* `gravityview_list_entry_before` and `gravityview/template/list/entry/before` actions. |
195
|
|
|
* `gravityview_list_entry_title_before` and `gravityview/template/list/entry/title/before` actions. |
196
|
|
|
* `gravityview_list_entry_content_before` and `gravityview/template/list/entry/content/before` actions. |
197
|
|
|
* `gravityview_list_entry_footer_before` and `gravityview/template/list/entry/footer/before` actions. |
198
|
|
|
* |
199
|
|
|
* Output inside the `entry` of the list. |
200
|
|
|
* |
201
|
|
|
* @param \GV\Entry $entry The entry. |
202
|
|
|
* @param \GV\Template_Context $context The 2.0 context. |
203
|
|
|
* @param string $zone The list zone (footer, image, title, etc.). |
204
|
|
|
* |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
3 |
|
public static function entry_before( $entry, $context, $zone = '' ) { |
208
|
3 |
|
$zone = str_replace( '//', '/', "/$zone/" ); |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @action `gravityview/template/list/entry/$zone/before` Output inside the `entry` of the list at the end. |
212
|
|
|
* @since 2.0 |
213
|
|
|
* @param \GV\Template_Context $context The template context. |
214
|
|
|
*/ |
215
|
3 |
|
do_action( sprintf( 'gravityview/template/list/entry%sbefore', $zone ), Template_Context::from_template( $context->template, compact( 'entry' ) ) ); |
216
|
|
|
|
217
|
3 |
|
$zone = str_replace( '/', '_', $zone ); |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @action `gravityview_list_entry_$zone_before` Inside the `entry`, before any rows are rendered. Can be used to insert additional rows. |
221
|
|
|
* @deprecated Use `gravityview/template/list/entry/$zone/before` |
222
|
|
|
* @since 1.0.7 |
223
|
|
|
* @param \GravityView_View $gravityview_view Current GravityView_View object. |
224
|
|
|
*/ |
225
|
3 |
|
do_action( sprintf( 'gravityview_list_entry%sbefore', $zone ), $entry->as_entry(), \GravityView_View::getInstance() /** ugh! */ ); |
226
|
3 |
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* `gravityview_list_entry_after` and `gravityview/template/list/entry/after` actions. |
230
|
|
|
* `gravityview_list_entry_title_after` and `gravityview/template/list/entry/title/after` actions. |
231
|
|
|
* `gravityview_list_entry_content_after` and `gravityview/template/list/entry/content/after` actions. |
232
|
|
|
* `gravityview_list_entry_footer_after` and `gravityview/template/list/entry/footer/after` actions. |
233
|
|
|
* |
234
|
|
|
* Output inside the `entry` of the list. |
235
|
|
|
* |
236
|
|
|
* @param \GV\Entry $entry The entry. |
237
|
|
|
* @param \GV\Template_Context $context The 2.0 context. |
238
|
|
|
* @param string $zone The list zone (footer, image, title, etc.). |
239
|
|
|
* |
240
|
|
|
* @return void |
241
|
|
|
*/ |
242
|
3 |
|
public static function entry_after( $entry, $context, $zone = '' ) { |
243
|
3 |
|
$zone = str_replace( '//', '/', "/$zone/" ); |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @action `gravityview/template/list/entry/$zone/after` Output inside the `entry` of the list at the end. |
247
|
|
|
* @since 2.0 |
248
|
|
|
* @param \GV\Template_Context $context The template context. |
249
|
|
|
*/ |
250
|
3 |
|
do_action( sprintf( 'gravityview/template/list/entry%safter', $zone ), Template_Context::from_template( $context->template, compact( 'entry' ) ) ); |
251
|
|
|
|
252
|
3 |
|
$zone = str_replace( '/', '_', $zone ); |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @action `gravityview_list_entry_$zone_after` Inside the `entry`, after any rows are rendered. Can be used to insert additional rows. |
256
|
|
|
* @deprecated Use `gravityview/template/list/entry/after` |
257
|
|
|
* @since 1.0.7 |
258
|
|
|
* @param \GravityView_View $gravityview_view Current GravityView_View object. |
259
|
|
|
*/ |
260
|
3 |
|
do_action( sprintf( 'gravityview_list_entry%safter', $zone ), $entry->as_entry(), \GravityView_View::getInstance() /** ugh! */ ); |
261
|
3 |
|
} |
262
|
|
|
} |
263
|
|
|
|
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.