Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
LCP_Invalidator
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 reset_data
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 reset_and_analyze
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 handle_environment_change
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 handle_post_update
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * LCP Invalidator
4 *
5 * Reset LCP analysis data on certain events.
6 */
7
8namespace Automattic\Jetpack_Boost\Modules\Optimizations\Lcp;
9
10use Automattic\Jetpack_Boost\Lib\Cornerstone\Cornerstone_Utils;
11
12class LCP_Invalidator {
13
14    public static function init() {
15        add_action( 'jetpack_boost_deactivate', array( self::class, 'reset_data' ) );
16        add_action( 'update_option_jetpack_boost_ds_cornerstone_pages_list', array( self::class, 'reset_and_analyze' ) );
17        add_action( 'jetpack_boost_environment_changed', array( self::class, 'handle_environment_change' ) );
18        add_action( 'post_updated', array( self::class, 'handle_post_update' ) );
19    }
20
21    /**
22     * Reset any LCP analysis data (state and storage).
23     *
24     * @since 4.0.0
25     */
26    public static function reset_data() {
27        $state = new LCP_State();
28        $state->clear();
29
30        $storage = new LCP_Storage();
31        $storage->clear();
32    }
33
34    /**
35     * Reset the LCP analysis data, and analyze the pages again.
36     *
37     * @since 4.0.0
38     */
39    public static function reset_and_analyze() {
40        self::reset_data();
41
42        /**
43         * Indicate that the latest LCP analysis data has been invalidated.
44         */
45        do_action( 'jetpack_boost_lcp_invalidated' );
46    }
47
48    /**
49     * Respond to environment changes; deciding whether or not to clear LCP analysis data.
50     *
51     * @since 4.0.0
52     */
53    public static function handle_environment_change( $is_major_change ) {
54        if ( $is_major_change ) {
55            self::reset_and_analyze();
56        }
57    }
58
59    /**
60     * Handle post updates to check if the post is a cornerstone page and schedule preload if needed.
61     *
62     * @since 4.0.0
63     * @param int $post_id The ID of the post being updated.
64     * @return void
65     */
66    public static function handle_post_update( int $post_id ) {
67        if ( Cornerstone_Utils::is_cornerstone_page( $post_id ) ) {
68            $url = get_permalink( $post_id );
69
70            $analyzer = new LCP_Analyzer();
71            $analyzer->start_partial_analysis(
72                array(
73                    Cornerstone_Utils::prepare_provider_data( $url ),
74                )
75            );
76        }
77    }
78}