Registerable_Middleware::process_shared_meta_box()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
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\Module\Middleware;
27
28
use PinkCrab\Registerables\Taxonomy;
29
use PinkCrab\Registerables\Post_Type;
30
use PinkCrab\Perique\Interfaces\Inject_Hook_Loader;
31
use PinkCrab\Perique\Interfaces\Inject_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\Module\Middleware\Registerable;
37
use PinkCrab\Registerables\Additional_Meta_Data_Controller;
38
use PinkCrab\Registerables\Registrar\Shared_Meta_Box_Registrar;
39
use PinkCrab\Registerables\Registrar\Additional_Meta_Data_Registrar;
40
use PinkCrab\Perique\Services\Container_Aware_Traits\Inject_Hook_Loader_Aware;
41
use PinkCrab\Perique\Services\Container_Aware_Traits\Inject_DI_Container_Aware;
42
43
class Registerable_Middleware implements Registration_Middleware, Inject_Hook_Loader, Inject_DI_Container {
44
45
	use Inject_Hook_Loader_Aware;
46
	use Inject_DI_Container_Aware;
47
48
	/**
49
	 * Register all valid registerables.
50
	 *
51
	 * @param Registerable $class_instance
52
	 * @return object
53
	 */
54
	public function process( object $class_instance ): object {
55
		/** @phpstan-ignore-next-line */
56
		if ( ! is_a( $class_instance, Registerable::class ) ) {
57
			return $class_instance;
58
		}
59
60
		// Based on the registerable type.
61
		switch ( true ) {
62
			case is_a( $class_instance, Post_Type::class ):
63
				$this->process_post_type( $class_instance );
64
				break;
65
66
			case is_a( $class_instance, Taxonomy::class ):
67
				$this->process_taxonomy( $class_instance );
68
				break;
69
70
			case is_a( $class_instance, Shared_Meta_Box_Controller::class ):
71
				$this->process_shared_meta_box( $class_instance );
72
				break;
73
74
			case is_a( $class_instance, Additional_Meta_Data_Controller::class ):
75
				$this->process_additional_meta_data( $class_instance );
76
				break;
77
78
			default:
79
				// Do nothing, but should not get to here.
80
				break;
81
		}
82
83
		return $class_instance;
84
	}
85
86
	/**
87
	 * Processes and registers a taxonomy
88
	 *
89
	 * @param \PinkCrab\Registerables\Taxonomy $taxonomy
90
	 * @return void
91
	 * @since 0.7.0
92
	 */
93
	protected function process_taxonomy( Taxonomy $taxonomy ): void {
94
		$this->loader->action(
95
			'init',
96
			static function () use ( $taxonomy ) {
97
				Registrar_Factory::new()
98
					->create_from_registerable( $taxonomy )
99
					->register( $taxonomy );
100
			}
101
		);
102
	}
103
104
	/**
105
	 * Processes and registers a post type.
106
	 *
107
	 * @param \PinkCrab\Registerables\Post_Type $post_type_registerable
108
	 * @return void
109
	 * @since 0.7.0
110
	 */
111
	protected function process_post_type( Post_Type $post_type_registerable ) {
112
		// Register registerable.
113
		$this->loader->action(
114
			'init',
115
			static function () use ( $post_type_registerable ) {
116
				Registrar_Factory::new()
117
					->create_from_registerable( $post_type_registerable )
118
					->register( $post_type_registerable );
119
			}
120
		);
121
122
		// Define use of gutenberg
123
		$this->loader->filter(
124
			'use_block_editor_for_post_type',
125
			static function ( bool $state, string $post_type ) use ( $post_type_registerable ): bool {
126
					return $post_type === $post_type_registerable->key
127
						? (bool) $post_type_registerable->gutenberg
128
						: $state;
129
			},
130
			10,
131
			2
132
		);
133
134
		// Register meta boxes.
135
		$meta_boxes = $post_type_registerable->meta_boxes( array() );
136
137
		if ( ! empty( $meta_boxes ) ) {
138
139
			// Create the registrar
140
			$meta_box_registrar = $this->get_meta_box_registrar();
141
142
			// Register each meta box.
143
			foreach ( $meta_boxes as $meta_box ) {
144
				$meta_box->screen( $post_type_registerable->key );
145
				$meta_box_registrar->register( $meta_box );
146
			}
147
		}
148
	}
149
150
	/**
151
	 * Processes a shared meta box controller.
152
	 * Registers both meta box and meta data.
153
	 *
154
	 * @param \PinkCrab\Registerables\Shared_Meta_Box_Controller $controller
155
	 * @return void
156
	 * @since 0.7.0
157
	 */
158
	public function process_shared_meta_box( Shared_Meta_Box_Controller $controller ): void {
159
		$registrar = new Shared_Meta_Box_Registrar(
160
			$this->get_meta_box_registrar(),
161
			Registrar_Factory::new()->meta_data_registrar()
162
		);
163
		$registrar->register( $controller );
164
	}
165
166
	/**
167
	 * Process the additional meta data controller.
168
	 *
169
	 * @param \PinkCrab\Registerables\Additional_Meta_Data_Controller $controller
170
	 * @return void
171
	 * @since 0.8.0
172
	 */
173
	public function process_additional_meta_data( Additional_Meta_Data_Controller $controller ): void {
174
		$registrar = new Additional_Meta_Data_Registrar(
175
			Registrar_Factory::new()->meta_data_registrar()
176
		);
177
		$registrar->register( $controller );
178
	}
179
180
	/**
181
	 * Constructs and returns and instance of the Meta Box Registrar
182
	 *
183
	 * @return \PinkCrab\Registerables\Registrar\Meta_Box_Registrar
184
	 * @since 0.7.0
185
	 */
186
	public function get_meta_box_registrar(): Meta_Box_Registrar {
187
		return Registrar_Factory::new()->meta_box_registrar( $this->di_container, $this->loader );
188
	}
189
190
	public function setup(): void {
191
		/*noOp*/
192
	}
193
194
	/**
195
	 * Register all routes with WordPress calls.
196
	 *
197
	 * @return void
198
	 */
199
	public function tear_down(): void {
200
		/*noOp*/
201
	}
202
}
203