wp/wp-includes/class.wp-scripts.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   123 	public $default_dirs;
   123 	public $default_dirs;
   124 
   124 
   125 	/**
   125 	/**
   126 	 * Holds a string which contains the type attribute for script tag.
   126 	 * Holds a string which contains the type attribute for script tag.
   127 	 *
   127 	 *
   128 	 * If the current theme does not declare HTML5 support for 'script',
   128 	 * If the active theme does not declare HTML5 support for 'script',
   129 	 * then it initializes as `type='text/javascript'`.
   129 	 * then it initializes as `type='text/javascript'`.
   130 	 *
   130 	 *
   131 	 * @since 5.3.0
   131 	 * @since 5.3.0
   132 	 * @var string
   132 	 * @var string
   133 	 */
   133 	 */
   160 		/**
   160 		/**
   161 		 * Fires when the WP_Scripts instance is initialized.
   161 		 * Fires when the WP_Scripts instance is initialized.
   162 		 *
   162 		 *
   163 		 * @since 2.6.0
   163 		 * @since 2.6.0
   164 		 *
   164 		 *
   165 		 * @param WP_Scripts $this WP_Scripts instance (passed by reference).
   165 		 * @param WP_Scripts $wp_scripts WP_Scripts instance (passed by reference).
   166 		 */
   166 		 */
   167 		do_action_ref_array( 'wp_default_scripts', array( &$this ) );
   167 		do_action_ref_array( 'wp_default_scripts', array( &$this ) );
   168 	}
   168 	}
   169 
   169 
   170 	/**
   170 	/**
   188 
   188 
   189 	/**
   189 	/**
   190 	 * Prints extra scripts of a registered script.
   190 	 * Prints extra scripts of a registered script.
   191 	 *
   191 	 *
   192 	 * @since 2.1.0
   192 	 * @since 2.1.0
   193 	 * @since 2.8.0 Added the `$echo` parameter.
   193 	 * @since 2.8.0 Added the `$display` parameter.
   194 	 * @deprecated 3.3.0
   194 	 * @deprecated 3.3.0
   195 	 *
   195 	 *
   196 	 * @see print_extra_script()
   196 	 * @see print_extra_script()
   197 	 *
   197 	 *
   198 	 * @param string $handle The script's registered handle.
   198 	 * @param string $handle  The script's registered handle.
   199 	 * @param bool   $echo   Optional. Whether to echo the extra script
   199 	 * @param bool   $display Optional. Whether to print the extra script
   200 	 *                       instead of just returning it. Default true.
   200 	 *                        instead of just returning it. Default true.
   201 	 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true,
   201 	 * @return bool|string|void Void if no data exists, extra scripts if `$display` is true,
   202 	 *                          true otherwise.
   202 	 *                          true otherwise.
   203 	 */
   203 	 */
   204 	public function print_scripts_l10n( $handle, $echo = true ) {
   204 	public function print_scripts_l10n( $handle, $display = true ) {
   205 		_deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' );
   205 		_deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' );
   206 		return $this->print_extra_script( $handle, $echo );
   206 		return $this->print_extra_script( $handle, $display );
   207 	}
   207 	}
   208 
   208 
   209 	/**
   209 	/**
   210 	 * Prints extra scripts of a registered script.
   210 	 * Prints extra scripts of a registered script.
   211 	 *
   211 	 *
   212 	 * @since 3.3.0
   212 	 * @since 3.3.0
   213 	 *
   213 	 *
   214 	 * @param string $handle The script's registered handle.
   214 	 * @param string $handle  The script's registered handle.
   215 	 * @param bool   $echo   Optional. Whether to echo the extra script
   215 	 * @param bool   $display Optional. Whether to print the extra script
   216 	 *                       instead of just returning it. Default true.
   216 	 *                        instead of just returning it. Default true.
   217 	 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true,
   217 	 * @return bool|string|void Void if no data exists, extra scripts if `$display` is true,
   218 	 *                          true otherwise.
   218 	 *                          true otherwise.
   219 	 */
   219 	 */
   220 	public function print_extra_script( $handle, $echo = true ) {
   220 	public function print_extra_script( $handle, $display = true ) {
   221 		$output = $this->get_data( $handle, 'data' );
   221 		$output = $this->get_data( $handle, 'data' );
   222 		if ( ! $output ) {
   222 		if ( ! $output ) {
   223 			return;
   223 			return;
   224 		}
   224 		}
   225 
   225 
   226 		if ( ! $echo ) {
   226 		if ( ! $display ) {
   227 			return $output;
   227 			return $output;
   228 		}
   228 		}
   229 
   229 
   230 		printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) );
   230 		printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) );
   231 
   231 
   308 		if ( $before_handle || $after_handle ) {
   308 		if ( $before_handle || $after_handle ) {
   309 			$inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after;
   309 			$inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after;
   310 		} else {
   310 		} else {
   311 			$inline_script_tag = '';
   311 			$inline_script_tag = '';
   312 		}
   312 		}
       
   313 
       
   314 		/*
       
   315 		 * Prevent concatenation of scripts if the text domain is defined
       
   316 		 * to ensure the dependency order is respected.
       
   317 		 */
       
   318 		$translations_stop_concat = ! empty( $obj->textdomain );
   313 
   319 
   314 		$translations = $this->print_translations( $handle, false );
   320 		$translations = $this->print_translations( $handle, false );
   315 		if ( $translations ) {
   321 		if ( $translations ) {
   316 			$translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations );
   322 			$translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations );
   317 		}
   323 		}
   325 			 * @param string $src    Script loader source path.
   331 			 * @param string $src    Script loader source path.
   326 			 * @param string $handle Script handle.
   332 			 * @param string $handle Script handle.
   327 			 */
   333 			 */
   328 			$srce = apply_filters( 'script_loader_src', $src, $handle );
   334 			$srce = apply_filters( 'script_loader_src', $src, $handle );
   329 
   335 
   330 			if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations ) ) {
   336 			if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations_stop_concat ) ) {
   331 				$this->do_concat = false;
   337 				$this->do_concat = false;
   332 
   338 
   333 				// Have to print the so-far concatenated scripts right away to maintain the right order.
   339 				// Have to print the so-far concatenated scripts right away to maintain the right order.
   334 				_print_scripts();
   340 				_print_scripts();
   335 				$this->reset();
   341 				$this->reset();
   442 	 *
   448 	 *
   443 	 * @param string $handle   Name of the script to add the inline script to.
   449 	 * @param string $handle   Name of the script to add the inline script to.
   444 	 *                         Must be lowercase.
   450 	 *                         Must be lowercase.
   445 	 * @param string $position Optional. Whether to add the inline script
   451 	 * @param string $position Optional. Whether to add the inline script
   446 	 *                         before the handle or after. Default 'after'.
   452 	 *                         before the handle or after. Default 'after'.
   447 	 * @param bool   $echo     Optional. Whether to echo the script
   453 	 * @param bool   $display  Optional. Whether to print the script
   448 	 *                         instead of just returning it. Default true.
   454 	 *                         instead of just returning it. Default true.
   449 	 * @return string|false Script on success, false otherwise.
   455 	 * @return string|false Script on success, false otherwise.
   450 	 */
   456 	 */
   451 	public function print_inline_script( $handle, $position = 'after', $echo = true ) {
   457 	public function print_inline_script( $handle, $position = 'after', $display = true ) {
   452 		$output = $this->get_data( $handle, $position );
   458 		$output = $this->get_data( $handle, $position );
   453 
   459 
   454 		if ( empty( $output ) ) {
   460 		if ( empty( $output ) ) {
   455 			return false;
   461 			return false;
   456 		}
   462 		}
   457 
   463 
   458 		$output = trim( implode( "\n", $output ), "\n" );
   464 		$output = trim( implode( "\n", $output ), "\n" );
   459 
   465 
   460 		if ( $echo ) {
   466 		if ( $display ) {
   461 			printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
   467 			printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
   462 		}
   468 		}
   463 
   469 
   464 		return $output;
   470 		return $output;
   465 	}
   471 	}
   580 	/**
   586 	/**
   581 	 * Prints translations set for a specific handle.
   587 	 * Prints translations set for a specific handle.
   582 	 *
   588 	 *
   583 	 * @since 5.0.0
   589 	 * @since 5.0.0
   584 	 *
   590 	 *
   585 	 * @param string $handle Name of the script to add the inline script to.
   591 	 * @param string $handle  Name of the script to add the inline script to.
   586 	 *                       Must be lowercase.
   592 	 *                        Must be lowercase.
   587 	 * @param bool   $echo   Optional. Whether to echo the script
   593 	 * @param bool   $display Optional. Whether to print the script
   588 	 *                       instead of just returning it. Default true.
   594 	 *                        instead of just returning it. Default true.
   589 	 * @return string|false Script on success, false otherwise.
   595 	 * @return string|false Script on success, false otherwise.
   590 	 */
   596 	 */
   591 	public function print_translations( $handle, $echo = true ) {
   597 	public function print_translations( $handle, $display = true ) {
   592 		if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
   598 		if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
   593 			return false;
   599 			return false;
   594 		}
   600 		}
   595 
   601 
   596 		$domain = $this->registered[ $handle ]->textdomain;
   602 		$domain = $this->registered[ $handle ]->textdomain;
   597 		$path   = $this->registered[ $handle ]->translations_path;
   603 		$path   = $this->registered[ $handle ]->translations_path;
   598 
   604 
   599 		$json_translations = load_script_textdomain( $handle, $domain, $path );
   605 		$json_translations = load_script_textdomain( $handle, $domain, $path );
   600 
   606 
   601 		if ( ! $json_translations ) {
   607 		if ( ! $json_translations ) {
   602 			// Register empty locale data object to ensure the domain still exists.
   608 			return false;
   603 			$json_translations = '{ "locale_data": { "messages": { "": {} } } }';
       
   604 		}
   609 		}
   605 
   610 
   606 		$output = <<<JS
   611 		$output = <<<JS
   607 ( function( domain, translations ) {
   612 ( function( domain, translations ) {
   608 	var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
   613 	var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
   609 	localeData[""].domain = domain;
   614 	localeData[""].domain = domain;
   610 	wp.i18n.setLocaleData( localeData, domain );
   615 	wp.i18n.setLocaleData( localeData, domain );
   611 } )( "{$domain}", {$json_translations} );
   616 } )( "{$domain}", {$json_translations} );
   612 JS;
   617 JS;
   613 
   618 
   614 		if ( $echo ) {
   619 		if ( $display ) {
   615 			printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output );
   620 			printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output );
   616 		}
   621 		}
   617 
   622 
   618 		return $output;
   623 		return $output;
   619 	}
   624 	}