1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* WP PHP Console Plugin Core Class |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/nekojira/wp-php-console |
6
|
|
|
* @since 1.0.0 |
7
|
|
|
* @package WP_PHP_Console |
8
|
|
|
*/ |
9
|
|
|
namespace WP_PHP_Console; |
10
|
|
|
|
11
|
|
|
use PhpConsole; |
12
|
|
|
|
13
|
|
|
if ( ! defined( 'WPINC' ) ) { |
14
|
|
|
exit; // Exit if accessed directly |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* WP PHP Console main class. |
19
|
|
|
* |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
* @package WP_PHP_Console |
22
|
|
|
*/ |
23
|
|
|
class Plugin { |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The plugin name. |
28
|
|
|
* |
29
|
|
|
* @since 1.4.0 |
30
|
|
|
* @access public |
31
|
|
|
* @var string $plugin_name The name of this plugin. |
32
|
|
|
*/ |
33
|
|
|
public $plugin_name = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The unique identifier of this plugin. |
37
|
|
|
* |
38
|
|
|
* @since 1.0.0 |
39
|
|
|
* @access public |
40
|
|
|
* @var string $plugin_slug The string used to uniquely identify this plugin. |
41
|
|
|
*/ |
42
|
|
|
public $plugin_slug = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The current version of the plugin. |
46
|
|
|
* |
47
|
|
|
* @since 1.0.0 |
48
|
|
|
* @access public |
49
|
|
|
* @var string $version The current version of the plugin. |
50
|
|
|
*/ |
51
|
|
|
public $version = ''; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This plugin's settings options. |
55
|
|
|
* |
56
|
|
|
* @since 1.0.0 |
57
|
|
|
* @access protected |
58
|
|
|
* @var array $options Array of this plugin settings options. |
59
|
|
|
*/ |
60
|
|
|
protected $options = array(); |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Init. |
65
|
|
|
* |
66
|
|
|
* @since 1.0.0 |
67
|
|
|
*/ |
68
|
|
|
public function __construct() { |
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->plugin_name = 'WP PHP Console'; |
71
|
|
|
$this->plugin_slug = 'wp-php-console'; |
72
|
|
|
$this->version = '1.4.1'; |
73
|
|
|
$this->options = $this->get_options(); |
74
|
|
|
|
75
|
|
|
// Bail out if PHP Console can't be found |
76
|
|
|
if ( ! class_exists( 'PhpConsole\Connector' ) ) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// By default PHP Console uses PhpConsole\Storage\Session for postponed responses, |
81
|
|
|
// so all temporary data will be stored in $_SESSION. |
82
|
|
|
// But there is some problem with frameworks like WordPress that override PHP session handler. |
83
|
|
|
// PHP Console has alternative storage drivers for this - we will write to a temporary file: |
84
|
|
|
$phpcdir = dirname( __FILE__ ) . '/tmp'; |
85
|
|
|
$make_dir = wp_mkdir_p( $phpcdir ); |
86
|
|
|
|
87
|
|
|
if ( $make_dir === true ) { |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$storage = new PhpConsole\Storage\File( $phpcdir . '/' . md5( __FILE__ ) . '_pc.data' ); |
91
|
|
|
PhpConsole\Connector::setPostponeStorage( $storage ); |
92
|
|
|
} catch( \Exception $e ) { |
93
|
|
|
// TODO $storage is under DOCUMENT_ROOT - it's insecure but did not find another solution in WP |
94
|
|
|
// $this->print_notice_exception( $e ); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Perform PHP Console initialisation required asap for other code to be able to output to the JavaScript console |
100
|
|
|
$connector = PhpConsole\Connector::getInstance(); |
101
|
|
|
|
102
|
|
|
// Apply 'register' option to PHP Console |
103
|
|
|
if ( ! empty( $this->options['register'] ) && ! class_exists( 'PC', false ) ) { |
104
|
|
|
// Only if PC not registered yet |
105
|
|
|
try { |
106
|
|
|
PhpConsole\Helper::register(); |
107
|
|
|
} catch( \Exception $e ) { |
108
|
|
|
$this->print_notice_exception( $e ); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Apply 'stack' option to PHP Console |
113
|
|
|
if ( ! empty( $this->options['stack'] ) ) { |
114
|
|
|
$connector->getDebugDispatcher()->detectTraceAndSource = true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Apply 'short' option to PHP Console |
118
|
|
|
if ( ! empty( $this->options['short'] ) ) { |
119
|
|
|
try { |
120
|
|
|
$connector->setSourcesBasePath( $_SERVER['DOCUMENT_ROOT'] ); |
121
|
|
|
} catch ( \Exception $e ) { |
122
|
|
|
$this->print_notice_exception( $e ); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Translation |
127
|
|
|
add_action( 'plugins_loaded', array( $this, 'set_locale' ) ); |
128
|
|
|
// Admin menu |
129
|
|
|
add_action( 'admin_menu', array( $this, 'register_settings_page' ) ); |
130
|
|
|
// Delay further PHP Console initialisation to have more context during Remote PHP execution |
131
|
|
|
add_action( 'wp_loaded', array( $this, 'init' ) ); |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get WP PHP Console settings options. |
138
|
|
|
* |
139
|
|
|
* @since 1.4.1 |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
protected function get_options() { |
143
|
|
|
return get_option( 'wp_php_console', array() ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Prints an exception message as WordPress admin notice |
149
|
|
|
* |
150
|
|
|
* @since 1.4.1 |
151
|
|
|
* @param \Exception $e Exception object |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
protected function print_notice_exception( \Exception $e ) { |
155
|
|
|
|
156
|
|
|
add_action( 'admin_notices', function() use ( $e ) { |
157
|
|
|
|
158
|
|
|
?> |
159
|
|
|
<div class="error"> |
160
|
|
|
<p><?php echo $this->plugin_name . ': ' . $e->getMessage(); ?></p> |
161
|
|
|
</div> |
162
|
|
|
<?php |
163
|
|
|
|
164
|
|
|
} ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set plugin text domain. |
170
|
|
|
* |
171
|
|
|
* @since 1.0.0 |
172
|
|
|
*/ |
173
|
|
|
public function set_locale() { |
174
|
|
|
|
175
|
|
|
load_plugin_textdomain( |
176
|
|
|
$this->plugin_slug, |
177
|
|
|
false, |
178
|
|
|
dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Plugin Settings menu. |
184
|
|
|
* |
185
|
|
|
* @since 1.0.0 |
186
|
|
|
*/ |
187
|
|
|
public function register_settings_page() { |
188
|
|
|
|
189
|
|
|
add_options_page( |
190
|
|
|
__( 'WP PHP Console', 'wp-php-console' ), |
191
|
|
|
__( 'WP PHP Console', 'wp-php-console' ), |
192
|
|
|
'manage_options', |
193
|
|
|
$this->plugin_slug, |
194
|
|
|
array( $this, 'settings_page' ) |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
add_action( 'admin_init', array( $this, 'register_settings' ) ); |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Register plugin settings. |
204
|
|
|
* |
205
|
|
|
* @since 1.0.0 |
206
|
|
|
*/ |
207
|
|
|
public function register_settings() { |
208
|
|
|
|
209
|
|
|
register_setting( |
210
|
|
|
'wp_php_console', |
211
|
|
|
'wp_php_console', |
212
|
|
|
array( $this, 'sanitize_field' ) |
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
add_settings_section( |
216
|
|
|
'wp_php_console', |
217
|
|
|
__( 'Settings', 'wp-php-console' ), |
218
|
|
|
array( $this, 'settings_info' ), |
219
|
|
|
$this->plugin_slug |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
add_settings_field( |
223
|
|
|
'password', |
224
|
|
|
__( 'Password', 'wp-php-console' ), |
225
|
|
|
array( $this, 'password_field' ), |
226
|
|
|
$this->plugin_slug, |
227
|
|
|
'wp_php_console' |
228
|
|
|
); |
229
|
|
|
|
230
|
|
|
add_settings_field( |
231
|
|
|
'ssl', |
232
|
|
|
__( 'Allow only on SSL', 'wp-php-console' ), |
233
|
|
|
array( $this, 'ssl_field' ), |
234
|
|
|
$this->plugin_slug, |
235
|
|
|
'wp_php_console' |
236
|
|
|
); |
237
|
|
|
|
238
|
|
|
add_settings_field( |
239
|
|
|
'ip', |
240
|
|
|
__( 'Allowed IP Masks', 'wp-php-console' ), |
241
|
|
|
array( $this, 'ip_field' ), |
242
|
|
|
$this->plugin_slug, |
243
|
|
|
'wp_php_console' |
244
|
|
|
); |
245
|
|
|
|
246
|
|
|
add_settings_field( |
247
|
|
|
'register', |
248
|
|
|
__( 'Register PC Class ', 'wp-php-console' ), |
249
|
|
|
array( $this, 'register_field' ), |
250
|
|
|
$this->plugin_slug, |
251
|
|
|
'wp_php_console' |
252
|
|
|
); |
253
|
|
|
|
254
|
|
|
add_settings_field( |
255
|
|
|
'stack', |
256
|
|
|
__( 'Show Call Stack', 'wp-php-console' ), |
257
|
|
|
array( $this, 'stack_field' ), |
258
|
|
|
$this->plugin_slug, |
259
|
|
|
'wp_php_console' |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
add_settings_field( |
263
|
|
|
'short', |
264
|
|
|
__( 'Short Path Names', 'wp-php-console' ), |
265
|
|
|
array( $this, 'short_field' ), |
266
|
|
|
$this->plugin_slug, |
267
|
|
|
'wp_php_console' |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Settings Page Password field. |
275
|
|
|
* |
276
|
|
|
* @since 1.0.0 |
277
|
|
|
*/ |
278
|
|
|
public function password_field() { |
279
|
|
|
|
280
|
|
|
printf ( |
281
|
|
|
'<input type="password" id="wp-php-console-password" name="wp_php_console[password]" value="%s" />', |
282
|
|
|
isset( $this->options['password'] ) ? esc_attr( $this->options['password'] ) : '' |
283
|
|
|
); |
284
|
|
|
echo '<label for="wp-php-console-ip">' . esc_html__( 'Required', 'wp-php-console' ) . '</label>'; |
285
|
|
|
echo '<br />'; |
286
|
|
|
echo '<small class="description">' . esc_html__( 'The password for the eval terminal. If empty, the plugin will not work.', 'wp-php-console' ) . '</small>'; |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Settings Page SSL option field. |
293
|
|
|
* |
294
|
|
|
* @since 1.0.0 |
295
|
|
|
*/ |
296
|
|
|
public function ssl_field() { |
297
|
|
|
|
298
|
|
|
$ssl = isset( $this->options['ssl'] ) ? esc_attr( $this->options['ssl']) : ''; |
299
|
|
|
|
300
|
|
|
printf ( |
301
|
|
|
'<input type="checkbox" id="wp-php-console-ssl" name="wp_php_console[ssl]" value="1" %s /> ', |
302
|
|
|
$ssl ? 'checked="checked"' : '' |
303
|
|
|
); |
304
|
|
|
echo '<label for="wp-php-console-ssl">' . esc_html__( 'Yes', 'wp-php-console' ) . '</label>'; |
305
|
|
|
echo '<br />'; |
306
|
|
|
echo '<small class="description">' . esc_html__( 'Tick this option if you want the eval terminal to work only on a SSL connection.', 'wp-php-console' ) . '</small>'; |
307
|
|
|
|
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Settings page IP Range field. |
313
|
|
|
* |
314
|
|
|
* @since 1.0.0 |
315
|
|
|
*/ |
316
|
|
|
public function ip_field() { |
317
|
|
|
|
318
|
|
|
printf ( |
319
|
|
|
'<input type="text" class="regular-text" id="wp-php-console-ip" name="wp_php_console[ip]" value="%s" /> ', |
320
|
|
|
isset( $this->options['ip'] ) ? esc_attr( $this->options['ip']) : '' |
321
|
|
|
); |
322
|
|
|
echo '<label for="wp-php-console-ip">' . esc_html__( 'IP addresses (optional)', 'wp-php-console' ) . '</label>'; |
323
|
|
|
echo '<br />'; |
324
|
|
|
/* translators: VVV Varying Vagrant Vagrants default IP address */ |
325
|
|
|
printf ( __( '<small class="description">' . __( 'You may specify an IP address (e.g. <code>192.168.50.4</code>, %s default IP address), a range of addresses (<code>192.168.*.*</code>) or multiple addresses, comma separated (<code>192.168.10.25,192.168.10.28</code>) to grant access to the eval terminal.', 'wp-php-console' ) . '</small>' ), '<a href="https://github.com/Varying-Vagrant-Vagrants/VVV">Varying Vagrant Vagrants</a>' ); |
326
|
|
|
|
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Settings page Register PC Class field. |
332
|
|
|
* |
333
|
|
|
* @since 1.2.4 |
334
|
|
|
*/ |
335
|
|
View Code Duplication |
public function register_field() { |
|
|
|
|
336
|
|
|
|
337
|
|
|
$register = ! empty( $this->options['register'] ); |
338
|
|
|
|
339
|
|
|
printf ( |
340
|
|
|
'<input type="checkbox" id="wp-php-console-register" name="wp_php_console[register]" value="1" %s /> ', |
341
|
|
|
$register ? 'checked="checked"' : '' |
342
|
|
|
); |
343
|
|
|
echo '<label for="wp-php-console-register">' . esc_html__( 'Yes', 'wp-php-console' ) . '</label>'; |
344
|
|
|
echo '<br />'; |
345
|
|
|
echo '<small class="description">' . __( 'Choose to register PC in the global namespace. Allows to write <code>PC::debug($var, $tag)</code> or <code>PC::magic_tag($var)</code> instructions in PHP to inspect <code>$var</code> in the JavaScript console.', 'wp-php-console' ) . '</small>'; |
346
|
|
|
|
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Settings page Show Call Stack field. |
352
|
|
|
* |
353
|
|
|
* @since 1.2.4 |
354
|
|
|
*/ |
355
|
|
View Code Duplication |
public function stack_field() { |
|
|
|
|
356
|
|
|
|
357
|
|
|
$stack = ! empty( $this->options['stack'] ); |
358
|
|
|
|
359
|
|
|
printf ( |
360
|
|
|
'<input type="checkbox" id="wp-php-console-stack" name="wp_php_console[stack]" value="1" %s /> ', |
361
|
|
|
$stack ? 'checked="checked"' : '' |
362
|
|
|
); |
363
|
|
|
echo '<label for="wp-php-console-stack">' . esc_html__( 'Yes', 'wp-php-console' ) . '</label>'; |
364
|
|
|
echo '<br />'; |
365
|
|
|
echo '<small class="description">' . __( "Choose to also see the call stack when PHP Console writes to the browser's JavaScript-console.", 'wp-php-console' ) . '</small>'; |
366
|
|
|
|
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Settings page Show Short Paths field. |
372
|
|
|
* |
373
|
|
|
* @since 1.2.4 |
374
|
|
|
*/ |
375
|
|
View Code Duplication |
public function short_field() { |
|
|
|
|
376
|
|
|
|
377
|
|
|
$short = ! empty( $this->options['short'] ); |
378
|
|
|
|
379
|
|
|
printf ( |
380
|
|
|
'<input type="checkbox" id="wp-php-console-short" name="wp_php_console[short]" value="1" %s /> ', |
381
|
|
|
$short ? 'checked="checked"' : '' |
382
|
|
|
); |
383
|
|
|
echo '<label for="wp-php-console-short">' . esc_html__( 'Yes', 'wp-php-console' ) . '</label>'; |
384
|
|
|
echo '<br />'; |
385
|
|
|
echo '<small class="description">' . __( "Choose to shorten PHP Console error sources and traces paths in browser's JavaScript-console. Paths like <code>/server/path/to/document/root/WP/wp-admin/admin.php:31</code> will be displayed as <code>/W/wp-admin/admin.php:31</code>", 'wp-php-console' ) . '</small>'; |
386
|
|
|
|
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Sanitize user input in settings page. |
392
|
|
|
* |
393
|
|
|
* @since 1.0.0 |
394
|
|
|
* @param string $input user input |
395
|
|
|
* @return array sanitized inputs |
396
|
|
|
*/ |
397
|
|
|
public function sanitize_field( $input ) { |
398
|
|
|
|
399
|
|
|
$sanitized_input = array(); |
400
|
|
|
|
401
|
|
|
if ( isset( $input['password'] ) ) { |
402
|
|
|
$sanitized_input['password'] = sanitize_text_field( $input['password'] ); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
if ( isset( $input['ssl'] ) ) { |
406
|
|
|
$sanitized_input['ssl'] = ! empty( $input['ssl'] ) ? 1 : ''; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
if ( isset( $input['ip'] ) ) { |
410
|
|
|
$sanitized_input['ip'] = sanitize_text_field( $input['ip'] ); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
$sanitized_input['register'] = empty( $input['register'] ) ? '' : 1; |
414
|
|
|
$sanitized_input['stack'] = empty( $input['stack'] ) ? '' : 1; |
415
|
|
|
$sanitized_input['short'] = empty( $input['short'] ) ? '' : 1; |
416
|
|
|
|
417
|
|
|
return $sanitized_input; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Settings page. |
423
|
|
|
* |
424
|
|
|
* @since 1.0.0 |
425
|
|
|
*/ |
426
|
|
|
public function settings_page() { |
427
|
|
|
|
428
|
|
|
?> |
429
|
|
|
<div class="wrap"> |
430
|
|
|
<h2><?php esc_html_e( 'WP PHP Console', 'wp-php-console' ); ?></h2> |
431
|
|
|
<hr /> |
432
|
|
|
<form method="post" action="options.php"> |
433
|
|
|
<?php |
434
|
|
|
settings_fields( 'wp_php_console' ); |
435
|
|
|
do_settings_sections( $this->plugin_slug ); |
436
|
|
|
submit_button(); |
437
|
|
|
?> |
438
|
|
|
</form> |
439
|
|
|
</div> |
440
|
|
|
<?php |
441
|
|
|
|
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Settings page additional info. |
447
|
|
|
* Prints more details on the plugin settings page. |
448
|
|
|
* |
449
|
|
|
* @since 1.3.0 |
450
|
|
|
*/ |
451
|
|
|
public function settings_info() { |
452
|
|
|
|
453
|
|
|
?> |
454
|
|
|
<p><?php /* translators: %s refers to 'PHP Console' Chrome extension, will print download link for the Chrome extension */ |
455
|
|
|
printf( _x( 'This plugin allows you to use %s within your WordPress installation for testing, debugging and development purposes.<br/>Usage instructions:', 'PHP Console, the Chrome Extension', 'wp-php-console' ), '<a href="https://github.com/barbushin/php-console" target="_blank">PHP Console</a>' ); ?></p> |
456
|
|
|
<ol> |
457
|
|
|
<li><?php /* translators: Install PHP Console extension for Google Chrome download link */ |
458
|
|
|
printf( _x( 'Make sure you have downloaded and installed %s.', 'PHP Console, the Chrome Extension', 'wp-php-console' ), '<a target="_blank" href="https://chrome.google.com/webstore/detail/php-console/nfhmhhlpfleoednkpnnnkolmclajemef">PHP Console extension for Google Chrome</a>' ); ?></li> |
459
|
|
|
<li><?php _e( 'Set a password for the eval terminal in the options below and hit <code>save changes</code>.', 'wp-php-console' ); ?></li> |
460
|
|
|
<li><?php _e( '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' ); ?></li> |
461
|
|
|
<li><?php _e( 'From the eval terminal you can execute any PHP or WordPress specific function, including functions from your plugins and active theme.', 'wp-php-console' ); ?></li> |
462
|
|
|
<li><?php _e( "In your PHP code, you can call PHP Console debug statements like <code>debug($var, $tag)</code> to display PHP variables in the browser's JavaScript-console (<code>Ctrl Shift J </code>) and optionally filter selected tags through the browser's Remote PHP Eval Terminal screen's Ignore Debug options.", 'wp-php-console' ); ?></li> |
463
|
|
|
</ol> |
464
|
|
|
<?php |
465
|
|
|
|
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Initialize PHP Console. |
471
|
|
|
* |
472
|
|
|
* @since 1.0.0 |
473
|
|
|
*/ |
474
|
|
|
public function init() { |
|
|
|
|
475
|
|
|
|
476
|
|
|
// Display admin notice and abort if no password has been set |
477
|
|
|
$password = isset( $this->options['password'] ) ? $this->options['password'] : ''; |
478
|
|
|
if ( ! $password ) { |
479
|
|
|
add_action( 'admin_notices', array( $this, 'password_notice' ) ); |
480
|
|
|
return; // abort |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
// Selectively remove slashes added by WordPress as expected by PhpConsole |
484
|
|
|
if ( isset( $_POST[PhpConsole\Connector::POST_VAR_NAME] ) ) { |
485
|
|
|
$_POST[ PhpConsole\Connector::POST_VAR_NAME ] = stripslashes_deep( $_POST[ PhpConsole\Connector::POST_VAR_NAME ] ); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
$connector = PhpConsole\Connector::getInstance(); |
489
|
|
|
|
490
|
|
|
try { |
491
|
|
|
$connector->setPassword( $password ); |
492
|
|
|
} catch ( \Exception $e ) { |
493
|
|
|
$this->print_notice_exception( $e ); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
// PhpConsole instance |
497
|
|
|
$handler = PhpConsole\Handler::getInstance(); |
498
|
|
|
if ( PhpConsole\Handler::getInstance()->isStarted() !== true ) { |
499
|
|
|
try { |
500
|
|
|
$handler->start(); |
501
|
|
|
} catch( \Exception $e ) { |
502
|
|
|
$this->print_notice_exception( $e ); |
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
// Enable SSL-only mode |
507
|
|
|
$enableSslOnlyMode = isset( $this->options['ssl'] ) ? ( ! empty( $this->options['ssl'] ) ? $this->options['ssl'] : '' ) : ''; |
508
|
|
|
if ( $enableSslOnlyMode ) { |
509
|
|
|
$connector->enableSslOnlyMode(); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
// Restrict IP addresses |
513
|
|
|
$allowedIpMasks = isset( $this->options['ip'] ) ? ( ! empty( $this->options['ip'] ) ? explode( ',', $this->options['ip'] ) : '' ) : ''; |
514
|
|
|
if ( is_array( $allowedIpMasks ) && ! empty( $allowedIpMasks ) ) { |
515
|
|
|
$connector->setAllowedIpMasks( (array) $allowedIpMasks ); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
$evalProvider = $connector->getEvalDispatcher()->getEvalProvider(); |
519
|
|
|
|
520
|
|
|
try { |
521
|
|
|
$evalProvider->addSharedVar( 'uri', $_SERVER['REQUEST_URI'] ); |
522
|
|
|
} catch ( \Exception $e ) { |
523
|
|
|
$this->print_notice_exception( $e ); |
524
|
|
|
} |
525
|
|
|
try { |
526
|
|
|
$evalProvider->addSharedVarReference( 'post', $_POST ); |
527
|
|
|
} catch ( \Exception $e ) { |
528
|
|
|
$this->print_notice_exception( $e ); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
$openBaseDirs = array( ABSPATH, get_template_directory() ); |
532
|
|
|
try { |
533
|
|
|
$evalProvider->addSharedVarReference( 'dirs', $openBaseDirs ); |
534
|
|
|
} catch ( \Exception $e ) { |
535
|
|
|
$this->print_notice_exception( $e ); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
$evalProvider->setOpenBaseDirs( $openBaseDirs ); |
539
|
|
|
|
540
|
|
|
try { |
541
|
|
|
$connector->startEvalRequestsListener(); |
542
|
|
|
} catch ( \Exception $e ) { |
543
|
|
|
$this->print_notice_exception( $e ); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Admin password notice. |
551
|
|
|
* Prompts user to set a password for PHP Console upon plugin activation. |
552
|
|
|
* |
553
|
|
|
* @since 1.3.2 |
554
|
|
|
*/ |
555
|
|
|
public function password_notice() { |
556
|
|
|
|
557
|
|
|
?> |
558
|
|
|
<div class="update-nag"> |
559
|
|
|
<?php |
560
|
|
|
|
561
|
|
|
$settings_page = esc_url( admin_url( 'options-general.php?page=wp-php-console' ) ); |
562
|
|
|
|
563
|
|
|
/* translators: Placeholders: %1$s - opening HTML <a> link tag; %2$s closing HTML </a> link tag */ |
564
|
|
|
printf( $this->plugin_name . ': ' . __( 'Please remember to %1$s set a password %2$s if you want to enable terminal.', 'wp-php-console' ), '<a href="' . $settings_page .'">', '</a>' ); |
565
|
|
|
|
566
|
|
|
?> |
567
|
|
|
</div> |
568
|
|
|
<?php |
569
|
|
|
|
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
|
573
|
|
|
} |
574
|
|
|
|
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.