| 1 |
<?php |
| 2 |
namespace Activitypub\Handler; |
| 3 |
|
| 4 |
use Activitypub\Comment; |
| 5 |
use Activitypub\Collection\Interactions; |
| 6 |
|
| 7 |
use function Activitypub\object_to_uri; |
| 8 |
|
| 9 |
/** |
| 10 |
* Handle Like requests |
| 11 |
*/ |
| 12 |
class Like { |
| 13 |
/** |
| 14 |
* Initialize the class, registering WordPress hooks |
| 15 |
*/ |
| 16 |
public static function init() { |
| 17 |
\add_action( |
| 18 |
'activitypub_inbox_like', |
| 19 |
array( self::class, 'handle_like' ), |
| 20 |
10, |
| 21 |
3 |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Handles "Like" requests |
| 27 |
* |
| 28 |
* @param array $array The Activity array. |
| 29 |
* @param int $user_id The ID of the local blog user. |
| 30 |
* @param \Activitypub\Activity $activity The Activity object. |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public static function handle_like( $array, $user_id, $activity = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound,VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable,Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 35 |
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$url = object_to_uri( $array['object'] ); |
| 40 |
|
| 41 |
if ( empty( $url ) ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$exists = Comment::object_id_to_comment( esc_url_raw( $url ) ); |
| 46 |
if ( $exists ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$state = Interactions::add_reaction( $array ); |
| 51 |
$reaction = null; |
| 52 |
|
| 53 |
if ( $state && ! is_wp_error( $state ) ) { |
| 54 |
$reaction = get_comment( $state ); |
| 55 |
} |
| 56 |
|
| 57 |
do_action( 'activitypub_handled_like', $array, $user_id, $state, $reaction ); |
| 58 |
} |
| 59 |
} |
| 60 |
|