Skip to content

Commit

Permalink
Only load front end CSS when it is needed (#1455)
Browse files Browse the repository at this point in the history
* Only load front end CSS when it is needed

This checks to make sure that a Coblock is being used before requiring the CSS to be loaded on the front end of the site.  This is to address #1454 

In my basic tests this worked fine, however I'd like to see it get broader testing to make sure I haven't missed other conditions.

* Define $post global and don't overwrite it

* Ensure assets load when block editor is used.

* Only check post content if not in the admin

* Resolve failing tests

* Adding tests

* Updating contributors

Co-authored-by: JR Tashjian <jrtashjian@gmail.com>
  • Loading branch information
josephscott and jrtashjian committed Apr 30, 2020
1 parent fb76a26 commit 2263dfb
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
38 changes: 38 additions & 0 deletions .dev/tests/phpunit/includes/test-coblocks-block-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,44 @@ public function test_editor_assets_localized_data() {

}

public function test_block_assets_not_loaded_when_no_coblocks_block() {
global $post, $wp_styles;
unset( $GLOBALS['current_screen'] );

$post_id = wp_insert_post( [
'post_author' => 1,
'post_content' => 'NoBlocks',
'post_title' => 'NoBlocks',
'post_status' => 'publish',
] );

$post = get_post( $post_id );

$this->coblocks_block_assets->block_assets();
do_action( 'enqueue_block_assets' );

$this->assertNotContains( 'coblocks-frontend', $wp_styles->queue );
}

public function test_block_assets_loaded_with_coblocks_block() {
global $post, $wp_styles;
unset( $GLOBALS['current_screen'] );

$post_id = wp_insert_post( [
'post_author' => 1,
'post_content' => '<!-- wp:coblocks/hasblocks --><!-- /wp:coblocks/hasblocks -->',
'post_title' => 'CoBlocks',
'post_status' => 'publish',
] );

$post = get_post( $post_id );

$this->coblocks_block_assets->block_assets();
do_action( 'enqueue_block_assets' );

$this->assertContains( 'coblocks-frontend', $wp_styles->queue );
}

/**
* Test the frontend scripts masonry are enqueued correctly
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class CoBlocks_Generated_Styles_Tests extends WP_UnitTestCase {

private $coblocks_generated_styles;
private $coblocks_block_assets;

private $dimensions;

Expand Down Expand Up @@ -46,6 +47,7 @@ public function setUp() {
];

$this->coblocks_generated_styles = new CoBlocks_Generated_Styles();
$this->coblocks_block_assets = new CoBlocks_Block_Assets();

}

Expand Down Expand Up @@ -105,10 +107,12 @@ public function test_construct_actions() {
*/
public function test_enqueue_styles() {

unset( $GLOBALS['current_screen'] );

$post_id = wp_insert_post(
[
'post_author' => 1,
'post_content' => 'Post content',
'post_content' => '<!-- wp:coblocks/block_with_custom_styles --><!-- /wp:coblocks/block_with_custom_styles -->',
'post_title' => 'Post title',
'post_status' => 'publish',
]
Expand All @@ -121,8 +125,10 @@ public function test_enqueue_styles() {

$post = get_post( $post_id );

$this->coblocks_block_assets->block_assets();
$this->coblocks_generated_styles->enqueue_styles();

do_action( 'enqueue_block_assets' );
do_action( 'wp_enqueue_scripts' );

global $wp_styles;
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ This list is manually curated to include valuable contributions by volunteers th
| @p-jackson | |
| @gmays | |
| @brandonpayton | |
| @josephscott | @josephscott |
| @passatgt | @passatgt |
65 changes: 65 additions & 0 deletions includes/class-coblocks-block-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ public function get_asset_file( $filepath ) {
* @access public
*/
public function block_assets() {
global $post;

// Only load the front end CSS if a Coblock is in use.
$has_coblock = false;

if ( ! is_admin() ) {
// This is similar to has_block() in core, but will match anything
// in the coblocks/* namespace.
$wp_post = get_post( $post );
if ( $wp_post instanceof WP_Post ) {
$post_content = $wp_post->post_content;
}

if ( false !== strpos( $post_content, '<!-- wp:coblocks/' ) ) {

This comment has been minimized.

Copy link
@afragen

afragen May 1, 2020

I'm getting a number of PHP errors for $post_content being undefined. It appears this happens if the preceding conditional fails.

$has_coblock = true;
}
}

if ( ! $has_coblock && ! $this->is_page_gutenberg() ) {
return;
}

// Styles.
$name = 'coblocks-style';
Expand Down Expand Up @@ -360,6 +381,50 @@ public function editor_scripts() {
true
);
}

/**
* Return whether a post type should display the Block Editor.
*
* @param string $post_type The post_type slug to check.
*/
protected function is_post_type_gutenberg( $post_type ) {
return use_block_editor_for_post_type( $post_type );
}

/**
* Return whether the page we are on is loading the Block Editor.
*/
protected function is_page_gutenberg() {
if ( ! is_admin() ) {
return false;
}

$admin_page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );

if ( false !== strpos( $admin_page, 'post-new.php' ) && empty( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return true;
}

if ( false !== strpos( $admin_page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return true;
}

if ( false !== strpos( $admin_page, 'post.php' ) ) {
$wp_post = get_post( $_GET['post'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $wp_post ) && isset( $wp_post->post_type ) && $this->is_post_type_gutenberg( $wp_post->post_type ) ) {
return true;
}
}

if ( false !== strpos( $admin_page, 'revision.php' ) ) {
$wp_post = get_post( $_GET['revision'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$post_parent = get_post( $wp_post->post_parent );
if ( isset( $post_parent ) && isset( $post_parent->post_type ) && $this->is_post_type_gutenberg( $post_parent->post_type ) ) {
return true;
}
}
return false;
}
}

CoBlocks_Block_Assets::register();

0 comments on commit 2263dfb

Please sign in to comment.