-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractPostType.php
141 lines (128 loc) · 3.11 KB
/
AbstractPostType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Base class for use when registering post types.
*
* The minimum requirement here is to set a $name. You should also override the
* labels and args - either via method override or by passing them into the
* class before calling 'register'.
*
* @package PattonWebz Post Type Registration Class
* @version 0.2.2
* @since 0.1.0
* @author William Patton <will@pattonwebz.com>
* @copyright Copyright (c) 2018-2019, William Patton
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
namespace PattonWebz\PostType;
/**
* Registers post types for use.
*
* @since 0.1.0
*/
abstract class AbstractPostType {
/**
* The identifier for this post type.
*
* @since 0.1.0
* @var string
*/
public $name = 'post';
/**
* Icon to use for this post type.
*
* @since 0.2.2
* @var string
*/
public $icon = 'dashicons-admin-post';
/**
* The arguments used when registering this post type.
*
* @since 0.1.0
* @var array
*/
public $args = [];
/**
* The arguments used when registering this post type.
*
* @since 0.1.0
* @var array
*/
public $labels = [];
/**
* Sets up the properties for use when registering.
*
* @method __construct
* @since 0.2.2
* @param array $args The array of args to use when registering the CPT.
* @param array $labels A custom labels array to use when registering the
* post. NOTE: If you are passing a custom $args array
* then this will not be included automatticaly so
* include it in $args['labels'] directly.
*/
public function __construct( array $args = [], array $labels = [] ) {
$this->labels = ( ! empty( $labels ) ) ? $labels : $this->get_labels();
$this->args = ( ! empty( $args ) ) ? $args : $this->get_args();
}
/**
* Hook in and register the post type.
*
* @method register
* @since 0.1.0
*/
public function register() {
add_action( 'init', [ $this, 'register_cpt' ], 0 );
}
/**
* Get the name of this post type.
*
* @method get_name
* @since 0.1.0
*
* @return string
*/
public function get_name() {
return $this->name;
}
/**
* Get the labels used with this post type.
*
* @method get_labels
* @since 0.1.0
*
* @return array
*/
public function get_labels() {
return ( ! empty( $this->labels ) ) ? $this->labels : [
'name' => ucwords( $this->name ),
];
}
/**
* Get the post type registration args.
*
* @method get_args
* @since 0.1.0
*
* @return array
*/
public function get_args() {
$labels = $this->get_labels();
$name = ( isset( $labels['menu_name'] ) ) ? $labels['menu_name'] : $this->name;
return ( ! empty( $this->args ) ) ? $this->args : [
'post_type' => $name,
'label' => $name,
'labels' => $labels,
'menu_icon' => ( ! empty( $this->icon ) ) ? $this->icon : 'dashicons-admin-post',
'public' => true,
];
}
/**
* Register the CPT used to hold posts.
*
* @since 0.1.0
*/
public function register_cpt() {
if ( ! post_type_exists( $this->name ) ) {
register_post_type( $this->name, $this->get_args() );
}
}
}