1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
declare(strict_types=1); |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Registerable Middleware |
8
|
|
|
* |
9
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
10
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
11
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
12
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
13
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
14
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
15
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
16
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
17
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
18
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
19
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
20
|
|
|
* |
21
|
|
|
* @author Glynn Quelch <[email protected]> |
22
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
23
|
|
|
* @package PinkCrab\Registerables |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace PinkCrab\Registerables\Registration_Middleware; |
27
|
|
|
|
28
|
|
|
use PinkCrab\Loader\Hook_Loader; |
29
|
|
|
use PinkCrab\Registerables\Taxonomy; |
30
|
|
|
use PinkCrab\Registerables\Post_Type; |
31
|
|
|
use PinkCrab\Perique\Interfaces\DI_Container; |
32
|
|
|
use PinkCrab\Registerables\Shared_Meta_Box_Controller; |
33
|
|
|
use PinkCrab\Registerables\Registrar\Registrar_Factory; |
34
|
|
|
use PinkCrab\Perique\Interfaces\Registration_Middleware; |
35
|
|
|
use PinkCrab\Registerables\Registrar\Meta_Box_Registrar; |
36
|
|
|
use PinkCrab\Registerables\Additional_Meta_Data_Controller; |
37
|
|
|
use PinkCrab\Registerables\Registrar\Shared_Meta_Box_Registrar; |
38
|
|
|
use PinkCrab\Registerables\Registration_Middleware\Registerable; |
39
|
|
|
use PinkCrab\Registerables\Registrar\Additional_Meta_Data_Registrar; |
40
|
|
|
|
41
|
|
|
class Registerable_Middleware implements Registration_Middleware { |
42
|
|
|
|
43
|
|
|
/** @var Hook_Loader */ |
44
|
|
|
protected $loader; |
45
|
|
|
|
46
|
|
|
/** @var DI_Container */ |
47
|
|
|
protected $container; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sets the global hook loader |
51
|
|
|
* |
52
|
|
|
* @param \PinkCrab\Loader\Hook_Loader $loader |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function set_hook_loader( Hook_Loader $loader ) { |
56
|
|
|
$this->loader = $loader; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Sets the global DI containers |
61
|
|
|
* |
62
|
|
|
* @param \PinkCrab\Perique\Interfaces\DI_Container $container |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function set_di_container( DI_Container $container ): void { |
66
|
|
|
$this->container = $container; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Register all valid registerables. |
71
|
|
|
* |
72
|
|
|
* @param object|Registerable $class |
73
|
|
|
* @return object |
74
|
|
|
*/ |
75
|
|
|
public function process( $class ) { |
76
|
|
|
if ( ! is_a( $class, Registerable::class ) ) { |
77
|
|
|
return $class; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Based on the registerable type. |
81
|
|
|
switch ( true ) { |
82
|
|
|
case is_a( $class, Post_Type::class ): |
83
|
|
|
$this->process_post_type( $class ); |
84
|
|
|
break; |
85
|
|
|
|
86
|
|
|
case is_a( $class, Taxonomy::class ): |
87
|
|
|
$this->process_taxonomy( $class ); |
88
|
|
|
break; |
89
|
|
|
|
90
|
|
|
case is_a( $class, Shared_Meta_Box_Controller::class ): |
91
|
|
|
$this->process_shared_meta_box( $class ); |
92
|
|
|
break; |
93
|
|
|
|
94
|
|
|
case is_a( $class, Additional_Meta_Data_Controller::class ): |
95
|
|
|
$this->process_additional_meta_data( $class ); |
96
|
|
|
break; |
97
|
|
|
|
98
|
|
|
default: |
99
|
|
|
// Do nothing, but should not get to here. |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $class; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Processes and registers a taxonomy |
108
|
|
|
* |
109
|
|
|
* @param \PinkCrab\Registerables\Taxonomy $taxonomy |
110
|
|
|
* @return void |
111
|
|
|
* @since 0.7.0 |
112
|
|
|
*/ |
113
|
|
|
protected function process_taxonomy( Taxonomy $taxonomy ): void { |
114
|
|
|
$this->loader->action( |
115
|
|
|
'init', |
116
|
|
|
static function() use ( $taxonomy ) { |
117
|
|
|
Registrar_Factory::new() |
118
|
|
|
->create_from_registerable( $taxonomy ) |
119
|
|
|
->register( $taxonomy ); |
120
|
|
|
} |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Processes and registers a post type. |
126
|
|
|
* |
127
|
|
|
* @param \PinkCrab\Registerables\Post_Type $post_type_registerable |
128
|
|
|
* @return void |
129
|
|
|
* @since 0.7.0 |
130
|
|
|
*/ |
131
|
|
|
protected function process_post_type( Post_Type $post_type_registerable ) { |
132
|
|
|
// Register registerable. |
133
|
|
|
$this->loader->action( |
134
|
|
|
'init', |
135
|
|
|
static function() use ( $post_type_registerable ) { |
136
|
|
|
Registrar_Factory::new() |
137
|
|
|
->create_from_registerable( $post_type_registerable ) |
138
|
|
|
->register( $post_type_registerable ); |
139
|
|
|
} |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
// Define use of gutenberg |
143
|
|
|
$this->loader->filter( |
144
|
|
|
'use_block_editor_for_post_type', |
145
|
|
|
static function( bool $state, string $post_type ) use ( $post_type_registerable ): bool { |
146
|
|
|
return $post_type === $post_type_registerable->key |
147
|
|
|
? (bool) $post_type_registerable->gutenberg |
148
|
|
|
: $state; |
149
|
|
|
}, |
150
|
|
|
10, |
151
|
|
|
2 |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
// Register meta boxes. |
155
|
|
|
$meta_boxes = $post_type_registerable->meta_boxes( array() ); |
156
|
|
|
|
157
|
|
|
if ( ! empty( $meta_boxes ) ) { |
158
|
|
|
|
159
|
|
|
// Create the registrar |
160
|
|
|
$meta_box_registrar = $this->get_meta_box_registrar(); |
161
|
|
|
|
162
|
|
|
// Register each meta box. |
163
|
|
|
foreach ( $meta_boxes as $meta_box ) { |
164
|
|
|
$meta_box->screen( $post_type_registerable->key ); |
165
|
|
|
$meta_box_registrar->register( $meta_box ); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Processes a shared meta box controller. |
172
|
|
|
* Registers both meta box and meta data. |
173
|
|
|
* |
174
|
|
|
* @param \PinkCrab\Registerables\Shared_Meta_Box_Controller $controller |
175
|
|
|
* @return void |
176
|
|
|
* @since 0.7.0 |
177
|
|
|
*/ |
178
|
|
|
public function process_shared_meta_box( Shared_Meta_Box_Controller $controller ): void { |
179
|
|
|
$registrar = new Shared_Meta_Box_Registrar( |
180
|
|
|
$this->get_meta_box_registrar(), |
181
|
|
|
Registrar_Factory::new()->meta_data_registrar() |
182
|
|
|
); |
183
|
|
|
$registrar->register( $controller ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Process the additional meta data controller. |
188
|
|
|
* |
189
|
|
|
* @param \PinkCrab\Registerables\Additional_Meta_Data_Controller $controller |
190
|
|
|
* @return void |
191
|
|
|
* @since 0.8.0 |
192
|
|
|
*/ |
193
|
|
|
public function process_additional_meta_data( Additional_Meta_Data_Controller $controller ): void { |
194
|
|
|
$registrar = new Additional_Meta_Data_Registrar( |
195
|
|
|
Registrar_Factory::new()->meta_data_registrar() |
196
|
|
|
); |
197
|
|
|
$registrar->register( $controller ); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Constructs and returns and instance of the Meta Box Registrar |
202
|
|
|
* |
203
|
|
|
* @return \PinkCrab\Registerables\Registrar\Meta_Box_Registrar |
204
|
|
|
* @since 0.7.0 |
205
|
|
|
*/ |
206
|
|
|
public function get_meta_box_registrar(): Meta_Box_Registrar { |
207
|
|
|
return Registrar_Factory::new()->meta_box_registrar( $this->container, $this->loader ); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function setup(): void { |
211
|
|
|
/*noOp*/ |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Register all routes with WordPress calls. |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function tear_down(): void { |
220
|
|
|
/*noOp*/ |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|