Skip to content

Commit 8580be9

Browse files
committed
improved error and exception handling
1 parent 60cd45c commit 8580be9

File tree

5 files changed

+112
-34
lines changed

5 files changed

+112
-34
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "unfulvio/wp-php-console",
33
"description": "A WordPress implementation of PHP Console.",
44
"type": "wordpress-plugin",
5-
"version": "1.4.0",
5+
"version": "1.4.1",
66
"keywords": ["wordpress", "debug", "debugging", "development", "php-console", "console", "terminal", "command-line", "cli" ],
77
"homepage": "https://github.com/unfulvio/wp-php-console",
88
"license": "GPLv2.0+",

includes/class-wp-php-console.php

Lines changed: 103 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
class Plugin {
2424

25+
2526
/**
2627
* The plugin name.
2728
*
@@ -66,11 +67,12 @@ class Plugin {
6667
*/
6768
public function __construct() {
6869

69-
$this->plugin_slug = 'WP PHP Console';
70+
$this->plugin_name = 'WP PHP Console';
7071
$this->plugin_slug = 'wp-php-console';
71-
$this->version = '1.4.0';
72-
$this->options = get_option( 'wp_php_console' );
72+
$this->version = '1.4.1';
73+
$this->options = $this->get_options();
7374

75+
// Bail out if PHP Console can't be found
7476
if ( ! class_exists( 'PhpConsole\Connector' ) ) {
7577
return;
7678
}
@@ -83,21 +85,28 @@ public function __construct() {
8385
$make_dir = wp_mkdir_p( $phpcdir );
8486

8587
if ( $make_dir === true ) {
86-
PhpConsole\Connector::setPostponeStorage(
87-
new PhpConsole\Storage\File( $phpcdir . '/' . md5( __FILE__ ) . '_pc.data', false )
88-
);
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+
8997
}
9098

9199
// Perform PHP Console initialisation required asap for other code to be able to output to the JavaScript console
92100
$connector = PhpConsole\Connector::getInstance();
93101

94102
// Apply 'register' option to PHP Console
95-
if ( ! empty( $this->options['register'] ) ) {
96-
if ( ! class_exists( 'PC', false ) ) {
97-
// Only if PC not registered yet
98-
PhpConsole\Helper::register( );
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 );
99109
}
100-
// PC::debug( 'PC::debug() is available' );
101110
}
102111

103112
// Apply 'stack' option to PHP Console
@@ -107,7 +116,11 @@ public function __construct() {
107116

108117
// Apply 'short' option to PHP Console
109118
if ( ! empty( $this->options['short'] ) ) {
110-
$connector->setSourcesBasePath( $_SERVER['DOCUMENT_ROOT'] );
119+
try {
120+
$connector->setSourcesBasePath( $_SERVER['DOCUMENT_ROOT'] );
121+
} catch ( \Exception $e ) {
122+
$this->print_notice_exception( $e );
123+
}
111124
}
112125

113126
// Translation
@@ -120,6 +133,38 @@ public function __construct() {
120133
}
121134

122135

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+
123168
/**
124169
* Set plugin text domain.
125170
*
@@ -238,7 +283,7 @@ public function password_field() {
238283
);
239284
echo '<label for="wp-php-console-ip">' . esc_html__( 'Required', 'wp-php-console' ) . '</label>';
240285
echo '<br />';
241-
echo '<small class="description">' . __( 'The password for the eval terminal. If empty, the plugin will not work.', 'wp-php-console' ) . '</small>';
286+
echo '<small class="description">' . esc_html__( 'The password for the eval terminal. If empty, the plugin will not work.', 'wp-php-console' ) . '</small>';
242287

243288
}
244289

@@ -258,7 +303,7 @@ public function ssl_field() {
258303
);
259304
echo '<label for="wp-php-console-ssl">' . esc_html__( 'Yes', 'wp-php-console' ) . '</label>';
260305
echo '<br />';
261-
echo '<small class="description">' . __( 'Tick this option if you want the eval terminal to work only on a SSL connection.', 'wp-php-console' ) . '</small>';
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>';
262307

263308
}
264309

@@ -276,7 +321,7 @@ public function ip_field() {
276321
);
277322
echo '<label for="wp-php-console-ip">' . esc_html__( 'IP addresses (optional)', 'wp-php-console' ) . '</label>';
278323
echo '<br />';
279-
// translators VVV Varying Vagrant Vagrants default IP address
324+
/* translators: VVV Varying Vagrant Vagrants default IP address */
280325
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>' );
281326

282327
}
@@ -406,10 +451,10 @@ public function settings_page() {
406451
public function settings_info() {
407452

408453
?>
409-
<p><?php // Translators: %s refers to 'PHP Console' Chrome extension, will print download link for the Chrome extension
454+
<p><?php /* translators: %s refers to 'PHP Console' Chrome extension, will print download link for the Chrome extension */
410455
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>
411456
<ol>
412-
<li><?php // Translators: Install PHP Console extension for Google Chrome download link
457+
<li><?php /* translators: Install PHP Console extension for Google Chrome download link */
413458
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>
414459
<li><?php _e( 'Set a password for the eval terminal in the options below and hit <code>save changes</code>.', 'wp-php-console' ); ?></li>
415460
<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>
@@ -441,12 +486,21 @@ public function init() {
441486
}
442487

443488
$connector = PhpConsole\Connector::getInstance();
444-
$connector->setPassword( $password );
489+
490+
try {
491+
$connector->setPassword( $password );
492+
} catch ( \Exception $e ) {
493+
$this->print_notice_exception( $e );
494+
}
445495

446496
// PhpConsole instance
447497
$handler = PhpConsole\Handler::getInstance();
448-
if ( PhpConsole\Handler::getInstance()->isStarted() != true ) {
449-
$handler->start();
498+
if ( PhpConsole\Handler::getInstance()->isStarted() !== true ) {
499+
try {
500+
$handler->start();
501+
} catch( \Exception $e ) {
502+
$this->print_notice_exception( $e );
503+
}
450504
}
451505

452506
// Enable SSL-only mode
@@ -463,15 +517,31 @@ public function init() {
463517

464518
$evalProvider = $connector->getEvalDispatcher()->getEvalProvider();
465519

466-
$evalProvider->addSharedVar( 'uri', $_SERVER['REQUEST_URI'] );
467-
$evalProvider->addSharedVarReference( 'post', $_POST );
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+
}
468530

469-
// $evalProvider->disableFileAccessByOpenBaseDir();
470531
$openBaseDirs = array( ABSPATH, get_template_directory() );
471-
$evalProvider->addSharedVarReference( 'dirs', $openBaseDirs );
532+
try {
533+
$evalProvider->addSharedVarReference( 'dirs', $openBaseDirs );
534+
} catch ( \Exception $e ) {
535+
$this->print_notice_exception( $e );
536+
}
537+
472538
$evalProvider->setOpenBaseDirs( $openBaseDirs );
473539

474-
$connector->startEvalRequestsListener();
540+
try {
541+
$connector->startEvalRequestsListener();
542+
} catch ( \Exception $e ) {
543+
$this->print_notice_exception( $e );
544+
}
475545

476546
}
477547

@@ -484,11 +554,16 @@ public function init() {
484554
*/
485555
public function password_notice() {
486556

487-
$settings_page = esc_url( admin_url( 'options-general.php?page=wp-php-console' ) );
488-
489557
?>
490558
<div class="update-nag">
491-
<?php 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>' ); ?>
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+
?>
492567
</div>
493568
<?php
494569

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "wp-php-console",
33
"title": "WP PHP Console",
44
"description": "An implementation of PHP Console as a WordPress plugin. Use Chrome Dev Tools to debug your WordPress installation!",
5-
"version": "1.4.0",
5+
"version": "1.4.1",
66
"homepage": "https://wordpress.org/plugins/wp-php-console/",
77
"repository": {
88
"type": "git",

readme.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Contributors: nekojira
33
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=P64V9NTEYFKDL
44
Tags: development, debug, debugging
55
Requires at least: 3.6.0
6-
Tested up to: 4.4
7-
Stable tag: 1.3.9
6+
Tested up to: 4.4.1
7+
Stable tag: 1.4.1
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -118,11 +118,14 @@ or
118118

119119
== Screenshots ==
120120

121-
None yet.
121+
None.
122122

123123

124124
== Changelog ==
125125

126+
= 1.4.1 =
127+
* Improved error and exception handling
128+
126129
= 1.4.0 =
127130
* Internal changes, improved usage of Composer in plugin development
128131
* Support for WordPress language packs

wp-php-console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin URI: https://github.com/unfulvio/wp-php-console/
55
* Description: An implementation of PHP Console for WordPress. Easily debug and trace PHP errors and warnings from your Chrome dev tools console using a Google Chrome extension.
66
*
7-
* Version: 1.4.0
7+
* Version: 1.4.1
88
*
99
* Author: Fulvio Notarstefano
1010
* Author URI: https://github.com/unfulvio/

0 commit comments

Comments
 (0)