| 1 |
<?php |
| 2 |
namespace Activitypub\Model; |
| 3 |
|
| 4 |
use WP_Query; |
| 5 |
use Activitypub\Signature; |
| 6 |
use Activitypub\Collection\Users; |
| 7 |
|
| 8 |
use function Activitypub\is_single_user; |
| 9 |
use function Activitypub\is_user_disabled; |
| 10 |
use function Activitypub\get_rest_url_by_path; |
| 11 |
|
| 12 |
class Blog_User extends User { |
| 13 |
/** |
| 14 |
* The User-ID |
| 15 |
* |
| 16 |
* @var int |
| 17 |
*/ |
| 18 |
protected $_id = Users::BLOG_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 19 |
|
| 20 |
/** |
| 21 |
* The User-Type |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $type = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Is Account discoverable? |
| 29 |
* |
| 30 |
* @var boolean |
| 31 |
*/ |
| 32 |
protected $discoverable = true; |
| 33 |
|
| 34 |
public static function from_wp_user( $user_id ) { |
| 35 |
if ( is_user_disabled( $user_id ) ) { |
| 36 |
return new WP_Error( |
| 37 |
'activitypub_user_not_found', |
| 38 |
\__( 'User not found', 'activitypub' ), |
| 39 |
array( 'status' => 404 ) |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
$object = new static(); |
| 44 |
$object->_id = $user_id; |
| 45 |
|
| 46 |
return $object; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the type of the object. |
| 51 |
* |
| 52 |
* If the Blog is in "single user" mode, return "Person" insted of "Group". |
| 53 |
* |
| 54 |
* @return string The type of the object. |
| 55 |
*/ |
| 56 |
public function get_type() { |
| 57 |
if ( is_single_user() ) { |
| 58 |
return 'Person'; |
| 59 |
} else { |
| 60 |
return 'Group'; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the User-Name. |
| 66 |
* |
| 67 |
* @return string The User-Name. |
| 68 |
*/ |
| 69 |
public function get_name() { |
| 70 |
return \wp_strip_all_tags( |
| 71 |
\html_entity_decode( |
| 72 |
\get_bloginfo( 'name' ), |
| 73 |
\ENT_QUOTES, |
| 74 |
'UTF-8' |
| 75 |
) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get the User-Description. |
| 81 |
* |
| 82 |
* @return string The User-Description. |
| 83 |
*/ |
| 84 |
public function get_summary() { |
| 85 |
return \wpautop( |
| 86 |
\wp_kses( |
| 87 |
\get_bloginfo( 'description' ), |
| 88 |
'default' |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the User-Url. |
| 95 |
* |
| 96 |
* @return string The User-Url. |
| 97 |
*/ |
| 98 |
public function get_url() { |
| 99 |
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns the User-URL with @-Prefix for the username. |
| 104 |
* |
| 105 |
* @return string The User-URL with @-Prefix for the username. |
| 106 |
*/ |
| 107 |
public function get_at_url() { |
| 108 |
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Generate a default Username. |
| 113 |
* |
| 114 |
* @return string The auto-generated Username. |
| 115 |
*/ |
| 116 |
public static function get_default_username() { |
| 117 |
// check if domain host has a subdomain |
| 118 |
$host = \wp_parse_url( \get_home_url(), \PHP_URL_HOST ); |
| 119 |
$host = \preg_replace( '/^www\./i', '', $host ); |
| 120 |
|
| 121 |
/** |
| 122 |
* Filter the default blog username. |
| 123 |
* |
| 124 |
* @param string $host The default username. |
| 125 |
*/ |
| 126 |
return apply_filters( 'activitypub_default_blog_username', $host ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get the preferred User-Name. |
| 131 |
* |
| 132 |
* @return string The User-Name. |
| 133 |
*/ |
| 134 |
public function get_preferred_username() { |
| 135 |
$username = \get_option( 'activitypub_blog_user_identifier' ); |
| 136 |
|
| 137 |
if ( $username ) { |
| 138 |
return $username; |
| 139 |
} |
| 140 |
|
| 141 |
return self::get_default_username(); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get the User-Icon. |
| 146 |
* |
| 147 |
* @return array The User-Icon. |
| 148 |
*/ |
| 149 |
public function get_icon() { |
| 150 |
// try site icon first |
| 151 |
$icon_id = get_option( 'site_icon' ); |
| 152 |
|
| 153 |
// try custom logo second |
| 154 |
if ( ! $icon_id ) { |
| 155 |
$icon_id = get_theme_mod( 'custom_logo' ); |
| 156 |
} |
| 157 |
|
| 158 |
$icon_url = false; |
| 159 |
|
| 160 |
if ( $icon_id ) { |
| 161 |
$icon = wp_get_attachment_image_src( $icon_id, 'full' ); |
| 162 |
if ( $icon ) { |
| 163 |
$icon_url = $icon[0]; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if ( ! $icon_url ) { |
| 168 |
// fallback to default icon |
| 169 |
$icon_url = plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE ); |
| 170 |
} |
| 171 |
|
| 172 |
return array( |
| 173 |
'type' => 'Image', |
| 174 |
'url' => esc_url( $icon_url ), |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Get the User-Header-Image. |
| 180 |
* |
| 181 |
* @return array|null The User-Header-Image. |
| 182 |
*/ |
| 183 |
public function get_header_image() { |
| 184 |
if ( \has_header_image() ) { |
| 185 |
return array( |
| 186 |
'type' => 'Image', |
| 187 |
'url' => esc_url( \get_header_image() ), |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
return null; |
| 192 |
} |
| 193 |
|
| 194 |
public function get_published() { |
| 195 |
$first_post = new WP_Query( |
| 196 |
array( |
| 197 |
'orderby' => 'date', |
| 198 |
'order' => 'ASC', |
| 199 |
'number' => 1, |
| 200 |
) |
| 201 |
); |
| 202 |
|
| 203 |
if ( ! empty( $first_post->posts[0] ) ) { |
| 204 |
$time = \strtotime( $first_post->posts[0]->post_date_gmt ); |
| 205 |
} else { |
| 206 |
$time = \time(); |
| 207 |
} |
| 208 |
|
| 209 |
return \gmdate( 'Y-m-d\TH:i:s\Z', $time ); |
| 210 |
} |
| 211 |
|
| 212 |
public function get_attachment() { |
| 213 |
return array(); |
| 214 |
} |
| 215 |
|
| 216 |
public function get_canonical_url() { |
| 217 |
return \home_url(); |
| 218 |
} |
| 219 |
|
| 220 |
public function get_moderators() { |
| 221 |
if ( is_single_user() || 'Group' !== $this->get_type() ) { |
| 222 |
return null; |
| 223 |
} |
| 224 |
|
| 225 |
return get_rest_url_by_path( 'collections/moderators' ); |
| 226 |
} |
| 227 |
|
| 228 |
public function get_attributed_to() { |
| 229 |
if ( is_single_user() || 'Group' !== $this->get_type() ) { |
| 230 |
return null; |
| 231 |
} |
| 232 |
|
| 233 |
return get_rest_url_by_path( 'collections/moderators' ); |
| 234 |
} |
| 235 |
|
| 236 |
public function get_posting_restricted_to_mods() { |
| 237 |
if ( 'Group' === $this->get_type() ) { |
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
return null; |
| 242 |
} |
| 243 |
} |
| 244 |
|