PageRenderTime 64ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/ctf-f2015/www/wp-includes/functions.wp-scripts.php

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