Passed
Push — master ( 24b191...649fed )
by Glynn
07:28 queued 04:26
created

register_meta_data_for_post_type()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
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 Exception;
29
use PinkCrab\Registerables\Meta_Data;
30
use PinkCrab\Registerables\Registrar\Registrar;
31
use PinkCrab\Registerables\Shared_Meta_Box_Controller;
32
use PinkCrab\Registerables\Registrar\Meta_Box_Registrar;
33
use PinkCrab\Registerables\Registration_Middleware\Registerable;
34
35
class Shared_Meta_Box_Registrar implements Registrar {
36
37
	/**
38
	 * The Meta Box Registrar
39
	 *
40
	 * @var Meta_Box_Registrar
41
	 */
42
	protected $meta_box_registrar;
43
44
	public function __construct( Meta_Box_Registrar $meta_box_registrar ) {
45
		$this->meta_box_registrar = $meta_box_registrar;
46
	}
47
48
	/**
49
	 * Used to register a registerable
50
	 *
51
	 * @param \PinkCrab\Registerables\Registration_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->register_meta_data_for_post_type( $meta_field, $post_type );
73
			}
74
		}
75
76
	}
77
78
	/**
79
	 * Filters all non meta data from array.
80
	 *
81
	 * @param mixed[] $meta_data
82
	 * @return Meta_Data[]
83
	 */
84
	protected function filter_meta_data( array $meta_data ): array {
85
		return array_filter(
86
			$meta_data,
87
			function( $e ) {
88
				return is_a( $e, Meta_Data::class );
89
			}
90
		);
91
	}
92
93
	/**
94
	 * Registers the Meta Data for a give post type.
95
	 *
96
	 * @param Meta_Data $meta_data
97
	 * @param string $post_type
98
	 * @return void
99
	 */
100
	protected function register_meta_data_for_post_type( Meta_Data $meta_data, string $post_type ): void {
101
		// Clone and set the post type, while enforcing it as a post meta.
102
		$meta_data = clone $meta_data;
103
		$meta_data->object_subtype( $post_type );
104
		$meta_data->meta_type( 'post' );
105
		$result = register_meta( $meta_data->get_meta_type(), $meta_data->get_meta_key(), $meta_data->parse_args() );
106
		if ( ! $result ) {
107
			throw new \Exception( "Failed to register {$meta_data->get_meta_key()} (meta) for {$post_type} post type" );
108
		}
109
	}
110
111
112
}
113