|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* GravityView templating engine class |
|
4
|
|
|
* |
|
5
|
|
|
* @package GravityView |
|
6
|
|
|
* @license GPL2+ |
|
7
|
|
|
* @author Katz Web Services, Inc. |
|
8
|
|
|
* @link http://gravityview.co |
|
9
|
|
|
* @copyright Copyright 2014, Katz Web Services, Inc. |
|
10
|
|
|
* |
|
11
|
|
|
* @since 1.0.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** If this file is called directly, abort. */ |
|
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
16
|
|
|
die; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
if( ! class_exists( 'Gamajo_Template_Loader' ) ) { |
|
20
|
|
|
require( GRAVITYVIEW_DIR . 'includes/lib/class-gamajo-template-loader.php' ); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
class GravityView_View extends Gamajo_Template_Loader { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Prefix for filter names. |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $filter_prefix = 'gravityview'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Directory name where custom templates for this plugin should be found in the theme. |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $theme_template_directory = 'gravityview'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Reference to the root directory path of this plugin. |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $plugin_directory = GRAVITYVIEW_DIR; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Store templates locations that have already been located |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $located_templates = array(); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The name of the template, like "list", "table", or "datatables" |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $template_part_slug = ''; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The name of the file part, like "body" or "single" |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $template_part_name = ''; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var int Gravity Forms form ID |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $form_id = NULL; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var int View ID |
|
68
|
|
|
* @todo: this needs to be public until extensions support 1.7+ |
|
69
|
|
|
*/ |
|
70
|
|
|
public $view_id = NULL; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var array Fields for the form |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $fields = array(); |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string Current screen. Defaults to "directory" or "single" |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $context = 'directory'; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var int|null If in embedded post or page, the ID of it |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $post_id = NULL; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var array Gravity Forms form array at ID $form_id |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $form = NULL; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @var array Configuration for the View |
|
94
|
|
|
*/ |
|
95
|
|
|
protected $atts = array(); |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @var array Entries for the current result. Single item in array for single entry View |
|
99
|
|
|
*/ |
|
100
|
|
|
protected $entries = array(); |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @var int Total entries count for the current result. |
|
104
|
|
|
*/ |
|
105
|
|
|
protected $total_entries = 0; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var string The label to display back links |
|
109
|
|
|
*/ |
|
110
|
|
|
protected $back_link_label = ''; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var array Array with `offset` and `page_size` keys |
|
114
|
|
|
*/ |
|
115
|
|
|
protected $paging = array(); |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @var array Array with `sort_field` and `sort_direction` keys |
|
119
|
|
|
*/ |
|
120
|
|
|
protected $sorting = array(); |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @var bool Whether to hide the results until a search is performed |
|
124
|
|
|
* @since 1.5.4 |
|
125
|
|
|
*/ |
|
126
|
|
|
protected $hide_until_searched = false; |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Current entry in the loop |
|
130
|
|
|
* @var array |
|
131
|
|
|
*/ |
|
132
|
|
|
protected $_current_entry = array(); |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @var array |
|
136
|
|
|
*/ |
|
137
|
|
|
protected $_current_field = array(); |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @var GravityView_View |
|
141
|
|
|
*/ |
|
142
|
|
|
static $instance = NULL; |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Construct the view object |
|
146
|
|
|
* @param array $atts Associative array to set the data of |
|
147
|
|
|
*/ |
|
148
|
|
|
function __construct( $atts = array() ) { |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
$atts = wp_parse_args( $atts, array( |
|
151
|
|
|
'form_id' => NULL, |
|
152
|
|
|
'view_id' => NULL, |
|
153
|
|
|
'fields' => NULL, |
|
154
|
|
|
'context' => NULL, |
|
155
|
|
|
'post_id' => NULL, |
|
156
|
|
|
'form' => NULL, |
|
157
|
|
|
'atts' => NULL, |
|
158
|
|
|
) ); |
|
159
|
|
|
|
|
160
|
|
|
foreach ($atts as $key => $value) { |
|
161
|
|
|
if( is_null( $value ) ) { |
|
162
|
|
|
continue; |
|
163
|
|
|
} |
|
164
|
|
|
$this->{$key} = $value; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
// Add granular overrides |
|
169
|
|
|
add_filter( $this->filter_prefix . '_get_template_part', array( $this, 'add_id_specific_templates' ), 10, 3 ); |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
// widget logic |
|
173
|
|
|
add_action( 'gravityview_before', array( $this, 'render_widget_hooks' ) ); |
|
174
|
|
|
add_action( 'gravityview_after', array( $this, 'render_widget_hooks' ) ); |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Clear the current entry after the loop is done |
|
178
|
|
|
* @since 1.7.3 |
|
179
|
|
|
*/ |
|
180
|
|
|
add_action( 'gravityview_footer', array( $this, 'clearCurrentEntry' ), 500 ); |
|
181
|
|
|
|
|
182
|
|
|
self::$instance = &$this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param null $passed_post |
|
187
|
|
|
* |
|
188
|
|
|
* @return GravityView_View |
|
189
|
|
|
*/ |
|
190
|
|
|
static function getInstance( $passed_post = NULL ) { |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
if( empty( self::$instance ) ) { |
|
193
|
|
|
self::$instance = new self( $passed_post ); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return self::$instance; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param string|null $key The key to a specific attribute of the current field |
|
201
|
|
|
* @return array|mixed|null If $key is set and attribute exists at $key, return that. If not set, return NULL. Otherwise, return current field array |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getCurrentField( $key = NULL ) { |
|
204
|
|
|
|
|
205
|
|
|
if( !empty( $key ) ) { |
|
206
|
|
|
if( isset( $this->_current_field[ $key ] ) ) { |
|
207
|
|
|
return $this->_current_field[ $key ]; |
|
208
|
|
|
} |
|
209
|
|
|
return NULL; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $this->_current_field; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function setCurrentFieldSetting( $key, $value ) { |
|
216
|
|
|
|
|
217
|
|
|
if( !empty( $this->_current_field ) ) { |
|
218
|
|
|
$this->_current_field['field_settings'][ $key ] = $value; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function getCurrentFieldSetting( $key ) { |
|
224
|
|
|
$settings = $this->getCurrentField('field_settings'); |
|
225
|
|
|
|
|
226
|
|
|
if( $settings && !empty( $settings[ $key ] ) ) { |
|
227
|
|
|
return $settings[ $key ]; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return NULL; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param array $passed_field |
|
235
|
|
|
*/ |
|
236
|
|
|
public function setCurrentField( $passed_field ) { |
|
237
|
|
|
|
|
238
|
|
|
$existing_field = $this->getCurrentField(); |
|
239
|
|
|
|
|
240
|
|
|
$set_field = wp_parse_args( $passed_field, $existing_field ); |
|
241
|
|
|
|
|
242
|
|
|
$this->_current_field = $set_field; |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Backward compatibility |
|
246
|
|
|
* @deprecated 1.6.2 |
|
247
|
|
|
*/ |
|
248
|
|
|
$this->field_data = $set_field; |
|
|
|
|
|
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param string|null $key The key to a specific field in the fields array |
|
253
|
|
|
* @return array|mixed|null If $key is set and field exists at $key, return that. If not set, return NULL. Otherwise, return array of fields. |
|
254
|
|
|
*/ |
|
255
|
|
View Code Duplication |
public function getAtts( $key = NULL ) { |
|
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
if( !empty( $key ) ) { |
|
258
|
|
|
if( isset( $this->atts[ $key ] ) ) { |
|
259
|
|
|
return $this->atts[ $key ]; |
|
260
|
|
|
} |
|
261
|
|
|
return NULL; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
return $this->atts; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @param array $atts |
|
269
|
|
|
*/ |
|
270
|
|
|
public function setAtts( $atts ) { |
|
271
|
|
|
$this->atts = $atts; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @return array |
|
276
|
|
|
*/ |
|
277
|
|
|
public function getForm() { |
|
278
|
|
|
return $this->form; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param array $form |
|
283
|
|
|
*/ |
|
284
|
|
|
public function setForm( $form ) { |
|
285
|
|
|
$this->form = $form; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @return int|null |
|
290
|
|
|
*/ |
|
291
|
|
|
public function getPostId() { |
|
292
|
|
|
return $this->post_id; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @param int|null $post_id |
|
297
|
|
|
*/ |
|
298
|
|
|
public function setPostId( $post_id ) { |
|
299
|
|
|
$this->post_id = $post_id; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @return string |
|
304
|
|
|
*/ |
|
305
|
|
|
public function getContext() { |
|
306
|
|
|
return $this->context; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* @param string $context |
|
311
|
|
|
*/ |
|
312
|
|
|
public function setContext( $context ) { |
|
313
|
|
|
$this->context = $context; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* @param string|null $key The key to a specific field in the fields array |
|
318
|
|
|
* @return array|mixed|null If $key is set and field exists at $key, return that. If not set, return NULL. Otherwise, return array of fields. |
|
319
|
|
|
*/ |
|
320
|
|
|
public function getFields( $key = null ) { |
|
321
|
|
|
|
|
322
|
|
|
$fields = empty( $this->fields ) ? NULL : $this->fields; |
|
323
|
|
|
|
|
324
|
|
|
if( $fields && !empty( $key ) ) { |
|
|
|
|
|
|
325
|
|
|
$fields = isset( $fields[ $key ] ) ? $fields[ $key ] : NULL; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
return $fields; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* @param array $fields |
|
333
|
|
|
*/ |
|
334
|
|
|
public function setFields( $fields ) { |
|
335
|
|
|
$this->fields = $fields; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* @param string $key The key to a specific field in the fields array |
|
340
|
|
|
* @return array|mixed|null If $key is set and field exists at $key, return that. If not set, return NULL. Otherwise, return array of fields. |
|
341
|
|
|
*/ |
|
342
|
|
View Code Duplication |
public function getField( $key ) { |
|
|
|
|
|
|
343
|
|
|
|
|
344
|
|
|
if( !empty( $key ) ) { |
|
345
|
|
|
if( isset( $this->fields[ $key ] ) ) { |
|
346
|
|
|
return $this->fields[ $key ]; |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
return NULL; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @param string $key The key to a specific field in the fields array |
|
355
|
|
|
* @param mixed $value The value to set for the field |
|
356
|
|
|
*/ |
|
357
|
|
|
public function setField( $key, $value ) { |
|
358
|
|
|
$this->fields[ $key ] = $value; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* @return int |
|
363
|
|
|
*/ |
|
364
|
|
|
public function getViewId() { |
|
365
|
|
|
return absint( $this->view_id ); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* @param int $view_id |
|
370
|
|
|
*/ |
|
371
|
|
|
public function setViewId( $view_id ) { |
|
372
|
|
|
$this->view_id = intval( $view_id ); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* @return int |
|
377
|
|
|
*/ |
|
378
|
|
|
public function getFormId() { |
|
379
|
|
|
return $this->form_id; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* @param int $form_id |
|
384
|
|
|
*/ |
|
385
|
|
|
public function setFormId( $form_id ) { |
|
386
|
|
|
$this->form_id = $form_id; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* @return array |
|
391
|
|
|
*/ |
|
392
|
|
|
public function getEntries() { |
|
393
|
|
|
return $this->entries; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* @param array $entries |
|
398
|
|
|
*/ |
|
399
|
|
|
public function setEntries( $entries ) { |
|
400
|
|
|
$this->entries = $entries; |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* @return int |
|
405
|
|
|
*/ |
|
406
|
|
|
public function getTotalEntries() { |
|
407
|
|
|
return (int)$this->total_entries; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* @param int $total_entries |
|
412
|
|
|
*/ |
|
413
|
|
|
public function setTotalEntries( $total_entries ) { |
|
414
|
|
|
$this->total_entries = intval( $total_entries ); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* @return array |
|
419
|
|
|
*/ |
|
420
|
|
|
public function getPaging() { |
|
421
|
|
|
return $this->paging; |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
/** |
|
425
|
|
|
* @param array $paging |
|
426
|
|
|
*/ |
|
427
|
|
|
public function setPaging( $paging ) { |
|
428
|
|
|
$this->paging = $paging; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* Get an array with pagination information |
|
433
|
|
|
* |
|
434
|
|
|
* @since 1.13 |
|
435
|
|
|
* |
|
436
|
|
|
* @return array { |
|
437
|
|
|
* @type int $first The starting entry number (counter, not ID) |
|
438
|
|
|
* @type int $last The last displayed entry number (counter, not ID) |
|
439
|
|
|
* @type int $total The total number of entries |
|
440
|
|
|
* } |
|
441
|
|
|
*/ |
|
442
|
|
|
public function getPaginationCounts() { |
|
443
|
|
|
|
|
444
|
|
|
$paging = $this->getPaging(); |
|
445
|
|
|
$offset = $paging['offset']; |
|
446
|
|
|
$page_size = $paging['page_size']; |
|
447
|
|
|
$total = $this->getTotalEntries(); |
|
448
|
|
|
|
|
449
|
|
|
if ( empty( $total ) ) { |
|
450
|
|
|
do_action( 'gravityview_log_debug', __METHOD__ . ': No entries. Returning empty array.' ); |
|
451
|
|
|
|
|
452
|
|
|
return array(); |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
$first = empty( $offset ) ? 1 : $offset + 1; |
|
456
|
|
|
|
|
457
|
|
|
// If the page size + starting entry is larger than total, the total is the max. |
|
458
|
|
|
$last = ( $offset + $page_size > $total ) ? $total : $offset + $page_size; |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* @filter `gravityview_pagination_counts` Modify the displayed pagination numbers |
|
462
|
|
|
* @since 1.13 |
|
463
|
|
|
* @param array $counts Array with $first, $last, $total numbers in that order |
|
464
|
|
|
*/ |
|
465
|
|
|
list( $first, $last, $total ) = apply_filters( 'gravityview_pagination_counts', array( $first, $last, $total ) ); |
|
466
|
|
|
|
|
467
|
|
|
return array( 'first' => (int) $first, 'last' => (int) $last, 'total' => (int) $total ); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
/** |
|
471
|
|
|
* @return array |
|
472
|
|
|
*/ |
|
473
|
|
|
public function getSorting() { |
|
474
|
|
|
return $this->sorting; |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
/** |
|
478
|
|
|
* @param array $sorting |
|
479
|
|
|
*/ |
|
480
|
|
|
public function setSorting( $sorting ) { |
|
481
|
|
|
$this->sorting = $sorting; |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
/** |
|
485
|
|
|
* @return string |
|
486
|
|
|
*/ |
|
487
|
|
|
public function getBackLinkLabel() { |
|
488
|
|
|
|
|
489
|
|
|
$back_link_label = GravityView_API::replace_variables( $this->back_link_label, $this->getForm(), $this->getCurrentEntry() ); |
|
490
|
|
|
|
|
491
|
|
|
$back_link_label = do_shortcode( $back_link_label ); |
|
492
|
|
|
|
|
493
|
|
|
return $back_link_label; |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
/** |
|
497
|
|
|
* @param string $back_link_label |
|
498
|
|
|
*/ |
|
499
|
|
|
public function setBackLinkLabel( $back_link_label ) { |
|
500
|
|
|
$this->back_link_label = $back_link_label; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
/** |
|
504
|
|
|
* @return boolean |
|
505
|
|
|
*/ |
|
506
|
|
|
public function isHideUntilSearched() { |
|
507
|
|
|
return $this->hide_until_searched; |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
/** |
|
511
|
|
|
* @param boolean $hide_until_searched |
|
512
|
|
|
*/ |
|
513
|
|
|
public function setHideUntilSearched( $hide_until_searched ) { |
|
514
|
|
|
$this->hide_until_searched = $hide_until_searched; |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
/** |
|
518
|
|
|
* @return string |
|
519
|
|
|
*/ |
|
520
|
|
|
public function getTemplatePartSlug() { |
|
521
|
|
|
return $this->template_part_slug; |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
/** |
|
525
|
|
|
* @param string $template_part_slug |
|
526
|
|
|
*/ |
|
527
|
|
|
public function setTemplatePartSlug( $template_part_slug ) { |
|
528
|
|
|
$this->template_part_slug = $template_part_slug; |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
/** |
|
532
|
|
|
* @return string |
|
533
|
|
|
*/ |
|
534
|
|
|
public function getTemplatePartName() { |
|
535
|
|
|
return $this->template_part_name; |
|
536
|
|
|
} |
|
537
|
|
|
|
|
538
|
|
|
/** |
|
539
|
|
|
* @param string $template_part_name |
|
540
|
|
|
*/ |
|
541
|
|
|
public function setTemplatePartName( $template_part_name ) { |
|
542
|
|
|
$this->template_part_name = $template_part_name; |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
/** |
|
546
|
|
|
* Return the current entry. If in the loop, the current entry. If single entry, the currently viewed entry. |
|
547
|
|
|
* @return array |
|
548
|
|
|
*/ |
|
549
|
|
|
public function getCurrentEntry() { |
|
550
|
|
|
|
|
551
|
|
|
if( in_array( $this->getContext(), array( 'edit', 'single') ) ) { |
|
552
|
|
|
$entries = $this->getEntries(); |
|
553
|
|
|
$entry = $entries[0]; |
|
554
|
|
|
} else { |
|
555
|
|
|
$entry = $this->_current_entry; |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
/** @since 1.16 Fixes DataTables empty entry issue */ |
|
559
|
|
|
if ( empty( $entry ) && ! empty( $this->_current_field['entry'] ) ) { |
|
560
|
|
|
$entry = $this->_current_field['entry']; |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
return $entry; |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
/** |
|
567
|
|
|
* @param array $current_entry |
|
568
|
|
|
* @return void |
|
569
|
|
|
*/ |
|
570
|
|
|
public function setCurrentEntry( $current_entry ) { |
|
571
|
|
|
$this->_current_entry = $current_entry; |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
/** |
|
575
|
|
|
* Clear the current entry after all entries in the loop have been displayed. |
|
576
|
|
|
* |
|
577
|
|
|
* @since 1.7.3 |
|
578
|
|
|
* @return void |
|
579
|
|
|
*/ |
|
580
|
|
|
public function clearCurrentEntry() { |
|
581
|
|
|
$this->_current_entry = NULL; |
|
|
|
|
|
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
/** |
|
585
|
|
|
* Render an output zone, as configured in the Admin |
|
586
|
|
|
* |
|
587
|
|
|
* @param string $zone The zone name, like 'footer-left' |
|
588
|
|
|
* @param array $atts |
|
589
|
|
|
* |
|
590
|
|
|
* @return string|null |
|
591
|
|
|
*/ |
|
592
|
|
|
public function renderZone( $zone = '', $atts = array() ) { |
|
593
|
|
|
|
|
594
|
|
|
if( empty( $zone ) ) { |
|
595
|
|
|
do_action('gravityview_log_error', 'GravityView_View[renderZone] No zone defined.'); |
|
596
|
|
|
return NULL; |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
$defaults = array( |
|
600
|
|
|
'slug' => $this->getTemplatePartSlug(), |
|
601
|
|
|
'context' => $this->getContext(), |
|
602
|
|
|
'entry' => $this->getCurrentEntry(), |
|
603
|
|
|
'form' => $this->getForm(), |
|
604
|
|
|
'hide_empty' => $this->getAtts('hide_empty'), |
|
605
|
|
|
); |
|
606
|
|
|
|
|
607
|
|
|
$final_atts = wp_parse_args( $atts, $defaults ); |
|
608
|
|
|
|
|
609
|
|
|
$output = ''; |
|
610
|
|
|
|
|
611
|
|
|
$final_atts['zone_id'] = "{$final_atts['context']}_{$final_atts['slug']}-{$zone}"; |
|
612
|
|
|
|
|
613
|
|
|
$fields = $this->getField( $final_atts['zone_id'] ); |
|
614
|
|
|
|
|
615
|
|
|
// Backward compatibility |
|
616
|
|
|
if( 'table' === $this->getTemplatePartSlug() ) { |
|
617
|
|
|
/** |
|
618
|
|
|
* Modify the fields displayed in the table |
|
619
|
|
|
* @var array |
|
620
|
|
|
*/ |
|
621
|
|
|
$fields = apply_filters("gravityview_table_cells", $fields, $this ); |
|
622
|
|
|
} |
|
623
|
|
|
|
|
624
|
|
|
if( empty( $fields ) ) { |
|
625
|
|
|
return NULL; |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
|
|
$field_output = ''; |
|
629
|
|
|
foreach ( $fields as $field ) { |
|
630
|
|
|
$final_atts['field'] = $field; |
|
631
|
|
|
|
|
632
|
|
|
$field_output .= gravityview_field_output( $final_atts ); |
|
633
|
|
|
} |
|
634
|
|
|
|
|
635
|
|
|
/** |
|
636
|
|
|
* If a zone has no field output, choose whether to show wrapper |
|
637
|
|
|
* False by default to keep backward compatibility |
|
638
|
|
|
* @since 1.7.6 |
|
639
|
|
|
* @param boolean $hide_empty_zone Default: false |
|
640
|
|
|
*/ |
|
641
|
|
|
if( empty( $field_output ) && apply_filters( 'gravityview/render/hide-empty-zone', false ) ) { |
|
642
|
|
|
return NULL; |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
if( !empty( $final_atts['wrapper_class'] ) ) { |
|
646
|
|
|
$output .= '<div class="'.gravityview_sanitize_html_class( $final_atts['wrapper_class'] ).'">'; |
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
$output .= $field_output; |
|
650
|
|
|
|
|
651
|
|
|
if( !empty( $final_atts['wrapper_class'] ) ) { |
|
652
|
|
|
$output .= '</div>'; |
|
653
|
|
|
} |
|
654
|
|
|
|
|
655
|
|
|
echo $output; |
|
656
|
|
|
|
|
657
|
|
|
return $output; |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* In order to improve lookup times, we store located templates in a local array. |
|
662
|
|
|
* |
|
663
|
|
|
* This improves performance by up to 1/2 second on a 250 entry View with 7 columns showing |
|
664
|
|
|
* |
|
665
|
|
|
* @inheritdoc |
|
666
|
|
|
* @see Gamajo_Template_Loader::locate_template() |
|
667
|
|
|
* @return null|string NULL: Template not found; String: path to template |
|
668
|
|
|
*/ |
|
669
|
|
|
function locate_template( $template_names, $load = false, $require_once = true ) { |
|
|
|
|
|
|
670
|
|
|
|
|
671
|
|
|
if( is_string( $template_names ) && isset( $this->located_templates[ $template_names ] ) ) { |
|
672
|
|
|
|
|
673
|
|
|
$located = $this->located_templates[ $template_names ]; |
|
674
|
|
|
|
|
675
|
|
|
} else { |
|
676
|
|
|
|
|
677
|
|
|
// Set $load to always false so we handle it here. |
|
678
|
|
|
$located = parent::locate_template( $template_names, false, $require_once ); |
|
679
|
|
|
|
|
680
|
|
|
if( is_string( $template_names ) ) { |
|
681
|
|
|
$this->located_templates[ $template_names ] = $located; |
|
682
|
|
|
} |
|
683
|
|
|
} |
|
684
|
|
|
|
|
685
|
|
|
if ( $load && $located ) { |
|
686
|
|
|
load_template( $located, $require_once ); |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
return $located; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
/** |
|
693
|
|
|
* Magic Method: Instead of throwing an error when a variable isn't set, return null. |
|
694
|
|
|
* @param string $name Key for the data retrieval. |
|
695
|
|
|
* @return mixed|null The stored data. |
|
696
|
|
|
*/ |
|
697
|
|
|
public function __get( $name ) { |
|
698
|
|
|
if( isset( $this->{$name} ) ) { |
|
699
|
|
|
return $this->{$name}; |
|
700
|
|
|
} else { |
|
701
|
|
|
return NULL; |
|
702
|
|
|
} |
|
703
|
|
|
} |
|
704
|
|
|
|
|
705
|
|
|
/** |
|
706
|
|
|
* Enable overrides of GravityView templates on a granular basis |
|
707
|
|
|
* |
|
708
|
|
|
* The loading order is: |
|
709
|
|
|
* |
|
710
|
|
|
* - view-[View ID]-table-footer.php |
|
711
|
|
|
* - form-[Form ID]-table-footer.php |
|
712
|
|
|
* - page-[ID of post or page where view is embedded]-table-footer.php |
|
713
|
|
|
* - table-footer.php |
|
714
|
|
|
* |
|
715
|
|
|
* @see Gamajo_Template_Loader::get_template_file_names() Where the filter is |
|
716
|
|
|
* @param array $templates Existing list of templates. |
|
717
|
|
|
* @param string $slug Name of the template base, example: `table`, `list`, `datatables`, `map` |
|
718
|
|
|
* @param string $name Name of the template part, example: `body`, `footer`, `head`, `single` |
|
719
|
|
|
* |
|
720
|
|
|
* @return array $templates Modified template array, merged with existing $templates values |
|
721
|
|
|
*/ |
|
722
|
|
|
function add_id_specific_templates( $templates, $slug, $name ) { |
|
|
|
|
|
|
723
|
|
|
|
|
724
|
|
|
$additional = array(); |
|
725
|
|
|
|
|
726
|
|
|
// form-19-table-body.php |
|
727
|
|
|
$additional[] = sprintf( 'form-%d-%s-%s.php', $this->getFormId(), $slug, $name ); |
|
728
|
|
|
|
|
729
|
|
|
// view-3-table-body.php |
|
730
|
|
|
$additional[] = sprintf( 'view-%d-%s-%s.php', $this->getViewId(), $slug, $name ); |
|
731
|
|
|
|
|
732
|
|
|
if( $this->getPostId() ) { |
|
|
|
|
|
|
733
|
|
|
|
|
734
|
|
|
// page-19-table-body.php |
|
735
|
|
|
$additional[] = sprintf( 'page-%d-%s-%s.php', $this->getPostId(), $slug, $name ); |
|
736
|
|
|
} |
|
737
|
|
|
|
|
738
|
|
|
// Combine with existing table-body.php and table.php |
|
739
|
|
|
$templates = array_merge( $additional, $templates ); |
|
740
|
|
|
|
|
741
|
|
|
do_action( 'gravityview_log_debug', '[add_id_specific_templates] List of Template Files', $templates ); |
|
742
|
|
|
|
|
743
|
|
|
return $templates; |
|
744
|
|
|
} |
|
745
|
|
|
|
|
746
|
|
|
// Load the template |
|
747
|
|
|
public function render( $slug, $name, $require_once = true ) { |
|
748
|
|
|
|
|
749
|
|
|
$this->setTemplatePartSlug( $slug ); |
|
750
|
|
|
|
|
751
|
|
|
$this->setTemplatePartName( $name ); |
|
752
|
|
|
|
|
753
|
|
|
$template_file = $this->get_template_part( $slug, $name, false ); |
|
754
|
|
|
|
|
755
|
|
|
do_action( 'gravityview_log_debug', '[render] Rendering Template File', $template_file ); |
|
756
|
|
|
|
|
757
|
|
|
if( !empty( $template_file) ) { |
|
758
|
|
|
|
|
759
|
|
|
if ( $require_once ) { |
|
760
|
|
|
require_once( $template_file ); |
|
761
|
|
|
} else { |
|
762
|
|
|
require( $template_file ); |
|
763
|
|
|
} |
|
764
|
|
|
|
|
765
|
|
|
} |
|
766
|
|
|
} |
|
767
|
|
|
|
|
768
|
|
|
/** |
|
769
|
|
|
* |
|
770
|
|
|
* @param $view_id |
|
771
|
|
|
*/ |
|
772
|
|
|
public function render_widget_hooks( $view_id ) { |
|
773
|
|
|
|
|
774
|
|
|
if( empty( $view_id ) || 'single' == gravityview_get_context() ) { |
|
775
|
|
|
do_action( 'gravityview_log_debug', __METHOD__ . ' - Not rendering widgets; single entry' ); |
|
776
|
|
|
return; |
|
777
|
|
|
} |
|
778
|
|
|
|
|
779
|
|
|
$view_data = gravityview_get_current_view_data( $view_id ); |
|
780
|
|
|
|
|
781
|
|
|
// get View widget configuration |
|
782
|
|
|
$widgets = (array)$view_data['widgets']; |
|
783
|
|
|
|
|
784
|
|
|
switch( current_filter() ) { |
|
785
|
|
|
default: |
|
786
|
|
|
case 'gravityview_before': |
|
787
|
|
|
$zone = 'header'; |
|
788
|
|
|
break; |
|
789
|
|
|
case 'gravityview_after': |
|
790
|
|
|
$zone = 'footer'; |
|
791
|
|
|
break; |
|
792
|
|
|
} |
|
793
|
|
|
|
|
794
|
|
|
/** |
|
795
|
|
|
* Filter widgets not in the current zone |
|
796
|
|
|
* @since 1.16 |
|
797
|
|
|
*/ |
|
798
|
|
|
foreach( $widgets as $key => $widget ) { |
|
799
|
|
|
// The widget isn't in the current zone |
|
800
|
|
|
if( false === strpos( $key, $zone ) ) { |
|
801
|
|
|
unset( $widgets[ $key ] ); |
|
802
|
|
|
} |
|
803
|
|
|
} |
|
804
|
|
|
|
|
805
|
|
|
/** |
|
806
|
|
|
* Prevent output if no widgets to show. |
|
807
|
|
|
* @since 1.16 |
|
808
|
|
|
*/ |
|
809
|
|
|
if ( empty( $widgets ) ) { |
|
810
|
|
|
do_action( 'gravityview_log_debug', sprintf( 'No widgets for View #%s', $view_id ) ); |
|
811
|
|
|
return; |
|
812
|
|
|
} |
|
813
|
|
|
|
|
814
|
|
|
// Prevent being called twice |
|
815
|
|
|
if( did_action( $zone.'_'.$view_id.'_widgets' ) ) { |
|
816
|
|
|
do_action( 'gravityview_log_debug', sprintf( '%s - Not rendering %s; already rendered', __METHOD__ , $zone.'_'.$view_id.'_widgets' ) ); |
|
817
|
|
|
return; |
|
818
|
|
|
} |
|
819
|
|
|
|
|
820
|
|
|
$rows = GravityView_Plugin::get_default_widget_areas(); |
|
821
|
|
|
|
|
822
|
|
|
// TODO: Move to sep. method, use an action instead |
|
823
|
|
|
wp_enqueue_style( 'gravityview_default_style' ); |
|
824
|
|
|
|
|
825
|
|
|
// TODO Convert to partials |
|
826
|
|
|
?> |
|
827
|
|
|
<div class="gv-grid"> |
|
828
|
|
|
<?php |
|
829
|
|
|
foreach( $rows as $row ) { |
|
830
|
|
|
foreach( $row as $col => $areas ) { |
|
831
|
|
|
$column = ($col == '2-2') ? '1-2 gv-right' : $col.' gv-left'; |
|
832
|
|
|
?> |
|
833
|
|
|
<div class="gv-grid-col-<?php echo esc_attr( $column ); ?>"> |
|
834
|
|
|
<?php |
|
835
|
|
|
if( !empty( $areas ) ) { |
|
836
|
|
|
foreach( $areas as $area ) { |
|
837
|
|
|
if( !empty( $widgets[ $zone .'_'. $area['areaid'] ] ) ) { |
|
838
|
|
|
foreach( $widgets[ $zone .'_'. $area['areaid'] ] as $widget ) { |
|
839
|
|
|
do_action( "gravityview_render_widget_{$widget['id']}", $widget ); |
|
840
|
|
|
} |
|
841
|
|
|
} |
|
842
|
|
|
} |
|
843
|
|
|
} ?> |
|
844
|
|
|
</div> |
|
845
|
|
|
<?php } // $row ?> |
|
846
|
|
|
<?php } // $rows ?> |
|
847
|
|
|
</div> |
|
848
|
|
|
|
|
849
|
|
|
<?php |
|
850
|
|
|
|
|
851
|
|
|
/** |
|
852
|
|
|
* Prevent widgets from being called twice. |
|
853
|
|
|
* Checking for loop_start prevents themes and plugins that pre-process shortcodes from triggering the action before displaying. Like, ahem, the Divi theme and WordPress SEO plugin |
|
854
|
|
|
*/ |
|
855
|
|
|
if( did_action( 'loop_start' ) ) { |
|
856
|
|
|
do_action( $zone.'_'.$view_id.'_widgets' ); |
|
857
|
|
|
} |
|
858
|
|
|
} |
|
859
|
|
|
|
|
860
|
|
|
} |
|
861
|
|
|
|
|
862
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.