Skip to content

Commit

Permalink
Boost: Add support for jb-generate-critical-css for cloud CSS gener…
Browse files Browse the repository at this point in the history
…ator (#35129)

* Remove unused code from generator

* Extract generator to outside of local C.CSS

* Add support for `jb-generate-critical-css` param in cloud CSS

* changelog
  • Loading branch information
haqadn authored Jan 22, 2024
1 parent 56b1c6d commit ca61b51
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Automattic\Jetpack_Boost\Data_Sync;

use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get;
use Automattic\Jetpack_Boost\Modules\Optimizations\Critical_CSS\Generator;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Generator;

class Critical_CSS_Meta_Entry implements Entry_Can_Get {
public function get() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
<?php

namespace Automattic\Jetpack_Boost\Modules\Optimizations\Critical_CSS;
namespace Automattic\Jetpack_Boost\Lib\Critical_CSS;

use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Source_Providers;
use Automattic\Jetpack_Boost\Modules\Optimizations\Critical_CSS\CSS_Proxy;

class Generator {

const GENERATE_QUERY_ACTION = 'jb-generate-critical-css';
const CSS_CALLBACK_ACTION = 'jb-critical-css-callback';

private $paths;
public static function init() {
$generator = new static();
if ( static::is_generating_critical_css() ) {
add_action( 'wp_head', array( $generator, 'display_generate_meta' ), 0 );
$generator->force_logged_out_render();
}
}

/**
* Force the current page to render as viewed by a logged out user. Useful when generating
* Critical CSS.
*/
private function force_logged_out_render() {
$current_user_id = get_current_user_id();

if ( 0 !== $current_user_id ) {
// Force current user to 0 to ensure page is rendered as a non-logged-in user.
wp_set_current_user( 0 );

public function __construct() {
$this->paths = new Source_Providers();
// Turn off display of admin bar.
add_filter( 'show_admin_bar', '__return_false', PHP_INT_MAX );
}
}

/**
Expand All @@ -38,6 +55,15 @@ public function get_generation_metadata() {
return $status;
}

/**
* Renders a <meta> tag used to verify this is a valid page to generate Critical CSS with.
*/
public function display_generate_meta() {
?>
<meta name="<?php echo esc_attr( self::GENERATE_QUERY_ACTION ); ?>" content="true"/>
<?php
}

/**
* Add the critical css generation flag to a list if it's present in the URL.
* This is mainly used by filters for compatibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_State;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_Storage;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Display_Critical_CSS;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Generator;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Source_Providers;
use Automattic\Jetpack_Boost\Lib\Premium_Features;
use Automattic\Jetpack_Boost\REST_API\Contracts\Has_Endpoints;
Expand Down Expand Up @@ -40,7 +41,9 @@ public function setup() {
add_action( 'save_post', array( $this, 'handle_save_post' ), 10, 2 );
add_filter( 'jetpack_boost_total_problem_count', array( $this, 'update_total_problem_count' ) );
add_filter( 'critical_css_invalidated', array( $this, 'handle_critical_css_invalidated' ) );
add_filter( 'query_vars', array( '\Automattic\Jetpack_Boost\Lib\Critical_CSS\Generator', 'add_generate_query_action_to_list' ) );

Generator::init();
Critical_CSS_Invalidator::init();
Cloud_CSS_Followup::init();

Expand Down Expand Up @@ -73,6 +76,11 @@ public function display_critical_css() {
return;
}

// Don't display Critical CSS, if current page load is by the Critical CSS generator.
if ( Generator::is_generating_critical_css() ) {
return;
}

// Get the Critical CSS to show.
$critical_css = $this->paths->get_current_request_css();
if ( ! $critical_css ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_State;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_Storage;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Display_Critical_CSS;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Generator;
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Source_Providers;
use Automattic\Jetpack_Boost\Lib\Premium_Features;

Expand Down Expand Up @@ -46,13 +47,9 @@ public static function is_available() {
public function setup() {
add_action( 'wp', array( $this, 'display_critical_css' ) );
add_filter( 'jetpack_boost_total_problem_count', array( $this, 'update_total_problem_count' ) );
add_filter( 'query_vars', array( '\Automattic\Jetpack_Boost\Modules\Optimizations\Critical_CSS\Generator', 'add_generate_query_action_to_list' ) );

if ( Generator::is_generating_critical_css() ) {
add_action( 'wp_head', array( $this, 'display_generate_meta' ), 0 );
$this->force_logged_out_render();
}
add_filter( 'query_vars', array( '\Automattic\Jetpack_Boost\Lib\Critical_CSS\Generator', 'add_generate_query_action_to_list' ) );

Generator::init();
Critical_CSS_Invalidator::init();
CSS_Proxy::init();

Expand All @@ -66,15 +63,6 @@ public static function get_slug() {
return 'critical_css';
}

/**
* Renders a <meta> tag used to verify this is a valid page to generate Critical CSS with.
*/
public function display_generate_meta() {
?>
<meta name="<?php echo esc_attr( Generator::GENERATE_QUERY_ACTION ); ?>" content="true"/>
<?php
}

public function display_critical_css() {
// Don't look for Critical CSS in the dashboard.
if ( is_admin() ) {
Expand Down Expand Up @@ -105,22 +93,6 @@ public function display_critical_css() {
Admin_Bar_Compatibility::init();
}

/**
* Force the current page to render as viewed by a logged out user. Useful when generating
* Critical CSS.
*/
private function force_logged_out_render() {
$current_user_id = get_current_user_id();

if ( 0 !== $current_user_id ) {
// Force current user to 0 to ensure page is rendered as a non-logged-in user.
wp_set_current_user( 0 );

// Turn off display of admin bar.
add_filter( 'show_admin_bar', '__return_false', PHP_INT_MAX );
}
}

public function update_total_problem_count( $count ) {
return ( new Critical_CSS_State() )->has_errors() ? ++$count : $count;
}
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/boost/changelog/update-ccss-generator
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Cloud CSS: Added custom query parameter support for more reliable cloud CSS generation
2 changes: 1 addition & 1 deletion projects/plugins/boost/compatibility/aioseo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

// Add the Critical CSS generation query arg to the list of allowed query args of All in One SEO.
// This prevents All in One SEO from removing the query arg, which breaks Critical CSS generation.
add_filter( 'aioseo_unrecognized_allowed_query_args', array( '\Automattic\Jetpack_Boost\Modules\Optimizations\Critical_CSS\Generator', 'add_generate_query_action_to_list' ) );
add_filter( 'aioseo_unrecognized_allowed_query_args', array( '\Automattic\Jetpack_Boost\Lib\Critical_CSS\Generator', 'add_generate_query_action_to_list' ) );
2 changes: 1 addition & 1 deletion projects/plugins/boost/compatibility/yoast.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

// Add the Critical CSS generation query arg to Yoast's allowed query args list.
// This prevents Yoast from removing the query arg, which breaks Critical CSS generation.
add_filter( 'Yoast\WP\SEO\allowlist_permalink_vars', array( '\Automattic\Jetpack_Boost\Modules\Optimizations\Critical_CSS\Generator', 'add_generate_query_action_to_list' ) );
add_filter( 'Yoast\WP\SEO\allowlist_permalink_vars', array( '\Automattic\Jetpack_Boost\Lib\Critical_CSS\Generator', 'add_generate_query_action_to_list' ) );

0 comments on commit ca61b51

Please sign in to comment.