RWMetaBox   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 48
c 1
b 1
f 0
dl 0
loc 120
ccs 0
cts 77
cp 0
rs 10
wmc 20

8 Methods

Rating   Name   Duplication   Size   Complexity  
A _normalize_field() 0 10 3
A _get_field_meta() 0 10 5
A add_meta_boxes() 0 9 1
A __construct() 0 15 1
A is_edit_screen() 0 3 1
A _normalize_field_meta() 0 12 5
A is_saved() 0 8 3
A normalize() 0 5 1
1
<?php
2
3
namespace GeminiLabs\Pollux\Settings;
4
5
use GeminiLabs\Pollux\Helper;
6
use RW_Meta_Box;
7
use RWMB_Helpers_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( Helper::toArray( $meta )) || empty( $field['slug'] )) {
40
			return $meta;
41
		}
42
		$meta = call_user_func( [RWMB_Helpers_Field::get_class( $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;
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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
		$metabox = parent::normalize( $metabox );
127
		$metabox['post_types'] = [];
128
		return wp_parse_args( $metabox, ['slug' => ''] );
129
	}
130
}
131