2024-11-18 16:07:09 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2024-12-16 17:36:23 +01:00
|
|
|
* ActivityPub Event Sources (=Followed Actors) Collection.
|
|
|
|
*
|
|
|
|
* The Event Sources are nothing else than follows in the ActivityPub world.
|
|
|
|
* However, this plugins currently only listens to Event object being created,
|
|
|
|
* updated or deleted by a follow.
|
|
|
|
*
|
|
|
|
* For the ActivityPub `Follow` the Blog-Actor from the ActivityPub plugin is used.
|
|
|
|
*
|
|
|
|
* This class is responsible for defining a custom post type in WordPress along
|
|
|
|
* with post-meta fields and methods to easily manage event sources. This includes
|
|
|
|
* handling side effects, like when an event source is added a follow request is sent
|
|
|
|
* or adding them to the `follow` collection o the blog-actor profile.
|
2024-11-18 16:07:09 +01:00
|
|
|
*
|
2024-12-05 17:50:17 +01:00
|
|
|
* @package Event_Bridge_For_ActivityPub
|
2024-12-08 21:57:53 +01:00
|
|
|
* @since 1.0.0
|
2024-11-18 16:07:09 +01:00
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*/
|
|
|
|
|
2024-12-05 17:50:17 +01:00
|
|
|
namespace Event_Bridge_For_ActivityPub\ActivityPub\Collection;
|
2024-11-18 16:07:09 +01:00
|
|
|
|
2024-12-15 16:12:24 +01:00
|
|
|
use Activitypub\Model\Blog;
|
|
|
|
use Event_Bridge_For_ActivityPub\ActivityPub\Model\Event_Source;
|
2024-11-18 16:07:09 +01:00
|
|
|
use WP_Error;
|
|
|
|
use WP_Query;
|
|
|
|
|
|
|
|
use function Activitypub\is_tombstone;
|
|
|
|
use function Activitypub\get_remote_metadata_by_actor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub Event Sources (=Followed Actors) Collection.
|
2024-12-16 17:36:23 +01:00
|
|
|
*
|
|
|
|
* The Event Sources are nothing else than follows in the ActivityPub world.
|
|
|
|
* However, this plugins currently only listens to Event object being created,
|
|
|
|
* updated or deleted by a follow.
|
|
|
|
*
|
|
|
|
* For the ActivityPub `Follow` the Blog-Actor from the ActivityPub plugin is used.
|
|
|
|
*
|
|
|
|
* This class is responsible for defining a custom post type in WordPress along
|
|
|
|
* with post-meta fields and methods to easily manage event sources. This includes
|
|
|
|
* handling side effects, like when an event source is added a follow request is sent
|
|
|
|
* or adding them to the `follow` collection o the blog-actor profile.
|
2024-11-18 16:07:09 +01:00
|
|
|
*/
|
|
|
|
class Event_Sources {
|
|
|
|
/**
|
|
|
|
* The custom post type.
|
|
|
|
*/
|
2024-12-08 17:38:05 +01:00
|
|
|
const POST_TYPE = 'ebap_event_source';
|
2024-11-18 16:07:09 +01:00
|
|
|
|
2024-12-10 19:34:15 +01:00
|
|
|
/**
|
|
|
|
* Init.
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
self::register_post_type();
|
2024-12-15 22:25:28 +01:00
|
|
|
\add_filter( 'allowed_redirect_hosts', array( self::class, 'add_event_sources_hosts_to_allowed_redirect_hosts' ) );
|
2024-12-15 16:12:24 +01:00
|
|
|
\add_action( 'event_bridge_for_activitypub_follow', array( self::class, 'activitypub_follow_actor' ), 10, 1 );
|
|
|
|
\add_action( 'event_bridge_for_activitypub_unfollow', array( self::class, 'activitypub_unfollow_actor' ), 10, 1 );
|
2024-12-10 19:34:15 +01:00
|
|
|
}
|
|
|
|
|
2024-11-18 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Register the post type used to store the external event sources (i.e., followed ActivityPub actors).
|
|
|
|
*/
|
|
|
|
public static function register_post_type() {
|
|
|
|
register_post_type(
|
|
|
|
self::POST_TYPE,
|
|
|
|
array(
|
|
|
|
'labels' => array(
|
2024-12-07 20:20:17 +01:00
|
|
|
'name' => _x( 'Event Sources', 'post_type plural name', 'event-bridge-for-activitypub' ),
|
|
|
|
'singular_name' => _x( 'Event Source', 'post_type single name', 'event-bridge-for-activitypub' ),
|
2024-11-18 16:07:09 +01:00
|
|
|
),
|
|
|
|
'public' => false,
|
|
|
|
'hierarchical' => false,
|
|
|
|
'rewrite' => false,
|
|
|
|
'query_var' => false,
|
|
|
|
'delete_with_user' => false,
|
|
|
|
'can_export' => true,
|
|
|
|
'supports' => array(),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\register_post_meta(
|
|
|
|
self::POST_TYPE,
|
2024-12-08 17:38:05 +01:00
|
|
|
'activitypub_actor_id',
|
2024-11-18 16:07:09 +01:00
|
|
|
array(
|
|
|
|
'type' => 'string',
|
|
|
|
'single' => true,
|
|
|
|
'sanitize_callback' => 'sanitize_url',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\register_post_meta(
|
|
|
|
self::POST_TYPE,
|
|
|
|
'activitypub_errors',
|
|
|
|
array(
|
|
|
|
'type' => 'string',
|
|
|
|
'single' => false,
|
|
|
|
'sanitize_callback' => function ( $value ) {
|
|
|
|
if ( ! is_string( $value ) ) {
|
2024-12-07 20:20:17 +01:00
|
|
|
throw new \Exception( 'Error message is no valid string' );
|
2024-11-18 16:07:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return esc_sql( $value );
|
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\register_post_meta(
|
|
|
|
self::POST_TYPE,
|
2024-12-08 17:38:05 +01:00
|
|
|
'activitypub_actor_json',
|
2024-11-18 16:07:09 +01:00
|
|
|
array(
|
|
|
|
'type' => 'string',
|
2024-12-08 17:38:05 +01:00
|
|
|
'single' => true,
|
2024-11-18 16:07:09 +01:00
|
|
|
'sanitize_callback' => function ( $value ) {
|
2024-12-08 17:38:05 +01:00
|
|
|
return sanitize_text_field( $value );
|
2024-11-18 16:07:09 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\register_post_meta(
|
|
|
|
self::POST_TYPE,
|
2024-12-08 17:38:05 +01:00
|
|
|
'event_source_active',
|
|
|
|
array(
|
2024-12-08 21:57:53 +01:00
|
|
|
'type' => 'bool',
|
|
|
|
'single' => true,
|
2024-12-08 17:38:05 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2024-12-08 18:38:47 +01:00
|
|
|
\register_post_meta(
|
|
|
|
self::POST_TYPE,
|
|
|
|
'activitypub_inbox',
|
|
|
|
array(
|
|
|
|
'type' => 'string',
|
|
|
|
'single' => true,
|
|
|
|
'sanitize_callback' => 'sanitize_url',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2024-12-08 17:38:05 +01:00
|
|
|
\register_post_meta(
|
|
|
|
self::POST_TYPE,
|
|
|
|
'event_source_utilize_announces',
|
2024-11-18 16:07:09 +01:00
|
|
|
array(
|
|
|
|
'type' => 'string',
|
|
|
|
'single' => true,
|
|
|
|
'sanitize_callback' => function ( $value ) {
|
2024-12-08 17:38:05 +01:00
|
|
|
if ( 'same_origin' === $value ) {
|
|
|
|
return 'same_origin';
|
|
|
|
}
|
|
|
|
return '';
|
2024-11-18 16:07:09 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add new Event Source.
|
|
|
|
*
|
2024-12-08 17:38:05 +01:00
|
|
|
* @param string $actor The Actor URL/ID.
|
2024-11-18 16:07:09 +01:00
|
|
|
*
|
|
|
|
* @return Event_Source|WP_Error The Followed (WP_Post array) or an WP_Error.
|
|
|
|
*/
|
|
|
|
public static function add_event_source( $actor ) {
|
|
|
|
$meta = get_remote_metadata_by_actor( $actor );
|
|
|
|
|
|
|
|
if ( is_tombstone( $meta ) ) {
|
|
|
|
return $meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) {
|
2024-12-07 20:20:17 +01:00
|
|
|
return new WP_Error( 'activitypub_invalid_actor', __( 'Invalid ActivityPub Actor', 'event-bridge-for-activitypub' ), array( 'status' => 400 ) );
|
2024-11-18 16:07:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$event_source = new Event_Source();
|
|
|
|
$event_source->from_array( $meta );
|
|
|
|
|
|
|
|
$post_id = $event_source->save();
|
|
|
|
|
|
|
|
if ( is_wp_error( $post_id ) ) {
|
|
|
|
return $post_id;
|
|
|
|
}
|
|
|
|
|
2024-12-15 16:12:24 +01:00
|
|
|
$success = self::queue_follow_actor( $actor );
|
2024-12-10 19:34:15 +01:00
|
|
|
|
2024-12-14 14:46:00 +01:00
|
|
|
self::delete_event_source_transients();
|
|
|
|
|
2024-11-18 16:07:09 +01:00
|
|
|
return $event_source;
|
|
|
|
}
|
|
|
|
|
2024-12-14 14:46:00 +01:00
|
|
|
/**
|
|
|
|
* Delete all transients related to the event sources.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function delete_event_source_transients(): void {
|
|
|
|
delete_transient( 'event_bridge_for_activitypub_event_sources' );
|
|
|
|
delete_transient( 'event_bridge_for_activitypub_event_sources_hosts' );
|
|
|
|
delete_transient( 'event_bridge_for_activitypub_event_sources_ids' );
|
|
|
|
}
|
|
|
|
|
2024-11-18 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Remove an Event Source (=Followed ActivityPub actor).
|
|
|
|
*
|
|
|
|
* @param string $actor The Actor URL.
|
|
|
|
*
|
2024-12-15 13:44:57 +01:00
|
|
|
* @return WP_Post|false|null Post data on success, false or null on failure.
|
2024-11-18 16:07:09 +01:00
|
|
|
*/
|
|
|
|
public static function remove_event_source( $actor ) {
|
2024-12-15 16:12:24 +01:00
|
|
|
$actor = Event_Source::get_by_id( $actor );
|
|
|
|
|
|
|
|
if ( ! $actor ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$post_id = $actor->get__id();
|
2024-12-15 13:08:26 +01:00
|
|
|
|
|
|
|
if ( ! $post_id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-15 16:12:24 +01:00
|
|
|
$thumbnail_id = get_post_thumbnail_id( $post_id );
|
|
|
|
|
|
|
|
if ( $thumbnail_id ) {
|
|
|
|
wp_delete_attachment( $thumbnail_id, true );
|
|
|
|
}
|
|
|
|
|
2024-12-15 13:08:26 +01:00
|
|
|
$result = wp_delete_post( $post_id, true );
|
|
|
|
|
|
|
|
// If the deletion was successful delete all transients regarding event sources.
|
|
|
|
if ( $result ) {
|
|
|
|
self::delete_event_source_transients();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2024-11-18 16:07:09 +01:00
|
|
|
}
|
|
|
|
|
2024-12-14 14:46:00 +01:00
|
|
|
/**
|
|
|
|
* Get all Event-Sources.
|
|
|
|
*
|
|
|
|
* @return array The Term list of Event Sources.
|
|
|
|
*/
|
|
|
|
public static function get_event_sources() {
|
|
|
|
return self::get_event_sources_with_count()['actors'];
|
2024-12-08 17:38:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Event Sources along with a total count for pagination purposes.
|
|
|
|
*
|
|
|
|
* @param int $number Maximum number of results to return.
|
|
|
|
* @param int $page Page number.
|
|
|
|
* @param array $args The WP_Query arguments.
|
|
|
|
*
|
|
|
|
* @return array {
|
|
|
|
* Data about the followers.
|
|
|
|
*
|
|
|
|
* @type array $followers List of `Follower` objects.
|
|
|
|
* @type int $total Total number of followers.
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
public static function get_event_sources_with_count( $number = -1, $page = null, $args = array() ) {
|
2024-12-14 14:46:00 +01:00
|
|
|
$event_sources = get_transient( 'event_bridge_for_activitypub_event_sources' );
|
|
|
|
|
|
|
|
if ( $event_sources ) {
|
|
|
|
return $event_sources;
|
|
|
|
}
|
|
|
|
|
2024-12-08 17:38:05 +01:00
|
|
|
$defaults = array(
|
|
|
|
'post_type' => self::POST_TYPE,
|
|
|
|
'posts_per_page' => $number,
|
|
|
|
'paged' => $page,
|
|
|
|
'orderby' => 'ID',
|
|
|
|
'order' => 'DESC',
|
|
|
|
);
|
|
|
|
|
|
|
|
$args = wp_parse_args( $args, $defaults );
|
|
|
|
$query = new WP_Query( $args );
|
|
|
|
$total = $query->found_posts;
|
|
|
|
$actors = array_map(
|
|
|
|
function ( $post ) {
|
|
|
|
return Event_Source::init_from_cpt( $post );
|
|
|
|
},
|
|
|
|
$query->get_posts()
|
|
|
|
);
|
|
|
|
|
2024-12-14 14:46:00 +01:00
|
|
|
$event_sources = compact( 'actors', 'total' );
|
|
|
|
|
|
|
|
set_transient( 'event_bridge_for_activitypub_event_sources', $event_sources );
|
|
|
|
|
|
|
|
return $event_sources;
|
2024-12-08 17:38:05 +01:00
|
|
|
}
|
|
|
|
|
2024-12-08 18:38:47 +01:00
|
|
|
/**
|
|
|
|
* Queue a hook to run async.
|
|
|
|
*
|
|
|
|
* @param string $hook The hook name.
|
|
|
|
* @param array $args The arguments to pass to the hook.
|
|
|
|
* @param string $unqueue_hook Optional a hook to unschedule before queuing.
|
|
|
|
* @return void|bool Whether the hook was queued.
|
|
|
|
*/
|
|
|
|
public static function queue( $hook, $args, $unqueue_hook = null ) {
|
|
|
|
if ( $unqueue_hook ) {
|
|
|
|
$hook_timestamp = wp_next_scheduled( $unqueue_hook, $args );
|
|
|
|
if ( $hook_timestamp ) {
|
|
|
|
wp_unschedule_event( $hook_timestamp, $unqueue_hook, $args );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( wp_next_scheduled( $hook, $args ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return \wp_schedule_single_event( \time(), $hook, $args );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare to follow an ActivityPub actor via a scheduled event.
|
|
|
|
*
|
|
|
|
* @param string $actor The ActivityPub actor.
|
|
|
|
*
|
|
|
|
* @return bool|WP_Error Whether the event was queued.
|
|
|
|
*/
|
|
|
|
public static function queue_follow_actor( $actor ) {
|
|
|
|
$queued = self::queue(
|
|
|
|
'event_bridge_for_activitypub_follow',
|
2024-12-15 16:12:24 +01:00
|
|
|
array( $actor ),
|
2024-12-08 18:38:47 +01:00
|
|
|
'event_bridge_for_activitypub_unfollow'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $queued;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-15 16:12:24 +01:00
|
|
|
* Follow an ActivityPub actor via the Blog user.
|
2024-12-08 18:38:47 +01:00
|
|
|
*
|
|
|
|
* @param string $actor_id The ID/URL of the Actor.
|
|
|
|
*/
|
|
|
|
public static function activitypub_follow_actor( $actor_id ) {
|
2024-12-15 16:12:24 +01:00
|
|
|
$actor = Event_Source::get_by_id( $actor_id );
|
2024-12-08 18:38:47 +01:00
|
|
|
|
2024-12-15 16:12:24 +01:00
|
|
|
if ( ! $actor ) {
|
2024-12-08 18:38:47 +01:00
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
$inbox = $actor->get_shared_inbox();
|
|
|
|
$to = $actor->get_id();
|
|
|
|
|
2024-12-15 16:12:24 +01:00
|
|
|
$application = new Blog();
|
2024-12-08 18:38:47 +01:00
|
|
|
|
|
|
|
$activity = new \Activitypub\Activity\Activity();
|
|
|
|
$activity->set_type( 'Follow' );
|
|
|
|
$activity->set_to( null );
|
|
|
|
$activity->set_cc( null );
|
|
|
|
$activity->set_actor( $application->get_id() );
|
|
|
|
$activity->set_object( $to );
|
2024-12-08 21:57:53 +01:00
|
|
|
$activity->set_id( $application->get_id() . '#follow-' . \preg_replace( '~^https?://~', '', $to ) );
|
2024-12-08 18:38:47 +01:00
|
|
|
$activity = $activity->to_json();
|
2024-12-15 13:42:57 +01:00
|
|
|
\Activitypub\safe_remote_post( $inbox, $activity, \Activitypub\Collection\Actors::BLOG_USER_ID );
|
2024-12-08 18:38:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare to unfollow an actor via a scheduled event.
|
|
|
|
*
|
|
|
|
* @param string $actor The ActivityPub actor ID.
|
|
|
|
*
|
|
|
|
* @return bool|WP_Error Whether the event was queued.
|
|
|
|
*/
|
|
|
|
public static function queue_unfollow_actor( $actor ) {
|
|
|
|
$queued = self::queue(
|
|
|
|
'event_bridge_for_activitypub_unfollow',
|
2024-12-15 16:12:24 +01:00
|
|
|
array( $actor ),
|
2024-12-08 18:38:47 +01:00
|
|
|
'event_bridge_for_activitypub_follow'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $queued;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unfollow an ActivityPub actor.
|
|
|
|
*
|
|
|
|
* @param string $actor_id The ActivityPub actor ID.
|
|
|
|
*/
|
|
|
|
public static function activitypub_unfollow_actor( $actor_id ) {
|
2024-12-15 16:12:24 +01:00
|
|
|
$actor = Event_Source::get_by_id( $actor_id );
|
2024-12-08 18:38:47 +01:00
|
|
|
|
2024-12-15 16:12:24 +01:00
|
|
|
if ( ! $actor ) {
|
2024-12-08 18:38:47 +01:00
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
$inbox = $actor->get_shared_inbox();
|
|
|
|
$to = $actor->get_id();
|
|
|
|
|
2024-12-15 16:12:24 +01:00
|
|
|
$application = new Blog();
|
2024-12-08 18:38:47 +01:00
|
|
|
|
|
|
|
if ( is_wp_error( $inbox ) ) {
|
|
|
|
return $inbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
$activity = new \Activitypub\Activity\Activity();
|
|
|
|
$activity->set_type( 'Undo' );
|
|
|
|
$activity->set_to( null );
|
|
|
|
$activity->set_cc( null );
|
|
|
|
$activity->set_actor( $application->get_id() );
|
|
|
|
$activity->set_object(
|
|
|
|
array(
|
|
|
|
'type' => 'Follow',
|
|
|
|
'actor' => $actor,
|
|
|
|
'object' => $to,
|
|
|
|
'id' => $to,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$activity->set_id( $actor . '#unfollow-' . \preg_replace( '~^https?://~', '', $to ) );
|
|
|
|
$activity = $activity->to_json();
|
2024-12-15 13:42:57 +01:00
|
|
|
\Activitypub\safe_remote_post( $inbox, $activity, \Activitypub\Collection\Actors::BLOG_USER_ID );
|
2024-12-08 18:38:47 +01:00
|
|
|
}
|
2024-11-18 16:07:09 +01:00
|
|
|
}
|