| 1 |
<?php |
| 2 |
/** |
| 3 |
* ActivityPub Relay Class |
| 4 |
* |
| 5 |
* Handles forwarding of activities when relay mode is enabled. |
| 6 |
* |
| 7 |
* @package Activitypub |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Activitypub; |
| 11 |
|
| 12 |
use Activitypub\Activity\Activity; |
| 13 |
use Activitypub\Collection\Actors; |
| 14 |
use Activitypub\Collection\Outbox; |
| 15 |
|
| 16 |
/** |
| 17 |
* ActivityPub Relay Class |
| 18 |
* |
| 19 |
* Provides relay functionality to forward public activities to all followers. |
| 20 |
*/ |
| 21 |
class Relay { |
| 22 |
/** |
| 23 |
* Initialize the class, registering WordPress hooks. |
| 24 |
*/ |
| 25 |
public static function init() { |
| 26 |
\add_action( 'activitypub_handled_create', array( self::class, 'handle_activity' ), 10, 3 ); |
| 27 |
\add_action( 'activitypub_handled_update', array( self::class, 'handle_activity' ), 10, 3 ); |
| 28 |
\add_action( 'activitypub_handled_delete', array( self::class, 'handle_activity' ), 10, 3 ); |
| 29 |
\add_action( 'activitypub_handled_announce', array( self::class, 'handle_activity' ), 10, 3 ); |
| 30 |
\add_action( 'load-settings_page_activitypub', array( self::class, 'unhook_settings_fields' ), 11 ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Handle incoming activity and relay if needed. |
| 35 |
* |
| 36 |
* @param array $activity The activity data. |
| 37 |
* @param array $user_ids The user IDs that are recipients. |
| 38 |
* @param bool $success Whether the activity was handled successfully. |
| 39 |
*/ |
| 40 |
public static function handle_activity( $activity, $user_ids, $success ) { |
| 41 |
// Only relay if: successfully handled, Blog actor is recipient, activity is public, and in single-user mode. |
| 42 |
if ( |
| 43 |
! $success || |
| 44 |
! \in_array( Actors::BLOG_USER_ID, (array) $user_ids, true ) || |
| 45 |
! is_activity_public( $activity ) || |
| 46 |
! is_single_user() |
| 47 |
) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// Create Announce wrapper. |
| 52 |
$announce = new Activity(); |
| 53 |
$announce->set_type( 'Announce' ); |
| 54 |
$announce->set_actor( Actors::BLOG_USER_ID ); |
| 55 |
$announce->set_object( $activity ); |
| 56 |
$announce->set_published( \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339 ) ); |
| 57 |
|
| 58 |
// Add to outbox for distribution. The outbox will generate the ID. |
| 59 |
Outbox::add( $announce, Actors::BLOG_USER_ID ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Unhook settings fields when relay mode is enabled. |
| 64 |
* |
| 65 |
* Removes all settings sections except moderation when relay mode is active. |
| 66 |
*/ |
| 67 |
public static function unhook_settings_fields() { |
| 68 |
global $wp_settings_sections; |
| 69 |
|
| 70 |
if ( ! isset( $wp_settings_sections['activitypub_settings'] ) ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Keep only the moderation section. |
| 75 |
foreach ( $wp_settings_sections['activitypub_settings'] as $section_id => $section ) { |
| 76 |
if ( 'activitypub_moderation' !== $section_id ) { |
| 77 |
unset( $wp_settings_sections['activitypub_settings'][ $section_id ] ); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|