forked from pfefferle/wordpress-semantic-linkbacks
/
class-linkbacks-handler.php
executable file
·674 lines (596 loc) · 23 KB
/
class-linkbacks-handler.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
<?php
// Mentions with content less than this length will be rendered inline.
define( 'MAX_INLINE_MENTION_LENGTH', 300 );
/**
* Semantic linkbacks class
*
* @author Matthias Pfefferle
*/
class Linkbacks_Handler {
/**
* Initialize the plugin, registering WordPress hooks.
*/
public static function init() {
/* Add meta boxes on the 'add_meta_boxes' hook. */
add_action( 'add_meta_boxes', array( 'Linkbacks_Handler', 'add_meta_boxes' ) );
// enhance linkbacks
add_filter( 'preprocess_comment', array( 'Linkbacks_Handler', 'enhance' ), 0, 1 );
add_filter( 'wp_update_comment_data', array( 'Linkbacks_Handler', 'enhance' ), 11, 3 );
// Updates Comment Meta if set in commentdata
add_action( 'edit_comment', array( 'Linkbacks_Handler', 'update_meta' ), 10, 2 );
add_action( 'edit_comment', array( 'Linkbacks_Handler', 'save_comment_meta' ) );
// To extend or to override the default behavior, just use the `comment_text` filter with a lower
// priority (so that it's called after this one) or remove the filters completely in
// your code: `remove_filter('comment_text', array('Linkbacks_Handler', 'comment_text_add_cite'), 11);`
if ( ! self::render_comments() ) {
add_filter( 'get_comment_text', array( 'Linkbacks_Handler', 'comment_text_add_cite' ), 11, 3 );
}
add_filter( 'get_comment_text', array( 'Linkbacks_Handler', 'comment_text_excerpt' ), 12, 3 );
add_filter( 'comment_excerpt', array( 'Linkbacks_Handler', 'comment_text_excerpt' ), 5, 2 );
add_filter( 'get_comment_link', array( 'Linkbacks_Handler', 'get_comment_link' ), 99, 3 );
add_filter( 'get_comment_author_url', array( 'Linkbacks_Handler', 'get_comment_author_url' ), 99, 3 );
add_filter( 'comment_class', array( 'Linkbacks_Handler', 'comment_class' ), 10, 4 );
add_filter( 'wp_list_comments_args', array( 'Linkbacks_Handler', 'filter_comment_args' ) );
add_action( 'comment_form_before', array( 'Linkbacks_Handler', 'show_mentions' ) );
add_action( 'comment_form_comments_closed', array( 'Linkbacks_Handler', 'show_mentions' ) );
// Domain Approval Check
add_filter( 'semantic_linkbacks_commentdata', array( 'Linkbacks_Handler', 'domain_approval_check' ), 99, 1 );
// Register Meta Keys
self::register_meta();
}
/**
* Create a meta boxes to be displayed on the comment editor screen.
*/
public static function add_meta_boxes() {
add_meta_box(
'semantic-linkbacks-meta',
esc_html__( 'Semantic Linkbacks Data', 'semantic-linkbacks' ),
array( 'Linkbacks_Handler', 'comment_metabox' ),
'comment',
'normal',
'default'
);
}
/**
* Load template for comment metabox.
*/
public static function comment_metabox() {
load_template( dirname( __FILE__ ) . '/../templates/linkbacks-edit-comment-form.php' );
}
/**
* Saves Comment Meta Options
*
* @param int $comment_id
*
*/
public static function save_comment_meta( $comment_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
return;
}
if ( ! empty( $_POST['semantic_linkbacks_type'] ) ) {
update_comment_meta( $comment_id, 'semantic_linkbacks_type', $_POST['semantic_linkbacks_type'] );
}
if ( ! empty( $_POST['semantic_linkbacks_avatar'] ) ) {
update_comment_meta( $comment_id, 'semantic_linkbacks_avatar', $_POST['semantic_linkbacks_avatar'] );
} else {
delete_comment_meta( $comment_id, 'semantic_linkbacks_avatar' );
}
}
/**
* Filter the comments and ignore every comment other than 'comment' and 'mention'
*
* @param array $args an array of arguments for displaying comments
*
* @return array the filtered array
*/
public static function filter_comment_args( $args ) {
$args['walker'] = new Semantic_Linkbacks_Walker_Comment();
return $args;
}
/**
* Filter whether to override comment presentation.
* To use the default html5_comment set this filter to false
*
* @return boolean
*/
public static function render_comments() {
return apply_filters( 'semantic_linkbacks_render_comments', ! current_theme_supports( 'microformats2' ) );
}
/**
*
*
*/
public static function show_mentions() {
// If this filter is set to false then hide the template and hide the option. This should be used by themes
if ( apply_filters( 'semantic_linkbacks_facepile', true ) ) {
load_template( plugin_dir_path( dirname( __FILE__ ) ) . 'templates/linkbacks.php' );
}
}
/**
* This is more to lay out the data structure than anything else.
*/
public static function register_meta() {
$args = array(
'sanitize_callback' => 'esc_url_raw',
'type' => 'string',
'description' => 'Author URL',
'single' => true,
'show_in_rest' => true,
);
register_meta( 'comment', 'semantic_linkbacks_author_url', $args );
$args = array(
'sanitize_callback' => 'esc_url_raw',
'type' => 'string',
'description' => 'Avatar URL',
'single' => true,
'show_in_rest' => true,
);
register_meta( 'comment', 'semantic_linkbacks_avatar', $args );
$args = array(
'sanitize_callback' => 'esc_url_raw',
'type' => 'string',
'description' => 'Canonical URL',
'single' => true,
'show_in_rest' => true,
);
register_meta( 'comment', 'semantic_linkbacks_canonical', $args );
$args = array(
'type' => 'string',
'description' => 'Linkbacks Type',
'single' => true,
'show_in_rest' => true,
);
register_meta( 'comment', 'semantic_linkbacks_type', $args );
}
/**
* Update an Enhanced Comment
*/
public static function enhance( $commentdata, $comment = array(), $commentarr = array() ) {
// check if comment is a linkback
if ( ! in_array( $commentdata['comment_type'], array( 'webmention', 'pingback', 'trackback', 'refback' ), true ) ) {
return $commentdata;
}
// only run the enhancer if `remote_source_original` is set
if ( empty( $commentdata['remote_source_original'] ) ) {
return $commentdata;
}
// initialize comment_meta array
if ( ! array_key_exists( 'comment_meta', $commentdata ) ) {
$commentdata['comment_meta'] = array();
}
// If target is not set then set based on permalink
if ( ! isset( $commentdata['target'] ) ) {
$commentdata['target'] = get_permalink( $commentdata['comment_post_ID'] );
}
// add replytocom if present
if ( isset( $commentdata['comment_parent'] ) && ! empty( $commentdata['comment_parent'] ) ) {
$commentdata['target'] = add_query_arg( array( 'replytocom' => $commentdata['comment_parent'] ), $commentdata['target'] );
}
// add source url as comment-meta
$commentdata['comment_meta']['semantic_linkbacks_source'] = esc_url_raw( $commentdata['comment_author_url'] );
// adds a hook to enable some other semantic handlers for example schema.org
$commentdata = apply_filters( 'semantic_linkbacks_commentdata', $commentdata );
// remove "webmention" comment-type if $type is "reply"
if ( isset( $commentdata['comment_meta']['semantic_linkbacks_type'] ) ) {
if ( in_array( $commentdata['comment_meta']['semantic_linkbacks_type'], apply_filters( 'semantic_linkbacks_comment_types', array( 'reply' ) ), true ) ) {
$commentdata['comment_type'] = '';
}
}
return wp_unslash( $commentdata );
}
/**
* Use the whitelist check function to approve a comment if the source domain is on the whitelist.
*
* @param array $commentdata
* @return array $commentdata
*/
public static function domain_approval_check( $commentdata ) {
if ( ! $commentdata || is_wp_error( $commentdata ) || ! function_exists( 'is_webmention_source_whitelisted' ) || ! isset( $commentdata['semantic_linkbacks_canonical'] ) ) {
return $commentdata;
}
// Check if the canonical URL is on the whitelist
if ( is_webmention_source_whitelisted( $commentdata['semantic_linkbacks_canonical'] ) ) {
$commentdata['comment_approved'] = 1;
}
return $commentdata;
}
/**
* Retrieve Remote Source
*
* @param string $url URL to Retrieve
* @return remote source
*/
public static function retrieve( $url ) {
global $wp_version;
$user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) );
$args = array(
'timeout' => 100,
'limit_response_size' => 1048576,
'redirection' => 20,
'user-agent' => "$user_agent; verifying linkback",
);
return wp_safe_remote_get( $url, $args );
}
/**
* Save Meta - to Match the core functionality in wp_insert_comment.
* To be Removed if This Functionality Hits Core.
*
* @param array $commentdata The new comment data
* @param array $comment The old comment data
*/
public static function update_meta( $comment_id, $commentdata ) {
// If metadata is provided, store it.
if ( isset( $commentdata['comment_meta'] ) && is_array( $commentdata['comment_meta'] ) ) {
foreach ( $commentdata['comment_meta'] as $meta_key => $meta_value ) {
update_comment_meta( $comment_id, $meta_key, $meta_value, true );
}
}
}
/**
* Returns an array of comment type excerpts to their translated and pretty display versions
*
* @return array The array of translated post format excerpts.
*/
public static function get_comment_type_excerpts() {
$strings = array(
// translators: Name verb on domain
'mention' => __( '%1$s mentioned %2$s on <a href="%3$s">%4$s</a>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'reply' => __( '%1$s replied to %2$s on <a href="%3$s">%4$s</a>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'repost' => __( '%1$s reposted %2$s on <a href="%3$s">%4$s</a>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'like' => __( '%1$s liked %2$s on <a href="%3$s">%4$s</a>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'favorite' => __( '%1$s favorited %2$s on <a href="%3$s">%4$s</a>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'tag' => __( '%1$s tagged %2$s on <a href="%3$s">%4$s</a>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'bookmark' => __( '%1$s bookmarked %2$s on <a href="%3$s">%4$s</a>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'rsvp:yes' => __( '%1$s is <strong>attending</strong>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'rsvp:no' => __( '%1$s is <strong>not attending</strong>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'rsvp:maybe' => __( 'Maybe %1$s will be <strong>attending</strong>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'rsvp:interested' => __( '%1$s is <strong>interested</strong> in this event.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'invited' => __( '%1$s is <strong>invited</strong>.', 'semantic-linkbacks' ),
// translators: Name verb on domain
'listen' => __( '%1$s <strong>listened</strong> to %2$s (via <a href="%3$s">%4$s</a>).', 'semantic-linkbacks' ),
// translators: Name verb on domain
'watch' => __( '%1$s <strong>watched</strong> %2$s (via <a href="%3$s">%4$s</a>).', 'semantic-linkbacks' ),
// translators: Name verb on domain
'read' => __( '%1$s <strong>read</strong> %2$s (via <a href="%3$s">%4$s</a>).', 'semantic-linkbacks' ),
// translators: Name verb on domain
'follow' => __( '%1$s <strong>followed</strong> %2$s (via <a href="%3$s">%4$s</a>).', 'semantic-linkbacks' ),
);
return $strings;
}
/**
* Returns an array of comment type slugs to their translated and pretty display versions
*
* @param $type Return only one type. All if null
* @return array The array of translated comment type names.
*/
public static function get_comment_type_strings( $type = null ) {
$strings = array(
// Special case. any value that evals to false will be considered standard
'mention' => __( 'Mention', 'semantic-linkbacks' ),
'reply' => __( 'Reply', 'semantic-linkbacks' ),
'repost' => __( 'Repost', 'semantic-linkbacks' ),
'like' => __( 'Like', 'semantic-linkbacks' ),
'favorite' => __( 'Favorite', 'semantic-linkbacks' ),
'tag' => __( 'Tag', 'semantic-linkbacks' ),
'bookmark' => __( 'Bookmark', 'semantic-linkbacks' ),
'rsvp:yes' => __( 'RSVP Yes', 'semantic-linkbacks' ),
'rsvp:no' => __( 'RSVP No', 'semantic-linkbacks' ),
'rsvp:maybe' => __( 'RSVP Maybe', 'semantic-linkbacks' ),
'rsvp:interested' => __( 'RSVP Interested', 'semantic-linkbacks' ),
'invited' => __( 'Invited', 'semantic-linkbacks' ),
'listen' => __( 'Listen', 'semantic-linkbacks' ),
'watch' => __( 'Watch', 'semantic-linkbacks' ),
'read' => __( 'Read', 'semantic-linkbacks' ),
'follow' => __( 'Follow', 'semantic-linkbacks' ),
'reacji' => __( 'Reacji', 'semantic-linkbacks' ),
);
if ( ! $type ) {
return $strings;
}
return isset( $strings[ $type ] ) ? $strings[ $type ] : $strings['mention'];
}
public static function comment_type_select( $type, $echo = false ) {
$choices = self::get_comment_type_strings();
$return = '';
foreach ( $choices as $value => $text ) {
$return .= sprintf( '<option value=%1s %2s>%3s</option>', $value, selected( $type, $value, false ), $text );
}
if ( ! $echo ) {
return $return;
}
echo $return;
}
/**
* Returns an array of post types (with articles)
*
* @return array The array of translated comment type names.
*/
public static function get_post_type_strings() {
$strings = array(
// Generic Post Types
'post' => __( 'this Post', 'semantic-linkbacks' ),
'page' => __( 'this Page', 'semantic-linkbacks' ),
// Post Format Types
'standard' => __( 'this Article', 'semantic-linkbacks' ),
'aside' => __( 'this Aside', 'semantic-linkbacks' ),
'chat' => __( 'this Chat', 'semantic-linkbacks' ),
'gallery' => __( 'this Gallery', 'semantic-linkbacks' ),
'link' => __( 'this Link', 'semantic-linkbacks' ),
'image' => __( 'this Image', 'semantic-linkbacks' ),
'quote' => __( 'this Quote', 'semantic-linkbacks' ),
'status' => __( 'this Status', 'semantic-linkbacks' ),
'video' => __( 'this Video', 'semantic-linkbacks' ),
'audio' => __( 'this Audio', 'semantic-linkbacks' ),
);
return $strings;
}
/**
* Return correct URL
*
* @param WP_Comment|int $comment the comment object or comment ID
*
* @return string the URL
*/
public static function get_url( $comment ) {
if ( is_numeric( $comment ) ) {
$comment = get_comment( $comment );
}
// get canonical url...
$semantic_linkbacks_canonical = get_comment_meta( $comment->comment_ID, 'semantic_linkbacks_canonical', true );
// ...or fall back to source
if ( ! $semantic_linkbacks_canonical ) {
$semantic_linkbacks_canonical = get_comment_meta( $comment->comment_ID, 'semantic_linkbacks_source', true );
}
// ...or author url
if ( ! $semantic_linkbacks_canonical ) {
$semantic_linkbacks_canonical = $comment->comment_author_url;
}
return $semantic_linkbacks_canonical;
}
/**
* Return canonical URL
*
* @param int|WP_Comment $comment Comment
*
* @return string the URL
*/
public static function get_canonical_url( $comment ) {
if ( is_numeric( $comment ) ) {
$comment = get_comment( $comment );
}
// get canonical url...
return get_comment_meta( $comment->comment_ID, 'semantic_linkbacks_canonical', true );
}
/**
* Return type
*
* @param int|WP_Comment $comment Comment
*
* @return string the type
*/
public static function get_type( $comment ) {
if ( $comment instanceof WP_Comment ) {
$comment = $comment->comment_ID;
}
// get type...
return get_comment_meta( $comment, 'semantic_linkbacks_type', true );
}
/**
* Return property
*
* @param int|WP_Comment $comment Comment
* @param string $key Key
*
* @return boolean|string|array Property or False if Property Does Not Exist
*/
public static function get_prop( $comment, $key ) {
if ( $comment instanceof WP_Comment ) {
$comment = $comment->comment_ID;
}
$prop = get_comment_meta( $comment, $key );
if ( ! $prop || is_string( $prop ) ) {
return $prop;
}
if ( 1 === count( $prop ) ) {
return array_shift( $prop );
}
return $prop;
}
/**
* Return author URL
*
* @param int|WP_Comment $comment Comment
*
* @return string the URL
*/
public static function get_author_url( $comment ) {
if ( $comment instanceof WP_Comment ) {
$comment = $comment->comment_ID;
}
// get author URL...
return get_comment_meta( $comment, 'semantic_linkbacks_author_url', true );
}
/**
* Add cite to "reply"s
*
* thanks to @snarfed for the idea
*
* @param string $text the comment text
* @param WP_Comment $comment the comment object
* @param array $args a list of arguments
*
* @return string the filtered comment text
*/
public static function comment_text_add_cite( $text, $comment = null, $args = array() ) {
if ( ! $comment ) {
return $text;
}
$semantic_linkbacks_type = self::get_type( $comment );
// only change text for "real" comments (replys)
if ( ! $semantic_linkbacks_type ||
'' !== $comment->comment_type ||
'reply' !== $semantic_linkbacks_type ) {
return $text;
}
$url = self::get_url( $comment );
$host = wp_parse_url( $url, PHP_URL_HOST );
// strip leading www, if any
$host = preg_replace( '/^www\./', '', $host );
// note that WordPress's sanitization strips the class="u-url". sigh. :/ also,
// <cite> is one of the few elements that make it through the sanitization and
// is still uncommon enough that we can use it for styling.
$cite = apply_filters( 'semantic_linkbacks_cite', '<p><cite><a class="u-url" href="%1s">via %2s</a></cite></p>' );
return $text . sprintf( $cite, $url, $host );
}
/**
* Returns a post type that is used to generate text. This can be from post formats/kinds/post type/etc
*
* @param int $post_id Post ID
*
* @return string the post type
*/
public static function get_post_type( $post_id ) {
$post_typestrings = self::get_post_type_strings();
$post_type = $post_typestrings['post'];
// If this is the page homepages are redirected to then use the site name
if ( get_option( 'webmention_home_mentions', 0 ) === $post_id ) {
$post_type = get_bloginfo( 'name' );
} elseif ( 'page' === get_post_type( $post_id ) ) {
$post_type = $post_typestrings['page'];
} elseif ( current_theme_supports( 'post-formats' ) ) {
$post_format = get_post_format( $post_id );
// add "standard" as default for post format enabled types
if ( ! $post_format || ! in_array( $post_format, array_keys( $post_typestrings ), true ) ) {
$post_format = 'standard';
}
$post_type = $post_typestrings[ $post_format ];
}
return apply_filters( 'semantic_linkbacks_post_type', $post_type, $post_id );
}
/**
* Generate excerpt for all types except "reply"
*
* @param string $text the comment text
* @param WP_Comment $comment the comment object
* @param array $args a list of arguments
*
* @return string the filtered comment text
*/
public static function comment_text_excerpt( $text, $comment = null, $args = array() ) {
if ( ! is_object( $comment ) ) {
$comment = get_comment( $comment );
}
if ( ! $comment ) {
return $text;
}
$semantic_linkbacks_type = self::get_type( $comment );
// only change text for pingbacks/trackbacks/webmentions
if ( '' === $comment->comment_type ||
! $semantic_linkbacks_type ||
'reply' === $semantic_linkbacks_type ) {
return $text;
}
// check semantic linkback type
if ( ! in_array( $semantic_linkbacks_type, array_keys( self::get_comment_type_strings() ), true ) ) {
$semantic_linkbacks_type = 'mention';
}
$post_type = self::get_post_type( $comment->comment_post_ID );
// get all the excerpts
$comment_type_excerpts = self::get_comment_type_excerpts();
$url = self::get_url( $comment );
// parse host
$host = wp_parse_url( $url, PHP_URL_HOST );
// strip leading www, if any
$host = preg_replace( '/^www\./', '', $host );
// generate output. use full content if it's small enough, otherwise use excerpt.
$text_len = mb_strlen( html_entity_decode( $text, ENT_QUOTES ) );
if ( ! ( 'mention' === $semantic_linkbacks_type && $text_len <= MAX_INLINE_MENTION_LENGTH ) ) {
$text = sprintf( $comment_type_excerpts[ $semantic_linkbacks_type ], get_comment_author_link( $comment->comment_ID ), $post_type, $url, $host );
}
return apply_filters( 'semantic_linkbacks_excerpt', $text );
}
/**
* Replace comment url with canonical url
*
* @param string $link the link url
* @param WP_Comment $comment the comment object
* @param array $args a list of arguments to generate the final link tag
*
* @return string the linkback source or the original comment link
*/
public static function get_comment_link( $link, $comment, $args ) {
$semantic_linkbacks_canonical = self::get_canonical_url( $comment );
if ( is_singular() && $semantic_linkbacks_canonical ) {
return $semantic_linkbacks_canonical;
}
return $link;
}
/**
* Replace comment url with author url
*
* @param string $url The comment author's URL.
* @param int $comment_ID The comment ID.
* @param WP_Comment $comment The comment object.
*
* @return string the replaced/parsed author url or the original comment link
*/
public static function get_comment_author_url( $url, $id, $comment ) {
$author_url = self::get_author_url( $comment );
if ( $author_url ) {
return $author_url;
}
return $url;
}
/**
* Add comment classes from `semantic_linkbacks_type`s
*
* @return array the extended comment classes as array
*/
public static function comment_class( $classes, $class, $comment_id, $post_id ) {
// get comment
$comment = get_comment( $comment_id );
// "comment type to class" mapper
$class_mapping = array(
'mention' => array( 'u-mention' ),
'reply' => array( 'u-comment' ),
'repost' => array( 'u-repost' ),
'like' => array( 'u-like' ),
'favorite' => array( 'u-favorite' ),
'tag' => array( 'u-tag' ),
'bookmark' => array( 'u-bookmark' ),
'rsvp:yes' => array( 'u-rsvp' ),
'rsvp:no' => array( 'u-rsvp' ),
'rsvp:maybe' => array( 'u-rsvp' ),
'rsvp:interested' => array( 'u-rsvp' ),
'invited' => array( 'u-invitee' ),
'listen' => array( 'u-listen' ),
'read' => array( 'u-read' ),
'watch' => array( 'u-watch' ),
'follow' => array( 'u-follow' ),
);
$semantic_linkbacks_type = self::get_type( $comment );
// check the comment type
if ( $semantic_linkbacks_type && isset( $class_mapping[ $semantic_linkbacks_type ] ) ) {
$classes = array_merge( $classes, $class_mapping[ $semantic_linkbacks_type ] );
$classes = array_unique( $classes );
}
$classes[] = 'h-cite';
return $classes;
}
}