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,12 +7,11 @@
namespace Activitypub\Model;
use WP_Query;
use Activitypub\Signature;
use Activitypub\Activity\Actor;
use Activitypub\Collection\Actors;
use function Activitypub\get_rest_url_by_path;
use function Activitypub\home_host;
/**
* Application class.
@ -34,7 +33,7 @@ class Application extends Actor {
*
* @context http://joinmastodon.org/ns#discoverable
*
* @var boolean
* @var bool
*/
protected $discoverable = false;
@ -43,34 +42,64 @@ class Application extends Actor {
*
* @context http://joinmastodon.org/ns#indexable
*
* @var boolean
* @var bool
*/
protected $indexable = false;
/**
* The WebFinger Resource.
* Whether the Application manually approves followers.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#as
*
* @context as:manuallyApprovesFollowers
*
* @var bool
*/
protected $manually_approves_followers = true;
/**
* List of software capabilities implemented by the Application.
*
* @see https://codeberg.org/silverpill/feps/src/branch/main/844e/fep-844e.md
*
* @var array
*/
protected $implements = array(
array(
'href' => 'https://datatracker.ietf.org/doc/html/rfc9421',
'name' => 'RFC-9421: HTTP Message Signatures',
),
);
/**
* Set Application as invisible.
*
* @see https://litepub.social/
*
* @var bool
*/
protected $invisible = true;
/**
* The type of the Actor.
*
* @var string
*/
protected $webfinger;
protected $type = 'Application';
/**
* Returns the type of the object.
* The Username.
*
* @return string The type of the object.
* @var string
*/
public function get_type() {
return 'Application';
}
protected $name = 'application';
/**
* Returns whether the Application manually approves followers.
* The preferred username.
*
* @return true Whether the Application manually approves followers.
* @var string
*/
public function get_manually_approves_followers() {
return true;
}
protected $preferred_username = 'application';
/**
* Returns the ID of the Application.
@ -99,24 +128,6 @@ class Application extends Actor {
return $this->get_id();
}
/**
* Get the Username.
*
* @return string The Username.
*/
public function get_name() {
return 'application';
}
/**
* Get the preferred username.
*
* @return string The preferred username.
*/
public function get_preferred_username() {
return $this->get_name();
}
/**
* Get the User-Icon.
*
@ -173,7 +184,7 @@ class Application extends Actor {
* @return string The published date.
*/
public function get_published() {
$first_post = new WP_Query(
$first_post = new \WP_Query(
array(
'orderby' => 'date',
'order' => 'ASC',
@ -226,7 +237,7 @@ class Application extends Actor {
return array(
'id' => $this->get_id() . '#main-key',
'owner' => $this->get_id(),
'publicKeyPem' => Signature::get_public_key_for( Actors::APPLICATION_USER_ID ),
'publicKeyPem' => Actors::get_public_key( Actors::APPLICATION_USER_ID ),
);
}
@ -236,11 +247,10 @@ class Application extends Actor {
* @return string The User description.
*/
public function get_summary() {
return \wpautop(
\wp_kses(
\get_bloginfo( 'description' ),
'default'
)
return sprintf(
/* translators: %s: Domain of the site */
__( 'This is the Application Actor for %s.', 'activitypub' ),
home_host()
);
}

View File

@ -10,14 +10,12 @@ namespace Activitypub\Model;
use Activitypub\Activity\Actor;
use Activitypub\Collection\Actors;
use Activitypub\Collection\Extra_Fields;
use Activitypub\Signature;
use WP_Query;
use function Activitypub\esc_hashtag;
use function Activitypub\is_single_user;
use function Activitypub\is_blog_public;
use function Activitypub\get_rest_url_by_path;
use function Activitypub\get_attribution_domains;
use function Activitypub\get_rest_url_by_path;
use function Activitypub\is_blog_public;
use function Activitypub\is_single_user;
/**
* Blog class.
@ -25,29 +23,6 @@ use function Activitypub\get_attribution_domains;
* @method int get__id() Gets the internal user ID for the blog (always returns BLOG_USER_ID).
*/
class Blog extends Actor {
/**
* The Featured-Posts.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#featured
*
* @context {
* "@id": "http://joinmastodon.org/ns#featured",
* "@type": "@id"
* }
*
* @var string
*/
protected $featured;
/**
* Moderators endpoint.
*
* @see https://join-lemmy.org/docs/contributors/05-federation.html
*
* @var string
*/
protected $moderators;
/**
* The User-ID
*
@ -56,40 +31,22 @@ class Blog extends Actor {
protected $_id = Actors::BLOG_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* If the User is indexable.
* The generator of the object.
*
* @context http://joinmastodon.org/ns#indexable
* @see https://www.w3.org/TR/activitypub/#generator
* @see https://codeberg.org/fediverse/fep/src/branch/main/fep/844e/fep-844e.md#discovery-through-an-actor
*
* @var boolean
* @var array
*/
protected $indexable;
/**
* The WebFinger Resource.
*
* @var string
*/
protected $webfinger;
/**
* Whether the User is discoverable.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
*
* @context http://joinmastodon.org/ns#discoverable
*
* @var boolean
*/
protected $discoverable;
/**
* Restrict posting to mods.
*
* @see https://join-lemmy.org/docs/contributors/05-federation.html
*
* @var boolean
*/
protected $posting_restricted_to_mods;
protected $generator = array(
'type' => 'Application',
'implements' => array(
array(
'href' => 'https://datatracker.ietf.org/doc/html/rfc9421',
'name' => 'RFC-9421: HTTP Message Signatures',
),
),
);
/**
* Constructor.
@ -136,20 +93,25 @@ class Blog extends Actor {
$permalink = \get_option( 'activitypub_use_permalink_as_id_for_blog', false );
if ( $permalink ) {
return $this->get_url();
return \esc_url( \home_url( '/@' . $this->get_preferred_username() ) );
}
return \add_query_arg( 'author', $this->_id, \trailingslashit( \home_url() ) );
return \add_query_arg( 'author', $this->_id, \home_url( '/' ) );
}
/**
* Get the type of the object.
*
* If relay mode is enabled, return "Service".
* If the Blog is in "single user" mode, return "Person" instead of "Group".
*
* @return string The type of the object.
*/
public function get_type() {
if ( \get_option( 'activitypub_relay_mode', false ) ) {
return 'Service';
}
if ( is_single_user() ) {
return 'Person';
} else {
@ -198,7 +160,7 @@ class Blog extends Actor {
* @return string The User url.
*/
public function get_url() {
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
return \get_bloginfo( 'url' );
}
/**
@ -314,21 +276,44 @@ class Blog extends Actor {
* @return string The published date.
*/
public function get_published() {
$first_post = new WP_Query(
$published = \get_option( 'activitypub_blog_published' );
if ( $published ) {
return $published;
}
// Backfill from the first federated post.
$first_federated = new \WP_Query(
array(
'orderby' => 'date',
'order' => 'ASC',
'number' => 1,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'post_status' => 'publish',
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
array(
'key' => 'activitypub_status',
'compare' => 'EXISTS',
),
),
)
);
if ( ! empty( $first_post->posts[0] ) ) {
$time = \strtotime( $first_post->posts[0]->post_date_gmt );
if ( ! empty( $first_federated->posts[0] ) ) {
$time = \strtotime( $first_federated->posts[0]->post_date_gmt );
} else {
$time = \time();
}
return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $time );
$published = \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $time );
\update_option( 'activitypub_blog_published', $published, false );
return $published;
}
/**
@ -375,7 +360,7 @@ class Blog extends Actor {
return array(
'id' => $this->get_id() . '#main-key',
'owner' => $this->get_id(),
'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
'publicKeyPem' => Actors::get_public_key( $this->get__id() ),
);
}
@ -434,15 +419,14 @@ class Blog extends Actor {
* @return string[]|null The endpoints.
*/
public function get_endpoints() {
$endpoints = null;
if ( \get_option( 'activitypub_shared_inbox' ) ) {
$endpoints = array(
'sharedInbox' => get_rest_url_by_path( 'inbox' ),
);
}
return $endpoints;
return array(
'sharedInbox' => get_rest_url_by_path( 'inbox' ),
'oauthAuthorizationEndpoint' => get_rest_url_by_path( 'oauth/authorize' ),
'oauthTokenEndpoint' => get_rest_url_by_path( 'oauth/token' ),
'oauthRegistrationEndpoint' => get_rest_url_by_path( 'oauth/clients' ),
'proxyUrl' => get_rest_url_by_path( 'proxy' ),
'proxyEventStream' => get_rest_url_by_path( 'proxy/stream' ),
);
}
/**
@ -454,6 +438,17 @@ class Blog extends Actor {
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
}
/**
* Returns the Liked API endpoint.
*
* @since 8.1.0
*
* @return string The Liked endpoint.
*/
public function get_liked() {
return get_rest_url_by_path( sprintf( 'actors/%d/liked', $this->get__id() ) );
}
/**
* Returns the Featured-API-Endpoint.
*
@ -463,6 +458,15 @@ class Blog extends Actor {
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
}
/**
* Returns the Featured-Tags-API-Endpoint.
*
* @return string The Featured-Tags-Endpoint.
*/
public function get_featured_tags() {
return get_rest_url_by_path( sprintf( 'actors/%d/collections/tags', $this->get__id() ) );
}
/**
* Returns whether the site is indexable.
*

View File

@ -7,9 +7,11 @@
namespace Activitypub\Model;
use WP_Error;
use Activitypub\Activity\Actor;
use Activitypub\Collection\Followers;
use Activitypub\Collection\Remote_Actors;
use function Activitypub\extract_name_from_uri;
/**
* ActivityPub Follower Class.
@ -20,6 +22,7 @@ use Activitypub\Collection\Followers;
* @author Matt Wiebe
* @author Matthias Pfefferle
*
* @deprecated 7.0.0
* @see https://www.w3.org/TR/activitypub/#follow-activity-inbox
*
* @method int get__id() Gets the post ID of the follower record.
@ -42,13 +45,31 @@ class Follower extends Actor {
*/
protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* Constructor.
*
* @deprecated Use Actor instead.
*/
public function __construct() {
\_deprecated_class( __CLASS__, '7.0.0', Actor::class );
}
/**
* Get the errors.
*
* @return mixed
*/
public function get_errors() {
return get_post_meta( $this->_id, '_activitypub_errors', false );
return Remote_Actors::get_errors( $this->_id );
}
/**
* Clear the errors for the current Follower.
*
* @return bool True on success, false on failure.
*/
public function clear_errors() {
return Remote_Actors::clear_errors( $this->_id );
}
/**
@ -82,9 +103,11 @@ class Follower extends Actor {
/**
* Reset (delete) all errors.
*
* @return bool True on success, false on failure.
*/
public function reset_errors() {
delete_post_meta( $this->_id, '_activitypub_errors' );
return Remote_Actors::clear_errors( $this->_id );
}
/**
@ -93,13 +116,7 @@ class Follower extends Actor {
* @return int The number of errors.
*/
public function count_errors() {
$errors = $this->get_errors();
if ( is_array( $errors ) && ! empty( $errors ) ) {
return count( $errors );
}
return 0;
return Remote_Actors::count_errors( $this->_id );
}
/**
@ -110,8 +127,8 @@ class Follower extends Actor {
public function get_latest_error_message() {
$errors = $this->get_errors();
if ( is_array( $errors ) && ! empty( $errors ) ) {
return reset( $errors );
if ( \is_array( $errors ) && ! empty( $errors ) ) {
return \reset( $errors );
}
return '';
@ -151,61 +168,26 @@ class Follower extends Actor {
/**
* Save the current Follower object.
*
* @return int|WP_Error The post ID or an WP_Error.
* @return int|\WP_Error The post ID or an WP_Error.
*/
public function save() {
if ( ! $this->is_valid() ) {
return new WP_Error( 'activitypub_invalid_follower', __( 'Invalid Follower', 'activitypub' ), array( 'status' => 400 ) );
return new \WP_Error( 'activitypub_invalid_follower', __( 'Invalid Follower', 'activitypub' ), array( 'status' => 400 ) );
}
if ( ! $this->get__id() ) {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$post_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid=%s",
esc_sql( $this->get_id() )
)
);
if ( $post_id ) {
$post = get_post( $post_id );
$this->set__id( $post->ID );
}
$id = Remote_Actors::upsert( $this );
if ( \is_wp_error( $id ) ) {
return $id;
}
$post_id = $this->get__id();
$args = array(
'ID' => $post_id,
'guid' => esc_url_raw( $this->get_id() ),
'post_title' => wp_strip_all_tags( sanitize_text_field( $this->get_name() ) ),
'post_author' => 0,
'post_type' => Followers::POST_TYPE,
'post_name' => esc_url_raw( $this->get_id() ),
'post_excerpt' => sanitize_text_field( wp_kses( $this->get_summary(), 'user_description' ) ),
'post_status' => 'publish',
'meta_input' => $this->get_post_meta_input(),
);
if ( ! empty( $post_id ) ) {
// If this is an update, prevent the "followed" date from being overwritten by the current date.
$post = get_post( $post_id );
$args['post_date'] = $post->post_date;
$args['post_date_gmt'] = $post->post_date_gmt;
}
$post_id = wp_insert_post( $args );
$this->_id = $post_id;
return $post_id;
$this->set__id( $id );
return $id;
}
/**
* Upsert the current Follower object.
*
* @return int|WP_Error The post ID or an WP_Error.
* @return int|\WP_Error The post ID or an WP_Error.
*/
public function upsert() {
return $this->save();
@ -221,18 +203,7 @@ class Follower extends Actor {
* @see \Activitypub\Rest\Followers::remove_follower()
*/
public function delete() {
wp_delete_post( $this->_id );
}
/**
* Update the post meta.
*/
protected function get_post_meta_input() {
$meta_input = array();
$meta_input['_activitypub_inbox'] = $this->get_shared_inbox();
$meta_input['_activitypub_actor_json'] = wp_slash( $this->to_json() );
return $meta_input;
Followers::remove_follower( $this->_id, $this->get_id() );
}
/**
@ -298,7 +269,7 @@ class Follower extends Actor {
return '';
}
if ( is_array( $icon ) ) {
if ( \is_array( $icon ) ) {
return $icon['url'];
}
@ -317,7 +288,7 @@ class Follower extends Actor {
return '';
}
if ( is_array( $image ) ) {
if ( \is_array( $image ) ) {
return $image['url'];
}
@ -346,12 +317,16 @@ class Follower extends Actor {
* @return Follower|false The Follower object or false on failure.
*/
public static function init_from_cpt( $post ) {
$actor_json = get_post_meta( $post->ID, '_activitypub_actor_json', true );
if ( empty( $post->post_content ) ) {
$json = \get_post_meta( $post->ID, '_activitypub_actor_json', true );
} else {
$json = $post->post_content;
}
/* @var Follower $object Follower object. */
$object = self::init_from_json( $actor_json );
$object = self::init_from_json( $json );
if ( is_wp_error( $object ) ) {
if ( \is_wp_error( $object ) ) {
return false;
}
@ -374,37 +349,11 @@ class Follower extends Actor {
protected function extract_name_from_uri() {
// prefer the URL, but fall back to the ID.
if ( $this->url ) {
$name = $this->url;
$uri = $this->url;
} else {
$name = $this->id;
$uri = $this->id;
}
if ( \filter_var( $name, FILTER_VALIDATE_URL ) ) {
$name = \rtrim( $name, '/' );
$path = \wp_parse_url( $name, PHP_URL_PATH );
if ( $path ) {
if ( \strpos( $name, '@' ) !== false ) {
// Expected: https://example.com/@user (default URL pattern).
$name = \preg_replace( '|^/@?|', '', $path );
} else {
// Expected: https://example.com/users/user (default ID pattern).
$parts = \explode( '/', $path );
$name = \array_pop( $parts );
}
}
} elseif (
\is_email( $name ) ||
\strpos( $name, 'acct' ) === 0 ||
\strpos( $name, '@' ) === 0
) {
// Expected: user@example.com or acct:user@example (WebFinger).
$name = \ltrim( $name, '@' );
$name = \ltrim( $name, 'acct:' );
$parts = \explode( '@', $name );
$name = $parts[0];
}
return $name;
return extract_name_from_uri( $uri );
}
}

View File

@ -8,13 +8,12 @@
namespace Activitypub\Model;
use Activitypub\Activity\Actor;
use Activitypub\Collection\Actors;
use Activitypub\Collection\Extra_Fields;
use Activitypub\Http;
use Activitypub\Signature;
use function Activitypub\is_blog_public;
use function Activitypub\get_rest_url_by_path;
use function Activitypub\get_attribution_domains;
use function Activitypub\get_rest_url_by_path;
use function Activitypub\is_blog_public;
use function Activitypub\user_can_activitypub;
/**
@ -30,20 +29,6 @@ class User extends Actor {
*/
protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* The Featured-Posts.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#featured
*
* @context {
* "@id": "http://joinmastodon.org/ns#featured",
* "@type": "@id"
* }
*
* @var string
*/
protected $featured;
/**
* Whether the User is discoverable.
*
@ -56,20 +41,22 @@ class User extends Actor {
protected $discoverable = true;
/**
* Whether the User is indexable.
* The generator of the object.
*
* @context http://joinmastodon.org/ns#indexable
* @see https://www.w3.org/TR/activitypub/#generator
* @see https://codeberg.org/fediverse/fep/src/branch/main/fep/844e/fep-844e.md#discovery-through-an-actor
*
* @var boolean
* @var array
*/
protected $indexable;
/**
* The WebFinger Resource.
*
* @var string
*/
protected $webfinger;
protected $generator = array(
'type' => 'Application',
'implements' => array(
array(
'href' => 'https://datatracker.ietf.org/doc/html/rfc9421',
'name' => 'RFC-9421: HTTP Message Signatures',
),
),
);
/**
* Constructor.
@ -135,7 +122,7 @@ class User extends Actor {
return $this->get_url();
}
return \add_query_arg( 'author', $this->_id, \trailingslashit( \home_url() ) );
return \add_query_arg( 'author', $this->_id, \home_url( '/' ) );
}
/**
@ -184,7 +171,14 @@ class User extends Actor {
* @return string The preferred username.
*/
public function get_preferred_username() {
return \get_the_author_meta( 'login', $this->_id );
$login = \get_the_author_meta( 'login', $this->_id );
// Handle cases where login is an email address (e.g., from Site Kit Google login).
if ( \filter_var( $login, FILTER_VALIDATE_EMAIL ) ) {
$login = \get_the_author_meta( 'user_nicename', $this->_id );
}
return $login;
}
/**
@ -259,7 +253,7 @@ class User extends Actor {
return array(
'id' => $this->get_id() . '#main-key',
'owner' => $this->get_id(),
'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
'publicKeyPem' => Actors::get_public_key( $this->get__id() ),
);
}
@ -299,6 +293,17 @@ class User extends Actor {
return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) );
}
/**
* Returns the Liked API endpoint.
*
* @since 8.1.0
*
* @return string The Liked endpoint.
*/
public function get_liked() {
return get_rest_url_by_path( sprintf( 'actors/%d/liked', $this->get__id() ) );
}
/**
* Returns the Featured-API-Endpoint.
*
@ -308,21 +313,29 @@ class User extends Actor {
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
}
/**
* Returns the Featured-Tags-API-Endpoint.
*
* @return string The Featured-Tags-Endpoint.
*/
public function get_featured_tags() {
return get_rest_url_by_path( sprintf( 'actors/%d/collections/tags', $this->get__id() ) );
}
/**
* Returns the endpoints.
*
* @return string[]|null The endpoints.
*/
public function get_endpoints() {
$endpoints = null;
if ( \get_option( 'activitypub_shared_inbox' ) ) {
$endpoints = array(
'sharedInbox' => get_rest_url_by_path( 'inbox' ),
);
}
return $endpoints;
return array(
'sharedInbox' => get_rest_url_by_path( 'inbox' ),
'oauthAuthorizationEndpoint' => get_rest_url_by_path( 'oauth/authorize' ),
'oauthTokenEndpoint' => get_rest_url_by_path( 'oauth/token' ),
'oauthRegistrationEndpoint' => get_rest_url_by_path( 'oauth/clients' ),
'proxyUrl' => get_rest_url_by_path( 'proxy' ),
'proxyEventStream' => get_rest_url_by_path( 'proxy/stream' ),
);
}
/**
@ -455,7 +468,6 @@ class User extends Actor {
$this->get_alternate_url(),
);
// phpcs:ignore Universal.Operators.DisallowShortTernary.Found
$also_known_as = array_merge( $also_known_as, \get_user_option( 'activitypub_also_known_as', $this->_id ) ?: array() );
return array_unique( $also_known_as );