|
1 | 1 | <?php |
2 | 2 | namespace Activitypub; |
3 | 3 |
|
| 4 | +use Activitypub\Rest\Webfinger; |
| 5 | + |
4 | 6 | /** |
5 | 7 | * ActivityPub Health_Check Class |
6 | 8 | * |
@@ -198,58 +200,37 @@ public static function is_author_url_accessible() { |
198 | 200 | * @return boolean|WP_Error |
199 | 201 | */ |
200 | 202 | public static function is_webfinger_endpoint_accessible() { |
201 | | - $user = \wp_get_current_user(); |
202 | | - $webfinger = \Activitypub\get_webfinger_resource( $user->ID ); |
203 | | - |
204 | | - $url = \wp_parse_url( \home_url(), \PHP_URL_SCHEME ) . '://' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); |
205 | | - |
206 | | - if ( \wp_parse_url( \home_url(), \PHP_URL_PORT ) ) { |
207 | | - $url .= ':' . \wp_parse_url( \home_url(), \PHP_URL_PORT ); |
208 | | - } |
209 | | - |
210 | | - $url = \trailingslashit( $url ) . '.well-known/webfinger'; |
| 203 | + $user = \wp_get_current_user(); |
| 204 | + $account = \Activitypub\get_webfinger_resource( $user->ID ); |
211 | 205 |
|
212 | | - $url = \add_query_arg( 'resource', 'acct:' . $webfinger, $url ); |
213 | | - |
214 | | - // try to access author URL |
215 | | - $response = \wp_remote_get( |
216 | | - $url, |
217 | | - array( |
218 | | - 'headers' => array( 'Accept' => 'application/activity+json' ), |
219 | | - 'redirection' => 0, |
220 | | - ) |
221 | | - ); |
222 | | - |
223 | | - if ( \is_wp_error( $response ) ) { |
224 | | - return new \WP_Error( |
225 | | - 'webfinger_url_not_accessible', |
226 | | - \sprintf( |
| 206 | + $url = Webfinger::resolve( $account ); |
| 207 | + if ( \is_wp_error( $url ) ) { |
| 208 | + $health_messages = array( |
| 209 | + 'webfinger_url_not_accessible' => \sprintf( |
227 | 210 | // translators: %s: Author URL |
228 | 211 | \__( |
229 | 212 | '<p>Your WebFinger endpoint <code>%s</code> is not accessible. Please check your WordPress setup or permalink structure.</p>', |
230 | 213 | 'activitypub' |
231 | 214 | ), |
232 | | - $url |
233 | | - ) |
234 | | - ); |
235 | | - } |
236 | | - |
237 | | - $response_code = \wp_remote_retrieve_response_code( $response ); |
238 | | - |
239 | | - // check if response is JSON |
240 | | - $body = \wp_remote_retrieve_body( $response ); |
241 | | - |
242 | | - if ( ! \is_string( $body ) || ! \is_array( \json_decode( $body, true ) ) ) { |
243 | | - return new \WP_Error( |
244 | | - 'webfinger_url_not_accessible', |
245 | | - \sprintf( |
| 215 | + $url->get_error_data() |
| 216 | + ), |
| 217 | + 'webfinger_url_invalid_response' => \sprintf( |
246 | 218 | // translators: %s: Author URL |
247 | 219 | \__( |
248 | 220 | '<p>Your WebFinger endpoint <code>%s</code> does not return valid JSON for <code>application/jrd+json</code>.</p>', |
249 | 221 | 'activitypub' |
250 | 222 | ), |
251 | | - $url |
252 | | - ) |
| 223 | + $url->get_error_data() |
| 224 | + ), |
| 225 | + ); |
| 226 | + $message = null; |
| 227 | + if ( isset( $health_messages[ $url->get_error_code() ] ) ) { |
| 228 | + $message = $health_messages[ $url->get_error_code() ]; |
| 229 | + } |
| 230 | + return new \WP_Error( |
| 231 | + $url->get_error_code(), |
| 232 | + $message, |
| 233 | + $url->get_error_data() |
253 | 234 | ); |
254 | 235 | } |
255 | 236 |
|
|
0 commit comments