PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
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-like.php

class-like.php in ActivityPub trunk, at includes/handler/class-like.php

88 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Like handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler;
9
10 use Activitypub\Collection\Interactions;
11 use Activitypub\Comment;
12
13 use function Activitypub\object_to_uri;
14
15 /**
16 * Handle Like requests.
17 */
18 class Like {
19 /**
20 * Initialize the class, registering WordPress hooks.
21 */
22 public static function init() {
23 \add_action( 'activitypub_inbox_like', array( self::class, 'handle_like' ), 10, 2 );
24 \add_filter( 'activitypub_get_outbox_activity', array( self::class, 'outbox_activity' ) );
25 }
26
27 /**
28 * Handles "Like" requests.
29 *
30 * @param array $like The Activity array.
31 * @param int|int[] $user_ids The user ID(s).
32 */
33 public static function handle_like( $like, $user_ids ) {
34 if ( ! Comment::is_comment_type_enabled( 'like' ) ) {
35 return;
36 }
37
38 $url = object_to_uri( $like['object'] );
39
40 if ( empty( $url ) ) {
41 return;
42 }
43
44 /*
45 * Dedupe on the Like activity ID (its source_id), not the liked object, so repeat
46 * deliveries stay idempotent even when a mangled author name defeats WordPress's own
47 * duplicate check. Match any status, so a like that was marked as spam or trashed
48 * still counts as seen. See https://github.com/Automattic/wordpress-activitypub/issues/3215.
49 */
50 $exists = Comment::object_id_to_comment( \esc_url_raw( object_to_uri( $like ) ), array( 'status' => 'any' ) );
51 if ( $exists ) {
52 return;
53 }
54
55 $success = false;
56 $result = Interactions::add_reaction( $like );
57
58 if ( $result && ! \is_wp_error( $result ) ) {
59 $success = true;
60 $result = \get_comment( $result );
61 }
62
63 /**
64 * Fires after an ActivityPub Like activity has been handled.
65 *
66 * @param array $like The ActivityPub activity data.
67 * @param int[] $user_ids The local user IDs.
68 * @param bool $success True on success, false otherwise.
69 * @param array|false|int|string|\WP_Comment|\WP_Error $result The WP_Comment object of the created like comment, or null if creation failed.
70 */
71 \do_action( 'activitypub_handled_like', $like, (array) $user_ids, $success, $result );
72 }
73
74 /**
75 * Set the object to the object ID.
76 *
77 * @param \Activitypub\Activity\Activity $activity The Activity object.
78 * @return \Activitypub\Activity\Activity The filtered Activity object.
79 */
80 public static function outbox_activity( $activity ) {
81 if ( 'Like' === $activity->get_type() ) {
82 $activity->set_object( object_to_uri( $activity->get_object() ) );
83 }
84
85 return $activity;
86 }
87 }
88