88namespace Activitypub ;
99
1010use Exception ;
11+ use Activitypub \Collection \Actors ;
1112use Activitypub \Collection \Outbox ;
1213use Activitypub \Collection \Followers ;
1314use Activitypub \Collection \Extra_Fields ;
@@ -248,29 +249,50 @@ public static function redirect_canonical( $redirect_url, $requested_url ) {
248249 * @return void
249250 */
250251 public static function template_redirect () {
252+ global $ wp_query ;
253+
251254 $ comment_id = get_query_var ( 'c ' , null );
252255
253256 // Check if it seems to be a comment.
254- if ( ! $ comment_id ) {
255- return ;
256- }
257+ if ( $ comment_id ) {
258+ $ comment = get_comment ( $ comment_id );
257259
258- $ comment = get_comment ( $ comment_id );
260+ // Load a 404-page if `c` is set but not valid.
261+ if ( ! $ comment ) {
262+ $ wp_query ->set_404 ();
263+ return ;
264+ }
259265
260- // Load a 404-page if `c` is set but not valid.
261- if ( ! $ comment ) {
262- global $ wp_query ;
263- $ wp_query ->set_404 ();
264- return ;
265- }
266+ // Stop if it's not an ActivityPub comment.
267+ if ( is_activitypub_request () && ! is_local_comment ( $ comment ) ) {
268+ return ;
269+ }
266270
267- // Stop if it's not an ActivityPub comment.
268- if ( is_activitypub_request () && ! is_local_comment ( $ comment ) ) {
269- return ;
271+ wp_safe_redirect ( get_comment_link ( $ comment ) );
272+ exit ;
270273 }
271274
272- wp_safe_redirect ( get_comment_link ( $ comment ) );
273- exit ;
275+ $ actor = get_query_var ( 'actor ' , null );
276+ if ( $ actor ) {
277+ $ actor = Actors::get_by_username ( $ actor );
278+ if ( ! $ actor || \is_wp_error ( $ actor ) ) {
279+ $ wp_query ->set_404 ();
280+ return ;
281+ }
282+
283+ if ( is_activitypub_request () ) {
284+ return ;
285+ }
286+
287+ if ( $ actor ->get__id () > 0 ) {
288+ $ redirect_url = $ actor ->get_url ();
289+ } else {
290+ $ redirect_url = get_bloginfo ( 'url ' );
291+ }
292+
293+ wp_safe_redirect ( $ redirect_url , 301 );
294+ exit ;
295+ }
274296 }
275297
276298 /**
@@ -284,6 +306,7 @@ public static function add_query_vars( $vars ) {
284306 $ vars [] = 'activitypub ' ;
285307 $ vars [] = 'preview ' ;
286308 $ vars [] = 'author ' ;
309+ $ vars [] = 'actor ' ;
287310 $ vars [] = 'c ' ;
288311 $ vars [] = 'p ' ;
289312
@@ -405,12 +428,7 @@ public static function add_rewrite_rules() {
405428 );
406429 }
407430
408- \add_rewrite_rule (
409- '^@([\w\-\.]+)$ ' ,
410- 'index.php?rest_route=/ ' . ACTIVITYPUB_REST_NAMESPACE . '/actors/$matches[1] ' ,
411- 'top '
412- );
413-
431+ \add_rewrite_rule ( '^@([\w\-\.]+)\/?$ ' , 'index.php?actor=$matches[1] ' , 'top ' );
414432 \add_rewrite_endpoint ( 'activitypub ' , EP_AUTHORS | EP_PERMALINK | EP_PAGES );
415433 }
416434
0 commit comments