2023-10-22 22:20:53 +00:00
|
|
|
<?php
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* User model file.
|
|
|
|
*
|
|
|
|
* @package Activitypub
|
|
|
|
*/
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
namespace Activitypub\Model;
|
|
|
|
|
|
|
|
use WP_Error;
|
|
|
|
use Activitypub\Signature;
|
|
|
|
use Activitypub\Activity\Actor;
|
2024-10-09 12:44:17 +00:00
|
|
|
use Activitypub\Collection\Extra_Fields;
|
2023-10-22 22:20:53 +00:00
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
use function Activitypub\is_blog_public;
|
2023-10-22 22:20:53 +00:00
|
|
|
use function Activitypub\is_user_disabled;
|
|
|
|
use function Activitypub\get_rest_url_by_path;
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* User class.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
class User extends Actor {
|
|
|
|
/**
|
|
|
|
* The local User-ID (WP_User).
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Featured-Posts.
|
|
|
|
*
|
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#featured
|
|
|
|
*
|
2024-03-28 09:39:50 +00:00
|
|
|
* @context {
|
|
|
|
* "@id": "http://joinmastodon.org/ns#featured",
|
|
|
|
* "@type": "@id"
|
|
|
|
* }
|
|
|
|
*
|
2023-10-22 22:20:53 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $featured;
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Whether the User is discoverable.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
|
|
|
|
*
|
2024-03-28 09:39:50 +00:00
|
|
|
* @context http://joinmastodon.org/ns#discoverable
|
|
|
|
*
|
2023-10-22 22:20:53 +00:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $discoverable = true;
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Whether the User is indexable.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-03-28 09:39:50 +00:00
|
|
|
* @context http://joinmastodon.org/ns#indexable
|
|
|
|
*
|
2023-10-22 22:20:53 +00:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $indexable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The WebFinger Resource.
|
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @var string
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
2024-02-08 12:31:25 +00:00
|
|
|
protected $webfinger;
|
2023-10-22 22:20:53 +00:00
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* The type of the object.
|
|
|
|
*
|
|
|
|
* @return string The type of the object.
|
|
|
|
*/
|
2024-06-27 12:10:38 +00:00
|
|
|
public function get_type() {
|
|
|
|
return 'Person';
|
|
|
|
}
|
2023-10-22 22:20:53 +00:00
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Generate a User object from a WP_User.
|
|
|
|
*
|
|
|
|
* @param int $user_id The user ID.
|
|
|
|
*
|
|
|
|
* @return WP_Error|User The User object or WP_Error if user not found.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public static function from_wp_user( $user_id ) {
|
|
|
|
if ( is_user_disabled( $user_id ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_user_not_found',
|
|
|
|
\__( 'User not found', 'activitypub' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
$object = new static();
|
2023-10-22 22:20:53 +00:00
|
|
|
$object->_id = $user_id;
|
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Get the user ID.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return string The user ID.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public function get_id() {
|
|
|
|
return $this->get_url();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Get the Username.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return string The Username.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public function get_name() {
|
|
|
|
return \esc_attr( \get_the_author_meta( 'display_name', $this->_id ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Get the User description.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return string The User description.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public function get_summary() {
|
2024-10-09 12:44:17 +00:00
|
|
|
$description = get_user_option( 'activitypub_description', $this->_id );
|
2023-10-22 22:20:53 +00:00
|
|
|
if ( empty( $description ) ) {
|
|
|
|
$description = get_user_meta( $this->_id, 'description', true );
|
|
|
|
}
|
|
|
|
return \wpautop( \wp_kses( $description, 'default' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Get the User url.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return string The User url.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public function get_url() {
|
|
|
|
return \esc_url( \get_author_posts_url( $this->_id ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Returns the User URL with @-Prefix for the username.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return string The User URL with @-Prefix for the username.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
2024-02-08 12:31:25 +00:00
|
|
|
public function get_alternate_url() {
|
|
|
|
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Get the preferred username.
|
|
|
|
*
|
|
|
|
* @return string The preferred username.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_preferred_username() {
|
|
|
|
return \esc_attr( \get_the_author_meta( 'login', $this->_id ) );
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Get the User icon.
|
|
|
|
*
|
|
|
|
* @return array The User icon.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_icon() {
|
2024-10-09 12:44:17 +00:00
|
|
|
$icon = \get_user_option( 'activitypub_icon', $this->_id );
|
|
|
|
if ( wp_attachment_is_image( $icon ) ) {
|
|
|
|
return array(
|
|
|
|
'type' => 'Image',
|
|
|
|
'url' => esc_url( wp_get_attachment_url( $icon ) ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
$icon = \esc_url(
|
|
|
|
\get_avatar_url(
|
|
|
|
$this->_id,
|
|
|
|
array( 'size' => 120 )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'type' => 'Image',
|
|
|
|
'url' => $icon,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the header image.
|
|
|
|
*
|
|
|
|
* @return array|null The header image.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_image() {
|
2024-10-09 12:44:17 +00:00
|
|
|
$header_image = get_user_option( 'activitypub_header_image', $this->_id );
|
|
|
|
$image_url = null;
|
|
|
|
|
|
|
|
if ( ! $header_image && \has_header_image() ) {
|
|
|
|
$image_url = \get_header_image();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $header_image ) {
|
|
|
|
$image_url = \wp_get_attachment_url( $header_image );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $image_url ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
return array(
|
|
|
|
'type' => 'Image',
|
2024-10-09 12:44:17 +00:00
|
|
|
'url' => esc_url( $image_url ),
|
2023-10-22 22:20:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the date the user was created.
|
|
|
|
*
|
|
|
|
* @return false|string The date the user was created.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_published() {
|
|
|
|
return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( \get_the_author_meta( 'registered', $this->_id ) ) );
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the public key.
|
|
|
|
*
|
|
|
|
* @return array The public key.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_public_key() {
|
|
|
|
return array(
|
2024-10-09 12:44:17 +00:00
|
|
|
'id' => $this->get_id() . '#main-key',
|
|
|
|
'owner' => $this->get_id(),
|
2023-10-22 22:20:53 +00:00
|
|
|
'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Inbox-API-Endpoint.
|
|
|
|
*
|
|
|
|
* @return string The Inbox-Endpoint.
|
|
|
|
*/
|
|
|
|
public function get_inbox() {
|
2024-06-27 12:10:38 +00:00
|
|
|
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Outbox-API-Endpoint.
|
|
|
|
*
|
|
|
|
* @return string The Outbox-Endpoint.
|
|
|
|
*/
|
|
|
|
public function get_outbox() {
|
2024-06-27 12:10:38 +00:00
|
|
|
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Followers-API-Endpoint.
|
|
|
|
*
|
|
|
|
* @return string The Followers-Endpoint.
|
|
|
|
*/
|
|
|
|
public function get_followers() {
|
2024-06-27 12:10:38 +00:00
|
|
|
return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Following-API-Endpoint.
|
|
|
|
*
|
|
|
|
* @return string The Following-Endpoint.
|
|
|
|
*/
|
|
|
|
public function get_following() {
|
2024-06-27 12:10:38 +00:00
|
|
|
return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Featured-API-Endpoint.
|
|
|
|
*
|
|
|
|
* @return string The Featured-Endpoint.
|
|
|
|
*/
|
|
|
|
public function get_featured() {
|
2024-06-27 12:10:38 +00:00
|
|
|
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the endpoints.
|
|
|
|
*
|
|
|
|
* @return array|null The endpoints.
|
|
|
|
*/
|
2024-02-08 12:31:25 +00:00
|
|
|
public function get_endpoints() {
|
|
|
|
$endpoints = null;
|
|
|
|
|
|
|
|
if ( ACTIVITYPUB_SHARED_INBOX_FEATURE ) {
|
|
|
|
$endpoints = array(
|
|
|
|
'sharedInbox' => get_rest_url_by_path( 'inbox' ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $endpoints;
|
|
|
|
}
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
/**
|
|
|
|
* Extend the User-Output with Attachments.
|
|
|
|
*
|
|
|
|
* @return array The extended User-Output.
|
|
|
|
*/
|
|
|
|
public function get_attachment() {
|
2024-10-09 12:44:17 +00:00
|
|
|
$extra_fields = Extra_Fields::get_actor_fields( $this->_id );
|
|
|
|
return Extra_Fields::fields_to_attachments( $extra_fields );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a user@domain type of identifier for the user.
|
|
|
|
*
|
|
|
|
* @return string The Webfinger-Identifier.
|
|
|
|
*/
|
2024-02-08 12:31:25 +00:00
|
|
|
public function get_webfinger() {
|
2023-10-22 22:20:53 +00:00
|
|
|
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the canonical URL.
|
|
|
|
*
|
|
|
|
* @return string The canonical URL.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_canonical_url() {
|
|
|
|
return $this->get_url();
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the streams.
|
|
|
|
*
|
|
|
|
* @return null The streams.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_streams() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the tag.
|
|
|
|
*
|
|
|
|
* @return array The tag.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_tag() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the indexable state.
|
|
|
|
*
|
|
|
|
* @return bool Whether the user is indexable.
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
public function get_indexable() {
|
2024-10-09 12:44:17 +00:00
|
|
|
if ( is_blog_public() ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-10-09 12:44:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the username.
|
|
|
|
*
|
|
|
|
* @param string $value The new value.
|
|
|
|
* @return int|WP_Error The updated user ID or WP_Error on failure.
|
|
|
|
*/
|
|
|
|
public function update_name( $value ) {
|
|
|
|
$userdata = array(
|
|
|
|
'ID' => $this->_id,
|
|
|
|
'display_name' => $value,
|
|
|
|
);
|
|
|
|
return \wp_update_user( $userdata );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the User description.
|
|
|
|
*
|
|
|
|
* @param string $value The new value.
|
|
|
|
* @return bool True if the attribute was updated, false otherwise.
|
|
|
|
*/
|
|
|
|
public function update_summary( $value ) {
|
|
|
|
return \update_user_option( $this->_id, 'activitypub_description', $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the User icon.
|
|
|
|
*
|
|
|
|
* @param int $value The new value. Should be an attachment ID.
|
|
|
|
* @return bool True if the attribute was updated, false otherwise.
|
|
|
|
*/
|
|
|
|
public function update_icon( $value ) {
|
|
|
|
if ( ! wp_attachment_is_image( $value ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return update_user_option( $this->_id, 'activitypub_icon', $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the User-Header-Image.
|
|
|
|
*
|
|
|
|
* @param int $value The new value. Should be an attachment ID.
|
|
|
|
* @return bool True if the attribute was updated, false otherwise.
|
|
|
|
*/
|
|
|
|
public function update_header( $value ) {
|
|
|
|
if ( ! wp_attachment_is_image( $value ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return \update_user_option( $this->_id, 'activitypub_header_image', $value );
|
|
|
|
}
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|