Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| LCP_Analyzer | |
0.00% |
0 / 37 |
|
0.00% |
0 / 7 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| start | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| start_partial_analysis | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| get_cornerstone_pages | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| analyze_pages | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| get_state | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_storage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Modules\Optimizations\Lcp; |
| 4 | |
| 5 | use Automattic\Jetpack\Boost_Core\Lib\Boost_API; |
| 6 | use Automattic\Jetpack_Boost\Lib\Cornerstone\Cornerstone_Utils; |
| 7 | |
| 8 | class LCP_Analyzer { |
| 9 | /** @var LCP_State */ |
| 10 | private $state; |
| 11 | |
| 12 | /** @var LCP_Storage */ |
| 13 | private $storage; |
| 14 | |
| 15 | /** |
| 16 | * @since 4.0.0 |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | $this->state = new LCP_State(); |
| 20 | $this->storage = new LCP_Storage(); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Start the LCP analysis process |
| 25 | * |
| 26 | * @return array The current state data |
| 27 | */ |
| 28 | public function start() { |
| 29 | // Get cornerstone pages to analyze |
| 30 | $pages = $this->get_cornerstone_pages(); |
| 31 | |
| 32 | // Store those pages in the LCP State |
| 33 | $this->state |
| 34 | ->prepare_request() |
| 35 | ->set_pages( $pages ) |
| 36 | ->set_pending_pages( $pages ) |
| 37 | ->save(); |
| 38 | |
| 39 | // Get the data |
| 40 | $data = $this->state->get(); |
| 41 | |
| 42 | // Start the analysis process |
| 43 | $this->analyze_pages( $pages ); |
| 44 | |
| 45 | // Clear previous LCP analysis data from storage |
| 46 | $this->storage->clear(); |
| 47 | |
| 48 | return $data; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Start a partial analysis for the given pages. |
| 53 | * Sets status to pending and updates only the pages that are in the $pages array. |
| 54 | * Pages that are not in the $pages array will not be updated. |
| 55 | * |
| 56 | * @param array $pages The pages to analyze. |
| 57 | * @return array The current state data |
| 58 | */ |
| 59 | public function start_partial_analysis( $pages ) { |
| 60 | $current_pages = $this->state->get_pages(); |
| 61 | |
| 62 | $this->state |
| 63 | ->prepare_request() |
| 64 | ->set_pages( $current_pages ) |
| 65 | ->set_pending_pages( $pages ) |
| 66 | ->save(); |
| 67 | |
| 68 | $this->analyze_pages( $pages ); |
| 69 | |
| 70 | // @todo - Clear previous LCP analysis data from storage for pages in the $pages array. |
| 71 | // We currently don't have a way to do this. |
| 72 | |
| 73 | return $this->state->get(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get cornerstone pages for analysis |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | protected function get_cornerstone_pages() { |
| 82 | $pages = array(); |
| 83 | $cornerstone_pages_list = Cornerstone_Utils::get_list(); |
| 84 | |
| 85 | foreach ( $cornerstone_pages_list as $url ) { |
| 86 | $pages[] = Cornerstone_Utils::prepare_provider_data( $url ); |
| 87 | } |
| 88 | |
| 89 | return $pages; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Run analysis for the given pages |
| 94 | * |
| 95 | * @param array $pages Pages to analyze |
| 96 | */ |
| 97 | private function analyze_pages( $pages ) { |
| 98 | $payload = array( |
| 99 | 'pages' => $pages, |
| 100 | 'requestId' => md5( |
| 101 | wp_json_encode( |
| 102 | $pages, |
| 103 | 0 // phpcs:ignore Jetpack.Functions.JsonEncodeFlags.ZeroFound -- No `json_encode()` flags because this needs to match whatever is calculating the hash on the other end. |
| 104 | ) |
| 105 | ), |
| 106 | ); |
| 107 | return Boost_API::post( 'lcp', $payload ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the current state |
| 112 | * |
| 113 | * @return LCP_State |
| 114 | */ |
| 115 | public function get_state() { |
| 116 | return $this->state; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the storage instance |
| 121 | * |
| 122 | * @return LCP_Storage |
| 123 | */ |
| 124 | public function get_storage() { |
| 125 | return $this->storage; |
| 126 | } |
| 127 | } |