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