WordPress.org

Make WordPress Core

Ticket #14853: wp-includes.patch

File wp-includes.patch, 2.5 KB (added by abiralneupane, 4 years ago)
  • class.wp-scripts.php

     
    8383         * @return bool|string|void
    8484         */
    8585        public function print_extra_script( $handle, $echo = true ) {
     86                $scripts = $this->get_data( $handle, 'after' );
     87                if($scripts){
     88                        foreach($scripts as $script){
     89                                echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
     90                                        echo "$script\n";
     91                                echo "</script>\n";     
     92                        }       
     93                }               
     94
    8695                if ( !$output = $this->get_data( $handle, 'data' ) )
    8796                        return;
    8897
     
    203212                return true;
    204213        }
    205214
     215        public function add_inline_script( $handle, $code ) {
     216                if ( ! $code ) {
     217                        return false;
     218                }
     219
     220                $after = $this->get_data( $handle, 'after' );
     221                if ( ! $after ) {
     222                        $after = array();
     223                }
     224
     225                $after[] = $code;
     226
     227                return $this->add_data( $handle, 'after', $after );
     228        }
     229
    206230        /**
    207231         * Localizes a script, only if the script has already been added
    208232         *
  • functions.wp-scripts.php

     
    8686}
    8787
    8888/**
     89 * Add extra code to a registered Script.
     90 *
     91 * Code will only be added if the script in already in the queue.
     92 * Accepts a string $data containing the Code. If two or more code blocks
     93 * are added to the same script $handle, they will be printed in the order
     94 * they were added, i.e. the latter added code can redeclare the previous.
     95 *
     96 */
     97function wp_add_inline_script( $handle, $data ) {
     98        global $wp_scripts;
     99        if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     100                if ( ! did_action( 'init' ) )
     101                        _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     102                                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
     103                $wp_scripts = new WP_Scripts();
     104        }
     105
     106        if ( false !== stripos( $data, '</script>' ) ) {
     107                _doing_it_wrong( __FUNCTION__, __( 'Do not pass script tags to wp_add_inline_script().' ), '4.1.3' );
     108                $data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
     109        }
     110
     111        return $wp_scripts->add_inline_script( $handle, $data );
     112}
     113
     114
     115/**
    89116 * Register a new script.
    90117 *
    91118 * Registers a script to be linked later using the wp_enqueue_script() function.