Passed
Push — develop ( 37b65a...9b346b )
by Paul
03:39
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 string
21
	 */
22
	const ID = 'metaboxes';
23
24
	/**
25
	 * @var array
26
	 */
27
	public $metaboxes = [];
28
29
	/**
30
	 * {@inheritdoc}
31
	 */
32
	public function init()
33
	{
34
		$this->normalize( $this->app->config[static::ID], [
35
			'post_types' => [],
36
		]);
37
38
		add_filter( 'rwmb_show',       [$this, 'show'], 10, 2 );
39
		add_filter( 'rwmb_meta_boxes', [$this, 'register'] );
40
		add_filter( 'rwmb_outer_html', [$this, 'renderField'], 10, 2 );
41
	}
42
43
	/**
44
	 * @return mixed
45
	 */
46
	public function filter()
47
	{
48
		$args = func_get_args();
49
		$hook = sprintf( 'pollux/%s/%s', strtolower(( new Helper )->getClassname( $this )), array_shift( $args ));
50
		return apply_filters_ref_array( $hook, $args );
51
	}
52
53
	/**
54
	 * @return array
55
	 * @filter rwmb_meta_boxes
56
	 */
57
	public function register()
58
	{
59
		if( current_user_can( 'switch_themes' )) {
60
			$this->addInstructions();
61
		}
62
		$metaboxes = func_num_args()
63
			? ( new Helper )->toArray( func_get_arg(0) )
64
			: [];
65
		return array_merge( $metaboxes, $this->metaboxes );
66
	}
67
68
	/**
69
	 * @return string
70
	 * @filter rwmb_outer_html
71
	 */
72
	public function renderField( $html, $field )
73
	{
74
		return $this->validate( $field['condition'] )
75
			? $html
76
			: '';
77
	}
78
79
	/**
80
	 * @return bool
81
	 * @filter rwmb_show
82
	 */
83
	public function show( $bool, array $metabox )
84
	{
85
		if( defined( 'DOING_AJAX' )
86
			|| !isset( $metabox['condition'] )
87
			|| !$this->hasPostType( $metabox )) {
88
			return $bool;
89
		}
90
		return $this->validate( $metabox['condition'] );
91
	}
92
93
	/**
94
	 * @return int
95
	 */
96
	protected function getPostId()
97
	{
98
		if( !( $postId = filter_input( INPUT_GET, 'post' ))) {
99
			$postId = filter_input( INPUT_POST, 'post_ID' );
100
		}
101
		return intval( $postId );
102
	}
103
104
	/**
105
	 * @return array
106
	 */
107
	protected function getPostTypes()
108
	{
109
		return array_unique( iterator_to_array(
110
			new RecursiveIteratorIterator(
111
				new RecursiveArrayIterator( array_column( $this->metaboxes, 'post_types' ))
112
			),
113
			false
114
		));
115
	}
116
117
	/**
118
	 * @return string|array
119
	 */
120
	protected function getValue( $key, $group )
121
	{
122
		return PostMeta::get( $key, [
123
			'id' => $this->getPostId(),
124
		]);
125
	}
126
127
	/**
128
	 * @return bool
129
	 */
130
	protected function hasPostType( array $metabox )
131
	{
132
		if( !isset( $metabox['post_types'] )) {
133
			return true;
134
		}
135
		return in_array( get_post_type( $this->getPostId() ), $metabox['post_types'] );
136
	}
137
138
	/**
139
	 * @return void
140
	 */
141
	protected function normalize( array $metaboxes, array $defaults = [] )
142
	{
143
		foreach( $metaboxes as $id => $metabox ) {
144
			$data = wp_parse_args( $defaults, [
145
				'condition' => [],
146
				'fields' => [],
147
				'id' => $id,
148
				'slug' => $id,
149
				'validation' => [],
150
			]);
151
			$this->metaboxes[] = $this->setDependencies(
152
				$this->normalizeThis( $metabox, $data, $id )
153
			);
154
		}
155
	}
156
157
	/**
158
	 * @param string $depends
159
	 * @param string $parentId
160
	 * @return string
161
	 */
162
	protected function normalizeDepends( $depends, array $data, $parentId )
163
	{
164
		return is_string( $depends ) && !empty( $depends )
165
			? $this->normalizeId( $depends, $data, $parentId )
166
			: '';
167
	}
168
169
	/**
170
	 * @param string $name
171
	 * @param string $parentId
172
	 * @return string
173
	 */
174
	protected function normalizeFieldName( $name, array $data, $parentId )
175
	{
176
		return $this->normalizeId( $name, $data, $parentId );
177
	}
178
179
	/**
180
	 * @return array
181
	 */
182
	protected function normalizeFields( array $fields, array $data, $parentId )
183
	{
184
		return array_map( function( $id, $field ) use( $parentId ) {
185
			$defaults =  [
186
				'attributes' => [],
187
				'class' => '',
188
				'condition' => [],
189
				'depends' => '',
190
				'field_name' => $id,
191
				'id' => $id,
192
				'slug' => $id,
193
			];
194
			return $this->normalizeThis( $field, $defaults, $parentId );
195
		}, array_keys( $fields ), $fields );
196
	}
197
198
	/**
199
	 * @param string $id
200
	 * @param string $parentId
201
	 * @return string
202
	 */
203
	protected function normalizeId( $id, array $data, $parentId )
204
	{
205
		return Application::prefix() . $id;
206
	}
207
208
	/**
209
	 * @param mixed $types
210
	 * @return array
211
	 */
212
	protected function normalizePostTypes( $types )
213
	{
214
		return ( new Helper )->toArray( $types );
215
	}
216
217
	/**
218
	 * @return array
219
	 */
220
	protected function normalizeValidation( array $validation, array $data, $parentId )
1 ignored issue
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
221
	{
222
		foreach( ['messages', 'rules'] as $key ) {
223
			if( empty( $validation[$key] ))continue;
224
			foreach( $validation[$key] as $id => $value ) {
225
				$validation[$key][$this->normalizeFieldName( $id, ['slug' => $id], $parentId )] = $value;
226
				unset( $validation[$key][$id] );
227
			}
228
		}
229
		return $validation;
230
	}
231
232
	/**
233
	 * @return array
234
	 */
235
	protected function setDependencies( array $metabox )
236
	{
237
		$fields = &$metabox['fields'];
238
		$depends = array_column( $fields, 'depends' );
239
		array_walk( $depends, function( $value, $index ) use( &$fields, $metabox ) {
240
			if( empty( $value ))return;
241
			$dependency = array_search( $value, array_column( $fields, 'id' ));
242
			$fields[$index]['attributes']['data-depends'] = $value;
243
			if( !$this->getValue( $fields[$dependency]['slug'], $metabox['slug'] )) {
244
				$fields[$index]['class'] = trim( 'hidden ' . $fields[$index]['class'] );
245
			}
246
		});
247
		return $metabox;
248
	}
249
}
250