-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
347 lines (290 loc) · 11.6 KB
/
index.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
* @package WP_Meetup_Events
* @version 0.1
*/
/*
Plugin Name: Meetup Events
Plugin URI: http://github.com/sdague/wp-meetup-events
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
Author: Sean Dague
Version: 0.1
Author URI: http://dague.net
*/
add_action('save_post', 'wp_meetup_events_sync');
add_action('admin_init', 'wp_meetup_events_register_settings');
add_action('tribe_events_single_event_after_the_content', 'wp_meetup_events_rsvp');
function wp_meetup_events_rsvp() {
$post_id = get_the_ID();
error_log("Post id = $post_id");
if (! $post_id ) {
return;
}
$meetup_id = get_post_meta($post_id, '_MeetupID', true);
if (! $meetup_id ) {
return;
}
echo "<a href=\"https://www.meetup.com/hvopen/events/$meetup_id\" ";
echo "data-event=\"$meetup_id\" class=\"mu-rsvp-btn\">";
echo '<button class="meetup_events">RSVP on Meetup</button></a>';
}
/**
Guess the venue from the name. Meetup API provides an interface
that returns that most active 10 venues, so this can be used to
fuzzy match our names.
*/
function wp_meetup_events_guess_venue($post_id) {
$params = array(
"key" => get_option("wp_meetup_apikey"),
"sign" => "true"
);
$payload = http_build_query($params, '', '&');
$url = "https://api.meetup.com/mhvlug/venues?" . $payload;
error_log("URL: $url");
$venue = tribe_get_venue($post_id);
error_log("Venue: $venue");
$data = array(
'method' => 'GET',
'timeout' => 45,
'redirection' => 5,
'blocking' => true,
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept-Charset' => 'utf-8'),
'cookies' => array()
);
$response = wp_remote_get( $url, $data);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
error_log("Something went wrong: $error_message");
} else {
$json = json_decode($response['body']);
foreach ($json as $v) {
error_log("Venue Name: " . $v->name);
if ($venue == $v->name) {
return $v->id;
}
}
error_log("No suitable venue found...");
}
}
function wp_meetup_events_create($post_id) {
$notice = AdminNotice::getInstance();
$MEETUP_API = "https://api.meetup.com/2/event";
$meetup_id = get_post_meta($post_id, '_MeetupID', true);
if ($meetup_id) {
$MEETUP_API .= "/$meetup_id";
}
error_log("Meetup ID: $meetup_id");
$key = get_option("wp_meetup_apikey");
$group_id = get_option("wp_meetup_id");
$post = get_post($post_id);
$title = $post->post_title;
$desc = $post->post_content;
$tz_string = get_post_meta( $post_id, '_EventTimezone', true );
# this converts us over to UTC time, which is what meetup needs
$start = Tribe__Events__Timezones::to_utc( tribe_get_start_date( $post_id, true, Tribe__Date_Utils::DBDATETIMEFORMAT ), $tz_string, 'c' );
$start = new DateTime($start);
$end = Tribe__Events__Timezones::to_utc( tribe_get_end_date( $post_id, true, Tribe__Date_Utils::DBDATETIMEFORMAT ), $tz_string, 'c' );
$end = new DateTime($end);
# $timezone = Tribe__Events__Timezones::get_event_timezone_string( $post_id );
# error_log("Start: $start");
# return;
# error_log($start);
# return;
# $start = new DateTime($start); # get_post_meta($post_id, '_EventStartDate', true));
$data = array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'blocking' => true,
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept-Charset' => 'utf-8'),
'body' => array(
// remove the TEST bit once we get to production
'name' => "TEST: $title",
'key' => $key,
'group_id' => $group_id,
'time' => $start->getTimestamp() * 1000,
'description' => $desc,
'duration' => ($end->getTimestamp() - $start->getTimestamp()) * 1000,
),
'cookies' => array()
);
error_log("API: $MEETUP_API");
$response = wp_remote_post( $MEETUP_API, $data);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
$notice->displayError('Error calling meetup API: $error_message');
error_log("Something went wrong: $error_message");
return;
}
$json = json_decode($response['body']);
if (isset($json->code) && $json->code == "not_found") {
error_log("Code was not found, deleting the meetup id");
$notice->displayError('The meetup id was not found in meetup, deleting our internal reference.');
delete_post_meta($post_id, '_MeetupID');
return;
}
$notice->displaySuccess('Event saved to meetup');
$new_meetup_id = $json->id;
error_log(print_r($json, true));
error_log("Meetup ID: $new_meetup_id");
if ($meetup_id) {
error_log("Trying to update");
update_post_meta($post_id, '_MeetupID', $new_meetup_id);
} else {
error_log("Trying to add");
add_post_meta($post_id, '_MeetupID', $new_meetup_id, true);
update_post_meta($post_id, '_MeetupID', $new_meetup_id);
}
// error_log(get_post_meta($post_id, "_MeetupID", true));
// We have to set the venue as an update, because the API expects
// a flow that looks like the manual flow.
$venue_id = wp_meetup_events_guess_venue($post_id);
if (! $venue_id ) {
error_log("No venue id found, not setting in meetup");
$notice->displayWarning('No venue id found in meetup, will have to save it manually');
return;
}
error_log("Setting venue_id: " . $venue_id);
$data = array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'blocking' => true,
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept-Charset' => 'utf-8'),
'body' => array(
'venue_id' => $venue_id,
),
'cookies' => array()
);
$response = wp_remote_post( "https://api.meetup.com/2/event/$new_meetup_id", $data);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
$notice->displayError('Failed to update venue: $error_message');
error_log("Something went wrong: $error_message");
}
// add_settings_error('wp_meetup_events_saved', 'wp_meetup_events_saved', "Updated meetup", "updated");
// add_action( 'admin_notices', 'sample_admin_notice__success' );
#);
# error_log(print_r($data, true));
}
function wp_meetup_events_sync($post_id){
$post_type = get_post_type($post_id);
if ($post_type == 'tribe_events'){
$event_datee = get_post_meta($post_id, '_EventStartDate', true);
$month = date("m",strtotime($event_datee));
wp_meetup_events_create($post_id);
error_log("Found an event!" . $post_id);
# update_post_meta($post_id, 'event_month', $month);
}
}
function wp_meetup_events_section($args) {
echo "<p>Settings for Wordpress to Meetup Synchronization</p>";
}
function wp_meetup_events_register_settings() {
register_setting( 'writing', 'wp_meetup_id' );
register_setting( 'writing', 'wp_meetup_apikey' );
add_settings_section( "wp_meetup",
"Meetup Events Sync Options",
"wp_meetup_events_section",
"writing");
add_settings_field(
"wp_meetup_events_meetup_apikey",
"Meetup API Key",
"wp_meetup_events_meetup_apikey_cb",
"writing",
"wp_meetup");
add_settings_field(
"wp_meetup_events_meetup_id",
"Meetup ID",
"wp_meetup_events_meetup_id_cb",
"writing",
"wp_meetup");
}
function wp_meetup_events_meetup_id_cb($args) {
// get the value of the setting we've registered with register_setting()
$setting = get_option('wp_meetup_id');
// output the field
?>
<input type="text" name="wp_meetup_id" value="<?= isset($setting) ? esc_attr($setting) : ''; ?>">
<?php
}
function wp_meetup_events_meetup_apikey_cb($args) {
// get the value of the setting we've registered with register_setting()
$setting = get_option('wp_meetup_apikey');
// output the field
?>
<input type="text" name="wp_meetup_apikey" value="<?= isset($setting) ? esc_attr($setting) : ''; ?>">
<?php
}
function sample_admin_notice__success() {
?>
<div class="notice notice-success is-dismissible">
<p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
</div>
<?php
}
# add_action( 'admin_notices', 'sample_admin_notice__success' );
// Utility class to send notices to the user
add_filter('post_updated_messages', [AdminNotice::getInstance(), 'displayAdminNotice']);
class AdminNotice {
private static $instance;
const NOTICE_FIELD = 'wp_meetup_events_admin_notice_message';
protected function __construct() {}
private function __clone() {}
private function __wakeup() {}
static function getInstance()
{
if (null === static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
public function displayAdminNotice()
{
$options = get_option(self::NOTICE_FIELD);
if ($options) {
foreach ($options as $option) {
$message = isset($option['message']) ? $option['message'] : false;
$noticeLevel = ! empty($option['notice-level']) ? $option['notice-level'] : 'notice-error';
if ($message) {
echo "<div class='notice {$noticeLevel} is-dismissible'><p>{$message}</p></div>";
}
}
delete_option(self::NOTICE_FIELD);
}
}
public function displayError($message)
{
$this->updateOption($message, 'notice-error');
}
public function displayWarning($message)
{
$this->updateOption($message, 'notice-warning');
}
public function displayInfo($message)
{
$this->updateOption($message, 'notice-info');
}
public function displaySuccess($message)
{
$this->updateOption($message, 'notice-success');
}
protected function updateOption($message, $noticeLevel) {
$current = get_option(self::NOTICE_FIELD);
if (! $current) {
$current = array();
}
error_log(print_r($current, true));
array_push($current, [
'message' => $message,
'notice-level' => $noticeLevel
]);
update_option(self::NOTICE_FIELD, $current);
}
}