Skip to content

Commit

Permalink
Fix Fediverse preview for "blog actor only" mode. (#966)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Menrath and pfefferle authored Nov 5, 2024
1 parent 66f9590 commit 30662e9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
8 changes: 5 additions & 3 deletions includes/collection/class-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
8 changes: 4 additions & 4 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -225,7 +225,7 @@ function url_to_authorid( $url ) {
}
}

return 0;
return null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/transformer/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion templates/post-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
404
);
}
$user = \Activitypub\Collection\Users::get_by_id( $post->post_author );

$object = $transformer->to_object();
$user = $transformer->get_actor_object();

?>
<DOCTYPE html>
<html>
Expand Down

0 comments on commit 30662e9

Please sign in to comment.