-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.php
559 lines (482 loc) · 15.4 KB
/
App.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
<?php
namespace App;
use BCcampus\MegaWalker;
use Inc2734\WP_Breadcrumbs;
use Sober\Controller\Controller;
class App extends Controller {
/**
* @return string|void
*/
public function siteName() {
return get_bloginfo( 'name' );
}
/**
* @return bool|mixed|null|string|string[]|void
*/
public static function title() {
if ( is_home() ) {
$home = get_option( 'page_for_posts', true );
if ( $home ) {
return get_the_title( $home );
}
return __( 'Latest Posts', 'bcc-sage' );
}
if ( is_archive() ) {
if ( is_category() ) {
return __( 'News', 'bcc-sage' );
} else {
return get_the_archive_title();
}
}
if ( is_search() ) {
return sprintf( __( 'Search Results for %s', 'bcc-sage' ), get_search_query() );
}
if ( is_404() ) {
return __( 'Not Found', 'bcc-sage' );
}
if ( is_singular( 'post' ) ) {
return __( 'News', 'bcc-sage' );
}
return get_the_title();
}
/**
* get wp menu to act like bootstrap menu
*
*/
public function navWalker() {
if ( class_exists( '\\BCcampus\MegaWalker' ) ) {
return new MegaWalker();
}
}
/**
* robust breadcrumb functionality
*
* @return array
*/
public function breadCrumbs() {
$bc = new WP_Breadcrumbs\Breadcrumbs();
return $bc->get();
}
/**
* Environment check
*
* @return bool
*/
public static function isProduction() {
$url = get_site_url( get_current_blog_id() );
$host = wp_parse_url( $url, PHP_URL_HOST );
$expected = [
'bccampus.ca',
'etug.ca',
'bctlc.ca',
];
// target subdomains
$parts = explode( '.', $host );
$tld = array_pop( $parts );
$sld = array_pop( $parts );
$base_domain = $sld . '.' . $tld;
if ( in_array( $base_domain, $expected, true ) && 'helga.bccampus.ca' !== $host ) {
return true;
}
return false;
}
/**
* Prefers to find posts in the same category or tag and accesses a back up
* for those that have neither (pages)
*
* @param $post
* @param array $post_types
* @param string $limit
* @param string $category_name
*
* @return array
*/
public static function getRelevant( $post, $post_types = [], $limit = '', $category_name = '' ) {
$append_c = [];
$append_t = [];
$more_related = [];
$tags = wp_get_post_tags( $post->ID );
$cats = wp_get_post_categories( $post->ID );
$this_many = ( empty( $limit ) ) ? 6 : $limit;
$type = ( empty( $post_types ) ) ? [
'post',
'page',
'ai1ec',
] : $post_types;
if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
$first_tag = $tags[0]->term_id;
$append_t = [ 'tag__in' => [ $first_tag ] ];
}
if ( ! empty( $cats ) && ! is_wp_error( $cats ) ) {
$first_cat = $cats[0];
$append_c = [ 'category__in' => [ $first_cat ] ];
}
$args = [
'post__not_in' => [ $post->ID ],
'posts_per_page' => $this_many * 3,
'ignore_sticky_posts' => 1,
'post_type' => $type,
'post_status' => 'publish',
'category_name' => $category_name,
'orderby' => 'date',
'order' => 'DESC',
];
$maybe_more_args = array_merge( $args, $append_t );
$maybe_more_args = array_merge( $maybe_more_args, $append_c );
$related_posts = get_posts( $maybe_more_args );
$how_many = count( $related_posts );
if ( $how_many >= $this_many ) {
shuffle( $related_posts );
$slice = array_slice( $related_posts, 0, $this_many );
} else {
$new_limit = intval( $this_many ) - intval( $how_many );
$more = self::matchRelevant( $post, $post_types, $this_many, $new_limit );
if ( $more ) {
$only = wp_list_pluck( $more, 'ID' );
$more_related = get_posts(
[
'include' => $only,
'post__not_in' => $post->ID,
'post_status' => 'publish',
'post_type' => $type,
'ignore_sticky_posts' => 1,
]
);
}
$related = array_merge( $related_posts, $more_related );
$slice = array_slice( $related, 0, $this_many );
shuffle( $slice );
}
return $slice;
}
/**
* Provides a mechanism to insert a default image if none exists
*
* @param $post_id
* @param array $size
*
* @return string
*/
public static function getThumb( $post_id, $size = [] ) {
static $current_domain = null;
if ( null === $current_domain ) {
$current_domain = site_url();
}
$dimensions = ( empty( $size ) ) ? [ 175, 175 ] : $size;
$result = get_the_post_thumbnail( $post_id, $dimensions );
if ( empty( $result ) ) {
$src = get_stylesheet_directory_uri() . '/assets/images/placeholder-image-300x200.jpg';
$result = "<img width='{$dimensions[0]}' height='{$dimensions[1]}' src='{$src}'/>";
}
return $result;
}
/**
* Returns the url of the post thumbnail, or default img url
*
* @param $id
*
* @return mixed
*/
public static function getThumbUrl( $id = 0 ) {
if ( 0 === $id ) {
$post = get_post( 0 );
$id = $post->ID;
}
if ( has_post_thumbnail( $id ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'single-post-thumbnail' );
} else {
$image[] = get_stylesheet_directory_uri() . '/assets/images/placeholder-image-300x200.jpg';
}
return $image[0];
}
/**
* Uses string values in post_title, post_content to find matches in DB
*
* @param $post
* @param array $post_types
* @param string $limit
*
* @return array|null|object
*/
public static function matchRelevant( $post, $post_types = [], $limit = '' ) {
global $wpdb;
$types = ( empty( $post_types ) ) ? [
'post',
'page',
'ai1ec',
] : $post_types;
$limits = ( empty( $limit ) ) ? '6' : $limit;
$now = gmdate( 'Y-m-d H:i:s', ( time() + ( 3600 ) ) );
$match_fields = 'post_title,post_content';
$string = $wpdb->prepare( '%s %s', $post->post_title, strip_tags( $post->post_content ) );
$match = $wpdb->prepare( ' AND MATCH (%s) AGAINST (%s) AS relevance', $match_fields, $string );
$before = $wpdb->prepare( " AND $wpdb->posts.post_date < %s ", $now );
$from = $wpdb->prepare( " AND $wpdb->posts.post_date >= %s ", gmdate( 'Y-m-d H:i:s', ( time() - ( YEAR_IN_SECONDS * 3 ) ) ) );
$where = " AND $wpdb->posts.post_status = 'publish' ";
$where .= " AND $wpdb->posts.post_type IN ('" . join( "', '", $types ) . "') ";
$where .= " AND $wpdb->posts.ID NOT IN ({$post->ID}) ";
$orderby = ' ORDER BY relevance DESC';
$limited = $wpdb->prepare( ' LIMIT %d, %d ', 0, $limits );
$results = $wpdb->get_results( $wpdb->prepare( 'SELECT DISTINCT %s FROM %s WHERE 1 = 1 %s %s %s %s %s %s', $wpdb->posts . '.ID', $wpdb->posts, $match, $before, $from, $where, $limited, $orderby ) );
return $results;
}
/**
* Schema.org microdata at WebPage level
*
* @return array
*/
public function getMicroData() {
global $post;
$id = $post->ID;
$meta = get_post( $id, ARRAY_A );
$post_author = get_the_author_meta( 'display_name', $meta['post_author'] );
$keywords = ( ! is_array( $meta['tags_input'] ) ) ? 'connect,collaborate,innovate' : implode( ',', $meta['tags_input'] );
$excerpt = ( is_front_page() ) ? get_bloginfo( 'description', 'display' ) : get_the_excerpt();
$categories = '';
$subject = [];
// about
if ( is_array( $meta['post_category'] ) && ! empty( $meta['post_category'] ) ) {
foreach ( $meta['post_category'] as $cat ) {
$result = get_category( $cat, ARRAY_A );
if ( 'Uncategorized' !== $result['cat_name'] ) {
$subject[] = $result['cat_name'];
}
}
$categories = implode( ',', $subject );
}
$about = "Author: {$post_author}, Publication Date: {$meta['post_date']}, Excerpt:{$excerpt}, Categories: {$categories}";
$micro_mapping = [
'author' => $post_author,
'description' => $about,
'dateModified' => $meta['post_modified'],
'datePublished' => $meta['post_date'],
'keywords' => $keywords,
'inLanguage' => 'en',
'name' => $meta['post_title'],
'sourceOrganization' => 'BCcampus',
'url' => get_permalink(),
];
return $micro_mapping;
}
/**
* Useful in `wp_list_pages` to switch context based on the existence of
* children
*
* @param $id
*
* @return false|int
*/
public static function getChildOf( $id ) {
// check for children
$args = [
'post_parent' => $id,
'post_type' => 'page',
'post_status' => 'publish',
];
$children = get_children( $args, ARRAY_A );
if ( empty( $children ) ) {
$parent_id = wp_get_post_parent_id( $id );
} else {
$parent_id = $id;
}
return $parent_id;
}
/**
* Useful in `wp_list_pages` to return different title based on
* ancestry context
*
* @param $id
*
* @return string
*/
public static function getListHeading( $id ) {
// check for children
$args = [
'post_parent' => $id,
'post_type' => 'page',
'post_status' => 'publish',
];
$children = get_children( $args );
$parent_id = wp_get_post_parent_id( $id );
// no sub-pages heading will be a parent
if ( empty( $children ) ) {
// top of the tree, return id of front page
if ( 0 === $parent_id ) {
$parent_id = get_option( 'page_on_front' );
}
$maybe_parent_title = get_the_title( $parent_id );
} else {
// heading will be current post
$maybe_parent_title = get_the_title( $id );
}
$html = $maybe_parent_title;
return $html;
}
/**
* @param $args
*
* @return array
*/
public static function getLatestNews( $args = [] ) {
$sticky = get_option( 'sticky_posts' );
$posts_to_exclude = ( ! empty( $sticky ) ) ? array_values( $sticky ) : [];
$defaults = [
'posts_per_page' => 1,
'post_status' => 'publish',
'exclude' => $posts_to_exclude,
];
$r = wp_parse_args( $args, $defaults );
$latest = get_posts( $r );
return $latest;
}
/**
* Returns events in a specific event category
*
* @param array $args
* @param $event_category
*
* @return array
*/
public static function getEvents( $event_category, $args = [] ) {
$defaults = [
'posts_per_page' => 1,
'post_status' => 'publish',
'exclude' => [],
'tax_query' => [ //@codingStandardsIgnoreLine
[
'taxonomy' => 'events_categories',
'field' => 'name',
'terms' => $event_category,
],
],
];
$r = wp_parse_args( $args, $defaults );
$events = get_posts( $r );
return $events;
}
/**
* Cloodge of a function for poorly documented plugin
*
* @see https://gist.github.com/lukaspawlik/045dbd5b517a9eb1cf95
*
* @param int $limit
* @param array $ids
* @param bool $last_day
* @param $exclude_ids
*
* @return array|bool
*/
public static function getUpcomingEvents( $limit = 7, $ids = [], $last_day = false, $exclude_ids = [] ) {
if ( ! class_exists( 'Ai1ec_Loader' ) ) {
return false;
}
global $ai1ec_registry;
$results = [];
$filter['cat_ids'] = $ids;
$t = $ai1ec_registry->get( 'date.system' );
// Get localized time
$time = $t->current_time();
$event_results = $ai1ec_registry->get( 'model.search' )
->get_events_relative_to( $time, $limit, '', $filter, $last_day );
$dates = $ai1ec_registry->get( 'view.calendar.view.agenda', $ai1ec_registry->get( 'http.request.parser' ) )
->get_agenda_like_date_array( $event_results['events'] );
foreach ( $dates as $date ) {
foreach ( $date['events']['allday'] as $instance ) {
$results[ $instance->get( 'instance_id' ) ]['title'] = $instance->get( 'post' )->post_title;
$results[ $instance->get( 'instance_id' ) ]['link'] = $instance->get( 'post' )->guid;
$results[ $instance->get( 'instance_id' ) ]['start'] = $date['month'] . ' ' . $date['day'] . ', ' . $date['year'];
$results[ $instance->get( 'instance_id' ) ]['post_id'] = $instance->get( 'post' )->ID;
$results[ $instance->get( 'instance_id' ) ]['post_content'] = $instance->get( 'post' )->post_content;
$results[ $instance->get( 'instance_id' ) ]['location'] = $instance->get( 'address' );
}
foreach ( $date['events']['notallday'] as $instance ) {
$results[ $instance->get( 'instance_id' ) ]['title'] = $instance->get( 'post' )->post_title;
$results[ $instance->get( 'instance_id' ) ]['link'] = $instance->get( 'post' )->guid;
$results[ $instance->get( 'instance_id' ) ]['start'] = $date['month'] . ' ' . $date['day'] . ', ' . $date['year'];
$results[ $instance->get( 'instance_id' ) ]['post_id'] = $instance->get( 'post' )->ID;
$results[ $instance->get( 'instance_id' ) ]['post_content'] = $instance->get( 'post' )->post_content;
$results[ $instance->get( 'instance_id' ) ]['location'] = $instance->get( 'address' );
}
}
// exclude certain ids
if ( ! empty( $exclude_ids ) ) {
foreach ( $exclude_ids as $exclude ) {
if ( array_key_exists( $exclude, $results ) ) {
unset( $results[ $exclude ] );
}
}
}
return $results;
}
/**
* @param array $cat_ids
*
* @return array|bool
*/
public static function getIdsInCategory( $cat_ids = [] ) {
if ( ! class_exists( 'Ai1ec_Loader' ) ) {
return false;
}
global $ai1ec_registry;
$results = [];
$filter['cat_ids'] = $cat_ids;
$t = $ai1ec_registry->get( 'date.system' );
$last_day = false;
$limit = 7;
// Get localized time
$time = $t->current_time();
$event_results = $ai1ec_registry->get( 'model.search' )
->get_events_relative_to( $time, $limit, '', $filter, $last_day );
$dates = $ai1ec_registry->get( 'view.calendar.view.agenda', $ai1ec_registry->get( 'http.request.parser' ) )
->get_agenda_like_date_array( $event_results['events'] );
foreach ( $dates as $date ) {
foreach ( $date['events']['allday'] as $instance ) {
$results[ $instance->get( 'instance_id' ) ]['post_id'] = $instance->get( 'post' )->ID;
}
foreach ( $date['events']['notallday'] as $instance ) {
$results[ $instance->get( 'instance_id' ) ]['post_id'] = $instance->get( 'post' )->ID;
}
}
if ( ! empty( $results ) ) {
$results = array_keys( $results );
}
return $results;
}
/**
* Content imported from other sites will have a permalink that leads back
* to the original site. This attempts to resolve that.
*
* @param int $id post_id
* @param string $name post_name
*
* @return string
*/
public static function maybeGuid( $id, $name ) {
static $current_domain = null;
if ( null === $current_domain ) {
$current_domain = site_url();
}
$current = wp_parse_url( $current_domain, PHP_URL_HOST );
$url = esc_url( get_permalink( $id ) );
$incoming = wp_parse_url( $url, PHP_URL_HOST );
if ( 0 !== strcmp( $current, $incoming ) ) {
$url = $current_domain . '/' . $name;
}
return $url;
}
/**
* Smooths out the uncertainty of whether or not there is an excerpt
*
* @param $id
* @param $content
* @param $link
* @param int $length
*
* @return string
*/
public static function maybeExcerpt( $id, $content, $link, $length = 15 ) {
$maybe_excerpt = apply_filters( 'the_excerpt', get_post_field( 'post_excerpt', $id ) );
$excerpt = ( empty( $maybe_excerpt ) ) ? wp_trim_words( $content, $length, "<a href='{$link}'>…<span class='fa fa-arrow-right'></span></a>" ) : wp_trim_words( $maybe_excerpt, $length, "<a href='{$link}'>…<span class='fa fa-arrow-right'></span></a>" );
return $excerpt;
}
}