| 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 |
$exists = Comment::object_id_to_comment( esc_url_raw( $url ) ); |
| 45 |
if ( $exists ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
$success = false; |
| 50 |
$result = Interactions::add_reaction( $like ); |
| 51 |
|
| 52 |
if ( $result && ! is_wp_error( $result ) ) { |
| 53 |
$success = true; |
| 54 |
$result = get_comment( $result ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Fires after an ActivityPub Like activity has been handled. |
| 59 |
* |
| 60 |
* @param array $like The ActivityPub activity data. |
| 61 |
* @param int[] $user_ids The local user IDs. |
| 62 |
* @param bool $success True on success, false otherwise. |
| 63 |
* @param array|false|int|string|\WP_Comment|\WP_Error $result The WP_Comment object of the created like comment, or null if creation failed. |
| 64 |
*/ |
| 65 |
\do_action( 'activitypub_handled_like', $like, (array) $user_ids, $success, $result ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Set the object to the object ID. |
| 70 |
* |
| 71 |
* @param \Activitypub\Activity\Activity $activity The Activity object. |
| 72 |
* @return \Activitypub\Activity\Activity The filtered Activity object. |
| 73 |
*/ |
| 74 |
public static function outbox_activity( $activity ) { |
| 75 |
if ( 'Like' === $activity->get_type() ) { |
| 76 |
$activity->set_object( object_to_uri( $activity->get_object() ) ); |
| 77 |
} |
| 78 |
|
| 79 |
return $activity; |
| 80 |
} |
| 81 |
} |
| 82 |
|