PluginProbe
WPJAM Basic / trunk
WPJAM Basic vtrunk
wpjam-basic / extends / wpjam-speculation-rules.php

wpjam-speculation-rules.php in WPJAM Basic trunk, at extends/wpjam-speculation-rules.php

41 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Name: 推测加载
4 URI: https://mp.weixin.qq.com/s/DAqil-PxyL8rxzWBiwlA3A
5 Description: 将网页推测加载规则放到 Head 以及支持设置规则的触发时机
6 Version: 2.0
7 */
8 class WPJAM_Speculation_Rules{
9 public static function __callStatic($method, $args){
10 return wpjam_call_option(static::class, $method, ...$args);
11 }
12
13 public static function get_fields(){
14 return ['speculation_rules' => ['title'=>'推测加载', 'type'=>'fieldset', 'fields'=>[
15 'speculation_rules_eagerness' => ['before'=>'触发时机:', 'options'=>[
16 'conservative' => ['title'=>'保守', 'description'=>'接近点击时才触发:只有鼠标被按下的那一瞬间。'],
17 'moderate' => ['title'=>'适度', 'description'=>'明确用户意向才触发:鼠标悬停/聚焦在链接一段时间,或�
18 反复将鼠标移至链接附近。'],
19 'eager' => ['title'=>'积极', 'description'=>'轻微用户行为即触发:鼠标移向/短暂悬停/聚焦在链接上,链接处于页面显眼位置并页面暂停滚动。'],
20 'immediate' => ['title'=>'立即', 'description'=>'无用户行为也触发:规则解析完成后立刻开始预加载所有匹�
21 �的链接。', 'disabled'],
22 ]]
23 ]]];
24 }
25
26 public static function add_hooks(){
27 remove_action('wp_footer', 'wp_print_speculation_rules');
28 add_action('wp_head', 'wp_print_speculation_rules');
29
30 add_filter('wp_speculation_rules_configuration', fn($config)=> is_array($config) ? ['eagerness'=> self::get_setting('speculation_rules_eagerness') ?: 'auto']+$config : $config);
31
32 // add_action('send_headers', fn()=> header('Speculation-Rules: "'.home_url('api/speculation/rules.json').'"'));
33
34 // wpjam_register_json('speculation.rules', ['callback'=>function(){
35 // header('Content-Type: application/speculationrules+json');
36 // echo wpjam_json_encode(wp_get_speculation_rules()); die;
37 // }]);
38 }
39 }
40
41 wpjam_add_option_section('wpjam-basic', 'enhance', ['model'=>'WPJAM_Speculation_Rules']);