Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions includes/Handler/class-mf2.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ protected function set_property_author( $properties ) {
foreach ( array( 'name', 'nickname', 'given-name', 'family-name', 'url', 'email', 'photo' ) as $prop ) {
$author[ $prop ] = $this->get_plaintext( $properties, $prop );
}

// If name is not available, use nickname or the combination of given name and family name as fallback
if ( empty( $author['name'] ) ) {
if ( ! empty( $author['nickname'] ) ) {
$author['name'] = $author['nickname'];
} elseif ( ! empty( $author['given-name'] ) || ! empty( $author['family-name'] ) ) {
$author['name'] = implode( ' ', array_filter( array( $author['given-name'], $author['family-name'] ) ) );
}
}
}

$this->webmention_item->add_author( array_filter( $author ) );
Expand Down Expand Up @@ -391,13 +400,18 @@ public function find_representative_hcard( $mf_array, $url ) {
*
* @return array the h-entry node or false
*/
public function find_representative_item( $mf_array, $target ) {
public function find_representative_item( $mf_array, $target, $ignore_hcards = true ) {
$items = $this->get_items( $mf_array );
if ( ! is_array( $items ) || empty( $items ) ) {
return false;
}

foreach ( $items as $item ) {
// we do not want to check h-cards as main items as they are not the content
if ( $this->is_type( $item, 'h-card' ) && $ignore_hcards ) {
continue;
}

// check properties
if ( isset( $item['properties'] ) ) {
// check properties if target urls was mentioned
Expand Down Expand Up @@ -459,13 +473,13 @@ public function find_representative_item( $mf_array, $target ) {
'content' === $key &&
! empty( $value['html'] ) &&
is_string( $value['html'] ) &&
preg_match_all( '/<a[^>]+?' . preg_quote( $target, '/' ) . '[^>]*>([^>]+?)<\/a>/i', $value['html'], $context )
preg_match_all( '/<a[^>]*href\s*=\s*["\']?' . preg_quote( $target, '/' ) . '["\']?/i', $value['html'] )
) {
return $item;
} elseif (
'summary' === $key &&
is_string( $value ) &&
preg_match_all( '/<a[^>]+?' . preg_quote( $target, '/' ) . '[^>]*>([^>]+?)<\/a>/i', $value, $context )
preg_match_all( '/<a[^>]*href\s*=\s*["\']?' . preg_quote( $target, '/' ) . '["\']?/i', $value )
) {
return $item;
}
Expand All @@ -488,6 +502,10 @@ public function find_representative_item( $mf_array, $target ) {
*/
protected function get_representative_item( $mf_array, $url ) {
$item = $this->find_representative_item( $mf_array, $url );
if ( empty( $item ) || ! is_array( $item ) ) {
$item = $this->find_representative_item( $mf_array, $url, false );
}

if ( empty( $item ) || ! is_array( $item ) ) {
return array();
}
Expand Down Expand Up @@ -797,13 +815,13 @@ protected function get_response_type( $entry, $mf_array, $target ) {
'content' === $obj_key &&
! empty( $obj_value['html'] ) &&
is_string( $obj_value['html'] ) &&
preg_match_all( '/<a[^>]+?' . preg_quote( $target, '/' ) . '[^>]*>([^>]+?)<\/a>/i', $obj_value['html'], $context )
preg_match_all( '/<a[^>]*href\s*=\s*["\']?' . preg_quote( $target, '/' ) . '["\']?/i', $obj_value['html'] )
) {
return $classes[ $key ];
} elseif (
'summary' === $obj_key &&
is_string( $obj_value ) &&
preg_match_all( '/<a[^>]+?' . preg_quote( $target, '/' ) . '[^>]*>([^>]+?)<\/a>/i', $obj_value, $context )
preg_match_all( '/<a[^>]*href\s*=\s*["\']?' . preg_quote( $target, '/' ) . '["\']?/i', $obj_value )
) {
return $classes[ $key ];
}
Expand Down
90 changes: 90 additions & 0 deletions tests/data/mf2/brid-gy-mastodon.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0;url=https://aseachange.com/@elena/statuses/01JZNGNC6TXX9QYZ3Q13ZK31C7">
<title><p><span class="h-card"><a href="https://mastodon.social/@pfefferle" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>pfefferle</span></a></span> such a fast solution wow! 🏆✨ congrats all</p><p><span class="h-card"><a href="https://social.wake.st/@liaizon" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>liaizon</span></a></span> <span class="h-card"><a href="https://snac.rohrmoser.name/social/wake_st" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>wake_st</span></a></span> <span class="h-card"><a href="https://notiz.blog/author/matthias-pfefferle/" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>pfefferle</span></a></span> <span class="h-card"><a href="https://comam.es/snac/grunfink" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>grunfink</span></a></span></p></title>
<style type="text/css">
body {
display: none;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
.p-uid {
display: none;
}
.u-photo {
max-width: 50px;
border-radius: 4px;
}
.e-content {
margin-top: 10px;
font-size: 1.3em;
}
</style>
</head>
<article class="h-entry">
<span class="p-uid">https://aseachange.com/users/elena/statuses/01JZNGNC6TXX9QYZ3Q13ZK31C7</span>

<time class="dt-published" datetime="2025-07-08T17:17:15+00:00">2025-07-08T17:17:15+00:00</time>

<span class="p-author h-card">
<data class="p-uid" value="https://aseachange.com/users/elena"></data>
<data class="p-numeric-id" value="113695878928179087"></data>
<a class="p-name u-url" href="https://aseachange.com/@elena">Elena Rossini on GoToSocial ⁂</a>
<a class="u-url" href="https://news.elenarossini.com"></a>
<a class="u-url" href="https://elena.social"></a>
<span class="p-nickname">elena</span>
<img class="u-photo" src="https://files.mastodon.social/cache/accounts/avatars/113/695/878/928/179/087/original/edadee646dc7593c.jpeg" alt="" />
</span>

<a title="aseachange.com/@elena/statuses/01JZNGNC6TXX9QYZ3Q13ZK31C7" class="u-url" href="https://aseachange.com/@elena/statuses/01JZNGNC6TXX9QYZ3Q13ZK31C7">aseachange.com/@elena/statuse...</a>
<div class="e-content p-name">

<p><span class="h-card"><a href="https://mastodon.social/@pfefferle" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>pfefferle</span></a></span> such a fast solution wow! 🏆✨ congrats all</p><p><span class="h-card"><a href="https://social.wake.st/@liaizon" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>liaizon</span></a></span> <span class="h-card"><a href="https://snac.rohrmoser.name/social/wake_st" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>wake_st</span></a></span> <span class="h-card"><a href="https://notiz.blog/author/matthias-pfefferle/" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>pfefferle</span></a></span> <span class="h-card"><a href="https://comam.es/snac/grunfink" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>grunfink</span></a></span></p>
</div>




<span class="u-category h-card">
<data class="p-uid" value="https://mastodon.social/users/pfefferle"></data>
<a class="p-name u-url" href="https://mastodon.social/@pfefferle">pfefferle</a>


</span>

<span class="u-category h-card">
<data class="p-uid" value="https://social.wake.st/users/liaizon"></data>
<a class="p-name u-url" href="https://social.wake.st/@liaizon">liaizon</a>


</span>

<span class="u-category h-card">
<data class="p-uid" value="https://snac.rohrmoser.name/social/wake_st"></data>
<a class="p-name u-url" href="https://snac.rohrmoser.name/social/wake_st">wake_st</a>


</span>

<span class="u-category h-card">
<data class="p-uid" value="http://example.com/webmention/target/placeholder"></data>
<a class="p-name u-url" href="http://example.com/webmention/target/placeholder">pfefferle</a>


</span>

<span class="u-category h-card">
<data class="p-uid" value="https://comam.es/snac/grunfink"></data>
<a class="p-name u-url" href="https://comam.es/snac/grunfink">grunfink</a>


</span>

<a class="u-in-reply-to" href="https://mastodon.social/web/statuses/114818494113762375"></a>
<a class="u-in-reply-to" href="tag:mastodon.social,2013:114818494113762375"></a>


</article>

</html>
7 changes: 7 additions & 0 deletions tests/data/mf2/brid-gy-mastodon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"author": {
"name": "Elena Rossini on GoToSocial ⁂",
"url": "https://aseachange.com/users/elena"
},
"response_type": "mention"
}
Loading