Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only load front end CSS when it is needed #1455

Merged
merged 8 commits into from
Apr 30, 2020
Merged
Prev Previous commit
Next Next commit
Ensure assets load when block editor is used.
  • Loading branch information
jrtashjian committed Apr 30, 2020
commit 0d126dcd55f3cbf3aa1b7a9a1eb1aebab10ed0c5
42 changes: 41 additions & 1 deletion includes/class-coblocks-block-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function block_assets() {
$has_coblock = true;
}

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

Expand Down Expand Up @@ -379,6 +379,46 @@ 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() {
$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );

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

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

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

CoBlocks_Block_Assets::register();