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