Add the ability to print data *after* a script, whether it is concatenated or not:
* Add a third argument to `WP_Scripts->print_extra_script()`, `$key`, which will be passed to `->get_data()` (no longer passes hardcoded `'data'`) * When `$key` is set to `'data-after'`, the inline script will be printed after the `<script>` tag. If the scripts are being concatenated, all scripts' `'data-after'` data will be printed after the concatenated `<script>` has been rendered. Props hakre, wonderboymusic. Fixes #25277. git-svn-id: https://develop.svn.wordpress.org/trunk@31032 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
d699ce8b13
commit
c21e4f95ce
@ -27,6 +27,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
public $print_html = '';
|
||||
public $print_code = '';
|
||||
public $ext_handles = '';
|
||||
public $print_after_html = '';
|
||||
public $ext_version = '';
|
||||
public $default_dirs;
|
||||
|
||||
@ -67,12 +68,14 @@ class WP_Scripts extends WP_Dependencies {
|
||||
return $this->print_extra_script( $handle, $echo );
|
||||
}
|
||||
|
||||
public function print_extra_script( $handle, $echo = true ) {
|
||||
if ( !$output = $this->get_data( $handle, 'data' ) )
|
||||
public function print_extra_script( $handle, $echo = true, $key = 'data' ) {
|
||||
if ( ! $output = $this->get_data( $handle, $key ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !$echo )
|
||||
if ( ! $echo ) {
|
||||
return $output;
|
||||
}
|
||||
|
||||
echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
|
||||
echo "/* <![CDATA[ */\n";
|
||||
@ -117,6 +120,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
$srce = apply_filters( 'script_loader_src', $src, $handle );
|
||||
if ( $this->in_default_dir($srce) ) {
|
||||
$this->print_code .= $this->print_extra_script( $handle, false );
|
||||
$this->print_after_html .= "\n" . $this->print_extra_script( $handle, false, 'data-after' );
|
||||
$this->concat .= "$handle,";
|
||||
$this->concat_version .= "$handle$ver";
|
||||
return true;
|
||||
@ -155,8 +159,10 @@ class WP_Scripts extends WP_Dependencies {
|
||||
|
||||
if ( $this->do_concat ) {
|
||||
$this->print_html .= $tag;
|
||||
$this->print_after_html .= $this->print_extra_script( $handle, false, 'data-after' ) . "\n";
|
||||
} else {
|
||||
echo $tag;
|
||||
$this->print_extra_script( $handle, true, 'data-after' );
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -261,6 +267,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
$this->concat = '';
|
||||
$this->concat_version = '';
|
||||
$this->print_html = '';
|
||||
$this->print_after_html = '';
|
||||
$this->ext_version = '';
|
||||
$this->ext_handles = '';
|
||||
}
|
||||
|
@ -857,8 +857,21 @@ function _print_scripts() {
|
||||
echo "<script type='text/javascript' src='" . esc_attr($src) . "'></script>\n";
|
||||
}
|
||||
|
||||
if ( !empty($wp_scripts->print_html) )
|
||||
if ( ! empty( $wp_scripts->print_html ) ) {
|
||||
echo $wp_scripts->print_html;
|
||||
}
|
||||
|
||||
if ( ! empty( $wp_scripts->print_after_html ) ) {
|
||||
if ( $wp_scripts->do_concat ) {
|
||||
echo "<script type='text/javascript'>\n";
|
||||
echo "/* <![CDATA[ */\n"; // not needed in HTML 5
|
||||
echo trim( $wp_scripts->print_after_html ) . "\n";
|
||||
echo "/* ]]> */\n";
|
||||
echo "</script>\n";
|
||||
} else {
|
||||
echo $wp_scripts->print_after_html;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user