| 1 |
<?php |
| 2 |
namespace Activitypub\Model; |
| 3 |
|
| 4 |
use WP_Query; |
| 5 |
use WP_Error; |
| 6 |
use Activitypub\Migration; |
| 7 |
use Activitypub\Signature; |
| 8 |
use Activitypub\Model\Blog; |
| 9 |
use Activitypub\Activity\Actor; |
| 10 |
use Activitypub\Collection\Users; |
| 11 |
use Activitypub\Collection\Extra_Fields; |
| 12 |
|
| 13 |
use function Activitypub\is_blog_public; |
| 14 |
use function Activitypub\is_user_disabled; |
| 15 |
use function Activitypub\get_rest_url_by_path; |
| 16 |
|
| 17 |
class User extends Actor { |
| 18 |
/** |
| 19 |
* The local User-ID (WP_User). |
| 20 |
* |
| 21 |
* @var int |
| 22 |
*/ |
| 23 |
protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 24 |
|
| 25 |
/** |
| 26 |
* The Featured-Posts. |
| 27 |
* |
| 28 |
* @see https://docs.joinmastodon.org/spec/activitypub/#featured |
| 29 |
* |
| 30 |
* @context { |
| 31 |
* "@id": "http://joinmastodon.org/ns#featured", |
| 32 |
* "@type": "@id" |
| 33 |
* } |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $featured; |
| 38 |
|
| 39 |
/** |
| 40 |
* If the User is discoverable. |
| 41 |
* |
| 42 |
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable |
| 43 |
* |
| 44 |
* @context http://joinmastodon.org/ns#discoverable |
| 45 |
* |
| 46 |
* @var boolean |
| 47 |
*/ |
| 48 |
protected $discoverable = true; |
| 49 |
|
| 50 |
/** |
| 51 |
* If the User is indexable. |
| 52 |
* |
| 53 |
* @context http://joinmastodon.org/ns#indexable |
| 54 |
* |
| 55 |
* @var boolean |
| 56 |
*/ |
| 57 |
protected $indexable; |
| 58 |
|
| 59 |
/** |
| 60 |
* The WebFinger Resource. |
| 61 |
* |
| 62 |
* @var string<url> |
| 63 |
*/ |
| 64 |
protected $webfinger; |
| 65 |
|
| 66 |
public function get_type() { |
| 67 |
return 'Person'; |
| 68 |
} |
| 69 |
|
| 70 |
public static function from_wp_user( $user_id ) { |
| 71 |
if ( is_user_disabled( $user_id ) ) { |
| 72 |
return new WP_Error( |
| 73 |
'activitypub_user_not_found', |
| 74 |
\__( 'User not found', 'activitypub' ), |
| 75 |
array( 'status' => 404 ) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
$object = new static(); |
| 80 |
$object->_id = $user_id; |
| 81 |
|
| 82 |
return $object; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get the User-ID. |
| 87 |
* |
| 88 |
* @return string The User-ID. |
| 89 |
*/ |
| 90 |
public function get_id() { |
| 91 |
return $this->get_url(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get the User-Name. |
| 96 |
* |
| 97 |
* @return string The User-Name. |
| 98 |
*/ |
| 99 |
public function get_name() { |
| 100 |
return \esc_attr( \get_the_author_meta( 'display_name', $this->_id ) ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get the User-Description. |
| 105 |
* |
| 106 |
* @return string The User-Description. |
| 107 |
*/ |
| 108 |
public function get_summary() { |
| 109 |
$description = get_user_option( 'activitypub_description', $this->_id ); |
| 110 |
if ( empty( $description ) ) { |
| 111 |
$description = get_user_meta( $this->_id, 'description', true ); |
| 112 |
} |
| 113 |
return \wpautop( \wp_kses( $description, 'default' ) ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get the User-Url. |
| 118 |
* |
| 119 |
* @return string The User-Url. |
| 120 |
*/ |
| 121 |
public function get_url() { |
| 122 |
return \esc_url( \get_author_posts_url( $this->_id ) ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns the User-URL with @-Prefix for the username. |
| 127 |
* |
| 128 |
* @return string The User-URL with @-Prefix for the username. |
| 129 |
*/ |
| 130 |
public function get_alternate_url() { |
| 131 |
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() ); |
| 132 |
} |
| 133 |
|
| 134 |
public function get_preferred_username() { |
| 135 |
return \esc_attr( \get_the_author_meta( 'login', $this->_id ) ); |
| 136 |
} |
| 137 |
|
| 138 |
public function get_icon() { |
| 139 |
$icon = \esc_url( |
| 140 |
\get_avatar_url( |
| 141 |
$this->_id, |
| 142 |
array( 'size' => 120 ) |
| 143 |
) |
| 144 |
); |
| 145 |
|
| 146 |
return array( |
| 147 |
'type' => 'Image', |
| 148 |
'url' => $icon, |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
public function get_image() { |
| 153 |
$header_image = get_user_option( 'activitypub_header_image', $this->_id ); |
| 154 |
$image_url = null; |
| 155 |
|
| 156 |
if ( $header_image ) { |
| 157 |
$image_url = \wp_get_attachment_url( $header_image ); |
| 158 |
} |
| 159 |
|
| 160 |
if ( ! $image_url && \has_header_image() ) { |
| 161 |
$image_url = \get_header_image(); |
| 162 |
} |
| 163 |
|
| 164 |
if ( $image_url ) { |
| 165 |
return array( |
| 166 |
'type' => 'Image', |
| 167 |
'url' => esc_url( $image_url ), |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
return null; |
| 172 |
} |
| 173 |
|
| 174 |
public function get_published() { |
| 175 |
return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( \get_the_author_meta( 'registered', $this->_id ) ) ); |
| 176 |
} |
| 177 |
|
| 178 |
public function get_public_key() { |
| 179 |
return array( |
| 180 |
'id' => $this->get_id() . '#main-key', |
| 181 |
'owner' => $this->get_id(), |
| 182 |
'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ), |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Returns the Inbox-API-Endpoint. |
| 188 |
* |
| 189 |
* @return string The Inbox-Endpoint. |
| 190 |
*/ |
| 191 |
public function get_inbox() { |
| 192 |
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Returns the Outbox-API-Endpoint. |
| 197 |
* |
| 198 |
* @return string The Outbox-Endpoint. |
| 199 |
*/ |
| 200 |
public function get_outbox() { |
| 201 |
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns the Followers-API-Endpoint. |
| 206 |
* |
| 207 |
* @return string The Followers-Endpoint. |
| 208 |
*/ |
| 209 |
public function get_followers() { |
| 210 |
return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Returns the Following-API-Endpoint. |
| 215 |
* |
| 216 |
* @return string The Following-Endpoint. |
| 217 |
*/ |
| 218 |
public function get_following() { |
| 219 |
return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Returns the Featured-API-Endpoint. |
| 224 |
* |
| 225 |
* @return string The Featured-Endpoint. |
| 226 |
*/ |
| 227 |
public function get_featured() { |
| 228 |
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) ); |
| 229 |
} |
| 230 |
|
| 231 |
public function get_endpoints() { |
| 232 |
$endpoints = null; |
| 233 |
|
| 234 |
if ( ACTIVITYPUB_SHARED_INBOX_FEATURE ) { |
| 235 |
$endpoints = array( |
| 236 |
'sharedInbox' => get_rest_url_by_path( 'inbox' ), |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
return $endpoints; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Extend the User-Output with Attachments. |
| 245 |
* |
| 246 |
* @return array The extended User-Output. |
| 247 |
*/ |
| 248 |
public function get_attachment() { |
| 249 |
$extra_fields = Extra_Fields::get_actor_fields( $this->_id ); |
| 250 |
return Extra_Fields::fields_to_attachments( $extra_fields ); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Returns a user@domain type of identifier for the user. |
| 255 |
* |
| 256 |
* @return string The Webfinger-Identifier. |
| 257 |
*/ |
| 258 |
public function get_webfinger() { |
| 259 |
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); |
| 260 |
} |
| 261 |
|
| 262 |
public function get_canonical_url() { |
| 263 |
return $this->get_url(); |
| 264 |
} |
| 265 |
|
| 266 |
public function get_streams() { |
| 267 |
return null; |
| 268 |
} |
| 269 |
|
| 270 |
public function get_tag() { |
| 271 |
return array(); |
| 272 |
} |
| 273 |
|
| 274 |
public function get_indexable() { |
| 275 |
if ( is_blog_public() ) { |
| 276 |
return true; |
| 277 |
} else { |
| 278 |
return false; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|