| @@ -1,10 +1,17 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * WebFinger integration file. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub\Integration; |
| 3 | 9 | |
| 4 | -use Activitypub\Rest\Webfinger as Webfinger_Rest; | |
| 5 | -use Activitypub\Collection\Users as User_Collection; | |
| 10 | +use Activitypub\Collection\Actors; | |
| 6 | 11 | |
| 12 | +use function Activitypub\get_rest_url_by_path; | |
| 13 | + | |
| 7 | 14 | /** |
| 8 | 15 | * Compatibility with the WebFinger plugin |
| 9 | 16 | * |
| 10 | 17 | * @see https://wordpress.org/plugins/webfinger/ |
| @@ -10,9 +17,9 @@ | ||
| 10 | 17 | * @see https://wordpress.org/plugins/webfinger/ |
| 11 | 18 | */ |
| 12 | 19 | class Webfinger { |
| 13 | 20 | /** |
| 14 | - * Initialize the class, registering WordPress hooks | |
| 21 | + * Initialize the class, registering WordPress hooks. | |
| 15 | 22 | */ |
| 16 | 23 | public static function init() { |
| 17 | 24 | \add_filter( 'webfinger_user_data', array( self::class, 'add_user_discovery' ), 1, 3 ); |
| 18 | 25 | \add_filter( 'webfinger_data', array( self::class, 'add_pseudo_user_discovery' ), 1, 2 ); |
| @@ -18,52 +25,95 @@ | ||
| 18 | 25 | \add_filter( 'webfinger_data', array( self::class, 'add_pseudo_user_discovery' ), 1, 2 ); |
| 19 | 26 | } |
| 20 | 27 | |
| 21 | 28 | /** |
| 22 | - * Add WebFinger discovery links | |
| 29 | + * Add WebFinger discovery links. | |
| 23 | 30 | * |
| 24 | - * @param array $array the jrd array | |
| 25 | - * @param string $resource the WebFinger resource | |
| 26 | - * @param WP_User $user the WordPress user | |
| 31 | + * @param array $jrd The jrd array. | |
| 32 | + * @param string $uri The WebFinger resource. | |
| 33 | + * @param \WP_User $user The WordPress user. | |
| 27 | 34 | * |
| 28 | - * @return array the jrd array | |
| 35 | + * @return array The jrd array. | |
| 29 | 36 | */ |
| 30 | - public static function add_user_discovery( $array, $resource, $user ) { | |
| 31 | - $user = User_Collection::get_by_id( $user->ID ); | |
| 37 | + public static function add_user_discovery( $jrd, $uri, $user ) { | |
| 38 | + $user = Actors::get_by_id( $user->ID ); | |
| 32 | 39 | |
| 33 | 40 | if ( ! $user || is_wp_error( $user ) ) { |
| 34 | - return $array; | |
| 41 | + return $jrd; | |
| 35 | 42 | } |
| 36 | 43 | |
| 37 | - $array['subject'] = sprintf( 'acct:%s', $user->get_webfinger() ); | |
| 44 | + $jrd['subject'] = sprintf( 'acct:%s', $user->get_webfinger() ); | |
| 38 | 45 | |
| 39 | - $array['aliases'][] = $user->get_url(); | |
| 40 | - $array['aliases'][] = $user->get_alternate_url(); | |
| 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'] ); | |
| 41 | 51 | |
| 42 | - $array['links'][] = array( | |
| 52 | + $jrd['links'][] = array( | |
| 43 | 53 | 'rel' => 'self', |
| 44 | 54 | 'type' => 'application/activity+json', |
| 45 | - 'href' => $user->get_url(), | |
| 55 | + 'href' => $user->get_id(), | |
| 46 | 56 | ); |
| 47 | 57 | |
| 48 | - return $array; | |
| 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; | |
| 49 | 64 | } |
| 50 | 65 | |
| 51 | 66 | /** |
| 52 | - * Add WebFinger discovery links | |
| 67 | + * Add WebFinger discovery links. | |
| 53 | 68 | * |
| 54 | - * @param array $array the jrd array | |
| 55 | - * @param string $resource the WebFinger resource | |
| 56 | - * @param WP_User $user the WordPress user | |
| 69 | + * @param array $jrd The jrd array. | |
| 70 | + * @param string $uri The WebFinger resource. | |
| 57 | 71 | * |
| 58 | - * @return array the jrd array | |
| 72 | + * @return array|\WP_Error The jrd array or WP_Error. | |
| 59 | 73 | */ |
| 60 | - public static function add_pseudo_user_discovery( $array, $resource ) { | |
| 61 | - $user = Webfinger_Rest::get_profile( $resource ); | |
| 74 | + public static function add_pseudo_user_discovery( $jrd, $uri ) { | |
| 75 | + $user = Actors::get_by_resource( $uri ); | |
| 62 | 76 | |
| 63 | - if ( ! $user || is_wp_error( $user ) ) { | |
| 64 | - return $array; | |
| 77 | + if ( \is_wp_error( $user ) ) { | |
| 78 | + return $user; | |
| 65 | 79 | } |
| 66 | 80 | |
| 67 | - return $user; | |
| 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; | |
| 68 | 118 | } |
| 69 | 119 | } |