wp/wp-includes/functions.wp-scripts.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * BackPress Scripts Procedural API
     3  * Dependencies API: Scripts functions
     4  *
     4  *
     5  * @since 2.6.0
     5  * @since 2.6.0
     6  *
     6  *
     7  * @package WordPress
     7  * @package WordPress
     8  * @subpackage BackPress
     8  * @subpackage Dependencies
     9  */
     9  */
    10 
    10 
    11 /**
    11 /**
    12  * Initialize $wp_scripts if it has not been set.
    12  * Initialize $wp_scripts if it has not been set.
    13  *
    13  *
    32  * @since 4.2.0
    32  * @since 4.2.0
    33  *
    33  *
    34  * @param string $function Function name.
    34  * @param string $function Function name.
    35  */
    35  */
    36 function _wp_scripts_maybe_doing_it_wrong( $function ) {
    36 function _wp_scripts_maybe_doing_it_wrong( $function ) {
    37 	if ( did_action( 'init' ) ) {
    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( $function, sprintf(
       
    42 		/* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
    42 		__( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    43 		__( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    43 		'<code>wp_enqueue_scripts</code>',
    44 		'<code>wp_enqueue_scripts</code>',
    44 		'<code>admin_enqueue_scripts</code>',
    45 		'<code>admin_enqueue_scripts</code>',
    45 		'<code>login_enqueue_scripts</code>'
    46 		'<code>login_enqueue_scripts</code>'
    46 	), '3.3' );
    47 	), '3.3.0' );
    47 }
    48 }
    48 
    49 
    49 /**
    50 /**
    50  * Print scripts in document head that are in the $handles queue.
    51  * Prints scripts in document head that are in the $handles queue.
    51  *
    52  *
    52  * Called by admin-header.php and wp_head hook. Since it is called by wp_head on every page load,
    53  * Called by admin-header.php and {@see 'wp_head'} hook. Since it is called by wp_head on every page load,
    53  * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
    54  * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
    54  * Makes use of already-instantiated $wp_scripts global if present. Use provided wp_print_scripts
    55  * Makes use of already-instantiated $wp_scripts global if present. Use provided {@see 'wp_print_scripts'}
    55  * hook to register/enqueue new scripts.
    56  * hook to register/enqueue new scripts.
    56  *
    57  *
    57  * @see WP_Scripts::do_items()
    58  * @see WP_Scripts::do_items()
    58  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
    59  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
    59  *
    60  *
    60  * @since 2.6.0
    61  * @since 2.1.0
    61  *
    62  *
    62  * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'.
    63  * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'.
    63  * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
    64  * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
    64  */
    65  */
    65 function wp_print_scripts( $handles = false ) {
    66 function wp_print_scripts( $handles = false ) {
    84 
    85 
    85 	return wp_scripts()->do_items( $handles );
    86 	return wp_scripts()->do_items( $handles );
    86 }
    87 }
    87 
    88 
    88 /**
    89 /**
       
    90  * Adds extra code to a registered script.
       
    91  *
       
    92  * Code will only be added if the script in already in the queue.
       
    93  * 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
       
    95  * they were added, i.e. the latter added code can redeclare the previous.
       
    96  *
       
    97  * @since 4.5.0
       
    98  *
       
    99  * @see WP_Scripts::add_inline_script()
       
   100  *
       
   101  * @param string $handle   Name of the script to add the inline script to.
       
   102  * @param string $data     String containing the javascript to be added.
       
   103  * @param string $position Optional. Whether to add the inline script before the handle
       
   104  *                         or after. Default 'after'.
       
   105  * @return bool True on success, false on failure.
       
   106  */
       
   107 function wp_add_inline_script( $handle, $data, $position = 'after' ) {
       
   108 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
       
   109 
       
   110 	if ( false !== stripos( $data, '</script>' ) ) {
       
   111 		_doing_it_wrong( __FUNCTION__, sprintf(
       
   112 			/* translators: 1: <script>, 2: wp_add_inline_script() */
       
   113 			__( 'Do not pass %1$s tags to %2$s.' ),
       
   114 			'<code>&lt;script&gt;</code>',
       
   115 			'<code>wp_add_inline_script()</code>'
       
   116 		), '4.5.0' );
       
   117 		$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
       
   118 	}
       
   119 
       
   120 	return wp_scripts()->add_inline_script( $handle, $data, $position );
       
   121 }
       
   122 
       
   123 /**
    89  * Register a new script.
   124  * Register a new script.
    90  *
   125  *
    91  * Registers a script to be linked later using the wp_enqueue_script() function.
   126  * Registers a script to be enqueued later using the wp_enqueue_script() function.
    92  *
   127  *
    93  * @see WP_Dependencies::add(), WP_Dependencies::add_data()
   128  * @see WP_Dependencies::add()
    94  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
   129  * @see WP_Dependencies::add_data()
    95  *
   130  *
    96  * @since 2.6.0
   131  * @since 2.1.0
    97  *
   132  * @since 4.3.0 A return value was added.
    98  * @param string      $handle    Name of the script. Should be unique.
   133  *
    99  * @param string      $src       Path to the script from the WordPress root directory. Example: '/js/myscript.js'.
   134  * @param string           $handle    Name of the script. Should be unique.
   100  * @param array       $deps      Optional. An array of registered script handles this script depends on. Set to false if there
   135  * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
   101  *                               are no dependencies. Default empty array.
   136  * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
   102  * @param string|bool $ver       Optional. String specifying script version number, if it has one, which is concatenated
   137  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
   103  *                               to end of path as a query string. If no version is specified or set to false, a version
   138  *                                    as a query string for cache busting purposes. If version is set to false, a version
   104  *                               number is automatically added equal to current installed WordPress version.
   139  *                                    number is automatically added equal to current installed WordPress version.
   105  *                               If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'.
   140  *                                    If set to null, no version is added.
   106  * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
   141  * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
   107  *                               Default 'false'. Accepts 'false' or 'true'.
   142  *                                    Default 'false'.
       
   143  * @return bool Whether the script has been registered. True on success, false on failure.
   108  */
   144  */
   109 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
   145 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
   110 	$wp_scripts = wp_scripts();
   146 	$wp_scripts = wp_scripts();
   111 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   147 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   112 
   148 
   113 	$wp_scripts->add( $handle, $src, $deps, $ver );
   149 	$registered = $wp_scripts->add( $handle, $src, $deps, $ver );
   114 	if ( $in_footer ) {
   150 	if ( $in_footer ) {
   115 		$wp_scripts->add_data( $handle, 'group', 1 );
   151 		$wp_scripts->add_data( $handle, 'group', 1 );
   116 	}
   152 	}
       
   153 
       
   154 	return $registered;
   117 }
   155 }
   118 
   156 
   119 /**
   157 /**
   120  * Localize a script.
   158  * Localize a script.
   121  *
   159  *
   132  *
   170  *
   133  * @see WP_Dependencies::localize()
   171  * @see WP_Dependencies::localize()
   134  * @link https://core.trac.wordpress.org/ticket/11520
   172  * @link https://core.trac.wordpress.org/ticket/11520
   135  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
   173  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
   136  *
   174  *
   137  * @since 2.6.0
   175  * @since 2.2.0
   138  *
   176  *
   139  * @todo Documentation cleanup
   177  * @todo Documentation cleanup
   140  *
   178  *
   141  * @param string $handle      Script handle the data will be attached to.
   179  * @param string $handle      Script handle the data will be attached to.
   142  * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
   180  * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
   149 	if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
   187 	if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
   150 		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   188 		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   151 		return false;
   189 		return false;
   152 	}
   190 	}
   153 
   191 
   154 	return wp_scripts()->localize( $handle, $object_name, $l10n );
   192 	return $wp_scripts->localize( $handle, $object_name, $l10n );
   155 }
   193 }
   156 
   194 
   157 /**
   195 /**
   158  * Remove a registered script.
   196  * Remove a registered script.
   159  *
   197  *
   160  * Note: there are intentional safeguards in place to prevent critical admin scripts,
   198  * Note: there are intentional safeguards in place to prevent critical admin scripts,
   161  * such as jQuery core, from being unregistered.
   199  * such as jQuery core, from being unregistered.
   162  *
   200  *
   163  * @see WP_Dependencies::remove()
   201  * @see WP_Dependencies::remove()
   164  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
   202  *
   165  *
   203  * @since 2.1.0
   166  * @since 2.6.0
       
   167  *
   204  *
   168  * @param string $handle Name of the script to be removed.
   205  * @param string $handle Name of the script to be removed.
   169  */
   206  */
   170 function wp_deregister_script( $handle ) {
   207 function wp_deregister_script( $handle ) {
   171 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   208 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   186 			'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
   223 			'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
   187 			'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
   224 			'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
   188 		);
   225 		);
   189 
   226 
   190 		if ( in_array( $handle, $no ) ) {
   227 		if ( in_array( $handle, $no ) ) {
   191 			$message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the frontend theme, use the %2$s hook.' ),
   228 			$message = sprintf(
   192 				"<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
   229 				/* translators: 1: script name, 2: wp_enqueue_scripts */
   193 			_doing_it_wrong( __FUNCTION__, $message, '3.6' );
   230 				__( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ),
       
   231 				"<code>$handle</code>",
       
   232 				'<code>wp_enqueue_scripts</code>'
       
   233 			);
       
   234 			_doing_it_wrong( __FUNCTION__, $message, '3.6.0' );
   194 			return;
   235 			return;
   195 		}
   236 		}
   196 	}
   237 	}
   197 
   238 
   198 	wp_scripts()->remove( $handle );
   239 	wp_scripts()->remove( $handle );
   201 /**
   242 /**
   202  * Enqueue a script.
   243  * Enqueue a script.
   203  *
   244  *
   204  * Registers the script if $src provided (does NOT overwrite), and enqueues it.
   245  * Registers the script if $src provided (does NOT overwrite), and enqueues it.
   205  *
   246  *
   206  * @see WP_Dependencies::add(), WP_Dependencies::add_data(), WP_Dependencies::enqueue()
   247  * @see WP_Dependencies::add()
   207  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
   248  * @see WP_Dependencies::add_data()
   208  *
   249  * @see WP_Dependencies::enqueue()
   209  * @since 2.6.0
   250  *
   210  *
   251  * @since 2.1.0
   211  * @param string      $handle    Name of the script.
   252  *
   212  * @param string|bool $src       Path to the script from the root directory of WordPress. Example: '/js/myscript.js'.
   253  * @param string           $handle    Name of the script. Should be unique.
   213  * @param array       $deps      An array of registered handles this script depends on. Default empty array.
   254  * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
   214  * @param string|bool $ver       Optional. String specifying the script version number, if it has one. This parameter
   255  *                                    Default empty.
   215  *                               is used to ensure that the correct version is sent to the client regardless of caching,
   256  * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
   216  *                               and so should be included if a version number is available and makes sense for the script.
   257  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
   217  * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
   258  *                                    as a query string for cache busting purposes. If version is set to false, a version
   218  *                               Default 'false'. Accepts 'false' or 'true'.
   259  *                                    number is automatically added equal to current installed WordPress version.
   219  */
   260  *                                    If set to null, no version is added.
   220 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
   261  * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
       
   262  *                                    Default 'false'.
       
   263  */
       
   264 function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
   221 	$wp_scripts = wp_scripts();
   265 	$wp_scripts = wp_scripts();
   222 
   266 
   223 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   267 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   224 
   268 
   225 
   269 
   240 
   284 
   241 /**
   285 /**
   242  * Remove a previously enqueued script.
   286  * Remove a previously enqueued script.
   243  *
   287  *
   244  * @see WP_Dependencies::dequeue()
   288  * @see WP_Dependencies::dequeue()
   245  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
       
   246  *
   289  *
   247  * @since 3.1.0
   290  * @since 3.1.0
   248  *
   291  *
   249  * @param string $handle Name of the script to be removed.
   292  * @param string $handle Name of the script to be removed.
   250  */
   293  */
   254 	wp_scripts()->dequeue( $handle );
   297 	wp_scripts()->dequeue( $handle );
   255 }
   298 }
   256 
   299 
   257 /**
   300 /**
   258  * Check whether a script has been added to the queue.
   301  * Check whether a script has been added to the queue.
   259  *
       
   260  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
       
   261  *
   302  *
   262  * @since 2.8.0
   303  * @since 2.8.0
   263  * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
   304  * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
   264  *
   305  *
   265  * @param string $handle Name of the script.
   306  * @param string $handle Name of the script.
   266  * @param string $list   Optional. Status of the script to check. Default 'enqueued'.
   307  * @param string $list   Optional. Status of the script to check. Default 'enqueued'.
   267  *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
   308  *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
   268  * @return bool Whether the script script is queued.
   309  * @return bool Whether the script is queued.
   269  */
   310  */
   270 function wp_script_is( $handle, $list = 'enqueued' ) {
   311 function wp_script_is( $handle, $list = 'enqueued' ) {
   271 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   312 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   272 
   313 
   273 	return (bool) wp_scripts()->query( $handle, $list );
   314 	return (bool) wp_scripts()->query( $handle, $list );
   289  * @param string $key    Name of data point for which we're storing a value.
   330  * @param string $key    Name of data point for which we're storing a value.
   290  * @param mixed  $value  String containing the data to be added.
   331  * @param mixed  $value  String containing the data to be added.
   291  * @return bool True on success, false on failure.
   332  * @return bool True on success, false on failure.
   292  */
   333  */
   293 function wp_script_add_data( $handle, $key, $value ){
   334 function wp_script_add_data( $handle, $key, $value ){
   294 	global $wp_scripts;
   335 	return wp_scripts()->add_data( $handle, $key, $value );
   295 	return $wp_scripts->add_data( $handle, $key, $value );
   336 }
   296 }