forked from saleandro/songkick-wp-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
songkick_presentable_events.php
139 lines (124 loc) · 5.97 KB
/
songkick_presentable_events.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
<?php
require_once dirname(__FILE__) . '/songkick_presentable_event.php';
require_once dirname(__FILE__) . '/songkick_user_events.php';
require_once dirname(__FILE__) . '/songkick_artist_events.php';
require_once dirname(__FILE__) . '/songkick_metro_area_events.php';
require_once dirname(__FILE__) . '/songkick_venue_events.php';
class SongkickPresentableEvents {
function SongkickPresentableEvents($options) {
if (isset($options['username']) && $options['username']) { // legacy
$songkick_id = $options['username'];
$songkick_id_type = 'user';
} else {
$songkick_id = $options['songkick_id'];
$songkick_id_type = $options['songkick_id_type'];
}
$apikey = (isset($options['apikey'])) ? $options['apikey'] : null;
$attendance = (isset($options['attendance'])) ? $options['attendance'] : false;
$gigography = (isset($options['gigography']) && $options['gigography'] == 'true') ? true : false;
$order = (isset($options['order'])) ? $options['order'] : null;
$this->number_of_events = (isset($options['number_of_events']) && is_numeric($options['number_of_events'])) ? $options['number_of_events'] : 10;
if (!isset($options['show_pagination'])) $options['show_pagination'] = false;
$this->show_pagination = $options['show_pagination'];
if (!isset($options['page'])) $options['page'] = 1;
$this->page = intval($options['page']);
if (empty($songkick_id)) {
throw new Exception("Blank songkick id");
}
switch ($songkick_id_type) {
case 'user':
$this->songkick_events = new SongkickUserEvents($apikey, $songkick_id, $attendance, $gigography, $order);
break;
case 'artist':
$this->songkick_events = new SongkickArtistEvents($apikey, $songkick_id, $gigography, $order);
break;
case 'metro_area':
$this->songkick_events = new SongkickMetroAreaEvents($apikey, $songkick_id);
break;
case 'venue':
$this->songkick_events = new SongkickVenueEvents($apikey, $songkick_id);
break;
default:
throw new Exception("Unknown songkick id type: $songkick_id_type");
}
$results = $this->songkick_events->get_events($this->page, $this->number_of_events);
$this->events = $results['events'];
$this->total = $results['total'];
$this->date_color = isset($options['date_color']) ? $options['date_color'] : null;
$this->logo = isset($options['logo']) ? $options['logo'] : null;
$this->no_calendar_style = false;
if (isset($options['no_calendar_style'])) {
$this->no_calendar_style = $options['no_calendar_style'];
}
}
function to_html() {
$profile_title = __('See all concerts', SONGKICK_TEXT_DOMAIN);
$str = '';
if (empty($this->events)) {
$str .= '<p>'. htmlentities(__('No events...'), ENT_QUOTES, SONGKICK_I18N_ENCODING). '</p>';
} else {
$str .= '<ul class="songkick-events">';
foreach($this->events as $event) {
$presentable_event = new SongkickPresentableEvent($event);
$str .= '<li>'.$presentable_event->to_html($this->no_calendar_style, $this->date_color).'</li>';
}
$str .= '</ul>';
}
if ($this->show_pagination) {
$pages = ceil($this->total / $this->number_of_events);
if ($pages > 1) {
$min = max($this->page - 2, 2);
$max = min($this->page + 2, $pages-1);
$str .= '<div class="pagination">';
if (1 == $this->page)
$str .= "« ";
else {
$prev = $this->page-1;
$str .= "<a href=\"".$this->current_url("skp=$prev")."\" rel=\"prev\">«</a> ";
}
$str .= $this->page_to_html(1);
if ($min > 2) $str .= "… ";
for ($i=$min; $i<$max+1; $i++) {
$str .= $this->page_to_html($i);
}
if ($max < $pages-1) $str .= "… ";
$str .= $this->page_to_html($pages);
if ($pages == $this->page)
$str .= "» ";
else {
$next = $this->page+1;
$str .= "<a href=\"".$this->current_url("skp=$next")."\" rel=\"next\">»</a> ";
}
$str .= '</div>';
}
} else {
$str .= '<p class="profile-title"><a href="'.$this->songkick_events->profile_url().'">';
$str .= htmlentities($profile_title, ENT_QUOTES, SONGKICK_I18N_ENCODING)."</a></p>";
}
$str .= $this->powered_by_songkick($this->logo);
return $str;
}
function no_events() {
return empty($this->events);
}
private function current_url($query_string) {
global $wp;
$current_url = remove_query_arg('skp', add_query_arg($wp->query_string, '', home_url($wp->request)));
return add_query_arg($query_string, '', $current_url);
}
private function page_to_html($page) {
$str = '';
if ($page == $this->page)
$str .= "$page ";
else
$str .= "<a href=\"".$this->current_url("skp=$page")."\">$page</a> ";
return $str;
}
private function powered_by_songkick($logo) {
$text = __('Concerts by Songkick', SONGKICK_TEXT_DOMAIN);
$html = "<a class='powered-by' href='http://www.songkick.com/'>";
$html .= "<img src='".site_url('/wp-content/plugins/songkick-concerts-and-festivals/'.$logo)."' title='".htmlentities($text, ENT_QUOTES, SONGKICK_I18N_ENCODING)."' alt='".htmlentities($text, ENT_QUOTES, SONGKICK_I18N_ENCODING)."' /></a>";
return $html;
}
}
?>