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-update.php +95 -35 1.3.07.7.0 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,35 +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( 'activitypub_inbox_update', array( self::class, 'handle_update' ), 10, 2 );
25 + \add_action( 'activitypub_handled_inbox_update', array( self::class, 'handle_update' ), 10, 3 );
18 26 }
19 27
20 28 /**
21 - * Handle "Update" requests
29 + * Handle "Update" requests.
22 30 *
23 - * @param array $array The activity-object
24 - * @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.
25 34 */
26 - public static function handle_update( $array, $user_id ) {
27 - $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'] ?? '';
28 37
29 38 switch ( $object_type ) {
30 - // Actor Types
31 - // @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 + */
32 44 case 'Person':
33 45 case 'Group':
34 46 case 'Organization':
35 47 case 'Service':
36 48 case 'Application':
37 - self::update_actor( $array );
49 + self::update_actor( $activity, $user_ids );
38 50 break;
39 - // Object and Link Types
40 - // @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 + */
41 57 case 'Note':
42 58 case 'Article':
43 59 case 'Image':
44 60 case 'Audio':
@@ -44,12 +60,16 @@
44 60 case 'Audio':
45 61 case 'Video':
46 62 case 'Event':
47 63 case 'Document':
48 - self::update_interaction( $array, $user_id );
64 + self::update_object( $activity, $user_ids, $activity_object );
49 65 break;
50 - // Minimal Activity
51 - // @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 + */
52 72 default:
53 73 break;
54 74 }
55 75 }
@@ -54,36 +74,76 @@
54 74 }
55 75 }
56 76
57 77 /**
58 - * Update an Interaction
78 + * Update an Object.
59 79 *
60 - * @param array $activity The activity-object
61 - * @param int $user_id The id of the local blog-user
62 - *
63 - * @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.
64 83 */
65 - public static function update_interaction( $activity, $user_id ) {
66 - $state = Interactions::update_comment( $activity );
67 - $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;
68 87
69 - if ( $state && ! \is_wp_error( $reaction ) ) {
70 - $reaction = \get_comment( $state );
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 + }
71 103 }
72 104
73 - \do_action( 'activitypub_handled_update', $activity, $user_id, $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 );
74 121 }
75 122
76 123 /**
77 - * Update an Actor
124 + * Update an Actor.
78 125 *
79 - * @param array $activity The activity-object
80 - *
81 - * @return void
126 + * @param array $activity The Activity object.
127 + * @param int[]|null $user_ids The user IDs. Always null for Update activities.
82 128 */
83 - public static function update_actor( $activity ) {
84 - // update cache
85 - 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 );
86 132
87 - // @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 );
88 148 }
89 149 }