-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcleverpush-api.php
96 lines (80 loc) · 2.72 KB
/
cleverpush-api.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
<?php
if (!defined('ABSPATH')) {
exit;
}
const CLEVERPUSH_API_ENDPOINT = 'https://api.cleverpush.com';
class CleverPush_Api
{
public static function request($path, $params)
{
$api_key_private = get_option('cleverpush_apikey_private');
if (empty($api_key_private)) {
return null;
}
$response = wp_remote_post(
CLEVERPUSH_API_ENDPOINT . $path, array(
'timeout' => 20, // phpcs:ignore
'headers' => array(
'authorization' => $api_key_private,
'content-type' => 'application/json'
),
'body' => json_encode($params)
)
);
$error_message = null;
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
} elseif (!in_array(wp_remote_retrieve_response_code($response), array(200, 201)) ) {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
if ($data && !empty($data->error)) {
$error_message = $data->error;
} else {
$error_message = 'HTTP ' . wp_remote_retrieve_response_code($response);
}
}
if (!empty($error_message)) {
throw new Exception($error_message);
}
return $response;
}
public static function send_notification($title, $body, $url, $options = array(), $subscriptionId = null)
{
$channel_id = get_option('cleverpush_channel_id');
if (get_option('cleverpush_enable_domain_replacement') == 'on') {
$option_url = get_option('cleverpush_replacement_domain');
if (!empty($option_url)) {
$parsed_url = parse_url($url);
if ($parsed_url) {
$host = $parsed_url['host'];
if (!empty($host)) {
$url = str_replace($host, $option_url, $url);
}
}
}
}
if (empty($channel_id)) {
return null;
}
$params = array_merge(
array(
'channel' => $channel_id,
'title' => $title,
'text' => $body,
'url' => $url
),
$options
);
if ($subscriptionId) {
$params['subscriptionId'] = $subscriptionId;
}
return CleverPush_Api::request('/notification/send', $params);
}
public static function update_channel($channel_id, $params = array())
{
if (empty($channel_id)) {
return null;
}
return CleverPush_Api::request('/channel/' . $channel_id, $params);
}
}