PluginProbe
ActivityPub / 7.6.1
ActivityPub v7.6.1
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 7.6.1, at includes/handler/class-update.php

158 lines 4.9 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\Posts;
12 use Activitypub\Collection\Remote_Actors;
13
14 use function Activitypub\get_remote_metadata_by_actor;
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 = 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 /**
108 * Fires when a Create activity is received for an existing object.
109 *
110 * @param array $activity The activity-object.
111 * @param int[] $user_ids The ids of the local blog-users.
112 * @param \Activitypub\Activity\Activity $activity_object The activity object.
113 */
114 \do_action( 'activitypub_inbox_create', $activity, $user_ids, $activity_object );
115 return false;
116 }
117
118 $success = ( $result && ! \is_wp_error( $result ) );
119
120 /**
121 * Fires after an ActivityPub Update activity has been handled.
122 *
123 * @param array $activity The ActivityPub activity data.
124 * @param int[]|null $user_ids The local user IDs.
125 * @param bool $success True on success, false otherwise.
126 * @param \WP_Comment|\WP_Post|\WP_Error $result The updated post, comment, or error.
127 */
128 \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $success, $result );
129 }
130
131 /**
132 * Update an Actor.
133 *
134 * @param array $activity The Activity object.
135 * @param int[]|null $user_ids The user IDs. Always null for Update activities.
136 */
137 public static function update_actor( $activity, $user_ids ) {
138 // Update cache.
139 $actor = get_remote_metadata_by_actor( $activity['actor'], false );
140
141 if ( ! $actor || \is_wp_error( $actor ) || ! isset( $actor['id'] ) ) {
142 $state = new \WP_Error( 'activitypub_update_failed', 'Update failed: could not fetch actor data' );
143 } else {
144 $state = Remote_Actors::upsert( $actor );
145 }
146
147 /**
148 * Fires after an ActivityPub Update activity has been handled.
149 *
150 * @param array $activity The ActivityPub activity data.
151 * @param int[] $user_ids The local user IDs.
152 * @param int|\WP_Error $state Actor post ID on success, WP_Error on failure.
153 * @param array $actor Remote actor meta data.
154 */
155 \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $state, $actor );
156 }
157 }
158