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

@ -7,7 +7,6 @@
namespace Activitypub;
use WP_Error;
use Activitypub\Collection\Actors;
/**
@ -23,7 +22,7 @@ class Http {
* @param string $body The Post Body.
* @param int $user_id The WordPress User-ID.
*
* @return array|WP_Error The POST Response or an WP_Error.
* @return array|\WP_Error The POST Response or an WP_Error.
*/
public static function post( $url, $body, $user_id ) {
/**
@ -35,38 +34,42 @@ class Http {
*/
\do_action( 'activitypub_pre_http_post', $url, $body, $user_id );
$date = \gmdate( 'D, d M Y H:i:s T' );
$digest = Signature::generate_digest( $body );
$signature = Signature::generate_signature( $user_id, 'post', $url, $date, $digest );
$wp_version = get_masked_wp_version();
/**
* Filters the HTTP headers user agent string.
*
* @param string $user_agent The user agent string.
* @param string $url The request URL.
*/
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
$args = array(
'timeout' => 100,
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . get_masked_wp_version() . '; ' . \get_bloginfo( 'url' ), $url );
/**
* Filters the timeout duration for remote POST requests in ActivityPub.
*
* @param int $timeout The timeout value in seconds. Default 10 seconds.
*/
$timeout = \apply_filters( 'activitypub_remote_post_timeout', 10 );
$args = array(
'timeout' => $timeout,
'limit_response_size' => 1048576,
'redirection' => 3,
'user-agent' => "$user_agent; ActivityPub",
'headers' => array(
'Accept' => 'application/activity+json',
'Content-Type' => 'application/activity+json',
'Digest' => $digest,
'Signature' => $signature,
'Date' => $date,
'Date' => \gmdate( 'D, d M Y H:i:s T' ),
),
'body' => $body,
'key_id' => \json_decode( $body )->actor . '#main-key',
'private_key' => Actors::get_private_key( $user_id ),
'user_id' => $user_id,
);
$response = \wp_safe_remote_post( $url, $args );
$code = \wp_remote_retrieve_response_code( $response );
if ( $code >= 400 ) {
$response = new WP_Error(
$response = new \WP_Error(
$code,
__( 'Failed HTTP Request', 'activitypub' ),
array(
@ -79,10 +82,10 @@ class Http {
/**
* Action to save the response of the remote POST request.
*
* @param array|WP_Error $response The response of the remote POST request.
* @param string $url The URL endpoint.
* @param string $body The Post Body.
* @param int $user_id The WordPress User-ID.
* @param array|\WP_Error $response The response of the remote POST request.
* @param string $url The URL endpoint.
* @param string $body The Post Body.
* @param int $user_id The WordPress User-ID.
*/
\do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
@ -93,11 +96,24 @@ class Http {
* Send a GET Request with the needed HTTP Headers.
*
* @param string $url The URL endpoint.
* @param bool|int $cached Optional. Whether the result should be cached, or its duration. Default false.
* @param array $args Optional. Additional arguments to customize the request.
* - 'headers': Array of headers to override defaults.
* @param bool|int $cached Optional. Whether to return cached results, or cache duration. Default false.
*
* @return array|WP_Error The GET Response or a WP_Error.
* @return array|\WP_Error The GET Response or a WP_Error.
*/
public static function get( $url, $cached = false ) {
public static function get( $url, $args = array(), $cached = false ) {
// Backward compatibility: if $args is boolean/int, it's the old $cached parameter.
if ( ! \is_array( $args ) ) {
\_deprecated_argument(
__METHOD__,
'7.9.0',
\esc_html__( 'The $cached parameter should now be passed as the third argument.', 'activitypub' )
);
$cached = $args;
$args = array();
}
/**
* Fires before an HTTP GET request is made.
*
@ -105,17 +121,18 @@ class Http {
*/
\do_action( 'activitypub_pre_http_get', $url );
if ( $cached ) {
$transient_key = self::generate_cache_key( $url );
$transient_key = self::generate_cache_key( $url );
// Check cache only if caching is requested.
if ( $cached ) {
$response = \get_transient( $transient_key );
if ( $response ) {
/**
* Action to save the response of the remote GET request.
*
* @param array|WP_Error $response The response of the remote GET request.
* @param string $url The URL endpoint.
* @param array|\WP_Error $response The response of the remote GET request.
* @param string $url The URL endpoint.
*/
\do_action( 'activitypub_safe_remote_get_response', $response, $url );
@ -123,29 +140,22 @@ class Http {
}
}
$date = \gmdate( 'D, d M Y H:i:s T' );
$signature = Signature::generate_signature( Actors::APPLICATION_USER_ID, 'get', $url, $date );
$wp_version = get_masked_wp_version();
/**
* Filters the HTTP headers user agent string.
*
* This filter allows developers to modify the user agent string that is
* sent with HTTP requests.
*
* @param string $user_agent The user agent string.
* @param string $url The request URL.
*/
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . get_masked_wp_version() . '; ' . \get_bloginfo( 'url' ), $url );
/**
* Filters the timeout duration for remote GET requests in ActivityPub.
*
* @param int $timeout The timeout value in seconds. Default 100 seconds.
* @param int $timeout The timeout value in seconds. Default 10 seconds.
*/
$timeout = \apply_filters( 'activitypub_remote_get_timeout', 100 );
$timeout = \apply_filters( 'activitypub_remote_get_timeout', 10 );
$args = array(
$defaults = array(
'timeout' => $timeout,
'limit_response_size' => 1048576,
'redirection' => 3,
@ -153,33 +163,54 @@ class Http {
'headers' => array(
'Accept' => 'application/activity+json',
'Content-Type' => 'application/activity+json',
'Signature' => $signature,
'Date' => $date,
'Date' => \gmdate( 'D, d M Y H:i:s T' ),
),
'key_id' => Actors::get_by_id( Actors::APPLICATION_USER_ID )->get_id() . '#main-key',
'private_key' => Actors::get_private_key( Actors::APPLICATION_USER_ID ),
);
$args = \wp_parse_args( $args, $defaults );
$args['headers'] = \wp_parse_args( $args['headers'], $defaults['headers'] );
$response = \wp_safe_remote_get( $url, $args );
$code = \wp_remote_retrieve_response_code( $response );
if ( $code >= 400 ) {
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) );
if ( \is_wp_error( $response ) || $code >= 400 ) {
if ( ! $code ) {
$code = 0;
}
$response = new \WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) );
/*
* Always cache errors to prevent repeated timeout waits.
* - Retriable errors (timeouts, 5xx): 1 minute (server may recover quickly).
* - Other errors (4xx): 15 minutes (client errors are more permanent).
*/
if ( \in_array( $code, ACTIVITYPUB_RETRY_ERROR_CODES, true ) || 0 === $code ) {
$cache_duration = MINUTE_IN_SECONDS;
} else {
$cache_duration = 15 * MINUTE_IN_SECONDS;
}
\set_transient( $transient_key, $response, $cache_duration );
return $response;
}
/**
* Action to save the response of the remote GET request.
*
* @param array|WP_Error $response The response of the remote GET request.
* @param string $url The URL endpoint.
* @param array|\WP_Error $response The response of the remote GET request.
* @param string $url The URL endpoint.
*/
\do_action( 'activitypub_safe_remote_get_response', $response, $url );
if ( $cached ) {
$cache_duration = $cached;
if ( ! is_int( $cache_duration ) ) {
$cache_duration = HOUR_IN_SECONDS;
}
\set_transient( $transient_key, $response, $cache_duration );
// Always cache successful responses.
$cache_duration = $cached;
if ( ! is_int( $cache_duration ) ) {
$cache_duration = HOUR_IN_SECONDS;
}
\set_transient( $transient_key, $response, $cache_duration );
return $response;
}
@ -192,27 +223,9 @@ class Http {
* @return bool True if the URL is a tombstone.
*/
public static function is_tombstone( $url ) {
/**
* Fires before checking if the URL is a tombstone.
*
* @param string $url The URL to check.
*/
\do_action( 'activitypub_pre_http_is_tombstone', $url );
_deprecated_function( __METHOD__, '7.3.0', 'Activitypub\Tombstone::exists_remote' );
$response = \wp_safe_remote_get( $url, array( 'headers' => array( 'Accept' => 'application/activity+json' ) ) );
$code = \wp_remote_retrieve_response_code( $response );
if ( in_array( (int) $code, array( 404, 410 ), true ) ) {
return true;
}
$data = \wp_remote_retrieve_body( $response );
$data = \json_decode( $data, true );
if ( $data && isset( $data['type'] ) && 'Tombstone' === $data['type'] ) {
return true;
}
return false;
return Tombstone::exists_remote( $url );
}
/**
@ -232,17 +245,28 @@ class Http {
* @param array|string $url_or_object The Object or the Object URL.
* @param bool $cached Optional. Whether the result should be cached. Default true.
*
* @return array|WP_Error The Object data as array or WP_Error on failure.
* @return array|\WP_Error The Object data as array or WP_Error on failure.
*/
public static function get_remote_object( $url_or_object, $cached = true ) {
/**
* Filters the preemptive return value of a remote object request.
*
* @param array|string|null $response The response.
* @param array|string|null $url_or_object The Object or the Object URL.
*/
$response = apply_filters( 'activitypub_pre_http_get_remote_object', null, $url_or_object );
if ( null !== $response ) {
return $response;
}
$url = object_to_uri( $url_or_object );
if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $url ) ) {
if ( Webfinger::is_acct( $url ) ) {
$url = Webfinger::resolve( $url );
}
if ( ! $url ) {
return new WP_Error(
return new \WP_Error(
'activitypub_no_valid_actor_identifier',
\__( 'The "actor" identifier is not valid', 'activitypub' ),
array(
@ -252,23 +276,12 @@ class Http {
);
}
if ( is_wp_error( $url ) ) {
if ( \is_wp_error( $url ) ) {
return $url;
}
$transient_key = self::generate_cache_key( $url );
// Only check the cache if needed.
if ( $cached ) {
$data = \get_transient( $transient_key );
if ( $data ) {
return $data;
}
}
if ( ! \wp_http_validate_url( $url ) ) {
return new WP_Error(
return new \WP_Error(
'activitypub_no_valid_object_url',
\__( 'The "object" is/has no valid URL', 'activitypub' ),
array(
@ -278,7 +291,7 @@ class Http {
);
}
$response = self::get( $url );
$response = self::get( $url, array(), $cached );
if ( \is_wp_error( $response ) ) {
return $response;
@ -288,7 +301,7 @@ class Http {
$data = \json_decode( $data, true );
if ( ! $data ) {
return new WP_Error(
return new \WP_Error(
'activitypub_invalid_json',
\__( 'No valid JSON data', 'activitypub' ),
array(
@ -298,8 +311,6 @@ class Http {
);
}
\set_transient( $transient_key, $data, WEEK_IN_SECONDS );
return $data;
}
}