-
Notifications
You must be signed in to change notification settings - Fork 3
/
wp-js-data-sync.php
387 lines (355 loc) · 12.5 KB
/
wp-js-data-sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
use Automattic\Jetpack\Schema\Schema;
use Automattic\Jetpack\Schema\Schema_Parser;
use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Action;
use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Entry;
use Automattic\Jetpack\WP_JS_Data_Sync\Data_Sync;
use Automattic\Jetpack\WP_JS_Data_Sync\Data_Sync_Readonly;
use Automattic\Jetpack_Boost\Data_Sync\Critical_CSS_Meta_Entry;
use Automattic\Jetpack_Boost\Data_Sync\Getting_Started_Entry;
use Automattic\Jetpack_Boost\Data_Sync\Mergeable_Array_Entry;
use Automattic\Jetpack_Boost\Data_Sync\Minify_Excludes_State_Entry;
use Automattic\Jetpack_Boost\Data_Sync\Modules_State_Entry;
use Automattic\Jetpack_Boost\Lib\Connection;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions\Regenerate_CSS;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions\Set_Provider_CSS;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions\Set_Provider_Error_Dismissed;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions\Set_Provider_Errors;
use Automattic\Jetpack_Boost\Lib\My_Jetpack;
use Automattic\Jetpack_Boost\Lib\Premium_Features;
use Automattic\Jetpack_Boost\Lib\Premium_Pricing;
use Automattic\Jetpack_Boost\Lib\Status;
use Automattic\Jetpack_Boost\Modules\Modules_Index;
use Automattic\Jetpack_Boost\Modules\Optimizations\Image_CDN\Liar;
use Automattic\Jetpack_Boost\Modules\Optimizations\Minify\Minify_CSS;
use Automattic\Jetpack_Boost\Modules\Optimizations\Minify\Minify_JS;
use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Data_Sync\Page_Cache_Entry;
use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Data_Sync_Actions\Clear_Page_Cache;
use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Data_Sync_Actions\Deactivate_WPSC;
use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Data_Sync_Actions\Run_Setup;
use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache;
use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Logger;
if ( ! defined( 'JETPACK_BOOST_DATASYNC_NAMESPACE' ) ) {
define( 'JETPACK_BOOST_DATASYNC_NAMESPACE', 'jetpack_boost_ds' );
}
/**
* Make it easier to register a Jetpack Boost Data-Sync option.
*
* @param string $key - The key for this option.
* @param \Automattic\Jetpack\Schema\Parser $parser - The schema for this option.
* @param \Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get|null $entry - The entry handler for this option.
*/
function jetpack_boost_register_option( $key, $parser, $entry = null ) {
Data_Sync::get_instance( JETPACK_BOOST_DATASYNC_NAMESPACE )
->register( $key, $parser, $entry );
}
/**
* Register a new Jetpack Boost Data_Sync Action
*
* @param string $key
* @param string $action_name
* @param Schema_Parser $request_schema
* @param Data_Sync_Action $instance
*
* @return void
*/
function jetpack_boost_register_action( $key, $action_name, $request_schema, $instance ) {
Data_Sync::get_instance( JETPACK_BOOST_DATASYNC_NAMESPACE )
->register_action( $key, $action_name, $request_schema, $instance );
}
/**
* Make it easier to register a Jetpack Boost Read-only Data-Sync option.
*/
function jetpack_boost_register_readonly_option( $key, $callback ) {
jetpack_boost_register_option( $key, Schema::as_unsafe_any(), new Data_Sync_Readonly( $callback ) );
}
/**
* @param string $key
*
* @return Data_Sync_Entry
*/
function jetpack_boost_ds_entry( $key ) {
return Data_Sync::get_instance( JETPACK_BOOST_DATASYNC_NAMESPACE )
->get_registry()
->get_entry( $key );
}
function jetpack_boost_ds_get( $key ) {
$entry = jetpack_boost_ds_entry( $key );
if ( ! $entry ) {
return null;
}
return $entry->get();
}
function jetpack_boost_ds_set( $key, $value ) {
$entry = jetpack_boost_ds_entry( $key );
if ( ! $entry ) {
return null;
}
return $entry->set( $value );
}
function jetpack_boost_ds_delete( $key ) {
$entry = jetpack_boost_ds_entry( $key );
if ( ! $entry ) {
return null;
}
return $entry->delete();
}
/**
* Ensure that Async Options are passed to the relevant scripts.
*/
function jetpack_boost_initialize_datasync() {
$data_sync = Data_Sync::get_instance( JETPACK_BOOST_DATASYNC_NAMESPACE );
$data_sync->attach_to_plugin( 'jetpack-boost-admin', 'jetpack_page_jetpack-boost' );
}
add_action( 'admin_init', 'jetpack_boost_initialize_datasync' );
// Represents a set of errors that can be stored for a single Provider Key in a Critical CSS state block.
$critical_css_provider_error_set_schema = Schema::as_array(
Schema::as_assoc_array(
array(
'url' => Schema::as_string(),
'message' => Schema::as_string(),
'type' => Schema::as_string(),
'meta' => Schema::any_json_data()->nullable(),
)
)->fallback(
array(
'url' => '',
'message' => '',
'type' => '',
)
)
);
$critical_css_state_schema = Schema::as_assoc_array(
array(
'providers' => Schema::as_array(
Schema::as_assoc_array(
array(
'key' => Schema::as_string(),
'label' => Schema::as_string(),
'urls' => Schema::as_array( Schema::as_string() ),
'success_ratio' => Schema::as_float(),
'status' => Schema::enum( array( 'success', 'pending', 'error', 'validation-error' ) )->fallback( 'validation-error' ),
'error_status' => Schema::enum( array( 'active', 'dismissed' ) )->nullable(),
'errors' => $critical_css_provider_error_set_schema->nullable(),
)
)
)->nullable(),
'status' => Schema::enum( array( 'not_generated', 'generated', 'pending', 'error' ) )->fallback( 'not_generated' ),
'created' => Schema::as_float()->nullable(),
'updated' => Schema::as_float()->nullable(),
'status_error' => Schema::as_string()->nullable(),
)
)->fallback(
array(
'providers' => array(),
'status' => 'not_generated',
'created' => null,
'updated' => null,
)
);
$critical_css_meta_schema = Schema::as_assoc_array(
array(
'proxy_nonce' => Schema::as_string()->nullable(),
)
);
$critical_css_suggest_regenerate_schema = Schema::enum(
array(
'1', // Old versions of Boost stored a boolean in the DB.
'page_saved',
'post_saved',
'switched_theme',
'plugin_change',
'cornerstone_page_saved',
'cornerstone_pages_list_updated',
)
)->nullable();
/**
* Register Data Sync Stores
*/
jetpack_boost_register_option( 'critical_css_state', $critical_css_state_schema );
jetpack_boost_register_option( 'critical_css_meta', $critical_css_meta_schema, new Critical_CSS_Meta_Entry() );
jetpack_boost_register_option( 'critical_css_suggest_regenerate', $critical_css_suggest_regenerate_schema );
jetpack_boost_register_action( 'critical_css_state', 'request-regenerate', Schema::as_void(), new Regenerate_CSS() );
jetpack_boost_register_action(
'critical_css_state',
'set-provider-css',
Schema::as_assoc_array(
array(
'key' => Schema::as_string(),
'css' => Schema::as_string(),
)
),
new Set_Provider_CSS()
);
jetpack_boost_register_action(
'critical_css_state',
'set-provider-errors',
Schema::as_assoc_array(
array(
'key' => Schema::as_string(),
'errors' => $critical_css_provider_error_set_schema,
)
),
new Set_Provider_Errors()
);
jetpack_boost_register_action(
'critical_css_state',
'set-provider-errors-dismissed',
Schema::as_array(
Schema::as_assoc_array(
array(
'key' => Schema::as_string(),
'dismissed' => Schema::as_boolean(),
)
)
),
new Set_Provider_Error_Dismissed()
);
$modules_state_schema = Schema::as_array(
Schema::as_assoc_array(
array(
'active' => Schema::as_boolean()->fallback( false ),
'available' => Schema::as_boolean()->nullable(),
)
)
)->fallback( array() );
$entry = new Modules_State_Entry( Modules_Index::FEATURES );
jetpack_boost_register_option( 'modules_state', $modules_state_schema, $entry );
require_once __DIR__ . '/app/modules/image-size-analysis/data-sync/init.php';
/**
* Register Minify Excludes stores.
*/
$js_excludes_entry = new Minify_Excludes_State_Entry( 'minify_js_excludes' );
$css_excludes_entry = new Minify_Excludes_State_Entry( 'minify_css_excludes' );
jetpack_boost_register_option( 'minify_js_excludes', Schema::as_array( Schema::as_string() )->fallback( Minify_JS::$default_excludes ), $js_excludes_entry );
jetpack_boost_register_option( 'minify_css_excludes', Schema::as_array( Schema::as_string() )->fallback( Minify_CSS::$default_excludes ), $css_excludes_entry );
jetpack_boost_register_option(
'image_cdn_quality',
Schema::as_assoc_array(
array(
'jpg' => Schema::as_assoc_array(
array(
'quality' => Schema::as_number(),
'lossless' => Schema::as_boolean(),
)
),
'png' => Schema::as_assoc_array(
array(
'quality' => Schema::as_number(),
'lossless' => Schema::as_boolean(),
)
),
'webp' => Schema::as_assoc_array(
array(
'quality' => Schema::as_number(),
'lossless' => Schema::as_boolean(),
)
),
)
)->fallback(
array(
'jpg' => array(
'quality' => 89,
'lossless' => false,
),
'png' => array(
'quality' => 80,
'lossless' => false,
),
'webp' => array(
'quality' => 80,
'lossless' => false,
),
)
)
);
jetpack_boost_register_option( 'performance_history_toggle', Schema::as_boolean()->fallback( false ) );
jetpack_boost_register_option(
'performance_history',
Schema::as_assoc_array(
array(
'periods' => Schema::as_array(
Schema::as_assoc_array(
array(
'timestamp' => Schema::as_number(),
'dimensions' => Schema::as_assoc_array(
array(
'desktop_overall_score' => Schema::as_number(),
'mobile_overall_score' => Schema::as_number(),
'desktop_cls' => Schema::as_number(),
'desktop_lcp' => Schema::as_number(),
'desktop_tbt' => Schema::as_number(),
'mobile_cls' => Schema::as_number(),
'mobile_lcp' => Schema::as_number(),
'mobile_tbt' => Schema::as_number(),
)
),
)
)
),
'annotations' => Schema::as_array(
Schema::as_assoc_array(
array(
'timestamp' => Schema::as_number(),
'text' => Schema::as_string(),
)
)
),
'startDate' => Schema::as_number(),
'endDate' => Schema::as_number(),
)
),
new Performance_History_Entry()
);
/**
* Entry to store alerts that shouldn't be shown again.
*/
jetpack_boost_register_option(
'dismissed_alerts',
Schema::as_assoc_array(
array(
'performance_history_fresh_start' => Schema::as_boolean(),
'score_increase' => Schema::as_boolean(),
'score_decrease' => Schema::as_boolean(),
)
)->fallback(
array(
'performance_history_fresh_start' => false,
'score_increase' => false,
'score_decrease' => false,
)
),
new Mergeable_Array_Entry( JETPACK_BOOST_DATASYNC_NAMESPACE . '_dismissed_alerts' )
);
jetpack_boost_register_readonly_option( 'connection', array( new Connection(), 'get_connection_api_response' ) );
jetpack_boost_register_readonly_option( 'pricing', array( Premium_Pricing::class, 'get_yearly_pricing' ) );
jetpack_boost_register_readonly_option( 'product', array( My_Jetpack::class, 'get_product' ) );
jetpack_boost_register_readonly_option( 'premium_features', array( Premium_Features::class, 'get_features' ) );
jetpack_boost_register_readonly_option( 'cache_debug_log', array( Logger::class, 'read' ) );
jetpack_boost_register_readonly_option( 'cache_engine_loading', array( Boost_Cache::class, 'is_loaded' ) );
jetpack_boost_register_option( 'getting_started', Schema::as_boolean()->fallback( false ), new Getting_Started_Entry() );
// Page Cache error
jetpack_boost_register_option(
'page_cache_error',
Schema::as_assoc_array(
array(
'code' => Schema::as_string(),
'message' => Schema::as_string(),
'dismissed' => Schema::as_boolean()->fallback( false ),
)
)->nullable()
);
jetpack_boost_register_action( 'page_cache', 'run-setup', Schema::as_void(), new Run_Setup() );
jetpack_boost_register_option(
'page_cache',
Schema::as_assoc_array(
array(
'bypass_patterns' => Schema::as_array( Schema::as_string() ),
'logging' => Schema::as_boolean(),
)
),
new Page_Cache_Entry( JETPACK_BOOST_DATASYNC_NAMESPACE . '_page_cache' )
);
jetpack_boost_register_action( 'page_cache', 'clear-page-cache', Schema::as_void(), new Clear_Page_Cache() );
jetpack_boost_register_action( 'page_cache', 'deactivate-wpsc', Schema::as_void(), new Deactivate_WPSC() );
jetpack_boost_register_option( 'image_cdn_liar', Schema::as_boolean()->fallback( false ), new Status( Liar::get_slug() ) );