WP_Styles

Core class used to register styles.

Defined (1)

The class is defined in the following location(s).

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