PluginProbe
ActivityPub / 8.2.0
ActivityPub v8.2.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
activitypub / includes / handler / class-update.php

class-update.php in ActivityPub 8.2.0, at includes/handler/class-update.php

169 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Update handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler;
9
10 use Activitypub\Collection\Interactions;
11 use Activitypub\Collection\Remote_Actors;
12 use Activitypub\Collection\Remote_Posts;
13 use Activitypub\Http;
14
15 use function Activitypub\is_activity_reply;
16
17 /**
18 * Handle Update requests.
19 */
20 class Update {
21 /**
22 * Initialize the class, registering WordPress hooks.
23 */
24 public static function init() {
25 \add_action( 'activitypub_handled_inbox_update', array( self::class, 'handle_update' ), 10, 3 );
26 }
27
28 /**
29 * Handle "Update" requests.
30 *
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.
34 */
35 public static function handle_update( $activity, $user_ids, $activity_object ) {
36 $object_type = $activity['object']['type'] ?? '';
37
38 switch ( $object_type ) {
39 /*
40 * Actor Types.
41 *
42 * @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
43 */
44 case 'Person':
45 case 'Group':
46 case 'Organization':
47 case 'Service':
48 case 'Application':
49 self::update_actor( $activity, $user_ids );
50 break;
51
52 /*
53 * Object and Link Types.
54 *
55 * @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
56 */
57 case 'Note':
58 case 'Article':
59 case 'Image':
60 case 'Audio':
61 case 'Video':
62 case 'Event':
63 case 'Document':
64 self::update_object( $activity, $user_ids, $activity_object );
65 break;
66
67 /*
68 * Minimal Activity.
69 *
70 * @see https://www.w3.org/TR/activitystreams-core/#example-1
71 */
72 default:
73 break;
74 }
75 }
76
77 /**
78 * Update an Object.
79 *
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.
83 */
84 public static function update_object( $activity, $user_ids, $activity_object ) {
85 $result = new \WP_Error( 'activitypub_update_failed', 'Update failed' );
86 $updated = true;
87
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 = Remote_Posts::update( $activity, $user_ids );
99
100 if ( \is_wp_error( $result ) && 'activitypub_post_not_found' === $result->get_error_code() ) {
101 $updated = false;
102 }
103 }
104
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 );
121 }
122
123 /**
124 * Update an Actor.
125 *
126 * @param array $activity The Activity object.
127 * @param int[]|null $user_ids The user IDs. Always null for Update activities.
128 */
129 public static function update_actor( $activity, $user_ids ) {
130 /*
131 * Prefer the actor data embedded in the activity object, as it contains
132 * the fresh data sent by the remote server.
133 */
134 $actor = $activity['object'] ?? null;
135
136 /*
137 * The object may be a string IRI instead of an embedded object,
138 * in which case we need to fetch the actor data remotely.
139 * We use Http::get_remote_object() directly instead of
140 * get_remote_metadata_by_actor() because the latter returns the
141 * stale locally cached copy via fetch_by_uri().
142 */
143 if ( ! \is_array( $actor ) || ! isset( $actor['id'] ) ) {
144 $object = Http::get_remote_object( $activity['actor'], false );
145
146 if ( ! \is_wp_error( $object ) && \is_array( $object ) ) {
147 $actor = $object;
148 }
149 }
150
151 if ( \is_array( $actor ) && isset( $actor['id'] ) ) {
152 $state = Remote_Actors::upsert( $actor );
153 } else {
154 $state = new \WP_Error( 'activitypub_update_failed', 'Update failed: missing or invalid actor object in Update activity' );
155 $actor = array();
156 }
157
158 /**
159 * Fires after an ActivityPub Update activity has been handled.
160 *
161 * @param array $activity The ActivityPub activity data.
162 * @param int[] $user_ids The local user IDs.
163 * @param int|\WP_Error $state Actor post ID on success, WP_Error on failure.
164 * @param array $actor Remote actor meta data.
165 */
166 \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $state, $actor );
167 }
168 }
169