| @@ -1,64 +1,30 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Users collection file. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub\Collection; |
| 3 | 9 | |
| 4 | -use WP_Error; | |
| 5 | -use WP_User_Query; | |
| 6 | -use Activitypub\Model\User; | |
| 7 | -use Activitypub\Model\Blog_User; | |
| 8 | -use Activitypub\Model\Application_User; | |
| 9 | - | |
| 10 | -use function Activitypub\url_to_authorid; | |
| 11 | -use function Activitypub\is_user_disabled; | |
| 12 | - | |
| 13 | -class Users { | |
| 10 | +/** | |
| 11 | + * Users collection. | |
| 12 | + * | |
| 13 | + * @deprecated version 4.2.0 | |
| 14 | + */ | |
| 15 | +class Users extends Actors { | |
| 14 | 16 | /** |
| 15 | - * The ID of the Blog User | |
| 17 | + * Get the User by ID. | |
| 16 | 18 | * |
| 17 | - * @var int | |
| 18 | - */ | |
| 19 | - const BLOG_USER_ID = 0; | |
| 20 | - | |
| 21 | - /** | |
| 22 | - * The ID of the Application User | |
| 23 | - * | |
| 24 | - * @var int | |
| 25 | - */ | |
| 26 | - const APPLICATION_USER_ID = -1; | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * Get the User by ID | |
| 30 | - * | |
| 31 | 19 | * @param int $user_id The User-ID. |
| 32 | 20 | * |
| 33 | - * @return \Acitvitypub\Model\User The User. | |
| 21 | + * @return User|Blog|Application|WP_Error The User or WP_Error if user not found. | |
| 34 | 22 | */ |
| 35 | 23 | public static function get_by_id( $user_id ) { |
| 36 | - if ( is_string( $user_id ) || is_numeric( $user_id ) ) { | |
| 37 | - $user_id = (int) $user_id; | |
| 38 | - } | |
| 24 | + _deprecated_function( __METHOD__, '4.2.0', 'Activitypub\Collection\Actors::get_by_id' ); | |
| 39 | 25 | |
| 40 | - if ( is_user_disabled( $user_id ) ) { | |
| 41 | - return new WP_Error( | |
| 42 | - 'activitypub_user_not_found', | |
| 43 | - \__( 'User not found', 'activitypub' ), | |
| 44 | - array( 'status' => 404 ) | |
| 45 | - ); | |
| 46 | - } | |
| 47 | - | |
| 48 | - if ( self::BLOG_USER_ID === $user_id ) { | |
| 49 | - return Blog_User::from_wp_user( $user_id ); | |
| 50 | - } elseif ( self::APPLICATION_USER_ID === $user_id ) { | |
| 51 | - return Application_User::from_wp_user( $user_id ); | |
| 52 | - } elseif ( $user_id > 0 ) { | |
| 53 | - return User::from_wp_user( $user_id ); | |
| 54 | - } | |
| 55 | - | |
| 56 | - return new WP_Error( | |
| 57 | - 'activitypub_user_not_found', | |
| 58 | - \__( 'User not found', 'activitypub' ), | |
| 59 | - array( 'status' => 404 ) | |
| 60 | - ); | |
| 26 | + return parent::get_by_id( $user_id ); | |
| 61 | 27 | } |
| 62 | 28 | |
| 63 | 29 | /** |
| 64 | 30 | * Get the User by username. |
| @@ -64,220 +30,49 @@ | ||
| 64 | 30 | * Get the User by username. |
| 65 | 31 | * |
| 66 | 32 | * @param string $username The User-Name. |
| 67 | 33 | * |
| 68 | - * @return \Acitvitypub\Model\User The User. | |
| 34 | + * @return User|Blog|Application|WP_Error The User or WP_Error if user not found. | |
| 69 | 35 | */ |
| 70 | 36 | public static function get_by_username( $username ) { |
| 71 | - // check for blog user. | |
| 72 | - if ( Blog_User::get_default_username() === $username ) { | |
| 73 | - return self::get_by_id( self::BLOG_USER_ID ); | |
| 74 | - } | |
| 37 | + _deprecated_function( __METHOD__, '4.2.0', 'Activitypub\Collection\Actors::get_by_username' ); | |
| 75 | 38 | |
| 76 | - if ( get_option( 'activitypub_blog_user_identifier' ) === $username ) { | |
| 77 | - return self::get_by_id( self::BLOG_USER_ID ); | |
| 78 | - } | |
| 79 | - | |
| 80 | - // check for application user. | |
| 81 | - if ( 'application' === $username ) { | |
| 82 | - return self::get_by_id( self::APPLICATION_USER_ID ); | |
| 83 | - } | |
| 84 | - | |
| 85 | - // check for 'activitypub_username' meta | |
| 86 | - $user = new WP_User_Query( | |
| 87 | - array( | |
| 88 | - 'number' => 1, | |
| 89 | - 'hide_empty' => true, | |
| 90 | - 'fields' => 'ID', | |
| 91 | - // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query | |
| 92 | - 'meta_query' => array( | |
| 93 | - 'relation' => 'OR', | |
| 94 | - array( | |
| 95 | - 'key' => 'activitypub_user_identifier', | |
| 96 | - 'value' => $username, | |
| 97 | - 'compare' => 'LIKE', | |
| 98 | - ), | |
| 99 | - ), | |
| 100 | - ) | |
| 101 | - ); | |
| 102 | - | |
| 103 | - if ( $user->results ) { | |
| 104 | - return self::get_by_id( $user->results[0] ); | |
| 105 | - } | |
| 106 | - | |
| 107 | - $username = str_replace( array( '*', '%' ), '', $username ); | |
| 108 | - | |
| 109 | - // check for login or nicename. | |
| 110 | - $user = new WP_User_Query( | |
| 111 | - array( | |
| 112 | - 'search' => $username, | |
| 113 | - 'search_columns' => array( 'user_login', 'user_nicename' ), | |
| 114 | - 'number' => 1, | |
| 115 | - 'hide_empty' => true, | |
| 116 | - 'fields' => 'ID', | |
| 117 | - ) | |
| 118 | - ); | |
| 119 | - | |
| 120 | - if ( $user->results ) { | |
| 121 | - return self::get_by_id( $user->results[0] ); | |
| 122 | - } | |
| 123 | - | |
| 124 | - return new WP_Error( | |
| 125 | - 'activitypub_user_not_found', | |
| 126 | - \__( 'User not found', 'activitypub' ), | |
| 127 | - array( 'status' => 404 ) | |
| 128 | - ); | |
| 39 | + return parent::get_by_username( $username ); | |
| 129 | 40 | } |
| 130 | 41 | |
| 131 | 42 | /** |
| 132 | 43 | * Get the User by resource. |
| 133 | 44 | * |
| 134 | - * @param string $resource The User-Resource. | |
| 45 | + * @param string $uri The User-Resource. | |
| 135 | 46 | * |
| 136 | - * @return \Acitvitypub\Model\User The User. | |
| 47 | + * @return User|WP_Error The User or WP_Error if user not found. | |
| 137 | 48 | */ |
| 138 | - public static function get_by_resource( $resource ) { | |
| 139 | - $scheme = 'acct'; | |
| 140 | - $match = array(); | |
| 141 | - // try to extract the scheme and the host | |
| 142 | - if ( preg_match( '/^([a-zA-Z^:]+):(.*)$/i', $resource, $match ) ) { | |
| 143 | - // extract the scheme | |
| 144 | - $scheme = esc_attr( $match[1] ); | |
| 145 | - } | |
| 49 | + public static function get_by_resource( $uri ) { | |
| 50 | + _deprecated_function( __METHOD__, '4.2.0', 'Activitypub\Collection\Actors::get_by_resource' ); | |
| 146 | 51 | |
| 147 | - switch ( $scheme ) { | |
| 148 | - // check for http(s) URIs | |
| 149 | - case 'http': | |
| 150 | - case 'https': | |
| 151 | - $url_parts = wp_parse_url( $resource ); | |
| 152 | - | |
| 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 | - | |
| 161 | - return self::get_by_username( $identifier ); | |
| 162 | - } | |
| 163 | - | |
| 164 | - // check for http(s)://blog.example.com/author/username | |
| 165 | - $user_id = url_to_authorid( $resource ); | |
| 166 | - | |
| 167 | - if ( $user_id ) { | |
| 168 | - return self::get_by_id( $user_id ); | |
| 169 | - } | |
| 170 | - | |
| 171 | - // check for http(s)://blog.example.com/ | |
| 172 | - if ( | |
| 173 | - self::normalize_url( site_url() ) === self::normalize_url( $resource ) || | |
| 174 | - self::normalize_url( home_url() ) === self::normalize_url( $resource ) | |
| 175 | - ) { | |
| 176 | - return self::get_by_id( self::BLOG_USER_ID ); | |
| 177 | - } | |
| 178 | - | |
| 179 | - return new WP_Error( | |
| 180 | - 'activitypub_no_user_found', | |
| 181 | - \__( 'User not found', 'activitypub' ), | |
| 182 | - array( 'status' => 404 ) | |
| 183 | - ); | |
| 184 | - // check for acct URIs | |
| 185 | - case 'acct': | |
| 186 | - $resource = \str_replace( 'acct:', '', $resource ); | |
| 187 | - $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 ) ); | |
| 190 | - | |
| 191 | - if ( $blog_host !== $host ) { | |
| 192 | - return new WP_Error( | |
| 193 | - 'activitypub_wrong_host', | |
| 194 | - \__( 'Resource host does not match blog host', 'activitypub' ), | |
| 195 | - array( 'status' => 404 ) | |
| 196 | - ); | |
| 197 | - } | |
| 198 | - | |
| 199 | - // prepare wildcards https://github.com/mastodon/mastodon/issues/22213 | |
| 200 | - if ( in_array( $identifier, array( '_', '*', '' ), true ) ) { | |
| 201 | - return self::get_by_id( self::BLOG_USER_ID ); | |
| 202 | - } | |
| 203 | - | |
| 204 | - return self::get_by_username( $identifier ); | |
| 205 | - default: | |
| 206 | - return new WP_Error( | |
| 207 | - 'activitypub_wrong_scheme', | |
| 208 | - \__( 'Wrong scheme', 'activitypub' ), | |
| 209 | - array( 'status' => 404 ) | |
| 210 | - ); | |
| 211 | - } | |
| 52 | + return parent::get_by_resource( $uri ); | |
| 212 | 53 | } |
| 213 | 54 | |
| 214 | 55 | /** |
| 215 | 56 | * Get the User by resource. |
| 216 | 57 | * |
| 217 | - * @param string $resource The User-Resource. | |
| 58 | + * @param string $id The User-Resource. | |
| 218 | 59 | * |
| 219 | - * @return \Acitvitypub\Model\User The User. | |
| 60 | + * @return User|Blog|Application|WP_Error The User or WP_Error if user not found. | |
| 220 | 61 | */ |
| 221 | 62 | public static function get_by_various( $id ) { |
| 222 | - if ( is_numeric( $id ) ) { | |
| 223 | - return self::get_by_id( $id ); | |
| 224 | - } elseif ( | |
| 225 | - // is URL | |
| 226 | - filter_var( $id, FILTER_VALIDATE_URL ) || | |
| 227 | - // is acct | |
| 228 | - str_starts_with( $id, 'acct:' ) | |
| 229 | - ) { | |
| 230 | - return self::get_by_resource( $id ); | |
| 231 | - } else { | |
| 232 | - return self::get_by_username( $id ); | |
| 233 | - } | |
| 234 | - } | |
| 63 | + _deprecated_function( __METHOD__, '4.2.0', 'Activitypub\Collection\Actors::get_by_various' ); | |
| 235 | 64 | |
| 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 ); | |
| 65 | + return parent::get_by_various( $id ); | |
| 245 | 66 | } |
| 246 | 67 | |
| 247 | 68 | /** |
| 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; | |
| 261 | - } | |
| 262 | - | |
| 263 | - /** | |
| 264 | 69 | * Get the User collection. |
| 265 | 70 | * |
| 266 | 71 | * @return array The User collection. |
| 267 | 72 | */ |
| 268 | 73 | public static function get_collection() { |
| 269 | - $users = \get_users( | |
| 270 | - array( | |
| 271 | - 'capability__in' => array( 'publish_posts' ), | |
| 272 | - ) | |
| 273 | - ); | |
| 74 | + _deprecated_function( __METHOD__, '4.2.0', 'Activitypub\Collection\Actors::get_collection' ); | |
| 274 | 75 | |
| 275 | - $return = array(); | |
| 276 | - | |
| 277 | - foreach ( $users as $user ) { | |
| 278 | - $return[] = User::from_wp_user( $user->ID ); | |
| 279 | - } | |
| 280 | - | |
| 281 | - return $return; | |
| 76 | + return parent::get_collection(); | |
| 282 | 77 | } |
| 283 | 78 | } |