Make WordPress Core

Changeset 31032


Ignore:
Timestamp:
01/03/2015 05:07:15 AM (10 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class.wp-scripts.php

    r31030 r31032  
    2828    public $print_code = '';
    2929    public $ext_handles = '';
     30    public $print_after_html = '';
    3031    public $ext_version = '';
    3132    public $default_dirs;
     
    6869    }
    6970
    70     public function print_extra_script( $handle, $echo = true ) {
    71         if ( !$output = $this->get_data( $handle, 'data' ) )
     71    public function print_extra_script( $handle, $echo = true, $key = 'data' ) {
     72        if ( ! $output = $this->get_data( $handle, $key ) ) {
    7273            return;
    73 
    74         if ( !$echo )
     74        }
     75
     76        if ( ! $echo ) {
    7577            return $output;
     78        }
    7679
    7780        echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
     
    118121            if ( $this->in_default_dir($srce) ) {
    119122                $this->print_code .= $this->print_extra_script( $handle, false );
     123                $this->print_after_html .= "\n" . $this->print_extra_script( $handle, false, 'data-after' );
    120124                $this->concat .= "$handle,";
    121125                $this->concat_version .= "$handle$ver";
     
    156160        if ( $this->do_concat ) {
    157161            $this->print_html .= $tag;
     162            $this->print_after_html .= $this->print_extra_script( $handle, false, 'data-after' ) . "\n";
    158163        } else {
    159164            echo $tag;
     165            $this->print_extra_script( $handle, true, 'data-after' );
    160166        }
    161167
     
    262268        $this->concat_version = '';
    263269        $this->print_html = '';
     270        $this->print_after_html = '';
    264271        $this->ext_version = '';
    265272        $this->ext_handles = '';
  • trunk/src/wp-includes/script-loader.php

    r31016 r31032  
    858858    }
    859859
    860     if ( !empty($wp_scripts->print_html) )
     860    if ( ! empty( $wp_scripts->print_html ) ) {
    861861        echo $wp_scripts->print_html;
     862    }
     863
     864    if ( ! empty( $wp_scripts->print_after_html ) ) {
     865        if ( $wp_scripts->do_concat ) {
     866            echo "<script type='text/javascript'>\n";
     867            echo "/* <![CDATA[ */\n"; // not needed in HTML 5
     868            echo trim( $wp_scripts->print_after_html ) . "\n";
     869            echo "/* ]]> */\n";
     870            echo "</script>\n";
     871        } else {
     872            echo $wp_scripts->print_after_html;
     873        }
     874    }
    862875}
    863876
Note: See TracChangeset for help on using the changeset viewer.