Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.90% covered (danger)
35.90%
14 / 39
40.00% covered (danger)
40.00%
4 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Environment_Change_Detector
35.90% covered (danger)
35.90%
14 / 39
40.00% covered (danger)
40.00%
4 / 10
83.43
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 register_hooks
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 handle_post_change
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 handle_theme_change
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle_plugin_change
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle_cornerstone_pages_list_update
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 do_action
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 get_available_env_change_statuses
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 is_post_type_invalidating
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 get_post_change_type
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Environment Change Detector class.
4 *
5 * @link       https://automattic.com
6 * @since      1.0.0
7 * @package    automattic/jetpack-boost
8 */
9
10namespace Automattic\Jetpack_Boost\Lib;
11
12use Automattic\Jetpack_Boost\Lib\Cornerstone\Cornerstone_Utils;
13
14/**
15 * Class Environment_Change_Detector
16 */
17class Environment_Change_Detector {
18
19    const ENV_CHANGE_LEGACY                         = '1';
20    const ENV_CHANGE_PAGE_SAVED                     = 'page_saved';
21    const ENV_CHANGE_POST_SAVED                     = 'post_saved';
22    const ENV_CHANGE_SWITCHED_THEME                 = 'switched_theme';
23    const ENV_CHANGE_PLUGIN_CHANGE                  = 'plugin_change';
24    const ENV_CHANGE_CORNERSTONE_PAGE_SAVED         = 'cornerstone_page_saved';
25    const ENV_CHANGE_CORNERSTONE_PAGES_LIST_UPDATED = 'cornerstone_pages_list_updated';
26
27    /**
28     * Initialize the change detection hooks.
29     */
30    public static function init() {
31        $object = new self();
32        $object->register_hooks();
33    }
34
35    public function register_hooks() {
36        add_action( 'after_switch_theme', array( $this, 'handle_theme_change' ) );
37        add_action( 'save_post', array( $this, 'handle_post_change' ), 10, 2 );
38        add_action( 'activated_plugin', array( $this, 'handle_plugin_change' ) );
39        add_action( 'deactivated_plugin', array( $this, 'handle_plugin_change' ) );
40    }
41
42    public function handle_post_change( $post_id, $post ) {
43        // Ignore changes to any post which is not published.
44        if ( 'publish' !== $post->post_status ) {
45            return;
46        }
47
48        // Ignore changes to post types which do not affect the front-end UI
49        if ( ! $this->is_post_type_invalidating( $post->post_type ) ) {
50            return;
51        }
52
53        $this->do_action( false, $this->get_post_change_type( $post ) );
54    }
55
56    public function handle_theme_change() {
57        $this->do_action( true, $this::ENV_CHANGE_SWITCHED_THEME );
58    }
59
60    public function handle_plugin_change() {
61        $this->do_action( false, $this::ENV_CHANGE_PLUGIN_CHANGE );
62    }
63
64    public function handle_cornerstone_pages_list_update() {
65        $this->do_action( false, $this::ENV_CHANGE_CORNERSTONE_PAGES_LIST_UPDATED );
66    }
67
68    /**
69     * Fire the environment change action.
70     *
71     * @param bool   $is_major_change Whether the change is such that we should stop serving existing critical CSS immediately unless refreshed.
72     * @param string $change_type The change type.
73     */
74    public function do_action( $is_major_change, $change_type ) {
75        do_action_deprecated(
76            'jetpack_boost_critical_css_environment_changed',
77            array( $is_major_change, $change_type ),
78            '3.20.0',
79            'jetpack_boost_environment_changed'
80        );
81
82        do_action( 'jetpack_boost_environment_changed', $is_major_change, $change_type );
83    }
84
85    public static function get_available_env_change_statuses() {
86        return array(
87            self::ENV_CHANGE_PAGE_SAVED,
88            self::ENV_CHANGE_POST_SAVED,
89            self::ENV_CHANGE_SWITCHED_THEME,
90            self::ENV_CHANGE_PLUGIN_CHANGE,
91            self::ENV_CHANGE_CORNERSTONE_PAGE_SAVED,
92            self::ENV_CHANGE_CORNERSTONE_PAGES_LIST_UPDATED,
93        );
94    }
95
96    /**
97     * Given a post_type, return true if this post type affects the front end of
98     * the site - i.e.: should cause cached optimizations to be invalidated.
99     *
100     * @param string $post_type The post type to check
101     * @return bool             True if this post type affects the front end of the site.
102     */
103    private function is_post_type_invalidating( $post_type ) {
104        // Special cases: items which are not viewable, but affect the UI.
105        if ( in_array( $post_type, array( 'wp_template', 'wp_template_part' ), true ) ) {
106            return true;
107        }
108
109        if ( is_post_type_viewable( $post_type ) ) {
110            return true;
111        }
112    }
113
114    /**
115     * Get the type of change for a specific post.
116     *
117     * @param \WP_Post $post The post object.
118     * @return string The change type.
119     */
120    private function get_post_change_type( $post ) {
121        if ( Cornerstone_Utils::is_cornerstone_page( $post->ID ) ) {
122            return $this::ENV_CHANGE_CORNERSTONE_PAGE_SAVED;
123        }
124
125        if ( 'page' === $post->post_type ) {
126            $change_type = $this::ENV_CHANGE_PAGE_SAVED;
127        } else {
128            $change_type = $this::ENV_CHANGE_POST_SAVED;
129        }
130
131        return $change_type;
132    }
133}