-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
105 lines (85 loc) · 2.78 KB
/
functions.php
File metadata and controls
105 lines (85 loc) · 2.78 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
<?php if(!defined('ABSPATH')) die();
/**
* Main file for the WordPress Theme
*
* @author Rickard Andersson <rickard@montania.se>
* @copyright Montania System AB
* @version 1.2
*/
// Load acf and class files
require 'acf.php';
require 'vendor/autoload.php';
/**
* Function to get the time in 'relative format', i.e. xxx seconds/minutes/hours/... ago
*
* @param int $timestamp
*
* @return string
* @since 1.0
*/
function relative_time($timestamp)
{
if (!is_numeric($timestamp)) {
return '';
}
$tz_string = get_option('timezone_string');
if ($tz_string) {
$tz = new DateTimeZone($tz_string);
$now = new DateTime('now', $tz);
$posted = DateTime::createFromFormat('U', $timestamp, $tz);
} else {
$now = new DateTime('now');
$posted = DateTime::createFromFormat('U', $timestamp);
}
$difference = strtotime($now->format('Y-m-d H:i:s')) - strtotime($posted->format('Y-m-d H:i:s'));
$lengths = array('60', '60', '24', '7', '4.35', '12', '10');
$text = '';
if ($difference > 0) { // this was in the past
$text = __('%1$s %2$s ago', 'montania');
} else { // this was in the future
$difference = -$difference;
$text = __('in %1$s %2$s', 'montania');
}
for ($j = 0; $difference >= $lengths[$j]; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
$periods = array(
_n('second', 'seconds', $difference, 'montania'),
_n('minute', 'minutes', $difference, 'montania'),
_n('hour', 'hours', $difference, 'montania'),
_n('day', 'days', $difference, 'montania'),
_n('week', 'weeks', $difference, 'montania'),
_n('month', 'months', $difference, 'montania'),
_n('year', 'years', $difference, 'montania'),
_n('decade', 'decades', $difference, 'montania')
);
return sprintf($text, $difference, $periods[$j]);
}
/**
* Returns true if the current page is the login page
*
* @return bool
*/
function is_login_page()
{
return in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'));
}
/**
* Returns excerpt for post
*
* @param int $post_id
* @param int $excerpt_length
*
* @return string
*/
function get_excerpt_by_id($post_id, $excerpt_length = 55)
{
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_excerpt ? $the_post->post_excerpt : $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$the_excerpt = wp_trim_words($the_excerpt, $excerpt_length, $excerpt_more);
$the_excerpt = wpautop($the_excerpt);
return $the_excerpt;
}