Passed
Push — develop ( 9b346b...48f539 )
by Paul
12:10
created

MetaBox   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 246
rs 9.2
c 0
b 0
f 0
wmc 34
lcom 2
cbo 5

18 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 10 1
A action() 0 4 1
A filter() 0 4 1
A register() 0 10 3
A renderField() 0 6 2
A show() 0 9 4
A getPostId() 0 7 2
A getPostTypes() 0 9 1
A getValue() 0 6 1
A hasPostType() 0 7 2
A normalize() 0 15 2
A normalizeDepends() 0 6 3
A normalizeFieldName() 0 4 1
A normalizeFields() 0 15 1
A normalizeId() 0 4 1
A normalizePostTypes() 0 4 1
A normalizeValidation() 0 11 4
A setDependencies() 0 14 3
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
	 * @param string $name
45
	 * @param mixed ...$args
46
	 * @return void
47
	 */
48
	public function action( $name, ...$args )
49
	{
50
		return do_action_ref_array( sprintf( 'pollux/%s/%s', static::ID, $name ), $args );
51
	}
52
53
	/**
54
	 * @param string $name
55
	 * @param mixed ...$args
56
	 * @return mixed
57
	 */
58
	public function filter( $name, ...$args )
59
	{
60
		return apply_filters_ref_array( sprintf( 'pollux/%s/%s', static::ID, $name ), $args );
61
	}
62
63
	/**
64
	 * @return array
65
	 * @filter rwmb_meta_boxes
66
	 */
67
	public function register()
68
	{
69
		if( current_user_can( 'switch_themes' )) {
70
			$this->addInstructions();
71
		}
72
		$metaboxes = func_num_args()
73
			? ( new Helper )->toArray( func_get_arg(0) )
74
			: [];
75
		return array_merge( $metaboxes, $this->metaboxes );
76
	}
77
78
	/**
79
	 * @return string
80
	 * @filter rwmb_outer_html
81
	 */
82
	public function renderField( $html, $field )
83
	{
84
		return $this->validate( $field['condition'] )
85
			? $html
86
			: '';
87
	}
88
89
	/**
90
	 * @return bool
91
	 * @filter rwmb_show
92
	 */
93
	public function show( $bool, array $metabox )
94
	{
95
		if( defined( 'DOING_AJAX' )
96
			|| !isset( $metabox['condition'] )
97
			|| !$this->hasPostType( $metabox )) {
98
			return $bool;
99
		}
100
		return $this->validate( $metabox['condition'] );
101
	}
102
103
	/**
104
	 * @return int
105
	 */
106
	protected function getPostId()
107
	{
108
		if( !( $postId = filter_input( INPUT_GET, 'post' ))) {
109
			$postId = filter_input( INPUT_POST, 'post_ID' );
110
		}
111
		return intval( $postId );
112
	}
113
114
	/**
115
	 * @return array
116
	 */
117
	protected function getPostTypes()
118
	{
119
		return array_unique( iterator_to_array(
120
			new RecursiveIteratorIterator(
121
				new RecursiveArrayIterator( array_column( $this->metaboxes, 'post_types' ))
122
			),
123
			false
124
		));
125
	}
126
127
	/**
128
	 * @return string|array
129
	 */
130
	protected function getValue( $key, $group )
131
	{
132
		return PostMeta::get( $key, [
133
			'id' => $this->getPostId(),
134
		]);
135
	}
136
137
	/**
138
	 * @return bool
139
	 */
140
	protected function hasPostType( array $metabox )
141
	{
142
		if( !isset( $metabox['post_types'] )) {
143
			return true;
144
		}
145
		return in_array( get_post_type( $this->getPostId() ), $metabox['post_types'] );
146
	}
147
148
	/**
149
	 * @return void
150
	 */
151
	protected function normalize( array $metaboxes, array $defaults = [] )
152
	{
153
		foreach( $metaboxes as $id => $metabox ) {
154
			$data = wp_parse_args( $defaults, [
155
				'condition' => [],
156
				'fields' => [],
157
				'id' => $id,
158
				'slug' => $id,
159
				'validation' => [],
160
			]);
161
			$this->metaboxes[] = $this->setDependencies(
162
				$this->normalizeThis( $metabox, $data, $id )
163
			);
164
		}
165
	}
166
167
	/**
168
	 * @param string $depends
169
	 * @param string $parentId
170
	 * @return string
171
	 */
172
	protected function normalizeDepends( $depends, array $data, $parentId )
173
	{
174
		return is_string( $depends ) && !empty( $depends )
175
			? $this->normalizeId( $depends, $data, $parentId )
176
			: '';
177
	}
178
179
	/**
180
	 * @param string $name
181
	 * @param string $parentId
182
	 * @return string
183
	 */
184
	protected function normalizeFieldName( $name, array $data, $parentId )
185
	{
186
		return $this->normalizeId( $name, $data, $parentId );
187
	}
188
189
	/**
190
	 * @return array
191
	 */
192
	protected function normalizeFields( array $fields, array $data, $parentId )
193
	{
194
		return array_map( function( $id, $field ) use( $parentId ) {
195
			$defaults =  [
196
				'attributes' => [],
197
				'class' => '',
198
				'condition' => [],
199
				'depends' => '',
200
				'field_name' => $id,
201
				'id' => $id,
202
				'slug' => $id,
203
			];
204
			return $this->normalizeThis( $field, $defaults, $parentId );
205
		}, array_keys( $fields ), $fields );
206
	}
207
208
	/**
209
	 * @param string $id
210
	 * @param string $parentId
211
	 * @return string
212
	 */
213
	protected function normalizeId( $id, array $data, $parentId )
214
	{
215
		return Application::prefix() . $id;
216
	}
217
218
	/**
219
	 * @param mixed $types
220
	 * @return array
221
	 */
222
	protected function normalizePostTypes( $types )
223
	{
224
		return ( new Helper )->toArray( $types );
225
	}
226
227
	/**
228
	 * @return array
229
	 */
230
	protected function normalizeValidation( array $validation, array $data, $parentId )
231
	{
232
		foreach( ['messages', 'rules'] as $key ) {
233
			if( empty( $validation[$key] ))continue;
234
			foreach( $validation[$key] as $id => $value ) {
235
				$validation[$key][$this->normalizeFieldName( $id, ['slug' => $id], $parentId )] = $value;
236
				unset( $validation[$key][$id] );
237
			}
238
		}
239
		return $validation;
240
	}
241
242
	/**
243
	 * @return array
244
	 */
245
	protected function setDependencies( array $metabox )
246
	{
247
		$fields = &$metabox['fields'];
248
		$depends = array_column( $fields, 'depends' );
249
		array_walk( $depends, function( $value, $index ) use( &$fields, $metabox ) {
250
			if( empty( $value ))return;
251
			$dependency = array_search( $value, array_column( $fields, 'id' ));
252
			$fields[$index]['attributes']['data-depends'] = $value;
253
			if( !$this->getValue( $fields[$dependency]['slug'], $metabox['slug'] )) {
254
				$fields[$index]['class'] = trim( 'hidden ' . $fields[$index]['class'] );
255
			}
256
		});
257
		return $metabox;
258
	}
259
}
260