2023-05-24 16:32:00 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Model;
|
|
|
|
|
|
|
|
use WP_Query;
|
2023-05-31 10:31:49 +02:00
|
|
|
use Activitypub\Signature;
|
2023-07-03 11:20:44 +02:00
|
|
|
use Activitypub\Collection\Users;
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-07-11 09:28:10 +02:00
|
|
|
use function Activitypub\is_single_user;
|
2023-06-28 16:43:41 +02:00
|
|
|
use function Activitypub\is_user_disabled;
|
2023-09-22 09:38:59 +02:00
|
|
|
use function Activitypub\get_rest_url_by_path;
|
2023-06-28 16:43:41 +02:00
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
class Blog_User extends User {
|
|
|
|
/**
|
|
|
|
* The User-ID
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2023-07-03 11:20:44 +02:00
|
|
|
protected $_id = Users::BLOG_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
2023-05-24 16:32:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The User-Type
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-09-22 09:38:59 +02:00
|
|
|
protected $type = null;
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-07-31 20:15:11 +02:00
|
|
|
/**
|
|
|
|
* Is Account discoverable?
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $discoverable = true;
|
|
|
|
|
2023-06-28 16:43:41 +02:00
|
|
|
public static function from_wp_user( $user_id ) {
|
|
|
|
if ( is_user_disabled( $user_id ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_user_not_found',
|
|
|
|
\__( 'User not found', 'activitypub' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$object = new static();
|
|
|
|
$object->_id = $user_id;
|
|
|
|
|
|
|
|
return $object;
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 09:38:59 +02:00
|
|
|
/**
|
|
|
|
* Get the type of the object.
|
|
|
|
*
|
|
|
|
* If the Blog is in "single user" mode, return "Person" insted of "Group".
|
|
|
|
*
|
|
|
|
* @return string The type of the object.
|
|
|
|
*/
|
|
|
|
public function get_type() {
|
|
|
|
if ( is_single_user() ) {
|
|
|
|
return 'Person';
|
|
|
|
} else {
|
|
|
|
return 'Group';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
/**
|
|
|
|
* Get the User-Name.
|
|
|
|
*
|
|
|
|
* @return string The User-Name.
|
|
|
|
*/
|
|
|
|
public function get_name() {
|
2023-10-02 17:11:56 +02:00
|
|
|
return \wp_strip_all_tags(
|
|
|
|
\html_entity_decode(
|
|
|
|
\get_bloginfo( 'name' ),
|
|
|
|
\ENT_QUOTES,
|
|
|
|
'UTF-8'
|
|
|
|
)
|
|
|
|
);
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User-Description.
|
|
|
|
*
|
|
|
|
* @return string The User-Description.
|
|
|
|
*/
|
|
|
|
public function get_summary() {
|
2023-06-14 15:02:45 +02:00
|
|
|
return \wpautop(
|
|
|
|
\wp_kses(
|
|
|
|
\get_bloginfo( 'description' ),
|
|
|
|
'default'
|
|
|
|
)
|
|
|
|
);
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User-Url.
|
|
|
|
*
|
|
|
|
* @return string The User-Url.
|
|
|
|
*/
|
|
|
|
public function get_url() {
|
2023-08-16 01:22:58 +02:00
|
|
|
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-21 10:04:15 +01:00
|
|
|
* Get blog's homepage URL.
|
2023-08-16 01:22:58 +02:00
|
|
|
*
|
2023-12-21 10:04:15 +01:00
|
|
|
* @return string The User-Url.
|
2023-08-16 01:22:58 +02:00
|
|
|
*/
|
2023-12-21 10:04:15 +01:00
|
|
|
public function get_alternate_url() {
|
|
|
|
return \esc_url( \trailingslashit( get_home_url() ) );
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 11:22:20 +02:00
|
|
|
/**
|
2023-07-11 14:34:11 +02:00
|
|
|
* Generate a default Username.
|
2023-05-30 11:22:20 +02:00
|
|
|
*
|
|
|
|
* @return string The auto-generated Username.
|
|
|
|
*/
|
|
|
|
public static function get_default_username() {
|
|
|
|
// check if domain host has a subdomain
|
2023-07-31 20:15:11 +02:00
|
|
|
$host = \wp_parse_url( \get_home_url(), \PHP_URL_HOST );
|
|
|
|
$host = \preg_replace( '/^www\./i', '', $host );
|
2023-05-30 11:22:20 +02:00
|
|
|
|
2023-07-11 14:34:11 +02:00
|
|
|
/**
|
|
|
|
* Filter the default blog username.
|
|
|
|
*
|
2023-07-14 11:29:03 +02:00
|
|
|
* @param string $host The default username.
|
2023-07-11 14:34:11 +02:00
|
|
|
*/
|
2023-07-14 11:29:03 +02:00
|
|
|
return apply_filters( 'activitypub_default_blog_username', $host );
|
2023-05-30 11:22:20 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 14:34:11 +02:00
|
|
|
/**
|
|
|
|
* Get the preferred User-Name.
|
|
|
|
*
|
|
|
|
* @return string The User-Name.
|
|
|
|
*/
|
2023-07-05 15:32:26 +02:00
|
|
|
public function get_preferred_username() {
|
2023-07-11 09:21:16 +02:00
|
|
|
$username = \get_option( 'activitypub_blog_user_identifier' );
|
|
|
|
|
|
|
|
if ( $username ) {
|
|
|
|
return $username;
|
|
|
|
}
|
|
|
|
|
2023-05-30 11:22:20 +02:00
|
|
|
return self::get_default_username();
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 14:34:11 +02:00
|
|
|
/**
|
|
|
|
* Get the User-Icon.
|
|
|
|
*
|
2023-09-01 19:08:27 +02:00
|
|
|
* @return array The User-Icon.
|
2023-07-11 14:34:11 +02:00
|
|
|
*/
|
2023-07-05 15:32:26 +02:00
|
|
|
public function get_icon() {
|
2023-08-17 04:39:55 +02:00
|
|
|
// try site icon first
|
|
|
|
$icon_id = get_option( 'site_icon' );
|
2023-09-01 19:08:27 +02:00
|
|
|
|
2023-08-17 04:39:55 +02:00
|
|
|
// try custom logo second
|
|
|
|
if ( ! $icon_id ) {
|
|
|
|
$icon_id = get_theme_mod( 'custom_logo' );
|
|
|
|
}
|
2023-09-01 19:08:27 +02:00
|
|
|
|
|
|
|
$icon_url = false;
|
|
|
|
|
|
|
|
if ( $icon_id ) {
|
|
|
|
$icon = wp_get_attachment_image_src( $icon_id, 'full' );
|
|
|
|
if ( $icon ) {
|
|
|
|
$icon_url = $icon[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $icon_url ) {
|
|
|
|
// fallback to default icon
|
|
|
|
$icon_url = plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE );
|
2023-07-05 15:32:26 +02:00
|
|
|
}
|
|
|
|
|
2023-08-17 04:39:55 +02:00
|
|
|
return array(
|
|
|
|
'type' => 'Image',
|
2023-09-01 19:08:27 +02:00
|
|
|
'url' => esc_url( $icon_url ),
|
2023-08-17 04:39:55 +02:00
|
|
|
);
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 14:34:11 +02:00
|
|
|
/**
|
|
|
|
* Get the User-Header-Image.
|
|
|
|
*
|
|
|
|
* @return array|null The User-Header-Image.
|
|
|
|
*/
|
2023-05-24 16:32:00 +02:00
|
|
|
public function get_header_image() {
|
|
|
|
if ( \has_header_image() ) {
|
2023-07-05 15:32:26 +02:00
|
|
|
return array(
|
|
|
|
'type' => 'Image',
|
|
|
|
'url' => esc_url( \get_header_image() ),
|
|
|
|
);
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_published() {
|
|
|
|
$first_post = new WP_Query(
|
|
|
|
array(
|
|
|
|
'orderby' => 'date',
|
|
|
|
'order' => 'ASC',
|
|
|
|
'number' => 1,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! empty( $first_post->posts[0] ) ) {
|
|
|
|
$time = \strtotime( $first_post->posts[0]->post_date_gmt );
|
|
|
|
} else {
|
|
|
|
$time = \time();
|
|
|
|
}
|
|
|
|
|
|
|
|
return \gmdate( 'Y-m-d\TH:i:s\Z', $time );
|
|
|
|
}
|
2023-05-24 17:27:46 +02:00
|
|
|
|
2023-06-28 16:43:41 +02:00
|
|
|
public function get_attachment() {
|
2023-09-28 14:38:48 +02:00
|
|
|
return array();
|
2023-06-28 16:43:41 +02:00
|
|
|
}
|
2023-06-28 18:02:14 +02:00
|
|
|
|
|
|
|
public function get_canonical_url() {
|
|
|
|
return \home_url();
|
|
|
|
}
|
2023-07-11 09:28:10 +02:00
|
|
|
|
2023-09-22 09:38:59 +02:00
|
|
|
public function get_moderators() {
|
|
|
|
if ( is_single_user() || 'Group' !== $this->get_type() ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_rest_url_by_path( 'collections/moderators' );
|
|
|
|
}
|
|
|
|
|
2023-09-27 11:08:55 +02:00
|
|
|
public function get_attributed_to() {
|
|
|
|
if ( is_single_user() || 'Group' !== $this->get_type() ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_rest_url_by_path( 'collections/moderators' );
|
|
|
|
}
|
|
|
|
|
2023-09-22 09:38:59 +02:00
|
|
|
public function get_posting_restricted_to_mods() {
|
|
|
|
if ( 'Group' === $this->get_type() ) {
|
|
|
|
return true;
|
2023-07-11 09:28:10 +02:00
|
|
|
}
|
2023-09-22 09:38:59 +02:00
|
|
|
|
|
|
|
return null;
|
2023-07-11 09:28:10 +02:00
|
|
|
}
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|