functions.wp-scripts.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * Dependencies API: Scripts functions
  4. *
  5. * @since 2.6.0
  6. *
  7. * @package WordPress
  8. * @subpackage Dependencies
  9. */
  10. /**
  11. * Initialize $wp_scripts if it has not been set.
  12. *
  13. * @global WP_Scripts $wp_scripts
  14. *
  15. * @since 4.2.0
  16. *
  17. * @return WP_Scripts WP_Scripts instance.
  18. */
  19. function wp_scripts() {
  20. global $wp_scripts;
  21. if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
  22. $wp_scripts = new WP_Scripts();
  23. }
  24. return $wp_scripts;
  25. }
  26. /**
  27. * Helper function to output a _doing_it_wrong message when applicable.
  28. *
  29. * @ignore
  30. * @since 4.2.0
  31. *
  32. * @param string $function Function name.
  33. */
  34. function _wp_scripts_maybe_doing_it_wrong( $function ) {
  35. if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) {
  36. return;
  37. }
  38. _doing_it_wrong(
  39. $function,
  40. sprintf(
  41. /* 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. '<code>wp_enqueue_scripts</code>',
  44. '<code>admin_enqueue_scripts</code>',
  45. '<code>login_enqueue_scripts</code>'
  46. ),
  47. '3.3.0'
  48. );
  49. }
  50. /**
  51. * Prints scripts in document head that are in the $handles queue.
  52. *
  53. * Called by admin-header.php and {@see 'wp_head'} hook. Since it is called by wp_head on every page load,
  54. * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
  55. * Makes use of already-instantiated $wp_scripts global if present. Use provided {@see 'wp_print_scripts'}
  56. * hook to register/enqueue new scripts.
  57. *
  58. * @see WP_Scripts::do_item()
  59. * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
  60. *
  61. * @since 2.1.0
  62. *
  63. * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'.
  64. * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
  65. */
  66. function wp_print_scripts( $handles = false ) {
  67. /**
  68. * Fires before scripts in the $handles queue are printed.
  69. *
  70. * @since 2.1.0
  71. */
  72. do_action( 'wp_print_scripts' );
  73. if ( '' === $handles ) { // for wp_head
  74. $handles = false;
  75. }
  76. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  77. global $wp_scripts;
  78. if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
  79. if ( ! $handles ) {
  80. return array(); // No need to instantiate if nothing is there.
  81. }
  82. }
  83. return wp_scripts()->do_items( $handles );
  84. }
  85. /**
  86. * Adds extra code to a registered script.
  87. *
  88. * Code will only be added if the script is already in the queue.
  89. * Accepts a string $data containing the Code. If two or more code blocks
  90. * are added to the same script $handle, they will be printed in the order
  91. * they were added, i.e. the latter added code can redeclare the previous.
  92. *
  93. * @since 4.5.0
  94. *
  95. * @see WP_Scripts::add_inline_script()
  96. *
  97. * @param string $handle Name of the script to add the inline script to.
  98. * @param string $data String containing the javascript to be added.
  99. * @param string $position Optional. Whether to add the inline script before the handle
  100. * or after. Default 'after'.
  101. * @return bool True on success, false on failure.
  102. */
  103. function wp_add_inline_script( $handle, $data, $position = 'after' ) {
  104. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  105. if ( false !== stripos( $data, '</script>' ) ) {
  106. _doing_it_wrong(
  107. __FUNCTION__,
  108. sprintf(
  109. /* translators: 1: <script>, 2: wp_add_inline_script() */
  110. __( 'Do not pass %1$s tags to %2$s.' ),
  111. '<code>&lt;script&gt;</code>',
  112. '<code>wp_add_inline_script()</code>'
  113. ),
  114. '4.5.0'
  115. );
  116. $data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
  117. }
  118. return wp_scripts()->add_inline_script( $handle, $data, $position );
  119. }
  120. /**
  121. * Register a new script.
  122. *
  123. * Registers a script to be enqueued later using the wp_enqueue_script() function.
  124. *
  125. * @see WP_Dependencies::add()
  126. * @see WP_Dependencies::add_data()
  127. *
  128. * @since 2.1.0
  129. * @since 4.3.0 A return value was added.
  130. *
  131. * @param string $handle Name of the script. Should be unique.
  132. * @param string|bool $src Full URL of the script, or path of the script relative to the WordPress root directory.
  133. * If source is set to false, script is an alias of other scripts it depends on.
  134. * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
  135. * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
  136. * as a query string for cache busting purposes. If version is set to false, a version
  137. * number is automatically added equal to current installed WordPress version.
  138. * If set to null, no version is added.
  139. * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
  140. * Default 'false'.
  141. * @return bool Whether the script has been registered. True on success, false on failure.
  142. */
  143. function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
  144. $wp_scripts = wp_scripts();
  145. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  146. $registered = $wp_scripts->add( $handle, $src, $deps, $ver );
  147. if ( $in_footer ) {
  148. $wp_scripts->add_data( $handle, 'group', 1 );
  149. }
  150. return $registered;
  151. }
  152. /**
  153. * Localize a script.
  154. *
  155. * Works only if the script has already been added.
  156. *
  157. * Accepts an associative array $l10n and creates a JavaScript object:
  158. *
  159. * "$object_name" = {
  160. * key: value,
  161. * key: value,
  162. * ...
  163. * }
  164. *
  165. * @see WP_Scripts::localize()
  166. * @link https://core.trac.wordpress.org/ticket/11520
  167. * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
  168. *
  169. * @since 2.2.0
  170. *
  171. * @todo Documentation cleanup
  172. *
  173. * @param string $handle Script handle the data will be attached to.
  174. * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
  175. * Example: '/[a-zA-Z0-9_]+/'.
  176. * @param array $l10n The data itself. The data can be either a single or multi-dimensional array.
  177. * @return bool True if the script was successfully localized, false otherwise.
  178. */
  179. function wp_localize_script( $handle, $object_name, $l10n ) {
  180. global $wp_scripts;
  181. if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
  182. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  183. return false;
  184. }
  185. return $wp_scripts->localize( $handle, $object_name, $l10n );
  186. }
  187. /**
  188. * Sets translated strings for a script.
  189. *
  190. * Works only if the script has already been added.
  191. *
  192. * @see WP_Scripts::set_translations()
  193. * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
  194. *
  195. * @since 5.0.0
  196. * @since 5.1.0 The `$domain` parameter was made optional.
  197. *
  198. * @param string $handle Script handle the textdomain will be attached to.
  199. * @param string $domain Optional. Text domain. Default 'default'.
  200. * @param string $path Optional. The full file path to the directory containing translation files.
  201. * @return bool True if the text domain was successfully localized, false otherwise.
  202. */
  203. function wp_set_script_translations( $handle, $domain = 'default', $path = null ) {
  204. global $wp_scripts;
  205. if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
  206. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  207. return false;
  208. }
  209. return $wp_scripts->set_translations( $handle, $domain, $path );
  210. }
  211. /**
  212. * Remove a registered script.
  213. *
  214. * Note: there are intentional safeguards in place to prevent critical admin scripts,
  215. * such as jQuery core, from being unregistered.
  216. *
  217. * @see WP_Dependencies::remove()
  218. *
  219. * @since 2.1.0
  220. *
  221. * @param string $handle Name of the script to be removed.
  222. */
  223. function wp_deregister_script( $handle ) {
  224. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  225. /**
  226. * Do not allow accidental or negligent de-registering of critical scripts in the admin.
  227. * Show minimal remorse if the correct hook is used.
  228. */
  229. $current_filter = current_filter();
  230. if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
  231. ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
  232. ) {
  233. $no = array(
  234. 'jquery',
  235. 'jquery-core',
  236. 'jquery-migrate',
  237. 'jquery-ui-core',
  238. 'jquery-ui-accordion',
  239. 'jquery-ui-autocomplete',
  240. 'jquery-ui-button',
  241. 'jquery-ui-datepicker',
  242. 'jquery-ui-dialog',
  243. 'jquery-ui-draggable',
  244. 'jquery-ui-droppable',
  245. 'jquery-ui-menu',
  246. 'jquery-ui-mouse',
  247. 'jquery-ui-position',
  248. 'jquery-ui-progressbar',
  249. 'jquery-ui-resizable',
  250. 'jquery-ui-selectable',
  251. 'jquery-ui-slider',
  252. 'jquery-ui-sortable',
  253. 'jquery-ui-spinner',
  254. 'jquery-ui-tabs',
  255. 'jquery-ui-tooltip',
  256. 'jquery-ui-widget',
  257. 'underscore',
  258. 'backbone',
  259. );
  260. if ( in_array( $handle, $no ) ) {
  261. $message = sprintf(
  262. /* translators: 1: Script name, 2: wp_enqueue_scripts */
  263. __( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ),
  264. "<code>$handle</code>",
  265. '<code>wp_enqueue_scripts</code>'
  266. );
  267. _doing_it_wrong( __FUNCTION__, $message, '3.6.0' );
  268. return;
  269. }
  270. }
  271. wp_scripts()->remove( $handle );
  272. }
  273. /**
  274. * Enqueue a script.
  275. *
  276. * Registers the script if $src provided (does NOT overwrite), and enqueues it.
  277. *
  278. * @see WP_Dependencies::add()
  279. * @see WP_Dependencies::add_data()
  280. * @see WP_Dependencies::enqueue()
  281. *
  282. * @since 2.1.0
  283. *
  284. * @param string $handle Name of the script. Should be unique.
  285. * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
  286. * Default empty.
  287. * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
  288. * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
  289. * as a query string for cache busting purposes. If version is set to false, a version
  290. * number is automatically added equal to current installed WordPress version.
  291. * If set to null, no version is added.
  292. * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
  293. * Default 'false'.
  294. */
  295. function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
  296. $wp_scripts = wp_scripts();
  297. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  298. if ( $src || $in_footer ) {
  299. $_handle = explode( '?', $handle );
  300. if ( $src ) {
  301. $wp_scripts->add( $_handle[0], $src, $deps, $ver );
  302. }
  303. if ( $in_footer ) {
  304. $wp_scripts->add_data( $_handle[0], 'group', 1 );
  305. }
  306. }
  307. $wp_scripts->enqueue( $handle );
  308. }
  309. /**
  310. * Remove a previously enqueued script.
  311. *
  312. * @see WP_Dependencies::dequeue()
  313. *
  314. * @since 3.1.0
  315. *
  316. * @param string $handle Name of the script to be removed.
  317. */
  318. function wp_dequeue_script( $handle ) {
  319. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  320. wp_scripts()->dequeue( $handle );
  321. }
  322. /**
  323. * Determines whether a script has been added to the queue.
  324. *
  325. * For more information on this and similar theme functions, check out
  326. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  327. * Conditional Tags} article in the Theme Developer Handbook.
  328. *
  329. * @since 2.8.0
  330. * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
  331. *
  332. * @param string $handle Name of the script.
  333. * @param string $list Optional. Status of the script to check. Default 'enqueued'.
  334. * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
  335. * @return bool Whether the script is queued.
  336. */
  337. function wp_script_is( $handle, $list = 'enqueued' ) {
  338. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  339. return (bool) wp_scripts()->query( $handle, $list );
  340. }
  341. /**
  342. * Add metadata to a script.
  343. *
  344. * Works only if the script has already been added.
  345. *
  346. * Possible values for $key and $value:
  347. * 'conditional' string Comments for IE 6, lte IE 7, etc.
  348. *
  349. * @since 4.2.0
  350. *
  351. * @see WP_Dependencies::add_data()
  352. *
  353. * @param string $handle Name of the script.
  354. * @param string $key Name of data point for which we're storing a value.
  355. * @param mixed $value String containing the data to be added.
  356. * @return bool True on success, false on failure.
  357. */
  358. function wp_script_add_data( $handle, $key, $value ) {
  359. return wp_scripts()->add_data( $handle, $key, $value );
  360. }