| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Log requests to the WordPress.org Events API and their responses. |
|---|
| 5 | * |
|---|
| 6 | * @param array|WP_Error $response HTTP response or WP_Error object. |
|---|
| 7 | * @param string $context Context under which the hook is fired. |
|---|
| 8 | * @param string $transport HTTP transport used. |
|---|
| 9 | * @param array $request_args HTTP request arguments. |
|---|
| 10 | * @param string $request_url The request URL. |
|---|
| 11 | */ |
|---|
| 12 | function debug_community_events_response( $response, $context, $transport, $request_args, $request_url ) { |
|---|
| 13 | if ( false === strpos( $request_url, 'api.wordpress.org/events' ) ) { |
|---|
| 14 | return; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | $response_code = wp_remote_retrieve_response_code( $response ); |
|---|
| 18 | $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|---|
| 19 | $response_error = null; |
|---|
| 20 | |
|---|
| 21 | // Avoid bloating the log with all the event data, but keep the count. |
|---|
| 22 | if ( isset( $response_body['events'] ) ) { |
|---|
| 23 | $response_body['events'] = count( $response_body['events'] ) . ' events trimmed.'; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | $message = is_wp_error( $response ) ? $response->get_error_message() : 'Valid response received'; |
|---|
| 27 | $request_args = $request_args['body']; |
|---|
| 28 | $details = compact( 'request_url', 'request_args', 'response_code', 'response_body' ); |
|---|
| 29 | |
|---|
| 30 | error_log( sprintf( |
|---|
| 31 | '%s: %s. Details: %s', |
|---|
| 32 | __METHOD__, |
|---|
| 33 | trim( $message, '.' ), |
|---|
| 34 | wp_json_encode( $details ) |
|---|
| 35 | ) ); |
|---|
| 36 | } |
|---|
| 37 | add_action( 'http_api_debug', 'debug_community_events_response', 10, 5 ); |
|---|