| 1 |
<?php |
| 2 |
/** |
| 3 |
* WebFinger integration file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Integration; |
| 9 |
|
| 10 |
use Activitypub\Collection\Actors; |
| 11 |
|
| 12 |
use function Activitypub\get_rest_url_by_path; |
| 13 |
|
| 14 |
/** |
| 15 |
* Compatibility with the WebFinger plugin |
| 16 |
* |
| 17 |
* @see https://wordpress.org/plugins/webfinger/ |
| 18 |
*/ |
| 19 |
class Webfinger { |
| 20 |
/** |
| 21 |
* Initialize the class, registering WordPress hooks. |
| 22 |
*/ |
| 23 |
public static function init() { |
| 24 |
\add_filter( 'webfinger_user_data', array( self::class, 'add_user_discovery' ), 1, 3 ); |
| 25 |
\add_filter( 'webfinger_data', array( self::class, 'add_pseudo_user_discovery' ), 1, 2 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Add WebFinger discovery links. |
| 30 |
* |
| 31 |
* @param array $jrd The jrd array. |
| 32 |
* @param string $uri The WebFinger resource. |
| 33 |
* @param \WP_User $user The WordPress user. |
| 34 |
* |
| 35 |
* @return array The jrd array. |
| 36 |
*/ |
| 37 |
public static function add_user_discovery( $jrd, $uri, $user ) { |
| 38 |
$user = Actors::get_by_id( $user->ID ); |
| 39 |
|
| 40 |
if ( ! $user || is_wp_error( $user ) ) { |
| 41 |
return $jrd; |
| 42 |
} |
| 43 |
|
| 44 |
$jrd['subject'] = sprintf( 'acct:%s', $user->get_webfinger() ); |
| 45 |
|
| 46 |
$jrd['aliases'][] = $user->get_id(); |
| 47 |
$jrd['aliases'][] = $user->get_url(); |
| 48 |
$jrd['aliases'][] = $user->get_alternate_url(); |
| 49 |
$jrd['aliases'] = array_unique( $jrd['aliases'] ); |
| 50 |
$jrd['aliases'] = array_values( $jrd['aliases'] ); |
| 51 |
|
| 52 |
$jrd['links'][] = array( |
| 53 |
'rel' => 'self', |
| 54 |
'type' => 'application/activity+json', |
| 55 |
'href' => $user->get_id(), |
| 56 |
); |
| 57 |
|
| 58 |
$jrd['links'][] = array( |
| 59 |
'rel' => 'http://ostatus.org/schema/1.0/subscribe', |
| 60 |
'template' => get_rest_url_by_path( 'interactions?uri={uri}' ), |
| 61 |
); |
| 62 |
|
| 63 |
return $jrd; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Add WebFinger discovery links. |
| 68 |
* |
| 69 |
* @param array $jrd The jrd array. |
| 70 |
* @param string $uri The WebFinger resource. |
| 71 |
* |
| 72 |
* @return array|\WP_Error The jrd array or WP_Error. |
| 73 |
*/ |
| 74 |
public static function add_pseudo_user_discovery( $jrd, $uri ) { |
| 75 |
$user = Actors::get_by_resource( $uri ); |
| 76 |
|
| 77 |
if ( \is_wp_error( $user ) ) { |
| 78 |
return $user; |
| 79 |
} |
| 80 |
|
| 81 |
$aliases = array( |
| 82 |
$user->get_id(), |
| 83 |
$user->get_url(), |
| 84 |
$user->get_alternate_url(), |
| 85 |
); |
| 86 |
|
| 87 |
$aliases = array_unique( $aliases ); |
| 88 |
$aliases = array_values( $aliases ); |
| 89 |
|
| 90 |
$profile = array( |
| 91 |
'subject' => sprintf( 'acct:%s', $user->get_webfinger() ), |
| 92 |
'aliases' => $aliases, |
| 93 |
'links' => array( |
| 94 |
array( |
| 95 |
'rel' => 'self', |
| 96 |
'type' => 'application/activity+json', |
| 97 |
'href' => $user->get_id(), |
| 98 |
), |
| 99 |
array( |
| 100 |
'rel' => 'http://webfinger.net/rel/profile-page', |
| 101 |
'type' => 'text/html', |
| 102 |
'href' => $user->get_id(), |
| 103 |
), |
| 104 |
array( |
| 105 |
'rel' => 'http://ostatus.org/schema/1.0/subscribe', |
| 106 |
'template' => get_rest_url_by_path( 'interactions?uri={uri}' ), |
| 107 |
), |
| 108 |
), |
| 109 |
); |
| 110 |
|
| 111 |
if ( 'Person' !== $user->get_type() ) { |
| 112 |
$profile['links'][0]['properties'] = array( |
| 113 |
'https://www.w3.org/ns/activitystreams#type' => $user->get_type(), |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
return $profile; |
| 118 |
} |
| 119 |
} |
| 120 |
|