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

https://bitbucket.org/akeda/bmw-id-hris · PHP · 244 lines · 148 code · 42 blank · 54 comment · 31 complexity · 7391b073607f1b2cb943f3f83d02e4f9 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. var $base_url; // Full URL with trailing slash
  20. var $content_url;
  21. var $default_version;
  22. var $in_footer = array();
  23. var $concat = '';
  24. var $concat_version = '';
  25. var $do_concat = false;
  26. var $print_html = '';
  27. var $print_code = '';
  28. var $ext_handles = '';
  29. var $ext_version = '';
  30. var $default_dirs;
  31. function __construct() {
  32. $this->init();
  33. add_action( 'init', array( $this, 'init' ), 0 );
  34. }
  35. function init() {
  36. /**
  37. * Fires when the WP_Scripts instance is initialized.
  38. *
  39. * @since 2.6.0
  40. *
  41. * @param WP_Scripts &$this WP_Scripts instance, passed by reference.
  42. */
  43. do_action_ref_array( 'wp_default_scripts', array(&$this) );
  44. }
  45. /**
  46. * Prints scripts
  47. *
  48. * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
  49. *
  50. * @param mixed $handles (optional) Scripts to be printed. (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
  51. * @param int $group (optional) If scripts were queued in groups prints this group number.
  52. * @return array Scripts that have been printed
  53. */
  54. function print_scripts( $handles = false, $group = false ) {
  55. return $this->do_items( $handles, $group );
  56. }
  57. // Deprecated since 3.3, see print_extra_script()
  58. function print_scripts_l10n( $handle, $echo = true ) {
  59. _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' );
  60. return $this->print_extra_script( $handle, $echo );
  61. }
  62. function print_extra_script( $handle, $echo = true ) {
  63. if ( !$output = $this->get_data( $handle, 'data' ) )
  64. return;
  65. if ( !$echo )
  66. return $output;
  67. echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
  68. echo "/* <![CDATA[ */\n";
  69. echo "$output\n";
  70. echo "/* ]]> */\n";
  71. echo "</script>\n";
  72. return true;
  73. }
  74. function do_item( $handle, $group = false ) {
  75. if ( !parent::do_item($handle) )
  76. return false;
  77. if ( 0 === $group && $this->groups[$handle] > 0 ) {
  78. $this->in_footer[] = $handle;
  79. return false;
  80. }
  81. if ( false === $group && in_array($handle, $this->in_footer, true) )
  82. $this->in_footer = array_diff( $this->in_footer, (array) $handle );
  83. if ( null === $this->registered[$handle]->ver )
  84. $ver = '';
  85. else
  86. $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
  87. if ( isset($this->args[$handle]) )
  88. $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
  89. $src = $this->registered[$handle]->src;
  90. if ( $this->do_concat ) {
  91. /**
  92. * Filter the script loader source.
  93. *
  94. * @since 2.2.0
  95. *
  96. * @param string $src Script loader source path.
  97. * @param string $handle Script handle.
  98. */
  99. $srce = apply_filters( 'script_loader_src', $src, $handle );
  100. if ( $this->in_default_dir($srce) ) {
  101. $this->print_code .= $this->print_extra_script( $handle, false );
  102. $this->concat .= "$handle,";
  103. $this->concat_version .= "$handle$ver";
  104. return true;
  105. } else {
  106. $this->ext_handles .= "$handle,";
  107. $this->ext_version .= "$handle$ver";
  108. }
  109. }
  110. $this->print_extra_script( $handle );
  111. if ( !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
  112. $src = $this->base_url . $src;
  113. }
  114. if ( !empty($ver) )
  115. $src = add_query_arg('ver', $ver, $src);
  116. /** This filter is documented in wp-includes/class.wp-scripts.php */
  117. $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
  118. if ( ! $src )
  119. return true;
  120. if ( $this->do_concat )
  121. $this->print_html .= "<script type='text/javascript' src='$src'></script>\n";
  122. else
  123. echo "<script type='text/javascript' src='$src'></script>\n";
  124. return true;
  125. }
  126. /**
  127. * Localizes a script
  128. *
  129. * Localizes only if the script has already been added
  130. */
  131. function localize( $handle, $object_name, $l10n ) {
  132. if ( $handle === 'jquery' )
  133. $handle = 'jquery-core';
  134. if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
  135. $after = $l10n['l10n_print_after'];
  136. unset($l10n['l10n_print_after']);
  137. }
  138. foreach ( (array) $l10n as $key => $value ) {
  139. if ( !is_scalar($value) )
  140. continue;
  141. $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
  142. }
  143. $script = "var $object_name = " . json_encode($l10n) . ';';
  144. if ( !empty($after) )
  145. $script .= "\n$after;";
  146. $data = $this->get_data( $handle, 'data' );
  147. if ( !empty( $data ) )
  148. $script = "$data\n$script";
  149. return $this->add_data( $handle, 'data', $script );
  150. }
  151. function set_group( $handle, $recursion, $group = false ) {
  152. if ( $this->registered[$handle]->args === 1 )
  153. $grp = 1;
  154. else
  155. $grp = (int) $this->get_data( $handle, 'group' );
  156. if ( false !== $group && $grp > $group )
  157. $grp = $group;
  158. return parent::set_group( $handle, $recursion, $grp );
  159. }
  160. function all_deps( $handles, $recursion = false, $group = false ) {
  161. $r = parent::all_deps( $handles, $recursion );
  162. if ( ! $recursion ) {
  163. /**
  164. * Filter the list of script dependencies left to print.
  165. *
  166. * @since 2.3.0
  167. *
  168. * @param array $to_do An array of script dependencies.
  169. */
  170. $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
  171. }
  172. return $r;
  173. }
  174. function do_head_items() {
  175. $this->do_items(false, 0);
  176. return $this->done;
  177. }
  178. function do_footer_items() {
  179. $this->do_items(false, 1);
  180. return $this->done;
  181. }
  182. function in_default_dir($src) {
  183. if ( ! $this->default_dirs )
  184. return true;
  185. if ( 0 === strpos( $src, '/wp-includes/js/l10n' ) )
  186. return false;
  187. foreach ( (array) $this->default_dirs as $test ) {
  188. if ( 0 === strpos($src, $test) )
  189. return true;
  190. }
  191. return false;
  192. }
  193. function reset() {
  194. $this->do_concat = false;
  195. $this->print_code = '';
  196. $this->concat = '';
  197. $this->concat_version = '';
  198. $this->print_html = '';
  199. $this->ext_version = '';
  200. $this->ext_handles = '';
  201. }
  202. }