| @@ -3,11 +3,14 @@ | ||
| 3 | 3 | |
| 4 | 4 | use WP_Error; |
| 5 | 5 | use WP_User_Query; |
| 6 | 6 | use Activitypub\Model\User; |
| 7 | -use Activitypub\Model\Blog_User; | |
| 8 | -use Activitypub\Model\Application_User; | |
| 7 | +use Activitypub\Model\Blog; | |
| 8 | +use Activitypub\Model\Application; | |
| 9 | 9 | |
| 10 | +use function Activitypub\object_to_uri; | |
| 11 | +use function Activitypub\normalize_url; | |
| 12 | +use function Activitypub\normalize_host; | |
| 10 | 13 | use function Activitypub\url_to_authorid; |
| 11 | 14 | use function Activitypub\is_user_disabled; |
| 12 | 15 | |
| 13 | 16 | class Users { |
| @@ -45,11 +48,11 @@ | ||
| 45 | 48 | ); |
| 46 | 49 | } |
| 47 | 50 | |
| 48 | 51 | if ( self::BLOG_USER_ID === $user_id ) { |
| 49 | - return Blog_User::from_wp_user( $user_id ); | |
| 52 | + return new Blog(); | |
| 50 | 53 | } elseif ( self::APPLICATION_USER_ID === $user_id ) { |
| 51 | - return Application_User::from_wp_user( $user_id ); | |
| 54 | + return new Application(); | |
| 52 | 55 | } elseif ( $user_id > 0 ) { |
| 53 | 56 | return User::from_wp_user( $user_id ); |
| 54 | 57 | } |
| 55 | 58 | |
| @@ -68,24 +71,25 @@ | ||
| 68 | 71 | * @return \Acitvitypub\Model\User The User. |
| 69 | 72 | */ |
| 70 | 73 | public static function get_by_username( $username ) { |
| 71 | 74 | // check for blog user. |
| 72 | - if ( Blog_User::get_default_username() === $username ) { | |
| 73 | - return self::get_by_id( self::BLOG_USER_ID ); | |
| 75 | + if ( Blog::get_default_username() === $username ) { | |
| 76 | + return new Blog(); | |
| 74 | 77 | } |
| 75 | 78 | |
| 76 | - if ( get_option( 'activitypub_blog_user_identifier' ) === $username ) { | |
| 77 | - return self::get_by_id( self::BLOG_USER_ID ); | |
| 79 | + if ( get_option( 'activitypub_blog_identifier' ) === $username ) { | |
| 80 | + return new Blog(); | |
| 78 | 81 | } |
| 79 | 82 | |
| 80 | 83 | // check for application user. |
| 81 | 84 | if ( 'application' === $username ) { |
| 82 | - return self::get_by_id( self::APPLICATION_USER_ID ); | |
| 85 | + return new Application(); | |
| 83 | 86 | } |
| 84 | 87 | |
| 85 | 88 | // check for 'activitypub_username' meta |
| 86 | 89 | $user = new WP_User_Query( |
| 87 | 90 | array( |
| 91 | + 'count_total' => false, | |
| 88 | 92 | 'number' => 1, |
| 89 | 93 | 'hide_empty' => true, |
| 90 | 94 | 'fields' => 'ID', |
| 91 | 95 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| @@ -108,8 +112,9 @@ | ||
| 108 | 112 | |
| 109 | 113 | // check for login or nicename. |
| 110 | 114 | $user = new WP_User_Query( |
| 111 | 115 | array( |
| 116 | + 'count_total' => false, | |
| 112 | 117 | 'search' => $username, |
| 113 | 118 | 'search_columns' => array( 'user_login', 'user_nicename' ), |
| 114 | 119 | 'number' => 1, |
| 115 | 120 | 'hide_empty' => true, |
| @@ -135,14 +140,16 @@ | ||
| 135 | 140 | * |
| 136 | 141 | * @return \Acitvitypub\Model\User The User. |
| 137 | 142 | */ |
| 138 | 143 | public static function get_by_resource( $resource ) { |
| 144 | + $resource = object_to_uri( $resource ); | |
| 145 | + | |
| 139 | 146 | $scheme = 'acct'; |
| 140 | 147 | $match = array(); |
| 141 | 148 | // try to extract the scheme and the host |
| 142 | 149 | if ( preg_match( '/^([a-zA-Z^:]+):(.*)$/i', $resource, $match ) ) { |
| 143 | 150 | // extract the scheme |
| 144 | - $scheme = esc_attr( $match[1] ); | |
| 151 | + $scheme = \esc_attr( $match[1] ); | |
| 145 | 152 | } |
| 146 | 153 | |
| 147 | 154 | switch ( $scheme ) { |
| 148 | 155 | // check for http(s) URIs |
| @@ -147,19 +154,26 @@ | ||
| 147 | 154 | switch ( $scheme ) { |
| 148 | 155 | // check for http(s) URIs |
| 149 | 156 | case 'http': |
| 150 | 157 | case 'https': |
| 151 | - $url_parts = wp_parse_url( $resource ); | |
| 158 | + $resource_path = \wp_parse_url( $resource, PHP_URL_PATH ); | |
| 152 | 159 | |
| 153 | - // check for http(s)://blog.example.com/@username | |
| 154 | - if ( | |
| 155 | - isset( $url_parts['path'] ) && | |
| 156 | - str_starts_with( $url_parts['path'], '/@' ) | |
| 157 | - ) { | |
| 158 | - $identifier = str_replace( '/@', '', $url_parts['path'] ); | |
| 159 | - $identifier = untrailingslashit( $identifier ); | |
| 160 | + if ( $resource_path ) { | |
| 161 | + $blog_path = \wp_parse_url( \home_url(), PHP_URL_PATH ); | |
| 160 | 162 | |
| 161 | - return self::get_by_username( $identifier ); | |
| 163 | + if ( $blog_path ) { | |
| 164 | + $resource_path = \str_replace( $blog_path, '', $resource_path ); | |
| 165 | + } | |
| 166 | + | |
| 167 | + $resource_path = \trim( $resource_path, '/' ); | |
| 168 | + | |
| 169 | + // check for http(s)://blog.example.com/@username | |
| 170 | + if ( str_starts_with( $resource_path, '@' ) ) { | |
| 171 | + $identifier = \str_replace( '@', '', $resource_path ); | |
| 172 | + $identifier = \trim( $identifier, '/' ); | |
| 173 | + | |
| 174 | + return self::get_by_username( $identifier ); | |
| 175 | + } | |
| 162 | 176 | } |
| 163 | 177 | |
| 164 | 178 | // check for http(s)://blog.example.com/author/username |
| 165 | 179 | $user_id = url_to_authorid( $resource ); |
| @@ -169,10 +183,10 @@ | ||
| 169 | 183 | } |
| 170 | 184 | |
| 171 | 185 | // check for http(s)://blog.example.com/ |
| 172 | 186 | if ( |
| 173 | - self::normalize_url( site_url() ) === self::normalize_url( $resource ) || | |
| 174 | - self::normalize_url( home_url() ) === self::normalize_url( $resource ) | |
| 187 | + normalize_url( site_url() ) === normalize_url( $resource ) || | |
| 188 | + normalize_url( home_url() ) === normalize_url( $resource ) | |
| 175 | 189 | ) { |
| 176 | 190 | return self::get_by_id( self::BLOG_USER_ID ); |
| 177 | 191 | } |
| 178 | 192 | |
| @@ -184,10 +198,10 @@ | ||
| 184 | 198 | // check for acct URIs |
| 185 | 199 | case 'acct': |
| 186 | 200 | $resource = \str_replace( 'acct:', '', $resource ); |
| 187 | 201 | $identifier = \substr( $resource, 0, \strrpos( $resource, '@' ) ); |
| 188 | - $host = self::normalize_host( \substr( \strrchr( $resource, '@' ), 1 ) ); | |
| 189 | - $blog_host = self::normalize_host( \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) ); | |
| 202 | + $host = normalize_host( \substr( \strrchr( $resource, '@' ), 1 ) ); | |
| 203 | + $blog_host = normalize_host( \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) ); | |
| 190 | 204 | |
| 191 | 205 | if ( $blog_host !== $host ) { |
| 192 | 206 | return new WP_Error( |
| 193 | 207 | 'activitypub_wrong_host', |
| @@ -218,47 +232,28 @@ | ||
| 218 | 232 | * |
| 219 | 233 | * @return \Acitvitypub\Model\User The User. |
| 220 | 234 | */ |
| 221 | 235 | public static function get_by_various( $id ) { |
| 236 | + $user = null; | |
| 237 | + | |
| 222 | 238 | if ( is_numeric( $id ) ) { |
| 223 | - return self::get_by_id( $id ); | |
| 239 | + $user = self::get_by_id( $id ); | |
| 224 | 240 | } elseif ( |
| 225 | 241 | // is URL |
| 226 | 242 | filter_var( $id, FILTER_VALIDATE_URL ) || |
| 227 | 243 | // is acct |
| 228 | - str_starts_with( $id, 'acct:' ) | |
| 244 | + str_starts_with( $id, 'acct:' ) || | |
| 245 | + // is email | |
| 246 | + filter_var( $id, FILTER_VALIDATE_EMAIL ) | |
| 229 | 247 | ) { |
| 230 | - return self::get_by_resource( $id ); | |
| 231 | - } else { | |
| 232 | - return self::get_by_username( $id ); | |
| 248 | + $user = self::get_by_resource( $id ); | |
| 233 | 249 | } |
| 234 | - } | |
| 235 | 250 | |
| 236 | - /** | |
| 237 | - * Normalize a host. | |
| 238 | - * | |
| 239 | - * @param string $host The host. | |
| 240 | - * | |
| 241 | - * @return string The normalized host. | |
| 242 | - */ | |
| 243 | - public static function normalize_host( $host ) { | |
| 244 | - return \str_replace( 'www.', '', $host ); | |
| 245 | - } | |
| 251 | + if ( $user && ! is_wp_error( $user ) ) { | |
| 252 | + return $user; | |
| 253 | + } | |
| 246 | 254 | |
| 247 | - /** | |
| 248 | - * Normalize a URL. | |
| 249 | - * | |
| 250 | - * @param string $url The URL. | |
| 251 | - * | |
| 252 | - * @return string The normalized URL. | |
| 253 | - */ | |
| 254 | - public static function normalize_url( $url ) { | |
| 255 | - $url = \untrailingslashit( $url ); | |
| 256 | - $url = \str_replace( 'https://', '', $url ); | |
| 257 | - $url = \str_replace( 'http://', '', $url ); | |
| 258 | - $url = \str_replace( 'www.', '', $url ); | |
| 259 | - | |
| 260 | - return $url; | |
| 255 | + return self::get_by_username( $id ); | |
| 261 | 256 | } |
| 262 | 257 | |
| 263 | 258 | /** |
| 264 | 259 | * Get the User collection. |
| @@ -267,9 +262,9 @@ | ||
| 267 | 262 | */ |
| 268 | 263 | public static function get_collection() { |
| 269 | 264 | $users = \get_users( |
| 270 | 265 | array( |
| 271 | - 'capability__in' => array( 'publish_posts' ), | |
| 266 | + 'capability__in' => array( 'activitypub' ), | |
| 272 | 267 | ) |
| 273 | 268 | ); |
| 274 | 269 | |
| 275 | 270 | $return = array(); |