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

150 lines 4.5 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 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 // Update cache.
131 $actor = get_remote_metadata_by_actor( $activity['actor'], false );
132
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 );
148 }
149 }
150