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
wp-debugging / src / Settings.php

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

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