-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheventcalendar3.php
1202 lines (528 loc) · 20 KB
/
eventcalendar3.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<?php
/*
Plugin Name: Event Calendar 3 for PHP 5.3
Plugin URI: http://wordpress.org/extend/plugins/event-calendar-3-for-php-53/
Description: Manage future events as an online calendar. Display upcoming events in a dynamic calendar, on a listings page, or as a list in the sidebar. You can subscribe to the calendar from iCal (OSX) or Sunbird. Change settings on the <a href="options-general.php?page=ec3_admin">Event Calendar Options</a> screen.
Version: 3.1.5
Author: Alex Tingle (PHP 5.3 version by Stig Ulfsby)
Author URI: http://blog.firetree.net/
Licence: GPL2
*/
/*
Copyright (c) 2005-2011, Alex Tingle.
This program 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.
This program 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.
*/
require_once(dirname(__FILE__).'/options.php');
require_once(dirname(__FILE__).'/date.php');
require_once(dirname(__FILE__).'/day.php');
require_once(dirname(__FILE__).'/template-functions.php');
require_once(dirname(__FILE__).'/admin.php');
require_once(dirname(__FILE__).'/tz.php');
$ec3_today_id=str_replace('_0','_',ec3_strftime("ec3_%Y_%m_%d"));
/** Read the schedule table for the posts, and add an ec3_schedule array
* to each post. */
function ec3_filter_the_posts($posts)
{
if('array'!=gettype($posts) || 0==count($posts))
return $posts;
$post_ids=array();
// Can't use foreach, because it gets *copies* (in PHP<5)
for($i=0; $i<count($posts); $i++)
{
$post_ids[]=intval($posts[$i]->ID);
$posts[$i]->ec3_schedule=array();
}
global $ec3,$wp_query,$wpdb;
$schedule=$wpdb->get_results(
"SELECT post_id,start,end,allday,rpt,IF(end>='$ec3->today',1,0) AS active
FROM $ec3->schedule
WHERE post_id IN (".implode(',',$post_ids).")
ORDER BY start"
);
// Flip $post_ids so that it maps post ID to position in the $posts array.
$post_ids=array_flip($post_ids);
if($post_ids && $schedule)
foreach($schedule as $s)
{
$i=$post_ids[$s->post_id];
$posts[$i]->ec3_schedule[]=$s;
}
return $posts;
}
function ec3_action_wp_head()
{
global $ec3,$month,$month_abbrev;
?>
<!-- Added by EventCalendar plugin. Version <?php echo $ec3->version; ?> -->
<script type='text/javascript' src='<?php echo $ec3->myfiles; ?>/xmlhttprequest.js'></script>
<script type='text/javascript' src='<?php echo $ec3->myfiles; ?>/ec3.js'></script>
<script type='text/javascript'><!--
ec3.start_of_week=<?php echo intval( get_option('start_of_week') ); ?>;
ec3.month_of_year=new Array('<?php echo implode("','",$month); ?>');
ec3.month_abbrev=new Array('<?php echo implode("','",$month_abbrev); ?>');
ec3.myfiles='<?php echo $ec3->myfiles; ?>';
ec3.home='<?php echo get_option('home'); ?>';
ec3.hide_logo=<?php echo $ec3->hide_logo; ?>;
ec3.viewpostsfor="<?php echo __('View posts for %1$s %2$s'); ?>";
// --></script>
<?php if(!$ec3->nocss): ?>
<style type='text/css' media='screen'>
@import url(<?php echo $ec3->myfiles; ?>/ec3.css);
.ec3_ec {
background-image:url(<?php echo $ec3->myfiles; ?>/ec.png) !IMPORTANT;
background-image:none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='<?php echo $ec3->myfiles; ?>/ec.png');
}
<?php if(!$ec3->disable_popups): ?>
#ec3_shadow0 {
background-image:url(<?php echo $ec3->myfiles; ?>/shadow0.png) !IMPORTANT;
background-image:none;
}
#ec3_shadow0 div {
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='<?php echo $ec3->myfiles; ?>/shadow0.png',sizingMethod='scale');
}
#ec3_shadow1 {
background-image:url(<?php echo $ec3->myfiles; ?>/shadow1.png) !IMPORTANT;
background-image:none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='<?php echo $ec3->myfiles; ?>/shadow1.png',sizingMethod='crop');
}
#ec3_shadow2 {
background-image:url(<?php echo $ec3->myfiles; ?>/shadow2.png) !IMPORTANT;
background-image:none;
}
#ec3_shadow2 div {
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='<?php echo $ec3->myfiles; ?>/shadow2.png',sizingMethod='scale');
}
<?php endif; ?>
</style>
<?php endif;
}
/** Turn OFF advanced mode when we're in the admin screens. */
function ec3_action_admin_head()
{
global $ec3;
$ec3->advanced=false;
}
/** Rewrite date restrictions if the query is day- or category- specific. */
function ec3_filter_posts_where($where)
{
global $ec3,$wp_query,$wpdb;
// To prevent breaking prior to WordPress v2.3
if(function_exists('get_the_tags') && $wp_query->is_tag)
return $where;
if($wp_query->is_page || $wp_query->is_single || $wp_query->is_admin)
return $where;
if($wp_query->is_date):
// Transfer events' 'post_date' restrictions to 'start'
$df='YEAR|MONTH|DAYOFMONTH|HOUR|MINUTE|SECOND|WEEK'; // date fields
$re="/ AND (($df)\($wpdb->posts\.post_date(,[^\)]+)?\) *= *('[^']+'|\d+\b))/i";
if(preg_match_all($re,$where,$matches)):
$where_post_date = implode(' AND ',$matches[1]);
$rdate=array(0,0,0);
$rtime=array(0,0,0);
for($i=0; $i<count($matches[1]); $i++)
{
if( 'YEAR'==$matches[2][$i]) $rdate[0]=$matches[4][$i];
elseif( 'MONTH'==$matches[2][$i]) $rdate[1]=$matches[4][$i];
elseif('DAYOFMONTH'==$matches[2][$i]) $rdate[2]=$matches[4][$i];
elseif( 'HOUR'==$matches[2][$i]) $rtime[0]=$matches[4][$i];
elseif( 'MINUTE'==$matches[2][$i]) $rtime[1]=$matches[4][$i];
elseif( 'SECOND'==$matches[2][$i]) $rtime[2]=$matches[4][$i];
}
// start either matches the date criteria,
// OR the rdate/rtime is between start..end:
$where_start=
sprintf("(%1\$s) OR (start<='%2\$s' AND end>='%2\$s')",
preg_replace("/\b$wpdb->posts\.post_date\b/",'start',$where_post_date),
str_replace( "'", '', implode('-',$rdate).' '.implode(':',$rtime) )
);
$where=preg_replace($re,'',$where);
if(is_category($ec3->event_category)):
$where.=" AND ($where_start) ";
$ec3->order_by_start=true;
else:
$where.=" AND (($where_post_date) OR ($where_start)) ";
endif;
$ec3->join_ec3_sch=true;
endif;
elseif($ec3->is_date_range):
$where_start=array();
if( !empty($ec3->range_from) )
$where_start[]="end>='$ec3->range_from'";
if( !empty($ec3->range_before) )
$where_start[]="start<='$ec3->range_before'";
if($where_start):
$where_start=implode(' AND ',$where_start);
$where.=" AND ($where_start AND ec3_sch.post_id IS NOT NULL) ";
$ec3->order_by_start=true;
$ec3->join_ec3_sch=true;
endif;
elseif($ec3->advanced):
if(is_category($ec3->event_category)):
// Hide inactive events
$where.=" AND ec3_sch.post_id IS NOT NULL ";
$ec3->join_ec3_sch=true;
$ec3->join_only_active_events=true;
$ec3->order_by_start=true;
global $wp;
$wp->did_permalink=false; // Allows zero results without -> 404
elseif($wp_query->is_search):
$where.=' AND (ec3_sch.post_id IS NULL OR '
."ec3_sch.end>='$ec3->today')";
$ec3->join_ec3_sch=true;
elseif(!$wp_query->is_category):
// Hide all events
$where.=" AND ec3_sch.post_id IS NULL ";
$ec3->join_ec3_sch=true;
endif;
endif;
return $where;
}
/** */
function ec3_filter_posts_join($join)
{
global $ec3,$wpdb;
// The necessary joins are decided upon in ec3_filter_posts_where().
if($ec3->join_ec3_sch || $ec3->order_by_start)
{
$join.=" LEFT JOIN $ec3->schedule ec3_sch ON ec3_sch.post_id=id ";
if($ec3->join_only_active_events)
$join.="AND ec3_sch.end>='$ec3->today' ";
}
return $join;
}
/** Change the order of event listings (only advanced mode). */
function ec3_filter_posts_orderby($orderby)
{
global $ec3, $wpdb;
if($ec3->order_by_start)
{
$regexp="/(?<!DATE_FORMAT[(])\b$wpdb->posts\.post_date\b( DESC\b| ASC\b)?/i";
if(preg_match($regexp,$orderby,$match))
{
if($match[1] && $match[1]==' DESC')
$orderby=preg_replace($regexp,'ec3_sch.start',$orderby);
else
$orderby=preg_replace($regexp,'ec3_sch.start DESC',$orderby);
}
else
{
// Someone's been playing around with the orderby - just overwrite it.
$orderby='ec3_sch.start';
}
}
return $orderby;
}
/** Eliminate double-listings for posts with >1 scheduled event. */
function ec3_filter_posts_groupby($groupby)
{
global $ec3,$wpdb;
if($ec3->join_ec3_sch || $ec3->order_by_start)
{
if(empty($groupby))
$groupby="{$wpdb->posts}.ID";
if($ec3->is_listing)
$groupby.=',ec3_sch.sched_id';
}
return $groupby;
}
/** Add a sched_id field, if we want a listing. */
function ec3_filter_posts_fields($fields)
{
global $ec3;
if($ec3->is_listing && ($ec3->join_ec3_sch || $ec3->order_by_start))
$fields.=',ec3_sch.sched_id';
return $fields;
}
function ec3_filter_query_vars($wpvarstoreset)
{
if(isset($_GET['ec3_xml']))
ec3_filter_query_vars_xml();
if(isset($_GET['ec3_ical']) || isset($_GET['ec3_vcal']))
ec3_filter_query_vars_ical();
if(isset($_GET['ec3_dump']))
ec3_filter_query_vars_dump();
// else...
$wpvarstoreset[]='ec3_today';
$wpvarstoreset[]='ec3_days';
$wpvarstoreset[]='ec3_from'; // ?? Deprecated
$wpvarstoreset[]='ec3_after';
$wpvarstoreset[]='ec3_before';
$wpvarstoreset[]='ec3_listing';
// Turn-off broken canonical redirection when both m= & cat= are set.
if(isset($_GET['m']) && isset($_GET['cat']))
remove_action('template_redirect','redirect_canonical');
return $wpvarstoreset;
}
/** If the parameter ec3_xml is set, then brutally hijack the page and replace
* it with XML calendar data. This is used by XmlHttpRequests from the
* active calendar JavaScript. */
function ec3_filter_query_vars_xml()
{
$components=explode('_',$_GET['ec3_xml']);
if(count($components)==2)
{
$date=new ec3_Date($components[0],$components[1]);
$end=$date->next_month();
$calendar_days=ec3_util_calendar_days($date->month_id(),$end->month_id());
@header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="'.get_option('blog_charset')
. '" standalone="yes"?>';
echo "<calendar><month id='".$date->month_id()."'>\n";
foreach($calendar_days as $day_id=>$day)
{
if('today'==$day_id)
$dc=explode('_', ec3_strftime(":_%Y_%m_%d") );
else
$dc=explode('_',$day_id);
if(count($dc)==4)
{
$date->day_num=$dc[3];
$titles=$day->get_titles();
echo "<day id='$day_id' is_event='$day->is_event'"
. " titles='$titles' link='" . $date->day_link() . "'/>\n";
}
}
echo "</month></calendar>\n";
exit(0);
}
}
/** If the parameter ec3_ical is set, then brutally hijack the page and replace
* it with iCalendar data.
* (Includes fixes contributed by Matthias Tarasiewicz & Marc Schumann.)*/
function ec3_filter_query_vars_ical($wpvarstoreset=NULL)
{
//
// Generate the iCalendar
$name=preg_replace('/([\\,;])/','\\\\$1',get_bloginfo_rss('name'));
$filename=preg_replace('/[^0-9a-zA-Z]/','',$name).'.ics';
header("Content-Type: text/calendar; charset=" . get_option('blog_charset'));
header("Content-Disposition: inline; filename=$filename");
header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
echo "BEGIN:VCALENDAR\r\n";
echo "VERSION:2.0\r\n";
echo "X-WR-CALNAME:$name\r\n";
global $ec3,$wpdb;
$calendar_entries = $wpdb->get_results(
"SELECT
post_id,
sched_id,
post_title,
post_excerpt,
DATE_FORMAT(start,IF(allday,'%Y%m%d','%Y-%m-%d %H:%i')) AS dt_start,
IF( allday,
DATE_FORMAT(DATE_ADD(end, INTERVAL 1 DAY),'%Y%m%d'),
DATE_FORMAT(end,'%Y-%m-%d %H:%i')
) AS dt_end,
$ec3->wp_user_nicename AS user_nicename,
IF(allday,'TRANSPARENT','OPAQUE') AS transp,
allday
FROM $wpdb->posts p
LEFT JOIN $wpdb->users u ON p.post_author=u.ID
INNER JOIN $ec3->schedule s ON p.id=s.post_id
WHERE post_status='publish'
ORDER BY start"
);
if($calendar_entries)
foreach($calendar_entries as $entry)
{
// ?? Should add line folding at 75 octets at some time as per RFC 2445.
$summary=preg_replace('/([\\,;])/','\\\\$1',$entry->post_title);
$permalink=get_permalink($entry->post_id);
echo "BEGIN:VEVENT\r\n";
echo "SUMMARY:$summary\r\n";
echo "URL;VALUE=URI:$permalink\r\n";
echo "UID:$entry->sched_id-$permalink\r\n";
$description='';
if(strlen($entry->post_excerpt)>0)
{
// I can't get iCal to understand iCalendar encoding.
// So just strip out newlines here:
$description=preg_replace('/[ \r\n]+/',' ',$entry->post_excerpt.' ');
$description=preg_replace('/([\\,;])/','\\\\$1',$description);
}
$description.='['.sprintf(__('by: %s'),$entry->user_nicename).']';
echo "DESCRIPTION:$description\r\n";
echo "TRANSP:$entry->transp\r\n"; // for availability.
if($entry->allday)
{
echo "DTSTART;VALUE=DATE:$entry->dt_start\r\n";
echo "DTEND;VALUE=DATE:$entry->dt_end\r\n";
}
else
{
// Convert timestamps to UTC
echo sprintf("DTSTART;VALUE=DATE-TIME:%s\r\n",ec3_to_utc($entry->dt_start));
echo sprintf("DTEND;VALUE=DATE-TIME:%s\r\n",ec3_to_utc($entry->dt_end));
}
echo "END:VEVENT\r\n";
}
echo "END:VCALENDAR\r\n";
exit(0);
}
/** Test function. Helps to diagnose problems.
* The output from this feature has been chosen to NOT reveal any private
* information, yet be of real use for debugging.
*/
function ec3_filter_query_vars_dump($wpvarstoreset=NULL)
{
global $ec3, $wpdb;
echo "<pre>\n";
echo "POSTS:\n";
print_r( $wpdb->get_results(
"SELECT ID,post_date,post_date_gmt,post_status,post_name,post_modified,
post_modified_gmt,post_type
FROM $wpdb->posts ORDER BY ID"
));
if($ec3->wp_have_categories)
{
echo "POST2CAT:\n";
print_r($wpdb->get_results("SELECT * FROM $wpdb->post2cat ORDER BY post_id"));
}
echo "EC3_SCHEDULE:\n";
print_r($wpdb->get_results("SELECT * FROM $ec3->schedule ORDER BY post_id"));
echo "EC3 OPTIONS:\n";
print_r($wpdb->get_results(
"SELECT option_name,option_value
FROM $wpdb->options WHERE option_name LIKE 'ec3_%'"
));
echo "ACTIVE PLUGINS:\n";
print_r( $wpdb->get_var(
"SELECT option_value
FROM $wpdb->options WHERE option_name='active_plugins'"
));
echo "</pre>\n";
exit(0);
}
/** Add support for new query vars:
*
* - ec3_today : sets date to today.
* - ec3_days=N : Finds events for the next N days.
* - ec3_after=YYYY-MM-DD : limits search to events on or after YYYY-MM-DD.
* - ec3_before=YYYY-MM-DD : limits search to events on or before YYYY-MM-DD.
*/
function ec3_filter_parse_query($wp_query)
{
global $ec3;
// query_posts() can be called multiple times. So reset all our variables.
$ec3->reset_query();
// Deal with EC3-specific parameters.
if( !empty($wp_query->query_vars['ec3_listing']) )
{
$ec3->is_listing=true;
}
if( !empty($wp_query->query_vars['ec3_today']) )
{
// Force the value of 'm' to today's date.
$wp_query->query_vars['m']=ec3_strftime('%Y%m%d');
$wp_query->is_date=true;
$wp_query->is_day=true;
$wp_query->is_month=true;
$wp_query->is_year=true;
$ec3->is_today=true;
return;
}
if( !empty($wp_query->query_vars['ec3_days']) )
{
// Show the next N days.
$ec3->days=intval($wp_query->query_vars['ec3_days']);
$secs=$ec3->days*24*3600;
$wp_query->query_vars['ec3_after' ]=ec3_strftime('%Y_%m_%d');
$wp_query->query_vars['ec3_before']=ec3_strftime('%Y_%m_%d',time()+$secs);
}
// Get values (if any) for after ($a) & before ($b).
if( !empty($wp_query->query_vars['ec3_after']) )
$a=$wp_query->query_vars['ec3_after'];
else if( !empty($wp_query->query_vars['ec3_from']) )
$a=$wp_query->query_vars['ec3_from'];
$b=$wp_query->query_vars['ec3_before'];
if( $a=='today' )
$a=ec3_strftime('%Y-%m-%d');
if( $b=='today' )
$b=ec3_strftime('%Y-%m-%d');
$re='/\d\d\d\d[-_]\d?\d[-_]\d?\d/';
if( !empty($a) && preg_match($re,$a) ||
!empty($b) && preg_match($re,$b) )
{
// Kill any other date parameters.
foreach(array('m','second','minute','hour','day','monthnum','year','w')
as $param)
{
unset($wp_query->query_vars[$param]);
}
$wp_query->is_date=false;
$wp_query->is_time=false;
$wp_query->is_day=false;
$wp_query->is_month=false;
$wp_query->is_year=false;
$ec3->is_date_range=true;
$ec3->range_from =$a;
$ec3->range_before=$b;
}
}
function ec3_filter_the_content($post_content)
{
return ec3_get_schedule() . $post_content;
}
/** Replaces default wp_trim_excerpt filter. Fakes an excerpt if needed.
* Adds a textual summary of the schedule to the excerpt.*/
function ec3_get_the_excerpt($text)
{
global $post;
if(empty($text))
{
$text=$post->post_content;
if(!$post->ec3_schedule)
$text=apply_filters('the_content', $text);
$text=str_replace(']]>', ']]>', $text);
$text=strip_tags($text);
$excerpt_length=55;
$words=explode(' ', $text, $excerpt_length + 1);
if(count($words) > $excerpt_length)
{
array_pop($words);
array_push($words, '[...]');
$text=implode(' ', $words);
}
}
if($post->ec3_schedule)
{
$schedule=ec3_get_schedule('%s; ',"%1\$s %3\$s %2\$s. ",'[ %s] ');
$text=$schedule.$text;
}
return $text;
}
//
// Hook in...
if($ec3->event_category)
{
add_action('wp_head', 'ec3_action_wp_head');
add_action('admin_head', 'ec3_action_admin_head');
add_filter('query_vars', 'ec3_filter_query_vars');
add_filter('parse_query', 'ec3_filter_parse_query');
add_filter('posts_where', 'ec3_filter_posts_where',11);
add_filter('posts_join', 'ec3_filter_posts_join');
add_filter('posts_groupby','ec3_filter_posts_groupby');
add_filter('posts_fields', 'ec3_filter_posts_fields');
add_filter('the_posts', 'ec3_filter_the_posts');
if(!$ec3->hide_event_box)
add_filter('the_content','ec3_filter_the_content',20);
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'ec3_get_the_excerpt');
if($ec3->advanced)
add_filter('posts_orderby','ec3_filter_posts_orderby',11);
}
?>