1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux\Settings; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Pollux\Settings\Settings; |
6
|
|
|
use GeminiLabs\Pollux\SiteMeta; |
7
|
|
|
use RW_Meta_Box; |
8
|
|
|
use RWMB_Field; |
9
|
|
|
|
10
|
|
|
class RWMetaBox extends RW_Meta_Box |
11
|
|
|
{ |
12
|
|
|
public function __construct( $metabox ) |
13
|
|
|
{ |
14
|
|
|
parent::__construct( $metabox ); |
15
|
|
|
$this->meta_box = static::normalize( $this->meta_box ); |
16
|
|
|
|
17
|
|
|
remove_action( 'add_meta_boxes', [$this, 'add_meta_boxes'] ); |
18
|
|
|
remove_action( 'save_post_post', [$this, 'save_post'] ); |
19
|
|
|
|
20
|
|
|
add_action( 'pollux/settings/init', [$this, 'add_meta_boxes'] ); |
21
|
|
|
add_filter( 'rwmb_field_meta', [$this, '_get_field_meta'], 10, 3 ); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param mixed $meta |
26
|
|
|
* @param bool $meta |
27
|
|
|
* @return mixed |
28
|
|
|
* @filter rwmb_field_meta |
29
|
|
|
*/ |
30
|
|
|
public function _get_field_meta( $meta, array $field, $saved ) |
31
|
|
|
{ |
32
|
|
|
if( !$this->is_edit_screen() || !empty( $meta ) || empty( $field['slug'] )) { |
33
|
|
|
return $meta; |
34
|
|
|
} |
35
|
|
|
$meta = call_user_func( [RWMB_Field::get_class_name( $field ), 'esc_meta'], ( $saved |
36
|
|
|
? (new SiteMeta)->get( $this->meta_box['slug'], $field['slug'], $meta ) |
37
|
|
|
: $field['std'] |
38
|
|
|
)); |
39
|
|
|
return $this->_normalize_field_meta( $meta, $field ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param mixed $meta |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function _normalize_field_meta( $meta, array $field ) |
47
|
|
|
{ |
48
|
|
|
if( !empty( $meta ) && is_array( $meta )) { |
49
|
|
|
return $meta; |
50
|
|
|
} |
51
|
|
|
if( $field['clone'] ) { |
52
|
|
|
return ['']; |
53
|
|
|
} |
54
|
|
|
if( $field['multiple'] ) { |
55
|
|
|
return []; |
56
|
|
|
} |
57
|
|
|
return $meta; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return void |
62
|
|
|
* @action pollux/settings/init |
63
|
|
|
*/ |
64
|
|
|
public function add_meta_boxes() |
65
|
|
|
{ |
66
|
|
|
add_meta_box( |
67
|
|
|
$this->meta_box['id'], |
68
|
|
|
$this->meta_box['title'], |
69
|
|
|
[$this, 'show'], |
70
|
|
|
null, |
71
|
|
|
$this->meta_box['context'], |
72
|
|
|
$this->meta_box['priority'] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function is_edit_screen( $screen = null ) |
80
|
|
|
{ |
81
|
|
|
return get_current_screen()->id == sprintf( 'toplevel_page_%s', apply_filters( 'pollux/settings/option', Settings::ID )); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function is_saved() |
88
|
|
|
{ |
89
|
|
|
foreach( array_column( $this->fields, 'slug' ) as $field ) { |
90
|
|
|
if( !is_null( (new SiteMeta)->get( $this->meta_box['slug'], $field, null ))) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $metabox |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public static function normalize( $metabox ) |
102
|
|
|
{ |
103
|
|
|
unset( $metabox['post_types'] ); |
104
|
|
|
return wp_parse_args( $metabox, ['slug' => ''] ); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|