Passed
Branch master (b44fdc)
by Benjamin
02:56
created

Settings_Menu::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
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
	 * Initialise the plugin settings and menus.
30
	 *
31
	 * @since 1.0.0
32
	 */
33 6
	public function __construct() {
34 6
		$this->loader = new Hook_Loader();
35
36 6
		$this->loader->add_action( 'admin_init', $this, 'init_settings' );
37 6
		$this->loader->add_action( 'admin_menu', $this, 'display_settings_page' );
38
39 6
		$this->loader->run();
40 6
	}
41
42
	/**
43
	 * Initialise plugin settings.
44
	 *
45
	 * @since 1.0.0
46
	 */
47 4
	public function init_settings() {
48 4
		register_setting( WP_Site_Monitor::OPTION_GROUP, WP_Site_Monitor::OPTION_NAME );
49
50 4
		add_settings_section(
51 4
			WP_Site_Monitor::OPTION_NAME . '_section',
52 4
			__( 'Enable/Disable WP Site Monitor', 'wp-site-monitor' ),
53 4
			array( $this, 'setting_section_html' ),
54
			WP_Site_Monitor::OPTION_GROUP
55 4
		);
56
57 4
		add_settings_field(
58 4
			WP_Site_Monitor::OPTION_NAME,
59 4
			__( 'Enable WP Site Monitor', 'wp-site-monitor' ),
60 4
			array( $this, 'setting_input_html' ),
61 4
			WP_Site_Monitor::OPTION_GROUP,
62 4
			WP_Site_Monitor::OPTION_NAME . '_section'
63 4
		);
64 4
	}
65
66
	/**
67
	 * Display the admin settings page.
68
	 *
69
	 * @since 1.0.0
70
	 */
71 1
	public function display_settings_page() {
72 1
		add_options_page(
73 1
			__( 'WP Site Monitor Settings', 'wp-site-monitor' ),
74 1
			__( 'WP Site Monitor', 'wp-site-monitor' ),
75 1
			'manage_options',
76 1
			WP_Site_Monitor::OPTION_GROUP,
77 1
			array( $this, 'settings_page_html' )
78 1
		);
79 1
	}
80
81
	/**
82
	 * Settings page template.
83
	 *
84
	 * @since 1.0.0
85
	 */
86
	public function settings_page_html() {
87
		if ( ! current_user_can( 'manage_options' ) ) {
88
			return;
89
		}
90
		?>
91
		<div class="wrap">
92
			<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
93
			<form action="options.php" method="POST">
94
				<?php
95
				settings_fields( WP_Site_Monitor::OPTION_GROUP );
96
				do_settings_sections( WP_Site_Monitor::OPTION_GROUP );
97
				submit_button();
98
				?>
99
			</form>
100
		</div>
101
		<?php
102
	}
103
104
	/**
105
	 * Form text.
106
	 *
107
	 * @param array $args Arguments passed into add_settings_section.
108
	 *
109
	 * @since 1.0.0
110
	 */
111
	public function setting_section_html( $args ) {
112
		?>
113
		<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>
114
		<?php
115
	}
116
117
	/**
118
	 * Form inputs.
119
	 *
120
	 * @since 1.0.0
121
	 */
122
	public function setting_input_html() {
123
		?>
124
		<input type="checkbox"
125
			name="<?php esc_attr( WP_Site_Monitor::OPTION_NAME ); ?>"
126
			id=""<?php checked( get_option( WP_Site_Monitor::OPTION_NAME ), 1 ); ?> value="1">
127
128
		<p class="description">
129
			<?php esc_html_e( 'This checkbox enables/disables all plugin functionality.', 'wp-site-monitor' ); ?>
130
		</p>
131
		<?php
132
	}
133
}
134