WP_Scripts

Core class used to register scripts.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class.wp-scripts.php  
  1. class WP_Scripts extends WP_Dependencies { 
  2. /** 
  3. * Base URL for scripts. 
  4. * Full URL with trailing slash. 
  5. * @since 2.6.0 
  6. * @access public 
  7. * @var string 
  8. */ 
  9. public $base_url; 
  10.  
  11. /** 
  12. * URL of the content directory. 
  13. * @since 2.8.0 
  14. * @access public 
  15. * @var string 
  16. */ 
  17. public $content_url; 
  18.  
  19. /** 
  20. * Default version string for stylesheets. 
  21. * @since 2.6.0 
  22. * @access public 
  23. * @var string 
  24. */ 
  25. public $default_version; 
  26.  
  27. /** 
  28. * Holds handles of scripts which are enqueued in footer. 
  29. * @since 2.8.0 
  30. * @access public 
  31. * @var array 
  32. */ 
  33. public $in_footer = array(); 
  34.  
  35. /** 
  36. * Holds a list of script handles which will be concatenated. 
  37. * @since 2.8.0 
  38. * @access public 
  39. * @var string 
  40. */ 
  41. public $concat = ''; 
  42.  
  43. /** 
  44. * Holds a string which contains script handles and their version. 
  45. * @since 2.8.0 
  46. * @deprecated 3.4.0 
  47. * @access public 
  48. * @var string 
  49. */ 
  50. public $concat_version = ''; 
  51.  
  52. /** 
  53. * Whether to perform concatenation. 
  54. * @since 2.8.0 
  55. * @access public 
  56. * @var bool 
  57. */ 
  58. public $do_concat = false; 
  59.  
  60. /** 
  61. * Holds HTML markup of scripts and additional data if concatenation 
  62. * is enabled. 
  63. * @since 2.8.0 
  64. * @access public 
  65. * @var string 
  66. */ 
  67. public $print_html = ''; 
  68.  
  69. /** 
  70. * Holds inline code if concatenation is enabled. 
  71. * @since 2.8.0 
  72. * @access public 
  73. * @var string 
  74. */ 
  75. public $print_code = ''; 
  76.  
  77. /** 
  78. * Holds a list of script handles which are not in the default directory 
  79. * if concatenation is enabled. 
  80. * Unused in core. 
  81. * @since 2.8.0 
  82. * @access public 
  83. * @var string 
  84. */ 
  85. public $ext_handles = ''; 
  86.  
  87. /** 
  88. * Holds a string which contains handles and versions of scripts which 
  89. * are not in the default directory if concatenation is enabled. 
  90. * Unused in core. 
  91. * @since 2.8.0 
  92. * @access public 
  93. * @var string 
  94. */ 
  95. public $ext_version = ''; 
  96.  
  97. /** 
  98. * List of default directories. 
  99. * @since 2.8.0 
  100. * @access public 
  101. * @var array 
  102. */ 
  103. public $default_dirs; 
  104.  
  105. /** 
  106. * Constructor. 
  107. * @since 2.6.0 
  108. * @access public 
  109. */ 
  110. public function __construct() { 
  111. $this->init(); 
  112. add_action( 'init', array( $this, 'init' ), 0 ); 
  113.  
  114. /** 
  115. * Initialize the class. 
  116. * @since 3.4.0 
  117. * @access public 
  118. */ 
  119. public function init() { 
  120. /** 
  121. * Fires when the WP_Scripts instance is initialized. 
  122. * @since 2.6.0 
  123. * @param WP_Scripts &$this WP_Scripts instance, passed by reference. 
  124. */ 
  125. do_action_ref_array( 'wp_default_scripts', array(&$this) ); 
  126.  
  127. /** 
  128. * Prints scripts. 
  129. * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. 
  130. * @since 2.1.0 
  131. * @since 2.8.0 Added the `$group` parameter. 
  132. * @access public 
  133. * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints 
  134. * that script, (array of strings) prints those scripts. Default false. 
  135. * @param int $group Optional. If scripts were queued in groups prints this group number. 
  136. * Default false. 
  137. * @return array Scripts that have been printed. 
  138. */ 
  139. public function print_scripts( $handles = false, $group = false ) { 
  140. return $this->do_items( $handles, $group ); 
  141.  
  142. /** 
  143. * Prints extra scripts of a registered script. 
  144. * @since 2.1.0 
  145. * @since 2.8.0 Added the `$echo` parameter. 
  146. * @deprecated 3.3.0 
  147. * @access public 
  148. * @see print_extra_script() 
  149. * @param string $handle The script's registered handle. 
  150. * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. 
  151. * Default true. 
  152. * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. 
  153. */ 
  154. public function print_scripts_l10n( $handle, $echo = true ) { 
  155. _deprecated_function( __FUNCTION__, '3.3.0', 'print_extra_script()' ); 
  156. return $this->print_extra_script( $handle, $echo ); 
  157.  
  158. /** 
  159. * Prints extra scripts of a registered script. 
  160. * @since 3.3.0 
  161. * @access public 
  162. * @param string $handle The script's registered handle. 
  163. * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. 
  164. * Default true. 
  165. * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. 
  166. */ 
  167. public function print_extra_script( $handle, $echo = true ) { 
  168. if ( !$output = $this->get_data( $handle, 'data' ) ) 
  169. return; 
  170.  
  171. if ( !$echo ) 
  172. return $output; 
  173.  
  174. echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5 
  175. echo "/* <![CDATA[ */\n"; 
  176. echo "$output\n"; 
  177. echo "/* ]]> */\n"; 
  178. echo "</script>\n"; 
  179.  
  180. return true; 
  181.  
  182. /** 
  183. * Processes a script dependency. 
  184. * @since 2.6.0 
  185. * @since 2.8.0 Added the `$group` parameter. 
  186. * @access public 
  187. * @see WP_Dependencies::do_item() 
  188. * @param string $handle The script's registered handle. 
  189. * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. 
  190. * @return bool True on success, false on failure. 
  191. */ 
  192. public function do_item( $handle, $group = false ) { 
  193. if ( !parent::do_item($handle) ) 
  194. return false; 
  195.  
  196. if ( 0 === $group && $this->groups[$handle] > 0 ) { 
  197. $this->in_footer[] = $handle; 
  198. return false; 
  199.  
  200. if ( false === $group && in_array($handle, $this->in_footer, true) ) 
  201. $this->in_footer = array_diff( $this->in_footer, (array) $handle ); 
  202.  
  203. $obj = $this->registered[$handle]; 
  204.  
  205. if ( null === $obj->ver ) { 
  206. $ver = ''; 
  207. } else { 
  208. $ver = $obj->ver ? $obj->ver : $this->default_version; 
  209.  
  210. if ( isset($this->args[$handle]) ) 
  211. $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; 
  212.  
  213. $src = $obj->src; 
  214. $cond_before = $cond_after = ''; 
  215. $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : ''; 
  216.  
  217. if ( $conditional ) { 
  218. $cond_before = "<!--[if {$conditional}]>\n"; 
  219. $cond_after = "<![endif]-->\n"; 
  220.  
  221. $before_handle = $this->print_inline_script( $handle, 'before', false ); 
  222. $after_handle = $this->print_inline_script( $handle, 'after', false ); 
  223.  
  224. if ( $before_handle ) { 
  225. $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle ); 
  226.  
  227. if ( $after_handle ) { 
  228. $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle ); 
  229.  
  230. if ( $this->do_concat ) { 
  231. /** 
  232. * Filters the script loader source. 
  233. * @since 2.2.0 
  234. * @param string $src Script loader source path. 
  235. * @param string $handle Script handle. 
  236. */ 
  237. $srce = apply_filters( 'script_loader_src', $src, $handle ); 
  238.  
  239. if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) { 
  240. $this->do_concat = false; 
  241.  
  242. // Have to print the so-far concatenated scripts right away to maintain the right order. 
  243. _print_scripts(); 
  244. $this->reset(); 
  245. } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) { 
  246. $this->print_code .= $this->print_extra_script( $handle, false ); 
  247. $this->concat .= "$handle, "; 
  248. $this->concat_version .= "$handle$ver"; 
  249. return true; 
  250. } else { 
  251. $this->ext_handles .= "$handle, "; 
  252. $this->ext_version .= "$handle$ver"; 
  253.  
  254. $has_conditional_data = $conditional && $this->get_data( $handle, 'data' ); 
  255.  
  256. if ( $has_conditional_data ) { 
  257. echo $cond_before; 
  258.  
  259. $this->print_extra_script( $handle ); 
  260.  
  261. if ( $has_conditional_data ) { 
  262. echo $cond_after; 
  263.  
  264. // A single item may alias a set of items, by having dependencies, but no source. 
  265. if ( ! $obj->src ) { 
  266. return true; 
  267.  
  268. if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) { 
  269. $src = $this->base_url . $src; 
  270.  
  271. if ( ! empty( $ver ) ) 
  272. $src = add_query_arg( 'ver', $ver, $src ); 
  273.  
  274. /** This filter is documented in wp-includes/class.wp-scripts.php */ 
  275. $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); 
  276.  
  277. if ( ! $src ) 
  278. return true; 
  279.  
  280. $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}"; 
  281.  
  282. /** 
  283. * Filters the HTML script tag of an enqueued script. 
  284. * @since 4.1.0 
  285. * @param string $tag The `<script>` tag for the enqueued script. 
  286. * @param string $handle The script's registered handle. 
  287. * @param string $src The script's source URL. 
  288. */ 
  289. $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src ); 
  290.  
  291. if ( $this->do_concat ) { 
  292. $this->print_html .= $tag; 
  293. } else { 
  294. echo $tag; 
  295.  
  296. return true; 
  297.  
  298. /** 
  299. * Adds extra code to a registered script. 
  300. * @since 4.5.0 
  301. * @access public 
  302. * @param string $handle Name of the script to add the inline script to. Must be lowercase. 
  303. * @param string $data String containing the javascript to be added. 
  304. * @param string $position Optional. Whether to add the inline script before the handle 
  305. * or after. Default 'after'. 
  306. * @return bool True on success, false on failure. 
  307. */ 
  308. public function add_inline_script( $handle, $data, $position = 'after' ) { 
  309. if ( ! $data ) { 
  310. return false; 
  311.  
  312. if ( 'after' !== $position ) { 
  313. $position = 'before'; 
  314.  
  315. $script = (array) $this->get_data( $handle, $position ); 
  316. $script[] = $data; 
  317.  
  318. return $this->add_data( $handle, $position, $script ); 
  319.  
  320. /** 
  321. * Prints inline scripts registered for a specific handle. 
  322. * @since 4.5.0 
  323. * @access public 
  324. * @param string $handle Name of the script to add the inline script to. Must be lowercase. 
  325. * @param string $position Optional. Whether to add the inline script before the handle 
  326. * or after. Default 'after'. 
  327. * @param bool $echo Optional. Whether to echo the script instead of just returning it. 
  328. * Default true. 
  329. * @return string|false Script on success, false otherwise. 
  330. */ 
  331. public function print_inline_script( $handle, $position = 'after', $echo = true ) { 
  332. $output = $this->get_data( $handle, $position ); 
  333.  
  334. if ( empty( $output ) ) { 
  335. return false; 
  336.  
  337. $output = trim( implode( "\n", $output ), "\n" ); 
  338.  
  339. if ( $echo ) { 
  340. printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); 
  341.  
  342. return $output; 
  343.  
  344. /** 
  345. * Localizes a script, only if the script has already been added. 
  346. * @since 2.1.0 
  347. * @access public 
  348. * @param string $handle 
  349. * @param string $object_name 
  350. * @param array $l10n 
  351. * @return bool 
  352. */ 
  353. public function localize( $handle, $object_name, $l10n ) { 
  354. if ( $handle === 'jquery' ) 
  355. $handle = 'jquery-core'; 
  356.  
  357. if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present 
  358. $after = $l10n['l10n_print_after']; 
  359. unset($l10n['l10n_print_after']); 
  360.  
  361. foreach ( (array) $l10n as $key => $value ) { 
  362. if ( !is_scalar($value) ) 
  363. continue; 
  364.  
  365. $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8'); 
  366.  
  367. $script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; 
  368.  
  369. if ( !empty($after) ) 
  370. $script .= "\n$after;"; 
  371.  
  372. $data = $this->get_data( $handle, 'data' ); 
  373.  
  374. if ( !empty( $data ) ) 
  375. $script = "$data\n$script"; 
  376.  
  377. return $this->add_data( $handle, 'data', $script ); 
  378.  
  379. /** 
  380. * Sets handle group. 
  381. * @since 2.8.0 
  382. * @access public 
  383. * @see WP_Dependencies::set_group() 
  384. * @param string $handle Name of the item. Should be unique. 
  385. * @param bool $recursion Internal flag that calling function was called recursively. 
  386. * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. 
  387. * @return bool Not already in the group or a lower group 
  388. */ 
  389. public function set_group( $handle, $recursion, $group = false ) { 
  390. if ( isset( $this->registered[$handle]->args ) && $this->registered[$handle]->args === 1 ) 
  391. $grp = 1; 
  392. else 
  393. $grp = (int) $this->get_data( $handle, 'group' ); 
  394.  
  395. if ( false !== $group && $grp > $group ) 
  396. $grp = $group; 
  397.  
  398. return parent::set_group( $handle, $recursion, $grp ); 
  399.  
  400. /** 
  401. * Determines script dependencies. 
  402. * @since 2.1.0 
  403. * @access public 
  404. * @see WP_Dependencies::all_deps() 
  405. * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). 
  406. * @param bool $recursion Internal flag that function is calling itself. 
  407. * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. 
  408. * @return bool True on success, false on failure. 
  409. */ 
  410. public function all_deps( $handles, $recursion = false, $group = false ) { 
  411. $r = parent::all_deps( $handles, $recursion, $group ); 
  412. if ( ! $recursion ) { 
  413. /** 
  414. * Filters the list of script dependencies left to print. 
  415. * @since 2.3.0 
  416. * @param array $to_do An array of script dependencies. 
  417. */ 
  418. $this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); 
  419. return $r; 
  420.  
  421. /** 
  422. * Processes items and dependencies for the head group. 
  423. * @since 2.8.0 
  424. * @access public 
  425. * @see WP_Dependencies::do_items() 
  426. * @return array Handles of items that have been processed. 
  427. */ 
  428. public function do_head_items() { 
  429. $this->do_items(false, 0); 
  430. return $this->done; 
  431.  
  432. /** 
  433. * Processes items and dependencies for the footer group. 
  434. * @since 2.8.0 
  435. * @access public 
  436. * @see WP_Dependencies::do_items() 
  437. * @return array Handles of items that have been processed. 
  438. */ 
  439. public function do_footer_items() { 
  440. $this->do_items(false, 1); 
  441. return $this->done; 
  442.  
  443. /** 
  444. * Whether a handle's source is in a default directory. 
  445. * @since 2.8.0 
  446. * @access public 
  447. * @param string $src The source of the enqueued script. 
  448. * @return bool True if found, false if not. 
  449. */ 
  450. public function in_default_dir( $src ) { 
  451. if ( ! $this->default_dirs ) { 
  452. return true; 
  453.  
  454. if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) { 
  455. return false; 
  456.  
  457. foreach ( (array) $this->default_dirs as $test ) { 
  458. if ( 0 === strpos( $src, $test ) ) { 
  459. return true; 
  460. return false; 
  461.  
  462. /** 
  463. * Resets class properties. 
  464. * @since 2.8.0 
  465. * @access public 
  466. */ 
  467. public function reset() { 
  468. $this->do_concat = false; 
  469. $this->print_code = ''; 
  470. $this->concat = ''; 
  471. $this->concat_version = ''; 
  472. $this->print_html = ''; 
  473. $this->ext_version = ''; 
  474. $this->ext_handles = '';