PluginProbe
ActivityPub / 4.3.0
ActivityPub v4.3.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 4.3.0, at includes/handler/class-create.php

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