-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-relative-time.php
198 lines (163 loc) · 5.39 KB
/
bp-relative-time.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
<?php
add_action( 'bp_init', array( 'BP_Relative_Time', 'init' ) );
/**
* BP Relative Time Core
*/
class BP_Relative_Time {
/**
* Static initializer.
*/
public static function init() {
return new self();
}
/**
* Constructor.
*/
public function __construct() {
// Theme compatibility doesn't need this.
if ( bp_detect_theme_compat_with_current_theme() ) {
return;
}
// assets
add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
/** hook stuff **/
// @todo - messages, blogs directory
// Remove core BP moment locale; this plugin handles moment differently.
// Works only for English installs though...
add_filter( 'bp_core_register_common_scripts', function( $retval ) {
if ( isset( $retval['bp-moment-locale'] ) ) {
unset( $retval['bp-moment-locale'] );
}
return $retval;
} );
// members
add_filter( 'bp_member_last_active', array( $this, 'member_last_active' ) );
add_filter( 'bp_member_registered', array( $this, 'member_registered' ) );
// groups
add_filter( 'bp_get_group_date_created', array( $this, 'group_date_created' ) );
add_filter( 'bp_get_group_last_active', array( $this, 'group_last_active' ) );
}
/**
* Enqueue our scripts.
*/
public function enqueue_scripts() {
$load = apply_filters( 'bp_relative_time_enqueue', is_buddypress() );
if ( false == (bool) $load ) {
return;
}
// enqueue livestamp.js
wp_enqueue_script( 'bp-livestamp' );
// we're only localizing the relative time strings for moment.js since that's
// all we need for now
wp_localize_script( 'bp-moment', 'BP_Moment_i18n', array(
'future' => __( 'in %s', 'buddypress' ),
'past' => __( '%s ago', 'buddypress' ),
's' => __( 'a few seconds', 'buddypress' ),
'm' => __( 'a minute', 'buddypress' ),
'mm' => __( '%d minutes', 'buddypress' ),
'h' => __( 'an hour', 'buddypress' ),
'hh' => __( '%d hours', 'buddypress' ),
'd' => __( 'a day', 'buddypress' ),
'dd' => __( '%d days', 'buddypress' ),
'M' => __( 'a month', 'buddypress' ),
'MM' => __( '%d months', 'buddypress' ),
'y' => __( 'a year', 'buddypress' ),
'yy' => __( '%d years', 'buddypress' ),
) );
add_action( 'wp_footer', array( $this, 'inline_js' ), 9999 );
}
/**
* Inline JS.
*/
public function inline_js() {
// if livestamp hasn't printed, stop now!
if ( false === wp_script_is( 'bp-livestamp', 'done' ) ) {
return;
}
?>
<script type="text/javascript">
jq(function() {
// Remove BP's livestamp. We handle context for English installs properly.
jq('span.activity').livestamp('destroy');
moment.updateLocale( 'en', {
relativeTime : BP_Moment_i18n
});
});
</script>
<?php
}
public function member_last_active( $retval ) {
global $members_template;
if ( is_numeric( $members_template->member->last_activity ) ) {
$last_active = gmdate( 'Y-m-d H:i:s', $members_template->member->last_activity );
} else {
$last_active = $members_template->member->last_activity;
}
/*
* If we're here, that means 'populate_extras' is false, so manually fetch
* last activity.
*/
if ( empty( $last_active ) ) {
$last_active = bp_get_user_last_activity( $members_template->member->id );
if ( ! empty( $last_active ) ) {
$members_template->member->last_activity = $activity;
// Rare, but fallback to no activity.
} else {
return __( 'Never active', 'buddypress' );
}
}
return sprintf(
// ugh... inconsistencies!
// groups component has the 'active' string separated from the timestamp,
// while the members component has it all-in-one. let's separate the two to
// match the groups component
__( 'active %s', 'buddypress' ),
sprintf(
'<span data-livestamp="%1$s">%2$s</span>',
bp_core_get_iso8601_date( $last_active ),
bp_core_time_since( $members_template->member->last_activity )
)
);
}
public function member_registered( $retval ) {
global $members_template;
return sprintf(
// ugh... inconsistencies!
// groups component has the 'active' string separated from the timestamp,
// while the members component has it all-in-one. let's separate the two to
// match the groups component
_x( 'registered %s', 'Records the timestamp that the user registered into the activy stream', 'buddypress' ),
sprintf(
'<span data-livestamp="%1$s">%2$s</span>',
bp_core_get_iso8601_date( $members_template->member->user_registered ),
bp_core_time_since( $members_template->member->user_registered )
)
);
}
public function group_date_created( $retval ) {
global $groups_template;
if ( empty( $groups_template->group ) ) {
return $retval;
}
return sprintf(
'<span data-livestamp="%1$s">%2$s</span>',
bp_core_get_iso8601_date( $groups_template->group->date_created ),
$retval
);
}
public function group_last_active( $retval ) {
global $groups_template;
if ( empty( $groups_template->group ) ) {
return $retval;
}
$last_active = ! empty( $groups_template->group->last_activity ) ? $groups_template->group->last_activity : false;
if ( false === $last_active ) {
$last_active = groups_get_groupmeta( $groups_template->group->id, 'last_activity' );
}
return sprintf(
'<span data-livestamp="%1$s">%2$s</span>',
bp_core_get_iso8601_date( $last_active ),
$retval
);
}
}