From 30662e9c53d71bc7668f3700a9a4977752b80a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= <99024746+Menrath@users.noreply.github.com> Date: Tue, 5 Nov 2024 15:38:10 +0100 Subject: [PATCH] Fix Fediverse preview for "blog actor only" mode. (#966) * Fix Fediverse preview for "blog actor only" mode. * that simply should do the trick! thanks for reporting that!!! * 0 is reserved for the blog user * simplify code a bit! --------- Co-authored-by: Matthias Pfefferle --- includes/collection/class-users.php | 8 +++++--- includes/functions.php | 8 ++++---- includes/transformer/class-post.php | 2 +- templates/post-preview.php | 4 +++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/includes/collection/class-users.php b/includes/collection/class-users.php index 036d210aa..b2c61ca6a 100644 --- a/includes/collection/class-users.php +++ b/includes/collection/class-users.php @@ -187,14 +187,16 @@ public static function get_by_resource( $uri ) { // Check for http(s)://blog.example.com/author/username. $user_id = url_to_authorid( $uri ); - if ( $user_id ) { + if ( \is_int( $user_id ) ) { return self::get_by_id( $user_id ); } // Check for http(s)://blog.example.com/. + $normalized_uri = normalize_url( $uri ); + if ( - normalize_url( site_url() ) === normalize_url( $uri ) || - normalize_url( home_url() ) === normalize_url( $uri ) + normalize_url( site_url() ) === $normalized_uri || + normalize_url( home_url() ) === $normalized_uri ) { return self::get_by_id( self::BLOG_USER_ID ); } diff --git a/includes/functions.php b/includes/functions.php index b283942ac..4ee0af8e2 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -187,14 +187,14 @@ function count_followers( $user_id ) { * * @param string $url Permalink to check. * - * @return int User ID, or 0 on failure. + * @return int|null User ID, or null on failure. */ function url_to_authorid( $url ) { global $wp_rewrite; // Check if url hase the same host. if ( \wp_parse_url( \home_url(), \PHP_URL_HOST ) !== \wp_parse_url( $url, \PHP_URL_HOST ) ) { - return 0; + return null; } // First, check to see if there is a 'author=N' to match against. @@ -210,7 +210,7 @@ function url_to_authorid( $url ) { // Not using rewrite rules, and 'author=N' method failed, so we're out of options. if ( empty( $rewrite ) ) { - return 0; + return null; } // Generate rewrite rule for the author url. @@ -225,7 +225,7 @@ function url_to_authorid( $url ) { } } - return 0; + return null; } /** diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index 23dd46e67..f5c544ad3 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -101,7 +101,7 @@ public function to_object() { * * @return \Activitypub\Activity\Actor The User-Object. */ - protected function get_actor_object() { + public function get_actor_object() { if ( $this->actor_object ) { return $this->actor_object; } diff --git a/templates/post-preview.php b/templates/post-preview.php index 86d733659..8d7d9206b 100644 --- a/templates/post-preview.php +++ b/templates/post-preview.php @@ -14,8 +14,10 @@ 404 ); } -$user = \Activitypub\Collection\Users::get_by_id( $post->post_author ); + $object = $transformer->to_object(); +$user = $transformer->get_actor_object(); + ?>