2024-11-18 16:07:09 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Event sources collection file.
|
|
|
|
*
|
2024-12-05 17:50:17 +01:00
|
|
|
* @package Event_Bridge_For_ActivityPub
|
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
|
|
|
|
|
|
|
use WP_Error;
|
|
|
|
use WP_Query;
|
2024-12-07 20:20:17 +01:00
|
|
|
use Event_Bridge_For_ActivityPub\ActivityPub\Model\Event_Source;
|
2024-11-18 16:07:09 +01:00
|
|
|
|
|
|
|
use function Activitypub\is_tombstone;
|
|
|
|
use function Activitypub\get_remote_metadata_by_actor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub Event Sources (=Followed Actors) Collection.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
|
|
|
'type' => 'bool',
|
|
|
|
'single' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $event_source;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an Event Source (=Followed ActivityPub actor).
|
|
|
|
*
|
|
|
|
* @param string $actor The Actor URL.
|
|
|
|
*
|
|
|
|
* @return bool True on success, false on failure.
|
|
|
|
*/
|
|
|
|
public static function remove_event_source( $actor ) {
|
|
|
|
$actor = true;
|
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-07 20:20:17 +01:00
|
|
|
* Get all Event-Sources.
|
2024-11-18 16:07:09 +01:00
|
|
|
*
|
2024-12-07 20:20:17 +01:00
|
|
|
* @return array The Term list of Event Sources.
|
2024-11-18 16:07:09 +01:00
|
|
|
*/
|
2024-12-08 17:38:05 +01:00
|
|
|
public static function get_event_sources() {
|
2024-11-18 16:07:09 +01:00
|
|
|
$args = array(
|
2024-12-08 17:38:05 +01:00
|
|
|
'post_type' => self::POST_TYPE,
|
|
|
|
'posts_per_page' => -1,
|
|
|
|
'orderby' => 'ID',
|
|
|
|
'order' => 'DESC',
|
2024-11-18 16:07:09 +01:00
|
|
|
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
|
2024-12-08 17:38:05 +01:00
|
|
|
'meta_query' => array(
|
2024-11-18 16:07:09 +01:00
|
|
|
array(
|
2024-12-08 17:38:05 +01:00
|
|
|
'key' => 'activitypub_actor_id',
|
2024-11-18 16:07:09 +01:00
|
|
|
'compare' => 'EXISTS',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2024-12-08 17:38:05 +01:00
|
|
|
|
|
|
|
$query = new WP_Query( $args );
|
|
|
|
|
|
|
|
$event_sources = array_map(
|
|
|
|
function ( $post ) {
|
|
|
|
return Event_Source::init_from_cpt( $post );
|
|
|
|
},
|
|
|
|
$query->get_posts()
|
|
|
|
);
|
|
|
|
|
|
|
|
return $event_sources;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() ) {
|
|
|
|
$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()
|
|
|
|
);
|
|
|
|
|
|
|
|
return compact( 'actors', 'total' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a Follower.
|
|
|
|
*
|
|
|
|
* @param string $event_source The Actor URL.
|
|
|
|
*
|
|
|
|
* @return mixed True on success, false on failure.
|
|
|
|
*/
|
|
|
|
public static function remove( $event_source ) {
|
|
|
|
$post_id = Event_Source::get_wp_post_from_activitypub_actor_id( $event_source );
|
|
|
|
return wp_delete_post( $post_id, true );
|
2024-11-18 16:07:09 +01:00
|
|
|
}
|
|
|
|
}
|