forked from betterangels/better-angels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-buoy-chat-room.php
311 lines (283 loc) · 9.56 KB
/
class-buoy-chat-room.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
<?php
/**
* Buoy Alert chat room for built-in WordPress "chat" using comments.
*
* @package WordPress\Plugin\WP_Buoy_Plugin\WP_Buoy_Alert\WordPress_Chat
*
* @copyright Copyright (c) 2015-2016 by Meitar "maymay" Moscovitz
*
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
if (!defined('ABSPATH')) { exit; } // Disallow direct HTTP access.
/**
* Loads and displays comments for built-in "chat" feature.
*/
class WP_Buoy_Chat_Room extends WP_Buoy_Plugin {
/**
* The visible chat messages.
*
* @var WP_Comment[]
*/
private $_comments;
/**
* The alert associatd with this chat room.
*
* @var WP_Buoy_Alert
*/
private $_alert;
/**
* Constructor.
*
* @param int|string $lookup
*/
public function __construct ($lookup) {
$this->_alert = new WP_Buoy_Alert($lookup);
$this->_comments = get_comments(array(
'post_id' => $this->_alert->wp_post->ID
));
}
/**
* Whether or not a given user is a responder for this alert.
*
* @param int $user_id
*
* @return bool
*/
public function is_responder ($user_id) {
return $this->_alert->is_responder($user_id);
}
/**
* Whether or not a given user is the "owner" of the chat room.
*
* @param int $user_id
*
* @return bool
*/
public function is_alerter ($user_id) {
return $user_id == $this->_alert->wp_post->post_author;
}
/**
* Retrieves the title of the alert for this chat room.
*
* @return string
*/
public function get_title () {
return $this->_alert->wp_post->post_title;
}
/**
* Gets the comment HTML.
*
* @uses wp_parse_args()
* @uses wp_list_comments()
*
* @param array $args Arguments to pass to `wp_list_comments()`
*
* @return string|void
*/
public function list_comments ($args = array()) {
add_filter('comment_class', array(__CLASS__, 'filterCommentClass'), 10, 5);
$defaults = array(
'reverse_top_level' => 'desc',
'callback' => array(__CLASS__, 'renderComment')
);
$args = wp_parse_args($args, $defaults);
wp_list_comments($args, $this->_comments);
}
/**
* Renders an individual comment.
*
* @param WP_Comment $comment
* @param array $args
* @param int $depth
*
* @return void
*/
public static function renderComment ($comment, $args, $depth) {
$side = (get_current_user_id() == $comment->user_id) ? 'right': 'left';
?>
<li id="comment-<?php print esc_attr($comment->comment_ID)?>" <?php comment_class("media media-on-$side", $comment);?>>
<?php
switch ($side) {
case 'right': // Body first, then media.
WP_Buoy_Chat_Room::renderCommentBody($comment, $args, $depth);
WP_Buoy_Chat_Room::renderCommentMedia($side, $comment, $args, $depth);
break;
default: // Media first, then body.
WP_Buoy_Chat_Room::renderCommentMedia($side, $comment, $args, $depth);
WP_Buoy_Chat_Room::renderCommentBody($comment, $args, $depth);
break;
}
?>
</li>
<?php
}
/**
* Renders an individual comment's media element.
*
* @param string $align Either `left` or `right`
* @param WP_Comment $comment
* @param array $args
* @param int $depth
*
* @return void
*/
protected static function renderCommentMedia ($align, $comment, $args, $depth) {
?>
<div class="media-<?php print esc_attr($align);?> media-bottom vcard">
<span class="comment-author fn"><?php comment_author($comment);?></span>
<a href="mailto:<?php print esc_attr(comment_author_email($comment));?>">
<?php print get_avatar($comment, 48, '', false, array(
'class' => 'media-object'
));?>
</a>
</div>
<?php
}
/**
* Renders an individual comment's body.
*
* @param WP_Comment $comment
* @param array $args
* @param int $depth
*
* @return void
*/
protected static function renderCommentBody ($comment, $args, $depth) {
$time_ago = human_time_diff(get_comment_time('U'), current_time('timestamp')) . ' ' . __('ago');
?>
<div class="media-body">
<?php comment_text($comment, $args);?>
<footer>
<time datetime="<?php print esc_attr(get_comment_time('c'));?>"><?php print esc_html($time_ago);?></time>
</footer>
</div>
<?php
}
/**
* Outputs the <meta> tag for refreshing the chat room automatically.
*
* @todo The default refresh rate could (should?) become an admin
* option configurable via the plugin's settings page.
*
* Is there a way to go to the #page-footer upon reresh by setting the url here?
* Placing it in the meta tag here doesn't seem to work (browser ignores it?)
*
* @return void
*/
public static function renderMetaRefresh () {
/**
* Filters the chat room refresh rate.
*/
$refresh = apply_filters(self::$prefix . '_chat_room_meta_refresh_rate', 5);
/**
* Filters the URL to which the chat room reloads to.
*/
$url = apply_filters(self::$prefix . '_chat_room_meta_refresh_url', $_SERVER['REQUEST_URI']);
$html = '<meta http-equiv="refresh" content="%1$s;url=%2$s" />';
$options = WP_Buoy_Settings::get_instance();
if ($options->get('debug')) {
return; // don't print anything
}
print sprintf($html, $refresh, str_replace('&reset', '', $url));
}
/**
* Adds "do_form_reset" to the body class for new chat reloads.
*
* @link https://developer.wordpress.org/reference/hooks/body_class/
*
* @param string[] $classes
*
* @return string[]
*/
public static function filterBodyClass ($classes) {
// If we're posting a new comment, then we tell the parent frame to
// reset the form field.
if (isset($_GET['reset'])) {
$classes[] = 'do_form_reset';
}
return $classes;
}
/**
* Adds our own class to each comment "chat message" output.
*
* @link https://developer.wordpress.org/reference/hooks/comment_class/
*
* @param string[] $classes
*
* @return string[]
*/
public static function filterCommentClass ($classes) {
$classes[] = self::$prefix . '-chat-message';
return $classes;
}
/**
* Spruces up the comment text when displaying a comment.
*
* Chat messages are stored as comments in the WordPress database
* so this filter takes the text (typically a single line) and
* adds some basic expected functionality like turning links to
* images into inline images, and parsing simple markdown.
*
* @link https://developer.wordpress.org/reference/hooks/comment_text/
*
* @uses Parsedown::text()
* @uses links_add_target()
*
* @param string $comment_text
*
* @return string
*/
public static function filterCommentText ($comment_text) {
// Detect any URLs that point to recognized images, and embed them.
$pat = '!(?:([^:/?#\s]+):)?(?://([^/?#]*))?([^?#\s]*\.(jpe?g|JPE?G|gif|GIF|png|PNG))(?:\?([^#]*))?(?:#(.*))?!';
$rep = '<a href="$0"><img src="$0" alt="[' . sprintf(esc_attr__('%1$s image from %2$s', 'buoy'), '.$4 ', '$2') . ']" style="max-width:100%;" /></a>';
$comment_text = preg_replace($pat, $rep, $comment_text);
// Finally, parse the result as markdown for more formatting
if (!class_exists('Parsedown')) {
require_once dirname(__FILE__) . '/includes/vendor/wp-screen-help-loader/vendor/parsedown/Parsedown.php';
}
$comment_text = Parsedown::instance()->text($comment_text);
return links_add_target($comment_text);
}
/**
* Renders a chat room.
*
* @global $buoy_chat_room
*
* @todo Remove this global. Maybe template-ize this a bit better
* with actual `load_template()` functions and similar to a
* WordPress front-end? That would let theme developers use
* their skills to customize the built-in chat room, too.
*
* @return void
*/
public function render () {
global $buoy_chat_room;
// TODO: This should become a "real" template, but for now, we just
// empty the major front-end template hooks so we have a clean
// slate from which to define a simple HTML "template."
remove_all_actions('wp_head');
remove_all_actions('wp_footer');
add_action('wp_head', array(__CLASS__, 'renderMetaRefresh'), 10, 2);
add_action('wp_head', 'wp_print_styles');
add_action('wp_head', 'wp_print_head_scripts');
WP_Buoy_Alert::enqueueBootstrapFramework();
wp_enqueue_style(
self::$prefix . '-chat-room',
plugins_url('/templates/comments-chat-room.css', __FILE__),
array(),
null
);
wp_enqueue_script(
self::$prefix . '-chat-room',
plugins_url('/templates/comments-chat-room.js', __FILE__),
array(),
null
);
add_filter('body_class', array(__CLASS__, 'filterBodyClass'));
add_filter('comment_text', array(__CLASS__, 'filterCommentText'), 5); // early priority
require_once dirname(__FILE__) . '/templates/comments-chat-room.php';
do_action('shutdown');
exit();
}
}