| 1 |
<?php |
| 2 |
/** |
| 3 |
* Application model file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Model; |
| 9 |
|
| 10 |
use WP_Query; |
| 11 |
use Activitypub\Signature; |
| 12 |
use Activitypub\Activity\Actor; |
| 13 |
use Activitypub\Collection\Actors; |
| 14 |
|
| 15 |
use function Activitypub\get_rest_url_by_path; |
| 16 |
|
| 17 |
/** |
| 18 |
* Application class. |
| 19 |
* |
| 20 |
* @method int get__id() Gets the internal user ID for the application (always returns APPLICATION_USER_ID). |
| 21 |
*/ |
| 22 |
class Application extends Actor { |
| 23 |
/** |
| 24 |
* The User-ID |
| 25 |
* |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
protected $_id = Actors::APPLICATION_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 29 |
|
| 30 |
/** |
| 31 |
* Whether the Application is discoverable. |
| 32 |
* |
| 33 |
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable |
| 34 |
* |
| 35 |
* @context http://joinmastodon.org/ns#discoverable |
| 36 |
* |
| 37 |
* @var boolean |
| 38 |
*/ |
| 39 |
protected $discoverable = false; |
| 40 |
|
| 41 |
/** |
| 42 |
* Whether the Application is indexable. |
| 43 |
* |
| 44 |
* @context http://joinmastodon.org/ns#indexable |
| 45 |
* |
| 46 |
* @var boolean |
| 47 |
*/ |
| 48 |
protected $indexable = false; |
| 49 |
|
| 50 |
/** |
| 51 |
* The WebFinger Resource. |
| 52 |
* |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
protected $webfinger; |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the type of the object. |
| 59 |
* |
| 60 |
* @return string The type of the object. |
| 61 |
*/ |
| 62 |
public function get_type() { |
| 63 |
return 'Application'; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns whether the Application manually approves followers. |
| 68 |
* |
| 69 |
* @return true Whether the Application manually approves followers. |
| 70 |
*/ |
| 71 |
public function get_manually_approves_followers() { |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the ID of the Application. |
| 77 |
* |
| 78 |
* @return string The ID of the Application. |
| 79 |
*/ |
| 80 |
public function get_id() { |
| 81 |
return get_rest_url_by_path( 'application' ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the User-Url. |
| 86 |
* |
| 87 |
* @return string The User-Url. |
| 88 |
*/ |
| 89 |
public function get_url() { |
| 90 |
return $this->get_id(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns the User-URL with @-Prefix for the username. |
| 95 |
* |
| 96 |
* @return string The User-URL with @-Prefix for the username. |
| 97 |
*/ |
| 98 |
public function get_alternate_url() { |
| 99 |
return $this->get_id(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the Username. |
| 104 |
* |
| 105 |
* @return string The Username. |
| 106 |
*/ |
| 107 |
public function get_name() { |
| 108 |
return 'application'; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the preferred username. |
| 113 |
* |
| 114 |
* @return string The preferred username. |
| 115 |
*/ |
| 116 |
public function get_preferred_username() { |
| 117 |
return $this->get_name(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the User-Icon. |
| 122 |
* |
| 123 |
* @return array The User-Icon. |
| 124 |
*/ |
| 125 |
public function get_icon() { |
| 126 |
// Try site icon first. |
| 127 |
$icon_id = get_option( 'site_icon' ); |
| 128 |
|
| 129 |
// Try custom logo second. |
| 130 |
if ( ! $icon_id ) { |
| 131 |
$icon_id = get_theme_mod( 'custom_logo' ); |
| 132 |
} |
| 133 |
|
| 134 |
$icon_url = false; |
| 135 |
|
| 136 |
if ( $icon_id ) { |
| 137 |
$icon = wp_get_attachment_image_src( $icon_id, 'full' ); |
| 138 |
if ( $icon ) { |
| 139 |
$icon_url = $icon[0]; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if ( ! $icon_url ) { |
| 144 |
// Fallback to default icon. |
| 145 |
$icon_url = plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE ); |
| 146 |
} |
| 147 |
|
| 148 |
return array( |
| 149 |
'type' => 'Image', |
| 150 |
'url' => esc_url( $icon_url ), |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get the User-Header-Image. |
| 156 |
* |
| 157 |
* @return array|null The User-Header-Image. |
| 158 |
*/ |
| 159 |
public function get_header_image() { |
| 160 |
if ( \has_header_image() ) { |
| 161 |
return array( |
| 162 |
'type' => 'Image', |
| 163 |
'url' => esc_url( \get_header_image() ), |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
return null; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get the first published date. |
| 172 |
* |
| 173 |
* @return string The published date. |
| 174 |
*/ |
| 175 |
public function get_published() { |
| 176 |
$first_post = new WP_Query( |
| 177 |
array( |
| 178 |
'orderby' => 'date', |
| 179 |
'order' => 'ASC', |
| 180 |
'number' => 1, |
| 181 |
) |
| 182 |
); |
| 183 |
|
| 184 |
if ( ! empty( $first_post->posts[0] ) ) { |
| 185 |
$time = \strtotime( $first_post->posts[0]->post_date_gmt ); |
| 186 |
} else { |
| 187 |
$time = \time(); |
| 188 |
} |
| 189 |
|
| 190 |
return \gmdate( 'Y-m-d\TH:i:s\Z', $time ); |
| 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( 'actors/%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( 'actors/%d/outbox', $this->get__id() ) ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Returns a user@domain type of identifier for the user. |
| 213 |
* |
| 214 |
* @return string The Webfinger-Identifier. |
| 215 |
*/ |
| 216 |
public function get_webfinger() { |
| 217 |
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns the public key. |
| 222 |
* |
| 223 |
* @return array The public key. |
| 224 |
*/ |
| 225 |
public function get_public_key() { |
| 226 |
return array( |
| 227 |
'id' => $this->get_id() . '#main-key', |
| 228 |
'owner' => $this->get_id(), |
| 229 |
'publicKeyPem' => Signature::get_public_key_for( Actors::APPLICATION_USER_ID ), |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Get the User description. |
| 235 |
* |
| 236 |
* @return string The User description. |
| 237 |
*/ |
| 238 |
public function get_summary() { |
| 239 |
return \wpautop( |
| 240 |
\wp_kses( |
| 241 |
\get_bloginfo( 'description' ), |
| 242 |
'default' |
| 243 |
) |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Returns the canonical URL of the object. |
| 249 |
* |
| 250 |
* @return string|null The canonical URL of the object. |
| 251 |
*/ |
| 252 |
public function get_canonical_url() { |
| 253 |
return \home_url(); |
| 254 |
} |
| 255 |
} |
| 256 |
|