PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / trunk
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution vtrunk
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 / Admin / Settings_Page.php

Settings_Page.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution trunk, at src/Performance/Admin/Settings_Page.php

322 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles all functionality related to the Settings Page.
4 *
5 * @since 0.1.1
6 *
7 * @package SolidWP\Performance
8 */
9
10 declare( strict_types=1 );
11
12 namespace SolidWP\Performance\Admin;
13
14 use SolidWP\Performance\API\Routes\Page_Cache\Cache_Delivery;
15 use SolidWP\Performance\Assets\Asset;
16 use SolidWP\Performance\Cache_Delivery\Manager_Collection;
17 use SolidWP\Performance\Config\Config;
18 use SolidWP\Performance\Config\Default_Config;
19 use SolidWP\Performance\Cache_Delivery\Cache_Delivery_Type;
20 use SolidWP\Performance\Image_Transformation\Processor_Type;
21 use SolidWP\Performance\View\Contracts\View;
22
23 /**
24 * Handles all functionality related to the settings page.
25 *
26 * @since 0.1.1
27 *
28 * @package SolidWP\Performance
29 */
30 class Settings_Page {
31
32 public const MENU_SLUG = 'swpsp-settings';
33 public const SETTINGS_SLUG = 'solid_performance_settings';
34 public const VIEW = 'settings/page';
35
36 /**
37 * @var View
38 */
39 private View $view;
40
41 /**
42 * @var Asset
43 */
44 private Asset $asset;
45
46 /**
47 * @var Default_Config
48 */
49 private Default_Config $default_config;
50
51 /**
52 * @var Config
53 */
54 private Config $config;
55
56 /**
57 * @var Manager_Collection
58 */
59 private Manager_Collection $manager_collection;
60
61 /**
62 * @param View $view The view renderer.
63 * @param Asset $asset The asset helper.
64 * @param Default_Config $default_config The default config items.
65 * @param Config $config The config object.
66 * @param Manager_Collection $manager_collection The cache delivery collection.
67 */
68 public function __construct(
69 View $view,
70 Asset $asset,
71 Default_Config $default_config,
72 Config $config,
73 Manager_Collection $manager_collection
74 ) {
75 $this->view = $view;
76 $this->asset = $asset;
77 $this->default_config = $default_config;
78 $this->config = $config;
79 $this->manager_collection = $manager_collection;
80 }
81
82 /**
83 * Adds a new menu item as a settings submenu.
84 *
85 * @since 0.1.1
86 *
87 * @action admin_menu
88 *
89 * @return void
90 */
91 public function add_settings_page(): void {
92 $page = add_options_page(
93 __( 'Kadence Performance', 'solid-performance' ),
94 __( 'Kadence Performance', 'solid-performance' ),
95 'manage_options',
96 self::MENU_SLUG,
97 [ $this, 'build_settings_page' ],
98 );
99 add_action( "load-{$page}", [ $this, 'refresh_config' ], 1 );
100 add_action( "load-{$page}", [ $this, 'load_page_scripts' ] );
101 }
102
103 /**
104 * Ensure the default settings data exists.
105 *
106 * @action load-settings_page_swpsp-settings
107 *
108 * @return void
109 */
110 public function refresh_config(): void {
111 $this->config->refresh()->save();
112 }
113
114 /**
115 * Trigger Loading Page Scripts
116 *
117 * @action load-settings_page_swpsp-settings
118 */
119 public function load_page_scripts(): void {
120 // Do admin head action for this page.
121 add_action( 'admin_enqueue_scripts', [ $this, 'admin_head' ] );
122 }
123
124 /**
125 * Register settings
126 */
127 public function register_settings(): void {
128 register_setting(
129 self::SETTINGS_SLUG,
130 self::SETTINGS_SLUG,
131 [
132 'type' => 'object',
133 'description' => esc_html__( 'Kadence Performance Settings', 'solid-performance' ),
134 'sanitize_callback' => [ $this, 'sanitize_setting' ],
135 'default' => [],
136 'show_in_rest' => [
137 'schema' => [
138 'properties' => [
139 'page_cache' => [
140 'type' => 'object',
141 'properties' => [
142 'cache_dir' => [
143 'type' => 'string',
144 'description' => esc_html__( 'The directory where cache files are stored.', 'solid-performance' ),
145 ],
146 'enabled' => [
147 'type' => 'boolean',
148 'description' => esc_html__( 'The current status of the page cache.', 'solid-performance' ),
149 ],
150 'debug' => [
151 'type' => 'boolean',
152 'description' => esc_html__( 'The current status of debug mode.', 'solid-performance' ),
153 ],
154 'expiration' => [
155 'type' => 'integer',
156 'description' => esc_html__( 'The number of seconds caches should exist before regenerating.', 'solid-performance' ),
157 ],
158 'exclusions' => [
159 'type' => 'array',
160 'description' => esc_html__( 'The rules to use in determining which urls should not be cached.', 'solid-performance' ),
161 'items' => [
162 'type' => 'string',
163 ],
164 ],
165 'compression' => [
166 'type' => 'object',
167 'properties' => [
168 'enabled' => [
169 'type' => 'boolean',
170 'description' => esc_html__( 'Whether storing and serving compressed cache files is enabled.', 'solid-performance' ),
171 ],
172 ],
173 ],
174 'preload' => [
175 'type' => 'object',
176 'properties' => [
177 'high_performance_mode' => [
178 'type' => 'boolean',
179 'description' => esc_html__( 'Preload with the maximum batch size. Automatically disabled if we encounter errors.', 'solid-performance' ),
180 ],
181 ],
182 ],
183 'lazy_loading' => [
184 'type' => 'object',
185 'properties' => [
186 'enabled' => [
187 'type' => 'boolean',
188 'description' => esc_html__( 'Whether automatic lazy loading is enabled.', 'solid-performance' ),
189 ],
190 ],
191 ],
192 'cache_delivery' => [
193 'type' => 'object',
194 'properties' => [
195 'method' => [
196 'type' => 'string',
197 'description' => esc_html__( 'The cache delivery method in use.', 'solid-performance' ),
198 'enum' => Cache_Delivery_Type::all(),
199 ],
200 ],
201 ],
202 'image_transformation' => [
203 'type' => 'object',
204 'properties' => [
205 'enabled' => [
206 'type' => 'boolean',
207 'description' => esc_html__( 'Whether image transformation is enabled.', 'solid-performance' ),
208 ],
209 'processor' => [
210 'type' => 'string',
211 'description' => esc_html__( 'The image transformation processor to use.', 'solid-performance' ),
212 'enum' => Processor_Type::names(),
213 ],
214 ],
215 ],
216 'mobile_cache' => [
217 'type' => 'object',
218 'properties' => [
219 'enabled' => [
220 'type' => 'boolean',
221 'description' => esc_html__( 'Store separate caches for mobiles devices.', 'solid-performance' ),
222 ],
223 ],
224 ],
225 ],
226 ],
227 ],
228 ],
229 ],
230 ]
231 );
232 }
233
234 /**
235 * Add settings link
236 *
237 * @param array $links plugin activate/deactivate links array.
238 */
239 public function settings_link( array $links ): array {
240 $settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=' . self::MENU_SLUG ) ) . '">' . __( 'Settings', 'solid-performance' ) . '</a>';
241 $help_link = '<a href="https://go.solidwp.com/performance-help">' . __( 'Help', 'solid-performance' ) . '</a>';
242
243 array_unshift( $links, $settings_link );
244
245 $links[] = $help_link;
246
247 return $links;
248 }
249
250 /**
251 * Load Page Scripts
252 *
253 * @see Cache_Delivery::schema_callback()
254 */
255 public function admin_head(): void {
256 $script_meta = $this->asset->get_meta( 'build/settings' );
257
258 // Enqueue the settings page styles and scripts.
259 wp_enqueue_style( 'solid-performance-settings', $this->asset->get_url( 'build/settings.css' ), [ 'wp-components' ], $script_meta['version'] );
260 wp_enqueue_script( 'solid-performance-settings', $this->asset->get_url( 'build/settings.js' ), $script_meta['dependencies'], $script_meta['version'], true );
261
262 $htaccess = $this->manager_collection->get( Cache_Delivery_Type::HTACCESS );
263 $nginx = $this->manager_collection->get( Cache_Delivery_Type::NGINX );
264
265 wp_localize_script(
266 'solid-performance-settings',
267 'swspParams',
268 [
269 'settings' => get_option( self::SETTINGS_SLUG, $this->default_config->get() ),
270 'cacheDelivery' => [
271 'htaccess' => [
272 'supported' => $htaccess->supported(),
273 'hasRules' => $htaccess->has_rules(),
274 ],
275 'nginx' => [
276 'supported' => $nginx->supported(),
277 'hasRules' => $nginx->has_rules(),
278 'path' => $nginx->path(),
279 ],
280 ],
281 ]
282 );
283 }
284
285 /**
286 * Outputs the settings page.
287 *
288 * @since 0.1.1
289 *
290 * @return void
291 */
292 public function build_settings_page(): void {
293 $this->view->render( self::VIEW );
294 }
295
296 /**
297 * Sanitize the values provided in the setting.
298 *
299 * @param array $value The solid_performance_settings value from the request.
300 *
301 * @return array
302 */
303 public function sanitize_setting( $value ) {
304 $parsed_args = wp_parse_args( $value['page_cache'], $this->default_config->get( 'page_cache' ) );
305
306 if ( isset( $parsed_args['exclusions'] ) ) {
307 $exclusions = array_filter(
308 $parsed_args['exclusions'],
309 function ( $exclusion ) {
310 return $exclusion !== '';
311 }
312 );
313
314 $parsed_args['exclusions'] = array_values( $exclusions );
315 }
316
317 $sanitized_value['page_cache'] = $parsed_args;
318
319 return $sanitized_value;
320 }
321 }
322