updated plugin ActivityPub version 9.1.0
This commit is contained in:
@ -2,13 +2,16 @@
|
||||
/**
|
||||
* Application model file.
|
||||
*
|
||||
* @deprecated Use {@see \Activitypub\Application} for key management
|
||||
* and {@see \Activitypub\Rest\Application_Controller} for the REST endpoint.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Model;
|
||||
|
||||
use Activitypub\Activity\Actor;
|
||||
use Activitypub\Collection\Actors;
|
||||
use Activitypub\Application as Application_Utility;
|
||||
|
||||
use function Activitypub\get_rest_url_by_path;
|
||||
use function Activitypub\home_host;
|
||||
@ -16,7 +19,7 @@ use function Activitypub\home_host;
|
||||
/**
|
||||
* Application class.
|
||||
*
|
||||
* @method int get__id() Gets the internal user ID for the application (always returns APPLICATION_USER_ID).
|
||||
* @deprecated Use {@see \Activitypub\Application} instead.
|
||||
*/
|
||||
class Application extends Actor {
|
||||
/**
|
||||
@ -24,7 +27,7 @@ class Application extends Actor {
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_id = Actors::APPLICATION_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
||||
protected $_id = -1; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
||||
|
||||
/**
|
||||
* Whether the Application is discoverable.
|
||||
@ -101,13 +104,20 @@ class Application extends Actor {
|
||||
*/
|
||||
protected $preferred_username = 'application';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
\_deprecated_class( __CLASS__, '9.1.0', 'Activitypub\Application' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of the Application.
|
||||
*
|
||||
* @return string The ID of the Application.
|
||||
*/
|
||||
public function get_id() {
|
||||
return get_rest_url_by_path( 'application' );
|
||||
return Application_Utility::get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,32 +144,7 @@ class Application extends Actor {
|
||||
* @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 ),
|
||||
);
|
||||
return Application_Utility::get_icon();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -171,7 +156,7 @@ class Application extends Actor {
|
||||
if ( \has_header_image() ) {
|
||||
return array(
|
||||
'type' => 'Image',
|
||||
'url' => esc_url( \get_header_image() ),
|
||||
'url' => \esc_url_raw( \get_header_image() ),
|
||||
);
|
||||
}
|
||||
|
||||
@ -184,21 +169,7 @@ class Application extends Actor {
|
||||
* @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 );
|
||||
return Application_Utility::get_published();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -207,7 +178,7 @@ class Application extends Actor {
|
||||
* @return string The Inbox-Endpoint.
|
||||
*/
|
||||
public function get_inbox() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
|
||||
return get_rest_url_by_path( 'inbox' );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -216,7 +187,7 @@ class Application extends Actor {
|
||||
* @return string The Outbox-Endpoint.
|
||||
*/
|
||||
public function get_outbox() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
|
||||
return get_rest_url_by_path( 'application/outbox' );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -235,9 +206,9 @@ class Application extends Actor {
|
||||
*/
|
||||
public function get_public_key() {
|
||||
return array(
|
||||
'id' => $this->get_id() . '#main-key',
|
||||
'id' => Application_Utility::get_key_id(),
|
||||
'owner' => $this->get_id(),
|
||||
'publicKeyPem' => Actors::get_public_key( Actors::APPLICATION_USER_ID ),
|
||||
'publicKeyPem' => Application_Utility::get_public_key(),
|
||||
);
|
||||
}
|
||||
|
||||
@ -247,9 +218,9 @@ class Application extends Actor {
|
||||
* @return string The User description.
|
||||
*/
|
||||
public function get_summary() {
|
||||
return sprintf(
|
||||
return \sprintf(
|
||||
/* translators: %s: Domain of the site */
|
||||
__( 'This is the Application Actor for %s.', 'activitypub' ),
|
||||
\__( 'This is the Application Actor for %s.', 'activitypub' ),
|
||||
home_host()
|
||||
);
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@ 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;
|
||||
use function Activitypub\site_icon;
|
||||
|
||||
/**
|
||||
* Blog class.
|
||||
@ -93,7 +94,7 @@ class Blog extends Actor {
|
||||
$permalink = \get_option( 'activitypub_use_permalink_as_id_for_blog', false );
|
||||
|
||||
if ( $permalink ) {
|
||||
return \esc_url( \home_url( '/@' . $this->get_preferred_username() ) );
|
||||
return \esc_url_raw( \home_url( '/@' . $this->get_preferred_username() ) );
|
||||
}
|
||||
|
||||
return \add_query_arg( 'author', $this->_id, \home_url( '/' ) );
|
||||
@ -169,7 +170,7 @@ class Blog extends Actor {
|
||||
* @return string The User-Url.
|
||||
*/
|
||||
public function get_alternate_url() {
|
||||
return \esc_url( \trailingslashit( get_home_url() ) );
|
||||
return \esc_url_raw( \trailingslashit( \get_home_url() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -191,7 +192,7 @@ class Blog extends Actor {
|
||||
*
|
||||
* @param string $host The default username (site's host name).
|
||||
*/
|
||||
return apply_filters( 'activitypub_default_blog_username', $host );
|
||||
return \apply_filters( 'activitypub_default_blog_username', $host );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -215,32 +216,7 @@ class Blog extends Actor {
|
||||
* @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 ),
|
||||
);
|
||||
return site_icon();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -249,7 +225,7 @@ class Blog extends Actor {
|
||||
* @return string[]|null The User-Header-Image.
|
||||
*/
|
||||
public function get_image() {
|
||||
$header_image = get_option( 'activitypub_header_image' );
|
||||
$header_image = \get_option( 'activitypub_header_image' );
|
||||
$image_url = null;
|
||||
|
||||
if ( $header_image ) {
|
||||
@ -263,7 +239,7 @@ class Blog extends Actor {
|
||||
if ( $image_url ) {
|
||||
return array(
|
||||
'type' => 'Image',
|
||||
'url' => esc_url( $image_url ),
|
||||
'url' => \esc_url_raw( $image_url ),
|
||||
);
|
||||
}
|
||||
|
||||
@ -383,7 +359,7 @@ class Blog extends Actor {
|
||||
* @return string The Inbox-Endpoint.
|
||||
*/
|
||||
public function get_inbox() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/inbox', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -392,7 +368,7 @@ class Blog extends Actor {
|
||||
* @return string The Outbox-Endpoint.
|
||||
*/
|
||||
public function get_outbox() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/outbox', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -401,7 +377,7 @@ class Blog extends Actor {
|
||||
* @return string The Followers-Endpoint.
|
||||
*/
|
||||
public function get_followers() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/followers', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -410,7 +386,7 @@ class Blog extends Actor {
|
||||
* @return string The Following-Endpoint.
|
||||
*/
|
||||
public function get_following() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/following', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -419,7 +395,7 @@ class Blog extends Actor {
|
||||
* @return string[]|null The endpoints.
|
||||
*/
|
||||
public function get_endpoints() {
|
||||
return array(
|
||||
$endpoints = array(
|
||||
'sharedInbox' => get_rest_url_by_path( 'inbox' ),
|
||||
'oauthAuthorizationEndpoint' => get_rest_url_by_path( 'oauth/authorize' ),
|
||||
'oauthTokenEndpoint' => get_rest_url_by_path( 'oauth/token' ),
|
||||
@ -427,6 +403,16 @@ class Blog extends Actor {
|
||||
'proxyUrl' => get_rest_url_by_path( 'proxy' ),
|
||||
'proxyEventStream' => get_rest_url_by_path( 'proxy/stream' ),
|
||||
);
|
||||
|
||||
if ( \get_option( 'activitypub_api', false ) ) {
|
||||
/*
|
||||
* RFC 6570 template. add_query_arg() picks the ?/& separator (plain permalinks already
|
||||
* carry a query string) and does not encode values, so the {q} placeholder stays intact.
|
||||
*/
|
||||
$endpoints['actorAutocomplete'] = \add_query_arg( 'q', '{q}', get_rest_url_by_path( 'actors/autocomplete' ) );
|
||||
}
|
||||
|
||||
return $endpoints;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -446,7 +432,7 @@ class Blog extends Actor {
|
||||
* @return string The Liked endpoint.
|
||||
*/
|
||||
public function get_liked() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/liked', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/liked', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -455,7 +441,7 @@ class Blog extends Actor {
|
||||
* @return string The Featured-Endpoint.
|
||||
*/
|
||||
public function get_featured() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -464,7 +450,7 @@ class Blog extends Actor {
|
||||
* @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() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/collections/tags', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -507,7 +493,7 @@ class Blog extends Actor {
|
||||
* @return bool True if the attribute was updated, false otherwise.
|
||||
*/
|
||||
public function update_icon( $value ) {
|
||||
if ( ! wp_attachment_is_image( $value ) ) {
|
||||
if ( ! \wp_attachment_is_image( $value ) ) {
|
||||
return false;
|
||||
}
|
||||
return \update_option( 'site_icon', $value );
|
||||
@ -520,7 +506,7 @@ class Blog extends Actor {
|
||||
* @return bool True if the attribute was updated, false otherwise.
|
||||
*/
|
||||
public function update_header( $value ) {
|
||||
if ( ! wp_attachment_is_image( $value ) ) {
|
||||
if ( ! \wp_attachment_is_image( $value ) ) {
|
||||
return false;
|
||||
}
|
||||
return \update_option( 'activitypub_header_image', $value );
|
||||
@ -542,7 +528,7 @@ class Blog extends Actor {
|
||||
'number' => 10,
|
||||
);
|
||||
|
||||
$tags = get_tags( $args );
|
||||
$tags = \get_tags( $args );
|
||||
|
||||
foreach ( $tags as $tag ) {
|
||||
$hashtags[] = array(
|
||||
@ -586,9 +572,9 @@ class Blog extends Actor {
|
||||
$this->get_alternate_url(),
|
||||
);
|
||||
|
||||
$also_known_as = array_merge( $also_known_as, \get_option( 'activitypub_blog_user_also_known_as', array() ) );
|
||||
$also_known_as = \array_merge( $also_known_as, \get_option( 'activitypub_blog_user_also_known_as', array() ) );
|
||||
|
||||
return array_unique( $also_known_as );
|
||||
return \array_unique( $also_known_as );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -601,4 +587,50 @@ class Blog extends Actor {
|
||||
|
||||
return $moved_to && $moved_to !== $this->get_id() ? $moved_to : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actor-level interaction policy.
|
||||
*
|
||||
* Overrides the magic property accessor on Base_Object so that we always
|
||||
* compute the policy from the current site setting rather than returning a
|
||||
* cached property value. Currently only emits `canFeature` (FEP-7aa9).
|
||||
* Driven by the site option `activitypub_default_feature_policy` and
|
||||
* defaults to denying all featured-collection requests, in line with
|
||||
* FEP-7aa9's "absence of policy = no consent" rule.
|
||||
*
|
||||
* @see https://w3id.org/fep/7aa9
|
||||
*
|
||||
* @since 9.0.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_interaction_policy() {
|
||||
$policy = array( 'canFeature' => $this->build_can_feature_policy() );
|
||||
|
||||
// Merge with an explicitly set interaction policy, if any.
|
||||
if ( $this->interaction_policy ) {
|
||||
$policy = \array_merge( (array) $this->interaction_policy, $policy );
|
||||
}
|
||||
|
||||
return $policy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the `canFeature` policy array from the site option.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function build_can_feature_policy() {
|
||||
$policy = \get_option( 'activitypub_default_feature_policy', ACTIVITYPUB_INTERACTION_POLICY_ME );
|
||||
|
||||
switch ( $policy ) {
|
||||
case ACTIVITYPUB_INTERACTION_POLICY_ANYONE:
|
||||
return array( 'automaticApproval' => array( 'https://www.w3.org/ns/activitystreams#Public' ) );
|
||||
case ACTIVITYPUB_INTERACTION_POLICY_FOLLOWERS:
|
||||
return array( 'automaticApproval' => array( $this->get_followers() ) );
|
||||
case ACTIVITYPUB_INTERACTION_POLICY_ME:
|
||||
default:
|
||||
return array( 'automaticApproval' => array( $this->get_id() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,359 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Follower class file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Model;
|
||||
|
||||
use Activitypub\Activity\Actor;
|
||||
use Activitypub\Collection\Followers;
|
||||
use Activitypub\Collection\Remote_Actors;
|
||||
|
||||
use function Activitypub\extract_name_from_uri;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @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.
|
||||
* @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
|
||||
|
||||
/**
|
||||
* 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 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 );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return bool True on success, false on failure.
|
||||
*/
|
||||
public function reset_errors() {
|
||||
return Remote_Actors::clear_errors( $this->_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the errors.
|
||||
*
|
||||
* @return int The number of errors.
|
||||
*/
|
||||
public function count_errors() {
|
||||
return Remote_Actors::count_errors( $this->_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ) );
|
||||
}
|
||||
|
||||
$id = Remote_Actors::upsert( $this );
|
||||
if ( \is_wp_error( $id ) ) {
|
||||
return $id;
|
||||
}
|
||||
|
||||
$this->set__id( $id );
|
||||
return $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() {
|
||||
Followers::remove_follower( $this->_id, $this->get_id() );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ) {
|
||||
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( $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 ) {
|
||||
$uri = $this->url;
|
||||
} else {
|
||||
$uri = $this->id;
|
||||
}
|
||||
|
||||
return extract_name_from_uri( $uri );
|
||||
}
|
||||
}
|
||||
@ -140,9 +140,9 @@ class User extends Actor {
|
||||
* @return string The User description.
|
||||
*/
|
||||
public function get_summary() {
|
||||
$description = get_user_option( 'activitypub_description', $this->_id );
|
||||
$description = \get_user_option( 'activitypub_description', $this->_id );
|
||||
if ( empty( $description ) ) {
|
||||
$description = get_user_meta( $this->_id, 'description', true );
|
||||
$description = \get_user_meta( $this->_id, 'description', true );
|
||||
}
|
||||
return \wpautop( \wp_kses( $description, 'default' ) );
|
||||
}
|
||||
@ -153,7 +153,7 @@ class User extends Actor {
|
||||
* @return string The User url.
|
||||
*/
|
||||
public function get_url() {
|
||||
return \esc_url( \get_author_posts_url( $this->_id ) );
|
||||
return \esc_url_raw( \get_author_posts_url( $this->_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -162,7 +162,7 @@ class User extends Actor {
|
||||
* @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() );
|
||||
return \esc_url_raw( \trailingslashit( \get_home_url() ) . '@' . $this->get_preferred_username() );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -188,14 +188,14 @@ class User extends Actor {
|
||||
*/
|
||||
public function get_icon() {
|
||||
$icon = \get_user_option( 'activitypub_icon', $this->_id );
|
||||
if ( false !== $icon && wp_attachment_is_image( $icon ) ) {
|
||||
if ( false !== $icon && \wp_attachment_is_image( $icon ) ) {
|
||||
return array(
|
||||
'type' => 'Image',
|
||||
'url' => esc_url( wp_get_attachment_url( $icon ) ),
|
||||
'url' => \esc_url_raw( \wp_get_attachment_url( $icon ) ),
|
||||
);
|
||||
}
|
||||
|
||||
$icon = \esc_url(
|
||||
$icon = \esc_url_raw(
|
||||
\get_avatar_url(
|
||||
$this->_id,
|
||||
array( 'size' => 120 )
|
||||
@ -214,7 +214,7 @@ class User extends Actor {
|
||||
* @return string[]|null The header image.
|
||||
*/
|
||||
public function get_image() {
|
||||
$header_image = get_user_option( 'activitypub_header_image', $this->_id );
|
||||
$header_image = \get_user_option( 'activitypub_header_image', $this->_id );
|
||||
$image_url = null;
|
||||
|
||||
if ( ! $header_image && \has_header_image() ) {
|
||||
@ -228,7 +228,7 @@ class User extends Actor {
|
||||
if ( $image_url ) {
|
||||
return array(
|
||||
'type' => 'Image',
|
||||
'url' => esc_url( $image_url ),
|
||||
'url' => \esc_url_raw( $image_url ),
|
||||
);
|
||||
}
|
||||
|
||||
@ -263,7 +263,7 @@ class User extends Actor {
|
||||
* @return string The Inbox-Endpoint.
|
||||
*/
|
||||
public function get_inbox() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/inbox', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -272,7 +272,7 @@ class User extends Actor {
|
||||
* @return string The Outbox-Endpoint.
|
||||
*/
|
||||
public function get_outbox() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/outbox', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -281,7 +281,7 @@ class User extends Actor {
|
||||
* @return string The Followers-Endpoint.
|
||||
*/
|
||||
public function get_followers() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/followers', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -290,7 +290,7 @@ class User extends Actor {
|
||||
* @return string The Following-Endpoint.
|
||||
*/
|
||||
public function get_following() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/following', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -301,7 +301,7 @@ class User extends Actor {
|
||||
* @return string The Liked endpoint.
|
||||
*/
|
||||
public function get_liked() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/liked', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/liked', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -310,7 +310,7 @@ class User extends Actor {
|
||||
* @return string The Featured-Endpoint.
|
||||
*/
|
||||
public function get_featured() {
|
||||
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -319,7 +319,7 @@ class User extends Actor {
|
||||
* @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() ) );
|
||||
return get_rest_url_by_path( \sprintf( 'actors/%d/collections/tags', $this->get__id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -328,7 +328,7 @@ class User extends Actor {
|
||||
* @return string[]|null The endpoints.
|
||||
*/
|
||||
public function get_endpoints() {
|
||||
return array(
|
||||
$endpoints = array(
|
||||
'sharedInbox' => get_rest_url_by_path( 'inbox' ),
|
||||
'oauthAuthorizationEndpoint' => get_rest_url_by_path( 'oauth/authorize' ),
|
||||
'oauthTokenEndpoint' => get_rest_url_by_path( 'oauth/token' ),
|
||||
@ -336,6 +336,16 @@ class User extends Actor {
|
||||
'proxyUrl' => get_rest_url_by_path( 'proxy' ),
|
||||
'proxyEventStream' => get_rest_url_by_path( 'proxy/stream' ),
|
||||
);
|
||||
|
||||
if ( \get_option( 'activitypub_api', false ) ) {
|
||||
/*
|
||||
* RFC 6570 template. add_query_arg() picks the ?/& separator (plain permalinks already
|
||||
* carry a query string) and does not encode values, so the {q} placeholder stays intact.
|
||||
*/
|
||||
$endpoints['actorAutocomplete'] = \add_query_arg( 'q', '{q}', get_rest_url_by_path( 'actors/autocomplete' ) );
|
||||
}
|
||||
|
||||
return $endpoints;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -428,10 +438,10 @@ class User extends Actor {
|
||||
* @return bool True if the attribute was updated, false otherwise.
|
||||
*/
|
||||
public function update_icon( $value ) {
|
||||
if ( ! wp_attachment_is_image( $value ) ) {
|
||||
if ( ! \wp_attachment_is_image( $value ) ) {
|
||||
return false;
|
||||
}
|
||||
return update_user_option( $this->_id, 'activitypub_icon', $value );
|
||||
return \update_user_option( $this->_id, 'activitypub_icon', $value );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -441,7 +451,7 @@ class User extends Actor {
|
||||
* @return bool True if the attribute was updated, false otherwise.
|
||||
*/
|
||||
public function update_header( $value ) {
|
||||
if ( ! wp_attachment_is_image( $value ) ) {
|
||||
if ( ! \wp_attachment_is_image( $value ) ) {
|
||||
return false;
|
||||
}
|
||||
return \update_user_option( $this->_id, 'activitypub_header_image', $value );
|
||||
@ -468,9 +478,9 @@ class User extends Actor {
|
||||
$this->get_alternate_url(),
|
||||
);
|
||||
|
||||
$also_known_as = array_merge( $also_known_as, \get_user_option( 'activitypub_also_known_as', $this->_id ) ?: array() );
|
||||
$also_known_as = \array_merge( $also_known_as, \get_user_option( 'activitypub_also_known_as', $this->_id ) ?: array() );
|
||||
|
||||
return array_unique( $also_known_as );
|
||||
return \array_unique( $also_known_as );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -483,4 +493,50 @@ class User extends Actor {
|
||||
|
||||
return $moved_to && $moved_to !== $this->get_id() ? $moved_to : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actor-level interaction policy.
|
||||
*
|
||||
* Overrides the magic property accessor on Base_Object so that we always
|
||||
* compute the policy from the current site setting rather than returning a
|
||||
* cached property value. Currently only emits `canFeature` (FEP-7aa9).
|
||||
* Driven by the site option `activitypub_default_feature_policy` and
|
||||
* defaults to denying all featured-collection requests, in line with
|
||||
* FEP-7aa9's "absence of policy = no consent" rule.
|
||||
*
|
||||
* @see https://w3id.org/fep/7aa9
|
||||
*
|
||||
* @since 9.0.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_interaction_policy() {
|
||||
$policy = array( 'canFeature' => $this->build_can_feature_policy() );
|
||||
|
||||
// Merge with an explicitly set interaction policy, if any.
|
||||
if ( $this->interaction_policy ) {
|
||||
$policy = \array_merge( (array) $this->interaction_policy, $policy );
|
||||
}
|
||||
|
||||
return $policy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the `canFeature` policy array from the site option.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function build_can_feature_policy() {
|
||||
$policy = \get_option( 'activitypub_default_feature_policy', ACTIVITYPUB_INTERACTION_POLICY_ME );
|
||||
|
||||
switch ( $policy ) {
|
||||
case ACTIVITYPUB_INTERACTION_POLICY_ANYONE:
|
||||
return array( 'automaticApproval' => array( 'https://www.w3.org/ns/activitystreams#Public' ) );
|
||||
case ACTIVITYPUB_INTERACTION_POLICY_FOLLOWERS:
|
||||
return array( 'automaticApproval' => array( $this->get_followers() ) );
|
||||
case ACTIVITYPUB_INTERACTION_POLICY_ME:
|
||||
default:
|
||||
return array( 'automaticApproval' => array( $this->get_id() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user