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