-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw-cache.php
More file actions
94 lines (48 loc) · 1.89 KB
/
sw-cache.php
File metadata and controls
94 lines (48 loc) · 1.89 KB
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
<?php
/*
Plugin Name: WP SW Offline Cache
Plugin URI:
Description: A plugin to enhance offline capabilities using a service worker
Version: 1.0
Author: Mr. Samuel Thomas
Author URI:
License: GPL2
*/
$sw_cache_setting = new sw_cache_setting;
class sw_cache_setting{
function __construct( ) {
add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
add_action('wp_footer', array( &$this, 'enqueue_script' ) );
}
function register_fields() {
register_setting( 'general', 'wp_sw_cache', 'esc_attr' );
add_settings_field('fav_color', '<label for="wp_sw_cache">'.__('Enable WP SW Cache' , 'wp_sw_cache' ).'</label>' , array(&$this, 'fields_html') , 'general' );
}
function is_cache_enabled(){
$value = get_option( 'wp_sw_cache', '' );
if( $value == 1 ){
return true;
}
return false;
}
function fields_html() {
$checked = '';
if( $this->is_cache_enabled() ){
$checked = 'checked=checked';
}
echo '<input type="checkbox" id="wp_sw_cache" name="wp_sw_cache" value="1" '.$checked.' />';
}
function get_js_url(){
return plugins_url('wp-cache-sw/js/');
}
function enqueue_script(){
$js_url = $this->get_js_url();
wp_enqueue_script('sw-cache', $js_url . 'main.js', array('jquery'), '3.3.9');
$sw_url = $js_url . 'sdk.js?v=6';
$url = site_url('service-worker.js') . "?file=" . $sw_url;
wp_localize_script('sw-cache', 'wp_sw_cache_settings', array(
'sw_js_url' => $url,
'sw_enable' => $this->is_cache_enabled()
));
}
}