PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class.wp-scripts.php

https://gitlab.com/Gashler/sg
PHP | 331 lines | 169 code | 49 blank | 113 comment | 38 complexity | 021bb2208761eec79699febbb1d487c6 MD5 | raw file
  1. <?php
  2. /**
  3. * BackPress Scripts enqueue.
  4. *
  5. * These classes were refactored from the WordPress WP_Scripts and WordPress
  6. * script enqueue API.
  7. *
  8. * @package BackPress
  9. * @since r16
  10. */
  11. /**
  12. * BackPress Scripts enqueue class.
  13. *
  14. * @package BackPress
  15. * @uses WP_Dependencies
  16. * @since r16
  17. */
  18. class WP_Scripts extends WP_Dependencies {
  19. public $base_url; // Full URL with trailing slash
  20. public $content_url;
  21. public $default_version;
  22. public $in_footer = array();
  23. public $concat = '';
  24. public $concat_version = '';
  25. public $do_concat = false;
  26. public $print_html = '';
  27. public $print_code = '';
  28. public $ext_handles = '';
  29. public $ext_version = '';
  30. public $default_dirs;
  31. public function __construct() {
  32. $this->init();
  33. add_action( 'init', array( $this, 'init' ), 0 );
  34. }
  35. /**
  36. * @access public
  37. */
  38. public function init() {
  39. /**
  40. * Fires when the WP_Scripts instance is initialized.
  41. *
  42. * @since 2.6.0
  43. *
  44. * @param WP_Scripts &$this WP_Scripts instance, passed by reference.
  45. */
  46. do_action_ref_array( 'wp_default_scripts', array(&$this) );
  47. }
  48. /**
  49. * Prints scripts.
  50. *
  51. * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
  52. *
  53. * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints
  54. * that script, (array of strings) prints those scripts. Default false.
  55. * @param int $group Optional. If scripts were queued in groups prints this group number.
  56. * Default false.
  57. * @return array Scripts that have been printed.
  58. */
  59. public function print_scripts( $handles = false, $group = false ) {
  60. return $this->do_items( $handles, $group );
  61. }
  62. /**
  63. * @deprecated 3.3
  64. * @see print_extra_script()
  65. *
  66. * @param string $handle
  67. * @param bool $echo
  68. * @return bool|string|void
  69. */
  70. public function print_scripts_l10n( $handle, $echo = true ) {
  71. _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' );
  72. return $this->print_extra_script( $handle, $echo );
  73. }
  74. /**
  75. * @param string $handle
  76. * @param bool $echo
  77. * @return bool|string|void
  78. */
  79. public function print_extra_script( $handle, $echo = true ) {
  80. if ( !$output = $this->get_data( $handle, 'data' ) )
  81. return;
  82. if ( !$echo )
  83. return $output;
  84. echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
  85. echo "/* <![CDATA[ */\n";
  86. echo "$output\n";
  87. echo "/* ]]> */\n";
  88. echo "</script>\n";
  89. return true;
  90. }
  91. /**
  92. * @param string $handle Name of the item. Should be unique.
  93. * @param int|bool $group
  94. * @return bool True on success, false if not set.
  95. */
  96. public function do_item( $handle, $group = false ) {
  97. if ( !parent::do_item($handle) )
  98. return false;
  99. if ( 0 === $group && $this->groups[$handle] > 0 ) {
  100. $this->in_footer[] = $handle;
  101. return false;
  102. }
  103. if ( false === $group && in_array($handle, $this->in_footer, true) )
  104. $this->in_footer = array_diff( $this->in_footer, (array) $handle );
  105. $obj = $this->registered[$handle];
  106. if ( null === $obj->ver ) {
  107. $ver = '';
  108. } else {
  109. $ver = $obj->ver ? $obj->ver : $this->default_version;
  110. }
  111. if ( isset($this->args[$handle]) )
  112. $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
  113. $src = $obj->src;
  114. $cond_before = $cond_after = '';
  115. $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
  116. if ( $conditional ) {
  117. $cond_before = "<!--[if {$conditional}]>\n";
  118. $cond_after = "<![endif]-->\n";
  119. }
  120. if ( $this->do_concat ) {
  121. /**
  122. * Filter the script loader source.
  123. *
  124. * @since 2.2.0
  125. *
  126. * @param string $src Script loader source path.
  127. * @param string $handle Script handle.
  128. */
  129. $srce = apply_filters( 'script_loader_src', $src, $handle );
  130. if ( $this->in_default_dir( $srce ) && ! $conditional ) {
  131. $this->print_code .= $this->print_extra_script( $handle, false );
  132. $this->concat .= "$handle,";
  133. $this->concat_version .= "$handle$ver";
  134. return true;
  135. } else {
  136. $this->ext_handles .= "$handle,";
  137. $this->ext_version .= "$handle$ver";
  138. }
  139. }
  140. $has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
  141. if ( $has_conditional_data ) {
  142. echo $cond_before;
  143. }
  144. $this->print_extra_script( $handle );
  145. if ( $has_conditional_data ) {
  146. echo $cond_after;
  147. }
  148. if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
  149. $src = $this->base_url . $src;
  150. }
  151. if ( ! empty( $ver ) )
  152. $src = add_query_arg( 'ver', $ver, $src );
  153. /** This filter is documented in wp-includes/class.wp-scripts.php */
  154. $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
  155. if ( ! $src )
  156. return true;
  157. $tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}";
  158. /**
  159. * Filter the HTML script tag of an enqueued script.
  160. *
  161. * @since 4.1.0
  162. *
  163. * @param string $tag The `<script>` tag for the enqueued script.
  164. * @param string $handle The script's registered handle.
  165. * @param string $src The script's source URL.
  166. */
  167. $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
  168. if ( $this->do_concat ) {
  169. $this->print_html .= $tag;
  170. } else {
  171. echo $tag;
  172. }
  173. return true;
  174. }
  175. /**
  176. * Localizes a script, only if the script has already been added
  177. *
  178. * @param string $handle
  179. * @param string $object_name
  180. * @param array $l10n
  181. * @return bool
  182. */
  183. public function localize( $handle, $object_name, $l10n ) {
  184. if ( $handle === 'jquery' )
  185. $handle = 'jquery-core';
  186. if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
  187. $after = $l10n['l10n_print_after'];
  188. unset($l10n['l10n_print_after']);
  189. }
  190. foreach ( (array) $l10n as $key => $value ) {
  191. if ( !is_scalar($value) )
  192. continue;
  193. $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
  194. }
  195. $script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
  196. if ( !empty($after) )
  197. $script .= "\n$after;";
  198. $data = $this->get_data( $handle, 'data' );
  199. if ( !empty( $data ) )
  200. $script = "$data\n$script";
  201. return $this->add_data( $handle, 'data', $script );
  202. }
  203. /**
  204. * @param string $handle Name of the item. Should be unique.
  205. * @param bool $recursion Internal flag that calling function was called recursively.
  206. * @param mixed $group Group level.
  207. * @return bool Not already in the group or a lower group
  208. */
  209. public function set_group( $handle, $recursion, $group = false ) {
  210. if ( $this->registered[$handle]->args === 1 )
  211. $grp = 1;
  212. else
  213. $grp = (int) $this->get_data( $handle, 'group' );
  214. if ( false !== $group && $grp > $group )
  215. $grp = $group;
  216. return parent::set_group( $handle, $recursion, $grp );
  217. }
  218. /**
  219. * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
  220. * @param bool $recursion Internal flag that function is calling itself.
  221. * @param mixed $group Group level: (int) level, (false) no groups.
  222. * @return bool True on success, false on failure.
  223. */
  224. public function all_deps( $handles, $recursion = false, $group = false ) {
  225. $r = parent::all_deps( $handles, $recursion );
  226. if ( ! $recursion ) {
  227. /**
  228. * Filter the list of script dependencies left to print.
  229. *
  230. * @since 2.3.0
  231. *
  232. * @param array $to_do An array of script dependencies.
  233. */
  234. $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
  235. }
  236. return $r;
  237. }
  238. /**
  239. * @return array
  240. */
  241. public function do_head_items() {
  242. $this->do_items(false, 0);
  243. return $this->done;
  244. }
  245. /**
  246. * @return array
  247. */
  248. public function do_footer_items() {
  249. $this->do_items(false, 1);
  250. return $this->done;
  251. }
  252. /**
  253. * @param string $src
  254. * @return bool
  255. */
  256. public function in_default_dir( $src ) {
  257. if ( ! $this->default_dirs ) {
  258. return true;
  259. }
  260. if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) {
  261. return false;
  262. }
  263. foreach ( (array) $this->default_dirs as $test ) {
  264. if ( 0 === strpos( $src, $test ) ) {
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. /**
  271. * @access public
  272. */
  273. public function reset() {
  274. $this->do_concat = false;
  275. $this->print_code = '';
  276. $this->concat = '';
  277. $this->concat_version = '';
  278. $this->print_html = '';
  279. $this->ext_version = '';
  280. $this->ext_handles = '';
  281. }
  282. }