Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
27.91% |
12 / 43 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Activity_Log_Bridge | |
29.27% |
12 / 41 |
|
50.00% |
1 / 2 |
37.66 | |
0.00% |
0 / 1 |
| register_routes | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| get_activity_log | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Activity log REST bridge — proxies /sites/{id}/activity/rewindable. |
| 4 | * |
| 5 | * @package automattic/jetpack-backup-plugin |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Backup\V0005\REST; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use WP_Error; |
| 12 | use WP_REST_Request; |
| 13 | use WP_REST_Server; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit( 0 ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Powers the modernized Overview's activity list. Forwards the raw |
| 21 | * WPCOM rewindable-activity response; the React layer's |
| 22 | * `normalize/activity-log.ts` converts entries to `ActivityItem` shape. |
| 23 | */ |
| 24 | class Activity_Log_Bridge { |
| 25 | |
| 26 | /** |
| 27 | * Register routes. |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public static function register_routes() { |
| 32 | // NOTE: not `/activity-log` — that namespace is owned by the |
| 33 | // `automattic/jetpack-activity-log` package's REST controller, |
| 34 | // which is loaded inside the Jetpack plugin. Sharing the route |
| 35 | // would let `register_rest_route`'s endpoint-merge pick the |
| 36 | // wrong handler at dispatch time (the two bridges proxy |
| 37 | // different WPCOM endpoints with different response shapes). |
| 38 | register_rest_route( |
| 39 | 'jetpack/v4', |
| 40 | '/site/rewindable-activity', |
| 41 | array( |
| 42 | 'methods' => WP_REST_Server::READABLE, |
| 43 | 'callback' => array( __CLASS__, 'get_activity_log' ), |
| 44 | 'permission_callback' => array( Rest_Controller::class, 'permission_check' ), |
| 45 | 'args' => array( |
| 46 | 'number' => array( 'type' => 'integer' ), |
| 47 | ), |
| 48 | ) |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Proxy the WPCOM rewindable activity log. |
| 54 | * |
| 55 | * @param WP_REST_Request $request The REST request. |
| 56 | * @return \WP_REST_Response|WP_Error |
| 57 | */ |
| 58 | public static function get_activity_log( WP_REST_Request $request ) { |
| 59 | $blog_id = Rest_Controller::get_blog_id_or_error(); |
| 60 | if ( is_wp_error( $blog_id ) ) { |
| 61 | return $blog_id; |
| 62 | } |
| 63 | |
| 64 | $query = array_filter( |
| 65 | array( 'number' => $request->get_param( 'number' ) ), |
| 66 | static function ( $value ) { |
| 67 | return null !== $value && '' !== $value; |
| 68 | } |
| 69 | ); |
| 70 | |
| 71 | $endpoint = sprintf( '/sites/%d/activity/rewindable', $blog_id ); |
| 72 | if ( ! empty( $query ) ) { |
| 73 | $endpoint .= '?' . http_build_query( $query ); |
| 74 | } |
| 75 | |
| 76 | $response = Client::wpcom_json_api_request_as_user( |
| 77 | $endpoint, |
| 78 | 'v2', |
| 79 | array(), |
| 80 | null, |
| 81 | 'wpcom' |
| 82 | ); |
| 83 | |
| 84 | if ( is_wp_error( $response ) ) { |
| 85 | return $response; |
| 86 | } |
| 87 | |
| 88 | $status_code = wp_remote_retrieve_response_code( $response ); |
| 89 | if ( 200 !== $status_code ) { |
| 90 | return new WP_Error( |
| 91 | 'activity_log_fetch_failed', |
| 92 | __( 'Could not fetch the site activity log.', 'jetpack-backup-pkg' ), |
| 93 | array( 'status' => is_int( $status_code ) && $status_code > 0 ? $status_code : 500 ) |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | return rest_ensure_response( json_decode( wp_remote_retrieve_body( $response ), true ) ); |
| 98 | } |
| 99 | } |