Config   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 114
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A registerPage() 0 9 1
A filterSavedSettings() 0 3 1
A __construct() 0 3 1
A registerSetting() 0 3 1
A resetPage() 0 16 4
A id() 0 3 1
A renderPage() 0 14 1
A init() 0 7 2
1
<?php
2
3
namespace GeminiLabs\Pollux\Config;
4
5
use GeminiLabs\Pollux\Application;
6
use GeminiLabs\Pollux\Config\ConfigManager;
7
8
class Config
9
{
10
	const ID = 'config';
11
12
	/**
13
	 * @var string
14
	 */
15
	public $hook;
16
17
	/**
18
	 * @var Application
19
	 */
20
	protected $app;
21
22
	/**
23
	 * @return string
24
	 */
25
	public static function id()
26
	{
27
		return Application::PREFIX . static::ID;
28
	}
29
30
	public function __construct( Application $app )
31
	{
32
		$this->app = $app;
33
	}
34
35
	/**
36
	 * @return void
37
	 */
38
	public function init()
39
	{
40
		if( $this->app->config->disable_config )return;
41
42
		add_action( 'admin_menu',     [$this, 'registerPage'] );
43
		add_action( 'admin_menu',     [$this, 'registerSetting'] );
44
		add_action( 'current_screen', [$this, 'resetPage'] );
45
	}
46
47
	/**
48
	 * @return array
49
	 * @callback register_setting
50
	 */
51
	public function filterSavedSettings( array $settings )
52
	{
53
		return $this->app->make( ConfigManager::class )->setTimestamp( $settings );
54
	}
55
56
	/**
57
	 * @return void
58
	 * @action admin_menu
59
	 */
60
	public function registerPage()
61
	{
62
		$this->hook = add_submenu_page(
0 ignored issues
show
Documentation Bug introduced by
It seems like add_submenu_page('option...y($this, 'renderPage')) can also be of type false. However, the property $hook is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
63
			'options-general.php',
64
			__( 'Pollux', 'pollux' ),
65
			__( 'Pollux', 'pollux' ),
66
			'manage_options',
67
			$this->app->id,
68
			[$this, 'renderPage']
69
		);
70
	}
71
72
	/**
73
	 * @return void
74
	 * @action admin_menu
75
	 */
76
	public function registerSetting()
77
	{
78
		register_setting( static::id(), static::id(), [$this, 'filterSavedSettings'] );
79
	}
80
81
	/**
82
	 * @return void
83
	 * @callback add_submenu_page
84
	 */
85
	public function renderPage()
86
	{
87
		$this->app->render( 'config/script' );
88
		$query = [
89
			'page' => $this->app->id,
90
			'action' => 'reset',
91
			'_wpnonce' => wp_create_nonce( static::id() ),
92
		];
93
		$this->app->render( 'config/index', [
94
			'config' => $this->app->make( ConfigManager::class ),
95
			'heading' => __( 'Pollux Settings', 'pollux' ),
96
			'id' => static::id(),
97
			'reset_url' => esc_url( add_query_arg( $query, admin_url( 'options-general.php' ))),
98
			'has_meta_box' => $this->app->gatekeeper->hasDependency( 'meta-box/meta-box.php' ),
99
		]);
100
	}
101
102
	/**
103
	 * @return void
104
	 * @action pollux/{static::ID}/init
105
	 */
106
	public function resetPage()
107
	{
108
		if( filter_input( INPUT_GET, 'page' ) !== $this->app->id
109
			|| filter_input( INPUT_GET, 'action' ) !== 'reset'
110
		)return;
111
		if( wp_verify_nonce( filter_input( INPUT_GET, '_wpnonce' ), static::id() )) {
112
			delete_option( static::id() );
113
			$this->app->make( ConfigManager::class )->compile( true );
114
			add_settings_error( static::id(), 'reset', __( 'Reset successful.', 'pollux' ), 'updated' );
115
		}
116
		else {
117
			add_settings_error( static::id(), 'failed', __( 'Failed to reset. Please try again.', 'pollux' ));
118
		}
119
		set_transient( 'settings_errors', get_settings_errors(), 30 );
120
		wp_safe_redirect( add_query_arg( 'settings-updated', 'true',  wp_get_referer() ));
121
		exit;
122
	}
123
}
124