1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
<?php
if ( defined( 'ABSPATH' ) && is_admin() && ! class_exists( 'MB_Include_Exclude' ) ) {
class MB_Include_Exclude { /** * Store the current post ID. * * @var string */ protected static $post_id;
/** * Check if meta box is displayed or not. * * @param bool $show Show or hide meta box. * @param array $meta_box Meta Box parameters. * * @return bool */ public static function check( $show, $meta_box ) { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return $show; }
self::$post_id = self::get_current_post_id();
if ( isset( $meta_box['include'] ) ) { $show = self::maybe_exclude_include( 'include', $meta_box ); }
if ( isset( $meta_box['exclude'] ) ) { $show = ! self::maybe_exclude_include( 'exclude', $meta_box ); }
return $show; }
/** * Check if meta box is excluded for current post. * * @param string $type Accept 'include' or 'exclude'. * @param array $meta_box Meta box data. * * @return bool */ protected static function maybe_exclude_include( $type, $meta_box ) { $conditions = $meta_box[ $type ]; $relation = isset( $conditions['relation'] ) && in_array( strtoupper( $conditions['relation'] ), array( 'AND', 'OR' ), true ) ? strtoupper( $conditions['relation'] ) : 'OR';
// Initial value. $value = 'OR' === $relation ? false : true;
// For better loop of checking terms. unset( $conditions['relation'] );
$check_by = array( 'ID', 'parent', 'slug', 'template', 'user_role', 'user_id', 'custom', 'is_child', 'edited_user_role', 'edited_user_id' ); foreach ( $check_by as $by ) { $func = "check_{$by}"; if ( ! isset( $conditions[ $by ] ) || ! method_exists( __CLASS__, $func ) ) { continue; }
$condition = self::$func( $conditions[ $by ], $meta_box ); if ( self::combine( $value, $condition, $relation ) ) { return $value; }
// For better loop of checking terms. unset( $conditions[ $by ] ); }
// By parent taxonomy, including category and post_tag. // Note that we unset all other parameters, so we can safely loop in the condition array. if ( empty( $conditions ) ) { return $value; } // Change 'tag' to correct name 'post_tag'. if ( isset( $conditions['parent_tag'] ) ) { $conditions['parent_post_tag'] = $conditions['parent_tag']; unset( $conditions['parent_tag'] ); } foreach ( $conditions as $key => $terms ) { if ( 0 !== strpos( $key, 'parent_' ) ) { continue; } $taxonomy = substr( $key, 7 ); $condition = self::check_parent_terms( $taxonomy, $terms );
if ( self::combine( $value, $condition, $relation ) ) { return $value; }
unset( $condition[ $key ] ); }
// By taxonomy, including category and post_tag. // Note that we unset all other parameters, so we can safely loop in the condition array. if ( empty( $conditions ) ) { return $value; } // Change 'tag' to correct name 'post_tag'. if ( isset( $conditions['tag'] ) ) { $conditions['post_tag'] = $conditions['tag']; unset( $conditions['tag'] ); }
foreach ( $conditions as $key => $terms ) { $condition = self::check_terms( $key, $terms ); if ( self::combine( $value, $condition, $relation ) ) { return $value; } }
return $value; }
/** * Check if current post has specific ID * * @param array $ids List of post IDs. Can be array or CSV. * * @return bool */ protected static function check_ID( $ids ) { return in_array( self::$post_id, self::csv_to_array( $ids ) ); }
/** * Check if current post has specific parent. * * @param array $ids List post ids. * * @return bool */ protected static function check_parent( $ids ) { $post = get_post( self::$post_id );
return $post && in_array( $post->post_parent, self::csv_to_array( $ids ) ); }
/** * Check if current post has specific slug. * * @param array $slugs List post slugs. * * @return bool */ protected static function check_slug( $slugs ) { $post = get_post( self::$post_id );
return $post && in_array( $post->post_name, self::csv_to_array( $slugs ) ); }
/** * Check if current post has specific template * * @param array $templates List page templates. * * @return bool */ protected static function check_template( $templates ) { $template = get_post_meta( self::$post_id, '_wp_page_template', true );
return in_array( $template, self::csv_to_array( $templates ) ); }
/** * Check if current post has specific term * * @param string $taxonomy Taxonomy name. * @param array $terms Terms. * * @return bool */ protected static function check_terms( $taxonomy, $terms ) { $terms = self::csv_to_array( $terms );
$post_terms = wp_get_post_terms( self::$post_id, $taxonomy ); if ( is_wp_error( $post_terms ) || ! is_array( $post_terms ) || empty( $post_terms ) ) { return false; }
foreach ( $post_terms as $post_term ) { if ( in_array( $post_term->term_id, $terms ) || in_array( $post_term->name, $terms ) || in_array( $post_term->slug, $terms ) ) { return true; } }
return false; }
/** * Check if current post has specific term * * @param string $taxonomy Taxonomy name. * @param array $terms Terms. * * @return bool */ protected static function check_parent_terms( $taxonomy, $terms ) { $terms = self::csv_to_array( $terms );
$post_terms = wp_get_post_terms( self::$post_id, $taxonomy ); if ( is_wp_error( $post_terms ) || ! is_array( $post_terms ) || empty( $post_terms ) ) { return false; }
foreach ( $post_terms as $post_term ) { if ( empty( $post_term->parent ) ) { continue; } $parent = get_term( $post_term->parent, $taxonomy ); if ( in_array( $parent->term_id, $terms ) || in_array( $parent->name, $terms ) || in_array( $parent->slug, $terms ) ) { return true; } }
return false; }
/** * Check by current user role. * * @param array|string $roles List of user roles. Array or CSV. * * @return bool */ protected static function check_user_role( $roles ) { $user = wp_get_current_user(); $roles = array_map( 'strtolower', self::csv_to_array( $roles ) ); $roles = array_intersect( $user->roles, $roles ); return ! empty( $roles ); }
/** * Check by current user ID. * * @param array|string $user_ids List of user IDs. Array or CSV. * * @return bool */ protected static function check_user_id( $user_ids ) { $user_id = get_current_user_id(); return in_array( $user_id, self::csv_to_array( $user_ids ) ); }
/** * Check by edit user role. * * @param array|string $roles List of user roles. Array of CSV. * @return boolean */ protected static function check_edited_user_role( $roles ) { /* * If edit another user's profile, get the edited user instead. * This is required for MB User Meta extension. */ if ( isset( $GLOBALS['pagenow'] ) && 'user-edit.php' === $GLOBALS['pagenow'] ) { // If edit other's profile, check edited user. $user_id = intval( $_REQUEST['user_id'] ); $user = get_userdata( $user_id );
$roles = array_map( 'strtolower', self::csv_to_array( $roles ) ); $roles = array_intersect( $user->roles, $roles );
return ! empty( $roles ); } elseif ( isset( $GLOBALS['pagenow'] ) && 'profile.php' === $GLOBALS['pagenow'] ) { // If edit profile, check current user. return self::check_user_role( $roles ); }
return true; }
/** * Check by edited user id. * * @param array|string $user_ids List of user ids. Array of CSV. * @return boolean */ protected static function check_edited_user_id( $user_ids ) { if ( isset( $GLOBALS['pagenow'] ) && 'user-edit.php' === $GLOBALS['pagenow'] ) { // If edit other's profile, check edited user. $user_id = intval( $_REQUEST['user_id'] );
return in_array( $user_id, self::csv_to_array( $user_ids ) ); } elseif ( isset( $GLOBALS['pagenow'] ) && 'profile.php' === $GLOBALS['pagenow'] ) { // If edit profile, check current user. return self::check_user_id( $user_ids ); }
return true; }
/** * Check by custom function * * @param array $func Callable. * @param array $meta_box Meta box data. * * @return bool */ protected static function check_custom( $func, $meta_box ) { return is_callable( $func ) ? call_user_func( $func, $meta_box ) : false; }
/** * Check the page is child or not * * @param bool $value Boolean value. * * @return bool */ protected static function check_is_child( $value ) { $post = get_post( self::$post_id ); $is_child = $post && $post->post_parent ? true : false;
return $is_child === $value; }
/** * Get current post ID. * * @return int|false Post ID if successful. False on failure. */ protected static function get_current_post_id() { $post_id = isset( $_GET['post'] ) ? $_GET['post'] : ( isset( $_POST['post_ID'] ) ? $_POST['post_ID'] : false ); return is_numeric( $post_id ) ? absint( $post_id ) : false; }
/** * Convert a comma separated string to array. * * @param string $string Comma separated string. * * @return array */ protected static function csv_to_array( $string ) { return is_array( $string ) ? $string : array_filter( array_map( 'trim', explode( ',', $string . ',' ) ) ); }
/** * Combine 2 logical value. * * @param bool $value1 First value. * @param bool $value2 Second value. * @param string $relation 'OR' or 'AND'. * * @return bool Indicator for quick break the check. */ protected static function combine( &$value1, $value2, $relation ) { if ( 'OR' === $relation ) { $value1 = $value1 || $value2;
return $value1; }
$value1 = $value1 && $value2;
return ! $value1; } }
add_filter( 'rwmb_show', array( 'MB_Include_Exclude', 'check' ), 10, 2 ); }
|