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 default GravityView View class. |
11
|
|
|
* |
12
|
|
|
* Houses all base View functionality. |
13
|
|
|
* |
14
|
|
|
* Can be accessed as an array for old compatibility's sake |
15
|
|
|
* in line with the elements inside the \GravityView_View_Data::$views array. |
16
|
|
|
*/ |
17
|
|
|
class View implements \ArrayAccess { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \WP_Post The backing post instance. |
21
|
|
|
*/ |
22
|
|
|
private $post; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \GV\View_Settings The settings. |
26
|
|
|
* |
27
|
|
|
* @api |
28
|
|
|
* @since future |
29
|
|
|
*/ |
30
|
|
|
public $settings; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \GV\Form The backing form for this view. |
34
|
|
|
* |
35
|
|
|
* Contains the form that is sourced for entries in this view. |
36
|
|
|
* |
37
|
|
|
* @api |
38
|
|
|
* @since future |
39
|
|
|
*/ |
40
|
|
|
public $form; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \GV\Field_Collection The fields for this view. |
44
|
|
|
* |
45
|
|
|
* Contains all the fields that are attached to this view. |
46
|
|
|
* |
47
|
|
|
* @api |
48
|
|
|
* @since future |
49
|
|
|
*/ |
50
|
|
|
public $fields; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \GV\View_Template The template attached to this view. |
54
|
|
|
*/ |
55
|
|
|
public $template; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The constructor. |
59
|
|
|
*/ |
60
|
3 |
|
public function __construct() { |
61
|
3 |
|
$this->settings = new View_Settings(); |
62
|
3 |
|
$this->fields = new Field_Collection(); |
63
|
3 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Register the gravityview WordPress Custom Post Type. |
67
|
|
|
* |
68
|
|
|
* @internal |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public static function register_post_type() { |
72
|
|
|
|
73
|
|
|
/** Register only once */ |
74
|
|
|
if ( post_type_exists( 'gravityview' ) ) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @filter `gravityview_is_hierarchical` Make GravityView Views hierarchical by returning TRUE |
80
|
|
|
* This will allow for Views to be nested with Parents and also allows for menu order to be set in the Page Attributes metabox |
81
|
|
|
* @since 1.13 |
82
|
|
|
* @param boolean $is_hierarchical Default: false |
83
|
|
|
*/ |
84
|
|
|
$is_hierarchical = (bool)apply_filters( 'gravityview_is_hierarchical', false ); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$supports = array( 'title', 'revisions' ); |
87
|
|
|
|
88
|
|
|
if ( $is_hierarchical ) { |
89
|
|
|
$supports[] = 'page-attributes'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @filter `gravityview_post_type_supports` Modify post type support values for `gravityview` post type |
94
|
|
|
* @see add_post_type_support() |
95
|
|
|
* @since 1.15.2 |
96
|
|
|
* @param array $supports Array of features associated with a functional area of the edit screen. Default: 'title', 'revisions'. If $is_hierarchical, also 'page-attributes' |
97
|
|
|
* @param[in] boolean $is_hierarchical Do Views support parent/child relationships? See `gravityview_is_hierarchical` filter. |
98
|
|
|
*/ |
99
|
|
|
$supports = apply_filters( 'gravityview_post_type_support', $supports, $is_hierarchical ); |
100
|
|
|
|
101
|
|
|
/** Register Custom Post Type - gravityview */ |
102
|
|
|
$labels = array( |
103
|
|
|
'name' => _x( 'Views', 'Post Type General Name', 'gravityview' ), |
104
|
|
|
'singular_name' => _x( 'View', 'Post Type Singular Name', 'gravityview' ), |
105
|
|
|
'menu_name' => _x( 'Views', 'Menu name', 'gravityview' ), |
106
|
|
|
'parent_item_colon' => __( 'Parent View:', 'gravityview' ), |
107
|
|
|
'all_items' => __( 'All Views', 'gravityview' ), |
108
|
|
|
'view_item' => _x( 'View', 'View Item', 'gravityview' ), |
109
|
|
|
'add_new_item' => __( 'Add New View', 'gravityview' ), |
110
|
|
|
'add_new' => __( 'New View', 'gravityview' ), |
111
|
|
|
'edit_item' => __( 'Edit View', 'gravityview' ), |
112
|
|
|
'update_item' => __( 'Update View', 'gravityview' ), |
113
|
|
|
'search_items' => __( 'Search Views', 'gravityview' ), |
114
|
|
|
'not_found' => \GravityView_Admin::no_views_text(), |
115
|
|
|
'not_found_in_trash' => __( 'No Views found in Trash', 'gravityview' ), |
116
|
|
|
'filter_items_list' => __( 'Filter Views list', 'gravityview' ), |
117
|
|
|
'items_list_navigation' => __( 'Views list navigation', 'gravityview' ), |
118
|
|
|
'items_list' => __( 'Views list', 'gravityview' ), |
119
|
|
|
'view_items' => __( 'See Views', 'gravityview' ), |
120
|
|
|
'attributes' => __( 'View Attributes', 'gravityview' ), |
121
|
|
|
); |
122
|
|
|
$args = array( |
123
|
|
|
'label' => __( 'view', 'gravityview' ), |
124
|
|
|
'description' => __( 'Create views based on a Gravity Forms form', 'gravityview' ), |
125
|
|
|
'labels' => $labels, |
126
|
|
|
'supports' => $supports, |
127
|
|
|
'hierarchical' => $is_hierarchical, |
128
|
|
|
/** |
129
|
|
|
* @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode? |
130
|
|
|
* @see https://codex.wordpress.org/Function_Reference/register_post_type#public |
131
|
|
|
* @since 1.15.2 |
132
|
|
|
* @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded via shortcode. Default: `true` |
133
|
|
|
* @param int $view_id The ID of the View currently being requested. `0` for general setting |
134
|
|
|
*/ |
135
|
|
|
'public' => apply_filters( 'gravityview_direct_access', gravityview()->plugin->is_compatible(), 0 ), |
136
|
|
|
'show_ui' => gravityview()->plugin->is_compatible(), |
137
|
|
|
'show_in_menu' => gravityview()->plugin->is_compatible(), |
138
|
|
|
'show_in_nav_menus' => true, |
139
|
|
|
'show_in_admin_bar' => true, |
140
|
|
|
'menu_position' => 17, |
141
|
|
|
'menu_icon' => '', |
142
|
|
|
'can_export' => true, |
143
|
|
|
/** |
144
|
|
|
* @filter `gravityview_has_archive` Enable Custom Post Type archive? |
145
|
|
|
* @since 1.7.3 |
146
|
|
|
* @param boolean False: don't have frontend archive; True: yes, have archive. Default: false |
147
|
|
|
*/ |
148
|
|
|
'has_archive' => apply_filters( 'gravityview_has_archive', false ), |
149
|
|
|
'exclude_from_search' => true, |
150
|
|
|
'rewrite' => array( |
151
|
|
|
/** |
152
|
|
|
* @filter `gravityview_slug` Modify the url part for a View. |
153
|
|
|
* @see http://docs.gravityview.co/article/62-changing-the-view-slug |
154
|
|
|
* @param string $slug The slug shown in the URL |
155
|
|
|
*/ |
156
|
|
|
'slug' => apply_filters( 'gravityview_slug', 'view' ), |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @filter `gravityview/post_type/with_front` Should the permalink structure |
160
|
|
|
* be prepended with the front base. |
161
|
|
|
* (example: if your permalink structure is /blog/, then your links will be: false->/view/, true->/blog/view/). |
162
|
|
|
* Defaults to true. |
163
|
|
|
* @see https://codex.wordpress.org/Function_Reference/register_post_type |
164
|
|
|
* @since future |
165
|
|
|
* @param bool $with_front |
166
|
|
|
*/ |
167
|
|
|
'with_front' => apply_filters( 'gravityview/post_type/with_front', true ), |
168
|
|
|
), |
169
|
|
|
'capability_type' => 'gravityview', |
170
|
|
|
'map_meta_cap' => true, |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
register_post_type( 'gravityview', $args ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Construct a \GV\View instance from a \WP_Post. |
179
|
|
|
* |
180
|
|
|
* @param $post The \WP_Post instance to wrap. |
181
|
|
|
* |
182
|
|
|
* @api |
183
|
|
|
* @since future |
184
|
|
|
* @return \GV\View|null An instance around this \WP_Post if valid, null otherwise. |
185
|
|
|
*/ |
186
|
4 |
|
public static function from_post( $post ) { |
187
|
4 |
|
if ( ! $post || get_post_type( $post ) != 'gravityview' ) { |
|
|
|
|
188
|
1 |
|
gravityview()->log->error( 'Only gravityview post types can be \GV\View instances.' ); |
189
|
1 |
|
return null; |
190
|
|
|
} |
191
|
|
|
|
192
|
4 |
|
$view = new self(); |
193
|
4 |
|
$view->post = $post; |
194
|
|
|
|
195
|
|
|
/** Get connected form. */ |
196
|
4 |
|
$view->form = GF_Form::by_id( $view->_gravityview_form_id ); |
|
|
|
|
197
|
4 |
|
if ( ! $view->form ) { |
198
|
|
|
gravityview()->log->error( 'View #{view_id} tried attaching non-existent Form #{form_id} to it.', array( |
199
|
|
|
'view_id' => $view->ID, |
|
|
|
|
200
|
|
|
'form_id' => $view->_gravityview_form_id ? : 0, |
|
|
|
|
201
|
|
|
) ); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @filter `gravityview/configuration/fields` Filter the View fields' configuration array |
206
|
|
|
* @since 1.6.5 |
207
|
|
|
* |
208
|
|
|
* @param $fields array Multi-array of fields with first level being the field zones |
209
|
|
|
* @param $view_id int The View the fields are being pulled for |
210
|
|
|
*/ |
211
|
4 |
|
$configuration = apply_filters( 'gravityview/configuration/fields', (array)$view->_gravityview_directory_fields, $view->ID ); |
|
|
|
|
212
|
|
|
|
213
|
|
|
/** Get all fields. */ |
214
|
4 |
|
$view->fields = Field_Collection::from_configuration( $configuration ); |
215
|
|
|
|
216
|
|
|
/** The settings. */ |
217
|
4 |
|
$view->settings->update( gravityview_get_template_settings( $view->ID ) ); |
|
|
|
|
218
|
|
|
|
219
|
|
|
/** Set the template. */ |
220
|
4 |
|
$view->template = new \GV\View_Template( $view->_gravityview_directory_template ); |
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @deprecated |
224
|
|
|
* |
225
|
|
|
* The data here has been moved to various keys in a \GV\View instance. |
226
|
|
|
* As a compatibilty layer we allow array access over any \GV\View instance with these keys. |
227
|
|
|
* |
228
|
|
|
* This data is immutable (for now). |
229
|
|
|
* |
230
|
|
|
* @see \GV\View::offsetGet() for internal mappings. |
231
|
|
|
*/ |
232
|
4 |
|
$view->_data = array( |
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @deprecated |
235
|
|
|
* @see \GV\View::$ID |
236
|
|
|
*/ |
237
|
|
|
// 'id' => $view->ID, |
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @deprecated |
241
|
|
|
* @see \GV\View::$ID |
242
|
|
|
*/ |
243
|
|
|
// 'view_id' => $view->ID, |
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @deprecated |
247
|
|
|
* @see \GV\View::$form |
248
|
|
|
*/ |
249
|
|
|
// 'form' => gravityview_get_form( $view->_gravityview_form_id ), |
|
|
|
|
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @deprecated |
253
|
|
|
* @see \GV\View::$form::$ID |
254
|
|
|
*/ |
255
|
|
|
// 'form_id' => $view->_gravityview_form_id, |
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @deprecated |
259
|
|
|
* @see \GV\View::$settings |
260
|
|
|
*/ |
261
|
|
|
// 'atts' => $view->settings->as_atts(), |
|
|
|
|
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @deprecated |
265
|
|
|
* @see \GV\View::$fields |
266
|
|
|
*/ |
267
|
|
|
// 'fields' => \GravityView_View_Data::getInstance()->get_fields( $view->ID ), |
|
|
|
|
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @deprecated |
271
|
|
|
* @see \GV\View::$template::$ID |
272
|
|
|
*/ |
273
|
|
|
// 'template_id' => gravityview_get_template_id( $view->ID ), |
|
|
|
|
274
|
|
|
|
275
|
4 |
|
'widgets' => gravityview_get_directory_widgets( $view->ID ), |
|
|
|
|
276
|
|
|
); |
277
|
|
|
|
278
|
4 |
|
return $view; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Construct a \GV\View instance from a post ID. |
283
|
|
|
* |
284
|
|
|
* @param int|string $post_id The post ID. |
285
|
|
|
* |
286
|
|
|
* @api |
287
|
|
|
* @since future |
288
|
|
|
* @return \GV\View|null An instance around this \WP_Post or null if not found. |
289
|
|
|
*/ |
290
|
4 |
|
public static function by_id( $post_id ) { |
291
|
4 |
|
if ( ! $post_id || ! $post = get_post( $post_id ) ) { |
292
|
1 |
|
return null; |
293
|
|
|
} |
294
|
4 |
|
return self::from_post( $post ); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Determines if a view exists to begin with. |
299
|
|
|
* |
300
|
|
|
* @param int|\WP_Post|null $view_id The WordPress post ID, a \WP_Post object or null for global $post; |
|
|
|
|
301
|
|
|
* |
302
|
|
|
* @api |
303
|
|
|
* @since future |
304
|
|
|
* @return bool Whether the post exists or not. |
305
|
|
|
*/ |
306
|
4 |
|
public static function exists( $view ) { |
307
|
4 |
|
return get_post_type( $view ) == 'gravityview'; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* ArrayAccess compatibility layer with GravityView_View_Data::$views |
312
|
|
|
* |
313
|
|
|
* @internal |
314
|
|
|
* @deprecated |
315
|
|
|
* @since future |
316
|
|
|
* @return bool Whether the offset exists or not, limited to GravityView_View_Data::$views element keys. |
317
|
|
|
*/ |
318
|
1 |
|
public function offsetExists( $offset ) { |
|
|
|
|
319
|
1 |
|
$data_keys = array( 'id', 'view_id', 'form_id', 'template_id', 'atts', 'fields', 'widgets', 'form' ); |
320
|
1 |
|
return in_array( $offset, $data_keys ); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* ArrayAccess compatibility layer with GravityView_View_Data::$views |
325
|
|
|
* |
326
|
|
|
* Maps the old keys to the new data; |
327
|
|
|
* |
328
|
|
|
* @internal |
329
|
|
|
* @deprecated |
330
|
|
|
* @since future |
331
|
|
|
* |
332
|
|
|
* @return mixed The value of the requested view data key limited to GravityView_View_Data::$views element keys. |
333
|
|
|
*/ |
334
|
1 |
|
public function offsetGet( $offset ) { |
|
|
|
|
335
|
|
|
|
336
|
1 |
|
gravityview()->log->notice( 'This is a \GV\View object should not be accessed as an array.' ); |
337
|
|
|
|
338
|
1 |
|
if ( ! isset( $this[$offset] ) ) { |
|
|
|
|
339
|
|
|
return null; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
switch ( $offset ) { |
343
|
1 |
|
case 'id': |
344
|
1 |
|
case 'view_id': |
345
|
1 |
|
return $this->ID; |
|
|
|
|
346
|
1 |
|
case 'form': |
347
|
1 |
|
return $this->form; |
348
|
1 |
|
case 'form_id': |
349
|
1 |
|
return $this->form ? $this->form->ID : null; |
350
|
1 |
|
case 'atts': |
351
|
|
|
return $this->as_atts(); |
|
|
|
|
352
|
1 |
|
case 'template_id': |
353
|
1 |
|
return $this->template ? $this->template->ID : null; |
354
|
|
|
default: |
355
|
|
|
/** @todo move the rest out and get rid of _data completely! */ |
356
|
|
|
return $this->_data[$offset]; |
|
|
|
|
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* ArrayAccess compatibility layer with GravityView_View_Data::$views |
362
|
|
|
* |
363
|
|
|
* @internal |
364
|
|
|
* @deprecated |
365
|
|
|
* @since future |
366
|
|
|
* |
367
|
|
|
* @return void |
368
|
|
|
*/ |
369
|
1 |
|
public function offsetSet( $offset, $value ) { |
|
|
|
|
370
|
1 |
|
gravityview()->log->error( 'The old view data is no longer mutable. This is a \GV\View object should not be accessed as an array.' ); |
371
|
1 |
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* ArrayAccess compatibility layer with GravityView_View_Data::$views |
375
|
|
|
* |
376
|
|
|
* @internal |
377
|
|
|
* @deprecated |
378
|
|
|
* @since future |
379
|
|
|
* @return void |
380
|
|
|
*/ |
381
|
1 |
|
public function offsetUnset( $offset ) { |
|
|
|
|
382
|
1 |
|
gravityview()->log->error( 'The old view data is no longer mutable. This is a \GV\View object should not be accessed as an array.' ); |
383
|
1 |
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Be compatible with the old data object. |
387
|
|
|
* |
388
|
|
|
* Some external code expects an array (doing things like foreach on this, or array_keys) |
389
|
|
|
* so let's return an array in the old format for such cases. Do not use unless using |
390
|
|
|
* for back-compatibility. |
391
|
|
|
* |
392
|
|
|
* @internal |
393
|
|
|
* @deprecated |
394
|
|
|
* @since future |
395
|
|
|
* @return array |
396
|
|
|
*/ |
397
|
1 |
|
public function as_data() { |
398
|
1 |
|
return array_merge( |
399
|
1 |
|
array( 'id' => $this->ID ), |
|
|
|
|
400
|
1 |
|
array( 'view_id' => $this->ID ), |
|
|
|
|
401
|
1 |
|
array( 'form_id' => $this->form ? $this->form->ID : null ), |
402
|
1 |
|
array( 'form' => $this->form ? gravityview_get_form( $this->form->ID ) : null ), |
403
|
1 |
|
array( 'atts' => $this->settings->as_atts() ), |
|
|
|
|
404
|
1 |
|
array( 'fields' => $this->fields->by_visible()->as_configuration() ), |
405
|
1 |
|
array( 'template_id' => $this->template? $this->template->ID : null ), |
406
|
1 |
|
$this->_data |
407
|
|
|
); |
408
|
|
|
} |
409
|
|
|
|
410
|
3 |
|
public function __get( $key ) { |
411
|
3 |
|
return $this->post->$key; |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|
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.