PluginProbe
ActivityPub / 7.7.0
ActivityPub v7.7.0
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
← All changes | includes/handler/class-create.php +118 -37 2.1.17.7.0 View file →
@@ -1,70 +1,151 @@
1 1 <?php
2 +/**
3 + * Create handler file.
4 + *
5 + * @package Activitypub
6 + */
7 +
2 8 namespace Activitypub\Handler;
3 9
4 -use WP_Error;
5 10 use Activitypub\Collection\Interactions;
11 +use Activitypub\Collection\Posts;
6 12
7 -use function Activitypub\is_activity_public;
13 +use function Activitypub\get_activity_visibility;
14 +use function Activitypub\is_activity_reply;
15 +use function Activitypub\is_quote_activity;
16 +use function Activitypub\is_self_ping;
8 17 use function Activitypub\object_id_to_comment;
9 18
10 19 /**
11 - * Handle Create requests
20 + * Handle Create requests.
12 21 */
13 22 class Create {
14 23 /**
15 - * Initialize the class, registering WordPress hooks
24 + * Initialize the class, registering WordPress hooks.
16 25 */
17 26 public static function init() {
18 - \add_action(
19 - 'activitypub_inbox_create',
20 - array( self::class, 'handle_create' ),
21 - 10,
22 - 3
23 - );
27 + \add_action( 'activitypub_handled_inbox_create', array( self::class, 'handle_create' ), 10, 3 );
28 + \add_filter( 'activitypub_validate_object', array( self::class, 'validate_object' ), 10, 3 );
24 29 }
25 30
26 31 /**
27 - * Handles "Create" requests
32 + * Handles "Create" requests.
28 33 *
29 - * @param array $array The activity-object
30 - * @param int $user_id The id of the local blog-user
31 - * @param Activitypub\Activity $object The activity object
34 + * @param array $activity The activity-object.
35 + * @param int|int[] $user_ids The id(s) of the local blog-user(s).
36 + * @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null.
37 + */
38 + public static function handle_create( $activity, $user_ids, $activity_object = null ) {
39 + // Check for private and/or direct messages.
40 + if ( ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE === get_activity_visibility( $activity ) ) {
41 + $result = false;
42 + } elseif ( is_activity_reply( $activity ) || is_quote_activity( $activity ) ) { // Check for replies and quotes.
43 + $result = self::create_interaction( $activity, $user_ids, $activity_object );
44 + } elseif ( \get_option( 'activitypub_create_posts', false ) ) { // Handle non-interaction objects.
45 + $result = self::create_post( $activity, $user_ids, $activity_object );
46 + } else {
47 + $result = false;
48 + }
49 +
50 + if ( false === $result ) {
51 + return;
52 + }
53 +
54 + $success = ! \is_wp_error( $result );
55 +
56 + /**
57 + * Fires after an ActivityPub Create activity has been handled.
58 + *
59 + * @param array $activity The ActivityPub activity data.
60 + * @param int[] $user_ids The local user IDs.
61 + * @param bool $success True on success, false otherwise.
62 + * @param \WP_Comment|\WP_Post|\WP_Error $result The WP_Comment object of the created comment, or null if creation failed.
63 + */
64 + \do_action( 'activitypub_handled_create', $activity, (array) $user_ids, $success, $result );
65 + }
66 +
67 + /**
68 + * Handle interactions like replies.
32 69 *
33 - * @return void
70 + * @param array $activity The activity-object.
71 + * @param int[] $user_ids The ids of the local blog-users.
72 + * @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null.
73 + *
74 + * @return \WP_Comment|\WP_Error|false The created comment, WP_Error on failure, false if already exists or not processed.
34 75 */
35 - public static function handle_create( $array, $user_id, $object = null ) {
36 - if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
37 - return;
76 + public static function create_interaction( $activity, $user_ids, $activity_object = null ) {
77 + $existing_comment = object_id_to_comment( $activity['object']['id'] );
78 +
79 + // If comment exists, call update action.
80 + if ( $existing_comment ) {
81 + Update::handle_update( $activity, (array) $user_ids, $activity_object );
82 +
83 + return false;
38 84 }
39 85
40 - if (
41 - ! isset( $array['object'] ) ||
42 - ! isset( $array['object']['id'] )
43 - ) {
44 - return;
86 + if ( is_self_ping( $activity['object']['id'] ) ) {
87 + return false;
45 88 }
46 89
47 - // check if Activity is public or not
48 - if ( ! is_activity_public( $array ) ) {
49 - // @todo maybe send email
50 - return;
90 + $result = Interactions::add_comment( $activity );
91 +
92 + if ( ! $result || \is_wp_error( $result ) ) {
93 + return $result;
51 94 }
52 95
53 - $check_dupe = object_id_to_comment( $array['object']['id'] );
96 + return \get_comment( $result );
97 + }
54 98
55 - // if comment exists, call update action
56 - if ( $check_dupe ) {
57 - \do_action( 'activitypub_inbox_update', $array, $user_id, $object );
58 - return;
99 + /**
100 + * Handle non-interaction posts like posts.
101 + *
102 + * @param array $activity The activity-object.
103 + * @param int[] $user_ids The ids of the local blog-users.
104 + * @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null.
105 + *
106 + * @return \WP_Post|\WP_Error|false The post on success, WP_Error on failure, false if already exists.
107 + */
108 + public static function create_post( $activity, $user_ids, $activity_object = null ) {
109 + $existing_post = Posts::get_by_guid( $activity['object']['id'] );
110 +
111 + // If post exists, call update action.
112 + if ( $existing_post instanceof \WP_Post ) {
113 + Update::handle_update( $activity, (array) $user_ids, $activity_object );
114 +
115 + return false;
59 116 }
60 117
61 - $state = Interactions::add_comment( $array );
62 - $reaction = null;
118 + return Posts::add( $activity, $user_ids );
119 + }
63 120
64 - if ( $state && ! \is_wp_error( $reaction ) ) {
65 - $reaction = \get_comment( $state );
121 + /**
122 + * Validate the object.
123 + *
124 + * @param bool $valid The validation state.
125 + * @param string $param The object parameter.
126 + * @param \WP_REST_Request $request The request object.
127 + *
128 + * @return bool The validation state: true if valid, false if not.
129 + */
130 + public static function validate_object( $valid, $param, $request ) {
131 + $activity = $request->get_json_params();
132 +
133 + if ( empty( $activity['type'] ) ) {
134 + return false;
66 135 }
67 136
68 - \do_action( 'activitypub_handled_create', $array, $user_id, $state, $reaction );
137 + if ( 'Create' !== $activity['type'] ) {
138 + return $valid;
139 + }
140 +
141 + if ( ! isset( $activity['object'] ) || ! \is_array( $activity['object'] ) ) {
142 + return false;
143 + }
144 +
145 + if ( ! isset( $activity['object']['id'], $activity['object']['content'] ) ) {
146 + return false;
147 + }
148 +
149 + return $valid;
69 150 }
70 151 }