PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/functions.wp-scripts.php

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