您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

337 行
11 KiB

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