WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/23/2016 04:43:41 PM (3 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

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

    r36550 r36633  
    2626    public $do_concat = false;
    2727    public $print_html = '';
     28    public $print_html_before = '';
    2829    public $print_code = '';
    2930    public $ext_handles = '';
     
    143144            $cond_before = "<!--[if {$conditional}]>\n";
    144145            $cond_after = "<![endif]-->\n";
     146        }
     147
     148        $before_handle = $this->print_inline_script( $handle, 'before', false );
     149        $after_handle = $this->print_inline_script( $handle, 'after', false );
     150
     151        if ( $before_handle ) {
     152            $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle );
     153        }
     154
     155        if ( $after_handle ) {
     156            $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
    145157        }
    146158
     
    155167             */
    156168            $srce = apply_filters( 'script_loader_src', $src, $handle );
    157             if ( $this->in_default_dir( $srce ) && ! $conditional ) {
     169
     170            if ( $before_handle && ! $conditional ) {
     171                $this->print_html_before .= $before_handle;
     172            }
     173
     174            if ( $this->in_default_dir( $srce ) && ! $conditional && ! $after_handle ) {
    158175                $this->print_code .= $this->print_extra_script( $handle, false );
    159176                $this->concat .= "$handle,";
     
    196213            return true;
    197214
    198         $tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}";
     215        $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
    199216
    200217        /**
     
    210227
    211228        if ( $this->do_concat ) {
    212             $this->print_html .= $tag;
     229            if ( $after_handle ) {
     230                $this->print_html_before .= $tag;
     231            } else {
     232                $this->print_html .= $tag;
     233            }
    213234        } else {
    214235            echo $tag;
     
    216237
    217238        return true;
     239    }
     240
     241    /**
     242     * Add extra code to a registered script.
     243     *
     244     * @since 4.5.0
     245     *
     246     * @param string $handle   Name of the script to add the inline script to. Must be lowercase.
     247     * @param string $data     String containing the javascript to be added.
     248     * @param string $position Optional. Whether to add the inline script before the handle
     249     *                         or after. Default 'after'.
     250     *
     251     * @return bool True on success, false on failure.
     252     */
     253    public function add_inline_script( $handle, $data, $position = 'after' ) {
     254        if ( ! $data ) {
     255            return false;
     256        }
     257
     258        if ( 'after' !== $position ) {
     259            $position = 'before';
     260        }
     261
     262        $script   = (array) $this->get_data( $handle, $position );
     263        $script[] = $data;
     264
     265        return $this->add_data( $handle, $position, $script );
     266    }
     267
     268    /**
     269     * Print inline scripts registered for a specific handle.
     270     *
     271     * @since 4.5.0
     272     *
     273     * @param string $handle   Name of the script to add the inline script to. Must be lowercase.
     274     * @param string $position Optional. Whether to add the inline script before the handle
     275     *                         or after. Default 'after'.
     276     * @param bool $echo       Optional. Whether to echo the script instead of just returning it.
     277     *                         Default true.
     278     * @return string|false Script on success, false otherwise.
     279     */
     280    public function print_inline_script( $handle, $position = 'after', $echo = true ) {
     281        $output = $this->get_data( $handle, $position );
     282
     283        if ( empty( $output ) ) {
     284            return false;
     285        }
     286
     287        $output = trim( implode( "\n", $output ), "\n" );
     288
     289        if ( $echo ) {
     290            printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
     291        }
     292
     293        return $output;
    218294    }
    219295
     
    340416        $this->concat_version = '';
    341417        $this->print_html = '';
     418        $this->print_html_before = '';
    342419        $this->ext_version = '';
    343420        $this->ext_handles = '';
Note: See TracChangeset for help on using the changeset viewer.