PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / class-relay.php

class-relay.php in ActivityPub trunk, at includes/class-relay.php

82 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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