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

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

209 lines 5.6 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\Assets\Asset;
15 use SolidWP\Performance\Config\Config;
16 use SolidWP\Performance\View\Contracts\View;
17
18 /**
19 * Handles all functionality related to the settings page.
20 *
21 * @since 0.1.1
22 *
23 * @package SolidWP\Performance
24 */
25 class Settings_Page {
26
27 public const MENU_SLUG = 'swpsp-settings';
28 public const SETTINGS_SLUG = 'solid_performance_settings';
29 public const VIEW = 'settings/page';
30
31 /**
32 * @var View
33 */
34 private View $view;
35
36 /**
37 * @var Asset
38 */
39 private Asset $asset;
40
41 /**
42 * @param View $view The view renderer.
43 * @param Asset $asset The asset helper.
44 */
45 public function __construct( View $view, Asset $asset ) {
46 $this->view = $view;
47 $this->asset = $asset;
48 }
49
50 /**
51 * Adds a new menu item as a settings submenu.
52 *
53 * @since 0.1.1
54 *
55 * @action admin_menu
56 *
57 * @return void
58 */
59 public function add_settings_page(): void {
60 $page = add_options_page(
61 __( 'Solid Performance', 'solid-performance' ),
62 __( 'Solid Performance', 'solid-performance' ),
63 'manage_options',
64 self::MENU_SLUG,
65 [ $this, 'build_settings_page' ],
66 );
67 add_action( "load-{$page}", [ $this, 'load_page_scripts' ] );
68 }
69 /**
70 * Register settings
71 */
72 public function register_settings(): void {
73 register_setting(
74 self::SETTINGS_SLUG,
75 self::SETTINGS_SLUG,
76 [
77 'type' => 'object',
78 'description' => esc_html__( 'Solid Performance Settings', 'solid-performance' ),
79 'sanitize_callback' => [ $this, 'sanitize_setting' ],
80 'default' => [],
81 'show_in_rest' => [
82 'schema' => [
83 'properties' => [
84 'page_cache' => [
85 'type' => 'object',
86 'properties' => [
87 'cache_dir' => [
88 'type' => 'string',
89 'description' => esc_html__( 'The directory where cache files are stored.', 'solid-performance' ),
90 ],
91 'enabled' => [
92 'type' => 'boolean',
93 'description' => esc_html__( 'The current status of the page cache.', 'solid-performance' ),
94 ],
95 'debug' => [
96 'type' => 'boolean',
97 'description' => esc_html__( 'The current status of debug mode.', 'solid-performance' ),
98 ],
99 'expiration' => [
100 'type' => 'integer',
101 'description' => esc_html__( 'The number of seconds caches should exist before regenerating.', 'solid-performance' ),
102 ],
103 'exclusions' => [
104 'type' => 'array',
105 'description' => esc_html__( 'The rules to use in determining which urls should not be cached.', 'solid-performance' ),
106 'items' => [
107 'type' => 'string',
108 ],
109 ],
110 ],
111 ],
112 ],
113 ],
114 ],
115 ]
116 );
117 }
118 /**
119 * Trigger Loading Page Scripts
120 */
121 public function load_page_scripts(): void {
122 // Do admin head action for this page.
123 add_action( 'admin_enqueue_scripts', [ $this, 'admin_head' ] );
124 }
125 /**
126 * Add settings link
127 *
128 * @param array $links plugin activate/deactivate links array.
129 */
130 public function settings_link( array $links ): array {
131 $settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=' . self::MENU_SLUG ) ) . '">' . __( 'Settings', 'solid-performance' ) . '</a>';
132 $help_link = '<a href="https://go.solidwp.com/performance-help">' . __( 'Help', 'solid-performance' ) . '</a>';
133
134 array_unshift( $links, $settings_link );
135
136 $links[] = $help_link;
137
138 return $links;
139 }
140 /**
141 * Get the asset file produced by wp scripts.
142 *
143 * @param string $filepath the file path.
144 * @return array
145 */
146 public function get_asset_file( string $filepath ): array {
147 $asset_path = realpath( $this->asset->get_dir() . $filepath . '.asset.php' );
148 return file_exists( $asset_path )
149 ? include $asset_path
150 : [
151 'dependencies' => [ 'lodash', 'react', 'react-dom', 'wp-block-editor', 'wp-blocks', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-primitives', 'wp-api' ],
152 'version' => '0.1.1',
153 ];
154 }
155 /**
156 * Load Page Scripts
157 */
158 public function admin_head(): void {
159 $script_meta = $this->get_asset_file( 'build/settings' );
160 // Enqueue the settings page styles and scripts.
161 wp_enqueue_style( 'solid-performance-settings', $this->asset->get_url( 'build/settings.css' ), [ 'wp-components' ], $script_meta['version'] );
162 wp_enqueue_script( 'solid-performance-settings', $this->asset->get_url( 'build/settings.js' ), $script_meta['dependencies'], $script_meta['version'], true );
163 wp_localize_script(
164 'solid-performance-settings',
165 'swspParams',
166 [
167 'settings' => get_option( self::SETTINGS_SLUG, Config::get_defaults() ),
168 'cache_path' => WP_CONTENT_DIR . '/cache',
169 ]
170 );
171 }
172 /**
173 * Outputs the settings page.
174 *
175 * @since 0.1.1
176 *
177 * @return void
178 */
179 public function build_settings_page(): void {
180 $this->view->render( self::VIEW );
181 }
182
183 /**
184 * Sanitize the values provided in the setting.
185 *
186 * @param array $value The solid_performance_settings value from the request.
187 *
188 * @return array
189 */
190 public function sanitize_setting( $value ) {
191 $parsed_args = wp_parse_args( $value['page_cache'], Config::get_defaults()['page_cache'] );
192
193 if ( isset( $parsed_args['exclusions'] ) ) {
194 $exclusions = array_filter(
195 $parsed_args['exclusions'],
196 function ( $exclusion ) {
197 return $exclusion !== '';
198 }
199 );
200
201 $parsed_args['exclusions'] = array_values( $exclusions );
202 }
203
204 $sanitized_value['page_cache'] = $parsed_args;
205
206 return $sanitized_value;
207 }
208 }
209