-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWCS_Type.php
160 lines (125 loc) · 4.15 KB
/
WCS_Type.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
if( !defined('ABSPATH') ) {
exit;
}
class WCS_Type
{
/**
* Post type system reference.
* Must not exceed 20 characters and may only contain lowercase alphanumeric characters, dashes, and underscores.
*
* @var string
*/
protected string $key;
/**
* Arguments passed to register_post_type
*
* @var array
*/
protected array $args = [];
/**
* Array of Wordpress Meta boxes that containts fields
* Must be instances of the WCSGroup class
*
* @var array
*/
protected array $groups = [];
/**
* Keep state of the hook int WP
*
* @var boolean
*/
protected bool $hooked = false;
/**
* Set the post type key
*
* @param string $key
* @throws Exception
*/
public function __construct( string $key, array $args = [] )
{
if( $this->validateKey($key) ) {
$this->key = $key;
$this->args = array_merge([
'supports' => ['title','editor','revisions','excerpt','thumbnail','custom-fields'],
'has_archive' => true
], $args);
} else {
throw new Exception("Invalid key-format");
}
return $this;
}
/**
* Set or update and argument
*
* @param string $key
* @param [type] $value
* @return void
*/
public function setArgs($key, $value = null ) : object
{
$key = is_array($key) ? $key : [$key => $value];
foreach($key as $index => $value) {
$this->args[$index] = $value;
}
return $this;
}
/**
* Hook into the wordpress eco-system and register post-type
* along all its groups that will render the custom fields
*
* @return boolean
* @throws Exception
*/
public function hook( bool $force = false ) : bool
{
if( function_exists("register_post_type") && function_exists('get_post_type_object') ) {
if( $this->hooked == false || $force ) {
if( get_post_type_object($this->key) === null ) {
register_post_type($this->key, $this->args);
}
// Hook each group into the current post type
foreach( $this->groups as $group ) {
$group->setPostTypes($this->key);
$group->hook();
}
$this->hooked = true;
} else {
throw new Exception("Type was already hooked into Wordpress");
}
} else {
throw new Exception("Wordpress functions register_post_type() and get_post_type_object was not available");
}
return true;
}
/**
* Add one or more instances of WCS_Group
*
* @param [type] $groups
* @return void
* @throws Exception
*/
public function addGroups( $groups ) : object
{
$groups = is_array($groups) ? $groups : [$groups];
foreach( $groups as $group ) {
if( is_a($group, "WCS_Group") ) {
$this->groups[$group->key] = $group;
} else {
throw new Exception("Group must inherit the WCS Group class");
}
}
return $this;
}
/**
* Function to validate key according to the requiements put up by
* wordpress built in function sanitize_key
*
* @param string $key
* @return bool
*/
private function validateKey(string $key ) : bool
{
return preg_match("/^[a-z\-_]{1,20}$/", $key);
}
}