|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Registration Registrar for Shared Meta Boxes. |
|
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
|
|
|
* @since 0.7.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace PinkCrab\Registerables\Registrar; |
|
27
|
|
|
|
|
28
|
|
|
use PinkCrab\Registerables\Meta_Data; |
|
29
|
|
|
use PinkCrab\Registerables\Registrar\Registrar; |
|
30
|
|
|
use PinkCrab\Registerables\Shared_Meta_Box_Controller; |
|
31
|
|
|
use PinkCrab\Registerables\Registrar\Meta_Box_Registrar; |
|
32
|
|
|
use PinkCrab\Registerables\Registrar\Meta_Data_Registrar; |
|
33
|
|
|
use PinkCrab\Registerables\Module\Middleware\Registerable; |
|
34
|
|
|
|
|
35
|
|
|
class Shared_Meta_Box_Registrar implements Registrar { |
|
36
|
|
|
|
|
37
|
|
|
protected Meta_Box_Registrar $meta_box_registrar; |
|
38
|
|
|
protected Meta_Data_Registrar $meta_data_registrar; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct( |
|
41
|
|
|
Meta_Box_Registrar $meta_box_registrar, |
|
42
|
|
|
Meta_Data_Registrar $meta_data_registrar |
|
43
|
|
|
) { |
|
44
|
|
|
$this->meta_box_registrar = $meta_box_registrar; |
|
45
|
|
|
$this->meta_data_registrar = $meta_data_registrar; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Used to register a registerable |
|
50
|
|
|
* |
|
51
|
|
|
* @param \PinkCrab\Registerables\Module\Middleware\Registerable $registerable |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function register( Registerable $registerable ): void { |
|
55
|
|
|
if ( ! is_a( $registerable, Shared_Meta_Box_Controller::class ) ) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @var Shared_Meta_Box_Controller $registerable, Validation call below catches no Shared_Meta_Box_Controller Registerables */ |
|
60
|
|
|
|
|
61
|
|
|
// Get the meta box and meta data. |
|
62
|
|
|
$meta_box = $registerable->meta_box(); |
|
63
|
|
|
$meta_data = $registerable->meta_data( array() ); |
|
64
|
|
|
|
|
65
|
|
|
// Register the meta box. |
|
66
|
|
|
$this->meta_box_registrar->register( $meta_box ); |
|
67
|
|
|
|
|
68
|
|
|
// Register all meta data. |
|
69
|
|
|
foreach ( $this->filter_meta_data( $meta_data ) as $meta_field ) { |
|
70
|
|
|
// Register meta data for each post type. |
|
71
|
|
|
foreach ( $meta_box->screen as $post_type ) { |
|
72
|
|
|
$this->meta_data_registrar->register_for_post_type( $meta_field, $post_type ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Filters all non meta data from array. |
|
79
|
|
|
* |
|
80
|
|
|
* @param mixed[] $meta_data |
|
81
|
|
|
* @return Meta_Data[] |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function filter_meta_data( array $meta_data ): array { |
|
84
|
|
|
return array_filter( |
|
85
|
|
|
$meta_data, |
|
86
|
|
|
function ( $e ) { |
|
87
|
|
|
return is_a( $e, Meta_Data::class ); |
|
88
|
|
|
} |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|