PluginProbe
ActivityPub / 7.8.2
ActivityPub v7.8.2
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
← All changes | integration/class-webfinger.php +116 -24 1.0.87.8.2 View file →
@@ -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,49 +17,134 @@
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 - \add_filter( 'webfinger_user_data', array( self::class, 'add_user_discovery' ), 10, 3 );
18 - \add_filter( 'webfinger_data', array( self::class, 'add_pseudo_user_discovery' ), 99, 2 );
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 );
19 28 }
20 29
21 30 /**
22 - * Add WebFinger discovery links
31 + * Add WebFinger discovery links.
23 32 *
24 - * @param array $array the jrd array
25 - * @param string $resource the WebFinger resource
26 - * @param WP_User $user the WordPress user
33 + * @param array $jrd The jrd array.
34 + * @param string $uri The WebFinger resource.
35 + * @param \WP_User $user The WordPress user.
27 36 *
28 - * @return array the jrd array
37 + * @return array The jrd array.
29 38 */
30 - public static function add_user_discovery( $array, $resource, $user ) {
31 - $user = User_Collection::get_by_id( $user->ID );
39 + public static function add_user_discovery( $jrd, $uri, $user ) {
40 + $user = Actors::get_by_id( $user->ID );
32 41
33 - $array['links'][] = array(
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(
34 55 'rel' => 'self',
35 56 'type' => 'application/activity+json',
36 - 'href' => $user->get_url(),
57 + 'href' => $user->get_id(),
37 58 );
38 59
39 - return $array;
60 + return $jrd;
40 61 }
41 62
42 63 /**
43 - * Add WebFinger discovery links
64 + * Add WebFinger discovery links.
44 65 *
45 - * @param array $array the jrd array
46 - * @param string $resource the WebFinger resource
47 - * @param WP_User $user the WordPress user
66 + * @param array $jrd The jrd array.
67 + * @param string $uri The WebFinger resource.
48 68 *
49 - * @return array the jrd array
69 + * @return array|\WP_Error The jrd array or WP_Error.
50 70 */
51 - public static function add_pseudo_user_discovery( $array, $resource ) {
52 - if ( $array ) {
53 - return $array;
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;
54 76 }
55 77
56 - return Webfinger_Rest::get_profile( $resource );
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;
57 149 }
58 150 }