-
Notifications
You must be signed in to change notification settings - Fork 1
/
comment-reactions.php
419 lines (360 loc) · 12.6 KB
/
comment-reactions.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
<?php
/*
Plugin Name: Comment Reactions
Plugin URI: https://wordpress.org/plugins/comment-reactions/
Description: Enable Slack style reactions to comments.
Author: Aki Björklund
Author URI: https://akibjorklund.com/
Version: 1.1.0
Text Domain: comment-reactions
Domain Path: /languages
*/
define( 'COMMENT_REACTIONS_VERSION', '1.0.0' );
define( 'COMMENT_REACTIONS_REST_NAMESPACE', 'comment-reactions/v1' );
add_action( 'wp_enqueue_scripts', 'creactions_load_script_and_style' );
add_action( 'comment_text', 'creactions_show_after_comment_text', 10, 2 );
add_action( 'init', 'creactions_load_textdomain' );
add_action( 'init', 'creactions_load_emoji' );
add_action( 'wp_footer', 'creactions_selector' );
add_action( 'rest_api_init', 'creactions_register_rest_routes' );
/**
* Load plugin textdomain
*
* @since 1.0.0
*/
function creactions_load_textdomain() {
load_plugin_textdomain( 'creactions', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Only load the huge Emoji definitions PHP file if really needed.
*
* @since 1.0.0
*/
function creactions_load_emoji() {
require_once( 'emoji-definitions.php' );
}
/**
* Get the reactions available with one click.
*
* @since 1.0.0
*
* @return array $reactions The reactions as an array. An alias as a key. Array of 'symbol'
* (the actual emoji) and 'description' (human readable text
* description of the emoji) as a value.
*/
function creactions_get_visible_reactions() {
$visible_reactions = array( 'thumbsup' );
/**
* Reactions visible, available with one click.
*
* @since 1.0.0
*
* @param array $reactions The reaction aliases.
*/
return apply_filters( 'creactions_visible', $visible_reactions );
}
/**
* Get reactions submitted to a comment plus all the always visible ones.
*
* @since 1.0.0
*
* @param int $comment_id The comment ID.
* @param array $always_visible_reactions Aliases of reactions always visible.
*/
function creactions_get_comment_reactions( $comment_id, $always_visible_reactions = array() ) {
$comment_meta = get_comment_meta( $comment_id );
$all = creactions_get_all_reactions();
$comment_reactions = array();
foreach ( $always_visible_reactions as $always_visible_reaction ) {
if ( isset( $all[ $always_visible_reaction ] ) ) {
$reaction_to_add = $all[ $always_visible_reaction ];
$reaction_to_add['visible'] = 'always';
$comment_reactions[ $always_visible_reaction ] = $reaction_to_add;
}
}
foreach ( $comment_meta as $single_meta => $meta_value ) {
if ( substr( $single_meta, 0, 11 ) == 'creactions_' ) {
$reaction = substr( $single_meta, 11 );
if ( isset( $all[ $reaction ] ) && ! isset( $comment_reactions[ $reaction ] ) ) {
$comment_reactions[ $reaction ] = $all[ $reaction ];
}
}
}
return $comment_reactions;
}
/**
* Comment content filter to show reactions for the comment.
*
* @since 1.0.0
*
* @param string $comment_content The comment text.
* @param WP_Comment $comment The comment.
*/
function creactions_show_after_comment_text( $comment_content, $comment = null ) {
// When comment is posted, the 'itext' filter is called without the second argument.
if ( $comment && ! is_admin() ) {
return $comment_content . creactions_show( $comment->comment_ID );
}
return $comment_content;
}
/**
* Print out reactions for a comment.
*
* @since 1.0.0
*
* @param int $comment_id The ID of the comment.
*/
function creactions_show( $comment_id ) {
$html = '';
// The empty <i> below mysteriously solves the issue of not being able
// to add reactions to a comment with no reactions yet. Somehow without
// it there seems not to be any parents for the reaction button in the
// selector element, so determining the comment id fails.
$html .= '<p class="reactions" data-comment_id="' . esc_attr( $comment_id ) . '"><i></i>';
$reactions_to_show = creactions_get_comment_reactions( $comment_id, creactions_get_visible_reactions() );
foreach ( $reactions_to_show as $reaction_alias => $reaction_info ) {
$count_reactions = get_comment_meta( $comment_id, 'creactions_' . $reaction_alias, true );
if ( empty( $count_reactions ) ) {
$count_reactions = 0;
}
$class = isset( $reaction_info['visible'] ) ? 'reaction-always-visible' : '';
$html .= creactions_single( $reaction_alias, $reaction_info['symbol'], $reaction_info['description'], $count_reactions, $class );
}
/**
* Whether to show the add new button.
*
* @since 1.0.0
*
* @param bool $show_add_new_button To show the add new button or not.
*/
$show_add_new_button = apply_filters( 'creactions_show_add_new_button', true );
if ( $show_add_new_button ) {
$html .= '<span class="all_reactions_wrapper"><button class="show_all_reactions" aria-controls="reactions_all" aria-label="' . esc_attr( __( 'Add new reaction', 'creactions' ) ) . '">😀+</button></span>';
}
$html .= '</p>';
return $html;
}
/**
* Return the HTML of a single Emoji reaction.
*
* @since 1.0.0
*
* @param string $alias The Emoji "slug"
* @param string $alias The Emoji character.
* @param string $alias Text that describes the reaction.
* @param int $count Count of submitted reactions to be shown.
* @param string $class Extra CSS class.
*/
function creactions_single( $alias, $symbol, $description, $count = 0, $class = '' ) {
if ( $class ) {
$class = ' ' . trim( $class );
}
$html = sprintf( '<button class="reaction reaction-%s%s" data-reaction="%s" aria-label="%s">', esc_attr( $alias ), esc_attr( $class ), esc_attr( $alias ), esc_attr( $description ) );
$html .= sprintf( '<span class="reactions-symbol">%s</span>', esc_html( $symbol ) );
$html .= sprintf( '<span class="reactions-description">%s</span>', esc_html( $description ) );
$html .= sprintf( '<span class="reactions-count"%s">', $count <= 0 ? ' style="display:none"' : '' );
$html .= sprintf( '<span class="reactions-num">%d</span></span></button>', $count );
return $html;
}
/**
* Prints out the selector element.
*
* @since 1.0.0
*/
function creactions_selector() {
// Selector is only available on pages that have comments.
if ( ! is_single() || ! comments_open() ) {
return;
}
/** This filter is documented in reactions.php */
$show_add_new_button = apply_filters( 'creactions_show_add_new_button', true );
if ( ! $show_add_new_button ) {
return;
}
// Printed out as a script type="text/html" to avoid loading a huge number of
// images for Emoji replacements on non-supporting browsers.
?><script type="text/html" id="reactions_all_wrapper"><div id="reactions_all" style="display:none;z-index:99"><?php
foreach ( creactions_get_all_reactions() as $reaction_alias => $reaction_info ) {
if ( 'section' == substr( $reaction_alias, 0, 7 ) ) {
?><h2><?php echo esc_html( $reaction_info ) ?></h2><?php
continue;
}
echo creactions_single( $reaction_alias, $reaction_info['symbol'], $reaction_info['description'] );
}
?></div></script><script type="text/template" id="reaction_template">
<button class="reaction reaction-<%= reaction.a %>" data-reaction="<%= reaction.a %>" aria-label="<%= reaction.d %>">
<span class="reactions-symbol"><%= reaction.s %></span>
<span class="reactions-description"><%= reaction.d %></span>
<span class="reactions-count" style="display:none"><span class="reactions-num">0</span></span>
</button>
</script><?php
}
/**
* Enqueue scripts and styles for the plugin.
*/
function creactions_load_script_and_style() {
if ( ! is_singular() ) {
return;
}
$js_suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min.js' : '.js';
wp_enqueue_script( 'creactions', plugin_dir_url( __FILE__ ) . 'comment-reactions' . $js_suffix, array( 'jquery', 'underscore' ), COMMENT_REACTIONS_VERSION, true );
/** This filter is documented in reactions.php */
$show_add_new_button = apply_filters( 'creactions_show_add_new_button', true );
$all_reactions = null;
if ( $show_add_new_button ) {
$all_reactions = creactions_filter_for_brevity( creactions_get_all_reactions() );
}
/**
* Cookie expires in number of days.
*
* @since 0.1.0
*
* @param int $cookie_days The number of days the cookie is set to last.
*/
$cookie_days = apply_filters( 'creactions_cookie_days', 30 );
wp_localize_script(
'creactions',
'Comment_Reactions',
array(
'rest_url' => get_rest_url() . COMMENT_REACTIONS_REST_NAMESPACE,
'cookie_days' => $cookie_days,
'all_reactions' => $all_reactions
)
);
/**
* The CSS URL the plugin enqueues.
*
* @since 0.1.0
*
* @param string|null $src URL of the CSS file. Null if not to be loaded.
*/
$css = apply_filters( 'creactions_css', plugin_dir_url( __FILE__ ) . 'comment-reactions.css' );
if ( $css ) {
wp_enqueue_style( 'creactions', $css, null, COMMENT_REACTIONS_VERSION );
}
}
/**
* Minimizes key names of reactions array to save bandwidth.
*
* @since 1.0.0
*
* @param array $all All the reactions.
*
* @return array $reactions A minimized copy of reactions array.
*/
function creactions_filter_for_brevity( $all ) {
$filtered = array();
foreach ( $all as $alias => $value ) {
$new_value = array( 'a' => $alias, 's' => $value['symbol'], 'd' => $value['description'] );
$filtered[] = $new_value;
}
return $filtered;
}
/**
* Register REST routes.
*
* @since 1.1.0
*/
function creactions_register_rest_routes() {
register_rest_route( COMMENT_REACTIONS_REST_NAMESPACE, '/comment/(?P<id>\d+)', array(
'methods' => 'POST',
'callback' => 'creactions_rest_request_handler',
'args' => array(
'id' => array(
'validate_callback' => function( $param, $request, $key ) {
return null != get_comment( absint( $param ) );
}
),
'reaction' => array(
'validate_callback' => function( $param, $request, $key ) {
return array_key_exists( $param, creactions_get_all_reactions() );
}
),
'action' => array(
'validate_callback' => function( $param, $request, $key ) {
return in_array( $param, array( 'react', 'revert' ) );
}
),
),
) );
}
/**
* Handler for a REST Request.
*
* @since 1.1.0
*
* @todo There is a race condition in counting of the reactions.
*
* @param WP_REST_Request $request The REST Request.
*/
function creactions_rest_request_handler( WP_REST_Request $request ) {
$reaction = sanitize_key( $request->get_param( 'reaction' ) );
$action = sanitize_key( $request->get_param( 'action' ) );
$comment_id = absint( $request->get_param( 'id' ) );
// Get reaction count before the action.
$meta_key = 'creactions_' . $reaction;
$count = get_comment_meta( $comment_id, $meta_key, true );
if ( empty( $count ) ) {
$count = 0;
}
// Figure out the new reaction count.
if ( 'react' == $action ) {
$count = ( int )$count + 1;
} else {
$count = ( int )$count - 1;
}
// Update comment meta accordingly.
if ( $count > 0 ) {
update_comment_meta( $comment_id, $meta_key, $count );
} else {
delete_comment_meta( $comment_id, $meta_key );
}
// Deal with caching.
creactions_clear_caching( $comment_id );
creactions_set_comment_cookie( wp_get_current_user() );
/**
* After submitting a reaction or a revert.
*
* @since 0.1.0
*
* @param string $reaction Reaction (Emoji) alias.
* @param string $action The submitted action, 'react' or 'revert'.
* @param int $comment_id Comment ID.
* @param int $count Count of these reactions on this comment after the execution.
*/
do_action( 'creactions_after_submit', $action, $comment_id, $count );
return new WP_REST_Response( array( 'count' => $count ) );
}
/**
* Clear cache for the post on known big caching plugins.
*
* @since 1.1.0
*
* @param int $comment_id ID of the comment there was a reaction to.
*/
function creactions_clear_caching( $comment_id ) {
$comment = get_comment( $comment_id );
if ( function_exists( 'wp_cache_post_id_gc' ) ) {
wp_cache_post_id_gc( '', $comment->comment_post_ID );
} else if ( function_exists( 'w3tc_pgcache_flush_post' ) ) {
w3tc_pgcache_flush_post( $comment->comment_post_ID );
}
}
/**
* Set comment cookie to deal with potential caching issues.
*
* @since 1.0.0
*
* @param object $user Comment author's object.
*/
function creactions_set_comment_cookie( $user ) {
if ( $user->exists() ) {
return;
}
/** This filter is documented in wp-includes/comment-functions.php */
$comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
$secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
if ( ! isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) {
setcookie( 'comment_author_' . COOKIEHASH, '', time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
}
}