| 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( |
| 24 |
'activitypub_inbox_like', |
| 25 |
array( self::class, 'handle_like' ), |
| 26 |
10, |
| 27 |
3 |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Handles "Like" requests. |
| 33 |
* |
| 34 |
* @param array $like The Activity array. |
| 35 |
* @param int $user_id The ID of the local blog user. |
| 36 |
*/ |
| 37 |
public static function handle_like( $like, $user_id ) { |
| 38 |
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$url = object_to_uri( $like['object'] ); |
| 43 |
|
| 44 |
if ( empty( $url ) ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$exists = Comment::object_id_to_comment( esc_url_raw( $url ) ); |
| 49 |
if ( $exists ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$state = Interactions::add_reaction( $like ); |
| 54 |
$reaction = null; |
| 55 |
|
| 56 |
if ( $state && ! is_wp_error( $state ) ) { |
| 57 |
$reaction = get_comment( $state ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Fires after a Like has been handled. |
| 62 |
* |
| 63 |
* @param array $like The Activity array. |
| 64 |
* @param int $user_id The ID of the local blog user. |
| 65 |
* @param mixed $state The state of the reaction. |
| 66 |
* @param mixed $reaction The reaction object. |
| 67 |
*/ |
| 68 |
do_action( 'activitypub_handled_like', $like, $user_id, $state, $reaction ); |
| 69 |
} |
| 70 |
} |
| 71 |
|