PluginProbe
ActivityPub / 8.0.2
ActivityPub v8.0.2
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-update.php +95 -41 2.0.08.0.2 View file →
@@ -1,11 +1,19 @@
1 1 <?php
2 +/**
3 + * Update 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;
12 +use Activitypub\Collection\Remote_Actors;
6 13
7 14 use function Activitypub\get_remote_metadata_by_actor;
15 +use function Activitypub\is_activity_reply;
8 16
9 17 /**
10 18 * Handle Update requests.
11 19 */
@@ -10,38 +18,43 @@
10 18 * Handle Update requests.
11 19 */
12 20 class Update {
13 21 /**
14 - * Initialize the class, registering WordPress hooks
22 + * Initialize the class, registering WordPress hooks.
15 23 */
16 24 public static function init() {
17 - \add_action(
18 - 'activitypub_inbox_update',
19 - array( self::class, 'handle_update' )
20 - );
25 + \add_action( 'activitypub_handled_inbox_update', array( self::class, 'handle_update' ), 10, 3 );
21 26 }
22 27
23 28 /**
24 - * Handle "Update" requests
29 + * Handle "Update" requests.
25 30 *
26 - * @param array $array The activity-object
27 - * @param int $user_id The id of the local blog-user
31 + * @param array $activity The Activity object.
32 + * @param int[] $user_ids The user IDs. Always null for Update activities.
33 + * @param \Activitypub\Activity\Activity $activity_object The activity object. Default null.
28 34 */
29 - public static function handle_update( $array ) {
30 - $object_type = isset( $array['object']['type'] ) ? $array['object']['type'] : '';
35 + public static function handle_update( $activity, $user_ids, $activity_object ) {
36 + $object_type = $activity['object']['type'] ?? '';
31 37
32 38 switch ( $object_type ) {
33 - // Actor Types
34 - // @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
39 + /*
40 + * Actor Types.
41 + *
42 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
43 + */
35 44 case 'Person':
36 45 case 'Group':
37 46 case 'Organization':
38 47 case 'Service':
39 48 case 'Application':
40 - self::update_actor( $array );
49 + self::update_actor( $activity, $user_ids );
41 50 break;
42 - // Object and Link Types
43 - // @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
51 +
52 + /*
53 + * Object and Link Types.
54 + *
55 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
56 + */
44 57 case 'Note':
45 58 case 'Article':
46 59 case 'Image':
47 60 case 'Audio':
@@ -47,12 +60,16 @@
47 60 case 'Audio':
48 61 case 'Video':
49 62 case 'Event':
50 63 case 'Document':
51 - self::update_interaction( $array );
64 + self::update_object( $activity, $user_ids, $activity_object );
52 65 break;
53 - // Minimal Activity
54 - // @see https://www.w3.org/TR/activitystreams-core/#example-1
66 +
67 + /*
68 + * Minimal Activity.
69 + *
70 + * @see https://www.w3.org/TR/activitystreams-core/#example-1
71 + */
55 72 default:
56 73 break;
57 74 }
58 75 }
@@ -57,39 +74,76 @@
57 74 }
58 75 }
59 76
60 77 /**
61 - * Update an Interaction
78 + * Update an Object.
62 79 *
63 - * @param array $activity The activity-object
64 - * @param int $user_id The id of the local blog-user
65 - *
66 - * @return void
80 + * @param array $activity The Activity object.
81 + * @param int[]|null $user_ids The user IDs. Always null for Update activities.
82 + * @param \Activitypub\Activity\Activity $activity_object The activity object. Default null.
67 83 */
68 - public static function update_interaction( $activity ) {
69 - $commentdata = Interactions::update_comment( $activity );
70 - $reaction = null;
84 + public static function update_object( $activity, $user_ids, $activity_object ) {
85 + $result = new \WP_Error( 'activitypub_update_failed', 'Update failed' );
86 + $updated = true;
71 87
72 - if ( ! empty( $commentdata['comment_ID'] ) ) {
73 - $state = 1;
74 - $reaction = \get_comment( $commentdata['comment_ID'] );
75 - } else {
76 - $state = $commentdata;
88 + // Check for private and/or direct messages.
89 + if ( is_activity_reply( $activity ) ) {
90 + $comment_data = Interactions::update_comment( $activity );
91 +
92 + if ( false === $comment_data ) {
93 + $updated = false;
94 + } elseif ( ! empty( $comment_data['comment_ID'] ) ) {
95 + $result = \get_comment( $comment_data['comment_ID'] );
96 + }
97 + } elseif ( \get_option( 'activitypub_create_posts', false ) ) {
98 + $result = Posts::update( $activity, $user_ids );
99 +
100 + if ( \is_wp_error( $result ) && 'activitypub_post_not_found' === $result->get_error_code() ) {
101 + $updated = false;
102 + }
77 103 }
78 104
79 - \do_action( 'activitypub_handled_update', $activity, null, $state, $reaction );
105 + // There is no object to update, try to trigger create instead.
106 + if ( ! $updated ) {
107 + return Create::handle_create( $activity, $user_ids, $activity_object );
108 + }
109 +
110 + $success = ( $result && ! \is_wp_error( $result ) );
111 +
112 + /**
113 + * Fires after an ActivityPub Update activity has been handled.
114 + *
115 + * @param array $activity The ActivityPub activity data.
116 + * @param int[]|null $user_ids The local user IDs.
117 + * @param bool $success True on success, false otherwise.
118 + * @param \WP_Comment|\WP_Post|\WP_Error $result The updated post, comment, or error.
119 + */
120 + \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $success, $result );
80 121 }
81 122
82 123 /**
83 - * Update an Actor
124 + * Update an Actor.
84 125 *
85 - * @param array $activity The activity-object
86 - *
87 - * @return void
126 + * @param array $activity The Activity object.
127 + * @param int[]|null $user_ids The user IDs. Always null for Update activities.
88 128 */
89 - public static function update_actor( $activity ) {
90 - // update cache
91 - get_remote_metadata_by_actor( $activity['actor'], false );
129 + public static function update_actor( $activity, $user_ids ) {
130 + // Update cache.
131 + $actor = get_remote_metadata_by_actor( $activity['actor'], false );
92 132
93 - // @todo maybe also update all interactions
133 + if ( ! $actor || \is_wp_error( $actor ) || ! isset( $actor['id'] ) ) {
134 + $state = new \WP_Error( 'activitypub_update_failed', 'Update failed: could not fetch actor data' );
135 + } else {
136 + $state = Remote_Actors::upsert( $actor );
137 + }
138 +
139 + /**
140 + * Fires after an ActivityPub Update activity has been handled.
141 + *
142 + * @param array $activity The ActivityPub activity data.
143 + * @param int[] $user_ids The local user IDs.
144 + * @param int|\WP_Error $state Actor post ID on success, WP_Error on failure.
145 + * @param array $actor Remote actor meta data.
146 + */
147 + \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $state, $actor );
94 148 }
95 149 }