Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit dc3ac9e

Browse files
committed
Merge pull request #30 from dshanske/master
Change Hook on Linkback Fix and Minor Refinements
2 parents 9bfd713 + bb13b0e commit dc3ac9e

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

semantic-linkbacks.php

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function semantic_linkbacks_activation() {
3333
*/
3434
class SemanticLinkbacksPlugin {
3535
/**
36-
* initialize the plugin, registering WordPess hooks.
36+
* initialize the plugin, registering WordPress hooks.
3737
*/
3838
public static function init() {
3939
if (did_action('plugins_loaded')) {
@@ -43,9 +43,7 @@ public static function init() {
4343
}
4444

4545
// hook into linkback functions to add more semantics
46-
add_action('pingback_post', array('SemanticLinkbacksPlugin', 'linkback_fix'));
47-
add_action('trackback_post', array('SemanticLinkbacksPlugin', 'linkback_fix'));
48-
add_action('webmention_post', array('SemanticLinkbacksPlugin', 'linkback_fix'));
46+
add_action('comment_post', array('SemanticLinkbacksPlugin', 'linkback_fix'));
4947
add_filter('pre_get_avatar_data', array('SemanticLinkbacksPlugin', 'pre_get_avatar_data'), 11, 5);
5048
// To extend or to override the default behavior, just use the `comment_text` filter with a lower
5149
// priority (so that it's called after this one) or remove the filters completely in
@@ -57,6 +55,7 @@ public static function init() {
5755
add_filter('get_comment_author_url', array('SemanticLinkbacksPlugin', 'get_comment_author_url'), 99, 3);
5856
add_filter('get_avatar_comment_types', array('SemanticLinkbacksPlugin', 'get_avatar_comment_types'));
5957
add_filter('comment_class', array('SemanticLinkbacksPlugin', 'comment_class'), 10, 4);
58+
6059
}
6160

6261
/**
@@ -85,7 +84,9 @@ public static function linkback_fix($comment_ID) {
8584
if (!$commentdata) {
8685
return $comment_ID;
8786
}
88-
87+
if ($commentdata['comment_type']=="") {
88+
return $comment_ID;
89+
}
8990
// source
9091
$source = esc_url_raw($commentdata['comment_author_url']);
9192

@@ -239,6 +240,22 @@ public static function get_post_format_strings() {
239240
return $strings;
240241
}
241242

243+
/**
244+
* return correct URL
245+
*
246+
* @param WP_Comment $comment the comment object
247+
* @return string the URL
248+
*/
249+
public static function get_url($comment = null) {
250+
// get URL canonical url...
251+
$semantic_linkbacks_canonical = get_comment_meta($comment->comment_ID, "semantic_linkbacks_canonical", true);
252+
// ...or fall back to source
253+
if (!$semantic_linkbacks_canonical) {
254+
$semantic_linkbacks_canonical = get_comment_meta($comment->comment_ID, "semantic_linkbacks_source", true);
255+
}
256+
return $semantic_linkbacks_canonical;
257+
}
258+
242259
/**
243260
* add cite to "reply"s
244261
*
@@ -251,30 +268,22 @@ public static function get_post_format_strings() {
251268
*/
252269
public static function comment_text_add_cite($text, $comment = null, $args = array()) {
253270
$semantic_linkbacks_type = get_comment_meta($comment->comment_ID, "semantic_linkbacks_type", true);
254-
255271
// only change text for "real" comments (replys)
256272
if (!$comment ||
257273
!$semantic_linkbacks_type ||
258274
$comment->comment_type != "" ||
259275
$semantic_linkbacks_type != "reply") {
260276
return $text;
261277
}
262-
263-
// get URL canonical url...
264-
$semantic_linkbacks_canonical = get_comment_meta($comment->comment_ID, "semantic_linkbacks_canonical", true);
265-
// ...or fall back to source
266-
if (!$semantic_linkbacks_canonical) {
267-
$semantic_linkbacks_canonical = get_comment_meta($comment->comment_ID, "semantic_linkbacks_source", true);
268-
}
269-
270-
$host = parse_url($semantic_linkbacks_canonical, PHP_URL_HOST);
278+
$url = self::get_url($comment);
279+
$host = parse_url($url, PHP_URL_HOST);
271280

272281
// strip leading www, if any
273282
$host = preg_replace("/^www\./", "", $host);
274283
// note that WordPress's sanitization strips the class="u-url". sigh. :/ also,
275284
// <cite> is one of the few elements that make it through the sanitization and
276285
// is still uncommon enough that we can use it for styling.
277-
$text .= '<p><small>&mdash;&nbsp;<cite><a class="u-url" href="' . $semantic_linkbacks_canonical . '">via ' . $host . '</a></cite></small></p>';
286+
$text .= '<p><small>&mdash;&nbsp;<cite><a class="u-url" href="' . $url . '">via ' . $host . '</a></cite></small></p>';
278287

279288
return apply_filters("semantic_linkbacks_cite", $text);
280289
}
@@ -301,33 +310,32 @@ public static function comment_text_excerpt($text, $comment = null, $args = arra
301310
if (!in_array($semantic_linkbacks_type, array_keys(self::get_comment_type_strings()))) {
302311
$semantic_linkbacks_type = "mention";
303312
}
313+
if (current_theme_supports('post-formats') ) {
314+
$post_format = get_post_format($comment->comment_post_ID);
315+
// replace "standard" with "Article"
316+
if (!$post_format || !in_array($post_format, array_keys(self::get_post_format_strings()))) {
317+
$post_format = "standard";
318+
}
304319

305-
$post_format = get_post_format($comment->comment_post_ID);
306-
307-
// replace "standard" with "Article"
308-
if (!$post_format || !in_array($post_format, array_keys(self::get_post_format_strings()))) {
309-
$post_format = "standard";
320+
$post_formatstrings = self::get_post_format_strings();
321+
$post_type = $post_formatstrings[$post_format];
310322
}
311-
312-
$post_formatstrings = self::get_post_format_strings();
323+
else {
324+
$post_type = __('this post', 'semantic_linkbacks');
325+
$post_type = apply_filters('semantic_linkbacks_post_type', $post_type, $comment->comment_post_ID); }
313326

314327
// get all the excerpts
315328
$comment_type_excerpts = self::get_comment_type_excerpts();
316329

317-
// get URL canonical url...
318-
$semantic_linkbacks_canonical = get_comment_meta($comment->comment_ID, "semantic_linkbacks_canonical", true);
319-
// ...or fall back to source
320-
if (!$semantic_linkbacks_canonical) {
321-
$semantic_linkbacks_canonical = get_comment_meta($comment->comment_ID, "semantic_linkbacks_source", true);
322-
}
330+
$url = self::get_url($comment);
323331

324332
// parse host
325-
$host = parse_url($semantic_linkbacks_canonical, PHP_URL_HOST);
333+
$host = parse_url($url, PHP_URL_HOST);
326334
// strip leading www, if any
327335
$host = preg_replace("/^www\./", "", $host);
328336

329337
// generate output
330-
$text = sprintf($comment_type_excerpts[$semantic_linkbacks_type], get_comment_author_link($comment->comment_ID), $post_formatstrings[$post_format], $semantic_linkbacks_canonical, $host);
338+
$text = sprintf($comment_type_excerpts[$semantic_linkbacks_type], get_comment_author_link($comment->comment_ID), $post_type, $url, $host);
331339

332340
return apply_filters("semantic_linkbacks_excerpt", $text);
333341
}
@@ -448,6 +456,7 @@ public static function get_avatar_comment_types($types) {
448456

449457
return $types;
450458
}
459+
451460
}
452461

453462
/**

0 commit comments

Comments
 (0)