| 1 |
<?php |
| 2 |
/** |
| 3 |
* Registers configuration definitions in the container. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Config; |
| 11 |
|
| 12 |
use SolidWP\Performance\Config\Writers\File_Writer; |
| 13 |
use SolidWP\Performance\Config\Writers\Network_Option_Writer; |
| 14 |
use SolidWP\Performance\Config\Writers\Option_Writer; |
| 15 |
use SolidWP\Performance\Contracts\Service_Provider; |
| 16 |
use SolidWP\Performance\Core; |
| 17 |
use SolidWP\Performance\Cache_Delivery\Cache_Delivery_Type; |
| 18 |
use SolidWP\Performance\Image_Transformation\Processor_Type; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Registers config definitions in the container. |
| 26 |
* |
| 27 |
* @note This is loaded earlier than other providers. |
| 28 |
* |
| 29 |
* @see Core::__construct() |
| 30 |
* |
| 31 |
* @package SolidWP\Performance |
| 32 |
*/ |
| 33 |
final class Provider extends Service_Provider { |
| 34 |
|
| 35 |
/** |
| 36 |
* The option we key use to save to wp_options/wp_sitemeta. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private string $option_key = Config_Loader::OPTION_KEY; |
| 41 |
|
| 42 |
/** |
| 43 |
* {@inheritdoc} |
| 44 |
*/ |
| 45 |
public function register(): void { |
| 46 |
$this->container->when( Advanced_Cache::class ) |
| 47 |
->needs( '$version' ) |
| 48 |
->give( static fn( $c ): string => $c->get( Core::PLUGIN_VERSION ) ); |
| 49 |
|
| 50 |
$this->container->when( Advanced_Cache::class ) |
| 51 |
->needs( '$destination' ) |
| 52 |
->give( static fn( $c ): string => $c->get( Core::ADVANCED_CACHE_PATH ) ); |
| 53 |
|
| 54 |
$this->container->when( Config_File::class ) |
| 55 |
->needs( '$dir' ) |
| 56 |
->give( static fn(): string => WP_CONTENT_DIR . '/cache/solid-performance' ); |
| 57 |
|
| 58 |
$this->container->when( Default_Config::class ) |
| 59 |
->needs( '$defaults' ) |
| 60 |
->give( |
| 61 |
// Configure the default configuration items here. |
| 62 |
static fn( $c ): array => [ |
| 63 |
'page_cache' => [ |
| 64 |
'cache_dir' => $c->get( Core::CACHE_DIR ), |
| 65 |
'debug' => false, |
| 66 |
'enabled' => true, |
| 67 |
'expiration' => 86400, // One day in seconds. |
| 68 |
'exclusions' => [], |
| 69 |
'compression' => [ |
| 70 |
'enabled' => true, |
| 71 |
], |
| 72 |
'preload' => [ |
| 73 |
'high_performance_mode' => true, |
| 74 |
], |
| 75 |
'lazy_loading' => [ |
| 76 |
'enabled' => true, |
| 77 |
], |
| 78 |
'cache_delivery' => [ |
| 79 |
'method' => Cache_Delivery_Type::HTACCESS, |
| 80 |
], |
| 81 |
'image_transformation' => [ |
| 82 |
'enabled' => false, |
| 83 |
'processor' => Processor_Type::CLOUDFLARE, |
| 84 |
], |
| 85 |
'mobile_cache' => [ |
| 86 |
'enabled' => false, |
| 87 |
], |
| 88 |
], |
| 89 |
] |
| 90 |
); |
| 91 |
|
| 92 |
$this->container->singleton( Config_File::class, Config_File::class ); |
| 93 |
$this->container->singleton( Default_Config::class, Default_Config::class ); |
| 94 |
$this->container->singleton( Config::class, Config::class ); |
| 95 |
|
| 96 |
// Refresh the configuration once the database is available. |
| 97 |
add_action( 'plugins_loaded', $this->container->callback( Config::class, 'refresh' ), 11 ); |
| 98 |
|
| 99 |
// Sync to the configuration file each time our settings are saved in the database. |
| 100 |
add_action( |
| 101 |
"add_option_{$this->option_key}", |
| 102 |
static function (): void { |
| 103 |
do_action( 'solidwp/performance/config/write_file' ); |
| 104 |
}, |
| 105 |
); |
| 106 |
|
| 107 |
add_action( |
| 108 |
"update_option_{$this->option_key}", |
| 109 |
static function (): void { |
| 110 |
do_action( 'solidwp/performance/config/write_file' ); |
| 111 |
}, |
| 112 |
); |
| 113 |
|
| 114 |
add_action( |
| 115 |
"delete_option_{$this->option_key}", |
| 116 |
static function (): void { |
| 117 |
do_action( 'solidwp/performance/config/write_file' ); |
| 118 |
}, |
| 119 |
); |
| 120 |
|
| 121 |
// Manually write to wp_options when $config->save() is called. |
| 122 |
add_action( 'solidwp/performance/config/save', $this->container->callback( Option_Writer::class, 'save' ) ); |
| 123 |
|
| 124 |
// Sync the config file. |
| 125 |
add_action( 'solidwp/performance/config/write_file', $this->container->callback( File_Writer::class, 'save' ) ); |
| 126 |
|
| 127 |
$this->register_multisite_configuration(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Register additional events if we're running on multisite. |
| 132 |
* |
| 133 |
* @note This is an incomplete feature, as we currently have no MS settings screen. |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
private function register_multisite_configuration(): void { |
| 138 |
if ( ! is_multisite() ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
// Sync to the configuration file each time our settings are saved in the database. |
| 142 |
add_action( |
| 143 |
"add_site_option_{$this->option_key}", |
| 144 |
function (): void { |
| 145 |
do_action( 'solidwp/performance/config/write_file' ); |
| 146 |
}, |
| 147 |
); |
| 148 |
|
| 149 |
add_action( |
| 150 |
"update_site_option_{$this->option_key}", |
| 151 |
function (): void { |
| 152 |
do_action( 'solidwp/performance/config/write_file' ); |
| 153 |
}, |
| 154 |
); |
| 155 |
|
| 156 |
add_action( |
| 157 |
"delete_site_option_{$this->option_key}", |
| 158 |
function (): void { |
| 159 |
do_action( 'solidwp/performance/config/write_file' ); |
| 160 |
}, |
| 161 |
); |
| 162 |
|
| 163 |
// Write to wp_network_options when $config->save() is called. |
| 164 |
add_action( 'solidwp/performance/config/save', $this->container->callback( Network_Option_Writer::class, 'save' ) ); |
| 165 |
} |
| 166 |
} |
| 167 |
|