Passed
Push — develop ( 1b9306...e448bf )
by Paul
02:50
created

MetaBox::normalizeFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 3
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux\MetaBox;
4
5
use GeminiLabs\Pollux\Application;
6
use GeminiLabs\Pollux\Component;
7
use GeminiLabs\Pollux\Facades\PostMeta;
8
use GeminiLabs\Pollux\Helper;
9
use GeminiLabs\Pollux\MetaBox\Condition;
10
use GeminiLabs\Pollux\MetaBox\Instruction;
11
use RecursiveArrayIterator;
12
use RecursiveIteratorIterator;
13
14
class MetaBox extends Component
15
{
16
	use Condition;
17
	use Instruction;
18
19
	/**
20
	 * @var array
21
	 */
22
	public $metaboxes = [];
23
24
	/**
25
	 * {@inheritdoc}
26
	 */
27
	public function init()
28
	{
29
		$this->normalize( $this->app->config['meta_boxes'], [
30
			'post_types' => [],
31
		]);
32
33
		add_filter( 'rwmb_show',       [$this, 'show'], 10, 2 );
34
		add_filter( 'rwmb_meta_boxes', [$this, 'register'] );
35
		add_filter( 'rwmb_outer_html', [$this, 'renderField'], 10, 2 );
36
	}
37
38
	/**
39
	 * @return array
40
	 * @filter rwmb_meta_boxes
41
	 */
42
	public function register()
43
	{
44
		if( current_user_can( 'switch_themes' )) {
45
			$this->addInstructions();
46
		}
47
		$metaboxes = func_num_args()
48
			? ( new Helper )->toArray( func_get_arg(0) )
49
			: [];
50
		return array_merge( $metaboxes, $this->metaboxes );
51
	}
52
53
	/**
54
	 * @return string
55
	 * @filter rwmb_outer_html
56
	 */
57
	public function renderField( $html, $field )
58
	{
59
		return $this->validate( $field['condition'] )
60
			? $html
61
			: '';
62
	}
63
64
	/**
65
	 * @return bool
66
	 * @filter rwmb_show
67
	 */
68
	public function show( $bool, array $metabox )
69
	{
70
		if( defined( 'DOING_AJAX' )
71
			|| !isset( $metabox['condition'] )
72
			|| !$this->hasPostType( $metabox )) {
73
			return $bool;
74
		}
75
		return $this->validate( $metabox['condition'] );
76
	}
77
78
	/**
79
	 * @return int
80
	 */
81
	protected function getPostId()
82
	{
83
		if( !( $postId = filter_input( INPUT_GET, 'post' ))) {
84
			$postId = filter_input( INPUT_POST, 'post_ID' );
85
		}
86
		return intval( $postId );
87
	}
88
89
	/**
90
	 * @return array
91
	 */
92
	protected function getPostTypes()
93
	{
94
		return array_unique( iterator_to_array(
95
			new RecursiveIteratorIterator(
96
				new RecursiveArrayIterator( array_column( $this->metaboxes, 'post_types' ))
97
			),
98
			false
99
		));
100
	}
101
102
	/**
103
	 * @return string|array
104
	 */
105
	protected function getValue( $key, $group )
106
	{
107
		return PostMeta::get( $key, [
108
			'id' => $this->getPostId(),
109
		]);
110
	}
111
112
	/**
113
	 * @return bool
114
	 */
115
	protected function hasPostType( array $metabox )
116
	{
117
		if( !isset( $metabox['post_types'] )) {
118
			return true;
119
		}
120
		return in_array( get_post_type( $this->getPostId() ), $metabox['post_types'] );
121
	}
122
123
	/**
124
	 * @return void
125
	 */
126
	protected function normalize( array $metaboxes, array $defaults = [] )
127
	{
128
		foreach( $metaboxes as $id => $metabox ) {
129
			$data = wp_parse_args( $defaults, [
130
				'condition' => [],
131
				'fields' => [],
132
				'id' => $id,
133
				'slug' => $id,
134
			]);
135
			$this->metaboxes[] = $this->setDependencies(
136
				$this->normalizeThis( $metabox, $data, $id )
137
			);
138
		}
139
	}
140
141
	/**
142
	 * @param string $depends
143
	 * @param string $parentId
144
	 * @return string
145
	 */
146
	protected function normalizeDepends( $depends, array $data, $parentId )
147
	{
148
		return is_string( $depends ) && !empty( $depends )
149
			? $this->normalizeId( $depends, $data, $parentId )
150
			: '';
151
	}
152
153
	/**
154
	 * @param string $name
155
	 * @param string $parentId
156
	 * @return string
157
	 */
158
	protected function normalizeFieldName( $name, array $data, $parentId )
159
	{
160
		return $this->normalizeId( $name, $data, $parentId );
161
	}
162
163
	/**
164
	 * @return array
165
	 */
166
	protected function normalizeFields( array $fields, array $data, $parentId )
167
	{
168
		return array_map( function( $id, $field ) use( $parentId ) {
169
			$defaults =  [
170
				'attributes' => [],
171
				'class' => '',
172
				'condition' => [],
173
				'depends' => '',
174
				'field_name' => $id,
175
				'id' => $id,
176
				'slug' => $id,
177
			];
178
			return $this->normalizeThis( $field, $defaults, $parentId );
179
		}, array_keys( $fields ), $fields );
180
	}
181
182
	/**
183
	 * @param string $id
184
	 * @param string $parentId
185
	 * @return string
186
	 */
187
	protected function normalizeId( $id, array $data, $parentId )
188
	{
189
		return apply_filters( 'pollux/prefix', Application::PREFIX ) . $id;
190
	}
191
192
	/**
193
	 * @param mixed $types
194
	 * @return array
195
	 */
196
	protected function normalizePostTypes( $types )
197
	{
198
		return ( new Helper )->toArray( $types );
199
	}
200
201
	/**
202
	 * @return array
203
	 */
204
	protected function setDependencies( array $metabox )
205
	{
206
		$fields = &$metabox['fields'];
207
		$depends = array_column( $fields, 'depends' );
208
		array_walk( $depends, function( $value, $index ) use( &$fields, $metabox ) {
209
			if( empty( $value ))return;
210
			$dependency = array_search( $value, array_column( $fields, 'id' ));
211
			$fields[$index]['attributes']['data-depends'] = $value;
212
			if( !$this->getValue( $fields[$dependency]['slug'], $metabox['slug'] )) {
213
				$fields[$index]['class'] = trim( 'hidden ' . $fields[$index]['class'] );
214
			}
215
		});
216
		return $metabox;
217
	}
218
}
219