Completed
Push — master ( 196a0a...af77e8 )
by Zack
11:10 queued 04:43
created

View::from_post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 5.

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.

Loading history...
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
6
	die();
7
8
/**
9
 * The default GravityView View class.
10
 *
11
 * Houses all base View functionality.
12
 */
13
class View {
14
15
	/**
16
	 * @var The backing \WP_Post instance.
17
	 */
18
	private $post;
19
20
	/**
21
	 * Register the gravityview WordPress Custom Post Type.
22
	 *
23
	 * @internal
24
	 * @return void
25
	 */
26
	public static function register_post_type() {
27
28
		/** Register only once */
29
		if ( post_type_exists( 'gravityview' ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
30
			return;
31
32
		/**
33
		 * @filter `gravityview_is_hierarchical` Make GravityView Views hierarchical by returning TRUE
34
		 * This will allow for Views to be nested with Parents and also allows for menu order to be set in the Page Attributes metabox
35
		 * @since 1.13
36
		 * @param boolean $is_hierarchical Default: false
37
		 */
38
		$is_hierarchical = (bool)apply_filters( 'gravityview_is_hierarchical', false );
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
39
40
		$supports = array( 'title', 'revisions' );
41
42
		if ( $is_hierarchical ) {
43
			$supports[] = 'page-attributes';
44
		}
45
46
		/**
47
		 * @filter  `gravityview_post_type_supports` Modify post type support values for `gravityview` post type
48
		 * @see add_post_type_support()
49
		 * @since 1.15.2
50
		 * @param array $supports Array of features associated with a functional area of the edit screen. Default: 'title', 'revisions'. If $is_hierarchical, also 'page-attributes'
51
		 * @param[in] boolean $is_hierarchical Do Views support parent/child relationships? See `gravityview_is_hierarchical` filter.
52
		 */
53
		$supports = apply_filters( 'gravityview_post_type_support', $supports, $is_hierarchical );
54
55
		/** Register Custom Post Type - gravityview */
56
		$labels = array(
57
			'name'                => _x( 'Views', 'Post Type General Name', 'gravityview' ),
58
			'singular_name'       => _x( 'View', 'Post Type Singular Name', 'gravityview' ),
59
			'menu_name'           => _x( 'Views', 'Menu name', 'gravityview' ),
60
			'parent_item_colon'   => __( 'Parent View:', 'gravityview' ),
61
			'all_items'           => __( 'All Views', 'gravityview' ),
62
			'view_item'           => _x( 'View', 'View Item', 'gravityview' ),
63
			'add_new_item'        => __( 'Add New View', 'gravityview' ),
64
			'add_new'             => __( 'New View', 'gravityview' ),
65
			'edit_item'           => __( 'Edit View', 'gravityview' ),
66
			'update_item'         => __( 'Update View', 'gravityview' ),
67
			'search_items'        => __( 'Search Views', 'gravityview' ),
68
			'not_found'           => \GravityView_Admin::no_views_text(),
69
			'not_found_in_trash'  => __( 'No Views found in Trash', 'gravityview' ),
70
			'filter_items_list'     => __( 'Filter Views list', 'gravityview' ),
71
			'items_list_navigation' => __( 'Views list navigation', 'gravityview' ),
72
			'items_list'            => __( 'Views list', 'gravityview' ),
73
			'view_items'            => __( 'See Views', 'gravityview' ),
74
			'attributes'            => __( 'View Attributes', 'gravityview' ),
75
		);
76
		$args = array(
77
			'label'               => __( 'view', 'gravityview' ),
78
			'description'         => __( 'Create views based on a Gravity Forms form', 'gravityview' ),
79
			'labels'              => $labels,
80
			'supports'            => $supports,
81
			'hierarchical'        => $is_hierarchical,
82
			/**
83
			 * @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode?
84
			 * @see https://codex.wordpress.org/Function_Reference/register_post_type#public
85
			 * @since 1.15.2
86
			 * @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded via shortcode. Default: `true`
87
			 * @param int $view_id The ID of the View currently being requested. `0` for general setting
88
			 */
89
			'public'              => apply_filters( 'gravityview_direct_access', gravityview()->plugin->is_compatible(), 0 ),
90
			'show_ui'             => gravityview()->plugin->is_compatible(),
91
			'show_in_menu'        => gravityview()->plugin->is_compatible(),
92
			'show_in_nav_menus'   => true,
93
			'show_in_admin_bar'   => true,
94
			'menu_position'       => 17,
95
			'menu_icon'           => '',
96
			'can_export'          => true,
97
			/**
98
			 * @filter `gravityview_has_archive` Enable Custom Post Type archive?
99
			 * @since 1.7.3
100
			 * @param boolean False: don't have frontend archive; True: yes, have archive. Default: false
101
			 */
102
			'has_archive'         => apply_filters( 'gravityview_has_archive', false ),
103
			'exclude_from_search' => true,
104
			'rewrite'             => array(
105
				/**
106
				 * @filter `gravityview_slug` Modify the url part for a View.
107
				 * @see http://docs.gravityview.co/article/62-changing-the-view-slug
108
				 * @param string $slug The slug shown in the URL
109
				 */
110
				'slug' => apply_filters( 'gravityview_slug', 'view' ),
111
112
				/**
113
				 * @filter `gravityview/post_type/with_front` Should the permalink structure
114
				 *  be prepended with the front base.
115
				 *  (example: if your permalink structure is /blog/, then your links will be: false->/view/, true->/blog/view/).
116
				 *  Defaults to true.
117
				 * @see https://codex.wordpress.org/Function_Reference/register_post_type
118
				 * @since future
119
				 * @param bool $with_front
120
				 */
121
				'with_front' => apply_filters( 'gravityview/post_type/with_front', true ),
122
			),
123
			'capability_type'     => 'gravityview',
124
			'map_meta_cap'        => true,
125
		);
126
127
		register_post_type( 'gravityview', $args );
128
	}
129
130
131
	/**
132
	 * Construct a \GV\View instance from a \WP_Post.
133
	 *
134
	 * @param \WP_Post $post The \WP_Post instance to wrap.
135
	 * @throws \InvalidArgumentException if $post is not of 'gravityview' type.
136
	 *
137
	 * @api
138
	 * @since future
139
	 * @return \GV\View An instance around this \WP_Post.
140
	 */
141 1
	public static function from_post( \WP_Post $post ) {
142 1
		if ( get_post_type( $post ) != 'gravityview' ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
143 1
			throw new \InvalidArgumentException( 'Only gravityview post types can be \GV\View instances.' );
144
		}
145
146 1
		$view = new self();
147 1
		$view->post = $post;
148
149 1
		return $view;
150
	}
151
152
	/**
153
	 * Construct a \GV\View instance from a post ID.
154
	 *
155
	 * @param int|string $post_id The post ID.
156
	 * @throws \InvalidArgumentException if $post is not of 'gravityview' type.
157
	 *
158
	 * @api
159
	 * @since future
160
	 * @return \GV\View|null An instance around this \WP_Post or null if not found.
161
	 */
162 1
	public static function by_id( $post_id ) {
163 1
		if ( ! $post = get_post( $post_id ) ) {
164
			return null;
165
		}
166 1
		return self::from_post( $post );
167
	}
168
169
	public function __get( $key ) {
170
		return $this->post->$key;
171
	}
172
}
173