Skip to content

Commit 3e3d045

Browse files
committed
Hook into init super early
1 parent 818c2be commit 3e3d045

File tree

5 files changed

+126
-102
lines changed

5 files changed

+126
-102
lines changed

ChangeLog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22

33
### 1.5.0 (29 May 2016)
4-
* Fix: Tries to address an incompatibility between WordPress and PHP Console: "PHP Warning: session_start(): Cannot send session cache limiter - headers already sent"
4+
* Fix: Fixes "PHP Warning: session_start(): Cannot send session cache limiter - headers already sent" notice in logs
55
* Misc: Internal changes, new Settings class, deprecated methods and properties in main Plugin class
66
* Misc: Updated PHP Console Library to 3.1.6
77
* Misc: Tested up to WordPress 4.5.2

includes/class-wp-php-console-settings.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class Settings {
2828
*/
2929
private $page = '';
3030

31-
3231
/**
3332
* This plugin's option name.
3433
*
@@ -119,7 +118,7 @@ public function register_settings() {
119118
'callback' => array( $this, 'ip_field' ),
120119
),
121120
'register' => array(
122-
'label' => esc_html__( 'Register PC Class ', 'wp-php-console' ),
121+
'label' => esc_html__( 'Register PC Class', 'wp-php-console' ),
123122
'callback' => array( $this, 'register_field' ),
124123
),
125124
'stack' => array(
@@ -156,8 +155,10 @@ public function settings_info() {
156155
?>
157156
<p><?php
158157
/* 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' ); ?>
158+
printf( _x( 'This plugin allows you to use %s within your WordPress installation for testing, debugging and development purposes.', 'PHP Console, the PHP Library', 'wp-php-console' ),
159+
'<a href="https://github.com/barbushin/php-console" target="_blank">PHP Console</a>'
160+
); ?><br>
161+
<?php esc_html_e( 'Usage instructions:', 'wp-php-console' ); ?>
161162
</p>
162163
<ol>
163164
<?php
@@ -235,7 +236,7 @@ public function ip_field() {
235236
<ol>
236237
<li><small><?php
237238
/* 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+
printf( __( 'An IP address (for example %1$s, %2$s default IP address).', 'wp-php-console' ),
239240
'<code>192.168.50.4</code>',
240241
'<a href="https://github.com/Varying-Vagrant-Vagrants/VVV">Varying Vagrant Vagrants</a>'
241242
); ?></small></li>
@@ -319,12 +320,12 @@ public function short_field() {
319320
* Sanitize user input in settings page.
320321
*
321322
* @since 1.5.0
322-
* @param array $field user input
323+
* @param array $option user input
323324
* @return array sanitized input
324325
*/
325-
public function sanitize_field( $field ) {
326+
public function sanitize_field( $option ) {
326327

327-
$input = wp_parse_args( $field, array(
328+
$input = wp_parse_args( $option, array(
328329
'ip' => '',
329330
'password' => '',
330331
'register' => false,

includes/class-wp-php-console.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Plugin {
4949
/**
5050
* Instance of PHP Console connector object.
5151
*
52-
* @since 1.4.1
52+
* @since 1.4.0
5353
* @access public
5454
* @var PhpConsole\Connector $connector Instance.
5555
*/
@@ -81,13 +81,17 @@ public function __construct() {
8181
}
8282

8383
// Connect to PHP Console.
84-
$this->connect();
84+
add_action( 'init', function() {
85+
86+
$this->connect();
87+
88+
// Apply PHP Console options.
89+
$this->apply_options();
8590

86-
// Apply PHP Console options.
87-
$this->apply_options();
91+
}, -100 );
8892

8993
// Delay further PHP Console initialisation to have more context during Remote PHP execution.
90-
add_action( 'wp_loaded', array( $this, 'init' ) );
94+
add_action( 'wp_loaded', array( $this, 'init' ), -100 );
9195

9296
}
9397

@@ -120,26 +124,25 @@ private function connect() {
120124
// But there is some problem with frameworks like WordPress that override PHP session handler.
121125
// PHP Console has alternative storage drivers for this - we will write to a temporary file:
122126

123-
// TODO this generates "PHP Warning: session_start(): Cannot send session cache limiter - headers already sent"
124-
125-
/**
126127
$phpcdir = __DIR__ . '/tmp';
127128
$make_dir = wp_mkdir_p( $phpcdir );
128129

129130
if ( true === $make_dir ) {
130131

131132
try {
132-
$storage = @new PhpConsole\Storage\File( $phpcdir . '/' . md5( __FILE__ ) . '_pc.data' );
133+
$storage = new PhpConsole\Storage\File( $phpcdir . '/' . md5( __FILE__ ) . '_pc.data' );
133134
PhpConsole\Connector::setPostponeStorage( $storage );
134135
} catch( \Exception $e ) {
135-
// TODO $storage is under DOCUMENT_ROOT - it's insecure but did not find another solution in WP
136-
$this->print_notice_exception( $e );
136+
// PHP Console will complain about this: "Path <...> is under DOCUMENT_ROOT. It's insecure!"
137+
// but there's nothing we can do at the moment as there doesn't seem to be another solution in WP
138+
// nevertheless PHP Console tool is not meant to be used in production so this shouldn't be a concern
139+
// $this->print_notice_exception( $e );
137140
}
138141

139142
}
140-
**/
141143

142-
// Perform PHP Console initialisation required asap for other code to be able to output to the JavaScript console.
144+
// Perform PHP Console initialisation required asap
145+
// for other code to be able to output to the JavaScript console.
143146
if ( ! $this->connector instanceof PhpConsole\Connector ) {
144147
$this->connector = PhpConsole\Connector::getInstance();
145148
}
@@ -171,7 +174,7 @@ protected function get_options() {
171174
/**
172175
* Apply options.
173176
*
174-
* @since 1.4.1
177+
* @since 1.4.0
175178
*/
176179
private function apply_options() {
177180

@@ -180,22 +183,22 @@ private function apply_options() {
180183
return;
181184
}
182185

183-
// Apply 'register' option to PHP Console
186+
// Apply 'register' option to PHP Console...
184187
if ( true === $this->options['register'] && ! class_exists( 'PC', false ) ) {
185-
// Only if PC not registered yet
188+
// ...only if PC not registered yet.
186189
try {
187190
PhpConsole\Helper::register();
188191
} catch( \Exception $e ) {
189192
$this->print_notice_exception( $e );
190193
}
191194
}
192195

193-
// Apply 'stack' option to PHP Console
196+
// Apply 'stack' option to PHP Console.
194197
if ( true === $this->options['stack'] ) {
195198
$this->connector->getDebugDispatcher()->detectTraceAndSource = true;
196199
}
197200

198-
// Apply 'short' option to PHP Console
201+
// Apply 'short' option to PHP Console.
199202
if ( true === $this->options['short'] ) {
200203
try {
201204
$this->connector->setSourcesBasePath( $_SERVER['DOCUMENT_ROOT'] );
@@ -214,7 +217,7 @@ private function apply_options() {
214217
*/
215218
public function init() {
216219

217-
// Set PHP Console extension password.
220+
// Get PHP Console extension password.
218221
$password = $this->options['password'];
219222

220223
if ( ! $password ) {
@@ -228,17 +231,19 @@ public function init() {
228231
$_POST[ PhpConsole\Connector::POST_VAR_NAME ] = stripslashes_deep( $_POST[ PhpConsole\Connector::POST_VAR_NAME ] );
229232
}
230233

234+
// Get PHP Console instance if wasn't set yet.
231235
if ( ! $this->connector instanceof PhpConsole\Connector ) {
232236
$this->connector = PhpConsole\Connector::getInstance();
233237
}
234238

239+
// Set PHP Console password.
235240
try {
236241
$this->connector->setPassword( $password );
237242
} catch ( \Exception $e ) {
238243
$this->print_notice_exception( $e );
239244
}
240245

241-
// PhpConsole instance.
246+
// Get PHP Console handler instance.
242247
$handler = PhpConsole\Handler::getInstance();
243248

244249
if ( true !== PhpConsole\Handler::getInstance()->isStarted() ) {
@@ -325,16 +330,14 @@ public function password_notice() {
325330

326331
?>
327332
<div class="update-nag">
328-
<?php
329-
333+
<p><?php
330334
/* translators: Placeholders: %1$s - WP Php Console name, %2$s - opening HTML <a> link tag; %3$s closing HTML </a> link tag */
331-
printf( __( '%1$s: Please remember to %2$s set a password %3$s if you want to enable the terminal.', 'wp-php-console' ),
335+
printf( __( '%1$s: Please remember to %2$sset a password%3$s if you want to enable the terminal.', 'wp-php-console' ),
332336
'<strong>' . self::NAME . '</strong>',
333337
'<a href="' . esc_url( admin_url( 'options-general.php?page=wp-php-console' ) ) .'">',
334338
'</a>'
335-
);
336-
337-
?>
339+
); ?>
340+
</p>
338341
</div>
339342
<?php
340343

0 commit comments

Comments
 (0)