Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.18% covered (success)
93.18%
41 / 44
70.00% covered (warning)
70.00%
7 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Environment_Change_Detector
93.18% covered (success)
93.18%
41 / 44
70.00% covered (warning)
70.00%
7 / 10
20.13
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
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 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
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 is_post_type_invalidating
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 get_post_change_type
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
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    const ENV_CHANGE_STYLES_SAVED                   = 'styles_saved';
27
28    /**
29     * Post types that hold site-wide styles: Global Styles and Additional CSS.
30     */
31    const STYLE_POST_TYPES = array( 'wp_global_styles', 'custom_css' );
32
33    /**
34     * Initialize the change detection hooks.
35     */
36    public static function init() {
37        $object = new self();
38        $object->register_hooks();
39    }
40
41    public function register_hooks() {
42        add_action( 'after_switch_theme', array( $this, 'handle_theme_change' ) );
43        add_action( 'save_post', array( $this, 'handle_post_change' ), 10, 3 );
44        add_action( 'activated_plugin', array( $this, 'handle_plugin_change' ) );
45        add_action( 'deactivated_plugin', array( $this, 'handle_plugin_change' ) );
46    }
47
48    public function handle_post_change( $post_id, $post, $update = false ) {
49        // Ignore changes to any post which is not published.
50        if ( 'publish' !== $post->post_status ) {
51            return;
52        }
53
54        // Core inserts an empty Global Styles post the first time an editor loads, so only updates are saves.
55        if ( ! $update && 'wp_global_styles' === $post->post_type ) {
56            return;
57        }
58
59        // Ignore changes to post types which do not affect the front-end UI
60        if ( ! $this->is_post_type_invalidating( $post->post_type ) ) {
61            return;
62        }
63
64        $this->do_action( false, $this->get_post_change_type( $post ) );
65    }
66
67    public function handle_theme_change() {
68        $this->do_action( true, $this::ENV_CHANGE_SWITCHED_THEME );
69    }
70
71    public function handle_plugin_change() {
72        $this->do_action( false, $this::ENV_CHANGE_PLUGIN_CHANGE );
73    }
74
75    public function handle_cornerstone_pages_list_update() {
76        $this->do_action( false, $this::ENV_CHANGE_CORNERSTONE_PAGES_LIST_UPDATED );
77    }
78
79    /**
80     * Fire the environment change action.
81     *
82     * @param bool   $is_major_change Whether the change is such that we should stop serving existing critical CSS immediately unless refreshed.
83     * @param string $change_type The change type.
84     */
85    public function do_action( $is_major_change, $change_type ) {
86        do_action_deprecated(
87            'jetpack_boost_critical_css_environment_changed',
88            array( $is_major_change, $change_type ),
89            '3.20.0',
90            'jetpack_boost_environment_changed'
91        );
92
93        do_action( 'jetpack_boost_environment_changed', $is_major_change, $change_type );
94    }
95
96    public static function get_available_env_change_statuses() {
97        return array(
98            self::ENV_CHANGE_PAGE_SAVED,
99            self::ENV_CHANGE_POST_SAVED,
100            self::ENV_CHANGE_SWITCHED_THEME,
101            self::ENV_CHANGE_PLUGIN_CHANGE,
102            self::ENV_CHANGE_CORNERSTONE_PAGE_SAVED,
103            self::ENV_CHANGE_CORNERSTONE_PAGES_LIST_UPDATED,
104            self::ENV_CHANGE_STYLES_SAVED,
105        );
106    }
107
108    /**
109     * Given a post_type, return true if this post type affects the front end of
110     * the site - i.e.: should cause cached optimizations to be invalidated.
111     *
112     * @param string $post_type The post type to check
113     * @return bool             True if this post type affects the front end of the site.
114     */
115    private function is_post_type_invalidating( $post_type ) {
116        // Special cases: items which are not viewable, but affect the UI.
117        if ( in_array( $post_type, array( 'wp_template', 'wp_template_part' ), true ) || in_array( $post_type, self::STYLE_POST_TYPES, true ) ) {
118            return true;
119        }
120
121        if ( is_post_type_viewable( $post_type ) ) {
122            return true;
123        }
124    }
125
126    /**
127     * Get the type of change for a specific post.
128     *
129     * @param \WP_Post $post The post object.
130     * @return string The change type.
131     */
132    private function get_post_change_type( $post ) {
133        if ( in_array( $post->post_type, self::STYLE_POST_TYPES, true ) ) {
134            return $this::ENV_CHANGE_STYLES_SAVED;
135        }
136
137        if ( Cornerstone_Utils::is_cornerstone_page( $post->ID ) ) {
138            return $this::ENV_CHANGE_CORNERSTONE_PAGE_SAVED;
139        }
140
141        if ( 'page' === $post->post_type ) {
142            $change_type = $this::ENV_CHANGE_PAGE_SAVED;
143        } else {
144            $change_type = $this::ENV_CHANGE_POST_SAVED;
145        }
146
147        return $change_type;
148    }
149}