| 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 |
\add_filter( 'webfinger_data', array( self::class, 'add_interaction_links' ), 11 ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Add WebFinger discovery links. |
| 32 |
* |
| 33 |
* @param array $jrd The jrd array. |
| 34 |
* @param string $uri The WebFinger resource. |
| 35 |
* @param \WP_User $user The WordPress user. |
| 36 |
* |
| 37 |
* @return array The jrd array. |
| 38 |
*/ |
| 39 |
public static function add_user_discovery( $jrd, $uri, $user ) { |
| 40 |
$user = Actors::get_by_id( $user->ID ); |
| 41 |
|
| 42 |
if ( ! $user || \is_wp_error( $user ) ) { |
| 43 |
return $jrd; |
| 44 |
} |
| 45 |
|
| 46 |
$jrd['subject'] = \sprintf( 'acct:%s', $user->get_webfinger() ); |
| 47 |
|
| 48 |
$jrd['aliases'][] = $user->get_id(); |
| 49 |
$jrd['aliases'][] = $user->get_url(); |
| 50 |
$jrd['aliases'][] = $user->get_alternate_url(); |
| 51 |
$jrd['aliases'] = \array_unique( $jrd['aliases'] ); |
| 52 |
$jrd['aliases'] = \array_values( $jrd['aliases'] ); |
| 53 |
|
| 54 |
$jrd['links'][] = array( |
| 55 |
'rel' => 'self', |
| 56 |
'type' => 'application/activity+json', |
| 57 |
'href' => $user->get_id(), |
| 58 |
); |
| 59 |
|
| 60 |
return $jrd; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Add WebFinger discovery links. |
| 65 |
* |
| 66 |
* @param array $jrd The jrd array. |
| 67 |
* @param string $uri The WebFinger resource. |
| 68 |
* |
| 69 |
* @return array|\WP_Error The jrd array or WP_Error. |
| 70 |
*/ |
| 71 |
public static function add_pseudo_user_discovery( $jrd, $uri ) { |
| 72 |
$user = Actors::get_by_resource( $uri ); |
| 73 |
|
| 74 |
if ( \is_wp_error( $user ) ) { |
| 75 |
return $user; |
| 76 |
} |
| 77 |
|
| 78 |
$aliases = array( |
| 79 |
$user->get_id(), |
| 80 |
$user->get_url(), |
| 81 |
$user->get_alternate_url(), |
| 82 |
); |
| 83 |
|
| 84 |
$aliases = \array_unique( $aliases ); |
| 85 |
$aliases = \array_values( $aliases ); |
| 86 |
|
| 87 |
$profile = array( |
| 88 |
'subject' => \sprintf( 'acct:%s', $user->get_webfinger() ), |
| 89 |
'aliases' => $aliases, |
| 90 |
'links' => array( |
| 91 |
array( |
| 92 |
'rel' => 'self', |
| 93 |
'type' => 'application/activity+json', |
| 94 |
'href' => $user->get_id(), |
| 95 |
), |
| 96 |
array( |
| 97 |
'rel' => 'http://webfinger.net/rel/profile-page', |
| 98 |
'type' => 'text/html', |
| 99 |
'href' => $user->get_id(), |
| 100 |
), |
| 101 |
), |
| 102 |
); |
| 103 |
|
| 104 |
if ( 'Person' !== $user->get_type() ) { |
| 105 |
$profile['links'][0]['properties'] = array( |
| 106 |
'https://www.w3.org/ns/activitystreams#type' => $user->get_type(), |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
return $profile; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Add interaction links to the WebFinger data. |
| 115 |
* |
| 116 |
* @see https://codeberg.org/fediverse/fep/src/branch/main/fep/3b86/fep-3b86.md |
| 117 |
* |
| 118 |
* @param array $jrd The jrd array. |
| 119 |
* |
| 120 |
* @return array The jrd array. |
| 121 |
*/ |
| 122 |
public static function add_interaction_links( $jrd ) { |
| 123 |
if ( ! \is_array( $jrd ) ) { |
| 124 |
return $jrd; |
| 125 |
} |
| 126 |
|
| 127 |
$jrd['links'][] = array( |
| 128 |
'rel' => 'http://ostatus.org/schema/1.0/subscribe', |
| 129 |
'template' => get_rest_url_by_path( 'interactions?uri={uri}' ), |
| 130 |
); |
| 131 |
|
| 132 |
/* |
| 133 |
* Note: The parameter name `{inReplyTo}` is used here for all 'Create' intents, |
| 134 |
* not just replies, to maintain compatibility with existing implementations and |
| 135 |
* the FEP-3b86 specification. If a more generic parameter name is adopted in the |
| 136 |
* future, this should be updated accordingly. |
| 137 |
*/ |
| 138 |
$jrd['links'][] = array( |
| 139 |
'rel' => 'https://w3id.org/fep/3b86/Create', |
| 140 |
'template' => get_rest_url_by_path( 'interactions?uri={inReplyTo}&intent=create' ), |
| 141 |
); |
| 142 |
|
| 143 |
$jrd['links'][] = array( |
| 144 |
'rel' => 'https://w3id.org/fep/3b86/Follow', |
| 145 |
'template' => get_rest_url_by_path( 'interactions?uri={object}&intent=follow' ), |
| 146 |
); |
| 147 |
|
| 148 |
return $jrd; |
| 149 |
} |
| 150 |
} |
| 151 |
|