Completed
Push — master ( 475e64...ab1972 )
by Fulvio
03:41
created

Settings::stack_field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * WP PHP Console Plugin Settings Class
4
 *
5
 * @link    https://github.com/unfulvio/wp-php-console
6
 * @since   1.0.0
7
 * @package WP_PHP_Console
8
 */
9
namespace WP_PHP_Console;
10
11
defined( 'ABSPATH' ) or exit;
1 ignored issue
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
12
13
/**
14
 * WP PHP Console settings class.
15
 *
16
 * @since   1.5.0
17
 * @package WP_PHP_Console
18
 */
19
class Settings {
20
21
22
	/**
23
	 * This plugin's settings page slug.
24
	 *
25
	 * @since  1.5.0
26
	 * @access private
27
	 * @var    string
28
	 */
29
	private $page = '';
30
31
32
	/**
33
	 * This plugin's option name.
34
	 *
35
	 * @since  1.5.0
36
	 * @access private
37
	 * @var    string
38
	 */
39
	private $option = '';
40
41
	/**
42
	 * This plugin's settings options.
43
	 *
44
	 * @since  1.5.0
45
	 * @access private
46
	 * @var    array $options Array of this plugin settings options.
47
	 */
48
	private $options = array();
49
50
51
	/**
52
	 * Register settings and admin menu.
53
	 *
54
	 * @since 1.5.0
55
	 * @param array $options Plugin settings options.
56
	 */
57
	public function __construct( array $options ) {
58
59
		$this->page    = sanitize_title( strtolower( Plugin::NAME ) );
60
		$this->option  = str_replace( '-', '_', $this->page );
61
		$this->options = $options;
62
63
		add_action( 'admin_menu', array( $this, 'register_settings_page' ) );
64
65
	}
66
67
68
	/**
69
	 * Plugin Settings menu.
70
	 *
71
	 * @since 1.5.0
72
	 */
73
	public function register_settings_page() {
74
75
		add_options_page(
76
			__( 'WP PHP Console', 'wp-php-console' ),
77
			__( 'WP PHP Console', 'wp-php-console' ),
78
			'manage_options',
79
			$this->page,
80
			array( $this, 'settings_page' )
81
		);
82
83
		add_action( 'admin_init', array( $this, 'register_settings'  ) );
84
85
	}
86
87
88
	/**
89
	 * Register plugin settings.
90
	 *
91
	 * @since 1.5.0
92
	 */
93
	public function register_settings() {
94
95
		register_setting(
96
			$this->option,
97
			$this->option,
98
			array( $this, 'sanitize_field' )
99
		);
100
101
		add_settings_section(
102
			$this->option,
103
			__( 'Settings', 'wp-php-console' ),
104
			array( $this, 'settings_info' ),
105
			$this->page
106
		);
107
108
		$settings_fields = array(
109
			'password' => array(
110
				 'label'    => esc_html__( 'Password',           'wp-php-console' ),
111
				 'callback' => array( $this, 'password_field' ),
112
			),
113
			'ssl'      => array(
114
				 'label'    => esc_html__( 'Allow only on SSL',  'wp-php-console' ),
115
				 'callback' => array( $this, 'ssl_field' ),
116
			),
117
			'ip' => array(
118
				 'label'    => esc_html__( 'Allowed IP Masks',   'wp-php-console' ),
119
				 'callback' => array( $this, 'ip_field' ),
120
			),
121
			'register' => array(
122
				 'label'    => esc_html__( 'Register PC Class ', 'wp-php-console' ),
123
				 'callback' => array( $this, 'register_field' ),
124
			),
125
			'stack'    => array(
126
				 'label'    => esc_html__( 'Show Call Stack',    'wp-php-console' ),
127
				 'callback' => array( $this, 'stack_field' ),
128
			),
129
			'short'    => array(
130
				 'label'    => esc_html__( 'Short Path Names',   'wp-php-console' ),
131
				 'callback' => array( $this, 'short_field' ),
132
			),
133
		);
134
135
		foreach ( $settings_fields as $key => $field ) {
136
			add_settings_field(
137
				$this->page . '['. $key . ']',
138
				$field['label'],
139
				$field['callback'],
140
				$this->page,
141
				$this->option
142
			);
143
		}
144
145
	}
146
147
148
	/**
149
	 * Settings page additional info.
150
	 * Prints more details on the plugin settings page.
151
	 *
152
	 * @since 1.5.0
153
	 */
154
	public function settings_info() {
155
156
		?>
157
		<p><?php
158
			/* translators: %s refers to 'PHP Console' Chrome extension, will print download link for the Chrome extension */
159
			printf( _x( 'This plugin allows you to use %s within your WordPress installation for testing, debugging and development purposes.', 'PHP Console, the Chrome Extension', 'wp-php-console' ), '<a href="https://github.com/barbushin/php-console" target="_blank">PHP Console</a>' ); ?>
160
			<br><?php esc_html_e( 'Usage instructions:', 'wp-php-console' ); ?>
161
		</p>
162
		<ol>
163
			<?php
164
165
			$instructions = array(
166
				/* translators: Install PHP Console extension for Google Chrome download link */
167
				sprintf( _x( 'Make sure you have downloaded and installed %s.', 'PHP Console, the Chrome Extension', 'wp-php-console' ),
168
					'<a target="_blank" href="https://chrome.google.com/webstore/detail/php-console/nfhmhhlpfleoednkpnnnkolmclajemef">PHP Console extension for Google Chrome</a>'
169
				),
170
				esc_html__( 'Set a password for the eval terminal in the options below and hit "Save Changes".', 'wp-php-console' ),
171
				esc_html__( 'Reload any page of your installation and click on the key icon in your Chrome browser address bar, enter your password and access the terminal.', 'wp-php-console' ),
172
				esc_html__( 'From the eval terminal you can execute any PHP or WordPress specific function, including functions from your plugins and active theme.', 'wp-php-console' ),
173
				/* translators: %1$s - PHP code snippet example, %2$s - Chrome javascript console shortcut */
174
				sprintf( __( 'In your PHP code, you can call PHP Console debug statements like %1$s to display PHP variables in the browser\'s JavaScript-console (e.g. %2$s) and optionally filter selected tags through the browser\'s Remote PHP Eval Terminal screen\'s "Ignore Debug options".', 'wp-php-console' ),
175
					'<code>debug(&#36;var, &#36;tag)</code>',
176
					'<code>CTRL+SHIFT+J</code>'
177
				),
178
			);
179
180
			foreach ( $instructions as $list_item ) {
181
				echo '<li>' . $list_item  . '</li>';
182
			}
183
184
			?>
185
		</ol>
186
		<hr>
187
		<?php
188
189
	}
190
191
192
	/**
193
	 * Settings Page Password field.
194
	 *
195
	 * @since 1.5.0
196
	 */
197
	public function password_field() {
198
199
		?>
200
		<input type="password" id="wp-php-console-password" name="wp_php_console[password]" value="<?php echo esc_attr( $this->options['password'] ); ?>">
201
		<label for="wp-php-console-password"><?php esc_html_e( 'Required', 'wp-php-console' ); ?></label><br>
202
		<p class="description"><?php esc_html_e( 'The password for the eval terminal. If empty, the plugin will not work.', 'wp-php-console' ); ?></p>
203
		<?php
204
205
	}
206
207
208
	/**
209
	 * Settings Page SSL option field.
210
	 *
211
	 * @since 1.5.0
212
	 */
213 View Code Duplication
	public function ssl_field() {
1 ignored issue
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...
214
215
		?>
216
		<input type="checkbox" id="wp-php-console-ssl" name="wp_php_console[ssl]" value="1" <?php checked( (bool) $this->options['ssl'] ); ?> />
217
		<label for="wp-php-console-ssl"><?php esc_html_e( 'Yes', 'wp-php-console' ); ?></label><br>
218
		<p class="description"><?php esc_html_e( 'Tick this option if you want the eval terminal to work only on a SSL connection.', 'wp-php-console' ); ?></p>
219
		<?php
220
221
	}
222
223
224
	/**
225
	 * Settings page IP Range field.
226
	 *
227
	 * @since 1.5.0
228
	 */
229
	public function ip_field() {
230
231
		?>
232
		<input type="text" class="regular-text" id="wp-php-console-ip" name="wp_php_console[ip]" value="<?php echo esc_attr( $this->options['ip'] ); ?>" />
233
		<label for="wp-php-console-ip"><?php esc_html_e( 'IP addresses (optional)', 'wp-php-console' ); ?></label><br>
234
		<p class="description"><?php esc_html_e( 'You may specify any of the following, to give access to specific IPs to the eval terminal:', 'wp-php-console' ); ?><br>
235
			<ol>
236
				<li><small><?php
237
					/* translators: Placeholders: %1$s - a single IP address, %2$s link to Varying Vagrant Vagrants repo */
238
					printf( __( 'An IP address (for example %1$s, %2$s default IP address).' ),
239
						'<code>192.168.50.4</code>',
240
						'<a href="https://github.com/Varying-Vagrant-Vagrants/VVV">Varying Vagrant Vagrants</a>'
241
					); ?></small></li>
242
				<li><small><?php
243
					/* translators: Placeholders: %1$s a range of IP addresses, %2$s - comma separated IP addresses */
244
					printf( __( 'A range of addresses (%1$s) or multiple addresses, comma separated (%2$s).', 'wp-php-console' ),
245
						'<code>192.168.*.*</code>',
246
						'<code>192.168.10.25,192.168.10.28</code>'
247
					); ?></small></li>
248
			</ol>
249
		</p>
250
		<?php
251
252
	}
253
254
255
	/**
256
	 * Settings page Register PC Class field.
257
	 *
258
	 * @since 1.5.0
259
	 */
260 View Code Duplication
	public function register_field() {
1 ignored issue
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...
261
262
		?>
263
		<input type="checkbox" id="wp-php-console-register" name="wp_php_console[register]" value="1" <?php checked( (bool) $this->options['register'] ); ?> />
264
		<label for="wp-php-console-register"><?php esc_html_e( 'Yes', 'wp-php-console' ); ?></label><br>
265
		<p class="description"><?php
266
			esc_html_e( 'Tick to register PC class in the global namespace.', 'wp-php-console' );
267
			echo '<br>';
268
			/* translators: Placeholders: PHP code snippets examples */
269
			printf( __( 'Allows to write %1$s or %2$s instructions in PHP to inspect %3$s in the JavaScript console.', 'wp-php-console' ),
270
				'<code>PC::debug(&#36;var, &#36;tag)</code>',
271
				'<code>PC::magic_tag(&#36;var)</code>',
272
				'<code>&#36;var</code>'
273
			); ?></p>
274
		<?php
275
276
	}
277
278
279
	/**
280
	 * Settings page Show Call Stack field.
281
	 *
282
	 * @since 1.5.0
283
	 */
284 View Code Duplication
	public function stack_field() {
1 ignored issue
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...
285
286
		?>
287
		<input type="checkbox" id="wp-php-console-stack" name="wp_php_console[stack]" value="1" <?php checked( (bool) $this->options['stack'] ); ?> />
288
		<label for="wp-php-console-stack"><?php esc_html_e( 'Yes', 'wp-php-console' ); ?></label><br />
289
		<p class="description"><?php esc_html_e( 'Tick to see the full call stack when PHP Console writes to the browser JavaScript console.', 'wp-php-console' ); ?></p>
290
		<?php
291
292
	}
293
294
295
	/**
296
	 * Settings page Show Short Paths field.
297
	 *
298
	 * @since 1.5.0
299
	 */
300 View Code Duplication
	public function short_field() {
1 ignored issue
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...
301
302
		?>
303
		<input type="checkbox" id="wp-php-console-short" name="wp_php_console[short]" value="1" <?php checked( (bool) $this->options['short'] ); ?> />
304
		<label for="wp-php-console-short"><?php esc_html_e( 'Yes', 'wp-php-console' ); ?></label><br>
305
		<p class="description"><?php
306
			esc_html_e( 'Tick to shorten the length of PHP Console error sources and traces paths in browser JavaScript console for better readability.', 'wp-php-console' );
307
			echo '<br>';
308
			/* translators: %1$s - long server path, %2$s shortened server path */
309
			printf( __( 'Paths like %1$s will be displayed as %2$s', 'wp-php-console' ),
310
				'<code>/server/path/to/document/root/WP/wp-admin/admin.php:31</code>',
311
				'<code>/WP/wp-admin/admin.php:31</code>'
312
			); ?></p>
313
		<?php
314
315
	}
316
317
318
	/**
319
	 * Sanitize user input in settings page.
320
	 *
321
	 * @since  1.5.0
322
	 * @param  array $field user input
323
	 * @return array sanitized input
324
	 */
325
	public function sanitize_field( $field ) {
326
327
		$input = wp_parse_args( $field, array(
328
			'ip'       => '',
329
			'password' => '',
330
			'register' => false,
331
			'short'    => false,
332
			'ssl'      => false,
333
			'stack'    => false,
334
		) );
335
336
		$sanitized_input = array(
337
			'ip'       => sanitize_text_field( $input['ip'] ),
338
			'password' => sanitize_text_field( $input['password'] ),
339
			'register' => ! empty( $input['register'] ),
340
			'short'    => ! empty( $input['short'] ),
341
			'ssl'      => ! empty( $input['ssl'] ),
342
			'stack'    => ! empty( $input['stack'] ),
343
		);
344
345
		return $sanitized_input;
346
	}
347
348
349
	/**
350
	 * Settings page.
351
	 *
352
	 * @since 1.5.0
353
	 */
354
	public function settings_page() {
355
356
		?>
357
		<div class="wrap">
358
			<h2><?php esc_html_e( 'WP PHP Console', 'wp-php-console' ); ?></h2>
359
			<hr />
360
			<form method="post" action="options.php">
361
				<?php
362
363
				settings_fields( $this->option );
364
365
				do_settings_sections( $this->page );
366
367
				submit_button();
368
369
				?>
370
			</form>
371
		</div>
372
		<?php
373
374
	}
375
376
377
}
378