PluginProbe
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant / 2.0.0
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant v2.0.0
2.0.4 2.0.3 2.0.2 2.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2 1.3 2.0.0
easyfonts / src / Admin / RestApi.php

RestApi.php in EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant 2.0.0, at src/Admin/RestApi.php

719 lines 21.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API.
4 *
5 * @package EasyFonts
6 */
7
8 namespace EasyFonts\Admin;
9
10 use EasyFonts\Database\Migrator;
11 use EasyFonts\Fonts\Registry;
12 use EasyFonts\Fonts\Storage;
13 use EasyFonts\Fonts\UsageTracker;
14 use EasyFonts\Settings;
15 use WP_REST_Request;
16 use WP_REST_Response;
17
18 defined( 'ABSPATH' ) || exit;
19
20 /**
21 * Registers and handles all REST routes.
22 */
23 class RestApi {
24
25 const NS = 'easyfonts/v1';
26
27 /**
28 * Register hooks.
29 */
30 public function boot(): void {
31 add_action( 'rest_api_init', array( $this, 'register' ) );
32 }
33
34 /**
35 * Register routes.
36 */
37 public function register(): void {
38 $admin = array( $this, 'can_manage' );
39
40 register_rest_route(
41 self::NS,
42 '/settings',
43 array(
44 array(
45 'methods' => 'GET',
46 'callback' => array( $this, 'get_settings' ),
47 'permission_callback' => $admin,
48 ),
49 array(
50 'methods' => 'POST',
51 'callback' => array( $this, 'save_settings' ),
52 'permission_callback' => $admin,
53 ),
54 )
55 );
56
57 register_rest_route( self::NS, '/fonts', array(
58 'methods' => 'GET',
59 'callback' => array( $this, 'get_fonts' ),
60 'permission_callback' => $admin,
61 ) );
62
63 register_rest_route( self::NS, '/font/toggle', array(
64 'methods' => 'POST',
65 'callback' => array( $this, 'toggle_font' ),
66 'permission_callback' => $admin,
67 ) );
68
69 register_rest_route( self::NS, '/fonts/bulk', array(
70 'methods' => 'POST',
71 'callback' => array( $this, 'bulk_toggle' ),
72 'permission_callback' => $admin,
73 ) );
74
75 register_rest_route( self::NS, '/optimize', array(
76 'methods' => 'POST',
77 'callback' => array( $this, 'optimize' ),
78 'permission_callback' => $admin,
79 ) );
80
81 register_rest_route( self::NS, '/usage', array(
82 'methods' => 'GET',
83 'callback' => array( $this, 'get_usage' ),
84 'permission_callback' => $admin,
85 ) );
86
87 register_rest_route( self::NS, '/stats', array(
88 'methods' => 'GET',
89 'callback' => array( $this, 'get_stats' ),
90 'permission_callback' => $admin,
91 ) );
92
93 register_rest_route( self::NS, '/purge', array(
94 'methods' => 'POST',
95 'callback' => array( $this, 'purge' ),
96 'permission_callback' => $admin,
97 ) );
98
99 register_rest_route( self::NS, '/repair', array(
100 'methods' => 'POST',
101 'callback' => array( $this, 'repair' ),
102 'permission_callback' => $admin,
103 ) );
104
105 register_rest_route( self::NS, '/diagnostics', array(
106 'methods' => 'GET',
107 'callback' => array( $this, 'diagnostics' ),
108 'permission_callback' => $admin,
109 ) );
110
111 // Public beacon ingest (nonce-checked in handler).
112 register_rest_route( self::NS, '/beacon', array(
113 'methods' => 'POST',
114 'callback' => array( $this, 'beacon' ),
115 'permission_callback' => '__return_true',
116 ) );
117
118 // Public async-font ingest: records Google/Bunny CSS URLs injected by JS
119 // at runtime so the next render can self-host them (nonce-checked).
120 register_rest_route( self::NS, '/async-fonts', array(
121 'methods' => 'POST',
122 'callback' => array( $this, 'async_fonts' ),
123 'permission_callback' => '__return_true',
124 ) );
125
126 // Settings export / import (admin only).
127 register_rest_route( self::NS, '/export', array(
128 'methods' => 'GET',
129 'callback' => array( $this, 'export_settings' ),
130 'permission_callback' => $admin,
131 ) );
132
133 register_rest_route( self::NS, '/import', array(
134 'methods' => 'POST',
135 'callback' => array( $this, 'import_settings' ),
136 'permission_callback' => $admin,
137 ) );
138 }
139
140 /**
141 * Capability gate.
142 *
143 * @return bool
144 */
145 public function can_manage(): bool {
146 return current_user_can( 'manage_options' );
147 }
148
149 /**
150 * GET /settings
151 *
152 * @return WP_REST_Response
153 */
154 public function get_settings(): WP_REST_Response {
155 return new WP_REST_Response( Settings::all(), 200 );
156 }
157
158 /**
159 * POST /settings
160 *
161 * @param WP_REST_Request $request Request.
162 * @return WP_REST_Response
163 */
164 public function save_settings( WP_REST_Request $request ): WP_REST_Response {
165 $input = (array) $request->get_json_params();
166 $current = Settings::all();
167
168 $bools = array( 'enabled', 'auto_config', 'strip_hints', 'smart_preload', 'metric_fallbacks', 'beacon', 'inline_css', 'async_blocker', 'remove_data_on_uninstall' );
169
170 foreach ( $bools as $key ) {
171 if ( array_key_exists( $key, $input ) ) {
172 $current[ $key ] = $input[ $key ] ? 1 : 0;
173 }
174 }
175
176 if ( isset( $input['font_display'] ) ) {
177 $allowed = array( 'swap', 'fallback', 'optional', 'block', 'auto', 'none' );
178 $val = sanitize_text_field( $input['font_display'] );
179 $current['font_display'] = in_array( $val, $allowed, true ) ? $val : 'swap';
180 }
181
182 if ( array_key_exists( 'cdn_url', $input ) ) {
183 $cdn = trim( (string) $input['cdn_url'] );
184
185 // Accept protocol-relative; require http(s); store without trailing slash.
186 if ( '' === $cdn ) {
187 $current['cdn_url'] = '';
188 } else {
189 if ( 0 === strpos( $cdn, '//' ) ) {
190 $cdn = ( is_ssl() ? 'https:' : 'http:' ) . $cdn;
191 }
192
193 $current['cdn_url'] = preg_match( '#^https?://#i', $cdn )
194 ? untrailingslashit( esc_url_raw( $cdn ) )
195 : '';
196 }
197 }
198
199 if ( isset( $input['subsets'] ) && is_array( $input['subsets'] ) ) {
200 $current['subsets'] = array_values( array_map( 'sanitize_text_field', $input['subsets'] ) );
201 }
202
203 if ( isset( $input['excluded_urls'] ) && is_array( $input['excluded_urls'] ) ) {
204 $current['excluded_urls'] = array_values( array_filter( array_map( 'sanitize_text_field', $input['excluded_urls'] ) ) );
205 }
206
207 Settings::save( $current );
208 Settings::bump_buster();
209
210 return new WP_REST_Response( Settings::all(), 200 );
211 }
212
213 /**
214 * GET /fonts
215 *
216 * @return WP_REST_Response
217 */
218 public function get_fonts(): WP_REST_Response {
219 $rendered = ( new UsageTracker() )->rendered_set();
220
221 $grouped = ( new Registry() )->grouped_with_usage(
222 $rendered['variants'],
223 $rendered['families']
224 );
225
226 return new WP_REST_Response( $grouped, 200 );
227 }
228
229 /**
230 * POST /font/toggle — enable/disable or set preload on a variant or family.
231 *
232 * Body: { field: 'enabled'|'preloaded', value: bool, family: string,
233 * weight?: string, style?: string }
234 * Omitting weight/style targets the whole family.
235 *
236 * @param WP_REST_Request $request Request.
237 * @return WP_REST_Response
238 */
239 public function toggle_font( WP_REST_Request $request ): WP_REST_Response {
240 $params = (array) $request->get_json_params();
241
242 $field = sanitize_text_field( $params['field'] ?? '' );
243 $family = sanitize_text_field( $params['family'] ?? '' );
244 $weight = isset( $params['weight'] ) ? sanitize_text_field( (string) $params['weight'] ) : '';
245 $style = isset( $params['style'] ) ? sanitize_text_field( (string) $params['style'] ) : '';
246 $value = ! empty( $params['value'] );
247
248 $column = 'enabled' === $field ? 'is_enabled' : ( 'preloaded' === $field ? 'is_preloaded' : '' );
249
250 if ( '' === $column || '' === $family ) {
251 return new WP_REST_Response( array( 'ok' => false, 'error' => 'bad_request' ), 400 );
252 }
253
254 ( new Registry() )->set_flag( $column, $value, $family, $weight, $style );
255
256 // Enabling/disabling changes which fonts the stylesheet contains, so the
257 // next render must rebuild it; bump the buster to invalidate caches.
258 if ( 'is_enabled' === $column ) {
259 Settings::bump_buster();
260 }
261
262 return new WP_REST_Response( array( 'ok' => true ), 200 );
263 }
264
265 /**
266 * POST /fonts/bulk — enable/disable (or preload) every font in a scope at
267 * once. Body: { field:'enabled'|'preloaded', value:bool, scope:'used'|'unused'|'all' }
268 *
269 * @param WP_REST_Request $request Request.
270 * @return WP_REST_Response
271 */
272 public function bulk_toggle( WP_REST_Request $request ): WP_REST_Response {
273 $params = (array) $request->get_json_params();
274
275 $field = 'preloaded' === ( $params['field'] ?? '' ) ? 'is_preloaded' : 'is_enabled';
276 $value = ! empty( $params['value'] );
277 $scope = in_array( $params['scope'] ?? 'all', array( 'used', 'unused', 'all' ), true ) ? $params['scope'] : 'all';
278
279 $registry = new Registry();
280
281 if ( 'all' === $scope ) {
282 $registry->set_flag_all( $field, $value );
283 } else {
284 $rendered = ( new UsageTracker() )->rendered_set();
285 $grouped = $registry->grouped_with_usage( $rendered['variants'], $rendered['families'] );
286
287 foreach ( $grouped as $family ) {
288 $is_used = ! empty( $family['used'] );
289
290 if ( ( 'used' === $scope && $is_used ) || ( 'unused' === $scope && ! $is_used ) ) {
291 $registry->set_flag( $field, $value, $family['family'] );
292 }
293 }
294 }
295
296 if ( 'is_enabled' === $field ) {
297 Settings::bump_buster();
298 }
299
300 return new WP_REST_Response( array( 'ok' => true ), 200 );
301 }
302
303 /**
304 * POST /optimize — prime optimisation now via a cache-busting loopback to
305 * the front page, so the user doesn't have to visit it manually.
306 *
307 * @return WP_REST_Response
308 */
309 public function optimize(): WP_REST_Response {
310 $targets = apply_filters(
311 'easyfonts_optimize_urls',
312 array( home_url( '/' ) )
313 );
314
315 $ok = 0;
316
317 foreach ( (array) $targets as $target ) {
318 // Cache-busting param forces a fresh PHP render (bypasses page cache).
319 $url = add_query_arg( 'easyfonts_probe', (string) time(), $target );
320
321 $response = wp_remote_get(
322 $url,
323 array(
324 'timeout' => 30,
325 /**
326 * Verify SSL on the loopback self-request. True by default;
327 * filter to false only for self-signed/staging certs.
328 *
329 * @param bool $verify
330 */
331 'sslverify' => (bool) apply_filters( 'easyfonts_loopback_sslverify', true ),
332 )
333 );
334
335 if ( ! is_wp_error( $response ) && (int) wp_remote_retrieve_response_code( $response ) < 400 ) {
336 $ok++;
337 }
338 }
339
340 $stats = ( new Registry() )->stats();
341
342 return new WP_REST_Response(
343 array(
344 'ok' => $ok > 0,
345 'probed' => $ok,
346 'families' => $stats['families'],
347 'variants' => $stats['variants'],
348 // The admin loads this in a hidden iframe to run the real-browser
349 // X-ray (beacon), which measures rendered fonts + subsets.
350 'probe_url' => add_query_arg( 'easyfonts_probe', (string) time(), home_url( '/' ) ),
351 // Snapshot of the last-beacon time BEFORE the X-ray; the client
352 // polls /stats and watches for this to advance, which confirms the
353 // iframe actually rendered (and wasn't blocked by X-Frame-Options).
354 'beacon_before' => (int) get_option( 'easyfonts_last_beacon', 0 ),
355 ),
356 200
357 );
358 }
359
360 /**
361 * GET /usage
362 *
363 * @return WP_REST_Response
364 */
365 public function get_usage(): WP_REST_Response {
366 return new WP_REST_Response( ( new UsageTracker() )->summary(), 200 );
367 }
368
369 /**
370 * GET /stats
371 *
372 * @return WP_REST_Response
373 */
374 public function get_stats(): WP_REST_Response {
375 $registry = new Registry();
376 $storage = new Storage();
377 $stats = $registry->stats();
378
379 return new WP_REST_Response(
380 array(
381 'families' => $stats['families'],
382 'variants' => $stats['variants'],
383 'hosted_kb' => (int) round( $stats['bytes'] / 1024 ),
384 'cache_kb' => (int) round( $storage->total_size() / 1024 ),
385 'detectors' => (array) Settings::get( 'detectors', array() ),
386 'last_buster' => Settings::buster(),
387 'last_beacon' => (int) get_option( 'easyfonts_last_beacon', 0 ),
388 ),
389 200
390 );
391 }
392
393 /**
394 * POST /purge — clear cache + tables, keep settings.
395 *
396 * @return WP_REST_Response
397 */
398 public function purge(): WP_REST_Response {
399 ( new Storage() )->purge();
400 ( new Registry() )->truncate();
401 ( new UsageTracker() )->truncate();
402
403 delete_option( 'easyfonts_processed_local' );
404 delete_option( 'easyfonts_processed_external' );
405
406 $settings = Settings::all();
407 $settings['detectors'] = array();
408 Settings::save( $settings );
409 Settings::bump_buster();
410
411 return new WP_REST_Response( array( 'ok' => true ), 200 );
412 }
413
414 /**
415 * POST /repair — drop + recreate the tables (fixes stale/legacy schema),
416 * then clear cache + learned state so the next page view rebuilds cleanly.
417 *
418 * @return WP_REST_Response
419 */
420 public function repair(): WP_REST_Response {
421 ( new Migrator() )->repair();
422 ( new Storage() )->purge();
423
424 delete_option( 'easyfonts_processed_local' );
425 delete_option( 'easyfonts_processed_external' );
426 delete_transient( 'easyfonts_schema_checked' );
427
428 $settings = Settings::all();
429 $settings['detectors'] = array();
430 Settings::save( $settings );
431 Settings::bump_buster();
432
433 return new WP_REST_Response(
434 array(
435 'ok' => true,
436 'tables' => ( new Migrator() )->status(),
437 ),
438 200
439 );
440 }
441
442 /**
443 * GET /diagnostics
444 *
445 * @return WP_REST_Response
446 */
447 public function diagnostics(): WP_REST_Response {
448 return new WP_REST_Response( Diagnostics::collect(), 200 );
449 }
450
451 /**
452 * POST /beacon — ingest real-render decisions.
453 *
454 * @param WP_REST_Request $request Request.
455 * @return WP_REST_Response
456 */
457 public function beacon( WP_REST_Request $request ): WP_REST_Response {
458 $nonce = $request->get_header( 'X-WP-Nonce' );
459
460 if ( ! $nonce ) {
461 $nonce = $request->get_param( '_wpnonce' );
462 }
463
464 if ( ! wp_verify_nonce( (string) $nonce, 'wp_rest' ) ) {
465 return new WP_REST_Response( array( 'ok' => false ), 403 );
466 }
467
468 $params = (array) $request->get_json_params();
469 $route = sanitize_text_field( $params['route'] ?? '/' );
470 $device = in_array( $params['device'] ?? '', array( 'mobile', 'desktop' ), true ) ? $params['device'] : 'any';
471
472 // Cap the route length to keep the indexed column sane.
473 $route = '' === $route ? '/' : substr( $route, 0, 480 );
474
475 $usage = new UsageTracker();
476
477 // Write-once per route + device: once a fresh decision exists for this
478 // route/device we ignore further public beacons for it until the
479 // retention window elapses. This bounds DB writes and neutralises
480 // repeated-request abuse (an anonymous endpoint must not be writable on
481 // every hit, nor able to keep flipping persistent flags) — without
482 // losing any functionality, since the first beacon already captured it.
483 if ( $usage->has_fresh_decision( $route, (string) $device ) ) {
484 return new WP_REST_Response( array( 'ok' => true, 'cached' => true ), 200 );
485 }
486
487 // Hard caps on payload size (defence against oversized public payloads).
488 $cap = 200;
489 $rendered = is_array( $params['rendered'] ?? null ) ? array_slice( $params['rendered'], 0, $cap ) : array();
490 $preload = is_array( $params['preload'] ?? null ) ? array_slice( $params['preload'], 0, $cap ) : array();
491 $unload = is_array( $params['unload'] ?? null ) ? array_slice( $params['unload'], 0, $cap ) : array();
492
493 // Mark that a real-browser beacon just reported in. The admin polls this
494 // after opening the optimize iframe to confirm the X-ray actually ran
495 // (vs. being blocked by X-Frame-Options / CSP).
496 update_option( 'easyfonts_last_beacon', time(), false );
497
498 // Record observed usage.
499 $records = array();
500
501 foreach ( $rendered as $r ) {
502 if ( empty( $r['family'] ) ) {
503 continue;
504 }
505
506 $records[] = array(
507 'family' => $r['family'],
508 'weight' => $r['weight'] ?? '400',
509 'style' => $r['style'] ?? 'normal',
510 'origin' => 'beacon',
511 'rendered' => 1,
512 'above_fold' => ! empty( $r['above_fold'] ) ? 1 : 0,
513 );
514 }
515
516 if ( ! empty( $records ) ) {
517 $usage->record( $route, url_to_postid( home_url( $route ) ), $records );
518 }
519
520 // Store the decision for future preloads/unloads.
521 $usage->store_decision( $route, (string) $device, $this->clean_variants( $preload ), $this->clean_variants( $unload ) );
522
523 // Auto-preload above-the-fold fonts the beacon just measured — capped,
524 // and only where the user hasn't already made a preload choice. Preload
525 // is a scarce resource: preloading every above-fold face hurts more than
526 // it helps, so we cap to the first few. Newly-detected fonts already
527 // default to Load=on at insert time, so used fonts light up automatically;
528 // this adds the Preload half for the genuine hero fonts only.
529 if ( Settings::get( 'smart_preload', 1 ) && ! empty( $preload ) ) {
530 $registry = new Registry();
531
532 /**
533 * Max number of fonts to auto-preload from a single page's
534 * above-the-fold set. Keep small — preload is for hero fonts only.
535 *
536 * @param int $cap
537 */
538 $preload_cap = (int) apply_filters( 'easyfonts_auto_preload_cap', 2 );
539 $done = 0;
540
541 foreach ( $this->clean_variants( $preload ) as $variant ) {
542 if ( $done >= $preload_cap ) {
543 break;
544 }
545
546 // Respect a user's explicit preload choice: only auto-set when
547 // preload_user_set = 0 for this variant.
548 if ( $registry->auto_preload( $variant['family'], $variant['weight'], $variant['style'] ) ) {
549 $done++;
550 }
551 }
552 }
553
554 return new WP_REST_Response( array( 'ok' => true ), 200 );
555 }
556
557 /**
558 * Sanitise a list of {family,weight,style} entries.
559 *
560 * @param array<int,mixed> $list List.
561 * @return array<int,array{family:string,weight:string,style:string}>
562 */
563 private function clean_variants( array $list ): array {
564 $out = array();
565
566 foreach ( $list as $item ) {
567 if ( empty( $item['family'] ) ) {
568 continue;
569 }
570
571 $out[] = array(
572 'family' => sanitize_text_field( $item['family'] ),
573 'weight' => sanitize_text_field( (string) ( $item['weight'] ?? '400' ) ),
574 'style' => sanitize_text_field( (string) ( $item['style'] ?? 'normal' ) ),
575 );
576 }
577
578 return $out;
579 }
580
581 /**
582 * POST /async-fonts — record Google/Bunny CSS URLs that were injected by
583 * JavaScript at runtime (caught client-side by async-blocker.js) so the next
584 * server render can self-host them. Nonce-checked, provider-validated, and
585 * capped. Public input only ever queues *provider* CSS URLs for hosting; it
586 * cannot write arbitrary data or flip any persistent setting.
587 *
588 * @param WP_REST_Request $request Request.
589 * @return WP_REST_Response
590 */
591 public function async_fonts( WP_REST_Request $request ): WP_REST_Response {
592 $nonce = $request->get_header( 'X-WP-Nonce' );
593
594 if ( ! $nonce ) {
595 $nonce = $request->get_param( '_wpnonce' );
596 }
597
598 if ( ! wp_verify_nonce( (string) $nonce, 'wp_rest' ) ) {
599 return new WP_REST_Response( array( 'ok' => false ), 403 );
600 }
601
602 if ( ! Settings::get( 'enabled', 1 ) || ! Settings::get( 'async_blocker', 0 ) ) {
603 return new WP_REST_Response( array( 'ok' => false ), 403 );
604 }
605
606 $params = (array) $request->get_json_params();
607 $urls = is_array( $params['urls'] ?? null ) ? array_slice( $params['urls'], 0, 25 ) : array();
608
609 $stored = (array) get_option( 'easyfonts_async_urls', array() );
610 $before = count( $stored );
611
612 foreach ( $urls as $raw ) {
613 $url = \EasyFonts\Detect\Providers::normalize_url( (string) $raw );
614
615 // Only accept genuine provider stylesheet URLs.
616 if ( ! \EasyFonts\Detect\Providers::is_css_url( $url ) ) {
617 continue;
618 }
619
620 $url = esc_url_raw( $url );
621
622 if ( '' !== $url && ! in_array( $url, $stored, true ) ) {
623 $stored[] = $url;
624 }
625 }
626
627 // Keep the queue bounded.
628 if ( count( $stored ) > 50 ) {
629 $stored = array_slice( $stored, -50 );
630 }
631
632 if ( count( $stored ) !== $before ) {
633 update_option( 'easyfonts_async_urls', array_values( $stored ), false );
634 // New runtime fonts discovered → invalidate so the next render hosts them.
635 Settings::bump_buster();
636 }
637
638 return new WP_REST_Response( array( 'ok' => true, 'queued' => count( $stored ) ), 200 );
639 }
640
641 /**
642 * GET /export — return the current settings as a portable JSON payload.
643 *
644 * @return WP_REST_Response
645 */
646 public function export_settings(): WP_REST_Response {
647 return new WP_REST_Response(
648 array(
649 '_type' => 'easyfonts-settings',
650 '_version' => EASYFONTS_VERSION,
651 'settings' => Settings::all(),
652 ),
653 200
654 );
655 }
656
657 /**
658 * POST /import — replace settings from an exported payload. Values are run
659 * through the same validation as the normal save path (no raw write).
660 *
661 * @param WP_REST_Request $request Request.
662 * @return WP_REST_Response
663 */
664 public function import_settings( WP_REST_Request $request ): WP_REST_Response {
665 $body = (array) $request->get_json_params();
666
667 // Accept either the wrapped export ({settings:{…}}) or a bare object.
668 $incoming = isset( $body['settings'] ) && is_array( $body['settings'] ) ? $body['settings'] : $body;
669
670 if ( empty( $incoming ) ) {
671 return new WP_REST_Response( array( 'ok' => false, 'error' => 'empty' ), 400 );
672 }
673
674 $defaults = Migrator::default_settings();
675 $current = Settings::all();
676
677 // Booleans.
678 foreach ( array( 'enabled', 'auto_config', 'strip_hints', 'smart_preload', 'metric_fallbacks', 'beacon', 'inline_css', 'async_blocker', 'remove_data_on_uninstall' ) as $key ) {
679 if ( array_key_exists( $key, $incoming ) ) {
680 $current[ $key ] = $incoming[ $key ] ? 1 : 0;
681 }
682 }
683
684 // font-display.
685 if ( isset( $incoming['font_display'] ) ) {
686 $allowed = array( 'swap', 'fallback', 'optional', 'block', 'auto', 'none' );
687 $val = sanitize_text_field( (string) $incoming['font_display'] );
688 $current['font_display'] = in_array( $val, $allowed, true ) ? $val : 'swap';
689 }
690
691 // Subsets / excluded URLs (arrays of strings).
692 foreach ( array( 'subsets', 'excluded_urls' ) as $key ) {
693 if ( isset( $incoming[ $key ] ) && is_array( $incoming[ $key ] ) ) {
694 $current[ $key ] = array_values( array_filter( array_map( 'sanitize_text_field', $incoming[ $key ] ) ) );
695 }
696 }
697
698 // CDN URL.
699 if ( array_key_exists( 'cdn_url', $incoming ) ) {
700 $cdn = trim( (string) $incoming['cdn_url'] );
701
702 if ( '' !== $cdn && 0 === strpos( $cdn, '//' ) ) {
703 $cdn = ( is_ssl() ? 'https:' : 'http:' ) . $cdn;
704 }
705
706 $current['cdn_url'] = ( '' !== $cdn && preg_match( '#^https?://#i', $cdn ) )
707 ? untrailingslashit( esc_url_raw( $cdn ) )
708 : '';
709 }
710
711 // Never import a foreign detector set; keep what this site learned.
712 $current['detectors'] = (array) ( $current['detectors'] ?? $defaults['detectors'] );
713
714 Settings::save( $current );
715 Settings::bump_buster();
716
717 return new WP_REST_Response( array( 'ok' => true, 'settings' => Settings::all() ), 200 );
718 }
719 }