-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp_gcm_push.php
132 lines (108 loc) · 3.51 KB
/
wp_gcm_push.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
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
<?php
/**
* @package wp_gcm_push
* @version 1.0
*/
/*
Plugin Name: WP GCM PUSH
Plugin URI:
Description:
Author: Vincenzo Bruno
Version: 1.0
Author URI: http://www.vincenzobruno.it
*/
$wpdb = $GLOBALS['wpdb'];
define('GCM_API_KEY', "");// Put your GCM APY KEY HERE. Obtain a key here https://console.developers.google.com "APIs & auth"->Credentials
// Table name where Android GCM users are stored
define('TABLE_NAME', $wpdb->prefix . 'gcm_users');
// Security check
defined('ABSPATH') or die("No script kiddies please!");
// Call our function when a post gets published the first time
add_action('draft_to_publish', 'wp_gcm_push', 10, 2);
add_action('auto-draft_to_publish', 'wp_gcm_push', 10, 2);
add_action('pending_to_publish', 'wp_gcm_push', 10, 2);
/**
* Function called by action hook
*
* @param type $ID ID of the post
* @param type $post Post object
*/
function wp_gcm_push($ID, $post) {
$link = get_permalink($ID);
$title = $post->post_title;
push_gcm($link, $title);
}
/**
* Push notification to registered users
*
* @global type $wpdb Global object for Wordpress database
* @param type $link Permalink of the post sent to Android for notification link
* @param type $title Title of the post sent to Android for notification text
*/
function push_gcm($link, $title) {
global $wpdb;
$table_name = TABLE_NAME;
// Replace with real BROWSER API key from Google APIs
$apiKey = GCM_API_KEY;
// Seleziona gli utenti
$users = $wpdb->get_results("SELECT * FROM $table_name LIMIT 999");
foreach ($users as $u) {
// Recipient's list
$registrationIDs[] = $u->gcm_regid;
}
// Message to be sent
$message = $title;
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array("message" => $message, "link" => $link),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
//echo $result." <br>".json_encode( $fields );
}
// Create data structure at installation
register_activation_hook(__FILE__, 'wp_gcm_push_install');
/**
* Function called at plugin activaction.
* It creates the GCM users table
*
*/
function wp_gcm_push_install() {
global $wpdb;
$table_name = TABLE_NAME;
/*
* We'll set the default character set and collation for this table.
* If we don't do this, some characters could end up being converted
* to just ?'s when saved in our table.
*/
$charset_collate = '';
if (!empty($wpdb->charset)) {
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
}
if (!empty($wpdb->collate)) {
$charset_collate .= " COLLATE {$wpdb->collate}";
}
$sql = "CREATE TABLE $table_name ( "
. "gcm_regid varchar(255) NOT NULL, "
. "created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, "
. "PRIMARY KEY (gcm_regid) "
. ") $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql);
}