Skip to content

Commit

Permalink
Use a more specific <script> matching (#23407)
Browse files Browse the repository at this point in the history
* Use a more specific matching pattern to only affect the `<script>` tags which we're interested in.

This uses the full `<script>` produced by `WP_Scripts::do_item()` ignoring the Translations and before/after inline scripts, which allows for it to exclude `<script%` tags contained within the before/after html chunks.

* Rename the variable
  • Loading branch information
dd32 authored and noisysocks committed Jun 24, 2020
1 parent 23e187c commit 37d37ba
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/block-directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,24 @@ function gutenberg_enqueue_block_editor_assets_block_directory() {
/**
* Add data attribute of handle to all script tags output in the wp-admin.
*
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $esc_src The script's pre-escaped registered src.
*
* @return string Filter script tag.
* @return string Filtered script tag.
*/
function gutenberg_change_script_tag( $tag, $handle ) {
function gutenberg_change_script_tag( $tag, $handle, $esc_src ) {
if ( ! is_admin() ) {
return $tag;
}
$tag = str_replace( '<script ', sprintf( '<script data-handle="%s" ', esc_attr( $handle ) ), $tag );

$tag = str_replace(
sprintf( "<script src='%s'></script>", $esc_src ),
sprintf( "<script data-handle='%s' src='%s'></script>", esc_attr( $handle ), $esc_src ),
$tag
);

return $tag;
}
add_filter( 'script_loader_tag', 'gutenberg_change_script_tag', 10, 2 );
add_filter( 'script_loader_tag', 'gutenberg_change_script_tag', 1, 3 );
}

0 comments on commit 37d37ba

Please sign in to comment.