-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcgit-wp-acf-events.php
More file actions
279 lines (237 loc) · 7.56 KB
/
cgit-wp-acf-events.php
File metadata and controls
279 lines (237 loc) · 7.56 KB
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
<?php
/*
Plugin Name: Castlegate IT WP ACF Events
Plugin URI: https://github.com/castlegateit/cgit-wp-acf-events/
Description: A simple and easy to use events interface with complete developer
control.
Version: 1.9.3
Author: Castlegate IT
Author URI: http://www.castlegateit.co.uk/
*/
/**
* Constants
*/
define('CGIT_EVENTS_POST_TYPE', 'event');
define('CGIT_EVENTS_POST_TYPE_CATEGORY', 'event-category');
/**
* Include
*/
include('admin-options.php');
include('post-type.php');
include('fields.php');
include('rewrite.php');
include('query.php');
include('activation.php');
include('calendar.php');
include('widget.php');
include('ajax.php');
include('save.php');
require_once __DIR__ . '/functions.php';
/**
* Activate/Uninstall hooks
*/
register_activation_hook(__FILE__, 'cgit_wp_events_activate');
register_uninstall_hook(__FILE__, 'cgit_wp_events_uninstall');
/**
* Retuns a calendar view of all events in the WordPress installation. The
* calendar is AJAX powered. Presentation can be customised through the admin
* settings page.
*
* @author Castlgate IT <info@castlegateit.co.uk>
* @author Andy Reading
*
* @param bool $full Show a full list of events for each day?
* @return string
*/
function cgit_wp_events_calendar(bool $full = false)
{
// Get the current year and month or set a default
$year = isset($_GET['cgit-year']) ? $_GET['cgit-year'] : date('Y');
$month = isset($_GET['cgit-month']) ? $_GET['cgit-month'] : date('m');
// Prevent infinite indexing of calendar pages by restricting display to
// fifteen year in the past/future
if ($year > date('Y') + 15 || $year < date('Y') - 15) {
$year = date('Y');
}
// New calendar instance
$events_calendar = new Cgit_event_calendar($year, $month);
$events_calendar->full = $full;
return $events_calendar->render();
}
/**
* Get the latest upcoming events. Automagically gets the meta values for start
* and end dates/time.
*
* @author Castlgate IT <info@castlegateit.co.uk>
* @author Andy Reading
*
* @return string
*/
function cgit_wp_events_latest($limit = 3)
{
$now = new DateTime('now');
// Get events that are not in the past, ordered by start date.
$args = array(
'post_type' => CGIT_EVENTS_POST_TYPE,
'post_status' => 'publish',
'meta_key' => 'start_date',
'orderby' => 'start_date',
'order' => 'ASC',
'posts_per_page' => $limit,
'meta_query' => array(
array(
'key' => 'end_date',
'value' => $now->format('Ymd'),
'type' => 'NUMERIC',
'compare' => '>='
)
)
);
$query = new WP_Query($args);
if ($query->posts) {
// For each post - get the meta data
foreach ($query->posts as $key => $event) {
// Start date
$query->posts[$key]->start_date = get_post_meta($event->ID, 'start_date', true);
// End date
$query->posts[$key]->end_date = get_post_meta($event->ID, 'end_date', true);
// Start time
$query->posts[$key]->start_time = get_post_meta($event->ID, 'start_time', true);
// End time
$query->posts[$key]->end_time = get_post_meta($event->ID, 'end_time', true);
// End time
$query->posts[$key]->price = (float)get_post_meta($event->ID, 'price', true);
}
return $query->posts;
}
return [];
}
/**
* Convert 12 hour time strings into 24 hour
*
* @param $hour12 12 hour time string
*
* @author Castlgate IT <info@castlegateit.co.uk>
* @author Andy Reading
*
* @return string
*/
function cgit_wp_events_to_24_hour_time($hour12)
{
return date("H:i", strtotime($hour12));
}
/**
* Returns a title for the events archive which displays the category name, or
* day, month, year, depending on the view.
*
* @param $title Main title
* @param $seperator Separator for the event title
* @param $year_format Format for the year view
* @param $month_format Format for the month view
* @param $day_format Format for the day view
*
* @author Castlgate IT <info@castlegateit.co.uk>
* @author Andy Reading
*
* @return string
*/
function cgit_wp_events_archive_title(
$title = 'Events',
$seperator = ' - ',
$year_format = 'Y',
$month_format = 'F Y',
$day_format = 'jS M Y'
) {
$return = $title . $seperator;
if (is_day()) {
$time = mktime(
0,
0,
0,
CGIT_WP_EVENTS_MONTH,
CGIT_WP_EVENTS_DAY,
CGIT_WP_EVENTS_YEAR
);
return $return . date($day_format, $time);
} elseif (is_month()) {
$time = mktime(
0,
0,
0,
CGIT_WP_EVENTS_MONTH,
1,
CGIT_WP_EVENTS_YEAR
);
return $return . date($month_format, $time);
} elseif (is_year()) {
$time = mktime(
0,
0,
0,
1,
1,
CGIT_WP_EVENTS_YEAR
);
return $return . date($year_format, $time);
} else {
return $title;
}
}
function cgit_wp_events_archive()
{
// Get events that are not in the past, ordered by start date.
$args = array(
'post_type' => CGIT_EVENTS_POST_TYPE,
'post_status' => 'publish',
'meta_key' => 'start_date',
'orderby' => 'start_date',
'order' => 'ASC',
'posts_per_page' => -1
);
$query = new WP_Query($args);
$archive = [];
if ($query->posts) {
// For each post - get the meta data
foreach ($query->posts as $key => $event) {
// Start month
$start = get_post_meta($event->ID, 'start_date', true);
$start = (new DateTime($start))->modify('first day of this month');
// End month (optional)
$end = get_post_meta($event->ID, 'end_date', true);
if (empty($end)) {
$end = $start;
} else {
$end = (new DateTime($end))->modify('last day of this month');
}
// Date range (used to)
$interval = DateInterval::createFromDateString('1 month');
$date_range = new DatePeriod($start, $interval, $end);
// DatePeriod extends \Traversable and if the start and end are
// within the same month, it will have a count of zero. Fix by
// making a simple array
if (iterator_count($date_range) == 0) {
$date_range = [$start];
}
foreach ($date_range as $date) {
// Add an index for this year, if it is new
if (!array_key_exists($date->format('Y'), $archive)) {
$archive[$date->format('Y')] = [];
}
// Check if this month exists in the year already
if (!in_array($date->format('m'), array_keys($archive[$date->format('Y')]))) {
// Add the month to the year index
$archive[$date->format('Y')][$date->format('m')] = [
'date' => (new DateTime())->setDate($date->format('Y'), $date->format('m'), 1),
'link' => '/' . CGIT_EVENTS_POST_TYPE . '/' . $date->format('Y') . '/' . $date->format('m') . '/',
'count' => 1
];
} else {
// Index already exists, increase the count.
$archive[$date->format('Y')][$date->format('m')]['count']++;
}
}
}
return $archive;
}
return array();
}