11<?php
2+ /**
3+ * WP PHP Console
4+ *
5+ * This source file is subject to the GNU General Public License v3.0
6+ * that is bundled with this package in the file license.txt.
7+ * It is also available through the world-wide-web at this URL:
8+ * http://www.gnu.org/licenses/gpl-3.0.html
9+ *
10+ * @author Fulvio Notarstefano <[email protected] > 11+ * @copyright Copyright (c) 2014-2019 Fulvio Notarstefano
12+ * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
13+ */
214
315namespace WP_PHP_Console ;
416
@@ -22,7 +34,7 @@ class Plugin {
2234
2335
2436 /** @var array settings options */
25- protected $ options = [];
37+ private $ options = [];
2638
2739 /** @var PhpConsole\Connector instance */
2840 public $ connector ;
@@ -35,24 +47,38 @@ class Plugin {
3547 */
3648 public function __construct () {
3749
38- // handle translations
39- add_action ( 'plugins_loaded ' , [ $ this , 'set_locale ' ] );
50+ $ this ->set_debug_mode ();
51+ $ this ->set_options ();
52+ $ this ->set_admin ();
53+ $ this ->set_hooks ();
54+ }
4055
41- // set options
42- $ this ->options = $ this ->get_options ();
4356
44- // load admin
45- $ this ->set_admin ();
57+ /**
58+ * Sets constants and elevates PHP error reporting.
59+ *
60+ * @since 1.5.4
61+ */
62+ private function set_debug_mode () {
4663
47- // bail out if PHP Console can't be found
48- if ( ! class_exists ( 'PhpConsole\Connector ' ) ) {
49- return ;
64+ @error_reporting ( E_ALL );
65+
66+ foreach ( [ 'WP_DEBUG ' , 'WP_DEBUG_LOG ' , 'WP_DEBUG_DISPLAY ' , ] as $ wp_debug_constant ) {
67+ if ( ! defined ( $ wp_debug_constant ) ) {
68+ define ( $ wp_debug_constant , true );
69+ }
5070 }
71+ }
5172
52- // connect to PHP Console
53- add_action ( 'init ' , [ $ this , 'connect ' ], -1000 );
54- // delay further PHP Console initialisation to have more context during Remote PHP execution
55- add_action ( 'wp_loaded ' , [ $ this , 'init ' ], -1000 );
73+
74+ /**
75+ * Sets the plugin options.
76+ *
77+ * @since 1.5.4
78+ */
79+ private function set_options () {
80+
81+ $ this ->options = $ this ->get_options ();
5682 }
5783
5884
@@ -88,14 +114,32 @@ private function set_admin() {
88114 ], $ actions );
89115 } );
90116
91- // init settings
92- require_once __DIR__ . '/class-wp-php-console-settings.php ' ;
93-
94117 new Settings ( $ this ->options );
95118 }
96119 }
97120
98121
122+ /**
123+ * Sets plugin hooks.
124+ *
125+ * @since 1.5.4
126+ */
127+ private function set_hooks () {
128+
129+ // bail out if PHP Console can't be found
130+ if ( ! class_exists ( 'PhpConsole\Connector ' ) ) {
131+ return ;
132+ }
133+
134+ // handle translations
135+ add_action ( 'plugins_loaded ' , [ $ this , 'set_locale ' ] );
136+ // connect to PHP Console
137+ add_action ( 'init ' , [ $ this , 'connect ' ], -1000 );
138+ // delay further PHP Console initialisation to have more context during Remote PHP execution
139+ add_action ( 'wp_loaded ' , [ $ this , 'init ' ], -1000 );
140+ }
141+
142+
99143 /**
100144 * Connects to PHP Console.
101145 *
@@ -110,21 +154,27 @@ public function connect() {
110154 // workaround for avoiding headers already sent warnings
111155 @error_reporting ( E_ALL & ~E_WARNING );
112156
113- if ( empty ( @session_id () ) ) {
157+ if ( ! @session_id () ) {
114158 @session_start ();
115159 }
116160
161+ $ connected = true ;
162+
117163 if ( ! $ this ->connector instanceof PhpConsole \Connector ) {
118164 try {
119165 $ this ->connector = PhpConsole \Connector::getInstance ();
120- } catch ( \Exception $ e ) {}
166+ } catch ( \Exception $ e ) {
167+ $ connected = false ;
168+ }
121169 }
122170
123171 // restore error reporting
124172 @error_reporting ( E_ALL );
125173
126174 // apply PHP Console options
127- $ this ->apply_options ();
175+ if ( $ connected ) {
176+ $ this ->apply_options ();
177+ }
128178 }
129179
130180
@@ -215,9 +265,20 @@ public function init() {
215265 // get PHP Console instance if wasn't set yet
216266 if ( ! $ this ->connector instanceof PhpConsole \Connector ) {
217267
268+ // workaround for avoiding headers already sent warnings
269+ @error_reporting ( E_ALL & ~E_WARNING );
270+
218271 try {
219272 $ this ->connector = PhpConsole \Connector::getInstance ();
273+ $ connected = true ;
220274 } catch ( \Exception $ e ) {
275+ $ connected = false ;
276+ }
277+
278+ // restore error reporting
279+ @error_reporting ( E_ALL );
280+
281+ if ( ! $ connected ) {
221282 return ;
222283 }
223284 }
@@ -321,7 +382,7 @@ public function password_notice() {
321382 ?>
322383 <div class="update-nag">
323384 <p><?php printf (
324- /* translators: Placeholders: %1$s - WP Php Console name, %2$s - opening HTML <a> link tag; %3$s closing HTML </a> link tag */
385+ /* translators: Placeholders: %1$s - WP PHP Console name, %2$s - opening HTML <a> link tag; %3$s closing HTML </a> link tag */
325386 __ ( '%1$s: Please remember to %2$sset a password%3$s if you want to enable the terminal. ' , 'wp-php-console ' ),
326387 '<strong> ' . self ::NAME . '</strong> ' ,
327388 '<a href=" ' . esc_url ( admin_url ( 'options-general.php?page=wp-php-console ' ) ) .'"> ' ,
0 commit comments