PluginProbe
ActivityPub / 4.2.1
ActivityPub v4.2.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-create.php

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

127 lines 3.2 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
12 use function Activitypub\is_activity_public;
13 use function Activitypub\object_id_to_comment;
14
15 /**
16 * Handle Create requests.
17 */
18 class Create {
19 /**
20 * Initialize the class, registering WordPress hooks.
21 */
22 public static function init() {
23 \add_action(
24 'activitypub_inbox_create',
25 array( self::class, 'handle_create' ),
26 10,
27 3
28 );
29
30 \add_filter(
31 'activitypub_validate_object',
32 array( self::class, 'validate_object' ),
33 10,
34 3
35 );
36 }
37
38 /**
39 * Handles "Create" requests.
40 *
41 * @param array $activity The activity-object.
42 * @param int $user_id The id of the local blog-user.
43 * @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null.
44 */
45 public static function handle_create( $activity, $user_id, $activity_object = null ) {
46 // Check if Activity is public or not.
47 if ( ! is_activity_public( $activity ) ) {
48 // @todo maybe send email.
49 return;
50 }
51
52 $check_dupe = object_id_to_comment( $activity['object']['id'] );
53
54 // If comment exists, call update action.
55 if ( $check_dupe ) {
56 /**
57 * Fires when a Create activity is received for an existing comment.
58 *
59 * @param array $activity The activity-object.
60 * @param int $user_id The id of the local blog-user.
61 * @param \WP_Comment|\WP_Error $check_dupe The comment object or WP_Error.
62 * @param \Activitypub\Activity\Activity $activity_object The activity object.
63 */
64 \do_action( 'activitypub_inbox_update', $activity, $user_id, $activity_object );
65 return;
66 }
67
68 $state = Interactions::add_comment( $activity );
69 $reaction = null;
70
71 if ( $state && ! \is_wp_error( $state ) ) {
72 $reaction = \get_comment( $state );
73 }
74
75 /**
76 * Fires after a Create activity has been handled.
77 *
78 * @param array $activity The activity-object.
79 * @param int $user_id The id of the local blog-user.
80 * @param \WP_Comment|\WP_Error $state The comment object or WP_Error.
81 * @param \WP_Comment|\WP_Error|null $reaction The reaction object or WP_Error.
82 */
83 \do_action( 'activitypub_handled_create', $activity, $user_id, $state, $reaction );
84 }
85
86 /**
87 * Validate the object.
88 *
89 * @param bool $valid The validation state.
90 * @param string $param The object parameter.
91 * @param \WP_REST_Request $request The request object.
92 *
93 * @return bool The validation state: true if valid, false if not.
94 */
95 public static function validate_object( $valid, $param, $request ) {
96 $json_params = $request->get_json_params();
97
98 if ( empty( $json_params['type'] ) ) {
99 return false;
100 }
101
102 if (
103 'Create' !== $json_params['type'] ||
104 is_wp_error( $request )
105 ) {
106 return $valid;
107 }
108
109 $object = $json_params['object'];
110 $required = array(
111 'id',
112 'inReplyTo',
113 'content',
114 );
115
116 if ( ! is_array( $object ) ) {
117 return false;
118 }
119
120 if ( array_intersect( $required, array_keys( $object ) ) !== $required ) {
121 return false;
122 }
123
124 return $valid;
125 }
126 }
127