Make WordPress Core

Changeset 43788


Ignore:
Timestamp:
10/22/2018 06:12:55 AM (6 years ago)
Author:
pento
Message:

Script/Style Dependencies: Make sure that inline scripts for handles without a source are printed.

See [36550] for WP_Styles.

Merges [43565] to the 5.0 branch.

Props bpayton.
Fixes #44551.

Location:
branches/5.0
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0

  • branches/5.0/src/wp-includes/class.wp-scripts.php

    r41686 r43788  
    265265        if ( $after_handle ) {
    266266            $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
     267        }
     268
     269        if ( $before_handle || $after_handle ) {
     270            $inline_script_tag = "{$cond_before}{$before_handle}{$after_handle}{$cond_after}";
     271        } else {
     272            $inline_script_tag = '';
    267273        }
    268274
     
    308314
    309315        // A single item may alias a set of items, by having dependencies, but no source.
    310         if ( ! $obj->src ) {
     316        if ( ! $src ) {
     317            if ( $inline_script_tag ) {
     318                if ( $this->do_concat ) {
     319                    $this->print_html .= $inline_script_tag;
     320                } else {
     321                    echo $inline_script_tag;
     322                }
     323            }
     324
    311325            return true;
    312326        }
  • branches/5.0/tests/phpunit/tests/dependencies/scripts.php

    r43738 r43788  
    396396        $expected  = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    397397        $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     398        $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     399
     400        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     401    }
     402
     403    /**
     404     * @ticket 44551
     405     */
     406    function test_wp_add_inline_script_before_for_handle_without_source() {
     407        wp_register_script( 'test-example', '' );
     408        wp_enqueue_script( 'test-example' );
     409        wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
     410
     411        $expected = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
     412
     413        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     414    }
     415
     416    /**
     417     * @ticket 44551
     418     */
     419    function test_wp_add_inline_script_after_for_handle_without_source() {
     420        wp_register_script( 'test-example', '' );
     421        wp_enqueue_script( 'test-example' );
     422        wp_add_inline_script( 'test-example', 'console.log("after");' );
     423
     424        $expected = "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     425
     426        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     427    }
     428
     429    /**
     430     * @ticket 44551
     431     */
     432    function test_wp_add_inline_script_before_and_after_for_handle_without_source() {
     433        wp_register_script( 'test-example', '' );
     434        wp_enqueue_script( 'test-example' );
     435        wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
     436        wp_add_inline_script( 'test-example', 'console.log("after");' );
     437
     438        $expected  = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    398439        $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
    399440
Note: See TracChangeset for help on using the changeset viewer.