wp/wp-includes/functions.wp-scripts.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    36 function _wp_scripts_maybe_doing_it_wrong( $function ) {
    36 function _wp_scripts_maybe_doing_it_wrong( $function ) {
    37 	if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) {
    37 	if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) {
    38 		return;
    38 		return;
    39 	}
    39 	}
    40 
    40 
    41 	_doing_it_wrong( $function, sprintf(
    41 	_doing_it_wrong(
    42 		/* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
    42 		$function,
    43 		__( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    43 		sprintf(
    44 		'<code>wp_enqueue_scripts</code>',
    44 			/* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
    45 		'<code>admin_enqueue_scripts</code>',
    45 			__( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    46 		'<code>login_enqueue_scripts</code>'
    46 			'<code>wp_enqueue_scripts</code>',
    47 	), '3.3.0' );
    47 			'<code>admin_enqueue_scripts</code>',
       
    48 			'<code>login_enqueue_scripts</code>'
       
    49 		),
       
    50 		'3.3.0'
       
    51 	);
    48 }
    52 }
    49 
    53 
    50 /**
    54 /**
    51  * Prints scripts in document head that are in the $handles queue.
    55  * Prints scripts in document head that are in the $handles queue.
    52  *
    56  *
    87 }
    91 }
    88 
    92 
    89 /**
    93 /**
    90  * Adds extra code to a registered script.
    94  * Adds extra code to a registered script.
    91  *
    95  *
    92  * Code will only be added if the script in already in the queue.
    96  * Code will only be added if the script is already in the queue.
    93  * Accepts a string $data containing the Code. If two or more code blocks
    97  * Accepts a string $data containing the Code. If two or more code blocks
    94  * are added to the same script $handle, they will be printed in the order
    98  * are added to the same script $handle, they will be printed in the order
    95  * they were added, i.e. the latter added code can redeclare the previous.
    99  * they were added, i.e. the latter added code can redeclare the previous.
    96  *
   100  *
    97  * @since 4.5.0
   101  * @since 4.5.0
   106  */
   110  */
   107 function wp_add_inline_script( $handle, $data, $position = 'after' ) {
   111 function wp_add_inline_script( $handle, $data, $position = 'after' ) {
   108 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   112 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   109 
   113 
   110 	if ( false !== stripos( $data, '</script>' ) ) {
   114 	if ( false !== stripos( $data, '</script>' ) ) {
   111 		_doing_it_wrong( __FUNCTION__, sprintf(
   115 		_doing_it_wrong(
   112 			/* translators: 1: <script>, 2: wp_add_inline_script() */
   116 			__FUNCTION__,
   113 			__( 'Do not pass %1$s tags to %2$s.' ),
   117 			sprintf(
   114 			'<code>&lt;script&gt;</code>',
   118 				/* translators: 1: <script>, 2: wp_add_inline_script() */
   115 			'<code>wp_add_inline_script()</code>'
   119 				__( 'Do not pass %1$s tags to %2$s.' ),
   116 		), '4.5.0' );
   120 				'<code>&lt;script&gt;</code>',
       
   121 				'<code>wp_add_inline_script()</code>'
       
   122 			),
       
   123 			'4.5.0'
       
   124 		);
   117 		$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
   125 		$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
   118 	}
   126 	}
   119 
   127 
   120 	return wp_scripts()->add_inline_script( $handle, $data, $position );
   128 	return wp_scripts()->add_inline_script( $handle, $data, $position );
   121 }
   129 }
   130  *
   138  *
   131  * @since 2.1.0
   139  * @since 2.1.0
   132  * @since 4.3.0 A return value was added.
   140  * @since 4.3.0 A return value was added.
   133  *
   141  *
   134  * @param string           $handle    Name of the script. Should be unique.
   142  * @param string           $handle    Name of the script. Should be unique.
   135  * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
   143  * @param string|bool      $src       Full URL of the script, or path of the script relative to the WordPress root directory.
       
   144  *                                    If source is set to false, script is an alias of other scripts it depends on.
   136  * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
   145  * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
   137  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
   146  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
   138  *                                    as a query string for cache busting purposes. If version is set to false, a version
   147  *                                    as a query string for cache busting purposes. If version is set to false, a version
   139  *                                    number is automatically added equal to current installed WordPress version.
   148  *                                    number is automatically added equal to current installed WordPress version.
   140  *                                    If set to null, no version is added.
   149  *                                    If set to null, no version is added.
   165  *         key: value,
   174  *         key: value,
   166  *         key: value,
   175  *         key: value,
   167  *         ...
   176  *         ...
   168  *     }
   177  *     }
   169  *
   178  *
   170  *
       
   171  * @see WP_Dependencies::localize()
   179  * @see WP_Dependencies::localize()
   172  * @link https://core.trac.wordpress.org/ticket/11520
   180  * @link https://core.trac.wordpress.org/ticket/11520
   173  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
   181  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
   174  *
   182  *
   175  * @since 2.2.0
   183  * @since 2.2.0
   188 		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   196 		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   189 		return false;
   197 		return false;
   190 	}
   198 	}
   191 
   199 
   192 	return $wp_scripts->localize( $handle, $object_name, $l10n );
   200 	return $wp_scripts->localize( $handle, $object_name, $l10n );
       
   201 }
       
   202 
       
   203 /**
       
   204  * Sets translated strings for a script.
       
   205  *
       
   206  * Works only if the script has already been added.
       
   207  *
       
   208  * @see WP_Scripts::set_translations()
       
   209  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
       
   210  *
       
   211  * @since 5.0.0
       
   212  * @since 5.1.0 The `$domain` parameter was made optional.
       
   213  *
       
   214  * @param string $handle Script handle the textdomain will be attached to.
       
   215  * @param string $domain Optional. Text domain. Default 'default'.
       
   216  * @param string $path   Optional. The full file path to the directory containing translation files.
       
   217  * @return bool True if the text domain was successfully localized, false otherwise.
       
   218  */
       
   219 function wp_set_script_translations( $handle, $domain = 'default', $path = null ) {
       
   220 	global $wp_scripts;
       
   221 	if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
       
   222 		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
       
   223 		return false;
       
   224 	}
       
   225 
       
   226 	return $wp_scripts->set_translations( $handle, $domain, $path );
   193 }
   227 }
   194 
   228 
   195 /**
   229 /**
   196  * Remove a registered script.
   230  * Remove a registered script.
   197  *
   231  *
   214 	$current_filter = current_filter();
   248 	$current_filter = current_filter();
   215 	if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
   249 	if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
   216 		( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
   250 		( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
   217 	) {
   251 	) {
   218 		$no = array(
   252 		$no = array(
   219 			'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
   253 			'jquery',
   220 			'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
   254 			'jquery-core',
   221 			'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
   255 			'jquery-migrate',
   222 			'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
   256 			'jquery-ui-core',
   223 			'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
   257 			'jquery-ui-accordion',
   224 			'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
   258 			'jquery-ui-autocomplete',
       
   259 			'jquery-ui-button',
       
   260 			'jquery-ui-datepicker',
       
   261 			'jquery-ui-dialog',
       
   262 			'jquery-ui-draggable',
       
   263 			'jquery-ui-droppable',
       
   264 			'jquery-ui-menu',
       
   265 			'jquery-ui-mouse',
       
   266 			'jquery-ui-position',
       
   267 			'jquery-ui-progressbar',
       
   268 			'jquery-ui-resizable',
       
   269 			'jquery-ui-selectable',
       
   270 			'jquery-ui-slider',
       
   271 			'jquery-ui-sortable',
       
   272 			'jquery-ui-spinner',
       
   273 			'jquery-ui-tabs',
       
   274 			'jquery-ui-tooltip',
       
   275 			'jquery-ui-widget',
       
   276 			'underscore',
       
   277 			'backbone',
   225 		);
   278 		);
   226 
   279 
   227 		if ( in_array( $handle, $no ) ) {
   280 		if ( in_array( $handle, $no ) ) {
   228 			$message = sprintf(
   281 			$message = sprintf(
   229 				/* translators: 1: script name, 2: wp_enqueue_scripts */
   282 				/* translators: 1: script name, 2: wp_enqueue_scripts */
   264 function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
   317 function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
   265 	$wp_scripts = wp_scripts();
   318 	$wp_scripts = wp_scripts();
   266 
   319 
   267 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   320 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   268 
   321 
   269 
       
   270 	if ( $src || $in_footer ) {
   322 	if ( $src || $in_footer ) {
   271 		$_handle = explode( '?', $handle );
   323 		$_handle = explode( '?', $handle );
   272 
   324 
   273 		if ( $src ) {
   325 		if ( $src ) {
   274 			$wp_scripts->add( $_handle[0], $src, $deps, $ver );
   326 			$wp_scripts->add( $_handle[0], $src, $deps, $ver );
   296 
   348 
   297 	wp_scripts()->dequeue( $handle );
   349 	wp_scripts()->dequeue( $handle );
   298 }
   350 }
   299 
   351 
   300 /**
   352 /**
   301  * Check whether a script has been added to the queue.
   353  * Determines whether a script has been added to the queue.
       
   354  *
       
   355  * For more information on this and similar theme functions, check out
       
   356  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
       
   357  * Conditional Tags} article in the Theme Developer Handbook.
   302  *
   358  *
   303  * @since 2.8.0
   359  * @since 2.8.0
   304  * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
   360  * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
   305  *
   361  *
   306  * @param string $handle Name of the script.
   362  * @param string $handle Name of the script.
   322  * Possible values for $key and $value:
   378  * Possible values for $key and $value:
   323  * 'conditional' string Comments for IE 6, lte IE 7, etc.
   379  * 'conditional' string Comments for IE 6, lte IE 7, etc.
   324  *
   380  *
   325  * @since 4.2.0
   381  * @since 4.2.0
   326  *
   382  *
   327  * @see WP_Dependency::add_data()
   383  * @see WP_Dependencies::add_data()
   328  *
   384  *
   329  * @param string $handle Name of the script.
   385  * @param string $handle Name of the script.
   330  * @param string $key    Name of data point for which we're storing a value.
   386  * @param string $key    Name of data point for which we're storing a value.
   331  * @param mixed  $value  String containing the data to be added.
   387  * @param mixed  $value  String containing the data to be added.
   332  * @return bool True on success, false on failure.
   388  * @return bool True on success, false on failure.
   333  */
   389  */
   334 function wp_script_add_data( $handle, $key, $value ){
   390 function wp_script_add_data( $handle, $key, $value ) {
   335 	return wp_scripts()->add_data( $handle, $key, $value );
   391 	return wp_scripts()->add_data( $handle, $key, $value );
   336 }
   392 }