-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproud-helpers.php
602 lines (530 loc) · 18.2 KB
/
proud-helpers.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
<?php
/**
* @author ProudCity
*/
namespace Proud\Core;
// Hacky copied function to produce exerpt
function wp_trim_excerpt($text = '', $more_link = false, $use_yoast = false, $words = false)
{
$raw_excerpt = $text;
// Try using yoast ?
if($use_yoast) {
global $post;
if(!empty($post)) {
$text = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
}
}
if (empty($text)) {
$text = get_the_content('');
$text = strip_shortcodes($text);
/** This filter is documented in wp-includes/post-template.php */
// $text = apply_filters( 'the_content', $text );
$text = str_replace(']]>', ']]>', $text);
/**
* Filter the number of words in an excerpt.
*
* @since 2.7.0
*
* @param int $number The number of words. Default 55.
*/
$excerpt_length = !empty($words) ? $words : apply_filters('excerpt_length', 55);
/**
* Filter the string in the "more" link displayed after a trimmed excerpt.
*
* @since 2.9.0
*
* @param string $more_string The string shown within the more link.
*/
$excerpt_more = $more_link ? apply_filters('excerpt_more', ' ' . '[…]') : '...';
$text = wp_trim_words($text, $excerpt_length, $excerpt_more);
}
/**
* Filter the trimmed excerpt string.
*
* @since 2.8.0
*
* @param string $text The trimmed text.
* @param string $raw_excerpt The text prior to trimming.
*/
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
/**
* Sanitize text input
*/
function sanitize_input_text_output($text, $shortcode = true)
{
$text = \wp_kses_post($text);
// Run some known stuff
if(!empty($GLOBALS['wp_embed'])) {
$text = $GLOBALS['wp_embed']->autoembed($text);
}
// Evaluate shortcode?
if($shortcode) {
$text = do_shortcode($text);
}
return $text;
}
// Recusive array merging from
// https://api.drupal.org/api/drupal/assets%21bootstrap.inc/function/drupal_array_merge_deep_array/7
function array_merge_deep_array($arrays)
{
$result = array();
foreach ($arrays as $array) {
foreach ($array as $key => $value) {
// Recurse when both values are arrays.
if (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
$result[$key] = array_merge_deep_array(array($result[$key], $value));
}
// Otherwise, use the latter value, overriding any previous value.
else {
$result[$key] = $value;
}
}
}
return $result;
}
/**
* Gets logo
* As of
*/
function get_proud_logo($use_theme = true, $logo_file = 'icon-white-1x.png')
{
if($use_theme) {
// New media ID based logo
$logo = get_theme_mod('proud_logo_id');
// Old url based logo
if(!$logo) {
$logo = get_theme_mod('proud_logo');
}
} elseif($logo_file) {
$logo = plugins_url('/assets/images/' . $logo_file, __FILE__);
}
return $logo ? $logo : false;
}
/**
* Prints retina version of proud logo
* $logo_version = 'icon-white' || 'logo-white' (with text)
*/
function print_proud_logo($logo_version = 'icon-white', $meta = [])
{
$image_meta = [
'srcset' => [
'1x' => get_proud_logo(false, $logo_version . '-1x.png'),
'2x' => get_proud_logo(false, $logo_version . '-2x.png')
],
'src' => get_proud_logo(false, $logo_version . '-1x.png'),
'meta' => [
'image_meta' => array_merge([
'alt' => __('ProudCity', 'proud-core'),
'class' => 'proud-logo'
], $meta)
]
];
print_retina_image($image_meta, false, true);
}
/**
* Build responsive image meta data from the post information.
* This is a replacement for wp_get_attachment_metadata(), which was returning no data.
*/
function build_responsive_image_metadata($media_id)
{
$media_post = get_post($media_id);
$title = is_object($media_post) && !empty($media_post->post_title) ? $media_post->post_title : '';
$alt = get_post_meta($media_id, '_wp_attachment_image_alt', true);
$metadata = [
'caption' => !empty($media_post->post_excerpt) ? $media_post->post_excerpt : null,
//'class' => @todo
];
if (!empty($alt)) {
$metadata['alt'] = $alt;
} else {
$metadata['title'] = $title;
}
return $metadata;
}
/**
* Build responsive image meta
*/
function build_responsive_image_meta($media_id, $size_max = 'full-screen', $size_small = 'medium')
{
$return = [
'srcset' => wp_get_attachment_image_srcset($media_id, $size_max, null),
'size' => wp_get_attachment_image_sizes($media_id, $size_max),
'src' => wp_get_attachment_image_src($media_id, $size_small),
'meta' => build_responsive_image_metadata($media_id),
];
return $return;
}
/**
* Print responsive image html
*/
function print_responsive_image($resp_img, $classes = [], $skip_media = false)
{
$classes[] = 'media';
$image_meta = !empty($resp_img['meta']) ? $resp_img['meta'] : [];
?>
<?php if(!empty($resp_img['src'])): ?>
<?php if(!$skip_media && !empty($classes)): ?>
<div class="<?php echo implode(' ', $classes) ?>">
<?php endif; ?>
<img src="<?php echo esc_url($resp_img['src'][0]); ?>"
srcset="<?php echo esc_attr($resp_img['srcset']); ?>"
sizes="<?php echo esc_attr($resp_img['size']); ?>"
<?php if (!empty($image_meta['class'])): ?> class="<?php echo $image_meta['class'] ?>"<?php endif; ?>
<?php if (!empty($image_meta['title'])): ?> title="<?php echo $image_meta['title'] ?>"<?php endif; ?>
<?php if (!empty($image_meta['alt'])): ?> alt="<?php echo $image_meta['alt'] ?>"<?php endif; ?>>
<?php if (!$skip_media && !empty($image_meta['caption'])): ?>
<div class="media-byline text-left" onclick="jQuery(this).toggleClass('active');"><span class="media-byline-inner"><?php echo $image_meta['caption'] ?></span></div>
<?php endif; ?>
<?php if(!$skip_media && !empty($classes)): ?>
</div>
<?php endif; ?>
<?php endif; ?>
<?php
}
/**
* Build retina image meta
*/
function build_retina_image_meta($media_id, $normal = 'medium', $retina = 'medium_large')
{
// Get meta
// @todo: replace this with build_responsive_image_metadata( $media_id )?
$media_meta = wp_get_attachment_metadata($media_id);
$src = wp_get_attachment_image_url($media_id, $normal);
return [
'srcset' => [
'1x' => $src,
'2x' => wp_get_attachment_image_url($media_id, $retina)
],
'src' => $src,
'meta' => $media_meta
];
}
/**
* Print retina image
*/
function print_retina_image($resp_img, $classes = [], $skip_media = false)
{
$classes = (! is_array($classes)) ? $classes = array( 'media' ) : $classes[] = 'media';
$image_meta = $resp_img['meta']['image_meta'];
$srcset = '';
foreach ($resp_img['srcset'] as $key => $image) {
$resp_img['srcset'][$key] = esc_url($image) . ' ' . $key;
}
?>
<?php if(!empty($resp_img['src'])): ?>
<?php if(!$skip_media && !empty($classes)): ?>
<div class="<?php echo implode(' ', $classes) ?>">
<?php endif; ?>
<img src="<?php echo esc_url($resp_img['src']); ?>"
srcset="<?php echo implode(', ', $resp_img['srcset']); ?>"
<?php if (!empty($image_meta['class'])): ?> class="<?php echo $image_meta['class'] ?>"<?php endif; ?>
<?php if (!empty($image_meta['title'])): ?> title="<?php echo $image_meta['title'] ?>"<?php endif; ?>
<?php if (!empty($image_meta['alt'])): ?> alt="<?php echo $image_meta['alt'] ?>"<?php endif; ?>>
<?php if (!$skip_media && !empty($image_meta['caption'])): ?>
<div class="media-byline text-left" onclick="jQuery(this).toggleClass('active');"><span class="media-byline-inner"><?php echo $image_meta['caption'] ?></span></div>
<?php endif; ?>
<?php if(!$skip_media && !empty($classes)): ?>
</div>
<?php endif; ?>
<?php endif; ?>
<?php
}
/**
* Helper function returns url to social acount
*/
function socialAccountUrl($service, $account)
{
switch ($service) {
case 'facebook':
case 'instagram':
case 'twitter':
return sprintf('https://%s.com/%s', $service, $account);
break;
// @TODO figure out youtube user / channel
case 'youtube':
return sprintf('https://%s.com/channel/%s', $service, $account);
break;
}
}
/**
* Helper function returns useful data for account
* $string: [service]:[account] eg: 'twitter:proudcity'
*/
function extractSocialData($string)
{
$account = explode(':', $string);
$url = socialAccountUrl($account[0], $account[1]);
return [
'service' => ucfirst($account[0]),
'account' => $account[1],
'url' => $url
];
}
/**
* Helper function gets social accounts from options
*/
function getSocialData()
{
$social = get_option('social_feeds');
if(!empty($social)) {
$social = explode(PHP_EOL, $social);
// Empty? Trim whitespace
return !empty($social) ? array_filter(array_map('trim', $social)) : [];
}
return [];
}
/**
* Returns the timezone string for a site, even if it's set to a UTC offset
*
* Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155
*
* @return string valid PHP timezone string
*/
function wpGetTimezoneString()
{
// if site timezone string exists, return it
if ($timezone = get_option('timezone_string')) {
return $timezone;
}
// get UTC offset, if it isn't set then return UTC
if (0 === ($utc_offset = get_option('gmt_offset', 0))) {
return 'UTC';
}
// adjust UTC offset from hours to seconds
$utc_offset *= 3600;
// attempt to guess the timezone string from the UTC offset
if ($timezone = timezone_name_from_abbr('', $utc_offset, 0)) {
return $timezone;
}
// last try, guess timezone string manually
$is_dst = date('I');
foreach (timezone_abbreviations_list() as $abbr) {
foreach ($abbr as $city) {
if ($city['dst'] == $is_dst && $city['offset'] == $utc_offset) {
return $city['timezone_id'];
}
}
}
// fallback to UTC
return 'UTC';
}
// Returns whether current office hours (formatted as just a plaintext string) are open
function isTimeOpen($string, &$alert, $holidays = '', $federal_holidays = true, $values = array('Closed', 'Open', 'Opening soon', 'Closing soon'))
{
/*
$string = "Mon- Fri:2:00am -5:00pm
Saturday: 9:00am - 12:00pm
Sunday: Closed";
*/
$string = str_replace(array('<br/>','<br>', '<br />'), "\n", $string);
$classes = array('text-danger', 'text-success', 'text-warning', 'text-warning'); // Same key as $values
$days = array( 'Mon', 'Monday', 'Tue', 'Tuesday', 'Wed', 'Wednesday', 'Thu', 'Thursday', 'Fri', 'Friday', 'Sat', 'Saturday', 'Sun', 'Sunday' );
$nums = array( 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 );
// Get the current site time
$blogtime = current_time('mysql');
list($today_year, $today_month, $today_day, $hour, $minute, $second) = preg_split('([^0-9])', $blogtime);
$timestamp = current_time('timestamp');
$week_day = date('N', $timestamp);
$daystamp = strtotime($hour .':'. $minute .':'. $second);
$datestamp = strtotime($today_year .'-'. $today_month .'-'. $today_day);
// Check today with the holidays
$holidays .= $federal_holidays ? federalHolidays() ."\n" : "\n";
$pattern = '/(.+?)\:\s?(.+?)\n/';
$matches = array();
$result = preg_match_all($pattern, $holidays, $matches);
for ($i = 0; $i < $result; $i++) {
if (strtotime($matches[2][$i]) == $datestamp) {
$alert = _x('Today is a holiday:', 'post name', 'wp-proud-core') .' '. $matches[1][$i];
return false;
}
}
// Deal with multiple office hours
$pattern = "/(^|\n)([a-zA-Z\ \-\.]+)\n/";
$type_matches = array();
if (preg_match_all($pattern, $string, $type_matches, PREG_OFFSET_CAPTURE)) {
$labels = array();
//print_r($type_matches[0]);
foreach ($type_matches[0] as $item) {
array_push($labels, array(
'label' => trim($item[0]),
'value' => false,
'class' => '',
'index' => $item[1],
));
}
} else {
$labels = array(array(
'label' => 'Currently',
'value' => false,
'class' => '',
'index' => 0,
));
}
$pattern = "/(^|\n)([a-zA-Z\ \-\.]+?)\:\s+?((\d+?)\:(\d+?)\s?(am|a\.m\.|pm|p\.m\.|AM|A\.M\.|PM|P\.M\.))\s?\-\s?((\d+?)\:(\d+?)\s?(am|a\.m\.|pm|p\.m\.|AM|A\.M\.|PM|P\.M\.))/";
$matches = array();
$result = preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
$status = null;
//print_r($result);
//print_r($matches);
// Cycles though all of the valid days
for ($i = 0; $i < $result; $i++) {
// This allows us to support lunch breaks like:
// Monday - Friday: 8:00am - 12:00pm
// Monday - Friday: 1:00pm - 5:00pm
if ($status == 1 || $status == 3) {
continue;
}
// Do lots of clean up on the day of the week to support ranges
$day = trim($matches[2][$i][0]);
$day = str_replace($days, $nums, $day);
$day = preg_replace("/[^0-9,.]/", "", $day);
if (strlen($day) == 1) {
$low = $high = (int)$day;
} else {
$low = substr($day, 0, 1);
$high = substr($day, 1, 1);
}
// Check if Day of the week matches
if ($week_day >= $low && $week_day <= $high) {
// Figure out the status:
// 0: closed
// 1: open
// 2: opening soon (<1 hr)
// 3: closing soon (<1 hr)
// $matches[2] is 9:00am; $matches[7] is 5:00pm
$opens = strtotime($matches[3][$i][0]);
$closes = strtotime($matches[7][$i][0]);
//print_r($opens - $daystamp.'opens ');
//print_r($closes - $daystamp.'closes ');
if ($opens - $daystamp <= 0 && $closes - $daystamp >= 0) {
if ($closes - $daystamp <= 3600) {
$status = 3;
} else {
$status = 1;
}
} elseif ($closes - $daystamp >= 0 && $opens - $daystamp > -3600) {
$status = 2;
} else {
$status = 0;
}
// Update the appropriate $labels value
$found = false;
for ($j = count($labels) - 1; $j >= 0; $j--) {
if ($labels[$j]['index'] <= $matches[0][$i][1] && !$found) {
$labels[$j]['value'] = $status;
//print_r('status'.$status);
$found = true;
}
}
}
} // for
// Clean up the return
foreach ($labels as $key => $label) {
$labels[$key]['class'] = $classes[ $label['value'] ];
$labels[$key]['value'] = $values[ $label['value'] ];
unset($labels[$key]['index']);
}
return $labels;
}
// Returns a text string of Federal Holidays.
// Form: https://www.redcort.com/us-federal-bank-holidays/
function federalHolidays()
{
return 'New Year\'s Day: Monday, January 1 2024
Martin Luther King, Jr. Day: Monday, January 15 2024
George Washington’s Birthday: Monday, February 19 2024
Memorial Day: Monday, May 27 2024
Juneteenth: Wednesday, June 19 2024
Independence Day: Thursday, July 4 2024
Labor Day: Monday, September 2 2024
Columbus Day: Monday, October 14 2024
Veterans Day: Monday, November 11 2024
Thanksgiving Day: Thursday, November 28 2024
Christmas Day: Wednesday, December 25 2024';
}
//
// WP Stateless helpers
//
/**
* Gets a meta array for a stateless hosted file
* @param $fid
* @return array
*/
function getStatelessFileMeta($fid)
{
$url = wp_get_attachment_url($fid);
$upload_root_url = trailingslashit('https://storage.googleapis.com/' . \ud_get_stateless_media()->get('sm.bucket'));
if (strpos($url, $upload_root_url) !== false) {
// Init WP-Stateless client
$client = \ud_get_stateless_media()->get_client();
// Get file name (https://storage.googleapis.com/.../wp-content/uploads/...)
$file = wp_normalize_path(str_replace($upload_root_url, '', $url));
$media_item = $client->get_media($file);
// Handle sizes
$size = $media_item['size'];
$size_nice = (float) $size;
$size_suffix = ' B';
$precision = 0;
if ($size_nice >= 1000000) {
$size_nice = $size_nice / 1000000;
$size_suffix = ' MB';
} elseif ($size_nice >= 1000) {
$size_nice = $size_nice / 1000;
$size_suffix = ' KB';
}
if ($size_nice < 10) {
$precision = 1;
}
$size_nice = round($size_nice, $precision) . $size_suffix;
// Mime, etc
$mime = $media_item['contentType'];
$fileparts = explode('/', $mime);
$filetype = $fileparts[1];
return [
'fid' => $fid,
'url' => $url,
'size' => $size_nice,
'size_bytes' => $size,
'icon' => document_icon_map($filetype),
'mime' => $mime,
'filetype' => $filetype,
];
}
return [
'fid' => $fid,
];
}
/**
* Helper returns a fontawesome icon
*/
function document_icon_map($filetype)
{
switch ($filetype) {
case 'pdf':
return 'fa-file-pdf-o';
case 'doc':
case 'docx':
return 'fa-file-word-o';
case 'ppt':
case 'pptx':
return 'fa-file-powerpoint-o';
case 'xls':
case 'xlsx':
return 'fa-file-excel-o';
case 'wav':
case 'aif':
case 'mp3':
return 'fa-file-audio-o';
case 'zip':
case 'tar':
return 'fa-file-zip-o';
case 'png':
case 'jpg':
case 'jpeg':
case 'gif':
return 'fa-file-photo-o';
default:
return 'fa-file-text-o';
}
}