PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

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