| 1 |
<?php |
| 2 |
/** |
| 3 |
* Outbox Remove handler file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Handler\Outbox; |
| 9 |
|
| 10 |
use Activitypub\Collection\Actors; |
| 11 |
|
| 12 |
use function Activitypub\object_to_uri; |
| 13 |
|
| 14 |
/** |
| 15 |
* Handle outgoing Remove activities. |
| 16 |
* |
| 17 |
* Supports removing objects from an actor's featured collection |
| 18 |
* by unsticking the corresponding WordPress post. |
| 19 |
*/ |
| 20 |
class Remove { |
| 21 |
/** |
| 22 |
* Initialize the class, registering WordPress hooks. |
| 23 |
*/ |
| 24 |
public static function init() { |
| 25 |
\add_filter( 'activitypub_outbox_remove', array( self::class, 'handle_remove' ), 10, 2 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Handle outgoing "Remove" activities from local actors. |
| 30 |
* |
| 31 |
* When the target is the actor's featured collection, the referenced |
| 32 |
* post is unstuck. The unstick action triggers the scheduler which |
| 33 |
* creates the outbox entry automatically. |
| 34 |
* |
| 35 |
* @since 8.1.0 |
| 36 |
* |
| 37 |
* @param array $data The activity data array. |
| 38 |
* @param int $user_id The user ID. |
| 39 |
* |
| 40 |
* @return \WP_Post|\WP_Error|array The post object on success, WP_Error on failure, or original data if unhandled. |
| 41 |
*/ |
| 42 |
public static function handle_remove( $data, $user_id = null ) { |
| 43 |
$object_uri = object_to_uri( $data['object'] ?? '' ); |
| 44 |
$target = object_to_uri( $data['target'] ?? '' ); |
| 45 |
|
| 46 |
if ( empty( $object_uri ) || empty( $target ) ) { |
| 47 |
return $data; |
| 48 |
} |
| 49 |
|
| 50 |
$actor = Actors::get_by_id( $user_id ); |
| 51 |
|
| 52 |
if ( \is_wp_error( $actor ) ) { |
| 53 |
return $actor; |
| 54 |
} |
| 55 |
|
| 56 |
// Only handle featured collection targets. |
| 57 |
if ( $target !== $actor->get_featured() ) { |
| 58 |
return $data; |
| 59 |
} |
| 60 |
|
| 61 |
$post_id = \url_to_postid( $object_uri ); |
| 62 |
|
| 63 |
if ( ! $post_id ) { |
| 64 |
return new \WP_Error( |
| 65 |
'activitypub_object_not_found', |
| 66 |
\__( 'The referenced object was not found.', 'activitypub' ), |
| 67 |
array( 'status' => 404 ) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
$post = \get_post( $post_id ); |
| 72 |
|
| 73 |
if ( ! $post ) { |
| 74 |
return new \WP_Error( |
| 75 |
'activitypub_object_not_found', |
| 76 |
\__( 'The referenced object was not found.', 'activitypub' ), |
| 77 |
array( 'status' => 404 ) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
// Verify the user owns this post. |
| 82 |
if ( $user_id > 0 && (int) $post->post_author !== $user_id ) { |
| 83 |
return new \WP_Error( |
| 84 |
'activitypub_forbidden', |
| 85 |
\__( 'You can only unfeature your own posts.', 'activitypub' ), |
| 86 |
array( 'status' => 403 ) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
// Unsticking the post triggers the scheduler which adds to outbox. |
| 91 |
\unstick_post( $post_id ); |
| 92 |
|
| 93 |
return $post; |
| 94 |
} |
| 95 |
} |
| 96 |
|