@@ -202,6 +202,29 @@ public static function get_by_uri( $actor_uri ) {
202
202
return \get_post ( $ post_id );
203
203
}
204
204
205
+ /**
206
+ * Fetch a remote actor post by either actor URI or acct, fetching from remote if not found locally.
207
+ *
208
+ * @param string $uri_or_acct The actor URI or acct identifier.
209
+ *
210
+ * @return \WP_Post|\WP_Error Post object or WP_Error if not found.
211
+ */
212
+ public static function fetch_by_various ( $ uri_or_acct ) {
213
+ if ( \filter_var ( $ uri_or_acct , FILTER_VALIDATE_URL ) ) {
214
+ return self ::fetch_by_uri ( $ uri_or_acct );
215
+ }
216
+
217
+ if ( preg_match ( '/^@? ' . ACTIVITYPUB_USERNAME_REGEXP . '$/i ' , $ uri_or_acct ) ) {
218
+ return self ::fetch_by_acct ( $ uri_or_acct );
219
+ }
220
+
221
+ return new \WP_Error (
222
+ 'activitypub_invalid_actor_identifier ' ,
223
+ 'The actor identifier is not supported ' ,
224
+ array ( 'status ' => 400 )
225
+ );
226
+ }
227
+
205
228
/**
206
229
* Lookup a remote actor post by actor URI (guid), fetching from remote if not found locally.
207
230
*
@@ -239,6 +262,45 @@ public static function fetch_by_uri( $actor_uri ) {
239
262
return \get_post ( $ post_id );
240
263
}
241
264
265
+ /**
266
+ * Fetch a remote actor post by acct, fetching from remote if not found locally.
267
+ *
268
+ * @param string $acct The acct identifier.
269
+ *
270
+ * @return \WP_Post|\WP_Error Post object or WP_Error if not found.
271
+ */
272
+ public static function fetch_by_acct ( $ acct ) {
273
+ $ acct = Sanitize::webfinger ( $ acct );
274
+
275
+ // Check local DB for acct post meta.
276
+ global $ wpdb ;
277
+ // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
278
+ $ post_id = $ wpdb ->get_var (
279
+ $ wpdb ->prepare (
280
+ "SELECT post_id FROM $ wpdb ->postmeta WHERE meta_key='_activitypub_acct' AND meta_value=%s " ,
281
+ $ acct
282
+ )
283
+ );
284
+
285
+ if ( $ post_id ) {
286
+ return \get_post ( $ post_id );
287
+ }
288
+
289
+ $ profile_uri = Webfinger::resolve ( $ acct );
290
+
291
+ if ( \is_wp_error ( $ profile_uri ) ) {
292
+ return $ profile_uri ;
293
+ }
294
+
295
+ $ post = self ::fetch_by_uri ( $ profile_uri );
296
+
297
+ if ( ! \is_wp_error ( $ post ) ) {
298
+ \update_post_meta ( $ post ->ID , '_activitypub_acct ' , $ acct );
299
+ }
300
+
301
+ return $ post ;
302
+ }
303
+
242
304
/**
243
305
* Store an error that occurred when sending an ActivityPub message to a follower.
244
306
*
0 commit comments