-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions-eventbrite.php
executable file
·198 lines (150 loc) · 3.74 KB
/
functions-eventbrite.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
<?php
/**
* Eventbrite API
*
*/
class Simple_Eventbrite_List {
/**
* @param $organiser
* @param $category
* @param $token
*
* @return string
*/
public function url( $organiser, $category, $token ) {
$url = 'https://www.eventbriteapi.com/v3/events/search/?sort_by=date&organizer.id=' . $organiser . $category . '&token=' . $token . '&expand=ticket_classes';
return $url;
}
/**
* @param $result
*
* @return bool
*/
public function check_result( $result ) {
if ( is_wp_error( $result ) ) {
$result = false;
} elseif ( wp_remote_retrieve_response_code( $result ) == '404' ) {
$result = false;
} else {
$result = true;
}
return $result;
}
/**
* @param $url
*
* @return null
*/
public function get_json( $url ) {
if ( ! class_exists( 'WP_Http' ) ) {
include_once( ABSPATH . WPINC . '/class-http.php' );
}
$request = new WP_Http;
$result = $request->request( $url );
if ( $this->check_result( $result ) ) {
$json = $result['body'];
} else {
$json = null;
}
return $json;
}
/**
* @param $status
*
* @return string
*/
public function event_status( $status ) {
$tickets = '';
if ( $status ) {
$count = count( $status );
for ( $tc = 0; $tc < $count; $tc ++ ) {
if ( $status[ $tc ]->on_sale_status == 'AVAILABLE' ) {
$tickets = ( $status[ $tc ]->free ) ? 'FREE' : 'PAID';
break;
} else {
$tickets = 'FULLY BOOKED';
}
}
}
return $tickets;
}
/**
* @param $online
*
* @return string
*/
public function event_online( $online ) {
$html = '';
if ( $online == true ) {
$html = '<div class="online-event"><span>Online event</span></div>';
}
return $html;
}
/**
* @param $online
* @param $url
* @param $image
* @param $title
*
* @return string
*/
public function event_image( $online, $url, $image, $title ) {
$html = '<div class="event-img">%s<a href="%s" target="_blank"><img src="%s" alt="%s"></a></div>';
return sprintf( $html, $online, $url, $image, $title );
}
/**
* @param $date
* @param $url
* @param $title
* @param $tickets
*
* @return string
*/
public function event_text( $date, $url, $title, $tickets ) {
$html = '<div class="event-text"><p>%s</p><h4><a href="%s" target="_blank">%s</a></h4><p class="event-status">%s</p></div>';
return sprintf( $html, $date, $url, $title, $tickets );
}
/**
* @param $organiser
* @param $category
* @param $token
* @param $number
*
* @return string
*/
public function display( $organiser, $category, $token, $number ) {
$url = $this->url( $organiser, $category, $token );
$json = $this->get_json( $url );
$html = '<div id="sebl_events"><ul class="event-list">';
if ( $json ) {
$obj = json_decode( $json );
if ( isset($obj->error_description) ) {
$html .= '<h2>API error ' . $obj->status_code . '</h2>';
$html .= '<p>' . $obj->error_description . '</p>';
} else {
$count = count( $obj->events );
if ( $number > $count ) {
$number = $count - 1;
} else {
$number = $number - 1;
}
for ( $i = 0; $i <= $number; $i ++ ) {
$url = $obj->events[ $i ]->url;
$title = $obj->events[ $i ]->name->text;
$image = $obj->events[ $i ]->logo->url;
$date = date( 'l j F Y, H:i', strtotime( $obj->events[ $i ]->start->local ) );
$tickets = $this->event_status( $obj->events[ $i ]->ticket_classes );
$online = $this->event_online( $obj->events[ $i ]->online_event );
$html .= '<li>';
$html .= $this->event_image( $online, $url, $image, $title );
$html .= $this->event_text( $date, $url, $title, $tickets );
$html .= '</li>';
}
}
} else {
$html .= '<h2>No events found</h2>';
}
$html .= '</ul></div>';
return $html;
}
}