Completed
Push — develop ( 790c8e...eab4f1 )
by Paul
04:44
created

RWMetaBox::_normalize_field_meta()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 2
dl 0
loc 13
ccs 0
cts 13
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
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
	 * @param array $field
51
	 * @return array
52
	 */
53
	public function _normalize_field( $field )
54
	{
55
		if( !empty( $field['multiple'] ) && $field['id'] == substr( $field['field_name'], 0, -2 )) {
56
			$parts = array_filter( explode( '-', $field['id'] ));
57
			$first = array_shift( $parts );
58
			$field['field_name'] = array_reduce( $parts, function( $carry, $part ) {
59
				return sprintf( '%s[%s]', $carry, $part );
60
			}, $first ) . '[]';
61
		}
62
		return $field;
63
	}
64
65
	/**
66
	 * @param mixed $meta
67
	 * @return array
68
	 */
69
	public function _normalize_field_meta( $meta, array $field )
70
	{
71
		if( !empty( $meta ) && is_array( $meta )) {
72
			return $meta;
73
		}
74
		if( $field['clone'] ) {
75
			return [''];
76
		}
77
		if( $field['multiple'] ) {
78
			return [];
79
		}
80
		return $meta;
81
	}
82
83
	/**
84
	 * @return void
85
	 * @action pollux/archives/init
86
	 * @action pollux/settings/init
87
	 */
88
	public function add_meta_boxes()
89
	{
90
		add_meta_box(
91
			$this->meta_box['id'],
92
			$this->meta_box['title'],
93
			[$this, 'show'],
94
			null,
95
			$this->meta_box['context'],
96
			$this->meta_box['priority']
97
		);
98
	}
99
100
	/**
101
	 * @return bool
102
	 */
103
	public function is_edit_screen( $screen = null )
104
	{
105
		return get_current_screen()->id == $this->pollux_caller->hook;
106
	}
107
108
	/**
109
	 * @return bool
110
	 */
111
	public function is_saved()
112
	{
113
		foreach( array_column( $this->fields, 'slug' ) as $field ) {
114
			if( !is_null( $this->pollux_caller->getMetaValue( $field, null, $this->meta_box['slug'] ))) {
115
				return true;
116
			}
117
		}
118
		return false;
119
	}
120
121
	/**
122
	 * @param array $metabox
123
	 * @return array
124
	 */
125
	public static function normalize( $metabox )
126
	{
127
		unset( $metabox['post_types'] );
128
		return wp_parse_args( $metabox, ['slug' => ''] );
129
	}
130
}
131