modified file smtp-mailer
This commit is contained in:
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
namespace Activitypub\Model;
|
||||
|
||||
use WP_Query;
|
||||
use Activitypub\Signature;
|
||||
use Activitypub\Collection\Users;
|
||||
|
||||
use function Activitypub\get_rest_url_by_path;
|
||||
|
||||
class Application_User extends Blog_User {
|
||||
/**
|
||||
* The User-ID
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_id = Users::APPLICATION_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
||||
|
||||
/**
|
||||
* The User-Type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'Application';
|
||||
|
||||
/**
|
||||
* If the User is discoverable.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $discoverable = false;
|
||||
|
||||
/**
|
||||
* Get the User-Url.
|
||||
*
|
||||
* @return string The User-Url.
|
||||
*/
|
||||
public function get_url() {
|
||||
return get_rest_url_by_path( 'application' );
|
||||
}
|
||||
|
||||
public function get_name() {
|
||||
return 'application';
|
||||
}
|
||||
|
||||
public function get_preferred_username() {
|
||||
return $this::get_name();
|
||||
}
|
||||
|
||||
public function get_followers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_following() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_attachment() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_featured() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_moderators() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_indexable() {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,243 @@
|
||||
<?php
|
||||
namespace Activitypub\Model;
|
||||
|
||||
use WP_Query;
|
||||
use Activitypub\Signature;
|
||||
use Activitypub\Collection\Users;
|
||||
|
||||
use function Activitypub\is_single_user;
|
||||
use function Activitypub\is_user_disabled;
|
||||
use function Activitypub\get_rest_url_by_path;
|
||||
|
||||
class Blog_User extends User {
|
||||
/**
|
||||
* The User-ID
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_id = Users::BLOG_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
||||
|
||||
/**
|
||||
* The User-Type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = null;
|
||||
|
||||
/**
|
||||
* Is Account discoverable?
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $discoverable = true;
|
||||
|
||||
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 )
|
||||
);
|
||||
}
|
||||
|
||||
$object = new static();
|
||||
$object->_id = $user_id;
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type of the object.
|
||||
*
|
||||
* If the Blog is in "single user" mode, return "Person" insted of "Group".
|
||||
*
|
||||
* @return string The type of the object.
|
||||
*/
|
||||
public function get_type() {
|
||||
if ( is_single_user() ) {
|
||||
return 'Person';
|
||||
} else {
|
||||
return 'Group';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User-Name.
|
||||
*
|
||||
* @return string The User-Name.
|
||||
*/
|
||||
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() {
|
||||
return \wpautop(
|
||||
\wp_kses(
|
||||
\get_bloginfo( 'description' ),
|
||||
'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() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the User-URL with @-Prefix for the username.
|
||||
*
|
||||
* @return string The User-URL with @-Prefix for the username.
|
||||
*/
|
||||
public function get_at_url() {
|
||||
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
|
||||
/**
|
||||
* Filter the default blog username.
|
||||
*
|
||||
* @param string $host The default username.
|
||||
*/
|
||||
return apply_filters( 'activitypub_default_blog_username', $host );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the preferred User-Name.
|
||||
*
|
||||
* @return string The User-Name.
|
||||
*/
|
||||
public function get_preferred_username() {
|
||||
$username = \get_option( 'activitypub_blog_user_identifier' );
|
||||
|
||||
if ( $username ) {
|
||||
return $username;
|
||||
}
|
||||
|
||||
return self::get_default_username();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User-Icon.
|
||||
*
|
||||
* @return array 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 array|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;
|
||||
}
|
||||
|
||||
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( 'Y-m-d\TH:i:s\Z', $time );
|
||||
}
|
||||
|
||||
public function get_attachment() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function get_canonical_url() {
|
||||
return \home_url();
|
||||
}
|
||||
|
||||
public function get_moderators() {
|
||||
if ( is_single_user() || 'Group' !== $this->get_type() ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return get_rest_url_by_path( 'collections/moderators' );
|
||||
}
|
||||
|
||||
public function get_attributed_to() {
|
||||
if ( is_single_user() || 'Group' !== $this->get_type() ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return get_rest_url_by_path( 'collections/moderators' );
|
||||
}
|
||||
|
||||
public function get_posting_restricted_to_mods() {
|
||||
if ( 'Group' === $this->get_type() ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,366 @@
|
||||
<?php
|
||||
namespace Activitypub\Model;
|
||||
|
||||
use WP_Error;
|
||||
use WP_Query;
|
||||
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
|
||||
*/
|
||||
class Follower extends Actor {
|
||||
/**
|
||||
* The complete Remote-Profile of the Follower
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
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' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Summary.
|
||||
*
|
||||
* @return int 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
|
||||
* Plattforms 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.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
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.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$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 );
|
||||
}
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'ID' => $this->get__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(),
|
||||
);
|
||||
|
||||
$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()
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete() {
|
||||
wp_delete_post( $this->_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the post meta.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function get_post_meta_input() {
|
||||
$meta_input = array();
|
||||
$meta_input['activitypub_inbox'] = $this->get_shared_inbox();
|
||||
$meta_input['activitypub_actor_json'] = $this->to_json();
|
||||
|
||||
return $meta_input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the icon.
|
||||
*
|
||||
* Sets a fallback to better handle API and HTML outputs.
|
||||
*
|
||||
* @return array 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 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.
|
||||
*
|
||||
* @return string The JSON string.
|
||||
*
|
||||
* @return array Activitypub\Model\Follower
|
||||
*/
|
||||
public static function init_from_cpt( $post ) {
|
||||
$actor_json = get_post_meta( $post->ID, 'activitypub_actor_json', true );
|
||||
$object = self::init_from_json( $actor_json );
|
||||
$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;
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
<?php
|
||||
namespace Activitypub\Model;
|
||||
|
||||
use Activitypub\Transformer\Post as Post_Transformer;
|
||||
|
||||
/**
|
||||
* ActivityPub Post Class
|
||||
*
|
||||
* @author Matthias Pfefferle
|
||||
*/
|
||||
class Post {
|
||||
/**
|
||||
* The \Activitypub\Activity\Base_Object object.
|
||||
*
|
||||
* @var \Activitypub\Activity\Base_Object
|
||||
*/
|
||||
protected $object;
|
||||
|
||||
/**
|
||||
* The WordPress Post Object.
|
||||
*
|
||||
* @var WP_Post
|
||||
*/
|
||||
private $post;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param WP_Post $post
|
||||
* @param int $post_author
|
||||
*/
|
||||
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
|
||||
public function __construct( $post, $post_author = null ) {
|
||||
_deprecated_function( __CLASS__, '1.0.0', '\Activitypub\Transformer\Post' );
|
||||
|
||||
$this->post = $post;
|
||||
$this->object = Post_Transformer::transform( $post )->to_object();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the User ID.
|
||||
*
|
||||
* @return int the User ID.
|
||||
*/
|
||||
public function get_user_id() {
|
||||
return apply_filters( 'activitypub_post_user_id', $this->post->post_author, $this->post );
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this Object into an Array.
|
||||
*
|
||||
* @return array the array representation of a Post.
|
||||
*/
|
||||
public function to_array() {
|
||||
return \apply_filters( 'activitypub_post', $this->object->to_array(), $this->post );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Actor of this Object.
|
||||
*
|
||||
* @return string The URL of the Actor.
|
||||
*/
|
||||
public function get_actor() {
|
||||
$user = User_Factory::get_by_id( $this->get_user_id() );
|
||||
|
||||
return $user->get_url();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this Object into a JSON String
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function to_json() {
|
||||
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL of an Activity Object
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_url() {
|
||||
return $this->object->get_url();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of an Activity Object
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_id() {
|
||||
return $this->object->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of Image Attachments
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_attachments() {
|
||||
return $this->object->get_attachment();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of Tags, used in the Post
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_tags() {
|
||||
return $this->object->get_tag();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the as2 object-type for a given post
|
||||
*
|
||||
* @return string the object-type
|
||||
*/
|
||||
public function get_object_type() {
|
||||
return $this->object->get_type();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content for the ActivityPub Item.
|
||||
*
|
||||
* @return string the content
|
||||
*/
|
||||
public function get_content() {
|
||||
return $this->object->get_content();
|
||||
}
|
||||
}
|
@ -0,0 +1,300 @@
|
||||
<?php
|
||||
namespace Activitypub\Model;
|
||||
|
||||
use WP_Query;
|
||||
use WP_Error;
|
||||
use Activitypub\Signature;
|
||||
use Activitypub\Collection\Users;
|
||||
use Activitypub\Activity\Actor;
|
||||
|
||||
use function Activitypub\is_user_disabled;
|
||||
use function Activitypub\get_rest_url_by_path;
|
||||
|
||||
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
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $featured;
|
||||
|
||||
/**
|
||||
* Moderators endpoint.
|
||||
*
|
||||
* @see https://join-lemmy.org/docs/contributors/05-federation.html
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $moderators;
|
||||
|
||||
/**
|
||||
* The User-Type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'Person';
|
||||
|
||||
/**
|
||||
* If the User is discoverable.
|
||||
*
|
||||
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $discoverable = true;
|
||||
|
||||
/**
|
||||
* If the User is indexable.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $indexable;
|
||||
|
||||
/**
|
||||
* The WebFinger Resource.
|
||||
*
|
||||
* @var string<url>
|
||||
*/
|
||||
protected $resource;
|
||||
|
||||
/**
|
||||
* Restrict posting to mods
|
||||
*
|
||||
* @see https://join-lemmy.org/docs/contributors/05-federation.html
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $posting_restricted_to_mods = null;
|
||||
|
||||
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 )
|
||||
);
|
||||
}
|
||||
|
||||
$object = new static();
|
||||
$object->_id = $user_id;
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User-ID.
|
||||
*
|
||||
* @return string The User-ID.
|
||||
*/
|
||||
public function get_id() {
|
||||
return $this->get_url();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User-Name.
|
||||
*
|
||||
* @return string The User-Name.
|
||||
*/
|
||||
public function get_name() {
|
||||
return \esc_attr( \get_the_author_meta( 'display_name', $this->_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User-Description.
|
||||
*
|
||||
* @return string The User-Description.
|
||||
*/
|
||||
public function get_summary() {
|
||||
$description = get_user_meta( $this->_id, 'activitypub_user_description', true );
|
||||
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_at_url() {
|
||||
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_username() );
|
||||
}
|
||||
|
||||
public function get_preferred_username() {
|
||||
return \esc_attr( \get_the_author_meta( 'login', $this->_id ) );
|
||||
}
|
||||
|
||||
public function get_icon() {
|
||||
$icon = \esc_url(
|
||||
\get_avatar_url(
|
||||
$this->_id,
|
||||
array( 'size' => 120 )
|
||||
)
|
||||
);
|
||||
|
||||
return array(
|
||||
'type' => 'Image',
|
||||
'url' => $icon,
|
||||
);
|
||||
}
|
||||
|
||||
public function get_image() {
|
||||
if ( \has_header_image() ) {
|
||||
$image = \esc_url( \get_header_image() );
|
||||
return array(
|
||||
'type' => 'Image',
|
||||
'url' => $image,
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_published() {
|
||||
return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( \get_the_author_meta( 'registered', $this->_id ) ) );
|
||||
}
|
||||
|
||||
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( 'users/%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( 'users/%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( 'users/%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( 'users/%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( 'users/%d/collections/featured', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the User-Output with Attachments.
|
||||
*
|
||||
* @return array The extended User-Output.
|
||||
*/
|
||||
public function get_attachment() {
|
||||
$array = array();
|
||||
|
||||
$array[] = array(
|
||||
'type' => 'PropertyValue',
|
||||
'name' => \__( 'Blog', 'activitypub' ),
|
||||
'value' => \html_entity_decode(
|
||||
'<a rel="me" title="' . \esc_attr( \home_url( '/' ) ) . '" target="_blank" href="' . \home_url( '/' ) . '">' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '</a>',
|
||||
\ENT_QUOTES,
|
||||
'UTF-8'
|
||||
),
|
||||
);
|
||||
|
||||
$array[] = array(
|
||||
'type' => 'PropertyValue',
|
||||
'name' => \__( 'Profile', 'activitypub' ),
|
||||
'value' => \html_entity_decode(
|
||||
'<a rel="me" title="' . \esc_attr( \get_author_posts_url( $this->get__id() ) ) . '" target="_blank" href="' . \get_author_posts_url( $this->get__id() ) . '">' . \wp_parse_url( \get_author_posts_url( $this->get__id() ), \PHP_URL_HOST ) . '</a>',
|
||||
\ENT_QUOTES,
|
||||
'UTF-8'
|
||||
),
|
||||
);
|
||||
|
||||
if ( \get_the_author_meta( 'user_url', $this->get__id() ) ) {
|
||||
$array[] = array(
|
||||
'type' => 'PropertyValue',
|
||||
'name' => \__( 'Website', 'activitypub' ),
|
||||
'value' => \html_entity_decode(
|
||||
'<a rel="me" title="' . \esc_attr( \get_the_author_meta( 'user_url', $this->get__id() ) ) . '" target="_blank" href="' . \get_the_author_meta( 'user_url', $this->get__id() ) . '">' . \wp_parse_url( \get_the_author_meta( 'user_url', $this->get__id() ), \PHP_URL_HOST ) . '</a>',
|
||||
\ENT_QUOTES,
|
||||
'UTF-8'
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a user@domain type of identifier for the user.
|
||||
*
|
||||
* @return string The Webfinger-Identifier.
|
||||
*/
|
||||
public function get_resource() {
|
||||
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
|
||||
}
|
||||
|
||||
public function get_canonical_url() {
|
||||
return $this->get_url();
|
||||
}
|
||||
|
||||
public function get_streams() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_tag() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function get_indexable() {
|
||||
if ( \get_option( 'blog_public', 1 ) ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user