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

Commit 07ba4db

Browse files
committed
basic rel-alternate support
1 parent 2d5a973 commit 07ba4db

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

includes/class-linkbacks-mf2-handler.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ public static function generate_commentdata( $commentdata ) {
144144
$parser = new Parser( $commentdata['remote_source_original'], $source );
145145
$mf_array = $parser->parse( true );
146146

147+
// check for rel-alternate links
148+
if ( $source = self::get_alternate_source( $mf_array ) ) {
149+
$mf_array = $source;
150+
}
151+
147152
// get all 'relevant' entries
148153
$entries = self::get_entries( $mf_array );
149154

@@ -431,6 +436,50 @@ public static function flatten_microformats( $item ) {
431436
return $flat;
432437
}
433438

439+
public static function has_alternate_url( $mf_array ) {
440+
return ( bool ) self::get_alternate_url( $mf_array );
441+
}
442+
443+
public static function get_alternate_url( $mf_array ) {
444+
if ( ! array_key_exists( 'rel-urls', $mf_array ) ) {
445+
return false;
446+
}
447+
448+
foreach ( $mf_array['rel-urls'] as $url => $meta ) ) {
449+
if (
450+
$meta['type'] === 'application/mf2+json' &&
451+
in_array( 'alternate', $meta['rels'] ) &&
452+
filter_var($url, FILTER_VALIDATE_URL) !== false
453+
) {
454+
return $url;
455+
}
456+
}
457+
458+
return false;
459+
}
460+
461+
public static function get_alternate_source( $mf_array ) {
462+
$url = self::get_alternate_url( $mf_array );
463+
464+
if ( ! $url ) {
465+
return false;
466+
}
467+
468+
$user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) );
469+
$args = array(
470+
'timeout' => 100,
471+
'limit_response_size' => 153600,
472+
'redirection' => 20,
473+
'user-agent' => "$user_agent; Semantic-Linkbacks/Webmention read rel-alternate source",
474+
);
475+
$response = wp_safe_remote_get( $url, $args );
476+
// check if source is accessible
477+
if ( is_wp_error( $response ) ) {
478+
return false;
479+
}
480+
481+
return wp_remote_retrieve_body( $response );
482+
}
434483

435484
/**
436485
* get all h-entry items

0 commit comments

Comments
 (0)