PluginProbe
WP Debugging / 2.4.2
WP Debugging v2.4.2
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
wp-debugging / src / Settings.php

Settings.php in WP Debugging 2.4.2, at src/Settings.php

361 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP Debugging
4 *
5 * @package wp-debugging
6 * @author Andy Fragen
7 * @license MIT
8 */
9
10 namespace Fragen\WP_Debugging;
11
12 /**
13 * Class Settings
14 */
15 class Settings {
16 /**
17 * Hold plugin options.
18 *
19 * @var array
20 */
21 protected static $options;
22
23 /**
24 * Hold `wp-config.php` file path.
25 *
26 * @var string
27 */
28 protected static $config_path;
29
30 /**
31 * Constructor.
32 *
33 * @param array $options Plugin options.
34 * @param string $config_path Path to config file.
35 * @return void
36 */
37 public function __construct( $options, $config_path ) {
38 self::$options = $options;
39 self::$config_path = $config_path;
40 }
41
42 /**
43 * Load hooks for settings.
44 *
45 * @return void
46 */
47 public function load_hooks() {
48 add_action( 'admin_init', [ $this, 'add_settings' ] );
49 add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', [ $this, 'add_plugin_menu' ] );
50 add_action( 'network_admin_edit_wp_debugging', [ $this, 'update_settings' ] );
51 add_action( 'admin_init', [ $this, 'update_settings' ] );
52 add_filter(
53 is_multisite()
54 ? 'network_admin_plugin_action_links_wp-debugging/wp-debugging.php'
55 : 'plugin_action_links_wp-debugging/wp-debugging.php',
56 [ $this, 'plugin_action_links' ]
57 );
58 }
59
60 /**
61 * Add plugin menu.
62 *
63 * @return void
64 */
65 public function add_plugin_menu() {
66 $parent = is_multisite() ? 'settings.php' : 'tools.php';
67 $capability = is_multisite() ? 'manage_network' : 'manage_options';
68
69 add_submenu_page(
70 $parent,
71 esc_html__( 'WP Debugging', 'wp-debugging' ),
72 esc_html__( 'WP Debugging', 'wp-debugging' ),
73 $capability,
74 'wp-debugging',
75 [ $this, 'create_settings_page' ]
76 );
77 }
78
79 /**
80 * Update settings on save.
81 *
82 * @return void
83 */
84 public function update_settings() {
85 if ( isset( $_POST['option_page'] ) &&
86 'wp_debugging' === $_POST['option_page']
87 ) {
88 $options = isset( $_POST['wp-debugging'] )
89 ? $_POST['wp-debugging']
90 : [];
91 $options = self::sanitize( $options );
92 $this->update_constants( self::$options, $options );
93 $filtered_options = array_filter(
94 self::$options,
95 function ( $e ) {
96 return '1' !== $e;
97 }
98 );
99 $options = array_merge( $filtered_options, $options );
100 update_site_option( 'wp_debugging', (array) $options );
101 $this->redirect_on_save();
102 }
103 }
104
105 /**
106 * Update constants in wp-config.php.
107 *
108 * @param array $old Current value of self::$options.
109 * @param mixed $new New value of $options.
110 * @return void
111 */
112 private function update_constants( $old, $new ) {
113 $remove = array_diff_assoc( $old, $new );
114 $add = array_diff_assoc( $new, $old );
115
116 if ( ! empty( $add ) ) {
117 $this->add_constants( $add );
118 }
119 if ( ! empty( $remove ) ) {
120 $this->remove_constants( $remove );
121 }
122 }
123
124 /**
125 * Add constants to wp-config.php file.
126 *
127 * @uses https://github.com/wp-cli/wp-config-transformer
128 *
129 * @param array $add Constants to add to wp-config.php.
130 * @return void
131 */
132 private function add_constants( $add ) {
133 $config_transformer = new \WPConfigTransformer( self::$config_path );
134 $config_args = [
135 'raw' => true,
136 'normalize' => true,
137 ];
138 foreach ( array_keys( $add ) as $constant ) {
139 $value = 'wp_debug_display' === $constant ? 'false' : 'true';
140 $config_transformer->update( 'constant', strtoupper( $constant ), $value, $config_args );
141 }
142 }
143
144 /**
145 * Remove constants from wp-config.php file.
146 *
147 * @uses https://github.com/wp-cli/wp-config-transformer
148 *
149 * @param array $remove Constants to remove from wp-config.php.
150 * @return void
151 */
152 private function remove_constants( $remove ) {
153 $config_transformer = new \WPConfigTransformer( self::$config_path );
154 foreach ( array_keys( $remove ) as $constant ) {
155 $config_transformer->remove( 'constant', strtoupper( $constant ) );
156 }
157 }
158
159 /**
160 * Redirect back to settings page on save.
161 *
162 * @return void
163 */
164 private function redirect_on_save() {
165 $update = false;
166 if ( ( isset( $_POST['action'] ) && 'update' === $_POST['action'] ) &&
167 ( isset( $_POST['option_page'] ) && 'wp_debugging' === $_POST['option_page'] )
168 ) {
169 $update = true;
170 }
171
172 $redirect_url = is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
173
174 if ( $update ) {
175 $location = add_query_arg(
176 [
177 'page' => 'wp-debugging',
178 'updated' => $update,
179 ],
180 $redirect_url
181 );
182 wp_safe_redirect( $location );
183 exit;
184 }
185 }
186
187 /**
188 * Add notice when settings are saved.
189 *
190 * @return void
191 */
192 private function saved_settings_notice() {
193 if ( ( isset( $_GET['updated'] ) && true == $_GET['updated'] ) ||
194 ( isset( $_GET['settings-updated'] ) && true == $_GET['settings-updated'] )
195 ) {
196 echo '<div class="updated"><p>';
197 esc_html_e( 'Saved.', 'wp-debugging' );
198 echo '</p></div>';
199 }
200 }
201
202 /**
203 * Register settings.
204 *
205 * @return void
206 */
207 public function add_settings() {
208 register_setting(
209 'wp_debugging',
210 'wp_debugging',
211 [ $this, 'sanitize' ]
212 );
213
214 add_settings_section(
215 'wp_debugging',
216 esc_html__( 'Debugging Constants', 'wp-debugging' ),
217 [ $this, 'print_settings_section' ],
218 'wp_debugging'
219 );
220
221 add_settings_field(
222 'wp_debug',
223 null,
224 [ $this, 'checkbox_setting' ],
225 'wp_debugging',
226 'wp_debugging',
227 [
228 'id' => 'wp_debug',
229 'title' => esc_html__( 'Set WP_DEBUG to true.', 'wp-debugging' ),
230 ]
231 );
232
233 add_settings_field(
234 'wp_debug_display',
235 null,
236 [ $this, 'checkbox_setting' ],
237 'wp_debugging',
238 'wp_debugging',
239 [
240 'id' => 'wp_debug_display',
241 'title' => esc_html__( 'Set WP_DEBUG_DISPLAY to false, default is true.', 'wp-debugging' ),
242 ]
243 );
244
245 if ( version_compare( get_bloginfo( 'version' ), '5.2-beta', '>=' ) ) {
246 add_settings_field(
247 'wp_disable_fatal_error_handler',
248 null,
249 [ $this, 'checkbox_setting' ],
250 'wp_debugging',
251 'wp_debugging',
252 [
253 'id' => 'wp_disable_fatal_error_handler',
254 'title' => esc_html__( 'Set WP_DISABLE_FATAL_ERROR_HANDLER to true.', 'wp-debugging' ),
255 ]
256 );
257 }
258 }
259
260 /**
261 * Print settings section information.
262 *
263 * @return void
264 */
265 public function print_settings_section() {
266 esc_html_e( 'The following constants are set with plugin activation and removed with plugin deactivation.', 'wp-debugging' );
267 $this->print_constants();
268 esc_html_e( 'Select the debugging constants.', 'wp-debugging' );
269 }
270
271 /**
272 * Print current constants.
273 *
274 * @return void
275 */
276 private function print_constants() {
277 $constants = [ 'wp_debug_log', 'script_debug', 'savequeries' ];
278 $constants = array_merge( array_keys( self::$options ), $constants );
279 echo '<pre>';
280 foreach ( $constants as $constant ) {
281 $value = 'wp_debug_display' === $constant ? 'false' : 'true';
282 $constant = strtoupper( $constant );
283 echo( wp_kses_post( "<code>define( '{$constant}', {$value} );</code><br>" ) );
284 }
285 echo '</pre>';
286 }
287
288 /**
289 * Create settings page.
290 *
291 * @return void
292 */
293 public function create_settings_page() {
294 $this->saved_settings_notice();
295 $action = is_multisite() ? 'edit.php?action=wp-debugging' : 'options.php'; ?>
296 <div class="wrap">
297 <h1><?php esc_html_e( 'WP Debugging', 'wp-debugging' ); ?></h1>
298 <div class="updated fade">
299 <p><?php echo wp_kses_post( __( '<strong>Please note:</strong> Your <code>wp-config.php</code> file must be writable by the filesystem. Any errors will result in a PHP Exception being thrown. Debug constants per <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a>.', 'wp-debugging' ) ); ?></p>
300 </div>
301 <div>
302 <form method="post" action="<?php esc_attr_e( $action ); ?>">
303 <?php settings_fields( 'wp_debugging' ); ?>
304 <?php do_settings_sections( 'wp_debugging' ); ?>
305 <?php submit_button(); ?>
306 </form>
307 </div>
308 <?php
309 }
310
311 /**
312 * Sanitize save settings.
313 *
314 * @param array $input Input.
315 * @return array $new_input Sanitized output.
316 */
317 public function sanitize( $input ) {
318 $new_input = [];
319 if ( ! is_array( $input ) ) {
320 $new_input = sanitize_text_field( $input );
321 } else {
322 foreach ( array_keys( (array) $input ) as $id ) {
323 $new_input[ sanitize_text_field( $id ) ] = sanitize_text_field( $input[ $id ] );
324 }
325 }
326
327 return $new_input;
328 }
329
330 /**
331 * Get the settings option array and print one of its values.
332 *
333 * @param array $args 'id' and 'title'
334 */
335 public function checkbox_setting( $args ) {
336 $checked = isset( self::$options[ $args['id'] ] ) ? self::$options[ $args['id'] ] : null;
337 ?>
338 <style> .form-table th { display:none; } </style>
339 <label for="<?php esc_attr_e( $args['id'] ); ?>">
340 <input type="checkbox" name="wp-debugging[<?php esc_attr_e( $args['id'] ); ?>]" value="1" <?php checked( '1', $checked ); ?> >
341 <?php echo $args['title']; ?>
342 </label>
343 <?php
344 }
345
346 /**
347 * Add setting link to plugin page.
348 * Applied to the list of links to display on the plugins page (beside the activate/deactivate links).
349 *
350 * @param array $links Plugin links on plugins.php.
351 *
352 * @return array
353 */
354 public function plugin_action_links( $links ) {
355 $settings_page = is_multisite() ? 'settings.php' : 'tools.php';
356 $link = [ '<a href="' . esc_url( network_admin_url( $settings_page ) ) . '?page=wp-debugging">' . esc_html__( 'Settings', 'wp-debugging' ) . '</a>' ];
357
358 return array_merge( $links, $link );
359 }
360 }
361