PluginProbe
ActivityPub / 7.6.0
ActivityPub v7.6.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-create.php

class-create.php in ActivityPub 7.6.0, at includes/handler/class-create.php

163 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Create handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler;
9
10 use Activitypub\Collection\Interactions;
11 use Activitypub\Collection\Posts;
12
13 use function Activitypub\get_activity_visibility;
14 use function Activitypub\is_activity_reply;
15 use function Activitypub\is_self_ping;
16 use function Activitypub\object_id_to_comment;
17
18 /**
19 * Handle Create requests.
20 */
21 class Create {
22 /**
23 * Initialize the class, registering WordPress hooks.
24 */
25 public static function init() {
26 \add_action( 'activitypub_handled_inbox_create', array( self::class, 'handle_create' ), 10, 3 );
27 \add_filter( 'activitypub_validate_object', array( self::class, 'validate_object' ), 10, 3 );
28 }
29
30 /**
31 * Handles "Create" requests.
32 *
33 * @param array $activity The activity-object.
34 * @param int|int[] $user_ids The id(s) of the local blog-user(s).
35 * @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null.
36 */
37 public static function handle_create( $activity, $user_ids, $activity_object = null ) {
38 // Check for private and/or direct messages.
39 if ( ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE === get_activity_visibility( $activity ) ) {
40 $result = false;
41 } elseif ( is_activity_reply( $activity ) ) { // Check for replies.
42 $result = self::create_interaction( $activity, $user_ids, $activity_object );
43 } elseif ( \get_option( 'activitypub_create_posts', false ) ) { // Handle non-interaction objects.
44 $result = self::create_post( $activity, $user_ids, $activity_object );
45 } else {
46 $result = false;
47 }
48
49 if ( false === $result ) {
50 return;
51 }
52
53 $success = ! \is_wp_error( $result );
54
55 /**
56 * Fires after an ActivityPub Create activity has been handled.
57 *
58 * @param array $activity The ActivityPub activity data.
59 * @param int[] $user_ids The local user IDs.
60 * @param bool $success True on success, false otherwise.
61 * @param \WP_Comment|\WP_Post|\WP_Error $result The WP_Comment object of the created comment, or null if creation failed.
62 */
63 \do_action( 'activitypub_handled_create', $activity, (array) $user_ids, $success, $result );
64 }
65
66 /**
67 * Handle interactions like replies.
68 *
69 * @param array $activity The activity-object.
70 * @param int[] $user_ids The ids of the local blog-users.
71 * @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null.
72 *
73 * @return \WP_Comment|\WP_Error|false The created comment, WP_Error on failure, false if not processed.
74 */
75 public static function create_interaction( $activity, $user_ids, $activity_object = null ) {
76 $check_dupe = object_id_to_comment( $activity['object']['id'] );
77
78 // If comment exists, call update action.
79 if ( $check_dupe ) {
80 /**
81 * Fires when a Create activity is received for an existing comment.
82 *
83 * @param array $activity The activity-object.
84 * @param int[] $user_ids The ids of the local blog-users.
85 * @param \Activitypub\Activity\Activity $activity_object The activity object.
86 */
87 \do_action( 'activitypub_inbox_update', $activity, (array) $user_ids, $activity_object );
88 return false;
89 }
90
91 if ( is_self_ping( $activity['object']['id'] ) ) {
92 return false;
93 }
94
95 $result = Interactions::add_comment( $activity );
96
97 if ( ! $result || \is_wp_error( $result ) ) {
98 return $result;
99 }
100
101 return \get_comment( $result );
102 }
103
104 /**
105 * Handle non-interaction posts like posts.
106 *
107 * @param array $activity The activity-object.
108 * @param int[] $user_ids The ids of the local blog-users.
109 * @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null.
110 *
111 * @return \WP_Post|\WP_Error|false The post on success or WP_Error on failure.
112 */
113 public static function create_post( $activity, $user_ids, $activity_object = null ) {
114 $check_dupe = Posts::get_by_guid( $activity['object']['id'] );
115
116 // If comment exists, call update action.
117 if ( ! \is_wp_error( $check_dupe ) ) {
118 /**
119 * Fires when a Create activity is received for an existing object.
120 *
121 * @param array $activity The activity-object.
122 * @param int[] $user_ids The id of the local blog-user.
123 * @param \Activitypub\Activity\Activity $activity_object The activity object.
124 */
125 \do_action( 'activitypub_inbox_update', $activity, (array) $user_ids, $activity_object );
126 return false;
127 }
128
129 return Posts::add( $activity, $user_ids );
130 }
131
132 /**
133 * Validate the object.
134 *
135 * @param bool $valid The validation state.
136 * @param string $param The object parameter.
137 * @param \WP_REST_Request $request The request object.
138 *
139 * @return bool The validation state: true if valid, false if not.
140 */
141 public static function validate_object( $valid, $param, $request ) {
142 $activity = $request->get_json_params();
143
144 if ( empty( $activity['type'] ) ) {
145 return false;
146 }
147
148 if ( 'Create' !== $activity['type'] ) {
149 return $valid;
150 }
151
152 if ( ! isset( $activity['object'] ) || ! \is_array( $activity['object'] ) ) {
153 return false;
154 }
155
156 if ( ! isset( $activity['object']['id'], $activity['object']['content'] ) ) {
157 return false;
158 }
159
160 return $valid;
161 }
162 }
163