-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-webpush.php
More file actions
160 lines (141 loc) · 4.26 KB
/
Copy pathwp-webpush.php
File metadata and controls
160 lines (141 loc) · 4.26 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
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
<?php
/**
* @package WebPush
*/
/*
Plugin Name: WebPush
Plugin URI: https://github.com/duraseb/wp-webpush
Description: Sends desktop notifications to your users about new content on your site
Version: 0.1.0
Author: Sebastian Szulc
Author URI: https://github.com/duraseb
*/
if (!function_exists('add_action')) exit;
require_once dirname( __FILE__ ) . '/webpushWidget.class.php';
add_action('init', 'webpush_init');
add_action('widgets_init', 'webpush_register_widgets');
function webpush_admin_css() {
?><style>
.toplevel_page_webpush_options > .wp-menu-image > img {
width: 16px;
padding-top: 7px;
}
.wrap .osrodek_form {
display: block;
vertical-align: top;
}
.wrap .osrodek_form input[type=text] {
width: 500px;
}
}
</style>
<?php
}
function webpush_menu() {
add_menu_page('WebPush configuration', 'WebPush', 'manage_options', 'webpush_options', 'webpush_main_options', plugins_url('/i/webpush-ico.png', __FILE__));
add_submenu_page('webpush_options', 'WebPush Stats', 'Statistics', 'manage_options', 'webpush_stats', 'webpush_submenu_stats');
}
function webpush_main_options() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2>WebPush Configuration</h2>
<p>Adjust settings for your WebPush configuration</p>
</div>
<?php
}
function webpush_submenu_stats() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2>WebPush - Stats</h2>
</div>
<?php
}
function webpush_init()
{
add_rewrite_tag('%subscription_id%', '([^/]+)');
add_rewrite_tag('%endpoint%', '([^/]+)');
add_rewrite_tag('%webpush_handler%', '([^/]+)');
add_action('wp_head', 'webpush_head');
add_action('parse_request', 'webpush_parse_request');
add_action('admin_menu', 'webpush_menu');
add_action('admin_head', 'webpush_admin_css');
if (!is_admin()) {
wp_enqueue_script('webpush-script',
plugins_url('/js/webpush.js', __FILE__ ),
array('jquery')
);
}
}
function webpush_parse_request(&$wp) {
if (array_key_exists('webpush_handler', $wp->query_vars)) {
webpush_handle_request($wp->query_vars);
}
}
function webpush_handle_request($q) {
switch ($q['webpush_handler']) {
case 'manifest':
header('Content-Type: application/json; charset=UTF-8');
echo webpush_manifest();
break;
case 'serviceworker':
header('Content-Type: application/javascript; charset=UTF-8');
echo webpush_serviceworker();
break;
case 'subscribe':
echo webpush_subscribe($q);
break;
case 'notifications':
echo webpush_notifications($q);
break;
}
exit;
}
function webpush_manifest() {
// TODO read values from config
$manifest = array(
"name" => "Blog title",
"short_name" => "Blog short name",
"icons" => [
[
"src" => plugins_url("/i/webpush-ico.png", __FILE__),
"sizes" => "120x120",
"type" => "image/png"
],
],
"start_url" => "/",
"display" => "standalone",
"gcm_sender_id" => "1111111111",
"gcm_user_visible_only" => true
);
return json_encode($manifest, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
function webpush_serviceworker() {
$sw_filename = plugin_dir_path(__FILE__) . 'js/service_worker.js';
if (file_exists($sw_filename)) {
return file_get_contents($sw_filename);
}
return '';
}
function webpush_subscribe($q) {
echo json_encode($q);
}
function webpush_notifications($q) {
}
function webpush_register_widgets()
{
register_widget('WebpushWidget');
}
function webpush_head()
{
?>
<link rel="manifest" href="<?php echo site_url('?webpush_handler=manifest'); ?>">
<?php
}