1 | <?php |
||
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() { |
|
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 ) { |
|
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 ) { |
|
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 ) { |
|
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 ) { |
|
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 ) { |
|
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 ) { |
|
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() { |
|
409 | |||
410 | 3 | public function __get( $key ) { |
|
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.