| 1 |
<?php |
| 2 |
/** |
| 3 |
* Writes only config items that differ from the defaults to a config |
| 4 |
* file on disk. |
| 5 |
* |
| 6 |
* This attempts to sync the database config items to the file based config. |
| 7 |
* |
| 8 |
* @package SolidWP\Performance |
| 9 |
*/ |
| 10 |
|
| 11 |
declare( strict_types=1 ); |
| 12 |
|
| 13 |
namespace SolidWP\Performance\Config\Writers; |
| 14 |
|
| 15 |
use SolidWP\Performance\Config\Config; |
| 16 |
use SolidWP\Performance\Config\Config_File; |
| 17 |
use SolidWP\Performance\Config\Default_Config; |
| 18 |
use SolidWP\Performance\Config\Writers\Contracts\Writable; |
| 19 |
|
| 20 |
/** |
| 21 |
* Writes only config items that differ from the defaults to a config |
| 22 |
* file on disk. |
| 23 |
* |
| 24 |
* This attempts to sync the database config items to the file based config. |
| 25 |
* |
| 26 |
* @package SolidWP\Performance |
| 27 |
*/ |
| 28 |
final class File_Writer implements Writable { |
| 29 |
|
| 30 |
/** |
| 31 |
* The config singleton. |
| 32 |
* |
| 33 |
* @var Config |
| 34 |
*/ |
| 35 |
private Config $config; |
| 36 |
|
| 37 |
/** |
| 38 |
* The config file location. |
| 39 |
* |
| 40 |
* @var Config_File |
| 41 |
*/ |
| 42 |
private Config_File $config_file; |
| 43 |
|
| 44 |
/** |
| 45 |
* The default configuration items. |
| 46 |
* |
| 47 |
* @var Default_Config |
| 48 |
*/ |
| 49 |
private Default_Config $default_config; |
| 50 |
|
| 51 |
/** |
| 52 |
* @param Config $config The config singleton. |
| 53 |
* @param Config_File $config_file The config file location. |
| 54 |
* @param Default_Config $default_config The default configuration items. |
| 55 |
*/ |
| 56 |
public function __construct( Config $config, Config_File $config_file, Default_Config $default_config ) { |
| 57 |
$this->config = $config; |
| 58 |
$this->config_file = $config_file; |
| 59 |
$this->default_config = $default_config; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Write config items that differ from the default values to a config file on disk. |
| 64 |
* |
| 65 |
* Fires automatically after our settings are saved. |
| 66 |
* |
| 67 |
* @action solidwp/performance/config/write_file |
| 68 |
* |
| 69 |
* @action add_option_solid_performance_settings |
| 70 |
* @action update_option_solid_performance_settings |
| 71 |
* |
| 72 |
* @action add_site_option_solid_performance_settings |
| 73 |
* @action update_site_option_solid_performance_settings |
| 74 |
* |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
public function save(): bool { |
| 78 |
// Make sure that the config directory exists. |
| 79 |
if ( ! is_dir( $this->config_file->dir() ) ) { |
| 80 |
wp_mkdir_p( $this->config_file->dir() ); |
| 81 |
} |
| 82 |
|
| 83 |
// Refresh the config and only store items that differ from the default configuration. |
| 84 |
$items = array_diff_multidimensional( $this->config->refresh()->all(), $this->default_config->get(), false ); |
| 85 |
|
| 86 |
// Get the config as a string to write to file. |
| 87 |
$config = var_export( $items, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
| 88 |
|
| 89 |
// Manually create the return for the file. |
| 90 |
$contents = sprintf( |
| 91 |
'<?php |
| 92 |
/** |
| 93 |
* Automatically Generated by Solid Performance every time the settings page is saved. |
| 94 |
*/ |
| 95 |
return %s;', |
| 96 |
$config |
| 97 |
); |
| 98 |
|
| 99 |
return (bool) file_put_contents( $this->config_file->get(), $contents ); |
| 100 |
} |
| 101 |
} |
| 102 |
|