| 1 |
<?php |
| 2 |
namespace Activitypub\Model; |
| 3 |
|
| 4 |
use WP_Query; |
| 5 |
use Activitypub\Signature; |
| 6 |
use Activitypub\Activity\Actor; |
| 7 |
use Activitypub\Collection\Users; |
| 8 |
|
| 9 |
use function Activitypub\get_rest_url_by_path; |
| 10 |
|
| 11 |
class Application extends Actor { |
| 12 |
/** |
| 13 |
* The User-ID |
| 14 |
* |
| 15 |
* @var int |
| 16 |
*/ |
| 17 |
protected $_id = Users::APPLICATION_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 18 |
|
| 19 |
/** |
| 20 |
* If the User is discoverable. |
| 21 |
* |
| 22 |
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable |
| 23 |
* |
| 24 |
* @context http://joinmastodon.org/ns#discoverable |
| 25 |
* |
| 26 |
* @var boolean |
| 27 |
*/ |
| 28 |
protected $discoverable = false; |
| 29 |
|
| 30 |
/** |
| 31 |
* If the User is indexable. |
| 32 |
* |
| 33 |
* @context http://joinmastodon.org/ns#indexable |
| 34 |
* |
| 35 |
* @var boolean |
| 36 |
*/ |
| 37 |
protected $indexable = false; |
| 38 |
|
| 39 |
/** |
| 40 |
* The WebFinger Resource. |
| 41 |
* |
| 42 |
* @var string<url> |
| 43 |
*/ |
| 44 |
protected $webfinger; |
| 45 |
|
| 46 |
public function get_type() { |
| 47 |
return 'Application'; |
| 48 |
} |
| 49 |
|
| 50 |
public function get_manually_approves_followers() { |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
public function get_id() { |
| 55 |
return get_rest_url_by_path( 'application' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get the User-Url. |
| 60 |
* |
| 61 |
* @return string The User-Url. |
| 62 |
*/ |
| 63 |
public function get_url() { |
| 64 |
return $this->get_id(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns the User-URL with @-Prefix for the username. |
| 69 |
* |
| 70 |
* @return string The User-URL with @-Prefix for the username. |
| 71 |
*/ |
| 72 |
public function get_alternate_url() { |
| 73 |
return $this->get_url(); |
| 74 |
} |
| 75 |
|
| 76 |
public function get_name() { |
| 77 |
return 'application'; |
| 78 |
} |
| 79 |
|
| 80 |
public function get_preferred_username() { |
| 81 |
return $this->get_name(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the User-Icon. |
| 86 |
* |
| 87 |
* @return array The User-Icon. |
| 88 |
*/ |
| 89 |
public function get_icon() { |
| 90 |
// try site icon first |
| 91 |
$icon_id = get_option( 'site_icon' ); |
| 92 |
|
| 93 |
// try custom logo second |
| 94 |
if ( ! $icon_id ) { |
| 95 |
$icon_id = get_theme_mod( 'custom_logo' ); |
| 96 |
} |
| 97 |
|
| 98 |
$icon_url = false; |
| 99 |
|
| 100 |
if ( $icon_id ) { |
| 101 |
$icon = wp_get_attachment_image_src( $icon_id, 'full' ); |
| 102 |
if ( $icon ) { |
| 103 |
$icon_url = $icon[0]; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! $icon_url ) { |
| 108 |
// fallback to default icon |
| 109 |
$icon_url = plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE ); |
| 110 |
} |
| 111 |
|
| 112 |
return array( |
| 113 |
'type' => 'Image', |
| 114 |
'url' => esc_url( $icon_url ), |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get the User-Header-Image. |
| 120 |
* |
| 121 |
* @return array|null The User-Header-Image. |
| 122 |
*/ |
| 123 |
public function get_header_image() { |
| 124 |
if ( \has_header_image() ) { |
| 125 |
return array( |
| 126 |
'type' => 'Image', |
| 127 |
'url' => esc_url( \get_header_image() ), |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
return null; |
| 132 |
} |
| 133 |
|
| 134 |
public function get_published() { |
| 135 |
$first_post = new WP_Query( |
| 136 |
array( |
| 137 |
'orderby' => 'date', |
| 138 |
'order' => 'ASC', |
| 139 |
'number' => 1, |
| 140 |
) |
| 141 |
); |
| 142 |
|
| 143 |
if ( ! empty( $first_post->posts[0] ) ) { |
| 144 |
$time = \strtotime( $first_post->posts[0]->post_date_gmt ); |
| 145 |
} else { |
| 146 |
$time = \time(); |
| 147 |
} |
| 148 |
|
| 149 |
return \gmdate( 'Y-m-d\TH:i:s\Z', $time ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns the Inbox-API-Endpoint. |
| 154 |
* |
| 155 |
* @return string The Inbox-Endpoint. |
| 156 |
*/ |
| 157 |
public function get_inbox() { |
| 158 |
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Returns the Outbox-API-Endpoint. |
| 163 |
* |
| 164 |
* @return string The Outbox-Endpoint. |
| 165 |
*/ |
| 166 |
public function get_outbox() { |
| 167 |
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Returns a user@domain type of identifier for the user. |
| 172 |
* |
| 173 |
* @return string The Webfinger-Identifier. |
| 174 |
*/ |
| 175 |
public function get_webfinger() { |
| 176 |
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); |
| 177 |
} |
| 178 |
|
| 179 |
public function get_public_key() { |
| 180 |
return array( |
| 181 |
'id' => $this->get_id() . '#main-key', |
| 182 |
'owner' => $this->get_id(), |
| 183 |
'publicKeyPem' => Signature::get_public_key_for( Users::APPLICATION_USER_ID ), |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get the User-Description. |
| 189 |
* |
| 190 |
* @return string The User-Description. |
| 191 |
*/ |
| 192 |
public function get_summary() { |
| 193 |
return \wpautop( |
| 194 |
\wp_kses( |
| 195 |
\get_bloginfo( 'description' ), |
| 196 |
'default' |
| 197 |
) |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
public function get_canonical_url() { |
| 202 |
return \home_url(); |
| 203 |
} |
| 204 |
} |
| 205 |
|