Passed
Push — master ( 51995c...0bccda )
by Paul
06:07 queued 02:50
created

Config::filterSavedSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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(
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
		]);
99
	}
100
101
	/**
102
	 * @return void
103
	 * @action pollux/{static::ID}/init
104
	 */
105 View Code Duplication
	public function resetPage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
	{
107
		if( filter_input( INPUT_GET, 'page' ) !== $this->app->id
108
			|| filter_input( INPUT_GET, 'action' ) !== 'reset'
109
		)return;
110
		if( wp_verify_nonce( filter_input( INPUT_GET, '_wpnonce' ), static::id() )) {
111
			delete_option( static::id() );
112
			add_settings_error( static::id(), 'reset', __( 'Reset successful.', 'pollux' ), 'updated' );
113
		}
114
		else {
115
			add_settings_error( static::id(), 'failed', __( 'Failed to reset. Please try again.', 'pollux' ));
116
		}
117
		set_transient( 'settings_errors', get_settings_errors(), 30 );
118
		wp_safe_redirect( add_query_arg( 'settings-updated', 'true',  wp_get_referer() ));
119
		exit;
120
	}
121
}
122