Make WordPress Core

Ticket #31281: 31281.diff

File 31281.diff, 4.2 KB (added by georgestephanis, 9 years ago)

New way of doing this, more in keeping with existing methods.

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

     
    312312                        }
    313313                }
    314314
     315                // Even if the script itself is being concatenated and printed later, we
     316                // may as well print the templates now.
     317                $this->print_templates( $handle );
     318
    315319                $has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
    316320
    317321                if ( $has_conditional_data ) {
     
    421425        }
    422426
    423427        /**
     428         * Add a javascript `text/html` template to print out before the specified script.
     429         *
     430         * @since 4.?.0
     431         * @access public
     432         *
     433         * @param string $script_handle   Name of the script to add the template to. Must be lowercase.
     434         * @param string $template_handle The handle of the template. Will be prefixed with `tmpl-`.
     435         * @param string $data            String containing the markup to be added.
     436         * @return bool True on success, false on failure.
     437         */
     438        public function add_template( $script_handle, $template_handle, $data ) {
     439                $templates = $this->get_data( $script_handle, 'templates' );
     440                if ( ! $templates ) {
     441                        $templates = array();
     442                }
     443
     444                $templates[ $template_handle ] = $data;
     445
     446                return $this->add_data( $script_handle, 'templates', $templates );
     447        }
     448
     449        /**
     450         * Print the javascript templates before the script that they are attached to.
     451         *
     452         * @since 4.?.0
     453         *
     454         * @param string $script_handle Name of the script to print the templates for. Must be lowercase.
     455         * @param bool $echo            Optional. Whether to echo the script instead of just returning it.
     456         *                              Default true.
     457         * @return null|string The templates, or null if there weren't any.
     458         */
     459        public function print_templates( $script_handle, $echo = true ) {
     460                $templates = $this->get_data( $script_handle, 'templates' );
     461                $output = '';
     462
     463                if ( empty( $templates ) ) {
     464                        return null;
     465                }
     466
     467                foreach ( $templates as $handle => $template ) {
     468                        $output .= sprintf( "<script type='text/html' id='tmpl-%s'>\n%s\n</script>\n", esc_attr( $handle ), $template );
     469                }
     470
     471                if ( $echo ) {
     472                        echo $output;
     473                }
     474
     475                return $output;
     476        }
     477
     478        /**
    424479         * Localizes a script, only if the script has already been added.
    425480         *
    426481         * @since 2.1.0
  • src/wp-includes/functions.wp-scripts.php

     
    328328function wp_script_add_data( $handle, $key, $value ){
    329329        return wp_scripts()->add_data( $handle, $key, $value );
    330330}
     331
     332/**
     333 * Add a template to a script.
     334 *
     335 * Works only if the script has already been added.
     336 *
     337 * @since 4.?.0
     338 *
     339 * @param string $handle    Name of the script.
     340 * @param string $key       ID of template. Will be prefixed with `tmpl-`.
     341 * @param mixed  $template  String containing the template to be added.
     342 * @return bool True on success, false on failure.
     343 */
     344function wp_script_add_template( $handle, $key, $template ){
     345        return wp_scripts()->add_template( $handle, $key, $template );
     346}
  • tests/phpunit/tests/dependencies/scripts.php

     
    724724
    725725                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
    726726        }
     727
     728        /**
     729         * Testing `wp_script_add_template`
     730         * @ticket 31281
     731         */
     732        function test_wp_script_add_template() {
     733                // Enqueue & add data
     734                $template_code = '<h1>test <em>template</em></h1>';
     735                wp_enqueue_script( 'test-only-data', 'example.com', array(), null );
     736                wp_script_add_template( 'test-only-data', 'test-template', $template_code );
     737                $expected  = "<script type='text/html' id='tmpl-test-template'>\n{$template_code}\n</script>\n";
     738                $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     739
     740                // Go!
     741                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     742
     743                // No scripts left to print
     744                $this->assertEquals( '', get_echo( 'wp_print_scripts' ) );
     745        }
    727746}