class.wp-styles.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * Dependencies API: WP_Styles class
  4. *
  5. * @since 2.6.0
  6. *
  7. * @package WordPress
  8. * @subpackage Dependencies
  9. */
  10. /**
  11. * Core class used to register styles.
  12. *
  13. * @since 2.6.0
  14. *
  15. * @see WP_Dependencies
  16. */
  17. class WP_Styles extends WP_Dependencies {
  18. /**
  19. * Base URL for styles.
  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. * The current text direction.
  43. *
  44. * @since 2.6.0
  45. * @var string
  46. */
  47. public $text_direction = 'ltr';
  48. /**
  49. * Holds a list of style 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 style 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 styles 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 styles if concatenation is enabled.
  80. *
  81. * @since 3.3.0
  82. * @var string
  83. */
  84. public $print_code = '';
  85. /**
  86. * List of default directories.
  87. *
  88. * @since 2.8.0
  89. * @var array
  90. */
  91. public $default_dirs;
  92. /**
  93. * Constructor.
  94. *
  95. * @since 2.6.0
  96. */
  97. public function __construct() {
  98. /**
  99. * Fires when the WP_Styles instance is initialized.
  100. *
  101. * @since 2.6.0
  102. *
  103. * @param WP_Styles $this WP_Styles instance (passed by reference).
  104. */
  105. do_action_ref_array( 'wp_default_styles', array(&$this) );
  106. }
  107. /**
  108. * Processes a style dependency.
  109. *
  110. * @since 2.6.0
  111. *
  112. * @see WP_Dependencies::do_item()
  113. *
  114. * @param string $handle The style's registered handle.
  115. * @return bool True on success, false on failure.
  116. */
  117. public function do_item( $handle ) {
  118. if ( !parent::do_item($handle) )
  119. return false;
  120. $obj = $this->registered[$handle];
  121. if ( null === $obj->ver )
  122. $ver = '';
  123. else
  124. $ver = $obj->ver ? $obj->ver : $this->default_version;
  125. if ( isset($this->args[$handle]) )
  126. $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
  127. if ( $this->do_concat ) {
  128. if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) {
  129. $this->concat .= "$handle,";
  130. $this->concat_version .= "$handle$ver";
  131. $this->print_code .= $this->print_inline_style( $handle, false );
  132. return true;
  133. }
  134. }
  135. if ( isset($obj->args) )
  136. $media = esc_attr( $obj->args );
  137. else
  138. $media = 'all';
  139. // A single item may alias a set of items, by having dependencies, but no source.
  140. if ( ! $obj->src ) {
  141. if ( $inline_style = $this->print_inline_style( $handle, false ) ) {
  142. $inline_style = sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style );
  143. if ( $this->do_concat ) {
  144. $this->print_html .= $inline_style;
  145. } else {
  146. echo $inline_style;
  147. }
  148. }
  149. return true;
  150. }
  151. $href = $this->_css_href( $obj->src, $ver, $handle );
  152. if ( ! $href ) {
  153. return true;
  154. }
  155. $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
  156. $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
  157. /**
  158. * Filters the HTML link tag of an enqueued style.
  159. *
  160. * @since 2.6.0
  161. * @since 4.3.0 Introduced the `$href` parameter.
  162. * @since 4.5.0 Introduced the `$media` parameter.
  163. *
  164. * @param string $html The link tag for the enqueued style.
  165. * @param string $handle The style's registered handle.
  166. * @param string $href The stylesheet's source URL.
  167. * @param string $media The stylesheet's media attribute.
  168. */
  169. $tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle, $href, $media);
  170. if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
  171. if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
  172. $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
  173. $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
  174. } else {
  175. $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
  176. }
  177. /** This filter is documented in wp-includes/class.wp-styles.php */
  178. $rtl_tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-rtl-css' $title href='$rtl_href' type='text/css' media='$media' />\n", $handle, $rtl_href, $media );
  179. if ( $obj->extra['rtl'] === 'replace' ) {
  180. $tag = $rtl_tag;
  181. } else {
  182. $tag .= $rtl_tag;
  183. }
  184. }
  185. $conditional_pre = $conditional_post = '';
  186. if ( isset( $obj->extra['conditional'] ) && $obj->extra['conditional'] ) {
  187. $conditional_pre = "<!--[if {$obj->extra['conditional']}]>\n";
  188. $conditional_post = "<![endif]-->\n";
  189. }
  190. if ( $this->do_concat ) {
  191. $this->print_html .= $conditional_pre;
  192. $this->print_html .= $tag;
  193. if ( $inline_style = $this->print_inline_style( $handle, false ) ) {
  194. $this->print_html .= sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style );
  195. }
  196. $this->print_html .= $conditional_post;
  197. } else {
  198. echo $conditional_pre;
  199. echo $tag;
  200. $this->print_inline_style( $handle );
  201. echo $conditional_post;
  202. }
  203. return true;
  204. }
  205. /**
  206. * Adds extra CSS styles to a registered stylesheet.
  207. *
  208. * @since 3.3.0
  209. *
  210. * @param string $handle The style's registered handle.
  211. * @param string $code String containing the CSS styles to be added.
  212. * @return bool True on success, false on failure.
  213. */
  214. public function add_inline_style( $handle, $code ) {
  215. if ( ! $code ) {
  216. return false;
  217. }
  218. $after = $this->get_data( $handle, 'after' );
  219. if ( ! $after ) {
  220. $after = array();
  221. }
  222. $after[] = $code;
  223. return $this->add_data( $handle, 'after', $after );
  224. }
  225. /**
  226. * Prints extra CSS styles of a registered stylesheet.
  227. *
  228. * @since 3.3.0
  229. *
  230. * @param string $handle The style's registered handle.
  231. * @param bool $echo Optional. Whether to echo the inline style instead of just returning it.
  232. * Default true.
  233. * @return string|bool False if no data exists, inline styles if `$echo` is true, true otherwise.
  234. */
  235. public function print_inline_style( $handle, $echo = true ) {
  236. $output = $this->get_data( $handle, 'after' );
  237. if ( empty( $output ) ) {
  238. return false;
  239. }
  240. $output = implode( "\n", $output );
  241. if ( ! $echo ) {
  242. return $output;
  243. }
  244. printf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $output );
  245. return true;
  246. }
  247. /**
  248. * Determines style dependencies.
  249. *
  250. * @since 2.6.0
  251. *
  252. * @see WP_Dependencies::all_deps()
  253. *
  254. * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
  255. * @param bool $recursion Internal flag that function is calling itself.
  256. * @param int|false $group Group level: (int) level, (false) no groups.
  257. * @return bool True on success, false on failure.
  258. */
  259. public function all_deps( $handles, $recursion = false, $group = false ) {
  260. $r = parent::all_deps( $handles, $recursion, $group );
  261. if ( ! $recursion ) {
  262. /**
  263. * Filters the array of enqueued styles before processing for output.
  264. *
  265. * @since 2.6.0
  266. *
  267. * @param array $to_do The list of enqueued styles about to be processed.
  268. */
  269. $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
  270. }
  271. return $r;
  272. }
  273. /**
  274. * Generates an enqueued style's fully-qualified URL.
  275. *
  276. * @since 2.6.0
  277. *
  278. * @param string $src The source of the enqueued style.
  279. * @param string $ver The version of the enqueued style.
  280. * @param string $handle The style's registered handle.
  281. * @return string Style's fully-qualified URL.
  282. */
  283. public function _css_href( $src, $ver, $handle ) {
  284. if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
  285. $src = $this->base_url . $src;
  286. }
  287. if ( !empty($ver) )
  288. $src = add_query_arg('ver', $ver, $src);
  289. /**
  290. * Filters an enqueued style's fully-qualified URL.
  291. *
  292. * @since 2.6.0
  293. *
  294. * @param string $src The source URL of the enqueued style.
  295. * @param string $handle The style's registered handle.
  296. */
  297. $src = apply_filters( 'style_loader_src', $src, $handle );
  298. return esc_url( $src );
  299. }
  300. /**
  301. * Whether a handle's source is in a default directory.
  302. *
  303. * @since 2.8.0
  304. *
  305. * @param string $src The source of the enqueued style.
  306. * @return bool True if found, false if not.
  307. */
  308. public function in_default_dir( $src ) {
  309. if ( ! $this->default_dirs )
  310. return true;
  311. foreach ( (array) $this->default_dirs as $test ) {
  312. if ( 0 === strpos($src, $test) )
  313. return true;
  314. }
  315. return false;
  316. }
  317. /**
  318. * Processes items and dependencies for the footer group.
  319. *
  320. * HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
  321. *
  322. * @since 3.3.0
  323. *
  324. * @see WP_Dependencies::do_items()
  325. *
  326. * @return array Handles of items that have been processed.
  327. */
  328. public function do_footer_items() {
  329. $this->do_items(false, 1);
  330. return $this->done;
  331. }
  332. /**
  333. * Resets class properties.
  334. *
  335. * @since 3.3.0
  336. */
  337. public function reset() {
  338. $this->do_concat = false;
  339. $this->concat = '';
  340. $this->concat_version = '';
  341. $this->print_html = '';
  342. }
  343. }