PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.8.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.8.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Config / Provider.php

Provider.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.8.0, at src/Performance/Config/Provider.php

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