class.wp-scripts.php 14 KB

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