WordPress 4.3
[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         public $base_url;
21         public $content_url;
22         public $default_version;
23         public $text_direction = 'ltr';
24         public $concat = '';
25         public $concat_version = '';
26         public $do_concat = false;
27         public $print_html = '';
28         public $print_code = '';
29         public $default_dirs;
30
31         public 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         /**
43          * @param string $handle
44          * @return bool
45          */
46         public function do_item( $handle ) {
47                 if ( !parent::do_item($handle) )
48                         return false;
49
50                 $obj = $this->registered[$handle];
51                 if ( null === $obj->ver )
52                         $ver = '';
53                 else
54                         $ver = $obj->ver ? $obj->ver : $this->default_version;
55
56                 if ( isset($this->args[$handle]) )
57                         $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
58
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";
63
64                                 $this->print_code .= $this->print_inline_style( $handle, false );
65
66                                 return true;
67                         }
68                 }
69
70                 if ( isset($obj->args) )
71                         $media = esc_attr( $obj->args );
72                 else
73                         $media = 'all';
74
75                 $href = $this->_css_href( $obj->src, $ver, $handle );
76                 if ( empty( $href ) ) {
77                         // Turns out there is nothing to print.
78                         return true;
79                 }
80                 $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
81                 $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
82
83                 /**
84                  * Filter the HTML link tag of an enqueued style.
85                  *
86                  * @since 2.6.0
87                  * @since 4.3.0 Introduced the `$href` parameter.
88                  *
89                  * @param string $html   The link tag for the enqueued style.
90                  * @param string $handle The style's registered handle.
91                  * @param string $href   The stylesheet's source URL.
92                  */
93                 $tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle, $href );
94                 if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
95                         if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
96                                 $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
97                                 $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
98                         } else {
99                                 $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
100                         }
101
102                         /** This filter is documented in wp-includes/class.wp-styles.php */
103                         $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 );
104
105                         if ( $obj->extra['rtl'] === 'replace' ) {
106                                 $tag = $rtl_tag;
107                         } else {
108                                 $tag .= $rtl_tag;
109                         }
110                 }
111
112                 $conditional_pre = $conditional_post = '';
113                 if ( isset( $obj->extra['conditional'] ) && $obj->extra['conditional'] ) {
114                         $conditional_pre  = "<!--[if {$obj->extra['conditional']}]>\n";
115                         $conditional_post = "<![endif]-->\n";
116                 }
117
118                 if ( $this->do_concat ) {
119                         $this->print_html .= $conditional_pre;
120                         $this->print_html .= $tag;
121                         if ( $inline_style = $this->print_inline_style( $handle, false ) ) {
122                                 $this->print_html .= sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style );
123                         }
124                         $this->print_html .= $conditional_post;
125                 } else {
126                         echo $conditional_pre;
127                         echo $tag;
128                         $this->print_inline_style( $handle );
129                         echo $conditional_post;
130                 }
131
132                 return true;
133         }
134
135         /**
136          * @param string $handle
137          * @param string $code
138          */
139         public function add_inline_style( $handle, $code ) {
140                 if ( ! $code ) {
141                         return false;
142                 }
143
144                 $after = $this->get_data( $handle, 'after' );
145                 if ( ! $after ) {
146                         $after = array();
147                 }
148
149                 $after[] = $code;
150
151                 return $this->add_data( $handle, 'after', $after );
152         }
153
154         /**
155          * @param string $handle
156          * @param bool $echo
157          * @return bool
158          */
159         public function print_inline_style( $handle, $echo = true ) {
160                 $output = $this->get_data( $handle, 'after' );
161
162                 if ( empty( $output ) ) {
163                         return false;
164                 }
165
166                 $output = implode( "\n", $output );
167
168                 if ( ! $echo ) {
169                         return $output;
170                 }
171
172                 printf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $output );
173
174                 return true;
175         }
176
177         /**
178          * @param mixed $handles
179          * @param bool $recursion
180          * @param mixed $group
181          * @return bool
182          */
183         public function all_deps( $handles, $recursion = false, $group = false ) {
184                 $r = parent::all_deps( $handles, $recursion );
185                 if ( !$recursion ) {
186                         /**
187                          * Filter the array of enqueued styles before processing for output.
188                          *
189                          * @since 2.6.0
190                          *
191                          * @param array $to_do The list of enqueued styles about to be processed.
192                          */
193                         $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
194                 }
195                 return $r;
196         }
197
198         /**
199          * @param string $src
200          * @param string $ver
201          * @param string $handle
202          * @return string
203          */
204         public function _css_href( $src, $ver, $handle ) {
205                 if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
206                         $src = $this->base_url . $src;
207                 }
208
209                 if ( !empty($ver) )
210                         $src = add_query_arg('ver', $ver, $src);
211
212                 /**
213                  * Filter an enqueued style's fully-qualified URL.
214                  *
215                  * @since 2.6.0
216                  *
217                  * @param string $src    The source URL of the enqueued style.
218                  * @param string $handle The style's registered handle.
219                  */
220                 $src = apply_filters( 'style_loader_src', $src, $handle );
221                 return esc_url( $src );
222         }
223
224         /**
225          * @param string $src
226          * @return bool
227          */
228         public function in_default_dir($src) {
229                 if ( ! $this->default_dirs )
230                         return true;
231
232                 foreach ( (array) $this->default_dirs as $test ) {
233                         if ( 0 === strpos($src, $test) )
234                                 return true;
235                 }
236                 return false;
237         }
238
239         /**
240          * @return array
241          */
242         public function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
243                 $this->do_items(false, 1);
244                 return $this->done;
245         }
246
247         /**
248          * @access public
249          */
250         public function reset() {
251                 $this->do_concat = false;
252                 $this->concat = '';
253                 $this->concat_version = '';
254                 $this->print_html = '';
255         }
256 }