This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmf_gig_calendar_widget.php
162 lines (132 loc) · 6.69 KB
/
mf_gig_calendar_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
<?php
/*
Copyright (C) 2012 Matthew Fries
MF Gig Calendar is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
MF Gig Calendar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class GigCalendarWidget extends WP_Widget {
function GigCalendarWidget() {
parent::WP_Widget( false, $name = 'MF Gig Calendar', array( 'description' => __('MF Gig Calendar Widget displays a list of upcoming events.', 'mfgigcal') ) );
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$events_to_display = apply_filters( 'events_to_display', $instance['events_to_display'] );
$display_when_empty = apply_filters( 'display_when_empty', $instance['display_when_empty'] );
$widget_date_format = apply_filters( 'widget_date_format', $instance['widget_date_format'] );
$calendar_link = apply_filters( 'calendar_link', $instance['calendar_link'] );
$link_text = apply_filters( 'link_text', $instance['link_text'] );
if ($widget_date_format == "") $widget_date_format = 'D j M';
global $wpdb;
$mfgigcal_table = $wpdb->prefix . "mfgigcal";
date_default_timezone_set(get_option('timezone_string'));
// get the dates
$today = date("Y-m-d");
$sql = "SELECT * FROM $mfgigcal_table WHERE end_date >= '$today' ORDER BY start_date ASC LIMIT " . $events_to_display;
$mfgigcal_events = $wpdb->get_results($sql);
// here are the events
if (!empty($mfgigcal_events) || $display_when_empty) {
$mfgigcal_settings = get_option('mfgigcal_settings');
echo $before_widget;
if ($title) {
echo $before_title . $title . $after_title;
}
if (!empty($mfgigcal_events)) {
echo "\n<ul id=\"mfgigcal-widget\">";
foreach ($mfgigcal_events as $mfgigcal_event) {
echo "\n<li>";
$startArray = explode("-", $mfgigcal_event->start_date);
$start_date = mktime(0,0,0,$startArray[1],$startArray[2],$startArray[0]);
$endArray = explode("-", $mfgigcal_event->end_date);
$end_date = mktime(0,0,0,$endArray[1],$endArray[2],$endArray[0]);
if ($start_date == $end_date) { // single date format
echo "<div class=\"date\">" . date_i18n($widget_date_format, $start_date) . " <span class=\"time\">$mfgigcal_event->time</span></div>";
}
else { // multi-day format
echo "<div class=\"date\">" . date_i18n($widget_date_format, $start_date) . "–" . date_i18n($widget_date_format, $end_date) . " <span class=\"time\">$mfgigcal_event->time</span></div>";
}
if ($mfgigcal_settings['calendar_url']) {
echo "\n<h4><a href=\"" . $mfgigcal_settings['calendar_url'] . "?event_id=" . $mfgigcal_event->id . "\">$mfgigcal_event->title</a></h4>";
}
else {
echo "\n<h4>$mfgigcal_event->title</h4>";
}
echo $mfgigcal_event->location;
echo "</li>";
}
echo "\n</ul>\n\n";
}
else if ($display_when_empty) {
echo "<p>" . $mfgigcal_settings['message'] . "</p>";
}
if ($calendar_link != "" && $mfgigcal_settings['calendar_url']) {
echo "<p><a href=\"" . $mfgigcal_settings['calendar_url'] . "\" class=\"calendar_url\">$link_text</a></p>";
}
echo $after_widget;
}
}
function update( $new_instance, $old_instance ) {
return $new_instance;
}
function form( $instance ) {
$title = esc_attr( $instance['title'] );
$events_to_display = esc_attr( $instance['events_to_display']);
$display_when_empty = esc_attr( $instance['display_when_empty']);
$widget_date_format = esc_attr( $instance['widget_date_format']);
$calendar_link = esc_attr( $instance['calendar_link']);
$link_text = esc_attr( $instance['link_text']);
if ($link_text == "") $link_text = __('View My Event Calendar', 'mfgigcal');
if ($widget_date_format == "") $widget_date_format = 'D j M';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Widget Title', 'mfgigcal'); ?>:
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</label>
</p>
<p>
<label><?php _e('How many events to display?', 'mfgigcal'); ?>
<input style="width:30px;" id="<?php echo $this->get_field_id( 'events_to_display' ); ?>" name="<?php echo $this->get_field_name( 'events_to_display' ); ?>" type="text" value="<?php echo $events_to_display; ?>" />
</label>
</p>
<p>
<label><input class="link_detail_switch" id="<?php echo $this->get_field_id( 'display_when_empty' ); ?>" name="<?php echo $this->get_field_name( 'display_when_empty' ); ?>" type="checkbox" value="1" <?php checked( '1', $display_when_empty ); ?> />
<?php _e('Display the widget even when there are no upcoming events.', 'mfgigcal'); ?>
</label>
</p>
<p>
<label><?php _e('Date Format:', 'mfgigcal'); ?>
<input style="width:70px;" id="<?php echo $this->get_field_id( 'widget_date_format' ); ?>" name="<?php echo $this->get_field_name( 'widget_date_format' ); ?>" type="text" value="<?php echo $widget_date_format; ?>" />
</label> <a href="http://codex.wordpress.org/Formatting_Date_and_Time" title="http://codex.wordpress.org/Formatting_Date_and_Time" target="_blank"><?php _e('Help'); ?>?</a>
</p>
<p>
<label><input class="link_detail_switch" id="<?php echo $this->get_field_id( 'calendar_link' ); ?>" name="<?php echo $this->get_field_name( 'calendar_link' ); ?>" type="checkbox" value="1" <?php checked( '1', $calendar_link ); ?> />
<?php _e('Add a link to the main event calendar (specified in MF Gig Calendar Settings).', 'mfgigcal'); ?>
</label>
</p>
<div class="link_details">
<p>
<label><?php _e('Link Text', 'mfgigcal'); ?>:
<input class="widefat" id="<?php echo $this->get_field_id( 'link_text' ); ?>" name="<?php echo $this->get_field_name( 'link_text' ); ?>" type="text" value="<?php echo $link_text; ?>" />
</label>
</p>
<p>
<?php _e('Note: If the "View My Calendar" link isn\'t displaying in your widget please make sure you have entered a URL for your calendar in the MF Gig Calendar Settings.', 'mfgigcal'); ?>
</p>
</div>
<?php
}
}
add_action( 'widgets_init', 'GigCalendarWidgetInit' );
function GigCalendarWidgetInit() {
register_widget( 'GigCalendarWidget' );
}
?>