Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Optimize_LCP_Endpoint
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Automattic\Jetpack_Boost\Modules\Optimizations\Lcp;
4
5use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Action;
6use Automattic\Jetpack_Boost\Modules\Modules_Setup;
7
8class Optimize_LCP_Endpoint implements Data_Sync_Action {
9
10    /**
11     * Handles the optimize LCP action.
12     *
13     * @param mixed            $_data    JSON Data passed to the action.
14     * @param \WP_REST_Request $_request The request object.
15     */
16    public function handle( $_data, $_request ) {
17        // Check if the module is enabled first.
18        $states = ( new Modules_Setup() )->get_status();
19        if ( ! $states['lcp'] ) {
20            return array(
21                'success' => false,
22                'state'   => array(),
23            );
24        }
25
26        $analyzer = new LCP_Analyzer();
27        $state    = $analyzer->get_state();
28        if ( $state->is_pending() ) {
29            // If the analysis is already in progress, return the current state.
30            return array(
31                'success' => true,
32                'state'   => $state->get(),
33            );
34        }
35
36        $state = $analyzer->start();
37
38        return array(
39            'success' => true,
40            'state'   => $state,
41        );
42    }
43}