PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
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-create.php

class-create.php in ActivityPub trunk, at includes/handler/outbox/class-create.php

122 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Outbox Create handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler\Outbox;
9
10 use Activitypub\Collection\Interactions;
11 use Activitypub\Collection\Posts;
12
13 use function Activitypub\is_activity_public;
14 use function Activitypub\is_activity_reply;
15 use function Activitypub\is_quote_activity;
16
17 /**
18 * Handle outgoing Create activities (C2S).
19 */
20 class Create {
21 /**
22 * Initialize the class, registering WordPress hooks.
23 */
24 public static function init() {
25 \add_filter( 'activitypub_outbox_create', array( self::class, 'handle_create' ), 10, 3 );
26 }
27
28 /**
29 * Handle outgoing "Create" activities from local actors.
30 *
31 * Creates WordPress content and adds to outbox for federation.
32 *
33 * @param array $activity The activity data.
34 * @param int $user_id The local user ID.
35 * @param string|null $visibility Content visibility.
36 *
37 * @return int|\WP_Error|false The outbox ID on success, WP_Error on failure, false if not handled.
38 */
39 public static function handle_create( $activity, $user_id = null, $visibility = null ) {
40 // Skip private/direct activities.
41 if ( ! is_activity_public( $activity ) ) {
42 return false;
43 }
44
45 $object = $activity['object'] ?? array();
46
47 if ( ! \is_array( $object ) ) {
48 return new \WP_Error( 'invalid_object', 'Invalid object in activity.' );
49 }
50
51 $object_type = $object['type'] ?? '';
52
53 // Only handle Note and Article types for now.
54 if ( ! \in_array( $object_type, array( 'Note', 'Article' ), true ) ) {
55 return false;
56 }
57
58 if ( is_activity_reply( $activity ) ) {
59 return self::create_comment( $activity, $user_id );
60 }
61
62 // TODO: Handle quotes differently.
63 if ( is_quote_activity( $activity ) ) {
64 return false;
65 }
66
67 return self::create_post( $activity, $user_id, $visibility );
68 }
69
70 /**
71 * Handle outgoing post from local actor.
72 *
73 * Creates a WordPress post. The scheduler will add it to the outbox.
74 *
75 * @param array $activity The activity data.
76 * @param int $user_id The local user ID.
77 * @param string|null $visibility Content visibility.
78 *
79 * @return \WP_Post|\WP_Error The created post on success, WP_Error on failure.
80 */
81 private static function create_post( $activity, $user_id, $visibility ) {
82 $post = Posts::create( $activity, $user_id, $visibility );
83
84 if ( \is_wp_error( $post ) ) {
85 return $post;
86 }
87
88 /**
89 * Fires after a post has been created from an outgoing Create activity.
90 *
91 * @param int $post_id The created post ID.
92 * @param array $activity The activity data.
93 * @param int $user_id The user ID.
94 * @param string $visibility The content visibility.
95 */
96 \do_action( 'activitypub_outbox_created_post', $post->ID, $activity, $user_id, $visibility );
97
98 return $post;
99 }
100
101 /**
102 * Handle outgoing reply from local actor.
103 *
104 * Creates a WordPress comment on the local post. The comment scheduler
105 * will add it to the outbox and federate it.
106 *
107 * @param array $activity The activity data.
108 * @param int $user_id The local user ID.
109 *
110 * @return \WP_Comment|false Comment on success, false if not a local reply.
111 */
112 private static function create_comment( $activity, $user_id ) {
113 $result = Interactions::add_comment( $activity, $user_id );
114
115 if ( ! $result ) {
116 return false;
117 }
118
119 return \get_comment( $result );
120 }
121 }
122