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

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