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