PluginProbe
ActivityPub / 9.2.1
ActivityPub v9.2.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / handler / outbox / class-add.php

class-add.php in ActivityPub 9.2.1, at includes/handler/outbox/class-add.php

96 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Outbox Add handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler\Outbox;
9
10 use Activitypub\Collection\Actors;
11
12 use function Activitypub\object_to_uri;
13
14 /**
15 * Handle outgoing Add activities.
16 *
17 * Supports adding objects to an actor's featured collection
18 * by making the corresponding WordPress post sticky.
19 */
20 class Add {
21 /**
22 * Initialize the class, registering WordPress hooks.
23 */
24 public static function init() {
25 \add_filter( 'activitypub_outbox_add', array( self::class, 'handle_add' ), 10, 2 );
26 }
27
28 /**
29 * Handle outgoing "Add" activities from local actors.
30 *
31 * When the target is the actor's featured collection, the referenced
32 * post is made sticky. The sticky action triggers the scheduler which
33 * creates the outbox entry automatically.
34 *
35 * @since 8.1.0
36 *
37 * @param array $data The activity data array.
38 * @param int $user_id The user ID.
39 *
40 * @return \WP_Post|\WP_Error|array The post object on success, WP_Error on failure, or original data if unhandled.
41 */
42 public static function handle_add( $data, $user_id = null ) {
43 $object_uri = object_to_uri( $data['object'] ?? '' );
44 $target = object_to_uri( $data['target'] ?? '' );
45
46 if ( empty( $object_uri ) || empty( $target ) ) {
47 return $data;
48 }
49
50 $actor = Actors::get_by_id( $user_id );
51
52 if ( \is_wp_error( $actor ) ) {
53 return $actor;
54 }
55
56 // Only handle featured collection targets.
57 if ( $target !== $actor->get_featured() ) {
58 return $data;
59 }
60
61 $post_id = \url_to_postid( $object_uri );
62
63 if ( ! $post_id ) {
64 return new \WP_Error(
65 'activitypub_object_not_found',
66 \__( 'The referenced object was not found.', 'activitypub' ),
67 array( 'status' => 404 )
68 );
69 }
70
71 $post = \get_post( $post_id );
72
73 if ( ! $post ) {
74 return new \WP_Error(
75 'activitypub_object_not_found',
76 \__( 'The referenced object was not found.', 'activitypub' ),
77 array( 'status' => 404 )
78 );
79 }
80
81 // Verify the user owns this post.
82 if ( $user_id > 0 && (int) $post->post_author !== $user_id ) {
83 return new \WP_Error(
84 'activitypub_forbidden',
85 \__( 'You can only feature your own posts.', 'activitypub' ),
86 array( 'status' => 403 )
87 );
88 }
89
90 // Making the post sticky triggers the scheduler which adds to outbox.
91 \stick_post( $post_id );
92
93 return $post;
94 }
95 }
96