@@ -85,35 +85,12 @@ public function get_activitypub_object() {
8585 return $ this ->activitypub_object ;
8686 }
8787
88- $ queried_object = $ this ->get_queried_object ();
89-
90- // Check for Outbox Activity.
91- if (
92- $ queried_object instanceof \WP_Post &&
93- Outbox::POST_TYPE === $ queried_object ->post_type
94- ) {
95- $ activitypub_object = Outbox::maybe_get_activity ( $ queried_object );
96-
97- // Check if the Outbox Activity is public.
98- if ( ! \is_wp_error ( $ activitypub_object ) ) {
99- $ this ->activitypub_object = $ activitypub_object ;
100-
101- return $ this ->activitypub_object ;
102- }
103- }
104-
105- if ( ! $ queried_object ) {
106- // If the object is not a valid ActivityPub object, try to get a virtual object.
107- $ activitypub_object = $ this ->maybe_get_virtual_object ();
108-
109- if ( $ activitypub_object ) {
110- $ this ->activitypub_object = $ activitypub_object ;
111-
112- return $ this ->activitypub_object ;
113- }
88+ if ( $ this ->prepare_activitypub_data () ) {
89+ return $ this ->activitypub_object ;
11490 }
11591
116- $ transformer = Factory::get_transformer ( $ queried_object );
92+ $ queried_object = $ this ->get_queried_object ();
93+ $ transformer = Factory::get_transformer ( $ queried_object );
11794
11895 if ( $ transformer && ! \is_wp_error ( $ transformer ) ) {
11996 $ this ->activitypub_object = $ transformer ->to_object ();
@@ -132,13 +109,55 @@ public function get_activitypub_object_id() {
132109 return $ this ->activitypub_object_id ;
133110 }
134111
135- if ( $ this ->activitypub_object ) {
136- $ this ->activitypub_object_id = $ this ->activitypub_object ->get_id ();
137-
112+ if ( $ this ->prepare_activitypub_data () ) {
138113 return $ this ->activitypub_object_id ;
139114 }
140115
141- return $ this ->get_activitypub_object ();
116+ $ queried_object = $ this ->get_queried_object ();
117+ $ transformer = Factory::get_transformer ( $ queried_object );
118+
119+ if ( $ transformer && ! \is_wp_error ( $ transformer ) ) {
120+ $ this ->activitypub_object_id = $ transformer ->to_id ();
121+ }
122+
123+ return $ this ->activitypub_object_id ;
124+ }
125+
126+ /**
127+ * Prepare and set both ActivityPub object and ID for Outbox activities and virtual objects.
128+ *
129+ * @return bool True if an object was found and set, false otherwise.
130+ */
131+ private function prepare_activitypub_data () {
132+ $ queried_object = $ this ->get_queried_object ();
133+
134+ // Check for Outbox Activity.
135+ if (
136+ $ queried_object instanceof \WP_Post &&
137+ Outbox::POST_TYPE === $ queried_object ->post_type
138+ ) {
139+ $ activitypub_object = Outbox::maybe_get_activity ( $ queried_object );
140+
141+ // Check if the Outbox Activity is public.
142+ if ( ! \is_wp_error ( $ activitypub_object ) ) {
143+ $ this ->activitypub_object = $ activitypub_object ;
144+ $ this ->activitypub_object_id = $ this ->activitypub_object ->get_id ();
145+ return true ;
146+ }
147+ }
148+
149+ if ( ! $ queried_object ) {
150+ // If the object is not a valid ActivityPub object, try to get a virtual object.
151+ $ activitypub_object = $ this ->maybe_get_virtual_object ();
152+
153+ if ( $ activitypub_object ) {
154+ $ this ->activitypub_object = $ activitypub_object ;
155+ $ this ->activitypub_object_id = $ this ->activitypub_object ->get_id ();
156+ return true ;
157+ }
158+ }
159+
160+ return false ;
142161 }
143162
144163 /**
@@ -253,7 +272,7 @@ public function is_activitypub_request() {
253272 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
254273 isset ( $ _GET ['activitypub ' ] )
255274 ) {
256- defined ( 'ACTIVITYPUB_REQUEST ' ) || \define ( 'ACTIVITYPUB_REQUEST ' , true );
275+ \ defined ( 'ACTIVITYPUB_REQUEST ' ) || \define ( 'ACTIVITYPUB_REQUEST ' , true );
257276 $ this ->is_activitypub_request = true ;
258277
259278 return true ;
@@ -275,7 +294,7 @@ public function is_activitypub_request() {
275294 * - application/json
276295 */
277296 if ( \preg_match ( '/(application\/(ld\+json|activity\+json|json))/i ' , $ accept ) ) {
278- defined ( 'ACTIVITYPUB_REQUEST ' ) || \define ( 'ACTIVITYPUB_REQUEST ' , true );
297+ \ defined ( 'ACTIVITYPUB_REQUEST ' ) || \define ( 'ACTIVITYPUB_REQUEST ' , true );
279298 $ this ->is_activitypub_request = true ;
280299
281300 return true ;
0 commit comments