my-calendar-search.php
12.3 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
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
<?php
/**
* Calendar search.
*
* @category Search
* @package My Calendar
* @author Joe Dolson
* @license GPLv2 or later
* @link https://www.joedolson.com/my-calendar/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Output search results for a given query
*
* @param string|array $query Search query.
*
* @return string HTML output
*/
function mc_search_results( $query ) {
$skip = 0;
/**
* Number of past results to show. Default `0`.
*
* @hook mc_past_search_results
*
* @param {int} $before Number of past results to display.
* @param {string} $context 'basic' for basic search results.
*
* @return {string}
*/
$before = apply_filters( 'mc_past_search_results', 0, 'basic' );
/**
* Number of future results to show. Default `20`.
*
* @hook mc_future_search_results
*
* @param {int} $after Number of future results to display.
* @param {string} $context 'basic' for basic search results.
*
* @return {string}
*/
$after = apply_filters( 'mc_future_search_results', 20, 'basic' ); // Return only future events, nearest 20.
$exports = '';
if ( is_string( $query ) ) {
$search = mc_prepare_search_query( $query );
$term = $query;
} else {
/**
* Build the advanced search query. Default ''
*
* @hook mc_advanced_search
*
* @param {string} $search Placeholder to create search results.
* @param {array|string} $query User search query parameters.
*
* @return {array}
*/
$search = apply_filters( 'mc_advanced_search', '', $query );
$term = $query['mcs'];
/**
* Number of results to skip. Default `0`. Used for pagination.
*
* @hook mc_skip_search_results
*
* @param {int} $after Number of results to skip.
* @param {string} $context 'advanced' for advanced search results.
*
* @return {string}
*/
$skip = apply_filters( 'mc_skip_search_results', 0, 'advanced' );
/**
* Number of past results to show. Default `0`.
*
* @hook mc_past_search_results
*
* @param {int} $before Number of past results to display.
* @param {string} $context 'advanced' for advanced search results.
*
* @return {string}
*/
$before = apply_filters( 'mc_past_search_results', 0, 'advanced' );
/**
* Number of future results to show. Default `20`.
*
* @hook mc_future_search_results
*
* @param {int} $after Number of future results to display.
* @param {string} $context 'advanced' for advanced search results.
*
* @return {string}
*/
$after = apply_filters( 'mc_future_search_results', 20, 'advanced' );
}
$event_array = mc_get_search_results( $search );
$count = count( $event_array );
if ( ! empty( $event_array ) ) {
$template = '<h3><strong>{timerange after=", "}{daterange}</strong> – {linking_title}</h3><div class="mcs-search-excerpt">{search_excerpt}</div>';
/**
* Template for outputting search results. Default `<strong>{date}</strong> {title} {details}`.
*
* @hook mc_search_template
*
* @param {string} $template String with HTML and template tags.
* @param {string|array} $term The search query arguments. Can be a string or an array of search parameters.
*
* @return {string}
*/
$template = apply_filters( 'mc_search_template', $template, $term );
// No filters parameter prevents infinite looping on the_content filters.
if ( is_string( $query ) ) {
$output = mc_produce_upcoming_events( $event_array, $template, 'list', 'ASC', $skip, $before, $after, 'yes', 'yes', 'nofilters', $term );
} else {
// Use this function for advanced search queries.
$output = mc_produce_search_results( $event_array, $template );
}
/**
* Filter that inserts search export links. Default empty string.
*
* @hook mc_search_exportlinks
*
* @param {string} $exports String.
* @param {string} $output Search results.
*
* @return {string}
*/
$exports = apply_filters( 'mc_search_exportlinks', '', $output );
} else {
/**
* HTML template if no search results. Default `<li class='no-results'>" . __( 'Sorry, your search produced no results.', 'my-calendar' ) . '</li>`.
*
* @hook mc_search_no_results
*
* @param {string} $output HTML output.
* @param {string|array} $term The search query arguments. Can be a string or an array of search parameters.
*
* @return {string}
*/
$output = apply_filters( 'mc_search_no_results', "<li class='no-results'>" . __( 'Sorry, your search produced no results.', 'my-calendar' ) . '</li>', $term );
}
/**
* HTML template before the search results. Default `<ol class="mc-search-results">`.
*
* @hook mc_search_before
*
* @param {string} $header HTML output.
* @param {string|array} $term The search query arguments. Can be a string or an array of search parameters.
*
* @return {string}
*/
$header = apply_filters( 'mc_search_before', '<h2>%s</h2><ol class="mc-search-results" role="list">', $term, $count );
// Translators: search term.
$header = sprintf( $header, sprintf( __( 'Search Results for "%s"', 'my-calendar' ), esc_html( $term ) ) );
/**
* HTML template after the search results. Default `</ol>`.
*
* @hook mc_search_after
*
* @param {string} $footer HTML output.
* @param {string|array} $term The search query arguments. Can be a string or an array of search parameters.
*
* @return {string}
*/
$footer = apply_filters( 'mc_search_after', '</ol>', $term );
return $header . $output . $footer . $exports;
}
add_filter( 'the_title', 'mc_search_results_title', 10, 2 );
/**
* Custom title for search results screen.
*
* @param string $title Current title.
* @param int $id post ID.
*
* @return string New title
*/
function mc_search_results_title( $title, $id = null ) {
if ( ( isset( $_GET['mcs'] ) || isset( $_POST['mcs'] ) ) && ( is_page( $id ) || is_single( $id ) ) && in_the_loop() ) {
$query = ( isset( $_GET['mcs'] ) ) ? $_GET['mcs'] : $_POST['mcs'];
// Translators: entered search query.
$title = sprintf( __( 'Events Search for “%s”', 'my-calendar' ), esc_html( $query ) );
}
return $title;
}
add_filter( 'the_content', 'mc_show_search_results' );
/**
* Show search results on predefined search page.
*
* @param string $content Post Content.
*
* @return string $content
*/
function mc_show_search_results( $content ) {
global $post;
if ( is_object( $post ) && in_the_loop() && ! is_page( mc_get_option( 'uri_id' ) ) ) {
// if this is the result of a search, show search output.
$ret = false;
$query = false;
if ( isset( $_GET['mcs'] ) && ! isset( $_GET['mcp'] ) ) { // Simple search.
$ret = true;
$query = sanitize_text_field( $_GET['mcs'] );
} elseif ( isset( $_GET['mcp'] ) && isset( $_GET['mcs'] ) ) { // Advanced search.
$ret = true;
$query = map_deep( $_GET, 'sanitize_text_field' );
}
if ( $ret && $query ) {
return mc_search_results( $query );
} else {
return $content;
}
} else {
return $content;
}
}
add_filter( 'mc_search_exportlinks', 'mc_search_exportlinks', 10, 0 );
/**
* Creates the export links for search result
*/
function mc_search_exportlinks() {
if ( ! session_id() ) {
return;
}
// Setup print link.
$print_add = array(
'format' => 'list',
'searched' => true,
'href' => urlencode( mc_get_current_url() ),
'cid' => 'mc-print-view',
);
$subtract = array( 'time', 'ltype', 'lvalue', 'mcat', 'yr', 'month', 'dy' );
$mc_print_url = mc_build_url( $print_add, '', home_url() );
$print = "<div class='mc-print'><a href='$mc_print_url'>" . __( 'Print<span class="maybe-hide"> View</span>', 'my-calendar' ) . '</a></div>';
$above = array();
$below = array();
// Set up exports.
if ( '' !== mc_get_option( 'topnav', '' ) ) {
$above = array_map( 'trim', explode( ',', mc_get_option( 'topnav' ) ) );
}
if ( '' !== mc_get_option( 'bottomnav', '' ) ) {
$below = array_map( 'trim', explode( ',', mc_get_option( 'bottomnav' ) ) );
}
$used = array_merge( $above, $below );
if ( in_array( 'exports', $used, true ) ) {
$ics_add = array( 'searched' => true );
$exports = mc_export_links( 1, 1, 1, $ics_add, $subtract );
} else {
$exports = '';
}
$before = "<div class='mc_bottomnav my-calendar-footer'>";
$after = '</div>';
return $before . $exports . $print . $after;
}
add_filter( 'mc_searched_events', 'mc_searched_events', 10, 1 );
/**
* Saves all searched events in $_SESSION
*
* @param array $event_array The events to be saved.
*
* @return array Events.
*/
function mc_searched_events( $event_array ) {
if ( session_id() ) {
$_SESSION['MC_SEARCH_RESULT'] = json_encode( $event_array );
}
return $event_array;
}
/**
* Get searched events from $_SESSION array
*
* @return array event_array
*/
function mc_get_searched_events() {
if ( ! session_id() || ! isset( $_SESSION['MC_SEARCH_RESULT'] ) ) {
return array();
}
$event_array = array();
$event_searched = json_decode( $_SESSION['MC_SEARCH_RESULT'], true );
foreach ( $event_searched as $key => $value ) {
$daily_events = array();
foreach ( $value as $k => $v ) {
$daily_events[] = (object) $v;
}
$event_array[ $key ] = $daily_events;
}
return $event_array;
}
/**
* Generates the list of search results.
*
* @param array $events Array of events to analyze, organized by date.
* @param string $template Custom template to use for display.
*
* @return string HTML output of search results.
*/
function mc_produce_search_results( $events, $template ) {
$output = array();
$near_events = array();
$temp_array = array();
$group = array();
$spans = array();
$occur = array();
$recurring_events = array();
$last_events = array();
$last_group = array();
if ( is_array( $events ) ) {
foreach ( $events as $k => $event ) {
if ( is_array( $event ) ) {
foreach ( $event as $e ) {
if ( ! mc_private_event( $e ) ) {
// Store span time in an array to avoid repeating database query.
if ( '1' === $e->event_span && ( ! isset( $spans[ $e->occur_group_id ] ) ) ) {
// This is a multi-day event: treat each event as if it spanned the entire range of the group.
$span_time = mc_span_time( $e->occur_group_id );
$spans[ $e->occur_group_id ] = $span_time;
} elseif ( '1' === $e->event_span && ( isset( $spans[ $e->occur_group_id ] ) ) ) {
$span_time = $spans[ $e->occur_group_id ];
}
if ( $e ) {
// If a multi-day event, show only once.
if ( '0' !== $e->occur_group_id && '1' === $e->event_span && in_array( $e->occur_group_id, $group, true ) || in_array( $e->occur_id, $occur, true ) ) {
$md = true;
} else {
$group[] = $e->occur_group_id;
$occur[] = $e->occur_id;
$md = false;
}
// end multi-day reduction.
if ( ! $md ) {
$instances = mc_get_occurrences( $e->occur_event_id );
if ( count( $instances ) > 1 ) {
$recurring_events[] = $e->occur_event_id;
}
$near_events[] = $e; // Split off another future event.
$last_events[] = $e->occur_id;
$last_group[] = $e->occur_group_id;
}
}
}
}
}
}
}
$events = $near_events;
usort( $events, 'mc_datetime_cmp' ); // Sort split events by date.
if ( is_array( $events ) ) {
foreach ( array_keys( $events ) as $key ) {
$event =& $events[ $key ];
$temp_array[] = $event;
}
$groups = array();
foreach ( reverse_array( $temp_array, true, 'asc' ) as $event ) {
$details = mc_create_tags( $event, 'nofilters' );
if ( ! in_array( $details['group'], $groups, true ) ) {
$output[] = array(
'event' => $event,
'tags' => $details,
);
if ( '1' === $details['event_span'] ) {
$groups[] = $details['group'];
}
}
}
}
$html = '';
foreach ( $output as $out ) {
$data = array(
'event' => $event,
'tags' => $details,
'template' => $template,
'type' => 'list',
);
$details = mc_load_template( 'upcoming', $data );
if ( ! $details ) {
$html .= mc_format_upcoming_event( $out, $template, 'list' );
}
}
return $html;
}