Make WordPress Core


Ignore:
Timestamp:
01/27/2024 01:02:35 AM (10 months ago)
Author:
westonruter
Message:

Script Loader: Clarify in docs that wp_get_inline_script_tag() and wp_print_inline_script_tag() can take non-JS data.

Props vladimiraus.
Fixes #60331.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-loader.php

    r57341 r57367  
    28352835
    28362836/**
    2837  * Wraps inline JavaScript in `<script>` tag.
     2837 * Constructs an inline script tag.
    28382838 *
    28392839 * It is possible to inject attributes in the `<script>` tag via the  {@see 'wp_script_attributes'}  filter.
     
    28422842 * @since 5.7.0
    28432843 *
    2844  * @param string $javascript Inline JavaScript code.
     2844 * @param string $data       Data for script tag: JavaScript, importmap, speculationrules, etc.
    28452845 * @param array  $attributes Optional. Key-value pairs representing `<script>` tag attributes.
    28462846 * @return string String containing inline JavaScript code wrapped around `<script>` tag.
    28472847 */
    2848 function wp_get_inline_script_tag( $javascript, $attributes = array() ) {
     2848function wp_get_inline_script_tag( $data, $attributes = array() ) {
    28492849    $is_html5 = current_theme_supports( 'html5', 'script' ) || is_admin();
    28502850    if ( ! isset( $attributes['type'] ) && ! $is_html5 ) {
     
    29002900         * an additional `<![CDATA[` leaves the contents unchanged.
    29012901         */
    2902         $javascript = str_replace( ']]>', ']]]]><![CDATA[>', $javascript );
     2902        $data = str_replace( ']]>', ']]]]><![CDATA[>', $data );
    29032903
    29042904        // Wrap the entire escaped script inside a CDATA section.
    2905         $javascript = sprintf( "/* <![CDATA[ */\n%s\n/* ]]> */", $javascript );
    2906     }
    2907 
    2908     $javascript = "\n" . trim( $javascript, "\n\r " ) . "\n";
     2905        $data = sprintf( "/* <![CDATA[ */\n%s\n/* ]]> */", $data );
     2906    }
     2907
     2908    $data = "\n" . trim( $data, "\n\r " ) . "\n";
    29092909
    29102910    /**
     
    29162916     *                           Only the attribute name is added to the `<script>` tag for
    29172917     *                           entries with a boolean value, and that are true.
    2918      * @param string $javascript Inline JavaScript code.
     2918     * @param string $data       Inline data.
    29192919     */
    2920     $attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $javascript );
    2921 
    2922     return sprintf( "<script%s>%s</script>\n", wp_sanitize_script_attributes( $attributes ), $javascript );
    2923 }
    2924 
    2925 /**
    2926  * Prints inline JavaScript wrapped in `<script>` tag.
     2920    $attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $data );
     2921
     2922    return sprintf( "<script%s>%s</script>\n", wp_sanitize_script_attributes( $attributes ), $data );
     2923}
     2924
     2925/**
     2926 * Prints an inline script tag.
    29272927 *
    29282928 * It is possible to inject attributes in the `<script>` tag via the  {@see 'wp_script_attributes'}  filter.
     
    29312931 * @since 5.7.0
    29322932 *
    2933  * @param string $javascript Inline JavaScript code.
     2933 * @param string $data       Data for script tag: JavaScript, importmap, speculationrules, etc.
    29342934 * @param array  $attributes Optional. Key-value pairs representing `<script>` tag attributes.
    29352935 */
    2936 function wp_print_inline_script_tag( $javascript, $attributes = array() ) {
    2937     echo wp_get_inline_script_tag( $javascript, $attributes );
     2936function wp_print_inline_script_tag( $data, $attributes = array() ) {
     2937    echo wp_get_inline_script_tag( $data, $attributes );
    29382938}
    29392939
Note: See TracChangeset for help on using the changeset viewer.