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