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

Eliminate manual construction of script tags #58

Closed
Show file tree
Hide file tree
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
103 changes: 39 additions & 64 deletions src/wp-includes/class-wp-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,6 @@ class WP_Scripts extends WP_Dependencies {
*/
public $default_dirs;

/**
* Holds a string which contains the type attribute for script tag.
*
* If the active theme does not declare HTML5 support for 'script',
* then it initializes as `type='text/javascript'`.
*
* @since 5.3.0
* @var string
*/
private $type_attr = '';

/**
* Holds a mapping of dependents (as handles) for a given script handle.
* Used to optimize recursive dependency tree checks.
Expand All @@ -158,14 +147,6 @@ public function __construct() {
* @since 3.4.0
*/
public function init() {
if (
function_exists( 'is_admin' ) && ! is_admin()
&&
function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'script' )
) {
$this->type_attr = " type='text/javascript'";
}

/**
* Fires when the WP_Scripts instance is initialized.
*
Expand Down Expand Up @@ -236,20 +217,10 @@ public function print_extra_script( $handle, $display = true ) {
return $output;
}

printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) );

// CDATA is not needed for HTML 5.
if ( $this->type_attr ) {
echo "/* <![CDATA[ */\n";
}

echo "$output\n";

if ( $this->type_attr ) {
echo "/* ]]> */\n";
}

echo "</script>\n";
wp_print_inline_script_tag(
$output,
array( 'id' => "{$handle}-js-extra" )
);

return true;
}
Expand Down Expand Up @@ -306,7 +277,10 @@ public function do_item( $handle, $group = false ) {
$before_handle = $this->print_inline_script( $handle, 'before', false );

if ( $before_handle ) {
$before_handle = sprintf( "<script%s id='%s-js-before'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $before_handle );
$before_handle = wp_get_inline_script_tag(
$before_handle,
array( 'id' => "{$handle}-js-before" )
);
}

// Eligible loading strategies will only be 'async', 'defer', or ''.
Expand All @@ -315,21 +289,22 @@ public function do_item( $handle, $group = false ) {
$after_handle = $this->print_inline_script( $handle, $after, false );

if ( $after_handle ) {
$after_handle = sprintf(
"<script%1\$s id='%2\$s-js-after'>\n%3\$s\n</script>\n",
$this->type_attr,
esc_attr( $handle ),
$after_handle
$after_handle = wp_get_inline_script_tag(
$after_handle,
array( 'id' => "{$handle}-js-after" )
);
}
if ( '' !== $strategy ) {
$after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false );

if ( $after_non_standalone_handle ) {
$after_handle .= sprintf(
"<script type='text/template' id='%1\$s-js-after' data-wp-executes-after='%1\$s'>\n%2\$s\n</script>\n",
esc_attr( $handle ),
$after_non_standalone_handle
$after_handle .= wp_get_inline_script_tag(
$after_non_standalone_handle,
array(
'type' => 'text/template',
'id' => "{$handle}-js-after",
'data-wp-executes-after' => $handle,
)
);
}
}
Expand All @@ -348,7 +323,10 @@ public function do_item( $handle, $group = false ) {

$translations = $this->print_translations( $handle, false );
if ( $translations ) {
$translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations );
$translations = wp_get_inline_script_tag(
$translations,
array( 'id' => "{$handle}-js-translations" )
);
}

if ( $this->do_concat ) {
Expand Down Expand Up @@ -425,28 +403,26 @@ public function do_item( $handle, $group = false ) {
return true;
}

$attributes = array(
'src' => $src,
'id' => "{$handle}-js",
);
if ( '' !== $strategy ) {
$strategy = ' ' . $strategy;
$attributes[ $strategy ] = true;
if ( ! empty( $after_non_standalone_handle ) ) {
$strategy .= sprintf( " onload='wpLoadAfterScripts(%s)'", esc_attr( wp_json_encode( $handle ) ) );
$attributes['onload'] = sprintf( 'wpLoadAfterScripts(%s)', wp_json_encode( $handle ) );
}
}
$tag = $translations . $cond_before . $before_handle;
$tag .= sprintf(
"<script%s src='%s' id='%s-js'%s></script>\n",
$this->type_attr,
esc_url( $src ),
esc_attr( $handle ),
$strategy
);
$tag .= wp_get_script_tag( $attributes );
$tag .= $after_handle . $cond_after;

/**
* Filters the HTML script tag of an enqueued script.
*
* @since 4.1.0
*
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $tag The script tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
*/
Expand Down Expand Up @@ -519,19 +495,15 @@ public function print_inline_script( $handle, $position = 'after', $display = tr
$output = trim( implode( "\n", $output ), "\n" );

if ( $display ) {
$attributes = array();
if ( 'after-non-standalone' === $position ) {
$script_output = "<script%1\$s id='%2\$s-js-after' data-wp-executes-after='%2\$s'>\n%4\$s\n</script>\n";
$attributes['id'] = "{$handle}-js-after";
$attributes['data-wp-executes-after'] = $handle;
Copy link
Collaborator Author

@westonruter westonruter May 4, 2023

Choose a reason for hiding this comment

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

(Yanked)

Copy link
Collaborator Author

@westonruter westonruter May 4, 2023

Choose a reason for hiding this comment

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

I mean:

Suggested change
$attributes['data-wp-executes-after'] = $handle;
$attributes['data-wp-executes-after'] = $handle;
$attributes['type'] = 'text/template';

WordPress#4391 (comment)

} else {
$script_output = "<script%1\$s id='%2\$s-js-%3\$s'>\n%4\$s\n</script>\n";
$attributes['id'] = "{$handle}-js-{$position}";
}

printf(
$script_output,
$this->type_attr,
esc_attr( $handle ),
esc_attr( $position ),
$output
);
wp_print_inline_script_tag( $output, $attributes );
}

return $output;
Expand Down Expand Up @@ -693,7 +665,10 @@ public function print_translations( $handle, $display = true ) {
JS;

if ( $display ) {
printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output );
wp_print_inline_script_tag(
$output,
array( 'id' => "{$handle}-js-translations" )
);
}

return $output;
Expand Down
11 changes: 9 additions & 2 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2798,7 +2798,8 @@ function wp_sanitize_script_attributes( $attributes ) {
* @return string String containing `<script>` opening and closing tags.
*/
function wp_get_script_tag( $attributes ) {
if ( ! isset( $attributes['type'] ) && ! is_admin() && ! current_theme_supports( 'html5', 'script' ) ) {
$is_html5 = is_admin() || current_theme_supports( 'html5', 'script' );
if ( ! isset( $attributes['type'] ) && ! $is_html5 ) {
$attributes['type'] = 'text/javascript';
}
/**
Expand Down Expand Up @@ -2842,7 +2843,8 @@ function wp_print_script_tag( $attributes ) {
* @return string String containing inline JavaScript code wrapped around `<script>` tag.
*/
function wp_get_inline_script_tag( $javascript, $attributes = array() ) {
if ( ! isset( $attributes['type'] ) && ! is_admin() && ! current_theme_supports( 'html5', 'script' ) ) {
$is_html5 = is_admin() || current_theme_supports( 'html5', 'script' );
if ( ! isset( $attributes['type'] ) && ! $is_html5 ) {
$attributes['type'] = 'text/javascript';
}
/**
Expand All @@ -2859,6 +2861,11 @@ function wp_get_inline_script_tag( $javascript, $attributes = array() ) {

$javascript = "\n" . trim( $javascript, "\n\r " ) . "\n";

// Add CDATA comments if not HTML 5.
if ( ! $is_html5 ) {
$javascript = "\n/* <![CDATA[ */{$javascript}/* ]]> */\n";
}

return sprintf( "<script%s>%s</script>\n", wp_sanitize_script_attributes( $attributes ), $javascript );
}

Expand Down