| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class for retrieving configuration values from a file. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Config\Strategies; |
| 11 |
|
| 12 |
use Closure; |
| 13 |
use SolidWP\Performance\Config\Config; |
| 14 |
use SolidWP\Performance\Config\Contracts\Strategy; |
| 15 |
use SolidWP\Performance\StellarWP\Arrays\Arr; |
| 16 |
|
| 17 |
/** |
| 18 |
* Stores and retrieves configuration values from a file. |
| 19 |
* |
| 20 |
* @since 0.1.0 |
| 21 |
* |
| 22 |
* @package SolidWP\Performance |
| 23 |
*/ |
| 24 |
class File implements Strategy { |
| 25 |
|
| 26 |
/** |
| 27 |
* The base path to the config directory. |
| 28 |
* |
| 29 |
* @since 0.1.0 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected string $config_dir = WP_CONTENT_DIR . '/cache/solid-performance'; |
| 34 |
|
| 35 |
/** |
| 36 |
* The config loaded from the site's config file. |
| 37 |
* |
| 38 |
* @since 0.1.0 |
| 39 |
* |
| 40 |
* @var array<string,mixed> |
| 41 |
*/ |
| 42 |
protected array $config = []; |
| 43 |
|
| 44 |
/** |
| 45 |
* Keys that should be saved in the config file. |
| 46 |
* |
| 47 |
* @since 0.1.0 |
| 48 |
* |
| 49 |
* @var array<int,string> |
| 50 |
*/ |
| 51 |
protected array $file_config_keys = [ |
| 52 |
'page_cache.cache_dir', |
| 53 |
'page_cache.debug', |
| 54 |
'page_cache.enabled', |
| 55 |
'page_cache.expiration', |
| 56 |
'page_cache.exclusions', |
| 57 |
]; |
| 58 |
|
| 59 |
/** |
| 60 |
* The class constructor. |
| 61 |
* |
| 62 |
* @since 0.1.0 |
| 63 |
*/ |
| 64 |
public function __construct() { |
| 65 |
if ( file_exists( $this->get_config_file_path() ) ) { |
| 66 |
$this->config = include $this->get_config_file_path(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* {@inheritdoc} |
| 72 |
*/ |
| 73 |
public function get( string $key, ?Closure $next = null ) { |
| 74 |
$value = Arr::get( $this->config, explode( '.', $key ) ); |
| 75 |
|
| 76 |
if ( isset( $value ) ) { |
| 77 |
return $value; |
| 78 |
} |
| 79 |
|
| 80 |
return $next === null ? null : $next( $key ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* {@inheritdoc} |
| 85 |
*/ |
| 86 |
public function save( array $changes, ?Closure $next = null ) { |
| 87 |
|
| 88 |
$defaults = Config::get_defaults(); |
| 89 |
|
| 90 |
// Undot exclusion values to prevent issues where they are transformed to have individual |
| 91 |
// keys per value (i.e. page_cache.exclusions.[n] = '/example/' ). |
| 92 |
if ( Arr::has( $changes, 'page_cache.exclusions.0' ) ) { |
| 93 |
$expanded_changes = Arr::undot( $changes ); |
| 94 |
$changes['page_cache.exclusions'] = $expanded_changes['page_cache']['exclusions']; |
| 95 |
} |
| 96 |
|
| 97 |
$responsible_changes = []; |
| 98 |
|
| 99 |
foreach ( $changes as $key => $value ) { |
| 100 |
// If the config file is responsible for the setting and does not match default. |
| 101 |
if ( $this->is_responsible_for( $key ) && $value !== Arr::get( $defaults, explode( '.', $key ) ) ) { |
| 102 |
$responsible_changes[ $key ] = $value; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$this->write_to_config( $this->get_config_file_path(), Arr::undot( $responsible_changes ) ); |
| 107 |
|
| 108 |
if ( $next !== null ) { |
| 109 |
return $next( $changes ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* {@inheritdoc} |
| 115 |
*/ |
| 116 |
public function is_responsible_for( string $option_key ): bool { |
| 117 |
return Arr::has( array_flip( $this->file_config_keys ), $option_key ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Gets the file path of the config file for a specific site. |
| 122 |
* |
| 123 |
* @since 0.1.0 |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
public function get_config_file_path(): string { |
| 128 |
return $this->config_dir . '/config.php'; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Writes a set of key value pairs to a config file. |
| 133 |
* |
| 134 |
* @since 0.1.0 |
| 135 |
* |
| 136 |
* @param string $path The file path to write to. |
| 137 |
* @param array $config The key value pairs to write. |
| 138 |
* |
| 139 |
* @return bool |
| 140 |
*/ |
| 141 |
protected function write_to_config( string $path, array $config ): bool { |
| 142 |
// Get the config as a string to write to file. |
| 143 |
$config = var_export( $config, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
| 144 |
|
| 145 |
// Make sure that the config directory exists. |
| 146 |
$config_dir = str_replace( '/config.php', '', $path ); |
| 147 |
|
| 148 |
if ( ! file_exists( $config_dir ) ) { |
| 149 |
wp_mkdir_p( $config_dir ); |
| 150 |
} |
| 151 |
|
| 152 |
// Manually create the return for the file. |
| 153 |
$contents = sprintf( |
| 154 |
'<?php |
| 155 |
/** |
| 156 |
* Automatically Generated by Solid Performance every time the settings page is saved. |
| 157 |
*/ |
| 158 |
return %s;', |
| 159 |
$config |
| 160 |
); |
| 161 |
|
| 162 |
return (bool) file_put_contents( $path, $contents ); |
| 163 |
} |
| 164 |
} |
| 165 |
|