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 7.6.8 7.6.9 7.7.0 7.8.0 7.8.1 7.8.10 7.8.11 7.8.12 7.8.13 7.8.14 7.8.15 7.8.16 7.8.17 7.8.18 7.8.2 7.8.3 7.8.4 7.8.5 7.8.5.1 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1 7.9.10 7.9.11 7.9.2 7.9.3 7.9.4 7.9.5 7.9.6 7.9.7 7.9.8 7.9.9 8.0.0 8.0.1 8.0.2 8.0.3 8.0.4 8.0.5 8.0.6 8.0.7 8.0.8 8.1.0 8.1.1 8.1.2 8.1.3 8.1.4 8.2.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.2.0 3.3.0 3.4.0 3.5.0 3.6.1 3.7.0 3.8.0 3.9.0 3.9.1 3.9.2 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.9.0 4.9.1 4.9.2 4.9.3 5.0.0
admin-site-enhancements / classes / class-wp-config-transformer.php
admin-site-enhancements / classes Last commit date
class-activation.php 7 months ago class-admin-menu-organizer.php 7 months ago class-admin-menu-svg-icon-mask.php 7 months ago class-auto-publish-posts-with-missed-schedule.php 7 months ago class-avif-upload.php 7 months ago class-captcha-protection.php 7 months ago class-change-login-url.php 7 months ago class-cleanup-admin-bar.php 7 months ago class-common-methods.php 7 months ago class-content-duplication.php 7 months ago class-content-order.php 7 months ago class-custom-admin-footer-text.php 7 months ago class-custom-body-class.php 7 months ago class-custom-css.php 7 months ago class-custom-nav-menu-items-in-new-tab.php 7 months ago class-deactivation.php 7 months ago class-disable-author-archives.php 7 months ago class-disable-comments.php 7 months ago class-disable-dashboard-widgets.php 7 months ago class-disable-embeds.php 7 months ago class-disable-feeds.php 7 months ago class-disable-gutenberg.php 7 months ago class-disable-rest-api.php 7 months ago class-disable-smaller-components.php 7 months ago class-disable-updates.php 7 months ago class-disable-xml-rpc.php 7 months ago class-display-system-summary.php 7 months ago class-email-address-obfuscator.php 7 months ago class-email-delivery.php 7 months ago class-enhance-list-tables.php 7 months ago class-external-permalinks.php 7 months ago class-heartbeat-control.php 7 months ago class-hide-admin-bar.php 7 months ago class-hide-admin-notices.php 7 months ago class-image-sizes-panel.php 7 months ago class-image-upload-control.php 7 months ago class-insert-head-body-footer-code.php 7 months ago class-last-login-column.php 7 months ago class-limit-login-attempts.php 7 months ago class-login-id-type.php 7 months ago class-login-logout-menu.php 7 months ago class-maintenance-mode.php 7 months ago class-manage-ads-appads-txt.php 7 months ago class-manage-robots-txt.php 7 months ago class-media-replacement.php 7 months ago class-multiple-user-roles.php 7 months ago class-obfuscate-author-slugs.php 7 months ago class-open-external-links-in-new-tab.php 7 months ago class-password-protection.php 7 months ago class-redirect-after-login.php 7 months ago class-redirect-after-logout.php 7 months ago class-redirect-fourofour.php 7 months ago class-registration-date-column.php 7 months ago class-revisions-control.php 7 months ago class-search-engines-visibility.php 7 months ago class-settings-fields-render.php 7 months ago class-settings-sanitization.php 7 months ago class-settings-sections-fields.php 7 months ago class-show-custom-taxonomy-filters.php 7 months ago class-site-identity-on-login-page.php 7 months ago class-svg-upload.php 7 months ago class-various-admin-ui-enhancements.php 7 months ago class-view-admin-as-role.php 7 months ago class-wider-admin-menu.php 7 months ago class-wp-config-transformer.php 7 months ago
class-wp-config-transformer.php
438 lines
1 <?php
2
3 namespace ASENHA\Classes;
4
5 use Exception;
6
7 /**
8 * Class with methods to manipulate wp-config.php
9 *
10 * @since 1.0.0
11 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
12 */
13 class WP_Config_Transformer {
14
15 /**
16 * The wp-config.php source file
17 *
18 * @since 1.0.0
19 * @access private
20 * @var string $version The current version of this plugin.
21 */
22 private $wp_config_src;
23
24 /**
25 * The configs defined in wp-config.php
26 *
27 * @since 1.0.0
28 * @access private
29 * @var string $version The current version of this plugin.
30 */
31 private $wp_configs;
32
33 /**
34 * Get wp-config.php file path
35 *
36 * @since 1.0.0
37 */
38 public function wpconfig_file( $type = 'path' ) {
39
40 // From wp-load.php
41
42 if ( file_exists( ABSPATH . 'wp-config.php' ) ) {
43
44 /** The config file resides in ABSPATH */
45 $file = ABSPATH . 'wp-config.php';
46 $location = 'WordPress root directory';
47
48
49 } elseif ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {
50
51 /** The config file resides one level above ABSPATH but is not part of another installation */
52 $file = dirname( ABSPATH ) . '/wp-config.php';
53 $location = 'parent directory of WordPress root';
54
55 } else {
56
57 $file = 'Undetectable.';
58 $location = 'not in WordPress root or it\'s parent directory';
59
60 }
61
62 if ( !is_writable( $file ) ) {
63 $writeability = false;
64 } else {
65 $writeability = true;
66 }
67
68 if ( $type == 'path' ) {
69 return $file;
70 } elseif ( $type == 'location' ) {
71 return $location;
72 } elseif ( $type == 'writeability' ) {
73 return $writeability;
74 } elseif ( $type == 'status' ) {
75 return '<div class="ase-wpconfig-status" style="display: none;">The wp-config.php file is located in ' . $location . ' ('. $file . ') and is ' . ( $writeability ) ? 'writeable' : 'not writeable' .'.</div>';
76 }
77
78 }
79
80 /**
81 * Get configs in wp-config.php
82 *
83 * @since 1.0.0
84 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
85 */
86 public function configs( $return_type = 'raw' ) {
87
88 $src = file_get_contents( $this->wpconfig_file( 'path' ) );
89
90 $configs = array();
91 $configs['constant'] = array();
92 $configs['variable'] = array();
93
94 // Strip comments.
95 foreach ( token_get_all( $src ) as $token ) {
96 if ( in_array( $token[0], array( T_COMMENT, T_DOC_COMMENT ), true ) ) {
97 $src = str_replace( $token[1], '', $src );
98 }
99 }
100
101 preg_match_all( '/(?<=^|;|<\?php\s|<\?\s)(\h*define\s*\(\s*[\'"](\w*?)[\'"]\s*)(,\s*(\'\'|""|\'.*?[^\\\\]\'|".*?[^\\\\]"|.*?)\s*)((?:,\s*(?:true|false)\s*)?\)\s*;)/ims', $src, $constants );
102 preg_match_all( '/(?<=^|;|<\?php\s|<\?\s)(\h*\$(\w+)\s*=)(\s*(\'\'|""|\'.*?[^\\\\]\'|".*?[^\\\\]"|.*?)\s*;)/ims', $src, $variables );
103
104 if ( ! empty( $constants[0] ) && ! empty( $constants[1] ) && ! empty( $constants[2] ) && ! empty( $constants[3] ) && ! empty( $constants[4] ) && ! empty( $constants[5] ) ) {
105 foreach ( $constants[2] as $index => $name ) {
106 $configs['constant'][ $name ] = array(
107 'src' => $constants[0][ $index ],
108 'value' => $constants[4][ $index ],
109 'parts' => array(
110 $constants[1][ $index ],
111 $constants[3][ $index ],
112 $constants[5][ $index ],
113 ),
114 );
115 }
116 }
117
118 if ( ! empty( $variables[0] ) && ! empty( $variables[1] ) && ! empty( $variables[2] ) && ! empty( $variables[3] ) && ! empty( $variables[4] ) ) {
119 // Remove duplicate(s), last definition wins.
120 $variables[2] = array_reverse( array_unique( array_reverse( $variables[2], true ) ), true );
121 foreach ( $variables[2] as $index => $name ) {
122 $configs['variable'][ $name ] = array(
123 'src' => $variables[0][ $index ],
124 'value' => $variables[4][ $index ],
125 'parts' => array(
126 $variables[1][ $index ],
127 $variables[3][ $index ],
128 ),
129 );
130 }
131 }
132
133 $this->wp_configs = $configs;
134
135 if ( $return_type == 'raw' ) {
136 return $configs;
137 } elseif ( $return_type == 'print_r' ) {
138 return '<pre>' . print_r( $configs, true ) . '</pre>';
139 }
140 }
141
142 /**
143 * Checks if a config exists in the wp-config.php file.
144 *
145 * @throws Exception If the wp-config.php file is empty.
146 * @throws Exception If the requested config type is invalid.
147 *
148 * @param string $type Config type (constant or variable).
149 * @param string $name Config name.
150 *
151 * @return bool
152 * @since 1.0.0
153 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
154 */
155 public function exists( $type, $name ) {
156 $wp_config_src = file_get_contents( $this->wpconfig_file( 'path' ) );
157
158 if ( ! trim( $wp_config_src ) ) {
159 // throw new Exception( 'Config file is empty.' );
160 return false; // added in v7.5.4, if wp-config is empty, the $name is definitely not there.
161 }
162
163 // Normalize the newline to prevent an issue coming from OSX.
164 $this->wp_config_src = str_replace( array( "\n\r", "\r" ), "\n", $wp_config_src );
165
166 $this->wp_configs = $this->configs( 'raw' );
167
168 if ( ! isset( $this->wp_configs[ $type ] ) ) {
169 // throw new Exception( esc_html( "Config type '{$type}' does not exist." ) );
170 return false; // added in v7.5.4, if Config type '{$type}' does not exist, the $name is definitely not there.
171 }
172
173 return isset( $this->wp_configs[ $type ][ $name ] );
174 }
175
176 /**
177 * Get the value of a config in the wp-config.php file.
178 *
179 * @throws Exception If the wp-config.php file is empty.
180 * @throws Exception If the requested config type is invalid.
181 *
182 * @param string $type Config type (constant or variable).
183 * @param string $name Config name.
184 *
185 * @return array
186 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
187 */
188 public function get_value( $type, $name ) {
189 $wp_config_src = file_get_contents( $this->wpconfig_file( 'path' ) );
190
191 if ( ! trim( $wp_config_src ) ) {
192 throw new Exception( 'Config file is empty.' );
193 }
194
195 $this->wp_config_src = $wp_config_src;
196 $this->wp_configs = $this->configs( 'raw' );
197
198 if ( ! isset( $this->wp_configs[ $type ] ) ) {
199 throw new Exception( esc_html( "Config type '{$type}' does not exist." ) );
200 }
201
202 if ( isset( $this->wp_configs[ $type ][ $name ] ) ) {
203 return $this->wp_configs[ $type ][ $name ]['value'];
204 } else {
205 return '';
206 }
207 }
208
209 /**
210 * Adds a config to the wp-config.php file.
211 *
212 * @throws Exception If the config value provided is not a string.
213 * @throws Exception If the config placement anchor could not be located.
214 *
215 * @param string $type Config type (constant or variable).
216 * @param string $name Config name.
217 * @param string $value Config value.
218 * @param array $options (optional) Array of special behavior options.
219 *
220 * @return bool
221 * @since 1.0.0
222 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
223 */
224 public function add( $type, $name, $value, array $options = array() ) {
225 if ( ! is_string( $value ) ) {
226 throw new Exception( 'Config value must be a string.' );
227 }
228
229 if ( $this->exists( $type, $name ) ) {
230 return false;
231 }
232
233 if ( in_array( $value, array( 'true', 'false' ), true ) ) {
234 $raw_input = true;
235 } else {
236 $raw_input = false;
237 }
238
239 $wp_config_src = file_get_contents( $this->wpconfig_file( 'path' ) );
240
241 if ( false !== strpos( $wp_config_src, "Happy publishing" ) ) {
242 $anchor = "/* That's all, stop editing! Happy publishing. */";
243 } elseif ( false !== strpos( $wp_config_src, "Happy blogging" ) ) {
244 $anchor = "/* That's all, stop editing! Happy blogging. */";
245 } elseif ( false !== strpos( $wp_config_src, "Absolute path to" ) ) {
246 $anchor = "/** Absolute path to the WordPress directory. */";
247 } else {
248 $anchor = '/* Add any custom values between this line and the "stop editing" line. */';
249 }
250
251 $defaults = array(
252 'raw' => $raw_input, // Display value in raw format without quotes.
253 'anchor' => $anchor, // Config placement anchor string.
254 'separator' => PHP_EOL, // Separator between config definition and anchor string.
255 'placement' => 'before', // Config placement direction (insert before or after).
256 );
257
258 // list( $raw, $anchor, $separator, $placement ) = array_values( $options );
259 list( $raw, $anchor, $separator, $placement ) = array_values( array_merge( $defaults, $options ) );;
260
261 $raw = (bool) $raw;
262 $anchor = (string) $anchor;
263 $separator = (string) $separator;
264 $placement = (string) $placement;
265
266 if ( 'EOF' === $anchor ) {
267 $contents = $this->wp_config_src . $this->normalize( $type, $name, $this->format_value( $value, $raw ) );
268 } else {
269 if ( false === strpos( $this->wp_config_src, $anchor ) ) {
270 throw new Exception( 'Unable to locate placement anchor.' );
271 }
272
273 $new_src = $this->normalize( $type, $name, $this->format_value( $value, $raw ) );
274 $new_src = ( 'after' === $placement ) ? $anchor . $separator . $new_src : $new_src . $separator . $anchor;
275 $contents = str_replace( $anchor, $new_src, $this->wp_config_src );
276 }
277
278 return $this->save( $contents );
279 }
280
281 /**
282 * Updates an existing config in the wp-config.php file.
283 *
284 * @throws Exception If the config value provided is not a string.
285 *
286 * @param string $type Config type (constant or variable).
287 * @param string $name Config name.
288 * @param string $value Config value.
289 * @param array $options (optional) Array of special behavior options.
290 *
291 * @return bool
292 * @since 1.0.0
293 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
294 */
295 public function update( $type, $name, $value, array $options = array() ) {
296 if ( ! is_string( $value ) ) {
297 throw new Exception( 'Config value must be a string.' );
298 }
299
300 // $defaults = array(
301 // 'add' => true, // Add the config if missing.
302 // 'raw' => false, // Display value in raw format without quotes.
303 // 'normalize' => false, // Normalize config output using WP Coding Standards.
304 // );
305
306 // list( $add, $raw, $normalize ) = array_values( array_merge( $defaults, $options ) );
307 list( $add, $raw, $normalize ) = array_values( $options );
308
309 $add = (bool) $add;
310 $raw = (bool) $raw;
311 $normalize = (bool) $normalize;
312
313 if ( ! $this->exists( $type, $name ) ) {
314 return ( $add ) ? $this->add( $type, $name, $value ) : false;
315 }
316
317 $old_src = $this->wp_configs[ $type ][ $name ]['src'];
318 $old_value = $this->wp_configs[ $type ][ $name ]['value'];
319 $new_value = $this->format_value( $value, $raw );
320
321 if ( $normalize ) {
322 $new_src = $this->normalize( $type, $name, $new_value );
323 } else {
324 $new_parts = $this->wp_configs[ $type ][ $name ]['parts'];
325 $new_parts[1] = str_replace( $old_value, $new_value, $new_parts[1] ); // Only edit the value part.
326 $new_src = implode( '', $new_parts );
327 }
328
329 $contents = preg_replace(
330 sprintf( '/(?<=^|;|<\?php\s|<\?\s)(\s*?)%s/m', preg_quote( trim( $old_src ), '/' ) ),
331 '$1' . str_replace( '$', '\$', trim( $new_src ) ),
332 $this->wp_config_src
333 );
334
335 return $this->save( $contents );
336 }
337
338 /**
339 * Removes a config from the wp-config.php file.
340 *
341 * @param string $type Config type (constant or variable).
342 * @param string $name Config name.
343 *
344 * @return bool
345 * @since 1.0.0
346 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
347 */
348 public function remove( $type, $name ) {
349 if ( ! $this->exists( $type, $name ) ) {
350 return false;
351 }
352
353 $wp_config_src = file_get_contents( $this->wpconfig_file( 'path' ) );
354 $this->wp_config_src = str_replace( array( "\n\r", "\r" ), "\n", $wp_config_src );
355 $this->wp_configs = $this->configs( 'raw' );
356
357 $pattern = sprintf( '/(?<=^|;|<\?php\s|<\?\s)%s\s*(\S|$)/m', preg_quote( $this->wp_configs[$type][$name]['src'], '/' ) );
358 $contents = preg_replace( $pattern, '$1', $this->wp_config_src );
359
360 return $this->save( $contents );
361 }
362
363 /**
364 * Applies formatting to a config value.
365 *
366 * @throws Exception When a raw value is requested for an empty string.
367 *
368 * @param string $value Config value.
369 * @param bool $raw Display value in raw format without quotes.
370 *
371 * @return mixed
372 * @since 1.0.0
373 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
374 */
375 public function format_value( $value, $raw ) {
376 if ( $raw && '' === trim( $value ) ) {
377 throw new Exception( 'Raw value for empty string not supported.' );
378 }
379
380 return ( $raw ) ? $value : var_export( $value, true );
381 }
382
383 /**
384 * Normalizes the source output for a name/value pair.
385 *
386 * @throws Exception If the requested config type does not support normalization.
387 *
388 * @param string $type Config type (constant or variable).
389 * @param string $name Config name.
390 * @param mixed $value Config value.
391 *
392 * @return string
393 * @since 1.0.0
394 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
395 */
396 public function normalize( $type, $name, $value ) {
397 if ( 'constant' === $type ) {
398 $placeholder = "define( '%s', %s );";
399 } elseif ( 'variable' === $type ) {
400 $placeholder = '$%s = %s;';
401 } else {
402 throw new Exception( esc_html( "Unable to normalize config type '{$type}'." ) );
403 }
404
405 return sprintf( $placeholder, $name, $value );
406 }
407
408 /**
409 * Saves new contents to the wp-config.php file.
410 *
411 * @throws Exception If the config file content provided is empty.
412 * @throws Exception If there is a failure when saving the wp-config.php file.
413 *
414 * @param string $contents New config contents.
415 *
416 * @return bool
417 * @since 1.0.0
418 * @link https://plugins.svn.wordpress.org/debug-log-config-tool/tags/1.1/src/Classes/vendor/WPConfigTransformer.php
419 */
420 public function save( $contents ) {
421 if ( ! trim( $contents ) ) {
422 throw new Exception( 'Cannot save the config file with empty contents.' );
423 }
424
425 if ( $contents === $this->wp_config_src ) {
426 return false;
427 }
428
429 $result = file_put_contents( $this->wpconfig_file( 'path' ), $contents, LOCK_EX );
430
431 if ( false === $result ) {
432 throw new Exception( 'Failed to update the config file.' );
433 }
434
435 return true;
436 }
437
438 }