| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles all necessary logic for adjusting the wp-config.php. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Config; |
| 11 |
|
| 12 |
use Exception; |
| 13 |
use SolidWP\Performance\Notices\Notice; |
| 14 |
use SolidWP\Performance\Notices\Notice_Handler; |
| 15 |
use SolidWP_Performance_WPConfigTransformer as WPConfigTransformer; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Provides some helper functions related to the WP_Config |
| 23 |
* |
| 24 |
* @since 0.1.0 |
| 25 |
* |
| 26 |
* @package SolidWP\Performance |
| 27 |
*/ |
| 28 |
class WP_Config { |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Notice_Handler |
| 32 |
*/ |
| 33 |
private Notice_Handler $notice_handler; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param Notice_Handler $notice_handler The notice handler. |
| 37 |
*/ |
| 38 |
public function __construct( Notice_Handler $notice_handler ) { |
| 39 |
$this->notice_handler = $notice_handler; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Gets the full path of the site's wp-config.php file. |
| 44 |
* |
| 45 |
* @since 0.1.0 |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function get_wp_config_file_path(): string { |
| 50 |
$path = ''; |
| 51 |
|
| 52 |
if ( getenv( 'WP_CONFIG_PATH' ) && file_exists( getenv( 'WP_CONFIG_PATH' ) ) ) { |
| 53 |
$path = getenv( 'WP_CONFIG_PATH' ); |
| 54 |
} elseif ( file_exists( ABSPATH . 'wp-config.php' ) ) { |
| 55 |
$path = ABSPATH . 'wp-config.php'; |
| 56 |
} elseif ( file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { |
| 57 |
$path = dirname( ABSPATH ) . '/wp-config.php'; |
| 58 |
} |
| 59 |
|
| 60 |
if ( $path ) { |
| 61 |
$path = realpath( $path ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Filter the config path. |
| 66 |
* |
| 67 |
* @since 0.1.0 |
| 68 |
* |
| 69 |
* @param string $path Absolute path to the config file. |
| 70 |
*/ |
| 71 |
return (string) apply_filters( 'solidwp/performance/get_wp_config_file_path', $path ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Adds the WP_CACHE constant if it's not already set or updates its value. |
| 76 |
* |
| 77 |
* The WP_CACHE constant configures WordPress to load the advanced-cache.php |
| 78 |
* drop-in. |
| 79 |
* |
| 80 |
* @since 0.1.0 |
| 81 |
* |
| 82 |
* @see https://github.com/wp-cli/wp-cli/blob/a339dca576df73c31af4b4d8054efc2dab9a0685/php/utils.php#L285-L310 |
| 83 |
* @see https://github.com/wp-cli/wp-config-transformer |
| 84 |
* |
| 85 |
* @param string $value The value to assign WP_CACHE (i.e. 'true', 'false'). |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public function set_wp_cache( string $value ): bool { |
| 90 |
|
| 91 |
$config_transformer = $this->get_config_transformer(); |
| 92 |
|
| 93 |
if ( $config_transformer === null ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
try { |
| 98 |
// Add WP_CACHE constant to the wp-config.php. |
| 99 |
$config_transformer->update( |
| 100 |
'constant', |
| 101 |
'WP_CACHE', |
| 102 |
$value, |
| 103 |
[ |
| 104 |
'raw' => true, |
| 105 |
] |
| 106 |
); |
| 107 |
|
| 108 |
} catch ( Exception $e ) { |
| 109 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 110 |
error_log( $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 111 |
} |
| 112 |
|
| 113 |
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
| 114 |
$notice = new Notice( |
| 115 |
'error', |
| 116 |
'Unable to write changes to <code>wp-config.php</code>, please add <code>define( \'WP_CACHE\', true );</code> manually to enable page caching.', |
| 117 |
true |
| 118 |
); |
| 119 |
$this->notice_handler->add( $notice ); |
| 120 |
} |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Gets the current state of the 'WP_CACHE' constant. |
| 130 |
* |
| 131 |
* @since 0.1.0 |
| 132 |
* |
| 133 |
* @return bool |
| 134 |
*/ |
| 135 |
public function get_wp_cache_status(): bool { |
| 136 |
if ( ! defined( 'WP_CACHE' ) ) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
$config_transformer = $this->get_config_transformer(); |
| 141 |
|
| 142 |
if ( $config_transformer === null ) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
try { |
| 147 |
if ( ! $config_transformer->exists( 'constant', 'WP_CACHE' ) ) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
$status = $config_transformer->get_value( 'constant', 'WP_CACHE' ); |
| 152 |
|
| 153 |
} catch ( Exception $e ) { |
| 154 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 155 |
error_log( $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 156 |
} |
| 157 |
|
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
return $status === 'true'; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Undocumented function |
| 166 |
* |
| 167 |
* @return WPConfigTransformer|null |
| 168 |
*/ |
| 169 |
protected function get_config_transformer(): ?WPConfigTransformer { |
| 170 |
try { |
| 171 |
$wp_config_transformer = new WPConfigTransformer( self::get_wp_config_file_path() ); |
| 172 |
} catch ( Exception $e ) { |
| 173 |
$notice = new Notice( 'error', 'Unable to write changes to <code>wp-config.php</code>, please add <code>define( \'WP_CACHE\', true );</code> manually to enable page caching.', true ); |
| 174 |
$this->notice_handler->add( $notice ); |
| 175 |
|
| 176 |
return null; |
| 177 |
} |
| 178 |
|
| 179 |
return $wp_config_transformer; |
| 180 |
} |
| 181 |
} |
| 182 |
|