Completed
Push — master ( 790d30...8429c0 )
by Benjamin
05:11 queued 35s
created

Settings_Menu   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 36.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 140
rs 10
ccs 17
cts 46
cp 0.3696
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A display_settings_page() 0 7 1
A setting_input_html() 0 10 2
A init_settings() 0 20 1
A settings_page_html() 0 14 2
A __construct() 0 10 1
A setting_section_html() 0 3 1
1
<?php
2
/**
3
 * Register all settings and menus for the plugin.
4
 *
5
 * @package WPSiteMonitor
6
 * @link https://github.com/BWibrew/WP-Site-Monitor/
7
 * @author Benjamin Wibrew <[email protected]>
8
 * @since 1.0.0
9
 */
10
11
namespace WPSiteMonitor;
12
13
/**
14
 * Class Settings_Menu
15
 *
16
 * @package WPSiteMonitor
17
 */
18
class Settings_Menu {
19
20
	/**
21
	 * The loader that's responsible for registering all hooks and filters.
22
	 *
23
	 * @var Hook_Loader
24
	 * @since 1.0.0
25
	 */
26
	protected $loader;
27
28
	/**
29
	 * Settings page slug.
30
	 *
31
	 * @var string
32
	 * @since 1.0.0
33
	 */
34
	protected $settings_page;
35
36
	/**
37
	 * Setting options group name.
38
	 *
39
	 * @var string
40
	 * @since 1.0.0
41
	 */
42
	protected $option_group;
43
44
	/**
45
	 * Initialise the plugin settings and menus.
46
	 *
47
	 * @since 1.0.0
48
	 */
49 1
	public function __construct() {
50 1
		$this->settings_page = 'wp_site_monitor';
51 1
		$this->option_group  = $this->settings_page;
52
53 1
		$this->loader = new Hook_Loader();
54
55 1
		$this->loader->add_action( 'admin_init', $this, 'init_settings' );
56 1
		$this->loader->add_action( 'admin_menu', $this, 'display_settings_page' );
57
58 1
		$this->loader->run();
59 1
	}
60
61
	/**
62
	 * Initialise plugin settings.
63
	 *
64
	 * @since 1.0.0
65
	 */
66
	public function init_settings() {
67
		register_setting( $this->option_group, 'wp_site_monitor_test_option' );
68
69
		add_settings_section(
70
			'wp_site_monitor',
71
			__( 'Available API data', 'wp-site-monitor' ),
72
			array( $this, 'setting_section_html' ),
73
			$this->settings_page
74
		);
75
76
		add_settings_field(
77
			'wp_site_monitor_test_option',
78
			__( 'Test Input', 'wp-site-monitor' ),
79
			array( $this, 'setting_input_html' ),
80
			$this->settings_page,
81
			'wp_site_monitor',
82
			[
83
				'label_for'         => 'wporg_field_pill',
84
				'class'             => 'wporg_row',
85
				'wporg_custom_data' => 'custom',
86
			]
87
		);
88
	}
89
90
	/**
91
	 * Display the admin settings page.
92
	 *
93
	 * @since 1.0.0
94
	 */
95 1
	public function display_settings_page() {
96 1
		add_options_page(
97 1
			__( 'WP Site Monitor Settings', 'wp-site-monitor' ),
98 1
			__( 'WP Site Monitor', 'wp-site-monitor' ),
99 1
			'manage_options',
100 1
			$this->settings_page,
101 1
			array( $this, 'settings_page_html' )
102 1
		);
103 1
	}
104
105
	/**
106
	 * Settings page template.
107
	 *
108
	 * @since 1.0.0
109
	 */
110
	public function settings_page_html() {
111
		if ( ! current_user_can( 'manage_options' ) ) {
112
			return;
113
		}
114
		?>
115
		<div class="wrap">
116
			<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
117
			<form action="options.php" method="POST">
118
				<?php
119
				settings_fields( $this->option_group );
120
				do_settings_sections( $this->settings_page );
121
				submit_button();
122
				?>
123
			</form>
124
		</div>
125
		<?php
126
	}
127
128
	/**
129
	 * Form text.
130
	 *
131
	 * @param array $args Arguments passed into add_settings_section.
132
	 *
133
	 * @since 1.0.0
134
	 */
135
	public function setting_section_html( $args ) {
136
		?>
137
		<p id="<?php echo esc_attr( $args['id'] ); ?>"><?php esc_html_e( 'Use these options to control which extra data is available over the REST API.', 'wp-site-monitor' ); ?></p>
138
		<?php
139
	}
140
141
	/**
142
	 * Form inputs.
143
	 *
144
	 * @param array $args Element parameters.
145
	 *
146
	 * @since 1.0.0
147
	 */
148
	public function setting_input_html( $args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

148
	public function setting_input_html( /** @scrutinizer ignore-unused */ $args ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
		?>
150
		<input type="text"
151
			name="wp_site_monitor_test_option"
152
			id="wp_site_monitor_test_option"
153
			value="<?php echo get_option( 'wp_site_monitor_test_option' ) ? esc_attr( get_option( 'wp_site_monitor_test_option' ) ) : ''; ?>" />
154
155
		<p class="description">
156
			<?php esc_html_e( 'This is a test setting', 'wp-site-monitor' ); ?>
157
		</p>
158
		<?php
159
	}
160
}
161