PluginProbe
ActivityPub / 7.8.2
ActivityPub v7.8.2
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.8.2, at includes/handler/class-create.php

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