From aa4d880148ea9bd62a973e24c3c2632e0b82f42c Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Thu, 22 Oct 2015 19:29:04 +0000 Subject: [PATCH] Search in sub-items for links to the target as well --- plugins/Linkback/lib/util.php | 60 +++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/plugins/Linkback/lib/util.php b/plugins/Linkback/lib/util.php index cf3f55c5ff..694dd84b0d 100644 --- a/plugins/Linkback/lib/util.php +++ b/plugins/Linkback/lib/util.php @@ -61,6 +61,41 @@ function linkback_get_target($target) { return NULL; } +function linkback_is_contained_in($entry, $target) { + foreach ((array)$entry['properties'] as $key => $values) { + if(count(array_filter($values, function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0) { + return $entry['properties']; + } + + // check included h-* formats and their links + foreach ($values as $obj) { + if(isset($obj['type']) && array_intersect(array('h-cite', 'h-entry'), $obj['type']) && + isset($obj['properties']) && isset($obj['properties']['url']) && + count(array_filter($obj['properties']['url'], + function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0 + ) { + return $entry['properties']; + } + } + + // check content for the link + if ($key == "content" && preg_match_all("/]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]['html']), $context)) { + return $entry['properties']; + // check summary for the link + } elseif ($key == "summary" && preg_match_all("/]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]), $context)) { + return $entry['properties']; + } + } + + foreach((array)$entry['children'] as $mf2) { + if(linkback_is_contained_in($mf2, $target)) { + return $entry['properties']; + } + } + + return null; +} + // Based on https://github.com/acegiak/Semantic-Linkbacks/blob/master/semantic-linkbacks-microformats-handler.php, GPL-2.0+ function linkback_find_entry($mf2, $target) { if(isset($mf2['items'][0]['type']) && in_array("h-feed", $mf2['items'][0]["type"]) && isset($mf2['items'][0]['children'])) { @@ -70,29 +105,8 @@ function linkback_find_entry($mf2, $target) { $entries = array_filter($mf2['items'], function($x) { return isset($x['type']) && in_array('h-entry', $x['type']); }); foreach ($entries as $entry) { - foreach ((array)$entry['properties'] as $key => $values) { - if(count(array_filter($values, function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0) { - return $entry['properties']; - } - - // check included h-* formats and their links - foreach ($values as $obj) { - if(isset($obj['type']) && array_intersect(array('h-cite', 'h-entry'), $obj['type']) && - isset($obj['properties']) && isset($obj['properties']['url']) && - count(array_filter($obj['properties']['url'], - function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0 - ) { - return $entry['properties']; - } - } - - // check content for the link - if ($key == "content" && preg_match_all("/]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]['html']), $context)) { - return $entry['properties']; - // check summary for the link - } elseif ($key == "summary" && preg_match_all("/]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]), $context)) { - return $entry['properties']; - } + if($prop = linkback_is_contained_in($entry, $target)) { + return $prop; } } -- 2.39.5