Skip to content

Commit d577dc0

Browse files
Menrathpfefferle
andauthored
Fix: wrong followers URL in "to" attribute of posts (#874)
* draft that shows potentially right behavior for what is set on the to attribute on posts * use Actor Object instead * reformat --------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent 57299fc commit d577dc0

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

includes/transformer/class-post.php

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
use WP_Post;
55
use Activitypub\Shortcodes;
66
use Activitypub\Model\Blog;
7-
use Activitypub\Transformer\Base;
87
use Activitypub\Collection\Users;
8+
use Activitypub\Transformer\Base;
99

1010
use function Activitypub\esc_hashtag;
1111
use function Activitypub\is_single_user;
1212
use function Activitypub\get_enclosures;
13-
use function Activitypub\generate_post_summary;
14-
use function Activitypub\get_rest_url_by_path;
1513
use function Activitypub\site_supports_blocks;
14+
use function Activitypub\get_rest_url_by_path;
15+
use function Activitypub\is_user_type_disabled;
16+
use function Activitypub\generate_post_summary;
1617

1718
/**
1819
* WordPress Post Transformer
@@ -25,6 +26,13 @@
2526
* - Activitypub\Activity\Base_Object
2627
*/
2728
class Post extends Base {
29+
/**
30+
* The User as Actor Object.
31+
*
32+
* @var Activitypub\Activity\Actor
33+
*/
34+
private $actor_object = null;
35+
2836
/**
2937
* Returns the ID of the WordPress Post.
3038
*
@@ -71,18 +79,46 @@ public function to_object() {
7179
$this->get_locale() => $this->get_content(),
7280
)
7381
);
74-
$path = sprintf( 'actors/%d/followers', intval( $post->post_author ) );
7582

7683
$object->set_to(
7784
array(
7885
'https://www.w3.org/ns/activitystreams#Public',
79-
get_rest_url_by_path( $path ),
86+
$this->get_actor_object()->get_followers(),
8087
)
8188
);
8289

8390
return $object;
8491
}
8592

93+
/**
94+
* Returns the User-Object of the Author of the Post.
95+
*
96+
* If `single_user` mode is enabled, the Blog-User is returned.
97+
*
98+
* @return Activitypub\Activity\Actor The User-Object.
99+
*/
100+
protected function get_actor_object() {
101+
if ( $this->actor_object ) {
102+
return $this->actor_object;
103+
}
104+
105+
$blog_user = new Blog();
106+
$this->actor_object = $blog_user;
107+
108+
if ( is_single_user() ) {
109+
return $blog_user;
110+
}
111+
112+
$user = Users::get_by_id( $this->wp_object->post_author );
113+
114+
if ( $user && ! is_wp_error( $user ) ) {
115+
$this->actor_object = $user;
116+
return $user;
117+
}
118+
119+
return $blog_user;
120+
}
121+
86122
/**
87123
* Returns the ID of the Post.
88124
*
@@ -128,19 +164,7 @@ public function get_url() {
128164
* @return string The User-URL.
129165
*/
130166
protected function get_attributed_to() {
131-
$blog_user = new Blog();
132-
133-
if ( is_single_user() ) {
134-
return $blog_user->get_url();
135-
}
136-
137-
$user = Users::get_by_id( $this->wp_object->post_author );
138-
139-
if ( $user && ! is_wp_error( $user ) ) {
140-
return $user->get_url();
141-
}
142-
143-
return $blog_user->get_url();
167+
return $this->get_actor_object()->get_url();
144168
}
145169

146170
/**

0 commit comments

Comments
 (0)