Script Loader: Introduce `wp_add_inline_script()`.

This new function can be used to add inline JavaScript before and after enqueued scripts, just like `wp_add_inline_style()` works for CSS.

Props atimmer, abiralneupane, ocean90, swissspidy.
Fixes #14853.
Built from https://develop.svn.wordpress.org/trunk@36633


git-svn-id: http://core.svn.wordpress.org/trunk@36600 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2016-02-23 16:44:26 +00:00
parent ba5cc4e776
commit b47a3831ee
4 changed files with 114 additions and 4 deletions

View File

@ -25,6 +25,7 @@ class WP_Scripts extends WP_Dependencies {
public $concat_version = '';
public $do_concat = false;
public $print_html = '';
public $print_html_before = '';
public $print_code = '';
public $ext_handles = '';
public $ext_version = '';
@ -144,6 +145,17 @@ class WP_Scripts extends WP_Dependencies {
$cond_after = "<![endif]-->\n";
}
$before_handle = $this->print_inline_script( $handle, 'before', false );
$after_handle = $this->print_inline_script( $handle, 'after', false );
if ( $before_handle ) {
$before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle );
}
if ( $after_handle ) {
$after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
}
if ( $this->do_concat ) {
/**
* Filter the script loader source.
@ -154,7 +166,12 @@ class WP_Scripts extends WP_Dependencies {
* @param string $handle Script handle.
*/
$srce = apply_filters( 'script_loader_src', $src, $handle );
if ( $this->in_default_dir( $srce ) && ! $conditional ) {
if ( $before_handle && ! $conditional ) {
$this->print_html_before .= $before_handle;
}
if ( $this->in_default_dir( $srce ) && ! $conditional && ! $after_handle ) {
$this->print_code .= $this->print_extra_script( $handle, false );
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
@ -195,7 +212,7 @@ class WP_Scripts extends WP_Dependencies {
if ( ! $src )
return true;
$tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}";
$tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
/**
* Filter the HTML script tag of an enqueued script.
@ -209,7 +226,11 @@ class WP_Scripts extends WP_Dependencies {
$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
if ( $this->do_concat ) {
$this->print_html .= $tag;
if ( $after_handle ) {
$this->print_html_before .= $tag;
} else {
$this->print_html .= $tag;
}
} else {
echo $tag;
}
@ -217,6 +238,61 @@ class WP_Scripts extends WP_Dependencies {
return true;
}
/**
* Add extra code to a registered script.
*
* @since 4.5.0
*
* @param string $handle Name of the script to add the inline script to. Must be lowercase.
* @param string $data String containing the javascript to be added.
* @param string $position Optional. Whether to add the inline script before the handle
* or after. Default 'after'.
*
* @return bool True on success, false on failure.
*/
public function add_inline_script( $handle, $data, $position = 'after' ) {
if ( ! $data ) {
return false;
}
if ( 'after' !== $position ) {
$position = 'before';
}
$script = (array) $this->get_data( $handle, $position );
$script[] = $data;
return $this->add_data( $handle, $position, $script );
}
/**
* Print inline scripts registered for a specific handle.
*
* @since 4.5.0
*
* @param string $handle Name of the script to add the inline script to. Must be lowercase.
* @param string $position Optional. Whether to add the inline script before the handle
* or after. Default 'after'.
* @param bool $echo Optional. Whether to echo the script instead of just returning it.
* Default true.
* @return string|false Script on success, false otherwise.
*/
public function print_inline_script( $handle, $position = 'after', $echo = true ) {
$output = $this->get_data( $handle, $position );
if ( empty( $output ) ) {
return false;
}
$output = trim( implode( "\n", $output ), "\n" );
if ( $echo ) {
printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
}
return $output;
}
/**
* Localizes a script, only if the script has already been added
*
@ -339,6 +415,7 @@ class WP_Scripts extends WP_Dependencies {
$this->concat = '';
$this->concat_version = '';
$this->print_html = '';
$this->print_html_before = '';
$this->ext_version = '';
$this->ext_handles = '';
}

View File

@ -85,6 +85,35 @@ function wp_print_scripts( $handles = false ) {
return wp_scripts()->do_items( $handles );
}
/**
* Add extra code to a registered script.
*
* Code will only be added if the script in already in the queue.
* Accepts a string $data containing the Code. If two or more code blocks
* are added to the same script $handle, they will be printed in the order
* they were added, i.e. the latter added code can redeclare the previous.
*
* @since 4.5.0
*
* @see WP_Scripts::add_inline_script()
*
* @param string $handle Name of the script to add the inline script to. Must be lowercase.
* @param string $data String containing the javascript to be added.
* @param string $position Optional. Whether to add the inline script before the handle
* or after. Default 'after'.
* @return bool True on success, false on failure.
*/
function wp_add_inline_script( $handle, $data, $position = 'after' ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
if ( false !== stripos( $data, '</script>' ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Do not pass script tags to wp_add_inline_script().' ), '4.5.0' );
$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
}
return wp_scripts()->add_inline_script( $handle, $data, $position );
}
/**
* Register a new script.
*

View File

@ -992,6 +992,10 @@ function _print_scripts() {
echo "</script>\n";
}
if ( ! empty( $wp_scripts->print_html_before ) ) {
echo $wp_scripts->print_html_before;
}
$concat = str_split( $concat, 128 );
$concat = 'load%5B%5D=' . implode( '&load%5B%5D=', $concat );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.5-alpha-36632';
$wp_version = '4.5-alpha-36633';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.