Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.00% covered (danger)
8.00%
2 / 25
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Performance_History_Entry
8.00% covered (danger)
8.00%
2 / 25
33.33% covered (danger)
33.33%
1 / 3
34.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
20
 set
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Automattic\Jetpack_Boost\Data_Sync;
3
4use Automattic\Jetpack\Boost_Speed_Score\Speed_Score_Graph_History_Request;
5use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get;
6use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Set;
7use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Lazy_Entry;
8
9class Performance_History_Entry implements Lazy_Entry, Entry_Can_Get, Entry_Can_Set {
10    private $start_date;
11    private $end_date;
12
13    public function __construct() {
14        // Default to the last 30 days
15        $this->start_date = ( time() - 60 * 60 * 24 * 30 ) * 1000;
16        $this->end_date   = time() * 1000;
17    }
18
19    public function get( $_fallback = false ) {
20        $request = new Speed_Score_Graph_History_Request( $this->start_date, $this->end_date, array() );
21        $result  = $request->execute();
22
23        if ( is_wp_error( $result ) || empty( $result['data'] ) ) {
24            return array(
25                'startDate'   => $this->start_date,
26                'endDate'     => $this->end_date,
27                'periods'     => array(),
28                'annotations' => array(),
29            );
30        }
31
32        $annotations = $result['data']['annotations'] ?? array();
33        // Sanitize the annotations
34        foreach ( $annotations as $key => $annotation ) {
35            $annotations[ $key ] = array(
36                'timestamp' => $annotation['timestamp'],
37                'text'      => wp_kses_post( $annotation['text'] ),
38            );
39        }
40
41        return array(
42            'startDate'   => $result['data']['_meta']['start'],
43            'endDate'     => $result['data']['_meta']['end'],
44            'periods'     => $result['data']['periods'],
45            'annotations' => $annotations,
46        );
47    }
48
49    public function set( $value ) {
50        $this->start_date = $value['startDate'];
51        $this->end_date   = $value['endDate'];
52    }
53}