class.wp-scripts.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 scripts.
  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. * Holds a string which contains the type attribute for script tag.
  114. *
  115. * If the current theme does not declare HTML5 support for 'script',
  116. * then it initializes as `type='text/javascript'`.
  117. *
  118. * @since 5.3.0
  119. * @var string
  120. */
  121. private $type_attr = '';
  122. /**
  123. * Constructor.
  124. *
  125. * @since 2.6.0
  126. */
  127. public function __construct() {
  128. $this->init();
  129. add_action( 'init', array( $this, 'init' ), 0 );
  130. }
  131. /**
  132. * Initialize the class.
  133. *
  134. * @since 3.4.0
  135. */
  136. public function init() {
  137. if (
  138. function_exists( 'is_admin' ) && ! is_admin()
  139. &&
  140. function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'script' )
  141. ) {
  142. $this->type_attr = " type='text/javascript'";
  143. }
  144. /**
  145. * Fires when the WP_Scripts instance is initialized.
  146. *
  147. * @since 2.6.0
  148. *
  149. * @param WP_Scripts $this WP_Scripts instance (passed by reference).
  150. */
  151. do_action_ref_array( 'wp_default_scripts', array( &$this ) );
  152. }
  153. /**
  154. * Prints scripts.
  155. *
  156. * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
  157. *
  158. * @since 2.1.0
  159. * @since 2.8.0 Added the `$group` parameter.
  160. *
  161. * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints
  162. * that script, (array of strings) prints those scripts. Default false.
  163. * @param int $group Optional. If scripts were queued in groups prints this group number.
  164. * Default false.
  165. * @return array Scripts that have been printed.
  166. */
  167. public function print_scripts( $handles = false, $group = false ) {
  168. return $this->do_items( $handles, $group );
  169. }
  170. /**
  171. * Prints extra scripts of a registered script.
  172. *
  173. * @since 2.1.0
  174. * @since 2.8.0 Added the `$echo` parameter.
  175. * @deprecated 3.3.0
  176. *
  177. * @see print_extra_script()
  178. *
  179. * @param string $handle The script's registered handle.
  180. * @param bool $echo Optional. Whether to echo the extra script instead of just returning it.
  181. * Default true.
  182. * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
  183. */
  184. public function print_scripts_l10n( $handle, $echo = true ) {
  185. _deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' );
  186. return $this->print_extra_script( $handle, $echo );
  187. }
  188. /**
  189. * Prints extra scripts of a registered script.
  190. *
  191. * @since 3.3.0
  192. *
  193. * @param string $handle The script's registered handle.
  194. * @param bool $echo Optional. Whether to echo the extra script instead of just returning it.
  195. * Default true.
  196. * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
  197. */
  198. public function print_extra_script( $handle, $echo = true ) {
  199. $output = $this->get_data( $handle, 'data' );
  200. if ( ! $output ) {
  201. return;
  202. }
  203. if ( ! $echo ) {
  204. return $output;
  205. }
  206. echo "<script{$this->type_attr}>\n";
  207. // CDATA is not needed for HTML 5.
  208. if ( $this->type_attr ) {
  209. echo "/* <![CDATA[ */\n";
  210. }
  211. echo "$output\n";
  212. if ( $this->type_attr ) {
  213. echo "/* ]]> */\n";
  214. }
  215. echo "</script>\n";
  216. return true;
  217. }
  218. /**
  219. * Processes a script dependency.
  220. *
  221. * @since 2.6.0
  222. * @since 2.8.0 Added the `$group` parameter.
  223. *
  224. * @see WP_Dependencies::do_item()
  225. *
  226. * @param string $handle The script's registered handle.
  227. * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false.
  228. * @return bool True on success, false on failure.
  229. */
  230. public function do_item( $handle, $group = false ) {
  231. if ( ! parent::do_item( $handle ) ) {
  232. return false;
  233. }
  234. if ( 0 === $group && $this->groups[ $handle ] > 0 ) {
  235. $this->in_footer[] = $handle;
  236. return false;
  237. }
  238. if ( false === $group && in_array( $handle, $this->in_footer, true ) ) {
  239. $this->in_footer = array_diff( $this->in_footer, (array) $handle );
  240. }
  241. $obj = $this->registered[ $handle ];
  242. if ( null === $obj->ver ) {
  243. $ver = '';
  244. } else {
  245. $ver = $obj->ver ? $obj->ver : $this->default_version;
  246. }
  247. if ( isset( $this->args[ $handle ] ) ) {
  248. $ver = $ver ? $ver . '&amp;' . $this->args[ $handle ] : $this->args[ $handle ];
  249. }
  250. $src = $obj->src;
  251. $cond_before = '';
  252. $cond_after = '';
  253. $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
  254. if ( $conditional ) {
  255. $cond_before = "<!--[if {$conditional}]>\n";
  256. $cond_after = "<![endif]-->\n";
  257. }
  258. $before_handle = $this->print_inline_script( $handle, 'before', false );
  259. $after_handle = $this->print_inline_script( $handle, 'after', false );
  260. if ( $before_handle ) {
  261. $before_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $before_handle );
  262. }
  263. if ( $after_handle ) {
  264. $after_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $after_handle );
  265. }
  266. if ( $before_handle || $after_handle ) {
  267. $inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after;
  268. } else {
  269. $inline_script_tag = '';
  270. }
  271. if ( $this->do_concat ) {
  272. /**
  273. * Filters the script loader source.
  274. *
  275. * @since 2.2.0
  276. *
  277. * @param string $src Script loader source path.
  278. * @param string $handle Script handle.
  279. */
  280. $srce = apply_filters( 'script_loader_src', $src, $handle );
  281. if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) {
  282. $this->do_concat = false;
  283. // Have to print the so-far concatenated scripts right away to maintain the right order.
  284. _print_scripts();
  285. $this->reset();
  286. } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) {
  287. $this->print_code .= $this->print_extra_script( $handle, false );
  288. $this->concat .= "$handle,";
  289. $this->concat_version .= "$handle$ver";
  290. return true;
  291. } else {
  292. $this->ext_handles .= "$handle,";
  293. $this->ext_version .= "$handle$ver";
  294. }
  295. }
  296. $has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
  297. if ( $has_conditional_data ) {
  298. echo $cond_before;
  299. }
  300. $this->print_extra_script( $handle );
  301. if ( $has_conditional_data ) {
  302. echo $cond_after;
  303. }
  304. // A single item may alias a set of items, by having dependencies, but no source.
  305. if ( ! $src ) {
  306. if ( $inline_script_tag ) {
  307. if ( $this->do_concat ) {
  308. $this->print_html .= $inline_script_tag;
  309. } else {
  310. echo $inline_script_tag;
  311. }
  312. }
  313. return true;
  314. }
  315. $translations = $this->print_translations( $handle, false );
  316. if ( $translations ) {
  317. $translations = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $translations );
  318. }
  319. if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
  320. $src = $this->base_url . $src;
  321. }
  322. if ( ! empty( $ver ) ) {
  323. $src = add_query_arg( 'ver', $ver, $src );
  324. }
  325. /** This filter is documented in wp-includes/class.wp-scripts.php */
  326. $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
  327. if ( ! $src ) {
  328. return true;
  329. }
  330. $tag = $translations . $cond_before . $before_handle;
  331. $tag .= sprintf( "<script%s src='%s'></script>\n", $this->type_attr, $src );
  332. $tag .= $after_handle . $cond_after;
  333. /**
  334. * Filters the HTML script tag of an enqueued script.
  335. *
  336. * @since 4.1.0
  337. *
  338. * @param string $tag The `<script>` tag for the enqueued script.
  339. * @param string $handle The script's registered handle.
  340. * @param string $src The script's source URL.
  341. */
  342. $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
  343. if ( $this->do_concat ) {
  344. $this->print_html .= $tag;
  345. } else {
  346. echo $tag;
  347. }
  348. return true;
  349. }
  350. /**
  351. * Adds extra code to a registered script.
  352. *
  353. * @since 4.5.0
  354. *
  355. * @param string $handle Name of the script to add the inline script to. Must be lowercase.
  356. * @param string $data String containing the javascript to be added.
  357. * @param string $position Optional. Whether to add the inline script before the handle
  358. * or after. Default 'after'.
  359. * @return bool True on success, false on failure.
  360. */
  361. public function add_inline_script( $handle, $data, $position = 'after' ) {
  362. if ( ! $data ) {
  363. return false;
  364. }
  365. if ( 'after' !== $position ) {
  366. $position = 'before';
  367. }
  368. $script = (array) $this->get_data( $handle, $position );
  369. $script[] = $data;
  370. return $this->add_data( $handle, $position, $script );
  371. }
  372. /**
  373. * Prints inline scripts registered for a specific handle.
  374. *
  375. * @since 4.5.0
  376. *
  377. * @param string $handle Name of the script to add the inline script to. Must be lowercase.
  378. * @param string $position Optional. Whether to add the inline script before the handle
  379. * or after. Default 'after'.
  380. * @param bool $echo Optional. Whether to echo the script instead of just returning it.
  381. * Default true.
  382. * @return string|false Script on success, false otherwise.
  383. */
  384. public function print_inline_script( $handle, $position = 'after', $echo = true ) {
  385. $output = $this->get_data( $handle, $position );
  386. if ( empty( $output ) ) {
  387. return false;
  388. }
  389. $output = trim( implode( "\n", $output ), "\n" );
  390. if ( $echo ) {
  391. printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
  392. }
  393. return $output;
  394. }
  395. /**
  396. * Localizes a script, only if the script has already been added.
  397. *
  398. * @since 2.1.0
  399. *
  400. * @param string $handle Name of the script to attach data to.
  401. * @param string $object_name Name of the variable that will contain the data.
  402. * @param array $l10n Array of data to localize.
  403. * @return bool True on success, false on failure.
  404. */
  405. public function localize( $handle, $object_name, $l10n ) {
  406. if ( $handle === 'jquery' ) {
  407. $handle = 'jquery-core';
  408. }
  409. if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present.
  410. $after = $l10n['l10n_print_after'];
  411. unset( $l10n['l10n_print_after'] );
  412. }
  413. foreach ( (array) $l10n as $key => $value ) {
  414. if ( ! is_scalar( $value ) ) {
  415. continue;
  416. }
  417. $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
  418. }
  419. $script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
  420. if ( ! empty( $after ) ) {
  421. $script .= "\n$after;";
  422. }
  423. $data = $this->get_data( $handle, 'data' );
  424. if ( ! empty( $data ) ) {
  425. $script = "$data\n$script";
  426. }
  427. return $this->add_data( $handle, 'data', $script );
  428. }
  429. /**
  430. * Sets handle group.
  431. *
  432. * @since 2.8.0
  433. *
  434. * @see WP_Dependencies::set_group()
  435. *
  436. * @param string $handle Name of the item. Should be unique.
  437. * @param bool $recursion Internal flag that calling function was called recursively.
  438. * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false.
  439. * @return bool Not already in the group or a lower group
  440. */
  441. public function set_group( $handle, $recursion, $group = false ) {
  442. if ( isset( $this->registered[ $handle ]->args ) && $this->registered[ $handle ]->args === 1 ) {
  443. $grp = 1;
  444. } else {
  445. $grp = (int) $this->get_data( $handle, 'group' );
  446. }
  447. if ( false !== $group && $grp > $group ) {
  448. $grp = $group;
  449. }
  450. return parent::set_group( $handle, $recursion, $grp );
  451. }
  452. /**
  453. * Sets a translation textdomain.
  454. *
  455. * @since 5.0.0
  456. * @since 5.1.0 The `$domain` parameter was made optional.
  457. *
  458. * @param string $handle Name of the script to register a translation domain to.
  459. * @param string $domain Optional. Text domain. Default 'default'.
  460. * @param string $path Optional. The full file path to the directory containing translation files.
  461. * @return bool True if the text domain was registered, false if not.
  462. */
  463. public function set_translations( $handle, $domain = 'default', $path = null ) {
  464. if ( ! isset( $this->registered[ $handle ] ) ) {
  465. return false;
  466. }
  467. /** @var \_WP_Dependency $obj */
  468. $obj = $this->registered[ $handle ];
  469. if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) {
  470. $obj->deps[] = 'wp-i18n';
  471. }
  472. return $obj->set_translations( $domain, $path );
  473. }
  474. /**
  475. * Prints translations set for a specific handle.
  476. *
  477. * @since 5.0.0
  478. *
  479. * @param string $handle Name of the script to add the inline script to. Must be lowercase.
  480. * @param bool $echo Optional. Whether to echo the script instead of just returning it.
  481. * Default true.
  482. * @return string|false Script on success, false otherwise.
  483. */
  484. public function print_translations( $handle, $echo = true ) {
  485. if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
  486. return false;
  487. }
  488. $domain = $this->registered[ $handle ]->textdomain;
  489. $path = $this->registered[ $handle ]->translations_path;
  490. $json_translations = load_script_textdomain( $handle, $domain, $path );
  491. if ( ! $json_translations ) {
  492. // Register empty locale data object to ensure the domain still exists.
  493. $json_translations = '{ "locale_data": { "messages": { "": {} } } }';
  494. }
  495. $output = <<<JS
  496. ( function( domain, translations ) {
  497. var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
  498. localeData[""].domain = domain;
  499. wp.i18n.setLocaleData( localeData, domain );
  500. } )( "{$domain}", {$json_translations} );
  501. JS;
  502. if ( $echo ) {
  503. printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
  504. }
  505. return $output;
  506. }
  507. /**
  508. * Determines script dependencies.
  509. *
  510. * @since 2.1.0
  511. *
  512. * @see WP_Dependencies::all_deps()
  513. *
  514. * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
  515. * @param bool $recursion Internal flag that function is calling itself.
  516. * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false.
  517. * @return bool True on success, false on failure.
  518. */
  519. public function all_deps( $handles, $recursion = false, $group = false ) {
  520. $r = parent::all_deps( $handles, $recursion, $group );
  521. if ( ! $recursion ) {
  522. /**
  523. * Filters the list of script dependencies left to print.
  524. *
  525. * @since 2.3.0
  526. *
  527. * @param string[] $to_do An array of script dependency handles.
  528. */
  529. $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
  530. }
  531. return $r;
  532. }
  533. /**
  534. * Processes items and dependencies for the head group.
  535. *
  536. * @since 2.8.0
  537. *
  538. * @see WP_Dependencies::do_items()
  539. *
  540. * @return array Handles of items that have been processed.
  541. */
  542. public function do_head_items() {
  543. $this->do_items( false, 0 );
  544. return $this->done;
  545. }
  546. /**
  547. * Processes items and dependencies for the footer group.
  548. *
  549. * @since 2.8.0
  550. *
  551. * @see WP_Dependencies::do_items()
  552. *
  553. * @return array Handles of items that have been processed.
  554. */
  555. public function do_footer_items() {
  556. $this->do_items( false, 1 );
  557. return $this->done;
  558. }
  559. /**
  560. * Whether a handle's source is in a default directory.
  561. *
  562. * @since 2.8.0
  563. *
  564. * @param string $src The source of the enqueued script.
  565. * @return bool True if found, false if not.
  566. */
  567. public function in_default_dir( $src ) {
  568. if ( ! $this->default_dirs ) {
  569. return true;
  570. }
  571. if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) {
  572. return false;
  573. }
  574. foreach ( (array) $this->default_dirs as $test ) {
  575. if ( 0 === strpos( $src, $test ) ) {
  576. return true;
  577. }
  578. }
  579. return false;
  580. }
  581. /**
  582. * Resets class properties.
  583. *
  584. * @since 2.8.0
  585. */
  586. public function reset() {
  587. $this->do_concat = false;
  588. $this->print_code = '';
  589. $this->concat = '';
  590. $this->concat_version = '';
  591. $this->print_html = '';
  592. $this->ext_version = '';
  593. $this->ext_handles = '';
  594. }
  595. }