Passed
Push — master ( 4d016b...b6e428 )
by Benjamin
02:33
created

Settings_Menu   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
dl 0
loc 132
ccs 30
cts 44
cp 0.6818
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A display_settings_page() 0 7 1
A setting_input_html() 0 9 1
A init_settings() 0 16 1
A settings_page_html() 0 14 2
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 2
	public function __construct() {
50 2
		$this->settings_page = 'wp_site_monitor';
51 2
		$this->option_group  = $this->settings_page;
52
53 2
		$this->loader = new Hook_Loader();
54
55 2
		$this->loader->add_action( 'admin_init', $this, 'init_settings' );
56 2
		$this->loader->add_action( 'admin_menu', $this, 'display_settings_page' );
57
58 2
		$this->loader->run();
59 2
	}
60
61
	/**
62
	 * Initialise plugin settings.
63
	 *
64
	 * @since 1.0.0
65
	 */
66 1
	public function init_settings() {
67 1
		register_setting( $this->option_group, 'wp_site_monitor_enable' );
68
69 1
		add_settings_section(
70 1
			'wp_site_monitor_enable_section',
71 1
			__( 'Enable/Disable WP Site Monitor', 'wp-site-monitor' ),
72 1
			array( $this, 'setting_section_html' ),
73 1
			$this->settings_page
74
		);
75
76 1
		add_settings_field(
77 1
			'wp_site_monitor_enable',
78 1
			__( 'Enable WP Site Monitor', 'wp-site-monitor' ),
79 1
			array( $this, 'setting_input_html' ),
80 1
			$this->settings_page,
81 1
			'wp_site_monitor_enable_section'
82
		);
83 1
	}
84
85
	/**
86
	 * Display the admin settings page.
87
	 *
88
	 * @since 1.0.0
89
	 */
90 1
	public function display_settings_page() {
91 1
		add_options_page(
92 1
			__( 'WP Site Monitor Settings', 'wp-site-monitor' ),
93 1
			__( 'WP Site Monitor', 'wp-site-monitor' ),
94 1
			'manage_options',
95 1
			$this->settings_page,
96 1
			array( $this, 'settings_page_html' )
97
		);
98 1
	}
99
100
	/**
101
	 * Settings page template.
102
	 *
103
	 * @since 1.0.0
104
	 */
105
	public function settings_page_html() {
106
		if ( ! current_user_can( 'manage_options' ) ) {
107
			return;
108
		}
109
		?>
110
		<div class="wrap">
111
			<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
112
			<form action="options.php" method="POST">
113
				<?php
114
				settings_fields( $this->option_group );
115
				do_settings_sections( $this->settings_page );
116
				submit_button();
117
				?>
118
			</form>
119
		</div>
120
		<?php
121
	}
122
123
	/**
124
	 * Form text.
125
	 *
126
	 * @param array $args Arguments passed into add_settings_section.
127
	 *
128
	 * @since 1.0.0
129
	 */
130
	public function setting_section_html( $args ) {
131
		?>
132
		<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>
0 ignored issues
show
Bug introduced by
Are you sure the usage of esc_attr($args['id']) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to esc_attr() has too many arguments starting with $args['id']. ( Ignorable by Annotation )

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

132
		<p id="<?php echo /** @scrutinizer ignore-call */ 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>

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
133
		<?php
134
	}
135
136
	/**
137
	 * Form inputs.
138
	 *
139
	 * @since 1.0.0
140
	 */
141
	public function setting_input_html() {
142
		?>
143
		<input type="checkbox"
144
			name="wp_site_monitor_enable"
145
			id=""<?php checked( get_option( 'wp_site_monitor_enable' ), 1 ); ?> value="1">
146
147
		<p class="description">
148
			<?php esc_html_e( 'This checkbox enables/disables all plugin functionality.', 'wp-site-monitor' ); ?>
149
		</p>
150
		<?php
151
	}
152
}
153