Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
10.71% |
3 / 28 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Update_LCP | |
10.71% |
3 / 28 |
|
25.00% |
1 / 4 |
175.15 | |
0.00% |
0 / 1 |
| name | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| request_methods | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| response | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
156 | |||
| permissions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Save generated LCP data. |
| 4 | * |
| 5 | * This endpoint is used by WP.com to push the generated LCP data to the boost plugin. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack_Boost\REST_API\Endpoints; |
| 9 | |
| 10 | use Automattic\Jetpack_Boost\Modules\Optimizations\Lcp\LCP_State; |
| 11 | use Automattic\Jetpack_Boost\Modules\Optimizations\Lcp\LCP_Storage; |
| 12 | use Automattic\Jetpack_Boost\REST_API\Contracts\Endpoint; |
| 13 | use Automattic\Jetpack_Boost\REST_API\Permissions\Signed_With_Blog_Token; |
| 14 | use WP_REST_Server; |
| 15 | |
| 16 | /** |
| 17 | * Handler for POST lcp/update. Expects the following body params: |
| 18 | * - success: boolean - False if the whole LCP job failed. |
| 19 | * - message: string - Error message if success is false. |
| 20 | * - data: object - All results from the LCP job: |
| 21 | * |
| 22 | * Each data key contains: |
| 23 | * - key: string - The key of the page. |
| 24 | * - url: string - The URL of the page. |
| 25 | * - devices: object - The LCP data for both mobile and desktop. |
| 26 | * |
| 27 | * Each device key contains: |
| 28 | * - success: boolean - False if this device key failed. |
| 29 | * - element: string - The selector of the LCP element. |
| 30 | * - type: string - The type of the LCP element. Either 'img' or 'background-image'. |
| 31 | * - url: string - Only for 'img' elements. The URL of LCP element. |
| 32 | * - html: string - The HTML of the LCP element. |
| 33 | * - report: object - The full report of the LCP element. |
| 34 | */ |
| 35 | class Update_LCP implements Endpoint { |
| 36 | |
| 37 | public function name() { |
| 38 | return 'lcp/update'; |
| 39 | } |
| 40 | |
| 41 | public function request_methods() { |
| 42 | return WP_REST_Server::EDITABLE; |
| 43 | } |
| 44 | |
| 45 | public function response( $request ) { |
| 46 | $state = new LCP_State(); |
| 47 | $storage = new LCP_Storage(); |
| 48 | $params = $request->get_params(); |
| 49 | $pages = empty( $params['data'] ) || ! is_array( $params['data'] ) ? array() : $params['data']; |
| 50 | $api_successful = array( 'success' => true ); |
| 51 | |
| 52 | // If success is false, the whole LCP generation process failed. |
| 53 | if ( empty( $params['success'] ) ) { |
| 54 | if ( empty( $params['message'] ) || ! is_string( $params['message'] ) ) { |
| 55 | $error = __( 'An unknown error occurred', 'jetpack-boost' ); |
| 56 | } else { |
| 57 | $error = $params['message']; |
| 58 | } |
| 59 | |
| 60 | $state->set_error( $error ); |
| 61 | $state->save(); |
| 62 | |
| 63 | return $api_successful; |
| 64 | } |
| 65 | |
| 66 | foreach ( $pages as $entry ) { |
| 67 | if ( $entry['success'] ) { |
| 68 | $state->set_page_success( $entry['key'] ); |
| 69 | } else { |
| 70 | $errors = array(); |
| 71 | foreach ( $entry['reports'] as $report ) { |
| 72 | if ( isset( $report['success'] ) && false === $report['success'] && ! empty( $report['data'] ) ) { |
| 73 | $errors[] = $report['data']; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | $state->set_page_errors( $entry['key'], $errors ); |
| 78 | } |
| 79 | |
| 80 | // Store the LCP data for this page. |
| 81 | $storage->store_lcp( $entry['key'], $entry['reports'] ); |
| 82 | |
| 83 | // Failures must have an array of urls. |
| 84 | // @TODO: figure out what to do with failures. |
| 85 | } |
| 86 | |
| 87 | // Save the state changes. |
| 88 | $state->save(); |
| 89 | |
| 90 | return $api_successful; |
| 91 | } |
| 92 | |
| 93 | public function permissions() { |
| 94 | return array( |
| 95 | new Signed_With_Blog_Token(), |
| 96 | ); |
| 97 | } |
| 98 | } |