Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.96% |
49 / 57 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Activity_Log_Bridge | |
89.09% |
49 / 55 |
|
50.00% |
1 / 2 |
9.11 | |
0.00% |
0 / 1 |
| register_routes | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
1 | |||
| get_activity_log | |
81.82% |
27 / 33 |
|
0.00% |
0 / 1 |
8.38 | |||
| 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 | // Bounds mirror what WPCOM's own `get_stream_args()` |
| 46 | // validate_callback enforces on `/activity/rewindable`, and |
| 47 | // the sibling `jetpack-activity-log` package's declaration. |
| 48 | // Declaring them here turns an out-of-range value into a 400 |
| 49 | // with a useful message instead of a round-trip that WPCOM |
| 50 | // rejects. |
| 51 | 'args' => array( |
| 52 | 'number' => array( |
| 53 | 'description' => __( 'Number of items to return per page.', 'jetpack-backup-pkg' ), |
| 54 | 'type' => 'integer', |
| 55 | 'minimum' => 1, |
| 56 | 'maximum' => 1000, |
| 57 | ), |
| 58 | 'page' => array( |
| 59 | 'description' => __( '1-indexed page number.', 'jetpack-backup-pkg' ), |
| 60 | 'type' => 'integer', |
| 61 | 'minimum' => 1, |
| 62 | ), |
| 63 | ), |
| 64 | ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Proxy the WPCOM rewindable activity log. |
| 70 | * |
| 71 | * Forwards `number` (per page) and `page` (1-indexed) so the dashboard |
| 72 | * paginates against the real server response — `totalItems` / |
| 73 | * `totalPages` come back in the WPCOM envelope and drive DataViews' |
| 74 | * pagination footer. Same param shape the `automattic/jetpack-activity-log` |
| 75 | * package uses against the parent `/sites/{id}/activity` endpoint. |
| 76 | * |
| 77 | * @param WP_REST_Request $request The REST request. |
| 78 | * @return \WP_REST_Response|WP_Error |
| 79 | */ |
| 80 | public static function get_activity_log( WP_REST_Request $request ) { |
| 81 | $blog_id = Rest_Controller::get_blog_id_or_error(); |
| 82 | if ( is_wp_error( $blog_id ) ) { |
| 83 | return $blog_id; |
| 84 | } |
| 85 | |
| 86 | $query = array_filter( |
| 87 | array( |
| 88 | 'number' => $request->get_param( 'number' ), |
| 89 | 'page' => $request->get_param( 'page' ), |
| 90 | // Activity summaries are rendered by WPCOM. Without this they |
| 91 | // come back in English whatever the reader's language is. |
| 92 | '_locale' => get_user_locale(), |
| 93 | ), |
| 94 | static function ( $value ) { |
| 95 | return null !== $value && '' !== $value; |
| 96 | } |
| 97 | ); |
| 98 | |
| 99 | $endpoint = sprintf( '/sites/%d/activity/rewindable', $blog_id ); |
| 100 | if ( ! empty( $query ) ) { |
| 101 | $endpoint .= '?' . http_build_query( $query ); |
| 102 | } |
| 103 | |
| 104 | $response = Client::wpcom_json_api_request_as_user( |
| 105 | $endpoint, |
| 106 | 'v2', |
| 107 | array(), |
| 108 | null, |
| 109 | 'wpcom' |
| 110 | ); |
| 111 | |
| 112 | if ( is_wp_error( $response ) ) { |
| 113 | return Rest_Controller::transport_error( $response, 'activity_log_fetch_failed' ); |
| 114 | } |
| 115 | |
| 116 | $status_code = wp_remote_retrieve_response_code( $response ); |
| 117 | if ( 200 !== $status_code ) { |
| 118 | return new WP_Error( |
| 119 | 'activity_log_fetch_failed', |
| 120 | __( 'Could not fetch the site activity log.', 'jetpack-backup-pkg' ), |
| 121 | array( 'status' => is_int( $status_code ) && $status_code > 0 ? $status_code : 500 ) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | return rest_ensure_response( json_decode( wp_remote_retrieve_body( $response ), true ) ); |
| 126 | } |
| 127 | } |