1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Registration Registrar for all post types. |
7
|
|
|
* |
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
19
|
|
|
* |
20
|
|
|
* @author Glynn Quelch <[email protected]> |
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
22
|
|
|
* @package PinkCrab\Registerables |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace PinkCrab\Registerables\Registrar; |
26
|
|
|
|
27
|
|
|
use Exception; |
28
|
|
|
use PinkCrab\Loader\Hook_Loader; |
29
|
|
|
use PinkCrab\Registerables\Meta_Box; |
30
|
|
|
use PinkCrab\Perique\Services\View\View; |
31
|
|
|
use PinkCrab\Perique\Interfaces\DI_Container; |
32
|
|
|
use PinkCrab\Registerables\Validator\Meta_Box_Validator; |
33
|
|
|
|
34
|
|
|
class Meta_Box_Registrar { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Meta_Box_Validator |
38
|
|
|
*/ |
39
|
|
|
protected $validator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var DI_Container |
43
|
|
|
*/ |
44
|
|
|
protected $container; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Hook_Loader |
48
|
|
|
*/ |
49
|
|
|
protected $loader; |
50
|
|
|
|
51
|
|
|
public function __construct( |
52
|
|
|
Meta_Box_Validator $validator, |
53
|
|
|
DI_Container $container, |
54
|
|
|
Hook_Loader $loader |
55
|
|
|
) { |
56
|
|
|
$this->validator = $validator; |
57
|
|
|
$this->container = $container; |
58
|
|
|
$this->loader = $loader; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Register a meta box. |
63
|
|
|
* |
64
|
|
|
* @param Meta_Box $meta_box |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function register( Meta_Box $meta_box ): void { |
68
|
|
|
|
69
|
|
|
if ( ! $this->validator->verify_meta_box( $meta_box ) ) { |
70
|
|
|
throw new Exception( 'Invalid meta box model' ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Set the view using View, if not traditional callback supplied and a defined template. |
74
|
|
|
if ( ! \is_callable( $meta_box->view ) && is_string( $meta_box->view_template ) ) { |
75
|
|
|
$meta_box = $this->set_view_callback_from_view( $meta_box ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Add meta_box to loader. |
79
|
|
|
$this->loader->action( |
80
|
|
|
'add_meta_boxes', |
81
|
|
|
function() use ( $meta_box ) : void { |
82
|
|
|
\add_meta_box( |
83
|
|
|
$meta_box->key, |
84
|
|
|
$meta_box->label, |
85
|
|
|
$meta_box->view, /** @phpstan-ignore-line, is validated above*/ |
86
|
|
|
$meta_box->screen, |
87
|
|
|
$meta_box->context, |
88
|
|
|
$meta_box->priority, |
89
|
|
|
$meta_box->view_vars |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
); |
93
|
|
|
// If we have any hook calls, add them to the loader. |
94
|
|
|
if ( $this->is_active( $meta_box ) && ! empty( $meta_box->actions ) ) { |
95
|
|
|
foreach ( $meta_box->actions as $handle => $hook ) { |
96
|
|
|
$this->loader->action( (string) $handle, $hook['callback'], $hook['priority'], $hook['params'] ); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Apply rendering the view using View to a meta_box |
104
|
|
|
* |
105
|
|
|
* @param \PinkCrab\Registerables\Meta_Box $meta_box |
106
|
|
|
* @return \PinkCrab\Registerables\Meta_Box |
107
|
|
|
*/ |
108
|
|
|
protected function set_view_callback_from_view( Meta_Box $meta_box ): Meta_Box { |
109
|
|
|
|
110
|
|
|
// Create View(View) |
111
|
|
|
$view = $this->container->create( View::class ); |
112
|
|
|
if ( is_null( $view ) || ! is_a( $view, View::class ) ) { |
113
|
|
|
throw new Exception( 'View not defined' ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$meta_box->view( |
117
|
|
|
function ( \WP_Post $post, array $args ) use ( $meta_box, $view ) { |
118
|
|
|
$args['args']['post'] = $post; |
119
|
|
|
|
120
|
|
|
// @phpstan-ignore-next-line, template should already be checked for valid template path in register() method (which calls this) |
121
|
|
|
$view->render( $meta_box->view_template, $args['args'] ); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $meta_box; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Checks if the checkbox should be active. |
130
|
|
|
* |
131
|
|
|
* @return boolean |
132
|
|
|
*/ |
133
|
|
|
protected function is_active( Meta_Box $meta_box ): bool { |
134
|
|
|
global $current_screen; |
135
|
|
|
|
136
|
|
|
return ! is_null( $current_screen ) |
137
|
|
|
&& ! empty( $current_screen->post_type ) |
138
|
|
|
&& in_array( $current_screen->post_type, $meta_box->screen, true ); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|