| @@ -1,42 +1,43 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Like handler file. | |
| 4 | - * | |
| 5 | - * @package Activitypub | |
| 6 | - */ | |
| 7 | - | |
| 8 | 2 | namespace Activitypub\Handler; |
| 9 | 3 | |
| 4 | +use Activitypub\Comment; | |
| 10 | 5 | use Activitypub\Collection\Interactions; |
| 11 | -use Activitypub\Comment; | |
| 12 | 6 | |
| 13 | 7 | use function Activitypub\object_to_uri; |
| 14 | 8 | |
| 15 | 9 | /** |
| 16 | - * Handle Like requests. | |
| 10 | + * Handle Like requests | |
| 17 | 11 | */ |
| 18 | 12 | class Like { |
| 19 | 13 | /** |
| 20 | - * Initialize the class, registering WordPress hooks. | |
| 14 | + * Initialize the class, registering WordPress hooks | |
| 21 | 15 | */ |
| 22 | 16 | 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' ) ); | |
| 17 | + \add_action( | |
| 18 | + 'activitypub_inbox_like', | |
| 19 | + array( self::class, 'handle_like' ), | |
| 20 | + 10, | |
| 21 | + 3 | |
| 22 | + ); | |
| 25 | 23 | } |
| 26 | 24 | |
| 27 | 25 | /** |
| 28 | - * Handles "Like" requests. | |
| 26 | + * Handles "Like" requests | |
| 29 | 27 | * |
| 30 | - * @param array $like The Activity array. | |
| 31 | - * @param int|int[] $user_ids The user ID(s). | |
| 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 | |
| 32 | 33 | */ |
| 33 | - public static function handle_like( $like, $user_ids ) { | |
| 34 | - if ( ! Comment::is_comment_type_enabled( 'like' ) ) { | |
| 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 ) { | |
| 35 | 36 | return; |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | - $url = object_to_uri( $like['object'] ); | |
| 39 | + $url = object_to_uri( $array['object'] ); | |
| 39 | 40 | |
| 40 | 41 | if ( empty( $url ) ) { |
| 41 | 42 | return; |
| 42 | 43 | } |
| @@ -45,37 +46,14 @@ | ||
| 45 | 46 | if ( $exists ) { |
| 46 | 47 | return; |
| 47 | 48 | } |
| 48 | 49 | |
| 49 | - $success = false; | |
| 50 | - $result = Interactions::add_reaction( $like ); | |
| 50 | + $state = Interactions::add_reaction( $array ); | |
| 51 | + $reaction = null; | |
| 51 | 52 | |
| 52 | - if ( $result && ! is_wp_error( $result ) ) { | |
| 53 | - $success = true; | |
| 54 | - $result = get_comment( $result ); | |
| 53 | + if ( $state && ! is_wp_error( $state ) ) { | |
| 54 | + $reaction = get_comment( $state ); | |
| 55 | 55 | } |
| 56 | 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; | |
| 57 | + do_action( 'activitypub_handled_like', $array, $user_id, $state, $reaction ); | |
| 80 | 58 | } |
| 81 | 59 | } |