Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Generator | |
0.00% |
0 / 13 |
|
0.00% |
0 / 5 |
56 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| force_logged_out_render | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| is_generating_critical_css | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_generation_metadata | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| display_generate_meta | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Lib\Critical_CSS; |
| 4 | |
| 5 | use Automattic\Jetpack_Boost\Modules\Optimizations\Critical_CSS\CSS_Proxy; |
| 6 | |
| 7 | class Generator { |
| 8 | |
| 9 | const GENERATE_QUERY_ACTION = 'jb-generate-critical-css'; |
| 10 | |
| 11 | public static function init() { |
| 12 | $generator = new static(); |
| 13 | if ( static::is_generating_critical_css() ) { |
| 14 | add_action( 'wp_head', array( $generator, 'display_generate_meta' ), 0 ); |
| 15 | $generator->force_logged_out_render(); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Force the current page to render as viewed by a logged out user. Useful when generating |
| 21 | * Critical CSS. |
| 22 | */ |
| 23 | private function force_logged_out_render() { |
| 24 | $current_user_id = get_current_user_id(); |
| 25 | |
| 26 | if ( 0 !== $current_user_id ) { |
| 27 | // Force current user to 0 to ensure page is rendered as a non-logged-in user. |
| 28 | wp_set_current_user( 0 ); |
| 29 | |
| 30 | // Turn off display of admin bar. |
| 31 | add_filter( 'show_admin_bar', '__return_false', PHP_INT_MAX ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Return true if page is loaded to generate critical CSS |
| 37 | * |
| 38 | * phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 39 | */ |
| 40 | public static function is_generating_critical_css() { |
| 41 | return isset( $_GET[ self::GENERATE_QUERY_ACTION ] ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get a Critical CSS status block, adding in local generation nonces (if applicable). |
| 46 | * i.e.: Call this method to supply enough Critical CSS status to kick off local generation, |
| 47 | * such as in response to a request-generate API call or during page initialization. |
| 48 | */ |
| 49 | public function get_generation_metadata() { |
| 50 | $status = array(); |
| 51 | |
| 52 | // Add a user-bound nonce to use when proxying CSS for Critical CSS generation. |
| 53 | $status['proxy_nonce'] = wp_create_nonce( CSS_Proxy::NONCE_ACTION ); |
| 54 | |
| 55 | return $status; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Renders a <meta> tag used to verify this is a valid page to generate Critical CSS with. |
| 60 | */ |
| 61 | public function display_generate_meta() { |
| 62 | ?> |
| 63 | <meta name="<?php echo esc_attr( self::GENERATE_QUERY_ACTION ); ?>" content="true"/> |
| 64 | <?php |
| 65 | } |
| 66 | } |