updated plugin ActivityPub
version 3.3.3
This commit is contained in:
@ -1,18 +1,24 @@
|
||||
<?php
|
||||
/**
|
||||
* User model file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Model;
|
||||
|
||||
use WP_Query;
|
||||
use WP_Error;
|
||||
use Activitypub\Migration;
|
||||
use Activitypub\Signature;
|
||||
use Activitypub\Model\Blog;
|
||||
use Activitypub\Activity\Actor;
|
||||
use Activitypub\Collection\Users;
|
||||
use Activitypub\Collection\Extra_Fields;
|
||||
|
||||
use function Activitypub\is_blog_public;
|
||||
use function Activitypub\is_user_disabled;
|
||||
use function Activitypub\get_rest_url_by_path;
|
||||
use function Activitypub\get_actor_extra_fields;
|
||||
|
||||
/**
|
||||
* User class.
|
||||
*/
|
||||
class User extends Actor {
|
||||
/**
|
||||
* The local User-ID (WP_User).
|
||||
@ -36,7 +42,7 @@ class User extends Actor {
|
||||
protected $featured;
|
||||
|
||||
/**
|
||||
* If the User is discoverable.
|
||||
* Whether the User is discoverable.
|
||||
*
|
||||
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
|
||||
*
|
||||
@ -47,7 +53,7 @@ class User extends Actor {
|
||||
protected $discoverable = true;
|
||||
|
||||
/**
|
||||
* If the User is indexable.
|
||||
* Whether the User is indexable.
|
||||
*
|
||||
* @context http://joinmastodon.org/ns#indexable
|
||||
*
|
||||
@ -58,14 +64,26 @@ class User extends Actor {
|
||||
/**
|
||||
* The WebFinger Resource.
|
||||
*
|
||||
* @var string<url>
|
||||
* @var string
|
||||
*/
|
||||
protected $webfinger;
|
||||
|
||||
/**
|
||||
* The type of the object.
|
||||
*
|
||||
* @return string The type of the object.
|
||||
*/
|
||||
public function get_type() {
|
||||
return 'Person';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public static function from_wp_user( $user_id ) {
|
||||
if ( is_user_disabled( $user_id ) ) {
|
||||
return new WP_Error(
|
||||
@ -75,37 +93,37 @@ class User extends Actor {
|
||||
);
|
||||
}
|
||||
|
||||
$object = new static();
|
||||
$object = new static();
|
||||
$object->_id = $user_id;
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User-ID.
|
||||
* Get the user ID.
|
||||
*
|
||||
* @return string The User-ID.
|
||||
* @return string The user ID.
|
||||
*/
|
||||
public function get_id() {
|
||||
return $this->get_url();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User-Name.
|
||||
* Get the Username.
|
||||
*
|
||||
* @return string The User-Name.
|
||||
* @return string The Username.
|
||||
*/
|
||||
public function get_name() {
|
||||
return \esc_attr( \get_the_author_meta( 'display_name', $this->_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User-Description.
|
||||
* Get the User description.
|
||||
*
|
||||
* @return string The User-Description.
|
||||
* @return string The User description.
|
||||
*/
|
||||
public function get_summary() {
|
||||
$description = get_user_meta( $this->_id, 'activitypub_user_description', true );
|
||||
$description = get_user_option( 'activitypub_description', $this->_id );
|
||||
if ( empty( $description ) ) {
|
||||
$description = get_user_meta( $this->_id, 'description', true );
|
||||
}
|
||||
@ -113,28 +131,46 @@ class User extends Actor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User-Url.
|
||||
* Get the User url.
|
||||
*
|
||||
* @return string The User-Url.
|
||||
* @return string The User url.
|
||||
*/
|
||||
public function get_url() {
|
||||
return \esc_url( \get_author_posts_url( $this->_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the User-URL with @-Prefix for the username.
|
||||
* Returns the User URL with @-Prefix for the username.
|
||||
*
|
||||
* @return string The User-URL with @-Prefix for the username.
|
||||
* @return string The User URL with @-Prefix for the username.
|
||||
*/
|
||||
public function get_alternate_url() {
|
||||
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the preferred username.
|
||||
*
|
||||
* @return string The preferred username.
|
||||
*/
|
||||
public function get_preferred_username() {
|
||||
return \esc_attr( \get_the_author_meta( 'login', $this->_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User icon.
|
||||
*
|
||||
* @return array The User icon.
|
||||
*/
|
||||
public function get_icon() {
|
||||
$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 ) ),
|
||||
);
|
||||
}
|
||||
|
||||
$icon = \esc_url(
|
||||
\get_avatar_url(
|
||||
$this->_id,
|
||||
@ -148,26 +184,51 @@ class User extends Actor {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the header image.
|
||||
*
|
||||
* @return array|null The header image.
|
||||
*/
|
||||
public function get_image() {
|
||||
if ( \has_header_image() ) {
|
||||
$image = \esc_url( \get_header_image() );
|
||||
$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 ) {
|
||||
return array(
|
||||
'type' => 'Image',
|
||||
'url' => $image,
|
||||
'url' => esc_url( $image_url ),
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the date the user was created.
|
||||
*
|
||||
* @return false|string The date the user was created.
|
||||
*/
|
||||
public function get_published() {
|
||||
return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( \get_the_author_meta( 'registered', $this->_id ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public key.
|
||||
*
|
||||
* @return array The public key.
|
||||
*/
|
||||
public function get_public_key() {
|
||||
return array(
|
||||
'id' => $this->get_id() . '#main-key',
|
||||
'owner' => $this->get_id(),
|
||||
'id' => $this->get_id() . '#main-key',
|
||||
'owner' => $this->get_id(),
|
||||
'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
|
||||
);
|
||||
}
|
||||
@ -217,6 +278,11 @@ class User extends Actor {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the endpoints.
|
||||
*
|
||||
* @return array|null The endpoints.
|
||||
*/
|
||||
public function get_endpoints() {
|
||||
$endpoints = null;
|
||||
|
||||
@ -235,74 +301,8 @@ class User extends Actor {
|
||||
* @return array The extended User-Output.
|
||||
*/
|
||||
public function get_attachment() {
|
||||
$extra_fields = get_actor_extra_fields( $this->_id );
|
||||
|
||||
$attachments = array();
|
||||
|
||||
foreach ( $extra_fields as $post ) {
|
||||
$content = \get_the_content( null, false, $post );
|
||||
$content = \make_clickable( $content );
|
||||
$content = \do_blocks( $content );
|
||||
$content = \wptexturize( $content );
|
||||
$content = \wp_filter_content_tags( $content );
|
||||
// replace script and style elements
|
||||
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
|
||||
$content = \strip_shortcodes( $content );
|
||||
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
|
||||
|
||||
$attachments[] = array(
|
||||
'type' => 'PropertyValue',
|
||||
'name' => \get_the_title( $post ),
|
||||
'value' => \html_entity_decode(
|
||||
$content,
|
||||
\ENT_QUOTES,
|
||||
'UTF-8'
|
||||
),
|
||||
);
|
||||
|
||||
$link_added = false;
|
||||
|
||||
// Add support for FEP-fb2a, for more information see FEDERATION.md
|
||||
if ( \class_exists( '\WP_HTML_Tag_Processor' ) ) {
|
||||
$tags = new \WP_HTML_Tag_Processor( $content );
|
||||
$tags->next_tag();
|
||||
|
||||
if ( 'P' === $tags->get_tag() ) {
|
||||
$tags->next_tag();
|
||||
}
|
||||
|
||||
if ( 'A' === $tags->get_tag() ) {
|
||||
$tags->set_bookmark( 'link' );
|
||||
if ( ! $tags->next_tag() ) {
|
||||
$tags->seek( 'link' );
|
||||
$attachment = array(
|
||||
'type' => 'Link',
|
||||
'name' => \get_the_title( $post ),
|
||||
'href' => \esc_url( $tags->get_attribute( 'href' ) ),
|
||||
'rel' => explode( ' ', $tags->get_attribute( 'rel' ) ),
|
||||
);
|
||||
|
||||
$link_added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $link_added ) {
|
||||
$attachment = array(
|
||||
'type' => 'Note',
|
||||
'name' => \get_the_title( $post ),
|
||||
'content' => \html_entity_decode(
|
||||
$content,
|
||||
\ENT_QUOTES,
|
||||
'UTF-8'
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
$attachments[] = $attachment;
|
||||
}
|
||||
|
||||
return $attachments;
|
||||
$extra_fields = Extra_Fields::get_actor_fields( $this->_id );
|
||||
return Extra_Fields::fields_to_attachments( $extra_fields );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -314,23 +314,93 @@ class User extends Actor {
|
||||
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the canonical URL.
|
||||
*
|
||||
* @return string The canonical URL.
|
||||
*/
|
||||
public function get_canonical_url() {
|
||||
return $this->get_url();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the streams.
|
||||
*
|
||||
* @return null The streams.
|
||||
*/
|
||||
public function get_streams() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tag.
|
||||
*
|
||||
* @return array The tag.
|
||||
*/
|
||||
public function get_tag() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the indexable state.
|
||||
*
|
||||
* @return bool Whether the user is indexable.
|
||||
*/
|
||||
public function get_indexable() {
|
||||
if ( \get_option( 'blog_public', 1 ) ) {
|
||||
if ( is_blog_public() ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user