wp/wp-includes/functions.wp-scripts.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
--- a/wp/wp-includes/functions.wp-scripts.php	Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-includes/functions.wp-scripts.php	Tue Dec 15 13:49:49 2020 +0100
@@ -19,9 +19,11 @@
  */
 function wp_scripts() {
 	global $wp_scripts;
+
 	if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
 		$wp_scripts = new WP_Scripts();
 	}
+
 	return $wp_scripts;
 }
 
@@ -30,23 +32,38 @@
  *
  * @ignore
  * @since 4.2.0
+ * @since 5.5.0 Added the `$handle` parameter.
  *
  * @param string $function Function name.
+ * @param string $handle   Optional. Name of the script or stylesheet that was
+ *                         registered or enqueued too early. Default empty.
  */
-function _wp_scripts_maybe_doing_it_wrong( $function ) {
-	if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) {
+function _wp_scripts_maybe_doing_it_wrong( $function, $handle = '' ) {
+	if ( did_action( 'init' ) || did_action( 'wp_enqueue_scripts' )
+		|| did_action( 'admin_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' )
+	) {
 		return;
 	}
 
+	$message = sprintf(
+		/* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
+		__( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
+		'<code>wp_enqueue_scripts</code>',
+		'<code>admin_enqueue_scripts</code>',
+		'<code>login_enqueue_scripts</code>'
+	);
+
+	if ( $handle ) {
+		$message .= ' ' . sprintf(
+			/* translators: %s: Name of the script or stylesheet. */
+			__( 'This notice was triggered by the %s handle.' ),
+			'<code>' . $handle . '</code>'
+		);
+	}
+
 	_doing_it_wrong(
 		$function,
-		sprintf(
-			/* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
-			__( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
-			'<code>wp_enqueue_scripts</code>',
-			'<code>admin_enqueue_scripts</code>',
-			'<code>login_enqueue_scripts</code>'
-		),
+		$message,
 		'3.3.0'
 	);
 }
@@ -59,28 +76,30 @@
  * Makes use of already-instantiated $wp_scripts global if present. Use provided {@see 'wp_print_scripts'}
  * hook to register/enqueue new scripts.
  *
- * @see WP_Scripts::do_items()
+ * @see WP_Scripts::do_item()
  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
  *
  * @since 2.1.0
  *
  * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'.
- * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
+ * @return string[] On success, an array of handles of processed WP_Dependencies items; otherwise, an empty array.
  */
 function wp_print_scripts( $handles = false ) {
+	global $wp_scripts;
+
 	/**
 	 * Fires before scripts in the $handles queue are printed.
 	 *
 	 * @since 2.1.0
 	 */
 	do_action( 'wp_print_scripts' );
-	if ( '' === $handles ) { // for wp_head
+
+	if ( '' === $handles ) { // For 'wp_head'.
 		$handles = false;
 	}
 
 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 
-	global $wp_scripts;
 	if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
 		if ( ! $handles ) {
 			return array(); // No need to instantiate if nothing is there.
@@ -109,7 +128,7 @@
  * @return bool True on success, false on failure.
  */
 function wp_add_inline_script( $handle, $data, $position = 'after' ) {
-	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
+	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
 
 	if ( false !== stripos( $data, '</script>' ) ) {
 		_doing_it_wrong(
@@ -142,7 +161,7 @@
  * @param string           $handle    Name of the script. Should be unique.
  * @param string|bool      $src       Full URL of the script, or path of the script relative to the WordPress root directory.
  *                                    If source is set to false, script is an alias of other scripts it depends on.
- * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
+ * @param string[]         $deps      Optional. An array of registered script handles this script depends on. Default empty array.
  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
  *                                    as a query string for cache busting purposes. If version is set to false, a version
  *                                    number is automatically added equal to current installed WordPress version.
@@ -152,8 +171,9 @@
  * @return bool Whether the script has been registered. True on success, false on failure.
  */
 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
+	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
+
 	$wp_scripts = wp_scripts();
-	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
 
 	$registered = $wp_scripts->add( $handle, $src, $deps, $ver );
 	if ( $in_footer ) {
@@ -176,7 +196,7 @@
  *         ...
  *     }
  *
- * @see WP_Dependencies::localize()
+ * @see WP_Scripts::localize()
  * @link https://core.trac.wordpress.org/ticket/11520
  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
  *
@@ -187,13 +207,14 @@
  * @param string $handle      Script handle the data will be attached to.
  * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
  *                            Example: '/[a-zA-Z0-9_]+/'.
- * @param array $l10n         The data itself. The data can be either a single or multi-dimensional array.
+ * @param array  $l10n        The data itself. The data can be either a single or multi-dimensional array.
  * @return bool True if the script was successfully localized, false otherwise.
  */
 function wp_localize_script( $handle, $object_name, $l10n ) {
 	global $wp_scripts;
+
 	if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
-		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
+		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
 		return false;
 	}
 
@@ -218,8 +239,9 @@
  */
 function wp_set_script_translations( $handle, $domain = 'default', $path = null ) {
 	global $wp_scripts;
+
 	if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
-		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
+		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
 		return false;
 	}
 
@@ -239,7 +261,7 @@
  * @param string $handle Name of the script to be removed.
  */
 function wp_deregister_script( $handle ) {
-	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
+	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
 
 	/**
 	 * Do not allow accidental or negligent de-registering of critical scripts in the admin.
@@ -249,7 +271,7 @@
 	if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
 		( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
 	) {
-		$no = array(
+		$not_allowed = array(
 			'jquery',
 			'jquery-core',
 			'jquery-migrate',
@@ -277,9 +299,9 @@
 			'backbone',
 		);
 
-		if ( in_array( $handle, $no ) ) {
+		if ( in_array( $handle, $not_allowed, true ) ) {
 			$message = sprintf(
-				/* translators: 1: script name, 2: wp_enqueue_scripts */
+				/* translators: 1: Script name, 2: wp_enqueue_scripts */
 				__( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ),
 				"<code>$handle</code>",
 				'<code>wp_enqueue_scripts</code>'
@@ -306,7 +328,7 @@
  * @param string           $handle    Name of the script. Should be unique.
  * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
  *                                    Default empty.
- * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
+ * @param string[]         $deps      Optional. An array of registered script handles this script depends on. Default empty array.
  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
  *                                    as a query string for cache busting purposes. If version is set to false, a version
  *                                    number is automatically added equal to current installed WordPress version.
@@ -315,9 +337,9 @@
  *                                    Default 'false'.
  */
 function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
-	$wp_scripts = wp_scripts();
+	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
 
-	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
+	$wp_scripts = wp_scripts();
 
 	if ( $src || $in_footer ) {
 		$_handle = explode( '?', $handle );
@@ -344,7 +366,7 @@
  * @param string $handle Name of the script to be removed.
  */
 function wp_dequeue_script( $handle ) {
-	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
+	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
 
 	wp_scripts()->dequeue( $handle );
 }
@@ -365,7 +387,7 @@
  * @return bool Whether the script is queued.
  */
 function wp_script_is( $handle, $list = 'enqueued' ) {
-	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
+	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
 
 	return (bool) wp_scripts()->query( $handle, $list );
 }