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