-
Notifications
You must be signed in to change notification settings - Fork 76
/
event-organiser-templates.php
333 lines (285 loc) · 10.9 KB
/
event-organiser-templates.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
<?php
/**
* Template functions
*
* @package template-functions
*/
/**
* Load a template part into a template
*
* Identical to {@see `get_template_part()`} except that it uses {@see `eo_locate_template()`}
* instead of {@see `locate_template()`}.
*
* Makes it easy for a theme to reuse sections of code in a easy to overload way
* for child themes. Looks for and includes templates {$slug}-{$name}.php
*
* You may include the same template part multiple times.
*
* @uses eo_locate_template()
* @since 1.7
* @uses do_action() Calls `get_template_part_{$slug}` action.
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialised template.
*/
function eo_get_template_part( $slug, $name = null ) {
/**
* @ignore
*/
do_action( "get_template_part_{$slug}", $slug, $name );
$templates = array();
if ( isset( $name ) ) {
$templates[] = "{$slug}-{$name}.php";
}
$templates[] = "{$slug}.php";
eo_locate_template( $templates, true, false );
}
/**
* Retrieve the name of the highest priority template file that exists.
*
* Searches the child theme first, then the parent theme before checking the plug-in templates folder.
* So parent themes can override the default plug-in templates, and child themes can over-ride both.
*
* Behaves almost identically to `{@see locate_template()}`
*
* @since 1.7
*
* @param string|array $template_names Template file(s) to search for, in order.
* @param bool $load If true the template file will be loaded if it is found.
* @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
* @return string The template filename if one is located.
*/
function eo_locate_template( $template_names, $load = false, $require_once = true ) {
$located = '';
$template_dir = get_stylesheet_directory(); //child theme
$parent_template_dir = get_template_directory(); //parent theme
$stack = array( $template_dir, $parent_template_dir, EVENT_ORGANISER_DIR . 'templates' );
/**
* Filters the template stack: an array of directories the plug-in looks for
* for templates.
*
* The directories are checked in the order in which they appear in this array.
* By default the array includes (in order)
*
* - child theme directory
* - parent theme directory
* - `event-organiser/templates`
*
* @param array $stack Array of directories (absolute path).
*/
$stack = apply_filters( 'eventorganiser_template_stack', $stack );
$stack = array_unique( $stack );
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
foreach ( $stack as $template_stack ) {
if ( file_exists( trailingslashit( $template_stack ) . $template_name ) ) {
$located = trailingslashit( $template_stack ) . $template_name;
break 2;
}
}
}
if ( $load && '' != $located ) {
load_template( $located, $require_once );
}
return $located;
}
/**
* A wrapper for {@see wp_enqueue_style()}. Filters the stylesheet url so themes can
* replace a stylesheet with their own.
*
* @uses wp_register_style()
* @since 3.0.0
*
* @param string $handle Name of the stylesheet.
* @param string|bool $src Path to the stylesheet from the WordPress root directory. Example: '/css/mystyle.css'.
* @param array $deps An array of registered style handles this stylesheet depends on. Default empty array.
* @param string|bool $ver String specifying the stylesheet version number. Used to ensure that the correct version
* is sent to the client regardless of caching. Default 'false'. Accepts 'false', 'null', or 'string'.
* @param string $media Optional. The media for which this stylesheet has been defined.
* Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print',
* 'screen', 'tty', or 'tv'.
*/
function eo_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
$src = apply_filters( 'eventorganiser_stylesheet_src', $src, $handle );
$src = apply_filters( 'eventorganiser_stylesheet_src_' . $handle, $src );
return wp_register_style( $handle, $src, $deps, $ver, $media );
}
/**
* Enqueues a stylesheet
* Respects the 'disable css' setting. It calls the `eventorganiser_stylesheet_{handle}`
* hook so that the handle can be mapped to another registered stylesheet
* @since 3.0.0
* @param string $handle
*/
function eo_enqueue_style( $handle ) {
if ( eventorganiser_get_option( 'disable_css' ) || get_theme_support( 'event-organiser' ) ) {
return;
}
/**
* Filters the handle for a stylesheet
*
* This can be used to swap a stylesheet for another one. While you can deregister a handle
* and re-register it with a different source, this also allow multiple handles to be mapped
* to one 'common' styelsheet. This is useful for themes that wish to amalgamate a stylesheet
* into it's style.css.
*
* **This is currently only used with front-end stylsheets**
*
* @param string $handle The stylesheet handle
*/
$handle = apply_filters( 'eventorganiser_stylesheet_handle_' . $handle, $handle );
wp_enqueue_style( $handle );
}
/**
* Whether an event archive is being viewed
*
* By specifying the type of archive ( e.g. 'day', 'month' or 'year'), we can check if its
* a day, month or year archive. By default, it will just return `is_post_type_archive('event')`
*
* You can get the date of the archive via {@see `eo_get_event_archive_date()`}
*
*@uses is_post_type_archive()
*@since 1.7
*@param string $type The type archive. 'day', 'month', or 'year'
*@return bool Whether an event archive is being viewed, where type is specified, if its an event archive of that type.
*/
function eo_is_event_archive( $type = false ) {
if ( ! is_post_type_archive( 'event' ) ) {
return false;
}
$ondate = str_replace( '/', '-', trim( get_query_var( 'ondate' ) ) );
switch ( $type ) {
case 'year':
return ( preg_match( '/\d{4}$/', $ondate ) && eo_check_datetime( 'Y-m-d', $ondate . '-01-01' ) );
break;
case 'month':
return ( preg_match( '/^\d{4}-\d{2}$/', $ondate ) && eo_check_datetime( 'Y-m-d', $ondate . '-01' ) );
break;
case 'day':
return ( preg_match( '/^\d{4}-\d{2}-\d{2}$/', $ondate ) && eo_check_datetime( 'Y-m-d', $ondate ) );
break;
default:
return true;
}
}
/**
* Returns formatted date of an event archive.
*
* Returns the formatted date of an event archive. E.g. for date archives, returns that date,
* for year archives returns 1st January of that year, for month archives 1st of that month.
* The date is formatted according to `$format` via {@see `eo_format_datetime()`}
*
* <code>
* <?php
* if( eo_is_event_archive('day') )
* //Viewing date archive: "Events: 3rd June 2013"
* echo __('Events: ','eventorganiser').' '.eo_get_event_archive_date('jS F Y');
* elseif( eo_is_event_archive('month') )
* //Viewing month archive: "Events: June 2013"
* echo __('Events: ','eventorganiser').' '.eo_get_event_archive_date('F Y');
* elseif( eo_is_event_archive('year') )
* //Viewing year archive: "Events: 2013"
* echo __('Events: ','eventorganiser').' '.eo_get_event_archive_date('Y');
* else
* _e('Events','eventorganiser');
* ?>
* </code>
* @since 1.7
* @uses is_post_type_archive()
* @uses eo_format_datetime()
* @link https://php.net/manual/en/function.date.php Formatting dates
* @param string|constant $format How to format the date, see https://php.net/manual/en/function.date.php or DATETIMEOBJ constant to return the datetime object.
* @return string|dateTime The formatted date
*/
function eo_get_event_archive_date( $format = DATETIMEOBJ ) {
if ( ! is_post_type_archive( 'event' ) ) {
return false;
}
$ondate = str_replace( '/', '-', get_query_var( 'ondate' ) );
if ( empty( $ondate ) ) {
return false;
}
$parts = count( explode( '-', $ondate ) );
if ( 1 == $parts && is_numeric( $ondate ) ) {
//Numeric - interpret as year
$ondate .= '-01-01';
} elseif ( 2 == $parts ) {
// 2012-01 format: interpret as month
$ondate .= '-01';
}
$ondate = eo_check_datetime( 'Y-m-d', $ondate );
return eo_format_datetime( $ondate, $format );
}
/**
* Checks if provided template path points to an 'event' template recognised by EO, given the context.
* This will one day ignore context, and if only the event archive template is present in theme folder
* it will use that regardless. If no event-archive tempate is present the plug-in will pick the most appropriate
* option, first from the theme/child-theme directory then the plugin.
*
* @ignore
* @since 1.3.1
*
* @param string $templatePath absolute path to template or filename (with .php extension)
* @param string $context What the template is for ('event','archive-event','event-venue', etc).
* @return (true|false) return true if template is recognised as an 'event' template. False otherwise.
*/
function eventorganiser_is_event_template( $templatepath, $context = '' ) {
$template = basename( $templatepath );
switch ( $context ) {
case 'event';
return 'single-event.php' == $template;
case 'archive':
return 'archive-event.php' == $template;
case 'event-venue':
return ( (1 == preg_match( '/^taxonomy-event-venue((-(\S*))?).php/', $template ) || 'venues-template.php' == $template ) );
case 'event-category':
return (1 == preg_match( '/^taxonomy-event-category((-(\S*))?).php/', $template ));
case 'event-tag':
return (1 == preg_match( '/^taxonomy-event-tag((-(\S*))?).php/', $template ));
}
return false;
}
function _eventorganiser_single_event_content( $content ) {
//Sanity check!
if ( ! is_singular( 'event' ) ) {
return $content;
}
//Check we are an event!
if ( get_post_type( get_the_ID() ) !== 'event' ) {
return $content;
}
/*
* This was introduced to fix an obscure bug with including pages
* in another page via shortcodes.
* But it breaks yoast SEO.
global $eo_event_parsed;
if( !empty( $eo_event_parsed[get_the_ID()] ) ){
return $content;
}else{
$eo_event_parsed[get_the_ID()] = 1;
}*/
//Object buffering
ob_start();
eo_get_template_part( 'event-meta','event-single' );
//include(EVENT_ORGANISER_DIR.'templates/event-meta-event-single.php');
$event_details = ob_get_contents();
ob_end_clean();
/**
* Filters the event details automatically appended to the event's content
* when single-event.php is not present in the theme.
*
* If template handling is enabled and the theme does not have `single-event.php`
* template, Event Organiser uses `the_content` filter to add prepend the content
* with event details. This filter allows you to change the prepended details.
*
* Unless you have a good reason, it's strongly recommended to change the templates
* rather than use this filter.
*
* @param string $event_details The event details to be added.
* @param string $content The original event content
*/
$event_details = apply_filters( 'eventorganiser_pre_event_content', $event_details, $content );
return $event_details . $content;
}