PluginProbe
ActivityPub / 5.9.0
ActivityPub v5.9.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-like.php

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

81 lines 1.9 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\Comment;
11 use Activitypub\Collection\Interactions;
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 $user_id The ID of the local blog user.
32 */
33 public static function handle_like( $like, $user_id ) {
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 $exists = Comment::object_id_to_comment( esc_url_raw( $url ) );
45 if ( $exists ) {
46 return;
47 }
48
49 $state = Interactions::add_reaction( $like );
50 $reaction = null;
51
52 if ( $state && ! is_wp_error( $state ) ) {
53 $reaction = get_comment( $state );
54 }
55
56 /**
57 * Fires after a Like has been handled.
58 *
59 * @param array $like The Activity array.
60 * @param int $user_id The ID of the local blog user.
61 * @param mixed $state The state of the reaction.
62 * @param mixed $reaction The reaction object.
63 */
64 do_action( 'activitypub_handled_like', $like, $user_id, $state, $reaction );
65 }
66
67 /**
68 * Set the object to the object ID.
69 *
70 * @param \Activitypub\Activity\Activity $activity The Activity object.
71 * @return \Activitypub\Activity\Activity The filtered Activity object.
72 */
73 public static function outbox_activity( $activity ) {
74 if ( 'Like' === $activity->get_type() ) {
75 $activity->set_object( object_to_uri( $activity->get_object() ) );
76 }
77
78 return $activity;
79 }
80 }
81