3 * BackPress Styles enqueue.
5 * These classes were refactored from the WordPress WP_Scripts and WordPress
13 * BackPress Styles enqueue class.
16 * @uses WP_Dependencies
19 class WP_Styles extends WP_Dependencies {
22 public $default_version;
23 public $text_direction = 'ltr';
25 public $concat_version = '';
26 public $do_concat = false;
27 public $print_html = '';
28 public $print_code = '';
31 public function __construct() {
33 * Fires when the WP_Styles instance is initialized.
37 * @param WP_Styles &$this WP_Styles instance, passed by reference.
39 do_action_ref_array( 'wp_default_styles', array(&$this) );
43 * @param string $handle
46 public function do_item( $handle ) {
47 if ( !parent::do_item($handle) )
50 $obj = $this->registered[$handle];
51 if ( null === $obj->ver )
54 $ver = $obj->ver ? $obj->ver : $this->default_version;
56 if ( isset($this->args[$handle]) )
57 $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle];
59 if ( $this->do_concat ) {
60 if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) {
61 $this->concat .= "$handle,";
62 $this->concat_version .= "$handle$ver";
64 $this->print_code .= $this->print_inline_style( $handle, false );
70 if ( isset($obj->args) )
71 $media = esc_attr( $obj->args );
75 $href = $this->_css_href( $obj->src, $ver, $handle );
76 if ( empty( $href ) ) {
77 // Turns out there is nothing to print.
80 $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
81 $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
84 * Filter the HTML link tag of an enqueued style.
88 * @param string The link tag for the enqueued style.
89 * @param string $handle The style's registered handle.
91 $tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle );
92 if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
93 if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
94 $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
95 $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
97 $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
100 /** This filter is documented in wp-includes/class.wp-styles.php */
101 $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 );
103 if ( $obj->extra['rtl'] === 'replace' ) {
110 $conditional_pre = $conditional_post = '';
111 if ( isset( $obj->extra['conditional'] ) && $obj->extra['conditional'] ) {
112 $conditional_pre = "<!--[if {$obj->extra['conditional']}]>\n";
113 $conditional_post = "<![endif]-->\n";
116 if ( $this->do_concat ) {
117 $this->print_html .= $conditional_pre;
118 $this->print_html .= $tag;
119 if ( $inline_style = $this->print_inline_style( $handle, false ) ) {
120 $this->print_html .= sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style );
122 $this->print_html .= $conditional_post;
124 echo $conditional_pre;
126 $this->print_inline_style( $handle );
127 echo $conditional_post;
134 * @param string $handle
135 * @param string $code
137 public function add_inline_style( $handle, $code ) {
142 $after = $this->get_data( $handle, 'after' );
149 return $this->add_data( $handle, 'after', $after );
153 * @param string $handle
157 public function print_inline_style( $handle, $echo = true ) {
158 $output = $this->get_data( $handle, 'after' );
160 if ( empty( $output ) ) {
164 $output = implode( "\n", $output );
170 printf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $output );
176 * @param mixed $handles
177 * @param bool $recursion
178 * @param mixed $group
181 public function all_deps( $handles, $recursion = false, $group = false ) {
182 $r = parent::all_deps( $handles, $recursion );
185 * Filter the array of enqueued styles before processing for output.
189 * @param array $to_do The list of enqueued styles about to be processed.
191 $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
199 * @param string $handle
202 public function _css_href( $src, $ver, $handle ) {
203 if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
204 $src = $this->base_url . $src;
208 $src = add_query_arg('ver', $ver, $src);
211 * Filter an enqueued style's fully-qualified URL.
215 * @param string $src The source URL of the enqueued style.
216 * @param string $handle The style's registered handle.
218 $src = apply_filters( 'style_loader_src', $src, $handle );
219 return esc_url( $src );
226 public function in_default_dir($src) {
227 if ( ! $this->default_dirs )
230 foreach ( (array) $this->default_dirs as $test ) {
231 if ( 0 === strpos($src, $test) )
237 public function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
238 $this->do_items(false, 1);
242 public function reset() {
243 $this->do_concat = false;
245 $this->concat_version = '';
246 $this->print_html = '';