/wp-includes/class.wp-styles.php

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