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

153 lines 4.2 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 ],
75 ]
76 );
77
78 $this->container->singleton( Config_File::class, Config_File::class );
79 $this->container->singleton( Default_Config::class, Default_Config::class );
80 $this->container->singleton( Config::class, Config::class );
81
82 // Refresh the configuration once the database is available.
83 add_action( 'plugins_loaded', $this->container->callback( Config::class, 'refresh' ), 11 );
84
85 // Sync to the configuration file each time our settings are saved in the database.
86 add_action(
87 "add_option_{$this->option_key}",
88 function (): void {
89 do_action( 'solidwp/performance/config/write_file' );
90 },
91 );
92
93 add_action(
94 "update_option_{$this->option_key}",
95 function (): void {
96 do_action( 'solidwp/performance/config/write_file' );
97 },
98 );
99
100 add_action(
101 "delete_option_{$this->option_key}",
102 function (): void {
103 do_action( 'solidwp/performance/config/write_file' );
104 },
105 );
106
107 // Manually write to wp_options when $config->save() is called.
108 add_action( 'solidwp/performance/config/save', $this->container->callback( Option_Writer::class, 'save' ) );
109
110 // Sync the config file.
111 add_action( 'solidwp/performance/config/write_file', $this->container->callback( File_Writer::class, 'save' ) );
112
113 $this->register_multisite_configuration();
114 }
115
116 /**
117 * Register additional events if we're running on multisite.
118 *
119 * @note This is an incomplete feature, as we currently have no MS settings screen.
120 *
121 * @return void
122 */
123 private function register_multisite_configuration(): void {
124 if ( ! is_multisite() ) {
125 return;
126 }
127 // Sync to the configuration file each time our settings are saved in the database.
128 add_action(
129 "add_site_option_{$this->option_key}",
130 function (): void {
131 do_action( 'solidwp/performance/config/write_file' );
132 },
133 );
134
135 add_action(
136 "update_site_option_{$this->option_key}",
137 function (): void {
138 do_action( 'solidwp/performance/config/write_file' );
139 },
140 );
141
142 add_action(
143 "delete_site_option_{$this->option_key}",
144 function (): void {
145 do_action( 'solidwp/performance/config/write_file' );
146 },
147 );
148
149 // Write to wp_network_options when $config->save() is called.
150 add_action( 'solidwp/performance/config/save', $this->container->callback( Network_Option_Writer::class, 'save' ) );
151 }
152 }
153