updated plugin Event Bridge for ActivityPub version 1.3.0
This commit is contained in:
@ -21,14 +21,15 @@
|
||||
namespace Event_Bridge_For_ActivityPub\ActivityPub\Collection;
|
||||
|
||||
// Exit if accessed directly.
|
||||
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
|
||||
\defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
|
||||
|
||||
use Activitypub\Model\Blog;
|
||||
use Activitypub\Tombstone;
|
||||
use Event_Bridge_For_ActivityPub\ActivityPub\Model\Event_Source;
|
||||
use WP_Error;
|
||||
use WP_Post;
|
||||
use WP_Query;
|
||||
|
||||
use function Activitypub\is_tombstone;
|
||||
use function Activitypub\get_remote_metadata_by_actor;
|
||||
|
||||
/**
|
||||
@ -49,7 +50,7 @@ class Event_Sources {
|
||||
/**
|
||||
* The custom post type.
|
||||
*/
|
||||
const POST_TYPE = 'ap_event_source';
|
||||
public const POST_TYPE = 'ap_event_source';
|
||||
|
||||
/**
|
||||
* Init.
|
||||
@ -173,10 +174,10 @@ class Event_Sources {
|
||||
*
|
||||
* @return Event_Source|WP_Error|null The Followed (WP_Post array) or an WP_Error.
|
||||
*/
|
||||
public static function add_event_source( $actor ) {
|
||||
public static function add_event_source( string $actor ) {
|
||||
$meta = get_remote_metadata_by_actor( $actor );
|
||||
|
||||
if ( is_tombstone( $meta ) ) {
|
||||
if ( Tombstone::exists_in_error( $meta ) ) {
|
||||
return $meta;
|
||||
}
|
||||
|
||||
@ -211,7 +212,7 @@ class Event_Sources {
|
||||
* @param string $followed_id The ActivityPub ID of the followed actor.
|
||||
* @return string The `Follow` ID.
|
||||
*/
|
||||
public static function compose_follow_id( $follower_id, $followed_id ) {
|
||||
public static function compose_follow_id( string $follower_id, string $followed_id ) {
|
||||
return $follower_id . '#follow-' . \preg_replace( '~^https?://~', '', $followed_id );
|
||||
}
|
||||
|
||||
@ -231,14 +232,14 @@ class Event_Sources {
|
||||
* @param string|int $attachment_id The numeric post ID of the attachment.
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_attachment_featured_image( $attachment_id ): bool {
|
||||
public static function is_attachment_featured_image( string|int $attachment_id ): bool {
|
||||
if ( ! is_numeric( $attachment_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Query posts with the given attachment ID as their featured image.
|
||||
$args = array(
|
||||
'post_type' => 'any',
|
||||
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => '_thumbnail_id',
|
||||
@ -261,8 +262,10 @@ class Event_Sources {
|
||||
* @param int $event_source_post_id The WordPress Post ID of the event source.
|
||||
* @return void
|
||||
*/
|
||||
public static function delete_events_by_event_source( $event_source_post_id ): void {
|
||||
public static function delete_events_by_event_source( int $event_source_post_id ): void {
|
||||
global $wpdb;
|
||||
|
||||
// phpcs:disable WordPress.DB.DirectDatabaseQuery
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s",
|
||||
@ -305,7 +308,7 @@ class Event_Sources {
|
||||
*
|
||||
* @return void Post data on success, false or null on failure.
|
||||
*/
|
||||
public static function remove_event_source( $event_source_post_id ): void {
|
||||
public static function remove_event_source( int|string $event_source_post_id ): void {
|
||||
$event_source = Event_Source::get_by_id( $event_source_post_id );
|
||||
|
||||
if ( ! $event_source ) {
|
||||
@ -320,7 +323,7 @@ class Event_Sources {
|
||||
$post = \get_post( $event_source->get__id() );
|
||||
$post->post_status = 'draft';
|
||||
|
||||
if ( $post instanceof \WP_Post ) {
|
||||
if ( $post instanceof WP_Post ) {
|
||||
$post = \get_object_vars( $post );
|
||||
$post = \wp_slash( $post );
|
||||
$post = \wp_update_post( $post );
|
||||
@ -358,9 +361,9 @@ class 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.
|
||||
* @param int $number Maximum number of results to return.
|
||||
* @param int|null $page Page number.
|
||||
* @param array $args The WP_Query arguments.
|
||||
*
|
||||
* @return array {
|
||||
* Data about the event sources.
|
||||
@ -369,7 +372,7 @@ class Event_Sources {
|
||||
* @type int $total Total number of followers.
|
||||
* }
|
||||
*/
|
||||
public static function get_event_sources_with_count( $number = -1, $page = null, $args = array() ): array {
|
||||
public static function get_event_sources_with_count( int $number = -1, int|null $page = null, array $args = array() ): array {
|
||||
$defaults = array(
|
||||
'post_type' => self::POST_TYPE,
|
||||
'posts_per_page' => $number,
|
||||
@ -396,12 +399,12 @@ class Event_Sources {
|
||||
/**
|
||||
* 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|WP_Error Whether the hook was queued.
|
||||
* @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 bool|WP_Error Whether the hook was queued.
|
||||
*/
|
||||
public static function queue( $hook, $args, $unqueue_hook = null ) {
|
||||
public static function queue( string $hook, array $args, $unqueue_hook = null ): bool|WP_Error {
|
||||
if ( $unqueue_hook ) {
|
||||
$hook_timestamp = \wp_next_scheduled( $unqueue_hook, $args );
|
||||
if ( $hook_timestamp ) {
|
||||
@ -410,7 +413,7 @@ class Event_Sources {
|
||||
}
|
||||
|
||||
if ( \wp_next_scheduled( $hook, $args ) ) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
return \wp_schedule_single_event( \time(), $hook, $args );
|
||||
@ -419,11 +422,11 @@ class Event_Sources {
|
||||
/**
|
||||
* Prepare to follow an ActivityPub actor via a scheduled event.
|
||||
*
|
||||
* @param string $actor The ActivityPub actor.
|
||||
* @param string $actor The ActivityPub actor.
|
||||
*
|
||||
* @return bool Whether the event was queued.
|
||||
* @return bool Whether the event was queued.
|
||||
*/
|
||||
public static function queue_follow_actor( $actor ): bool {
|
||||
public static function queue_follow_actor( string $actor ): bool {
|
||||
$queued = self::queue(
|
||||
'event_bridge_for_activitypub_follow',
|
||||
array( $actor ),
|
||||
@ -435,7 +438,7 @@ class Event_Sources {
|
||||
}
|
||||
|
||||
// Following this actor has already been queued.
|
||||
if ( null === $queued ) {
|
||||
if ( false === $queued ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -447,11 +450,11 @@ class Event_Sources {
|
||||
*
|
||||
* @param string $actor_id The ID/URL of the Actor.
|
||||
*/
|
||||
public static function activitypub_follow_actor( $actor_id ) {
|
||||
public static function activitypub_follow_actor( string $actor_id ): void {
|
||||
$actor = Event_Source::get_by_id( $actor_id );
|
||||
|
||||
if ( ! $actor ) {
|
||||
return $actor;
|
||||
return;
|
||||
}
|
||||
|
||||
$inbox = $actor->get_shared_inbox();
|
||||
@ -461,8 +464,7 @@ class Event_Sources {
|
||||
|
||||
$activity = new \Activitypub\Activity\Activity();
|
||||
$activity->set_type( 'Follow' );
|
||||
$activity->set_to( null );
|
||||
$activity->set_cc( null );
|
||||
$activity->set_to( array( $to ) );
|
||||
$activity->set_actor( $from_actor->get_id() );
|
||||
$activity->set_object( $to );
|
||||
$activity->set_id( self::compose_follow_id( $from_actor->get_id(), $to ) );
|
||||
@ -475,9 +477,9 @@ class Event_Sources {
|
||||
*
|
||||
* @param string $actor The ActivityPub actor ID.
|
||||
*
|
||||
* @return bool|void|WP_Error Whether the event was queued.
|
||||
* @return bool|WP_Error Whether the event was queued.
|
||||
*/
|
||||
public static function queue_unfollow_actor( $actor ) {
|
||||
public static function queue_unfollow_actor( string $actor ): bool|WP_Error {
|
||||
$queued = self::queue(
|
||||
'event_bridge_for_activitypub_unfollow',
|
||||
array( $actor ),
|
||||
@ -489,7 +491,7 @@ class Event_Sources {
|
||||
}
|
||||
|
||||
// Following this actor has already been queued.
|
||||
if ( null === $queued ) {
|
||||
if ( false === $queued ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -502,7 +504,7 @@ class Event_Sources {
|
||||
* @param string $actor The ActivityPub ID of the actor to unfollow.
|
||||
* @return void
|
||||
*/
|
||||
public static function activitypub_unfollow_actor( $actor ): void {
|
||||
public static function activitypub_unfollow_actor( string $actor ): void {
|
||||
$actor = Event_Source::get_by_id( $actor );
|
||||
|
||||
if ( ! $actor ) {
|
||||
@ -520,8 +522,7 @@ class Event_Sources {
|
||||
|
||||
$activity = new \Activitypub\Activity\Activity();
|
||||
$activity->set_type( 'Undo' );
|
||||
$activity->set_to( null );
|
||||
$activity->set_cc( null );
|
||||
$activity->set_to( array( $to ) );
|
||||
$activity->set_actor( $from_actor->get_id() );
|
||||
$activity->set_object(
|
||||
array(
|
||||
|
||||
Reference in New Issue
Block a user