-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass-meetup-widget.php
247 lines (216 loc) · 6.03 KB
/
class-meetup-widget.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
<?php
/**
* All the logic for fetching and displaying Meetup.com data
*
* @package Meetup_Widgets
*/
/**
* Class container for requesting & displaying meetup.com data
*/
class Meetup_Widget {
/**
* The URL used to fetch event data.
*
* @var string $base_url
*/
private $base_url = 'http://api.meetup.com/2/events/';
/**
* Holds the user's Meetup.com API key
*
* @var string $api_key
*/
protected $api_key = '';
/**
* Set our API key by getting it from the database
*/
public function __construct() {
$options = get_option( 'vs_meet_options' );
$this->api_key = $options['vs_meetup_api_key'];
}
/**
* Given arguments & a transient name, grab data from the events API
*
* @param array $args Query params to send to events.json call.
* @param string $transient The transient name (if empty, no transient stored).
* @return array Event data (single event or list)
*/
public function get_data( $args, $transient = '' ) {
if ( $transient ) {
$event = get_transient( $transient );
}
$defaults = array(
'key' => $this->api_key,
);
if ( false === $event ) {
$args = wp_parse_args( $args, $defaults );
$url = add_query_arg( $args, $this->base_url );
$event_response = wp_remote_get( $url );
if ( is_wp_error( $event_response ) ) {
if ( WP_DEBUG ) {
echo 'Something went wrong!';
var_dump( $event_response );
}
return false;
}
$event = json_decode( $event_response['body'] );
// Single events only return first result.
if ( ! isset( $event->results ) || ! isset( $event->results[0] ) ) {
return false;
}
if ( isset( $args['event_id'] ) ) {
$event = $event->results[0];
} else {
$event = $event->results;
}
if ( $transient ) {
set_transient( $transient, $event, 2 * HOUR_IN_SECONDS );
}
}
return $event;
}
/**
* Get a single event, with a link to RSVP.
*
* @param string $id Event ID.
* @return string Event details formatted for display in widget
*/
public function get_single_event( $id ) {
global $event;
$options = get_option( 'vs_meet_options' );
$this->api_key = $options['vs_meetup_api_key'];
$out = '';
if ( ! empty( $this->api_key ) ) {
$args = array(
'event_id' => $id,
);
$event = $this->get_data( $args, 'vsm_single_event_' . $id );
if ( ! $event ) {
return;
}
ob_start();
$template = '';
if ( isset( $event->group ) && isset( $event->group->urlname ) ) {
$template = $event->group->urlname;
}
get_template_part( 'meetup-single', apply_filters( 'vsm_single_template', $template, $event ) );
$out = ob_get_contents();
if ( empty( $out ) ) {
// No theme template found, grab the template included in plugin.
$template = VSMEET_TEMPLATE_DIR . '/meetup-single.php';
if ( file_exists( $template ) ) {
load_template( $template, false );
}
$out = ob_get_contents();
}
ob_end_clean();
} else {
if ( is_user_logged_in() ) {
$out = sprintf(
'<p><a href="%1$s">%2$s</a></p>',
admin_url( 'options-general.php' ),
__( 'Please enter an API key', 'meetup-widgets' )
);
}
}
return $out;
}
/**
* Get the HTML for a group's events via Meetup API
*
* @param string $id Meetup ID or URL name.
* @param string $limit Number of events to display, default 5.
* @return string Event list formatted for display in widget
*/
public function get_group_events( $id, $limit = 5 ) {
global $events;
$options = get_option( 'vs_meet_options' );
$this->api_key = $options['vs_meetup_api_key'];
if ( ! empty( $this->api_key ) ) {
$args = array(
'status' => 'upcoming',
'page' => $limit,
);
if ( preg_match( '/[a-zA-Z]/', $id ) ) {
$args['group_urlname'] = $id;
} else {
$args['group_id'] = $id;
}
$events = $this->get_data( $args, 'vsm_group_events_' . $id . '_' . $limit );
if ( ! $events ) {
return;
}
ob_start();
get_template_part( 'meetup-list', 'group' );
$out = ob_get_contents();
if ( empty( $out ) ) {
// No theme template found, grab the template included in plugin.
$template = VSMEET_TEMPLATE_DIR . '/meetup-list.php';
if ( file_exists( $template ) ) {
load_template( $template, false );
}
$out = ob_get_contents();
}
ob_end_clean();
} else {
if ( is_user_logged_in() ) {
$out = sprintf(
'<p><a href="%1$s">%2$s</a></p>',
admin_url( 'options-general.php' ),
__( 'Please enter an API key', 'meetup-widgets' )
);
}
}
return $out;
}
/**
* Function name was changed in 2.1, leave this for backwards compatibilty.
*
* @deprecated 2.2.1 Use Meetup_Widget::get_group_events
* @param string $id Meetup ID or URL name.
* @param string $limit Number of events to display, default 5.
* @param string $deprecated Unused.
*/
function get_list_events( $id, $limit = 5, $deprecated = '' ) {
$this->get_group_events( $id, $limit );
}
/**
* Get user's list of events
*
* @param string $limit Number of events to display, default 5.
* @return string Event list formatted for display in widget
*/
public function get_user_events( $limit = 5 ) {
global $events;
$options = get_option( 'vs_meet_options' );
$this->api_key = $options['vs_meetup_api_key'];
if ( ! empty( $this->api_key ) ) {
$args = array(
'rsvp' => 'yes',
'page' => $limit,
);
$events = $this->get_data( $args, 'vsm_user_events_' . $limit );
if ( ! $events ) {
return;
}
ob_start();
get_template_part( 'meetup-list', 'group' );
$out = ob_get_contents();
if ( empty( $out ) ) {
// No theme template found, grab the template included in plugin.
$template = VSMEET_TEMPLATE_DIR . '/meetup-list.php';
if ( file_exists( $template ) ) {
load_template( $template, false );
}
$out = ob_get_contents();
}
ob_end_clean();
} else {
$out = sprintf(
'<p><a href="%1$s">%2$s</a></p>',
admin_url( 'options-general.php' ),
__( 'Please enter an API key', 'meetup-widgets' )
);
}
return $out;
}
}