PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.5.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.5.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.5.0, at src/Performance/Config/Provider.php

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