WordPress 3.9
[autoinstalls/wordpress.git] / wp-includes / class.wp-styles.php
1 <?php
2 /**
3  * BackPress Styles enqueue.
4  *
5  * These classes were refactored from the WordPress WP_Scripts and WordPress
6  * script enqueue API.
7  *
8  * @package BackPress
9  * @since r74
10  */
11
12 /**
13  * BackPress Styles enqueue class.
14  *
15  * @package BackPress
16  * @uses WP_Dependencies
17  * @since r74
18  */
19 class WP_Styles extends WP_Dependencies {
20         var $base_url;
21         var $content_url;
22         var $default_version;
23         var $text_direction = 'ltr';
24         var $concat = '';
25         var $concat_version = '';
26         var $do_concat = false;
27         var $print_html = '';
28         var $print_code = '';
29         var $default_dirs;
30
31         function __construct() {
32                 /**
33                  * Fires when the WP_Styles instance is initialized.
34                  *
35                  * @since 2.6.0
36                  *
37                  * @param WP_Styles &$this WP_Styles instance, passed by reference.
38                  */
39                 do_action_ref_array( 'wp_default_styles', array(&$this) );
40         }
41
42         function do_item( $handle ) {
43                 if ( !parent::do_item($handle) )
44                         return false;
45
46                 $obj = $this->registered[$handle];
47                 if ( null === $obj->ver )
48                         $ver = '';
49                 else
50                         $ver = $obj->ver ? $obj->ver : $this->default_version;
51
52                 if ( isset($this->args[$handle]) )
53                         $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
54
55                 if ( $this->do_concat ) {
56                         if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) {
57                                 $this->concat .= "$handle,";
58                                 $this->concat_version .= "$handle$ver";
59
60                                 $this->print_code .= $this->print_inline_style( $handle, false );
61
62                                 return true;
63                         }
64                 }
65
66                 if ( isset($obj->args) )
67                         $media = esc_attr( $obj->args );
68                 else
69                         $media = 'all';
70
71                 $href = $this->_css_href( $obj->src, $ver, $handle );
72                 if ( empty( $href ) ) {
73                         // Turns out there is nothing to print.
74                         return true;
75                 }
76                 $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
77                 $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
78
79                 /**
80                  * Filter the HTML link tag of an enqueued style.
81                  *
82                  * @since 2.6.0
83                  *
84                  * @param string         The link tag for the enqueued style.
85                  * @param string $handle The style's registered handle.
86                  */
87                 $tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle );
88                 if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
89                         if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
90                                 $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
91                                 $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
92                         } else {
93                                 $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
94                         }
95
96                         /**
97                          * Filter the right-to-left (RTL) HTML link tag of an enqueued style.
98                          *
99                          * @since 2.6.0
100                          *
101                          * @param string $rtl_style The right to left link tag for the enqueued style.
102                          * @param string $handle    The style's registered handle.
103                          */
104                         $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 );
105
106                         if ( $obj->extra['rtl'] === 'replace' ) {
107                                 $tag = $rtl_tag;
108                         } else {
109                                 $tag .= $rtl_tag;
110                         }
111                 }
112
113                 if ( isset($obj->extra['conditional']) && $obj->extra['conditional'] ) {
114                         $tag = "<!--[if {$obj->extra['conditional']}]>\n" . $tag . "<![endif]-->\n";
115                 }
116
117                 if ( $this->do_concat ) {
118                         $this->print_html .= $tag;
119                         if ( $inline_style = $this->print_inline_style( $handle, false ) )
120                                 $this->print_html .= sprintf( "<style type='text/css'>\n%s\n</style>\n", $inline_style );
121                 } else {
122                         echo $tag;
123                         $this->print_inline_style( $handle );
124                 }
125
126                 return true;
127         }
128
129         function add_inline_style( $handle, $code ) {
130                 if ( !$code )
131                         return false;
132
133                 $after = $this->get_data( $handle, 'after' );
134                 if ( !$after )
135                         $after = array();
136
137                 $after[] = $code;
138
139                 return $this->add_data( $handle, 'after', $after );
140         }
141
142         function print_inline_style( $handle, $echo = true ) {
143                 $output = $this->get_data( $handle, 'after' );
144
145                 if ( empty( $output ) )
146                         return false;
147
148                 $output = implode( "\n", $output );
149
150                 if ( !$echo )
151                         return $output;
152
153                 echo "<style type='text/css'>\n";
154                 echo "$output\n";
155                 echo "</style>\n";
156
157                 return true;
158         }
159
160         function all_deps( $handles, $recursion = false, $group = false ) {
161                 $r = parent::all_deps( $handles, $recursion );
162                 if ( !$recursion ) {
163                         /**
164                          * Filter the array of enqueued styles before processing for output.
165                          *
166                          * @since 2.6.0
167                          *
168                          * @param array $to_do The list of enqueued styles about to be processed.
169                          */
170                         $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
171                 }
172                 return $r;
173         }
174
175         function _css_href( $src, $ver, $handle ) {
176                 if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
177                         $src = $this->base_url . $src;
178                 }
179
180                 if ( !empty($ver) )
181                         $src = add_query_arg('ver', $ver, $src);
182
183                 /**
184                  * Filter an enqueued style's fully-qualified URL.
185                  *
186                  * @since 2.6.0
187                  *
188                  * @param string $src    The source URL of the enqueued style.
189                  * @param string $handle The style's registered handle.
190                  */
191                 $src = apply_filters( 'style_loader_src', $src, $handle );
192                 return esc_url( $src );
193         }
194
195         function in_default_dir($src) {
196                 if ( ! $this->default_dirs )
197                         return true;
198
199                 foreach ( (array) $this->default_dirs as $test ) {
200                         if ( 0 === strpos($src, $test) )
201                                 return true;
202                 }
203                 return false;
204         }
205
206         function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
207                 $this->do_items(false, 1);
208                 return $this->done;
209         }
210
211         function reset() {
212                 $this->do_concat = false;
213                 $this->concat = '';
214                 $this->concat_version = '';
215                 $this->print_html = '';
216         }
217 }