-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush-tags.php
91 lines (76 loc) · 2.18 KB
/
push-tags.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
<?php
//function that sets the last notified post
function ml_set_post_id_as_notified($postID)
{
global $wpdb;
$table_name = $wpdb->prefix . "mobiloud_notifications";
$wpdb->insert(
$table_name,
array(
'time' => current_time("timestamp"),
'post_id' => $postID,
)
);
}
function ml_is_notified($post_id)
{
global $wpdb;
$table_name = $wpdb->prefix . "mobiloud_notifications";
$num = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %s WHERE post_id = %d",$table_name, $post_id));
return $num > 0;
}
function ml_post_published_notification($post_id)
{
$post = get_post($post_id,OBJECT);
$alert = $post->post_title;
$custom_properties = array('post_id' => $post_id);
//tags
$tags = array();
//subscriptions
if(ml_subscriptions_enable()) {
$tags[] = "all";
$capabilities = ml_subscriptions_post_capabilities($post);
foreach($capabilities as $c) {
$tags[] = $c;
}
} else {
$tags[] = "all";
$categories = wp_get_post_categories($post->ID);
foreach($categories as $c) {
if($c != NULL) $tags[] = $c;
}
}
ml_send_notification($alert, true,NULL,$custom_properties,$tags,$post_id);
}
//true if the notification was sent successfully
//false if there was an error
function ml_send_notification($alert, $sound=true, $badge=NULL, $custom_properties=NULL, $tags=NULL,$remote_identifier=NULL)
{
global $ml_api_key, $ml_secret_key;
//push notification only when api key is set
if(($ml_api_key == NULL || strlen($ml_api_key) < 5) &&
($ml_secret_key == NULL || strlen($ml_secret_key) < 5))
{
return false;
}
$notification = array('alert' => $alert);
if($sound) $notification['sound'] = $sound;
if($badge) $notification['badge'] = $badge;
if($custom_properties) $notification['custom_properties'] = $custom_properties;
if($tags) $notification['tags'] = $tags;
$parameters = array(
'api_key' => $ml_api_key,
'secret_key' => $ml_secret_key,
'notification' => $notification,
);
//postID
if($remote_identifier)
{
$parameters['remote_identifier'] = "$remote_identifier";
}
$request = new WP_Http;
$result = $request->request(MOBILOUD_PUSH_API_PUBLISH_URL,
array('method' => 'POST', 'timeout' => 10,'body' => $parameters) );
return false;
}
?>