1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux\MetaBox; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Pollux\Application; |
6
|
|
|
use GeminiLabs\Pollux\Component; |
7
|
|
|
use GeminiLabs\Pollux\Helper; |
8
|
|
|
use GeminiLabs\Pollux\MetaBox\Condition; |
9
|
|
|
use GeminiLabs\Pollux\MetaBox\Instruction; |
10
|
|
|
use GeminiLabs\Pollux\PostMeta; |
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(); |
30
|
|
|
|
31
|
|
|
add_filter( 'rwmb_show', [$this, 'show'], 10, 2 ); |
32
|
|
|
add_filter( 'rwmb_meta_boxes', [$this, 'register'] ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return array |
37
|
|
|
* @filter rwmb_meta_boxes |
38
|
|
|
*/ |
39
|
|
|
public function register() |
40
|
|
|
{ |
41
|
|
|
if( current_user_can( 'switch_themes' )) { |
42
|
|
|
$this->addInstructions(); |
43
|
|
|
} |
44
|
|
|
$metaboxes = func_num_args() |
45
|
|
|
? ( new Helper )->toArray( func_get_arg(0) ) |
46
|
|
|
: []; |
47
|
|
|
return array_merge( $metaboxes, $this->metaboxes ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return bool |
52
|
|
|
* @filter rwmb_show |
53
|
|
|
*/ |
54
|
|
|
public function show( $bool, array $metabox ) |
55
|
|
|
{ |
56
|
|
|
if( defined( 'DOING_AJAX' ) |
57
|
|
|
|| !isset( $metabox['condition'] ) |
58
|
|
|
|| !$this->hasPostType( $metabox )) { |
59
|
|
|
return $bool; |
60
|
|
|
} |
61
|
|
|
return $this->validate( $metabox['condition'] ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
|
|
protected function getPostId() |
68
|
|
|
{ |
69
|
|
|
if( !( $postId = filter_input( INPUT_GET, 'post' ))) { |
70
|
|
|
$postId = filter_input( INPUT_POST, 'post_ID' ); |
71
|
|
|
} |
72
|
|
|
return intval( $postId ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
protected function getPostTypes() |
79
|
|
|
{ |
80
|
|
|
return array_unique( iterator_to_array( |
81
|
|
|
new RecursiveIteratorIterator( |
82
|
|
|
new RecursiveArrayIterator( array_column( $this->metaboxes, 'post_types' )) |
83
|
|
|
), |
84
|
|
|
false |
85
|
|
|
)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string|array |
90
|
|
|
*/ |
91
|
|
|
protected function getValue( $key, $group ) |
92
|
|
|
{ |
93
|
|
|
return ( new PostMeta )->get( $key, [ |
94
|
|
|
'id' => $this->getPostId(), |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
protected function hasPostType( array $metabox ) |
102
|
|
|
{ |
103
|
|
|
if( !isset( $metabox['post_types'] )) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
return in_array( get_post_type( $this->getPostId() ), $metabox['post_types'] ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
protected function normalize() |
113
|
|
|
{ |
114
|
|
|
foreach( $this->app->config['meta_boxes'] as $id => $metabox ) { |
115
|
|
|
$defaults = [ |
116
|
|
|
'condition' => [], |
117
|
|
|
'fields' => [], |
118
|
|
|
'id' => $id, |
119
|
|
|
'post_types' => [], |
120
|
|
|
'slug' => $id, |
121
|
|
|
]; |
122
|
|
|
$this->metaboxes[] = $this->setDependencies( |
123
|
|
|
$this->normalizeThis( $metabox, $defaults, $id ) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $depends |
130
|
|
|
* @param string $parentId |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
protected function normalizeDepends( $depends, array $data, $parentId ) |
134
|
|
|
{ |
135
|
|
|
return is_string( $depends ) && !empty( $depends ) |
136
|
|
|
? $this->normalizeId( $depends, $data, $parentId ) |
137
|
|
|
: ''; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
protected function normalizeFields( array $fields, array $data, $parentId ) |
144
|
|
|
{ |
145
|
|
|
return array_map( function( $id, $field ) use( $parentId ) { |
146
|
|
|
$defaults = [ |
147
|
|
|
'attributes' => [], |
148
|
|
|
'class' => '', |
149
|
|
|
// 'condition' => [], |
|
|
|
|
150
|
|
|
'depends' => '', |
151
|
|
|
'id' => $id, |
152
|
|
|
'field_name' => '', |
153
|
|
|
'slug' => $id, |
154
|
|
|
]; |
155
|
|
|
return $this->normalizeThis( $field, $defaults, $parentId ); |
156
|
|
|
}, array_keys( $fields ), $fields ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $id |
161
|
|
|
* @param string $parentId |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
protected function normalizeId( $id, array $data, $parentId ) |
165
|
|
|
{ |
166
|
|
|
return apply_filters( 'pollux/prefix', Application::PREFIX ) . $id; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param mixed $types |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
protected function normalizePostTypes( $types ) |
174
|
|
|
{ |
175
|
|
|
return ( new Helper )->toArray( $types ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
protected function setDependencies( array $metabox ) |
182
|
|
|
{ |
183
|
|
|
$fields = &$metabox['fields']; |
184
|
|
|
$depends = array_column( $fields, 'depends' ); |
185
|
|
|
array_walk( $depends, function( $value, $index ) use( &$fields, $metabox ) { |
186
|
|
|
if( empty( $value ))return; |
187
|
|
|
$dependency = array_search( $value, array_column( $fields, 'id' )); |
188
|
|
|
$fields[$index]['attributes']['data-depends'] = $value; |
189
|
|
|
if( !$this->getValue( $fields[$dependency]['slug'], $metabox['slug'] )) { |
190
|
|
|
$fields[$index]['class'] = trim( 'hidden ' . $fields[$index]['class'] ); |
191
|
|
|
} |
192
|
|
|
}); |
193
|
|
|
return $metabox; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.