modified file wp-piwik

This commit is contained in:
2026-06-03 21:29:22 +00:00
committed by Gitium
parent 57bccfdbd1
commit 769efed689
2556 changed files with 368982 additions and 126264 deletions

View File

@ -0,0 +1,255 @@
<?php
/**
* Application model file.
*
* @package Activitypub
*/
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;
/**
* Application class.
*
* @method int get__id() Gets the internal user ID for the application (always returns APPLICATION_USER_ID).
*/
class Application extends Actor {
/**
* The User-ID
*
* @var int
*/
protected $_id = Actors::APPLICATION_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* Whether the Application is discoverable.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
*
* @context http://joinmastodon.org/ns#discoverable
*
* @var boolean
*/
protected $discoverable = false;
/**
* Whether the Application is indexable.
*
* @context http://joinmastodon.org/ns#indexable
*
* @var boolean
*/
protected $indexable = false;
/**
* The WebFinger Resource.
*
* @var string
*/
protected $webfinger;
/**
* Returns the type of the object.
*
* @return string The type of the object.
*/
public function get_type() {
return 'Application';
}
/**
* Returns whether the Application manually approves followers.
*
* @return true Whether the Application manually approves followers.
*/
public function get_manually_approves_followers() {
return true;
}
/**
* Returns the ID of the Application.
*
* @return string The ID of the Application.
*/
public function get_id() {
return get_rest_url_by_path( 'application' );
}
/**
* Get the User-Url.
*
* @return string The User-Url.
*/
public function get_url() {
return $this->get_id();
}
/**
* Returns the User-URL with @-Prefix for the username.
*
* @return string The User-URL with @-Prefix for the username.
*/
public function get_alternate_url() {
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.
*
* @return string[] The User-Icon.
*/
public function get_icon() {
// Try site icon first.
$icon_id = get_option( 'site_icon' );
// Try custom logo second.
if ( ! $icon_id ) {
$icon_id = get_theme_mod( 'custom_logo' );
}
$icon_url = false;
if ( $icon_id ) {
$icon = wp_get_attachment_image_src( $icon_id, 'full' );
if ( $icon ) {
$icon_url = $icon[0];
}
}
if ( ! $icon_url ) {
// Fallback to default icon.
$icon_url = plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE );
}
return array(
'type' => 'Image',
'url' => esc_url( $icon_url ),
);
}
/**
* Get the User-Header-Image.
*
* @return string[]|null The User-Header-Image.
*/
public function get_header_image() {
if ( \has_header_image() ) {
return array(
'type' => 'Image',
'url' => esc_url( \get_header_image() ),
);
}
return null;
}
/**
* Get the first published date.
*
* @return string The published date.
*/
public function get_published() {
$first_post = new WP_Query(
array(
'orderby' => 'date',
'order' => 'ASC',
'number' => 1,
)
);
if ( ! empty( $first_post->posts[0] ) ) {
$time = \strtotime( $first_post->posts[0]->post_date_gmt );
} else {
$time = \time();
}
return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $time );
}
/**
* Returns the Inbox-API-Endpoint.
*
* @return string The Inbox-Endpoint.
*/
public function get_inbox() {
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
}
/**
* Returns the Outbox-API-Endpoint.
*
* @return string The Outbox-Endpoint.
*/
public function get_outbox() {
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
}
/**
* Returns a user@domain type of identifier for the user.
*
* @return string The Webfinger-Identifier.
*/
public function get_webfinger() {
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
}
/**
* Returns the public key.
*
* @return string[] The public key.
*/
public function get_public_key() {
return array(
'id' => $this->get_id() . '#main-key',
'owner' => $this->get_id(),
'publicKeyPem' => Signature::get_public_key_for( Actors::APPLICATION_USER_ID ),
);
}
/**
* Get the User description.
*
* @return string The User description.
*/
public function get_summary() {
return \wpautop(
\wp_kses(
\get_bloginfo( 'description' ),
'default'
)
);
}
/**
* Returns the canonical URL of the object.
*
* @return string|null The canonical URL of the object.
*/
public function get_canonical_url() {
return \home_url();
}
}

View File

@ -0,0 +1,600 @@
<?php
/**
* Blog model file.
*
* @package Activitypub
*/
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;
/**
* Blog class.
*
* @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
*
* @var int
*/
protected $_id = Actors::BLOG_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* If the User is indexable.
*
* @context http://joinmastodon.org/ns#indexable
*
* @var boolean
*/
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;
/**
* Constructor.
*/
public function __construct() {
/**
* Fires when a model actor is constructed.
*
* @param Blog $this The Blog model.
*/
\do_action( 'activitypub_construct_model_actor', $this );
}
/**
* Whether the User manually approves followers.
*
* @return false
*/
public function get_manually_approves_followers() {
return false;
}
/**
* Whether the User is discoverable.
*
* @return boolean
*/
public function get_discoverable() {
return true;
}
/**
* Get the User ID.
*
* @return string The User ID.
*/
public function get_id() {
$id = parent::get_id();
if ( $id ) {
return $id;
}
$permalink = \get_option( 'activitypub_use_permalink_as_id_for_blog', false );
if ( $permalink ) {
return $this->get_url();
}
return \add_query_arg( 'author', $this->_id, \trailingslashit( \home_url() ) );
}
/**
* Get the type of the object.
*
* 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 ( is_single_user() ) {
return 'Person';
} else {
return 'Group';
}
}
/**
* Get the Username.
*
* @return string The Username.
*/
public function get_name() {
return \wp_strip_all_tags(
\html_entity_decode(
\get_bloginfo( 'name' ),
\ENT_QUOTES,
'UTF-8'
)
);
}
/**
* Get the User description.
*
* @return string The User description.
*/
public function get_summary() {
$summary = \get_option( 'activitypub_blog_description', null );
if ( ! $summary ) {
$summary = \get_bloginfo( 'description' );
}
return \wpautop(
\wp_kses(
$summary,
'default'
)
);
}
/**
* Get the User url.
*
* @return string The User url.
*/
public function get_url() {
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
}
/**
* Get blog's homepage URL.
*
* @return string The User-Url.
*/
public function get_alternate_url() {
return \esc_url( \trailingslashit( get_home_url() ) );
}
/**
* Generate a default Username.
*
* @return string The auto-generated Username.
*/
public static function get_default_username() {
// Check if domain host has a subdomain.
$host = \wp_parse_url( \get_home_url(), \PHP_URL_HOST );
$host = \preg_replace( '/^www\./i', '', $host );
/**
* Filters the default blog username.
*
* This filter allows developers to modify the default username that is
* generated for the blog, which by default is the site's host name
* without the 'www.' prefix.
*
* @param string $host The default username (site's host name).
*/
return apply_filters( 'activitypub_default_blog_username', $host );
}
/**
* Get the preferred Username.
*
* @return string The Username.
*/
public function get_preferred_username() {
$username = \get_option( 'activitypub_blog_identifier' );
if ( $username ) {
return $username;
}
return self::get_default_username();
}
/**
* Get the User icon.
*
* @return string[] The User icon.
*/
public function get_icon() {
// Try site_logo, falling back to site_icon, first.
$icon_id = get_option( 'site_icon' );
// Try custom logo second.
if ( ! $icon_id ) {
$icon_id = get_theme_mod( 'custom_logo' );
}
$icon_url = false;
if ( $icon_id ) {
$icon = wp_get_attachment_image_src( $icon_id, 'full' );
if ( $icon ) {
$icon_url = $icon[0];
}
}
if ( ! $icon_url ) {
// Fallback to default icon.
$icon_url = plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE );
}
return array(
'type' => 'Image',
'url' => esc_url( $icon_url ),
);
}
/**
* Get the User-Header-Image.
*
* @return string[]|null The User-Header-Image.
*/
public function get_image() {
$header_image = get_option( 'activitypub_header_image' );
$image_url = null;
if ( $header_image ) {
$image_url = \wp_get_attachment_url( $header_image );
}
if ( ! $image_url && \has_header_image() ) {
$image_url = \get_header_image();
}
if ( $image_url ) {
return array(
'type' => 'Image',
'url' => esc_url( $image_url ),
);
}
return null;
}
/**
* Get the published date.
*
* @return string The published date.
*/
public function get_published() {
$first_post = new WP_Query(
array(
'orderby' => 'date',
'order' => 'ASC',
'number' => 1,
)
);
if ( ! empty( $first_post->posts[0] ) ) {
$time = \strtotime( $first_post->posts[0]->post_date_gmt );
} else {
$time = \time();
}
return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $time );
}
/**
* Get the canonical URL.
*
* @return string|null The canonical URL.
*/
public function get_canonical_url() {
return \home_url();
}
/**
* Get the Moderators endpoint.
*
* @return string|null The Moderators endpoint.
*/
public function get_moderators() {
if ( is_single_user() || 'Group' !== $this->get_type() ) {
return null;
}
return get_rest_url_by_path( 'collections/moderators' );
}
/**
* Get attributedTo value.
*
* @return string|null The attributedTo value.
*/
public function get_attributed_to() {
if ( is_single_user() || 'Group' !== $this->get_type() ) {
return null;
}
return get_rest_url_by_path( 'collections/moderators' );
}
/**
* Get the public key information.
*
* @return string[] The public key.
*/
public function get_public_key() {
return array(
'id' => $this->get_id() . '#main-key',
'owner' => $this->get_id(),
'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
);
}
/**
* Returns whether posting is restricted to mods.
*
* @return bool|null True if posting is restricted to mods, null if not applicable.
*/
public function get_posting_restricted_to_mods() {
if ( 'Group' === $this->get_type() ) {
return true;
}
return null;
}
/**
* Returns the Inbox-API-Endpoint.
*
* @return string The Inbox-Endpoint.
*/
public function get_inbox() {
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
}
/**
* Returns the Outbox-API-Endpoint.
*
* @return string The Outbox-Endpoint.
*/
public function get_outbox() {
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
}
/**
* Returns the Followers-API-Endpoint.
*
* @return string The Followers-Endpoint.
*/
public function get_followers() {
return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) );
}
/**
* Returns the Following-API-Endpoint.
*
* @return string The Following-Endpoint.
*/
public function get_following() {
return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) );
}
/**
* Returns 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;
}
/**
* Returns a user@domain type of identifier for the user.
*
* @return string The Webfinger-Identifier.
*/
public function get_webfinger() {
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
}
/**
* Returns the Featured-API-Endpoint.
*
* @return string The Featured-Endpoint.
*/
public function get_featured() {
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
}
/**
* Returns whether the site is indexable.
*
* @return bool Whether the site is indexable.
*/
public function get_indexable() {
if ( is_blog_public() ) {
return true;
} else {
return false;
}
}
/**
* Update the Username.
*
* @param mixed $value The new value.
* @return bool True if the attribute was updated, false otherwise.
*/
public function update_name( $value ) {
return \update_option( 'blogname', $value );
}
/**
* Update the User description.
*
* @param mixed $value The new value.
* @return bool True if the attribute was updated, false otherwise.
*/
public function update_summary( $value ) {
return \update_option( 'blogdescription', $value );
}
/**
* Update the User icon.
*
* @param mixed $value The new value.
* @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_option( 'site_icon', $value );
}
/**
* Update the User-Header-Image.
*
* @param mixed $value The new value.
* @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_option( 'activitypub_header_image', $value );
}
/**
* Get the User - Hashtags.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#Hashtag
*
* @return string[] The User - Hashtags.
*/
public function get_tag() {
$hashtags = array();
$args = array(
'orderby' => 'count',
'order' => 'DESC',
'number' => 10,
);
$tags = get_tags( $args );
foreach ( $tags as $tag ) {
$hashtags[] = array(
'type' => 'Hashtag',
'href' => \get_tag_link( $tag->term_id ),
'name' => esc_hashtag( $tag->name ),
);
}
return $hashtags;
}
/**
* Extend the User-Output with Attachments.
*
* @return array The extended User-Output.
*/
public function get_attachment() {
$extra_fields = Extra_Fields::get_actor_fields( $this->_id );
return Extra_Fields::fields_to_attachments( $extra_fields );
}
/**
* Returns the website hosts allowed to credit this blog.
*
* @return string[]|null The attribution domains or null if not found.
*/
public function get_attribution_domains() {
return get_attribution_domains();
}
/**
* Returns the alsoKnownAs.
*
* @return string[] The alsoKnownAs.
*/
public function get_also_known_as() {
$also_known_as = array(
\add_query_arg( 'author', $this->_id, \home_url( '/' ) ),
$this->get_url(),
$this->get_alternate_url(),
);
$also_known_as = array_merge( $also_known_as, \get_option( 'activitypub_blog_user_also_known_as', array() ) );
return array_unique( $also_known_as );
}
/**
* Returns the movedTo.
*
* @return string The movedTo.
*/
public function get_moved_to() {
$moved_to = \get_option( 'activitypub_blog_user_moved_to' );
return $moved_to && $moved_to !== $this->get_id() ? $moved_to : null;
}
}

View File

@ -0,0 +1,410 @@
<?php
/**
* Follower class file.
*
* @package Activitypub
*/
namespace Activitypub\Model;
use WP_Error;
use Activitypub\Activity\Actor;
use Activitypub\Collection\Followers;
/**
* ActivityPub Follower Class.
*
* This Object represents a single Follower.
* There is no direct reference to a WordPress User here.
*
* @author Matt Wiebe
* @author Matthias Pfefferle
*
* @see https://www.w3.org/TR/activitypub/#follow-activity-inbox
*
* @method int get__id() Gets the post ID of the follower record.
* @method string[]|null get_image() Gets the follower's profile image data.
* @method string|null get_inbox() Gets the follower's ActivityPub inbox URL.
* @method string[]|null get_endpoints() Gets the follower's ActivityPub endpoints.
*
* @method Follower set__id( int $id ) Sets the post ID of the follower record.
* @method Follower set_id( string $guid ) Sets the follower's GUID.
* @method Follower set_name( string $name ) Sets the follower's display name.
* @method Follower set_summary( string $summary ) Sets the follower's bio/summary.
* @method Follower set_published( string $datetime ) Sets the follower's published datetime in ISO 8601 format.
* @method Follower set_updated( string $datetime ) Sets the follower's last updated datetime in ISO 8601 format.
*/
class Follower extends Actor {
/**
* The complete Remote-Profile of the Follower.
*
* @var int
*/
protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* Get the errors.
*
* @return mixed
*/
public function get_errors() {
return get_post_meta( $this->_id, '_activitypub_errors', false );
}
/**
* Get the Summary.
*
* @return string The Summary.
*/
public function get_summary() {
if ( isset( $this->summary ) ) {
return $this->summary;
}
return '';
}
/**
* Getter for URL attribute.
*
* Falls back to ID, if no URL is set. This is relevant for
* Platforms like Lemmy, where the ID is the URL.
*
* @return string The URL.
*/
public function get_url() {
if ( $this->url ) {
return $this->url;
}
return $this->id;
}
/**
* Reset (delete) all errors.
*/
public function reset_errors() {
delete_post_meta( $this->_id, '_activitypub_errors' );
}
/**
* Count the errors.
*
* @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 the latest error message.
*
* @return string The error message.
*/
public function get_latest_error_message() {
$errors = $this->get_errors();
if ( is_array( $errors ) && ! empty( $errors ) ) {
return reset( $errors );
}
return '';
}
/**
* Update the current Follower object.
*/
public function update() {
$this->save();
}
/**
* Validate the current Follower object.
*
* @return boolean True if the verification was successful.
*/
public function is_valid() {
// The minimum required attributes.
$required_attributes = array(
'id',
'preferredUsername',
'inbox',
'publicKey',
'publicKeyPem',
);
foreach ( $required_attributes as $attribute ) {
if ( ! $this->get( $attribute ) ) {
return false;
}
}
return true;
}
/**
* Save the current Follower object.
*
* @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 ) );
}
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 );
}
}
$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;
}
/**
* Upsert the current Follower object.
*
* @return int|WP_Error The post ID or an WP_Error.
*/
public function upsert() {
return $this->save();
}
/**
* Delete the current Follower object.
*
* Beware that this os deleting a Follower for ALL users!!!
*
* To delete only the User connection (unfollow)
*
* @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;
}
/**
* Get the icon.
*
* Sets a fallback to better handle API and HTML outputs.
*
* @return string[] The icon.
*/
public function get_icon() {
if ( isset( $this->icon['url'] ) ) {
return $this->icon;
}
return array(
'type' => 'Image',
'mediaType' => 'image/jpeg',
'url' => ACTIVITYPUB_PLUGIN_URL . 'assets/img/mp.jpg',
);
}
/**
* Get Name.
*
* Tries to extract a name from the URL or ID if not set.
*
* @return string The name.
*/
public function get_name() {
if ( $this->name ) {
return $this->name;
} elseif ( $this->preferred_username ) {
return $this->preferred_username;
}
return $this->extract_name_from_uri();
}
/**
* The preferred Username.
*
* Tries to extract a name from the URL or ID if not set.
*
* @return string The preferred Username.
*/
public function get_preferred_username() {
if ( $this->preferred_username ) {
return $this->preferred_username;
}
return $this->extract_name_from_uri();
}
/**
* Get the Icon URL (Avatar).
*
* @return string The URL to the Avatar.
*/
public function get_icon_url() {
$icon = $this->get_icon();
if ( ! $icon ) {
return '';
}
if ( is_array( $icon ) ) {
return $icon['url'];
}
return $icon;
}
/**
* Get the Icon URL (Avatar).
*
* @return string The URL to the Avatar.
*/
public function get_image_url() {
$image = $this->get_image();
if ( ! $image ) {
return '';
}
if ( is_array( $image ) ) {
return $image['url'];
}
return $image;
}
/**
* Get the shared inbox, with a fallback to the inbox.
*
* @return string|null The URL to the shared inbox, the inbox or null.
*/
public function get_shared_inbox() {
if ( ! empty( $this->get_endpoints()['sharedInbox'] ) ) {
return $this->get_endpoints()['sharedInbox'];
} elseif ( ! empty( $this->get_inbox() ) ) {
return $this->get_inbox();
}
return null;
}
/**
* Convert a Custom-Post-Type input to an Activitypub\Model\Follower.
*
* @param \WP_Post $post The post object.
* @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 );
/* @var Follower $object Follower object. */
$object = self::init_from_json( $actor_json );
if ( is_wp_error( $object ) ) {
return false;
}
$object->set__id( $post->ID );
$object->set_id( $post->guid );
$object->set_name( $post->post_title );
$object->set_summary( $post->post_excerpt );
$object->set_published( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_date ) ) );
$object->set_updated( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) ) );
return $object;
}
/**
* Infer a shortname from the Actor ID or URL. Used only for fallbacks,
* we will try to use what's supplied.
*
* @return string Hopefully the name of the Follower.
*/
protected function extract_name_from_uri() {
// prefer the URL, but fall back to the ID.
if ( $this->url ) {
$name = $this->url;
} else {
$name = $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;
}
}

View File

@ -0,0 +1,474 @@
<?php
/**
* User model file.
*
* @package Activitypub
*/
namespace Activitypub\Model;
use Activitypub\Activity\Actor;
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\user_can_activitypub;
/**
* User class.
*
* @method int get__id() Gets the WordPress user ID.
*/
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
*
* @context {
* "@id": "http://joinmastodon.org/ns#featured",
* "@type": "@id"
* }
*
* @var string
*/
protected $featured;
/**
* Whether the User is discoverable.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
*
* @context http://joinmastodon.org/ns#discoverable
*
* @var boolean
*/
protected $discoverable = true;
/**
* Whether the User is indexable.
*
* @context http://joinmastodon.org/ns#indexable
*
* @var boolean
*/
protected $indexable;
/**
* The WebFinger Resource.
*
* @var string
*/
protected $webfinger;
/**
* Constructor.
*
* @param int $user_id Optional. The WordPress user ID. Default null.
*/
public function __construct( $user_id = null ) {
if ( $user_id ) {
$this->_id = $user_id;
/**
* Fires when a model actor is constructed.
*
* @param User $this The User object.
*/
\do_action( 'activitypub_construct_model_actor', $this );
}
}
/**
* 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 ( ! user_can_activitypub( $user_id ) ) {
return new \WP_Error(
'activitypub_user_not_found',
\__( 'User not found', 'activitypub' ),
array( 'status' => 404 )
);
}
return new static( $user_id );
}
/**
* Get the user ID.
*
* @return string The user ID.
*/
public function get_id() {
$id = parent::get_id();
if ( $id ) {
return $id;
}
$permalink = \get_user_option( 'activitypub_use_permalink_as_id', $this->_id );
if ( '1' === $permalink ) {
return $this->get_url();
}
return \add_query_arg( 'author', $this->_id, \trailingslashit( \home_url() ) );
}
/**
* Get the Username.
*
* @return string The Username.
*/
public function get_name() {
return \get_the_author_meta( 'display_name', $this->_id );
}
/**
* Get the User description.
*
* @return string The User description.
*/
public function get_summary() {
$description = get_user_option( 'activitypub_description', $this->_id );
if ( empty( $description ) ) {
$description = get_user_meta( $this->_id, 'description', true );
}
return \wpautop( \wp_kses( $description, 'default' ) );
}
/**
* Get 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.
*
* @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 \get_the_author_meta( 'login', $this->_id );
}
/**
* Get the User icon.
*
* @return string[] The User icon.
*/
public function get_icon() {
$icon = \get_user_option( 'activitypub_icon', $this->_id );
if ( false !== $icon && 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,
array( 'size' => 120 )
)
);
return array(
'type' => 'Image',
'url' => $icon,
);
}
/**
* Returns the header image.
*
* @return string[]|null The header image.
*/
public function get_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' => 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( ACTIVITYPUB_DATE_TIME_RFC3339, \strtotime( \get_the_author_meta( 'registered', $this->_id ) ) );
}
/**
* Returns the public key.
*
* @return string[] The public key.
*/
public function get_public_key() {
return array(
'id' => $this->get_id() . '#main-key',
'owner' => $this->get_id(),
'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
);
}
/**
* Returns the Inbox-API-Endpoint.
*
* @return string The Inbox-Endpoint.
*/
public function get_inbox() {
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
}
/**
* Returns the Outbox-API-Endpoint.
*
* @return string The Outbox-Endpoint.
*/
public function get_outbox() {
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
}
/**
* Returns the Followers-API-Endpoint.
*
* @return string The Followers-Endpoint.
*/
public function get_followers() {
return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) );
}
/**
* Returns the Following-API-Endpoint.
*
* @return string The Following-Endpoint.
*/
public function get_following() {
return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) );
}
/**
* Returns the Featured-API-Endpoint.
*
* @return string The Featured-Endpoint.
*/
public function get_featured() {
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $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;
}
/**
* Extend the User-Output with Attachments.
*
* @return array The extended User-Output.
*/
public function get_attachment() {
$extra_fields = Extra_Fields::get_actor_fields( $this->_id );
return Extra_Fields::fields_to_attachments( $extra_fields );
}
/**
* Returns a user@domain type of identifier for the user.
*
* @return string The Webfinger-Identifier.
*/
public function get_webfinger() {
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 ( 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 );
}
/**
* Returns the website hosts allowed to credit this blog.
*
* @return string[]|null The attribution domains or null if not found.
*/
public function get_attribution_domains() {
return get_attribution_domains();
}
/**
* Returns the alsoKnownAs.
*
* @return string[] The alsoKnownAs.
*/
public function get_also_known_as() {
$also_known_as = array(
\add_query_arg( 'author', $this->_id, \home_url( '/' ) ),
$this->get_url(),
$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 );
}
/**
* Returns the movedTo.
*
* @return string The movedTo.
*/
public function get_moved_to() {
$moved_to = \get_user_option( 'activitypub_moved_to', $this->_id );
return $moved_to && $moved_to !== $this->get_id() ? $moved_to : null;
}
}