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

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