Passed
Push — master ( 06feff...e4a90e )
by Paul
02:34
created

Settings::isVisible()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 2
nop 2
dl 7
loc 7
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
use GeminiLabs\Pollux\Application;
6
use GeminiLabs\Pollux\MetaBox;
7
use GeminiLabs\Pollux\SettingsMetaBox;
8
9
class Settings extends MetaBox
10
{
11
	const CONDITIONS = [
12
		'hook', 'is_plugin_active', 'is_plugin_inactive',
13
	];
14
15
	/**
16
	 * @var string
17
	 */
18
	CONST ID = 'pollux-settings';
19
20
	/**
21
	 * @var string
22
	 */
23
	public $hook;
24
25
	/**
26
	 * {@inheritdoc}
27
	 */
28
	public function init()
29
	{
30
		if( !is_plugin_active( 'meta-box/meta-box.php' ))return;
31
32
		$this->normalize();
33
34
		add_action( 'admin_menu',                             [$this, 'addPage'] );
35
		add_action( 'pollux/settings/init',                   [$this, 'addSubmitMetaBox'] );
36
		add_filter( 'pollux/settings/instruction',            [$this, 'filterInstruction'], 10, 3 );
37
		add_action( 'current_screen',                         [$this, 'register'] );
38
		add_action( 'admin_footer-toplevel_page_' . self::ID, [$this, 'renderFooterScript'] );
39
40
		// add_filter( 'pollux/settings/save',        [$this, 'save'] );
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
	}
42
43
	/**
44
	 * @return void
45
	 */
46
	public function addPage()
47
	{
48
		$this->hook = call_user_func_array( 'add_menu_page', apply_filters( 'pollux/settings/page', [
49
			__( 'Site Settings', 'pollux' ),
50
			__( 'Site Settings', 'pollux' ),
51
			'edit_theme_options',
52
			self::ID,
53
			[$this, 'renderPage'],
54
			'dashicons-screenoptions',
55
			1313
56
		]));
57
	}
58
59
	/**
60
	 * @return void
61
	 */
62
	public function addSubmitMetaBox()
63
	{
64
		call_user_func_array( 'add_meta_box', apply_filters( 'pollux/settings/metabox/submit', [
65
			'submitdiv',
66
			__( 'Save Settings', 'pollux' ),
67
			[ $this, 'renderSubmitMetaBox'],
68
			$this->hook,
69
			'side',
70
			'high',
71
		]));
72
	}
73
74
	/**
75
	 * @param string $instruction
76
	 * @param string $fieldId
77
	 * @param string $metaboxId
78
	 * @return string
79
	 */
80
	public function filterInstruction( $instruction, $fieldId, $metaboxId )
81
	{
82
		return sprintf( "SiteMeta::get('%s', '%s');", $metaboxId, $fieldId );
83
	}
84
85
	/**
86
	 * @return void
87
	 */
88
	public function register( $metaboxes = [] )
89
	{
90
		if( get_current_screen()->id != $this->hook )return;
91
		foreach( parent::register() as $metabox ) {
92
			new SettingsMetaBox( $metabox );
93
		}
94
95
		do_action( 'pollux/settings/init' );
96
	}
97
98
	/**
99
	 * @return void
100
	 */
101
	public function renderFooterScript()
102
	{
103
		$this->render( 'settings/script', [
104
			'confirm' => __( 'Are you sure want to do this?', 'pollux' ),
105
			'hook' => $this->hook,
106
			'id' => self::ID,
107
		]);
108
	}
109
110
	/**
111
	 * @return void
112
	 */
113
	public function renderPage()
114
	{
115
		// add_screen_option( 'layout_columns', ['max' => 2, 'default' => 2] );
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
116
		$this->render( 'settings/index', [
117
			'columns' => 2,//get_current_screen()->get_columns(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
			'id' => self::ID,
119
			'title' => __( 'Site Settings', 'pollux' ),
120
		]);
121
	}
122
123
	/**
124
	 * @return void
125
	 */
126
	public function renderSubmitMetaBox()
127
	{
128
		$query = [
129
			'_wpnonce' => wp_create_nonce( sprintf( '%s-reset', self::ID )),
130
			'action' => 'reset_settings',
131
			'page' => self::ID,
132
		];
133
		$this->render( 'settings/submit', [
134
			'reset' => __( 'Reset Settings', 'pollux' ),
135
			'reset_url' => esc_url( add_query_arg( $query, admin_url( 'admin.php' ))),
136
			'submit' => get_submit_button( __( 'Save', 'pollux' ), 'primary', 'submit', false ),
137
		]);
138
	}
139
140
	/**
141
	 * @return array
142
	 */
143
	protected function getInstructions()
144
	{
145
		return array_filter( $this->metaboxes, function( $metabox ) {
146
			return $this->verifyMetaBoxCondition( $metabox['condition'] );
147
		});
148
	}
149
150
	/**
151
	 * @return array
152
	 */
153
	protected function getPostTypes()
154
	{
155
		return [];
156
	}
157
158
	/**
159
	 * {@inheritdoc}
160
	 */
161
	protected function normalize()
162
	{
163
		$this->metaboxes = [];
164
		foreach( $this->app->config['settings'] as $id => $metabox ) {
165
			unset( $metabox['post_types'], $metabox['pages'] );
166
			$defaults = [
167
				'condition' => [],
168
				'fields' => [],
169
				'id' => $id,
170
				'slug' => $id,
171
			];
172
			$this->metaboxes[] = $this->normalizeThis( $metabox, $defaults, $id );
173
		}
174
	}
175
176
	/**
177
	 * @param string $name
178
	 * @param string $parentId
179
	 * @return string
180
	 */
181
	protected function normalizeFieldName( $name, array $data, $parentId )
182
	{
183
		if( !empty( $name )) {
184
			return $name;
185
		}
186
		$name = str_replace( sprintf( '%s-%s-', self::ID, $parentId ), '', $data['id'] );
187
		return sprintf( '%s[%s][%s]', self::ID, $parentId, $name );
188
	}
189
190
	/**
191
	 * @param string $id
192
	 * @param string $parentId
193
	 * @return string
194
	 */
195
	protected function normalizeId( $id, array $data, $parentId )
196
	{
197
		return $parentId == $id
198
			? sprintf( '%s-%s', self::ID, $id )
199
			: sprintf( '%s-%s-%s', self::ID, $parentId, $id );
200
	}
201
}
202