Passed
Push — master ( d406ab...3befbe )
by Benjamin
02:31
created

Settings_Menu   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 76.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 140
rs 10
c 1
b 0
f 0
ccs 33
cts 43
cp 0.7674
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
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 __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 setting option name.
22
	 *
23
	 * @var string
24
	 * @since 1.0.0
25
	 */
26
	const OPTION_NAME = 'wp_site_monitor_enable';
27
28
	/**
29
	 * The loader that's responsible for registering all hooks and filters.
30
	 *
31
	 * @var Hook_Loader
32
	 * @since 1.0.0
33
	 */
34
	protected $loader;
35
36
	/**
37
	 * Settings page slug.
38
	 *
39
	 * @var string
40
	 * @since 1.0.0
41
	 */
42
	protected $settings_page;
43
44
	/**
45
	 * Setting options group name.
46
	 *
47
	 * @var string
48
	 * @since 1.0.0
49
	 */
50
	protected $option_group;
51
52
	/**
53
	 * Initialise the plugin settings and menus.
54
	 *
55
	 * @since 1.0.0
56
	 */
57 4
	public function __construct() {
58 4
		$this->settings_page = 'wp_site_monitor';
59 4
		$this->option_group  = $this->settings_page;
60
61 4
		$this->loader = new Hook_Loader();
62
63 4
		$this->loader->add_action( 'admin_init', $this, 'init_settings' );
64 4
		$this->loader->add_action( 'admin_menu', $this, 'display_settings_page' );
65
66 4
		$this->loader->run();
67 4
	}
68
69
	/**
70
	 * Initialise plugin settings.
71
	 *
72
	 * @since 1.0.0
73
	 */
74 3
	public function init_settings() {
75 3
		register_setting( $this->option_group, self::OPTION_NAME );
76
77 3
		add_settings_section(
78 3
			self::OPTION_NAME . '_section',
79 3
			__( 'Enable/Disable WP Site Monitor', 'wp-site-monitor' ),
80 3
			array( $this, 'setting_section_html' ),
81 3
			$this->settings_page
82 3
		);
83
84 3
		add_settings_field(
85 3
			self::OPTION_NAME,
86 3
			__( 'Enable WP Site Monitor', 'wp-site-monitor' ),
87 3
			array( $this, 'setting_input_html' ),
88 3
			$this->settings_page,
89 3
			self::OPTION_NAME . '_section'
90 3
		);
91 3
	}
92
93
	/**
94
	 * Display the admin settings page.
95
	 *
96
	 * @since 1.0.0
97
	 */
98 1
	public function display_settings_page() {
99 1
		add_options_page(
100 1
			__( 'WP Site Monitor Settings', 'wp-site-monitor' ),
101 1
			__( 'WP Site Monitor', 'wp-site-monitor' ),
102 1
			'manage_options',
103 1
			$this->settings_page,
104 1
			array( $this, 'settings_page_html' )
105 1
		);
106 1
	}
107
108
	/**
109
	 * Settings page template.
110
	 *
111
	 * @since 1.0.0
112
	 */
113
	public function settings_page_html() {
114
		if ( ! current_user_can( 'manage_options' ) ) {
115
			return;
116
		}
117
		?>
118
		<div class="wrap">
119
			<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
120
			<form action="options.php" method="POST">
121
				<?php
122
				settings_fields( $this->option_group );
123
				do_settings_sections( $this->settings_page );
124
				submit_button();
125
				?>
126
			</form>
127
		</div>
128
		<?php
129
	}
130
131
	/**
132
	 * Form text.
133
	 *
134
	 * @param array $args Arguments passed into add_settings_section.
135
	 *
136
	 * @since 1.0.0
137
	 */
138
	public function setting_section_html( $args ) {
139
		?>
140
		<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
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

140
		<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...
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...
141
		<?php
142
	}
143
144
	/**
145
	 * Form inputs.
146
	 *
147
	 * @since 1.0.0
148
	 */
149
	public function setting_input_html() {
150
		?>
151
		<input type="checkbox"
152
			name="<?php esc_attr( self::OPTION_NAME ); ?>"
0 ignored issues
show
Unused Code introduced by
The call to esc_attr() has too many arguments starting with self::OPTION_NAME. ( Ignorable by Annotation )

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

152
			name="<?php /** @scrutinizer ignore-call */ esc_attr( self::OPTION_NAME ); ?>"

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...
153
			id=""<?php checked( get_option( self::OPTION_NAME ), 1 ); ?> value="1">
154
155
		<p class="description">
156
			<?php esc_html_e( 'This checkbox enables/disables all plugin functionality.', 'wp-site-monitor' ); ?>
157
		</p>
158
		<?php
159
	}
160
}
161