PluginProbe
WP Debugging / 2.2.0
WP Debugging v2.2.0
2.12.6 2.12.5 trunk 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.11.10 2.11.11 2.11.12 2.11.13 2.11.14 2.11.15 2.11.16 2.11.17 2.11.18 2.11.19 2.11.2 2.11.20 2.11.21 2.11.22 2.11.23 2.11.24 2.11.3 All 59 releases
← All changes | src/Settings.php +39 -162 2.10.02.2.0 View file →
@@ -8,13 +8,8 @@
8 8 */
9 9
10 10 namespace Fragen\WP_Debugging;
11 11
12 -// Exit if called directly.
13 -if ( ! defined( 'WPINC' ) ) {
14 - die;
15 -}
16 -
17 12 /**
18 13 * Class Settings
19 14 */
20 15 class Settings {
@@ -20,63 +15,26 @@
20 15 class Settings {
21 16 /**
22 17 * Hold plugin options.
23 18 *
24 - * @var array
19 + * @var array $options
25 20 */
26 21 protected static $options;
27 22
28 23 /**
29 - * Hold `wp-config.php` file path.
30 - *
31 - * @var string
32 - */
33 - protected static $config_path;
34 -
35 - /**
36 - * Holds pre-defined constants for `wp-config.php`.
37 - *
38 - * @var array
39 - */
40 - protected $defined_constants;
41 -
42 - /**
43 - * Holds config args for WPConfigTransformer.
44 - *
45 - * @var array
46 - */
47 - protected static $config_args;
48 -
49 - /**
50 24 * Constructor.
51 25 *
52 - * @param array $options Plugin options.
53 - * @param string $config_path Path to config file.
54 - * @param array $defined_constants Pre-defined constant group.
26 + * @param array $options Plugin options.
55 27 * @return void
56 28 */
57 - public function __construct( $options, $config_path, $defined_constants ) {
58 - self::$options = $options;
59 - self::$config_path = $config_path;
60 - $this->defined_constants = $defined_constants;
61 - self::$config_args = [ 'normalize' => true ];
62 -
63 - if ( false === strpos( file_get_contents( self::$config_path ), "/* That's all, stop editing!" ) ) {
64 - preg_match( '@\$table_prefix = (.*);@', file_get_contents( self::$config_path ), $matches );
65 - self::$config_args = array_merge(
66 - self::$config_args,
67 - [
68 - 'anchor' => "$matches[0]",
69 - 'placement' => 'after',
70 - ]
71 - );
72 - }
29 + public function __construct( $options ) {
30 + self::$options = $options;
73 31 }
74 32
75 33 /**
76 34 * Load hooks for settings.
77 35 *
78 - * @return self
36 + * @return void
79 37 */
80 38 public function load_hooks() {
81 39 add_action( 'admin_init', [ $this, 'add_settings' ] );
82 40 add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', [ $this, 'add_plugin_menu' ] );
@@ -87,10 +45,8 @@
87 45 ? 'network_admin_plugin_action_links_wp-debugging/wp-debugging.php'
88 46 : 'plugin_action_links_wp-debugging/wp-debugging.php',
89 47 [ $this, 'plugin_action_links' ]
90 48 );
91 -
92 - return $this;
93 49 }
94 50
95 51 /**
96 52 * Add plugin menu.
@@ -98,14 +54,14 @@
98 54 * @return void
99 55 */
100 56 public function add_plugin_menu() {
101 57 $parent = is_multisite() ? 'settings.php' : 'tools.php';
102 - $capability = is_multisite() ? 'manage_network_options' : 'manage_options';
58 + $capability = is_multisite() ? 'manage_network' : 'manage_options';
103 59
104 60 add_submenu_page(
105 61 $parent,
106 62 esc_html__( 'WP Debugging', 'wp-debugging' ),
107 - esc_html_x( 'WP Debugging', 'Menu item', 'wp-debugging' ),
63 + esc_html__( 'WP Debugging', 'wp-debugging' ),
108 64 $capability,
109 65 'wp-debugging',
110 66 [ $this, 'create_settings_page' ]
111 67 );
@@ -113,10 +69,8 @@
113 69
114 70 /**
115 71 * Update settings on save.
116 72 *
117 - * phpcs:disable WordPress.Security.NonceVerification.Missing
118 - *
119 73 * @return void
120 74 */
121 75 public function update_settings() {
122 76 if ( isset( $_POST['option_page'] ) &&
@@ -122,14 +76,11 @@
122 76 if ( isset( $_POST['option_page'] ) &&
123 77 'wp_debugging' === $_POST['option_page']
124 78 ) {
125 79 $options = isset( $_POST['wp-debugging'] )
126 - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
127 - ? wp_unslash( $_POST['wp-debugging'] )
80 + ? $_POST['wp-debugging']
128 81 : [];
129 - // phpcs:enable
130 -
131 - $options = $this->sanitize( $options );
82 + $options = self::sanitize( $options );
132 83 $this->update_constants( self::$options, $options );
133 84 $filtered_options = array_filter(
134 85 self::$options,
135 86 function ( $e ) {
@@ -166,66 +117,20 @@
166 117 *
167 118 * @uses https://github.com/wp-cli/wp-config-transformer
168 119 *
169 120 * @param array $add Constants to add to wp-config.php.
170 - * @return array $added Array of added constants.
171 - */
172 - public function add_constants( $add ) {
173 - $added = [];
174 - try {
175 - $config_transformer = new \WPConfigTransformer( self::$config_path );
176 - foreach ( $add as $constant => $config ) {
177 - $value = 'wp_debug_display' === $constant ? 'false' : 'true';
178 - $value = isset( $config['value'] ) ? $config['value'] : $value;
179 - $raw = isset( $config['raw'] ) ? $config['raw'] : true;
180 - self::$config_args = array_merge( self::$config_args, [ 'raw' => $raw ] );
181 - $config_transformer->update( 'constant', strtoupper( $constant ), $value, self::$config_args );
182 - $added[ $constant ] = $value;
183 - }
184 -
185 - return $added;
186 - } catch ( \Exception $e ) {
187 - $messsage = 'Caught Exception: \Fragen\WP_Debugging\Settings::add_constants() - ' . $e->getMessage();
188 - // error_log( $messsage );
189 - wp_die( esc_html( $messsage ) );
190 - }
191 - }
192 -
193 - /**
194 - * Process user defined constants added via filter.
195 - *
196 121 * @return void
197 122 */
198 - public function process_filter_constants() {
199 - /**
200 - * Filter to add user define constants.
201 - *
202 - * @since 2.5.0
203 - *
204 - * @param array Array of added constants.
205 - * The format for adding constants is to return an array of arrays.
206 - * Each array will have the constant as the key with an array of configuration data.
207 - * array(
208 - * 'my_constant' =>
209 - * array(
210 - * 'value' => $value, @type string
211 - * 'raw' => $raw, // Optional. @type bool $raw is a boolean where false will return the value in quotes and true will return the raw value. Default is true.
212 - * ),
213 - * )
214 - * Default is an empty array.
215 - */
216 - $filter_constants = apply_filters( 'wp_debugging_add_constants', [] );
217 - $remove_user_defined = array_diff( self::$options, array_flip( $this->defined_constants ) );
218 -
219 - // Remove and re-add user defined constants. Clean up for when filter removed or changed.
220 - if ( ! empty( $remove_user_defined ) ) {
221 - $this->remove_constants( $remove_user_defined );
123 + private function add_constants( $add ) {
124 + $config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' );
125 + $config_args = [
126 + 'raw' => true,
127 + 'normalize' => true,
128 + ];
129 + foreach ( array_keys( $add ) as $constant ) {
130 + $value = 'wp_debug_display' === $constant ? 'false' : 'true';
131 + $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args );
222 132 }
223 - $added_constants = $this->add_constants( $filter_constants );
224 -
225 - $options = array_diff( self::$options, $remove_user_defined );
226 - self::$options = array_merge( $options, $added_constants );
227 - update_site_option( 'wp_debugging', (array) self::$options );
228 133 }
229 134
230 135 /**
231 136 * Remove constants from wp-config.php file.
@@ -234,18 +139,12 @@
234 139 *
235 140 * @param array $remove Constants to remove from wp-config.php.
236 141 * @return void
237 142 */
238 - public function remove_constants( $remove ) {
239 - try {
240 - $config_transformer = new \WPConfigTransformer( self::$config_path );
241 - foreach ( array_keys( $remove ) as $constant ) {
242 - $config_transformer->remove( 'constant', strtoupper( $constant ) );
243 - }
244 - } catch ( \Exception $e ) {
245 - $messsage = 'Caught Exception: \Fragen\WP_Debugging\Settings::remove_constants() - ' . $e->getMessage();
246 - // error_log( $messsage );
247 - wp_die( esc_html( $messsage ) );
143 + private function remove_constants( $remove ) {
144 + $config_transformer = new \WPConfigTransformer( ABSPATH . 'wp-config.php' );
145 + foreach ( array_keys( $remove ) as $constant ) {
146 + $config_transformer->remove( 'constant', strtoupper( $constant ) );
248 147 }
249 148 }
250 149
251 150 /**
@@ -250,10 +149,8 @@
250 149
251 150 /**
252 151 * Redirect back to settings page on save.
253 152 *
254 - * phpcs:disable WordPress.Security.NonceVerification.Missing
255 - *
256 153 * @return void
257 154 */
258 155 private function redirect_on_save() {
259 156 $update = false;
@@ -261,9 +158,8 @@
261 158 ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] )
262 159 ) {
263 160 $update = true;
264 161 }
265 - // phpcs:enable
266 162
267 163 $redirect_url = is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
268 164
269 165 if ( $update ) {
@@ -281,11 +177,8 @@
281 177
282 178 /**
283 179 * Add notice when settings are saved.
284 180 *
285 - * phpcs:disable WordPress.PHP.StrictComparisons.LooseComparison
286 - * phpcs:disable WordPress.Security.NonceVerification.Recommended
287 - *
288 181 * @return void
289 182 */
290 183 private function saved_settings_notice() {
291 184 if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) ||
@@ -294,9 +187,8 @@
294 187 echo '<div class="updated"><p>';
295 188 esc_html_e( 'Saved.', 'wp-debugging' );
296 189 echo '</p></div>';
297 190 }
298 - // phpcs:enable
299 191 }
300 192
301 193 /**
302 194 * Register settings.
@@ -340,20 +232,21 @@
340 232 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ),
341 233 ]
342 234 );
343 235
344 - add_settings_field(
345 - 'wp_disable_fatal_error_handler',
346 - null,
347 - [ $this, 'checkbox_setting' ],
348 - 'wp_debugging',
349 - 'wp_debugging',
350 - [
351 - 'id' => 'wp_disable_fatal_error_handler',
352 - 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ),
353 - 'class' => version_compare( get_bloginfo( 'version' ), '5.2', '>=' ) ? '' : 'hidden',
354 - ]
355 - );
236 + if ( version_compare( get_bloginfo( 'version' ), '5.2-beta', '>=' ) ) {
237 + add_settings_field(
238 + 'wp_disable_fatal_error_handler',
239 + null,
240 + [ $this, 'checkbox_setting' ],
241 + 'wp_debugging',
242 + 'wp_debugging',
243 + [
244 + 'id' => 'wp_disable_fatal_error_handler',
245 + 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ),
246 + ]
247 + );
248 + }
356 249 }
357 250
358 251 /**
359 252 * Print settings section information.
@@ -371,32 +264,16 @@
371 264 *
372 265 * @return void
373 266 */
374 267 private function print_constants() {
375 - $added_constants = apply_filters( 'wp_debugging_add_constants', [] );
376 - $additional_constants = [];
377 - if ( $added_constants ) {
378 - foreach ( $added_constants as $constant => $config ) {
379 - $additional_constants[ $constant ] = $config['value'];
380 - }
381 - }
382 -
383 - // Strip user defined constants from $constants array.
384 268 $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ];
385 269 $constants = array_merge( array_keys( self::$options ), $constants );
386 - $constants = array_diff( $constants, array_keys( $additional_constants ) );
387 -
388 270 echo '<pre>';
389 271 foreach ( $constants as $constant ) {
390 272 $value = 'wp_debug_display' === $constant ? 'false' : 'true';
391 273 $constant = strtoupper( $constant );
392 - echo wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" );
274 + echo( wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" ) );
393 275 }
394 - foreach ( $additional_constants as $constant => $value ) {
395 - $value = in_array( $value, [ 'true', 'false' ], true ) ? $value : "'$value'";
396 - $constant = strtoupper( $constant );
397 - echo wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" );
398 - }
399 276 echo '</pre>';
400 277 }
401 278
402 279 /**
@@ -443,9 +320,9 @@
443 320
444 321 /**
445 322 * Get the settings option array and print one of its values.
446 323 *
447 - * @param array $args 'id' and 'title'.
324 + * @param array $args 'id' and 'title'
448 325 */
449 326 public function checkbox_setting( $args ) {
450 327 $checked = isset( self::$options[ $args['id'] ] ) ? self::$options[ $args['id'] ] : null;
451 328 ?>
@@ -451,9 +328,9 @@
451 328 ?>
452 329 <style> .form-table th { display:none; } </style>
453 330 <label for="<?php esc_attr_e( $args['id'] ); ?>">
454 331 <input type="checkbox" name="wp-debugging[<?php esc_attr_e( $args['id'] ); ?>]" value="1" <?php checked( '1', $checked ); ?> >
455 - <?php esc_html_e( $args['title'] ); ?>
332 + <?php echo $args['title']; ?>
456 333 </label>
457 334 <?php
458 335 }
459 336
@@ -468,7 +345,7 @@
468 345 public function plugin_action_links( $links ) {
469 346 $settings_page = is_multisite() ? 'settings.php' : 'tools.php';
470 347 $link = [ '<a href="' . esc_url( network_admin_url( $settings_page ) ) . '?page=wp-debugging">' . esc_html__( 'Settings', 'wp-debugging' ) . '</a>' ];
471 348
472 - return array_merge( $link, $links );
349 + return array_merge( $links, $link );
473 350 }
474 351 }