3 * Dependencies API: WP_Styles class
8 * @subpackage Dependencies
12 * Core class used to register styles.
15 * @uses WP_Dependencies
18 class WP_Styles extends WP_Dependencies {
20 * Base URL for styles.
22 * Full URL with trailing slash.
31 * URL of the content directory.
40 * Default version string for stylesheets.
46 public $default_version;
49 * The current text direction.
55 public $text_direction = 'ltr';
58 * Holds a list of style handles which will be concatenated.
67 * Holds a string which contains style handles and their version.
74 public $concat_version = '';
77 * Whether to perform concatenation.
83 public $do_concat = false;
86 * Holds HTML markup of styles and additional data if concatenation
93 public $print_html = '';
96 * Holds inline styles if concatenation is enabled.
102 public $print_code = '';
105 * List of default directories.
111 public $default_dirs;
119 public function __construct() {
121 * Fires when the WP_Styles instance is initialized.
125 * @param WP_Styles &$this WP_Styles instance, passed by reference.
127 do_action_ref_array( 'wp_default_styles', array(&$this) );
131 * Processes a style dependency.
136 * @see WP_Dependencies::do_item()
138 * @param string $handle The style's registered handle.
139 * @return bool True on success, false on failure.
141 public function do_item( $handle ) {
142 if ( !parent::do_item($handle) )
145 $obj = $this->registered[$handle];
146 if ( null === $obj->ver )
149 $ver = $obj->ver ? $obj->ver : $this->default_version;
151 if ( isset($this->args[$handle]) )
152 $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle];
154 if ( $this->do_concat ) {
155 if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) {
156 $this->concat .= "$handle,";
157 $this->concat_version .= "$handle$ver";
159 $this->print_code .= $this->print_inline_style( $handle, false );
165 if ( isset($obj->args) )
166 $media = esc_attr( $obj->args );
170 // A single item may alias a set of items, by having dependencies, but no source.
172 if ( $inline_style = $this->print_inline_style( $handle, false ) ) {
173 $inline_style = sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style );
174 if ( $this->do_concat ) {
175 $this->print_html .= $inline_style;
183 $href = $this->_css_href( $obj->src, $ver, $handle );
188 $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
189 $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
192 * Filter the HTML link tag of an enqueued style.
195 * @since 4.3.0 Introduced the `$href` parameter.
196 * @since 4.5.0 Introduced the `$media` parameter.
198 * @param string $html The link tag for the enqueued style.
199 * @param string $handle The style's registered handle.
200 * @param string $href The stylesheet's source URL.
201 * @param string $media The stylesheet's media attribute.
203 $tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle, $href, $media);
204 if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
205 if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
206 $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
207 $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
209 $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
212 /** This filter is documented in wp-includes/class.wp-styles.php */
213 $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 );
215 if ( $obj->extra['rtl'] === 'replace' ) {
222 $conditional_pre = $conditional_post = '';
223 if ( isset( $obj->extra['conditional'] ) && $obj->extra['conditional'] ) {
224 $conditional_pre = "<!--[if {$obj->extra['conditional']}]>\n";
225 $conditional_post = "<![endif]-->\n";
228 if ( $this->do_concat ) {
229 $this->print_html .= $conditional_pre;
230 $this->print_html .= $tag;
231 if ( $inline_style = $this->print_inline_style( $handle, false ) ) {
232 $this->print_html .= sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style );
234 $this->print_html .= $conditional_post;
236 echo $conditional_pre;
238 $this->print_inline_style( $handle );
239 echo $conditional_post;
246 * Adds extra CSS styles to a registered stylesheet.
251 * @param string $handle The style's registered handle.
252 * @param string $code String containing the CSS styles to be added.
253 * @return bool True on success, false on failure.
255 public function add_inline_style( $handle, $code ) {
260 $after = $this->get_data( $handle, 'after' );
267 return $this->add_data( $handle, 'after', $after );
271 * Prints extra CSS styles of a registered stylesheet.
276 * @param string $handle The style's registered handle.
277 * @param bool $echo Optional. Whether to echo the inline style instead of just returning it.
279 * @return string|bool False if no data exists, inline styles if `$echo` is true, true otherwise.
281 public function print_inline_style( $handle, $echo = true ) {
282 $output = $this->get_data( $handle, 'after' );
284 if ( empty( $output ) ) {
288 $output = implode( "\n", $output );
294 printf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $output );
300 * Determines style dependencies.
305 * @see WP_Dependencies::all_deps()
307 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
308 * @param bool $recursion Internal flag that function is calling itself.
309 * @param int|false $group Group level: (int) level, (false) no groups.
310 * @return bool True on success, false on failure.
312 public function all_deps( $handles, $recursion = false, $group = false ) {
313 $r = parent::all_deps( $handles, $recursion, $group );
314 if ( ! $recursion ) {
316 * Filter the array of enqueued styles before processing for output.
320 * @param array $to_do The list of enqueued styles about to be processed.
322 $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
328 * Generates an enqueued style's fully-qualified URL.
333 * @param string $src The source of the enqueued style.
334 * @param string $ver The version of the enqueued style.
335 * @param string $handle The style's registered handle.
336 * @return string Style's fully-qualified URL.
338 public function _css_href( $src, $ver, $handle ) {
339 if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
340 $src = $this->base_url . $src;
344 $src = add_query_arg('ver', $ver, $src);
347 * Filter an enqueued style's fully-qualified URL.
351 * @param string $src The source URL of the enqueued style.
352 * @param string $handle The style's registered handle.
354 $src = apply_filters( 'style_loader_src', $src, $handle );
355 return esc_url( $src );
359 * Whether a handle's source is in a default directory.
364 * @param string $src The source of the enqueued style.
365 * @return bool True if found, false if not.
367 public function in_default_dir( $src ) {
368 if ( ! $this->default_dirs )
371 foreach ( (array) $this->default_dirs as $test ) {
372 if ( 0 === strpos($src, $test) )
379 * Processes items and dependencies for the footer group.
381 * HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
386 * @see WP_Dependencies::do_items()
388 * @return array Handles of items that have been processed.
390 public function do_footer_items() {
391 $this->do_items(false, 1);
396 * Resets class properties.
401 public function reset() {
402 $this->do_concat = false;
404 $this->concat_version = '';
405 $this->print_html = '';