Completed
Push — master ( 9c4460...83949e )
by Fulvio
01:54
created
includes/class-wp-php-console.php 1 patch
Spacing   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 
5 5
 use PhpConsole;
6 6
 
7
-defined( 'ABSPATH' ) or exit;
7
+defined('ABSPATH') or exit;
8 8
 
9 9
 /**
10 10
  * WP PHP Console main class.
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
 	public function __construct() {
37 37
 
38 38
 		// handle translations
39
-		add_action( 'plugins_loaded', [ $this, 'set_locale' ] );
39
+		add_action('plugins_loaded', [$this, 'set_locale']);
40 40
 
41 41
 		// set options
42 42
 		$this->options = $this->get_options();
@@ -45,14 +45,14 @@  discard block
 block discarded – undo
45 45
 		$this->set_admin();
46 46
 
47 47
 		// bail out if PHP Console can't be found
48
-		if ( ! class_exists( 'PhpConsole\Connector' ) ) {
48
+		if ( ! class_exists('PhpConsole\Connector')) {
49 49
 			return;
50 50
 		}
51 51
 
52 52
 		// connect to PHP Console
53
-		add_action( 'init',      [ $this, 'connect' ], -1000 );
53
+		add_action('init', [$this, 'connect'], -1000);
54 54
 		// delay further PHP Console initialisation to have more context during Remote PHP execution
55
-		add_action( 'wp_loaded', [ $this, 'init' ], -1000 );
55
+		add_action('wp_loaded', [$this, 'init'], -1000);
56 56
 	}
57 57
 
58 58
 
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
 		load_plugin_textdomain(
67 67
 			'wp-php-console',
68 68
 			false,
69
-			dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/'
69
+			dirname(dirname(plugin_basename(__FILE__))).'/languages/'
70 70
 		);
71 71
 	}
72 72
 
@@ -78,20 +78,20 @@  discard block
 block discarded – undo
78 78
 	 */
79 79
 	private function set_admin() {
80 80
 
81
-		if ( ! defined( 'DOING_AJAX' ) && is_admin() ) {
81
+		if ( ! defined('DOING_AJAX') && is_admin()) {
82 82
 
83 83
 			// add a settings link to the plugins admin screen
84
-			$plugin_name = str_replace( 'includes/class-', '', plugin_basename( __FILE__ ) );
85
-			add_filter( "plugin_action_links_{$plugin_name}", static function( $actions ) {
86
-				return array_merge( [
87
-					'<a href="' . esc_url( admin_url( 'options-general.php?page=wp-php-console' ) ) . '">' . __( 'Settings', 'wp-php-console' ) . '</a>',
88
-				], $actions );
84
+			$plugin_name = str_replace('includes/class-', '', plugin_basename(__FILE__));
85
+			add_filter("plugin_action_links_{$plugin_name}", static function($actions) {
86
+				return array_merge([
87
+					'<a href="'.esc_url(admin_url('options-general.php?page=wp-php-console')).'">'.__('Settings', 'wp-php-console').'</a>',
88
+				], $actions);
89 89
 			} );
90 90
 
91 91
 			// init settings
92
-			require_once __DIR__ . '/class-wp-php-console-settings.php';
92
+			require_once __DIR__.'/class-wp-php-console-settings.php';
93 93
 
94
-			new Settings( $this->options );
94
+			new Settings($this->options);
95 95
 		}
96 96
 	}
97 97
 
@@ -108,20 +108,20 @@  discard block
 block discarded – undo
108 108
 	public function connect() {
109 109
 
110 110
 		// workaround for avoiding headers already sent warnings
111
-		@error_reporting( E_ALL & ~E_WARNING );
111
+		@error_reporting(E_ALL & ~E_WARNING);
112 112
 
113
-		if ( empty( @session_id() ) ) {
113
+		if (empty(@session_id())) {
114 114
 			@session_start();
115 115
 		}
116 116
 
117
-		if ( ! $this->connector instanceof PhpConsole\Connector ) {
117
+		if ( ! $this->connector instanceof PhpConsole\Connector) {
118 118
 			try {
119 119
 				$this->connector = PhpConsole\Connector::getInstance();
120
-			} catch ( \Exception $e ) {}
120
+			} catch (\Exception $e) {}
121 121
 		}
122 122
 
123 123
 		// restore error reporting
124
-		@error_reporting( E_ALL );
124
+		@error_reporting(E_ALL);
125 125
 
126 126
 		// apply PHP Console options
127 127
 		$this->apply_options();
@@ -137,16 +137,16 @@  discard block
 block discarded – undo
137 137
 	 */
138 138
 	protected function get_options() {
139 139
 
140
-		$options = get_option( 'wp_php_console', [] );
140
+		$options = get_option('wp_php_console', []);
141 141
 
142
-		return wp_parse_args( $options, [
142
+		return wp_parse_args($options, [
143 143
 			'ip'       => '',
144 144
 			'password' => '',
145 145
 			'register' => false,
146 146
 			'short'    => false,
147 147
 			'ssl'      => false,
148 148
 			'stack'    => false,
149
-		] );
149
+		]);
150 150
 	}
151 151
 
152 152
 
@@ -158,31 +158,31 @@  discard block
 block discarded – undo
158 158
 	private function apply_options() {
159 159
 
160 160
 		// bail out if not connected yet to PHP Console
161
-		if ( ! $this->connector instanceof PhpConsole\Connector ) {
161
+		if ( ! $this->connector instanceof PhpConsole\Connector) {
162 162
 			return;
163 163
 		}
164 164
 
165 165
 		// apply 'register' option to PHP Console...
166
-		if ( true === $this->options['register'] && ! class_exists( 'PC', false ) ) {
166
+		if (true === $this->options['register'] && ! class_exists('PC', false)) {
167 167
 			// ...only if PC not registered yet
168 168
 			try {
169 169
 				PhpConsole\Helper::register();
170
-			} catch( \Exception $e ) {
171
-				$this->print_notice_exception( $e );
170
+			} catch (\Exception $e) {
171
+				$this->print_notice_exception($e);
172 172
 			}
173 173
 		}
174 174
 
175 175
 		// apply 'stack' option to PHP Console
176
-		if ( true === $this->options['stack'] ) {
176
+		if (true === $this->options['stack']) {
177 177
 			$this->connector->getDebugDispatcher()->detectTraceAndSource = true;
178 178
 		}
179 179
 
180 180
 		// apply 'short' option to PHP Console
181
-		if ( true === $this->options['short'] ) {
181
+		if (true === $this->options['short']) {
182 182
 			try {
183
-				$this->connector->setSourcesBasePath( $_SERVER['DOCUMENT_ROOT'] );
184
-			} catch ( \Exception $e ) {
185
-				$this->print_notice_exception( $e );
183
+				$this->connector->setSourcesBasePath($_SERVER['DOCUMENT_ROOT']);
184
+			} catch (\Exception $e) {
185
+				$this->print_notice_exception($e);
186 186
 			}
187 187
 		}
188 188
 	}
@@ -198,89 +198,89 @@  discard block
 block discarded – undo
198 198
 	public function init() {
199 199
 
200 200
 		// get PHP Console extension password
201
-		$password = trim( $this->options['password'] );
201
+		$password = trim($this->options['password']);
202 202
 
203
-		if ( empty( $password ) ) {
203
+		if (empty($password)) {
204 204
 
205 205
 			// display admin notice and abort if no password has been set
206
-			add_action( 'admin_notices', [ $this, 'password_notice' ] );
206
+			add_action('admin_notices', [$this, 'password_notice']);
207 207
 			return;
208 208
 		}
209 209
 
210 210
 		// selectively remove slashes added by WordPress as expected by PHP Console
211
-		if ( array_key_exists( PhpConsole\Connector::POST_VAR_NAME, $_POST ) ) {
212
-			$_POST[ PhpConsole\Connector::POST_VAR_NAME ] = stripslashes_deep( $_POST[ PhpConsole\Connector::POST_VAR_NAME ] );
211
+		if (array_key_exists(PhpConsole\Connector::POST_VAR_NAME, $_POST)) {
212
+			$_POST[PhpConsole\Connector::POST_VAR_NAME] = stripslashes_deep($_POST[PhpConsole\Connector::POST_VAR_NAME]);
213 213
 		}
214 214
 
215 215
 		// get PHP Console instance if wasn't set yet
216
-		if ( ! $this->connector instanceof PhpConsole\Connector ) {
216
+		if ( ! $this->connector instanceof PhpConsole\Connector) {
217 217
 
218 218
 			try {
219 219
 				$this->connector = PhpConsole\Connector::getInstance();
220
-			} catch ( \Exception $e ) {
220
+			} catch (\Exception $e) {
221 221
 				return;
222 222
 			}
223 223
 		}
224 224
 
225 225
 		// set PHP Console password
226 226
 		try {
227
-			$this->connector->setPassword( $password );
228
-		} catch ( \Exception $e ) {
229
-			$this->print_notice_exception( $e );
227
+			$this->connector->setPassword($password);
228
+		} catch (\Exception $e) {
229
+			$this->print_notice_exception($e);
230 230
 		}
231 231
 
232 232
 		// get PHP Console handler instance
233 233
 		$handler = PhpConsole\Handler::getInstance();
234 234
 
235
-		if ( true !== PhpConsole\Handler::getInstance()->isStarted() ) {
235
+		if (true !== PhpConsole\Handler::getInstance()->isStarted()) {
236 236
 			try {
237 237
 				$handler->start();
238
-			} catch( \Exception $e ) {
239
-				$this->print_notice_exception( $e );
238
+			} catch (\Exception $e) {
239
+				$this->print_notice_exception($e);
240 240
 				return;
241 241
 			}
242 242
 		}
243 243
 
244 244
 		// enable SSL-only mode
245
-		if ( true === $this->options['ssl'] ) {
245
+		if (true === $this->options['ssl']) {
246 246
 			$this->connector->enableSslOnlyMode();
247 247
 		}
248 248
 
249 249
 		// restrict IP addresses
250
-		$allowedIpMasks = ! empty( $this->options['ip'] ) ? explode( ',', $this->options['ip'] ) : '';
250
+		$allowedIpMasks = ! empty($this->options['ip']) ? explode(',', $this->options['ip']) : '';
251 251
 
252
-		if ( is_array( $allowedIpMasks ) && count( $allowedIpMasks ) > 0 ) {
253
-			$this->connector->setAllowedIpMasks( $allowedIpMasks );
252
+		if (is_array($allowedIpMasks) && count($allowedIpMasks) > 0) {
253
+			$this->connector->setAllowedIpMasks($allowedIpMasks);
254 254
 		}
255 255
 
256 256
 		$evalProvider = $this->connector->getEvalDispatcher()->getEvalProvider();
257 257
 
258 258
 		try {
259
-			$evalProvider->addSharedVar( 'uri', $_SERVER['REQUEST_URI'] );
260
-		} catch ( \Exception $e ) {
261
-			$this->print_notice_exception( $e );
259
+			$evalProvider->addSharedVar('uri', $_SERVER['REQUEST_URI']);
260
+		} catch (\Exception $e) {
261
+			$this->print_notice_exception($e);
262 262
 		}
263 263
 
264 264
 		try {
265
-			$evalProvider->addSharedVarReference( 'post', $_POST );
266
-		} catch ( \Exception $e ) {
267
-			$this->print_notice_exception( $e );
265
+			$evalProvider->addSharedVarReference('post', $_POST);
266
+		} catch (\Exception $e) {
267
+			$this->print_notice_exception($e);
268 268
 		}
269 269
 
270
-		$openBaseDirs = [ ABSPATH, get_template_directory() ];
270
+		$openBaseDirs = [ABSPATH, get_template_directory()];
271 271
 
272 272
 		try {
273
-			$evalProvider->addSharedVarReference( 'dirs', $openBaseDirs );
274
-		} catch ( \Exception $e ) {
275
-			$this->print_notice_exception( $e );
273
+			$evalProvider->addSharedVarReference('dirs', $openBaseDirs);
274
+		} catch (\Exception $e) {
275
+			$this->print_notice_exception($e);
276 276
 		}
277 277
 
278
-		$evalProvider->setOpenBaseDirs( $openBaseDirs );
278
+		$evalProvider->setOpenBaseDirs($openBaseDirs);
279 279
 
280 280
 		try {
281 281
 			$this->connector->startEvalRequestsListener();
282
-		} catch ( \Exception $e ) {
283
-			$this->print_notice_exception( $e );
282
+		} catch (\Exception $e) {
283
+			$this->print_notice_exception($e);
284 284
 		}
285 285
 	}
286 286
 
@@ -292,13 +292,13 @@  discard block
 block discarded – undo
292 292
 	 *
293 293
 	 * @param \Exception $e Exception object
294 294
 	 */
295
-	public function print_notice_exception( \Exception $e ) {
295
+	public function print_notice_exception(\Exception $e) {
296 296
 
297
-		add_action( 'admin_notices', static function() use ( $e ) {
297
+		add_action('admin_notices', static function() use ($e) {
298 298
 
299 299
 			?>
300 300
 			<div class="error">
301
-				<p><?php printf( '%1$s: %2$s', self::NAME, $e->getMessage() ); ?></p>
301
+				<p><?php printf('%1$s: %2$s', self::NAME, $e->getMessage()); ?></p>
302 302
 			</div>
303 303
 			<?php
304 304
 
@@ -322,9 +322,9 @@  discard block
 block discarded – undo
322 322
 		<div class="update-nag">
323 323
 			<p><?php printf(
324 324
 				/* translators: Placeholders: %1$s - WP Php Console name, %2$s - opening HTML <a> link tag; %3$s closing HTML </a> link tag */
325
-				__( '%1$s: Please remember to %2$sset a password%3$s if you want to enable the terminal.', 'wp-php-console' ),
326
-				'<strong>' . self::NAME . '</strong>',
327
-				'<a href="' . esc_url( admin_url( 'options-general.php?page=wp-php-console' ) ) .'">',
325
+				__('%1$s: Please remember to %2$sset a password%3$s if you want to enable the terminal.', 'wp-php-console'),
326
+				'<strong>'.self::NAME.'</strong>',
327
+				'<a href="'.esc_url(admin_url('options-general.php?page=wp-php-console')).'">',
328 328
 				'</a>'
329 329
 			); ?></p>
330 330
 		</div>
Please login to merge, or discard this patch.