forked from sebet/freemius-webhook-listener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreemius-webhook-listener.php
256 lines (187 loc) · 6.83 KB
/
freemius-webhook-listener.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* Plugin Name: Simple Freemius WebHooks Listener
* Description: WebHook listener for subscribing Freemium users to 3rd party services like MailChimp.
* Author: SebeT
* Contributors: Freemius, PressWizards
* Version: 1.0
* Author URI: https://bruno-carreco.com
*/
/**
* Notes:
*
* . This plugin subscribes/unsubscribes users to specific MailChimp list segments depending on their state: Free or Premium.
* . The plugin can be extended to use other services.
*
*/
/**
* The base classe for listening to WebHooks.
*/
final class Freemius_WebHook_Listener {
private static $plugin = array(
'type' => 'plugin', // 'plugin' or 'theme'
'id' => '<YOUR THEME/PLUGIN ID>',
'public_key' => '<THE PUBLIC KEY>',
'secret_key' => '<THE SECRET KEY>'
);
/**
* Listens to Freemius WebHooks using a specific query string param 'fwebhook' which informs on the service to use.
*
* e.g:: http://your-site.com?fwebhook=mailchimp (this is the link you would set on your Freemius dashboard under 'Settings' > 'WebHooks' to use 'MailChimp')
*/
public static function listen() {
if ( empty( $_SERVER['QUERY_STRING'] ) || false === strpos( $_SERVER['QUERY_STRING'], 'fwebhook' ) ) {
return;
}
parse_str( $_SERVER['QUERY_STRING'] );
switch ( $fwebhook ) {
case 'mailchimp':
self::mailchimp();
break;
// other services here
}
http_response_code(200);
}
/**
* Execute Mailchimp related API calls.
*
* http://developer.mailchimp.com/documentation/mailchimp/reference
*/
protected static function mailchimp() {
// Retrieve the request's body
$input = @file_get_contents("php://input");
extract( self::$plugin );
// Verify the authenticity of the request.
$hash = hash_hmac('sha256', $input, $secret_key);
$signature = $_SERVER['HTTP_X_SIGNATURE'] ?? '';
if ( ! hash_equals($hash, $signature))
{
// Invalid signature, don't expose any data to attackers.
http_response_code(200);
exit;
}
// Decode the request.
$fs_event = json_decode($input);
$user = $fs_event->objects->user;
$email = $user->email;
$first = $user->first;
$last = $user->last;
$data = array(
'email_address' => $email,
'merge_fields' => array(
'FNAME' => $first,
'LNAME' => $last
),
);
$mc = new PW_Mailchimp_API();
switch ( $fs_event->type ) {
case 'install.installed':
// User installed the plugin.
$list_id = '<THE MAILCHIMP LIST ID>';
// Subscribe users to specific Mailchimp list.
$mc->subscribe( $data, $list_id );
// Subscribe FREE users to a specific list FREE segment.
$mc->subscribe( $data, $list_id, $segment_id = '<THE MAILCHIMP FREE LIST SEGMENT ID>' ); // test bfc
break;
case 'license.created':
// User upgraded.
$list_id = '<THE MAILCHIMP LIST ID>';
// Subscribe PREMIUM users to the list PREMIUM segment.
$mc->subscribe( $data, $list_id, $segment_id = '<THE MAILCHIMP PREMIUM LIST SEGMENT ID>' ); // teste bfc
// Remove the user from the FREE list segment.
$mc->remove( $email, $list_id, $segment_id = '<THE MAILCHIMP FREE LIST SEGMENT ID>' ); // teste bfc
break;
case 'license.expired':
case 'install.plan.downgraded':
// User downgraded/license expired.
$list_id = '<THE MAILCHIMP LIST ID>';
// Subscribe downgraded user to the MailChimp list FREE segment.
$mc->subscribe( $data, $list_id, $segment_id = 'THE MAILCHIMP FREE LIST SEGMENT ID>' ); // teste bfc
// Remove the user from the list PREMIUM segment.
$mc->remove( $email, $list_id, $segment_id = '<THE MAILCHIMP PREMIUM LIST SEGMENT ID>' ); // teste bfc
break;
}
}
}
/**
* Base class for Mailchimp API callbacks.
*/
class PW_Mailchimp_API {
/**
* The Mailchimp API key.
*/
private $api_key = '<YOUR MAILCHIMP API KEY>';
/**
* __construct.
*/
public function __construct( $api_key = '' ) {
if ( $api_key ) {
$this->api_key = $api_key;
}
}
/**
* Subscribe a user to a specific Mailchimp list ID and segment ID (if provided).
*/
public function subscribe( $data, $list_id, $segment_id = 0 ) {
$data['status'] = 'subscribed';
$args = array(
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key )
),
'body' => json_encode( $data )
);
$api_key_parts = explode( '-', $this->api_key );
$dc = $api_key_parts[1];
$url = 'http://' . $dc . '.api.mailchimp.com/3.0';
if ( $segment_id ) {
$url .= "/lists/{$list_id}/segments/{$segment_id}/members";
} else {
$url .= "/lists/{$list_id}/members";
}
return $this->_wp_remote_post( $url, $args );
}
/**
* Unsubscribe a user to a specific Mailchimp list ID and segment ID (if provided).
*/
public function remove( $email, $list_id, $segment_id = 0 ) {
$args = array(
'method' => 'DELETE',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key )
),
);
$api_key_parts = explode( '-', $this->api_key );
$dc = $api_key_parts[1];
$url = 'http://' . $dc . '.api.mailchimp.com/3.0';
$subscriber_hash = md5( strtolower( $email ) );
if ( $segment_id ) {
$url .= "/lists/{$list_id}/segments/{$segment_id}/members/{$subscriber_hash}";
} else {
$url .= "/lists/{$list_id}/members/{$subscriber_hash}";
}
return $this->_wp_remote_post( $url, $args );
}
/**
* Wrapper for the 'wp_remote_post()' calls.
*/
private function _wp_remote_post( $url, $args ) {
$response = wp_remote_post( $url, $args );
if ( is_wp_error( $response ) ) {
$this->log( $response );
return $response;
}
return true;
}
/**
* Log any errors.
*/
public function log( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
Freemius_WebHook_Listener::listen();