wp/wp-includes/class.wp-scripts.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
--- a/wp/wp-includes/class.wp-scripts.php	Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-includes/class.wp-scripts.php	Tue Dec 15 13:49:49 2020 +0100
@@ -123,6 +123,17 @@
 	public $default_dirs;
 
 	/**
+	 * Holds a string which contains the type attribute for script tag.
+	 *
+	 * If the current theme does not declare HTML5 support for 'script',
+	 * then it initializes as `type='text/javascript'`.
+	 *
+	 * @since 5.3.0
+	 * @var string
+	 */
+	private $type_attr = '';
+
+	/**
 	 * Constructor.
 	 *
 	 * @since 2.6.0
@@ -138,6 +149,14 @@
 	 * @since 3.4.0
 	 */
 	public function init() {
+		if (
+			function_exists( 'is_admin' ) && ! is_admin()
+		&&
+			function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'script' )
+		) {
+			$this->type_attr = " type='text/javascript'";
+		}
+
 		/**
 		 * Fires when the WP_Scripts instance is initialized.
 		 *
@@ -156,11 +175,12 @@
 	 * @since 2.1.0
 	 * @since 2.8.0 Added the `$group` parameter.
 	 *
-	 * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints
-	 *                       that script, (array of strings) prints those scripts. Default false.
-	 * @param int   $group   Optional. If scripts were queued in groups prints this group number.
-	 *                       Default false.
-	 * @return array Scripts that have been printed.
+	 * @param string|string[]|false $handles Optional. Scripts to be printed: queue (false),
+	 *                                       single script (string), or multiple scripts (array of strings).
+	 *                                       Default false.
+	 * @param int|false             $group   Optional. Group level: level (int), no groups (false).
+	 *                                       Default false.
+	 * @return string[] Handles of scripts that have been printed.
 	 */
 	public function print_scripts( $handles = false, $group = false ) {
 		return $this->do_items( $handles, $group );
@@ -176,9 +196,10 @@
 	 * @see print_extra_script()
 	 *
 	 * @param string $handle The script's registered handle.
-	 * @param bool   $echo   Optional. Whether to echo the extra script instead of just returning it.
-	 *                       Default true.
-	 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
+	 * @param bool   $echo   Optional. Whether to echo the extra script
+	 *                       instead of just returning it. Default true.
+	 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true,
+	 *                          true otherwise.
 	 */
 	public function print_scripts_l10n( $handle, $echo = true ) {
 		_deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' );
@@ -191,12 +212,14 @@
 	 * @since 3.3.0
 	 *
 	 * @param string $handle The script's registered handle.
-	 * @param bool   $echo   Optional. Whether to echo the extra script instead of just returning it.
-	 *                       Default true.
-	 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
+	 * @param bool   $echo   Optional. Whether to echo the extra script
+	 *                       instead of just returning it. Default true.
+	 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true,
+	 *                          true otherwise.
 	 */
 	public function print_extra_script( $handle, $echo = true ) {
-		if ( ! $output = $this->get_data( $handle, 'data' ) ) {
+		$output = $this->get_data( $handle, 'data' );
+		if ( ! $output ) {
 			return;
 		}
 
@@ -204,10 +227,19 @@
 			return $output;
 		}
 
-		echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5.
-		echo "/* <![CDATA[ */\n";
+		printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) );
+
+		// CDATA is not needed for HTML 5.
+		if ( $this->type_attr ) {
+			echo "/* <![CDATA[ */\n";
+		}
+
 		echo "$output\n";
-		echo "/* ]]> */\n";
+
+		if ( $this->type_attr ) {
+			echo "/* ]]> */\n";
+		}
+
 		echo "</script>\n";
 
 		return true;
@@ -222,7 +254,8 @@
 	 * @see WP_Dependencies::do_item()
 	 *
 	 * @param string    $handle The script's registered handle.
-	 * @param int|false $group  Optional. Group level: (int) level, (false) no groups. Default false.
+	 * @param int|false $group  Optional. Group level: level (int), no groups (false).
+	 *                          Default false.
 	 * @return bool True on success, false on failure.
 	 */
 	public function do_item( $handle, $group = false ) {
@@ -252,7 +285,8 @@
 		}
 
 		$src         = $obj->src;
-		$cond_before = $cond_after = '';
+		$cond_before = '';
+		$cond_after  = '';
 		$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
 
 		if ( $conditional ) {
@@ -264,19 +298,24 @@
 		$after_handle  = $this->print_inline_script( $handle, 'after', false );
 
 		if ( $before_handle ) {
-			$before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle );
+			$before_handle = sprintf( "<script%s id='%s-js-before'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $before_handle );
 		}
 
 		if ( $after_handle ) {
-			$after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
+			$after_handle = sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_handle );
 		}
 
 		if ( $before_handle || $after_handle ) {
-			$inline_script_tag = "{$cond_before}{$before_handle}{$after_handle}{$cond_after}";
+			$inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after;
 		} else {
 			$inline_script_tag = '';
 		}
 
+		$translations = $this->print_translations( $handle, false );
+		if ( $translations ) {
+			$translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations );
+		}
+
 		if ( $this->do_concat ) {
 			/**
 			 * Filters the script loader source.
@@ -288,7 +327,7 @@
 			 */
 			$srce = apply_filters( 'script_loader_src', $src, $handle );
 
-			if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) {
+			if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations ) ) {
 				$this->do_concat = false;
 
 				// Have to print the so-far concatenated scripts right away to maintain the right order.
@@ -330,11 +369,6 @@
 			return true;
 		}
 
-		$translations = $this->print_translations( $handle, false );
-		if ( $translations ) {
-			$translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations );
-		}
-
 		if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
 			$src = $this->base_url . $src;
 		}
@@ -350,7 +384,9 @@
 			return true;
 		}
 
-		$tag = "{$translations}{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
+		$tag  = $translations . $cond_before . $before_handle;
+		$tag .= sprintf( "<script%s src='%s' id='%s-js'></script>\n", $this->type_attr, $src, esc_attr( $handle ) );
+		$tag .= $after_handle . $cond_after;
 
 		/**
 		 * Filters the HTML script tag of an enqueued script.
@@ -377,10 +413,11 @@
 	 *
 	 * @since 4.5.0
 	 *
-	 * @param string $handle   Name of the script to add the inline script to. Must be lowercase.
+	 * @param string $handle   Name of the script to add the inline script to.
+	 *                         Must be lowercase.
 	 * @param string $data     String containing the javascript to be added.
-	 * @param string $position Optional. Whether to add the inline script before the handle
-	 *                         or after. Default 'after'.
+	 * @param string $position Optional. Whether to add the inline script
+	 *                         before the handle or after. Default 'after'.
 	 * @return bool True on success, false on failure.
 	 */
 	public function add_inline_script( $handle, $data, $position = 'after' ) {
@@ -403,11 +440,12 @@
 	 *
 	 * @since 4.5.0
 	 *
-	 * @param string $handle   Name of the script to add the inline script to. Must be lowercase.
-	 * @param string $position Optional. Whether to add the inline script before the handle
-	 *                         or after. Default 'after'.
-	 * @param bool   $echo     Optional. Whether to echo the script instead of just returning it.
-	 *                         Default true.
+	 * @param string $handle   Name of the script to add the inline script to.
+	 *                         Must be lowercase.
+	 * @param string $position Optional. Whether to add the inline script
+	 *                         before the handle or after. Default 'after'.
+	 * @param bool   $echo     Optional. Whether to echo the script
+	 *                         instead of just returning it. Default true.
 	 * @return string|false Script on success, false otherwise.
 	 */
 	public function print_inline_script( $handle, $position = 'after', $echo = true ) {
@@ -420,7 +458,7 @@
 		$output = trim( implode( "\n", $output ), "\n" );
 
 		if ( $echo ) {
-			printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
+			printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
 		}
 
 		return $output;
@@ -437,7 +475,7 @@
 	 * @return bool True on success, false on failure.
 	 */
 	public function localize( $handle, $object_name, $l10n ) {
-		if ( $handle === 'jquery' ) {
+		if ( 'jquery' === $handle ) {
 			$handle = 'jquery-core';
 		}
 
@@ -478,11 +516,12 @@
 	 *
 	 * @param string    $handle    Name of the item. Should be unique.
 	 * @param bool      $recursion Internal flag that calling function was called recursively.
-	 * @param int|false $group     Optional. Group level: (int) level, (false) no groups. Default false.
-	 * @return bool Not already in the group or a lower group
+	 * @param int|false $group     Optional. Group level: level (int), no groups (false).
+	 *                             Default false.
+	 * @return bool Not already in the group or a lower group.
 	 */
 	public function set_group( $handle, $recursion, $group = false ) {
-		if ( isset( $this->registered[ $handle ]->args ) && $this->registered[ $handle ]->args === 1 ) {
+		if ( isset( $this->registered[ $handle ]->args ) && 1 === $this->registered[ $handle ]->args ) {
 			$grp = 1;
 		} else {
 			$grp = (int) $this->get_data( $handle, 'group' );
@@ -526,9 +565,10 @@
 	 *
 	 * @since 5.0.0
 	 *
-	 * @param string $handle Name of the script to add the inline script to. Must be lowercase.
-	 * @param bool   $echo   Optional. Whether to echo the script instead of just returning it.
-	 *                       Default true.
+	 * @param string $handle Name of the script to add the inline script to.
+	 *                       Must be lowercase.
+	 * @param bool   $echo   Optional. Whether to echo the script
+	 *                       instead of just returning it. Default true.
 	 * @return string|false Script on success, false otherwise.
 	 */
 	public function print_translations( $handle, $echo = true ) {
@@ -555,7 +595,7 @@
 JS;
 
 		if ( $echo ) {
-			printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
+			printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output );
 		}
 
 		return $output;
@@ -568,9 +608,11 @@
 	 *
 	 * @see WP_Dependencies::all_deps()
 	 *
-	 * @param mixed     $handles   Item handle and argument (string) or item handles and arguments (array of strings).
-	 * @param bool      $recursion Internal flag that function is calling itself.
-	 * @param int|false $group     Optional. Group level: (int) level, (false) no groups. Default false.
+	 * @param string|string[] $handles   Item handle (string) or item handles (array of strings).
+	 * @param bool            $recursion Optional. Internal flag that function is calling itself.
+	 *                                   Default false.
+	 * @param int|false       $group     Optional. Group level: level (int), no groups (false).
+	 *                                   Default false.
 	 * @return bool True on success, false on failure.
 	 */
 	public function all_deps( $handles, $recursion = false, $group = false ) {
@@ -595,7 +637,7 @@
 	 *
 	 * @see WP_Dependencies::do_items()
 	 *
-	 * @return array Handles of items that have been processed.
+	 * @return string[] Handles of items that have been processed.
 	 */
 	public function do_head_items() {
 		$this->do_items( false, 0 );
@@ -609,7 +651,7 @@
 	 *
 	 * @see WP_Dependencies::do_items()
 	 *
-	 * @return array Handles of items that have been processed.
+	 * @return string[] Handles of items that have been processed.
 	 */
 	public function do_footer_items() {
 		$this->do_items( false, 1 );