| 1 |
<?php |
| 2 |
/** |
| 3 |
* Outbox Block handler file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Handler\Outbox; |
| 9 |
|
| 10 |
use Activitypub\Moderation; |
| 11 |
|
| 12 |
use function Activitypub\add_to_outbox; |
| 13 |
use function Activitypub\object_to_uri; |
| 14 |
|
| 15 |
/** |
| 16 |
* Handle outgoing Block activities. |
| 17 |
*/ |
| 18 |
class Block { |
| 19 |
/** |
| 20 |
* Initialize the class, registering WordPress hooks. |
| 21 |
*/ |
| 22 |
public static function init() { |
| 23 |
\add_filter( 'activitypub_outbox_block', array( self::class, 'handle_block' ), 10, 2 ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Handle outgoing "Block" activities from local actors. |
| 28 |
* |
| 29 |
* Blocks a remote actor using the Moderation system, then adds |
| 30 |
* the activity to the outbox for federation. |
| 31 |
* |
| 32 |
* @since 8.1.0 |
| 33 |
* |
| 34 |
* @param array $data The activity data array. |
| 35 |
* @param int $user_id The user ID. |
| 36 |
* |
| 37 |
* @return array|int|\WP_Error The original data if unhandled, outbox post ID on success, or WP_Error on failure. |
| 38 |
*/ |
| 39 |
public static function handle_block( $data, $user_id = null ) { |
| 40 |
$actor_uri = object_to_uri( $data['object'] ?? '' ); |
| 41 |
|
| 42 |
if ( empty( $actor_uri ) ) { |
| 43 |
return $data; |
| 44 |
} |
| 45 |
|
| 46 |
$result = Moderation::add_user_block( $user_id, Moderation::TYPE_ACTOR, $actor_uri ); |
| 47 |
|
| 48 |
if ( ! $result ) { |
| 49 |
return new \WP_Error( |
| 50 |
'activitypub_block_failed', |
| 51 |
\__( 'Failed to block the actor.', 'activitypub' ), |
| 52 |
array( 'status' => 500 ) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
// Block activities should only be sent to the blocked actor. |
| 57 |
$data['to'] = array( $actor_uri ); |
| 58 |
unset( $data['cc'] ); |
| 59 |
|
| 60 |
return add_to_outbox( $data, 'Block', $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE ); |
| 61 |
} |
| 62 |
} |
| 63 |
|