| 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 |
|
| 12 |
use function Activitypub\is_user_disabled; |
| 13 |
use function Activitypub\get_rest_url_by_path; |
| 14 |
use function Activitypub\get_actor_extra_fields; |
| 15 |
|
| 16 |
class User extends Actor { |
| 17 |
/** |
| 18 |
* The local User-ID (WP_User). |
| 19 |
* |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 23 |
|
| 24 |
/** |
| 25 |
* The Featured-Posts. |
| 26 |
* |
| 27 |
* @see https://docs.joinmastodon.org/spec/activitypub/#featured |
| 28 |
* |
| 29 |
* @context { |
| 30 |
* "@id": "http://joinmastodon.org/ns#featured", |
| 31 |
* "@type": "@id" |
| 32 |
* } |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $featured; |
| 37 |
|
| 38 |
/** |
| 39 |
* If the User is discoverable. |
| 40 |
* |
| 41 |
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable |
| 42 |
* |
| 43 |
* @context http://joinmastodon.org/ns#discoverable |
| 44 |
* |
| 45 |
* @var boolean |
| 46 |
*/ |
| 47 |
protected $discoverable = true; |
| 48 |
|
| 49 |
/** |
| 50 |
* If the User is indexable. |
| 51 |
* |
| 52 |
* @context http://joinmastodon.org/ns#indexable |
| 53 |
* |
| 54 |
* @var boolean |
| 55 |
*/ |
| 56 |
protected $indexable; |
| 57 |
|
| 58 |
/** |
| 59 |
* The WebFinger Resource. |
| 60 |
* |
| 61 |
* @var string<url> |
| 62 |
*/ |
| 63 |
protected $webfinger; |
| 64 |
|
| 65 |
public function get_type() { |
| 66 |
return 'Person'; |
| 67 |
} |
| 68 |
|
| 69 |
public static function from_wp_user( $user_id ) { |
| 70 |
if ( is_user_disabled( $user_id ) ) { |
| 71 |
return new WP_Error( |
| 72 |
'activitypub_user_not_found', |
| 73 |
\__( 'User not found', 'activitypub' ), |
| 74 |
array( 'status' => 404 ) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
$object = new static(); |
| 79 |
$object->_id = $user_id; |
| 80 |
|
| 81 |
return $object; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the User-ID. |
| 86 |
* |
| 87 |
* @return string The User-ID. |
| 88 |
*/ |
| 89 |
public function get_id() { |
| 90 |
return $this->get_url(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the User-Name. |
| 95 |
* |
| 96 |
* @return string The User-Name. |
| 97 |
*/ |
| 98 |
public function get_name() { |
| 99 |
return \esc_attr( \get_the_author_meta( 'display_name', $this->_id ) ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the User-Description. |
| 104 |
* |
| 105 |
* @return string The User-Description. |
| 106 |
*/ |
| 107 |
public function get_summary() { |
| 108 |
$description = get_user_meta( $this->_id, 'activitypub_user_description', true ); |
| 109 |
if ( empty( $description ) ) { |
| 110 |
$description = get_user_meta( $this->_id, 'description', true ); |
| 111 |
} |
| 112 |
return \wpautop( \wp_kses( $description, 'default' ) ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get the User-Url. |
| 117 |
* |
| 118 |
* @return string The User-Url. |
| 119 |
*/ |
| 120 |
public function get_url() { |
| 121 |
return \esc_url( \get_author_posts_url( $this->_id ) ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the User-URL with @-Prefix for the username. |
| 126 |
* |
| 127 |
* @return string The User-URL with @-Prefix for the username. |
| 128 |
*/ |
| 129 |
public function get_alternate_url() { |
| 130 |
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() ); |
| 131 |
} |
| 132 |
|
| 133 |
public function get_preferred_username() { |
| 134 |
return \esc_attr( \get_the_author_meta( 'login', $this->_id ) ); |
| 135 |
} |
| 136 |
|
| 137 |
public function get_icon() { |
| 138 |
$icon = \esc_url( |
| 139 |
\get_avatar_url( |
| 140 |
$this->_id, |
| 141 |
array( 'size' => 120 ) |
| 142 |
) |
| 143 |
); |
| 144 |
|
| 145 |
return array( |
| 146 |
'type' => 'Image', |
| 147 |
'url' => $icon, |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
public function get_image() { |
| 152 |
if ( \has_header_image() ) { |
| 153 |
$image = \esc_url( \get_header_image() ); |
| 154 |
return array( |
| 155 |
'type' => 'Image', |
| 156 |
'url' => $image, |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
return null; |
| 161 |
} |
| 162 |
|
| 163 |
public function get_published() { |
| 164 |
return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( \get_the_author_meta( 'registered', $this->_id ) ) ); |
| 165 |
} |
| 166 |
|
| 167 |
public function get_public_key() { |
| 168 |
return array( |
| 169 |
'id' => $this->get_id() . '#main-key', |
| 170 |
'owner' => $this->get_id(), |
| 171 |
'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ), |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Returns the Inbox-API-Endpoint. |
| 177 |
* |
| 178 |
* @return string The Inbox-Endpoint. |
| 179 |
*/ |
| 180 |
public function get_inbox() { |
| 181 |
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Returns the Outbox-API-Endpoint. |
| 186 |
* |
| 187 |
* @return string The Outbox-Endpoint. |
| 188 |
*/ |
| 189 |
public function get_outbox() { |
| 190 |
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Returns the Followers-API-Endpoint. |
| 195 |
* |
| 196 |
* @return string The Followers-Endpoint. |
| 197 |
*/ |
| 198 |
public function get_followers() { |
| 199 |
return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Returns the Following-API-Endpoint. |
| 204 |
* |
| 205 |
* @return string The Following-Endpoint. |
| 206 |
*/ |
| 207 |
public function get_following() { |
| 208 |
return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Returns the Featured-API-Endpoint. |
| 213 |
* |
| 214 |
* @return string The Featured-Endpoint. |
| 215 |
*/ |
| 216 |
public function get_featured() { |
| 217 |
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) ); |
| 218 |
} |
| 219 |
|
| 220 |
public function get_endpoints() { |
| 221 |
$endpoints = null; |
| 222 |
|
| 223 |
if ( ACTIVITYPUB_SHARED_INBOX_FEATURE ) { |
| 224 |
$endpoints = array( |
| 225 |
'sharedInbox' => get_rest_url_by_path( 'inbox' ), |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
return $endpoints; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Extend the User-Output with Attachments. |
| 234 |
* |
| 235 |
* @return array The extended User-Output. |
| 236 |
*/ |
| 237 |
public function get_attachment() { |
| 238 |
$extra_fields = get_actor_extra_fields( \get_current_user_id() ); |
| 239 |
|
| 240 |
$attachments = array(); |
| 241 |
|
| 242 |
foreach ( $extra_fields as $post ) { |
| 243 |
$content = \get_the_content( null, false, $post ); |
| 244 |
$content = \make_clickable( $content ); |
| 245 |
$content = \do_blocks( $content ); |
| 246 |
$content = \wptexturize( $content ); |
| 247 |
$content = \wp_filter_content_tags( $content ); |
| 248 |
// replace script and style elements |
| 249 |
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content ); |
| 250 |
$content = \strip_shortcodes( $content ); |
| 251 |
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); |
| 252 |
|
| 253 |
$attachments[] = array( |
| 254 |
'type' => 'PropertyValue', |
| 255 |
'name' => \get_the_title( $post ), |
| 256 |
'value' => \html_entity_decode( |
| 257 |
$content, |
| 258 |
\ENT_QUOTES, |
| 259 |
'UTF-8' |
| 260 |
), |
| 261 |
); |
| 262 |
|
| 263 |
$link_added = false; |
| 264 |
|
| 265 |
// Add support for FEP-fb2a, for more information see FEDERATION.md |
| 266 |
if ( \class_exists( '\WP_HTML_Tag_Processor' ) ) { |
| 267 |
$tags = new \WP_HTML_Tag_Processor( $content ); |
| 268 |
$tags->next_tag(); |
| 269 |
|
| 270 |
if ( 'P' === $tags->get_tag() ) { |
| 271 |
$tags->next_tag(); |
| 272 |
} |
| 273 |
|
| 274 |
if ( 'A' === $tags->get_tag() ) { |
| 275 |
$tags->set_bookmark( 'link' ); |
| 276 |
if ( ! $tags->next_tag() ) { |
| 277 |
$tags->seek( 'link' ); |
| 278 |
$attachment = array( |
| 279 |
'type' => 'Link', |
| 280 |
'name' => \get_the_title( $post ), |
| 281 |
'href' => \esc_url( $tags->get_attribute( 'href' ) ), |
| 282 |
'rel' => explode( ' ', $tags->get_attribute( 'rel' ) ), |
| 283 |
); |
| 284 |
|
| 285 |
$link_added = true; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if ( ! $link_added ) { |
| 291 |
$attachment = array( |
| 292 |
'type' => 'Note', |
| 293 |
'name' => \get_the_title( $post ), |
| 294 |
'content' => \html_entity_decode( |
| 295 |
$content, |
| 296 |
\ENT_QUOTES, |
| 297 |
'UTF-8' |
| 298 |
), |
| 299 |
); |
| 300 |
} |
| 301 |
|
| 302 |
$attachments[] = $attachment; |
| 303 |
} |
| 304 |
|
| 305 |
return $attachments; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Returns a user@domain type of identifier for the user. |
| 310 |
* |
| 311 |
* @return string The Webfinger-Identifier. |
| 312 |
*/ |
| 313 |
public function get_webfinger() { |
| 314 |
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); |
| 315 |
} |
| 316 |
|
| 317 |
public function get_canonical_url() { |
| 318 |
return $this->get_url(); |
| 319 |
} |
| 320 |
|
| 321 |
public function get_streams() { |
| 322 |
return null; |
| 323 |
} |
| 324 |
|
| 325 |
public function get_tag() { |
| 326 |
return array(); |
| 327 |
} |
| 328 |
|
| 329 |
public function get_indexable() { |
| 330 |
if ( \get_option( 'blog_public', 1 ) ) { |
| 331 |
return true; |
| 332 |
} else { |
| 333 |
return false; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|