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 add skip-link for block themes & templates on the frontend #32451

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/full-site-editing/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,16 @@ function gutenberg_the_skip_link() {
return;
}

// Early exit if not an FSE theme.
// Early exit if not a block theme.
if ( ! gutenberg_supports_block_templates() ) {
return;
}

// Early exit if not a block template.
global $_wp_current_template_content;
if ( ! $_wp_current_template_content ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how confident are you about the future-proof aspect of that check? Is there a way we can add a test for this?

Copy link
Member Author

@aristath aristath Jun 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... Ideally we'd have an is_block_template() function or something similar, but since we don't have something like that, checking for $_wp_current_template_content seems like the safest option.
That global var is only set in https://github.com/WordPress/WordPress/blob/4ae0e4220f0c01005afa0dcbb74979c0d5cb9abc/wp-includes/block-template.php#L45-L58 in WordPress Core, and

if ( $block_template ) {
if ( empty( $block_template->content ) && is_user_logged_in() ) {
$_wp_current_template_content =
sprintf(
/* translators: %s: Template title */
__( 'Empty template: %s', 'gutenberg' ),
$block_template->title
);
} elseif ( ! empty( $block_template->content ) ) {
$_wp_current_template_content = $block_template->content;
}
if ( isset( $_GET['_wp-find-template'] ) ) {
wp_send_json_success( $block_template );
}
in Gutenberg.
So in both cases it's only not-null when we're in a block template.

Even if we had a function is_block_template(), I can't think of any other way to do it except checking for that var 🤔

I'm pretty sure it's safe to use and future-proof - unless we change the implementation for block-templates completely

return;
}
?>

<?php
Expand Down Expand Up @@ -249,7 +255,12 @@ function gutenberg_the_skip_link() {

// Get the site wrapper.
// The skip-link will be injected in the beginning of it.
parentEl = document.querySelector( '.wp-site-blocks' ) || document.body,
parentEl = document.querySelector( '.wp-site-blocks' );

// Early exit if the root element was not found.
if ( ! parentEl ) {
return;
}

// Get the skip-link target's ID, and generate one if it doesn't exist.
skipLinkTargetID = skipLinkTarget.id;
Expand Down