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 | } |