updated plugin ActivityPub version 8.3.0

This commit is contained in:
2026-06-03 21:28:46 +00:00
committed by Gitium
parent a4b78ec277
commit 6fe182458a
340 changed files with 43232 additions and 7568 deletions

View File

@ -19,62 +19,93 @@ use Activitypub\Collection\Outbox;
* @see https://www.w3.org/TR/activitypub/
*/
class Dispatcher {
/**
* Batch size.
*
* @deprecated 7.6.0 Use {@see Dispatcher::get_batch_size()}.
*
* @var int
*/
public static $batch_size = ACTIVITYPUB_OUTBOX_PROCESSING_BATCH_SIZE;
/**
* Callback for the async batch processing.
*
* @var array
*/
public static $callback = array( self::class, 'send_to_followers' );
/**
* Error codes that qualify for a retry.
*
* @see https://github.com/tfredrich/RestApiTutorial.com/blob/fd08b0f67f07450521d143b123cd6e1846cb2e3b/content/advanced/responses/retries.md
* @var int[]
*/
public static $retry_error_codes = array( 408, 429, 500, 502, 503, 504 );
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action( 'activitypub_process_outbox', array( self::class, 'process_outbox' ) );
\add_action( 'post_activitypub_add_to_outbox', array( self::class, 'fire_outbox_handlers' ), 5, 2 );
\add_action( 'post_activitypub_add_to_outbox', array( self::class, 'send_immediate_accept' ), 10, 2 );
// Default filters to add Inboxes to sent to.
\add_filter( 'activitypub_additional_inboxes', array( self::class, 'add_inboxes_by_mentioned_actors' ), 10, 3 );
\add_filter( 'activitypub_additional_inboxes', array( self::class, 'add_inboxes_of_replied_urls' ), 10, 3 );
\add_filter( 'activitypub_additional_inboxes', array( self::class, 'add_inboxes_of_relays' ), 10, 3 );
// Fallback for `activitypub_send_to_inboxes` filter.
\add_filter(
'activitypub_additional_inboxes',
function ( $inboxes, $actor_id, $activity ) {
/**
* Filters the list of interactees inboxes to send the Activity to.
*
* @param array $inboxes The list of inboxes to send to.
* @param int $actor_id The actor ID.
* @param Activity $activity The ActivityPub Activity.
*
* @deprecated 5.2.0 Use `activitypub_additional_inboxes` instead.
* @deprecated 5.4.0 Use `activitypub_additional_inboxes` instead.
*/
$inboxes = \apply_filters_deprecated( 'activitypub_send_to_inboxes', array( $inboxes, $actor_id, $activity ), '5.2.0', 'activitypub_additional_inboxes' );
$inboxes = \apply_filters_deprecated( 'activitypub_interactees_inboxes', array( $inboxes, $actor_id, $activity ), '5.4.0', 'activitypub_additional_inboxes' );
Scheduler::register_async_batch_callback( 'activitypub_send_activity', array( self::class, 'send_to_followers' ) );
Scheduler::register_async_batch_callback( 'activitypub_retry_activity', array( self::class, 'retry_send_to_followers' ) );
}
return $inboxes;
},
10,
3
);
/**
* Get the batch size for processing outbox items.
*
* @return int The batch size.
*/
public static function get_batch_size() {
/**
* Filters the batch size for processing outbox items.
*
* @param int $batch_size The batch size. Default ACTIVITYPUB_OUTBOX_PROCESSING_BATCH_SIZE.
*/
return apply_filters( 'activitypub_dispatcher_batch_size', ACTIVITYPUB_OUTBOX_PROCESSING_BATCH_SIZE );
}
/**
* Get the maximum number of retry attempts.
*
* @return int The maximum number of retry attempts.
*/
public static function get_retry_max_attempts() {
/**
* Filters the maximum number of retry attempts.
*
* @param int $retry_max_attempts The maximum number of retry attempts. Default ACTIVITYPUB_OUTBOX_RETRY_MAX_ATTEMPTS.
*/
return apply_filters( 'activitypub_dispatcher_retry_max_attempts', 3 );
}
/**
* Get the retry delay unit (in seconds).
*
* Used to calculate exponential backoff: time() + (attempt * attempt * retry_delay_unit).
*
* @return int The retry delay unit in seconds.
*/
public static function get_retry_delay() {
/**
* Filters the retry delay unit (in seconds).
*
* Used to calculate exponential backoff: time() + (attempt * attempt * retry_delay_unit).
*
* @param int $retry_delay_unit The retry delay unit in seconds. Default ACTIVITYPUB_OUTBOX_RETRY_DELAY_UNIT.
*/
return apply_filters( 'activitypub_dispatcher_retry_delay', HOUR_IN_SECONDS );
}
/**
* Get the error codes that qualify for a retry.
*
* @see https://github.com/tfredrich/RestApiTutorial.com/blob/fd08b0f67f07450521d143b123cd6e1846cb2e3b/content/advanced/responses/retries.md
*
* @return int[] The error codes.
*/
public static function get_retry_error_codes() {
/**
* Filters the error codes that qualify for a retry.
*
* @param int[] $retry_error_codes The error codes. Default array( 408, 429, 500, 502, 503, 504 ).
*/
return apply_filters( 'activitypub_dispatcher_retry_error_codes', ACTIVITYPUB_RETRY_ERROR_CODES );
}
/**
@ -90,8 +121,9 @@ class Dispatcher {
return;
}
$type = \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true );
$actor = Outbox::get_actor( $outbox_item );
if ( \is_wp_error( $actor ) ) {
if ( \is_wp_error( $actor ) && 'Delete' !== $type ) {
// If the actor is not found, publish the post and don't try again.
\wp_publish_post( $outbox_item );
return;
@ -100,13 +132,13 @@ class Dispatcher {
$activity = Outbox::get_activity( $outbox_item );
// Send to mentioned and replied-to users. Everyone other than followers.
self::send_to_additional_inboxes( $activity, $actor->get__id(), $outbox_item );
self::send_to_additional_inboxes( $activity, $outbox_item->post_author, $outbox_item );
if ( self::should_send_to_followers( $activity, $actor, $outbox_item ) ) {
Scheduler::async_batch(
self::$callback,
\do_action(
'activitypub_send_activity',
$outbox_item->ID,
self::$batch_size,
self::get_batch_size(),
\get_post_meta( $outbox_item->ID, '_activitypub_outbox_offset', true ) ?: 0 // phpcs:ignore
);
} else {
@ -119,17 +151,31 @@ class Dispatcher {
/**
* Asynchronously runs batch processing routines.
*
* @param int $outbox_item_id The Outbox item ID.
* @param int $batch_size Optional. The batch size. Default ACTIVITYPUB_OUTBOX_PROCESSING_BATCH_SIZE.
* @param int $offset Optional. The offset. Default 0.
* @param int $outbox_item_id The Outbox item ID.
* @param int|null $batch_size Optional. The batch size. Default null (uses filtered batch size).
* @param int $offset Optional. The offset. Default 0.
*
* @return array|void The next batch of followers to process, or void if done.
*/
public static function send_to_followers( $outbox_item_id, $batch_size = ACTIVITYPUB_OUTBOX_PROCESSING_BATCH_SIZE, $offset = 0 ) {
$json = Outbox::get_activity( $outbox_item_id )->to_json();
$actor = Outbox::get_actor( \get_post( $outbox_item_id ) );
$inboxes = Followers::get_inboxes_for_activity( $json, $actor->get__id(), $batch_size, $offset );
if ( null === $batch_size ) {
$batch_size = self::get_batch_size();
}
$outbox_item = \get_post( $outbox_item_id );
if ( ! $outbox_item ) {
return;
}
$activity = Outbox::get_activity( $outbox_item_id );
if ( \is_wp_error( $activity ) ) {
return;
}
$json = $activity->to_json();
$inboxes = Followers::get_inboxes_for_activity( $json, $outbox_item->post_author, $batch_size, $offset );
$retries = self::send_to_inboxes( $inboxes, $outbox_item_id );
// Retry failed inboxes.
@ -150,7 +196,7 @@ class Dispatcher {
* @param int $batch_size The batch size.
* @param int $offset The offset.
*/
\do_action( 'activitypub_outbox_processing_complete', $inboxes, $json, $actor->get__id(), $outbox_item_id, $batch_size, $offset );
\do_action( 'activitypub_outbox_processing_complete', $inboxes, $json, $outbox_item->post_author, $outbox_item_id, $batch_size, $offset );
// No more followers to process for this update.
\wp_publish_post( $outbox_item_id );
@ -167,7 +213,7 @@ class Dispatcher {
* @param int $batch_size The batch size.
* @param int $offset The offset.
*/
\do_action( 'activitypub_outbox_processing_batch_complete', $inboxes, $json, $actor->get__id(), $outbox_item_id, $batch_size, $offset );
\do_action( 'activitypub_outbox_processing_batch_complete', $inboxes, $json, $outbox_item->post_author, $outbox_item_id, $batch_size, $offset );
return array( $outbox_item_id, $batch_size, $offset + $batch_size );
}
@ -192,7 +238,7 @@ class Dispatcher {
$retries = self::send_to_inboxes( $inboxes, $outbox_item_id );
// Retry failed inboxes.
if ( ++$attempt < 3 && ! empty( $retries ) ) {
if ( ++$attempt < self::get_retry_max_attempts() && ! empty( $retries ) ) {
self::schedule_retry( $retries, $outbox_item_id, $attempt );
}
}
@ -205,8 +251,16 @@ class Dispatcher {
* @return array The failed inboxes.
*/
private static function send_to_inboxes( $inboxes, $outbox_item_id ) {
$json = Outbox::get_activity( $outbox_item_id )->to_json();
$actor = Outbox::get_actor( \get_post( $outbox_item_id ) );
$outbox_item = \get_post( $outbox_item_id );
$activity = Outbox::get_activity( $outbox_item_id );
if ( \is_wp_error( $activity ) ) {
return array();
}
$json = $activity->to_json();
$retries = array();
/**
@ -219,27 +273,67 @@ class Dispatcher {
\do_action( 'activitypub_pre_send_to_inboxes', $json, $inboxes, $outbox_item_id );
foreach ( $inboxes as $inbox ) {
$result = safe_remote_post( $inbox, $json, $actor->get__id() );
// Handle local inboxes via internal REST API, remote via HTTP.
if ( is_same_domain( $inbox ) ) {
$result = self::send_to_local_inbox( $inbox, $json );
} else {
$result = safe_remote_post( $inbox, $json, $outbox_item->post_author );
}
if ( is_wp_error( $result ) && in_array( $result->get_error_code(), self::$retry_error_codes, true ) ) {
if ( \is_wp_error( $result ) && in_array( $result->get_error_code(), self::get_retry_error_codes(), true ) ) {
$retries[] = $inbox;
}
/**
* Fires after an Activity has been sent to an inbox.
*
* @param array $result The result of the remote post request.
* @param array $result The result of the internal or remote post request.
* @param string $inbox The inbox URL.
* @param string $json The ActivityPub Activity JSON.
* @param int $actor_id The actor ID.
* @param int $outbox_item_id The Outbox item ID.
*/
\do_action( 'activitypub_sent_to_inbox', $result, $inbox, $json, $actor->get__id(), $outbox_item_id );
\do_action( 'activitypub_sent_to_inbox', $result, $inbox, $json, $outbox_item->post_author, $outbox_item_id );
}
return $retries;
}
/**
* Send an activity to a local inbox via internal REST API request.
*
* @param string $inbox_url The local inbox URL.
* @param string $json The ActivityPub Activity JSON.
* @return array|\WP_Error The result in the format of a remote post response, or WP_Error on failure.
*/
private static function send_to_local_inbox( $inbox_url, $json ) {
// Parse the inbox URL to extract the REST route.
$path = \wp_parse_url( $inbox_url, PHP_URL_PATH ) ?? '';
$rest_route = \preg_replace( '#^/' . preg_quote( \rest_get_url_prefix(), '#' ) . '#', '', $path );
// Create a REST request.
$request = new \WP_REST_Request( 'POST', $rest_route );
$request->set_header( 'Content-Type', 'application/activity+json' );
$request->set_body( $json );
$request->get_json_params();
\add_filter( 'activitypub_defer_signature_verification', '__return_true' );
$response = \rest_do_request( $request );
\remove_filter( 'activitypub_defer_signature_verification', '__return_true' );
// Return result in format similar to remote post response.
if ( $response->is_error() ) {
return $response->as_error();
}
return array(
'response' => array(
'code' => $response->get_status(),
),
'body' => \wp_json_encode( $response->get_data() ),
);
}
/**
* Schedule a retry.
*
@ -252,14 +346,9 @@ class Dispatcher {
\set_transient( $transient_key, $retries, WEEK_IN_SECONDS );
\wp_schedule_single_event(
\time() + ( $attempt * $attempt * HOUR_IN_SECONDS ),
'activitypub_async_batch',
array(
array( self::class, 'retry_send_to_followers' ),
$transient_key,
$outbox_item_id,
$attempt,
)
\time() + ( $attempt * $attempt * self::get_retry_delay() ),
'activitypub_retry_activity',
array( $transient_key, $outbox_item_id, $attempt )
);
}
@ -306,13 +395,8 @@ class Dispatcher {
$audience = array_merge( $cc, $to );
// Remove "public placeholder" and "same domain" from the audience.
$audience = array_filter(
$audience,
function ( $actor ) {
return 'https://www.w3.org/ns/activitystreams#Public' !== $actor && ! is_same_domain( $actor );
}
);
// Remove "public placeholder" from the audience.
$audience = array_diff( $audience, ACTIVITYPUB_PUBLIC_AUDIENCE_IDENTIFIERS );
if ( $audience ) {
$mentioned_inboxes = Mention::get_inboxes( $audience );
@ -377,47 +461,31 @@ class Dispatcher {
}
/**
* Adds Blog Actor inboxes to Updates so the Blog User's followers are notified of edits.
*
* @deprecated 5.2.0 Use {@see Followers::maybe_add_inboxes_of_blog_user} instead.
*
* @param array $inboxes The list of Inboxes.
* @param int $actor_id The WordPress Actor-ID.
* @param Activity $activity The ActivityPub Activity.
*
* @return array The filtered Inboxes.
*/
public static function maybe_add_inboxes_of_blog_user( $inboxes, $actor_id, $activity ) { // phpcs:ignore
_deprecated_function( __METHOD__, '5.2.0', 'Followers::maybe_add_inboxes_of_blog_user' );
return $inboxes;
}
/**
* Check if passed Activity is public.
* Check if an Activity should be sent to followers.
*
* @param Activity $activity The Activity object.
* @param \Activitypub\Model\User|\Activitypub\Model\Blog $actor The Actor object.
* @param \WP_Post $outbox_item The Outbox item.
*
* @return boolean True if public, false if not.
* @return boolean True if the Activity should be sent to followers, false if not.
*/
protected static function should_send_to_followers( $activity, $actor, $outbox_item ) {
// Check if follower endpoint is set.
$cc = $activity->get_cc() ?? array();
$to = $activity->get_to() ?? array();
$cc = (array) ( $activity->get_cc() ?? array() );
$to = (array) ( $activity->get_to() ?? array() );
$bcc = (array) ( $activity->get_bcc() ?? array() );
$bto = (array) ( $activity->get_bto() ?? array() );
$audience = array_merge( $cc, $to );
$audience = array_merge( $cc, $to, $bcc, $bto );
$send = (
// Check if activity is public.
in_array( 'https://www.w3.org/ns/activitystreams#Public', $audience, true ) ||
is_activity_public( $activity ) ||
// ...or check if follower endpoint is set.
in_array( $actor->get_followers(), $audience, true )
);
if ( $send ) {
$followers = Followers::get_inboxes_for_activity( $activity->to_json(), $actor->get__id() );
$followers = Followers::get_inboxes_for_activity( $activity->to_json(), $outbox_item->post_author );
// Only send if there are followers to send to.
$send = ! is_countable( $followers ) || 0 < count( $followers );
@ -431,7 +499,7 @@ class Dispatcher {
* @param int $actor_id The actor ID.
* @param \WP_Post $outbox_item The WordPress object.
*/
return apply_filters( 'activitypub_send_activity_to_followers', $send, $activity, $actor->get__id(), $outbox_item );
return apply_filters( 'activitypub_send_activity_to_followers', $send, $activity, $outbox_item->post_author, $outbox_item );
}
/**
@ -444,14 +512,8 @@ class Dispatcher {
* @return array The filtered Inboxes.
*/
public static function add_inboxes_of_relays( $inboxes, $actor_id, $activity ) {
// Check if follower endpoint is set.
$cc = $activity->get_cc() ?? array();
$to = $activity->get_to() ?? array();
$audience = array_merge( $cc, $to );
// Check if activity is public.
if ( ! in_array( 'https://www.w3.org/ns/activitystreams#Public', $audience, true ) ) {
if ( ! is_activity_public( $activity ) ) {
return $inboxes;
}
@ -463,4 +525,54 @@ class Dispatcher {
return array_merge( $inboxes, $relays );
}
/**
* Fire outbox handlers for activities.
*
* Triggers activity type-specific handlers to process outbox activities,
* allowing handlers to create WordPress posts or perform other side effects.
*
* @param int $outbox_id The Outbox item ID.
* @param Activity $activity The Activity that was just added to the Outbox.
*/
public static function fire_outbox_handlers( $outbox_id, $activity ) {
$outbox_item = \get_post( $outbox_id );
if ( ! $outbox_item ) {
return;
}
$type = $activity->get_type();
$user_id = $outbox_item->post_author;
$data = $activity->to_array( false );
/**
* Fires when an activity has been added to the outbox.
*
* Handlers can implement side effects like creating WordPress posts.
*
* @param array $data The activity data array.
* @param int $user_id The user ID.
* @param Activity $activity The Activity object.
* @param int $outbox_id The outbox post ID.
*/
\do_action( 'activitypub_handled_outbox_' . \strtolower( $type ), $data, $user_id, $activity, $outbox_id );
}
/**
* Send an immediate Accept activity for the given Outbox item.
*
* @param int $outbox_id The Outbox item ID.
* @param Activity $activity The Activity that was just added to the Outbox.
*/
public static function send_immediate_accept( $outbox_id, $activity ) {
$outbox_item = \get_post( $outbox_id );
if ( ! $outbox_item || 'Accept' !== $activity->get_type() ) {
return;
}
// Send to mentioned and replied-to users. Everyone other than followers.
self::send_to_additional_inboxes( $activity, $outbox_item->post_author, $outbox_item );
}
}