-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathics-shortcode.php
More file actions
77 lines (73 loc) · 2.14 KB
/
ics-shortcode.php
File metadata and controls
77 lines (73 loc) · 2.14 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
<?php
/**
* Import iCalendar events in your WordPress articles
*
* PHP Version 5.4
*
* @category Plugin
* @package ICSShortcode
* @author Pierre Rudloff <contact@rudloff.pro>
* @license GPL http://www.gnu.org/licenses/gpl.html
* @link https://github.com/StrasWeb/html5-simple-video-gallery
*/
/*
Plugin Name: iCalendar Shortcode
Plugin URI: https://github.com/TC-Alsace/wordpress-ics-shortcode
Description: Import iCalendar events in your WordPress articles
Author: Pierre Rudloff
Version: 0.1
Author URI: https://rudloff.pro/
*/
require_once 'ics-parser/class.iCalReader.php';
/**
* Display events
*
* @param array $atts Shortcode attributes
*
* @return void
* */
function ICSEvents($atts)
{
if (isset($atts['locale'])) {
setlocale(LC_TIME, $atts['locale']);
}
if (!isset($atts['nb'])) {
$atts['nb'] = 5;
}
$ical = new ical($atts['url']);
$events = $ical->sortEventsWithOrder($ical->events());
$now = time();
$eventsToDisplay = array();
foreach ($events as $event) {
if ($ical->iCalDateToUnixTimestamp(
$event['DTSTART']
) > $now && count($eventsToDisplay) < $atts['nb']) {
$eventsToDisplay[] = $event;
}
}
$html = '';
if (empty($eventsToDisplay)) {
if (isset($atts['noeventsmsg'])) {
$html .= $atts['noeventsmsg'];
}
} else {
foreach ($eventsToDisplay as $event) {
$timestamp = $ical->iCalDateToUnixTimestamp($event['DTSTART']);
$html .= '<ul class="next-date">
<li><time datetime="'.strftime('%F', $timestamp).'">
<span>'.strftime('%e', $timestamp).'</span>
<span>'.strftime('%b', $timestamp).'</span></time></li>
<li>'.$event['SUMMARY'].'</li>';
if (!empty($event['LOCATION'])) {
$html .= '<li>'.$event['LOCATION'].'</li>';
}
if (strlen($event['DTSTART']) > 8) {
$html .= '<li>'.strftime('%Hh%M', $timestamp).'</li>';
}
$html .= '</ul>';
}
}
return $html;
}
add_shortcode('ics_events', 'ICSEvents');
?>